@sixb/server 0.2.2 → 0.2.4

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 (147) hide show
  1. package/README.md +3 -3
  2. package/dist/artifacts/frame.d.ts.map +1 -1
  3. package/dist/auth/scope.d.ts +3 -0
  4. package/dist/auth/scope.d.ts.map +1 -1
  5. package/dist/auth/shared-access.d.ts +23 -0
  6. package/dist/auth/shared-access.d.ts.map +1 -1
  7. package/dist/connectors/http.d.ts +6 -1
  8. package/dist/connectors/http.d.ts.map +1 -1
  9. package/dist/index.js +2777 -683
  10. package/dist/index.js.map +41 -34
  11. package/dist/live/awareness.d.ts +48 -0
  12. package/dist/live/awareness.d.ts.map +1 -0
  13. package/dist/live/room.d.ts +198 -0
  14. package/dist/live/room.d.ts.map +1 -0
  15. package/dist/live/rooms.d.ts +67 -0
  16. package/dist/live/rooms.d.ts.map +1 -0
  17. package/dist/registerRoutes.d.ts.map +1 -1
  18. package/dist/routes/actions.d.ts +3 -3
  19. package/dist/routes/agent-approvals.d.ts +10 -10
  20. package/dist/routes/agent-children.d.ts +2 -2
  21. package/dist/routes/agent-computers.d.ts +32 -0
  22. package/dist/routes/agent-computers.d.ts.map +1 -1
  23. package/dist/routes/agent-runners.d.ts.map +1 -1
  24. package/dist/routes/agents.d.ts +48 -48
  25. package/dist/routes/artifact-frames.d.ts +2 -1
  26. package/dist/routes/artifact-frames.d.ts.map +1 -1
  27. package/dist/routes/artifacts.d.ts +1713 -178
  28. package/dist/routes/artifacts.d.ts.map +1 -1
  29. package/dist/routes/auth.d.ts +21 -21
  30. package/dist/routes/connector-connection-runs.d.ts +124 -5
  31. package/dist/routes/connector-connection-runs.d.ts.map +1 -1
  32. package/dist/routes/connector-connections.d.ts +9 -1
  33. package/dist/routes/connector-connections.d.ts.map +1 -1
  34. package/dist/routes/datasets.d.ts +5 -5
  35. package/dist/routes/events.d.ts +4 -4
  36. package/dist/routes/lake.d.ts +91 -0
  37. package/dist/routes/lake.d.ts.map +1 -0
  38. package/dist/routes/links.d.ts +6 -6
  39. package/dist/routes/logs.d.ts +2 -2
  40. package/dist/routes/model-generations.d.ts +2 -2
  41. package/dist/routes/objects.d.ts +6 -6
  42. package/dist/routes/objects.d.ts.map +1 -1
  43. package/dist/routes/observed-agent-threads.d.ts +7 -7
  44. package/dist/routes/ontology.d.ts +6 -6
  45. package/dist/routes/pipelines.d.ts +8 -4
  46. package/dist/routes/pipelines.d.ts.map +1 -1
  47. package/dist/routes/projections.d.ts +2 -2
  48. package/dist/routes/share-grants.d.ts +9 -9
  49. package/dist/routes/shared-access.d.ts +57 -2
  50. package/dist/routes/shared-access.d.ts.map +1 -1
  51. package/dist/routes/status.d.ts +11 -15
  52. package/dist/routes/status.d.ts.map +1 -1
  53. package/dist/routes/syncs.d.ts +5 -2
  54. package/dist/routes/syncs.d.ts.map +1 -1
  55. package/dist/routes/telemetry.d.ts +7 -7
  56. package/dist/routes/webhooks.d.ts.map +1 -1
  57. package/dist/routes/workflows.d.ts +3 -3
  58. package/dist/routes/ws/artifacts.d.ts +57 -0
  59. package/dist/routes/ws/artifacts.d.ts.map +1 -0
  60. package/dist/routes/ws/events.d.ts +8 -7
  61. package/dist/routes/ws/events.d.ts.map +1 -1
  62. package/dist/routes/ws/index.d.ts +2 -1
  63. package/dist/routes/ws/index.d.ts.map +1 -1
  64. package/dist/routes/ws/logs.d.ts +10 -10
  65. package/dist/routes/ws/shared-sockets.d.ts +52 -0
  66. package/dist/routes/ws/shared-sockets.d.ts.map +1 -0
  67. package/dist/schemas/actions.d.ts +7 -7
  68. package/dist/schemas/activity.d.ts +8 -8
  69. package/dist/schemas/agent-approvals.d.ts +27 -27
  70. package/dist/schemas/agents.d.ts +195 -154
  71. package/dist/schemas/agents.d.ts.map +1 -1
  72. package/dist/schemas/ai-accounting.d.ts +12 -12
  73. package/dist/schemas/artifacts.d.ts +2106 -127
  74. package/dist/schemas/artifacts.d.ts.map +1 -1
  75. package/dist/schemas/auth.d.ts +63 -63
  76. package/dist/schemas/connectors.d.ts +180 -20
  77. package/dist/schemas/connectors.d.ts.map +1 -1
  78. package/dist/schemas/datasets.d.ts +44 -44
  79. package/dist/schemas/event-payloads.d.ts +2 -2
  80. package/dist/schemas/events.d.ts +26 -26
  81. package/dist/schemas/events.d.ts.map +1 -1
  82. package/dist/schemas/files.d.ts +12 -0
  83. package/dist/schemas/files.d.ts.map +1 -1
  84. package/dist/schemas/lake.d.ts +251 -0
  85. package/dist/schemas/lake.d.ts.map +1 -0
  86. package/dist/schemas/lineage.d.ts +56 -56
  87. package/dist/schemas/logs.d.ts +18 -18
  88. package/dist/schemas/models.d.ts +4 -4
  89. package/dist/schemas/objects.d.ts.map +1 -1
  90. package/dist/schemas/ontology.d.ts +14 -14
  91. package/dist/schemas/pipelines.d.ts +70 -48
  92. package/dist/schemas/pipelines.d.ts.map +1 -1
  93. package/dist/schemas/projections.d.ts +14 -14
  94. package/dist/schemas/projections.d.ts.map +1 -1
  95. package/dist/schemas/share-grants.d.ts +30 -30
  96. package/dist/schemas/shared-access.d.ts +17 -4
  97. package/dist/schemas/shared-access.d.ts.map +1 -1
  98. package/dist/schemas/status.d.ts +59 -79
  99. package/dist/schemas/status.d.ts.map +1 -1
  100. package/dist/schemas/syncs.d.ts +26 -10
  101. package/dist/schemas/syncs.d.ts.map +1 -1
  102. package/dist/schemas/telemetry.d.ts +26 -26
  103. package/dist/schemas/workflows.d.ts +19 -19
  104. package/dist/server.d.ts +13 -0
  105. package/dist/server.d.ts.map +1 -1
  106. package/dist/utils/ws.d.ts +3 -0
  107. package/dist/utils/ws.d.ts.map +1 -1
  108. package/package.json +11 -7
  109. package/src/artifacts/frame.ts +4 -13
  110. package/src/auth/scope.ts +10 -1
  111. package/src/auth/shared-access.ts +108 -12
  112. package/src/connectors/http.ts +35 -1
  113. package/src/live/README.md +136 -0
  114. package/src/live/awareness.ts +141 -0
  115. package/src/live/room.ts +933 -0
  116. package/src/live/rooms.ts +288 -0
  117. package/src/registerRoutes.ts +2 -0
  118. package/src/routes/agent-computers.ts +59 -1
  119. package/src/routes/agent-runners.ts +1 -0
  120. package/src/routes/artifact-frames.ts +40 -12
  121. package/src/routes/artifacts.ts +713 -28
  122. package/src/routes/connector-connection-runs.ts +7 -12
  123. package/src/routes/connector-connections.ts +10 -7
  124. package/src/routes/lake.ts +73 -0
  125. package/src/routes/objects.ts +3 -9
  126. package/src/routes/pipelines.ts +1 -0
  127. package/src/routes/shared-access.ts +53 -1
  128. package/src/routes/status.ts +9 -4
  129. package/src/routes/syncs.ts +1 -0
  130. package/src/routes/webhooks.ts +44 -3
  131. package/src/routes/ws/artifacts.ts +138 -0
  132. package/src/routes/ws/events.ts +7 -2
  133. package/src/routes/ws/index.ts +13 -2
  134. package/src/routes/ws/shared-sockets.ts +152 -0
  135. package/src/schemas/agents.ts +11 -0
  136. package/src/schemas/artifacts.ts +255 -18
  137. package/src/schemas/connectors.ts +17 -2
  138. package/src/schemas/files.ts +12 -0
  139. package/src/schemas/lake.ts +53 -0
  140. package/src/schemas/objects.ts +1 -0
  141. package/src/schemas/pipelines.ts +2 -0
  142. package/src/schemas/projections.ts +2 -2
  143. package/src/schemas/shared-access.ts +16 -0
  144. package/src/schemas/status.ts +16 -22
  145. package/src/schemas/syncs.ts +2 -0
  146. package/src/server.ts +35 -4
  147. package/src/utils/ws.ts +7 -0
