Automations workspace phase 1: sidebar groups, detail view, recompile-to-edit, named runs
Promotes Automations to a top-level sidebar group (below Bookmarks) with user-defined AgentGroup folders (migration 025, FK SET NULL on delete). The /automations page becomes master-detail: overview (composer + grouped list + named global runs) and per-task detail (original prompt, group assignment, run-now/pause/delete, per-task run timeline with true/false/ error pills). Editing recompiles through /api/agent/compile and confirms the proposal before a versioned PATCH applies spec+prompt — the model never writes. Tasks store prompt+groupId; projections add runCount/ errorCount; runs carry taskName. The engine is untouched: groups/prompt are metadata, the validated spec remains the only thing it executes. Chaining, refinements and dry-run stay phase 2. Verified: full DB integration suite under -race, db-converge + migration atomicity, web lint/SSR/build, and a rewritten browser regression covering the sidebar folder, detail prompt, per-task runs, recompile-to-edit and named global runs.
This commit is contained in:
+137
-3
@@ -76,6 +76,8 @@ func agentTaskProjection(row object) object {
|
||||
return object{
|
||||
"id": row["id"], "name": row["name"], "spec": spec,
|
||||
"summary": agent.Summary(spec),
|
||||
"prompt": row["prompt"], "groupId": row["groupId"],
|
||||
"runCount": row["runCount"], "errorCount": row["errorCount"],
|
||||
"enabled": row["enabled"], "status": row["status"],
|
||||
"fireCount": row["fireCount"], "lastCheckedAt": row["lastCheckedAt"],
|
||||
"lastFiredAt": row["lastFiredAt"], "version": row["version"],
|
||||
@@ -83,6 +85,26 @@ func agentTaskProjection(row object) object {
|
||||
}
|
||||
}
|
||||
|
||||
// agentGroupRef validates an optional groupId against the caller's own
|
||||
// groups; "" / null clears it (ungrouped).
|
||||
func agentGroupRef(r *http.Request, v any) (any, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
return nil, bad("groupId must be a string or null")
|
||||
}
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if _, e := owned(r, database(r), "AgentGroup", s, "ownerId"); e != nil {
|
||||
return nil, bad("Group not found")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// tenantAITaskLimits resolves the caller's plan cap on stored automations.
|
||||
func tenantAITaskLimits(r *http.Request) (int64, error) {
|
||||
var tenantID *string
|
||||
@@ -100,7 +122,10 @@ func tenantAITaskLimits(r *http.Request) (int64, error) {
|
||||
|
||||
func HandleAgentTasksList(w http.ResponseWriter, r *http.Request) {
|
||||
rows, e := db.Many(r.Context(), database(r),
|
||||
`SELECT to_jsonb(t) FROM "AgentTask" t WHERE "ownerId"=$1 ORDER BY "createdAt" DESC`, user(r))
|
||||
`SELECT to_jsonb(t)||jsonb_build_object(
|
||||
'runCount',(SELECT count(*) FROM "AgentRun" r WHERE r."taskId"=t.id),
|
||||
'errorCount',(SELECT count(*) FROM "AgentRun" r WHERE r."taskId"=t.id AND r.outcome='failed'))
|
||||
FROM "AgentTask" t WHERE "ownerId"=$1 ORDER BY "createdAt" DESC`, user(r))
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
@@ -169,8 +194,19 @@ func HandleAgentTasksCreate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
clean, _ := json.Marshal(spec) // re-marshal the NORMALIZED spec
|
||||
prompt := strings.TrimSpace(str(b, "prompt"))
|
||||
if len([]rune(prompt)) > 4000 {
|
||||
respond(w, 0, nil, bad("prompt must be at most 4000 characters"))
|
||||
return
|
||||
}
|
||||
groupID, e := agentGroupRef(r, b["groupId"])
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
row, e := db.Insert(r.Context(), database(r), "AgentTask", object{
|
||||
"id": db.NewID(), "ownerId": user(r), "name": name, "spec": string(clean),
|
||||
"prompt": prompt, "groupId": groupID,
|
||||
})
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
@@ -220,6 +256,22 @@ func HandleAgentTasksUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
updates["status"] = "active"
|
||||
}
|
||||
}
|
||||
if _, present := b["prompt"]; present {
|
||||
s := strings.TrimSpace(str(b, "prompt"))
|
||||
if len([]rune(s)) > 4000 {
|
||||
respond(w, 0, nil, bad("prompt must be at most 4000 characters"))
|
||||
return
|
||||
}
|
||||
updates["prompt"] = s
|
||||
}
|
||||
if _, present := b["groupId"]; present {
|
||||
g, e := agentGroupRef(r, b["groupId"])
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
updates["groupId"] = g
|
||||
}
|
||||
if _, present := b["spec"]; present {
|
||||
rawSpec, e := json.Marshal(b["spec"])
|
||||
if e != nil {
|
||||
@@ -311,7 +363,8 @@ func HandleAgentTasksRun(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func HandleAgentRunsList(w http.ResponseWriter, r *http.Request) {
|
||||
taskID := strings.TrimSpace(r.URL.Query().Get("taskId"))
|
||||
q := `SELECT to_jsonb(r) FROM "AgentRun" r WHERE r."ownerId"=$1`
|
||||
q := `SELECT to_jsonb(r)||jsonb_build_object('taskName',t.name)
|
||||
FROM "AgentRun" r JOIN "AgentTask" t ON t.id=r."taskId" WHERE r."ownerId"=$1`
|
||||
args := []any{user(r)}
|
||||
if taskID != "" {
|
||||
q += ` AND r."taskId"=$2`
|
||||
@@ -326,9 +379,90 @@ func HandleAgentRunsList(w http.ResponseWriter, r *http.Request) {
|
||||
out := []object{}
|
||||
for _, row := range rows {
|
||||
out = append(out, object{
|
||||
"id": row["id"], "taskId": row["taskId"], "outcome": row["outcome"],
|
||||
"id": row["id"], "taskId": row["taskId"], "taskName": row["taskName"],
|
||||
"outcome": row["outcome"],
|
||||
"detail": row["detail"], "createdAt": row["createdAt"],
|
||||
})
|
||||
}
|
||||
respond(w, 200, out, nil)
|
||||
}
|
||||
|
||||
// --- Automation groups (workspace phase 1) ---------------------------------
|
||||
// User-defined folders for the sidebar and the workspace. Deleting a group
|
||||
// ungroups its tasks (FK SET NULL); it never deletes automations. The
|
||||
// engine never reads groups.
|
||||
|
||||
func HandleAgentGroupsList(w http.ResponseWriter, r *http.Request) {
|
||||
rows, e := db.Many(r.Context(), database(r),
|
||||
`SELECT to_jsonb(g) FROM "AgentGroup" g WHERE "ownerId"=$1 ORDER BY g.position, g."createdAt"`, user(r))
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
out := []object{}
|
||||
for _, row := range rows {
|
||||
out = append(out, object{
|
||||
"id": row["id"], "name": row["name"], "position": row["position"],
|
||||
"version": row["version"], "createdAt": row["createdAt"],
|
||||
})
|
||||
}
|
||||
respond(w, 200, out, nil)
|
||||
}
|
||||
|
||||
func HandleAgentGroupsCreate(w http.ResponseWriter, r *http.Request) {
|
||||
b, ok := read(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(str(b, "name"))
|
||||
if name == "" || len([]rune(name)) > 80 {
|
||||
respond(w, 0, nil, bad("name is required (max 80 characters)"))
|
||||
return
|
||||
}
|
||||
var pos int
|
||||
if e := database(r).QueryRowContext(r.Context(),
|
||||
`SELECT COALESCE(max(position),-1)+1 FROM "AgentGroup" WHERE "ownerId"=$1`, user(r)).Scan(&pos); e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
row, e := db.Insert(r.Context(), database(r), "AgentGroup", object{
|
||||
"id": db.NewID(), "ownerId": user(r), "name": name, "position": pos,
|
||||
})
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
respond(w, 201, object{"id": row["id"], "name": row["name"], "position": row["position"], "version": row["version"]}, nil)
|
||||
}
|
||||
|
||||
func HandleAgentGroupsUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if _, e := owned(r, database(r), "AgentGroup", id, "ownerId"); e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
b, ok := read(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
baseVersion, e := requiredVersion(b)
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(str(b, "name"))
|
||||
if name == "" || len([]rune(name)) > 80 {
|
||||
respond(w, 0, nil, bad("name is required (max 80 characters)"))
|
||||
return
|
||||
}
|
||||
row, e := patchVersioned(r, "AgentGroup", id, baseVersion, object{"name": name})
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
respond(w, 200, object{"id": row["id"], "name": row["name"], "position": row["position"], "version": row["version"]}, nil)
|
||||
}
|
||||
|
||||
func HandleAgentGroupsDelete(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, 200, object{"ok": true}, remove(r, "AgentGroup", r.PathValue("id"), "ownerId"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user