@swarmclawai/swarmclaw 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/README.md +20 -0
  2. package/bin/daemon-cmd.js +169 -0
  3. package/bin/server-cmd.js +3 -0
  4. package/bin/swarmclaw.js +11 -0
  5. package/package.json +17 -16
  6. package/src/app/api/agents/[id]/clone/route.ts +3 -32
  7. package/src/app/api/agents/[id]/route.ts +6 -158
  8. package/src/app/api/agents/[id]/status/route.ts +2 -3
  9. package/src/app/api/agents/[id]/thread/route.ts +4 -17
  10. package/src/app/api/agents/bulk/route.ts +5 -47
  11. package/src/app/api/agents/route.ts +5 -119
  12. package/src/app/api/agents/trash/route.ts +13 -24
  13. package/src/app/api/auth/route.ts +3 -9
  14. package/src/app/api/autonomy/estop/route.ts +5 -5
  15. package/src/app/api/chatrooms/[id]/chat/route.ts +11 -5
  16. package/src/app/api/chatrooms/[id]/route.ts +23 -2
  17. package/src/app/api/chatrooms/route.ts +13 -2
  18. package/src/app/api/chats/[id]/clear/route.ts +2 -13
  19. package/src/app/api/chats/[id]/deploy/route.ts +2 -3
  20. package/src/app/api/chats/[id]/edit-resend/route.ts +7 -13
  21. package/src/app/api/chats/[id]/mailbox/route.ts +6 -8
  22. package/src/app/api/chats/[id]/queue/route.ts +17 -64
  23. package/src/app/api/chats/[id]/retry/route.ts +4 -22
  24. package/src/app/api/chats/[id]/route.ts +10 -138
  25. package/src/app/api/chats/heartbeat/route.ts +2 -1
  26. package/src/app/api/chats/migrate-messages/route.ts +7 -0
  27. package/src/app/api/chats/route.ts +13 -134
  28. package/src/app/api/connectors/[id]/access/route.ts +12 -229
  29. package/src/app/api/connectors/[id]/doctor/route.ts +1 -1
  30. package/src/app/api/connectors/[id]/health/route.ts +12 -39
  31. package/src/app/api/connectors/[id]/route.ts +14 -122
  32. package/src/app/api/connectors/[id]/webhook/route.ts +1 -1
  33. package/src/app/api/connectors/doctor/route.ts +1 -1
  34. package/src/app/api/connectors/route.ts +12 -70
  35. package/src/app/api/credentials/[id]/route.ts +2 -4
  36. package/src/app/api/credentials/route.ts +10 -19
  37. package/src/app/api/daemon/health-check/route.ts +3 -4
  38. package/src/app/api/daemon/route.ts +10 -8
  39. package/src/app/api/documents/route.ts +11 -10
  40. package/src/app/api/external-agents/route.ts +3 -3
  41. package/src/app/api/gateways/[id]/health/route.ts +2 -3
  42. package/src/app/api/gateways/[id]/route.ts +7 -122
  43. package/src/app/api/gateways/route.ts +3 -103
  44. package/src/app/api/mcp-servers/[id]/tools/route.ts +5 -5
  45. package/src/app/api/openclaw/dashboard-url/route.ts +8 -16
  46. package/src/app/api/openclaw/directory/route.ts +2 -2
  47. package/src/app/api/openclaw/history/route.ts +3 -5
  48. package/src/app/api/providers/[id]/models/route.test.ts +60 -0
  49. package/src/app/api/providers/[id]/models/route.ts +33 -1
  50. package/src/app/api/providers/[id]/route.test.ts +49 -0
  51. package/src/app/api/providers/[id]/route.ts +30 -1
  52. package/src/app/api/providers/ollama/route.ts +6 -5
  53. package/src/app/api/schedules/[id]/route.ts +14 -108
  54. package/src/app/api/schedules/[id]/run/route.ts +6 -67
  55. package/src/app/api/schedules/route.ts +9 -51
  56. package/src/app/api/settings/route.ts +4 -3
  57. package/src/app/api/setup/check-provider/route.ts +15 -1
  58. package/src/app/api/setup/openclaw-device/route.ts +2 -2
  59. package/src/app/api/system/status/route.ts +2 -2
  60. package/src/app/api/tasks/[id]/route.ts +16 -202
  61. package/src/app/api/tasks/bulk/route.ts +5 -86
  62. package/src/app/api/tasks/metrics/route.ts +2 -1
  63. package/src/app/api/tasks/route.ts +11 -171
  64. package/src/app/api/upload/route.ts +1 -1
  65. package/src/app/api/uploads/[filename]/route.ts +1 -1
  66. package/src/app/api/uploads/route.ts +1 -1
  67. package/src/app/api/webhooks/[id]/history/route.ts +2 -2
  68. package/src/app/layout.tsx +9 -6
  69. package/src/app/protocols/page.tsx +71 -89
  70. package/src/app/tasks/page.tsx +32 -32
  71. package/src/cli/index.js +1 -0
  72. package/src/cli/spec.js +1 -0
  73. package/src/components/agents/agent-sheet.tsx +51 -25
  74. package/src/components/agents/inspector-panel.tsx +15 -4
  75. package/src/components/auth/setup-wizard/index.tsx +27 -18
  76. package/src/components/auth/setup-wizard/shared.tsx +2 -2
  77. package/src/components/auth/setup-wizard/step-agents.tsx +51 -38
  78. package/src/components/auth/setup-wizard/step-connect.tsx +48 -17
  79. package/src/components/auth/setup-wizard/types.ts +6 -4
  80. package/src/components/auth/setup-wizard/utils.test.ts +38 -8
  81. package/src/components/auth/setup-wizard/utils.ts +14 -8
  82. package/src/components/chatrooms/chatroom-sheet.tsx +16 -276
  83. package/src/components/connectors/connector-list.tsx +26 -40
  84. package/src/components/connectors/connector-sheet.tsx +95 -149
  85. package/src/components/gateways/gateway-sheet.tsx +61 -110
  86. package/src/components/layout/live-query-sync.tsx +121 -0
  87. package/src/components/protocols/structured-session-launcher.tsx +24 -45
  88. package/src/components/providers/app-query-provider.tsx +17 -0
  89. package/src/components/providers/provider-list.tsx +150 -77
  90. package/src/components/providers/provider-sheet.tsx +102 -77
  91. package/src/components/shared/model-combobox.tsx +5 -4
  92. package/src/components/skills/skill-list.tsx +5 -18
  93. package/src/components/skills/skill-sheet.tsx +21 -20
  94. package/src/components/skills/skills-workspace.tsx +48 -87
  95. package/src/components/tasks/task-card.tsx +20 -13
  96. package/src/components/tasks/task-column.tsx +22 -7
  97. package/src/components/tasks/task-list.tsx +8 -11
  98. package/src/components/tasks/task-sheet.tsx +111 -103
  99. package/src/features/agents/queries.ts +20 -0
  100. package/src/features/chatrooms/queries.ts +20 -0
  101. package/src/features/chats/queries.ts +27 -0
  102. package/src/features/connectors/queries.ts +145 -0
  103. package/src/features/credentials/queries.ts +37 -0
  104. package/src/features/extensions/queries.ts +26 -0
  105. package/src/features/external-agents/queries.ts +36 -0
  106. package/src/features/gateways/queries.ts +274 -0
  107. package/src/features/missions/queries.ts +23 -0
  108. package/src/features/projects/queries.ts +20 -0
  109. package/src/features/protocols/queries.ts +149 -0
  110. package/src/features/providers/queries.ts +142 -0
  111. package/src/features/settings/queries.ts +20 -0
  112. package/src/features/skills/queries.ts +182 -0
  113. package/src/features/tasks/queries.ts +189 -0
  114. package/src/hooks/use-ws.ts +3 -2
  115. package/src/lib/agent-provider-options.test.ts +152 -0
  116. package/src/lib/agent-provider-options.ts +84 -0
  117. package/src/lib/app/api-client.ts +2 -2
  118. package/src/lib/providers/index.test.ts +78 -0
  119. package/src/lib/providers/index.ts +13 -10
  120. package/src/lib/query/client.ts +17 -0
  121. package/src/lib/server/agents/agent-runtime-config.ts +6 -6
  122. package/src/lib/server/agents/agent-service.ts +429 -0
  123. package/src/lib/server/agents/agent-thread-session.ts +6 -5
  124. package/src/lib/server/agents/autonomy-contract.ts +1 -4
  125. package/src/lib/server/agents/delegation-advisory.test.ts +206 -0
  126. package/src/lib/server/agents/delegation-advisory.ts +251 -0
  127. package/src/lib/server/agents/main-agent-loop.ts +98 -40
  128. package/src/lib/server/agents/subagent-runtime.ts +12 -0
  129. package/src/lib/server/autonomy/supervisor-reflection.test.ts +20 -1
  130. package/src/lib/server/autonomy/supervisor-reflection.ts +39 -19
  131. package/src/lib/server/build-llm.ts +7 -15
  132. package/src/lib/server/capability-router.test.ts +70 -1
  133. package/src/lib/server/capability-router.ts +24 -99
  134. package/src/lib/server/chat-execution/chat-execution-utils.ts +0 -15
  135. package/src/lib/server/chat-execution/chat-streaming-utils.ts +2 -4
  136. package/src/lib/server/chat-execution/chat-turn-finalization.ts +77 -12
  137. package/src/lib/server/chat-execution/chat-turn-partial-persistence.ts +4 -4
  138. package/src/lib/server/chat-execution/chat-turn-preflight.ts +2 -2
  139. package/src/lib/server/chat-execution/chat-turn-preparation.ts +41 -17
  140. package/src/lib/server/chat-execution/chat-turn-stream-execution.ts +4 -2
  141. package/src/lib/server/chat-execution/chat-turn-tool-routing.test.ts +45 -0
  142. package/src/lib/server/chat-execution/chat-turn-tool-routing.ts +48 -17
  143. package/src/lib/server/chat-execution/continuation-evaluator.ts +4 -1
  144. package/src/lib/server/chat-execution/direct-memory-intent.test.ts +9 -0
  145. package/src/lib/server/chat-execution/direct-memory-intent.ts +12 -2
  146. package/src/lib/server/chat-execution/message-classifier.test.ts +35 -23
  147. package/src/lib/server/chat-execution/message-classifier.ts +74 -32
  148. package/src/lib/server/chat-execution/prompt-builder.test.ts +29 -0
  149. package/src/lib/server/chat-execution/prompt-builder.ts +37 -2
  150. package/src/lib/server/chat-execution/prompt-sections.test.ts +56 -0
  151. package/src/lib/server/chat-execution/prompt-sections.ts +193 -0
  152. package/src/lib/server/chat-execution/stream-agent-chat.ts +63 -7
  153. package/src/lib/server/chat-execution/stream-continuation.test.ts +36 -0
  154. package/src/lib/server/chat-execution/stream-continuation.ts +28 -13
  155. package/src/lib/server/chatrooms/chatroom-agent-signals.ts +26 -18
  156. package/src/lib/server/chatrooms/chatroom-helpers.ts +19 -18
  157. package/src/lib/server/chatrooms/chatroom-repository.ts +16 -0
  158. package/src/lib/server/chatrooms/chatroom-routing.test.ts +96 -0
  159. package/src/lib/server/chatrooms/chatroom-routing.ts +207 -53
  160. package/src/lib/server/chatrooms/mailbox-utils.ts +4 -2
  161. package/src/lib/server/chatrooms/session-mailbox.ts +50 -40
  162. package/src/lib/server/chats/chat-session-service.ts +410 -0
  163. package/src/lib/server/connectors/access.ts +1 -1
  164. package/src/lib/server/connectors/commands.ts +7 -6
  165. package/src/lib/server/connectors/connector-inbound.ts +14 -7
  166. package/src/lib/server/connectors/connector-outbound.ts +16 -11
  167. package/src/lib/server/connectors/connector-service.ts +453 -0
  168. package/src/lib/server/connectors/delivery.ts +17 -12
  169. package/src/lib/server/connectors/inbound-audio-transcription.ts +5 -14
  170. package/src/lib/server/connectors/media.ts +1 -1
  171. package/src/lib/server/connectors/response-media.ts +1 -1
  172. package/src/lib/server/connectors/session-consolidation.ts +11 -7
  173. package/src/lib/server/connectors/session.ts +9 -7
  174. package/src/lib/server/connectors/voice-note.ts +2 -1
  175. package/src/lib/server/context-manager.ts +20 -1
  176. package/src/lib/server/cost.ts +2 -3
  177. package/src/lib/server/credentials/credential-repository.ts +43 -4
  178. package/src/lib/server/credentials/credential-service.ts +112 -0
  179. package/src/lib/server/daemon/admin-metadata.ts +64 -0
  180. package/src/lib/server/daemon/controller.ts +577 -0
  181. package/src/lib/server/daemon/daemon-runtime.ts +352 -0
  182. package/src/lib/server/daemon/daemon-status-repository.ts +63 -0
  183. package/src/lib/server/daemon/types.ts +101 -0
  184. package/src/lib/server/embeddings.ts +3 -9
  185. package/src/lib/server/eval/agent-regression.ts +3 -2
  186. package/src/lib/server/eval/runner.ts +2 -2
  187. package/src/lib/server/execution-brief.test.ts +167 -0
  188. package/src/lib/server/execution-brief.ts +295 -0
  189. package/src/lib/server/execution-engine/chat-turn.ts +9 -0
  190. package/src/lib/server/execution-engine/import-boundary.test.ts +44 -0
  191. package/src/lib/server/execution-engine/index.ts +35 -0
  192. package/src/lib/server/execution-engine/task-attempt.ts +303 -0
  193. package/src/lib/server/execution-engine/types.ts +33 -0
  194. package/src/lib/server/gateways/gateway-profile-repository.ts +47 -3
  195. package/src/lib/server/gateways/gateway-profile-service.ts +200 -0
  196. package/src/lib/server/memory/session-archive-memory.ts +12 -10
  197. package/src/lib/server/messages/message-repository.ts +330 -0
  198. package/src/lib/server/missions/mission-service/core.ts +8 -6
  199. package/src/lib/server/openclaw/agent-resolver.ts +2 -3
  200. package/src/lib/server/openclaw/doctor.ts +1 -1
  201. package/src/lib/server/openclaw/gateway.test.ts +10 -1
  202. package/src/lib/server/openclaw/gateway.ts +5 -14
  203. package/src/lib/server/openclaw/health.ts +3 -11
  204. package/src/lib/server/openclaw/sync.ts +8 -6
  205. package/src/lib/server/persistence/storage-context.ts +3 -0
  206. package/src/lib/server/protocols/protocol-agent-turn.ts +25 -17
  207. package/src/lib/server/protocols/protocol-normalization.ts +1 -1
  208. package/src/lib/server/protocols/protocol-queries.ts +13 -7
  209. package/src/lib/server/protocols/protocol-run-lifecycle.ts +16 -20
  210. package/src/lib/server/protocols/protocol-run-repository.ts +81 -0
  211. package/src/lib/server/protocols/protocol-step-processors.ts +23 -31
  212. package/src/lib/server/protocols/protocol-swarm.ts +8 -8
  213. package/src/lib/server/protocols/protocol-template-repository.ts +42 -0
  214. package/src/lib/server/protocols/protocol-templates.ts +4 -2
  215. package/src/lib/server/protocols/protocol-types.ts +10 -7
  216. package/src/lib/server/provider-endpoint.ts +7 -12
  217. package/src/lib/server/provider-model-discovery.ts +2 -11
  218. package/src/lib/server/query-expansion.ts +5 -6
  219. package/src/lib/server/run-context.test.ts +365 -0
  220. package/src/lib/server/run-context.ts +367 -0
  221. package/src/lib/server/runtime/heartbeat-service.ts +7 -5
  222. package/src/lib/server/runtime/queue/core.ts +61 -190
  223. package/src/lib/server/runtime/run-ledger.ts +8 -0
  224. package/src/lib/server/runtime/session-run-manager/drain.ts +2 -2
  225. package/src/lib/server/runtime/session-run-manager/enqueue.ts +6 -0
  226. package/src/lib/server/runtime/session-run-manager/state.ts +4 -0
  227. package/src/lib/server/schedules/schedule-route-service.ts +230 -0
  228. package/src/lib/server/service-result.ts +16 -0
  229. package/src/lib/server/session-note.ts +2 -3
  230. package/src/lib/server/session-reset-policy.ts +4 -3
  231. package/src/lib/server/session-tools/connector.ts +9 -6
  232. package/src/lib/server/session-tools/context-mgmt.ts +58 -9
  233. package/src/lib/server/session-tools/crud.ts +162 -10
  234. package/src/lib/server/session-tools/delegate.ts +1 -1
  235. package/src/lib/server/session-tools/manage-tasks.test.ts +152 -0
  236. package/src/lib/server/session-tools/memory.ts +6 -4
  237. package/src/lib/server/session-tools/session-info.test.ts +56 -0
  238. package/src/lib/server/session-tools/session-info.ts +119 -12
  239. package/src/lib/server/session-tools/skill-runtime.ts +3 -1
  240. package/src/lib/server/session-tools/skills.ts +15 -15
  241. package/src/lib/server/session-tools/subagent.test.ts +115 -1
  242. package/src/lib/server/session-tools/subagent.ts +125 -7
  243. package/src/lib/server/session-tools/team-context.ts +4 -3
  244. package/src/lib/server/session-tools/wallet.ts +0 -58
  245. package/src/lib/server/sessions/session-lineage.ts +55 -0
  246. package/src/lib/server/sessions/session-repository.ts +2 -2
  247. package/src/lib/server/skills/learned-skills.ts +24 -23
  248. package/src/lib/server/skills/runtime-skill-resolver.ts +2 -1
  249. package/src/lib/server/skills/skill-repository.ts +136 -13
  250. package/src/lib/server/skills/skill-suggestions.ts +25 -28
  251. package/src/lib/server/storage-normalization.test.ts +42 -215
  252. package/src/lib/server/storage-normalization.ts +98 -0
  253. package/src/lib/server/storage.ts +19 -0
  254. package/src/lib/server/structured-extract.ts +3 -14
  255. package/src/lib/server/tasks/task-followups.ts +16 -11
  256. package/src/lib/server/tasks/task-result.test.ts +25 -29
  257. package/src/lib/server/tasks/task-result.ts +5 -9
  258. package/src/lib/server/tasks/task-route-service.ts +449 -0
  259. package/src/lib/server/text-normalization.ts +41 -0
  260. package/src/lib/server/tool-planning.ts +6 -42
  261. package/src/lib/server/upload-path.ts +5 -0
  262. package/src/lib/server/working-state/extraction.ts +614 -0
  263. package/src/lib/server/working-state/normalization.ts +866 -0
  264. package/src/lib/server/working-state/prompt.ts +60 -0
  265. package/src/lib/server/working-state/repository.ts +38 -0
  266. package/src/lib/server/working-state/service.test.ts +253 -0
  267. package/src/lib/server/working-state/service.ts +293 -0
  268. package/src/lib/validation/schemas.ts +1 -0
  269. package/src/lib/ws-client.ts +3 -3
  270. package/src/stores/slices/task-slice.ts +1 -4
  271. package/src/stores/use-chatroom-store.ts +2 -2
  272. package/src/types/index.ts +288 -22
  273. package/src/views/settings/section-providers.tsx +2 -2