@@ -8,6 +8,7 @@ import {
8
8
  } from "../auth/connector-callback-cookie"
9
9
  import { requireRequestSixb } from "../auth/scope"
10
10
  import {
11
+ connectorConnectionOwner,
11
12
  connectorRouteRuntime,
12
13
  handleConnectorRouteError,
13
14
  serializeConnectorConnection,
@@ -83,14 +84,11 @@ export function registerConnectorConnectionRunRoutes(
83
84
  async (context) => {
84
85
  const { params, body, request, set } = context
85
86
  try {
86
- const runtime = connectorRouteRuntime(
87
- host,
88
- requireRequestSixb(context),
89
- params.connectorId
90
- )
87
+ const sixb = requireRequestSixb(context)
88
+ const runtime = connectorRouteRuntime(host, sixb, params.connectorId)
91
89
  const parsed = StartConnectorConnectionRunBodySchema.parse(body)
92
90
  const started = await runtime.startConnectionRun(params.connectorId, {
93
- owner: { type: "project" },
91
+ owner: connectorConnectionOwner(sixb, params.connectorId, parsed.owner),
94
92
  slot: parsed.slot,
95
93
  returnTo: options.resolveReturnTo(request, parsed.returnTo),
96
94
  redirectUri: options.resolveCallbackUrl(request),
@@ -177,15 +175,12 @@ export function registerConnectorConnectionRunRoutes(
177
175
  async (context) => {
178
176
  const { params, body, set } = context
179
177
  try {
180
- const runtime = connectorRouteRuntime(
181
- host,
182
- requireRequestSixb(context),
183
- params.connectorId
184
- )
178
+ const sixb = requireRequestSixb(context)
179
+ const runtime = connectorRouteRuntime(host, sixb, params.connectorId)
185
180
  const parsed = AddConnectorConnectionBodySchema.parse(body)
186
181
  const run = await runtime.addConnection(params.connectorId, {
187
182
  fromConnectionId: params.connectionId,
188
- owner: { type: "project" },
183
+ owner: connectorConnectionOwner(sixb, params.connectorId, parsed.owner),
189
184
  slot: parsed.slot,
190
185
  })
191
186
  set.status = 201
@@ -4,6 +4,7 @@ import { requireRequestSixb } from "../auth/scope"
4
4
  import {
5
5
  connectorRouteRuntime,
6
6
  handleConnectorRouteError,
7
+ matchesConnectorConnectionOwner,
7
8
  serializeConnectorConnection,
8
9
  } from "../connectors/http"
9
10
  import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
@@ -19,6 +20,7 @@ import {
19
20
  ConnectorNotFoundResponseSchema,
20
21
  ConnectorParamsSchema,
21
22
  ConnectorUnavailableResponseSchema,
23
+ ListConnectorConnectionsQuerySchema,
22
24
  StartConnectorConnectionRunResponseSchema,
23
25
  } from "../schemas/connectors"
24
26
 
@@ -27,21 +29,22 @@ export function registerConnectorConnectionRoutes(app: Elysia, host: SixbHostVie
27
29
  .get(
28
30
  "/api/connectors/:connectorId/connections",
29
31
  async (context) => {
30
- const { params, set } = context
32
+ const { params, query, set } = context
31
33
  try {
32
- const runtime = connectorRouteRuntime(
33
- host,
34
- requireRequestSixb(context),
35
- params.connectorId
36
- )
34
+ const sixb = requireRequestSixb(context)
35
+ const runtime = connectorRouteRuntime(host, sixb, params.connectorId)
36
+ const { owner } = ListConnectorConnectionsQuerySchema.parse(query)
37
37
  const connections = await runtime.listConnections(params.connectorId)
38
- return connections.map(serializeConnectorConnection)
38
+ return connections
39
+ .filter((connection) => matchesConnectorConnectionOwner(sixb, connection, owner))
40
+ .map(serializeConnectorConnection)
39
41
  } catch (error) {
40
42
  return handleConnectorRouteError(error, set)
41
43
  }
42
44
  },
43
45
  {
44
46
  params: ConnectorParamsSchema,
47
+ query: ListConnectorConnectionsQuerySchema,
45
48
  response: {
46
49
  200: ConnectorConnectionSchema.array(),
47
50
  400: ConnectorBadRequestResponseSchema,
@@ -0,0 +1,73 @@
1
+ import type { SixbHostView } from "@sixb/core"
2
+ import { getLakeRetentionRuntime } from "@sixb/core/internal/runtime"
3
+ import type { DatasetIngestSummary } from "@sixb/core/storage"
4
+ import type { Elysia } from "elysia"
5
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
6
+ import { requireRequestSixb } from "../auth/scope"
7
+ import { OPENAPI_TAGS } from "../openapi/tags"
8
+ import { ErrorResponseSchema } from "../schemas/common"
9
+ import { LakeHealthResponseSchema } from "../schemas/lake"
10
+ import { handleRouteError } from "../utils/http"
11
+
12
+ export function registerLakeRoutes(app: Elysia, host: SixbHostView) {
13
+ return app.get(
14
+ "/api/lake/health",
15
+ async (context) => {
16
+ const { set } = context
17
+ const sixb = requireRequestSixb(context)
18
+
19
+ try {
20
+ const retention = getLakeRetentionRuntime(host)
21
+ const { settings, maintenance } = retention
22
+ const cleanup = {
23
+ ...(await retention.status()),
24
+ keepVersionsFor: settings.keepVersionsFor,
25
+ deleteFilesAfter: settings.deleteFilesAfter,
26
+ cleanupEvery: settings.cleanupEvery,
27
+ }
28
+ if (maintenance === null) return { available: false, cleanup, datasets: [] }
29
+
30
+ // The datasets the caller may view, as `/api/datasets` lists them.
31
+ const datasetIds = sixb.datasets.list().map((dataset) => dataset.id)
32
+ if (datasetIds.length === 0) {
33
+ return LakeHealthResponseSchema.parse({ available: true, cleanup, datasets: [] })
34
+ }
35
+
36
+ const [storage, waiting] = await Promise.all([
37
+ maintenance.describeStorage(datasetIds),
38
+ host.storage.datasetIngest?.summarize({ projectId: host.id, datasetIds }) ?? [],
39
+ ])
40
+ const waitingById = new Map(waiting.map((summary) => [summary.datasetId, summary]))
41
+ const datasets = storage.map((dataset) => ({
42
+ ...dataset,
43
+ ingest: ingestWaiting(waitingById.get(dataset.datasetId)),
44
+ }))
45
+
46
+ return LakeHealthResponseSchema.parse({ available: true, cleanup, datasets })
47
+ } catch (error) {
48
+ return handleRouteError(error, set)
49
+ }
50
+ },
51
+ {
52
+ response: { 200: LakeHealthResponseSchema, 400: ErrorResponseSchema },
53
+ detail: {
54
+ summary: "Describe how the lake stores each dataset, and what its retention does",
55
+ tags: [OPENAPI_TAGS.datasets.name],
56
+ operationId: "getLakeHealth",
57
+ security: accessTokenSecurityRequirement("getLakeHealth"),
58
+ },
59
+ }
60
+ )
61
+ }
62
+
63
+ /** What waits for a flush, or null when nothing does. */
64
+ function ingestWaiting(summary: DatasetIngestSummary | undefined) {
65
+ if (!summary || (summary.pendingDeliveries === 0 && summary.rejectedDeliveries === 0)) return null
66
+
67
+ return {
68
+ pendingDeliveries: summary.pendingDeliveries,
69
+ pendingChanges: summary.pendingChanges,
70
+ oldestReceivedAt: summary.oldestReceivedAt?.toISOString() ?? null,
71
+ rejectedDeliveries: summary.rejectedDeliveries,
72
+ }
73
+ }
@@ -21,7 +21,7 @@ import {
21
21
  } from "../files/content"
22
22
  import { OPENAPI_TAGS } from "../openapi/tags"
23
23
  import { ErrorResponseSchema } from "../schemas/common"
24
- import { FileContentQuerySchema } from "../schemas/files"
24
+ import { FileContentQuerySchema, ShareGrantQuerySchema } from "../schemas/files"
25
25
  import { ObjectLineageSchema } from "../schemas/lineage"
26
26
  import {
27
27
  GetObjectByUniquePropertyBodySchema,
@@ -42,14 +42,8 @@ import {
42
42
  import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
43
43
  import { noteRequestSubject } from "../utils/request-timing"
44
44
 
45
- // Only object files are served to shared sessions. The boundary reads `shareGrant` before routing;
46
- // declaring it here keeps it in the OpenAPI contract and through query validation.
47
- const ObjectFileContentRouteQuerySchema = FileContentQuerySchema.extend({
48
- shareGrant: z
49
- .string()
50
- .optional()
51
- .describe("Share grant selector for elements that cannot send the x-sixb-share-grant header"),
52
- })
45
+ // Object files are served to shared sessions, which name their grant in the query.
46
+ const ObjectFileContentRouteQuerySchema = FileContentQuerySchema.extend(ShareGrantQuerySchema.shape)
53
47
 
54
48
  const ObjectFileContentQuerySchema = ObjectFileContentRouteQuerySchema.extend({
55
49
  path: z
@@ -29,6 +29,7 @@ function serializePipelineRun(run: PipelineRunRecord) {
29
29
  projectId: run.projectId,
30
30
  pipelineId: run.pipelineId,
31
31
  status: run.status,
32
+ attempt: run.attempt,
32
33
  queuedAt: toIsoString(run.queuedAt),
33
34
  startedAt: run.startedAt ? toIsoString(run.startedAt) : undefined,
34
35
  finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
@@ -1,4 +1,4 @@
1
- import type { SharedSessionContext } from "@sixb/core/internal/shares"
1
+ import { GUEST_NAME_MAX_LENGTH, type SharedSessionContext } from "@sixb/core/internal/shares"
2
2
  import type { Elysia } from "elysia"
3
3
  import { CSRF_TOKEN_RESPONSE_HEADER_NAME } from "../auth/csrf"
4
4
  import {
@@ -7,12 +7,14 @@ import {
7
7
  sharedAccessSecurityHeaders,
8
8
  sharedAccessUnauthenticatedResponse,
9
9
  sharedAccessUnavailableResponse,
10
+ sharedBoundaryJsonResponse,
10
11
  } from "../auth/shared-access"
11
12
  import { SIXB_CSRF_SECURITY_SCHEME_ID } from "../openapi/security"
12
13
  import { OPENAPI_TAGS } from "../openapi/tags"
13
14
  import { ErrorResponseSchema } from "../schemas/common"
14
15
  import {
15
16
  ExchangeSharedAccessBodySchema,
17
+ SetSharedAccessGuestNameBodySchema,
16
18
  SharedAccessGrantParamsSchema,
17
19
  SharedAccessSessionResponseSchema,
18
20
  SharedAccessSignOutResponseSchema,
@@ -80,6 +82,54 @@ export function registerSharedAccessRoutes(app: Elysia, boundary: SharedAccessBo
80
82
  },
81
83
  }
82
84
  )
85
+ .put(
86
+ "/api/shared-access/:grantId/name",
87
+ async ({ body, params, request }) => {
88
+ try {
89
+ const result = await boundary.setGuestName(request, params.grantId, body.name)
90
+ switch (result.kind) {
91
+ case "csrf":
92
+ return sharedAccessForbiddenResponse()
93
+ case "invalid":
94
+ return withSetCookies(sharedAccessUnauthenticatedResponse(), result.setCookies)
95
+ case "unnamed":
96
+ return sharedBoundaryJsonResponse(
97
+ {
98
+ error: `A name holds 1 to ${GUEST_NAME_MAX_LENGTH} characters on one line.`,
99
+ code: "invalid_name",
100
+ },
101
+ 400
102
+ )
103
+ case "named":
104
+ return sharedSessionResponse(result.context, result.cookies)
105
+ }
106
+ } catch {
107
+ return sharedAccessUnavailableResponse()
108
+ }
109
+ },
110
+ {
111
+ params: SharedAccessGrantParamsSchema,
112
+ body: SetSharedAccessGuestNameBodySchema,
113
+ response: {
114
+ 200: SharedAccessSessionResponseSchema,
115
+ 400: ErrorResponseSchema,
116
+ 401: ErrorResponseSchema,
117
+ 403: ErrorResponseSchema,
118
+ 503: ErrorResponseSchema,
119
+ },
120
+ error: sharedAccessRouteError,
121
+ detail: {
122
+ summary: "Name a shared-access session's visitor",
123
+ description:
124
+ "The visitor of a shared link has no account: they give their name once, and their comments and live presence carry it, marked as a guest's. Giving it again replaces it for what comes next; comments keep the name they were written under.",
125
+ tags: [OPENAPI_TAGS.sharedAccess.name],
126
+ operationId: "setSharedAccessGuestName",
127
+ // Public to the auth guard, as sign-out is: the session cookie the path's grant names,
128
+ // and its CSRF token, are checked here.
129
+ security: [{ [SIXB_CSRF_SECURITY_SCHEME_ID]: [] }, {}],
130
+ },
131
+ }
132
+ )
83
133
  .post(
84
134
  "/api/shared-access/:grantId/sign-out",
85
135
  async ({ params, request }) => {
@@ -133,6 +183,8 @@ function sharedSessionResponse(
133
183
  expiresAt: context.expiresAt.toISOString(),
134
184
  absoluteExpiresAt: context.absoluteExpiresAt.toISOString(),
135
185
  csrfToken: cookies.csrfToken,
186
+ guestId: context.sessionId,
187
+ ...(context.guestName === undefined ? {} : { guestName: context.guestName }),
136
188
  }),
137
189
  { status: 200, headers }
138
190
  )
@@ -37,10 +37,15 @@ export function registerStatusRoutes(app: Elysia, host: SixbHostView) {
37
37
 
38
38
  return app.get(
39
39
  "/api/status",
40
- async () => ({
41
- ...host.getOntologyOperationalStatus(),
42
- objectTypes: host.definitions.ontology.listObjectTypes().length,
43
- }),
40
+ async () => {
41
+ const { status, maintenance } = await host.getOntologyOperationalStatus()
42
+
43
+ return {
44
+ status,
45
+ objectTypes: host.definitions.ontology.listObjectTypes().length,
46
+ maintenance: { ...maintenance, passes: [...maintenance.passes] },
47
+ }
48
+ },
44
49
  {
45
50
  response: { 200: StatusResponseSchema },
46
51
  detail: {
@@ -30,6 +30,7 @@ function serializeSyncRun(run: SyncRunRecord) {
30
30
  datasetId: run.datasetId,
31
31
  mode: run.mode,
32
32
  status: run.status,
33
+ attempt: run.attempt,
33
34
  queuedAt: toIsoString(run.queuedAt),
34
35
  startedAt: run.startedAt ? toIsoString(run.startedAt) : undefined,
35
36
  finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
@@ -18,9 +18,14 @@ import {
18
18
  type AdmitWebhookRunResult,
19
19
  admitWebhookRun,
20
20
  createWebhookConnections,
21
+ holdWebhookRun,
21
22
  isStaticConnectorDefinition,
22
23
  } from "@sixb/core/internal/webhooks"
23
- import type { WebhookRunFailureCode } from "@sixb/core/storage"
24
+ import type {
25
+ WebhookRunExecution,
26
+ WebhookRunFailureCode,
27
+ WebhookRunRecord,
28
+ } from "@sixb/core/storage"
24
29
  import { WEBHOOK_RUN_FAILURE_CODES, WebhookRunError } from "@sixb/core/storage"
25
30
  import type { Elysia } from "elysia"
26
31
  import { RequestBodyTooLargeError, readRequestBodyWithLimit } from "../utils/request-body"
@@ -60,11 +65,13 @@ type WebhookRunFailure = SixbFailure<WebhookRunFailureCode>
60
65
  type FinishWebhookRunInput =
61
66
  | {
62
67
  readonly runId: string
68
+ readonly executionToken: string
63
69
  readonly status: "succeeded"
64
70
  readonly responseStatus: number
65
71
  }
66
72
  | {
67
73
  readonly runId: string
74
+ readonly executionToken: string
68
75
  readonly status: "failed"
69
76
  readonly responseStatus: number
70
77
  readonly failure: WebhookRunFailure
@@ -92,8 +99,16 @@ async function dispatchWebhook(options: DispatchWebhookOptions): Promise<unknown
92
99
 
93
100
  const admission = await admitPreparedWebhook(options, candidateRunId, prepared.request)
94
101
  if (admission.status === "responded") return admission.response
102
+ if (admission.status === "in_progress") return retryLater(options.set, admission.run)
95
103
  if (admission.status !== "admitted") return accepted(options.set)
96
104
 
105
+ const execution = requireWebhookExecution(admission.run)
106
+ const stopHolding = holdWebhookRun({
107
+ storage: options.host.storage,
108
+ projectId: options.host.id,
109
+ runId: admission.run.id,
110
+ execution,
111
+ })
97
112
  const executionLogs = options.host.logging.startExecution({
98
113
  kind: "webhook",
99
114
  id: admission.run.id,
@@ -103,9 +118,11 @@ async function dispatchWebhook(options: DispatchWebhookOptions): Promise<unknown
103
118
  options,
104
119
  prepared.request,
105
120
  admission,
121
+ execution,
106
122
  executionLogs.withContext({ phase: "handle" })
107
123
  )
108
124
  } finally {
125
+ stopHolding()
109
126
  await executionLogs.flush()
110
127
  }
111
128
  }
@@ -214,6 +231,7 @@ async function executeWebhookHandler(
214
231
  options: DispatchWebhookOptions,
215
232
  prepared: PreparedWebhookRequest,
216
233
  admission: Extract<AdmitWebhookRunResult, { readonly status: "admitted" }>,
234
+ { token: executionToken }: WebhookRunExecution,
217
235
  logger: Logger
218
236
  ): Promise<unknown> {
219
237
  const { host, registered, request, set } = options
@@ -251,6 +269,7 @@ async function executeWebhookHandler(
251
269
  try {
252
270
  await finishWebhookRun(host, {
253
271
  runId,
272
+ executionToken,
254
273
  status: "failed",
255
274
  responseStatus: 500,
256
275
  failure: failed.failure,
@@ -278,8 +297,8 @@ async function executeWebhookHandler(
278
297
  await finishWebhookRun(
279
298
  host,
280
299
  failed
281
- ? { runId, status: "failed", responseStatus, failure: failed.failure }
282
- : { runId, status: "succeeded", responseStatus }
300
+ ? { runId, executionToken, status: "failed", responseStatus, failure: failed.failure }
301
+ : { runId, executionToken, status: "succeeded", responseStatus }
283
302
  )
284
303
  } catch (error) {
285
304
  const failure = captureSixbFailure(error, {
@@ -302,6 +321,27 @@ async function executeWebhookHandler(
302
321
  return applyWebhookResponse(set, response)
303
322
  }
304
323
 
324
+ /**
325
+ * The handler may still fail, so "accepted" could lose the delivery: a provider stops retrying once
326
+ * it hears that. It deduplicates by the same key, so answering "later" is safe either way.
327
+ */
328
+ function retryLater(set: DispatchWebhookOptions["set"], run: WebhookRunRecord): unknown {
329
+ const untilLeaseEnds = (run.execution?.leaseExpiresAt.getTime() ?? 0) - Date.now()
330
+
331
+ set.status = 503
332
+ setHeader(set, "retry-after", String(Math.max(1, Math.ceil(untilLeaseEnds / 1_000))))
333
+ return { error: "Webhook delivery in progress" }
334
+ }
335
+
336
+ function requireWebhookExecution(run: WebhookRunRecord): WebhookRunExecution {
337
+ if (run.execution) return run.execution
338
+
339
+ throw createSixbError(
340
+ "internal.unexpected",
341
+ `[SixbServer] Admitted webhook run '${run.id}' has no execution.`
342
+ )
343
+ }
344
+
305
345
  function createFailureReporter(
306
346
  host: SixbHostView,
307
347
  registered: RegisteredWebhook,
@@ -335,6 +375,7 @@ async function finishWebhookRun(host: SixbHostView, input: FinishWebhookRunInput
335
375
  const terminal = {
336
376
  id: input.runId,
337
377
  projectId: host.id,
378
+ executionToken: input.executionToken,
338
379
  responseStatus: input.responseStatus,
339
380
  }
340
381
  await storage.finish(
@@ -0,0 +1,138 @@
1
+ import { randomUUID } from "node:crypto"
2
+ import { ARTIFACT_LIVE_ROUTE, type ArtifactLiveClientMessage } from "@sixb/core/artifacts/live"
3
+ import type { Elysia } from "elysia"
4
+ import { z } from "zod"
5
+ import { guestLiveUser } from "../../live/awareness"
6
+ import type { LiveConnection } from "../../live/room"
7
+ import { LiveRooms } from "../../live/rooms"
8
+ import type { SixbServer } from "../../server"
9
+ import {
10
+ decodeWsMessage,
11
+ safeSend,
12
+ wsRequestSixb,
13
+ wsSharedSession,
14
+ wsStateKey,
15
+ } from "../../utils/ws"
16
+ import type { ClosableSocket, SharedSockets } from "./shared-sockets"
17
+
18
+ const ArtifactId = z.string().min(1).max(200)
19
+
20
+ const LiveMessageSchema = z.discriminatedUnion("type", [
21
+ z.object({ type: z.literal("join"), artifactId: ArtifactId }),
22
+ z.object({ type: z.literal("leave"), artifactId: ArtifactId }),
23
+ z.object({
24
+ type: z.literal("sync"),
25
+ artifactId: ArtifactId,
26
+ data: z.string().min(1),
27
+ more: z.literal(true).optional(),
28
+ }),
29
+ z.object({ type: z.literal("awareness"), artifactId: ArtifactId, data: z.string().min(1) }),
30
+ ])
31
+
32
+ /**
33
+ * The message as JSON. Elysia hands a JSON text over parsed, and the live messages carry their
34
+ * bytes in `data`, which `decodeWsMessage` would take for a wrapper: a parsed message is kept.
35
+ */
36
+ async function decodeLiveMessage(message: unknown): Promise<unknown> {
37
+ if (typeof message === "object" && message !== null && "type" in message) return message
38
+ return decodeWsMessage(message)
39
+ }
40
+
41
+ export function parseArtifactLiveMessage(
42
+ payload: unknown
43
+ ): { ok: true; data: ArtifactLiveClientMessage } | { ok: false; error: string } {
44
+ const parsed = LiveMessageSchema.safeParse(payload)
45
+ if (!parsed.success) {
46
+ return { ok: false, error: parsed.error.issues[0]?.message ?? "Invalid websocket message." }
47
+ }
48
+ return { ok: true, data: parsed.data }
49
+ }
50
+
51
+ /**
52
+ * `/ws/artifacts`: artifacts edited live, through Yjs's sync and awareness protocols. Whoever may
53
+ * change an artifact edits it; whoever may only read it watches, a shared link's guest included,
54
+ * under the name they gave. See `../../live/README.md`.
55
+ */
56
+ export function registerArtifactLiveRoutes(app: Elysia, server: SixbServer, shared: SharedSockets) {
57
+ const host = server.getHost()
58
+ const rooms = new LiveRooms({
59
+ projectId: host.id,
60
+ storage: host.storage,
61
+ blobStorage: host.blobStorage,
62
+ broker: host.broker,
63
+ events: host.events,
64
+ instance: randomUUID(),
65
+ errorReporterHost: host,
66
+ })
67
+ const connections = new WeakMap<object, LiveConnection>()
68
+ /** The socket's connection, as the person its request authenticated. */
69
+ const connectionOf = (
70
+ ws: ClosableSocket & { send: (message: string) => void }
71
+ ): LiveConnection | null => {
72
+ const known = connections.get(wsStateKey(ws))
73
+ if (known) return known
74
+
75
+ const sixb = wsRequestSixb(ws)
76
+ if (!sixb) return null
77
+
78
+ // A guest's SDK names nobody: their session says who they are, as it is when they join.
79
+ const guest = wsSharedSession(ws)
80
+ const connection: LiveConnection = {
81
+ id: randomUUID(),
82
+ sixb,
83
+ send: (message) => safeSend(ws, message),
84
+ ...(guest
85
+ ? {
86
+ user: async () => guestLiveUser(guest.sessionId, (await shared.session(ws))?.guestName),
87
+ }
88
+ : {}),
89
+ }
90
+ connections.set(wsStateKey(ws), connection)
91
+ return connection
92
+ }
93
+
94
+ app.onStop(() => rooms.close())
95
+ return app.ws(ARTIFACT_LIVE_ROUTE, {
96
+ open(ws) {
97
+ // A guest who gives their name shows under it, in every artifact they hold live.
98
+ shared.open(ws, () => {
99
+ const connection = connections.get(wsStateKey(ws))
100
+ if (connection) void rooms.refreshUser(connection)
101
+ })
102
+ safeSend(ws, { type: "connected", channel: "artifacts" })
103
+ },
104
+
105
+ async message(ws, message) {
106
+ if (!(await shared.holds(ws))) return
107
+ const connection = connectionOf(ws)
108
+ if (!connection) {
109
+ safeSend(ws, { type: "error", message: "Execution scope is not available." })
110
+ return
111
+ }
112
+
113
+ const parsed = parseArtifactLiveMessage(await decodeLiveMessage(message))
114
+ if (!parsed.ok) {
115
+ safeSend(ws, { type: "error", message: parsed.error })
116
+ return
117
+ }
118
+
119
+ try {
120
+ await rooms.receive(connection, parsed.data)
121
+ } catch (error) {
122
+ console.error("[SixbServer] A live artifact message failed:", error)
123
+ safeSend(ws, {
124
+ type: "error",
125
+ artifactId: parsed.data.artifactId,
126
+ message: "[SixbServer] The live artifact could not handle that message.",
127
+ })
128
+ }
129
+ },
130
+
131
+ async close(ws) {
132
+ shared.close(ws)
133
+ const connection = connections.get(wsStateKey(ws))
134
+ connections.delete(wsStateKey(ws))
135
+ if (connection) await rooms.disconnect(connection)
136
+ },
137
+ })
138
+ }
@@ -6,6 +6,7 @@ import { z } from "zod"
6
6
  import { EVENT_TOPICS, EVENT_TYPES } from "../../schemas/events"
7
7
  import type { SixbServer } from "../../server"
8
8
  import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
9
+ import type { SharedSockets } from "./shared-sockets"
9
10
  import { SubscriptionHub } from "./subscription-hub"
10
11
 
11
12
  const DEFAULT_READ_LIMIT = 200
@@ -58,7 +59,7 @@ export function parseSubscriptionMessage(payload: unknown):
58
59
  return { ok: true, data: parsed.data }
59
60
  }
60
61
 
61
- export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
62
+ export function registerEventStreamRoutes(app: Elysia, server: SixbServer, shared: SharedSockets) {
62
63
  const events = server.getHost().events
63
64
  const states = new WeakMap<object, EventSocketState>()
64
65
  const hub = new SubscriptionHub<StoredDomainEvent>({
@@ -94,7 +95,9 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
94
95
  return app.ws("/ws/events", {
95
96
  async open(ws) {
96
97
  // Any authenticated principal may connect; events are filtered per-event
97
- // by grants as they stream (see `view` below).
98
+ // by grants as they stream (see `view` below). A shared link's session receives the events
99
+ // of the artifacts it reads, while the session holds.
100
+ shared.open(ws)
98
101
  const state: EventSocketState = {
99
102
  replayFrom: null,
100
103
  initialized: Promise.resolve(),
@@ -109,6 +112,7 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
109
112
  },
110
113
 
111
114
  async message(ws, message) {
115
+ if (!(await shared.holds(ws))) return
112
116
  const decoded = await decodeWsMessage(message)
113
117
  const parsed = parseSubscriptionMessage(decoded)
114
118
  if (!parsed.ok) {
@@ -196,6 +200,7 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
196
200
  },
197
201
 
198
202
  close(ws) {
203
+ shared.close(ws)
199
204
  hub.unsubscribe(wsStateKey(ws))
200
205
  states.delete(wsStateKey(ws))
201
206
  },
@@ -1,12 +1,23 @@
1
1
  import type { Elysia } from "elysia"
2
+ import type { SharedAccessBoundary } from "../../auth/shared-access"
2
3
  import type { SixbServer } from "../../server"
3
4
  import { registerAgentStreamRoutes } from "./agents"
5
+ import { registerArtifactLiveRoutes } from "./artifacts"
4
6
  import { registerEventStreamRoutes } from "./events"
5
7
  import { registerLogStreamRoutes } from "./logs"
8
+ import { SharedSockets } from "./shared-sockets"
6
9
 
7
- export function registerWebSocketRoutes(app: Elysia, server: SixbServer) {
8
- registerEventStreamRoutes(app, server)
10
+ export function registerWebSocketRoutes(
11
+ app: Elysia,
12
+ server: SixbServer,
13
+ sharedAccess: SharedAccessBoundary
14
+ ) {
15
+ // A shared link's session opens these two only: the boundary refuses it every other socket.
16
+ const shared = new SharedSockets(sharedAccess)
17
+
18
+ registerEventStreamRoutes(app, server, shared)
9
19
  registerLogStreamRoutes(app, server)
10
20
  registerAgentStreamRoutes(app, server)
21
+ registerArtifactLiveRoutes(app, server, shared)
11
22
  return app
12
23
  }