@ouispec/agent-worker 0.13.0 → 0.14.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 +10 -9
- package/dist/approvals/client.d.ts +2 -2
- package/dist/approvals/continuation.d.ts +2 -2
- package/dist/approvals/preview.d.ts +1 -1
- package/dist/approvals/preview.js +1 -1
- package/dist/approvals/requirement.js +1 -1
- package/dist/attachments/content.d.ts +2 -2
- package/dist/attachments/guard.js +1 -1
- package/dist/attachments/limits.d.ts +2 -2
- package/dist/attachments/limits.d.ts.map +1 -1
- package/dist/attachments/limits.js +2 -2
- package/dist/attachments/limits.js.map +1 -1
- package/dist/attachments/store.d.ts +3 -3
- package/dist/attachments/store.d.ts.map +1 -1
- package/dist/attachments/tools.js +2 -2
- package/dist/attachments/tools.js.map +1 -1
- package/dist/authz/tool-policy.d.ts +4 -4
- package/dist/authz/tool-policy.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/model.d.ts +1 -1
- package/dist/openapi/executor.d.ts +1 -1
- package/dist/openapi/index.d.ts +1 -1
- package/dist/openapi/index.js +1 -1
- package/dist/openapi/loader.d.ts +4 -4
- package/dist/openapi/loader.js +7 -7
- package/dist/openapi/loader.js.map +1 -1
- package/dist/openapi/x-agent.d.ts +2 -2
- package/dist/openapi/x-agent.js +2 -2
- package/dist/orchestrator.d.ts +1 -1
- package/dist/orchestrator.d.ts.map +1 -1
- package/dist/orchestrator.js +61 -43
- package/dist/orchestrator.js.map +1 -1
- package/dist/prompt/rules/ui-control.d.ts +3 -3
- package/dist/prompt/rules/ui-control.js +3 -3
- package/dist/prompt/spoken-input.d.ts +1 -1
- package/dist/prompt/spoken-input.js +1 -1
- package/dist/reasoning-relay.d.ts +32 -0
- package/dist/reasoning-relay.d.ts.map +1 -0
- package/dist/reasoning-relay.js +96 -0
- package/dist/reasoning-relay.js.map +1 -0
- package/dist/runtime/config.js +1 -1
- package/dist/runtime/turn-runner.d.ts +2 -2
- package/dist/runtime/turn-runner.d.ts.map +1 -1
- package/dist/runtime/turn-runner.js +6 -5
- package/dist/runtime/turn-runner.js.map +1 -1
- package/dist/runtime/types.d.ts +26 -16
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/stop/partial.d.ts +1 -1
- package/dist/stop/partial.js +1 -1
- package/dist/stop/results.d.ts +1 -1
- package/dist/stop/turn-stop.d.ts +3 -3
- package/dist/stop/turn-stop.d.ts.map +1 -1
- package/dist/stop/turn-stop.js +1 -1
- package/dist/stop/turn-stop.js.map +1 -1
- package/dist/testing/desk-events.d.ts +1 -1
- package/dist/testing/desk-events.js +1 -1
- package/dist/time-hand-back.d.ts +3 -3
- package/dist/time-hand-back.js +3 -3
- package/dist/tools/async-binding.d.ts +1 -1
- package/dist/tools/builtin-tools.d.ts +3 -3
- package/dist/tools/entity-tools.d.ts.map +1 -1
- package/dist/tools/entity-tools.js +0 -1
- package/dist/tools/entity-tools.js.map +1 -1
- package/dist/tools/load-all-tools.d.ts.map +1 -1
- package/dist/tools/load-all-tools.js +0 -1
- package/dist/tools/load-all-tools.js.map +1 -1
- package/dist/tools/schema-loader.d.ts.map +1 -1
- package/dist/tools/schema-loader.js +1 -2
- package/dist/tools/schema-loader.js.map +1 -1
- package/dist/tools/types.d.ts +9 -9
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/turn-ledger.d.ts +1 -1
- package/dist/turn-ledger.js +1 -1
- package/dist/types.d.ts +23 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/answer-image.d.ts +1 -1
- package/dist/ui/answer-image.js +1 -1
- package/dist/ui/channel.d.ts +1 -1
- package/dist/ui/knowledge.d.ts +2 -2
- package/dist/ui/observations.d.ts +2 -2
- package/dist/ui/observations.js +2 -2
- package/dist/ui/outline.d.ts +1 -1
- package/dist/ui/outline.js +1 -1
- package/dist/ui/page-index.d.ts +1 -1
- package/dist/ui/page-index.js +1 -1
- package/dist/ui/snapshot.d.ts +2 -2
- package/dist/ui/snapshot.d.ts.map +1 -1
- package/dist/ui/ui-tools.d.ts +1 -1
- package/dist/ui/ui-tools.js +8 -8
- package/dist/ui/ui-tools.js.map +1 -1
- package/package.json +6 -6
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `x-agent`: how an operation opts in as an agent tool
|
|
2
|
+
* `x-agent`: how an operation opts in as an agent tool.
|
|
3
3
|
*
|
|
4
4
|
* ```yaml
|
|
5
5
|
* x-agent:
|
|
6
6
|
* expose: true # without it, the operation is not a tool
|
|
7
|
-
* effect: mutate #
|
|
7
|
+
* effect: mutate # view | file | mutate | job | transaction, or its object form
|
|
8
8
|
* destructive: true # optional, on a write: removes or replaces something the person made
|
|
9
9
|
* consequence: "..." # optional: what happens, for the tool's description and an approval's preview
|
|
10
10
|
* pa: true # optional: the product's own assistant may call it; only on a read
|
package/dist/openapi/x-agent.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `x-agent`: how an operation opts in as an agent tool
|
|
2
|
+
* `x-agent`: how an operation opts in as an agent tool.
|
|
3
3
|
*
|
|
4
4
|
* ```yaml
|
|
5
5
|
* x-agent:
|
|
6
6
|
* expose: true # without it, the operation is not a tool
|
|
7
|
-
* effect: mutate #
|
|
7
|
+
* effect: mutate # view | file | mutate | job | transaction, or its object form
|
|
8
8
|
* destructive: true # optional, on a write: removes or replaces something the person made
|
|
9
9
|
* consequence: "..." # optional: what happens, for the tool's description and an approval's preview
|
|
10
10
|
* pa: true # optional: the product's own assistant may call it; only on a read
|
package/dist/orchestrator.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { AgentWorkerConfig, AgentTurnInput, AgentTurnResult } from './types
|
|
|
5
5
|
* A turn runs in one or more segments. Each segment is one `streamText` call
|
|
6
6
|
* with a fixed tool set. When a UI action changes what the client's page
|
|
7
7
|
* offers (navigating is the common case), the segment ends and the turn
|
|
8
|
-
* continues in a new one whose tools are the new page's
|
|
8
|
+
* continues in a new one whose tools are the new page's. The
|
|
9
9
|
* round cap counts across segments.
|
|
10
10
|
*/
|
|
11
11
|
export declare function runAgentTurn(config: AgentWorkerConfig, input: AgentTurnInput): Promise<AgentTurnResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,eAAe,EAAmC,MAAM,YAAY,CAAC;AA0GtH;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,eAAe,CAAC,CAy5D1B"}
|
package/dist/orchestrator.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Agent Orchestrator — powered by Vercel AI SDK.
|
|
3
3
|
*
|
|
4
4
|
* The model is the host's: any `ai` library model, passed in configuration
|
|
5
|
-
* (
|
|
5
|
+
* ("Model"). The core names no provider. The AI SDK handles:
|
|
6
6
|
* - each provider's wire format (internal, never exposed)
|
|
7
7
|
* - tool_use / tool_result pairing (automatic, no manual repair needed)
|
|
8
8
|
* - Multi-round tool loops (via stopWhen)
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* - Error categorization and retries
|
|
12
12
|
*
|
|
13
13
|
* Our value-add stays:
|
|
14
|
-
* - UI tools from the client's OUI surfaces, answered by the client
|
|
14
|
+
* - UI tools from the client's OUI surfaces, answered by the client
|
|
15
15
|
* - Schema-based tool loading
|
|
16
16
|
* - Persona/prompt compilation
|
|
17
17
|
* - Emit adapter (realtime socket streaming)
|
|
@@ -19,12 +19,13 @@
|
|
|
19
19
|
* - Configurable TurnPolicy for product-specific step control
|
|
20
20
|
*/
|
|
21
21
|
import { streamText, dynamicTool, jsonSchema, isStepCount, hasToolCall } from 'ai';
|
|
22
|
-
import { AGENT_SOCKET_EVENTS, argsHash, canonicalJson, turnStoppedNote, } from '@ouispec/agent-core';
|
|
22
|
+
import { AGENT_SOCKET_EVENTS, argsHash, canonicalJson, isTurnEndReason, turnStoppedNote, } from '@ouispec/agent-core';
|
|
23
23
|
import { DEFAULT_STOP_GRACE_MS, TurnStopped, stopOf, watchTurnStop } from './stop/turn-stop.js';
|
|
24
24
|
import { stoppedTurnMessages } from './stop/partial.js';
|
|
25
25
|
import { resultText, stoppedNotRun, stoppedWhileRunning } from './stop/results.js';
|
|
26
26
|
import { defaultTurnPolicy } from './turn-policy.js';
|
|
27
27
|
import { createStepClock, timeHandBackNote } from './time-hand-back.js';
|
|
28
|
+
import { createReasoningRelay } from './reasoning-relay.js';
|
|
28
29
|
import { evaluateToolPolicySafe } from './authz/tool-policy.js';
|
|
29
30
|
import { createToolInputValidator } from './tools/input-validation.js';
|
|
30
31
|
import { readClientPage, withoutClientUI } from './ui/snapshot.js';
|
|
@@ -93,7 +94,7 @@ const notRun = (payload) => ({ text: JSON.stringify(payload), ran: false });
|
|
|
93
94
|
* A turn runs in one or more segments. Each segment is one `streamText` call
|
|
94
95
|
* with a fixed tool set. When a UI action changes what the client's page
|
|
95
96
|
* offers (navigating is the common case), the segment ends and the turn
|
|
96
|
-
* continues in a new one whose tools are the new page's
|
|
97
|
+
* continues in a new one whose tools are the new page's. The
|
|
97
98
|
* round cap counts across segments.
|
|
98
99
|
*/
|
|
99
100
|
export async function runAgentTurn(config, input) {
|
|
@@ -107,7 +108,7 @@ export async function runAgentTurn(config, input) {
|
|
|
107
108
|
const uiResultTimeoutMs = config.ui?.resultTimeoutMs ?? DEFAULT_UI_RESULT_TIMEOUT_MS;
|
|
108
109
|
const uiJobWaitMs = jobWaitOf({ jobWaitMs: config.ui?.jobWaitMs });
|
|
109
110
|
if (!config.model) {
|
|
110
|
-
throw new Error('[agent-sdk] config.model is required: pass an `ai` library model
|
|
111
|
+
throw new Error('[agent-sdk] config.model is required: pass an `ai` library model');
|
|
111
112
|
}
|
|
112
113
|
const { turnId, socketRoom } = input;
|
|
113
114
|
log('info', 'agent:turn', 'Turn started', {
|
|
@@ -120,7 +121,7 @@ export async function runAgentTurn(config, input) {
|
|
|
120
121
|
maxTokens,
|
|
121
122
|
temperature,
|
|
122
123
|
});
|
|
123
|
-
// ─── The client's UI
|
|
124
|
+
// ─── The client's UI ─────────────────────────────────────────
|
|
124
125
|
// The turn carries the surfaces of the client that sent it. They are the only
|
|
125
126
|
// source of UI tools, so the tools always describe the page the user is on.
|
|
126
127
|
const client = readClientPage(input.context ?? null);
|
|
@@ -128,7 +129,7 @@ export async function runAgentTurn(config, input) {
|
|
|
128
129
|
throw new Error('[agent-sdk] The turn carries UI surfaces but the worker has no UI action channel (config.ui): ' +
|
|
129
130
|
'its UI actions could be sent but never answered.');
|
|
130
131
|
}
|
|
131
|
-
// What the page offers, in index
|
|
132
|
+
// What the page offers, in index: kept current by every answer.
|
|
132
133
|
let currentPage = client?.page ?? [];
|
|
133
134
|
// The turn's UI actions run one at a time, in the order the model called them (ui-sequence.ts).
|
|
134
135
|
const uiSequence = createUISequence();
|
|
@@ -137,14 +138,14 @@ export async function runAgentTurn(config, input) {
|
|
|
137
138
|
uiSequence.record(client.page, client.surfacesHash);
|
|
138
139
|
// The definitions the turn holds: those a client sent, and those fetched since.
|
|
139
140
|
const heldDefinitions = client?.held ?? new Map();
|
|
140
|
-
// Whether the page can be seen
|
|
141
|
+
// Whether the page can be seen: nothing that changes it runs while it cannot.
|
|
141
142
|
const sight = createPageSight();
|
|
142
143
|
const uiCounts = { describes: 0, reads: 0, acts: 0, guides: 0 };
|
|
143
144
|
// Answers whose page state was moved to the end of what the model reads, summed over the turn's steps.
|
|
144
145
|
let pageStatesMoved = 0;
|
|
145
146
|
// The knowledge the client sent for its page goes after the host's prompt.
|
|
146
147
|
const knowledge = readClientKnowledge(input.context ?? null);
|
|
147
|
-
// ─── Files
|
|
148
|
+
// ─── Files ─────────────────────────────────────────
|
|
148
149
|
// With the host's file area, the turn's files go to the model with its
|
|
149
150
|
// message, the attachment tools reach every file of the conversation, and the
|
|
150
151
|
// cost guard caps what the model is given of them.
|
|
@@ -201,7 +202,7 @@ export async function runAgentTurn(config, input) {
|
|
|
201
202
|
const hostPrompt = typeof config.systemPrompt === 'function'
|
|
202
203
|
? config.systemPrompt({ userId: input.userId, accountId: input.accountId, context: withoutClientUI(input.context) })
|
|
203
204
|
: config.systemPrompt;
|
|
204
|
-
// With a file area, the model is told a file's name and content are data, never instructions
|
|
205
|
+
// With a file area, the model is told a file's name and content are data, never instructions.
|
|
205
206
|
const systemPrompt = withClientKnowledge(fileArea ? `${hostPrompt}\n\n${ATTACHMENT_DATA_NOTE}` : hostPrompt, input.context);
|
|
206
207
|
log('debug', 'agent:prompt', 'System prompt built', { turnId, promptLength: systemPrompt.length, hostPromptLength: hostPrompt.length });
|
|
207
208
|
// Wall-clock deadline: abort the entire streamText call if it exceeds turnDeadlineMs.
|
|
@@ -209,14 +210,14 @@ export async function runAgentTurn(config, input) {
|
|
|
209
210
|
// A UI tool waiting on work it started stops in time for the model to answer.
|
|
210
211
|
const uiWaitDeadline = () => turnStartedAt + turnDeadlineMs - UI_ANSWER_MARGIN_MS;
|
|
211
212
|
const abortController = new AbortController();
|
|
212
|
-
// ─── Stopping
|
|
213
|
+
// ─── Stopping ───────────────────────────────────────────────────
|
|
213
214
|
// The person's Stop, or a newer message superseding this turn, is kept by the
|
|
214
215
|
// realtime server; the watch asks for it for as long as the turn runs. A stop
|
|
215
216
|
// aborts the turn's signal with a typed reason, which is what everything that
|
|
216
217
|
// waits (the model stream, a UI action's answer, a job's outcome) already
|
|
217
218
|
// honours. What the turn had produced is then stored and announced (below).
|
|
218
219
|
const stopWatch = input.stopWatch ?? watchTurnStop(config.stops, turnId, input.userId);
|
|
219
|
-
/** Set when the turn begins its own end: a stop that lands after is not one
|
|
220
|
+
/** Set when the turn begins its own end: a stop that lands after is not one. */
|
|
220
221
|
let ending = false;
|
|
221
222
|
/** Kept back from a stopped turn's grace for storing and announcing it. */
|
|
222
223
|
const STOP_STORE_MARGIN_MS = 3_000;
|
|
@@ -251,10 +252,10 @@ export async function runAgentTurn(config, input) {
|
|
|
251
252
|
abortController.abort(stop);
|
|
252
253
|
};
|
|
253
254
|
stopWatch.onStop((record) => stopTurn(new TurnStopped(record.reason, record.at)));
|
|
254
|
-
// The turn's deadline ends it on the same path
|
|
255
|
+
// The turn's deadline ends it on the same path: what it did
|
|
255
256
|
// before it ran out of time is kept, marked, and the next turn is told.
|
|
256
257
|
const deadlineTimer = setTimeout(() => stopTurn(new TurnStopped('deadline', Date.now())), turnDeadlineMs);
|
|
257
|
-
// Before the deadline, the turn hands back with an answer when time is short
|
|
258
|
+
// Before the deadline, the turn hands back with an answer when time is short.
|
|
258
259
|
const stepClock = createStepClock({
|
|
259
260
|
deadlineAt: turnStartedAt + turnDeadlineMs,
|
|
260
261
|
turnDeadlineMs,
|
|
@@ -304,16 +305,16 @@ export async function runAgentTurn(config, input) {
|
|
|
304
305
|
const toolInvocationCounts = new Map();
|
|
305
306
|
const DEFAULT_TOOL_QUOTA = 12;
|
|
306
307
|
const SIDE_EFFECT_TOOL_QUOTA = 2;
|
|
307
|
-
// A UI action is not counted (
|
|
308
|
+
// A UI action is not counted (filling 12 slides of 3 values each
|
|
308
309
|
// ran one action 36 times, and a cap of 12 stopped it). What is refused is a loop:
|
|
309
310
|
// the same call with the same input, made again after it changed nothing three
|
|
310
311
|
// times in a row. Undo five times, or nudge a layer five times, is work: each changes the page.
|
|
311
312
|
const UNCHANGED_REPEATS = 3;
|
|
312
313
|
const uiRepeats = new Map();
|
|
313
314
|
// What each call of the turn did: said back to the model before every step
|
|
314
|
-
// after one did not succeed, so its reply matches it
|
|
315
|
+
// after one did not succeed, so its reply matches it.
|
|
315
316
|
const ledger = createTurnLedger();
|
|
316
|
-
// ─── Approvals
|
|
317
|
+
// ─── Approvals ──────────────────────────────────────────────────
|
|
317
318
|
// The first call in a turn that needs the user's approval stops the turn:
|
|
318
319
|
// it is stored as a pending approval, the card is shown, and nothing else
|
|
319
320
|
// runs until they decide. Each call is approved separately.
|
|
@@ -348,7 +349,7 @@ export async function runAgentTurn(config, input) {
|
|
|
348
349
|
* A call refused before its tool ran: invalid input, over its quota, or
|
|
349
350
|
* denied by policy. The model gets the reason; so does the client, as a
|
|
350
351
|
* call that started and failed, so a session's record holds every call
|
|
351
|
-
* the model made with its arguments
|
|
352
|
+
* the model made with its arguments.
|
|
352
353
|
*/
|
|
353
354
|
const refused = async (payload, given) => {
|
|
354
355
|
ledger.record({ tool: t.name, ok: false, error: payload.error });
|
|
@@ -377,7 +378,7 @@ export async function runAgentTurn(config, input) {
|
|
|
377
378
|
const timeoutMs = isUI
|
|
378
379
|
? Math.max(toolTimeoutMs, uiResultTimeoutMs + 5_000, Math.min(uiJobWaitMs, Math.max(0, uiWaitDeadline() - Date.now())) + uiResultTimeoutMs + 5_000)
|
|
379
380
|
: toolTimeoutMs;
|
|
380
|
-
// ── No changes while blind
|
|
381
|
+
// ── No changes while blind ──
|
|
381
382
|
// After an answer that came without the page's state, or no answer, only
|
|
382
383
|
// reading runs until a read shows the page again. A change called then is
|
|
383
384
|
// refused here, before its definition is fetched or policy is asked; one
|
|
@@ -395,7 +396,7 @@ export async function runAgentTurn(config, input) {
|
|
|
395
396
|
// Before quota and policy: an invalid call consumes no quota and never
|
|
396
397
|
// reaches the host. The model gets the errors so it can correct the call.
|
|
397
398
|
// A UI action's schema is its definition's, fetched from the page when the
|
|
398
|
-
// action is first used
|
|
399
|
+
// action is first used.
|
|
399
400
|
let schema = (t.inputSchema ?? {});
|
|
400
401
|
if (t.resolveInputSchema) {
|
|
401
402
|
try {
|
|
@@ -428,7 +429,7 @@ export async function runAgentTurn(config, input) {
|
|
|
428
429
|
}, rawArgs);
|
|
429
430
|
}
|
|
430
431
|
const args = validation.value;
|
|
431
|
-
// ── Attached files named in the call
|
|
432
|
+
// ── Attached files named in the call ──
|
|
432
433
|
// Each must be a file of this turn's conversation, ready, and of a type the
|
|
433
434
|
// input takes, checked before anything is dispatched; the page then
|
|
434
435
|
// resolves it through its own host. An action declaring a file where none
|
|
@@ -492,13 +493,13 @@ export async function runAgentTurn(config, input) {
|
|
|
492
493
|
// The call as a repeat is judged by: its name and its exact input. Set for a UI action that changes the page.
|
|
493
494
|
let repeatKey = null;
|
|
494
495
|
// ── Quota enforcement ──
|
|
495
|
-
// UI actions run as the user in their own session
|
|
496
|
-
// backend side-effect cap does not apply to them
|
|
496
|
+
// UI actions run as the user in their own session, so the
|
|
497
|
+
// backend side-effect cap does not apply to them. They are
|
|
497
498
|
// bounded by the turn's steps and deadline; only a repeated identical call is refused.
|
|
498
499
|
const currentCount = toolInvocationCounts.get(t.name) ?? 0;
|
|
499
500
|
const isSideEffecting = schema.sideEffects !== false; // fail-closed: undefined = side-effecting
|
|
500
501
|
if (isUI) {
|
|
501
|
-
// A UI action that only reads (a room's inspect and query
|
|
502
|
+
// A UI action that only reads (a room's inspect and query)
|
|
502
503
|
// changes nothing however often it is called.
|
|
503
504
|
if (!reads) {
|
|
504
505
|
let input;
|
|
@@ -530,7 +531,7 @@ export async function runAgentTurn(config, input) {
|
|
|
530
531
|
toolInvocationCounts.set(t.name, currentCount + 1);
|
|
531
532
|
}
|
|
532
533
|
// ── Policy enforcement ──
|
|
533
|
-
// A deny always wins, over an approval too
|
|
534
|
+
// A deny always wins, over an approval too.
|
|
534
535
|
const requirement = approvalRequirement(t);
|
|
535
536
|
let policyRequiresApproval = false;
|
|
536
537
|
if (config.toolPolicy) {
|
|
@@ -551,7 +552,7 @@ export async function runAgentTurn(config, input) {
|
|
|
551
552
|
}
|
|
552
553
|
policyRequiresApproval = decision.action === 'require_approval';
|
|
553
554
|
}
|
|
554
|
-
// ── Approval
|
|
555
|
+
// ── Approval ──
|
|
555
556
|
// Checked and set with no await in between, so of the calls in one
|
|
556
557
|
// response exactly the first that needs approval stops the turn.
|
|
557
558
|
if (approvalHold && !approved) {
|
|
@@ -696,12 +697,12 @@ export async function runAgentTurn(config, input) {
|
|
|
696
697
|
};
|
|
697
698
|
};
|
|
698
699
|
/**
|
|
699
|
-
* Stops the turn at a call that needs the user's approval
|
|
700
|
+
* Stops the turn at a call that needs the user's approval:
|
|
700
701
|
* stores it as a pending approval, shows the card, and tells the model only
|
|
701
702
|
* that it is waiting. The model never sees a token.
|
|
702
703
|
*/
|
|
703
704
|
const requestApproval = async (t, args, toolUseId, requirement, files) => {
|
|
704
|
-
// A file is shown by its name and size; the approval stays bound to its id
|
|
705
|
+
// A file is shown by its name and size; the approval stays bound to its id.
|
|
705
706
|
const preview = buildApprovalPreview(t, args, files);
|
|
706
707
|
approvalHold = { approvalId: toolUseId, title: preview.title };
|
|
707
708
|
const refuse = (why) => {
|
|
@@ -842,7 +843,7 @@ export async function runAgentTurn(config, input) {
|
|
|
842
843
|
return builtUI;
|
|
843
844
|
}
|
|
844
845
|
// The host's tools the model is given. A host tool with a UI tool's name, or
|
|
845
|
-
// with the id of an action of the page, is withheld
|
|
846
|
+
// with the id of an action of the page, is withheld: for a
|
|
846
847
|
// UI-only agent, the page is the authority.
|
|
847
848
|
function hostTools(ui) {
|
|
848
849
|
const sdkOwned = new Set(fileTools.map((t) => t.name));
|
|
@@ -952,7 +953,7 @@ export async function runAgentTurn(config, input) {
|
|
|
952
953
|
return tools;
|
|
953
954
|
}
|
|
954
955
|
/**
|
|
955
|
-
* The turn after the user's decision on an approval card
|
|
956
|
+
* The turn after the user's decision on an approval card:
|
|
956
957
|
* redeem the token and run exactly the stored call, or learn that they
|
|
957
958
|
* declined. What happened goes to the model as an `<approval>` note.
|
|
958
959
|
*
|
|
@@ -1084,6 +1085,14 @@ export async function runAgentTurn(config, input) {
|
|
|
1084
1085
|
});
|
|
1085
1086
|
}
|
|
1086
1087
|
};
|
|
1088
|
+
// The model's thinking, relayed to the room: its start and end, and its
|
|
1089
|
+
// summary when the host asked the model for one (reasoning-relay.ts).
|
|
1090
|
+
const reasoningRelay = createReasoningRelay({
|
|
1091
|
+
turnId,
|
|
1092
|
+
emit: (event, data) => config.emit.emit(socketRoom, event, data),
|
|
1093
|
+
coalesceMs: COALESCE_MS,
|
|
1094
|
+
coalesceChars: COALESCE_CHARS,
|
|
1095
|
+
});
|
|
1087
1096
|
// Run the multi-round generation
|
|
1088
1097
|
try {
|
|
1089
1098
|
// ─── Prompt cache breakpoints ──────────────────────────────────────────────
|
|
@@ -1130,7 +1139,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1130
1139
|
const stoppedBeforeStart = stopOf(abortController.signal);
|
|
1131
1140
|
if (stoppedBeforeStart)
|
|
1132
1141
|
return await finishStopped(stoppedBeforeStart, { expired: null, continued: null });
|
|
1133
|
-
// ─── The turn after an approval decision
|
|
1142
|
+
// ─── The turn after an approval decision ────────────────
|
|
1134
1143
|
const continued = input.approval ? await continueApproval(input.approval) : null;
|
|
1135
1144
|
// ─── Calls still stored as waiting, whose approval expired undecided ──────
|
|
1136
1145
|
//
|
|
@@ -1170,7 +1179,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1170
1179
|
continue;
|
|
1171
1180
|
// The user had approved it and it was never run, or nobody decided: the result says which.
|
|
1172
1181
|
const wasApproved = answer.decided === 'approved';
|
|
1173
|
-
// Withdrawn (a new message, or its turn stopped) is said as that, not as time running out
|
|
1182
|
+
// Withdrawn (a new message, or its turn stopped) is said as that, not as time running out.
|
|
1174
1183
|
const content = notRunResult(answer.withdrawn
|
|
1175
1184
|
? withdrawnMarker(answer.withdrawn, wasApproved)
|
|
1176
1185
|
: wasApproved
|
|
@@ -1224,7 +1233,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1224
1233
|
turnFilesGiven && input.history?.at(-1)?.role === 'user' ? input.history.length - 1 : null),
|
|
1225
1234
|
...(continued?.messages ?? []),
|
|
1226
1235
|
];
|
|
1227
|
-
// The turn's files go with its message, after everything said about it
|
|
1236
|
+
// The turn's files go with its message, after everything said about it.
|
|
1228
1237
|
// A file that cannot be had is named with why; the turn goes on with the others.
|
|
1229
1238
|
if (fileArea && turnFilesGiven) {
|
|
1230
1239
|
try {
|
|
@@ -1360,12 +1369,15 @@ export async function runAgentTurn(config, input) {
|
|
|
1360
1369
|
maxOutputTokens: maxTokens,
|
|
1361
1370
|
maxRetries: retries,
|
|
1362
1371
|
abortSignal: abortController.signal,
|
|
1372
|
+
// The host's, the same for every call (a thinking setting that changes invalidates the cache).
|
|
1373
|
+
...(config.modelProviderOptions ? { providerOptions: config.modelProviderOptions } : {}),
|
|
1374
|
+
onChunk: ({ chunk }) => reasoningRelay.onPart(chunk),
|
|
1363
1375
|
stopWhen: [
|
|
1364
1376
|
isStepCount(maxRounds - allSteps.length),
|
|
1365
1377
|
...USER_INPUT_TOOLS.map((name) => hasToolCall(name)),
|
|
1366
|
-
// A call is waiting for the user's approval: the turn ends here
|
|
1378
|
+
// A call is waiting for the user's approval: the turn ends here.
|
|
1367
1379
|
() => approvalHold !== null,
|
|
1368
|
-
// The step that handed the turn back for time was its last
|
|
1380
|
+
// The step that handed the turn back for time was its last.
|
|
1369
1381
|
() => handedBackForTime,
|
|
1370
1382
|
],
|
|
1371
1383
|
...(temperature !== undefined ? { temperature } : {}),
|
|
@@ -1464,7 +1476,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1464
1476
|
if (trimmed.newest !== undefined)
|
|
1465
1477
|
newestAnswerState = trimmed.newest;
|
|
1466
1478
|
}
|
|
1467
|
-
// The files this step would carry, within the turn's and the conversation's caps
|
|
1479
|
+
// The files this step would carry, within the turn's and the conversation's caps.
|
|
1468
1480
|
const leftOutNow = fileGuard.beforeStep();
|
|
1469
1481
|
if (leftOutNow.length > 0) {
|
|
1470
1482
|
log('info', 'agent:tool', 'Files left out of the next step to keep within the cap', {
|
|
@@ -1580,7 +1592,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1580
1592
|
if (!stopOf(abortController.signal))
|
|
1581
1593
|
throw err;
|
|
1582
1594
|
}
|
|
1583
|
-
// The model's tools no longer change with the page
|
|
1595
|
+
// The model's tools no longer change with the page: what
|
|
1584
1596
|
// the page offers reaches it in each answer, so the turn runs as one segment.
|
|
1585
1597
|
break;
|
|
1586
1598
|
}
|
|
@@ -1655,7 +1667,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1655
1667
|
promptTokens: usage.promptTokens,
|
|
1656
1668
|
cacheReadTokens: usage.cacheReadTokens,
|
|
1657
1669
|
cacheWriteTokens: usage.cacheWriteTokens,
|
|
1658
|
-
// How the page was worked
|
|
1670
|
+
// How the page was worked.
|
|
1659
1671
|
...(client
|
|
1660
1672
|
? {
|
|
1661
1673
|
ui: {
|
|
@@ -1692,10 +1704,14 @@ export async function runAgentTurn(config, input) {
|
|
|
1692
1704
|
});
|
|
1693
1705
|
}
|
|
1694
1706
|
}
|
|
1707
|
+
// Everything before the turn's end has been sent: its thinking too.
|
|
1708
|
+
await reasoningRelay.settle();
|
|
1695
1709
|
await config.emit.emit(socketRoom, AGENT_SOCKET_EVENTS.TURN_COMPLETE, {
|
|
1696
1710
|
turnId,
|
|
1697
1711
|
rounds: steps.length,
|
|
1698
1712
|
usage,
|
|
1713
|
+
// How it ended, so a view can tell an answer from a cut-off or a hand-back.
|
|
1714
|
+
...(isTurnEndReason(stopReason) ? { endReason: stopReason } : {}),
|
|
1699
1715
|
timestamp: Date.now(),
|
|
1700
1716
|
});
|
|
1701
1717
|
log('info', 'agent:turn', 'Turn completed', {
|
|
@@ -1726,7 +1742,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1726
1742
|
stopWatch.close();
|
|
1727
1743
|
}
|
|
1728
1744
|
/**
|
|
1729
|
-
* The end of a stopped turn
|
|
1745
|
+
* The end of a stopped turn: settle what was in flight, store
|
|
1730
1746
|
* what the turn had produced, then say so. Persist first, announce after, as
|
|
1731
1747
|
* a turn that ends by itself does.
|
|
1732
1748
|
*
|
|
@@ -1757,7 +1773,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1757
1773
|
clearTimeout(timer);
|
|
1758
1774
|
}
|
|
1759
1775
|
// A stopped turn leaves no live card: an approval it asked for as the stop came
|
|
1760
|
-
// is withdrawn now, and a later turn records it as that
|
|
1776
|
+
// is withdrawn now, and a later turn records it as that.
|
|
1761
1777
|
const held = approvalHold;
|
|
1762
1778
|
if (held && config.approvals?.withdraw) {
|
|
1763
1779
|
try {
|
|
@@ -1835,7 +1851,7 @@ export async function runAgentTurn(config, input) {
|
|
|
1835
1851
|
return true;
|
|
1836
1852
|
};
|
|
1837
1853
|
// Whether what the turn had produced was stored: a deadline stop that could not store it is
|
|
1838
|
-
// not a stopped turn but one that ran out of time with nothing kept
|
|
1854
|
+
// not a stopped turn but one that ran out of time with nothing kept.
|
|
1839
1855
|
let stored = true;
|
|
1840
1856
|
if (config.beforeTurnComplete) {
|
|
1841
1857
|
try {
|
|
@@ -1850,6 +1866,8 @@ export async function runAgentTurn(config, input) {
|
|
|
1850
1866
|
});
|
|
1851
1867
|
}
|
|
1852
1868
|
}
|
|
1869
|
+
// A block the stop cut off is ended; its emits are not waited for, as the announcement is time-boxed.
|
|
1870
|
+
void reasoningRelay.settle();
|
|
1853
1871
|
try {
|
|
1854
1872
|
await within('announcement', STOP_STEP_FLOOR_MS, () => config.emit.emit(socketRoom, AGENT_SOCKET_EVENTS.TURN_COMPLETE, {
|
|
1855
1873
|
turnId,
|
|
@@ -1936,11 +1954,11 @@ turnMessageAt = null) {
|
|
|
1936
1954
|
for (let idx = 0; idx < history.length; idx++) {
|
|
1937
1955
|
const msg = history[idx];
|
|
1938
1956
|
if (msg.role === 'user') {
|
|
1939
|
-
// A message's files are in history as their reference lines, never their content
|
|
1957
|
+
// A message's files are in history as their reference lines, never their content.
|
|
1940
1958
|
messages.push({ role: 'user', content: idx === turnMessageAt ? msg.content : withReferenceLines(msg.content, msg.attachments ?? undefined) });
|
|
1941
1959
|
}
|
|
1942
1960
|
else if (msg.role === 'assistant') {
|
|
1943
|
-
// A stopped turn's last message says so after its text
|
|
1961
|
+
// A stopped turn's last message says so after its text.
|
|
1944
1962
|
// A message that has only that carries the line as its whole text: an
|
|
1945
1963
|
// empty text block is refused by a model provider.
|
|
1946
1964
|
const stoppedLine = msg.stopped ? turnStoppedNote(msg.stopped) : null;
|