@sixb/server 0.1.25 → 0.2.0

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 (135) hide show
  1. package/README.md +1 -1
  2. package/dist/auth/public-routes.d.ts.map +1 -1
  3. package/dist/auth/run-token.d.ts +28 -0
  4. package/dist/auth/run-token.d.ts.map +1 -0
  5. package/dist/auth/scope.d.ts +11 -13
  6. package/dist/auth/scope.d.ts.map +1 -1
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +4999 -4151
  10. package/dist/index.js.map +47 -43
  11. package/dist/registerRoutes.d.ts.map +1 -1
  12. package/dist/routes/actions.d.ts +1 -1
  13. package/dist/routes/agent-approval-gate.d.ts +7 -4
  14. package/dist/routes/agent-approval-gate.d.ts.map +1 -1
  15. package/dist/routes/agent-approvals.d.ts +14 -5
  16. package/dist/routes/agent-approvals.d.ts.map +1 -1
  17. package/dist/routes/agent-children.d.ts +2 -2
  18. package/dist/routes/agent-computers.d.ts +338 -0
  19. package/dist/routes/agent-computers.d.ts.map +1 -0
  20. package/dist/routes/agent-runners.d.ts +74 -0
  21. package/dist/routes/agent-runners.d.ts.map +1 -0
  22. package/dist/routes/agents.d.ts +250 -16
  23. package/dist/routes/agents.d.ts.map +1 -1
  24. package/dist/routes/ai-accounting.d.ts.map +1 -1
  25. package/dist/routes/artifacts.d.ts +5 -28
  26. package/dist/routes/artifacts.d.ts.map +1 -1
  27. package/dist/routes/auth.d.ts +10 -1
  28. package/dist/routes/auth.d.ts.map +1 -1
  29. package/dist/routes/datasets.d.ts +93 -17
  30. package/dist/routes/datasets.d.ts.map +1 -1
  31. package/dist/routes/events.d.ts +1 -1
  32. package/dist/routes/links.d.ts +121 -81
  33. package/dist/routes/links.d.ts.map +1 -1
  34. package/dist/routes/logs.d.ts +3 -3
  35. package/dist/routes/objects.d.ts +149 -58
  36. package/dist/routes/objects.d.ts.map +1 -1
  37. package/dist/routes/observed-agent-threads.d.ts +25 -4
  38. package/dist/routes/observed-agent-threads.d.ts.map +1 -1
  39. package/dist/routes/pipelines.d.ts +2 -2
  40. package/dist/routes/pipelines.d.ts.map +1 -1
  41. package/dist/routes/projections.d.ts +124 -122
  42. package/dist/routes/projections.d.ts.map +1 -1
  43. package/dist/routes/rules.d.ts +2 -2
  44. package/dist/routes/shared-access.d.ts +2 -2
  45. package/dist/routes/syncs.d.ts +34 -34
  46. package/dist/routes/syncs.d.ts.map +1 -1
  47. package/dist/routes/telemetry.d.ts +0 -50
  48. package/dist/routes/telemetry.d.ts.map +1 -1
  49. package/dist/routes/users.d.ts +1 -0
  50. package/dist/routes/users.d.ts.map +1 -1
  51. package/dist/routes/webhook-runs.d.ts +1 -1
  52. package/dist/routes/workflows.d.ts +25 -14
  53. package/dist/routes/workflows.d.ts.map +1 -1
  54. package/dist/routes/ws/agents.d.ts +4 -1
  55. package/dist/routes/ws/agents.d.ts.map +1 -1
  56. package/dist/routes/ws/logs.d.ts +10 -10
  57. package/dist/schemas/actions.d.ts +3 -3
  58. package/dist/schemas/activity.d.ts +4 -4
  59. package/dist/schemas/agent-approvals.d.ts +21 -21
  60. package/dist/schemas/agents.d.ts +819 -88
  61. package/dist/schemas/agents.d.ts.map +1 -1
  62. package/dist/schemas/ai-accounting.d.ts +1114 -6
  63. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  64. package/dist/schemas/auth.d.ts +49 -2
  65. package/dist/schemas/auth.d.ts.map +1 -1
  66. package/dist/schemas/datasets.d.ts +303 -60
  67. package/dist/schemas/datasets.d.ts.map +1 -1
  68. package/dist/schemas/event-payloads.d.ts +6 -6
  69. package/dist/schemas/events.d.ts +8 -8
  70. package/dist/schemas/lineage.d.ts +1272 -0
  71. package/dist/schemas/lineage.d.ts.map +1 -0
  72. package/dist/schemas/links.d.ts +4 -27
  73. package/dist/schemas/links.d.ts.map +1 -1
  74. package/dist/schemas/logs.d.ts +22 -22
  75. package/dist/schemas/objects.d.ts +7 -26
  76. package/dist/schemas/objects.d.ts.map +1 -1
  77. package/dist/schemas/pipelines.d.ts +12 -12
  78. package/dist/schemas/projections.d.ts +747 -733
  79. package/dist/schemas/projections.d.ts.map +1 -1
  80. package/dist/schemas/rules.d.ts +4 -4
  81. package/dist/schemas/shared-access.d.ts +2 -2
  82. package/dist/schemas/syncs.d.ts +24 -24
  83. package/dist/schemas/telemetry.d.ts +0 -13
  84. package/dist/schemas/telemetry.d.ts.map +1 -1
  85. package/dist/schemas/users.d.ts +8 -0
  86. package/dist/schemas/users.d.ts.map +1 -1
  87. package/dist/schemas/webhook-runs.d.ts +2 -2
  88. package/dist/schemas/workflows.d.ts +133 -44
  89. package/dist/schemas/workflows.d.ts.map +1 -1
  90. package/dist/server.d.ts +5 -0
  91. package/dist/server.d.ts.map +1 -1
  92. package/dist/utils/request-body.d.ts +5 -0
  93. package/dist/utils/request-body.d.ts.map +1 -1
  94. package/dist/utils/request-timing.d.ts +18 -0
  95. package/dist/utils/request-timing.d.ts.map +1 -0
  96. package/package.json +7 -7
  97. package/src/auth/public-routes.ts +0 -6
  98. package/src/auth/run-token.ts +327 -0
  99. package/src/auth/scope.ts +16 -37
  100. package/src/index.ts +1 -0
  101. package/src/registerRoutes.ts +4 -2
  102. package/src/routes/agent-approval-gate.ts +40 -14
  103. package/src/routes/agent-approvals.ts +3 -3
  104. package/src/routes/agent-computers.ts +348 -0
  105. package/src/routes/agent-runners.ts +205 -0
  106. package/src/routes/agents.ts +193 -26
  107. package/src/routes/ai-accounting.ts +149 -12
  108. package/src/routes/artifacts.ts +55 -11
  109. package/src/routes/auth.ts +24 -2
  110. package/src/routes/datasets.ts +60 -5
  111. package/src/routes/links.ts +43 -76
  112. package/src/routes/objects.ts +49 -42
  113. package/src/routes/observed-agent-threads.ts +2 -2
  114. package/src/routes/pipelines.ts +5 -0
  115. package/src/routes/syncs.ts +4 -0
  116. package/src/routes/telemetry.ts +1 -42
  117. package/src/routes/users.ts +15 -2
  118. package/src/routes/ws/agents.ts +10 -1
  119. package/src/schemas/agents.ts +131 -1
  120. package/src/schemas/ai-accounting.ts +78 -15
  121. package/src/schemas/artifacts.ts +1 -1
  122. package/src/schemas/auth.ts +19 -0
  123. package/src/schemas/datasets.ts +36 -0
  124. package/src/schemas/lineage.ts +115 -0
  125. package/src/schemas/links.ts +3 -11
  126. package/src/schemas/objects.ts +6 -10
  127. package/src/schemas/projections.ts +2 -0
  128. package/src/schemas/telemetry.ts +0 -6
  129. package/src/schemas/users.ts +6 -0
  130. package/src/server.ts +53 -15
  131. package/src/utils/request-body.ts +10 -4
  132. package/src/utils/request-timing.ts +136 -0
  133. package/dist/routes/agent-api-gateway.d.ts +0 -4
  134. package/dist/routes/agent-api-gateway.d.ts.map +0 -1
  135. package/src/routes/agent-api-gateway.ts +0 -404
