@sixb/server 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/auth/access-token-boundary.d.ts +23 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -0
- package/dist/auth/application-access.d.ts +4 -0
- package/dist/auth/application-access.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +62 -0
- package/dist/auth/browser-origin.d.ts.map +1 -0
- package/dist/auth/csrf.d.ts +2 -0
- package/dist/auth/csrf.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +35 -0
- package/dist/auth/guard.d.ts.map +1 -0
- package/dist/auth/public-routes.d.ts +8 -0
- package/dist/auth/public-routes.d.ts.map +1 -0
- package/dist/auth/responses.d.ts +11 -0
- package/dist/auth/responses.d.ts.map +1 -0
- package/dist/auth/return-to.d.ts +3 -0
- package/dist/auth/return-to.d.ts.map +1 -0
- package/dist/auth/scope.d.ts +43 -0
- package/dist/auth/scope.d.ts.map +1 -0
- package/dist/auth/session-activity.d.ts +4 -0
- package/dist/auth/session-activity.d.ts.map +1 -0
- package/dist/auth/session-cookies.d.ts +12 -0
- package/dist/auth/session-cookies.d.ts.map +1 -0
- package/dist/files/content.d.ts +141 -0
- package/dist/files/content.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11522 -0
- package/dist/index.js.map +78 -0
- package/dist/openapi/security.d.ts +26 -0
- package/dist/openapi/security.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +151 -0
- package/dist/openapi/tags.d.ts.map +1 -0
- package/dist/registerRoutes.d.ts +31 -0
- package/dist/registerRoutes.d.ts.map +1 -0
- package/dist/routes/action-runs.d.ts +244 -0
- package/dist/routes/action-runs.d.ts.map +1 -0
- package/dist/routes/actions.d.ts +183 -0
- package/dist/routes/actions.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts +4 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1061 -0
- package/dist/routes/agents.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +1366 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +108 -0
- package/dist/routes/connectors.d.ts.map +1 -0
- package/dist/routes/datasets.d.ts +339 -0
- package/dist/routes/datasets.d.ts.map +1 -0
- package/dist/routes/events.d.ts +116 -0
- package/dist/routes/events.d.ts.map +1 -0
- package/dist/routes/files.d.ts +340 -0
- package/dist/routes/files.d.ts.map +1 -0
- package/dist/routes/links.d.ts +175 -0
- package/dist/routes/links.d.ts.map +1 -0
- package/dist/routes/logs.d.ts +88 -0
- package/dist/routes/logs.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +450 -0
- package/dist/routes/objects.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +240 -0
- package/dist/routes/ontology.d.ts.map +1 -0
- package/dist/routes/pipelines.d.ts +399 -0
- package/dist/routes/pipelines.d.ts.map +1 -0
- package/dist/routes/project.d.ts +56 -0
- package/dist/routes/project.d.ts.map +1 -0
- package/dist/routes/projections.d.ts +916 -0
- package/dist/routes/projections.d.ts.map +1 -0
- package/dist/routes/rules.d.ts +165 -0
- package/dist/routes/rules.d.ts.map +1 -0
- package/dist/routes/status.d.ts +81 -0
- package/dist/routes/status.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +294 -0
- package/dist/routes/syncs.d.ts.map +1 -0
- package/dist/routes/telemetry.d.ts +241 -0
- package/dist/routes/telemetry.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +89 -0
- package/dist/routes/webhook-runs.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +60 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +973 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/routes/ws/agents.d.ts +99 -0
- package/dist/routes/ws/agents.d.ts.map +1 -0
- package/dist/routes/ws/events.d.ts +89 -0
- package/dist/routes/ws/events.d.ts.map +1 -0
- package/dist/routes/ws/index.d.ts +31 -0
- package/dist/routes/ws/index.d.ts.map +1 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +47 -0
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -0
- package/dist/routes/ws/logs.d.ts +110 -0
- package/dist/routes/ws/logs.d.ts.map +1 -0
- package/dist/schemas/actions.d.ts +644 -0
- package/dist/schemas/actions.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +3157 -0
- package/dist/schemas/agents.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +2023 -0
- package/dist/schemas/auth.d.ts.map +1 -0
- package/dist/schemas/common.d.ts +30 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/connectors.d.ts +82 -0
- package/dist/schemas/connectors.d.ts.map +1 -0
- package/dist/schemas/datasets.d.ts +744 -0
- package/dist/schemas/datasets.d.ts.map +1 -0
- package/dist/schemas/event-payloads.d.ts +477 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -0
- package/dist/schemas/events.d.ts +665 -0
- package/dist/schemas/events.d.ts.map +1 -0
- package/dist/schemas/files.d.ts +241 -0
- package/dist/schemas/files.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +90 -0
- package/dist/schemas/links.d.ts.map +1 -0
- package/dist/schemas/logs.d.ts +231 -0
- package/dist/schemas/logs.d.ts.map +1 -0
- package/dist/schemas/objects.d.ts +288 -0
- package/dist/schemas/objects.d.ts.map +1 -0
- package/dist/schemas/ontology.d.ts +778 -0
- package/dist/schemas/ontology.d.ts.map +1 -0
- package/dist/schemas/pipelines.d.ts +1373 -0
- package/dist/schemas/pipelines.d.ts.map +1 -0
- package/dist/schemas/project.d.ts +9 -0
- package/dist/schemas/project.d.ts.map +1 -0
- package/dist/schemas/projections.d.ts +6992 -0
- package/dist/schemas/projections.d.ts.map +1 -0
- package/dist/schemas/rules.d.ts +204 -0
- package/dist/schemas/rules.d.ts.map +1 -0
- package/dist/schemas/status.d.ts +189 -0
- package/dist/schemas/status.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +538 -0
- package/dist/schemas/syncs.d.ts.map +1 -0
- package/dist/schemas/telemetry.d.ts +265 -0
- package/dist/schemas/telemetry.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +169 -0
- package/dist/schemas/webhook-runs.d.ts.map +1 -0
- package/dist/schemas/workflows.d.ts +1720 -0
- package/dist/schemas/workflows.d.ts.map +1 -0
- package/dist/server.d.ts +64 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/utils/http.d.ts +23 -0
- package/dist/utils/http.d.ts.map +1 -0
- package/dist/utils/request-body.d.ts +15 -0
- package/dist/utils/request-body.d.ts.map +1 -0
- package/dist/utils/ws.d.ts +10 -0
- package/dist/utils/ws.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/auth/access-token-boundary.ts +68 -0
- package/src/auth/application-access.ts +16 -0
- package/src/auth/browser-origin.ts +367 -0
- package/src/auth/csrf.ts +1 -0
- package/src/auth/guard.ts +169 -0
- package/src/auth/public-routes.ts +87 -0
- package/src/auth/responses.ts +54 -0
- package/src/auth/return-to.ts +21 -0
- package/src/auth/scope.ts +64 -0
- package/src/auth/session-activity.ts +19 -0
- package/src/auth/session-cookies.ts +43 -0
- package/src/files/content.ts +419 -0
- package/src/index.ts +2 -0
- package/src/openapi/security.ts +37 -0
- package/src/openapi/tags.ts +151 -0
- package/src/registerRoutes.ts +49 -0
- package/src/routes/action-runs.ts +238 -0
- package/src/routes/actions.ts +126 -0
- package/src/routes/agent-api-gateway.ts +260 -0
- package/src/routes/agents.ts +943 -0
- package/src/routes/auth.ts +2233 -0
- package/src/routes/connectors.ts +69 -0
- package/src/routes/datasets.ts +402 -0
- package/src/routes/events.ts +55 -0
- package/src/routes/files.ts +675 -0
- package/src/routes/links.ts +143 -0
- package/src/routes/logs.ts +65 -0
- package/src/routes/objects.ts +838 -0
- package/src/routes/ontology.ts +138 -0
- package/src/routes/pipelines.ts +332 -0
- package/src/routes/project.ts +16 -0
- package/src/routes/projections.ts +251 -0
- package/src/routes/rules.ts +124 -0
- package/src/routes/status.ts +53 -0
- package/src/routes/syncs.ts +243 -0
- package/src/routes/telemetry.ts +207 -0
- package/src/routes/webhook-runs.ts +81 -0
- package/src/routes/webhooks.ts +508 -0
- package/src/routes/workflows.ts +1333 -0
- package/src/routes/ws/agents.ts +301 -0
- package/src/routes/ws/events.ts +278 -0
- package/src/routes/ws/index.ts +12 -0
- package/src/routes/ws/log-subscription-hub.ts +375 -0
- package/src/routes/ws/logs.ts +117 -0
- package/src/schemas/actions.ts +123 -0
- package/src/schemas/agents.ts +293 -0
- package/src/schemas/auth.ts +317 -0
- package/src/schemas/common.ts +60 -0
- package/src/schemas/connectors.ts +21 -0
- package/src/schemas/datasets.ts +107 -0
- package/src/schemas/event-payloads.ts +79 -0
- package/src/schemas/events.ts +117 -0
- package/src/schemas/files.ts +82 -0
- package/src/schemas/links.ts +38 -0
- package/src/schemas/logs.ts +76 -0
- package/src/schemas/objects.ts +759 -0
- package/src/schemas/ontology.ts +77 -0
- package/src/schemas/pipelines.ts +150 -0
- package/src/schemas/project.ts +5 -0
- package/src/schemas/projections.ts +144 -0
- package/src/schemas/rules.ts +98 -0
- package/src/schemas/status.ts +51 -0
- package/src/schemas/syncs.ts +83 -0
- package/src/schemas/telemetry.ts +59 -0
- package/src/schemas/webhook-runs.ts +40 -0
- package/src/schemas/workflows.ts +229 -0
- package/src/server.ts +383 -0
- package/src/utils/http.ts +108 -0
- package/src/utils/request-body.ts +62 -0
- package/src/utils/ws.ts +59 -0
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AgentDefinition,
|
|
3
|
+
AgentRequestError,
|
|
4
|
+
type AuthorizationContext,
|
|
5
|
+
type FileRef,
|
|
6
|
+
type OntologySource,
|
|
7
|
+
type Principal,
|
|
8
|
+
type ScopedSixb,
|
|
9
|
+
type Sixb,
|
|
10
|
+
SYSTEM_PRINCIPAL,
|
|
11
|
+
} from "@sixb/core"
|
|
12
|
+
import { agentRunStreamId } from "@sixb/core/agents/streams"
|
|
13
|
+
import {
|
|
14
|
+
createAgentRunId,
|
|
15
|
+
createAgentThreadId,
|
|
16
|
+
dispatchQueuedAgentRuns,
|
|
17
|
+
publishAgentRunCancel,
|
|
18
|
+
publishAgentRunFinished,
|
|
19
|
+
} from "@sixb/core/internal/agents"
|
|
20
|
+
import {
|
|
21
|
+
type AgentMessageRecord,
|
|
22
|
+
type AgentRunDiagnostic,
|
|
23
|
+
type AgentRunRecord,
|
|
24
|
+
type AgentStorage,
|
|
25
|
+
AgentStorageError,
|
|
26
|
+
type AgentThreadRecord,
|
|
27
|
+
} from "@sixb/core/storage"
|
|
28
|
+
import type { Elysia } from "elysia"
|
|
29
|
+
import { ZodError, z } from "zod"
|
|
30
|
+
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
31
|
+
import { type RequestAuthState, requestAuthState } from "../auth/scope"
|
|
32
|
+
import {
|
|
33
|
+
createFileContentResponse,
|
|
34
|
+
fileContentGetResponses,
|
|
35
|
+
fileContentHeadResponses,
|
|
36
|
+
resolveFileRefAtPath,
|
|
37
|
+
} from "../files/content"
|
|
38
|
+
import { SIXB_CSRF_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
39
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
40
|
+
import {
|
|
41
|
+
AgentCatalogItemSchema,
|
|
42
|
+
AgentIdParamsSchema,
|
|
43
|
+
AgentMessageFileContentParamsSchema,
|
|
44
|
+
AgentMessageListResponseSchema,
|
|
45
|
+
AgentMessageSchema,
|
|
46
|
+
AgentMessagesQuerySchema,
|
|
47
|
+
AgentRunListQuerySchema,
|
|
48
|
+
AgentRunListResponseSchema,
|
|
49
|
+
AgentRunParamsSchema,
|
|
50
|
+
AgentRunSchema,
|
|
51
|
+
AgentThreadListQuerySchema,
|
|
52
|
+
AgentThreadListResponseSchema,
|
|
53
|
+
AgentThreadParamsSchema,
|
|
54
|
+
AgentThreadSchema,
|
|
55
|
+
CancelAgentRunBodySchema,
|
|
56
|
+
CancelAgentRunResponseSchema,
|
|
57
|
+
CreateAgentThreadBodySchema,
|
|
58
|
+
CreateAgentThreadResponseSchema,
|
|
59
|
+
PostAgentMessageBodySchema,
|
|
60
|
+
PostAgentMessageResponseSchema,
|
|
61
|
+
RetryAgentRunResponseSchema,
|
|
62
|
+
} from "../schemas/agents"
|
|
63
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
64
|
+
import { FileContentQuerySchema } from "../schemas/files"
|
|
65
|
+
import {
|
|
66
|
+
handleRouteError,
|
|
67
|
+
parseOptionalInt,
|
|
68
|
+
toIsoString,
|
|
69
|
+
unconfiguredStorageResponse,
|
|
70
|
+
} from "../utils/http"
|
|
71
|
+
|
|
72
|
+
const AgentMessageFileContentQuerySchema = FileContentQuerySchema.extend({
|
|
73
|
+
path: z
|
|
74
|
+
.string()
|
|
75
|
+
.min(1)
|
|
76
|
+
.regex(/^\/parts(?:\/|$)/, "Agent message file content paths must start with /parts/"),
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
function serializeAgent(agent: AgentDefinition): ReturnType<typeof AgentCatalogItemSchema.parse> {
|
|
80
|
+
return AgentCatalogItemSchema.parse({
|
|
81
|
+
id: agent.id,
|
|
82
|
+
name: agent.name,
|
|
83
|
+
description: agent.description,
|
|
84
|
+
modelId: agent.model.modelId,
|
|
85
|
+
reasoning: agent.reasoning,
|
|
86
|
+
groupIds: agent.groupIds,
|
|
87
|
+
loop: agent.loop,
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function serializeThread(thread: AgentThreadRecord): ReturnType<typeof AgentThreadSchema.parse> {
|
|
92
|
+
return AgentThreadSchema.parse({
|
|
93
|
+
id: thread.id,
|
|
94
|
+
projectId: thread.projectId,
|
|
95
|
+
agentId: thread.agentId,
|
|
96
|
+
ownerPrincipal: thread.ownerPrincipal,
|
|
97
|
+
title: thread.title,
|
|
98
|
+
status: thread.status,
|
|
99
|
+
activeRunId: thread.activeRunId,
|
|
100
|
+
lastMessageAt: thread.lastMessageAt ? toIsoString(thread.lastMessageAt) : undefined,
|
|
101
|
+
messageCount: thread.messageCount,
|
|
102
|
+
createdAt: toIsoString(thread.createdAt),
|
|
103
|
+
updatedAt: toIsoString(thread.updatedAt),
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function serializeMessage(
|
|
108
|
+
message: AgentMessageRecord,
|
|
109
|
+
annotations: readonly AgentRunDiagnostic[] = []
|
|
110
|
+
): ReturnType<typeof AgentMessageSchema.parse> {
|
|
111
|
+
return AgentMessageSchema.parse({
|
|
112
|
+
id: message.id,
|
|
113
|
+
projectId: message.projectId,
|
|
114
|
+
threadId: message.threadId,
|
|
115
|
+
runId: message.runId,
|
|
116
|
+
role: message.role,
|
|
117
|
+
authorPrincipal: message.authorPrincipal,
|
|
118
|
+
seq: message.seq,
|
|
119
|
+
parts: message.parts,
|
|
120
|
+
annotations,
|
|
121
|
+
metadata: message.metadata,
|
|
122
|
+
contentVersion: message.contentVersion,
|
|
123
|
+
createdAt: toIsoString(message.createdAt),
|
|
124
|
+
completedAt: message.completedAt ? toIsoString(message.completedAt) : undefined,
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function serializeAgentRun(run: AgentRunRecord): ReturnType<typeof AgentRunSchema.parse> {
|
|
129
|
+
return AgentRunSchema.parse({
|
|
130
|
+
id: run.id,
|
|
131
|
+
projectId: run.projectId,
|
|
132
|
+
threadId: run.threadId,
|
|
133
|
+
agentId: run.agentId,
|
|
134
|
+
triggerMessageId: run.triggerMessageId,
|
|
135
|
+
requestedByPrincipal: run.requestedByPrincipal,
|
|
136
|
+
executionPrincipal: run.executionPrincipal,
|
|
137
|
+
status: run.status,
|
|
138
|
+
modelId: run.modelId,
|
|
139
|
+
finishReason: run.finishReason,
|
|
140
|
+
usage: run.usage,
|
|
141
|
+
diagnostics: run.diagnostics,
|
|
142
|
+
error: run.error,
|
|
143
|
+
attempt: run.attempt,
|
|
144
|
+
streamId: agentRunStreamId(run.id),
|
|
145
|
+
createdAt: toIsoString(run.createdAt),
|
|
146
|
+
startedAt: run.startedAt ? toIsoString(run.startedAt) : undefined,
|
|
147
|
+
completedAt: run.completedAt ? toIsoString(run.completedAt) : undefined,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function getAccessibleThread(params: {
|
|
152
|
+
readonly scoped: ScopedSixb<readonly OntologySource[]> | null
|
|
153
|
+
readonly storage: AgentStorage
|
|
154
|
+
readonly projectId: string
|
|
155
|
+
readonly threadId: string
|
|
156
|
+
}): Promise<AgentThreadRecord | null> {
|
|
157
|
+
// A scoped principal reads through the core scoped surface, which applies the owner + run:agent
|
|
158
|
+
// filter. When scoped is null the request is privileged (auth disabled), so nothing is filtered.
|
|
159
|
+
if (params.scoped) {
|
|
160
|
+
return params.scoped.getThread(params.threadId)
|
|
161
|
+
}
|
|
162
|
+
return params.storage.threads.getById({ projectId: params.projectId, id: params.threadId })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function getAgentMessageFileContentThread(params: {
|
|
166
|
+
readonly authState: RequestAuthState
|
|
167
|
+
readonly storage: AgentStorage
|
|
168
|
+
readonly projectId: string
|
|
169
|
+
readonly threadId: string
|
|
170
|
+
}): Promise<AgentThreadRecord | null> {
|
|
171
|
+
const { agentRun } = params.authState
|
|
172
|
+
if (agentRun) {
|
|
173
|
+
if (agentRun.projectId !== params.projectId || agentRun.threadId !== params.threadId) {
|
|
174
|
+
return null
|
|
175
|
+
}
|
|
176
|
+
return params.storage.threads.getById({ projectId: params.projectId, id: params.threadId })
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return getAccessibleThread({
|
|
180
|
+
scoped: params.authState.scoped,
|
|
181
|
+
storage: params.storage,
|
|
182
|
+
projectId: params.projectId,
|
|
183
|
+
threadId: params.threadId,
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function principalForRequest(authz: AuthorizationContext | null): Principal {
|
|
188
|
+
return authz?.principal ?? SYSTEM_PRINCIPAL
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Publish the terminal record core owns; stream delivery stays best-effort at this boundary. */
|
|
192
|
+
async function publishQueuedRunCancellation(
|
|
193
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
194
|
+
run: AgentRunRecord
|
|
195
|
+
): Promise<void> {
|
|
196
|
+
try {
|
|
197
|
+
await publishAgentRunFinished(sixb.broker, run)
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error(`[SixbServer] Agent run '${run.id}' cancellation stream publish failed:`, error)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function handleAgentRouteError(
|
|
204
|
+
error: unknown,
|
|
205
|
+
set: { status?: number | string }
|
|
206
|
+
): { error: string } {
|
|
207
|
+
// A duplicate id is a conflict, not a bad request. Map it to a generic 409 rather than echoing the
|
|
208
|
+
// provider's raw message (which leaks the id, project, and storage prefix).
|
|
209
|
+
if (error instanceof AgentStorageError && error.code === "duplicate_id") {
|
|
210
|
+
set.status = 409
|
|
211
|
+
return { error: "Agent thread already exists" }
|
|
212
|
+
}
|
|
213
|
+
if (error instanceof AgentStorageError && error.code === "active_run_exists") {
|
|
214
|
+
set.status = 409
|
|
215
|
+
return { error: "This conversation already has a response in progress" }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (error instanceof AgentRequestError) {
|
|
219
|
+
switch (error.code) {
|
|
220
|
+
case "agent_not_found":
|
|
221
|
+
case "thread_not_found":
|
|
222
|
+
set.status = 404
|
|
223
|
+
break
|
|
224
|
+
case "active_run_exists":
|
|
225
|
+
set.status = 409
|
|
226
|
+
break
|
|
227
|
+
case "storage_unavailable":
|
|
228
|
+
case "thread_agent_mismatch":
|
|
229
|
+
case "invalid_context":
|
|
230
|
+
set.status = 400
|
|
231
|
+
break
|
|
232
|
+
}
|
|
233
|
+
return { error: error.message }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return handleRouteError(error, set)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function missingAgentStorageResponse(set: { status?: number | string }): { error: string } {
|
|
240
|
+
return unconfiguredStorageResponse(set, "Agent storage")
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async function agentMessageFileContentResponse(
|
|
244
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
245
|
+
context: {
|
|
246
|
+
readonly params: { readonly threadId: string; readonly messageId: string }
|
|
247
|
+
readonly query: unknown
|
|
248
|
+
readonly request: Request
|
|
249
|
+
readonly set: { status?: number | string }
|
|
250
|
+
},
|
|
251
|
+
options: { readonly head?: boolean } = {}
|
|
252
|
+
) {
|
|
253
|
+
const authState = requestAuthState(context)
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
const storage = sixb.storage.agents
|
|
257
|
+
if (!storage) {
|
|
258
|
+
return missingAgentStorageResponse(context.set)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const parsed = AgentMessageFileContentQuerySchema.parse(context.query)
|
|
262
|
+
const thread = await getAgentMessageFileContentThread({
|
|
263
|
+
authState,
|
|
264
|
+
storage,
|
|
265
|
+
projectId: sixb.id,
|
|
266
|
+
threadId: context.params.threadId,
|
|
267
|
+
})
|
|
268
|
+
if (!thread) {
|
|
269
|
+
context.set.status = 404
|
|
270
|
+
return { error: "File not found" }
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const message = await storage.messages.getById({
|
|
274
|
+
projectId: sixb.id,
|
|
275
|
+
id: context.params.messageId,
|
|
276
|
+
})
|
|
277
|
+
if (!message || message.threadId !== thread.id) {
|
|
278
|
+
context.set.status = 404
|
|
279
|
+
return { error: "File not found" }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const fileRef = resolveFileRefAtPath(serializeMessage(message), parsed.path)
|
|
283
|
+
if (!fileRef) {
|
|
284
|
+
context.set.status = 404
|
|
285
|
+
return { error: "File not found" }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const response = await createFileContentResponse({
|
|
289
|
+
blobStorage: sixb.blobStorage,
|
|
290
|
+
fileRef,
|
|
291
|
+
disposition: parsed.disposition,
|
|
292
|
+
head: options.head,
|
|
293
|
+
rangeHeader: context.request.headers.get("range"),
|
|
294
|
+
})
|
|
295
|
+
if (!response) {
|
|
296
|
+
context.set.status = 404
|
|
297
|
+
return { error: "File not found" }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return response
|
|
301
|
+
} catch (error) {
|
|
302
|
+
if (error instanceof ZodError) {
|
|
303
|
+
context.set.status = 400
|
|
304
|
+
return { error: "Invalid file content query" }
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
throw error
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
312
|
+
return app
|
|
313
|
+
.get(
|
|
314
|
+
"/api/agents",
|
|
315
|
+
(context) => {
|
|
316
|
+
const { scoped } = requestAuthState(context)
|
|
317
|
+
const agents = scoped ? scoped.listAgents() : sixb.agents.list()
|
|
318
|
+
return agents.map(serializeAgent)
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
response: { 200: AgentCatalogItemSchema.array() },
|
|
322
|
+
detail: {
|
|
323
|
+
summary: "List registered agents",
|
|
324
|
+
tags: [OPENAPI_TAGS.agents.name],
|
|
325
|
+
operationId: "listAgents",
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
)
|
|
329
|
+
.get(
|
|
330
|
+
"/api/agents/:agentId",
|
|
331
|
+
(context) => {
|
|
332
|
+
const { params, set } = context
|
|
333
|
+
const { scoped } = requestAuthState(context)
|
|
334
|
+
const agent = scoped
|
|
335
|
+
? scoped.getAgentById(params.agentId)
|
|
336
|
+
: sixb.agents.getById(params.agentId)
|
|
337
|
+
if (!agent) {
|
|
338
|
+
set.status = 404
|
|
339
|
+
return { error: "Agent not found" }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return serializeAgent(agent)
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
params: AgentIdParamsSchema,
|
|
346
|
+
response: { 200: AgentCatalogItemSchema, 404: ErrorResponseSchema },
|
|
347
|
+
detail: {
|
|
348
|
+
summary: "Get agent metadata",
|
|
349
|
+
tags: [OPENAPI_TAGS.agents.name],
|
|
350
|
+
operationId: "getAgent",
|
|
351
|
+
},
|
|
352
|
+
}
|
|
353
|
+
)
|
|
354
|
+
.get(
|
|
355
|
+
"/api/agent-threads",
|
|
356
|
+
async (context) => {
|
|
357
|
+
const { query, set } = context
|
|
358
|
+
const { scoped } = requestAuthState(context)
|
|
359
|
+
try {
|
|
360
|
+
const storage = sixb.storage.agents
|
|
361
|
+
if (!storage) {
|
|
362
|
+
return missingAgentStorageResponse(set)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const parsed = AgentThreadListQuerySchema.parse(query)
|
|
366
|
+
const listInput = {
|
|
367
|
+
agentId: parsed.agentId,
|
|
368
|
+
statuses: parsed.status ? [parsed.status] : undefined,
|
|
369
|
+
limit: parseOptionalInt(parsed.limit),
|
|
370
|
+
offset: parseOptionalInt(parsed.offset),
|
|
371
|
+
order: parsed.order,
|
|
372
|
+
}
|
|
373
|
+
// Scoped principals list through the core scoped surface (owner + run:agent filtered);
|
|
374
|
+
// a null scope is privileged (auth disabled), so it lists unfiltered.
|
|
375
|
+
const result = scoped
|
|
376
|
+
? await scoped.listThreads(listInput)
|
|
377
|
+
: await storage.threads.list({ projectId: sixb.id, ...listInput })
|
|
378
|
+
|
|
379
|
+
return AgentThreadListResponseSchema.parse({
|
|
380
|
+
threads: result.threads.map(serializeThread),
|
|
381
|
+
hasMore: result.hasMore,
|
|
382
|
+
total: result.total,
|
|
383
|
+
})
|
|
384
|
+
} catch (error) {
|
|
385
|
+
return handleAgentRouteError(error, set)
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
query: AgentThreadListQuerySchema,
|
|
390
|
+
response: {
|
|
391
|
+
200: AgentThreadListResponseSchema,
|
|
392
|
+
400: ErrorResponseSchema,
|
|
393
|
+
501: ErrorResponseSchema,
|
|
394
|
+
},
|
|
395
|
+
detail: {
|
|
396
|
+
summary: "List agent threads",
|
|
397
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
398
|
+
operationId: "listAgentThreads",
|
|
399
|
+
},
|
|
400
|
+
}
|
|
401
|
+
)
|
|
402
|
+
.post(
|
|
403
|
+
"/api/agent-threads",
|
|
404
|
+
async (context) => {
|
|
405
|
+
const { body, set } = context
|
|
406
|
+
const { authz, scoped } = requestAuthState(context)
|
|
407
|
+
try {
|
|
408
|
+
const parsed = CreateAgentThreadBodySchema.parse(body)
|
|
409
|
+
// scoped.getAgentById returns null for agents the principal cannot run, so an ungranted
|
|
410
|
+
// agent 404s (not found) instead of 403 — the latter would disclose that the id exists.
|
|
411
|
+
const agent = scoped
|
|
412
|
+
? scoped.getAgentById(parsed.agentId)
|
|
413
|
+
: sixb.agents.getById(parsed.agentId)
|
|
414
|
+
if (!agent) {
|
|
415
|
+
set.status = 404
|
|
416
|
+
return { error: "Agent not found" }
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const storage = sixb.storage.agents
|
|
420
|
+
if (!storage) {
|
|
421
|
+
return missingAgentStorageResponse(set)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const thread = await storage.threads.create({
|
|
425
|
+
id: parsed.threadId ?? createAgentThreadId(),
|
|
426
|
+
projectId: sixb.id,
|
|
427
|
+
agentId: agent.id,
|
|
428
|
+
ownerPrincipal: principalForRequest(authz),
|
|
429
|
+
...(parsed.title === undefined ? {} : { title: parsed.title }),
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
set.status = 201
|
|
433
|
+
return CreateAgentThreadResponseSchema.parse({ thread: serializeThread(thread) })
|
|
434
|
+
} catch (error) {
|
|
435
|
+
return handleAgentRouteError(error, set)
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
body: CreateAgentThreadBodySchema,
|
|
440
|
+
response: {
|
|
441
|
+
201: CreateAgentThreadResponseSchema,
|
|
442
|
+
400: ErrorResponseSchema,
|
|
443
|
+
// No 403: an agent the caller cannot run is reported as 404 (see getAgentById above) so the
|
|
444
|
+
// response never discloses that a forbidden id exists.
|
|
445
|
+
404: ErrorResponseSchema,
|
|
446
|
+
409: ErrorResponseSchema,
|
|
447
|
+
501: ErrorResponseSchema,
|
|
448
|
+
},
|
|
449
|
+
detail: {
|
|
450
|
+
summary: "Create an agent thread",
|
|
451
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
452
|
+
operationId: "createAgentThread",
|
|
453
|
+
security: SIXB_CSRF_SECURITY_REQUIREMENT,
|
|
454
|
+
},
|
|
455
|
+
}
|
|
456
|
+
)
|
|
457
|
+
.get(
|
|
458
|
+
"/api/agent-threads/:threadId",
|
|
459
|
+
async (context) => {
|
|
460
|
+
const { params, set } = context
|
|
461
|
+
const { scoped } = requestAuthState(context)
|
|
462
|
+
try {
|
|
463
|
+
const storage = sixb.storage.agents
|
|
464
|
+
if (!storage) {
|
|
465
|
+
return missingAgentStorageResponse(set)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const thread = await getAccessibleThread({
|
|
469
|
+
scoped,
|
|
470
|
+
storage,
|
|
471
|
+
projectId: sixb.id,
|
|
472
|
+
threadId: params.threadId,
|
|
473
|
+
})
|
|
474
|
+
if (!thread) {
|
|
475
|
+
set.status = 404
|
|
476
|
+
return { error: "Agent thread not found" }
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
return serializeThread(thread)
|
|
480
|
+
} catch (error) {
|
|
481
|
+
return handleAgentRouteError(error, set)
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
params: AgentThreadParamsSchema,
|
|
486
|
+
response: {
|
|
487
|
+
200: AgentThreadSchema,
|
|
488
|
+
400: ErrorResponseSchema,
|
|
489
|
+
404: ErrorResponseSchema,
|
|
490
|
+
501: ErrorResponseSchema,
|
|
491
|
+
},
|
|
492
|
+
detail: {
|
|
493
|
+
summary: "Get agent thread",
|
|
494
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
495
|
+
operationId: "getAgentThread",
|
|
496
|
+
},
|
|
497
|
+
}
|
|
498
|
+
)
|
|
499
|
+
.get(
|
|
500
|
+
"/api/agent-threads/:threadId/messages",
|
|
501
|
+
async (context) => {
|
|
502
|
+
const { params, query, set } = context
|
|
503
|
+
const { scoped } = requestAuthState(context)
|
|
504
|
+
try {
|
|
505
|
+
const storage = sixb.storage.agents
|
|
506
|
+
if (!storage) {
|
|
507
|
+
return missingAgentStorageResponse(set)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const thread = await getAccessibleThread({
|
|
511
|
+
scoped,
|
|
512
|
+
storage,
|
|
513
|
+
projectId: sixb.id,
|
|
514
|
+
threadId: params.threadId,
|
|
515
|
+
})
|
|
516
|
+
if (!thread) {
|
|
517
|
+
set.status = 404
|
|
518
|
+
return { error: "Agent thread not found" }
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const parsed = AgentMessagesQuerySchema.parse(query)
|
|
522
|
+
const limit = parseOptionalInt(parsed.limit)
|
|
523
|
+
const offset = parseOptionalInt(parsed.offset)
|
|
524
|
+
const result = await storage.messages.list({
|
|
525
|
+
projectId: sixb.id,
|
|
526
|
+
threadId: thread.id,
|
|
527
|
+
roles: parsed.role ? [parsed.role] : undefined,
|
|
528
|
+
limit,
|
|
529
|
+
offset,
|
|
530
|
+
order: parsed.order,
|
|
531
|
+
})
|
|
532
|
+
const runIds = result.messages.flatMap((message) =>
|
|
533
|
+
message.runId === null ? [] : [message.runId]
|
|
534
|
+
)
|
|
535
|
+
const runs = await storage.runs.getByIds({ projectId: sixb.id, ids: runIds })
|
|
536
|
+
const diagnosticsByRunId = new Map(
|
|
537
|
+
runs.map((run) => [run.id, run.diagnostics ?? []] as const)
|
|
538
|
+
)
|
|
539
|
+
|
|
540
|
+
return AgentMessageListResponseSchema.parse({
|
|
541
|
+
messages: result.messages.map((message) =>
|
|
542
|
+
serializeMessage(
|
|
543
|
+
message,
|
|
544
|
+
message.runId === null ? [] : (diagnosticsByRunId.get(message.runId) ?? [])
|
|
545
|
+
)
|
|
546
|
+
),
|
|
547
|
+
hasMore: result.hasMore,
|
|
548
|
+
total: result.total,
|
|
549
|
+
})
|
|
550
|
+
} catch (error) {
|
|
551
|
+
return handleAgentRouteError(error, set)
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
params: AgentThreadParamsSchema,
|
|
556
|
+
query: AgentMessagesQuerySchema,
|
|
557
|
+
response: {
|
|
558
|
+
200: AgentMessageListResponseSchema,
|
|
559
|
+
400: ErrorResponseSchema,
|
|
560
|
+
404: ErrorResponseSchema,
|
|
561
|
+
501: ErrorResponseSchema,
|
|
562
|
+
},
|
|
563
|
+
detail: {
|
|
564
|
+
summary: "List agent thread messages",
|
|
565
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
566
|
+
operationId: "listAgentThreadMessages",
|
|
567
|
+
},
|
|
568
|
+
}
|
|
569
|
+
)
|
|
570
|
+
.get(
|
|
571
|
+
"/api/agent-threads/:threadId/messages/:messageId/files/content",
|
|
572
|
+
async (context) => agentMessageFileContentResponse(sixb, context),
|
|
573
|
+
{
|
|
574
|
+
params: AgentMessageFileContentParamsSchema,
|
|
575
|
+
// Keep framework validation loose so invalid roots return the compact route-level 400 body.
|
|
576
|
+
query: FileContentQuerySchema,
|
|
577
|
+
detail: {
|
|
578
|
+
summary: "Get agent message file content",
|
|
579
|
+
tags: ["Agents"],
|
|
580
|
+
operationId: "getAgentMessageFileContent",
|
|
581
|
+
security: bearerSecurityRequirement("getAgentMessageFileContent"),
|
|
582
|
+
responses: fileContentGetResponses({ optionalStorage: true }),
|
|
583
|
+
},
|
|
584
|
+
}
|
|
585
|
+
)
|
|
586
|
+
.head(
|
|
587
|
+
"/api/agent-threads/:threadId/messages/:messageId/files/content",
|
|
588
|
+
async (context) => agentMessageFileContentResponse(sixb, context, { head: true }),
|
|
589
|
+
{
|
|
590
|
+
params: AgentMessageFileContentParamsSchema,
|
|
591
|
+
query: FileContentQuerySchema,
|
|
592
|
+
detail: {
|
|
593
|
+
summary: "Head agent message file content",
|
|
594
|
+
tags: ["Agents"],
|
|
595
|
+
operationId: "headAgentMessageFileContent",
|
|
596
|
+
security: bearerSecurityRequirement("headAgentMessageFileContent"),
|
|
597
|
+
responses: fileContentHeadResponses({ optionalStorage: true }),
|
|
598
|
+
},
|
|
599
|
+
}
|
|
600
|
+
)
|
|
601
|
+
.post(
|
|
602
|
+
"/api/agent-threads/:threadId/messages",
|
|
603
|
+
async (context) => {
|
|
604
|
+
const { params, body, set } = context
|
|
605
|
+
const { authz, scoped } = requestAuthState(context)
|
|
606
|
+
try {
|
|
607
|
+
const storage = sixb.storage.agents
|
|
608
|
+
if (!storage) {
|
|
609
|
+
return missingAgentStorageResponse(set)
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const thread = await getAccessibleThread({
|
|
613
|
+
scoped,
|
|
614
|
+
storage,
|
|
615
|
+
projectId: sixb.id,
|
|
616
|
+
threadId: params.threadId,
|
|
617
|
+
})
|
|
618
|
+
if (!thread) {
|
|
619
|
+
set.status = 404
|
|
620
|
+
return { error: "Agent thread not found" }
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
const parsed = PostAgentMessageBodySchema.parse(body)
|
|
624
|
+
const requestInput = {
|
|
625
|
+
agentId: thread.agentId,
|
|
626
|
+
threadId: thread.id,
|
|
627
|
+
text: parsed.text,
|
|
628
|
+
attachments: parsed.attachments as readonly FileRef[] | undefined,
|
|
629
|
+
context: parsed.context,
|
|
630
|
+
messageId: parsed.messageId,
|
|
631
|
+
principal: principalForRequest(authz),
|
|
632
|
+
}
|
|
633
|
+
const result = await (scoped
|
|
634
|
+
? scoped.requestAgentRun(requestInput)
|
|
635
|
+
: sixb.agents.request(requestInput))
|
|
636
|
+
|
|
637
|
+
set.status = 202
|
|
638
|
+
return PostAgentMessageResponseSchema.parse({ run: serializeAgentRun(result.run) })
|
|
639
|
+
} catch (error) {
|
|
640
|
+
return handleAgentRouteError(error, set)
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
{
|
|
644
|
+
params: AgentThreadParamsSchema,
|
|
645
|
+
body: PostAgentMessageBodySchema,
|
|
646
|
+
response: {
|
|
647
|
+
202: PostAgentMessageResponseSchema,
|
|
648
|
+
400: ErrorResponseSchema,
|
|
649
|
+
403: ErrorResponseSchema,
|
|
650
|
+
404: ErrorResponseSchema,
|
|
651
|
+
409: ErrorResponseSchema,
|
|
652
|
+
501: ErrorResponseSchema,
|
|
653
|
+
},
|
|
654
|
+
detail: {
|
|
655
|
+
summary: "Post an agent thread message",
|
|
656
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
657
|
+
operationId: "postAgentThreadMessage",
|
|
658
|
+
security: SIXB_CSRF_SECURITY_REQUIREMENT,
|
|
659
|
+
},
|
|
660
|
+
}
|
|
661
|
+
)
|
|
662
|
+
.post(
|
|
663
|
+
"/api/agent-threads/:threadId/cancel",
|
|
664
|
+
async (context) => {
|
|
665
|
+
const { params, body, set } = context
|
|
666
|
+
const { scoped } = requestAuthState(context)
|
|
667
|
+
try {
|
|
668
|
+
const storage = sixb.storage.agents
|
|
669
|
+
if (!storage) {
|
|
670
|
+
return missingAgentStorageResponse(set)
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const thread = await getAccessibleThread({
|
|
674
|
+
scoped,
|
|
675
|
+
storage,
|
|
676
|
+
projectId: sixb.id,
|
|
677
|
+
threadId: params.threadId,
|
|
678
|
+
})
|
|
679
|
+
if (!thread) {
|
|
680
|
+
set.status = 404
|
|
681
|
+
return { error: "Agent thread not found" }
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const parsed = CancelAgentRunBodySchema.parse(body)
|
|
685
|
+
const run = await storage.runs.getById({ projectId: sixb.id, id: parsed.runId })
|
|
686
|
+
if (!run || run.threadId !== thread.id) {
|
|
687
|
+
set.status = 404
|
|
688
|
+
return { error: "Agent run not found" }
|
|
689
|
+
}
|
|
690
|
+
let current = run
|
|
691
|
+
let cancelledWhileQueued = false
|
|
692
|
+
if (current.status === "queued") {
|
|
693
|
+
try {
|
|
694
|
+
current = await storage.runs.finishQueued({
|
|
695
|
+
projectId: sixb.id,
|
|
696
|
+
id: current.id,
|
|
697
|
+
status: "cancelled",
|
|
698
|
+
})
|
|
699
|
+
cancelledWhileQueued = true
|
|
700
|
+
} catch (error) {
|
|
701
|
+
if (!(error instanceof AgentStorageError) || error.code !== "invalid_state") {
|
|
702
|
+
throw error
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// The worker may have started the run after our read but before finishQueued locked
|
|
706
|
+
// it. Re-read so that pickup race becomes a running-run cancellation instead of an
|
|
707
|
+
// invalid-state response. A retained control record is safe even if the worker has
|
|
708
|
+
// not attached its cancel subscription yet.
|
|
709
|
+
const refreshed = await storage.runs.getById({ projectId: sixb.id, id: current.id })
|
|
710
|
+
if (!refreshed || refreshed.threadId !== thread.id) {
|
|
711
|
+
set.status = 404
|
|
712
|
+
return { error: "Agent run not found" }
|
|
713
|
+
}
|
|
714
|
+
current = refreshed
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
if (cancelledWhileQueued) {
|
|
719
|
+
await publishQueuedRunCancellation(sixb, current)
|
|
720
|
+
} else if (current.status === "running") {
|
|
721
|
+
await publishAgentRunCancel(sixb.broker, { projectId: sixb.id, runId: current.id })
|
|
722
|
+
} else {
|
|
723
|
+
set.status = 409
|
|
724
|
+
return { error: "Agent run is not active" }
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
set.status = 202
|
|
728
|
+
return CancelAgentRunResponseSchema.parse({ run: serializeAgentRun(current) })
|
|
729
|
+
} catch (error) {
|
|
730
|
+
return handleAgentRouteError(error, set)
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
{
|
|
734
|
+
params: AgentThreadParamsSchema,
|
|
735
|
+
body: CancelAgentRunBodySchema,
|
|
736
|
+
response: {
|
|
737
|
+
202: CancelAgentRunResponseSchema,
|
|
738
|
+
400: ErrorResponseSchema,
|
|
739
|
+
404: ErrorResponseSchema,
|
|
740
|
+
409: ErrorResponseSchema,
|
|
741
|
+
501: ErrorResponseSchema,
|
|
742
|
+
},
|
|
743
|
+
detail: {
|
|
744
|
+
summary: "Cancel an agent thread's active run",
|
|
745
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
746
|
+
operationId: "cancelAgentRun",
|
|
747
|
+
security: SIXB_CSRF_SECURITY_REQUIREMENT,
|
|
748
|
+
},
|
|
749
|
+
}
|
|
750
|
+
)
|
|
751
|
+
.post(
|
|
752
|
+
"/api/agent-threads/:threadId/runs/:runId/retry",
|
|
753
|
+
async (context) => {
|
|
754
|
+
const { params, set } = context
|
|
755
|
+
const { authz, scoped } = requestAuthState(context)
|
|
756
|
+
try {
|
|
757
|
+
const storage = sixb.storage.agents
|
|
758
|
+
if (!storage) {
|
|
759
|
+
return missingAgentStorageResponse(set)
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const thread = await getAccessibleThread({
|
|
763
|
+
scoped,
|
|
764
|
+
storage,
|
|
765
|
+
projectId: sixb.id,
|
|
766
|
+
threadId: params.threadId,
|
|
767
|
+
})
|
|
768
|
+
if (!thread) {
|
|
769
|
+
set.status = 404
|
|
770
|
+
return { error: "Agent thread not found" }
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const failedRun = await storage.runs.getById({ projectId: sixb.id, id: params.runId })
|
|
774
|
+
if (!failedRun || failedRun.threadId !== thread.id) {
|
|
775
|
+
set.status = 404
|
|
776
|
+
return { error: "Agent run not found" }
|
|
777
|
+
}
|
|
778
|
+
if (failedRun.status !== "failed") {
|
|
779
|
+
set.status = 409
|
|
780
|
+
return { error: "Only failed agent runs can be retried" }
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const run = await storage.runs.create({
|
|
784
|
+
id: createAgentRunId(),
|
|
785
|
+
projectId: sixb.id,
|
|
786
|
+
threadId: thread.id,
|
|
787
|
+
agentId: failedRun.agentId,
|
|
788
|
+
triggerMessageId: failedRun.triggerMessageId,
|
|
789
|
+
requestedByPrincipal: principalForRequest(authz),
|
|
790
|
+
})
|
|
791
|
+
|
|
792
|
+
// As with a new message, the durable queued run is the dispatch intent. A worker will
|
|
793
|
+
// reconcile it if this best-effort publication cannot reach the queue right now.
|
|
794
|
+
try {
|
|
795
|
+
const dispatch = await dispatchQueuedAgentRuns({
|
|
796
|
+
projectId: sixb.id,
|
|
797
|
+
storage,
|
|
798
|
+
queue: sixb.queues.agents,
|
|
799
|
+
runIds: [run.id],
|
|
800
|
+
})
|
|
801
|
+
const failure = dispatch.failures[0]
|
|
802
|
+
if (failure) {
|
|
803
|
+
console.error(
|
|
804
|
+
`[SixbServer] Could not dispatch retried agent run '${run.id}'; retrying later.`,
|
|
805
|
+
failure.error
|
|
806
|
+
)
|
|
807
|
+
}
|
|
808
|
+
} catch (error) {
|
|
809
|
+
console.error(
|
|
810
|
+
`[SixbServer] Could not dispatch retried agent run '${run.id}'; retrying later.`,
|
|
811
|
+
error
|
|
812
|
+
)
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
set.status = 202
|
|
816
|
+
return RetryAgentRunResponseSchema.parse({ run: serializeAgentRun(run) })
|
|
817
|
+
} catch (error) {
|
|
818
|
+
return handleAgentRouteError(error, set)
|
|
819
|
+
}
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
params: AgentRunParamsSchema.extend({ threadId: z.string().trim().min(1) }),
|
|
823
|
+
response: {
|
|
824
|
+
202: RetryAgentRunResponseSchema,
|
|
825
|
+
400: ErrorResponseSchema,
|
|
826
|
+
404: ErrorResponseSchema,
|
|
827
|
+
409: ErrorResponseSchema,
|
|
828
|
+
501: ErrorResponseSchema,
|
|
829
|
+
},
|
|
830
|
+
detail: {
|
|
831
|
+
summary: "Retry a failed agent run",
|
|
832
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
833
|
+
operationId: "retryAgentRun",
|
|
834
|
+
security: SIXB_CSRF_SECURITY_REQUIREMENT,
|
|
835
|
+
},
|
|
836
|
+
}
|
|
837
|
+
)
|
|
838
|
+
.get(
|
|
839
|
+
"/api/agent-threads/:threadId/runs",
|
|
840
|
+
async (context) => {
|
|
841
|
+
const { params, query, set } = context
|
|
842
|
+
const { scoped } = requestAuthState(context)
|
|
843
|
+
try {
|
|
844
|
+
const storage = sixb.storage.agents
|
|
845
|
+
if (!storage) {
|
|
846
|
+
return missingAgentStorageResponse(set)
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
const thread = await getAccessibleThread({
|
|
850
|
+
scoped,
|
|
851
|
+
storage,
|
|
852
|
+
projectId: sixb.id,
|
|
853
|
+
threadId: params.threadId,
|
|
854
|
+
})
|
|
855
|
+
if (!thread) {
|
|
856
|
+
set.status = 404
|
|
857
|
+
return { error: "Agent thread not found" }
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
const parsed = AgentRunListQuerySchema.parse(query)
|
|
861
|
+
const result = await storage.runs.list({
|
|
862
|
+
projectId: sixb.id,
|
|
863
|
+
threadId: thread.id,
|
|
864
|
+
statuses: parsed.status ? [parsed.status] : undefined,
|
|
865
|
+
limit: parseOptionalInt(parsed.limit),
|
|
866
|
+
offset: parseOptionalInt(parsed.offset),
|
|
867
|
+
order: parsed.order,
|
|
868
|
+
})
|
|
869
|
+
|
|
870
|
+
return AgentRunListResponseSchema.parse({
|
|
871
|
+
runs: result.runs.map(serializeAgentRun),
|
|
872
|
+
hasMore: result.hasMore,
|
|
873
|
+
total: result.total,
|
|
874
|
+
})
|
|
875
|
+
} catch (error) {
|
|
876
|
+
return handleAgentRouteError(error, set)
|
|
877
|
+
}
|
|
878
|
+
},
|
|
879
|
+
{
|
|
880
|
+
params: AgentThreadParamsSchema,
|
|
881
|
+
query: AgentRunListQuerySchema,
|
|
882
|
+
response: {
|
|
883
|
+
200: AgentRunListResponseSchema,
|
|
884
|
+
400: ErrorResponseSchema,
|
|
885
|
+
404: ErrorResponseSchema,
|
|
886
|
+
501: ErrorResponseSchema,
|
|
887
|
+
},
|
|
888
|
+
detail: {
|
|
889
|
+
summary: "List an agent thread's runs",
|
|
890
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
891
|
+
operationId: "listAgentThreadRuns",
|
|
892
|
+
},
|
|
893
|
+
}
|
|
894
|
+
)
|
|
895
|
+
.get(
|
|
896
|
+
"/api/agent-runs/:runId",
|
|
897
|
+
async (context) => {
|
|
898
|
+
const { params, set } = context
|
|
899
|
+
const { scoped } = requestAuthState(context)
|
|
900
|
+
try {
|
|
901
|
+
const storage = sixb.storage.agents
|
|
902
|
+
if (!storage) {
|
|
903
|
+
return missingAgentStorageResponse(set)
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const run = await storage.runs.getById({ projectId: sixb.id, id: params.runId })
|
|
907
|
+
if (!run) {
|
|
908
|
+
set.status = 404
|
|
909
|
+
return { error: "Agent run not found" }
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
const thread = await getAccessibleThread({
|
|
913
|
+
scoped,
|
|
914
|
+
storage,
|
|
915
|
+
projectId: sixb.id,
|
|
916
|
+
threadId: run.threadId,
|
|
917
|
+
})
|
|
918
|
+
if (!thread) {
|
|
919
|
+
set.status = 404
|
|
920
|
+
return { error: "Agent run not found" }
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
return serializeAgentRun(run)
|
|
924
|
+
} catch (error) {
|
|
925
|
+
return handleAgentRouteError(error, set)
|
|
926
|
+
}
|
|
927
|
+
},
|
|
928
|
+
{
|
|
929
|
+
params: AgentRunParamsSchema,
|
|
930
|
+
response: {
|
|
931
|
+
200: AgentRunSchema,
|
|
932
|
+
400: ErrorResponseSchema,
|
|
933
|
+
404: ErrorResponseSchema,
|
|
934
|
+
501: ErrorResponseSchema,
|
|
935
|
+
},
|
|
936
|
+
detail: {
|
|
937
|
+
summary: "Get agent run",
|
|
938
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
939
|
+
operationId: "getAgentRun",
|
|
940
|
+
},
|
|
941
|
+
}
|
|
942
|
+
)
|
|
943
|
+
}
|