@sixb/server 0.2.4 → 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.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +877 -89
- package/dist/index.js.map +26 -25
- package/dist/live/room.d.ts +2 -0
- package/dist/live/room.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +1 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/agent-approvals.d.ts +22 -0
- package/dist/routes/agent-approvals.d.ts.map +1 -1
- package/dist/routes/agent-computers.d.ts +36 -0
- package/dist/routes/agent-computers.d.ts.map +1 -1
- package/dist/routes/agent-runners.d.ts +5 -1
- package/dist/routes/agent-runners.d.ts.map +1 -1
- package/dist/routes/agent-thread-reads.d.ts +82 -0
- package/dist/routes/agent-thread-reads.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1760 -86
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/artifact-frames.d.ts +3 -3
- package/dist/routes/artifact-frames.d.ts.map +1 -1
- package/dist/routes/artifacts.d.ts +287 -21
- package/dist/routes/artifacts.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +7 -7
- package/dist/routes/events.d.ts +4 -4
- package/dist/routes/links.d.ts +8 -0
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/model-generations.d.ts +6 -0
- package/dist/routes/model-generations.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +8 -0
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +22 -0
- package/dist/routes/observed-agent-threads.d.ts.map +1 -1
- package/dist/routes/projections.d.ts +2 -0
- package/dist/routes/projections.d.ts.map +1 -1
- package/dist/routes/status.d.ts +4 -1
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/syncs.d.ts +175 -12
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +6 -6
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts +6 -6
- package/dist/schemas/activity.d.ts +4 -4
- package/dist/schemas/agents.d.ts +7697 -262
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +3 -0
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/artifacts.d.ts +676 -36
- package/dist/schemas/artifacts.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +8 -8
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/events.d.ts +36 -24
- package/dist/schemas/events.d.ts.map +1 -1
- package/dist/schemas/lineage.d.ts +112 -0
- package/dist/schemas/lineage.d.ts.map +1 -1
- package/dist/schemas/models.d.ts +46 -0
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/projections.d.ts +11 -0
- package/dist/schemas/projections.d.ts.map +1 -1
- package/dist/schemas/syncs.d.ts +724 -25
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +22 -22
- package/dist/server.d.ts +16 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +6 -1
- package/src/live/README.md +9 -6
- package/src/live/room.ts +16 -8
- package/src/registerRoutes.ts +4 -1
- package/src/routes/agent-computers.ts +7 -6
- package/src/routes/agent-runners.ts +15 -4
- package/src/routes/agent-thread-reads.ts +67 -0
- package/src/routes/agents.ts +331 -20
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/artifact-frames.ts +22 -9
- package/src/routes/artifacts.ts +124 -3
- package/src/routes/model-generations.ts +1 -1
- package/src/routes/observed-agent-threads.ts +3 -4
- package/src/routes/status.ts +18 -1
- package/src/routes/syncs.ts +135 -8
- package/src/routes/ws/agents.ts +43 -3
- package/src/schemas/agents.ts +148 -12
- package/src/schemas/ai-accounting.ts +12 -0
- package/src/schemas/artifacts.ts +66 -1
- package/src/schemas/common.ts +15 -9
- package/src/schemas/events.ts +1 -0
- package/src/schemas/models.ts +10 -0
- package/src/schemas/projections.ts +1 -0
- package/src/schemas/syncs.ts +63 -1
- package/src/server.ts +208 -14
package/src/routes/artifacts.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
boardPreviewStatuses,
|
|
23
23
|
boardPreviews,
|
|
24
24
|
boardsReadLiveData,
|
|
25
|
+
documentPdfKey,
|
|
25
26
|
} from "@sixb/core/internal/artifacts/publish"
|
|
26
27
|
import type {
|
|
27
28
|
ArtifactCommentRecord,
|
|
@@ -53,6 +54,9 @@ import {
|
|
|
53
54
|
ArtifactListQuerySchema,
|
|
54
55
|
ArtifactListResponseSchema,
|
|
55
56
|
ArtifactParamsSchema,
|
|
57
|
+
ArtifactPdfDownloadQuerySchema,
|
|
58
|
+
ArtifactPdfQuerySchema,
|
|
59
|
+
ArtifactPdfResponseSchema,
|
|
56
60
|
ArtifactQueryParamsSchema,
|
|
57
61
|
ArtifactResponseSchema,
|
|
58
62
|
ArtifactRestorePointListResponseSchema,
|
|
@@ -204,6 +208,8 @@ async function versionResponse(
|
|
|
204
208
|
return {
|
|
205
209
|
...versionBody(version),
|
|
206
210
|
...(statuses ? { boardPreviews: Object.fromEntries(statuses) } : {}),
|
|
211
|
+
// A designed document prints to PDF when asked, in a project that has sandboxes.
|
|
212
|
+
...(host.sandboxes && documentPdfKey(version) ? { printable: true } : {}),
|
|
207
213
|
}
|
|
208
214
|
}
|
|
209
215
|
|
|
@@ -334,6 +340,7 @@ function handleArtifactRouteError(error: unknown, set: RouteSet) {
|
|
|
334
340
|
switch (error.code) {
|
|
335
341
|
case "artifact_not_found":
|
|
336
342
|
case "comment_not_found":
|
|
343
|
+
case "holder_not_found":
|
|
337
344
|
case "query_not_found":
|
|
338
345
|
case "restore_point_not_found":
|
|
339
346
|
set.status = 404
|
|
@@ -358,6 +365,7 @@ function handleArtifactRouteError(error: unknown, set: RouteSet) {
|
|
|
358
365
|
case "read_only":
|
|
359
366
|
set.status = 403
|
|
360
367
|
break
|
|
368
|
+
case "pdf_unavailable":
|
|
361
369
|
case "storage_unavailable":
|
|
362
370
|
set.status = 501
|
|
363
371
|
break
|
|
@@ -568,6 +576,56 @@ async function artifactBoardPreviewResponse(
|
|
|
568
576
|
}
|
|
569
577
|
}
|
|
570
578
|
|
|
579
|
+
/** A designed document's PDF as a download, once printed; 404 `pdf_not_found` until then. */
|
|
580
|
+
async function artifactPdfResponse(
|
|
581
|
+
host: SixbHostView,
|
|
582
|
+
context: {
|
|
583
|
+
readonly params: { readonly artifactId: string; readonly version: string }
|
|
584
|
+
readonly query: { readonly key?: string | undefined }
|
|
585
|
+
readonly request: Request
|
|
586
|
+
readonly set: RouteSet & { headers: Record<string, string | number> }
|
|
587
|
+
}
|
|
588
|
+
) {
|
|
589
|
+
try {
|
|
590
|
+
const { artifactId } = context.params
|
|
591
|
+
const number = versionParam(context.params.version)
|
|
592
|
+
const sixb = requireRequestSixb(context)
|
|
593
|
+
const version = await sixb.artifacts.getVersion(artifactId, number)
|
|
594
|
+
if (!version) return await refusal(context, artifactId, context.set)
|
|
595
|
+
|
|
596
|
+
// As the caller reads it: a document that shows live data prints their values, which the key
|
|
597
|
+
// its POST answered with names.
|
|
598
|
+
const pdf = await sixb.artifacts.pdf(artifactId, {
|
|
599
|
+
version: number,
|
|
600
|
+
print: false,
|
|
601
|
+
...(context.query.key ? { key: context.query.key } : {}),
|
|
602
|
+
})
|
|
603
|
+
const response =
|
|
604
|
+
pdf?.status === "ready" &&
|
|
605
|
+
(await createFileContentResponse({
|
|
606
|
+
blobStorage: host.blobStorage,
|
|
607
|
+
fileRef: pdf.file,
|
|
608
|
+
request: context.request,
|
|
609
|
+
disposition: "attachment",
|
|
610
|
+
}))
|
|
611
|
+
if (!response) {
|
|
612
|
+
context.set.status = 404
|
|
613
|
+
context.set.headers["cache-control"] = "no-store"
|
|
614
|
+
return { error: "PDF not found", code: "pdf_not_found" as const }
|
|
615
|
+
}
|
|
616
|
+
// What a version prints changes as its content does: a frozen one's never.
|
|
617
|
+
response.headers.set(
|
|
618
|
+
"cache-control",
|
|
619
|
+
number !== "latest" && version.frozenAt
|
|
620
|
+
? "private, max-age=31536000, immutable"
|
|
621
|
+
: "private, no-cache"
|
|
622
|
+
)
|
|
623
|
+
return response
|
|
624
|
+
} catch (error) {
|
|
625
|
+
return handleArtifactRouteError(error, context.set)
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
571
629
|
/**
|
|
572
630
|
* Whether the caller sees the pictures of a version's boards. A version that holds queries has its
|
|
573
631
|
* boards pictured with its author's answers, which a shared link's guest, whose link grants its
|
|
@@ -1002,7 +1060,7 @@ export function registerArtifactRoutes(app: Elysia, host: SixbHostView) {
|
|
|
1002
1060
|
detail: {
|
|
1003
1061
|
summary: "List an artifact's comments",
|
|
1004
1062
|
description:
|
|
1005
|
-
"Every comment and reply, oldest first, resolved ones included. Whoever reads the artifact reads them.",
|
|
1063
|
+
"Every comment and reply, oldest first, resolved ones included. Whoever reads the artifact reads them; a shared link's guest reads only the threads started by a guest of the same link, with every reply in them.",
|
|
1006
1064
|
tags: [OPENAPI_TAGS.artifacts.name],
|
|
1007
1065
|
operationId: "listArtifactComments",
|
|
1008
1066
|
security: accessTokenSecurityRequirement("listArtifactComments"),
|
|
@@ -1046,7 +1104,7 @@ export function registerArtifactRoutes(app: Elysia, host: SixbHostView) {
|
|
|
1046
1104
|
detail: {
|
|
1047
1105
|
summary: "Comment on an artifact",
|
|
1048
1106
|
description:
|
|
1049
|
-
"Adds a comment, or a reply with parentId, as the caller. Whoever reads the artifact may, a shared link's guest included, under the name they gave its session (400 name_required until they give one). A canvas's comment can be about one of its boards, or an element of a board by its address. Readers receive artifact.commented.",
|
|
1107
|
+
"Adds a comment, or a reply with parentId, as the caller. Whoever reads the artifact may, a shared link's guest included, under the name they gave its session (400 name_required until they give one), and replies only in a thread they read (404 comment_not_found otherwise). A canvas's comment can be about one of its boards, or an element of a board by its address. Readers of the thread receive artifact.commented.",
|
|
1050
1108
|
tags: [OPENAPI_TAGS.artifacts.name],
|
|
1051
1109
|
operationId: "addArtifactComment",
|
|
1052
1110
|
security: accessTokenSecurityRequirement("addArtifactComment"),
|
|
@@ -1330,6 +1388,69 @@ export function registerArtifactRoutes(app: Elysia, host: SixbHostView) {
|
|
|
1330
1388
|
},
|
|
1331
1389
|
}
|
|
1332
1390
|
)
|
|
1391
|
+
.post(
|
|
1392
|
+
"/api/artifacts/:artifactId/versions/:version/pdf",
|
|
1393
|
+
async (context) => {
|
|
1394
|
+
try {
|
|
1395
|
+
const { artifactId } = context.params
|
|
1396
|
+
const pdf = await requireRequestSixb(context).artifacts.pdf(artifactId, {
|
|
1397
|
+
version: versionParam(context.params.version),
|
|
1398
|
+
retry: context.query.retry === "true",
|
|
1399
|
+
...(context.query.key ? { key: context.query.key } : {}),
|
|
1400
|
+
})
|
|
1401
|
+
if (!pdf) return await refusal(context, artifactId, context.set)
|
|
1402
|
+
if (pdf.status !== "ready") return pdf
|
|
1403
|
+
const { file: _file, ...ready } = pdf
|
|
1404
|
+
return ready
|
|
1405
|
+
} catch (error) {
|
|
1406
|
+
return handleArtifactRouteError(error, context.set)
|
|
1407
|
+
}
|
|
1408
|
+
},
|
|
1409
|
+
{
|
|
1410
|
+
params: ArtifactVersionParamsSchema,
|
|
1411
|
+
query: ArtifactPdfQuerySchema,
|
|
1412
|
+
response: {
|
|
1413
|
+
200: ArtifactPdfResponseSchema,
|
|
1414
|
+
400: ArtifactErrorResponseSchema,
|
|
1415
|
+
403: ArtifactErrorResponseSchema,
|
|
1416
|
+
404: ArtifactErrorResponseSchema,
|
|
1417
|
+
501: ArtifactErrorResponseSchema,
|
|
1418
|
+
},
|
|
1419
|
+
detail: {
|
|
1420
|
+
summary: "Print a designed document to PDF",
|
|
1421
|
+
description:
|
|
1422
|
+
"Answers ready once the version is printed, and printing until then, having asked for it: ask again after a few seconds, then get it. Failed with why when it could not be printed; retry=true asks again. A document that shows live data answers with a key: ask again and get it with that key, which names the values the first ask printed. It prints as its stylesheets' @page rule lays it out, in a sandbox of the project. Whoever reads the artifact may ask. 400 invalid_artifact when the version is not a designed document; 501 pdf_unavailable in a project without sandboxes.",
|
|
1423
|
+
tags: [OPENAPI_TAGS.artifacts.name],
|
|
1424
|
+
operationId: "printArtifactPdf",
|
|
1425
|
+
security: accessTokenSecurityRequirement("printArtifactPdf"),
|
|
1426
|
+
},
|
|
1427
|
+
}
|
|
1428
|
+
)
|
|
1429
|
+
.get(
|
|
1430
|
+
"/api/artifacts/:artifactId/versions/:version/pdf",
|
|
1431
|
+
async (context) => artifactPdfResponse(host, context),
|
|
1432
|
+
{
|
|
1433
|
+
params: ArtifactVersionParamsSchema,
|
|
1434
|
+
query: ArtifactPdfDownloadQuerySchema,
|
|
1435
|
+
detail: {
|
|
1436
|
+
summary: "Get a designed document's PDF",
|
|
1437
|
+
description:
|
|
1438
|
+
"The version printed to PDF, with the values its fields read as the caller reads them, as a download named after the title it printed with: for a document that shows live data, those its POST's key names. 404 pdf_not_found until it is printed: ask for it with POST first. A frozen version's PDF never changes, and is cached as such.",
|
|
1439
|
+
tags: [OPENAPI_TAGS.artifacts.name],
|
|
1440
|
+
operationId: "getArtifactPdf",
|
|
1441
|
+
security: accessTokenSecurityRequirement("getArtifactPdf"),
|
|
1442
|
+
responses: {
|
|
1443
|
+
200: {
|
|
1444
|
+
description: "The PDF",
|
|
1445
|
+
content: { "application/pdf": { schema: { type: "string", format: "binary" } } },
|
|
1446
|
+
},
|
|
1447
|
+
304: { description: "The PDF is unchanged; reuse the privately cached one" },
|
|
1448
|
+
403: artifactErrorResponse(403),
|
|
1449
|
+
404: artifactErrorResponse(404),
|
|
1450
|
+
},
|
|
1451
|
+
},
|
|
1452
|
+
}
|
|
1453
|
+
)
|
|
1333
1454
|
.get(
|
|
1334
1455
|
"/api/artifacts/:artifactId/versions/:version/boards/:board/preview",
|
|
1335
1456
|
async (context) => artifactBoardPreviewResponse(host, context),
|
|
@@ -1384,7 +1505,7 @@ export function registerArtifactRoutes(app: Elysia, host: SixbHostView) {
|
|
|
1384
1505
|
detail: {
|
|
1385
1506
|
summary: "Run an artifact's query",
|
|
1386
1507
|
description:
|
|
1387
|
-
'Runs the version\'s queries/<name>.json as the caller, with their rights: a type they cannot view is refused with 403, and a marked property they are not cleared for is left out of each row. Rows are serialized as POST /api/objects/query returns them, beside the schema of what they hold as the caller may view it. Use "latest" for the newest version. A query the ontology no longer admits is refused with 409 query_invalid.',
|
|
1508
|
+
'Runs the version\'s queries/<name>.json as the caller, with their rights: a type they cannot view is refused with 403, and a marked property they are not cleared for is left out of each row. Rows are serialized as POST /api/objects/query returns them, beside the schema of what they hold as the caller may view it. Use "latest" for the newest version. A query the ontology no longer admits is refused with 409 query_invalid. A designed document\'s "holder" is the object that holds it, of the type it names, with the links its fields read through: 404 holder_not_found when no one such object the caller reads holds it.',
|
|
1388
1509
|
tags: [OPENAPI_TAGS.artifacts.name],
|
|
1389
1510
|
operationId: "getArtifactQuery",
|
|
1390
1511
|
security: accessTokenSecurityRequirement("getArtifactQuery"),
|
|
@@ -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),
|
|
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
|
-
|
|
93
|
-
|
|
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
|
})
|
package/src/routes/status.ts
CHANGED
|
@@ -6,7 +6,16 @@ import {
|
|
|
6
6
|
ReadinessResponseSchema,
|
|
7
7
|
StatusResponseSchema,
|
|
8
8
|
} from "../schemas/status"
|
|
9
|
-
|
|
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
|
package/src/routes/syncs.ts
CHANGED
|
@@ -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
|
|
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(
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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)
|
package/src/routes/ws/agents.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|