@zenera/neo 1.1.54 → 1.1.55
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/kernel.d.ts +11 -1
- package/dist/kernel.js +24 -1
- package/dist/memory/graph.d.ts +22 -0
- package/dist/memory/graph.js +72 -7
- package/dist/memory/grep.js +1 -0
- package/dist/memory/index.d.ts +3 -1
- package/dist/memory/index.js +95 -31
- package/dist/memory/merge.js +29 -17
- package/dist/memory/migrate.d.ts +14 -0
- package/dist/memory/migrate.js +112 -0
- package/dist/memory/store.d.ts +36 -27
- package/dist/memory/store.js +409 -126
- package/dist/memory/tools.js +8 -0
- package/dist/runner.d.ts +20 -4
- package/dist/runner.js +71 -7
- package/dist/state.d.ts +8 -2
- package/dist/types.d.ts +41 -0
- package/dist/types.js +21 -0
- package/package.json +1 -1
package/dist/runner.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { Services } from './services.ts';
|
|
|
10
10
|
import { type SkillProvider } from './skills.ts';
|
|
11
11
|
import { type AgentState } from './state.ts';
|
|
12
12
|
import { type HandoffNode, type TrajectoryNode } from './trajectory.ts';
|
|
13
|
-
import { type Input, type TokenUsage } from './types.ts';
|
|
13
|
+
import { type Input, type ResumeAnswer, type TokenUsage } from './types.ts';
|
|
14
14
|
/**
|
|
15
15
|
* Collapsing history is two decisions, and they belong on different sides of
|
|
16
16
|
* the I/O line: *which* nodes go (pure, so the kernel can stay pure) and *how*
|
|
@@ -67,6 +67,15 @@ export interface RunOptions<T = string, TCtx = unknown> {
|
|
|
67
67
|
signal?: AbortSignal;
|
|
68
68
|
maxForkDepth?: number;
|
|
69
69
|
}
|
|
70
|
+
export interface ResumeOptions<T = string, TCtx = unknown> extends RunOptions<T, TCtx> {
|
|
71
|
+
/**
|
|
72
|
+
* Answers to the calls the run is suspended on, by call id. A suspended call
|
|
73
|
+
* without an answer stays suspended: the run stops again at once.
|
|
74
|
+
*/
|
|
75
|
+
answers?: Record<string, ResumeAnswer>;
|
|
76
|
+
}
|
|
77
|
+
/** What a new message says to a call still waiting for a person. */
|
|
78
|
+
export declare const SUPERSEDED_MESSAGE = "cancelled: the user sent a new message instead of answering this; continue without it";
|
|
70
79
|
/**
|
|
71
80
|
* The driver: it owns all nondeterminism and all I/O, and calls the kernel to
|
|
72
81
|
* advance the state. Every checkpoint event carries a complete snapshot, so a
|
|
@@ -97,9 +106,16 @@ export declare class AgentRunner<TCtx = unknown> {
|
|
|
97
106
|
* consumer sees (`run_created`) already carries a persistable snapshot.
|
|
98
107
|
*/
|
|
99
108
|
run<T = string>(agent: Agent<TCtx> | string, input?: Input, opts?: RunOptions<T, TCtx>): RunStream<T>;
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Continues a persisted run from its own snapshot. A run that stopped
|
|
111
|
+
* `suspended` continues once `answers` answer its suspended calls.
|
|
112
|
+
*/
|
|
113
|
+
resume<T = string>(state: AgentState, opts?: ResumeOptions<T, TCtx>): RunStream<T>;
|
|
114
|
+
/**
|
|
115
|
+
* Adds a follow-up message to a finished (or paused) run and continues it.
|
|
116
|
+
* Calls still suspended are cancelled first: the person chose to say
|
|
117
|
+
* something else rather than answer them.
|
|
118
|
+
*/
|
|
103
119
|
send<T = string>(state: AgentState, input: Input, opts?: RunOptions<T, TCtx>): RunStream<T>;
|
|
104
120
|
}
|
|
105
121
|
//# sourceMappingURL=runner.d.ts.map
|
package/dist/runner.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Services } from "./services.js";
|
|
|
9
9
|
import { activeSkillNames } from "./skills.js";
|
|
10
10
|
import { lastText } from "./state.js";
|
|
11
11
|
import { lastOfType, projectMessages, } from "./trajectory.js";
|
|
12
|
-
import { isToolReturn, stringify, zeroUsage, } from "./types.js";
|
|
12
|
+
import { isToolReturn, isToolSuspend, stringify, zeroUsage, } from "./types.js";
|
|
13
13
|
const defaultJoinPolicy = {
|
|
14
14
|
summarize: (child, services) => lastText(child, services.payloads),
|
|
15
15
|
onBranchError: 'continue',
|
|
@@ -51,6 +51,8 @@ export function modelSummarizer(model, instructions = SUMMARY_INSTRUCTIONS) {
|
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
|
+
/** What a new message says to a call still waiting for a person. */
|
|
55
|
+
export const SUPERSEDED_MESSAGE = 'cancelled: the user sent a new message instead of answering this; continue without it';
|
|
54
56
|
// ---------------------------------------------------------------------------
|
|
55
57
|
// Runner
|
|
56
58
|
// ---------------------------------------------------------------------------
|
|
@@ -126,18 +128,59 @@ export class AgentRunner {
|
|
|
126
128
|
// awaited, so building a RunStream costs nothing.
|
|
127
129
|
return new RunStream(this.#start(agent, input, opts));
|
|
128
130
|
}
|
|
129
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Continues a persisted run from its own snapshot. A run that stopped
|
|
133
|
+
* `suspended` continues once `answers` answer its suspended calls.
|
|
134
|
+
*/
|
|
130
135
|
resume(state, opts = {}) {
|
|
131
|
-
return new RunStream(this.#
|
|
136
|
+
return new RunStream(this.#resumeAndDrive(state, opts));
|
|
132
137
|
}
|
|
133
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Adds a follow-up message to a finished (or paused) run and continues it.
|
|
140
|
+
* Calls still suspended are cancelled first: the person chose to say
|
|
141
|
+
* something else rather than answer them.
|
|
142
|
+
*/
|
|
134
143
|
send(state, input, opts = {}) {
|
|
135
144
|
return new RunStream(this.#sendAndDrive(state, input, opts));
|
|
136
145
|
}
|
|
146
|
+
async *#resumeAndDrive(state, opts) {
|
|
147
|
+
const answered = yield* this.#answer(state, opts.answers ?? {}, opts);
|
|
148
|
+
return yield* this.#drive(answered, opts);
|
|
149
|
+
}
|
|
137
150
|
async *#sendAndDrive(state, input, opts) {
|
|
138
|
-
const
|
|
151
|
+
const cancel = {};
|
|
152
|
+
for (const s of state.suspended ?? []) {
|
|
153
|
+
cancel[s.callId] = { status: 'cancelled', message: SUPERSEDED_MESSAGE };
|
|
154
|
+
}
|
|
155
|
+
const answered = yield* this.#answer(state, cancel, opts);
|
|
156
|
+
const next = await Kernel.applyUserInput(answered, input, this.#env(opts));
|
|
139
157
|
return yield* this.#drive(next, opts);
|
|
140
158
|
}
|
|
159
|
+
/** Applies answers to suspended calls; answers to anything else are ignored. */
|
|
160
|
+
async *#answer(initial, answers, opts) {
|
|
161
|
+
const env = this.#env(opts);
|
|
162
|
+
let state = initial;
|
|
163
|
+
for (const s of initial.suspended ?? []) {
|
|
164
|
+
const answer = answers[s.callId];
|
|
165
|
+
if (!answer) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (answer.status === 'resolved' && 'rerun' in answer) {
|
|
169
|
+
state = Kernel.releaseSuspension(state, s.callId);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const outcome = answer.status === 'resolved'
|
|
173
|
+
? { output: stringify(answer.payload), isError: false }
|
|
174
|
+
: { output: answer.message ?? 'cancelled: nobody answered', isError: true };
|
|
175
|
+
const was = state;
|
|
176
|
+
state = await Kernel.applyToolResult(state, s.callId, outcome, env);
|
|
177
|
+
const node = appended(was, state).find((n) => n.type === 'tool_result');
|
|
178
|
+
if (node) {
|
|
179
|
+
yield tag(state, undefined, { type: 'after_tool_call', state, node });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return state;
|
|
183
|
+
}
|
|
141
184
|
#env(opts) {
|
|
142
185
|
return {
|
|
143
186
|
services: this.services,
|
|
@@ -207,11 +250,23 @@ export class AgentRunner {
|
|
|
207
250
|
break;
|
|
208
251
|
}
|
|
209
252
|
case 'tools': {
|
|
253
|
+
// A suspended call waits for `resume`; when nothing else is left
|
|
254
|
+
// to run, the run stops here and says what it waits for.
|
|
255
|
+
const waiting = new Set((state.suspended ?? []).map((x) => x.callId));
|
|
256
|
+
const runnable = action.calls.filter((c) => !waiting.has(c.callId));
|
|
257
|
+
if (!runnable.length) {
|
|
258
|
+
return yield* this.#finish(state, branch, 'suspended');
|
|
259
|
+
}
|
|
210
260
|
const tools = await Kernel.resolveTools(state, this.registry, env);
|
|
211
|
-
for (const call of
|
|
261
|
+
for (const call of runnable) {
|
|
212
262
|
yield tag(state, branch, { type: 'before_tool_call', state, call });
|
|
213
263
|
const was = state;
|
|
214
|
-
const
|
|
264
|
+
const ran = await this.#runTool(state, tools, call, opts);
|
|
265
|
+
if ('suspend' in ran) {
|
|
266
|
+
state = Kernel.applySuspension(state, call, ran.suspend, this.#clock.now());
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const outcome = ran;
|
|
215
270
|
state = await Kernel.applyToolResult(state, call.callId, outcome, env);
|
|
216
271
|
const added = appended(was, state);
|
|
217
272
|
const node = added.find((n) => n.type === 'tool_result');
|
|
@@ -465,6 +520,15 @@ export class AgentRunner {
|
|
|
465
520
|
services: this.services,
|
|
466
521
|
signal: opts.signal,
|
|
467
522
|
});
|
|
523
|
+
if (isToolSuspend(out)) {
|
|
524
|
+
// A branch runs alongside its siblings and is joined when all end:
|
|
525
|
+
// there is no run of its own to stop and resume.
|
|
526
|
+
// Its message still reaches the model, which can pass it on.
|
|
527
|
+
return opts.branch
|
|
528
|
+
? fail(`error: ${def.name} needs a person and cannot wait inside a fork branch` +
|
|
529
|
+
(out.spec.message ? `: ${out.spec.message}` : ''))
|
|
530
|
+
: { suspend: out.spec };
|
|
531
|
+
}
|
|
468
532
|
const effects = isToolReturn(out) ? out.effects : [];
|
|
469
533
|
const value = isToolReturn(out) ? out.output : out;
|
|
470
534
|
return {
|
package/dist/state.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PayloadResolver } from './payload.ts';
|
|
2
2
|
import { type TrajectoryNode } from './trajectory.ts';
|
|
3
|
-
import type { JsonSchema, TokenUsage } from './types.ts';
|
|
3
|
+
import type { JsonSchema, Suspension, TokenUsage } from './types.ts';
|
|
4
4
|
export type RunPhase =
|
|
5
5
|
/** initial context built, nothing executed */
|
|
6
6
|
'created'
|
|
@@ -56,6 +56,11 @@ export interface AgentState {
|
|
|
56
56
|
callId: string;
|
|
57
57
|
branches: string[];
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Pending tool calls the run is suspended on, waiting for a person (see
|
|
61
|
+
* `suspend`). Each is also in `pendingToolCalls`; `runner.resume` answers them.
|
|
62
|
+
*/
|
|
63
|
+
suspended?: Suspension[];
|
|
59
64
|
/** derived cache; always recomputable from the trajectory */
|
|
60
65
|
usage: TokenUsage;
|
|
61
66
|
/** serialized user context (must be JSON) */
|
|
@@ -69,7 +74,8 @@ export interface RunResult<T = string> {
|
|
|
69
74
|
agent: string;
|
|
70
75
|
state: AgentState;
|
|
71
76
|
usage: TokenUsage;
|
|
72
|
-
|
|
77
|
+
/** suspended: waiting on `state.suspended`; `runner.resume` continues it */
|
|
78
|
+
stopReason: 'final' | 'aborted' | 'failed' | 'suspended';
|
|
73
79
|
}
|
|
74
80
|
/**
|
|
75
81
|
* Structural check for a rehydrated state. There is no prototype to restore —
|
package/dist/types.d.ts
CHANGED
|
@@ -144,6 +144,47 @@ export interface ToolReturn {
|
|
|
144
144
|
}
|
|
145
145
|
export declare function withEffects(output: unknown, ...effects: ToolEffect[]): ToolReturn;
|
|
146
146
|
export declare function isToolReturn(v: unknown): v is ToolReturn;
|
|
147
|
+
declare const TOOL_SUSPEND: unique symbol;
|
|
148
|
+
export interface SuspendSpec {
|
|
149
|
+
/** what the run waits for, in the host's own words (e.g. "mcp_auth", "input") */
|
|
150
|
+
reason: string;
|
|
151
|
+
/** a human-readable prompt for whoever answers */
|
|
152
|
+
message?: string;
|
|
153
|
+
/** anything the host needs to resolve it; must be JSON */
|
|
154
|
+
data?: unknown;
|
|
155
|
+
/** the shape of the answer expected, when the answer becomes the result */
|
|
156
|
+
responseSchema?: Record<string, unknown>;
|
|
157
|
+
}
|
|
158
|
+
export interface ToolSuspend {
|
|
159
|
+
[TOOL_SUSPEND]: true;
|
|
160
|
+
spec: SuspendSpec;
|
|
161
|
+
}
|
|
162
|
+
export declare function suspend(spec: SuspendSpec): ToolSuspend;
|
|
163
|
+
export declare function isToolSuspend(v: unknown): v is ToolSuspend;
|
|
164
|
+
/** A pending tool call a run is suspended on. Lives in `AgentState.suspended`. */
|
|
165
|
+
export interface Suspension extends SuspendSpec {
|
|
166
|
+
callId: string;
|
|
167
|
+
/** the tool that suspended */
|
|
168
|
+
name: string;
|
|
169
|
+
/** when it suspended, as the run's clock writes times */
|
|
170
|
+
at: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The answer to one suspension, given to `runner.resume`.
|
|
174
|
+
* resolved + rerun - what was needed is done; run the call again
|
|
175
|
+
* resolved + payload - the payload is the call's result
|
|
176
|
+
* cancelled - nobody will answer; the call gets `message` as an error
|
|
177
|
+
*/
|
|
178
|
+
export type ResumeAnswer = {
|
|
179
|
+
status: 'resolved';
|
|
180
|
+
rerun: true;
|
|
181
|
+
} | {
|
|
182
|
+
status: 'resolved';
|
|
183
|
+
payload: unknown;
|
|
184
|
+
} | {
|
|
185
|
+
status: 'cancelled';
|
|
186
|
+
message?: string;
|
|
187
|
+
};
|
|
147
188
|
export interface Tool<TArgs = unknown, TCtx = unknown> extends ToolSchema {
|
|
148
189
|
/**
|
|
149
190
|
* The family this tool belongs to, so config can name the whole set at
|
package/dist/types.js
CHANGED
|
@@ -50,6 +50,27 @@ export function withEffects(output, ...effects) {
|
|
|
50
50
|
export function isToolReturn(v) {
|
|
51
51
|
return v !== null && typeof v === 'object' && TOOL_RETURN in v;
|
|
52
52
|
}
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// Suspending a run
|
|
55
|
+
//
|
|
56
|
+
// Some tools cannot finish without a person: an account to connect, a value
|
|
57
|
+
// only someone knows, an approval. Such a tool returns `suspend(...)` instead
|
|
58
|
+
// of a result. The run then stops with the call still pending - nothing is
|
|
59
|
+
// held while it waits - and a later `runner.resume(state, { answers })` either
|
|
60
|
+
// runs the call again (the person did what was needed) or hands the call an
|
|
61
|
+
// answer as its result. What the pause is about is the host's business: the
|
|
62
|
+
// runner only carries `reason`, `message` and `data` through to whoever shows
|
|
63
|
+
// it, and `responseSchema` to whoever collects the answer.
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Registered (`Symbol.for`) so a plugin with no dependencies can suspend too:
|
|
66
|
+
// `{ [Symbol.for('zenera.neo.toolSuspend')]: true, spec }` is what `suspend` makes.
|
|
67
|
+
const TOOL_SUSPEND = Symbol.for('zenera.neo.toolSuspend');
|
|
68
|
+
export function suspend(spec) {
|
|
69
|
+
return { [TOOL_SUSPEND]: true, spec };
|
|
70
|
+
}
|
|
71
|
+
export function isToolSuspend(v) {
|
|
72
|
+
return v !== null && typeof v === 'object' && TOOL_SUSPEND in v;
|
|
73
|
+
}
|
|
53
74
|
/**
|
|
54
75
|
* Identity helper. Its only job is inference: written inline, `execute`'s
|
|
55
76
|
* `args` would be `unknown`; going through `tool<{city: string}>()` types it.
|