@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
|
@@ -110,6 +110,40 @@ function completeStreamingItem(item) {
|
|
|
110
110
|
isComplete: true,
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
|
+
/** The id of the item an SDK event is about, when the event names one. */
|
|
114
|
+
function itemIdOf(event) {
|
|
115
|
+
const { itemId, item } = event.data;
|
|
116
|
+
if (typeof itemId === 'string') {
|
|
117
|
+
return itemId;
|
|
118
|
+
}
|
|
119
|
+
return isItemRecord(item) && typeof item.id === 'string' ? item.id : undefined;
|
|
120
|
+
}
|
|
121
|
+
/** Framework-authored snapshots: a custom item in progress, or an appended item. */
|
|
122
|
+
function* frameworkSnapshots(event) {
|
|
123
|
+
// A custom item's event streams in progress under the id its final item takes.
|
|
124
|
+
if (event.type.endsWith(':custom_item_event')) {
|
|
125
|
+
yield frameworkCast({
|
|
126
|
+
...event.data,
|
|
127
|
+
isComplete: false,
|
|
128
|
+
});
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (event.type.endsWith(':item_appended') &&
|
|
132
|
+
typeof event.data.item === 'object' &&
|
|
133
|
+
event.data.item !== null) {
|
|
134
|
+
yield {
|
|
135
|
+
...frameworkCast(event.data.item),
|
|
136
|
+
isComplete: true,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** How each delta event changes the item it is about. */
|
|
141
|
+
const ACCUMULATOR_UPDATES = {
|
|
142
|
+
'response.output_text.delta': updateMessageAccumulator,
|
|
143
|
+
'response.output_text.done': updateMessageAccumulator,
|
|
144
|
+
'response.function_call_arguments.delta': updateFunctionCallAccumulator,
|
|
145
|
+
'response.function_call_arguments.done': updateFunctionCallAccumulator,
|
|
146
|
+
};
|
|
113
147
|
//#endregion
|
|
114
148
|
//#region Public stream filters
|
|
115
149
|
/** @internal Yield text deltas from the session broadcaster. */
|
|
@@ -136,92 +170,103 @@ export async function* filterReasoningStream(broadcaster) {
|
|
|
136
170
|
}
|
|
137
171
|
}
|
|
138
172
|
}
|
|
139
|
-
/**
|
|
140
|
-
*
|
|
141
|
-
* multiple turns within a session don't collide.
|
|
173
|
+
/**
|
|
174
|
+
* @internal Yield cumulative StreamingItem snapshots.
|
|
142
175
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
176
|
+
* SDK events cover model output only; framework-authored items — turn input
|
|
177
|
+
* messages, between-rounds injections, tool results — arrive via the
|
|
178
|
+
* `item_appended` framework event and are yielded already complete.
|
|
179
|
+
*/
|
|
146
180
|
export async function* buildItemStream(broadcaster, itemSchemas = defaultItemSchemaRegistry) {
|
|
147
|
-
const
|
|
148
|
-
let roundOffset = 0;
|
|
181
|
+
const stream = new ItemStream(itemSchemas);
|
|
149
182
|
for await (const event of broadcaster) {
|
|
183
|
+
yield* stream.push(event);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region ItemStream
|
|
188
|
+
/**
|
|
189
|
+
* @internal Folds stream events into item snapshots, one event at a time: what
|
|
190
|
+
* `getItemStream` yields and what an `OutputStore` keeps.
|
|
191
|
+
*
|
|
192
|
+
* An event that names its item is matched by that id, which keeps model calls
|
|
193
|
+
* running at once apart. One that does not is matched by round and output index,
|
|
194
|
+
* the round counted from `response.created`, so the items of later tool rounds
|
|
195
|
+
* and later turns do not collide.
|
|
196
|
+
*/
|
|
197
|
+
export class ItemStream {
|
|
198
|
+
accumulators = new Map();
|
|
199
|
+
byId = new Map();
|
|
200
|
+
roundOffset = 0;
|
|
201
|
+
itemSchemas;
|
|
202
|
+
constructor(itemSchemas = defaultItemSchemaRegistry) {
|
|
203
|
+
this.itemSchemas = itemSchemas;
|
|
204
|
+
}
|
|
205
|
+
/** The snapshots `event` produces: none, or one. */
|
|
206
|
+
*push(event) {
|
|
150
207
|
if (event.source === 'framework') {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
yield frameworkCast({
|
|
154
|
-
...event.data,
|
|
155
|
-
isComplete: false,
|
|
156
|
-
});
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
if (event.type.endsWith(':item_appended') &&
|
|
160
|
-
typeof event.data.item === 'object' &&
|
|
161
|
-
event.data.item !== null) {
|
|
162
|
-
yield {
|
|
163
|
-
...frameworkCast(event.data.item),
|
|
164
|
-
isComplete: true,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
continue;
|
|
208
|
+
yield* frameworkSnapshots(event);
|
|
209
|
+
return;
|
|
168
210
|
}
|
|
169
211
|
if (event.source !== 'sdk') {
|
|
170
|
-
|
|
212
|
+
return;
|
|
171
213
|
}
|
|
172
214
|
if (event.type === 'response.created') {
|
|
173
|
-
roundOffset++;
|
|
174
|
-
|
|
215
|
+
this.roundOffset++;
|
|
216
|
+
return;
|
|
175
217
|
}
|
|
176
218
|
if (event.type === 'response.output_item.added') {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const acc = createAccumulatorFromItemAdded(event, itemSchemas);
|
|
180
|
-
if (acc) {
|
|
181
|
-
accumulators.set(key, acc);
|
|
182
|
-
yield {
|
|
183
|
-
...acc.item,
|
|
184
|
-
isComplete: false,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
continue;
|
|
219
|
+
yield* this.add(event);
|
|
220
|
+
return;
|
|
188
221
|
}
|
|
189
222
|
if (event.type === 'response.output_item.done') {
|
|
190
|
-
const
|
|
191
|
-
const key = accumulatorKey(roundOffset, outputIndex);
|
|
192
|
-
const acc = accumulators.get(key);
|
|
223
|
+
const acc = this.find(event, -1);
|
|
193
224
|
if (acc) {
|
|
194
225
|
acc.isComplete = true;
|
|
195
226
|
yield completeStreamingItem(acc.item);
|
|
196
227
|
}
|
|
197
|
-
|
|
228
|
+
return;
|
|
198
229
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
isComplete: acc.isComplete,
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
continue;
|
|
230
|
+
const update = ACCUMULATOR_UPDATES[event.type];
|
|
231
|
+
const acc = update ? this.find(event, 0) : undefined;
|
|
232
|
+
if (update && acc) {
|
|
233
|
+
update(acc, event);
|
|
234
|
+
yield {
|
|
235
|
+
...acc.item,
|
|
236
|
+
isComplete: acc.isComplete,
|
|
237
|
+
};
|
|
211
238
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
239
|
+
}
|
|
240
|
+
*add(event) {
|
|
241
|
+
const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : this.accumulators.size;
|
|
242
|
+
const acc = createAccumulatorFromItemAdded(event, this.itemSchemas);
|
|
243
|
+
if (!acc) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
this.accumulators.set(accumulatorKey(this.roundOffset, outputIndex), acc);
|
|
247
|
+
if (acc.id !== '') {
|
|
248
|
+
this.byId.set(acc.id, acc);
|
|
249
|
+
}
|
|
250
|
+
yield {
|
|
251
|
+
...acc.item,
|
|
252
|
+
isComplete: false,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/** The accumulator `event` is about. `missingIndex` stands in for an absent output index. */
|
|
256
|
+
find(event, missingIndex) {
|
|
257
|
+
const id = itemIdOf(event);
|
|
258
|
+
const named = id === undefined ? undefined : this.byId.get(id);
|
|
259
|
+
if (named) {
|
|
260
|
+
return named;
|
|
261
|
+
}
|
|
262
|
+
// An event that names an item this stream never saw is about the item at its
|
|
263
|
+
// own output index, which the provider renamed after `added`. One with no
|
|
264
|
+
// index is given to no item: it could be another call's.
|
|
265
|
+
if (id !== undefined && typeof event.outputIndex !== 'number') {
|
|
266
|
+
return undefined;
|
|
224
267
|
}
|
|
268
|
+
const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : missingIndex;
|
|
269
|
+
return this.accumulators.get(accumulatorKey(this.roundOffset, outputIndex));
|
|
225
270
|
}
|
|
226
271
|
}
|
|
227
272
|
//#endregion
|
|
@@ -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;
|