@qvac/harness 1.0.0-rc.20 → 1.0.0-rc.21
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/CHANGELOG.md +23 -0
- package/dist-lib/index.d.ts +7 -5
- package/dist-lib/index.js +5 -4
- package/dist-lib/lib/agent/resolve.js +2 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +2 -2
- package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +1 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +1 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +8 -6
- package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +1 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/inspect-prepare.js +1 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +2 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +8 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +10 -4
- package/dist-lib/lib/capabilities/completion-engine/actors/run-prepare.js +2 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +8 -8
- package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +2 -2
- package/dist-lib/lib/capabilities/completion-engine/loop.d.ts +3 -2
- package/dist-lib/lib/capabilities/completion-engine/loop.js +1 -0
- package/dist-lib/lib/capabilities/completion-engine/round.js +9 -9
- package/dist-lib/lib/capabilities/completion-engine/tool-caller.js +3 -1
- package/dist-lib/lib/capabilities/completion-engine/tool-execution.d.ts +5 -41
- package/dist-lib/lib/capabilities/completion-engine/tool-execution.js +4 -133
- package/dist-lib/lib/capabilities/completion-engine/tool-outcome.d.ts +4 -0
- package/dist-lib/lib/capabilities/completion-engine/tool-outcome.js +42 -0
- package/dist-lib/lib/capabilities/completion-engine/tool-preflight.d.ts +43 -0
- package/dist-lib/lib/capabilities/completion-engine/tool-preflight.js +91 -0
- package/dist-lib/lib/capabilities/completion-engine/turns.js +7 -4
- package/dist-lib/lib/capabilities/tool-turns/attachments.d.ts +7 -0
- package/dist-lib/lib/capabilities/tool-turns/attachments.js +39 -0
- package/dist-lib/lib/capabilities/tool-turns/copy.d.ts +13 -0
- package/dist-lib/lib/capabilities/tool-turns/copy.js +29 -0
- package/dist-lib/lib/capabilities/tool-turns/result-turn.d.ts +22 -0
- package/dist-lib/lib/capabilities/tool-turns/result-turn.js +120 -0
- package/dist-lib/lib/capabilities/tool-turns/run-limits.d.ts +6 -0
- package/dist-lib/lib/capabilities/tool-turns/run-limits.js +32 -0
- package/dist-lib/lib/capabilities/tool-turns/skill-loads.d.ts +9 -0
- package/dist-lib/lib/capabilities/tool-turns/skill-loads.js +71 -0
- package/dist-lib/lib/capabilities/tool-turns.d.ts +8 -54
- package/dist-lib/lib/capabilities/tool-turns.js +6 -281
- package/dist-lib/lib/client/index.js +3 -3
- package/dist-lib/lib/connect/abort.d.ts +6 -0
- package/dist-lib/lib/connect/abort.js +52 -0
- package/dist-lib/lib/connect/bridges.d.ts +21 -0
- package/dist-lib/lib/connect/bridges.js +185 -0
- package/dist-lib/lib/connect/decode.d.ts +31 -0
- package/dist-lib/lib/connect/decode.js +114 -0
- package/dist-lib/lib/connect/host-run-log.d.ts +16 -0
- package/dist-lib/lib/connect/host-run-log.js +74 -0
- package/dist-lib/lib/connect/served.d.ts +7 -0
- package/dist-lib/lib/connect/served.js +45 -0
- package/dist-lib/lib/connect.d.ts +3 -3
- package/dist-lib/lib/connect.js +24 -468
- package/dist-lib/lib/context-window.d.ts +13 -7
- package/dist-lib/lib/context-window.js +13 -6
- package/dist-lib/lib/contract/agent.d.ts +12 -1
- package/dist-lib/lib/contract/completion.d.ts +5 -1
- package/dist-lib/lib/contract/drive.d.ts +130 -4
- package/dist-lib/lib/contract/ocr.d.ts +5 -1
- package/dist-lib/lib/contract-types.d.ts +5 -3
- package/dist-lib/lib/errors.js +3 -3
- package/dist-lib/lib/helpers/goal-check/asks.d.ts +25 -0
- package/dist-lib/lib/helpers/goal-check/asks.js +52 -0
- package/dist-lib/lib/helpers/goal-check/checks.d.ts +8 -0
- package/dist-lib/lib/helpers/goal-check/checks.js +92 -0
- package/dist-lib/lib/helpers/goal-check/commands.d.ts +7 -0
- package/dist-lib/lib/helpers/goal-check/commands.js +130 -0
- package/dist-lib/lib/helpers/goal-check/paths.d.ts +5 -0
- package/dist-lib/lib/helpers/goal-check/paths.js +44 -0
- package/dist-lib/lib/helpers/goal-check/shell.d.ts +9 -0
- package/dist-lib/lib/helpers/goal-check/shell.js +96 -0
- package/dist-lib/lib/helpers/goal-check.d.ts +35 -43
- package/dist-lib/lib/helpers/goal-check.js +34 -406
- package/dist-lib/lib/helpers/recent-map.d.ts +7 -0
- package/dist-lib/lib/helpers/recent-map.js +23 -0
- package/dist-lib/lib/helpers/tool-call-markup/emit.d.ts +6 -0
- package/dist-lib/lib/helpers/tool-call-markup/emit.js +15 -0
- package/dist-lib/lib/helpers/tool-call-markup/recover.d.ts +22 -0
- package/dist-lib/lib/helpers/tool-call-markup/recover.js +130 -0
- package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.d.ts +3 -0
- package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.js +56 -0
- package/dist-lib/lib/helpers/tool-call-markup/strip.d.ts +4 -0
- package/dist-lib/lib/helpers/tool-call-markup/strip.js +149 -0
- package/dist-lib/lib/helpers/tool-call-markup.d.ts +19 -24
- package/dist-lib/lib/helpers/tool-call-markup.js +17 -343
- package/dist-lib/lib/machine/completion/actions/completion.d.ts +12 -18
- package/dist-lib/lib/machine/completion/actions/completion.js +19 -178
- package/dist-lib/lib/machine/completion/actions/goal.d.ts +10 -0
- package/dist-lib/lib/machine/completion/actions/goal.js +93 -0
- package/dist-lib/lib/machine/completion/actions/prepare.js +2 -1
- package/dist-lib/lib/machine/completion/actions/reject-tool.d.ts +16 -0
- package/dist-lib/lib/machine/completion/actions/reject-tool.js +147 -0
- package/dist-lib/lib/machine/completion/actions/retry-round.d.ts +6 -0
- package/dist-lib/lib/machine/completion/actions/retry-round.js +50 -0
- package/dist-lib/lib/machine/completion/actions/rounds.d.ts +5 -0
- package/dist-lib/lib/machine/completion/actions/rounds.js +31 -0
- package/dist-lib/lib/machine/completion/actions/tools.d.ts +1 -16
- package/dist-lib/lib/machine/completion/actions/tools.js +16 -161
- package/dist-lib/lib/machine/completion/context.js +3 -3
- package/dist-lib/lib/machine/completion/implementations.d.ts +2 -2
- package/dist-lib/lib/machine/completion/implementations.js +6 -2
- package/dist-lib/lib/machine/completion/machine.d.ts +2 -2
- package/dist-lib/lib/machine/completion/metrics.d.ts +25 -0
- package/dist-lib/lib/machine/completion/metrics.js +68 -0
- package/dist-lib/lib/machine/completion/nudges.d.ts +5 -0
- package/dist-lib/lib/machine/completion/nudges.js +33 -0
- package/dist-lib/lib/machine/completion/rules.d.ts +5 -34
- package/dist-lib/lib/machine/completion/rules.js +4 -120
- package/dist-lib/lib/machine/completion/splice.d.ts +9 -0
- package/dist-lib/lib/machine/completion/splice.js +19 -0
- package/dist-lib/lib/machine/completion/types.d.ts +4 -13
- package/dist-lib/lib/machine/index.d.ts +3 -3
- package/dist-lib/lib/machine/index.js +2 -2
- package/dist-lib/lib/media-host.js +2 -2
- package/dist-lib/lib/media-wire.js +2 -2
- package/dist-lib/lib/residency/completion-runtime.d.ts +2 -1
- package/dist-lib/lib/residency/completion-runtime.js +44 -1
- package/dist-lib/lib/residency/index.d.ts +3 -2
- package/dist-lib/lib/residency/index.js +2 -2
- package/dist-lib/lib/round-host.js +3 -3
- package/dist-lib/lib/round-port.js +3 -3
- package/dist-lib/lib/run-log/redact.js +7 -7
- package/dist-lib/lib/serve/completion.d.ts +7 -0
- package/dist-lib/lib/serve/completion.js +553 -0
- package/dist-lib/lib/serve/downloads.d.ts +4 -0
- package/dist-lib/lib/serve/downloads.js +79 -0
- package/dist-lib/lib/serve/models.d.ts +5 -0
- package/dist-lib/lib/serve/models.js +120 -0
- package/dist-lib/lib/serve/noop.d.ts +1 -0
- package/dist-lib/lib/serve/noop.js +1 -0
- package/dist-lib/lib/serve/requests.d.ts +147 -0
- package/dist-lib/lib/serve/requests.js +1 -0
- package/dist-lib/lib/serve/rounds.d.ts +11 -0
- package/dist-lib/lib/serve/rounds.js +67 -0
- package/dist-lib/lib/serve/run-log-start.d.ts +38 -0
- package/dist-lib/lib/serve/run-log-start.js +254 -0
- package/dist-lib/lib/serve/skills.d.ts +32 -0
- package/dist-lib/lib/serve/skills.js +46 -0
- package/dist-lib/lib/serve.d.ts +5 -1
- package/dist-lib/lib/serve.js +14 -1071
- package/dist-lib/lib/skills-host/index.d.ts +4 -2
- package/dist-lib/lib/skills-host/index.js +20 -8
- package/dist-lib/lib/tools/fs/drive.d.ts +1 -17
- package/dist-lib/lib/tools/fs/remote-protocol.d.ts +1 -81
- package/dist-lib/lib/tools/fs/scope.d.ts +2 -17
- package/dist-lib/lib/tools/fs/search.d.ts +2 -18
- package/dist-lib/lib/tools/fs/walk.d.ts +1 -5
- package/dist-lib/lib/tools/pdf/build.d.ts +2 -2
- package/dist-lib/lib/tools/pdf/recognize.js +4 -8
- package/dist-lib/lib/tools/pdf/request.d.ts +3 -8
- package/dist-lib/lib/tools/pdf/request.js +4 -4
- package/dist-lib/lib/tools/pdf.d.ts +0 -1
- package/dist-lib/lib/tools/pdf.js +0 -1
- package/dist-lib/lib/tools/skill.js +13 -4
- package/dist-lib/lib/tools/skills/bundled-dir.d.ts +3 -0
- package/dist-lib/lib/tools/skills/bundled-dir.js +27 -24
- package/dist-lib/lib/tools/skills/bundled-hash.d.ts +1 -0
- package/dist-lib/lib/tools/skills/bundled-hash.js +4 -1
- package/dist-lib/lib/tools/skills/catalog-loader.d.ts +12 -0
- package/dist-lib/lib/tools/skills/catalog-loader.js +173 -0
- package/dist-lib/lib/tools/skills/catalog.d.ts +5 -8
- package/dist-lib/lib/tools/skills/catalog.js +1 -162
- package/dist-lib/lib/tools/skills/index.d.ts +1 -0
- package/dist-lib/lib/tools/skills/index.js +1 -0
- package/dist-lib/lib/tools/skills/skill-tree.d.ts +5 -0
- package/dist-lib/lib/tools/skills/skill-tree.js +54 -0
- package/dist-lib/lib/wire/download.d.ts +10 -0
- package/dist-lib/lib/wire/download.js +35 -0
- package/dist-lib/lib/wire/event.d.ts +41 -0
- package/dist-lib/lib/wire/event.js +306 -0
- package/dist-lib/lib/wire/fields.d.ts +3 -0
- package/dist-lib/lib/wire/fields.js +12 -0
- package/dist-lib/lib/wire/knowledge.d.ts +9 -0
- package/dist-lib/lib/wire/knowledge.js +68 -0
- package/dist-lib/lib/wire/round.d.ts +14 -0
- package/dist-lib/lib/wire/round.js +45 -0
- package/dist-lib/lib/wire/vectors.d.ts +6 -0
- package/dist-lib/lib/wire/vectors.js +33 -0
- package/dist-lib/lib/wire.d.ts +28 -77
- package/dist-lib/lib/wire.js +24 -489
- package/dist-lib/plugins/ocr/coverage.d.ts +3 -0
- package/dist-lib/plugins/ocr/coverage.js +67 -0
- package/dist-lib/plugins/ocr/ocr-runtime.d.ts +4 -2
- package/dist-lib/plugins/ocr/ocr-runtime.js +25 -4
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
- package/dist-lib/schema/harness.js +4 -1
- package/dist-lib/spec/hrpc/messages.js +25 -1
- package/dist-lib/spec/hyperschema/index.js +25 -1
- package/dist-lib/spec/hyperschema/schema.json +6 -1
- package/dist-lib/spec/hyperschema/types.d.ts +1 -0
- package/package.json +2 -2
|
@@ -3,133 +3,16 @@ import { AbortedError, raceAbort } from '../../helpers/race-abort.js';
|
|
|
3
3
|
import { errorCode, errorMessage, isRecord } from '../../guards.js';
|
|
4
4
|
import { detachJson } from '../../helpers/detach-json.js';
|
|
5
5
|
import { mapStrings, restoreTemplateTokens } from '../../helpers/control-tokens.js';
|
|
6
|
-
import {
|
|
6
|
+
import { ToolCallMarkup } from '../../helpers/tool-call-markup.js';
|
|
7
7
|
import { INTENT_PARAM, takeToolIntent } from '../../helpers/tool-intent.js';
|
|
8
8
|
import { withHookContext } from '../tool-turns.js';
|
|
9
9
|
import { HarnessLog } from '../../log.js';
|
|
10
|
-
import { stableStringify } from '../../helpers/stable-stringify.js';
|
|
11
|
-
import { CompletionMachine } from '../../machine/index.js';
|
|
12
10
|
import { ToolPolicyChanged } from './operations.js';
|
|
11
|
+
import { preflightToolCall, ruleInputs } from './tool-preflight.js';
|
|
13
12
|
import { toolDeniedResult } from '../../helpers/failure-result.js';
|
|
14
13
|
function validationMessage(result) {
|
|
15
14
|
return isRecord(result) && 'error' in result ? result.error : result;
|
|
16
15
|
}
|
|
17
|
-
const RETRYABLE_STATUS = new Set([408, 429]);
|
|
18
|
-
const RETRYABLE_CODE = new Set(['timeout', 'transport', 'busy']);
|
|
19
|
-
export function callSucceeded(result, outcome) {
|
|
20
|
-
if (outcome.kind !== 'success') {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
const exitCode = result === null || result === undefined ? undefined : Reflect.get(Object(result), 'exitCode');
|
|
24
|
-
return typeof exitCode !== 'number' || exitCode === 0;
|
|
25
|
-
}
|
|
26
|
-
export function answerOf(result) {
|
|
27
|
-
if (!isRecord(result) || result.answer !== true || typeof result.content !== 'string') {
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
return result.content.trim() ? result.content : undefined;
|
|
31
|
-
}
|
|
32
|
-
function retryHints(result) {
|
|
33
|
-
if (!isRecord(result) || Array.isArray(result)) {
|
|
34
|
-
return {};
|
|
35
|
-
}
|
|
36
|
-
const hints = {};
|
|
37
|
-
if (typeof result.status === 'number') {
|
|
38
|
-
hints.status = result.status;
|
|
39
|
-
}
|
|
40
|
-
if (typeof result.code === 'string') {
|
|
41
|
-
hints.code = result.code;
|
|
42
|
-
}
|
|
43
|
-
return hints;
|
|
44
|
-
}
|
|
45
|
-
export function settledFailure(result, outcome) {
|
|
46
|
-
if (outcome.kind === 'denied' || callSucceeded(result, outcome)) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
if (outcome.kind === 'error' && outcome.error !== undefined) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
const { code, status } = retryHints(result);
|
|
53
|
-
if (code !== undefined && RETRYABLE_CODE.has(code)) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
return status === undefined || (status < 500 && !RETRYABLE_STATUS.has(status));
|
|
57
|
-
}
|
|
58
|
-
export function preflightToolCall(tool, args, ctx, options) {
|
|
59
|
-
const facts = inspectToolPolicy(tool, args, ctx, options);
|
|
60
|
-
if (facts.validationError) {
|
|
61
|
-
return {
|
|
62
|
-
status: 'blocked',
|
|
63
|
-
reason: 'validation',
|
|
64
|
-
result: { error: facts.validationError, guidance: true },
|
|
65
|
-
effect: 'write'
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
if (facts.refusal) {
|
|
69
|
-
return {
|
|
70
|
-
status: 'blocked',
|
|
71
|
-
reason: 'refusal',
|
|
72
|
-
result: { error: facts.refusal, guidance: true },
|
|
73
|
-
effect: facts.effect
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
if (facts.deniedKey) {
|
|
77
|
-
return {
|
|
78
|
-
status: 'denied',
|
|
79
|
-
result: toolDeniedResult(options.deniedMessage),
|
|
80
|
-
key: facts.deniedKey
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
status: 'ready',
|
|
85
|
-
key: facts.key,
|
|
86
|
-
effect: facts.effect,
|
|
87
|
-
resource: facts.resource,
|
|
88
|
-
autoRun: CompletionMachine.runsUnasked(facts),
|
|
89
|
-
permission: facts.permission
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
export function inspectToolPolicy(tool, args, ctx, options) {
|
|
93
|
-
const validationError = tool.validate?.(detachJson(args), ctx) || null;
|
|
94
|
-
const key = options.key ?? toolCallKey(tool.name, args);
|
|
95
|
-
const refusal = validationError
|
|
96
|
-
? null
|
|
97
|
-
: options.outbound.check(tool.name, args, {
|
|
98
|
-
sources: options.sources ?? [],
|
|
99
|
-
sourceFacts: options.sourceFacts,
|
|
100
|
-
externalWrite: tool.externalWrite === true
|
|
101
|
-
});
|
|
102
|
-
const deniedKey = !validationError && !refusal && options.denied?.has(key) ? key : null;
|
|
103
|
-
const effect = options.effect ?? (validationError ? 'write' : tool.effect(detachJson(args), ctx));
|
|
104
|
-
const resource = validationError || deniedKey ? null : detachJson(tool.resource?.(detachJson(args), ctx) ?? null);
|
|
105
|
-
const facts = {
|
|
106
|
-
key,
|
|
107
|
-
validationError,
|
|
108
|
-
refusal,
|
|
109
|
-
deniedKey,
|
|
110
|
-
effect,
|
|
111
|
-
autoApprove: tool.autoApprove === true,
|
|
112
|
-
skipApprovals: options.skipApprovals === true,
|
|
113
|
-
resource
|
|
114
|
-
};
|
|
115
|
-
if (options.permission) {
|
|
116
|
-
facts.permission = options.permission;
|
|
117
|
-
}
|
|
118
|
-
return facts;
|
|
119
|
-
}
|
|
120
|
-
export function readyPreflightFromFacts(facts) {
|
|
121
|
-
if (facts.validationError || facts.refusal || facts.deniedKey) {
|
|
122
|
-
throw new Error('tool policy facts are not ready for execution');
|
|
123
|
-
}
|
|
124
|
-
return {
|
|
125
|
-
status: 'ready',
|
|
126
|
-
key: facts.key,
|
|
127
|
-
effect: facts.effect,
|
|
128
|
-
resource: facts.resource,
|
|
129
|
-
autoRun: CompletionMachine.runsUnasked(facts),
|
|
130
|
-
permission: facts.permission
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
16
|
function decisionFor(options) {
|
|
134
17
|
if (options.permission !== undefined) {
|
|
135
18
|
return options.approved ? 'allowed' : 'denied';
|
|
@@ -271,10 +154,6 @@ export async function reportFailure(pipeline, failure) {
|
|
|
271
154
|
const context = await pipeline.toolFailure(failure);
|
|
272
155
|
return withHookContext(failure.result, context);
|
|
273
156
|
}
|
|
274
|
-
export function ruleInputs(tool, ctx) {
|
|
275
|
-
const { paths } = tool;
|
|
276
|
-
return { command: tool.command, paths: paths && ((args) => paths(args, ctx)) };
|
|
277
|
-
}
|
|
278
157
|
export async function resolveToolCalls(toolCalls, enabled, { runId, agentId, pipeline, skillNames, network, toolContext, signal }) {
|
|
279
158
|
const resolved = [];
|
|
280
159
|
for (const call of toolCalls) {
|
|
@@ -282,7 +161,7 @@ export async function resolveToolCalls(toolCalls, enabled, { runId, agentId, pip
|
|
|
282
161
|
throw new AbortedError();
|
|
283
162
|
}
|
|
284
163
|
const tool = enabled.find((t) => t.name === call.name);
|
|
285
|
-
const rawCall = call.raw === undefined ? {} : { rawCall: clipRawCall(call.raw) };
|
|
164
|
+
const rawCall = call.raw === undefined ? {} : { rawCall: ToolCallMarkup.clipRawCall(call.raw) };
|
|
286
165
|
const object = isRecord(call.arguments) && !Array.isArray(call.arguments);
|
|
287
166
|
if (!tool) {
|
|
288
167
|
const args = object ? takeToolIntent(call.arguments).args : {};
|
|
@@ -305,7 +184,7 @@ export async function resolveToolCalls(toolCalls, enabled, { runId, agentId, pip
|
|
|
305
184
|
? { args: call.arguments }
|
|
306
185
|
: takeToolIntent(call.arguments);
|
|
307
186
|
const intent = taken.intent ? { intent: taken.intent } : {};
|
|
308
|
-
const args = normalizeToolArgs(taken.args, Object.keys(tool.parameters.properties));
|
|
187
|
+
const args = ToolCallMarkup.normalizeToolArgs(taken.args, Object.keys(tool.parameters.properties));
|
|
309
188
|
const ctx = toolContext(tool.name);
|
|
310
189
|
const fold = await pipeline.runPreToolFold({
|
|
311
190
|
runId,
|
|
@@ -329,11 +208,3 @@ export async function resolveToolCalls(toolCalls, enabled, { runId, agentId, pip
|
|
|
329
208
|
}
|
|
330
209
|
return resolved;
|
|
331
210
|
}
|
|
332
|
-
function toolCallKey(name, args) {
|
|
333
|
-
try {
|
|
334
|
-
return `${name}:${stableStringify(args)}`;
|
|
335
|
-
}
|
|
336
|
-
catch {
|
|
337
|
-
return name;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ToolExecutionOutcome } from '../../contract/index.ts';
|
|
2
|
+
export declare function callSucceeded(result: unknown, outcome: ToolExecutionOutcome): boolean;
|
|
3
|
+
export declare function answerOf(result: unknown): string | undefined;
|
|
4
|
+
export declare function settledFailure(result: unknown, outcome: ToolExecutionOutcome): boolean;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isRecord } from '../../guards.js';
|
|
2
|
+
const RETRYABLE_STATUS = new Set([408, 429]);
|
|
3
|
+
const RETRYABLE_CODE = new Set(['timeout', 'transport', 'busy']);
|
|
4
|
+
export function callSucceeded(result, outcome) {
|
|
5
|
+
if (outcome.kind !== 'success') {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const exitCode = result === null || result === undefined ? undefined : Reflect.get(Object(result), 'exitCode');
|
|
9
|
+
return typeof exitCode !== 'number' || exitCode === 0;
|
|
10
|
+
}
|
|
11
|
+
export function answerOf(result) {
|
|
12
|
+
if (!isRecord(result) || result.answer !== true || typeof result.content !== 'string') {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
return result.content.trim() ? result.content : undefined;
|
|
16
|
+
}
|
|
17
|
+
function retryHints(result) {
|
|
18
|
+
if (!isRecord(result) || Array.isArray(result)) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
const hints = {};
|
|
22
|
+
if (typeof result.status === 'number') {
|
|
23
|
+
hints.status = result.status;
|
|
24
|
+
}
|
|
25
|
+
if (typeof result.code === 'string') {
|
|
26
|
+
hints.code = result.code;
|
|
27
|
+
}
|
|
28
|
+
return hints;
|
|
29
|
+
}
|
|
30
|
+
export function settledFailure(result, outcome) {
|
|
31
|
+
if (outcome.kind === 'denied' || callSucceeded(result, outcome)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if (outcome.kind === 'error' && outcome.error !== undefined) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const { code, status } = retryHints(result);
|
|
38
|
+
if (code !== undefined && RETRYABLE_CODE.has(code)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return status === undefined || (status < 500 && !RETRYABLE_STATUS.has(status));
|
|
42
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { OutboundGuard, PreToolCallPayload, ResourceDescriptor, ToolDefinition, ToolEffect, ToolExecuteContext, TrustedSource, TrustedSourceFacts } from '../../contract/index.ts';
|
|
2
|
+
import type { ToolPreflightFacts } from './stages.ts';
|
|
3
|
+
export type ToolPreflight = {
|
|
4
|
+
status: 'blocked';
|
|
5
|
+
reason: 'validation' | 'refusal';
|
|
6
|
+
result: unknown;
|
|
7
|
+
effect: ToolEffect;
|
|
8
|
+
} | {
|
|
9
|
+
status: 'denied';
|
|
10
|
+
result: {
|
|
11
|
+
error: string;
|
|
12
|
+
denied: true;
|
|
13
|
+
};
|
|
14
|
+
key: string;
|
|
15
|
+
} | {
|
|
16
|
+
status: 'ready';
|
|
17
|
+
key: string;
|
|
18
|
+
effect: ToolEffect;
|
|
19
|
+
resource: ResourceDescriptor | null;
|
|
20
|
+
autoRun: boolean;
|
|
21
|
+
permission?: 'allow' | 'ask' | undefined;
|
|
22
|
+
};
|
|
23
|
+
interface ToolPolicyOptions {
|
|
24
|
+
sources?: readonly TrustedSource[];
|
|
25
|
+
sourceFacts?: readonly TrustedSourceFacts[];
|
|
26
|
+
outbound: OutboundGuard;
|
|
27
|
+
denied?: Set<string>;
|
|
28
|
+
effect?: ToolEffect | undefined;
|
|
29
|
+
skipApprovals?: boolean;
|
|
30
|
+
key?: string;
|
|
31
|
+
permission?: 'allow' | 'ask' | undefined;
|
|
32
|
+
}
|
|
33
|
+
interface PreflightOptions extends ToolPolicyOptions {
|
|
34
|
+
deniedMessage: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function preflightToolCall(tool: ToolDefinition, args: Record<string, unknown>, ctx: ToolExecuteContext, options: PreflightOptions): ToolPreflight;
|
|
37
|
+
export declare function inspectToolPolicy(tool: ToolDefinition, args: Record<string, unknown>, ctx: ToolExecuteContext, options: ToolPolicyOptions): ToolPreflightFacts;
|
|
38
|
+
type ReadyToolPreflight = Extract<ToolPreflight, {
|
|
39
|
+
status: 'ready';
|
|
40
|
+
}>;
|
|
41
|
+
export declare function readyPreflightFromFacts(facts: ToolPreflightFacts): ReadyToolPreflight;
|
|
42
|
+
export declare function ruleInputs(tool: ToolDefinition, ctx: ToolExecuteContext | undefined): Pick<PreToolCallPayload, 'command' | 'paths'>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { detachJson } from '../../helpers/detach-json.js';
|
|
2
|
+
import { stableStringify } from '../../helpers/stable-stringify.js';
|
|
3
|
+
import { CompletionMachine } from '../../machine/index.js';
|
|
4
|
+
import { toolDeniedResult } from '../../helpers/failure-result.js';
|
|
5
|
+
export function preflightToolCall(tool, args, ctx, options) {
|
|
6
|
+
const facts = inspectToolPolicy(tool, args, ctx, options);
|
|
7
|
+
if (facts.validationError) {
|
|
8
|
+
return {
|
|
9
|
+
status: 'blocked',
|
|
10
|
+
reason: 'validation',
|
|
11
|
+
result: { error: facts.validationError, guidance: true },
|
|
12
|
+
effect: 'write'
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (facts.refusal) {
|
|
16
|
+
return {
|
|
17
|
+
status: 'blocked',
|
|
18
|
+
reason: 'refusal',
|
|
19
|
+
result: { error: facts.refusal, guidance: true },
|
|
20
|
+
effect: facts.effect
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (facts.deniedKey) {
|
|
24
|
+
return {
|
|
25
|
+
status: 'denied',
|
|
26
|
+
result: toolDeniedResult(options.deniedMessage),
|
|
27
|
+
key: facts.deniedKey
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
status: 'ready',
|
|
32
|
+
key: facts.key,
|
|
33
|
+
effect: facts.effect,
|
|
34
|
+
resource: facts.resource,
|
|
35
|
+
autoRun: CompletionMachine.runsUnasked(facts),
|
|
36
|
+
permission: facts.permission
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function inspectToolPolicy(tool, args, ctx, options) {
|
|
40
|
+
const validationError = tool.validate?.(detachJson(args), ctx) || null;
|
|
41
|
+
const key = options.key ?? toolCallKey(tool.name, args);
|
|
42
|
+
const refusal = validationError
|
|
43
|
+
? null
|
|
44
|
+
: options.outbound.check(tool.name, args, {
|
|
45
|
+
sources: options.sources ?? [],
|
|
46
|
+
sourceFacts: options.sourceFacts,
|
|
47
|
+
externalWrite: tool.externalWrite === true
|
|
48
|
+
});
|
|
49
|
+
const deniedKey = !validationError && !refusal && options.denied?.has(key) ? key : null;
|
|
50
|
+
const effect = options.effect ?? (validationError ? 'write' : tool.effect(detachJson(args), ctx));
|
|
51
|
+
const resource = validationError || deniedKey ? null : detachJson(tool.resource?.(detachJson(args), ctx) ?? null);
|
|
52
|
+
const facts = {
|
|
53
|
+
key,
|
|
54
|
+
validationError,
|
|
55
|
+
refusal,
|
|
56
|
+
deniedKey,
|
|
57
|
+
effect,
|
|
58
|
+
autoApprove: tool.autoApprove === true,
|
|
59
|
+
skipApprovals: options.skipApprovals === true,
|
|
60
|
+
resource
|
|
61
|
+
};
|
|
62
|
+
if (options.permission) {
|
|
63
|
+
facts.permission = options.permission;
|
|
64
|
+
}
|
|
65
|
+
return facts;
|
|
66
|
+
}
|
|
67
|
+
export function readyPreflightFromFacts(facts) {
|
|
68
|
+
if (facts.validationError || facts.refusal || facts.deniedKey) {
|
|
69
|
+
throw new Error('tool policy facts are not ready for execution');
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
status: 'ready',
|
|
73
|
+
key: facts.key,
|
|
74
|
+
effect: facts.effect,
|
|
75
|
+
resource: facts.resource,
|
|
76
|
+
autoRun: CompletionMachine.runsUnasked(facts),
|
|
77
|
+
permission: facts.permission
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export function ruleInputs(tool, ctx) {
|
|
81
|
+
const { paths } = tool;
|
|
82
|
+
return { command: tool.command, paths: paths && ((args) => paths(args, ctx)) };
|
|
83
|
+
}
|
|
84
|
+
function toolCallKey(name, args) {
|
|
85
|
+
try {
|
|
86
|
+
return `${name}:${stableStringify(args)}`;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return name;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { stripControlTokens } from '../../helpers/control-tokens.js';
|
|
2
2
|
import { isRecord } from '../../guards.js';
|
|
3
|
-
import {
|
|
3
|
+
import { ToolCallMarkup } from '../../helpers/tool-call-markup.js';
|
|
4
4
|
import { Agent } from '../../agent/index.js';
|
|
5
5
|
import { DIRECT_CALL_ID, PRELOAD_ID_PREFIX, skillLoadedTurn, skillMentionNudge, toolResultHistoryEntry, toolResultTurn } from '../tool-turns.js';
|
|
6
6
|
import { annotateUserTimes, currentTimeAnnotation } from '../../prompt.js';
|
|
@@ -26,7 +26,7 @@ export function sanitizeTurn(turn, answered = false) {
|
|
|
26
26
|
return turn;
|
|
27
27
|
}
|
|
28
28
|
const stripped = stripControlTokens(turn.content);
|
|
29
|
-
const content = turn.role === 'assistant' && !answered ? stripToolCallMarkup(stripped) : stripped;
|
|
29
|
+
const content = turn.role === 'assistant' && !answered ? ToolCallMarkup.stripToolCallMarkup(stripped) : stripped;
|
|
30
30
|
return content === turn.content ? turn : { ...turn, content };
|
|
31
31
|
}
|
|
32
32
|
export function modelTurns(messages, nudge) {
|
|
@@ -152,7 +152,10 @@ function explicitlyDisablesWebSearch(text) {
|
|
|
152
152
|
export function directToolHistory(history, { tool = Agent.WEB_SEARCH_TOOL_NAME, id = DIRECT_CALL_ID, args, output, resultRef, nudge }) {
|
|
153
153
|
return [
|
|
154
154
|
...history,
|
|
155
|
-
{
|
|
155
|
+
{
|
|
156
|
+
role: 'assistant',
|
|
157
|
+
content: ToolCallMarkup.toolCallMarkup([{ name: tool, arguments: args }])
|
|
158
|
+
},
|
|
156
159
|
toolResultHistoryEntry({
|
|
157
160
|
name: tool,
|
|
158
161
|
id,
|
|
@@ -166,7 +169,7 @@ export function assistantToolTurn(contentText, calls) {
|
|
|
166
169
|
if (!calls.length || contentText.includes('<function=')) {
|
|
167
170
|
return contentText;
|
|
168
171
|
}
|
|
169
|
-
const markup = toolCallMarkup(calls);
|
|
172
|
+
const markup = ToolCallMarkup.toolCallMarkup(calls);
|
|
170
173
|
return contentText ? `${contentText}\n\n${markup}` : markup;
|
|
171
174
|
}
|
|
172
175
|
export function withLastUserNote(messages, note) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function deliversFile(output: unknown): boolean;
|
|
2
|
+
export interface AttachedFile {
|
|
3
|
+
attachmentId: string;
|
|
4
|
+
fileName: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function attachedFiles(result: unknown): AttachedFile[];
|
|
7
|
+
export declare function attachmentTurn(name: string, result: unknown): string | null;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isRecord } from '../../guards.js';
|
|
2
|
+
import { DIRECT_CALL_ID, toolResultTurn } from './result-turn.js';
|
|
3
|
+
export function deliversFile(output) {
|
|
4
|
+
return isRecord(output) && Array.isArray(output.attachments) && output.attachments.length > 0;
|
|
5
|
+
}
|
|
6
|
+
function attachedFile(value) {
|
|
7
|
+
if (!isRecord(value) || typeof value.attachmentId !== 'string') {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const fileName = typeof value.fileName === 'string' ? value.fileName : '';
|
|
11
|
+
return { attachmentId: value.attachmentId, fileName };
|
|
12
|
+
}
|
|
13
|
+
export function attachedFiles(result) {
|
|
14
|
+
const direct = attachedFile(result);
|
|
15
|
+
if (direct) {
|
|
16
|
+
return [direct];
|
|
17
|
+
}
|
|
18
|
+
const listed = isRecord(result) ? result.attachments : undefined;
|
|
19
|
+
return Array.isArray(listed) ? listed.flatMap((entry) => attachedFile(entry) ?? []) : [];
|
|
20
|
+
}
|
|
21
|
+
function refFrom({ attachmentId, fileName }) {
|
|
22
|
+
const ref = { attachmentId };
|
|
23
|
+
const dot = fileName.lastIndexOf('.');
|
|
24
|
+
if (dot > 0) {
|
|
25
|
+
ref.type = fileName.slice(dot + 1).toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
return ref;
|
|
28
|
+
}
|
|
29
|
+
export function attachmentTurn(name, result) {
|
|
30
|
+
const direct = attachedFile(result);
|
|
31
|
+
if (direct) {
|
|
32
|
+
return toolResultTurn(name, DIRECT_CALL_ID, refFrom(direct));
|
|
33
|
+
}
|
|
34
|
+
const files = attachedFiles(result);
|
|
35
|
+
if (!files.length) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return toolResultTurn(name, DIRECT_CALL_ID, { attachments: files.map(refFrom) });
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const TOOL_CONTINUATION_NUDGE = "[Tool results above. Continue the user's request: call the next tool, or answer.]";
|
|
2
|
+
export declare const FILE_DELIVERED_NUDGE = "[Tool results above. The file is attached to the chat and shown to the user. Answer now, unless the request still needs a file that is not made yet.]";
|
|
3
|
+
export declare const WRAP_UP_NUDGE = "[Tool results above. No tool can be called for this reply. Answer the user's request now, in plain text.]";
|
|
4
|
+
export declare const TOOL_DENIED_MESSAGE: string;
|
|
5
|
+
export declare const TOOL_CUT_OFF_MESSAGE: string;
|
|
6
|
+
export declare const TOOL_ROUND_CUT_MESSAGE: string;
|
|
7
|
+
export declare const TOOL_CONTEXT_BOUNDARY_MESSAGE: string;
|
|
8
|
+
export declare const TOOL_REPEATED_MESSAGE: string;
|
|
9
|
+
export declare const TOOL_FAILING_MESSAGE: string;
|
|
10
|
+
export declare const TOOL_REPEATED_FAILURE_MESSAGE: string;
|
|
11
|
+
export declare const TOOL_UNCHANGED_MESSAGE: string;
|
|
12
|
+
export declare const GOAL_CONTINUATION_NUDGE: string;
|
|
13
|
+
export declare const GOAL_STALLED_NUDGE: string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const TOOL_CONTINUATION_NUDGE = "[Tool results above. Continue the user's request: call the next tool, or answer.]";
|
|
2
|
+
export const FILE_DELIVERED_NUDGE = '[Tool results above. The file is attached to the chat and shown to the user. Answer now, unless the request still needs a file that is not made yet.]';
|
|
3
|
+
export const WRAP_UP_NUDGE = "[Tool results above. No tool can be called for this reply. Answer the user's request now, in plain text.]";
|
|
4
|
+
export const TOOL_DENIED_MESSAGE = 'The user declined this call. Do not retry it or call it with different arguments to get ' +
|
|
5
|
+
'around the refusal. Tell the user plainly what you could not do, and never present a ' +
|
|
6
|
+
'result you did not obtain.';
|
|
7
|
+
export const TOOL_CUT_OFF_MESSAGE = 'This tool call was not executed: the response hit the output token limit, so its ' +
|
|
8
|
+
'arguments may be truncated. Re-issue the tool call with complete arguments, and keep ' +
|
|
9
|
+
'them short enough to finish.';
|
|
10
|
+
export const TOOL_ROUND_CUT_MESSAGE = 'This tool call was not executed: the round was stopped before it finished, so its ' +
|
|
11
|
+
'arguments may be truncated. Re-issue the call with complete arguments if it is still needed.';
|
|
12
|
+
export const TOOL_CONTEXT_BOUNDARY_MESSAGE = 'This tool call was not executed: the context window filled before its arguments were ' +
|
|
13
|
+
'complete, so they may be truncated. Re-issue the call with complete arguments, and keep ' +
|
|
14
|
+
'them short.';
|
|
15
|
+
export const TOOL_REPEATED_MESSAGE = 'You sent the same tool call more than once in one response. It ran once, and its result ' +
|
|
16
|
+
'is below. Send each call once.';
|
|
17
|
+
export const TOOL_FAILING_MESSAGE = 'This call has failed {count} times in a row. Before changing anything again, step back: ' +
|
|
18
|
+
're-read the failing output and the requirements, work out what the failures have in common, ' +
|
|
19
|
+
'and change your approach instead of patching the last error.';
|
|
20
|
+
export const TOOL_REPEATED_FAILURE_MESSAGE = 'You already made this exact call and it failed as shown; it was not run again. Do what the ' +
|
|
21
|
+
'error says: change the arguments, use another tool, or answer the user.';
|
|
22
|
+
export const TOOL_UNCHANGED_MESSAGE = 'This call returned the same result the last {count} times it ran. Running it again will ' +
|
|
23
|
+
'not change it: use the result you have, try something else, or answer.';
|
|
24
|
+
export const GOAL_CONTINUATION_NUDGE = '[The goal is not met yet. Keep working on it: call the next tool. Track the work with ' +
|
|
25
|
+
'update_plan. With no plan yet, write one first: one step per requirement the goal states, ' +
|
|
26
|
+
'the last step a check of the whole result with a tool. Mark a step done only once a tool ' +
|
|
27
|
+
'result shows it is done; the goal is met when every step is done.]';
|
|
28
|
+
export const GOAL_STALLED_NUDGE = '[Stalled: {call} kept returning the same result, and another identical run will not change ' +
|
|
29
|
+
'it. Step back: re-read the goal and the results you have, then try a different approach.]';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CompletionHistoryEntry, Message, SdkMessage } from '../../contract/index.ts';
|
|
2
|
+
import { type FailureResultValue } from '../../helpers/failure-result.ts';
|
|
3
|
+
export declare const DIRECT_CALL_ID = "call-direct";
|
|
4
|
+
export declare const TOOL_RESULT_MAX_CHARS = 65536;
|
|
5
|
+
export declare function withHookContext(output: unknown, context: string | undefined): unknown;
|
|
6
|
+
export declare function toolResultTurn(name: string, id: string, output: unknown): string;
|
|
7
|
+
interface ToolResultHistoryInput {
|
|
8
|
+
name: string;
|
|
9
|
+
id: string;
|
|
10
|
+
output: unknown;
|
|
11
|
+
resultRef?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function toolResultHistoryEntry({ name, id, output, resultRef }: ToolResultHistoryInput): CompletionHistoryEntry;
|
|
14
|
+
export declare function materializeCompletionHistory(history: readonly CompletionHistoryEntry[], values: Readonly<Record<string, FailureResultValue>>): Message[];
|
|
15
|
+
export interface FailureTurn extends FailureResultValue {
|
|
16
|
+
at: number;
|
|
17
|
+
callId: string;
|
|
18
|
+
compactedTo?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function redactFailureTurns(history: readonly SdkMessage[], failures: readonly FailureTurn[], redact: <Value>(value: Value) => Value): SdkMessage[];
|
|
21
|
+
export declare function historyPayload(output: unknown): unknown;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { isRecord } from '../../guards.js';
|
|
2
|
+
import { Agent } from '../../agent/index.js';
|
|
3
|
+
import { headTail } from '../../helpers/head-tail.js';
|
|
4
|
+
import { ToolCallMarkup } from '../../helpers/tool-call-markup.js';
|
|
5
|
+
import { ContextWindow } from '../../context-window.js';
|
|
6
|
+
import { defuseTemplateTokens } from '../../helpers/control-tokens.js';
|
|
7
|
+
import { TOOL_SEARCH_TOOL_NAME } from '../sdk-tool.js';
|
|
8
|
+
import { isFailureResultReference, readFailureResult } from '../../helpers/failure-result.js';
|
|
9
|
+
import { TOOL_FAILED_NOTE } from '../tool-failed-note.js';
|
|
10
|
+
export const DIRECT_CALL_ID = 'call-direct';
|
|
11
|
+
export const TOOL_RESULT_MAX_CHARS = 65_536;
|
|
12
|
+
const MIN_LEAF_CHARS = 256;
|
|
13
|
+
const HOOK_CONTEXT_KEY = 'hookContext';
|
|
14
|
+
export function withHookContext(output, context) {
|
|
15
|
+
if (context === undefined) {
|
|
16
|
+
return output;
|
|
17
|
+
}
|
|
18
|
+
const base = isRecord(output) && !Array.isArray(output) ? output : { result: output };
|
|
19
|
+
const prior = base[HOOK_CONTEXT_KEY];
|
|
20
|
+
return {
|
|
21
|
+
...base,
|
|
22
|
+
[HOOK_CONTEXT_KEY]: typeof prior === 'string' ? `${prior}\n${context}` : context
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function toolResultTurn(name, id, output) {
|
|
26
|
+
let context = '';
|
|
27
|
+
let shown = output;
|
|
28
|
+
if (isRecord(output) && typeof output[HOOK_CONTEXT_KEY] === 'string') {
|
|
29
|
+
const { [HOOK_CONTEXT_KEY]: added, ...rest } = output;
|
|
30
|
+
context = `\n${String(added)}`;
|
|
31
|
+
shown = rest;
|
|
32
|
+
}
|
|
33
|
+
const payload = historyPayload(shown);
|
|
34
|
+
if (name === TOOL_SEARCH_TOOL_NAME) {
|
|
35
|
+
return JSON.stringify(payload);
|
|
36
|
+
}
|
|
37
|
+
if (name === Agent.SKILL_TOOL_NAME && isRecord(shown) && typeof shown.content === 'string') {
|
|
38
|
+
const { content, ...rest } = shown;
|
|
39
|
+
return `[Tool: ${name} (${id})]\n${JSON.stringify(historyPayload(rest))}\n\n${content}${context}`;
|
|
40
|
+
}
|
|
41
|
+
const text = name === Agent.SKILL_TOOL_NAME ? JSON.stringify(payload) : fitted(payload);
|
|
42
|
+
const failed = id !== ToolCallMarkup.MALFORMED_CALL_ID &&
|
|
43
|
+
isRecord(shown) &&
|
|
44
|
+
'error' in shown &&
|
|
45
|
+
shown.guidance !== true
|
|
46
|
+
? `\n${TOOL_FAILED_NOTE}`
|
|
47
|
+
: '';
|
|
48
|
+
return `[Tool: ${name} (${id})]\n${text}${context}${failed}`;
|
|
49
|
+
}
|
|
50
|
+
function fitted(payload) {
|
|
51
|
+
const whole = JSON.stringify(payload);
|
|
52
|
+
if (whole === undefined || whole.length <= TOOL_RESULT_MAX_CHARS) {
|
|
53
|
+
return whole;
|
|
54
|
+
}
|
|
55
|
+
const render = (cap) => JSON.stringify(payload, (_key, value) => typeof value === 'string' && value.length > cap ? headTail(value, cap) : value);
|
|
56
|
+
const floor = render(MIN_LEAF_CHARS);
|
|
57
|
+
if (floor.length > TOOL_RESULT_MAX_CHARS) {
|
|
58
|
+
return floor;
|
|
59
|
+
}
|
|
60
|
+
let fits = MIN_LEAF_CHARS;
|
|
61
|
+
let over = whole.length;
|
|
62
|
+
while (over - fits > 1) {
|
|
63
|
+
const mid = Math.floor((fits + over) / 2);
|
|
64
|
+
if (render(mid).length <= TOOL_RESULT_MAX_CHARS) {
|
|
65
|
+
fits = mid;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
over = mid;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return render(fits);
|
|
72
|
+
}
|
|
73
|
+
export function toolResultHistoryEntry({ name, id, output, resultRef }) {
|
|
74
|
+
return resultRef
|
|
75
|
+
? {
|
|
76
|
+
type: 'qvac-failure-result-ref',
|
|
77
|
+
ref: resultRef,
|
|
78
|
+
callId: id
|
|
79
|
+
}
|
|
80
|
+
: { role: 'tool', content: toolResultTurn(name, id, output) };
|
|
81
|
+
}
|
|
82
|
+
export function materializeCompletionHistory(history, values) {
|
|
83
|
+
return history.map((entry) => {
|
|
84
|
+
if (!isFailureResultReference(entry)) {
|
|
85
|
+
return entry;
|
|
86
|
+
}
|
|
87
|
+
const stored = values[entry.ref];
|
|
88
|
+
if (!stored) {
|
|
89
|
+
throw new Error(`History references missing failed tool result: ${entry.ref}`);
|
|
90
|
+
}
|
|
91
|
+
const content = toolResultTurn(stored.name, entry.callId, readFailureResult(stored.result));
|
|
92
|
+
return {
|
|
93
|
+
role: 'tool',
|
|
94
|
+
content: entry.compactedTo === undefined
|
|
95
|
+
? content
|
|
96
|
+
: ContextWindow.compactContent(content, entry.compactedTo)
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
export function redactFailureTurns(history, failures, redact) {
|
|
101
|
+
const out = [...history];
|
|
102
|
+
for (const { at, name, callId, result, compactedTo } of redact(failures)) {
|
|
103
|
+
const content = toolResultTurn(name, callId, readFailureResult(result));
|
|
104
|
+
const sent = compactedTo === undefined ? content : ContextWindow.compactContent(content, compactedTo);
|
|
105
|
+
out[at] = { role: 'tool', content: defuseTemplateTokens(sent) };
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
const HOST_ONLY_KEYS = ['data', 'hostNotice', 'mention'];
|
|
110
|
+
export function historyPayload(output) {
|
|
111
|
+
if (!output || typeof output !== 'object')
|
|
112
|
+
return output;
|
|
113
|
+
if (!HOST_ONLY_KEYS.some((key) => key in output))
|
|
114
|
+
return output;
|
|
115
|
+
const rest = { ...output };
|
|
116
|
+
for (const key of HOST_ONLY_KEYS) {
|
|
117
|
+
delete rest[key];
|
|
118
|
+
}
|
|
119
|
+
return rest;
|
|
120
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { RunGoal } from '../../contract/index.ts';
|
|
2
|
+
export declare const MAX_TOOL_ROUNDS = 10;
|
|
3
|
+
export declare const GOAL_ROUNDS = 40;
|
|
4
|
+
export declare const FAILURE_STREAK = 3;
|
|
5
|
+
export declare function checkMaxToolRounds(rounds: number): number;
|
|
6
|
+
export declare function checkGoal(goal: RunGoal, responseFormat?: unknown): RunGoal;
|