@@ -1,13 +1,6 @@
1
1
  import { NextResponse } from 'next/server'
2
2
  import { safeParseBody } from '@/lib/server/safe-parse-body'
3
- import { loadTasks, logActivity, upsertStoredItems } from '@/lib/server/storage'
4
- import { enqueueTask, disableSessionHeartbeat } from '@/lib/server/runtime/queue'
5
- import { pushMainLoopEventToMainSessions } from '@/lib/server/agents/main-agent-loop'
6
- import { notify } from '@/lib/server/ws-hub'
7
- import { createNotification } from '@/lib/server/create-notification'
8
- import type { BoardTask, BoardTaskStatus } from '@/types'
9
-
10
- const VALID_STATUSES: BoardTaskStatus[] = ['backlog', 'queued', 'running', 'completed', 'failed', 'archived']
3
+ import { bulkUpdateTasksFromRoute } from '@/lib/server/tasks/task-route-service'
11
4
 
12
5
  /**
13
6
  * Bulk update tasks — batch status changes, agent/project reassignment, or archive/delete.
@@ -21,82 +14,8 @@ const VALID_STATUSES: BoardTaskStatus[] = ['backlog', 'queued', 'running', 'comp
21
14
  export async function POST(req: Request) {
22
15
  const { data: body, error } = await safeParseBody<Record<string, unknown>>(req)
23
16
  if (error) return error
24
- const ids: unknown = body.ids
25
- if (!Array.isArray(ids) || ids.length === 0) {
26
- return NextResponse.json({ error: 'ids must be a non-empty array' }, { status: 400 })
27
- }
28
-
29
- const taskIds = ids.filter((id): id is string => typeof id === 'string')
30
- if (taskIds.length === 0) {
31
- return NextResponse.json({ error: 'No valid task IDs provided' }, { status: 400 })
32
- }
33
-
34
- const tasks = loadTasks()
35
- let updated = 0
36
- const results: string[] = []
37
-
38
- for (const id of taskIds) {
39
- if (!tasks[id]) continue
40
- const prevStatus = tasks[id].status
41
-
42
- if (typeof body.status === 'string' && VALID_STATUSES.includes(body.status as BoardTaskStatus)) {
43
- tasks[id].status = body.status as BoardTaskStatus
44
- if (body.status === 'archived' && prevStatus !== 'archived') {
45
- tasks[id].archivedAt = Date.now()
46
- }
47
- }
48
-
49
- if ('agentId' in body) {
50
- tasks[id].agentId = body.agentId === null ? '' : String(body.agentId)
51
- }
52
-
53
- if ('projectId' in body) {
54
- if (body.projectId === null) {
55
- delete tasks[id].projectId
56
- } else {
57
- tasks[id].projectId = String(body.projectId)
58
- }
59
- }
60
-
61
- tasks[id].updatedAt = Date.now()
62
- updated++
63
- results.push(id)
64
-
65
- // Side-effects for status transitions
66
- if (prevStatus !== tasks[id].status) {
67
- logActivity({
68
- entityType: 'task',
69
- entityId: id,
70
- action: 'updated',
71
- actor: 'user',
72
- summary: `Bulk update: "${tasks[id].title}" (${prevStatus} → ${tasks[id].status})`,
73
- })
74
- pushMainLoopEventToMainSessions({
75
- type: 'task_status_changed',
76
- text: `Task "${tasks[id].title}" (${id}) moved ${prevStatus} → ${tasks[id].status}.`,
77
- })
78
- if (tasks[id].status === 'completed' || tasks[id].status === 'failed') {
79
- disableSessionHeartbeat(tasks[id].sessionId)
80
- }
81
- if (prevStatus !== 'queued' && tasks[id].status === 'queued') {
82
- enqueueTask(id)
83
- }
84
- }
85
- }
86
-
87
- upsertStoredItems('tasks', results.map((id) => [id, tasks[id]] as [string, BoardTask]))
88
-
89
- if (updated > 0) {
90
- const action = body.status
91
- ? `moved ${updated} task(s) to ${body.status}`
92
- : `updated ${updated} task(s)`
93
- createNotification({
94
- type: 'success',
95
- title: `Bulk update: ${action}`,
96
- entityType: 'task',
97
- })
98
- }
99
-
100
- notify('tasks')
101
- return NextResponse.json({ updated, ids: results })
17
+ const result = bulkUpdateTasksFromRoute(body)
18
+ return result.ok
19
+ ? NextResponse.json(result.payload)
20
+ : NextResponse.json(result.payload, { status: result.status })
102
21
  }
@@ -1,5 +1,6 @@
1
1
  import { NextResponse } from 'next/server'
2
- import { loadTasks, loadAgents } from '@/lib/server/storage'
2
+ import { loadAgents } from '@/lib/server/agents/agent-repository'
3
+ import { loadTasks } from '@/lib/server/tasks/task-repository'
3
4
 
4
5
  type Range = '24h' | '7d' | '30d'
5
6
 
@@ -1,35 +1,19 @@
1
1
  import { NextResponse } from 'next/server'
2
2
  import { safeParseBody } from '@/lib/server/safe-parse-body'
3
- import { genId } from '@/lib/id'
4
3
  import { perf } from '@/lib/server/runtime/perf'
5
- import { deleteTask, loadAgents, loadSettings, loadTasks, logActivity, upsertTask } from '@/lib/server/storage'
6
4
  import { TaskCreateSchema, formatZodError } from '@/lib/validation/schemas'
7
5
  import { z } from 'zod'
8
- import { enqueueTask, recoverStalledRunningTasks, validateCompletedTasksQueue } from '@/lib/server/runtime/queue'
9
- import { pushMainLoopEventToMainSessions } from '@/lib/server/agents/main-agent-loop'
10
- import { notify } from '@/lib/server/ws-hub'
11
- import { resolveTaskAgentFromDescription } from '@/lib/server/tasks/task-mention'
12
- import { validateDag } from '@/lib/server/dag-validation'
13
- import { getExtensionManager } from '@/lib/server/extensions'
14
- import { getEnabledCapabilityIds } from '@/lib/capability-selection'
15
6
  import {
16
- prepareTaskCreation,
17
- } from '@/lib/server/tasks/task-service'
18
- import { ensureMissionForTask, enrichTaskWithMissionSummary } from '@/lib/server/missions/mission-service'
19
- import '@/lib/server/builtin-extensions'
7
+ createTaskFromRoute,
8
+ deleteTasksByFilter,
9
+ prepareTasksForListing,
10
+ } from '@/lib/server/tasks/task-route-service'
20
11
 
21
12
  export async function GET(req: Request) {
22
13
  const endPerf = perf.start('api', 'GET /api/tasks')
23
- // Keep completed queue integrity even if daemon is not running.
24
- validateCompletedTasksQueue()
25
- recoverStalledRunningTasks()
26
-
27
14
  const { searchParams } = new URL(req.url)
28
15
  const includeArchived = searchParams.get('includeArchived') === 'true'
29
- const allTasks = loadTasks()
30
- const missionTasks = Object.fromEntries(
31
- Object.entries(allTasks).map(([id, task]) => [id, enrichTaskWithMissionSummary(task)]),
32
- )
16
+ const missionTasks = prepareTasksForListing()
33
17
 
34
18
  if (includeArchived) {
35
19
  endPerf({ count: Object.keys(missionTasks).length })
@@ -37,7 +21,7 @@ export async function GET(req: Request) {
37
21
  }
38
22
 
39
23
  // Exclude archived tasks by default
40
- const filtered: Record<string, typeof allTasks[string]> = {}
24
+ const filtered: Record<string, (typeof missionTasks)[string]> = {}
41
25
  for (const [id, task] of Object.entries(missionTasks)) {
42
26
  if (task.status !== 'archived') {
43
27
  filtered[id] = task
@@ -50,23 +34,7 @@ export async function GET(req: Request) {
50
34
  export async function DELETE(req: Request) {
51
35
  const { searchParams } = new URL(req.url)
52
36
  const filter = searchParams.get('filter') // 'all' | 'schedule' | 'done' | null
53
- const tasks = loadTasks()
54
- let removed = 0
55
-
56
- const shouldRemove = (task: { status: string; sourceType?: string }) =>
57
- filter === 'all' ||
58
- (filter === 'schedule' && task.sourceType === 'schedule') ||
59
- (filter === 'done' && (task.status === 'completed' || task.status === 'failed')) ||
60
- (!filter && task.status === 'archived')
61
-
62
- for (const [id, task] of Object.entries(tasks)) {
63
- if (shouldRemove(task as { status: string; sourceType?: string })) {
64
- deleteTask(id)
65
- removed++
66
- }
67
- }
68
- notify('tasks')
69
- return NextResponse.json({ removed, remaining: Object.keys(tasks).length - removed })
37
+ return NextResponse.json(deleteTasksByFilter(filter))
70
38
  }
71
39
 
72
40
  export async function POST(req: Request) {
@@ -76,136 +44,8 @@ export async function POST(req: Request) {
76
44
  if (!parsed.success) {
77
45
  return NextResponse.json(formatZodError(parsed.error as z.ZodError), { status: 400 })
78
46
  }
79
- const body = { ...raw, ...parsed.data } as Record<string, unknown> & typeof parsed.data
80
- const id = genId()
81
- const now = Date.now()
82
- const tasks = loadTasks()
83
- const settings = loadSettings()
84
- const maxAttempts = Number.isFinite(Number(body.maxAttempts))
85
- ? Math.max(1, Math.min(20, Math.trunc(Number(body.maxAttempts))))
86
- : Math.max(1, Math.min(20, Math.trunc(Number(settings.defaultTaskMaxAttempts ?? 3))))
87
- const retryBackoffSec = Number.isFinite(Number(body.retryBackoffSec))
88
- ? Math.max(1, Math.min(3600, Math.trunc(Number(body.retryBackoffSec))))
89
- : Math.max(1, Math.min(3600, Math.trunc(Number(settings.taskRetryBackoffSec ?? 30))))
90
- // DAG validation: reject if proposed blockedBy would create a cycle
91
- if (Array.isArray(body.blockedBy) && body.blockedBy.length > 0) {
92
- const dagResult = validateDag(tasks, id, body.blockedBy)
93
- if (!dagResult.valid) {
94
- return NextResponse.json(
95
- { error: 'Dependency cycle detected', cycle: dagResult.cycle },
96
- { status: 400 },
97
- )
98
- }
99
- }
100
-
101
- // Resolve @mentions in description to auto-assign agent
102
- const resolvedAgentId = body.description
103
- ? resolveTaskAgentFromDescription(body.description, body.agentId || '', loadAgents())
104
- : (body.agentId || '')
105
-
106
- const prepared = prepareTaskCreation({
107
- id,
108
- input: {
109
- ...body,
110
- agentId: resolvedAgentId,
111
- },
112
- tasks,
113
- now,
114
- settings,
115
- seed: {
116
- projectId: typeof body.projectId === 'string' && body.projectId ? body.projectId : null,
117
- goalContract: body.goalContract || null,
118
- cwd: typeof body.cwd === 'string' ? body.cwd : null,
119
- file: typeof body.file === 'string' ? body.file : null,
120
- sessionId: typeof body.sessionId === 'string' ? body.sessionId : null,
121
- result: typeof body.result === 'string' ? body.result : null,
122
- error: typeof body.error === 'string' ? body.error : null,
123
- outputFiles: Array.isArray(body.outputFiles)
124
- ? body.outputFiles.filter((entry: unknown) => typeof entry === 'string').slice(0, 24)
125
- : [],
126
- artifacts: Array.isArray(body.artifacts)
127
- ? body.artifacts
128
- .filter((artifact: unknown) => artifact && typeof artifact === 'object')
129
- .map((artifact: unknown) => {
130
- const row = artifact as {
131
- url?: unknown
132
- type?: unknown
133
- filename?: unknown
134
- }
135
- const normalizedType = String(row.type || '')
136
- return {
137
- url: String(row.url || ''),
138
- type: ['image', 'video', 'pdf', 'file'].includes(normalizedType)
139
- ? (normalizedType as 'image' | 'video' | 'pdf' | 'file')
140
- : 'file',
141
- filename: String(row.filename || ''),
142
- }
143
- })
144
- .filter((artifact: { url: string; filename: string }) => artifact.url && artifact.filename)
145
- .slice(0, 24)
146
- : [],
147
- archivedAt: null,
148
- attempts: 0,
149
- maxAttempts,
150
- retryBackoffSec,
151
- retryScheduledAt: null,
152
- deadLetteredAt: null,
153
- checkpoint: null,
154
- blockedBy: Array.isArray(body.blockedBy) ? body.blockedBy.filter((s: unknown) => typeof s === 'string') : [],
155
- blocks: Array.isArray(body.blocks) ? body.blocks.filter((s: unknown) => typeof s === 'string') : [],
156
- tags: Array.isArray(body.tags) ? body.tags.filter((s: unknown) => typeof s === 'string') : [],
157
- dueAt: typeof body.dueAt === 'number' ? body.dueAt : null,
158
- customFields: body.customFields && typeof body.customFields === 'object' ? body.customFields : undefined,
159
- priority: body.priority && ['low', 'medium', 'high', 'critical'].includes(body.priority) ? body.priority : undefined,
160
- },
161
- })
162
- if (!prepared.ok) {
163
- return NextResponse.json({ error: prepared.error }, { status: 400 })
164
- }
165
-
166
- if (prepared.duplicate) {
167
- return NextResponse.json({ ...prepared.duplicate, deduplicated: true })
168
- }
169
-
170
- const task = prepared.task
171
- if (task.status === 'completed') {
172
- const agentExtensions = resolvedAgentId ? getEnabledCapabilityIds(loadAgents()[resolvedAgentId]) : []
173
- getExtensionManager().runHook(
174
- 'onTaskComplete',
175
- { taskId: id, result: task.result },
176
- { enabledIds: agentExtensions },
177
- )
178
- }
179
-
180
- // Parent/child hierarchy
181
- const parentTaskId = typeof body.parentTaskId === 'string' && body.parentTaskId.trim() ? body.parentTaskId.trim() : null
182
- if (parentTaskId) {
183
- task.parentTaskId = parentTaskId
184
- const parentTask = tasks[parentTaskId]
185
- if (parentTask) {
186
- const subtaskIds = Array.isArray(parentTask.subtaskIds) ? parentTask.subtaskIds : []
187
- if (!subtaskIds.includes(id)) {
188
- parentTask.subtaskIds = [...subtaskIds, id]
189
- parentTask.updatedAt = now
190
- upsertTask(parentTaskId, parentTask)
191
- }
192
- }
193
- }
194
-
195
- upsertTask(id, task)
196
- const mission = ensureMissionForTask(task, { source: 'manual' })
197
- const finalTask = enrichTaskWithMissionSummary({
198
- ...task,
199
- missionId: mission?.id || task.missionId || null,
200
- })
201
- logActivity({ entityType: 'task', entityId: id, action: 'created', actor: 'user', summary: `Task created: "${task.title}"` })
202
- pushMainLoopEventToMainSessions({
203
- type: 'task_created',
204
- text: `Task created: "${task.title}" (${id}) with status ${task.status}.`,
205
- })
206
- if (task.status === 'queued') {
207
- enqueueTask(id)
208
- }
209
- notify('tasks')
210
- return NextResponse.json(finalTask)
47
+ const result = createTaskFromRoute({ ...raw, ...parsed.data } as Record<string, unknown>)
48
+ return result.ok
49
+ ? NextResponse.json(result.payload)
50
+ : NextResponse.json(result.payload, { status: result.status })
211
51
  }
@@ -2,8 +2,8 @@ import { NextResponse } from 'next/server'
2
2
  import fs from 'fs'
3
3
  import path from 'path'
4
4
  import { genId } from '@/lib/id'
5
- import { UPLOAD_DIR } from '@/lib/server/storage'
6
5
  import { log } from '@/lib/server/logger'
6
+ import { UPLOAD_DIR } from '@/lib/server/upload-path'
7
7
 
8
8
  const TAG = 'api-upload'
9
9
 
@@ -2,8 +2,8 @@ import { NextResponse } from 'next/server'
2
2
  import { notFound } from '@/lib/server/collection-helpers'
3
3
  import fs from 'fs'
4
4
  import path from 'path'
5
- import { UPLOAD_DIR } from '@/lib/server/storage'
6
5
  import { MIME_TYPES } from '@/lib/server/mime'
6
+ import { UPLOAD_DIR } from '@/lib/server/upload-path'
7
7
 
8
8
  export async function GET(_req: Request, { params }: { params: Promise<{ filename: string }> }) {
9
9
  const { filename } = await params
@@ -2,8 +2,8 @@ import { NextResponse } from 'next/server'
2
2
  import fs from 'fs'
3
3
  import path from 'path'
4
4
  import { safeParseBody } from '@/lib/server/safe-parse-body'
5
- import { UPLOAD_DIR } from '@/lib/server/storage'
6
5
  import { getFileCategory } from '@/lib/server/mime'
6
+ import { UPLOAD_DIR } from '@/lib/server/upload-path'
7
7
 
8
8
  interface UploadFile {
9
9
  name: string
@@ -5,8 +5,8 @@ export async function GET(_req: Request, { params }: { params: Promise<{ id: str
5
5
  const { id } = await params
6
6
  const allLogs = loadWebhookLogs()
7
7
  const entries = Object.values(allLogs)
8
- .filter((entry: any) => entry.webhookId === id)
9
- .sort((a: any, b: any) => (b.timestamp || 0) - (a.timestamp || 0))
8
+ .filter((entry) => (entry as Record<string, unknown>).webhookId === id)
9
+ .sort((a, b) => ((b as Record<string, unknown>).timestamp as number || 0) - ((a as Record<string, unknown>).timestamp as number || 0))
10
10
  .slice(0, 100)
11
11
 
12
12
  return NextResponse.json(entries)
@@ -2,6 +2,7 @@ import type { Metadata, Viewport } from "next"
2
2
  import { TooltipProvider } from "@/components/ui/tooltip"
3
3
  import { Toaster } from "@/components/ui/sonner"
4
4
  import { DashboardShell } from "@/components/layout/dashboard-shell"
5
+ import { AppQueryProvider } from "@/components/providers/app-query-provider"
5
6
  import "./globals.css"
6
7
 
7
8
  export const metadata: Metadata = {
@@ -28,12 +29,14 @@ export default function RootLayout({
28
29
  return (
29
30
  <html lang="en" className="dark">
30
31
  <body className="antialiased" cz-shortcut-listen="true">
31
- <TooltipProvider>
32
- <DashboardShell>
33
- {children}
34
- </DashboardShell>
35
- <Toaster />
36
- </TooltipProvider>
32
+ <AppQueryProvider>
33
+ <TooltipProvider>
34
+ <DashboardShell>
35
+ {children}
36
+ </DashboardShell>
37
+ <Toaster />
38
+ </TooltipProvider>
39
+ </AppQueryProvider>
37
40
  </body>
38
41
  </html>
39
42
  )