@sixb/server 0.2.5 → 0.2.6

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 (56) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +525 -61
  4. package/dist/index.js.map +19 -18
  5. package/dist/registerRoutes.d.ts +1 -0
  6. package/dist/registerRoutes.d.ts.map +1 -1
  7. package/dist/routes/agent-approvals.d.ts +22 -0
  8. package/dist/routes/agent-approvals.d.ts.map +1 -1
  9. package/dist/routes/agent-computers.d.ts +36 -0
  10. package/dist/routes/agent-computers.d.ts.map +1 -1
  11. package/dist/routes/agent-thread-reads.d.ts +82 -0
  12. package/dist/routes/agent-thread-reads.d.ts.map +1 -0
  13. package/dist/routes/agents.d.ts +783 -1
  14. package/dist/routes/agents.d.ts.map +1 -1
  15. package/dist/routes/ai-accounting.d.ts.map +1 -1
  16. package/dist/routes/model-generations.d.ts +6 -0
  17. package/dist/routes/model-generations.d.ts.map +1 -1
  18. package/dist/routes/observed-agent-threads.d.ts +22 -0
  19. package/dist/routes/observed-agent-threads.d.ts.map +1 -1
  20. package/dist/routes/status.d.ts +4 -1
  21. package/dist/routes/status.d.ts.map +1 -1
  22. package/dist/routes/syncs.d.ts +175 -12
  23. package/dist/routes/syncs.d.ts.map +1 -1
  24. package/dist/routes/workflows.d.ts +6 -6
  25. package/dist/routes/ws/agents.d.ts.map +1 -1
  26. package/dist/schemas/activity.d.ts +4 -4
  27. package/dist/schemas/agents.d.ts +2602 -6
  28. package/dist/schemas/agents.d.ts.map +1 -1
  29. package/dist/schemas/ai-accounting.d.ts +3 -0
  30. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  31. package/dist/schemas/common.d.ts.map +1 -1
  32. package/dist/schemas/models.d.ts +46 -0
  33. package/dist/schemas/models.d.ts.map +1 -1
  34. package/dist/schemas/syncs.d.ts +724 -25
  35. package/dist/schemas/syncs.d.ts.map +1 -1
  36. package/dist/schemas/workflows.d.ts +22 -22
  37. package/dist/server.d.ts +16 -1
  38. package/dist/server.d.ts.map +1 -1
  39. package/package.json +5 -5
  40. package/src/index.ts +6 -1
  41. package/src/registerRoutes.ts +4 -1
  42. package/src/routes/agent-computers.ts +7 -6
  43. package/src/routes/agent-thread-reads.ts +67 -0
  44. package/src/routes/agents.ts +129 -16
  45. package/src/routes/ai-accounting.ts +1 -0
  46. package/src/routes/model-generations.ts +1 -1
  47. package/src/routes/observed-agent-threads.ts +3 -4
  48. package/src/routes/status.ts +18 -1
  49. package/src/routes/syncs.ts +135 -8
  50. package/src/routes/ws/agents.ts +43 -3
  51. package/src/schemas/agents.ts +80 -10
  52. package/src/schemas/ai-accounting.ts +12 -0
  53. package/src/schemas/common.ts +15 -9
  54. package/src/schemas/models.ts +10 -0
  55. package/src/schemas/syncs.ts +63 -1
  56. package/src/server.ts +205 -13
