@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
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { principalsEqual, type SixbHostView, SYSTEM_PRINCIPAL } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
AgentRunRefusedError,
|
|
4
|
+
type AgentRunSessionHost,
|
|
5
|
+
afterAgentRunFinished,
|
|
6
|
+
claimAgentRunForRunner,
|
|
7
|
+
createBrokerStreamSink,
|
|
8
|
+
encodeWireError,
|
|
9
|
+
encodeWireValue,
|
|
10
|
+
endLapsedAgentRuns,
|
|
11
|
+
isAgentRunnerToken,
|
|
12
|
+
isolateStreamSink,
|
|
13
|
+
serveAgentRunSession,
|
|
14
|
+
withAgentActivityStream,
|
|
15
|
+
} from "@sixb/core/internal/agents"
|
|
16
|
+
import { enqueueAiModelCallRecovery } from "@sixb/core/internal/model-execution"
|
|
17
|
+
import type { Elysia } from "elysia"
|
|
18
|
+
import { requestActor, requireRequestSixb } from "../auth/scope"
|
|
19
|
+
import { liftRequestBodyCap } from "../utils/request-body"
|
|
20
|
+
import { computerClaiming, endLapsedComputerSessions } from "./agent-computers"
|
|
21
|
+
|
|
22
|
+
// A runner outside the worker, such as one on a person's computer, runs a turn through the API:
|
|
23
|
+
// it claims the turn as the person who asked for it, then reaches the turn's session with the
|
|
24
|
+
// runner token the claim returns (see serveAgentRunSession). Neither route is part of the public
|
|
25
|
+
// API.
|
|
26
|
+
|
|
27
|
+
/** The session host the API opens a run's session against: the worker's, without the worker. */
|
|
28
|
+
export function agentRunSessionHost(host: SixbHostView): AgentRunSessionHost | null {
|
|
29
|
+
const { storage } = host
|
|
30
|
+
if (!storage.agents || !storage.auth || !storage.aiUsage || !storage.aiCosts) return null
|
|
31
|
+
const sessions: AgentRunSessionHost = {
|
|
32
|
+
projectId: host.id,
|
|
33
|
+
storage: storage as AgentRunSessionHost["storage"],
|
|
34
|
+
broker: host.broker,
|
|
35
|
+
recoverAiModelCall: (input) => enqueueAiModelCallRecovery(host.queues.agents, input),
|
|
36
|
+
events: isolateStreamSink(
|
|
37
|
+
withAgentActivityStream(
|
|
38
|
+
createBrokerStreamSink({ broker: host.broker, projectId: host.id }),
|
|
39
|
+
host.broker
|
|
40
|
+
)
|
|
41
|
+
),
|
|
42
|
+
security: host.definitions.security,
|
|
43
|
+
sdk: host,
|
|
44
|
+
definitions: host.definitions,
|
|
45
|
+
logging: host.logging,
|
|
46
|
+
// A turn a runner finished here stops its children and resumes on calls already decided.
|
|
47
|
+
afterFinish: (run) => afterAgentRunFinished(sessions, run),
|
|
48
|
+
}
|
|
49
|
+
return sessions
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** How often the API ends the runs no runner renews any more. */
|
|
53
|
+
const SWEEP_INTERVAL_MS = 30_000
|
|
54
|
+
/** How long the claim that ends a lapsed run holds it: as long as recording the end takes. */
|
|
55
|
+
const SWEEP_CLAIM_MS = 60_000
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* End the runs whose runner stopped renewing them, as the worker's repair pass does: a runner on
|
|
59
|
+
* a computer has no worker behind it. Several API servers may sweep at once: each run is ended
|
|
60
|
+
* once, by whichever takes it over. Computer sessions that lapsed end too. Returns the stop.
|
|
61
|
+
*/
|
|
62
|
+
export function startLapsedRunSweeper(host: SixbHostView): () => void {
|
|
63
|
+
const sessions = agentRunSessionHost(host)
|
|
64
|
+
if (!sessions) return () => {}
|
|
65
|
+
// One sweep of sessions at a time: a slow one is not joined by the next.
|
|
66
|
+
let sweepingSessions = false
|
|
67
|
+
const sweep = () => {
|
|
68
|
+
void endLapsedAgentRuns(sessions, { leaseMs: SWEEP_CLAIM_MS }).catch((error: unknown) => {
|
|
69
|
+
console.error("[SixbServer] Could not end lapsed agent runs; retrying.", error)
|
|
70
|
+
})
|
|
71
|
+
if (sweepingSessions) return
|
|
72
|
+
sweepingSessions = true
|
|
73
|
+
void endLapsedComputerSessions(host)
|
|
74
|
+
.catch((error: unknown) => {
|
|
75
|
+
console.error("[SixbServer] Could not end lapsed computer sessions; retrying.", error)
|
|
76
|
+
})
|
|
77
|
+
.finally(() => {
|
|
78
|
+
sweepingSessions = false
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
sweep()
|
|
82
|
+
const timer = setInterval(sweep, SWEEP_INTERVAL_MS)
|
|
83
|
+
return () => clearInterval(timer)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Answer a runner's session request before the API parses it: its runner token speaks only for its
|
|
88
|
+
* run's session, and any other route refuses it. Null for any other request.
|
|
89
|
+
*/
|
|
90
|
+
export function agentRunSessionHandler(
|
|
91
|
+
host: SixbHostView
|
|
92
|
+
): (request: Request) => Promise<Response | null> {
|
|
93
|
+
let sessions: AgentRunSessionHost | null | undefined
|
|
94
|
+
return async (request) => {
|
|
95
|
+
sessions ??= agentRunSessionHost(host)
|
|
96
|
+
// The session reads its runner's body within its own limit, past the server's default cap.
|
|
97
|
+
const served = sessions
|
|
98
|
+
? await serveAgentRunSession(sessions, request, {
|
|
99
|
+
admitted: () => liftRequestBodyCap(request),
|
|
100
|
+
})
|
|
101
|
+
: null
|
|
102
|
+
if (served) return served
|
|
103
|
+
|
|
104
|
+
// It carries the runner's authority over its run, not anyone's over the project.
|
|
105
|
+
const token = request.headers.get("authorization")?.match(/^Bearer\s+(\S+)$/i)?.[1]
|
|
106
|
+
if (token === undefined || !isAgentRunnerToken(token)) return null
|
|
107
|
+
const { pathname } = new URL(request.url)
|
|
108
|
+
return Response.json(
|
|
109
|
+
{
|
|
110
|
+
error: `[SixbServer] A runner token cannot call ${request.method} ${pathname}: it reaches only its run's session.`,
|
|
111
|
+
},
|
|
112
|
+
{ status: 403 }
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function registerAgentRunnerRoutes(app: Elysia, host: SixbHostView) {
|
|
118
|
+
return app.post(
|
|
119
|
+
"/api/agent-runs/:runId/claim",
|
|
120
|
+
async (context) => {
|
|
121
|
+
const sessions = agentRunSessionHost(host)
|
|
122
|
+
if (!sessions) return answer({ error: notAvailable() }, 501)
|
|
123
|
+
try {
|
|
124
|
+
const sixb = requireRequestSixb(context)
|
|
125
|
+
const runId = (context.params as { readonly runId: string }).runId
|
|
126
|
+
// Elysia parsed it already: a lease is a plain string, nothing the wire tags.
|
|
127
|
+
const { leaseExpiresAt } = (context.body ?? {}) as { readonly leaseExpiresAt?: string }
|
|
128
|
+
if (typeof leaseExpiresAt !== "string") {
|
|
129
|
+
return answer(
|
|
130
|
+
{ error: { kind: "Error", message: "[SixbServer] A claim names its lease." } },
|
|
131
|
+
400
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
// As its reader: a run the caller cannot read is none of theirs.
|
|
135
|
+
const visible = await sixb.agent.runs.getById(runId)
|
|
136
|
+
const run = visible
|
|
137
|
+
? await sessions.storage.agents.runs.getById({ projectId: host.id, id: runId })
|
|
138
|
+
: null
|
|
139
|
+
const execution =
|
|
140
|
+
run &&
|
|
141
|
+
(await sessions.storage.executions.getById({ projectId: host.id, id: run.executionId }))
|
|
142
|
+
// With sign-in off, no one asked for a turn: the system did, as such a request acts.
|
|
143
|
+
if (
|
|
144
|
+
run?.kind !== "conversation" ||
|
|
145
|
+
!execution ||
|
|
146
|
+
!principalsEqual(execution.requestedBy ?? SYSTEM_PRINCIPAL, requestActor(sixb))
|
|
147
|
+
) {
|
|
148
|
+
return answer(
|
|
149
|
+
{
|
|
150
|
+
error: {
|
|
151
|
+
kind: "AgentRunRefusedError",
|
|
152
|
+
message: `[SixbServer] Only the person who asked for agent run '${runId}' runs it.`,
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
403
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
// A cloud turn is the agent worker's: a runner here would race it, and skip the sandbox
|
|
159
|
+
// the thread chose.
|
|
160
|
+
if (run.spec?.runsOn !== "computer") {
|
|
161
|
+
return answer(
|
|
162
|
+
{
|
|
163
|
+
error: {
|
|
164
|
+
kind: "AgentRunRefusedError",
|
|
165
|
+
message: `[SixbServer] Agent run '${runId}' runs in the cloud: only a computer's turns are claimed here.`,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
403
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
const claimed = await computerClaiming(sessions, run, context.body)
|
|
172
|
+
if ("refused" in claimed)
|
|
173
|
+
return answer({ error: claimed.refused.error }, claimed.refused.status)
|
|
174
|
+
return answer({
|
|
175
|
+
result: await claimAgentRunForRunner(sessions, {
|
|
176
|
+
run,
|
|
177
|
+
leaseExpiresAt,
|
|
178
|
+
onComputer: claimed.computer,
|
|
179
|
+
}),
|
|
180
|
+
})
|
|
181
|
+
} catch (error) {
|
|
182
|
+
// A turn whose runner stopped ends interrupted rather than being claimed again.
|
|
183
|
+
return answer(
|
|
184
|
+
{ error: encodeWireError(error) },
|
|
185
|
+
error instanceof AgentRunRefusedError ? 409 : 400
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
{ detail: { hide: true } }
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function notAvailable() {
|
|
194
|
+
return {
|
|
195
|
+
kind: "Error",
|
|
196
|
+
message: "[SixbServer] Agent runs are not available: this runtime stores none.",
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function answer(body: unknown, status = 200): Response {
|
|
201
|
+
return new Response(encodeWireValue(body), {
|
|
202
|
+
status,
|
|
203
|
+
headers: { "content-type": "application/json" },
|
|
204
|
+
})
|
|
205
|
+
}
|
package/src/routes/agents.ts
CHANGED
|
@@ -4,9 +4,16 @@ import {
|
|
|
4
4
|
type AgentRunView,
|
|
5
5
|
type AgentThreadView,
|
|
6
6
|
type FileRef,
|
|
7
|
+
type LanguageModelEntry,
|
|
8
|
+
principalsEqual,
|
|
7
9
|
type SixbHostView,
|
|
8
10
|
} from "@sixb/core"
|
|
9
|
-
import {
|
|
11
|
+
import { sameComputerPlace } from "@sixb/core/internal/agent-run-storage-provider"
|
|
12
|
+
import {
|
|
13
|
+
assertConversationThread,
|
|
14
|
+
isRetryableAgentRun,
|
|
15
|
+
readAgentThreadContextUse,
|
|
16
|
+
} from "@sixb/core/internal/agents"
|
|
10
17
|
import { agentRunStreamId } from "@sixb/core/internal/agents/streams"
|
|
11
18
|
import { resolveFileRefAtPath } from "@sixb/core/internal/blob-storage"
|
|
12
19
|
import {
|
|
@@ -15,18 +22,25 @@ import {
|
|
|
15
22
|
type AgentRunDiagnostic,
|
|
16
23
|
type AgentStorage,
|
|
17
24
|
AgentStorageError,
|
|
25
|
+
type AgentThreadComputer,
|
|
18
26
|
type AgentThreadRecord,
|
|
19
27
|
type Principal,
|
|
20
28
|
} from "@sixb/core/storage"
|
|
21
29
|
import type { Elysia } from "elysia"
|
|
22
30
|
import { ZodError, z } from "zod"
|
|
23
31
|
import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
|
|
24
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
type RequestAuthState,
|
|
34
|
+
requestActor,
|
|
35
|
+
requestAuthState,
|
|
36
|
+
requireRequestSixb,
|
|
37
|
+
} from "../auth/scope"
|
|
25
38
|
import {
|
|
26
39
|
createFileContentResponse,
|
|
27
40
|
fileContentGetResponses,
|
|
28
41
|
fileContentHeadResponses,
|
|
29
42
|
} from "../files/content"
|
|
43
|
+
import { resolvedForDisplay } from "../models/display"
|
|
30
44
|
import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
31
45
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
32
46
|
import {
|
|
@@ -41,6 +55,8 @@ import {
|
|
|
41
55
|
AgentRunListResponseSchema,
|
|
42
56
|
AgentRunParamsSchema,
|
|
43
57
|
AgentRunSchema,
|
|
58
|
+
AgentThreadContextQuerySchema,
|
|
59
|
+
AgentThreadContextSchema,
|
|
44
60
|
AgentThreadListQuerySchema,
|
|
45
61
|
AgentThreadListResponseSchema,
|
|
46
62
|
AgentThreadParamsSchema,
|
|
@@ -71,20 +87,27 @@ const AgentMessageFileContentQuerySchema = FileContentQuerySchema.extend({
|
|
|
71
87
|
})
|
|
72
88
|
|
|
73
89
|
const SHARING =
|
|
74
|
-
"Only the thread's owner adds participants: active users of the project, by id or by email. They read the whole conversation, its files and its live stream, and send messages that run with their own rights. Nothing checks what they may see: the conversation holds what the Agent read with the rights of whoever asked."
|
|
90
|
+
"Only the thread's owner adds participants: active users of the project, by id or by email. They read the whole conversation, its files and its live stream, and send messages that run with their own rights; a thread that runs on its owner's computer they only read. Nothing checks what they may see: the conversation holds what the Agent read with the rights of whoever asked."
|
|
75
91
|
|
|
76
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Threads as the API returns them to `viewer`, their owner and participants named by their
|
|
94
|
+
* accounts. Where a computer's thread runs, and whether `sixb` is open there, is its owner's alone.
|
|
95
|
+
*/
|
|
77
96
|
export async function serializeThreads(
|
|
78
97
|
host: SixbHostView,
|
|
79
|
-
threads: readonly AgentThreadView[]
|
|
98
|
+
threads: readonly AgentThreadView[],
|
|
99
|
+
viewer: Principal
|
|
80
100
|
): Promise<ReturnType<typeof AgentThreadSchema.parse>[]> {
|
|
81
|
-
const people = await
|
|
82
|
-
host,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
101
|
+
const [placeOf, people] = await Promise.all([
|
|
102
|
+
computerPlaces(host, threads, viewer),
|
|
103
|
+
namePrincipals(
|
|
104
|
+
host,
|
|
105
|
+
threads.flatMap((thread) => [
|
|
106
|
+
thread.ownerPrincipal,
|
|
107
|
+
...thread.participants.map((participant) => participant.principal),
|
|
108
|
+
])
|
|
109
|
+
),
|
|
110
|
+
])
|
|
88
111
|
const named = (principal: Principal) => people.get(principalKey(principal)) ?? principal
|
|
89
112
|
|
|
90
113
|
return threads.map((thread) =>
|
|
@@ -102,6 +125,9 @@ export async function serializeThreads(
|
|
|
102
125
|
})),
|
|
103
126
|
title: thread.title,
|
|
104
127
|
sandboxParams: thread.sandboxParams,
|
|
128
|
+
runsOn: thread.runsOn,
|
|
129
|
+
computer: placeOf(thread),
|
|
130
|
+
movedAt: thread.movedAt ? toIsoString(thread.movedAt) : undefined,
|
|
105
131
|
sandboxState: thread.sandboxState && {
|
|
106
132
|
name: thread.sandboxState.name,
|
|
107
133
|
status: thread.sandboxState.status,
|
|
@@ -119,13 +145,38 @@ export async function serializeThreads(
|
|
|
119
145
|
|
|
120
146
|
export async function serializeThread(
|
|
121
147
|
host: SixbHostView,
|
|
122
|
-
thread: AgentThreadView
|
|
148
|
+
thread: AgentThreadView,
|
|
149
|
+
viewer: Principal
|
|
123
150
|
): Promise<ReturnType<typeof AgentThreadSchema.parse>> {
|
|
124
|
-
const [serialized] = await serializeThreads(host, [thread])
|
|
151
|
+
const [serialized] = await serializeThreads(host, [thread], viewer)
|
|
125
152
|
if (!serialized) throw new Error(`[SixbServer] Could not serialize Agent thread '${thread.id}'.`)
|
|
126
153
|
return serialized
|
|
127
154
|
}
|
|
128
155
|
|
|
156
|
+
/** Where each of the viewer's own computer threads runs, and whether its folder is open now. */
|
|
157
|
+
async function computerPlaces(
|
|
158
|
+
host: SixbHostView,
|
|
159
|
+
threads: readonly AgentThreadView[],
|
|
160
|
+
viewer: Principal
|
|
161
|
+
): Promise<(thread: AgentThreadView) => (AgentThreadComputer & { online: boolean }) | undefined> {
|
|
162
|
+
const owned = (thread: AgentThreadView) =>
|
|
163
|
+
thread.computer !== undefined && principalsEqual(thread.ownerPrincipal, viewer)
|
|
164
|
+
const live =
|
|
165
|
+
host.storage.agents && threads.some(owned)
|
|
166
|
+
? await host.storage.agents.computers.listLive({
|
|
167
|
+
projectId: host.id,
|
|
168
|
+
ownerPrincipal: viewer,
|
|
169
|
+
now: new Date(),
|
|
170
|
+
})
|
|
171
|
+
: []
|
|
172
|
+
return (thread) => {
|
|
173
|
+
const computer = thread.computer
|
|
174
|
+
if (!computer || !owned(thread)) return undefined
|
|
175
|
+
const online = live.some((session) => sameComputerPlace(session.computer, computer))
|
|
176
|
+
return { ...computer, online }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
129
180
|
function serializeMessage(
|
|
130
181
|
message: AgentMessageRecord,
|
|
131
182
|
annotations: readonly AgentRunDiagnostic[] = [],
|
|
@@ -144,6 +195,7 @@ function serializeMessage(
|
|
|
144
195
|
compaction: checkpoint
|
|
145
196
|
? {
|
|
146
197
|
checkpointId: checkpoint.id,
|
|
198
|
+
reason: checkpoint.reason,
|
|
147
199
|
summary: checkpoint.summary,
|
|
148
200
|
createdAt: toIsoString(checkpoint.createdAt),
|
|
149
201
|
}
|
|
@@ -169,6 +221,7 @@ export function serializeAgentRun(run: AgentRunView): ReturnType<typeof AgentRun
|
|
|
169
221
|
tier: run.spec?.tier,
|
|
170
222
|
reasoning: run.spec?.reasoning,
|
|
171
223
|
approvalMode: run.spec?.approvalMode,
|
|
224
|
+
runsOn: run.kind === "conversation" ? run.spec?.runsOn : undefined,
|
|
172
225
|
modelId: run.modelId,
|
|
173
226
|
finishReason: run.finishReason,
|
|
174
227
|
usage: run.usage,
|
|
@@ -351,7 +404,7 @@ export async function agentMessageFileContentResponse(
|
|
|
351
404
|
export async function readThreadMessages(
|
|
352
405
|
host: SixbHostView,
|
|
353
406
|
storage: AgentStorage,
|
|
354
|
-
thread: AgentThreadRecord,
|
|
407
|
+
thread: Pick<AgentThreadRecord, "id">,
|
|
355
408
|
query: z.infer<typeof AgentMessagesQuerySchema>
|
|
356
409
|
) {
|
|
357
410
|
const projectId = host.id
|
|
@@ -431,10 +484,20 @@ async function namePrincipals(
|
|
|
431
484
|
return new Map(named)
|
|
432
485
|
}
|
|
433
486
|
|
|
434
|
-
function principalKey(principal: Principal): string {
|
|
487
|
+
export function principalKey(principal: Principal): string {
|
|
435
488
|
return `${principal.type}:${principal.id}`
|
|
436
489
|
}
|
|
437
490
|
|
|
491
|
+
/** A thread as reading it answers: with the permission mode its reader shows. */
|
|
492
|
+
export async function serializeThreadRead(
|
|
493
|
+
host: SixbHostView,
|
|
494
|
+
sixb: ReturnType<typeof requireRequestSixb>,
|
|
495
|
+
thread: AgentThreadView
|
|
496
|
+
) {
|
|
497
|
+
const approvalMode = await sixb.agent.threads.getApprovalMode(thread.id)
|
|
498
|
+
return { ...(await serializeThread(host, thread, requestActor(sixb))), ...approvalMode }
|
|
499
|
+
}
|
|
500
|
+
|
|
438
501
|
export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
439
502
|
return app
|
|
440
503
|
.get(
|
|
@@ -478,7 +541,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
478
541
|
const result = await sixb.agent.threads.list(listInput)
|
|
479
542
|
|
|
480
543
|
return AgentThreadListResponseSchema.parse({
|
|
481
|
-
threads: await serializeThreads(host, result.threads),
|
|
544
|
+
threads: await serializeThreads(host, result.threads, requestActor(sixb)),
|
|
482
545
|
hasMore: result.hasMore,
|
|
483
546
|
total: result.total,
|
|
484
547
|
})
|
|
@@ -516,11 +579,13 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
516
579
|
...(parsed.threadId === undefined ? {} : { id: parsed.threadId }),
|
|
517
580
|
...(parsed.title === undefined ? {} : { title: parsed.title }),
|
|
518
581
|
...(parsed.sandbox === undefined ? {} : { sandbox: parsed.sandbox }),
|
|
582
|
+
...(parsed.runsOn === undefined ? {} : { runsOn: parsed.runsOn }),
|
|
583
|
+
...(parsed.computer === undefined ? {} : { computer: parsed.computer }),
|
|
519
584
|
})
|
|
520
585
|
|
|
521
586
|
set.status = 201
|
|
522
587
|
return CreateAgentThreadResponseSchema.parse({
|
|
523
|
-
thread: await serializeThread(host, thread),
|
|
588
|
+
thread: await serializeThread(host, thread, requestActor(sixb)),
|
|
524
589
|
})
|
|
525
590
|
} catch (error) {
|
|
526
591
|
return handleAgentRouteError(error, set)
|
|
@@ -561,8 +626,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
561
626
|
return { error: "Agent thread not found" }
|
|
562
627
|
}
|
|
563
628
|
|
|
564
|
-
|
|
565
|
-
return { ...(await serializeThread(host, thread)), ...approvalMode }
|
|
629
|
+
return await serializeThreadRead(host, sixb, thread)
|
|
566
630
|
} catch (error) {
|
|
567
631
|
return handleAgentRouteError(error, set)
|
|
568
632
|
}
|
|
@@ -595,7 +659,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
595
659
|
const parsed = AddAgentThreadParticipantsBodySchema.parse(body)
|
|
596
660
|
const thread = await sixb.agent.threads.participants.add(params.threadId, parsed.users)
|
|
597
661
|
|
|
598
|
-
return serializeThread(host, thread)
|
|
662
|
+
return serializeThread(host, thread, requestActor(sixb))
|
|
599
663
|
} catch (error) {
|
|
600
664
|
return handleAgentRouteError(error, set)
|
|
601
665
|
}
|
|
@@ -675,7 +739,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
675
739
|
|
|
676
740
|
set.status = 201
|
|
677
741
|
return CreateAgentThreadResponseSchema.parse({
|
|
678
|
-
thread: await serializeThread(host, thread),
|
|
742
|
+
thread: await serializeThread(host, thread, requestActor(sixb)),
|
|
679
743
|
})
|
|
680
744
|
} catch (error) {
|
|
681
745
|
return handleAgentRouteError(error, set)
|
|
@@ -708,7 +772,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
708
772
|
context.params.threadId,
|
|
709
773
|
context.body
|
|
710
774
|
)
|
|
711
|
-
return serializeThread(host, thread)
|
|
775
|
+
return serializeThread(host, thread, requestActor(sixb))
|
|
712
776
|
} catch (error) {
|
|
713
777
|
if (error instanceof AgentStorageError && error.code === "invalid_state") {
|
|
714
778
|
context.set.status = 409
|
|
@@ -740,14 +804,24 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
740
804
|
"/api/agent-threads/:threadId/messages",
|
|
741
805
|
async (context) => {
|
|
742
806
|
const { params, query, set } = context
|
|
743
|
-
const
|
|
807
|
+
const authState = requestAuthState(context)
|
|
744
808
|
try {
|
|
745
809
|
const storage = host.storage.agents
|
|
746
810
|
if (!storage) {
|
|
747
811
|
return missingAgentStorageResponse(set)
|
|
748
812
|
}
|
|
749
813
|
|
|
750
|
-
|
|
814
|
+
// A run reads its own conversation alone, as it reads that conversation's files; a
|
|
815
|
+
// child or a workflow step reads none.
|
|
816
|
+
const thread =
|
|
817
|
+
authState.agentExecution && !authState.agentRun
|
|
818
|
+
? null
|
|
819
|
+
: await getAgentMessageFileContentThread({
|
|
820
|
+
authState,
|
|
821
|
+
storage,
|
|
822
|
+
projectId: host.id,
|
|
823
|
+
threadId: params.threadId,
|
|
824
|
+
})
|
|
751
825
|
if (!thread) {
|
|
752
826
|
set.status = 404
|
|
753
827
|
return { error: "Agent thread not found" }
|
|
@@ -774,6 +848,7 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
774
848
|
summary: "List agent thread messages",
|
|
775
849
|
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
776
850
|
operationId: "listAgentThreadMessages",
|
|
851
|
+
security: accessTokenSecurityRequirement("listAgentThreadMessages"),
|
|
777
852
|
},
|
|
778
853
|
}
|
|
779
854
|
)
|
|
@@ -855,7 +930,8 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
855
930
|
response: {
|
|
856
931
|
202: PostAgentMessageResponseSchema,
|
|
857
932
|
400: AgentRequestErrorResponseSchema,
|
|
858
|
-
|
|
933
|
+
// Coded: `not_thread_owner` when the thread runs on its owner's computer.
|
|
934
|
+
403: AgentRequestErrorResponseSchema,
|
|
859
935
|
404: ErrorResponseSchema,
|
|
860
936
|
409: AgentRequestErrorResponseSchema,
|
|
861
937
|
429: AiUsageLimitErrorResponseSchema,
|
|
@@ -981,6 +1057,97 @@ export function registerAgentRoutes(app: Elysia, host: SixbHostView) {
|
|
|
981
1057
|
},
|
|
982
1058
|
}
|
|
983
1059
|
)
|
|
1060
|
+
.get(
|
|
1061
|
+
"/api/agent-threads/:threadId/context",
|
|
1062
|
+
async (context) => {
|
|
1063
|
+
const { params, query, set } = context
|
|
1064
|
+
const sixb = requireRequestSixb(context)
|
|
1065
|
+
try {
|
|
1066
|
+
const storage = host.storage.agents
|
|
1067
|
+
const aiUsage = host.storage.aiUsage
|
|
1068
|
+
const language = host.definitions.models?.language
|
|
1069
|
+
if (!storage || !aiUsage) {
|
|
1070
|
+
return missingAgentStorageResponse(set)
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
const parsed = AgentThreadContextQuerySchema.parse(query)
|
|
1074
|
+
if ((parsed.provider === undefined) !== (parsed.modelId === undefined)) {
|
|
1075
|
+
set.status = 400
|
|
1076
|
+
return { error: "Name the model with both 'provider' and 'modelId', or neither." }
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
const thread = await sixb.agent.threads.getById(params.threadId)
|
|
1080
|
+
if (!thread) {
|
|
1081
|
+
set.status = 404
|
|
1082
|
+
return { error: "Agent thread not found" }
|
|
1083
|
+
}
|
|
1084
|
+
// A sub-agent's thread is measured through its conversation's turns.
|
|
1085
|
+
assertConversationThread(thread)
|
|
1086
|
+
|
|
1087
|
+
// The model asked for, else the one the latest turn ran on, else the default.
|
|
1088
|
+
const latest = await sixb.agent.runs.listForThread(thread.id, { limit: 1, order: "desc" })
|
|
1089
|
+
const latestSpec = latest?.runs[0]?.spec
|
|
1090
|
+
const latestModel = latestSpec?.model
|
|
1091
|
+
const entry: LanguageModelEntry | null | undefined =
|
|
1092
|
+
parsed.provider !== undefined && parsed.modelId !== undefined
|
|
1093
|
+
? language?.getByRef({ provider: parsed.provider, modelId: parsed.modelId })
|
|
1094
|
+
: ((latestModel && language?.getByRef(latestModel)) ?? language?.default)
|
|
1095
|
+
if (!entry) {
|
|
1096
|
+
set.status = 404
|
|
1097
|
+
return { error: "Language model not found" }
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
// The reasoning the latest turn ran with, on its model; the model's default on another.
|
|
1101
|
+
const latestRunsOn =
|
|
1102
|
+
latestModel?.provider === entry.provider && latestModel.modelId === entry.modelId
|
|
1103
|
+
const reasoning = latestRunsOn ? latestSpec?.reasoning : entry.model.defaultReasoning
|
|
1104
|
+
|
|
1105
|
+
const use = await readAgentThreadContextUse(
|
|
1106
|
+
{ projectId: host.id, storage: { agents: storage, aiUsage } },
|
|
1107
|
+
{
|
|
1108
|
+
threadId: thread.id,
|
|
1109
|
+
model: await resolvedForDisplay(entry),
|
|
1110
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
1111
|
+
}
|
|
1112
|
+
)
|
|
1113
|
+
return AgentThreadContextSchema.parse({
|
|
1114
|
+
...use,
|
|
1115
|
+
model: { provider: entry.provider, modelId: entry.modelId },
|
|
1116
|
+
...(use.lastCall
|
|
1117
|
+
? { lastCall: { ...use.lastCall, occurredAt: toIsoString(use.lastCall.occurredAt) } }
|
|
1118
|
+
: {}),
|
|
1119
|
+
...(use.lastCompaction
|
|
1120
|
+
? {
|
|
1121
|
+
lastCompaction: {
|
|
1122
|
+
...use.lastCompaction,
|
|
1123
|
+
createdAt: toIsoString(use.lastCompaction.createdAt),
|
|
1124
|
+
},
|
|
1125
|
+
}
|
|
1126
|
+
: {}),
|
|
1127
|
+
})
|
|
1128
|
+
} catch (error) {
|
|
1129
|
+
return handleAgentRouteError(error, set)
|
|
1130
|
+
}
|
|
1131
|
+
},
|
|
1132
|
+
{
|
|
1133
|
+
params: AgentThreadParamsSchema,
|
|
1134
|
+
query: AgentThreadContextQuerySchema,
|
|
1135
|
+
response: {
|
|
1136
|
+
200: AgentThreadContextSchema,
|
|
1137
|
+
400: ErrorResponseSchema,
|
|
1138
|
+
404: ErrorResponseSchema,
|
|
1139
|
+
409: AgentRequestErrorResponseSchema,
|
|
1140
|
+
501: ErrorResponseSchema,
|
|
1141
|
+
},
|
|
1142
|
+
detail: {
|
|
1143
|
+
summary: "Measure an agent thread's context against a model's limits",
|
|
1144
|
+
description:
|
|
1145
|
+
"What the conversation's next request starts from, the model's limit, and the latest compaction. Read by the context meter after each turn.",
|
|
1146
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
1147
|
+
operationId: "getAgentThreadContext",
|
|
1148
|
+
},
|
|
1149
|
+
}
|
|
1150
|
+
)
|
|
984
1151
|
.get(
|
|
985
1152
|
"/api/agent-threads/:threadId/runs",
|
|
986
1153
|
async (context) => {
|