@@ -1,404 +0,0 @@
1
- import type { SixbHostView } from "@sixb/core"
2
- import {
3
- AGENT_API_GATEWAY_PREFIX,
4
- AGENT_API_ROUTES,
5
- type AgentRunExecutionScope,
6
- AgentRunScopeError,
7
- isAllowedAgentApiRequest,
8
- isValidAgentApiGatewayCapability,
9
- resolveAgentRunExecutionScope,
10
- } from "@sixb/core/internal/agents"
11
- import { normalizeRoutePath, pathSegmentsFor } from "@sixb/core/internal/http"
12
- import type {
13
- AgentRunRecord,
14
- ConversationAgentRunRecord,
15
- SubagentRunRecord,
16
- WorkflowAgentNodeRunRecord,
17
- } from "@sixb/core/storage"
18
- import type { Elysia } from "elysia"
19
- import { type InternalRequestAuthState, registerInternalRequestAuthState } from "../auth/scope"
20
- import { RequestBodyTooLargeError, readRequestBodyWithLimit } from "../utils/request-body"
21
- import { gateAgentApiCall } from "./agent-approval-gate"
22
- import { DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES } from "./files"
23
-
24
- const MAX_AGENT_API_BODY_BYTES = 1_000_000
25
- const HOP_BY_HOP_RESPONSE_HEADERS = new Set([
26
- "connection",
27
- "keep-alive",
28
- "proxy-authenticate",
29
- "proxy-authorization",
30
- "te",
31
- "trailer",
32
- "transfer-encoding",
33
- "upgrade",
34
- ])
35
-
36
- export function registerAgentApiGatewayRoutes(app: Elysia, host: SixbHostView) {
37
- for (const route of AGENT_API_ROUTES) {
38
- const path = `${AGENT_API_GATEWAY_PREFIX}/:agentGatewayRunId/:agentGatewayCapability${route.path}`
39
- const handler = async (context: AgentApiGatewayRouteContext) =>
40
- handleAgentApiGatewayRequest({
41
- host,
42
- app,
43
- request: context.request,
44
- agentRunId: context.params.agentGatewayRunId ?? "",
45
- capability: context.params.agentGatewayCapability ?? "",
46
- })
47
-
48
- if (route.method === "GET") {
49
- app.get(path, handler, { detail: { hide: true } })
50
- continue
51
- }
52
- if (route.method === "POST") {
53
- app.post(path, handler, { detail: { hide: true } })
54
- continue
55
- }
56
-
57
- throw new Error(`[SixbServer] Unsupported agent API gateway method '${route.method}'.`)
58
- }
59
- }
60
-
61
- interface AgentApiGatewayRouteContext {
62
- readonly request: Request
63
- readonly params: Readonly<Record<string, string | undefined>>
64
- }
65
-
66
- async function handleAgentApiGatewayRequest(input: {
67
- readonly host: SixbHostView
68
- readonly app: Elysia
69
- readonly request: Request
70
- readonly agentRunId: string
71
- readonly capability: string
72
- }): Promise<Response> {
73
- const sourceUrl = new URL(input.request.url)
74
- const upstreamPath = gatewayUpstreamPath(sourceUrl.pathname)
75
- if (!isAllowedAgentApiRequest(input.request.method, upstreamPath)) {
76
- return jsonError(
77
- 403,
78
- `Agent API gateway does not allow ${input.request.method} ${upstreamPath}.`
79
- )
80
- }
81
-
82
- const resolved = await resolveAgentRunAuthState(input.host, {
83
- runId: input.agentRunId,
84
- capability: input.capability,
85
- })
86
- if (resolved instanceof Response) {
87
- return resolved
88
- }
89
- const { authState, run } = resolved
90
- if (
91
- authState.agentExecution.kind !== "conversation" &&
92
- input.request.method === "POST" &&
93
- matchesWorkflowRunStart(upstreamPath)
94
- ) {
95
- return jsonError(
96
- 409,
97
- authState.agentExecution.kind === "workflow"
98
- ? "Workflow agent nodes cannot start another workflow run."
99
- : "Child agents cannot start a workflow run."
100
- )
101
- }
102
- // A workflow step is a fixed assignment: what it does must not change because a conversation
103
- // saved a note.
104
- if (authState.agentExecution.kind === "workflow" && matchesAgentMemory(upstreamPath)) {
105
- return jsonError(403, "Workflow agent nodes have no Agent memory.")
106
- }
107
- // A child works for its turn: once the turn has ended, or a retry has taken back its work, the
108
- // child changes nothing more.
109
- if ("kind" in run && run.kind === "subagent" && !(await parentRunActive(input.host, run))) {
110
- return jsonError(409, "The child agent's parent run has ended.")
111
- }
112
- // A child reads what its parent's conversation remembers; only the conversation saves to it.
113
- if (
114
- authState.agentExecution.kind === "subagent" &&
115
- input.request.method !== "GET" &&
116
- matchesAgentMemory(upstreamPath)
117
- ) {
118
- return jsonError(403, "Child agents cannot save Agent memory.")
119
- }
120
-
121
- // Others read a shared conversation: whoever sends the turn, their own memory stays out of it.
122
- // Anything but the project's exact path is refused, as a route parameter is percent-decoded.
123
- if (
124
- authState.agentRun &&
125
- matchesAgentMemory(upstreamPath) &&
126
- pathSegmentsFor(upstreamPath)[2] !== "project" &&
127
- (await hasParticipants(input.host, authState.agentRun))
128
- ) {
129
- return jsonError(
130
- 403,
131
- "A shared conversation reaches only the project's memory. Save nothing about one person here."
132
- )
133
- }
134
-
135
- let body: Uint8Array<ArrayBuffer> | undefined
136
- try {
137
- body = await readGatewayRequestBody(input.request, upstreamPath)
138
- } catch (error) {
139
- if (error instanceof RequestBodyTooLargeError) {
140
- return jsonError(413, error.message)
141
- }
142
- throw error
143
- }
144
-
145
- // The run's approval mode decides, never the model: a held call does not reach its route.
146
- const gate = await gateAgentApiCall({
147
- host: input.host,
148
- method: input.request.method,
149
- upstreamPath,
150
- body,
151
- run,
152
- agentExecution: authState.agentExecution,
153
- runExecution: authState.runExecution,
154
- signal: input.request.signal,
155
- })
156
- if (gate.kind === "answer") return gate.response
157
-
158
- const internalUrl = new URL(`${upstreamPath}${sourceUrl.search}`, input.request.url)
159
- // The caller's signal travels with the request: a sandbox client that gives up cancels the
160
- // forwarded work, model calls included.
161
- const internalRequest = new Request(internalUrl, {
162
- method: input.request.method,
163
- headers: forwardedRequestHeaders(input.request.headers),
164
- body,
165
- signal: input.request.signal,
166
- })
167
-
168
- registerInternalRequestAuthState(internalRequest, authState)
169
-
170
- const response = await input.app.fetch(internalRequest)
171
- return new Response(response.body, {
172
- status: response.status,
173
- statusText: response.statusText,
174
- headers: forwardedResponseHeaders(response.headers),
175
- })
176
- }
177
-
178
- async function resolveAgentRunAuthState(
179
- host: SixbHostView,
180
- input: { readonly runId: string; readonly capability: string }
181
- ) {
182
- const agentStorage = host.storage.agents
183
- const workflowRuns = host.storage.workflowRuns
184
- if (!agentStorage && !workflowRuns) {
185
- // 501, not 403: nothing about the caller is being refused — neither storage role
186
- // that could hold an agent run is configured, so no request can ever succeed here.
187
- return jsonError(501, "Agent API gateway is not configured for this runtime.")
188
- }
189
-
190
- const storedAgentRun = await agentStorage?.runs.getById({
191
- projectId: host.id,
192
- id: input.runId,
193
- })
194
- const workflowRun = storedAgentRun
195
- ? null
196
- : await workflowRuns?.agentNodes.getByNodeRunId({
197
- projectId: host.id,
198
- nodeRunId: input.runId,
199
- })
200
- const runState = toGatewayRunState(storedAgentRun ?? null, workflowRun ?? null)
201
- if (
202
- !runState ||
203
- runState.run.status !== "running" ||
204
- !runState.run.execution ||
205
- runState.run.execution.queueLeaseExpiresAt.getTime() <= Date.now() ||
206
- !isValidAgentApiGatewayCapability({
207
- projectId: host.id,
208
- runId: input.runId,
209
- executionToken: runState.run.execution.token,
210
- capability: input.capability,
211
- })
212
- ) {
213
- return jsonError(403, "Agent API gateway capability is not valid for this run.")
214
- }
215
-
216
- let scope: AgentRunExecutionScope
217
- try {
218
- scope = await resolveAgentRunExecutionScope(host, {
219
- runId: runState.runId,
220
- executionId: runState.run.executionId,
221
- authority:
222
- runState.authority === "managed"
223
- ? { type: "managed", actorId: runState.actorId }
224
- : { type: "inherited" },
225
- })
226
- } catch (error) {
227
- if (!(error instanceof AgentRunScopeError)) throw error
228
- return scopeErrorResponse(error)
229
- }
230
-
231
- // The request runs inside the run's own execution, restored from server-side records only
232
- // (run, then its execution record): what it starts is a child of the run and keeps the run's
233
- // requester and correlation, whatever the request's headers say. Authority is the record's
234
- // current grants, resolved on every request when it is admitted; a request admitted before the
235
- // run ends finishes under it, like an in-process tool call. An agent execution also gets
236
- // provider access (blobs, models, connectors), and so does any route added to the allowlist.
237
- return {
238
- authState: {
239
- runExecution: { ...scope, attempt: runState.run.attempt },
240
- agentExecution: runState.agentExecution,
241
- ...(runState.agentRun === undefined ? {} : { agentRun: runState.agentRun }),
242
- },
243
- run: runState.run,
244
- }
245
- }
246
-
247
- type GatewayRunState = {
248
- readonly run: AgentRunRecord | WorkflowAgentNodeRunRecord
249
- readonly runId: string
250
- readonly agentExecution: NonNullable<InternalRequestAuthState["agentExecution"]>
251
- readonly agentRun?: ConversationAgentRunRecord
252
- } & (
253
- | { readonly authority: "inherited" }
254
- | { readonly authority: "managed"; readonly actorId: string }
255
- )
256
-
257
- function toGatewayRunState(
258
- storedRun: AgentRunRecord | null,
259
- workflowRun: WorkflowAgentNodeRunRecord | null
260
- ): GatewayRunState | null {
261
- if (storedRun?.kind === "subagent") {
262
- return {
263
- run: storedRun,
264
- runId: storedRun.id,
265
- agentExecution: {
266
- kind: "subagent",
267
- runId: storedRun.id,
268
- parentRunId: storedRun.parentRunId,
269
- },
270
- authority: "inherited",
271
- }
272
- }
273
-
274
- if (storedRun?.kind === "conversation") {
275
- const common = {
276
- run: storedRun,
277
- runId: storedRun.id,
278
- agentExecution: { kind: "conversation", runId: storedRun.id } as const,
279
- agentRun: storedRun,
280
- }
281
- return { ...common, authority: "inherited" }
282
- }
283
-
284
- if (!workflowRun) return null
285
- const common = {
286
- run: workflowRun,
287
- runId: workflowRun.nodeRunId,
288
- agentExecution: { kind: "workflow", nodeRunId: workflowRun.nodeRunId } as const,
289
- }
290
- return { ...common, authority: "managed", actorId: workflowRun.actorId }
291
- }
292
-
293
- function scopeErrorResponse(error: AgentRunScopeError): Response {
294
- switch (error.code) {
295
- case "execution_unavailable":
296
- return jsonError(403, "Agent run execution is not available.")
297
- case "auth_unavailable":
298
- return jsonError(501, "Agent API gateway is not configured for authenticated agent access.")
299
- case "authority_invalid":
300
- return jsonError(403, "Agent run execution authority is not valid.")
301
- }
302
- }
303
-
304
- function gatewayUpstreamPath(pathname: string): string {
305
- const prefixLength = pathSegmentsFor(AGENT_API_GATEWAY_PREFIX).length
306
- const segments = pathSegmentsFor(pathname)
307
- const upstreamSegments = segments.slice(prefixLength + 2)
308
- return normalizeRoutePath(`/${upstreamSegments.join("/")}`)
309
- }
310
-
311
- function forwardedRequestHeaders(source: Headers): Headers {
312
- const headers = new Headers()
313
- copyHeader(source, headers, "accept")
314
- copyHeader(source, headers, "content-type")
315
- return headers
316
- }
317
-
318
- function forwardedResponseHeaders(source: Headers): Headers {
319
- const headers = new Headers()
320
- for (const [key, value] of source) {
321
- const normalized = key.toLowerCase()
322
- if (
323
- HOP_BY_HOP_RESPONSE_HEADERS.has(normalized) ||
324
- normalized === "content-length" ||
325
- normalized === "set-cookie"
326
- ) {
327
- continue
328
- }
329
- headers.append(key, value)
330
- }
331
- return headers
332
- }
333
-
334
- function copyHeader(source: Headers, target: Headers, name: string): void {
335
- const value = source.get(name)
336
- if (value !== null) {
337
- target.set(name, value)
338
- }
339
- }
340
-
341
- async function readGatewayRequestBody(
342
- request: Request,
343
- upstreamPath: string
344
- ): Promise<Uint8Array<ArrayBuffer> | undefined> {
345
- if (!request.body || request.method === "GET" || request.method === "HEAD") {
346
- return undefined
347
- }
348
-
349
- if (isSimpleFileUpload(request.method, upstreamPath)) {
350
- return readRequestBodyWithLimit(
351
- request,
352
- DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES,
353
- `Agent API gateway file upload request body exceeds the ${DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES} byte limit.`
354
- )
355
- }
356
- return readRequestBodyWithLimit(
357
- request,
358
- MAX_AGENT_API_BODY_BYTES,
359
- "Agent API gateway request body exceeds 1MB."
360
- )
361
- }
362
-
363
- function isSimpleFileUpload(method: string, pathname: string): boolean {
364
- return method === "POST" && normalizeRoutePath(pathname) === "/api/files"
365
- }
366
-
367
- async function parentRunActive(host: SixbHostView, run: SubagentRunRecord): Promise<boolean> {
368
- const parent = await host.storage.agents?.runs.getById({
369
- projectId: host.id,
370
- id: run.parentRunId,
371
- })
372
- return parent?.status === "queued" || parent?.status === "running"
373
- }
374
-
375
- function matchesAgentMemory(pathname: string): boolean {
376
- const segments = pathSegmentsFor(pathname)
377
- return segments[0] === "api" && segments[1] === "agent-memory"
378
- }
379
-
380
- /** Anyone added to the run's thread, even one who cannot read it now, as the worker counts them. */
381
- async function hasParticipants(
382
- host: SixbHostView,
383
- run: ConversationAgentRunRecord
384
- ): Promise<boolean> {
385
- const participants = await host.storage.agents?.threads.listParticipants({
386
- projectId: run.projectId,
387
- threadIds: [run.threadId],
388
- })
389
- return (participants?.length ?? 0) > 0
390
- }
391
-
392
- function matchesWorkflowRunStart(pathname: string): boolean {
393
- const segments = pathSegmentsFor(pathname)
394
- return (
395
- segments.length === 4 &&
396
- segments[0] === "api" &&
397
- segments[1] === "workflows" &&
398
- segments[3] === "runs"
399
- )
400
- }
401
-
402
- function jsonError(status: number, message: string): Response {
403
- return Response.json({ error: message }, { status })
404
- }