@noetic-tools/core 5.22.0-next.1 → 5.22.0-next.3
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/in-memory-subprocess-adapter.js +4 -2
- package/dist/adapters/openrouter.d.ts +6 -1
- package/dist/adapters/openrouter.js +10 -0
- package/dist/builders/custom-item.d.ts +1 -1
- package/dist/builders/custom-item.js +14 -3
- package/dist/builders/decide-builders.d.ts +7 -0
- package/dist/builders/tool-builder.d.ts +5 -1
- package/dist/builders/tool-builder.js +2 -0
- package/dist/builders/workflow-control-nodes.js +1 -0
- package/dist/connections/pool.d.ts +18 -0
- package/dist/connections/pool.js +54 -9
- package/dist/connections/registry.js +11 -0
- package/dist/harness/agent-harness.d.ts +130 -3
- package/dist/harness/agent-harness.js +337 -22
- package/dist/harness/decide-call.d.ts +15 -0
- package/dist/harness/decide-call.js +57 -7
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/forget.d.ts +57 -0
- package/dist/harness/forget.js +496 -0
- package/dist/harness/harness-options.d.ts +24 -1
- package/dist/harness/llm-client.d.ts +2 -0
- package/dist/harness/llm-client.js +9 -5
- package/dist/harness/model-rounds.d.ts +6 -0
- package/dist/harness/model-rounds.js +7 -3
- package/dist/harness/model-tool-round.js +67 -8
- package/dist/harness/recorded-tool-calls.d.ts +16 -0
- package/dist/harness/recorded-tool-calls.js +106 -0
- package/dist/harness/spend.d.ts +13 -1
- package/dist/harness/spend.js +82 -1
- package/dist/harness/steer-inbox.js +9 -5
- package/dist/harness/thread-record.d.ts +12 -1
- package/dist/harness/thread-record.js +63 -19
- package/dist/index.d.ts +9 -6
- package/dist/index.js +4 -3
- package/dist/interpreter/acp-tools.js +21 -4
- package/dist/interpreter/execute-action.js +7 -1
- package/dist/interpreter/execute-decide.js +5 -0
- package/dist/interpreter/execute.js +55 -3
- package/dist/interpreter/step-dispatcher.js +17 -1
- package/dist/interpreter/step-kind-dispatch.js +9 -1
- package/dist/portable/runtime.d.ts +4 -0
- package/dist/portable/runtime.js +2 -0
- package/dist/runtime/durable/child-links.d.ts +61 -0
- package/dist/runtime/durable/child-links.js +127 -0
- package/dist/runtime/durable/session-store.d.ts +6 -0
- package/dist/runtime/durable/session-store.js +16 -2
- package/dist/runtime/durable/turn-lease.d.ts +10 -0
- package/dist/runtime/durable/turn-lease.js +10 -2
- package/dist/runtime/event-broadcaster.d.ts +9 -1
- package/dist/runtime/event-broadcaster.js +16 -1
- package/dist/runtime/held-calls.d.ts +65 -6
- package/dist/runtime/held-calls.js +257 -5
- package/dist/runtime/output-store.d.ts +90 -0
- package/dist/runtime/output-store.js +243 -0
- package/dist/runtime/session-streams.d.ts +28 -7
- package/dist/runtime/session-streams.js +112 -67
- package/dist/runtime/tool-approval.d.ts +1 -3
- package/dist/runtime/tool-approval.js +1 -1
- package/package.json +2 -2
- package/schema/noetic-workflow.schema.json +27 -0
|
@@ -23,14 +23,29 @@ export class EventBroadcaster {
|
|
|
23
23
|
err;
|
|
24
24
|
iterators = new Set();
|
|
25
25
|
hasHadConsumers = false;
|
|
26
|
+
observers = new Set();
|
|
26
27
|
constructor(opts) {
|
|
27
28
|
this.maxBufferSize = opts?.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE;
|
|
28
29
|
}
|
|
29
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Hear every event as it is emitted, whether or not a consumer is attached: the
|
|
32
|
+
* replay buffer stops filling once every consumer has left, an observer does not.
|
|
33
|
+
* Nothing is replayed to it. Returns the function that stops it.
|
|
34
|
+
*/
|
|
35
|
+
observe(observer) {
|
|
36
|
+
this.observers.add(observer);
|
|
37
|
+
return () => {
|
|
38
|
+
this.observers.delete(observer);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Push an event to every observer, all active iterators and the replay buffer. */
|
|
30
42
|
emit(event) {
|
|
31
43
|
if (this.isDone) {
|
|
32
44
|
return;
|
|
33
45
|
}
|
|
46
|
+
for (const observer of this.observers) {
|
|
47
|
+
observer(event);
|
|
48
|
+
}
|
|
34
49
|
// Skip buffering when all consumers have departed
|
|
35
50
|
if (this.hasHadConsumers && this.iterators.size === 0) {
|
|
36
51
|
return;
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* turn resumed on another harness finds the same record and the same answer.
|
|
12
12
|
*/
|
|
13
13
|
import type { StorageAdapter } from '@noetic-tools/context';
|
|
14
|
-
import type { Context, HeldCall, HeldResolution, ToolApprover } from '@noetic-tools/types';
|
|
14
|
+
import type { Context, HeldCall, HeldCallHandler, HeldCallSettled, HeldResolution, ToolApprover } from '@noetic-tools/types';
|
|
15
15
|
import { z } from 'zod';
|
|
16
16
|
declare const HeldRecordSchema: z.ZodObject<{
|
|
17
17
|
callId: z.ZodString;
|
|
@@ -49,7 +49,24 @@ export interface HoldHost {
|
|
|
49
49
|
readonly storage?: StorageAdapter;
|
|
50
50
|
readonly now?: () => number;
|
|
51
51
|
readonly approveToolCall?: ToolApprover;
|
|
52
|
+
readonly onHeld?: HeldCallHandler;
|
|
52
53
|
};
|
|
54
|
+
/** The thread a context's held calls belong to, when it is a thread's turn. */
|
|
55
|
+
heldThreadOf?(ctx: Context): string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Answer a call held under `executionId`, as a decision item does, and wake its
|
|
58
|
+
* thread's turn when that turn is this execution and parked.
|
|
59
|
+
*/
|
|
60
|
+
answerHeld?(answer: HeldAnswerRequest): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/** @internal An answer to a held call, for the execution that holds it. */
|
|
63
|
+
export interface HeldAnswerRequest {
|
|
64
|
+
readonly executionId: string;
|
|
65
|
+
/** The thread whose turn held the call, when it was a thread's turn. */
|
|
66
|
+
readonly threadId?: string;
|
|
67
|
+
readonly callId: string;
|
|
68
|
+
readonly argsHash: string;
|
|
69
|
+
readonly resolution: HeldResolution;
|
|
53
70
|
}
|
|
54
71
|
/** @internal How long a hold lasts and what happens when that runs out. */
|
|
55
72
|
export interface HoldDue {
|
|
@@ -74,6 +91,12 @@ export declare class HeldCalls {
|
|
|
74
91
|
private readonly storage;
|
|
75
92
|
private readonly clock;
|
|
76
93
|
private readonly waiters;
|
|
94
|
+
/** Calls announced held in this process, by execution and call. */
|
|
95
|
+
private readonly watches;
|
|
96
|
+
/** Watched calls this process is closing: a sweep leaves them to `close`. */
|
|
97
|
+
private readonly closing;
|
|
98
|
+
/** Re-reads watched calls on shared storage, which another harness may settle. */
|
|
99
|
+
private sweeper;
|
|
77
100
|
constructor(storage: StorageAdapter, clock: () => number);
|
|
78
101
|
now(): number;
|
|
79
102
|
/** The record of `callId`, when the call is held. */
|
|
@@ -100,8 +123,32 @@ export declare class HeldCalls {
|
|
|
100
123
|
wasStarted(executionId: string, callId: string, argsHash: string): Promise<boolean>;
|
|
101
124
|
/** The call finished and its result is on record. */
|
|
102
125
|
disarm(executionId: string, callId: string): Promise<void>;
|
|
103
|
-
/**
|
|
104
|
-
|
|
126
|
+
/**
|
|
127
|
+
* The call was settled and its result recorded, or it is being dropped: drop its
|
|
128
|
+
* record. `by` says why, for a call this process announced; without it the record
|
|
129
|
+
* says: answered, overdue, or else cancelled.
|
|
130
|
+
*/
|
|
131
|
+
close(executionId: string, callId: string, by?: HeldCallSettled['by']): Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Note that this process announced `held`. Resolves the signal a handler waits on,
|
|
134
|
+
* or `undefined` when the same question is already being watched here. A different
|
|
135
|
+
* question under the same id replaces the old one: a call held for a new reason
|
|
136
|
+
* moved on because the old question was answered; one held for other arguments
|
|
137
|
+
* voided it.
|
|
138
|
+
*/
|
|
139
|
+
watch(executionId: string, held: HeldCall, emit: (settled: HeldCallSettled) => void): AbortSignal | undefined;
|
|
140
|
+
/**
|
|
141
|
+
* On shared storage another harness may answer, skip or drop a call this process
|
|
142
|
+
* announced, and finish its turn, so nothing here would ever close it. Watched
|
|
143
|
+
* calls are read again every so often while any are watched.
|
|
144
|
+
*/
|
|
145
|
+
private armSweep;
|
|
146
|
+
private stopSweep;
|
|
147
|
+
/** Settle every watched call whose record says it is no longer the question shown. */
|
|
148
|
+
sweep(): Promise<void>;
|
|
149
|
+
/** The harness is closing: every handler still waiting is let go, with no event. */
|
|
150
|
+
releaseWatches(): void;
|
|
151
|
+
private settleWatch;
|
|
105
152
|
/**
|
|
106
153
|
* Answer a held call. The first answer wins: the same answer again is a no-op, a
|
|
107
154
|
* different one is refused.
|
|
@@ -178,15 +225,27 @@ export declare function holdAndWait(opts: {
|
|
|
178
225
|
harness: HoldHost;
|
|
179
226
|
ctx: Context;
|
|
180
227
|
record: HeldRecord;
|
|
181
|
-
/** Called once per process, when the call is first seen held. */
|
|
182
|
-
onHeld?: (held: HeldCall) => void;
|
|
183
228
|
}): Promise<HeldAnswer>;
|
|
184
229
|
/**
|
|
185
230
|
* @internal
|
|
186
231
|
* Says on the stream that a call is held: `{name}:tool_call_held`, with the fields
|
|
187
|
-
* `harness.pending()` returns
|
|
232
|
+
* `harness.pending()` returns, and puts it to the harness's `onHeld` handler.
|
|
188
233
|
*/
|
|
189
234
|
export declare function announcerOf(harness: HoldHost, ctx: Context): (held: HeldCall) => void;
|
|
235
|
+
/**
|
|
236
|
+
* @internal
|
|
237
|
+
* A call was announced held in this process (spec 08 § Held tool calls): watch it, so
|
|
238
|
+
* `tool_call_settled` follows when it stops being held, and put it to the harness's
|
|
239
|
+
* `onHeld` handler, once per question. Never waits on the handler.
|
|
240
|
+
*
|
|
241
|
+
* @param emit - How the settling is said, when not on `ctx`'s stream as is.
|
|
242
|
+
*/
|
|
243
|
+
export declare function noticeHeld(opts: {
|
|
244
|
+
harness: HoldHost;
|
|
245
|
+
ctx: Context;
|
|
246
|
+
held: HeldCall;
|
|
247
|
+
emit?: (settled: HeldCallSettled) => void;
|
|
248
|
+
}): void;
|
|
190
249
|
/** True once a hold with a `deny` deadline has run out. */
|
|
191
250
|
export declare function isOverdue(record: HeldRecord, now: number): boolean;
|
|
192
251
|
export {};
|
|
@@ -113,6 +113,38 @@ function toResolutionRecord(resolution) {
|
|
|
113
113
|
hasOutput: true,
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
|
+
function watchKey(executionId, callId) {
|
|
117
|
+
return `${executionId}\u0000${callId}`;
|
|
118
|
+
}
|
|
119
|
+
function settledOf(held, by) {
|
|
120
|
+
return {
|
|
121
|
+
callId: held.callId,
|
|
122
|
+
name: held.name,
|
|
123
|
+
argsHash: held.argsHash,
|
|
124
|
+
reason: held.reason,
|
|
125
|
+
by,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Why a record that is being closed stopped being held, as the record says. */
|
|
129
|
+
function settledByOf(record, now) {
|
|
130
|
+
if (record?.resolution !== undefined) {
|
|
131
|
+
return 'answer';
|
|
132
|
+
}
|
|
133
|
+
if (record && isOverdue(record, now)) {
|
|
134
|
+
return 'due';
|
|
135
|
+
}
|
|
136
|
+
return 'cancelled';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Whether a watched call is still the question shown: held, unanswered, for the same
|
|
140
|
+
* arguments and reason. Anything else means another harness settled it.
|
|
141
|
+
*/
|
|
142
|
+
function stillShown(record, held) {
|
|
143
|
+
return (record !== undefined &&
|
|
144
|
+
record.resolution === undefined &&
|
|
145
|
+
record.argsHash === held.argsHash &&
|
|
146
|
+
record.reason === held.reason);
|
|
147
|
+
}
|
|
116
148
|
/** A held record as hosts see it. */
|
|
117
149
|
export function toHeldCall(record) {
|
|
118
150
|
return {
|
|
@@ -145,6 +177,12 @@ export class HeldCalls {
|
|
|
145
177
|
storage;
|
|
146
178
|
clock;
|
|
147
179
|
waiters = new Map();
|
|
180
|
+
/** Calls announced held in this process, by execution and call. */
|
|
181
|
+
watches = new Map();
|
|
182
|
+
/** Watched calls this process is closing: a sweep leaves them to `close`. */
|
|
183
|
+
closing = new Set();
|
|
184
|
+
/** Re-reads watched calls on shared storage, which another harness may settle. */
|
|
185
|
+
sweeper;
|
|
148
186
|
constructor(storage, clock) {
|
|
149
187
|
this.storage = storage;
|
|
150
188
|
this.clock = clock;
|
|
@@ -190,6 +228,13 @@ export class HeldCalls {
|
|
|
190
228
|
}
|
|
191
229
|
/** Drop every held call and intent of an execution that is over or was abandoned. */
|
|
192
230
|
async clear(executionId) {
|
|
231
|
+
for (const key of [
|
|
232
|
+
...this.watches.keys(),
|
|
233
|
+
]) {
|
|
234
|
+
if (key.startsWith(`${executionId}\u0000`)) {
|
|
235
|
+
this.settleWatch(key, 'cancelled');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
193
238
|
for (const prefix of [
|
|
194
239
|
heldPrefix(executionId),
|
|
195
240
|
intentPrefix(executionId),
|
|
@@ -218,9 +263,118 @@ export class HeldCalls {
|
|
|
218
263
|
async disarm(executionId, callId) {
|
|
219
264
|
await this.storage.delete(`${intentPrefix(executionId)}${callId}`);
|
|
220
265
|
}
|
|
221
|
-
/**
|
|
222
|
-
|
|
223
|
-
|
|
266
|
+
/**
|
|
267
|
+
* The call was settled and its result recorded, or it is being dropped: drop its
|
|
268
|
+
* record. `by` says why, for a call this process announced; without it the record
|
|
269
|
+
* says: answered, overdue, or else cancelled.
|
|
270
|
+
*/
|
|
271
|
+
async close(executionId, callId, by) {
|
|
272
|
+
const key = watchKey(executionId, callId);
|
|
273
|
+
const watched = this.watches.has(key);
|
|
274
|
+
if (watched) {
|
|
275
|
+
this.closing.add(key);
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const settledBy = watched && by === undefined
|
|
279
|
+
? settledByOf(await this.load(executionId, callId), this.now())
|
|
280
|
+
: by;
|
|
281
|
+
await this.storage.delete(`${heldPrefix(executionId)}${callId}`);
|
|
282
|
+
if (settledBy !== undefined) {
|
|
283
|
+
this.settleWatch(key, settledBy);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
this.closing.delete(key);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Note that this process announced `held`. Resolves the signal a handler waits on,
|
|
292
|
+
* or `undefined` when the same question is already being watched here. A different
|
|
293
|
+
* question under the same id replaces the old one: a call held for a new reason
|
|
294
|
+
* moved on because the old question was answered; one held for other arguments
|
|
295
|
+
* voided it.
|
|
296
|
+
*/
|
|
297
|
+
watch(executionId, held, emit) {
|
|
298
|
+
const key = watchKey(executionId, held.callId);
|
|
299
|
+
const existing = this.watches.get(key);
|
|
300
|
+
if (existing?.held.argsHash === held.argsHash && existing.held.reason === held.reason) {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
if (existing) {
|
|
304
|
+
this.settleWatch(key, existing.held.reason === held.reason ? 'cancelled' : 'answer');
|
|
305
|
+
}
|
|
306
|
+
const controller = new AbortController();
|
|
307
|
+
this.watches.set(key, {
|
|
308
|
+
executionId,
|
|
309
|
+
held,
|
|
310
|
+
controller,
|
|
311
|
+
emit,
|
|
312
|
+
});
|
|
313
|
+
this.armSweep();
|
|
314
|
+
return controller.signal;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* On shared storage another harness may answer, skip or drop a call this process
|
|
318
|
+
* announced, and finish its turn, so nothing here would ever close it. Watched
|
|
319
|
+
* calls are read again every so often while any are watched.
|
|
320
|
+
*/
|
|
321
|
+
armSweep() {
|
|
322
|
+
if (!this.shared || this.sweeper !== undefined) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const sweeper = setInterval(() => {
|
|
326
|
+
void this.sweep().catch(() => undefined);
|
|
327
|
+
}, HELD_POLL_MS);
|
|
328
|
+
if (typeof sweeper === 'object' && 'unref' in sweeper) {
|
|
329
|
+
sweeper.unref();
|
|
330
|
+
}
|
|
331
|
+
this.sweeper = sweeper;
|
|
332
|
+
}
|
|
333
|
+
stopSweep() {
|
|
334
|
+
clearInterval(this.sweeper);
|
|
335
|
+
this.sweeper = undefined;
|
|
336
|
+
}
|
|
337
|
+
/** Settle every watched call whose record says it is no longer the question shown. */
|
|
338
|
+
async sweep() {
|
|
339
|
+
for (const [key, watch] of [
|
|
340
|
+
...this.watches,
|
|
341
|
+
]) {
|
|
342
|
+
const record = await this.load(watch.executionId, watch.held.callId);
|
|
343
|
+
if (this.watches.get(key) !== watch || this.closing.has(key)) {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// Settled by another harness, which cannot say how: reported as answered.
|
|
347
|
+
if (!stillShown(record, watch.held)) {
|
|
348
|
+
this.settleWatch(key, 'answer');
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** The harness is closing: every handler still waiting is let go, with no event. */
|
|
353
|
+
releaseWatches() {
|
|
354
|
+
this.stopSweep();
|
|
355
|
+
for (const [key, watch] of [
|
|
356
|
+
...this.watches,
|
|
357
|
+
]) {
|
|
358
|
+
this.watches.delete(key);
|
|
359
|
+
watch.controller.abort(settledOf(watch.held, 'cancelled'));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
settleWatch(key, by) {
|
|
363
|
+
const watch = this.watches.get(key);
|
|
364
|
+
if (!watch) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
this.watches.delete(key);
|
|
368
|
+
if (this.watches.size === 0) {
|
|
369
|
+
this.stopSweep();
|
|
370
|
+
}
|
|
371
|
+
const settled = settledOf(watch.held, by);
|
|
372
|
+
try {
|
|
373
|
+
watch.emit(settled);
|
|
374
|
+
}
|
|
375
|
+
finally {
|
|
376
|
+
watch.controller.abort(settled);
|
|
377
|
+
}
|
|
224
378
|
}
|
|
225
379
|
/**
|
|
226
380
|
* Answer a held call. The first answer wins: the same answer again is a no-op, a
|
|
@@ -288,6 +442,7 @@ export class HeldCalls {
|
|
|
288
442
|
}
|
|
289
443
|
throw refused(callId, 'the call was already answered differently');
|
|
290
444
|
}
|
|
445
|
+
this.settleWatch(watchKey(executionId, callId), 'answer');
|
|
291
446
|
this.notify(executionId);
|
|
292
447
|
}
|
|
293
448
|
/** True when other processes may share this storage, and so may answer a call. */
|
|
@@ -450,7 +605,7 @@ export async function holdAndWait(opts) {
|
|
|
450
605
|
const { callId } = opts.record;
|
|
451
606
|
const opened = mayWriteHolds(opts.ctx) ? await holds.open(executionId, opts.record) : opts.record;
|
|
452
607
|
if (opened.resolution === undefined) {
|
|
453
|
-
|
|
608
|
+
announcerOf(opts.harness, opts.ctx)(toHeldCall(opened));
|
|
454
609
|
}
|
|
455
610
|
for (;;) {
|
|
456
611
|
const current = await holds.load(executionId, callId);
|
|
@@ -475,7 +630,7 @@ export async function holdAndWait(opts) {
|
|
|
475
630
|
/**
|
|
476
631
|
* @internal
|
|
477
632
|
* Says on the stream that a call is held: `{name}:tool_call_held`, with the fields
|
|
478
|
-
* `harness.pending()` returns
|
|
633
|
+
* `harness.pending()` returns, and puts it to the harness's `onHeld` handler.
|
|
479
634
|
*/
|
|
480
635
|
export function announcerOf(harness, ctx) {
|
|
481
636
|
return (held) => {
|
|
@@ -487,8 +642,105 @@ export function announcerOf(harness, ctx) {
|
|
|
487
642
|
...held,
|
|
488
643
|
},
|
|
489
644
|
});
|
|
645
|
+
noticeHeld({
|
|
646
|
+
harness,
|
|
647
|
+
ctx,
|
|
648
|
+
held,
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
/** Emits `{name}:tool_call_settled` on the context's stream. */
|
|
653
|
+
function settledAnnouncerOf(harness, ctx) {
|
|
654
|
+
return (settled) => {
|
|
655
|
+
emitFrameworkEvent({
|
|
656
|
+
broadcaster: getBroadcaster(ctx),
|
|
657
|
+
agentName: harness.config.name,
|
|
658
|
+
eventType: 'tool_call_settled',
|
|
659
|
+
data: {
|
|
660
|
+
...settled,
|
|
661
|
+
},
|
|
662
|
+
});
|
|
490
663
|
};
|
|
491
664
|
}
|
|
665
|
+
/**
|
|
666
|
+
* @internal
|
|
667
|
+
* A call was announced held in this process (spec 08 § Held tool calls): watch it, so
|
|
668
|
+
* `tool_call_settled` follows when it stops being held, and put it to the harness's
|
|
669
|
+
* `onHeld` handler, once per question. Never waits on the handler.
|
|
670
|
+
*
|
|
671
|
+
* @param emit - How the settling is said, when not on `ctx`'s stream as is.
|
|
672
|
+
*/
|
|
673
|
+
export function noticeHeld(opts) {
|
|
674
|
+
const { harness, ctx, held } = opts;
|
|
675
|
+
const executionId = heldScopeOf(ctx);
|
|
676
|
+
const signal = heldCallsOf(harness).watch(executionId, held, opts.emit ?? settledAnnouncerOf(harness, ctx));
|
|
677
|
+
const handler = harness.config.onHeld;
|
|
678
|
+
if (!signal || !handler) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
void askHandler({
|
|
682
|
+
harness,
|
|
683
|
+
handler,
|
|
684
|
+
held,
|
|
685
|
+
signal,
|
|
686
|
+
executionId,
|
|
687
|
+
threadId: harness.heldThreadOf?.(ctx),
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
/** Put a held call to the host's handler and take its answer as a decision. */
|
|
691
|
+
async function askHandler(opts) {
|
|
692
|
+
const { harness, held, signal, executionId, threadId } = opts;
|
|
693
|
+
let answer;
|
|
694
|
+
try {
|
|
695
|
+
answer = await opts.handler(held, {
|
|
696
|
+
signal,
|
|
697
|
+
executionId,
|
|
698
|
+
...(threadId === undefined
|
|
699
|
+
? {}
|
|
700
|
+
: {
|
|
701
|
+
threadId,
|
|
702
|
+
}),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
catch (e) {
|
|
706
|
+
console.warn(`[noetic] ${harness.config.name}: the onHeld handler failed for '${held.name}' (${held.callId}); the call stays held: ${messageOf(e)}`);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
// Settled some other way first: there is nothing left to answer.
|
|
710
|
+
if (answer === undefined || signal.aborted) {
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
try {
|
|
714
|
+
if (harness.answerHeld) {
|
|
715
|
+
await harness.answerHeld({
|
|
716
|
+
executionId,
|
|
717
|
+
...(threadId === undefined
|
|
718
|
+
? {}
|
|
719
|
+
: {
|
|
720
|
+
threadId,
|
|
721
|
+
}),
|
|
722
|
+
callId: held.callId,
|
|
723
|
+
argsHash: held.argsHash,
|
|
724
|
+
resolution: answer,
|
|
725
|
+
});
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
await heldCallsOf(harness).resolve(executionId, {
|
|
729
|
+
callId: held.callId,
|
|
730
|
+
argsHash: held.argsHash,
|
|
731
|
+
resolution: answer,
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
catch (e) {
|
|
735
|
+
if (signal.aborted) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
console.warn(`[noetic] ${harness.config.name}: the onHeld answer for '${held.name}' (${held.callId}) was refused; the call stays held: ${messageOf(e)}`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function messageOf(e) {
|
|
742
|
+
return e instanceof Error ? e.message : String(e);
|
|
743
|
+
}
|
|
492
744
|
/** True once a hold with a `deny` deadline has run out. */
|
|
493
745
|
export function isOverdue(record, now) {
|
|
494
746
|
return record.dueAt !== undefined && record.onDue !== 'keep' && record.dueAt <= now;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ItemSchemaRegistry, StorageAdapter, StreamEvent } from '@noetic-tools/types';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
declare const OutputSnapshotSchema: z.ZodObject<{
|
|
4
|
+
type: z.ZodString;
|
|
5
|
+
id: z.ZodOptional<z.ZodString>;
|
|
6
|
+
isComplete: z.ZodOptional<z.ZodBoolean>;
|
|
7
|
+
}, z.core.$catchall<z.ZodUnknown>>;
|
|
8
|
+
/**
|
|
9
|
+
* @public One item of a thread's display output: an item snapshot as the item
|
|
10
|
+
* stream yields it, unfinished ones included (`isComplete: false`), or whatever
|
|
11
|
+
* a protocol adapter saved in its place.
|
|
12
|
+
*/
|
|
13
|
+
export type OutputSnapshot = z.infer<typeof OutputSnapshotSchema>;
|
|
14
|
+
/**
|
|
15
|
+
* @public A thread's display output could not be read or written. `scope` is the
|
|
16
|
+
* thread id; `cause` is what storage, or the snapshot check, failed with.
|
|
17
|
+
*/
|
|
18
|
+
export declare class OutputStoreError extends Error {
|
|
19
|
+
readonly scope: string;
|
|
20
|
+
constructor(options: {
|
|
21
|
+
scope: string;
|
|
22
|
+
message: string;
|
|
23
|
+
cause: unknown;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** @internal The prefix every thread's display output key starts with. */
|
|
27
|
+
export declare const OUTPUT_PREFIX = "output:";
|
|
28
|
+
/** @internal The storage key a thread's display output is kept under. */
|
|
29
|
+
export declare function outputKey(scope: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* @public A thread's display output, kept in storage under `output:<scope>`:
|
|
32
|
+
* the latest snapshot of every item its stream produced, including items that
|
|
33
|
+
* never finished. It is for showing a thread again, in a new process or after a
|
|
34
|
+
* failed run. Nothing resumes from it: checkpoints, the step ledger and the
|
|
35
|
+
* model's history are stored apart and never read here.
|
|
36
|
+
*
|
|
37
|
+
* `record` and `write` queue snapshots and save them in the background, one
|
|
38
|
+
* write at a time, each holding the whole output. `flush` and `read` wait for
|
|
39
|
+
* what is queued and throw `OutputStoreError` when it could not be saved; the
|
|
40
|
+
* snapshots stay queued, so the next call tries again. On storage with `swap` a
|
|
41
|
+
* write never replaces what another store saved meanwhile.
|
|
42
|
+
*/
|
|
43
|
+
export declare class OutputStore {
|
|
44
|
+
private readonly storage;
|
|
45
|
+
private readonly scope;
|
|
46
|
+
private readonly stream;
|
|
47
|
+
private readonly pending;
|
|
48
|
+
private writing?;
|
|
49
|
+
private failure?;
|
|
50
|
+
constructor(storage: StorageAdapter, scope: string, schemas?: ItemSchemaRegistry);
|
|
51
|
+
/**
|
|
52
|
+
* Fold one stream event into the output. Never throws: an event that cannot be
|
|
53
|
+
* read as display output is left out, with a warning, and the run goes on.
|
|
54
|
+
*/
|
|
55
|
+
record(event: StreamEvent): void;
|
|
56
|
+
/**
|
|
57
|
+
* Save display snapshots, each replacing the stored one with its id, and resolve
|
|
58
|
+
* the whole stored output.
|
|
59
|
+
*
|
|
60
|
+
* @throws OutputStoreError when `items` are not snapshots, or storage fails.
|
|
61
|
+
*/
|
|
62
|
+
write(items: ReadonlyArray<unknown>): Promise<OutputSnapshot[]>;
|
|
63
|
+
/**
|
|
64
|
+
* The stored output, after everything queued is saved.
|
|
65
|
+
*
|
|
66
|
+
* @throws OutputStoreError when storage fails or holds something else.
|
|
67
|
+
*/
|
|
68
|
+
read(): Promise<OutputSnapshot[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Wait until everything queued is saved.
|
|
71
|
+
*
|
|
72
|
+
* @throws OutputStoreError when it could not be. What was queued stays queued,
|
|
73
|
+
* and every flush throws until a later one saves it.
|
|
74
|
+
*/
|
|
75
|
+
flush(): Promise<void>;
|
|
76
|
+
private queue;
|
|
77
|
+
private start;
|
|
78
|
+
/** What storage holds under the key, as it is stored and as snapshots. */
|
|
79
|
+
private load;
|
|
80
|
+
/** Save what is queued over what is stored, until nothing is queued. */
|
|
81
|
+
private drain;
|
|
82
|
+
private save;
|
|
83
|
+
/**
|
|
84
|
+
* Write `batch` over what is stored now. On storage with `swap` the write lands
|
|
85
|
+
* only if nothing else was saved since the read, and false means it did not.
|
|
86
|
+
*/
|
|
87
|
+
private saveOver;
|
|
88
|
+
private error;
|
|
89
|
+
}
|
|
90
|
+
export {};
|