Verify control plane / verify (push) Canceled after 0s
Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/12
125 lines
4.5 KiB
Java
125 lines
4.5 KiB
Java
package run.webup.platform;
|
|
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.constraints.*;
|
|
import java.math.BigDecimal;
|
|
import java.util.*;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/tasks")
|
|
public class TaskController {
|
|
private final TaskService tasks;
|
|
private final JdbcTemplate db;
|
|
private final ExecutionAvailability execution;
|
|
|
|
public TaskController(TaskService tasks, JdbcTemplate db, ExecutionAvailability execution) {
|
|
this.tasks = tasks;
|
|
this.db = db;
|
|
this.execution = execution;
|
|
}
|
|
|
|
@GetMapping("/execution-status")
|
|
public Object executionStatus() {
|
|
return execution.status();
|
|
}
|
|
|
|
public record Create(
|
|
@NotNull UUID projectId,
|
|
@NotNull UUID modelId,
|
|
@NotBlank @Size(max = 10000) String prompt,
|
|
@NotNull @DecimalMin("0.00000001") @Digits(integer = 12, fraction = 8) BigDecimal budget,
|
|
@NotBlank @Size(max = 200) String idempotencyKey) {}
|
|
|
|
@GetMapping
|
|
public List<Map<String, Object>> list(@AuthenticationPrincipal SecurityConfig.Identity who) {
|
|
return db.queryForList(
|
|
"SELECT t.id,t.project_id,p.name AS"
|
|
+ " project_name,t.model_id,t.prompt,t.status,t.requested_action,t.step_count,t.error,t.created_at,t.admin_priority,t.run_next,t.paid_priority,t.started_at,r.amount"
|
|
+ " AS budget,r.spent FROM ai_tasks t JOIN projects p ON p.id=t.project_id JOIN"
|
|
+ " wallet_reservations r ON r.id=t.reservation_id WHERE t.user_id=? ORDER BY"
|
|
+ " t.created_at DESC LIMIT 100",
|
|
who.id());
|
|
}
|
|
|
|
@PostMapping
|
|
public Map<String, Object> create(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @Valid @RequestBody Create c) {
|
|
if (db.queryForObject(
|
|
"SELECT COUNT(*) FROM ai_tasks WHERE user_id=? AND idempotency_key=?",
|
|
Integer.class,
|
|
who.id(),
|
|
c.idempotencyKey())
|
|
== 0) execution.requireReady();
|
|
return tasks.create(
|
|
who.id(), c.projectId(), c.modelId(), c.prompt(), c.budget(), c.idempotencyKey());
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public Map<String, Object> find(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID id) {
|
|
return tasks.find(id, who.id());
|
|
}
|
|
|
|
@GetMapping("/{id}/events")
|
|
public List<Map<String, Object>> events(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID id) {
|
|
tasks.find(id, who.id());
|
|
return db.queryForList(
|
|
"SELECT id,task_id,event,created_at,CASE WHEN event='CONTEXT_COMPACTED' THEN"
|
|
+ " '压缩上下文中' ELSE detail END AS detail FROM (SELECT * FROM ai_task_steps WHERE"
|
|
+ " task_id=? AND event NOT IN"
|
|
+ " ('AUTO_SETTLEMENT','MANUAL_SETTLEMENT') ORDER BY created_at DESC,id DESC LIMIT"
|
|
+ " 1000) AS recent ORDER BY created_at,id",
|
|
id);
|
|
}
|
|
|
|
@GetMapping("/{id}/messages")
|
|
public Object messages(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID id) {
|
|
tasks.find(id, who.id());
|
|
return tasks.history(id);
|
|
}
|
|
|
|
@GetMapping("/{id}/changes")
|
|
public Map<String, Object> changes(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID id,
|
|
@RequestParam(defaultValue = "0") @Min(0) @Max(1000000) int page) {
|
|
var task = tasks.find(id, who.id());
|
|
var totals =
|
|
db.queryForMap(
|
|
"SELECT COUNT(*) AS file_count,COALESCE(SUM(added_lines),0) AS added_lines,"
|
|
+ " COALESCE(SUM(removed_lines),0) AS removed_lines"
|
|
+ " FROM ai_task_file_changes WHERE task_id=?",
|
|
id);
|
|
var files =
|
|
db.queryForList(
|
|
"SELECT file_path,last_status,added_lines,removed_lines,binary_change FROM"
|
|
+ " ai_task_file_changes WHERE task_id=? ORDER BY file_path LIMIT 100 OFFSET ?",
|
|
id,
|
|
page * 100);
|
|
return Map.of(
|
|
"checks",
|
|
task.get("file_change_checks"),
|
|
"partial",
|
|
task.get("file_changes_partial"),
|
|
"totals",
|
|
totals,
|
|
"files",
|
|
files);
|
|
}
|
|
|
|
@PostMapping("/{id}/{action:pause|resume|cancel}")
|
|
public Map<String, Object> action(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID id,
|
|
@PathVariable String action) {
|
|
tasks.find(id, who.id());
|
|
if (action.equals("resume")) execution.requireReady();
|
|
return tasks.action(id, who.id(), action);
|
|
}
|
|
}
|