@sixb/server 0.1.20 → 0.1.22
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/artifacts/frame.d.ts +18 -0
- package/dist/artifacts/frame.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +10 -1
- package/dist/auth/browser-origin.d.ts.map +1 -1
- package/dist/auth/experience.d.ts +1 -0
- package/dist/auth/experience.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/responses.d.ts +1 -1
- package/dist/auth/responses.d.ts.map +1 -1
- package/dist/auth/scope.d.ts +19 -3
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/files/content.d.ts +2 -3
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.js +10636 -7071
- package/dist/index.js.map +56 -38
- package/dist/models/display.d.ts +27 -0
- package/dist/models/display.d.ts.map +1 -1
- package/dist/openapi/components.d.ts +14 -0
- package/dist/openapi/components.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +22 -2
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +3 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +5 -5
- package/dist/routes/activity.d.ts +35 -0
- package/dist/routes/activity.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -1
- package/dist/routes/agent-approval-gate.d.ts +27 -0
- package/dist/routes/agent-approval-gate.d.ts.map +1 -0
- package/dist/routes/agent-approvals.d.ts +599 -0
- package/dist/routes/agent-approvals.d.ts.map +1 -0
- package/dist/routes/agent-children.d.ts +318 -0
- package/dist/routes/agent-children.d.ts.map +1 -0
- package/dist/routes/agent-memory.d.ts +267 -0
- package/dist/routes/agent-memory.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1008 -98
- 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 +98 -0
- package/dist/routes/artifact-frames.d.ts.map +1 -0
- package/dist/routes/artifacts.d.ts +1508 -0
- package/dist/routes/artifacts.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +117 -11
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/connector-connection-runs.d.ts +84 -84
- package/dist/routes/connector-connections.d.ts +2 -2
- package/dist/routes/connectors.d.ts +2 -2
- package/dist/routes/datasets.d.ts +27 -27
- package/dist/routes/events.d.ts +5 -5
- package/dist/routes/files.d.ts +14 -14
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/links.d.ts +2 -2
- package/dist/routes/logs.d.ts +5 -5
- package/dist/routes/model-calls.d.ts +155 -0
- package/dist/routes/model-calls.d.ts.map +1 -0
- package/dist/routes/model-generations.d.ts +400 -0
- package/dist/routes/model-generations.d.ts.map +1 -0
- package/dist/routes/models.d.ts +434 -1
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +71 -40
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +682 -0
- package/dist/routes/observed-agent-threads.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +66 -62
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +53 -53
- package/dist/routes/projections.d.ts +63 -63
- package/dist/routes/rules.d.ts +2 -2
- package/dist/routes/share-grants.d.ts +4 -4
- package/dist/routes/syncs.d.ts +18 -18
- package/dist/routes/users.d.ts +117 -0
- package/dist/routes/users.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +1 -1
- package/dist/routes/workflows.d.ts +142 -76
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +7 -2
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts +6 -6
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +14 -14
- package/dist/routes/ws/subscription-hub.d.ts +8 -3
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -1
- package/dist/schemas/actions.d.ts +15 -15
- package/dist/schemas/activity.d.ts +1298 -0
- package/dist/schemas/activity.d.ts.map +1 -0
- package/dist/schemas/agent-approvals.d.ts +1091 -0
- package/dist/schemas/agent-approvals.d.ts.map +1 -0
- package/dist/schemas/agent-memory.d.ts +250 -0
- package/dist/schemas/agent-memory.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +5485 -460
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1315 -436
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-limits.d.ts +36 -36
- package/dist/schemas/ai-usage.d.ts +12 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -1
- package/dist/schemas/artifacts.d.ts +1857 -0
- package/dist/schemas/artifacts.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +125 -52
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/connectors.d.ts +42 -42
- package/dist/schemas/datasets.d.ts +108 -108
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +32 -32
- package/dist/schemas/events.d.ts.map +1 -1
- package/dist/schemas/files.d.ts +24 -24
- package/dist/schemas/links.d.ts +2 -2
- package/dist/schemas/logs.d.ts +32 -32
- package/dist/schemas/models.d.ts +8889 -10
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/objects.d.ts +48 -12
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +127 -104
- package/dist/schemas/ontology.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +166 -166
- package/dist/schemas/projections.d.ts +346 -346
- package/dist/schemas/rules.d.ts +4 -4
- package/dist/schemas/share-grants.d.ts +14 -14
- package/dist/schemas/syncs.d.ts +40 -40
- package/dist/schemas/users.d.ts +72 -0
- package/dist/schemas/users.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +6 -6
- package/dist/schemas/workflows.d.ts +769 -254
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/http.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/artifacts/frame.ts +51 -0
- package/src/auth/browser-origin.ts +20 -5
- package/src/auth/experience.ts +1 -1
- package/src/auth/guard.ts +2 -2
- package/src/auth/public-routes.ts +16 -0
- package/src/auth/responses.ts +7 -6
- package/src/auth/scope.ts +32 -3
- package/src/files/content.ts +9 -13
- package/src/models/display.ts +61 -16
- package/src/openapi/components.ts +54 -0
- package/src/openapi/tags.ts +20 -0
- package/src/registerRoutes.ts +23 -0
- package/src/routes/activity.ts +327 -0
- package/src/routes/agent-api-gateway.ts +115 -91
- package/src/routes/agent-approval-gate.ts +213 -0
- package/src/routes/agent-approvals.ts +299 -0
- package/src/routes/agent-children.ts +209 -0
- package/src/routes/agent-memory.ts +210 -0
- package/src/routes/agents.ts +364 -143
- package/src/routes/ai-accounting.ts +4 -0
- package/src/routes/artifact-frames.ts +144 -0
- package/src/routes/artifacts.ts +717 -0
- package/src/routes/auth.ts +306 -13
- package/src/routes/file-downloads.ts +1 -3
- package/src/routes/files.ts +6 -7
- package/src/routes/model-calls.ts +120 -0
- package/src/routes/model-generations.ts +245 -0
- package/src/routes/models.ts +116 -9
- package/src/routes/objects.ts +55 -61
- package/src/routes/observed-agent-threads.ts +253 -0
- package/src/routes/ontology.ts +1 -0
- package/src/routes/users.ts +73 -0
- package/src/routes/workflows.ts +6 -9
- package/src/routes/ws/agents.ts +10 -5
- package/src/routes/ws/events.ts +3 -2
- package/src/routes/ws/subscription-hub.ts +53 -20
- package/src/schemas/activity.ts +287 -0
- package/src/schemas/agent-approvals.ts +129 -0
- package/src/schemas/agent-memory.ts +96 -0
- package/src/schemas/agents.ts +240 -2
- package/src/schemas/ai-accounting.ts +40 -4
- package/src/schemas/ai-usage.ts +10 -0
- package/src/schemas/artifacts.ts +246 -0
- package/src/schemas/auth.ts +25 -0
- package/src/schemas/models.ts +197 -1
- package/src/schemas/objects.ts +181 -0
- package/src/schemas/ontology.ts +1 -0
- package/src/schemas/users.ts +24 -0
- package/src/schemas/workflows.ts +2 -0
- package/src/server.ts +34 -19
- package/src/utils/http.ts +2 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import type { SixbHostView } from "@sixb/core"
|
|
2
|
+
import { bindAgentGatewayRequestExecution } from "@sixb/core/internal/agent-execution"
|
|
3
|
+
import {
|
|
4
|
+
agentRunApprovalMode,
|
|
5
|
+
announceAgentApprovals,
|
|
6
|
+
approvalCallSummary,
|
|
7
|
+
findAgentApiRoute,
|
|
8
|
+
holdAgentApprovalCall,
|
|
9
|
+
} from "@sixb/core/internal/agents"
|
|
10
|
+
import { pathSegmentsFor } from "@sixb/core/internal/http"
|
|
11
|
+
import {
|
|
12
|
+
prepareBoundActionRequest,
|
|
13
|
+
prepareBoundWorkflowRequest,
|
|
14
|
+
} from "@sixb/core/internal/request-execution"
|
|
15
|
+
import {
|
|
16
|
+
type AgentApprovalCall,
|
|
17
|
+
type AgentApprovalRecord,
|
|
18
|
+
type AgentRunRecord,
|
|
19
|
+
AgentStorageError,
|
|
20
|
+
type WorkflowAgentNodeRunRecord,
|
|
21
|
+
} from "@sixb/core/storage"
|
|
22
|
+
import { ZodError, type z } from "zod"
|
|
23
|
+
import type { AgentGatewayRunExecution, RequestAuthState } from "../auth/scope"
|
|
24
|
+
import { RequestActionBodySchema } from "../schemas/actions"
|
|
25
|
+
import { RequestWorkflowRunBodySchema } from "../schemas/workflows"
|
|
26
|
+
import { handleRouteError } from "../utils/http"
|
|
27
|
+
|
|
28
|
+
/** What the gateway does with an Agent's call: forward it, or answer it itself. */
|
|
29
|
+
export type AgentApiGate =
|
|
30
|
+
| { readonly kind: "pass" }
|
|
31
|
+
| { readonly kind: "answer"; readonly response: Response }
|
|
32
|
+
|
|
33
|
+
export interface AgentApiGateInput {
|
|
34
|
+
readonly host: SixbHostView
|
|
35
|
+
readonly method: string
|
|
36
|
+
readonly upstreamPath: string
|
|
37
|
+
readonly body: Uint8Array | undefined
|
|
38
|
+
readonly run: AgentRunRecord | WorkflowAgentNodeRunRecord
|
|
39
|
+
readonly agentExecution: NonNullable<RequestAuthState["agentExecution"]>
|
|
40
|
+
readonly runExecution: AgentGatewayRunExecution
|
|
41
|
+
readonly signal: AbortSignal
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Hold an action or a workflow start the turn's approval mode does not run. Core decides; this
|
|
46
|
+
* reads the call the way its route would, and answers for it. A call the route would refuse is
|
|
47
|
+
* refused here, before anything is held.
|
|
48
|
+
*/
|
|
49
|
+
export async function gateAgentApiCall(input: AgentApiGateInput): Promise<AgentApiGate> {
|
|
50
|
+
const effect = findAgentApiRoute(input.method, input.upstreamPath)?.effect
|
|
51
|
+
if (effect !== "action" && effect !== "workflow") return { kind: "pass" }
|
|
52
|
+
// A workflow AI task runs on its own: it has no mode.
|
|
53
|
+
if (input.agentExecution.kind === "workflow" || !isAgentRun(input.run)) return { kind: "pass" }
|
|
54
|
+
if (agentRunApprovalMode(input.run) === "auto") return { kind: "pass" }
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const call = effect === "action" ? await prepareActionCall(input) : prepareWorkflowCall(input)
|
|
58
|
+
return await answerHold(input, input.run, call)
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof GateRefusal) return { kind: "answer", response: error.response }
|
|
61
|
+
throw error
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function answerHold(
|
|
66
|
+
input: AgentApiGateInput,
|
|
67
|
+
run: AgentRunRecord,
|
|
68
|
+
call: AgentApprovalCall
|
|
69
|
+
): Promise<AgentApiGate> {
|
|
70
|
+
const agents = input.host.storage.agents
|
|
71
|
+
if (!agents) throw refusal(501, "Agent approvals are not configured for this runtime.")
|
|
72
|
+
|
|
73
|
+
let hold: Awaited<ReturnType<typeof holdAgentApprovalCall>>
|
|
74
|
+
try {
|
|
75
|
+
hold = await holdAgentApprovalCall(agents, {
|
|
76
|
+
projectId: input.host.id,
|
|
77
|
+
run,
|
|
78
|
+
requestedBy: input.runExecution.execution.requestedBy,
|
|
79
|
+
call,
|
|
80
|
+
})
|
|
81
|
+
} catch (error) {
|
|
82
|
+
// The run ended, or its delivery changed, since the gateway checked its capability.
|
|
83
|
+
if (!(error instanceof AgentStorageError)) throw error
|
|
84
|
+
throw refusal(403, "Agent API gateway capability is not valid for this run.")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
switch (hold.kind) {
|
|
88
|
+
case "pass":
|
|
89
|
+
return { kind: "pass" }
|
|
90
|
+
case "held":
|
|
91
|
+
// Announced on every answer: the feed keeps one record per change.
|
|
92
|
+
await announceAgentApprovals(input.host.broker, [hold.approval])
|
|
93
|
+
return {
|
|
94
|
+
kind: "answer",
|
|
95
|
+
response: Response.json(heldCallBody(hold.approval), { status: 202 }),
|
|
96
|
+
}
|
|
97
|
+
case "denied":
|
|
98
|
+
throw refusal(403, "The user denied this request. It did not run.")
|
|
99
|
+
case "conflict":
|
|
100
|
+
throw refusal(
|
|
101
|
+
409,
|
|
102
|
+
`Run id '${hold.approval.callKey}' already names another request in this conversation. Name a new run.`
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isAgentRun(run: AgentRunRecord | WorkflowAgentNodeRunRecord): run is AgentRunRecord {
|
|
108
|
+
return "kind" in run
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The Action call as its route would run it: admitted, with its params normalized. */
|
|
112
|
+
async function prepareActionCall(input: AgentApiGateInput): Promise<AgentApprovalCall> {
|
|
113
|
+
const body = parseBody(input.body, RequestActionBodySchema)
|
|
114
|
+
const actionRunId = requiredRunId(body.runId)
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const prepared = await prepareBoundActionRequest(boundSixb(input), {
|
|
118
|
+
actionId: pathParam(input.upstreamPath),
|
|
119
|
+
...(body.subject === undefined ? {} : { subject: body.subject }),
|
|
120
|
+
...(body.params === undefined ? {} : { params: body.params }),
|
|
121
|
+
})
|
|
122
|
+
return { kind: "action", ...prepared, actionRunId }
|
|
123
|
+
} catch (error) {
|
|
124
|
+
throw routeRefusal(error)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The workflow start as its route would run it: admitted, with its input snapshotted. */
|
|
129
|
+
function prepareWorkflowCall(input: AgentApiGateInput): AgentApprovalCall {
|
|
130
|
+
const body = parseBody(input.body, RequestWorkflowRunBodySchema)
|
|
131
|
+
const workflowRunId = requiredRunId(body.runId)
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const prepared = prepareBoundWorkflowRequest(boundSixb(input), {
|
|
135
|
+
workflowId: pathParam(input.upstreamPath),
|
|
136
|
+
input: body.input ?? {},
|
|
137
|
+
})
|
|
138
|
+
return { kind: "workflow", ...prepared, workflowRunId }
|
|
139
|
+
} catch (error) {
|
|
140
|
+
throw routeRefusal(error)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function boundSixb(input: AgentApiGateInput): object {
|
|
145
|
+
return bindAgentGatewayRequestExecution(input.host, {
|
|
146
|
+
signal: input.signal,
|
|
147
|
+
...input.runExecution,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The run id the call names is what keeps a repeated request to one approval and one run. */
|
|
152
|
+
function requiredRunId(runId: string | undefined): string {
|
|
153
|
+
if (runId !== undefined) return runId
|
|
154
|
+
throw refusal(
|
|
155
|
+
400,
|
|
156
|
+
"This request needs the user's approval, so it must name its run: pass a non-empty runId."
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `/api/actions/:actionId` and `/api/workflows/:workflowId/runs` name their target third. */
|
|
161
|
+
function pathParam(upstreamPath: string): string {
|
|
162
|
+
const segment = pathSegmentsFor(upstreamPath)[2] ?? ""
|
|
163
|
+
try {
|
|
164
|
+
return decodeURIComponent(segment)
|
|
165
|
+
} catch {
|
|
166
|
+
return segment
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Parsed with the route's own schema, so the gate reads the body the route would. */
|
|
171
|
+
function parseBody<TSchema extends z.ZodTypeAny>(
|
|
172
|
+
body: Uint8Array | undefined,
|
|
173
|
+
schema: TSchema
|
|
174
|
+
): z.output<TSchema> {
|
|
175
|
+
try {
|
|
176
|
+
const text = body && body.byteLength > 0 ? new TextDecoder().decode(body) : "{}"
|
|
177
|
+
return schema.parse(JSON.parse(text))
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof SyntaxError) throw refusal(400, "The request body must be valid JSON.")
|
|
180
|
+
if (error instanceof ZodError) throw refusal(400, "The request body is not valid.")
|
|
181
|
+
throw error
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The gate answers for the call itself: it never reaches its route. */
|
|
186
|
+
class GateRefusal extends Error {
|
|
187
|
+
constructor(readonly response: Response) {
|
|
188
|
+
super("[SixbServer] The agent API gateway answered the call itself.")
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function refusal(status: number, message: string): GateRefusal {
|
|
193
|
+
return new GateRefusal(Response.json({ error: message }, { status }))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** A call the route would refuse is answered as the route would answer it. */
|
|
197
|
+
function routeRefusal(error: unknown): GateRefusal {
|
|
198
|
+
const set: { status?: number | string } = {}
|
|
199
|
+
const body = handleRouteError(error, set)
|
|
200
|
+
return new GateRefusal(
|
|
201
|
+
Response.json(body, { status: typeof set.status === "number" ? set.status : 500 })
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** What the CLI prints for a held call: the model reads it and ends its turn. */
|
|
206
|
+
function heldCallBody(approval: AgentApprovalRecord) {
|
|
207
|
+
return {
|
|
208
|
+
status: "awaiting_approval",
|
|
209
|
+
approval: { id: approval.id, ...approvalCallSummary(approval.call) },
|
|
210
|
+
message:
|
|
211
|
+
"The user approves or denies this in the conversation. Do not retry it or ask in text; end your turn.",
|
|
212
|
+
} as const
|
|
213
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { AgentApprovalRecord, SixbHostView } from "@sixb/core"
|
|
2
|
+
import { agentApprovalCallers } from "@sixb/core/internal/agents"
|
|
3
|
+
import type { AgentStorage } from "@sixb/core/storage"
|
|
4
|
+
import type { Elysia } from "elysia"
|
|
5
|
+
import { requireRequestSixb } from "../auth/scope"
|
|
6
|
+
import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
7
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
8
|
+
import {
|
|
9
|
+
AgentApprovalListQuerySchema,
|
|
10
|
+
AgentApprovalListResponseSchema,
|
|
11
|
+
AgentApprovalParamsSchema,
|
|
12
|
+
AgentApprovalSchema,
|
|
13
|
+
AgentPreferencesBodySchema,
|
|
14
|
+
AgentPreferencesSchema,
|
|
15
|
+
AgentThreadApprovalModeBodySchema,
|
|
16
|
+
DecideAgentApprovalBodySchema,
|
|
17
|
+
MyAgentApprovalListQuerySchema,
|
|
18
|
+
} from "../schemas/agent-approvals"
|
|
19
|
+
import {
|
|
20
|
+
AgentRequestErrorResponseSchema,
|
|
21
|
+
AgentThreadParamsSchema,
|
|
22
|
+
AgentThreadSchema,
|
|
23
|
+
DecideAgentApprovalResponseSchema,
|
|
24
|
+
} from "../schemas/agents"
|
|
25
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
26
|
+
import { parseOptionalInt, toIsoString, unconfiguredStorageResponse } from "../utils/http"
|
|
27
|
+
import { handleAgentRouteError, serializeAgentRun, serializeThread } from "./agents"
|
|
28
|
+
|
|
29
|
+
const DECIDING =
|
|
30
|
+
"Only whoever asked for the turn decides: an approved call runs now, as the turn, with the rights their request carries, and the approval comes back with its outcome. A turn paused on its calls resumes once all of them are decided: `run` is the turn that continues it, and the Agent reads how each call ended. Once the call ran, the decision never fails: when the turn cannot resume, `resume` says why, and whether Sixb tries again, as it does once an AI limit resets. A new message sets the calls still waiting aside. None of these routes is reachable from the Agent's sandbox."
|
|
31
|
+
|
|
32
|
+
type SerializedApproval = ReturnType<typeof AgentApprovalSchema.parse>
|
|
33
|
+
|
|
34
|
+
/** Each with the sub-agent that made it, when its turn did not make it itself. */
|
|
35
|
+
async function serializeApprovals(
|
|
36
|
+
agents: AgentStorage,
|
|
37
|
+
approvals: readonly AgentApprovalRecord[]
|
|
38
|
+
): Promise<SerializedApproval[]> {
|
|
39
|
+
const projectId = approvals[0]?.projectId
|
|
40
|
+
const callers = projectId ? await agentApprovalCallers(agents, projectId, approvals) : new Map()
|
|
41
|
+
|
|
42
|
+
return approvals.map((approval) =>
|
|
43
|
+
serializeApproval(approval, callers.get(approval.callerRunId)?.key)
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function serializeApproval(approval: AgentApprovalRecord, callerKey?: string): SerializedApproval {
|
|
48
|
+
return AgentApprovalSchema.parse({
|
|
49
|
+
id: approval.id,
|
|
50
|
+
threadId: approval.threadId,
|
|
51
|
+
runId: approval.runId,
|
|
52
|
+
callerRunId: approval.callerRunId,
|
|
53
|
+
caller: callerKey === undefined ? undefined : { runId: approval.callerRunId, key: callerKey },
|
|
54
|
+
requestedBy: approval.requestedBy,
|
|
55
|
+
mode: approval.mode,
|
|
56
|
+
status: approval.status,
|
|
57
|
+
call: approval.call,
|
|
58
|
+
createdAt: toIsoString(approval.createdAt),
|
|
59
|
+
expiresAt: toIsoString(approval.expiresAt),
|
|
60
|
+
decidedAt: approval.decidedAt ? toIsoString(approval.decidedAt) : undefined,
|
|
61
|
+
decidedBy: approval.decidedBy,
|
|
62
|
+
outcome: approval.outcome,
|
|
63
|
+
reported: approval.reportedInMessageId !== undefined,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Approval modes and held calls. Session routes only, and never on the agent API gateway. */
|
|
68
|
+
export function registerAgentApprovalRoutes(app: Elysia, host: SixbHostView) {
|
|
69
|
+
const missingStorage = (set: { status?: number | string }) =>
|
|
70
|
+
unconfiguredStorageResponse(set, "Agent storage")
|
|
71
|
+
|
|
72
|
+
return app
|
|
73
|
+
.get(
|
|
74
|
+
"/api/agent/preferences",
|
|
75
|
+
async (context) => {
|
|
76
|
+
const { set } = context
|
|
77
|
+
try {
|
|
78
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
79
|
+
return AgentPreferencesSchema.parse(
|
|
80
|
+
await requireRequestSixb(context).agent.preferences.get()
|
|
81
|
+
)
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return handleAgentRouteError(error, set)
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
response: {
|
|
88
|
+
200: AgentPreferencesSchema,
|
|
89
|
+
404: AgentRequestErrorResponseSchema,
|
|
90
|
+
501: ErrorResponseSchema,
|
|
91
|
+
},
|
|
92
|
+
detail: {
|
|
93
|
+
summary: "Get your Agent preferences",
|
|
94
|
+
tags: [OPENAPI_TAGS.agents.name],
|
|
95
|
+
operationId: "getAgentPreferences",
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
.put(
|
|
100
|
+
"/api/agent/preferences",
|
|
101
|
+
async (context) => {
|
|
102
|
+
const { body, set } = context
|
|
103
|
+
try {
|
|
104
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
105
|
+
const parsed = AgentPreferencesBodySchema.parse(body)
|
|
106
|
+
return AgentPreferencesSchema.parse(
|
|
107
|
+
await requireRequestSixb(context).agent.preferences.update(parsed)
|
|
108
|
+
)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return handleAgentRouteError(error, set)
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
body: AgentPreferencesBodySchema,
|
|
115
|
+
response: {
|
|
116
|
+
200: AgentPreferencesSchema,
|
|
117
|
+
400: AgentRequestErrorResponseSchema,
|
|
118
|
+
404: AgentRequestErrorResponseSchema,
|
|
119
|
+
501: ErrorResponseSchema,
|
|
120
|
+
},
|
|
121
|
+
detail: {
|
|
122
|
+
summary: "Update your Agent preferences",
|
|
123
|
+
description:
|
|
124
|
+
"`approvalMode` is your default in the threads where you chose none: `manual` asks before every action and workflow, `accept-edits` runs the actions that only change Sixb, `auto` runs everything.",
|
|
125
|
+
tags: [OPENAPI_TAGS.agents.name],
|
|
126
|
+
operationId: "updateAgentPreferences",
|
|
127
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
.put(
|
|
132
|
+
"/api/agent-threads/:threadId/approval-mode",
|
|
133
|
+
async (context) => {
|
|
134
|
+
const { params, body, set } = context
|
|
135
|
+
const sixb = requireRequestSixb(context)
|
|
136
|
+
try {
|
|
137
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
138
|
+
const parsed = AgentThreadApprovalModeBodySchema.parse(body)
|
|
139
|
+
const approvalMode = await sixb.agent.threads.setApprovalMode(
|
|
140
|
+
params.threadId,
|
|
141
|
+
parsed.approvalMode
|
|
142
|
+
)
|
|
143
|
+
const thread = await sixb.agent.threads.getById(params.threadId)
|
|
144
|
+
if (!thread) {
|
|
145
|
+
set.status = 404
|
|
146
|
+
return { error: "Agent thread not found" }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { ...(await serializeThread(host, thread)), ...approvalMode }
|
|
150
|
+
} catch (error) {
|
|
151
|
+
return handleAgentRouteError(error, set)
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
params: AgentThreadParamsSchema,
|
|
156
|
+
body: AgentThreadApprovalModeBodySchema,
|
|
157
|
+
response: {
|
|
158
|
+
200: AgentThreadSchema,
|
|
159
|
+
400: AgentRequestErrorResponseSchema,
|
|
160
|
+
404: AgentRequestErrorResponseSchema,
|
|
161
|
+
409: AgentRequestErrorResponseSchema,
|
|
162
|
+
501: ErrorResponseSchema,
|
|
163
|
+
},
|
|
164
|
+
detail: {
|
|
165
|
+
summary: "Choose your approval mode in an agent thread",
|
|
166
|
+
description:
|
|
167
|
+
"Applies to your next turns in the thread; other participants keep theirs. `null` follows your default again. A message can still pass its own `approvalMode`.",
|
|
168
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
169
|
+
operationId: "setAgentThreadApprovalMode",
|
|
170
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
.get(
|
|
175
|
+
"/api/agent-threads/:threadId/approvals",
|
|
176
|
+
async (context) => {
|
|
177
|
+
const { params, query, set } = context
|
|
178
|
+
const sixb = requireRequestSixb(context)
|
|
179
|
+
try {
|
|
180
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
181
|
+
if (!(await sixb.agent.threads.getById(params.threadId))) {
|
|
182
|
+
set.status = 404
|
|
183
|
+
return { error: "Agent thread not found" }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const parsed = AgentApprovalListQuerySchema.parse(query)
|
|
187
|
+
const result = await sixb.agent.approvals.list({
|
|
188
|
+
threadId: params.threadId,
|
|
189
|
+
...(parsed.runId === undefined ? {} : { runId: parsed.runId }),
|
|
190
|
+
...(parsed.status === undefined ? {} : { statuses: [parsed.status] }),
|
|
191
|
+
limit: parseOptionalInt(parsed.limit),
|
|
192
|
+
offset: parseOptionalInt(parsed.offset),
|
|
193
|
+
})
|
|
194
|
+
return AgentApprovalListResponseSchema.parse({
|
|
195
|
+
...result,
|
|
196
|
+
approvals: await serializeApprovals(host.storage.agents, result.approvals),
|
|
197
|
+
})
|
|
198
|
+
} catch (error) {
|
|
199
|
+
return handleAgentRouteError(error, set)
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
params: AgentThreadParamsSchema,
|
|
204
|
+
query: AgentApprovalListQuerySchema,
|
|
205
|
+
response: {
|
|
206
|
+
200: AgentApprovalListResponseSchema,
|
|
207
|
+
400: ErrorResponseSchema,
|
|
208
|
+
404: ErrorResponseSchema,
|
|
209
|
+
501: ErrorResponseSchema,
|
|
210
|
+
},
|
|
211
|
+
detail: {
|
|
212
|
+
summary: "List an agent thread's held calls",
|
|
213
|
+
description: "Oldest first. Whoever reads the thread reads its approvals.",
|
|
214
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
215
|
+
operationId: "listAgentThreadApprovals",
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
)
|
|
219
|
+
.post(
|
|
220
|
+
"/api/agent-threads/:threadId/approvals/:approvalId/decision",
|
|
221
|
+
async (context) => {
|
|
222
|
+
const { params, body, set } = context
|
|
223
|
+
const sixb = requireRequestSixb(context)
|
|
224
|
+
try {
|
|
225
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
226
|
+
const approval = await sixb.agent.approvals.getById(params.approvalId)
|
|
227
|
+
if (!approval || approval.threadId !== params.threadId) {
|
|
228
|
+
set.status = 404
|
|
229
|
+
return { error: "Agent approval not found" }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const parsed = DecideAgentApprovalBodySchema.parse(body)
|
|
233
|
+
const decided = await sixb.agent.approvals.decide(approval.id, parsed)
|
|
234
|
+
const [serialized] = await serializeApprovals(host.storage.agents, [decided.approval])
|
|
235
|
+
return DecideAgentApprovalResponseSchema.parse({
|
|
236
|
+
approval: serialized,
|
|
237
|
+
...(decided.run === undefined ? {} : { run: serializeAgentRun(decided.run) }),
|
|
238
|
+
...(decided.resume === undefined ? {} : { resume: decided.resume }),
|
|
239
|
+
})
|
|
240
|
+
} catch (error) {
|
|
241
|
+
return handleAgentRouteError(error, set)
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
params: AgentApprovalParamsSchema,
|
|
246
|
+
body: DecideAgentApprovalBodySchema,
|
|
247
|
+
response: {
|
|
248
|
+
200: DecideAgentApprovalResponseSchema,
|
|
249
|
+
400: AgentRequestErrorResponseSchema,
|
|
250
|
+
403: AgentRequestErrorResponseSchema,
|
|
251
|
+
404: ErrorResponseSchema,
|
|
252
|
+
409: AgentRequestErrorResponseSchema,
|
|
253
|
+
501: ErrorResponseSchema,
|
|
254
|
+
},
|
|
255
|
+
detail: {
|
|
256
|
+
summary: "Approve or deny a held agent call",
|
|
257
|
+
description: DECIDING,
|
|
258
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
259
|
+
operationId: "decideAgentApproval",
|
|
260
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
261
|
+
},
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
.get(
|
|
265
|
+
"/api/agent-approvals",
|
|
266
|
+
async (context) => {
|
|
267
|
+
const { query, set } = context
|
|
268
|
+
try {
|
|
269
|
+
if (!host.storage.agents) return missingStorage(set)
|
|
270
|
+
const parsed = MyAgentApprovalListQuerySchema.parse(query)
|
|
271
|
+
const result = await requireRequestSixb(context).agent.approvals.list({
|
|
272
|
+
...(parsed.status === undefined ? {} : { statuses: [parsed.status] }),
|
|
273
|
+
limit: parseOptionalInt(parsed.limit),
|
|
274
|
+
offset: parseOptionalInt(parsed.offset),
|
|
275
|
+
})
|
|
276
|
+
return AgentApprovalListResponseSchema.parse({
|
|
277
|
+
...result,
|
|
278
|
+
approvals: await serializeApprovals(host.storage.agents, result.approvals),
|
|
279
|
+
})
|
|
280
|
+
} catch (error) {
|
|
281
|
+
return handleAgentRouteError(error, set)
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
query: MyAgentApprovalListQuerySchema,
|
|
286
|
+
response: {
|
|
287
|
+
200: AgentApprovalListResponseSchema,
|
|
288
|
+
400: ErrorResponseSchema,
|
|
289
|
+
501: ErrorResponseSchema,
|
|
290
|
+
},
|
|
291
|
+
detail: {
|
|
292
|
+
summary: "List the calls held for your decision",
|
|
293
|
+
description: "Your own, across threads, oldest first.",
|
|
294
|
+
tags: [OPENAPI_TAGS.agentRuns.name],
|
|
295
|
+
operationId: "listAgentApprovals",
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
)
|
|
299
|
+
}
|