@sixb/server 0.1.1 → 0.1.2
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/README.md +3 -3
- package/dist/auth/application-access.d.ts +2 -2
- package/dist/auth/application-access.d.ts.map +1 -1
- package/dist/auth/connector-callback-cookie.d.ts +12 -0
- package/dist/auth/connector-callback-cookie.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +3 -3
- package/dist/auth/guard.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/scope.d.ts +13 -27
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/auth/session-cookies.d.ts +2 -2
- package/dist/auth/session-cookies.d.ts.map +1 -1
- package/dist/connectors/http.d.ts +16 -0
- package/dist/connectors/http.d.ts.map +1 -0
- package/dist/index.js +3293 -2704
- package/dist/index.js.map +55 -50
- package/dist/openapi/tags.d.ts +11 -3
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +6 -2
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/action-runs.d.ts +21 -31
- package/dist/routes/action-runs.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +9 -8
- package/dist/routes/actions.d.ts.map +1 -1
- package/dist/routes/agent-api-gateway.d.ts +2 -2
- package/dist/routes/agent-api-gateway.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +130 -131
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +84 -84
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/connector-connection-runs.d.ts +794 -0
- package/dist/routes/connector-connection-runs.d.ts.map +1 -0
- package/dist/routes/connector-connections.d.ts +219 -0
- package/dist/routes/connector-connections.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +10 -4
- package/dist/routes/connectors.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +8 -4
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +8 -8
- package/dist/routes/events.d.ts.map +1 -1
- package/dist/routes/files.d.ts +11 -10
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/links.d.ts +2 -2
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +5 -5
- package/dist/routes/logs.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +34 -34
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +50 -50
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +24 -34
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/project.d.ts +2 -2
- package/dist/routes/project.d.ts.map +1 -1
- package/dist/routes/projections.d.ts +131 -83
- package/dist/routes/projections.d.ts.map +1 -1
- package/dist/routes/rules.d.ts +6 -6
- package/dist/routes/rules.d.ts.map +1 -1
- package/dist/routes/status.d.ts +2 -2
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/syncs.d.ts +20 -25
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/telemetry.d.ts +2 -2
- package/dist/routes/telemetry.d.ts.map +1 -1
- package/dist/routes/webhook-runs.d.ts +9 -9
- package/dist/routes/webhook-runs.d.ts.map +1 -1
- package/dist/routes/webhooks.d.ts +2 -2
- package/dist/routes/webhooks.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +74 -48
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +11 -5
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts +8 -8
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +10 -10
- package/dist/routes/ws/logs.d.ts.map +1 -1
- package/dist/schemas/actions.d.ts +97 -202
- package/dist/schemas/actions.d.ts.map +1 -1
- package/dist/schemas/agents.d.ts +442 -439
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-usage.d.ts +34 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +142 -142
- package/dist/schemas/common.d.ts +37 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/connectors.d.ts +713 -2
- package/dist/schemas/connectors.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +2 -2
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +34 -34
- package/dist/schemas/logs.d.ts +22 -22
- package/dist/schemas/objects.d.ts +10 -10
- package/dist/schemas/ontology.d.ts +118 -118
- package/dist/schemas/pipelines.d.ts +95 -190
- package/dist/schemas/pipelines.d.ts.map +1 -1
- package/dist/schemas/projections.d.ts +918 -563
- package/dist/schemas/projections.d.ts.map +1 -1
- package/dist/schemas/rules.d.ts +6 -6
- package/dist/schemas/status.d.ts +2 -2
- package/dist/schemas/syncs.d.ts +65 -108
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/webhook-runs.d.ts +41 -41
- package/dist/schemas/webhook-runs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +516 -183
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +6 -6
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/http.d.ts +2 -0
- package/dist/utils/http.d.ts.map +1 -1
- package/dist/utils/ws.d.ts +4 -3
- package/dist/utils/ws.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/auth/access-token-boundary.ts +4 -4
- package/src/auth/application-access.ts +8 -9
- package/src/auth/connector-callback-cookie.ts +95 -0
- package/src/auth/guard.ts +11 -11
- package/src/auth/public-routes.ts +13 -0
- package/src/auth/scope.ts +26 -36
- package/src/auth/session-cookies.ts +3 -3
- package/src/connectors/http.ts +110 -0
- package/src/openapi/tags.ts +9 -1
- package/src/registerRoutes.ts +34 -23
- package/src/routes/action-runs.ts +39 -39
- package/src/routes/actions.ts +7 -13
- package/src/routes/agent-api-gateway.ts +41 -38
- package/src/routes/agents.ts +89 -200
- package/src/routes/auth.ts +87 -91
- package/src/routes/connector-connection-runs.ts +346 -0
- package/src/routes/connector-connections.ts +135 -0
- package/src/routes/connectors.ts +24 -12
- package/src/routes/datasets.ts +55 -54
- package/src/routes/events.ts +5 -5
- package/src/routes/files.ts +22 -30
- package/src/routes/links.ts +31 -35
- package/src/routes/logs.ts +6 -11
- package/src/routes/objects.ts +51 -114
- package/src/routes/ontology.ts +27 -38
- package/src/routes/pipelines.ts +28 -62
- package/src/routes/project.ts +3 -3
- package/src/routes/projections.ts +26 -38
- package/src/routes/rules.ts +11 -25
- package/src/routes/status.ts +5 -5
- package/src/routes/syncs.ts +19 -45
- package/src/routes/telemetry.ts +18 -29
- package/src/routes/webhook-runs.ts +5 -5
- package/src/routes/webhooks.ts +259 -341
- package/src/routes/workflows.ts +226 -248
- package/src/routes/ws/agents.ts +196 -66
- package/src/routes/ws/events.ts +11 -10
- package/src/routes/ws/log-subscription-hub.ts +6 -12
- package/src/routes/ws/logs.ts +13 -5
- package/src/schemas/actions.ts +58 -25
- package/src/schemas/agents.ts +19 -14
- package/src/schemas/ai-usage.ts +16 -0
- package/src/schemas/common.ts +24 -0
- package/src/schemas/connectors.ts +164 -0
- package/src/schemas/pipelines.ts +23 -15
- package/src/schemas/projections.ts +17 -3
- package/src/schemas/syncs.ts +13 -8
- package/src/schemas/webhook-runs.ts +10 -5
- package/src/schemas/workflows.ts +16 -6
- package/src/server.ts +65 -26
- package/src/utils/http.ts +34 -6
- package/src/utils/ws.ts +10 -5
package/src/routes/agents.ts
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type AgentDefinition,
|
|
3
3
|
AgentRequestError,
|
|
4
|
-
type
|
|
4
|
+
type AgentRunView,
|
|
5
5
|
type FileRef,
|
|
6
|
-
type
|
|
7
|
-
type Principal,
|
|
8
|
-
type ScopedSixb,
|
|
9
|
-
type Sixb,
|
|
10
|
-
SYSTEM_PRINCIPAL,
|
|
6
|
+
type SixbHostView,
|
|
11
7
|
} from "@sixb/core"
|
|
12
8
|
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"
|
|
9
|
+
import { publishAgentRunCancel, publishAgentRunFinished } from "@sixb/core/internal/agents"
|
|
20
10
|
import {
|
|
21
11
|
type AgentMessageRecord,
|
|
22
12
|
type AgentRunDiagnostic,
|
|
@@ -28,7 +18,7 @@ import {
|
|
|
28
18
|
import type { Elysia } from "elysia"
|
|
29
19
|
import { ZodError, z } from "zod"
|
|
30
20
|
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
31
|
-
import { type RequestAuthState, requestAuthState } from "../auth/scope"
|
|
21
|
+
import { type RequestAuthState, requestAuthState, requireRequestSixb } from "../auth/scope"
|
|
32
22
|
import {
|
|
33
23
|
createFileContentResponse,
|
|
34
24
|
fileContentGetResponses,
|
|
@@ -125,15 +115,14 @@ function serializeMessage(
|
|
|
125
115
|
})
|
|
126
116
|
}
|
|
127
117
|
|
|
128
|
-
export function serializeAgentRun(run:
|
|
118
|
+
export function serializeAgentRun(run: AgentRunView): ReturnType<typeof AgentRunSchema.parse> {
|
|
129
119
|
return AgentRunSchema.parse({
|
|
130
120
|
id: run.id,
|
|
131
121
|
projectId: run.projectId,
|
|
132
122
|
threadId: run.threadId,
|
|
133
123
|
agentId: run.agentId,
|
|
134
124
|
triggerMessageId: run.triggerMessageId,
|
|
135
|
-
|
|
136
|
-
executionPrincipal: run.executionPrincipal,
|
|
125
|
+
requestedBy: run.requestedBy,
|
|
137
126
|
status: run.status,
|
|
138
127
|
modelId: run.modelId,
|
|
139
128
|
finishReason: run.finishReason,
|
|
@@ -148,20 +137,6 @@ export function serializeAgentRun(run: AgentRunRecord): ReturnType<typeof AgentR
|
|
|
148
137
|
})
|
|
149
138
|
}
|
|
150
139
|
|
|
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
140
|
async function getAgentMessageFileContentThread(params: {
|
|
166
141
|
readonly authState: RequestAuthState
|
|
167
142
|
readonly storage: AgentStorage
|
|
@@ -176,25 +151,19 @@ async function getAgentMessageFileContentThread(params: {
|
|
|
176
151
|
return params.storage.threads.getById({ projectId: params.projectId, id: params.threadId })
|
|
177
152
|
}
|
|
178
153
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
threadId: params.threadId,
|
|
184
|
-
})
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function principalForRequest(authz: AuthorizationContext | null): Principal {
|
|
188
|
-
return authz?.principal ?? SYSTEM_PRINCIPAL
|
|
154
|
+
if (!params.authState.sixb) {
|
|
155
|
+
throw new Error("[SixbServer] Execution scope is not available for this route.")
|
|
156
|
+
}
|
|
157
|
+
return params.authState.sixb.agents.threads.getById(params.threadId)
|
|
189
158
|
}
|
|
190
159
|
|
|
191
160
|
/** Publish the terminal record core owns; stream delivery stays best-effort at this boundary. */
|
|
192
161
|
async function publishQueuedRunCancellation(
|
|
193
|
-
|
|
162
|
+
host: SixbHostView,
|
|
194
163
|
run: AgentRunRecord
|
|
195
164
|
): Promise<void> {
|
|
196
165
|
try {
|
|
197
|
-
await publishAgentRunFinished(
|
|
166
|
+
await publishAgentRunFinished(host.broker, run)
|
|
198
167
|
} catch (error) {
|
|
199
168
|
console.error(`[SixbServer] Agent run '${run.id}' cancellation stream publish failed:`, error)
|
|
200
169
|
}
|
|
@@ -218,10 +187,12 @@ function handleAgentRouteError(
|
|
|
218
187
|
if (error instanceof AgentRequestError) {
|
|
219
188
|
switch (error.code) {
|
|
220
189
|
case "agent_not_found":
|
|
190
|
+
case "run_not_found":
|
|
221
191
|
case "thread_not_found":
|
|
222
192
|
set.status = 404
|
|
223
193
|
break
|
|
224
194
|
case "active_run_exists":
|
|
195
|
+
case "run_not_retryable":
|
|
225
196
|
set.status = 409
|
|
226
197
|
break
|
|
227
198
|
case "storage_unavailable":
|
|
@@ -241,7 +212,7 @@ function missingAgentStorageResponse(set: { status?: number | string }): { error
|
|
|
241
212
|
}
|
|
242
213
|
|
|
243
214
|
async function agentMessageFileContentResponse(
|
|
244
|
-
|
|
215
|
+
host: SixbHostView,
|
|
245
216
|
context: {
|
|
246
217
|
readonly params: { readonly threadId: string; readonly messageId: string }
|
|
247
218
|
readonly query: unknown
|
|
@@ -253,7 +224,7 @@ async function agentMessageFileContentResponse(
|
|
|
253
224
|
const authState = requestAuthState(context)
|
|
254
225
|
|
|
255
226
|
try {
|
|
256
|
-
const storage =
|
|
227
|
+
const storage = host.storage.agents
|
|
257
228
|
if (!storage) {
|
|
258
229
|
return missingAgentStorageResponse(context.set)
|
|
259
230
|
}
|
|
@@ -262,7 +233,7 @@ async function agentMessageFileContentResponse(
|
|
|
262
233
|
const thread = await getAgentMessageFileContentThread({
|
|
263
234
|
authState,
|
|
264
235
|
storage,
|
|
265
|
-
projectId:
|
|
236
|
+
projectId: host.id,
|
|
266
237
|
threadId: context.params.threadId,
|
|
267
238
|
})
|
|
268
239
|
if (!thread) {
|
|
@@ -271,7 +242,7 @@ async function agentMessageFileContentResponse(
|
|
|
271
242
|
}
|
|
272
243
|
|
|
273
244
|
const message = await storage.messages.getById({
|
|
274
|
-
projectId:
|
|
245
|
+
projectId: host.id,
|
|
275
246
|
id: context.params.messageId,
|
|
276
247
|
})
|
|
277
248
|
if (!message || message.threadId !== thread.id) {
|
|
@@ -286,7 +257,7 @@ async function agentMessageFileContentResponse(
|
|
|
286
257
|
}
|
|
287
258
|
|
|
288
259
|
const response = await createFileContentResponse({
|
|
289
|
-
blobStorage:
|
|
260
|
+
blobStorage: host.blobStorage,
|
|
290
261
|
fileRef,
|
|
291
262
|
disposition: parsed.disposition,
|
|
292
263
|
head: options.head,
|
|
@@ -308,14 +279,12 @@ async function agentMessageFileContentResponse(
|
|
|
308
279
|
}
|
|
309
280
|
}
|
|
310
281
|
|
|
311
|
-
export function registerAgentRoutes(app: Elysia,
|
|
282
|
+
export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
312
283
|
return app
|
|
313
284
|
.get(
|
|
314
285
|
"/api/agents",
|
|
315
286
|
(context) => {
|
|
316
|
-
|
|
317
|
-
const agents = scoped ? scoped.listAgents() : sixb.agents.list()
|
|
318
|
-
return agents.map(serializeAgent)
|
|
287
|
+
return requireRequestSixb(context).agents.list().map(serializeAgent)
|
|
319
288
|
},
|
|
320
289
|
{
|
|
321
290
|
response: { 200: AgentCatalogItemSchema.array() },
|
|
@@ -330,10 +299,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
330
299
|
"/api/agents/:agentId",
|
|
331
300
|
(context) => {
|
|
332
301
|
const { params, set } = context
|
|
333
|
-
const
|
|
334
|
-
const agent = scoped
|
|
335
|
-
? scoped.getAgentById(params.agentId)
|
|
336
|
-
: sixb.agents.getById(params.agentId)
|
|
302
|
+
const agent = requireRequestSixb(context).agents.getById(params.agentId)
|
|
337
303
|
if (!agent) {
|
|
338
304
|
set.status = 404
|
|
339
305
|
return { error: "Agent not found" }
|
|
@@ -355,9 +321,9 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
355
321
|
"/api/agent-threads",
|
|
356
322
|
async (context) => {
|
|
357
323
|
const { query, set } = context
|
|
358
|
-
const
|
|
324
|
+
const sixb = requireRequestSixb(context)
|
|
359
325
|
try {
|
|
360
|
-
const storage =
|
|
326
|
+
const storage = host.storage.agents
|
|
361
327
|
if (!storage) {
|
|
362
328
|
return missingAgentStorageResponse(set)
|
|
363
329
|
}
|
|
@@ -370,11 +336,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
370
336
|
offset: parseOptionalInt(parsed.offset),
|
|
371
337
|
order: parsed.order,
|
|
372
338
|
}
|
|
373
|
-
|
|
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 })
|
|
339
|
+
const result = await sixb.agents.threads.list(listInput)
|
|
378
340
|
|
|
379
341
|
return AgentThreadListResponseSchema.parse({
|
|
380
342
|
threads: result.threads.map(serializeThread),
|
|
@@ -403,29 +365,17 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
403
365
|
"/api/agent-threads",
|
|
404
366
|
async (context) => {
|
|
405
367
|
const { body, set } = context
|
|
406
|
-
const
|
|
368
|
+
const sixb = requireRequestSixb(context)
|
|
407
369
|
try {
|
|
408
370
|
const parsed = CreateAgentThreadBodySchema.parse(body)
|
|
409
|
-
|
|
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
|
|
371
|
+
const storage = host.storage.agents
|
|
420
372
|
if (!storage) {
|
|
421
373
|
return missingAgentStorageResponse(set)
|
|
422
374
|
}
|
|
423
375
|
|
|
424
|
-
const thread = await
|
|
425
|
-
id: parsed.threadId
|
|
426
|
-
|
|
427
|
-
agentId: agent.id,
|
|
428
|
-
ownerPrincipal: principalForRequest(authz),
|
|
376
|
+
const thread = await sixb.agents.threads.create({
|
|
377
|
+
...(parsed.threadId === undefined ? {} : { id: parsed.threadId }),
|
|
378
|
+
agentId: parsed.agentId,
|
|
429
379
|
...(parsed.title === undefined ? {} : { title: parsed.title }),
|
|
430
380
|
})
|
|
431
381
|
|
|
@@ -458,19 +408,14 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
458
408
|
"/api/agent-threads/:threadId",
|
|
459
409
|
async (context) => {
|
|
460
410
|
const { params, set } = context
|
|
461
|
-
const
|
|
411
|
+
const sixb = requireRequestSixb(context)
|
|
462
412
|
try {
|
|
463
|
-
const storage =
|
|
413
|
+
const storage = host.storage.agents
|
|
464
414
|
if (!storage) {
|
|
465
415
|
return missingAgentStorageResponse(set)
|
|
466
416
|
}
|
|
467
417
|
|
|
468
|
-
const thread = await
|
|
469
|
-
scoped,
|
|
470
|
-
storage,
|
|
471
|
-
projectId: sixb.id,
|
|
472
|
-
threadId: params.threadId,
|
|
473
|
-
})
|
|
418
|
+
const thread = await sixb.agents.threads.getById(params.threadId)
|
|
474
419
|
if (!thread) {
|
|
475
420
|
set.status = 404
|
|
476
421
|
return { error: "Agent thread not found" }
|
|
@@ -500,19 +445,14 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
500
445
|
"/api/agent-threads/:threadId/messages",
|
|
501
446
|
async (context) => {
|
|
502
447
|
const { params, query, set } = context
|
|
503
|
-
const
|
|
448
|
+
const sixb = requireRequestSixb(context)
|
|
504
449
|
try {
|
|
505
|
-
const storage =
|
|
450
|
+
const storage = host.storage.agents
|
|
506
451
|
if (!storage) {
|
|
507
452
|
return missingAgentStorageResponse(set)
|
|
508
453
|
}
|
|
509
454
|
|
|
510
|
-
const thread = await
|
|
511
|
-
scoped,
|
|
512
|
-
storage,
|
|
513
|
-
projectId: sixb.id,
|
|
514
|
-
threadId: params.threadId,
|
|
515
|
-
})
|
|
455
|
+
const thread = await sixb.agents.threads.getById(params.threadId)
|
|
516
456
|
if (!thread) {
|
|
517
457
|
set.status = 404
|
|
518
458
|
return { error: "Agent thread not found" }
|
|
@@ -522,7 +462,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
522
462
|
const limit = parseOptionalInt(parsed.limit)
|
|
523
463
|
const offset = parseOptionalInt(parsed.offset)
|
|
524
464
|
const result = await storage.messages.list({
|
|
525
|
-
projectId:
|
|
465
|
+
projectId: host.id,
|
|
526
466
|
threadId: thread.id,
|
|
527
467
|
roles: parsed.role ? [parsed.role] : undefined,
|
|
528
468
|
limit,
|
|
@@ -532,9 +472,11 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
532
472
|
const runIds = result.messages.flatMap((message) =>
|
|
533
473
|
message.runId === null ? [] : [message.runId]
|
|
534
474
|
)
|
|
535
|
-
const runs = await storage.runs.getByIds({ projectId:
|
|
475
|
+
const runs = await storage.runs.getByIds({ projectId: host.id, ids: runIds })
|
|
536
476
|
const diagnosticsByRunId = new Map(
|
|
537
|
-
runs
|
|
477
|
+
runs
|
|
478
|
+
.filter((run) => run.threadId === thread.id)
|
|
479
|
+
.map((run) => [run.id, run.diagnostics ?? []] as const)
|
|
538
480
|
)
|
|
539
481
|
|
|
540
482
|
return AgentMessageListResponseSchema.parse({
|
|
@@ -569,7 +511,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
569
511
|
)
|
|
570
512
|
.get(
|
|
571
513
|
"/api/agent-threads/:threadId/messages/:messageId/files/content",
|
|
572
|
-
async (context) => agentMessageFileContentResponse(
|
|
514
|
+
async (context) => agentMessageFileContentResponse(host, context),
|
|
573
515
|
{
|
|
574
516
|
params: AgentMessageFileContentParamsSchema,
|
|
575
517
|
// Keep framework validation loose so invalid roots return the compact route-level 400 body.
|
|
@@ -585,7 +527,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
585
527
|
)
|
|
586
528
|
.head(
|
|
587
529
|
"/api/agent-threads/:threadId/messages/:messageId/files/content",
|
|
588
|
-
async (context) => agentMessageFileContentResponse(
|
|
530
|
+
async (context) => agentMessageFileContentResponse(host, context, { head: true }),
|
|
589
531
|
{
|
|
590
532
|
params: AgentMessageFileContentParamsSchema,
|
|
591
533
|
query: FileContentQuerySchema,
|
|
@@ -602,19 +544,14 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
602
544
|
"/api/agent-threads/:threadId/messages",
|
|
603
545
|
async (context) => {
|
|
604
546
|
const { params, body, set } = context
|
|
605
|
-
const
|
|
547
|
+
const sixb = requireRequestSixb(context)
|
|
606
548
|
try {
|
|
607
|
-
const storage =
|
|
549
|
+
const storage = host.storage.agents
|
|
608
550
|
if (!storage) {
|
|
609
551
|
return missingAgentStorageResponse(set)
|
|
610
552
|
}
|
|
611
553
|
|
|
612
|
-
const thread = await
|
|
613
|
-
scoped,
|
|
614
|
-
storage,
|
|
615
|
-
projectId: sixb.id,
|
|
616
|
-
threadId: params.threadId,
|
|
617
|
-
})
|
|
554
|
+
const thread = await sixb.agents.threads.getById(params.threadId)
|
|
618
555
|
if (!thread) {
|
|
619
556
|
set.status = 404
|
|
620
557
|
return { error: "Agent thread not found" }
|
|
@@ -628,14 +565,13 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
628
565
|
attachments: parsed.attachments as readonly FileRef[] | undefined,
|
|
629
566
|
context: parsed.context,
|
|
630
567
|
messageId: parsed.messageId,
|
|
631
|
-
principal: principalForRequest(authz),
|
|
632
568
|
}
|
|
633
|
-
const result = await (
|
|
634
|
-
? scoped.requestAgentRun(requestInput)
|
|
635
|
-
: sixb.agents.request(requestInput))
|
|
569
|
+
const result = await sixb.agents.runs.request(requestInput)
|
|
636
570
|
|
|
637
571
|
set.status = 202
|
|
638
|
-
return PostAgentMessageResponseSchema.parse({
|
|
572
|
+
return PostAgentMessageResponseSchema.parse({
|
|
573
|
+
run: serializeAgentRun(result.run),
|
|
574
|
+
})
|
|
639
575
|
} catch (error) {
|
|
640
576
|
return handleAgentRouteError(error, set)
|
|
641
577
|
}
|
|
@@ -663,26 +599,21 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
663
599
|
"/api/agent-threads/:threadId/cancel",
|
|
664
600
|
async (context) => {
|
|
665
601
|
const { params, body, set } = context
|
|
666
|
-
const
|
|
602
|
+
const sixb = requireRequestSixb(context)
|
|
667
603
|
try {
|
|
668
|
-
const storage =
|
|
604
|
+
const storage = host.storage.agents
|
|
669
605
|
if (!storage) {
|
|
670
606
|
return missingAgentStorageResponse(set)
|
|
671
607
|
}
|
|
672
608
|
|
|
673
|
-
const thread = await
|
|
674
|
-
scoped,
|
|
675
|
-
storage,
|
|
676
|
-
projectId: sixb.id,
|
|
677
|
-
threadId: params.threadId,
|
|
678
|
-
})
|
|
609
|
+
const thread = await sixb.agents.threads.getById(params.threadId)
|
|
679
610
|
if (!thread) {
|
|
680
611
|
set.status = 404
|
|
681
612
|
return { error: "Agent thread not found" }
|
|
682
613
|
}
|
|
683
614
|
|
|
684
615
|
const parsed = CancelAgentRunBodySchema.parse(body)
|
|
685
|
-
const run = await
|
|
616
|
+
const run = await sixb.agents.runs.getById(parsed.runId)
|
|
686
617
|
if (!run || run.threadId !== thread.id) {
|
|
687
618
|
set.status = 404
|
|
688
619
|
return { error: "Agent run not found" }
|
|
@@ -692,7 +623,7 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
692
623
|
if (current.status === "queued") {
|
|
693
624
|
try {
|
|
694
625
|
current = await storage.runs.finishQueued({
|
|
695
|
-
projectId:
|
|
626
|
+
projectId: host.id,
|
|
696
627
|
id: current.id,
|
|
697
628
|
status: "cancelled",
|
|
698
629
|
})
|
|
@@ -706,8 +637,12 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
706
637
|
// it. Re-read so that pickup race becomes a running-run cancellation instead of an
|
|
707
638
|
// invalid-state response. A retained control record is safe even if the worker has
|
|
708
639
|
// not attached its cancel subscription yet.
|
|
709
|
-
const refreshed = await storage.runs.getById({ projectId:
|
|
710
|
-
if (
|
|
640
|
+
const refreshed = await storage.runs.getById({ projectId: host.id, id: current.id })
|
|
641
|
+
if (
|
|
642
|
+
!refreshed ||
|
|
643
|
+
refreshed.threadId !== thread.id ||
|
|
644
|
+
refreshed.agentId !== thread.agentId
|
|
645
|
+
) {
|
|
711
646
|
set.status = 404
|
|
712
647
|
return { error: "Agent run not found" }
|
|
713
648
|
}
|
|
@@ -716,16 +651,23 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
716
651
|
}
|
|
717
652
|
|
|
718
653
|
if (cancelledWhileQueued) {
|
|
719
|
-
await publishQueuedRunCancellation(
|
|
654
|
+
await publishQueuedRunCancellation(host, current)
|
|
720
655
|
} else if (current.status === "running") {
|
|
721
|
-
await publishAgentRunCancel(
|
|
656
|
+
await publishAgentRunCancel(host.broker, { projectId: host.id, runId: current.id })
|
|
722
657
|
} else {
|
|
723
658
|
set.status = 409
|
|
724
659
|
return { error: "Agent run is not active" }
|
|
725
660
|
}
|
|
726
661
|
|
|
727
662
|
set.status = 202
|
|
728
|
-
|
|
663
|
+
const currentView = await sixb.agents.runs.getById(current.id)
|
|
664
|
+
if (!currentView) {
|
|
665
|
+
set.status = 404
|
|
666
|
+
return { error: "Agent run not found" }
|
|
667
|
+
}
|
|
668
|
+
return CancelAgentRunResponseSchema.parse({
|
|
669
|
+
run: serializeAgentRun(currentView),
|
|
670
|
+
})
|
|
729
671
|
} catch (error) {
|
|
730
672
|
return handleAgentRouteError(error, set)
|
|
731
673
|
}
|
|
@@ -752,25 +694,20 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
752
694
|
"/api/agent-threads/:threadId/runs/:runId/retry",
|
|
753
695
|
async (context) => {
|
|
754
696
|
const { params, set } = context
|
|
755
|
-
const
|
|
697
|
+
const sixb = requireRequestSixb(context)
|
|
756
698
|
try {
|
|
757
|
-
const storage =
|
|
699
|
+
const storage = host.storage.agents
|
|
758
700
|
if (!storage) {
|
|
759
701
|
return missingAgentStorageResponse(set)
|
|
760
702
|
}
|
|
761
703
|
|
|
762
|
-
const thread = await
|
|
763
|
-
scoped,
|
|
764
|
-
storage,
|
|
765
|
-
projectId: sixb.id,
|
|
766
|
-
threadId: params.threadId,
|
|
767
|
-
})
|
|
704
|
+
const thread = await sixb.agents.threads.getById(params.threadId)
|
|
768
705
|
if (!thread) {
|
|
769
706
|
set.status = 404
|
|
770
707
|
return { error: "Agent thread not found" }
|
|
771
708
|
}
|
|
772
709
|
|
|
773
|
-
const failedRun = await
|
|
710
|
+
const failedRun = await sixb.agents.runs.getById(params.runId)
|
|
774
711
|
if (!failedRun || failedRun.threadId !== thread.id) {
|
|
775
712
|
set.status = 404
|
|
776
713
|
return { error: "Agent run not found" }
|
|
@@ -780,40 +717,12 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
780
717
|
return { error: "Only failed agent runs can be retried" }
|
|
781
718
|
}
|
|
782
719
|
|
|
783
|
-
const run = await
|
|
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
|
-
}
|
|
720
|
+
const { run } = await sixb.agents.runs.retry(failedRun.id)
|
|
814
721
|
|
|
815
722
|
set.status = 202
|
|
816
|
-
return RetryAgentRunResponseSchema.parse({
|
|
723
|
+
return RetryAgentRunResponseSchema.parse({
|
|
724
|
+
run: serializeAgentRun(run),
|
|
725
|
+
})
|
|
817
726
|
} catch (error) {
|
|
818
727
|
return handleAgentRouteError(error, set)
|
|
819
728
|
}
|
|
@@ -839,33 +748,24 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
839
748
|
"/api/agent-threads/:threadId/runs",
|
|
840
749
|
async (context) => {
|
|
841
750
|
const { params, query, set } = context
|
|
842
|
-
const
|
|
751
|
+
const sixb = requireRequestSixb(context)
|
|
843
752
|
try {
|
|
844
|
-
const storage =
|
|
753
|
+
const storage = host.storage.agents
|
|
845
754
|
if (!storage) {
|
|
846
755
|
return missingAgentStorageResponse(set)
|
|
847
756
|
}
|
|
848
757
|
|
|
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
758
|
const parsed = AgentRunListQuerySchema.parse(query)
|
|
861
|
-
const result = await
|
|
862
|
-
projectId: sixb.id,
|
|
863
|
-
threadId: thread.id,
|
|
759
|
+
const result = await sixb.agents.runs.listForThread(params.threadId, {
|
|
864
760
|
statuses: parsed.status ? [parsed.status] : undefined,
|
|
865
761
|
limit: parseOptionalInt(parsed.limit),
|
|
866
762
|
offset: parseOptionalInt(parsed.offset),
|
|
867
763
|
order: parsed.order,
|
|
868
764
|
})
|
|
765
|
+
if (!result) {
|
|
766
|
+
set.status = 404
|
|
767
|
+
return { error: "Agent thread not found" }
|
|
768
|
+
}
|
|
869
769
|
|
|
870
770
|
return AgentRunListResponseSchema.parse({
|
|
871
771
|
runs: result.runs.map(serializeAgentRun),
|
|
@@ -896,30 +796,19 @@ export function registerAgentRoutes(app: Elysia, sixb: Sixb<readonly OntologySou
|
|
|
896
796
|
"/api/agent-runs/:runId",
|
|
897
797
|
async (context) => {
|
|
898
798
|
const { params, set } = context
|
|
899
|
-
const
|
|
799
|
+
const sixb = requireRequestSixb(context)
|
|
900
800
|
try {
|
|
901
|
-
const storage =
|
|
801
|
+
const storage = host.storage.agents
|
|
902
802
|
if (!storage) {
|
|
903
803
|
return missingAgentStorageResponse(set)
|
|
904
804
|
}
|
|
905
805
|
|
|
906
|
-
const run = await
|
|
806
|
+
const run = await sixb.agents.runs.getById(params.runId)
|
|
907
807
|
if (!run) {
|
|
908
808
|
set.status = 404
|
|
909
809
|
return { error: "Agent run not found" }
|
|
910
810
|
}
|
|
911
811
|
|
|
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
812
|
return serializeAgentRun(run)
|
|
924
813
|
} catch (error) {
|
|
925
814
|
return handleAgentRouteError(error, set)
|