@noetic-tools/core 5.21.0 → 5.22.0-next.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.d.ts +6 -1
- package/dist/adapters/openrouter.js +10 -0
- package/dist/builders/decide-builders.d.ts +7 -0
- package/dist/builders/tool-builder.d.ts +25 -1
- package/dist/builders/tool-builder.js +25 -1
- package/dist/builders/workflow-control-nodes.js +1 -0
- package/dist/builders/workflow-hydrator.d.ts +19 -0
- package/dist/builders/workflow-hydrator.js +19 -0
- package/dist/builders/workflow-step.js +7 -1
- package/dist/connections/cron-schedule.js +12 -3
- package/dist/connections/durability.d.ts +1 -2
- package/dist/connections/durability.js +1 -4
- package/dist/connections/gates.js +6 -2
- package/dist/connections/harness-wiring.d.ts +5 -0
- package/dist/connections/harness-wiring.js +76 -10
- package/dist/connections/in-memory-connection.d.ts +4 -0
- package/dist/connections/in-memory-connection.js +10 -0
- package/dist/connections/model-tools.js +5 -1
- package/dist/connections/registry.d.ts +6 -2
- package/dist/connections/registry.js +10 -0
- package/dist/connections/sources.js +261 -20
- package/dist/connections/triggers.d.ts +62 -2
- package/dist/connections/triggers.js +375 -35
- package/dist/harness/agent-harness.d.ts +317 -4
- package/dist/harness/agent-harness.js +1264 -96
- package/dist/harness/connection-gate.d.ts +3 -0
- package/dist/harness/connection-gate.js +4 -0
- package/dist/harness/decide-call.d.ts +15 -0
- package/dist/harness/decide-call.js +57 -7
- package/dist/harness/deps/runtime.d.ts +3 -0
- package/dist/harness/deps/runtime.js +2 -0
- package/dist/harness/flow-version.d.ts +13 -0
- package/dist/harness/flow-version.js +66 -0
- package/dist/harness/harness-options.d.ts +91 -0
- package/dist/harness/llm-client.d.ts +2 -0
- package/dist/harness/llm-client.js +9 -5
- package/dist/harness/model-call.js +8 -0
- package/dist/harness/model-rounds.d.ts +30 -0
- package/dist/harness/model-rounds.js +146 -1
- package/dist/harness/model-tool-round.d.ts +3 -0
- package/dist/harness/model-tool-round.js +249 -22
- package/dist/harness/park-watchers.d.ts +93 -0
- package/dist/harness/park-watchers.js +217 -0
- package/dist/harness/read-decision.d.ts +23 -0
- package/dist/harness/read-decision.js +100 -0
- package/dist/harness/spend.js +4 -0
- package/dist/harness/steer-inbox.js +3 -1
- package/dist/harness/thread-record.d.ts +77 -1
- package/dist/harness/thread-record.js +272 -13
- package/dist/index.d.ts +22 -8
- package/dist/index.js +12 -2
- package/dist/interpreter/acp-resume.d.ts +77 -0
- package/dist/interpreter/acp-resume.js +164 -0
- package/dist/interpreter/acp-tools.d.ts +11 -0
- package/dist/interpreter/acp-tools.js +94 -7
- package/dist/interpreter/connection-steps.d.ts +24 -1
- package/dist/interpreter/connection-steps.js +77 -2
- package/dist/interpreter/execute-acp-agent.js +111 -15
- package/dist/interpreter/execute-action.js +7 -0
- package/dist/interpreter/execute-call.js +30 -4
- package/dist/interpreter/execute-control.js +21 -0
- package/dist/interpreter/execute-decide.js +5 -0
- package/dist/interpreter/execute-invoke-tool.js +149 -5
- package/dist/interpreter/execute-wait.js +67 -9
- package/dist/interpreter/execute.js +4 -0
- package/dist/interpreter/turn-ending.d.ts +15 -0
- package/dist/interpreter/turn-ending.js +46 -0
- package/dist/portable/builders.d.ts +1 -1
- package/dist/portable/builders.js +1 -1
- package/dist/portable/runtime.d.ts +4 -0
- package/dist/portable/runtime.js +2 -0
- package/dist/runtime/durable/parking.d.ts +61 -0
- package/dist/runtime/durable/parking.js +137 -0
- package/dist/runtime/durable/session-store.d.ts +38 -0
- package/dist/runtime/durable/session-store.js +79 -1
- package/dist/runtime/durable/step-ledger.d.ts +121 -2
- package/dist/runtime/durable/step-ledger.js +352 -15
- package/dist/runtime/durable/turn-lease.d.ts +128 -0
- package/dist/runtime/durable/turn-lease.js +307 -0
- package/dist/runtime/event-broadcaster.d.ts +9 -1
- package/dist/runtime/event-broadcaster.js +16 -1
- package/dist/runtime/held-calls.d.ts +192 -0
- package/dist/runtime/held-calls.js +496 -0
- package/dist/runtime/in-memory-storage.d.ts +10 -1
- package/dist/runtime/in-memory-storage.js +20 -2
- package/dist/runtime/message-queue.d.ts +2 -0
- package/dist/runtime/message-queue.js +8 -0
- package/dist/runtime/output-store.d.ts +88 -0
- package/dist/runtime/output-store.js +241 -0
- package/dist/runtime/session-runner.d.ts +18 -2
- package/dist/runtime/session-runner.js +88 -3
- package/dist/runtime/session-streams.d.ts +28 -7
- package/dist/runtime/session-streams.js +112 -67
- package/dist/runtime/tool-approval.d.ts +46 -6
- package/dist/runtime/tool-approval.js +160 -38
- package/dist/runtime/tool-call.d.ts +34 -2
- package/dist/runtime/tool-call.js +182 -13
- package/dist/runtime/tool-context.d.ts +15 -1
- package/dist/runtime/tool-context.js +38 -2
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +27 -0
|
@@ -27,9 +27,38 @@
|
|
|
27
27
|
import { frameworkCast, isAssistantMessage } from '@noetic-tools/types';
|
|
28
28
|
import { z } from 'zod';
|
|
29
29
|
import { isFunctionCall } from '../interpreter/typeguards.js';
|
|
30
|
+
import { hashStepInput } from '../runtime/durable/step-ledger.js';
|
|
30
31
|
import { ContextImpl } from './deps/runtime.js';
|
|
31
32
|
//#region Types
|
|
32
33
|
const ROUND_KIND = 'callModel:round';
|
|
34
|
+
const CALL_KIND = 'callModel:call';
|
|
35
|
+
/** What a recorded tool call left: the result the round went on with. */
|
|
36
|
+
const CallOutputSchema = z.object({
|
|
37
|
+
output: z.string(),
|
|
38
|
+
result: z.unknown().optional(),
|
|
39
|
+
/**
|
|
40
|
+
* `result` was left out because `output` holds the same thing: as text, or as
|
|
41
|
+
* its JSON. A record that kept both was twice the size of what it recorded.
|
|
42
|
+
*/
|
|
43
|
+
resultAs: z
|
|
44
|
+
.enum([
|
|
45
|
+
'text',
|
|
46
|
+
'json',
|
|
47
|
+
])
|
|
48
|
+
.optional(),
|
|
49
|
+
/** The id of the result item the call appended, which a replay appends again. */
|
|
50
|
+
itemId: z.string().optional(),
|
|
51
|
+
/** The tool's body ran, so its UI showed a call and how it ended. */
|
|
52
|
+
ran: z.boolean().optional(),
|
|
53
|
+
error: z.boolean().optional(),
|
|
54
|
+
approval: z
|
|
55
|
+
.enum([
|
|
56
|
+
'approved',
|
|
57
|
+
'denied',
|
|
58
|
+
'none_configured',
|
|
59
|
+
])
|
|
60
|
+
.optional(),
|
|
61
|
+
});
|
|
33
62
|
const RoundOutputSchema = z.object({
|
|
34
63
|
items: z.array(z.unknown()),
|
|
35
64
|
input: z.array(z.unknown()),
|
|
@@ -152,9 +181,57 @@ export class RoundLedger {
|
|
|
152
181
|
this.recorded.add(round);
|
|
153
182
|
// A new record holds a bill no context has been charged with yet.
|
|
154
183
|
chargedRounds.get(ledger)?.delete(this.key(round));
|
|
155
|
-
|
|
184
|
+
// The next round's record, and the calls recorded beneath it, belong to the
|
|
185
|
+
// conversation an earlier attempt had. This one may ask for other calls.
|
|
186
|
+
await ledger.dropWithin(this.key(round + 1));
|
|
156
187
|
return true;
|
|
157
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* The call ledger of round `round`. Each call is recorded beneath the round's key,
|
|
191
|
+
* so the round's own record prunes them once it lands.
|
|
192
|
+
*/
|
|
193
|
+
calls(round) {
|
|
194
|
+
const ledger = this.ctx.ledger;
|
|
195
|
+
const stepId = this.dispatch.stepId;
|
|
196
|
+
const keyOf = (call) => `${this.key(round)}/call:${call.callId}`;
|
|
197
|
+
// A call is the same call only with the same name and arguments, asked for by
|
|
198
|
+
// the same step input: a provider that numbers its calls from one each time
|
|
199
|
+
// would otherwise hand a call the result of another conversation's.
|
|
200
|
+
const hashOf = (call) => hashStepInput({
|
|
201
|
+
name: call.name,
|
|
202
|
+
arguments: call.arguments,
|
|
203
|
+
step: this.dispatch.inputHash,
|
|
204
|
+
});
|
|
205
|
+
return {
|
|
206
|
+
scope: this.key(round),
|
|
207
|
+
noteEffect: (call) => {
|
|
208
|
+
ledger?.noteEffect(keyOf(call));
|
|
209
|
+
ledger?.noteEffect(this.key(round));
|
|
210
|
+
},
|
|
211
|
+
take: (call) => {
|
|
212
|
+
const entry = ledger?.take(keyOf(call), {
|
|
213
|
+
id: stepId,
|
|
214
|
+
kind: CALL_KIND,
|
|
215
|
+
inputHash: hashOf(call),
|
|
216
|
+
});
|
|
217
|
+
const parsed = entry ? CallOutputSchema.safeParse(entry.output) : undefined;
|
|
218
|
+
return parsed?.success ? expandResult(parsed.data) : undefined;
|
|
219
|
+
},
|
|
220
|
+
record: async (call, result) => {
|
|
221
|
+
if (!ledger) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
return ledger.record({
|
|
225
|
+
path: keyOf(call),
|
|
226
|
+
stepId,
|
|
227
|
+
kind: CALL_KIND,
|
|
228
|
+
inputHash: hashOf(call),
|
|
229
|
+
output: compactResult(result),
|
|
230
|
+
completedAt: new Date().toISOString(),
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
158
235
|
/** Drop the record of round `round`: the loop threw that round away. */
|
|
159
236
|
async drop(round) {
|
|
160
237
|
await this.ctx.ledger?.drop(this.key(round));
|
|
@@ -175,6 +252,74 @@ export class RoundLedger {
|
|
|
175
252
|
return roundPath(this.dispatch.path, round);
|
|
176
253
|
}
|
|
177
254
|
}
|
|
255
|
+
/** What a call record stores: the result, with `result` left out where `output` says it. */
|
|
256
|
+
function compactResult(result) {
|
|
257
|
+
const kept = {
|
|
258
|
+
output: result.output,
|
|
259
|
+
...(result.error === undefined
|
|
260
|
+
? {}
|
|
261
|
+
: {
|
|
262
|
+
error: result.error,
|
|
263
|
+
}),
|
|
264
|
+
...(result.approval === undefined
|
|
265
|
+
? {}
|
|
266
|
+
: {
|
|
267
|
+
approval: result.approval,
|
|
268
|
+
}),
|
|
269
|
+
...(result.itemId === undefined
|
|
270
|
+
? {}
|
|
271
|
+
: {
|
|
272
|
+
itemId: result.itemId,
|
|
273
|
+
}),
|
|
274
|
+
...(result.ran === undefined
|
|
275
|
+
? {}
|
|
276
|
+
: {
|
|
277
|
+
ran: result.ran,
|
|
278
|
+
}),
|
|
279
|
+
};
|
|
280
|
+
if (result.result === undefined) {
|
|
281
|
+
return kept;
|
|
282
|
+
}
|
|
283
|
+
const resultAs = sameAsOutput(result.result, result.output);
|
|
284
|
+
return resultAs === undefined
|
|
285
|
+
? {
|
|
286
|
+
...kept,
|
|
287
|
+
result: result.result,
|
|
288
|
+
}
|
|
289
|
+
: {
|
|
290
|
+
...kept,
|
|
291
|
+
resultAs,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
/** How `output` holds `result`, or undefined when it does not. */
|
|
295
|
+
function sameAsOutput(result, output) {
|
|
296
|
+
if (typeof result === 'string') {
|
|
297
|
+
return result === output ? 'text' : undefined;
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
return JSON.stringify(result) === output ? 'json' : undefined;
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return undefined;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/** The result a call record stands for. */
|
|
307
|
+
function expandResult(stored) {
|
|
308
|
+
const { resultAs, ...result } = stored;
|
|
309
|
+
if (resultAs === 'text') {
|
|
310
|
+
return {
|
|
311
|
+
...result,
|
|
312
|
+
result: result.output,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (resultAs === 'json') {
|
|
316
|
+
return {
|
|
317
|
+
...result,
|
|
318
|
+
result: JSON.parse(result.output),
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
178
323
|
/** A cut round holding a function call or an answer has something to go on from. */
|
|
179
324
|
function goesOn(item) {
|
|
180
325
|
return isFunctionCall(item) || isAssistantMessage(item);
|
|
@@ -7,6 +7,7 @@ import type { AgentHarnessContract, CallModelRequest, Context, Item, ItemSchemaR
|
|
|
7
7
|
import { itemsToInput } from '../adapters/openrouter.js';
|
|
8
8
|
import type { CallSpanCollector } from './call-span-collector.js';
|
|
9
9
|
import type { PreparedModelRequest } from './model-request.js';
|
|
10
|
+
import type { CallLedger } from './model-rounds.js';
|
|
10
11
|
import type { SteerInbox } from './steer-inbox.js';
|
|
11
12
|
/** What the tool round needs from the model caller that runs it. */
|
|
12
13
|
export interface ToolRoundScope {
|
|
@@ -34,4 +35,6 @@ export declare function runToolRound(scope: ToolRoundScope, params: {
|
|
|
34
35
|
spans: CallSpanCollector;
|
|
35
36
|
modelSpan: Span | null;
|
|
36
37
|
steers: SteerInbox | undefined;
|
|
38
|
+
/** Where the round's calls are recorded as they finish. */
|
|
39
|
+
calls?: CallLedger;
|
|
37
40
|
}): Promise<boolean>;
|
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
* answer it unrun when a steer arrives first), append its result item to the
|
|
4
4
|
* conversation, and record the tool span.
|
|
5
5
|
*/
|
|
6
|
-
import { SteerPoint } from '@noetic-tools/types';
|
|
7
|
-
import { itemsToInput } from '../adapters/openrouter.js';
|
|
6
|
+
import { SteerPoint, validateSchema } from '@noetic-tools/types';
|
|
7
|
+
import { itemsToInput, toolModelOutputToProvider } from '../adapters/openrouter.js';
|
|
8
8
|
import { isFunctionCall } from '../interpreter/typeguards.js';
|
|
9
9
|
import { drainsOf } from '../runtime/drain.js';
|
|
10
|
+
import { heldCallsOf, heldScopeOf, mayWriteHolds, waitOnHeldCalls } from '../runtime/held-calls.js';
|
|
10
11
|
import { createToolResultItem, executeToolCall } from '../runtime/tool-call.js';
|
|
12
|
+
import { emitToolUi } from '../runtime/tool-ui.js';
|
|
11
13
|
import { findTool } from './model-request.js';
|
|
12
14
|
//#endregion
|
|
13
15
|
//#region Tool Round
|
|
@@ -41,6 +43,7 @@ export async function runToolRound(scope, params) {
|
|
|
41
43
|
spans: params.spans,
|
|
42
44
|
modelSpan: params.modelSpan,
|
|
43
45
|
steers: params.steers,
|
|
46
|
+
calls: params.calls,
|
|
44
47
|
});
|
|
45
48
|
return true;
|
|
46
49
|
}
|
|
@@ -54,18 +57,25 @@ async function executeToolRound(scope, params) {
|
|
|
54
57
|
conversationInput.push({
|
|
55
58
|
type: 'function_call',
|
|
56
59
|
callId: fc.callId,
|
|
57
|
-
|
|
60
|
+
// Derived, so a resumed round sends the id it sent before.
|
|
61
|
+
id: fc.id ?? `fc_${fc.callId}`,
|
|
58
62
|
name: fc.name,
|
|
59
63
|
arguments: fc.arguments,
|
|
60
64
|
});
|
|
61
65
|
}
|
|
66
|
+
const heldCalls = [];
|
|
62
67
|
for (const [index, fc] of functionCalls.entries()) {
|
|
63
68
|
await drainsOf(request.ctx)?.hold(request.ctx);
|
|
64
69
|
if (request.signal?.aborted) {
|
|
65
70
|
break;
|
|
66
71
|
}
|
|
67
72
|
const remaining = functionCalls.slice(index);
|
|
68
|
-
|
|
73
|
+
// A call an earlier attempt finished is not up for skipping: it already ran.
|
|
74
|
+
const finished = params.calls?.take(fc) !== undefined;
|
|
75
|
+
if (!finished &&
|
|
76
|
+
steers?.hasQueued() &&
|
|
77
|
+
(await steers.offer(SteerPoint.BeforeToolCall, remaining))) {
|
|
78
|
+
await dropHolds(scope, request, remaining);
|
|
69
79
|
for (const skipped of remaining) {
|
|
70
80
|
recordSkippedToolCall(scope, {
|
|
71
81
|
fc: skipped,
|
|
@@ -79,15 +89,67 @@ async function executeToolRound(scope, params) {
|
|
|
79
89
|
break;
|
|
80
90
|
}
|
|
81
91
|
const toolForCall = findTool(allowedTools, fc.name);
|
|
82
|
-
const
|
|
92
|
+
const call = await executeFunctionCall(scope, {
|
|
83
93
|
fc,
|
|
84
94
|
request,
|
|
85
95
|
allowedTools,
|
|
86
96
|
allItems,
|
|
87
97
|
conversationInput,
|
|
88
98
|
emitIfAllowed: params.emitIfAllowed,
|
|
99
|
+
calls: params.calls,
|
|
89
100
|
});
|
|
90
|
-
|
|
101
|
+
if (call.held) {
|
|
102
|
+
heldCalls.push(fc);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
spans.recordToolSpan(modelSpan, fc.name, toolForCall?.needsApproval ?? false, call.approval);
|
|
106
|
+
}
|
|
107
|
+
// Calls held for a person: the round goes no further until each has an answer.
|
|
108
|
+
while (heldCalls.length > 0) {
|
|
109
|
+
// A message that arrives while calls are held is a steer like any other: the
|
|
110
|
+
// context layers decide. Applied, the held calls are skipped and the round goes on.
|
|
111
|
+
if (steers?.hasQueued() && (await steers.offer(SteerPoint.Held, heldCalls))) {
|
|
112
|
+
await dropHolds(scope, request, heldCalls);
|
|
113
|
+
for (const skipped of heldCalls.splice(0)) {
|
|
114
|
+
recordUnrunToolCall(scope, {
|
|
115
|
+
fc: skipped,
|
|
116
|
+
output: SKIPPED_FOR_STEER_OUTPUT,
|
|
117
|
+
request,
|
|
118
|
+
allowedTools,
|
|
119
|
+
allItems,
|
|
120
|
+
conversationInput,
|
|
121
|
+
emitIfAllowed: params.emitIfAllowed,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (!request.ctx) {
|
|
127
|
+
throw new Error('A held tool call needs a context to wait on.');
|
|
128
|
+
}
|
|
129
|
+
await waitOnHeldCalls({
|
|
130
|
+
harness: scope.harness,
|
|
131
|
+
ctx: request.ctx,
|
|
132
|
+
callIds: heldCalls.map((fc) => fc.callId),
|
|
133
|
+
signal: request.signal,
|
|
134
|
+
});
|
|
135
|
+
for (const fc of heldCalls.splice(0)) {
|
|
136
|
+
const toolForCall = findTool(allowedTools, fc.name);
|
|
137
|
+
const call = await executeFunctionCall(scope, {
|
|
138
|
+
fc,
|
|
139
|
+
request,
|
|
140
|
+
allowedTools,
|
|
141
|
+
allItems,
|
|
142
|
+
conversationInput,
|
|
143
|
+
emitIfAllowed: params.emitIfAllowed,
|
|
144
|
+
calls: params.calls,
|
|
145
|
+
announced: true,
|
|
146
|
+
});
|
|
147
|
+
if (call.held) {
|
|
148
|
+
heldCalls.push(fc);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
spans.recordToolSpan(modelSpan, fc.name, toolForCall?.needsApproval ?? false, call.approval);
|
|
152
|
+
}
|
|
91
153
|
}
|
|
92
154
|
params.emitIfAllowed('tool_round_completed', {
|
|
93
155
|
round: params.round,
|
|
@@ -96,10 +158,12 @@ async function executeToolRound(scope, params) {
|
|
|
96
158
|
}
|
|
97
159
|
async function executeFunctionCall(scope, params) {
|
|
98
160
|
const { fc, request, allowedTools, allItems, conversationInput } = params;
|
|
99
|
-
params.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
161
|
+
if (!params.announced) {
|
|
162
|
+
params.emitIfAllowed('tool_call_started', {
|
|
163
|
+
name: fc.name,
|
|
164
|
+
callId: fc.callId,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
103
167
|
let parsedArgs;
|
|
104
168
|
try {
|
|
105
169
|
parsedArgs = JSON.parse(fc.arguments);
|
|
@@ -114,25 +178,81 @@ async function executeFunctionCall(scope, params) {
|
|
|
114
178
|
conversationInput,
|
|
115
179
|
emitIfAllowed: params.emitIfAllowed,
|
|
116
180
|
});
|
|
117
|
-
return
|
|
181
|
+
return {};
|
|
118
182
|
}
|
|
119
183
|
if (!request.tools) {
|
|
120
184
|
throw new Error(`executeFunctionCall invoked without tools on CallModelRequest (tool name: ${fc.name}).`);
|
|
121
185
|
}
|
|
122
186
|
const toolForCall = findTool(allowedTools, fc.name);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
187
|
+
// An earlier attempt of this round may have finished this call: go on with what
|
|
188
|
+
// it produced instead of running the tool again.
|
|
189
|
+
// A durable tool's call marks its model step as having had a lasting effect.
|
|
190
|
+
if (toolForCall?.durable === true) {
|
|
191
|
+
// Under the round, which is what gets recorded: a note at the step's own path
|
|
192
|
+
// would mark nothing until the whole step finished.
|
|
193
|
+
params.calls?.noteEffect(fc);
|
|
194
|
+
}
|
|
195
|
+
// A tool that opts out of recording (`durable: false`) always runs again.
|
|
196
|
+
const keeps = toolForCall?.durable !== false;
|
|
197
|
+
const recorded = keeps ? params.calls?.take(fc) : undefined;
|
|
198
|
+
const toolResult = recorded ??
|
|
199
|
+
(await executeToolCall({
|
|
200
|
+
toolName: fc.name,
|
|
201
|
+
args: parsedArgs,
|
|
202
|
+
tools: allowedTools,
|
|
203
|
+
context: request.ctx,
|
|
204
|
+
harness: scope.harness,
|
|
205
|
+
layers: request.layers,
|
|
206
|
+
callId: fc.callId,
|
|
207
|
+
keyScope: params.calls?.scope,
|
|
208
|
+
}));
|
|
209
|
+
// Held: it has no result yet. Nothing goes into the conversation for it.
|
|
210
|
+
if (toolResult.held) {
|
|
211
|
+
if (!params.announced) {
|
|
212
|
+
params.emitIfAllowed('tool_call_held', {
|
|
213
|
+
...toolResult.held,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
held: true,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
// A replayed call appends the item it appended before, under the same id.
|
|
221
|
+
const itemId = recorded?.itemId ?? crypto.randomUUID();
|
|
222
|
+
const stored = recorded || !keeps
|
|
223
|
+
? undefined
|
|
224
|
+
: await params.calls?.record(fc, {
|
|
225
|
+
...toolResult,
|
|
226
|
+
itemId,
|
|
227
|
+
});
|
|
228
|
+
// The intent of a durable call is dropped once its result is on record. A result
|
|
229
|
+
// that could not be recorded keeps it, so a resume asks rather than runs again.
|
|
230
|
+
const writes = request.ctx !== undefined && mayWriteHolds(request.ctx);
|
|
231
|
+
if (toolResult.armed && stored !== false && writes) {
|
|
232
|
+
await heldCallsOf(scope.harness).disarm(heldScopeOf(request.ctx), fc.callId);
|
|
233
|
+
}
|
|
234
|
+
if (toolResult.settled && writes) {
|
|
235
|
+
await heldCallsOf(scope.harness).close(heldScopeOf(request.ctx), fc.callId);
|
|
236
|
+
}
|
|
237
|
+
if (recorded && request.ctx) {
|
|
238
|
+
// A host that died between recording the result and the two steps above left
|
|
239
|
+
// the call's intent or its answered hold behind. The record says it is done.
|
|
240
|
+
if (writes) {
|
|
241
|
+
await tidyFinishedCall(scope, request.ctx, fc.callId);
|
|
242
|
+
}
|
|
243
|
+
replayToolUi({
|
|
244
|
+
ctx: request.ctx,
|
|
245
|
+
tool: toolForCall,
|
|
246
|
+
callId: fc.callId,
|
|
247
|
+
args: parsedArgs,
|
|
248
|
+
result: recorded,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
132
251
|
// Owner-scoped result validation: a tool's `toolResults` schemas apply
|
|
133
252
|
// only to that tool's own result items. Harness-level `opts.itemSchemas`
|
|
134
253
|
// stay global by design; sibling tools' schemas never reject this item.
|
|
135
254
|
const outputItem = createToolResultItem({
|
|
255
|
+
id: itemId,
|
|
136
256
|
output: toolResult.output,
|
|
137
257
|
callId: fc.callId,
|
|
138
258
|
roundItemSchemas: scope.itemSchemas.extend(toolForCall?.itemSchemas),
|
|
@@ -146,7 +266,13 @@ async function executeFunctionCall(scope, params) {
|
|
|
146
266
|
conversationInput.push({
|
|
147
267
|
type: 'function_call_output',
|
|
148
268
|
callId: fc.callId,
|
|
149
|
-
output:
|
|
269
|
+
output: await modelOutputOf({
|
|
270
|
+
tool: toolForCall,
|
|
271
|
+
toolResult,
|
|
272
|
+
args: parsedArgs,
|
|
273
|
+
emitIfAllowed: params.emitIfAllowed,
|
|
274
|
+
callId: fc.callId,
|
|
275
|
+
}),
|
|
150
276
|
});
|
|
151
277
|
scope.emitItemAppended(request.ctx, outputItem);
|
|
152
278
|
params.emitIfAllowed('tool_call_completed', {
|
|
@@ -154,8 +280,109 @@ async function executeFunctionCall(scope, params) {
|
|
|
154
280
|
callId: fc.callId,
|
|
155
281
|
error: toolResult.error === true,
|
|
156
282
|
output: toolResult.output,
|
|
283
|
+
...(recorded
|
|
284
|
+
? {
|
|
285
|
+
replayed: true,
|
|
286
|
+
}
|
|
287
|
+
: {}),
|
|
157
288
|
});
|
|
158
|
-
return
|
|
289
|
+
return {
|
|
290
|
+
approval: toolResult.approval,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* What the model is given as a call's result: the tool's `toModelOutput` value for
|
|
295
|
+
* a call that did not fail, else the output text. The result item, the completed
|
|
296
|
+
* event and the call's record keep the text whatever this returns, so a replayed
|
|
297
|
+
* call lands here again with its recorded result.
|
|
298
|
+
*
|
|
299
|
+
* Live and on replay alike, `toModelOutput` reads the result as its JSON round
|
|
300
|
+
* trip, which is what a call record keeps, and the arguments as the tool's input
|
|
301
|
+
* schema reads them, defaults included: a replay must give the model what the
|
|
302
|
+
* live call gave it.
|
|
303
|
+
*/
|
|
304
|
+
async function modelOutputOf(params) {
|
|
305
|
+
const { tool, toolResult } = params;
|
|
306
|
+
if (toolResult.error || !tool?.toModelOutput) {
|
|
307
|
+
return toolResult.output;
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const args = await validateSchema(tool.input, params.args);
|
|
311
|
+
if (!args.success) {
|
|
312
|
+
throw args.zodError;
|
|
313
|
+
}
|
|
314
|
+
return toolModelOutputToProvider(tool.toModelOutput(asRecorded(toolResult.result), args.value));
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
// The call is done and on record: a throw here would fail it again on every
|
|
318
|
+
// resume. The model gets the JSON output, and the event says why.
|
|
319
|
+
params.emitIfAllowed('tool_model_output_failed', {
|
|
320
|
+
name: tool.name,
|
|
321
|
+
callId: params.callId,
|
|
322
|
+
error: error instanceof Error ? error.message : String(error),
|
|
323
|
+
});
|
|
324
|
+
return toolResult.output;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** `result` as a call record gives it back: its JSON round trip. */
|
|
328
|
+
function asRecorded(result) {
|
|
329
|
+
const json = result === undefined ? undefined : JSON.stringify(result);
|
|
330
|
+
return json === undefined ? result : JSON.parse(json);
|
|
331
|
+
}
|
|
332
|
+
/** Drop what a finished call may have left in storage: its intent, its answered hold. */
|
|
333
|
+
async function tidyFinishedCall(scope, ctx, callId) {
|
|
334
|
+
const holds = heldCallsOf(scope.harness);
|
|
335
|
+
const executionId = heldScopeOf(ctx);
|
|
336
|
+
await holds.disarm(executionId, callId);
|
|
337
|
+
if (await holds.load(executionId, callId)) {
|
|
338
|
+
await holds.close(executionId, callId);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Show a replayed call in the tool's UI as it was shown the first time: the call,
|
|
343
|
+
* then how it ended. Nothing for a call whose body never ran.
|
|
344
|
+
*/
|
|
345
|
+
function replayToolUi(params) {
|
|
346
|
+
const { tool, result } = params;
|
|
347
|
+
if (!tool || !result.ran) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const base = {
|
|
351
|
+
ctx: params.ctx,
|
|
352
|
+
tool,
|
|
353
|
+
callId: params.callId,
|
|
354
|
+
args: params.args,
|
|
355
|
+
};
|
|
356
|
+
emitToolUi({
|
|
357
|
+
...base,
|
|
358
|
+
phase: 'call',
|
|
359
|
+
});
|
|
360
|
+
if (result.error) {
|
|
361
|
+
emitToolUi({
|
|
362
|
+
...base,
|
|
363
|
+
phase: 'error',
|
|
364
|
+
error: new Error(result.output.replace(/^Error: /, '')),
|
|
365
|
+
});
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
emitToolUi({
|
|
369
|
+
...base,
|
|
370
|
+
phase: 'result',
|
|
371
|
+
output: result.result,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
/** Drop the records of calls that are being skipped, so none stays listed as held. */
|
|
375
|
+
async function dropHolds(scope, request, calls) {
|
|
376
|
+
if (!request.ctx || !mayWriteHolds(request.ctx)) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const holds = heldCallsOf(scope.harness);
|
|
380
|
+
const executionId = heldScopeOf(request.ctx);
|
|
381
|
+
for (const call of calls) {
|
|
382
|
+
if (await holds.load(executionId, call.callId)) {
|
|
383
|
+
await holds.close(executionId, call.callId);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
159
386
|
}
|
|
160
387
|
function recordSkippedToolCall(scope, params) {
|
|
161
388
|
params.emitIfAllowed('tool_call_started', {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-wake: a live harness watching over the turns it parked
|
|
3
|
+
* (spec 23 § Parked turns).
|
|
4
|
+
*
|
|
5
|
+
* Parking closes a turn's own listener, so without this a parked turn in a
|
|
6
|
+
* long-lived process would sit until something outside called `wake()`. A
|
|
7
|
+
* watcher stands in for the turn: a timer for its deadline, and a listener on
|
|
8
|
+
* each event it waits for, from the cursor the wait recorded. Either one wakes
|
|
9
|
+
* the turn.
|
|
10
|
+
*
|
|
11
|
+
* A watcher knows the event's type, not the wait's `filter`, which is code in
|
|
12
|
+
* the flow. So it wakes on any event of that type, and a turn the filter turns
|
|
13
|
+
* down parks again. It remembers the last cursor it saw, so each event wakes
|
|
14
|
+
* the turn at most once, and a burst of events wakes it once.
|
|
15
|
+
*/
|
|
16
|
+
import type { ConnectionEventInit, EventStream, ExecuteOptions } from '@noetic-tools/types';
|
|
17
|
+
/** An event a parked turn waits for. */
|
|
18
|
+
export interface ParkedWait {
|
|
19
|
+
readonly connection: string;
|
|
20
|
+
readonly event: string;
|
|
21
|
+
readonly cursor: string | null;
|
|
22
|
+
}
|
|
23
|
+
/** A long operation a parked turn's call waits on. */
|
|
24
|
+
export interface ParkedTask {
|
|
25
|
+
readonly connection: string;
|
|
26
|
+
readonly taskId: string;
|
|
27
|
+
}
|
|
28
|
+
/** What the watchers need of their harness. */
|
|
29
|
+
export interface ParkWatcherHost {
|
|
30
|
+
/** Listen on a harness connection. */
|
|
31
|
+
listen(connection: string, request: {
|
|
32
|
+
event: string;
|
|
33
|
+
cursor: string | null;
|
|
34
|
+
signal: AbortSignal;
|
|
35
|
+
}): Promise<EventStream<ConnectionEventInit>>;
|
|
36
|
+
/** The events the parked execution waits for. */
|
|
37
|
+
waits(executionId: string): Promise<ParkedWait[]>;
|
|
38
|
+
/** The long operations the parked execution's calls wait on. */
|
|
39
|
+
tasks(executionId: string): Promise<ParkedTask[]>;
|
|
40
|
+
/** The state of one of them now, or undefined when it cannot be read. */
|
|
41
|
+
taskState(task: ParkedTask, signal: AbortSignal): Promise<string | undefined>;
|
|
42
|
+
/** Resume the thread's turn. Resolves false when it could not be resumed just now. */
|
|
43
|
+
wake(threadId: string, options: ExecuteOptions): Promise<boolean>;
|
|
44
|
+
/** The execution of the turn the thread has parked now, or undefined when it has none. */
|
|
45
|
+
parked(threadId: string): Promise<string | undefined>;
|
|
46
|
+
now(): number;
|
|
47
|
+
}
|
|
48
|
+
/** What is known about one parked turn. */
|
|
49
|
+
export interface ParkedTurn {
|
|
50
|
+
readonly executionId: string;
|
|
51
|
+
/** When its earliest wait runs out, or undefined when none has a deadline. */
|
|
52
|
+
readonly until: number | undefined;
|
|
53
|
+
/** The `execute()` options the turn ran with, to resume it with. */
|
|
54
|
+
readonly options: ExecuteOptions;
|
|
55
|
+
}
|
|
56
|
+
/** @public How long auto-wake waits after an event before waking a turn, so a burst wakes it once. */
|
|
57
|
+
export declare const DEFAULT_WAKE_DEBOUNCE_MS = 250;
|
|
58
|
+
/** @public How often auto-wake reads the state of a long operation a parked turn waits on. */
|
|
59
|
+
export declare const DEFAULT_TASK_POLL_MS = 5000;
|
|
60
|
+
/** One harness's watchers, by thread. */
|
|
61
|
+
export declare class ParkWatchers {
|
|
62
|
+
private readonly host;
|
|
63
|
+
private readonly debounceMs;
|
|
64
|
+
private readonly taskPollMs;
|
|
65
|
+
private readonly watchers;
|
|
66
|
+
/** The last cursor seen for a wait, so a turn that parks again does not see it twice. */
|
|
67
|
+
private readonly seen;
|
|
68
|
+
/** The harness closed: a wake still being tried is given up. */
|
|
69
|
+
private closed;
|
|
70
|
+
constructor(host: ParkWatcherHost, debounceMs: number, taskPollMs: number);
|
|
71
|
+
/** True when the thread's parked turn is being watched. */
|
|
72
|
+
has(threadId: string): boolean;
|
|
73
|
+
/** Watch over a turn that just parked, or that storage says is parked. */
|
|
74
|
+
watch(threadId: string, turn: ParkedTurn): void;
|
|
75
|
+
/** Stop watching a thread: its turn was woken, or is being. */
|
|
76
|
+
stop(threadId: string): void;
|
|
77
|
+
/** The turn ended: drop what was remembered about its waits. */
|
|
78
|
+
forget(threadId: string, executionId: string): void;
|
|
79
|
+
stopAll(): void;
|
|
80
|
+
private armDeadline;
|
|
81
|
+
private listenForWaits;
|
|
82
|
+
/** Listen for one awaited event; the first one seen wakes the turn, after the debounce. */
|
|
83
|
+
private listenFor;
|
|
84
|
+
/**
|
|
85
|
+
* A long operation has no event to listen for, so its state is read every so
|
|
86
|
+
* often. The turn is woken when the state is no longer the one it parked on:
|
|
87
|
+
* finished, failed, or asking a question. A state it already woke for does not
|
|
88
|
+
* wake it again, or a turn waiting on a person's answer would never rest.
|
|
89
|
+
*/
|
|
90
|
+
private pollTasks;
|
|
91
|
+
/** Wake the turn. The runner may still be closing the parked turn out, so try for a while. */
|
|
92
|
+
private fire;
|
|
93
|
+
}
|