@noetic-tools/core 5.19.1-next.1 → 5.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/openrouter.js +5 -0
- package/dist/builders/control-flow-builders.d.ts +15 -6
- package/dist/builders/control-flow-builders.js +8 -0
- package/dist/builders/decide-builders.d.ts +2 -5
- package/dist/builders/dynamic-workflow.d.ts +16 -1
- package/dist/builders/dynamic-workflow.js +222 -11
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- package/dist/builders/provide-builder.d.ts +3 -2
- package/dist/builders/provide-builder.js +2 -1
- package/dist/builders/sequence.d.ts +14 -0
- package/dist/builders/sequence.js +103 -0
- package/dist/builders/spawn-builder.d.ts +3 -2
- package/dist/builders/spawn-builder.js +3 -2
- package/dist/builders/step-builders.d.ts +80 -1
- package/dist/builders/step-builders.js +115 -1
- package/dist/builders/verify-builder.d.ts +79 -0
- package/dist/builders/verify-builder.js +69 -0
- package/dist/builders/workflow-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +335 -0
- package/dist/builders/workflow-connection-nodes.d.ts +17 -0
- package/dist/builders/workflow-connection-nodes.js +160 -0
- package/dist/builders/workflow-connections.d.ts +90 -0
- package/dist/builders/workflow-connections.js +560 -0
- package/dist/builders/workflow-control-nodes.d.ts +13 -0
- package/dist/builders/workflow-control-nodes.js +213 -0
- package/dist/builders/workflow-hydrator.d.ts +44 -3
- package/dist/builders/workflow-hydrator.js +53 -495
- package/dist/builders/workflow-verify.d.ts +19 -0
- package/dist/builders/workflow-verify.js +194 -0
- package/dist/conditions/conditions.d.ts +7 -5
- package/dist/conditions/conditions.js +7 -4
- package/dist/connections/blobs.d.ts +33 -0
- package/dist/connections/blobs.js +77 -0
- package/dist/connections/cron-schedule.d.ts +32 -0
- package/dist/connections/cron-schedule.js +213 -0
- package/dist/connections/durability.d.ts +65 -0
- package/dist/connections/durability.js +54 -0
- package/dist/connections/errors.d.ts +30 -0
- package/dist/connections/errors.js +100 -0
- package/dist/connections/event-stream.d.ts +24 -0
- package/dist/connections/event-stream.js +176 -0
- package/dist/connections/gates.d.ts +94 -0
- package/dist/connections/gates.js +271 -0
- package/dist/connections/glob.d.ts +10 -0
- package/dist/connections/glob.js +46 -0
- package/dist/connections/handle.d.ts +22 -0
- package/dist/connections/handle.js +84 -0
- package/dist/connections/harness-wiring.d.ts +52 -0
- package/dist/connections/harness-wiring.js +143 -0
- package/dist/connections/in-memory-connection.d.ts +147 -0
- package/dist/connections/in-memory-connection.js +593 -0
- package/dist/connections/index.d.ts +34 -0
- package/dist/connections/index.js +23 -0
- package/dist/connections/json-schema.d.ts +28 -0
- package/dist/connections/json-schema.js +338 -0
- package/dist/connections/model-tools.d.ts +46 -0
- package/dist/connections/model-tools.js +466 -0
- package/dist/connections/negotiation.d.ts +30 -0
- package/dist/connections/negotiation.js +159 -0
- package/dist/connections/policy.d.ts +21 -0
- package/dist/connections/policy.js +34 -0
- package/dist/connections/pool.d.ts +111 -0
- package/dist/connections/pool.js +339 -0
- package/dist/connections/refs.d.ts +74 -0
- package/dist/connections/refs.js +316 -0
- package/dist/connections/registry-types.d.ts +141 -0
- package/dist/connections/registry-types.js +14 -0
- package/dist/connections/registry.d.ts +125 -0
- package/dist/connections/registry.js +775 -0
- package/dist/connections/secrets.d.ts +59 -0
- package/dist/connections/secrets.js +153 -0
- package/dist/connections/settle.d.ts +9 -0
- package/dist/connections/settle.js +26 -0
- package/dist/connections/sources.d.ts +27 -0
- package/dist/connections/sources.js +300 -0
- package/dist/connections/triggers.d.ts +78 -0
- package/dist/connections/triggers.js +310 -0
- package/dist/connections/values.d.ts +26 -0
- package/dist/connections/values.js +93 -0
- package/dist/connections/views.d.ts +31 -0
- package/dist/connections/views.js +176 -0
- package/dist/expression/check.js +80 -1
- package/dist/expression/jsonata.d.ts +14 -4
- package/dist/expression/jsonata.js +27 -9
- package/dist/expression/step-outputs.d.ts +25 -0
- package/dist/expression/step-outputs.js +78 -0
- package/dist/harness/agent-harness.d.ts +69 -166
- package/dist/harness/agent-harness.js +170 -262
- package/dist/harness/call-span-collector.d.ts +33 -0
- package/dist/harness/call-span-collector.js +83 -0
- package/dist/harness/call-spend.d.ts +2 -0
- package/dist/harness/call-spend.js +10 -14
- package/dist/harness/connection-gate.d.ts +78 -0
- package/dist/harness/connection-gate.js +169 -0
- package/dist/harness/deps/interpreter.d.ts +1 -0
- package/dist/harness/deps/interpreter.js +1 -0
- package/dist/harness/deps/runtime.d.ts +1 -1
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/harness-options.d.ts +153 -0
- package/dist/harness/harness-options.js +36 -0
- package/dist/harness/harness-tracing.d.ts +16 -0
- package/dist/harness/harness-tracing.js +35 -0
- package/dist/harness/llm-client.d.ts +71 -0
- package/dist/harness/llm-client.js +218 -0
- package/dist/harness/model-call.d.ts +2 -7
- package/dist/harness/model-call.js +141 -558
- package/dist/harness/model-request.d.ts +42 -0
- package/dist/harness/model-request.js +159 -0
- package/dist/harness/model-rounds.d.ts +18 -2
- package/dist/harness/model-rounds.js +39 -8
- package/dist/harness/model-stream.d.ts +39 -0
- package/dist/harness/model-stream.js +131 -0
- package/dist/harness/model-tool-round.d.ts +37 -0
- package/dist/harness/model-tool-round.js +197 -0
- package/dist/harness/spend.d.ts +2 -0
- package/dist/harness/spend.js +5 -1
- package/dist/index.d.ts +31 -6
- package/dist/index.js +18 -1
- package/dist/interpreter/connection-steps.d.ts +194 -0
- package/dist/interpreter/connection-steps.js +537 -0
- package/dist/interpreter/execute-action.d.ts +2 -8
- package/dist/interpreter/execute-action.js +3 -178
- package/dist/interpreter/execute-call.d.ts +19 -0
- package/dist/interpreter/execute-call.js +99 -0
- package/dist/interpreter/execute-control.d.ts +6 -1
- package/dist/interpreter/execute-control.js +14 -0
- package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
- package/dist/interpreter/execute-invoke-tool.js +183 -0
- package/dist/interpreter/execute-verify.d.ts +21 -0
- package/dist/interpreter/execute-verify.js +391 -0
- package/dist/interpreter/execute-wait.d.ts +21 -0
- package/dist/interpreter/execute-wait.js +233 -0
- package/dist/interpreter/execute.js +24 -133
- package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
- package/dist/interpreter/step-kind-dispatch.js +157 -0
- package/dist/interpreter/step-walk.d.ts +2 -0
- package/dist/interpreter/step-walk.js +24 -0
- package/dist/interpreter/verify-host.d.ts +40 -0
- package/dist/interpreter/verify-host.js +245 -0
- package/dist/runtime/durable/step-ledger.d.ts +34 -2
- package/dist/runtime/durable/step-ledger.js +97 -3
- package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
- package/dist/runtime/in-memory-artifact-store.js +240 -0
- package/dist/schemas/workflow.d.ts +2 -2
- package/dist/schemas/workflow.js +1 -1
- package/dist/util/harness-internals.d.ts +23 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +1170 -92
|
@@ -1,106 +1,29 @@
|
|
|
1
|
-
import { frameworkCast, NoeticConfigError, SpendKind, SteerPoint
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { isFunctionCall } from '../interpreter/typeguards.js';
|
|
5
|
-
import { GenAI, NoeticAttr, ToolAttr } from '../observability/genai-attributes.js';
|
|
6
|
-
import { emitFrameworkEvent, getBroadcaster, shouldEmit } from '../runtime/broadcaster-utils.js';
|
|
1
|
+
import { frameworkCast, NoeticConfigError, SpendKind, SteerPoint } from '@noetic-tools/types';
|
|
2
|
+
import { extractOutputItems, extractUsage, itemsToInput, toolsOnWire, } from '../adapters/openrouter.js';
|
|
3
|
+
import { emitFrameworkEvent, getBroadcaster } from '../runtime/broadcaster-utils.js';
|
|
7
4
|
import { drainsOf } from '../runtime/drain.js';
|
|
8
5
|
import { markSpent } from '../util/spent.js';
|
|
9
|
-
import {
|
|
10
|
-
import { CallSpend } from './call-spend.js';
|
|
11
|
-
import {
|
|
6
|
+
import { CallSpanCollector } from './call-span-collector.js';
|
|
7
|
+
import { CallSpend, noBill } from './call-spend.js';
|
|
8
|
+
import { buildTextFormat, prepareModelRequest } from './model-request.js';
|
|
12
9
|
import { RoundLedger } from './model-rounds.js';
|
|
13
|
-
import { buildItemSchemaRegistry
|
|
10
|
+
import { buildItemSchemaRegistry } from './model-schema.js';
|
|
11
|
+
import { awaitWithAbort, hasUsableResponseOutput, noteStreamGeneration, pipeStreamEventsToBroadcaster, providerTerminalError, } from './model-stream.js';
|
|
12
|
+
import { hasFunctionCalls, replayOutput, runToolRound } from './model-tool-round.js';
|
|
14
13
|
import { openSteerInbox } from './steer-inbox.js';
|
|
15
14
|
const MAX_TOOL_ROUNDS = 32;
|
|
16
15
|
const MAX_RECOVERY_CONTINUATIONS = 3;
|
|
17
16
|
const EPHEMERAL_CONTINUE_INPUT = 'continue';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
25
|
-
return {
|
|
26
|
-
format: {
|
|
27
|
-
type: 'json_schema',
|
|
28
|
-
name: 'output',
|
|
29
|
-
schema: jsonSchema,
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function isStreamRecord(value) {
|
|
34
|
-
return typeof value === 'object' && value !== null;
|
|
35
|
-
}
|
|
36
|
-
function providerTerminalError(response) {
|
|
37
|
-
if (!isStreamRecord(response) || response.status === undefined) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
const status = String(response.status);
|
|
41
|
-
if (status === 'completed') {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
if (status === 'incomplete') {
|
|
45
|
-
const details = response.incompleteDetails;
|
|
46
|
-
const reason = isStreamRecord(details) && typeof details.reason === 'string' ? `: ${details.reason}` : '';
|
|
47
|
-
return {
|
|
48
|
-
status,
|
|
49
|
-
message: `LLM response incomplete${reason}`,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
if (status === 'failed') {
|
|
53
|
-
const error = response.error;
|
|
54
|
-
const message = isStreamRecord(error) && typeof error.message === 'string' ? `: ${error.message}` : '';
|
|
55
|
-
return {
|
|
56
|
-
status,
|
|
57
|
-
message: `LLM response failed${message}`,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
status,
|
|
62
|
-
message: `LLM response ended with status '${status}'`,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Tool rounds replay only their function calls. A round a steer follows must
|
|
67
|
-
* also replay what the model said, or the next round starts without it.
|
|
68
|
-
* Function calls are left to the tool round, which adds each with its output.
|
|
69
|
-
*/
|
|
70
|
-
function replayOutput(input, roundItems) {
|
|
71
|
-
input.push(...itemsToInput(roundItems.filter((item) => !isFunctionCall(item))));
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Save the generation id a stream's `response.created` event carries, for a response
|
|
75
|
-
* whose headers did not. OpenRouter's ids start `gen-`; other ids price nothing.
|
|
76
|
-
*/
|
|
77
|
-
function noteStreamGeneration(dispatch, event) {
|
|
78
|
-
if (!dispatch || !isStreamRecord(event) || event.type !== 'response.created') {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const response = event.response;
|
|
82
|
-
if (!isStreamRecord(response) || typeof response.id !== 'string') {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (response.id.startsWith('gen-')) {
|
|
86
|
-
void dispatch.noteGeneration(response.id);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
/** The provider refused the call before generating anything, so nothing was billed. */
|
|
90
|
-
function wasRefused(err, dispatch) {
|
|
91
|
-
return isErrorStatus(err) && dispatch.record.generationId === undefined;
|
|
17
|
+
function itemIds(items) {
|
|
18
|
+
return items.flatMap((item) => 'id' in item && typeof item.id === 'string'
|
|
19
|
+
? [
|
|
20
|
+
item.id,
|
|
21
|
+
]
|
|
22
|
+
: []);
|
|
92
23
|
}
|
|
93
24
|
function hasItem(items, id) {
|
|
94
25
|
return items.some((item) => 'id' in item && item.id === id);
|
|
95
26
|
}
|
|
96
|
-
function hasUsableResponseOutput(response, items) {
|
|
97
|
-
if (items.length > 0) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
return (isStreamRecord(response) &&
|
|
101
|
-
typeof response.outputText === 'string' &&
|
|
102
|
-
response.outputText.length > 0);
|
|
103
|
-
}
|
|
104
27
|
function withEphemeralContinueInput(input) {
|
|
105
28
|
return [
|
|
106
29
|
...frameworkCast(input),
|
|
@@ -111,74 +34,6 @@ function withEphemeralContinueInput(input) {
|
|
|
111
34
|
}),
|
|
112
35
|
];
|
|
113
36
|
}
|
|
114
|
-
/** Race a promise against an AbortSignal so callers (e.g. `SessionRunner.abort`)
|
|
115
|
-
* can break out of a long `await` without waiting for the underlying call to
|
|
116
|
-
* settle. When the signal fires, the returned promise rejects with
|
|
117
|
-
* `signal.reason` (an `Error`) or a generic `Error('aborted')` fallback. */
|
|
118
|
-
function awaitWithAbort(p, signal) {
|
|
119
|
-
if (!signal) {
|
|
120
|
-
return p;
|
|
121
|
-
}
|
|
122
|
-
if (signal.aborted) {
|
|
123
|
-
return Promise.reject(signal.reason instanceof Error ? signal.reason : new Error('aborted'));
|
|
124
|
-
}
|
|
125
|
-
return new Promise((resolve, reject) => {
|
|
126
|
-
const onAbort = () => {
|
|
127
|
-
reject(signal.reason instanceof Error ? signal.reason : new Error('aborted'));
|
|
128
|
-
};
|
|
129
|
-
signal.addEventListener('abort', onAbort, {
|
|
130
|
-
once: true,
|
|
131
|
-
});
|
|
132
|
-
p.then((v) => {
|
|
133
|
-
signal.removeEventListener('abort', onAbort);
|
|
134
|
-
resolve(v);
|
|
135
|
-
}, (e) => {
|
|
136
|
-
signal.removeEventListener('abort', onAbort);
|
|
137
|
-
reject(e);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
async function pipeStreamEventsToBroadcaster(opts) {
|
|
142
|
-
const { stream, broadcaster, agentName, signal, onEvent, onItemDone } = opts;
|
|
143
|
-
try {
|
|
144
|
-
for await (const event of stream) {
|
|
145
|
-
if (signal?.aborted) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
onEvent?.(event);
|
|
149
|
-
if (!isStreamRecord(event)) {
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
broadcaster?.emit({
|
|
153
|
-
source: 'sdk',
|
|
154
|
-
type: typeof event.type === 'string' ? event.type : 'unknown',
|
|
155
|
-
data: event,
|
|
156
|
-
outputIndex: typeof event.outputIndex === 'number' ? event.outputIndex : undefined,
|
|
157
|
-
contentIndex: typeof event.contentIndex === 'number' ? event.contentIndex : undefined,
|
|
158
|
-
});
|
|
159
|
-
if (event.type === 'response.output_item.done' && onItemDone) {
|
|
160
|
-
await onItemDone(event.item);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
catch (err) {
|
|
165
|
-
// The stream ends badly when the round is aborted; that is not a pipe fault.
|
|
166
|
-
if (signal?.aborted) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
if (broadcaster) {
|
|
170
|
-
emitFrameworkEvent({
|
|
171
|
-
broadcaster,
|
|
172
|
-
agentName,
|
|
173
|
-
eventType: 'stream_pipe_error',
|
|
174
|
-
data: {
|
|
175
|
-
error: err instanceof Error ? err.message : String(err),
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
throw err;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
37
|
/** @internal The error for a model call on a harness with no LLM credentials. */
|
|
183
38
|
export function noLlmProviderError() {
|
|
184
39
|
return new NoeticConfigError({
|
|
@@ -230,217 +85,6 @@ export function createStreamIdleWatchdog(timeoutMs, controller, onTimeout) {
|
|
|
230
85
|
},
|
|
231
86
|
};
|
|
232
87
|
}
|
|
233
|
-
function prepareModelRequest(request, agentName) {
|
|
234
|
-
const { instructions: extractedInstructions, remaining } = extractSystemInstruction(request.items);
|
|
235
|
-
const instructions = [
|
|
236
|
-
request.instructions,
|
|
237
|
-
extractedInstructions,
|
|
238
|
-
]
|
|
239
|
-
.filter(Boolean)
|
|
240
|
-
.join('\n\n') || undefined;
|
|
241
|
-
const broadcaster = getBroadcaster(request.ctx);
|
|
242
|
-
const allowedTools = filterAllowedTools(request) ?? [];
|
|
243
|
-
// Every request carries the full tool list, so the prompt-cache prefix is the
|
|
244
|
-
// same for every step in the tree; `toolChoice` does the narrowing. A call
|
|
245
|
-
// that may use no client tool sends none.
|
|
246
|
-
// Client (function) tools convert to SDK tool definitions; OpenRouter server
|
|
247
|
-
// tools (web_search/web_fetch) are wrapped via the SDK's `serverTool()` so
|
|
248
|
-
// its convertToolsToAPIFormat accepts them — a raw `{type}` entry crashes it.
|
|
249
|
-
// `parameters` keys stay camelCase: the SDK's outbound zod schema strips
|
|
250
|
-
// unknown keys, so snake_case would be silently dropped.
|
|
251
|
-
const clientSdkTools = request.tools && allowedTools.length > 0
|
|
252
|
-
? convertTools({
|
|
253
|
-
tools: request.tools,
|
|
254
|
-
})
|
|
255
|
-
: [];
|
|
256
|
-
const serverSdkTools = (request._serverTools ?? []).map((s) => serverTool(frameworkCast({
|
|
257
|
-
type: s.type,
|
|
258
|
-
parameters: s.parameters,
|
|
259
|
-
})));
|
|
260
|
-
const sdkTools = [
|
|
261
|
-
...clientSdkTools,
|
|
262
|
-
...frameworkCast(serverSdkTools),
|
|
263
|
-
];
|
|
264
|
-
return {
|
|
265
|
-
instructions,
|
|
266
|
-
remaining,
|
|
267
|
-
broadcaster,
|
|
268
|
-
sdkTools: sdkTools.length > 0 ? sdkTools : undefined,
|
|
269
|
-
toolChoice: roundToolChoices({
|
|
270
|
-
request,
|
|
271
|
-
allowedTools,
|
|
272
|
-
sendsClientTools: clientSdkTools.length > 0,
|
|
273
|
-
sendsTools: sdkTools.length > 0,
|
|
274
|
-
}),
|
|
275
|
-
allowedTools,
|
|
276
|
-
emitIfAllowed(eventType, data) {
|
|
277
|
-
if (!shouldEmit(request.emit, eventType, data)) {
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
emitFrameworkEvent({
|
|
281
|
-
broadcaster,
|
|
282
|
-
agentName,
|
|
283
|
-
eventType,
|
|
284
|
-
data,
|
|
285
|
-
});
|
|
286
|
-
},
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
function roundToolChoices(scope) {
|
|
290
|
-
const choice = scope.request.toolChoice ?? ToolChoiceMode.Auto;
|
|
291
|
-
return {
|
|
292
|
-
first: wireToolChoice(scope, choice),
|
|
293
|
-
rest: wireToolChoice(scope, choice === ToolChoiceMode.None ? choice : ToolChoiceMode.Auto),
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Map a `toolChoice` onto the wire, inside the step's restriction.
|
|
298
|
-
* A restricted call narrows with an `allowed_tools` choice (plus the step's
|
|
299
|
-
* server tools) rather than by trimming `tools`, so the request keeps the
|
|
300
|
-
* shared cache prefix. `none` keeps the tools too, for the same reason. A call
|
|
301
|
-
* that sends no tools sends no choice.
|
|
302
|
-
*
|
|
303
|
-
* @throws NoeticConfigError `UNKNOWN_TOOL_CHOICE` when `{ tool }` names a tool
|
|
304
|
-
* this call cannot run.
|
|
305
|
-
*/
|
|
306
|
-
function wireToolChoice(scope, choice) {
|
|
307
|
-
const { request, allowedTools } = scope;
|
|
308
|
-
if (typeof choice === 'object') {
|
|
309
|
-
return forcedToolChoice(choice.tool, allowedTools);
|
|
310
|
-
}
|
|
311
|
-
if (!scope.sendsTools) {
|
|
312
|
-
return undefined;
|
|
313
|
-
}
|
|
314
|
-
if (choice === ToolChoiceMode.None) {
|
|
315
|
-
return 'none';
|
|
316
|
-
}
|
|
317
|
-
if (request.allowedToolNames && scope.sendsClientTools) {
|
|
318
|
-
return allowedToolsChoice(request, allowedTools, choice);
|
|
319
|
-
}
|
|
320
|
-
return choice === ToolChoiceMode.Required ? 'required' : undefined;
|
|
321
|
-
}
|
|
322
|
-
/** Force one function tool. It must be one the call may run. */
|
|
323
|
-
function forcedToolChoice(name, allowedTools) {
|
|
324
|
-
const tool = allowedTools.find((candidate) => candidate.name === name);
|
|
325
|
-
if (!tool) {
|
|
326
|
-
const offered = allowedTools.map((t) => t.name).join(', ') || 'none';
|
|
327
|
-
throw new NoeticConfigError({
|
|
328
|
-
code: 'UNKNOWN_TOOL_CHOICE',
|
|
329
|
-
message: `toolChoice forces tool ${JSON.stringify(name)}, which this model call cannot run.`,
|
|
330
|
-
hint: `Force a tool the call offers (offered: ${offered}), or give '${name}' to the step or a context layer.`,
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
return {
|
|
334
|
-
type: 'function',
|
|
335
|
-
name: sanitizeToolNameForWire(tool.name),
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
/** Restrict the model to `allowedTools` (plus the step's server tools). */
|
|
339
|
-
function allowedToolsChoice(request, allowedTools, mode) {
|
|
340
|
-
return {
|
|
341
|
-
type: 'allowed_tools',
|
|
342
|
-
mode,
|
|
343
|
-
tools: [
|
|
344
|
-
...allowedTools.map((tool) => ({
|
|
345
|
-
type: 'function',
|
|
346
|
-
name: sanitizeToolNameForWire(tool.name),
|
|
347
|
-
})),
|
|
348
|
-
...(request._serverTools ?? []).map((spec) => ({
|
|
349
|
-
type: spec.type,
|
|
350
|
-
})),
|
|
351
|
-
],
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
/** Match a wire tool name against both the original and the sanitised name. */
|
|
355
|
-
function findTool(tools, wireName) {
|
|
356
|
-
return tools.find((tool) => tool.name === wireName || sanitizeToolNameForWire(tool.name) === wireName);
|
|
357
|
-
}
|
|
358
|
-
function filterAllowedTools(request) {
|
|
359
|
-
if (!request.tools) {
|
|
360
|
-
return undefined;
|
|
361
|
-
}
|
|
362
|
-
if (!('allowedToolNames' in request) || !request.allowedToolNames) {
|
|
363
|
-
return request.tools;
|
|
364
|
-
}
|
|
365
|
-
const allowedNames = new Set(request.allowedToolNames);
|
|
366
|
-
return request.tools.filter((tool) => allowedNames.has(tool.name));
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Per-`callModel` span accumulator. Spans are created lazily from the calling
|
|
370
|
-
* context's span (so the trace tree mirrors the execution tree) and flushed to
|
|
371
|
-
* the harness `traceExporter` once the call settles — see issue #50, where the
|
|
372
|
-
* exporter was stored but never invoked.
|
|
373
|
-
*/
|
|
374
|
-
class CallSpanCollector {
|
|
375
|
-
harness;
|
|
376
|
-
exporter;
|
|
377
|
-
parent;
|
|
378
|
-
nodeId;
|
|
379
|
-
firstSpanAttributes;
|
|
380
|
-
spans = [];
|
|
381
|
-
constructor(harness, exporter, parent, nodeId,
|
|
382
|
-
/** Stamped on the first model span only, then dropped. */
|
|
383
|
-
firstSpanAttributes) {
|
|
384
|
-
this.harness = harness;
|
|
385
|
-
this.exporter = exporter;
|
|
386
|
-
this.parent = parent;
|
|
387
|
-
this.nodeId = nodeId;
|
|
388
|
-
this.firstSpanAttributes = firstSpanAttributes;
|
|
389
|
-
}
|
|
390
|
-
/** Link a span back to its declared workflow node, when the call carries one. */
|
|
391
|
-
stampNode(span) {
|
|
392
|
-
if (this.nodeId !== undefined) {
|
|
393
|
-
span.setAttribute(NoeticAttr.NODE_ID, this.nodeId);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
/** Open a model-call span stamped with GenAI semantic-convention attributes. */
|
|
397
|
-
startModelSpan(model) {
|
|
398
|
-
const span = this.harness.createSpan('llm.call', this.parent);
|
|
399
|
-
span.setAttribute(GenAI.SYSTEM, 'openrouter');
|
|
400
|
-
span.setAttribute(GenAI.REQUEST_MODEL, model);
|
|
401
|
-
this.stampNode(span);
|
|
402
|
-
for (const [key, value] of Object.entries(this.firstSpanAttributes ?? {})) {
|
|
403
|
-
span.setAttribute(key, value);
|
|
404
|
-
}
|
|
405
|
-
this.firstSpanAttributes = undefined;
|
|
406
|
-
this.spans.push(span);
|
|
407
|
-
return span;
|
|
408
|
-
}
|
|
409
|
-
/** Record usage + cost on a model-call span. */
|
|
410
|
-
finishModelSpan(span, usage, cost) {
|
|
411
|
-
span.setAttribute(GenAI.USAGE_INPUT_TOKENS, usage.inputTokens);
|
|
412
|
-
span.setAttribute(GenAI.USAGE_OUTPUT_TOKENS, usage.outputTokens);
|
|
413
|
-
if (usage.cachedTokens !== undefined) {
|
|
414
|
-
span.setAttribute(GenAI.USAGE_CACHED_INPUT_TOKENS, usage.cachedTokens);
|
|
415
|
-
}
|
|
416
|
-
if (usage.cacheWriteTokens !== undefined) {
|
|
417
|
-
span.setAttribute(GenAI.USAGE_CACHE_WRITE_TOKENS, usage.cacheWriteTokens);
|
|
418
|
-
}
|
|
419
|
-
if (cost !== undefined) {
|
|
420
|
-
span.setAttribute(GenAI.COST, cost);
|
|
421
|
-
}
|
|
422
|
-
span.end();
|
|
423
|
-
}
|
|
424
|
-
/** Open and immediately close a tool-call span under the given model span. */
|
|
425
|
-
recordToolSpan(parent, toolName, needsApproval, approval) {
|
|
426
|
-
const span = this.harness.createSpan('tool.call', parent);
|
|
427
|
-
span.setAttribute(ToolAttr.NAME, toolName);
|
|
428
|
-
span.setAttribute(ToolAttr.NEEDS_APPROVAL, needsApproval);
|
|
429
|
-
if (approval !== undefined) {
|
|
430
|
-
span.setAttribute(ToolAttr.APPROVAL, approval);
|
|
431
|
-
}
|
|
432
|
-
this.stampNode(span);
|
|
433
|
-
span.end();
|
|
434
|
-
this.spans.push(span);
|
|
435
|
-
}
|
|
436
|
-
/** Flush all accumulated spans to the exporter. */
|
|
437
|
-
async flush() {
|
|
438
|
-
if (this.spans.length === 0) {
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
await exportSpans(this.exporter, this.spans);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
88
|
export class AgentHarnessModelCaller {
|
|
445
89
|
opts;
|
|
446
90
|
constructor(opts) {
|
|
@@ -465,6 +109,14 @@ export class AgentHarnessModelCaller {
|
|
|
465
109
|
},
|
|
466
110
|
});
|
|
467
111
|
}
|
|
112
|
+
/** What the tool-round functions need from this caller. */
|
|
113
|
+
toolRoundScope() {
|
|
114
|
+
return {
|
|
115
|
+
harness: this.opts.harness,
|
|
116
|
+
itemSchemas: this.opts.itemSchemas,
|
|
117
|
+
emitItemAppended: (ctx, item) => this.emitItemAppended(ctx, item),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
468
120
|
async callOverriddenModel(request) {
|
|
469
121
|
if (!this.opts.callModelOverride) {
|
|
470
122
|
throw new Error('No callModel override configured.');
|
|
@@ -553,17 +205,79 @@ export class AgentHarnessModelCaller {
|
|
|
553
205
|
input: 0,
|
|
554
206
|
items: 0,
|
|
555
207
|
};
|
|
556
|
-
|
|
557
|
-
|
|
208
|
+
const markRound = () => {
|
|
209
|
+
marks = {
|
|
210
|
+
round,
|
|
211
|
+
log: roundLedger?.mark() ?? 0,
|
|
212
|
+
input: conversationInput.length,
|
|
213
|
+
items: allItems.length,
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
// The current round's checkpoint writes, in order. Its own record waits for
|
|
217
|
+
// them, so a late checkpoint never replaces it.
|
|
218
|
+
let checkpoints = Promise.resolve();
|
|
219
|
+
const writeRound = async (record) => {
|
|
558
220
|
if (!roundLedger || request.signal?.aborted) {
|
|
559
221
|
return;
|
|
560
222
|
}
|
|
561
|
-
await roundLedger.record(round, marks.log, {
|
|
223
|
+
if (!(await roundLedger.record(round, marks.log, record))) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
prepared.emitIfAllowed('model_round_recorded', {
|
|
227
|
+
round,
|
|
228
|
+
itemIds: itemIds(record.items),
|
|
229
|
+
done: !record.cut,
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
// A rejected round is asked again on the conversation it was asked on, or fails
|
|
233
|
+
// the call, so a resume must not go on from the items it streamed. The same goes
|
|
234
|
+
// for the round that ends the loop.
|
|
235
|
+
const dropCheckpoints = async () => {
|
|
236
|
+
await checkpoints;
|
|
237
|
+
await roundLedger?.drop(round);
|
|
238
|
+
};
|
|
239
|
+
const rejectRound = async (check) => {
|
|
240
|
+
let result;
|
|
241
|
+
try {
|
|
242
|
+
result = check();
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
await dropCheckpoints();
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
if (result.kind === 'continue') {
|
|
249
|
+
await dropCheckpoints();
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
};
|
|
253
|
+
// Record a round the loop goes on from, so a resumed loop can replay it.
|
|
254
|
+
const recordRound = async (bill) => {
|
|
255
|
+
await checkpoints;
|
|
256
|
+
await writeRound({
|
|
562
257
|
items: allItems.slice(marks.items),
|
|
563
258
|
input: conversationInput.slice(marks.input),
|
|
564
259
|
...bill,
|
|
565
260
|
});
|
|
566
261
|
};
|
|
262
|
+
// Record the output items a round has finished while it still streams, so a
|
|
263
|
+
// resume goes on from them rather than making them again. The input holds only
|
|
264
|
+
// what the round began with: the tool round adds each function call with its
|
|
265
|
+
// output, as it does for a round that finishes.
|
|
266
|
+
const checkpoint = (finished) => {
|
|
267
|
+
if (!roundLedger) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const record = {
|
|
271
|
+
items: [
|
|
272
|
+
...allItems.slice(marks.items),
|
|
273
|
+
...finished,
|
|
274
|
+
],
|
|
275
|
+
input: conversationInput.slice(marks.input),
|
|
276
|
+
...noBill,
|
|
277
|
+
cut: true,
|
|
278
|
+
};
|
|
279
|
+
checkpoints = checkpoints.then(() => writeRound(record));
|
|
280
|
+
};
|
|
567
281
|
let stoppedForRecall = false;
|
|
568
282
|
const steers = openSteerInbox({
|
|
569
283
|
ctx: request.ctx,
|
|
@@ -575,17 +289,39 @@ export class AgentHarnessModelCaller {
|
|
|
575
289
|
while (!request.signal?.aborted) {
|
|
576
290
|
const replayed = roundLedger?.take(round, replaySchemas);
|
|
577
291
|
if (replayed) {
|
|
292
|
+
markRound();
|
|
578
293
|
allItems.push(...replayed.items);
|
|
579
294
|
conversationInput.push(...frameworkCast(replayed.input));
|
|
580
295
|
for (const item of replayed.appended) {
|
|
581
296
|
request.ctx?.itemLog.append(item);
|
|
582
297
|
}
|
|
583
|
-
|
|
298
|
+
// A cut round never reported usage, so it has no bill to count.
|
|
299
|
+
if (!replayed.charged && !replayed.cut) {
|
|
584
300
|
spend.replay(replayed);
|
|
585
301
|
}
|
|
586
302
|
prepared.emitIfAllowed('model_round_replayed', {
|
|
587
303
|
round,
|
|
588
304
|
});
|
|
305
|
+
if (replayed.cut && !hasFunctionCalls(replayed.items)) {
|
|
306
|
+
// The stream was cut after the answer finished: it ends the loop.
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
if (replayed.cut) {
|
|
310
|
+
// Go on from the function calls the stream finished: run them, then ask
|
|
311
|
+
// the model again with their results in the conversation.
|
|
312
|
+
await runToolRound(this.toolRoundScope(), {
|
|
313
|
+
roundItems: replayed.items,
|
|
314
|
+
request,
|
|
315
|
+
prepared,
|
|
316
|
+
allItems,
|
|
317
|
+
conversationInput,
|
|
318
|
+
round,
|
|
319
|
+
spans,
|
|
320
|
+
modelSpan: null,
|
|
321
|
+
steers,
|
|
322
|
+
});
|
|
323
|
+
await recordRound(spend.keep());
|
|
324
|
+
}
|
|
589
325
|
round += 1;
|
|
590
326
|
const replayLimit = this.handleToolRoundLimit({
|
|
591
327
|
round,
|
|
@@ -599,12 +335,7 @@ export class AgentHarnessModelCaller {
|
|
|
599
335
|
continue;
|
|
600
336
|
}
|
|
601
337
|
if (marks.round !== round) {
|
|
602
|
-
|
|
603
|
-
round,
|
|
604
|
-
log: roundLedger?.mark() ?? 0,
|
|
605
|
-
input: conversationInput.length,
|
|
606
|
-
items: allItems.length,
|
|
607
|
-
};
|
|
338
|
+
markRound();
|
|
608
339
|
}
|
|
609
340
|
// A graceful stop starts no new round: wait here until it cancels the call.
|
|
610
341
|
await drains?.hold(request.ctx);
|
|
@@ -706,6 +437,10 @@ export class AgentHarnessModelCaller {
|
|
|
706
437
|
// Output items the stream has finished, kept so a steer can stop the
|
|
707
438
|
// round early without losing them.
|
|
708
439
|
const finishedItems = [];
|
|
440
|
+
// Parsed for the round ledger; none once the stream has failed, since a call
|
|
441
|
+
// that has thrown must write nothing more.
|
|
442
|
+
const parsedItems = [];
|
|
443
|
+
let streaming = true;
|
|
709
444
|
let steered = false;
|
|
710
445
|
const pipePromise = pipeStreamEventsToBroadcaster({
|
|
711
446
|
stream: callResult.getFullResponsesStream(),
|
|
@@ -724,6 +459,19 @@ export class AgentHarnessModelCaller {
|
|
|
724
459
|
},
|
|
725
460
|
onItemDone: async (item) => {
|
|
726
461
|
finishedItems.push(item);
|
|
462
|
+
if (streaming && roundLedger) {
|
|
463
|
+
try {
|
|
464
|
+
parsedItems.push(...replaySchemas.parseMany(frameworkCast([
|
|
465
|
+
item,
|
|
466
|
+
])));
|
|
467
|
+
checkpoint(parsedItems);
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
// The round fails on this item when it ends; nothing after it can be
|
|
471
|
+
// gone on from.
|
|
472
|
+
streaming = false;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
727
475
|
if (!steers?.hasQueued() || !(await steers.offer(SteerPoint.ItemDone))) {
|
|
728
476
|
return;
|
|
729
477
|
}
|
|
@@ -755,7 +503,9 @@ export class AgentHarnessModelCaller {
|
|
|
755
503
|
await awaitWithAbort(pipePromise, roundSignal);
|
|
756
504
|
}
|
|
757
505
|
catch (err) {
|
|
758
|
-
|
|
506
|
+
streaming = false;
|
|
507
|
+
await checkpoints;
|
|
508
|
+
if (dispatch?.wasRefused(err)) {
|
|
759
509
|
await dispatch.settle();
|
|
760
510
|
}
|
|
761
511
|
if (idleStalled) {
|
|
@@ -789,7 +539,7 @@ export class AgentHarnessModelCaller {
|
|
|
789
539
|
});
|
|
790
540
|
allItems.push(...roundItems);
|
|
791
541
|
replayOutput(conversationInput, roundItems);
|
|
792
|
-
await this.
|
|
542
|
+
await runToolRound(this.toolRoundScope(), {
|
|
793
543
|
roundItems,
|
|
794
544
|
request,
|
|
795
545
|
prepared,
|
|
@@ -813,25 +563,25 @@ export class AgentHarnessModelCaller {
|
|
|
813
563
|
}
|
|
814
564
|
continue;
|
|
815
565
|
}
|
|
816
|
-
const recovery = this.handleModelRecovery({
|
|
566
|
+
const recovery = await rejectRound(() => this.handleModelRecovery({
|
|
817
567
|
sdkResponse,
|
|
818
568
|
round,
|
|
819
569
|
invalidRecoveryContinuations,
|
|
820
570
|
emitIfAllowed: prepared.emitIfAllowed,
|
|
821
|
-
});
|
|
571
|
+
}));
|
|
822
572
|
if (recovery.kind === 'continue') {
|
|
823
573
|
invalidRecoveryContinuations = recovery.invalidRecoveryContinuations;
|
|
824
574
|
useEphemeralContinue = true;
|
|
825
575
|
continue;
|
|
826
576
|
}
|
|
827
577
|
const roundItems = this.parseRoundItems(request, extractOutputItems(sdkResponse));
|
|
828
|
-
const outputRecovery = this.handleEmptyOutputRecovery({
|
|
578
|
+
const outputRecovery = await rejectRound(() => this.handleEmptyOutputRecovery({
|
|
829
579
|
sdkResponse,
|
|
830
580
|
roundItems,
|
|
831
581
|
round,
|
|
832
582
|
invalidRecoveryContinuations,
|
|
833
583
|
emitIfAllowed: prepared.emitIfAllowed,
|
|
834
|
-
});
|
|
584
|
+
}));
|
|
835
585
|
if (outputRecovery.kind === 'continue') {
|
|
836
586
|
invalidRecoveryContinuations = outputRecovery.invalidRecoveryContinuations;
|
|
837
587
|
useEphemeralContinue = true;
|
|
@@ -845,7 +595,7 @@ export class AgentHarnessModelCaller {
|
|
|
845
595
|
});
|
|
846
596
|
servedTier = sdkResponse.serviceTier ?? undefined;
|
|
847
597
|
allItems.push(...roundItems);
|
|
848
|
-
const ranTools = await this.
|
|
598
|
+
const ranTools = await runToolRound(this.toolRoundScope(), {
|
|
849
599
|
roundItems,
|
|
850
600
|
request,
|
|
851
601
|
prepared,
|
|
@@ -859,6 +609,9 @@ export class AgentHarnessModelCaller {
|
|
|
859
609
|
// A steer staged while the final answer streamed gets its own round, so
|
|
860
610
|
// the model answers it inside this call rather than in the next turn.
|
|
861
611
|
if (!ranTools && !steers?.hasStaged()) {
|
|
612
|
+
// The step's own record covers the round that ends the loop. A cut record
|
|
613
|
+
// left behind would hand a retry this answer after the step rejected it.
|
|
614
|
+
await dropCheckpoints();
|
|
862
615
|
break;
|
|
863
616
|
}
|
|
864
617
|
if (!ranTools) {
|
|
@@ -924,26 +677,6 @@ export class AgentHarnessModelCaller {
|
|
|
924
677
|
});
|
|
925
678
|
return roundItemSchemas.parseMany(items);
|
|
926
679
|
}
|
|
927
|
-
/** Run the round's function calls. Returns `false` when there were none to run. */
|
|
928
|
-
async runToolRound(params) {
|
|
929
|
-
const functionCalls = params.roundItems.filter(isFunctionCall);
|
|
930
|
-
if (functionCalls.length === 0 || !params.request.tools) {
|
|
931
|
-
return false;
|
|
932
|
-
}
|
|
933
|
-
await this.executeToolRound({
|
|
934
|
-
functionCalls,
|
|
935
|
-
request: params.request,
|
|
936
|
-
allowedTools: params.prepared.allowedTools,
|
|
937
|
-
allItems: params.allItems,
|
|
938
|
-
conversationInput: params.conversationInput,
|
|
939
|
-
round: params.round,
|
|
940
|
-
emitIfAllowed: params.prepared.emitIfAllowed,
|
|
941
|
-
spans: params.spans,
|
|
942
|
-
modelSpan: params.modelSpan,
|
|
943
|
-
steers: params.steers,
|
|
944
|
-
});
|
|
945
|
-
return true;
|
|
946
|
-
}
|
|
947
680
|
handleModelRecovery(params) {
|
|
948
681
|
const terminalError = providerTerminalError(params.sdkResponse);
|
|
949
682
|
if (!terminalError) {
|
|
@@ -1000,156 +733,6 @@ export class AgentHarnessModelCaller {
|
|
|
1000
733
|
invalidRecoveryContinuations: next,
|
|
1001
734
|
};
|
|
1002
735
|
}
|
|
1003
|
-
async executeToolRound(params) {
|
|
1004
|
-
const { functionCalls, request, allowedTools, allItems, conversationInput, spans, modelSpan, steers, } = params;
|
|
1005
|
-
params.emitIfAllowed('tool_round_started', {
|
|
1006
|
-
round: params.round,
|
|
1007
|
-
toolCount: functionCalls.length,
|
|
1008
|
-
});
|
|
1009
|
-
for (const fc of functionCalls) {
|
|
1010
|
-
conversationInput.push({
|
|
1011
|
-
type: 'function_call',
|
|
1012
|
-
callId: fc.callId,
|
|
1013
|
-
id: fc.id ?? crypto.randomUUID(),
|
|
1014
|
-
name: fc.name,
|
|
1015
|
-
arguments: fc.arguments,
|
|
1016
|
-
});
|
|
1017
|
-
}
|
|
1018
|
-
for (const [index, fc] of functionCalls.entries()) {
|
|
1019
|
-
await drainsOf(request.ctx)?.hold(request.ctx);
|
|
1020
|
-
if (request.signal?.aborted) {
|
|
1021
|
-
break;
|
|
1022
|
-
}
|
|
1023
|
-
const remaining = functionCalls.slice(index);
|
|
1024
|
-
if (steers?.hasQueued() && (await steers.offer(SteerPoint.BeforeToolCall, remaining))) {
|
|
1025
|
-
for (const skipped of remaining) {
|
|
1026
|
-
this.recordSkippedToolCall({
|
|
1027
|
-
fc: skipped,
|
|
1028
|
-
request,
|
|
1029
|
-
allowedTools,
|
|
1030
|
-
allItems,
|
|
1031
|
-
conversationInput,
|
|
1032
|
-
emitIfAllowed: params.emitIfAllowed,
|
|
1033
|
-
});
|
|
1034
|
-
}
|
|
1035
|
-
break;
|
|
1036
|
-
}
|
|
1037
|
-
const toolForCall = findTool(allowedTools, fc.name);
|
|
1038
|
-
const approval = await this.executeFunctionCall({
|
|
1039
|
-
fc,
|
|
1040
|
-
request,
|
|
1041
|
-
allowedTools,
|
|
1042
|
-
allItems,
|
|
1043
|
-
conversationInput,
|
|
1044
|
-
emitIfAllowed: params.emitIfAllowed,
|
|
1045
|
-
});
|
|
1046
|
-
spans.recordToolSpan(modelSpan, fc.name, toolForCall?.needsApproval ?? false, approval);
|
|
1047
|
-
}
|
|
1048
|
-
params.emitIfAllowed('tool_round_completed', {
|
|
1049
|
-
round: params.round,
|
|
1050
|
-
toolCount: functionCalls.length,
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
async executeFunctionCall(params) {
|
|
1054
|
-
const { fc, request, allowedTools, allItems, conversationInput } = params;
|
|
1055
|
-
params.emitIfAllowed('tool_call_started', {
|
|
1056
|
-
name: fc.name,
|
|
1057
|
-
callId: fc.callId,
|
|
1058
|
-
});
|
|
1059
|
-
let parsedArgs;
|
|
1060
|
-
try {
|
|
1061
|
-
parsedArgs = JSON.parse(fc.arguments);
|
|
1062
|
-
}
|
|
1063
|
-
catch {
|
|
1064
|
-
this.recordUnrunToolCall({
|
|
1065
|
-
fc,
|
|
1066
|
-
output: `Error: malformed JSON in tool arguments: ${fc.arguments}`,
|
|
1067
|
-
request,
|
|
1068
|
-
allowedTools,
|
|
1069
|
-
allItems,
|
|
1070
|
-
conversationInput,
|
|
1071
|
-
emitIfAllowed: params.emitIfAllowed,
|
|
1072
|
-
});
|
|
1073
|
-
return undefined;
|
|
1074
|
-
}
|
|
1075
|
-
if (!request.tools) {
|
|
1076
|
-
throw new Error(`executeFunctionCall invoked without tools on CallModelRequest (tool name: ${fc.name}).`);
|
|
1077
|
-
}
|
|
1078
|
-
const toolForCall = findTool(allowedTools, fc.name);
|
|
1079
|
-
const toolResult = await executeToolCall({
|
|
1080
|
-
toolName: fc.name,
|
|
1081
|
-
args: parsedArgs,
|
|
1082
|
-
tools: allowedTools,
|
|
1083
|
-
context: request.ctx,
|
|
1084
|
-
harness: this.opts.harness,
|
|
1085
|
-
layers: request.layers,
|
|
1086
|
-
callId: fc.callId,
|
|
1087
|
-
});
|
|
1088
|
-
// Owner-scoped result validation: a tool's `toolResults` schemas apply
|
|
1089
|
-
// only to that tool's own result items. Harness-level `opts.itemSchemas`
|
|
1090
|
-
// stay global by design; sibling tools' schemas never reject this item.
|
|
1091
|
-
const outputItem = createToolResultItem({
|
|
1092
|
-
output: toolResult.output,
|
|
1093
|
-
callId: fc.callId,
|
|
1094
|
-
roundItemSchemas: this.opts.itemSchemas.extend(toolForCall?.itemSchemas),
|
|
1095
|
-
tool: toolForCall,
|
|
1096
|
-
callItem: fc,
|
|
1097
|
-
args: parsedArgs,
|
|
1098
|
-
result: toolResult.result,
|
|
1099
|
-
error: toolResult.error,
|
|
1100
|
-
});
|
|
1101
|
-
allItems.push(outputItem);
|
|
1102
|
-
conversationInput.push({
|
|
1103
|
-
type: 'function_call_output',
|
|
1104
|
-
callId: fc.callId,
|
|
1105
|
-
output: toolResult.output,
|
|
1106
|
-
});
|
|
1107
|
-
this.emitItemAppended(request.ctx, outputItem);
|
|
1108
|
-
params.emitIfAllowed('tool_call_completed', {
|
|
1109
|
-
name: fc.name,
|
|
1110
|
-
callId: fc.callId,
|
|
1111
|
-
error: toolResult.error === true,
|
|
1112
|
-
output: toolResult.output,
|
|
1113
|
-
});
|
|
1114
|
-
return toolResult.approval;
|
|
1115
|
-
}
|
|
1116
|
-
recordSkippedToolCall(params) {
|
|
1117
|
-
params.emitIfAllowed('tool_call_started', {
|
|
1118
|
-
name: params.fc.name,
|
|
1119
|
-
callId: params.fc.callId,
|
|
1120
|
-
});
|
|
1121
|
-
this.recordUnrunToolCall({
|
|
1122
|
-
...params,
|
|
1123
|
-
output: SKIPPED_FOR_STEER_OUTPUT,
|
|
1124
|
-
});
|
|
1125
|
-
}
|
|
1126
|
-
/** Answer a call that never ran, so every call in the history has an output. */
|
|
1127
|
-
recordUnrunToolCall(params) {
|
|
1128
|
-
const { fc, request, allowedTools, allItems, conversationInput } = params;
|
|
1129
|
-
const toolForCall = findTool(allowedTools, fc.name);
|
|
1130
|
-
// Owner-scoped result validation (see executeFunctionCall).
|
|
1131
|
-
const outputItem = createToolResultItem({
|
|
1132
|
-
output: params.output,
|
|
1133
|
-
callId: fc.callId,
|
|
1134
|
-
roundItemSchemas: this.opts.itemSchemas.extend(toolForCall?.itemSchemas),
|
|
1135
|
-
tool: toolForCall,
|
|
1136
|
-
callItem: fc,
|
|
1137
|
-
error: true,
|
|
1138
|
-
});
|
|
1139
|
-
allItems.push(outputItem);
|
|
1140
|
-
conversationInput.push({
|
|
1141
|
-
type: 'function_call_output',
|
|
1142
|
-
callId: fc.callId,
|
|
1143
|
-
output: params.output,
|
|
1144
|
-
});
|
|
1145
|
-
this.emitItemAppended(request.ctx, outputItem);
|
|
1146
|
-
params.emitIfAllowed('tool_call_completed', {
|
|
1147
|
-
name: fc.name,
|
|
1148
|
-
callId: fc.callId,
|
|
1149
|
-
error: true,
|
|
1150
|
-
output: params.output,
|
|
1151
|
-
});
|
|
1152
|
-
}
|
|
1153
736
|
handleToolRoundLimit(params) {
|
|
1154
737
|
if (params.round < MAX_TOOL_ROUNDS) {
|
|
1155
738
|
return {
|