@@ -1,4 +1,5 @@
1
1
  import {
2
+ type AgentMessagePart,
2
3
  AgentRequestError,
3
4
  type AgentRequestErrorCode,
4
5
  type AgentRunView,
@@ -53,7 +54,7 @@ import {
53
54
  AgentInboxListResponseSchema,
54
55
  AgentMessageFileContentParamsSchema,
55
56
  AgentMessageListResponseSchema,
56
- AgentMessageSchema,
57
+ type AgentMessageSchema,
57
58
  AgentMessagesQuerySchema,
58
59
  AgentRequestErrorResponseSchema,
59
60
  AgentRunListQuerySchema,
@@ -72,6 +73,8 @@ import {
72
73
  CancelAgentRunResponseSchema,
73
74
  CreateAgentThreadBodySchema,
74
75
  CreateAgentThreadResponseSchema,
76
+ EditAgentMessageBodySchema,
77
+ EditAgentMessageResponseSchema,
75
78
  PostAgentMessageBodySchema,
76
79
  PostAgentMessageResponseSchema,
77
80
  RetryAgentRunResponseSchema,
@@ -147,6 +150,23 @@ export async function serializeThreads(
147
150
  messageCount: thread.messageCount,
148
151
  createdAt: toIsoString(thread.createdAt),
149
152
  updatedAt: toIsoString(thread.updatedAt),
153
+ read: thread.read && {
154
+ threadId: thread.read.threadId,
155
+ throughSeq: thread.read.throughSeq,
156
+ headSeq: thread.read.headSeq,
157
+ unread: thread.read.unread,
158
+ readAt: thread.read.readAt ? toIsoString(thread.read.readAt) : undefined,
159
+ },
160
+ lastRun: thread.lastRun && {
161
+ id: thread.lastRun.id,
162
+ status: thread.lastRun.status,
163
+ finishReason: thread.lastRun.finishReason,
164
+ errorCode: thread.lastRun.errorCode,
165
+ completedAt: thread.lastRun.completedAt
166
+ ? toIsoString(thread.lastRun.completedAt)
167
+ : undefined,
168
+ requestedBy: thread.lastRun.requestedBy,
169
+ },
150
170
  })
151
171
  )
152
172
  }
@@ -228,12 +248,16 @@ export function sameBackgroundCommand(
228
248
  )
229
249
  }
230
250
 
