This commit is contained in:
commit
db82325ed7
216 files changed
+33718
No files matched your search
@@ -0,0 +1,92 @@
|
||||
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.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 * FROM ai_task_steps WHERE task_id=? AND event NOT IN"
|
||||
+ " ('AUTO_SETTLEMENT','MANUAL_SETTLEMENT') ORDER BY created_at,id LIMIT 1000",
|
||||
id);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/messages")
|
||||
public Object messages(
|
||||
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID id) {
|
||||
tasks.find(id, who.id());
|
||||
return tasks.history(id);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user