@sixb/server 0.1.1 → 0.1.3

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