@noetic-tools/core 5.20.0 → 5.22.0-next.1
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/builders/tool-builder.d.ts +20 -0
- package/dist/builders/tool-builder.js +23 -1
- 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 +263 -4
- package/dist/harness/agent-harness.js +1156 -95
- package/dist/harness/connection-gate.d.ts +3 -0
- package/dist/harness/connection-gate.js +4 -0
- 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 +82 -0
- package/dist/harness/model-call.d.ts +0 -1
- package/dist/harness/model-call.js +8 -48
- 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 +201 -19
- 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 +17 -7
- package/dist/index.js +10 -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-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/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/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/session-runner.d.ts +18 -2
- package/dist/runtime/session-runner.js +88 -3
- 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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isNoeticError } from '@noetic-tools/types';
|
|
2
2
|
import { emitFrameworkEvent } from './broadcaster-utils.js';
|
|
3
|
+
import { ContextImpl } from './context-impl.js';
|
|
3
4
|
import { EventBroadcaster } from './event-broadcaster.js';
|
|
4
5
|
import { inputToItems, isSteer, MessageQueue } from './message-queue.js';
|
|
5
6
|
//#endregion
|
|
@@ -48,9 +49,12 @@ export class SessionRunner {
|
|
|
48
49
|
totalCost = 0;
|
|
49
50
|
totalBilledCost = 0;
|
|
50
51
|
onTurnUsage;
|
|
52
|
+
heldCalls;
|
|
51
53
|
currentController;
|
|
52
54
|
currentCtx;
|
|
53
55
|
loopPromise;
|
|
56
|
+
/** The message ids of the turn being started or run, set before anything is awaited. */
|
|
57
|
+
currentIds = [];
|
|
54
58
|
lastResponse;
|
|
55
59
|
lastError;
|
|
56
60
|
responseWaiters = [];
|
|
@@ -60,6 +64,7 @@ export class SessionRunner {
|
|
|
60
64
|
this.runTurn = opts.runTurn;
|
|
61
65
|
this.createContext = opts.createContext;
|
|
62
66
|
this.onTurnUsage = opts.onTurnUsage;
|
|
67
|
+
this.heldCalls = opts.heldCalls;
|
|
63
68
|
this.queue.subscribe(() => {
|
|
64
69
|
this.kick();
|
|
65
70
|
});
|
|
@@ -88,6 +93,16 @@ export class SessionRunner {
|
|
|
88
93
|
}
|
|
89
94
|
this.queue.enqueue(message);
|
|
90
95
|
}
|
|
96
|
+
/** True while the turn being started or run took the message with this id. */
|
|
97
|
+
isRunning(messageId) {
|
|
98
|
+
return this.status.kind !== 'idle' && this.currentIds.includes(messageId);
|
|
99
|
+
}
|
|
100
|
+
/** Resolves once the queue loop has gone idle: no turn running or closing out. */
|
|
101
|
+
async whenIdle() {
|
|
102
|
+
while (this.loopPromise) {
|
|
103
|
+
await this.loopPromise.catch(() => undefined);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
91
106
|
/** Cumulative token usage and cost across all turns this session has run. */
|
|
92
107
|
getUsage() {
|
|
93
108
|
return {
|
|
@@ -127,6 +142,31 @@ export class SessionRunner {
|
|
|
127
142
|
});
|
|
128
143
|
});
|
|
129
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Answer whoever is waiting with a response no turn of this runner produced: the
|
|
147
|
+
* recorded answer to a message that was delivered again.
|
|
148
|
+
*/
|
|
149
|
+
answerWith(response, messageId) {
|
|
150
|
+
this.lastResponse = response;
|
|
151
|
+
this.lastError = undefined;
|
|
152
|
+
emitFrameworkEvent({
|
|
153
|
+
broadcaster: this.broadcaster,
|
|
154
|
+
agentName: this.agentName,
|
|
155
|
+
eventType: 'turn_replayed',
|
|
156
|
+
data: {
|
|
157
|
+
messageIds: [
|
|
158
|
+
messageId,
|
|
159
|
+
],
|
|
160
|
+
// Nothing streams for a replayed turn: its answer is here for those who post it.
|
|
161
|
+
text: response.text,
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
this.resolveWaiters(response);
|
|
165
|
+
}
|
|
166
|
+
/** The abort signal of the turn in flight, from before its context is built. */
|
|
167
|
+
get signal() {
|
|
168
|
+
return this.currentController?.signal;
|
|
169
|
+
}
|
|
130
170
|
/** Cancel the in-flight turn. Does NOT re-kick — callers that want restart
|
|
131
171
|
* behaviour should enqueue a message before/after calling abort. */
|
|
132
172
|
async abort(reason) {
|
|
@@ -176,6 +216,7 @@ export class SessionRunner {
|
|
|
176
216
|
async runOneTurn(messages) {
|
|
177
217
|
const turnId = crypto.randomUUID();
|
|
178
218
|
const startedAt = Date.now();
|
|
219
|
+
this.currentIds = messages.map((message) => message.id);
|
|
179
220
|
this.status = {
|
|
180
221
|
kind: 'generating',
|
|
181
222
|
startedAt,
|
|
@@ -189,7 +230,20 @@ export class SessionRunner {
|
|
|
189
230
|
const items = mergeInputsToItems(messages);
|
|
190
231
|
let ctx;
|
|
191
232
|
try {
|
|
192
|
-
|
|
233
|
+
const built = await this.createContext(items, turnId, messages);
|
|
234
|
+
if (built === undefined) {
|
|
235
|
+
// The host set these messages aside (the thread's turn is parked or stuck):
|
|
236
|
+
// no turn starts, and whoever is waiting keeps the answer they had.
|
|
237
|
+
this.currentController = undefined;
|
|
238
|
+
this.status = {
|
|
239
|
+
kind: 'idle',
|
|
240
|
+
};
|
|
241
|
+
if (this.lastResponse) {
|
|
242
|
+
this.resolveWaiters(this.lastResponse);
|
|
243
|
+
}
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
ctx = built;
|
|
193
247
|
}
|
|
194
248
|
catch (err) {
|
|
195
249
|
this.failBeforeStart(turnId, err);
|
|
@@ -227,16 +281,38 @@ export class SessionRunner {
|
|
|
227
281
|
try {
|
|
228
282
|
const text = await this.runTurn(ctx, turn, controller.signal);
|
|
229
283
|
await this.settleUsage(start, ctx);
|
|
230
|
-
const
|
|
284
|
+
const wakeAt = parkedUntilOf(ctx);
|
|
285
|
+
const held = wakeAt === undefined ? [] : ((await this.heldCalls?.(ctx)) ?? []);
|
|
286
|
+
const response = wakeAt === undefined
|
|
287
|
+
? buildResponse(text, ctx)
|
|
288
|
+
: {
|
|
289
|
+
...buildResponse(text, ctx),
|
|
290
|
+
...(held.length > 0
|
|
291
|
+
? {
|
|
292
|
+
held,
|
|
293
|
+
}
|
|
294
|
+
: {}),
|
|
295
|
+
// A turn parked on a held call alone has no time to wake at.
|
|
296
|
+
parked: Number.isFinite(wakeAt)
|
|
297
|
+
? {
|
|
298
|
+
wakeAt,
|
|
299
|
+
}
|
|
300
|
+
: {},
|
|
301
|
+
};
|
|
231
302
|
this.lastResponse = response;
|
|
232
303
|
this.lastError = undefined;
|
|
233
304
|
emitFrameworkEvent({
|
|
234
305
|
broadcaster: this.broadcaster,
|
|
235
306
|
agentName: this.agentName,
|
|
236
|
-
eventType: 'turn_completed',
|
|
307
|
+
eventType: wakeAt === undefined ? 'turn_completed' : 'turn_parked',
|
|
237
308
|
data: {
|
|
238
309
|
turnId,
|
|
239
310
|
durationMs: Date.now() - startedAt,
|
|
311
|
+
...(wakeAt === undefined
|
|
312
|
+
? {}
|
|
313
|
+
: {
|
|
314
|
+
wakeAt,
|
|
315
|
+
}),
|
|
240
316
|
},
|
|
241
317
|
});
|
|
242
318
|
// Only resolve waiters when the queue is fully drained; otherwise let
|
|
@@ -252,6 +328,11 @@ export class SessionRunner {
|
|
|
252
328
|
await this.settleUsage(start, ctx);
|
|
253
329
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
254
330
|
this.lastError = error;
|
|
331
|
+
// A turn that parked and then failed once woken did not end parked: stop
|
|
332
|
+
// answering with the parked response.
|
|
333
|
+
if (this.lastResponse?.parked) {
|
|
334
|
+
this.lastResponse = undefined;
|
|
335
|
+
}
|
|
255
336
|
emitFrameworkEvent({
|
|
256
337
|
broadcaster: this.broadcaster,
|
|
257
338
|
agentName: this.agentName,
|
|
@@ -345,6 +426,10 @@ function usageSince(start, ctx) {
|
|
|
345
426
|
billedCost: now.billedCost - start.billedCost,
|
|
346
427
|
};
|
|
347
428
|
}
|
|
429
|
+
/** When the turn on `ctx` parked until, or undefined when it ran to its end. */
|
|
430
|
+
function parkedUntilOf(ctx) {
|
|
431
|
+
return ctx instanceof ContextImpl ? ctx.ledger?.parking.parkedUntil : undefined;
|
|
432
|
+
}
|
|
348
433
|
function buildResponse(text, ctx) {
|
|
349
434
|
return {
|
|
350
435
|
items: ctx.itemLog.items,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context, HeldCall, SteeringDecision } from '@noetic-tools/types';
|
|
2
|
+
import type { HoldHost } from './held-calls.js';
|
|
2
3
|
/** How the approval stage settled a call, as recorded on its `tool.call` span. */
|
|
3
4
|
export declare const ApprovalOutcome: {
|
|
4
5
|
readonly Approved: "approved";
|
|
5
6
|
readonly Denied: "denied";
|
|
6
|
-
/**
|
|
7
|
+
/** A call with no id ran unapproved, because no approver is set and it cannot be held. */
|
|
7
8
|
readonly NoneConfigured: "none_configured";
|
|
8
9
|
};
|
|
9
10
|
export type ApprovalOutcome = (typeof ApprovalOutcome)[keyof typeof ApprovalOutcome];
|
|
@@ -12,14 +13,53 @@ export interface ToolApproval {
|
|
|
12
13
|
decision: SteeringDecision;
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
|
-
* Ask
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* Ask whether a call that needs approval may run (spec 08 § Held tool calls).
|
|
17
|
+
*
|
|
18
|
+
* A call already held under `callId` answers from its record: its resolution, a
|
|
19
|
+
* denial once its deadline has passed, or another hold. Otherwise the harness
|
|
20
|
+
* approver decides. With no approver the call is held, so a person decides.
|
|
21
|
+
*
|
|
22
|
+
* An approver that throws denies the call.
|
|
23
|
+
*
|
|
24
|
+
* @throws HoldSignal when the call is to be held. The caller writes the record.
|
|
18
25
|
*/
|
|
19
26
|
export declare function askApproval(params: {
|
|
20
|
-
harness:
|
|
27
|
+
harness: HoldHost;
|
|
21
28
|
toolName: string;
|
|
22
29
|
toolArgs: unknown;
|
|
23
30
|
callId?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The id the call's held record is under, when it is not `callId`. The approver
|
|
33
|
+
* is still shown `callId`, the id the call goes by everywhere else.
|
|
34
|
+
*/
|
|
35
|
+
recordId?: string;
|
|
24
36
|
ctx: Context;
|
|
37
|
+
/**
|
|
38
|
+
* The caller already matched the held record to this call, so `toolArgs` need not
|
|
39
|
+
* hash the same. Set by a seam that sees the arguments in another shape.
|
|
40
|
+
*/
|
|
41
|
+
matched?: boolean;
|
|
42
|
+
}): Promise<ToolApproval>;
|
|
43
|
+
/**
|
|
44
|
+
* `askApproval` for a caller with no round to come back to (an `invokeTool` step):
|
|
45
|
+
* a call that is held waits here until it is answered, and the turn may park
|
|
46
|
+
* meanwhile. Resolves the settled approval.
|
|
47
|
+
*
|
|
48
|
+
* @throws NoeticError `cancelled` when the context is aborted while it waits.
|
|
49
|
+
*/
|
|
50
|
+
export declare function awaitApproval(params: {
|
|
51
|
+
harness: HoldHost;
|
|
52
|
+
toolName: string;
|
|
53
|
+
toolArgs: unknown;
|
|
54
|
+
callId: string;
|
|
55
|
+
/**
|
|
56
|
+
* The id the call's held record is under, when it is not `callId`: one that is
|
|
57
|
+
* unique to this call in the run. The approver is still shown `callId`.
|
|
58
|
+
*/
|
|
59
|
+
recordId?: string;
|
|
60
|
+
ctx: Context;
|
|
61
|
+
/** Called once, when the call is first held. */
|
|
62
|
+
onHeld?: (held: HeldCall) => void;
|
|
63
|
+
/** False when the turn must stay live while the call is held. Default true. */
|
|
64
|
+
park?: boolean;
|
|
25
65
|
}): Promise<ToolApproval>;
|
|
@@ -1,67 +1,189 @@
|
|
|
1
1
|
import { SteeringAction } from '@noetic-tools/types';
|
|
2
|
+
import { announcerOf, HoldSignal, heldArgsHash, heldCallsOf, heldScopeOf, isOverdue, mayWriteHolds, toHeldCall, waitOnHeldCalls, } from './held-calls.js';
|
|
2
3
|
//#region Types
|
|
3
4
|
/** How the approval stage settled a call, as recorded on its `tool.call` span. */
|
|
4
5
|
export const ApprovalOutcome = {
|
|
5
6
|
Approved: 'approved',
|
|
6
7
|
Denied: 'denied',
|
|
7
|
-
/**
|
|
8
|
+
/** A call with no id ran unapproved, because no approver is set and it cannot be held. */
|
|
8
9
|
NoneConfigured: 'none_configured',
|
|
9
10
|
};
|
|
11
|
+
const warnedHeld = new WeakMap();
|
|
12
|
+
function warnHeldUnasked(harness, toolName) {
|
|
13
|
+
const names = warnedHeld.get(harness) ?? new Set();
|
|
14
|
+
warnedHeld.set(harness, names);
|
|
15
|
+
if (names.has(toolName)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
names.add(toolName);
|
|
19
|
+
console.warn(`[noetic] tool '${toolName}' needs approval and harness '${harness.config.name}' has no approver, so its calls are held until someone answers. List them with harness.pending() and answer with a decision item passed to harness.execute(), or set AgentHarness({ approveToolCall }) to ask as each call is made.`);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region Helpers
|
|
23
|
+
function approvalOf(decision) {
|
|
24
|
+
return {
|
|
25
|
+
outcome: decision.action === SteeringAction.Allow ? ApprovalOutcome.Approved : ApprovalOutcome.Denied,
|
|
26
|
+
decision,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function denied(guidance) {
|
|
30
|
+
return approvalOf({
|
|
31
|
+
action: SteeringAction.Deny,
|
|
32
|
+
guidance,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** What a held approval settled as, or undefined while it is still open. */
|
|
36
|
+
function settledHold(held, now) {
|
|
37
|
+
const action = held.resolution?.action;
|
|
38
|
+
if (action !== undefined) {
|
|
39
|
+
return approvalOf({
|
|
40
|
+
action,
|
|
41
|
+
guidance: held.resolution?.guidance,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (isOverdue(held, now)) {
|
|
45
|
+
return denied(`nobody approved '${held.name}' before the hold ran out`);
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const warned = new WeakMap();
|
|
50
|
+
function warnOnce(harness, toolName) {
|
|
51
|
+
let names = warned.get(harness);
|
|
52
|
+
if (!names) {
|
|
53
|
+
names = new Set();
|
|
54
|
+
warned.set(harness, names);
|
|
55
|
+
}
|
|
56
|
+
if (names.has(toolName)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
names.add(toolName);
|
|
60
|
+
console.warn(`[noetic] tool '${toolName}' needs approval, but harness '${harness.config.name}' has no approver and the call has no id to hold it under, so it runs unapproved. Set AgentHarness({ approveToolCall }) to approve or refuse it.`);
|
|
61
|
+
}
|
|
10
62
|
//#endregion
|
|
11
63
|
//#region Public API
|
|
12
64
|
/**
|
|
13
|
-
* Ask
|
|
14
|
-
*
|
|
15
|
-
*
|
|
65
|
+
* Ask whether a call that needs approval may run (spec 08 § Held tool calls).
|
|
66
|
+
*
|
|
67
|
+
* A call already held under `callId` answers from its record: its resolution, a
|
|
68
|
+
* denial once its deadline has passed, or another hold. Otherwise the harness
|
|
69
|
+
* approver decides. With no approver the call is held, so a person decides.
|
|
70
|
+
*
|
|
71
|
+
* An approver that throws denies the call.
|
|
72
|
+
*
|
|
73
|
+
* @throws HoldSignal when the call is to be held. The caller writes the record.
|
|
16
74
|
*/
|
|
17
75
|
export async function askApproval(params) {
|
|
76
|
+
const holds = heldCallsOf(params.harness);
|
|
77
|
+
const recordId = params.recordId ?? params.callId;
|
|
78
|
+
const held = recordId === undefined ? undefined : await holds.load(heldScopeOf(params.ctx), recordId);
|
|
79
|
+
const sameCall = held?.reason === 'approval' &&
|
|
80
|
+
(params.matched === true || held.argsHash === heldArgsHash(params.toolName, params.toolArgs));
|
|
81
|
+
if (held && sameCall) {
|
|
82
|
+
const settled = settledHold(held, holds.now());
|
|
83
|
+
if (settled) {
|
|
84
|
+
return settled;
|
|
85
|
+
}
|
|
86
|
+
throw new HoldSignal({
|
|
87
|
+
dueAt: held.dueAt,
|
|
88
|
+
onDue: held.onDue,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
18
91
|
const approver = params.harness.config.approveToolCall;
|
|
19
92
|
if (!approver) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
93
|
+
if (params.callId === undefined) {
|
|
94
|
+
// Nobody to ask and no id to hold it under. Approval is not something a
|
|
95
|
+
// call gets by default: it is refused.
|
|
96
|
+
warnOnce(params.harness, params.toolName);
|
|
97
|
+
return denied(`'${params.toolName}' needs approval, and there is no approver and no call id to hold it under`);
|
|
98
|
+
}
|
|
99
|
+
// Held, and nothing will ask anyone: a host that expected a prompt sees a
|
|
100
|
+
// call that never finishes. Say where the call is and how it is answered.
|
|
101
|
+
warnHeldUnasked(params.harness, params.toolName);
|
|
102
|
+
throw new HoldSignal();
|
|
27
103
|
}
|
|
104
|
+
let decision;
|
|
28
105
|
try {
|
|
29
|
-
|
|
106
|
+
decision = await approver({
|
|
30
107
|
toolName: params.toolName,
|
|
31
108
|
toolArgs: params.toolArgs,
|
|
32
109
|
callId: params.callId,
|
|
33
110
|
ctx: params.ctx,
|
|
34
111
|
});
|
|
35
|
-
return {
|
|
36
|
-
outcome: decision.action === SteeringAction.Allow
|
|
37
|
-
? ApprovalOutcome.Approved
|
|
38
|
-
: ApprovalOutcome.Denied,
|
|
39
|
-
decision,
|
|
40
|
-
};
|
|
41
112
|
}
|
|
42
113
|
catch (e) {
|
|
43
|
-
return {
|
|
44
|
-
outcome: ApprovalOutcome.Denied,
|
|
45
|
-
decision: {
|
|
46
|
-
action: SteeringAction.Deny,
|
|
47
|
-
guidance: `approval for tool '${params.toolName}' failed: ${e instanceof Error ? e.message : String(e)}`,
|
|
48
|
-
},
|
|
49
|
-
};
|
|
114
|
+
return denied(`approval for tool '${params.toolName}' failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
50
115
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
//#region Helpers
|
|
54
|
-
const warned = new WeakMap();
|
|
55
|
-
function warnOnce(harness, toolName) {
|
|
56
|
-
let names = warned.get(harness);
|
|
57
|
-
if (!names) {
|
|
58
|
-
names = new Set();
|
|
59
|
-
warned.set(harness, names);
|
|
116
|
+
if (decision.action !== SteeringAction.Hold) {
|
|
117
|
+
return approvalOf(decision);
|
|
60
118
|
}
|
|
61
|
-
if (
|
|
62
|
-
return;
|
|
119
|
+
if (params.callId === undefined) {
|
|
120
|
+
return denied(`'${params.toolName}' cannot be held: the call has no id`);
|
|
121
|
+
}
|
|
122
|
+
throw new HoldSignal({
|
|
123
|
+
dueAt: decision.dueAt,
|
|
124
|
+
onDue: decision.onDue,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* `askApproval` for a caller with no round to come back to (an `invokeTool` step):
|
|
129
|
+
* a call that is held waits here until it is answered, and the turn may park
|
|
130
|
+
* meanwhile. Resolves the settled approval.
|
|
131
|
+
*
|
|
132
|
+
* @throws NoeticError `cancelled` when the context is aborted while it waits.
|
|
133
|
+
*/
|
|
134
|
+
export async function awaitApproval(params) {
|
|
135
|
+
const holds = heldCallsOf(params.harness);
|
|
136
|
+
const scope = heldScopeOf(params.ctx);
|
|
137
|
+
const heldAs = params.recordId ?? params.callId;
|
|
138
|
+
let announced = false;
|
|
139
|
+
for (;;) {
|
|
140
|
+
try {
|
|
141
|
+
const approval = await askApproval(params);
|
|
142
|
+
// Whether this process saw the call held or a resumed one found it answered,
|
|
143
|
+
// the answer is spent here. Left in storage, it would approve the next ask
|
|
144
|
+
// under this id with nobody asked.
|
|
145
|
+
if (mayWriteHolds(params.ctx)) {
|
|
146
|
+
await holds.close(scope, heldAs);
|
|
147
|
+
}
|
|
148
|
+
return approval;
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
if (!(e instanceof HoldSignal)) {
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
const open = mayWriteHolds(params.ctx)
|
|
155
|
+
? (record) => holds.open(scope, record)
|
|
156
|
+
: async (record) => record;
|
|
157
|
+
const record = await open({
|
|
158
|
+
callId: heldAs,
|
|
159
|
+
name: params.toolName,
|
|
160
|
+
args: params.toolArgs,
|
|
161
|
+
argsHash: heldArgsHash(params.toolName, params.toolArgs),
|
|
162
|
+
reason: 'approval',
|
|
163
|
+
...(e.due.dueAt === undefined
|
|
164
|
+
? {}
|
|
165
|
+
: {
|
|
166
|
+
dueAt: e.due.dueAt,
|
|
167
|
+
}),
|
|
168
|
+
...(e.due.onDue === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: {
|
|
171
|
+
onDue: e.due.onDue,
|
|
172
|
+
}),
|
|
173
|
+
});
|
|
174
|
+
if (!announced) {
|
|
175
|
+
announced = true;
|
|
176
|
+
(params.onHeld ?? announcerOf(params.harness, params.ctx))(toHeldCall(record));
|
|
177
|
+
}
|
|
178
|
+
await waitOnHeldCalls({
|
|
179
|
+
harness: params.harness,
|
|
180
|
+
ctx: params.ctx,
|
|
181
|
+
callIds: [
|
|
182
|
+
heldAs,
|
|
183
|
+
],
|
|
184
|
+
park: params.park,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
63
187
|
}
|
|
64
|
-
names.add(toolName);
|
|
65
|
-
console.warn(`[noetic] tool '${toolName}' needs approval, but harness '${harness.config.name}' has no approver, so its calls run unapproved. Set AgentHarness({ approveToolCall }) to approve or refuse them.`);
|
|
66
188
|
}
|
|
67
189
|
//#endregion
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* tools over MCP. Keeping one copy is what makes the two behave the same.
|
|
5
5
|
*/
|
|
6
6
|
import type { ContextLayer } from '@noetic-tools/context';
|
|
7
|
-
import type { AgentHarnessContract, Context, Item, ItemSchemaRegistry, StandardSchemaV1, Tool } from '@noetic-tools/types';
|
|
7
|
+
import type { AgentHarnessContract, Context, HeldCall, Item, ItemSchemaRegistry, StandardSchemaV1, Tool, ToolHold } from '@noetic-tools/types';
|
|
8
8
|
import type { ApprovalOutcome } from './tool-approval.js';
|
|
9
9
|
/**
|
|
10
10
|
* Resolve the JSON Schema sent to the model. Zod keeps its legacy-compatible
|
|
@@ -31,6 +31,18 @@ export interface ExecuteToolCallParams {
|
|
|
31
31
|
* `ToolExecutionContext.callId`, a connection tool's idempotency key.
|
|
32
32
|
*/
|
|
33
33
|
callId?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The id the call's durable records go under: its held record, a `durable` tool's
|
|
36
|
+
* intent, its idempotency key. Defaults to `callId`. A caller whose `callId` does
|
|
37
|
+
* not survive a resume (an ACP agent makes the call again under a new id) gives
|
|
38
|
+
* one that does.
|
|
39
|
+
*/
|
|
40
|
+
recordId?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Where in the run the call was made: a model step's path and round. Goes into the
|
|
43
|
+
* idempotency key, so two calls a provider gave the same id do not share one.
|
|
44
|
+
*/
|
|
45
|
+
keyScope?: string;
|
|
34
46
|
}
|
|
35
47
|
/** @internal What one model-requested tool call produced. */
|
|
36
48
|
export interface ToolCallResult {
|
|
@@ -39,6 +51,22 @@ export interface ToolCallResult {
|
|
|
39
51
|
error?: boolean;
|
|
40
52
|
/** Set when the tool declares `needsApproval` and the call reached the approver. */
|
|
41
53
|
approval?: ApprovalOutcome;
|
|
54
|
+
/**
|
|
55
|
+
* Set when the call is held for a person (spec 08 § Held tool calls): it has no
|
|
56
|
+
* result yet, and `output` is empty.
|
|
57
|
+
*/
|
|
58
|
+
held?: HeldCall;
|
|
59
|
+
/** The call was held and is now settled: its record can be dropped. */
|
|
60
|
+
settled?: boolean;
|
|
61
|
+
/** The tool's body ran (and its UI showed the call), whether it returned or threw. */
|
|
62
|
+
ran?: boolean;
|
|
63
|
+
/** The id of the result item this call appended, kept so a replay appends the same. */
|
|
64
|
+
itemId?: string;
|
|
65
|
+
/**
|
|
66
|
+
* A `durable` tool ran under an intent record. The caller drops it once the
|
|
67
|
+
* result is on record; until then a resumed run treats the call as started.
|
|
68
|
+
*/
|
|
69
|
+
armed?: boolean;
|
|
42
70
|
}
|
|
43
71
|
/**
|
|
44
72
|
* @internal Execute a single tool call. Gates run in order: the
|
|
@@ -47,6 +75,8 @@ export interface ToolCallResult {
|
|
|
47
75
|
* error tool result; the tool never runs.
|
|
48
76
|
*/
|
|
49
77
|
export declare function executeToolCall(params: ExecuteToolCallParams): Promise<ToolCallResult>;
|
|
78
|
+
/** True for what `hold()` returns: a tool leaving its result open. */
|
|
79
|
+
export declare function isToolHold(value: unknown): value is ToolHold;
|
|
50
80
|
/**
|
|
51
81
|
* Build and validate a `function_call_output` item for a tool result.
|
|
52
82
|
*
|
|
@@ -55,7 +85,9 @@ export declare function executeToolCall(params: ExecuteToolCallParams): Promise<
|
|
|
55
85
|
* `toolResults` schemas never reject a sibling tool's result items. Tools
|
|
56
86
|
* without `itemSchemas` fall back to the base structural parse.
|
|
57
87
|
*/
|
|
58
|
-
export declare function createToolResultItem({ output, callId, roundItemSchemas, tool, callItem, args, result, error, }: {
|
|
88
|
+
export declare function createToolResultItem({ id, output, callId, roundItemSchemas, tool, callItem, args, result, error, }: {
|
|
89
|
+
/** The item's id, when it is one made before: a replayed call keeps its id. */
|
|
90
|
+
id?: string;
|
|
59
91
|
output: string;
|
|
60
92
|
callId: string;
|
|
61
93
|
roundItemSchemas: ItemSchemaRegistry;
|