@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.
- package/README.md +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/run-token.d.ts +28 -0
- package/dist/auth/run-token.d.ts.map +1 -0
- package/dist/auth/scope.d.ts +11 -13
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4999 -4151
- package/dist/index.js.map +47 -43
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +1 -1
- package/dist/routes/agent-approval-gate.d.ts +7 -4
- package/dist/routes/agent-approval-gate.d.ts.map +1 -1
- package/dist/routes/agent-approvals.d.ts +14 -5
- package/dist/routes/agent-approvals.d.ts.map +1 -1
- package/dist/routes/agent-children.d.ts +2 -2
- package/dist/routes/agent-computers.d.ts +338 -0
- package/dist/routes/agent-computers.d.ts.map +1 -0
- package/dist/routes/agent-runners.d.ts +74 -0
- package/dist/routes/agent-runners.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +250 -16
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/artifacts.d.ts +5 -28
- package/dist/routes/artifacts.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +10 -1
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +93 -17
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +1 -1
- package/dist/routes/links.d.ts +121 -81
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +3 -3
- package/dist/routes/objects.d.ts +149 -58
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +25 -4
- package/dist/routes/observed-agent-threads.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +2 -2
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/projections.d.ts +124 -122
- package/dist/routes/projections.d.ts.map +1 -1
- package/dist/routes/rules.d.ts +2 -2
- package/dist/routes/shared-access.d.ts +2 -2
- package/dist/routes/syncs.d.ts +34 -34
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/telemetry.d.ts +0 -50
- package/dist/routes/telemetry.d.ts.map +1 -1
- package/dist/routes/users.d.ts +1 -0
- package/dist/routes/users.d.ts.map +1 -1
- package/dist/routes/webhook-runs.d.ts +1 -1
- package/dist/routes/workflows.d.ts +25 -14
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +4 -1
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +10 -10
- package/dist/schemas/actions.d.ts +3 -3
- package/dist/schemas/activity.d.ts +4 -4
- package/dist/schemas/agent-approvals.d.ts +21 -21
- package/dist/schemas/agents.d.ts +819 -88
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1114 -6
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +49 -2
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +303 -60
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +8 -8
- package/dist/schemas/lineage.d.ts +1272 -0
- package/dist/schemas/lineage.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +4 -27
- package/dist/schemas/links.d.ts.map +1 -1
- package/dist/schemas/logs.d.ts +22 -22
- package/dist/schemas/objects.d.ts +7 -26
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +12 -12
- package/dist/schemas/projections.d.ts +747 -733
- package/dist/schemas/projections.d.ts.map +1 -1
- package/dist/schemas/rules.d.ts +4 -4
- package/dist/schemas/shared-access.d.ts +2 -2
- package/dist/schemas/syncs.d.ts +24 -24
- package/dist/schemas/telemetry.d.ts +0 -13
- package/dist/schemas/telemetry.d.ts.map +1 -1
- package/dist/schemas/users.d.ts +8 -0
- package/dist/schemas/users.d.ts.map +1 -1
- package/dist/schemas/webhook-runs.d.ts +2 -2
- package/dist/schemas/workflows.d.ts +133 -44
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/request-body.d.ts +5 -0
- package/dist/utils/request-body.d.ts.map +1 -1
- package/dist/utils/request-timing.d.ts +18 -0
- package/dist/utils/request-timing.d.ts.map +1 -0
- package/package.json +7 -7
- package/src/auth/public-routes.ts +0 -6
- package/src/auth/run-token.ts +327 -0
- package/src/auth/scope.ts +16 -37
- package/src/index.ts +1 -0
- package/src/registerRoutes.ts +4 -2
- package/src/routes/agent-approval-gate.ts +40 -14
- package/src/routes/agent-approvals.ts +3 -3
- package/src/routes/agent-computers.ts +348 -0
- package/src/routes/agent-runners.ts +205 -0
- package/src/routes/agents.ts +193 -26
- package/src/routes/ai-accounting.ts +149 -12
- package/src/routes/artifacts.ts +55 -11
- package/src/routes/auth.ts +24 -2
- package/src/routes/datasets.ts +60 -5
- package/src/routes/links.ts +43 -76
- package/src/routes/objects.ts +49 -42
- package/src/routes/observed-agent-threads.ts +2 -2
- package/src/routes/pipelines.ts +5 -0
- package/src/routes/syncs.ts +4 -0
- package/src/routes/telemetry.ts +1 -42
- package/src/routes/users.ts +15 -2
- package/src/routes/ws/agents.ts +10 -1
- package/src/schemas/agents.ts +131 -1
- package/src/schemas/ai-accounting.ts +78 -15
- package/src/schemas/artifacts.ts +1 -1
- package/src/schemas/auth.ts +19 -0
- package/src/schemas/datasets.ts +36 -0
- package/src/schemas/lineage.ts +115 -0
- package/src/schemas/links.ts +3 -11
- package/src/schemas/objects.ts +6 -10
- package/src/schemas/projections.ts +2 -0
- package/src/schemas/telemetry.ts +0 -6
- package/src/schemas/users.ts +6 -0
- package/src/server.ts +53 -15
- package/src/utils/request-body.ts +10 -4
- package/src/utils/request-timing.ts +136 -0
- package/dist/routes/agent-api-gateway.d.ts +0 -4
- package/dist/routes/agent-api-gateway.d.ts.map +0 -1
- 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
|
-
}
|