@sixb/agent 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/abort.d.ts +6 -0
- package/dist/abort.d.ts.map +1 -0
- package/dist/agent-prompt.d.ts +120 -0
- package/dist/agent-prompt.d.ts.map +1 -0
- package/dist/agent-runtime/errors.d.ts +13 -0
- package/dist/agent-runtime/errors.d.ts.map +1 -0
- package/dist/agent-runtime/preflight.d.ts +18 -0
- package/dist/agent-runtime/preflight.d.ts.map +1 -0
- package/dist/agent-runtime/profile.d.ts +14 -0
- package/dist/agent-runtime/profile.d.ts.map +1 -0
- package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
- package/dist/artifacts/materialize.d.ts +115 -0
- package/dist/artifacts/materialize.d.ts.map +1 -0
- package/dist/artifacts/publish-tool.d.ts +86 -0
- package/dist/artifacts/publish-tool.d.ts.map +1 -0
- package/dist/ask-parent-tool.d.ts +36 -0
- package/dist/ask-parent-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +68 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/child-files.d.ts +96 -0
- package/dist/child-files.d.ts.map +1 -0
- package/dist/child-questions.d.ts +24 -0
- package/dist/child-questions.d.ts.map +1 -0
- package/dist/computer-runner.d.ts +65 -0
- package/dist/computer-runner.d.ts.map +1 -0
- package/dist/context-budget.d.ts +28 -0
- package/dist/context-budget.d.ts.map +1 -0
- package/dist/context-compaction.d.ts +48 -0
- package/dist/context-compaction.d.ts.map +1 -0
- package/dist/conversation-memory.d.ts +21 -0
- package/dist/conversation-memory.d.ts.map +1 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/execution-plan.d.ts +34 -0
- package/dist/execution-plan.d.ts.map +1 -0
- package/dist/failure.d.ts +14 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/family-mailbox.d.ts +60 -0
- package/dist/family-mailbox.d.ts.map +1 -0
- package/dist/file-ref.d.ts +6 -0
- package/dist/file-ref.d.ts.map +1 -0
- package/dist/image-attachments.d.ts +34 -0
- package/dist/image-attachments.d.ts.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12168 -0
- package/dist/index.js.map +65 -0
- package/dist/message-authors.d.ts +34 -0
- package/dist/message-authors.d.ts.map +1 -0
- package/dist/message-times.d.ts +8 -0
- package/dist/message-times.d.ts.map +1 -0
- package/dist/model-adapters.d.ts +55 -0
- package/dist/model-adapters.d.ts.map +1 -0
- package/dist/model-lines.d.ts +16 -0
- package/dist/model-lines.d.ts.map +1 -0
- package/dist/requester.d.ts +15 -0
- package/dist/requester.d.ts.map +1 -0
- package/dist/run-agent-turn.d.ts +63 -0
- package/dist/run-agent-turn.d.ts.map +1 -0
- package/dist/run-child.d.ts +77 -0
- package/dist/run-child.d.ts.map +1 -0
- package/dist/run-conversation-turn.d.ts +42 -0
- package/dist/run-conversation-turn.d.ts.map +1 -0
- package/dist/run-credential.d.ts +22 -0
- package/dist/run-credential.d.ts.map +1 -0
- package/dist/run-environment.d.ts +115 -0
- package/dist/run-environment.d.ts.map +1 -0
- package/dist/run-fate.d.ts +15 -0
- package/dist/run-fate.d.ts.map +1 -0
- package/dist/run-sandbox.d.ts +60 -0
- package/dist/run-sandbox.d.ts.map +1 -0
- package/dist/run-subagent.d.ts +51 -0
- package/dist/run-subagent.d.ts.map +1 -0
- package/dist/run-workflow-agent-node.d.ts +41 -0
- package/dist/run-workflow-agent-node.d.ts.map +1 -0
- package/dist/sandbox-api-context.d.ts +34 -0
- package/dist/sandbox-api-context.d.ts.map +1 -0
- package/dist/sandbox-file-registry.d.ts +9 -0
- package/dist/sandbox-file-registry.d.ts.map +1 -0
- package/dist/sandbox-files.d.ts +71 -0
- package/dist/sandbox-files.d.ts.map +1 -0
- package/dist/sandbox-handle.d.ts +15 -0
- package/dist/sandbox-handle.d.ts.map +1 -0
- package/dist/sandbox-readiness.d.ts +8 -0
- package/dist/sandbox-readiness.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts +15 -0
- package/dist/skills/built-in.d.ts.map +1 -0
- package/dist/subagent-continuation.d.ts +27 -0
- package/dist/subagent-continuation.d.ts.map +1 -0
- package/dist/subagent-inbox.d.ts +53 -0
- package/dist/subagent-inbox.d.ts.map +1 -0
- package/dist/subagent-tools.d.ts +282 -0
- package/dist/subagent-tools.d.ts.map +1 -0
- package/dist/thread-context.d.ts +12 -0
- package/dist/thread-context.d.ts.map +1 -0
- package/dist/tools/attachments.d.ts +11 -0
- package/dist/tools/attachments.d.ts.map +1 -0
- package/dist/tools/bash.d.ts +60 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts +4 -0
- package/dist/tools/model-spec.d.ts.map +1 -0
- package/dist/tools/read.d.ts +43 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/result-media.d.ts +22 -0
- package/dist/tools/result-media.d.ts.map +1 -0
- package/dist/tools/result-output.d.ts +18 -0
- package/dist/tools/result-output.d.ts.map +1 -0
- package/dist/tools/tool-files.d.ts +59 -0
- package/dist/tools/tool-files.d.ts.map +1 -0
- package/dist/tools/view-file.d.ts +40 -0
- package/dist/tools/view-file.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/turn-runtime.d.ts +19 -0
- package/dist/turn-runtime.d.ts.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workflow-input-attachments.d.ts +18 -0
- package/dist/workflow-input-attachments.d.ts.map +1 -0
- package/package.json +59 -4
- package/src/abort.ts +40 -0
- package/src/agent-prompt.ts +758 -0
- package/src/agent-runtime/errors.ts +44 -0
- package/src/agent-runtime/preflight.ts +314 -0
- package/src/agent-runtime/profile.ts +23 -0
- package/src/artifacts/layout-check/check.cjs +478 -0
- package/src/artifacts/layout-check/layout-check.ts +460 -0
- package/src/artifacts/materialize.ts +380 -0
- package/src/artifacts/publish-tool.ts +700 -0
- package/src/ask-parent-tool.ts +100 -0
- package/src/attachments.ts +850 -0
- package/src/child-files.ts +540 -0
- package/src/child-questions.ts +42 -0
- package/src/computer-runner.ts +284 -0
- package/src/context-budget.ts +161 -0
- package/src/context-compaction.ts +529 -0
- package/src/conversation-memory.ts +45 -0
- package/src/errors.ts +85 -0
- package/src/execution-plan.ts +50 -0
- package/src/failure.ts +71 -0
- package/src/family-mailbox.ts +189 -0
- package/src/file-ref.ts +18 -0
- package/src/image-attachments.ts +322 -0
- package/src/index.ts +54 -0
- package/src/message-authors.ts +77 -0
- package/src/message-times.ts +21 -0
- package/src/model-adapters.ts +227 -0
- package/src/model-lines.ts +28 -0
- package/src/requester.ts +21 -0
- package/src/run-agent-turn.ts +347 -0
- package/src/run-child.ts +319 -0
- package/src/run-conversation-turn.ts +257 -0
- package/src/run-credential.ts +55 -0
- package/src/run-environment.ts +769 -0
- package/src/run-fate.ts +51 -0
- package/src/run-sandbox.ts +63 -0
- package/src/run-subagent.ts +347 -0
- package/src/run-workflow-agent-node.ts +270 -0
- package/src/sandbox-api-context.ts +234 -0
- package/src/sandbox-file-registry.ts +26 -0
- package/src/sandbox-files.ts +326 -0
- package/src/sandbox-handle.ts +19 -0
- package/src/sandbox-readiness.ts +22 -0
- package/src/skills/built-in.ts +183 -0
- package/src/skills/sixb-artifacts/SKILL.md +95 -0
- package/src/skills/sixb-artifacts/canvas.md +104 -0
- package/src/skills/sixb-artifacts/document.md +63 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
- package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
- package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
- package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
- package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
- package/src/skills/sixb-artifacts/file.md +17 -0
- package/src/skills/sixb-artifacts/page.md +215 -0
- package/src/skills/sixb-artifacts/queries.md +66 -0
- package/src/skills/sixb-artifacts/references.md +68 -0
- package/src/skills/sixb-artifacts/sheet.md +223 -0
- package/src/skills/sixb-artifacts/slides.md +137 -0
- package/src/skills/sixb-memory/SKILL.md +68 -0
- package/src/skills/text-assets.d.ts +25 -0
- package/src/subagent-continuation.ts +51 -0
- package/src/subagent-inbox.ts +101 -0
- package/src/subagent-tools.ts +1609 -0
- package/src/thread-context.ts +25 -0
- package/src/tools/attachments.ts +28 -0
- package/src/tools/bash.ts +217 -0
- package/src/tools/model-spec.ts +22 -0
- package/src/tools/read.ts +262 -0
- package/src/tools/result-media.ts +147 -0
- package/src/tools/result-output.ts +44 -0
- package/src/tools/tool-files.ts +204 -0
- package/src/tools/view-file.ts +155 -0
- package/src/turn-runtime.ts +33 -0
- package/src/types.ts +79 -0
- package/src/workflow-input-attachments.ts +140 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { principalsEqual } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
AgentExecutionLostError,
|
|
4
|
+
AgentFinalizationError,
|
|
5
|
+
type AgentRunSession,
|
|
6
|
+
AgentTurnInterruptedError,
|
|
7
|
+
} from "@sixb/core/internal/agents"
|
|
8
|
+
import { createSixbError } from "@sixb/core/internal/errors"
|
|
9
|
+
import { ModelUsageRecordingError } from "@sixb/core/internal/model-execution"
|
|
10
|
+
import { isAbortError } from "@sixb/core/internal/workers"
|
|
11
|
+
import type {
|
|
12
|
+
AgentRunRecord,
|
|
13
|
+
AgentThreadParticipantRecord,
|
|
14
|
+
AgentThreadRecord,
|
|
15
|
+
ConversationAgentRunRecord,
|
|
16
|
+
ExecutionRecord,
|
|
17
|
+
SubagentRunRecord,
|
|
18
|
+
} from "@sixb/core/storage"
|
|
19
|
+
import { prepareAgentModel } from "./context-budget"
|
|
20
|
+
import { AgentEnvironmentSaveError, AgentRequesterLeftThreadError, isLostExecution } from "./errors"
|
|
21
|
+
import { resolveRunPlan } from "./execution-plan"
|
|
22
|
+
import type { AgentRunFailure } from "./failure"
|
|
23
|
+
import type { FamilyMailbox } from "./family-mailbox"
|
|
24
|
+
import { agentRequester } from "./requester"
|
|
25
|
+
import { runAgentTurn, stoppedByWorker } from "./run-agent-turn"
|
|
26
|
+
import {
|
|
27
|
+
type ConversationAgentExecutionEnvironment,
|
|
28
|
+
createConversationAgentEnvironment,
|
|
29
|
+
type OpenRunSandbox,
|
|
30
|
+
} from "./run-environment"
|
|
31
|
+
import { recordFate } from "./run-fate"
|
|
32
|
+
import { SubagentCoordinator } from "./subagent-tools"
|
|
33
|
+
import { type AgentTurnRuntime, createAgentTurnRuntime } from "./turn-runtime"
|
|
34
|
+
import type { AgentExecutionContext, AgentRunSettings } from "./types"
|
|
35
|
+
|
|
36
|
+
// A conversation turn, from its claimed session to its end, whichever runner runs it: the worker,
|
|
37
|
+
// under a queue delivery, or a runner on a computer, over HTTP. The runner keeps what is its own:
|
|
38
|
+
// how it holds the run, where the turn's sandbox comes from, and where its children run.
|
|
39
|
+
|
|
40
|
+
/** What a runner lends the turns it runs. */
|
|
41
|
+
export interface ConversationTurnRunner {
|
|
42
|
+
/** What every run of the project shares. */
|
|
43
|
+
readonly settings: AgentRunSettings
|
|
44
|
+
/** The sandbox the turn works in, opened when it first needs one. */
|
|
45
|
+
openSandbox(turn: {
|
|
46
|
+
readonly session: AgentRunSession<ConversationAgentRunRecord>
|
|
47
|
+
readonly context: AgentExecutionContext
|
|
48
|
+
readonly execution: ExecutionRecord
|
|
49
|
+
readonly thread: AgentThreadRecord | null
|
|
50
|
+
}): OpenRunSandbox
|
|
51
|
+
/** Run a child the turn spawned or continues, beside the turn, on the turn's mailbox. */
|
|
52
|
+
startChild(run: SubagentRunRecord, family: FamilyMailbox): void
|
|
53
|
+
/** A sandbox teardown that outlives the turn: the runner waits for it before it stops. */
|
|
54
|
+
onDetachedTeardown(teardown: Promise<void>): void
|
|
55
|
+
/** A turn that failed: the runner reports it, once its end is recorded. */
|
|
56
|
+
reportFailure?(error: unknown, run: AgentRunRecord, failure: AgentRunFailure): void
|
|
57
|
+
/**
|
|
58
|
+
* Runs the turn's preparation, which reads and resolves and calls no model: the worker retries a
|
|
59
|
+
* storage blip in place. Runs it once when absent.
|
|
60
|
+
*/
|
|
61
|
+
prepare?<T>(prepare: () => Promise<T>): Promise<T>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Run a claimed conversation turn and record how it ended. Returns once its end is recorded, or
|
|
66
|
+
* at once when its runner lost the run. Throws {@link AgentFinalizationError} when its end could
|
|
67
|
+
* not be recorded, and rethrows its failure when `signal` stopped it, for the runner to release
|
|
68
|
+
* the run rather than call it done.
|
|
69
|
+
*/
|
|
70
|
+
export async function runConversationTurn(input: {
|
|
71
|
+
readonly session: AgentRunSession<ConversationAgentRunRecord>
|
|
72
|
+
readonly runner: ConversationTurnRunner
|
|
73
|
+
/** The runner's own stop: its process stopping, or its hold on the run lost. */
|
|
74
|
+
readonly signal: AbortSignal
|
|
75
|
+
}): Promise<void> {
|
|
76
|
+
const { session, runner, signal } = input
|
|
77
|
+
const { run } = session
|
|
78
|
+
let environment: ConversationAgentExecutionEnvironment | null = null
|
|
79
|
+
let runtime: AgentTurnRuntime | null = null
|
|
80
|
+
let delegation: SubagentCoordinator | undefined
|
|
81
|
+
// A user cancel joins the turn's abort sources, so it stops the model stream as a shutdown does.
|
|
82
|
+
const turnSignal = AbortSignal.any([signal, session.cancelled])
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const prepare = () => prepareConversationTurn(session, runner.settings)
|
|
86
|
+
const prepared = await (runner.prepare ? runner.prepare(prepare) : prepare())
|
|
87
|
+
const plan = Object.freeze({ ...prepared.plan, model: prepared.model.model })
|
|
88
|
+
|
|
89
|
+
await session.events.publishStarted(run)
|
|
90
|
+
runtime = createAgentTurnRuntime({ signal: turnSignal })
|
|
91
|
+
delegation = new SubagentCoordinator(
|
|
92
|
+
prepared.delegation,
|
|
93
|
+
runner.settings,
|
|
94
|
+
run,
|
|
95
|
+
session,
|
|
96
|
+
(child, family) => runner.startChild(child, family)
|
|
97
|
+
)
|
|
98
|
+
const frameworkTools = await delegation.createTools()
|
|
99
|
+
environment = await createConversationAgentEnvironment({
|
|
100
|
+
session,
|
|
101
|
+
frameworkTools,
|
|
102
|
+
thread: prepared.thread,
|
|
103
|
+
...(prepared.execution.requestedBy === undefined
|
|
104
|
+
? {}
|
|
105
|
+
: { requestedBy: prepared.execution.requestedBy }),
|
|
106
|
+
...(prepared.conversation === undefined ? {} : { conversation: prepared.conversation }),
|
|
107
|
+
shared: prepared.shared,
|
|
108
|
+
openSandbox: runner.openSandbox({
|
|
109
|
+
session,
|
|
110
|
+
context: prepared.context,
|
|
111
|
+
execution: prepared.execution,
|
|
112
|
+
thread: prepared.thread,
|
|
113
|
+
}),
|
|
114
|
+
preflight: { budget: prepared.model.budget, runtime },
|
|
115
|
+
context: prepared.context,
|
|
116
|
+
plan,
|
|
117
|
+
run,
|
|
118
|
+
history: prepared.history,
|
|
119
|
+
signal: runtime.signal,
|
|
120
|
+
onDetachedTeardown: (teardown) => runner.onDetachedTeardown(teardown),
|
|
121
|
+
})
|
|
122
|
+
delegation.attach(environment.runFiles)
|
|
123
|
+
// A resumed turn's paused children go on first, as the model reads that they do.
|
|
124
|
+
await delegation.continuePausedChildren(runtime.signal)
|
|
125
|
+
runtime.assertCanContinue()
|
|
126
|
+
await runAgentTurn({
|
|
127
|
+
context: environment.turnContext,
|
|
128
|
+
plan,
|
|
129
|
+
run,
|
|
130
|
+
session,
|
|
131
|
+
...(prepared.conversation === undefined ? {} : { conversation: prepared.conversation }),
|
|
132
|
+
signal: turnSignal,
|
|
133
|
+
stopSignal: signal,
|
|
134
|
+
runtime,
|
|
135
|
+
threadContext: environment.threadContext,
|
|
136
|
+
children: await delegation.turnChildren(),
|
|
137
|
+
})
|
|
138
|
+
} catch (caughtError) {
|
|
139
|
+
let error = caughtError
|
|
140
|
+
let environmentSaveFailed = error instanceof AgentEnvironmentSaveError
|
|
141
|
+
// Its runner lost the run: touch nothing; whoever holds it now ends it.
|
|
142
|
+
if (isLostExecution(error, signal)) return
|
|
143
|
+
// It ended, but its end could not be recorded (storage down): its runner tries again.
|
|
144
|
+
if (error instanceof AgentFinalizationError) throw error
|
|
145
|
+
if (environment) {
|
|
146
|
+
try {
|
|
147
|
+
await environment.beforeFinalize()
|
|
148
|
+
} catch (saveError) {
|
|
149
|
+
if (saveError instanceof AgentExecutionLostError) return
|
|
150
|
+
error = saveError
|
|
151
|
+
environmentSaveFailed = true
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// A user cancel is read off its own signal, so it records `cancelled` however the aborted
|
|
155
|
+
// stream surfaced. `recordFate` retries a blip, and throws AgentFinalizationError past it.
|
|
156
|
+
const aborted =
|
|
157
|
+
error instanceof AgentRequesterLeftThreadError ||
|
|
158
|
+
(!environmentSaveFailed &&
|
|
159
|
+
!(error instanceof ModelUsageRecordingError) &&
|
|
160
|
+
(signal.aborted || session.cancelled.aborted || isAbortError(error)))
|
|
161
|
+
// A runner that stops, unlike a user cancel, leaves the user something to resume.
|
|
162
|
+
const interrupted = aborted && stoppedByWorker(turnSignal, signal)
|
|
163
|
+
const finalized = await recordFate(
|
|
164
|
+
session,
|
|
165
|
+
aborted ? "cancelled" : "failed",
|
|
166
|
+
interrupted ? new AgentTurnInterruptedError(run.id) : error
|
|
167
|
+
)
|
|
168
|
+
if (finalized) {
|
|
169
|
+
if (finalized.run.status === "failed") {
|
|
170
|
+
runner.reportFailure?.(error, finalized.run, finalized.failure)
|
|
171
|
+
}
|
|
172
|
+
await session.events.publishRunFinished(finalized.run)
|
|
173
|
+
}
|
|
174
|
+
// Its runner stopped: it releases the run rather than calls it done.
|
|
175
|
+
if (signal.aborted) throw error
|
|
176
|
+
} finally {
|
|
177
|
+
delegation?.close()
|
|
178
|
+
runtime?.dispose()
|
|
179
|
+
await environment?.dispose()
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Everything a turn needs before its first model call, read through its session. */
|
|
184
|
+
async function prepareConversationTurn(
|
|
185
|
+
session: AgentRunSession<ConversationAgentRunRecord>,
|
|
186
|
+
settings: AgentRunSettings
|
|
187
|
+
) {
|
|
188
|
+
const { run } = session
|
|
189
|
+
const turn = await session.context()
|
|
190
|
+
const { thread, execution, participants } = turn
|
|
191
|
+
const runPlan = await session.plan()
|
|
192
|
+
const plan = resolveRunPlan(runPlan, { model: await session.models.language(runPlan.model) })
|
|
193
|
+
// A turn's plan always carries it; only a child's goes without.
|
|
194
|
+
if (!runPlan.delegation) {
|
|
195
|
+
throw createSixbError(
|
|
196
|
+
"internal.unexpected",
|
|
197
|
+
`[SixbAgent] Agent run '${run.id}' has no models to delegate to.`
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
if (!execution) {
|
|
201
|
+
throw createSixbError(
|
|
202
|
+
"internal.unexpected",
|
|
203
|
+
`[SixbAgent] Agent run '${run.id}' references missing execution '${run.executionId}'.`,
|
|
204
|
+
{ details: { runId: run.id, threadId: run.threadId, executionId: run.executionId } }
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
assertRequesterTakesPart(run, thread, participants, execution)
|
|
208
|
+
|
|
209
|
+
const conversation = thread
|
|
210
|
+
? { thread, participants: turn.readers, names: turn.names }
|
|
211
|
+
: undefined
|
|
212
|
+
// The rule run tokens apply to memory: someone who loses access may regain it, and read back
|
|
213
|
+
// whatever memory the thread held meanwhile.
|
|
214
|
+
const shared = participants.length > 0
|
|
215
|
+
// A shared thread's system prompt names nobody, so it stays the same whoever sends the turn.
|
|
216
|
+
const requester =
|
|
217
|
+
(conversation?.participants.length ?? 0) > 0
|
|
218
|
+
? undefined
|
|
219
|
+
: agentRequester(execution.requestedBy, turn.names)
|
|
220
|
+
const context: AgentExecutionContext = {
|
|
221
|
+
...settings,
|
|
222
|
+
...(requester === undefined ? {} : { requester }),
|
|
223
|
+
blobStorage: session.files,
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
thread,
|
|
227
|
+
...(conversation === undefined ? {} : { conversation }),
|
|
228
|
+
shared,
|
|
229
|
+
plan,
|
|
230
|
+
delegation: {
|
|
231
|
+
models: runPlan.delegation,
|
|
232
|
+
toolNames: runPlan.tools.map((tool) => tool.name),
|
|
233
|
+
},
|
|
234
|
+
execution,
|
|
235
|
+
context,
|
|
236
|
+
model: prepareAgentModel(plan),
|
|
237
|
+
history: turn.history,
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A shared thread's turn waits queued: its requester may leave the thread, or be removed, before
|
|
243
|
+
* it starts. Such a turn never reaches the model. One already running when they leave finishes,
|
|
244
|
+
* as it already read the thread.
|
|
245
|
+
*/
|
|
246
|
+
function assertRequesterTakesPart(
|
|
247
|
+
run: ConversationAgentRunRecord,
|
|
248
|
+
thread: AgentThreadRecord | null,
|
|
249
|
+
participants: readonly AgentThreadParticipantRecord[],
|
|
250
|
+
execution: ExecutionRecord
|
|
251
|
+
): void {
|
|
252
|
+
const requester = execution.requestedBy
|
|
253
|
+
if (!thread || !requester || principalsEqual(requester, thread.ownerPrincipal)) return
|
|
254
|
+
if (participants.some((participant) => principalsEqual(participant.principal, requester))) return
|
|
255
|
+
|
|
256
|
+
throw new AgentRequesterLeftThreadError(run.id)
|
|
257
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { SandboxRequestCredential } from "@sixb/core"
|
|
2
|
+
import { createAgentRunnerToken, createAgentRunToken } from "@sixb/core/internal/agents"
|
|
3
|
+
|
|
4
|
+
/** The token the run's commands call the API with, valid while this execution owns the run. */
|
|
5
|
+
export function runTokenFor(input: {
|
|
6
|
+
readonly projectId: string
|
|
7
|
+
readonly runId: string
|
|
8
|
+
readonly executionToken?: string
|
|
9
|
+
}): string {
|
|
10
|
+
if (!input.executionToken) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`[SixbAgent] Agent execution '${input.runId}' must hold an execution token before it gets a run token.`
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
return createAgentRunToken({
|
|
16
|
+
projectId: input.projectId,
|
|
17
|
+
runId: input.runId,
|
|
18
|
+
executionToken: input.executionToken,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The token a runner over HTTP reaches its run's session with. It is the runner's alone: the run's
|
|
24
|
+
* commands get the run token, which the session refuses.
|
|
25
|
+
*/
|
|
26
|
+
export function runnerTokenFor(input: {
|
|
27
|
+
readonly projectId: string
|
|
28
|
+
readonly runId: string
|
|
29
|
+
readonly executionToken?: string
|
|
30
|
+
}): string {
|
|
31
|
+
if (!input.executionToken) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`[SixbAgent] Agent execution '${input.runId}' must hold an execution token before it gets a runner token.`
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
return createAgentRunnerToken({
|
|
37
|
+
projectId: input.projectId,
|
|
38
|
+
runId: input.runId,
|
|
39
|
+
executionToken: input.executionToken,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The run token as a request credential: on a provider that adds credentials outside the guest,
|
|
45
|
+
* every call to the API under `apiBaseUrl` gets it, and the run's commands never hold it.
|
|
46
|
+
*/
|
|
47
|
+
export function runApiCredential(apiBaseUrl: string, runToken: string): SandboxRequestCredential {
|
|
48
|
+
const url = new URL(apiBaseUrl)
|
|
49
|
+
return {
|
|
50
|
+
origin: url.origin,
|
|
51
|
+
path: { prefix: `${url.pathname.replace(/\/+$/, "")}/api/` },
|
|
52
|
+
method: ["GET", "POST", "PUT", "PATCH", "DELETE"],
|
|
53
|
+
headers: { Authorization: `Bearer ${runToken}` },
|
|
54
|
+
}
|
|
55
|
+
}
|