251
+ /**
252
+ * A message as the API returns it, unchecked: the route's response schema checks it once, and drops
253
+ * what the schema does not name. Parsed here too, a thread's messages were checked three times.
254
+ */
231
255
  function serializeMessage(
232
256
  message: AgentMessageRecord,
233
257
  annotations: readonly AgentRunDiagnostic[] = [],
234
258
  checkpoint?: AgentContextCheckpointRecord
235
- ): ReturnType<typeof AgentMessageSchema.parse> {
236
- return AgentMessageSchema.parse({
259
+ ): z.infer<typeof AgentMessageSchema> {
260
+ return {
237
261
  id: message.id,
238
262
  projectId: message.projectId,
239
263
  threadId: message.threadId,
@@ -241,7 +265,7 @@ function serializeMessage(
241
265
  role: message.role,
242
266
  authorPrincipal: message.authorPrincipal,
243
267
  seq: message.seq,
244
- parts: message.parts,
268
+ parts: message.parts.map(withoutReplayState),
245
269
  annotations,
246
270
  compaction: checkpoint
247
271
  ? {
@@ -255,7 +279,20 @@ function serializeMessage(
255
279
  contentVersion: message.contentVersion,
256
280
  createdAt: toIsoString(message.createdAt),
257
281
  completedAt: message.completedAt ? toIsoString(message.completedAt) : undefined,
258
- })
282
+ } as z.infer<typeof AgentMessageSchema>
283
+ }
284
+
285
+ /**
286
+ * A part without what only replays the conversation to its model: a provider's opaque state and
287
+ * metadata, such as encrypted reasoning, which no reader shows and which made up some 40% of a long
288
+ * thread's messages. A provider-state part stays, with no data, so each part keeps its index for
289
+ * the `/parts/<index>/...` paths files are read at.
290
+ */
291
+ function withoutReplayState(part: AgentMessagePart): AgentMessagePart {
292
+ if (part.type === "provider-state") return { ...part, data: null }
293
+ if (!("providerMetadata" in part) || part.providerMetadata === undefined) return part
294
+ const { providerMetadata: _replay, ...shown } = part
295
+ return shown as AgentMessagePart
259
296
  }
260
297
 
261
298
  export function serializeAgentInboxItem(
@@ -274,12 +311,20 @@ export function serializeAgentInboxItem(
274
311
  timeZone: item.timeZone,
275
312
  locale: item.locale,
276
313
  immediate: item.immediate === true ? true : undefined,
314
+ replacesMessageId: item.replacesMessageId,
277
315
  createdAt: toIsoString(item.createdAt),
278
316
  updatedAt: toIsoString(item.updatedAt),
279
317
  })
280
318
  }
281
319
 
282
- export function serializeAgentRun(run: AgentRunView): ReturnType<typeof AgentRunSchema.parse> {
320
+ /**
321
+ * A run as the API returns it. `ownerReads`: its thread's owner reads it, and alone sees the folder
322
+ * on their computer it runs in, as with the thread's own `computer`.
323
+ */
324
+ export function serializeAgentRun(
325
+ run: AgentRunView,
326
+ { ownerReads = false }: { readonly ownerReads?: boolean } = {}
327
+ ): ReturnType<typeof AgentRunSchema.parse> {
283
328
  return AgentRunSchema.parse({
284
329
  id: run.id,
285
330
  projectId: run.projectId,
@@ -294,6 +339,7 @@ export function serializeAgentRun(run: AgentRunView): ReturnType<typeof AgentRun
294
339
  reasoning: run.spec?.reasoning,
295
340
  approvalMode: run.spec?.approvalMode,
296
341
  runsOn: run.kind === "conversation" ? run.spec?.runsOn : undefined,
342
+ computer: ownerReads && run.kind === "conversation" ? run.spec?.computer : undefined,
297
343
  modelId: run.modelId,
298
344
  finishReason: run.finishReason,
299
345
  usage: run.usage,
@@ -374,12 +420,14 @@ export function handleAgentRouteError(
374
420
  // Core performs visibility checks before workspace state transitions.
375
421
  switch (error.code) {
376
422
  case "agent_not_found":
423
+ case "message_not_found":
377
424
  case "run_not_found":
378
425
  case "thread_not_found":
379
426
  case "approval_not_found":
380
427
  set.status = 404
381
428
  break
382
429
  case "active_run_exists":
430
+ case "message_not_editable":
383
431
  case "thread_not_conversation":
384
432
  case "run_not_retryable":
385
433
  case "run_not_active":
@@ -389,6 +437,7 @@ export function handleAgentRouteError(
389
437
  set.status = 409
390
438
  break
391
439
  case "authority_not_inheritable":
440
+ case "not_message_author":
392
441
  case "not_thread_owner":
393
442
  case "not_run_requester":
394
443
  set.status = 403
@@ -958,9 +1007,8 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
958
1007
  }
959
1008
 
960
1009
  const parsed = AgentMessagesQuerySchema.parse(query)
961
- return AgentMessageListResponseSchema.parse(
962
- await readThreadMessages(host, storage, thread, parsed)
963
- )
1010
+ // The response schema checks the page once, as it leaves.
1011
+ return await readThreadMessages(host, storage, thread, parsed)
964
1012
  } catch (error) {
965
1013
  return handleAgentRouteError(error, set)
966
1014
  }
@@ -1188,6 +1236,64 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
1188
1236
  },
1189
1237
  }
1190
1238
  )
1239
+ .post(
1240
+ "/api/agent-threads/:threadId/messages/:messageId/edit",
1241
+ async (context) => {
1242
+ const { params, body, set } = context
1243
+ const sixb = requireRequestSixb(context)
1244
+ try {
1245
+ const storage = host.storage.agents
1246
+ if (!storage) {
1247
+ return missingAgentStorageResponse(set)
1248
+ }
1249
+
1250
+ const parsed = EditAgentMessageBodySchema.parse(body)
1251
+ const result = await sixb.agent.runs.editMessage({
1252
+ threadId: params.threadId,
1253
+ messageId: params.messageId,
1254
+ text: parsed.text,
1255
+ model: parsed.model,
1256
+ reasoning: parsed.reasoning,
1257
+ timeZone: parsed.timeZone,
1258
+ locale: parsed.locale,
1259
+ approvalMode: parsed.approvalMode,
1260
+ attachments: parsed.attachments as readonly FileRef[] | undefined,
1261
+ context: parsed.context,
1262
+ nextMessageId: parsed.nextMessageId,
1263
+ })
1264
+
1265
+ set.status = 202
1266
+ return EditAgentMessageResponseSchema.parse(
1267
+ result.kind === "turn"
1268
+ ? { run: serializeAgentRun(result.run) }
1269
+ : { item: serializeAgentInboxItem(result.item) }
1270
+ )
1271
+ } catch (error) {
1272
+ return handleAgentInboxRouteError(error, set)
1273
+ }
1274
+ },
1275
+ {
1276
+ params: AgentMessageFileContentParamsSchema,
1277
+ body: EditAgentMessageBodySchema,
1278
+ response: {
1279
+ 202: EditAgentMessageResponseSchema,
1280
+ 400: AgentRequestErrorResponseSchema,
1281
+ 403: AgentRequestErrorResponseSchema,
1282
+ 404: ErrorResponseSchema,
1283
+ 409: AgentRequestErrorResponseSchema,
1284
+ 429: AiUsageLimitErrorResponseSchema,
1285
+ 501: ErrorResponseSchema,
1286
+ },
1287
+ detail: {
1288
+ summary: "Edit an agent thread message and restart from that turn",
1289
+ description:
1290
+ "Replaces a user-authored message in place, removes any messages and compaction summaries from that turn onward, and starts a new turn from the edited message. While a turn runs, the edit stops it and waits in the thread's inbox: the turn's end rewinds the thread and starts the edited message.",
1291
+ tags: [OPENAPI_TAGS.agentThreads.name],
1292
+ operationId: "editAgentThreadMessage",
1293
+ security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
1294
+ },
1295
+ }
1296
+ )
1191
1297
  .post(
1192
1298
  "/api/agent-threads/:threadId/cancel",
1193
1299
  async (context) => {
@@ -1403,19 +1509,26 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
1403
1509
  }
1404
1510
 
1405
1511
  const parsed = AgentRunListQuerySchema.parse(query)
1406
- const result = await sixb.agent.runs.listForThread(params.threadId, {
1407
- statuses: parsed.status ? [parsed.status] : undefined,
1408
- limit: parseOptionalInt(parsed.limit),
1409
- offset: parseOptionalInt(parsed.offset),
1410
- order: parsed.order,
1411
- })
1512
+ const [result, thread] = await Promise.all([
1513
+ sixb.agent.runs.listForThread(params.threadId, {
1514
+ statuses: parsed.status ? [parsed.status] : undefined,
1515
+ limit: parseOptionalInt(parsed.limit),
1516
+ offset: parseOptionalInt(parsed.offset),
1517
+ order: parsed.order,
1518
+ }),
1519
+ storage.threads.getById({ projectId: host.id, id: params.threadId }),
1520
+ ])
1412
1521
  if (!result) {
1413
1522
  set.status = 404
1414
1523
  return { error: "Agent thread not found" }
1415
1524
  }
1416
1525
 
1526
+ // Its owner reads where each turn ran, down to the folder: the app marks every move.
1527
+ const ownerReads =
1528
+ thread !== null && principalsEqual(thread.ownerPrincipal, requestActor(sixb))
1529
+
1417
1530
  return AgentRunListResponseSchema.parse({
1418
- runs: result.runs.map(serializeAgentRun),
1531
+ runs: result.runs.map((run) => serializeAgentRun(run, { ownerReads })),
1419
1532
  hasMore: result.hasMore,
1420
1533
  total: result.total,
1421
1534
  })
@@ -348,6 +348,7 @@ export function registerAiAccountingRoutes(app: Elysia, host: SixbHostView) {
348
348
  from: new Date(parsed.from),
349
349
  to: new Date(parsed.to),
350
350
  bucket: parsed.bucket,
351
+ ...(parsed.timeZone === undefined ? {} : { timeZone: parsed.timeZone }),
351
352
  providerId: parsed.providerId,
352
353
  modelId: parsed.modelId,
353
354
  ...accountingFilters(parsed),
@@ -275,7 +275,7 @@ export function registerModelGenerationRoutes(app: Elysia, host: SixbHostView) {
275
275
  model: entry.model,
276
276
  signal: request.signal,
277
277
  })
278
- return TranscriptionSchema.parse({ ...result(entry, call), text: call.output.text })
278
+ return TranscriptionSchema.parse({ ...result(entry, call), ...call.output })
279
279
  } catch (error) {
280
280
  return generationError(error, set)
281
281
  }
@@ -89,9 +89,8 @@ export function registerObservedAgentThreadRoutes(app: Elysia, host: SixbHostVie
89
89
  }
90
90
 
91
91
  const query = AgentMessagesQuerySchema.parse(context.query)
92
- return AgentMessageListResponseSchema.parse(
93
- await readThreadMessages(host, storage, thread, query)
94
- )
92
+ // The response schema checks the page once, as it leaves.
93
+ return await readThreadMessages(host, storage, thread, query)
95
94
  } catch (error) {
96
95
  return handleAgentRouteError(error, context.set)
97
96
  }
@@ -159,7 +158,7 @@ export function registerObservedAgentThreadRoutes(app: Elysia, host: SixbHostVie
159
158
  }
160
159
 
161
160
  return AgentRunListResponseSchema.parse({
162
- runs: result.runs.map(serializeAgentRun),
161
+ runs: result.runs.map((run) => serializeAgentRun(run)),
163
162
  hasMore: result.hasMore,
164
163
  total: result.total,
165
164
  })
@@ -6,7 +6,16 @@ import {
6
6
  ReadinessResponseSchema,
7
7
  StatusResponseSchema,
8
8
  } from "../schemas/status"
9
- export function registerStatusRoutes(app: Elysia, host: SixbHostView) {
9
+
10
+ export interface StatusRouteOptions {
11
+ readonly isStopping?: () => boolean
12
+ }
13
+
14
+ export function registerStatusRoutes(
15
+ app: Elysia,
16
+ host: SixbHostView,
17
+ options: StatusRouteOptions = {}
18
+ ) {
10
19
  app.get("/health", () => ({ status: "ok" as const }), {
11
20
  response: { 200: HealthResponseSchema },
12
21
  detail: {
@@ -20,6 +29,14 @@ export function registerStatusRoutes(app: Elysia, host: SixbHostView) {
20
29
  app.get(
21
30
  "/ready",
22
31
  async ({ set }) => {
32
+ if (options.isStopping?.()) {
33
+ set.status = 503
34
+ return {
35
+ status: "unready" as const,
36
+ storage: { reachable: false, schemaValid: false },
37
+ reason: "Server is shutting down.",
38
+ }
39
+ }
23
40
  const readiness = await host.checkReadiness()
24
41
  if (readiness.status === "unready") set.status = 503
25
42
  return readiness
@@ -1,4 +1,5 @@
1
- import type { SixbHostView, SyncDefinition } from "@sixb/core"
1
+ import type { SixbHostView, SyncAccount, SyncDefinition } from "@sixb/core"
2
+ import { isOAuthConnectorDefinition } from "@sixb/core/internal/connector-connections"
2
3
  import type { SyncRunRecord } from "@sixb/core/storage"
3
4
  import type { Elysia } from "elysia"
4
5
  import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
@@ -7,6 +8,10 @@ import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
7
8
  import { OPENAPI_TAGS } from "../openapi/tags"
8
9
  import { ErrorResponseSchema } from "../schemas/common"
9
10
  import {
11
+ ConnectionSyncRunsParamsSchema,
12
+ ConnectionSyncRunsResponseSchema,
13
+ ConnectorAccountListResponseSchema,
14
+ ConnectorAccountsParamsSchema,
10
15
  RequestSyncRunBodySchema,
11
16
  RequestSyncRunResponseSchema,
12
17
  SyncParamsSchema,
@@ -27,6 +32,7 @@ function serializeSyncRun(run: SyncRunRecord) {
27
32
  id: run.id,
28
33
  projectId: run.projectId,
29
34
  syncId: run.syncId,
35
+ connectionId: run.connectionId,
30
36
  datasetId: run.datasetId,
31
37
  mode: run.mode,
32
38
  status: run.status,
@@ -67,9 +73,71 @@ async function getLatestSyncRuns(
67
73
  return new Map(result.runs.map((run) => [run.syncId, serializeSyncRun(run)]))
68
74
  }
69
75
 
76
+ function serializeAccount(account: SyncAccount) {
77
+ const { id, owner, slot, account: candidate } = account.connection
78
+ return {
79
+ connection: { id, owner, slot, account: { id: candidate.id, label: candidate.label } },
80
+ latestRuns: account.latestRuns.map(serializeSyncRun),
81
+ }
82
+ }
83
+
84
+ type RequestSixb = ReturnType<typeof requireRequestSixb>
85
+
86
+ /** Each connector's accounts, read once however many of its syncs a response describes. */
87
+ function connectorAccounts(sixb: RequestSixb) {
88
+ const byConnector = new Map<string, Promise<readonly SyncAccount[]>>()
89
+ return (connectorId: string) => {
90
+ let accounts = byConnector.get(connectorId)
91
+ if (!accounts) {
92
+ accounts = sixb.syncs.accounts.list(connectorId)
93
+ byConnector.set(connectorId, accounts)
94
+ }
95
+ return accounts
96
+ }
97
+ }
98
+
99
+ /** On a sync of an OAuth connector, where the accounts the caller may see stand in it. */
100
+ async function summarizeAccounts(
101
+ sync: SyncDefinition,
102
+ accountsOf: ReturnType<typeof connectorAccounts>
103
+ ) {
104
+ if (!isOAuthConnectorDefinition(sync.connector)) return undefined
105
+
106
+ return summarizeSyncAccounts(sync.id, await accountsOf(sync.connector.id))
107
+ }
108
+
109
+ /**
110
+ * Where a sync's accounts stand, by the latest run of each. An account counts once: running,
111
+ * needing reconnecting, failing or synced. One whose latest run was cancelled, or that never ran,
112
+ * is in `total` alone.
113
+ */
114
+ export function summarizeSyncAccounts(syncId: string, accounts: readonly SyncAccount[]) {
115
+ const latest = accounts.flatMap(({ latestRuns }) =>
116
+ latestRuns.filter((run) => run.syncId === syncId)
117
+ )
118
+ const count = (matches: (run: SyncRunRecord) => boolean) => latest.filter(matches).length
119
+ const reconnect = (run: SyncRunRecord) => run.error?.code === "connector.authorization_required"
120
+
121
+ const synced = latest.filter((run) => run.status === "succeeded")
122
+ const lastSyncedAt = synced.reduce<Date | undefined>((last, run) => {
123
+ const at = run.finishedAt ?? run.queuedAt
124
+ return last && last >= at ? last : at
125
+ }, undefined)
126
+
127
+ return {
128
+ total: accounts.length,
129
+ running: count((run) => run.status === "queued" || run.status === "running"),
130
+ needsReconnecting: count((run) => run.status === "failed" && reconnect(run)),
131
+ failing: count((run) => run.status === "failed" && !reconnect(run)),
132
+ synced: synced.length,
133
+ ...(lastSyncedAt === undefined ? {} : { lastSyncedAt: toIsoString(lastSyncedAt) }),
134
+ }
135
+ }
136
+
70
137
  function serializeSync(
71
138
  sync: SyncDefinition,
72
- latestRun: SerializedSyncRun | null
139
+ latestRun: SerializedSyncRun | null,
140
+ accounts?: Awaited<ReturnType<typeof summarizeAccounts>>
73
141
  ): ReturnType<typeof SyncSchema.parse> {
74
142
  return SyncSchema.parse({
75
143
  id: sync.id,
@@ -84,6 +152,7 @@ function serializeSync(
84
152
  },
85
153
  triggers: sync.triggers,
86
154
  latestRun,
155
+ ...(accounts === undefined ? {} : { accounts }),
87
156
  })
88
157
  }
89
158
 
@@ -98,8 +167,17 @@ export function registerSyncRoutes(app: Elysia, host: SixbHostView) {
98
167
  sixb,
99
168
  syncs.map((sync) => sync.id)
100
169
  )
170
+ const accountsOf = connectorAccounts(sixb)
101
171
 
102
- return syncs.map((sync) => serializeSync(sync, latestRuns.get(sync.id) ?? null))
172
+ return Promise.all(
173
+ syncs.map(async (sync) =>
174
+ serializeSync(
175
+ sync,
176
+ latestRuns.get(sync.id) ?? null,
177
+ await summarizeAccounts(sync, accountsOf)
178
+ )
179
+ )
180
+ )
103
181
  },
104
182
  {
105
183
  response: { 200: SyncSchema.array() },
@@ -122,7 +200,11 @@ export function registerSyncRoutes(app: Elysia, host: SixbHostView) {
122
200
  return { error: "Sync not found" }
123
201
  }
124
202
 
125
- return serializeSync(sync, await getLatestSyncRun(sixb, sync.id))
203
+ return serializeSync(
204
+ sync,
205
+ await getLatestSyncRun(sixb, sync.id),
206
+ await summarizeAccounts(sync, connectorAccounts(sixb))
207
+ )
126
208
  },
127
209
  {
128
210
  params: SyncParamsSchema,
@@ -135,6 +217,47 @@ export function registerSyncRoutes(app: Elysia, host: SixbHostView) {
135
217
  },
136
218
  }
137
219
  )
220
+ .get(
221
+ "/api/connectors/:connectorId/accounts",
222
+ async (context) => {
223
+ const { params } = context
224
+ const sixb = requireRequestSixb(context)
225
+ const accounts = await sixb.syncs.accounts.list(params.connectorId)
226
+ return { accounts: accounts.map(serializeAccount) }
227
+ },
228
+ {
229
+ params: ConnectorAccountsParamsSchema,
230
+ response: { 200: ConnectorAccountListResponseSchema },
231
+ detail: {
232
+ summary: "List a connector's accounts and how they synced",
233
+ description:
234
+ "On an OAuth connector, each connected account you may see, with the latest run of each of its syncs you may run. Empty for a static connector.",
235
+ tags: [OPENAPI_TAGS.connectorConnections.name],
236
+ operationId: "listConnectorAccounts",
237
+ security: accessTokenSecurityRequirement("listConnectorAccounts"),
238
+ },
239
+ }
240
+ )
241
+ .get(
242
+ "/api/connectors/:connectorId/connections/:connectionId/sync-runs",
243
+ async (context) => {
244
+ const { params } = context
245
+ const sixb = requireRequestSixb(context)
246
+ const runs = await sixb.syncs.accounts.latestRuns(params.connectorId, params.connectionId)
247
+ return { runs: runs.map(serializeSyncRun) }
248
+ },
249
+ {
250
+ params: ConnectionSyncRunsParamsSchema,
251
+ response: { 200: ConnectionSyncRunsResponseSchema },
252
+ detail: {
253
+ summary: "Latest sync runs of a connection",
254
+ description:
255
+ "The latest run of each sync of one connection: your own, or anyone's with can.manage on its connector. Empty for a connection you don't see.",
256
+ tags: [OPENAPI_TAGS.connectorConnections.name],
257
+ operationId: "listConnectorConnectionSyncRuns",
258
+ },
259
+ }
260
+ )
138
261
  .get(
139
262
  "/api/sync-runs",
140
263
  async (context) => {
@@ -149,6 +272,7 @@ export function registerSyncRoutes(app: Elysia, host: SixbHostView) {
149
272
 
150
273
  const result = await sixb.syncs.runs.list({
151
274
  syncId: parsed.syncId,
275
+ ...(parsed.connectionId === undefined ? {} : { connectionId: parsed.connectionId }),
152
276
  datasetId: parsed.datasetId,
153
277
  statuses: parsed.status ? [parsed.status] : undefined,
154
278
  startedAfter: parseDate(parsed.startedAfter),
@@ -193,10 +317,13 @@ export function registerSyncRoutes(app: Elysia, host: SixbHostView) {
193
317
 
194
318
  set.status = 202
195
319
  return {
196
- runId: result.runId,
197
- jobId: result.jobId ?? "",
198
- syncId: result.syncId,
199
- queuedAt: result.queuedAt,
320
+ runs: result.runs.map((run) => ({
321
+ runId: run.runId,
322
+ jobId: run.jobId ?? "",
323
+ syncId: run.syncId,
324
+ connectionId: run.connectionId,
325
+ queuedAt: run.queuedAt,
326
+ })),
200
327
  }
201
328
  } catch (error) {
202
329
  return handleRouteError(error, set)
@@ -1,4 +1,5 @@
1
1
  import { principalsEqual } from "@sixb/core"
2
+ import type { AgentRunStepsEvent } from "@sixb/core/agents/streams"
2
3
  import {
3
4
  AGENT_ACTIVITY_STREAM_ID,
4
5
  type AgentActivityEvent,
@@ -9,6 +10,8 @@ import {
9
10
  agentActivityStreamDefinition,
10
11
  agentRunStreamDefinition,
11
12
  agentRunStreamId,
13
+ coveredBySteps,
14
+ readAgentRunSteps,
12
15
  } from "@sixb/core/internal/agents/streams"
13
16
  import type { Sixb } from "@sixb/core/internal/request-execution"
14
17
  import type { Principal } from "@sixb/core/storage"
@@ -193,12 +196,21 @@ async function subscribeAgentStream(
193
196
 
194
197
  const buffered: BrokerRecord[] = []
195
198
  let live = false
199
+ // The finished steps sent first, whose live chunks this reader then skips.
200
+ let steps: AgentRunStepsEvent | null = null
201
+ const send = (records: readonly BrokerRecord[]): void => {
202
+ const current = steps
203
+ sendRecords(
204
+ ws,
205
+ current ? records.filter((record) => !coveredBySteps(current, record.payload)) : records
206
+ )
207
+ }
196
208
  const receive = (records: readonly BrokerRecord[]): void => {
197
209
  if (!live) {
198
210
  buffered.push(...records)
199
211
  return
200
212
  }
201
- sendRecords(ws, records)
213
+ send(records)
202
214
  }
203
215
 
204
216
  // Set when the requested cursor had already been trimmed, so the client learns its resume point
@@ -251,13 +263,41 @@ async function subscribeAgentStream(
251
263
  runId: message.runId,
252
264
  afterCursor: resumedFromEarliest ? null : (message.afterCursor ?? null),
253
265
  })
254
- sendRecords(ws, buffered.splice(0))
266
+ // A reader without its place in the stream, opening the turn or back after its place was
267
+ // dropped, reads the finished steps first: the live stream keeps only its newest records, and a
268
+ // long turn outgrows it.
269
+ if (message.afterCursor === undefined || resumedFromEarliest) {
270
+ steps = await readRunSteps(server, sixb, message.runId)
271
+ if (steps) safeSend(ws, { type: "run.steps", event: steps })
272
+ }
273
+ send(buffered.splice(0))
255
274
  if (!(await sendRunSnapshot(sixb, ws, message.runId))) {
256
275
  stopSubscription(state)
257
276
  return
258
277
  }
259
278
  live = true
260
- sendRecords(ws, buffered.splice(0))
279
+ send(buffered.splice(0))
280
+ }
281
+
282
+ /** The run's finished steps for its current attempt, or null: without them it streams as before. */
283
+ async function readRunSteps(
284
+ server: SixbServer,
285
+ sixb: Sixb,
286
+ runId: string
287
+ ): Promise<AgentRunStepsEvent | null> {
288
+ try {
289
+ const run = await sixb.agent.runs.getById(runId)
290
+ if (!run || (run.status !== "queued" && run.status !== "running")) return null
291
+ const host = server.getHost()
292
+ return await readAgentRunSteps(host.broker, {
293
+ projectId: host.id,
294
+ runId,
295
+ attempt: run.attempt,
296
+ })
297
+ } catch (error) {
298
+ console.error(`[SixbServer] Agent run '${runId}' finished steps could not be read:`, error)
299
+ return null
300
+ }
261
301
  }
262
302
 
263
303
  async function subscribeAgentActivity(