@ank1015/agents-runtime 0.1.11
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/CHANGELOG.md +21 -0
- package/README.md +92 -0
- package/dist/agent.d.ts +42 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +102 -0
- package/dist/agent.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel/context.d.ts +28 -0
- package/dist/kernel/context.d.ts.map +1 -0
- package/dist/kernel/context.js +31 -0
- package/dist/kernel/context.js.map +1 -0
- package/dist/kernel/driver.d.ts +27 -0
- package/dist/kernel/driver.d.ts.map +1 -0
- package/dist/kernel/driver.js +479 -0
- package/dist/kernel/driver.js.map +1 -0
- package/dist/plugins/registry.d.ts +14 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +53 -0
- package/dist/plugins/registry.js.map +1 -0
- package/dist/stream/consume.d.ts +10 -0
- package/dist/stream/consume.d.ts.map +1 -0
- package/dist/stream/consume.js +25 -0
- package/dist/stream/consume.js.map +1 -0
- package/dist/tools/build-result.d.ts +14 -0
- package/dist/tools/build-result.d.ts.map +1 -0
- package/dist/tools/build-result.js +32 -0
- package/dist/tools/build-result.js.map +1 -0
- package/dist/tools/define-tool.d.ts +16 -0
- package/dist/tools/define-tool.d.ts.map +1 -0
- package/dist/tools/define-tool.js +18 -0
- package/dist/tools/define-tool.js.map +1 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +5 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +3 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +28 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/validate.d.ts +15 -0
- package/dist/tools/validate.d.ts.map +1 -0
- package/dist/tools/validate.js +35 -0
- package/dist/tools/validate.js.map +1 -0
- package/dist/utils/abort.d.ts +5 -0
- package/dist/utils/abort.d.ts.map +1 -0
- package/dist/utils/abort.js +13 -0
- package/dist/utils/abort.js.map +1 -0
- package/dist/utils/content.d.ts +8 -0
- package/dist/utils/content.d.ts.map +1 -0
- package/dist/utils/content.js +19 -0
- package/dist/utils/content.js.map +1 -0
- package/dist/utils/errors.d.ts +16 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +43 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/ids.d.ts +6 -0
- package/dist/utils/ids.d.ts.map +1 -0
- package/dist/utils/ids.js +12 -0
- package/dist/utils/ids.js.map +1 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/time.d.ts +5 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +5 -0
- package/dist/utils/time.js.map +1 -0
- package/dist/utils/usage.d.ts +9 -0
- package/dist/utils/usage.d.ts.map +1 -0
- package/dist/utils/usage.js +15 -0
- package/dist/utils/usage.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { consumeModelStream } from '../stream/consume.js';
|
|
2
|
+
import { buildToolResultMessage } from '../tools/build-result.js';
|
|
3
|
+
import { validateToolArguments } from '../tools/validate.js';
|
|
4
|
+
import { costTotal, createAgentError, getErrorMessage, isAbortError, textContent, toToolResultError, tokenTotal, } from '../utils/index.js';
|
|
5
|
+
import { makeStageContext } from './context.js';
|
|
6
|
+
/**
|
|
7
|
+
* Per-run executor of the 5-edge / 2-node loop. Every stage is dispatched
|
|
8
|
+
* through {@link runStage}, which composes the stage's interceptors as an onion
|
|
9
|
+
* around the stage's real work. The loop owns the run invariants (one terminal
|
|
10
|
+
* event, tool-call/result pairing, abort propagation, continue ownership);
|
|
11
|
+
* everything else is a plugin's job.
|
|
12
|
+
*/
|
|
13
|
+
export class AgentLoop {
|
|
14
|
+
#transport;
|
|
15
|
+
#tools;
|
|
16
|
+
#interceptors;
|
|
17
|
+
#baseConfig;
|
|
18
|
+
#ready;
|
|
19
|
+
#createId;
|
|
20
|
+
#now;
|
|
21
|
+
#abortController = new AbortController();
|
|
22
|
+
#signal;
|
|
23
|
+
#state = freshState();
|
|
24
|
+
#config;
|
|
25
|
+
#metadata = {};
|
|
26
|
+
#transportMetadata;
|
|
27
|
+
#inbound = [];
|
|
28
|
+
#stopRequested = false;
|
|
29
|
+
#stopReason;
|
|
30
|
+
#continueRequested = false;
|
|
31
|
+
#pendingTerminate = false;
|
|
32
|
+
#source;
|
|
33
|
+
#runStartLen = 0;
|
|
34
|
+
#bindings;
|
|
35
|
+
constructor(deps) {
|
|
36
|
+
this.#transport = deps.transport;
|
|
37
|
+
this.#tools = deps.tools;
|
|
38
|
+
this.#interceptors = deps.interceptors;
|
|
39
|
+
this.#baseConfig = deps.config;
|
|
40
|
+
this.#ready = deps.ready;
|
|
41
|
+
this.#createId = deps.createId;
|
|
42
|
+
this.#now = deps.now;
|
|
43
|
+
this.#config = { ...deps.config };
|
|
44
|
+
this.#signal = this.#abortController.signal;
|
|
45
|
+
}
|
|
46
|
+
abort() {
|
|
47
|
+
this.#abortController.abort();
|
|
48
|
+
}
|
|
49
|
+
send(message) {
|
|
50
|
+
this.#inbound.push(message);
|
|
51
|
+
}
|
|
52
|
+
// drive() is launched detached, so it must never reject and must always end
|
|
53
|
+
// the stream — otherwise a consumer awaiting result() hangs forever.
|
|
54
|
+
async drive(input, source) {
|
|
55
|
+
this.#source = source;
|
|
56
|
+
this.#state = freshState();
|
|
57
|
+
this.#config = { ...this.#baseConfig };
|
|
58
|
+
this.#metadata = { ...(input.metadata ?? {}) };
|
|
59
|
+
this.#transportMetadata = input.transportMetadata;
|
|
60
|
+
this.#runStartLen = 0;
|
|
61
|
+
this.#wireSignal(input.signal);
|
|
62
|
+
this.#bindings = this.#makeBindings();
|
|
63
|
+
try {
|
|
64
|
+
await this.#ready;
|
|
65
|
+
await this.#runStage('input', {}, async () => {
|
|
66
|
+
this.#state.messages = [...input.messages];
|
|
67
|
+
});
|
|
68
|
+
this.#runStartLen = this.#state.messages.length;
|
|
69
|
+
this.#emit({ type: 'run_start' });
|
|
70
|
+
for (;;) {
|
|
71
|
+
// continue() is a per-turn request: clear it at the top of every turn so a
|
|
72
|
+
// continue() issued during a tool turn (which already loops on its own) can't
|
|
73
|
+
// leak forward and force a spurious extra turn after the final answer.
|
|
74
|
+
this.#continueRequested = false;
|
|
75
|
+
this.#drainInbound();
|
|
76
|
+
if (this.#signal.aborted) {
|
|
77
|
+
this.#abortRun();
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const turn = (this.#state.turn += 1);
|
|
81
|
+
this.#emit({ type: 'turn_start', turn });
|
|
82
|
+
let request;
|
|
83
|
+
try {
|
|
84
|
+
request = await this.#runStage('request', {}, async () => this.#buildRequest());
|
|
85
|
+
}
|
|
86
|
+
catch (cause) {
|
|
87
|
+
this.#failFromThrow('plugin', cause);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
let assistant;
|
|
91
|
+
try {
|
|
92
|
+
assistant = await this.#runStage('model', { request }, async (ctx) => this.#callModel(ctx.request, ctx));
|
|
93
|
+
}
|
|
94
|
+
catch (cause) {
|
|
95
|
+
if (isAbortError(cause, this.#signal)) {
|
|
96
|
+
this.#abortRun();
|
|
97
|
+
}
|
|
98
|
+
else if (cause instanceof ModelInvocationError) {
|
|
99
|
+
// The transport/stream threw — a genuine model failure.
|
|
100
|
+
this.#failFromThrow('model', cause.inner);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// A model-stage interceptor threw — attribute to the plugin, not the model.
|
|
104
|
+
this.#failFromThrow('plugin', cause);
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
// Account usage for every attempt that returns a message — failed and
|
|
109
|
+
// aborted messages can still carry tokens the provider consumed.
|
|
110
|
+
this.#accumulateUsage(assistant);
|
|
111
|
+
if (this.#signal.aborted || assistant.stopReason === 'aborted') {
|
|
112
|
+
this.#abortRun();
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (assistant.stopReason === 'error') {
|
|
116
|
+
this.#failFromAssistant(assistant);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
this.#state.assistant = assistant;
|
|
120
|
+
this.#commit(assistant);
|
|
121
|
+
await this.#runStage('response', {}, async () => { });
|
|
122
|
+
// Abort is a hard cancel: re-check after every interceptor-running stage,
|
|
123
|
+
// since an abort can land while an async interceptor is in flight.
|
|
124
|
+
if (this.#signal.aborted) {
|
|
125
|
+
this.#abortRun();
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
const toolCalls = assistant.content.filter((part) => part.type === 'toolCall');
|
|
129
|
+
if (toolCalls.length === 0) {
|
|
130
|
+
await this.#runStage('finish', {}, async () => { });
|
|
131
|
+
if (this.#signal.aborted) {
|
|
132
|
+
this.#abortRun();
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
this.#emit({ type: 'turn_end', turn });
|
|
136
|
+
if (this.#stopRequested) {
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
if (this.#continueRequested) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (this.#inbound.length > 0) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const aborted = await this.#runToolCalls(toolCalls);
|
|
148
|
+
if (aborted) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
await this.#runStage('result', {}, async () => { });
|
|
152
|
+
if (this.#signal.aborted) {
|
|
153
|
+
this.#abortRun();
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
this.#emit({ type: 'turn_end', turn });
|
|
157
|
+
if (this.#stopRequested || this.#pendingTerminate) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.#finish();
|
|
162
|
+
}
|
|
163
|
+
catch (cause) {
|
|
164
|
+
this.#endDefensive(cause);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async #runStage(stage, extras, work) {
|
|
168
|
+
const ctx = makeStageContext(this.#bindings, stage, extras);
|
|
169
|
+
const interceptors = this.#interceptors[stage];
|
|
170
|
+
let next = () => work(ctx);
|
|
171
|
+
for (let i = interceptors.length - 1; i >= 0; i -= 1) {
|
|
172
|
+
const handler = interceptors[i];
|
|
173
|
+
const inner = next;
|
|
174
|
+
next = () => handler(ctx, inner);
|
|
175
|
+
}
|
|
176
|
+
return next();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Runs the transport call inside the model node. Wraps a genuine transport
|
|
180
|
+
* throw in {@link ModelInvocationError} so the loop can tell it apart from a
|
|
181
|
+
* model-stage interceptor throwing (which is a plugin fault, not a model one).
|
|
182
|
+
* Aborts pass through unwrapped so the loop's abort handling stays in charge.
|
|
183
|
+
*/
|
|
184
|
+
async #callModel(request, ctx) {
|
|
185
|
+
try {
|
|
186
|
+
return await consumeModelStream(this.#transport.stream(request), ctx);
|
|
187
|
+
}
|
|
188
|
+
catch (cause) {
|
|
189
|
+
if (isAbortError(cause, this.#signal)) {
|
|
190
|
+
throw cause;
|
|
191
|
+
}
|
|
192
|
+
throw new ModelInvocationError(cause);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async #runToolCalls(toolCalls) {
|
|
196
|
+
this.#pendingTerminate = false;
|
|
197
|
+
for (let i = 0; i < toolCalls.length; i += 1) {
|
|
198
|
+
if (this.#signal.aborted) {
|
|
199
|
+
this.#cancelToolCalls(toolCalls, i, false);
|
|
200
|
+
this.#abortRun();
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
const call = toolCalls[i];
|
|
204
|
+
this.#emit({
|
|
205
|
+
type: 'tool_start',
|
|
206
|
+
toolCallId: call.toolCallId,
|
|
207
|
+
toolName: call.name,
|
|
208
|
+
arguments: call.arguments,
|
|
209
|
+
});
|
|
210
|
+
let result;
|
|
211
|
+
try {
|
|
212
|
+
result = await this.#runStage('tool', { call: copyToolCall(call) }, async (ctx) => this.#executeTool(ctx.call));
|
|
213
|
+
}
|
|
214
|
+
catch (cause) {
|
|
215
|
+
if (isAbortError(cause, this.#signal)) {
|
|
216
|
+
this.#cancelToolCalls(toolCalls, i, true);
|
|
217
|
+
this.#abortRun();
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
throw cause;
|
|
221
|
+
}
|
|
222
|
+
// Pairing is the kernel's invariant, not the plugin's: a short-circuiting
|
|
223
|
+
// interceptor may return a result with mismatched ids, so pin the committed
|
|
224
|
+
// message to the call it answers — otherwise replayed provider history would
|
|
225
|
+
// contain a tool_result that pairs to no tool_call.
|
|
226
|
+
const paired = pairToolResult(result, call);
|
|
227
|
+
this.#emit({
|
|
228
|
+
type: 'tool_end',
|
|
229
|
+
toolCallId: call.toolCallId,
|
|
230
|
+
toolName: call.name,
|
|
231
|
+
result: toExecutionResult(paired),
|
|
232
|
+
isError: paired.isError,
|
|
233
|
+
});
|
|
234
|
+
this.#commit(paired);
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
async #executeTool(call) {
|
|
239
|
+
const tool = this.#tools.get(call.name);
|
|
240
|
+
if (!tool) {
|
|
241
|
+
const message = `Unknown tool: ${call.name}`;
|
|
242
|
+
return buildToolResultMessage(call, { content: textContent(message), isError: true }, true, { message, name: 'ToolNotFoundError' }, { id: this.#createId('tr'), timestamp: this.#now() });
|
|
243
|
+
}
|
|
244
|
+
let result;
|
|
245
|
+
let thrownDetails;
|
|
246
|
+
try {
|
|
247
|
+
const args = validateToolArguments(tool, call);
|
|
248
|
+
result = await tool.execute({
|
|
249
|
+
toolCallId: call.toolCallId,
|
|
250
|
+
args,
|
|
251
|
+
context: { messages: this.#state.messages },
|
|
252
|
+
signal: this.#signal,
|
|
253
|
+
onUpdate: (update) => this.#emit({
|
|
254
|
+
type: 'tool_update',
|
|
255
|
+
toolCallId: call.toolCallId,
|
|
256
|
+
toolName: call.name,
|
|
257
|
+
update,
|
|
258
|
+
}),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
catch (cause) {
|
|
262
|
+
if (isAbortError(cause, this.#signal)) {
|
|
263
|
+
throw cause;
|
|
264
|
+
}
|
|
265
|
+
result = { content: textContent(getErrorMessage(cause)), isError: true };
|
|
266
|
+
thrownDetails = toToolResultError(cause);
|
|
267
|
+
}
|
|
268
|
+
if (result.terminate === true) {
|
|
269
|
+
this.#pendingTerminate = true;
|
|
270
|
+
}
|
|
271
|
+
return buildToolResultMessage(call, result, result.isError === true, thrownDetails, {
|
|
272
|
+
id: this.#createId('tr'),
|
|
273
|
+
timestamp: this.#now(),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Commits synthetic cancelled results for `toolCalls[fromIndex..]` so an abort
|
|
278
|
+
* never strands an assistant `tool_use` without a paired `tool_result` (which
|
|
279
|
+
* providers reject when the history is resumed).
|
|
280
|
+
*/
|
|
281
|
+
#cancelToolCalls(toolCalls, fromIndex, firstAlreadyStarted) {
|
|
282
|
+
for (let i = fromIndex; i < toolCalls.length; i += 1) {
|
|
283
|
+
const call = toolCalls[i];
|
|
284
|
+
if (i > fromIndex || !firstAlreadyStarted) {
|
|
285
|
+
this.#emit({
|
|
286
|
+
type: 'tool_start',
|
|
287
|
+
toolCallId: call.toolCallId,
|
|
288
|
+
toolName: call.name,
|
|
289
|
+
arguments: call.arguments,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const result = {
|
|
293
|
+
content: textContent('Tool execution cancelled.'),
|
|
294
|
+
isError: true,
|
|
295
|
+
};
|
|
296
|
+
this.#emit({
|
|
297
|
+
type: 'tool_end',
|
|
298
|
+
toolCallId: call.toolCallId,
|
|
299
|
+
toolName: call.name,
|
|
300
|
+
result,
|
|
301
|
+
isError: true,
|
|
302
|
+
});
|
|
303
|
+
this.#commit(buildToolResultMessage(call, result, true, { message: 'Tool execution cancelled', name: 'AbortError' }, { id: this.#createId('tr'), timestamp: this.#now() }));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
#buildRequest() {
|
|
307
|
+
const config = this.#config;
|
|
308
|
+
const request = {
|
|
309
|
+
provider: config.provider,
|
|
310
|
+
modelId: config.modelId,
|
|
311
|
+
messages: [...this.#state.messages],
|
|
312
|
+
};
|
|
313
|
+
if (config.instructions !== undefined) {
|
|
314
|
+
request.instructions = config.instructions;
|
|
315
|
+
}
|
|
316
|
+
if (config.providerOptions !== undefined) {
|
|
317
|
+
request.providerOptions = config.providerOptions;
|
|
318
|
+
}
|
|
319
|
+
const tools = this.#tools.list().map((tool) => tool.definition);
|
|
320
|
+
if (tools.length > 0) {
|
|
321
|
+
request.tools = tools;
|
|
322
|
+
}
|
|
323
|
+
if (this.#transportMetadata !== undefined) {
|
|
324
|
+
// Copy per turn so a model-stage plugin mutating ctx.request.metadata
|
|
325
|
+
// can't leak across turns or back into the caller's object.
|
|
326
|
+
request.metadata = { ...this.#transportMetadata };
|
|
327
|
+
}
|
|
328
|
+
request.signal = this.#signal;
|
|
329
|
+
return request;
|
|
330
|
+
}
|
|
331
|
+
#makeBindings() {
|
|
332
|
+
return {
|
|
333
|
+
state: this.#state,
|
|
334
|
+
getConfig: () => this.#config,
|
|
335
|
+
setConfig: (config) => {
|
|
336
|
+
this.#config = config;
|
|
337
|
+
},
|
|
338
|
+
signal: this.#signal,
|
|
339
|
+
metadata: this.#metadata,
|
|
340
|
+
transport: this.#transport,
|
|
341
|
+
tools: this.#tools,
|
|
342
|
+
emit: (event) => this.#emit(event),
|
|
343
|
+
addMessage: (message) => this.#commit(message),
|
|
344
|
+
updateMessages: (update) => {
|
|
345
|
+
this.#state.messages = update(this.#state.messages);
|
|
346
|
+
},
|
|
347
|
+
requestContinue: () => {
|
|
348
|
+
this.#continueRequested = true;
|
|
349
|
+
},
|
|
350
|
+
requestStop: (reason) => {
|
|
351
|
+
this.#stopRequested = true;
|
|
352
|
+
if (reason !== undefined) {
|
|
353
|
+
this.#stopReason = reason;
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
#wireSignal(external) {
|
|
359
|
+
if (external === undefined) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (external.aborted) {
|
|
363
|
+
this.#abortController.abort();
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
external.addEventListener('abort', () => this.#abortController.abort(), { once: true });
|
|
367
|
+
}
|
|
368
|
+
#drainInbound() {
|
|
369
|
+
if (this.#inbound.length === 0) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
for (const message of this.#inbound.splice(0)) {
|
|
373
|
+
this.#commit(message);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
#commit(message) {
|
|
377
|
+
this.#state.messages.push(message);
|
|
378
|
+
this.#emit({ type: 'message_end', message });
|
|
379
|
+
}
|
|
380
|
+
#accumulateUsage(assistant) {
|
|
381
|
+
this.#state.usage.totalCost += costTotal(assistant.usage);
|
|
382
|
+
this.#state.usage.totalTokens += tokenTotal(assistant.usage);
|
|
383
|
+
}
|
|
384
|
+
#abortRun() {
|
|
385
|
+
this.#state.status = 'aborted';
|
|
386
|
+
this.#emit({ type: 'aborted' });
|
|
387
|
+
}
|
|
388
|
+
#failFromThrow(phase, cause) {
|
|
389
|
+
const error = createAgentError({ phase, cause, canRetry: false });
|
|
390
|
+
this.#state.status = 'error';
|
|
391
|
+
this.#state.error = error;
|
|
392
|
+
this.#emit({ type: 'error', error });
|
|
393
|
+
}
|
|
394
|
+
#failFromAssistant(assistant) {
|
|
395
|
+
const error = createAgentError({
|
|
396
|
+
phase: 'model',
|
|
397
|
+
message: assistant.error.message,
|
|
398
|
+
canRetry: assistant.error.canRetry,
|
|
399
|
+
...(assistant.error.attempts !== undefined ? { attempts: assistant.error.attempts } : {}),
|
|
400
|
+
assistantMessage: assistant,
|
|
401
|
+
});
|
|
402
|
+
this.#state.status = 'error';
|
|
403
|
+
this.#state.error = error;
|
|
404
|
+
this.#emit({ type: 'error', error });
|
|
405
|
+
}
|
|
406
|
+
#finish() {
|
|
407
|
+
const result = this.#buildResult();
|
|
408
|
+
this.#emit({ type: 'run_end', result });
|
|
409
|
+
this.#source?.end(result);
|
|
410
|
+
}
|
|
411
|
+
#endDefensive(cause) {
|
|
412
|
+
const error = createAgentError({ phase: 'plugin', cause, canRetry: false });
|
|
413
|
+
this.#state.status = 'error';
|
|
414
|
+
this.#state.error = error;
|
|
415
|
+
this.#emit({ type: 'error', error });
|
|
416
|
+
const result = this.#buildResult();
|
|
417
|
+
this.#emit({ type: 'run_end', result });
|
|
418
|
+
this.#source?.end(result);
|
|
419
|
+
}
|
|
420
|
+
#buildResult() {
|
|
421
|
+
const status = this.#state.status === 'running' ? 'done' : this.#state.status;
|
|
422
|
+
this.#state.status = status;
|
|
423
|
+
const result = {
|
|
424
|
+
state: this.#state,
|
|
425
|
+
newMessages: this.#state.messages.slice(this.#runStartLen),
|
|
426
|
+
status,
|
|
427
|
+
};
|
|
428
|
+
if (this.#state.error !== undefined) {
|
|
429
|
+
result.error = this.#state.error;
|
|
430
|
+
}
|
|
431
|
+
if (this.#stopReason !== undefined) {
|
|
432
|
+
result.stopReason = this.#stopReason;
|
|
433
|
+
}
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
#emit(event) {
|
|
437
|
+
this.#source?.push(event);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Internal marker: a throw that originated in the transport/stream (the model
|
|
442
|
+
* node's real work) rather than in a model-stage interceptor. Lets the loop
|
|
443
|
+
* attribute the former to the `model` phase and the latter to `plugin`.
|
|
444
|
+
*/
|
|
445
|
+
class ModelInvocationError extends Error {
|
|
446
|
+
inner;
|
|
447
|
+
constructor(inner) {
|
|
448
|
+
super('Model invocation threw');
|
|
449
|
+
this.name = 'ModelInvocationError';
|
|
450
|
+
this.inner = inner;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function freshState() {
|
|
454
|
+
return { messages: [], turn: 0, usage: { totalCost: 0, totalTokens: 0 }, status: 'running' };
|
|
455
|
+
}
|
|
456
|
+
function copyToolCall(call) {
|
|
457
|
+
return {
|
|
458
|
+
...call,
|
|
459
|
+
arguments: typeof call.arguments === 'string' ? call.arguments : { ...call.arguments },
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function toExecutionResult(message) {
|
|
463
|
+
const result = { content: message.content, isError: message.isError };
|
|
464
|
+
if (message.details !== undefined) {
|
|
465
|
+
result.details = message.details;
|
|
466
|
+
}
|
|
467
|
+
return result;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Guarantees a tool result pairs to the call it answers. The plugin owns the
|
|
471
|
+
* result's content/error; the kernel owns its identity (`toolCallId`/`toolName`).
|
|
472
|
+
*/
|
|
473
|
+
function pairToolResult(result, call) {
|
|
474
|
+
if (result.toolCallId === call.toolCallId && result.toolName === call.name) {
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
return { ...result, toolCallId: call.toolCallId, toolName: call.name };
|
|
478
|
+
}
|
|
479
|
+
//# sourceMappingURL=driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.js","sourceRoot":"","sources":["../../src/kernel/driver.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,WAAW,EACX,iBAAiB,EACjB,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14 @@
|
|
|
1
|
+
import type { AgentPlugin, PluginApi, Stage, ToolRegistry } from '@ank1015/agents-contracts';
|
|
2
|
+
type AnyInterceptor = (ctx: never, next: () => Promise<unknown>) => Promise<unknown>;
|
|
3
|
+
export type InterceptorStore = Record<Stage, AnyInterceptor[]>;
|
|
4
|
+
export declare function createInterceptorStore(): InterceptorStore;
|
|
5
|
+
/**
|
|
6
|
+
* Builds the surface a plugin's `setup` uses. `on` is the full interceptor
|
|
7
|
+
* primitive; `before`/`after` are sugar that manage `next()` so a pure
|
|
8
|
+
* transform/observe handler can't stall the loop by forgetting it.
|
|
9
|
+
*/
|
|
10
|
+
export declare function makePluginApi(store: InterceptorStore, tools: ToolRegistry): PluginApi;
|
|
11
|
+
/** Runs each plugin's `setup` in order (awaiting async setups). */
|
|
12
|
+
export declare function installPlugins(plugins: readonly AgentPlugin[], api: PluginApi): Promise<void>;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/plugins/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAGX,SAAS,EACT,KAAK,EAEL,YAAY,EACb,MAAM,2BAA2B,CAAC;AAInC,KAAK,cAAc,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAYrF,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,KAAK,EAAE,cAAc,EAAE,CAAC,CAAC;AAE/D,wBAAgB,sBAAsB,IAAI,gBAAgB,CAMzD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,YAAY,GAAG,SAAS,CA8BrF;AAED,mEAAmE;AACnE,wBAAsB,cAAc,CAClC,OAAO,EAAE,SAAS,WAAW,EAAE,EAC/B,GAAG,EAAE,SAAS,GACb,OAAO,CAAC,IAAI,CAAC,CAIf"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const STAGES = [
|
|
2
|
+
'input',
|
|
3
|
+
'request',
|
|
4
|
+
'model',
|
|
5
|
+
'response',
|
|
6
|
+
'tool',
|
|
7
|
+
'result',
|
|
8
|
+
'finish',
|
|
9
|
+
];
|
|
10
|
+
export function createInterceptorStore() {
|
|
11
|
+
const store = {};
|
|
12
|
+
for (const stage of STAGES) {
|
|
13
|
+
store[stage] = [];
|
|
14
|
+
}
|
|
15
|
+
return store;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Builds the surface a plugin's `setup` uses. `on` is the full interceptor
|
|
19
|
+
* primitive; `before`/`after` are sugar that manage `next()` so a pure
|
|
20
|
+
* transform/observe handler can't stall the loop by forgetting it.
|
|
21
|
+
*/
|
|
22
|
+
export function makePluginApi(store, tools) {
|
|
23
|
+
return {
|
|
24
|
+
on(stage, interceptor) {
|
|
25
|
+
store[stage].push(interceptor);
|
|
26
|
+
},
|
|
27
|
+
before(stage, handler) {
|
|
28
|
+
const interceptor = async (ctx, next) => {
|
|
29
|
+
await handler(ctx);
|
|
30
|
+
return next();
|
|
31
|
+
};
|
|
32
|
+
store[stage].push(interceptor);
|
|
33
|
+
},
|
|
34
|
+
after(stage, handler) {
|
|
35
|
+
const interceptor = async (ctx, next) => {
|
|
36
|
+
const output = await next();
|
|
37
|
+
await handler(ctx);
|
|
38
|
+
return output;
|
|
39
|
+
};
|
|
40
|
+
store[stage].push(interceptor);
|
|
41
|
+
},
|
|
42
|
+
tool(tool) {
|
|
43
|
+
tools.register(tool);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Runs each plugin's `setup` in order (awaiting async setups). */
|
|
48
|
+
export async function installPlugins(plugins, api) {
|
|
49
|
+
for (const plugin of plugins) {
|
|
50
|
+
await plugin.setup(api);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/plugins/registry.ts"],"names":[],"mappings":"AAcA,MAAM,MAAM,GAAqB;IAC/B,OAAO;IACP,SAAS;IACT,OAAO;IACP,UAAU;IACV,MAAM;IACN,QAAQ;IACR,QAAQ;CACT,CAAC;AAIF,MAAM,UAAU,sBAAsB;IACpC,MAAM,KAAK,GAAG,EAAsB,CAAC;IACrC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IACpB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAuB,EAAE,KAAmB;IACxE,OAAO;QACL,EAAE,CAAkB,KAAQ,EAAE,WAA2B;YACvD,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,WAAwC,CAAC,CAAC;QAC9D,CAAC;QACD,MAAM,CACJ,KAAQ,EACR,OAAuD;YAEvD,MAAM,WAAW,GAAmB,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;gBACtD,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;gBACnB,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC,CAAC;YACF,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,WAAwC,CAAC,CAAC;QAC9D,CAAC;QACD,KAAK,CACH,KAAQ,EACR,OAAuD;YAEvD,MAAM,WAAW,GAAmB,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;gBACtD,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;gBAC5B,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;gBACnB,OAAO,MAAM,CAAC;YAChB,CAAC,CAAC;YACF,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,WAAwC,CAAC,CAAC;QAC9D,CAAC;QACD,IAAI,CAAC,IAAoB;YACvB,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,mEAAmE;AACnE,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAA+B,EAC/B,GAAc;IAEd,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AgentContext, AssistantMessage, AssistantMessageEventStream, Provider } from '@ank1015/agents-contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Drains a model-call stream: emits `message_start`, forwards each provider
|
|
4
|
+
* event as `message_delta`, and translates `retry` control events into
|
|
5
|
+
* `message_retry` (so consumers can reset rendered attempt content before the
|
|
6
|
+
* next attempt's deltas arrive). Returns the finalized message via the stream's
|
|
7
|
+
* non-consuming `result()`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function consumeModelStream<P extends Provider = Provider>(stream: AssistantMessageEventStream<P>, ctx: AgentContext<P>): Promise<AssistantMessage<P>>;
|
|
10
|
+
//# sourceMappingURL=consume.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consume.d.ts","sourceRoot":"","sources":["../../src/stream/consume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,2BAA2B,EAC3B,QAAQ,EACT,MAAM,2BAA2B,CAAC;AAEnC;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EACpE,MAAM,EAAE,2BAA2B,CAAC,CAAC,CAAC,EACtC,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAkB9B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drains a model-call stream: emits `message_start`, forwards each provider
|
|
3
|
+
* event as `message_delta`, and translates `retry` control events into
|
|
4
|
+
* `message_retry` (so consumers can reset rendered attempt content before the
|
|
5
|
+
* next attempt's deltas arrive). Returns the finalized message via the stream's
|
|
6
|
+
* non-consuming `result()`.
|
|
7
|
+
*/
|
|
8
|
+
export async function consumeModelStream(stream, ctx) {
|
|
9
|
+
ctx.emit({ type: 'message_start' });
|
|
10
|
+
for await (const event of stream) {
|
|
11
|
+
if (event.type === 'retry') {
|
|
12
|
+
ctx.emit({
|
|
13
|
+
type: 'message_retry',
|
|
14
|
+
attempt: event.attempt,
|
|
15
|
+
nextAttempt: event.nextAttempt,
|
|
16
|
+
reason: event.reason,
|
|
17
|
+
...(event.message !== undefined ? { message: event.message } : {}),
|
|
18
|
+
});
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
ctx.emit({ type: 'message_delta', event });
|
|
22
|
+
}
|
|
23
|
+
return stream.result();
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=consume.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consume.js","sourceRoot":"","sources":["../../src/stream/consume.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,MAAsC,EACtC,GAAoB;IAEpB,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC,CAAC;IAEpC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACjC,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAC3B,GAAG,CAAC,IAAI,CAAC;gBACP,IAAI,EAAE,eAAe;gBACrB,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,WAAW,EAAE,KAAK,CAAC,WAAW;gBAC9B,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,GAAG,CAAC,KAAK,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACnE,CAAC,CAAC;YACH,SAAS;QACX,CAAC;QACD,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AssistantToolCall, ToolExecutionResult, ToolResultError, ToolResultMessage } from '@ank1015/agents-contracts';
|
|
2
|
+
export interface BuildToolResultOptions {
|
|
3
|
+
/** Override the generated message id. */
|
|
4
|
+
id?: string;
|
|
5
|
+
/** Override the generated timestamp. */
|
|
6
|
+
timestamp?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a {@link ToolExecutionResult} into a persisted `ToolResultMessage`
|
|
10
|
+
* (successful or failed variant). For failures, `errorDetails` is used or a
|
|
11
|
+
* generic error is synthesized.
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildToolResultMessage(toolCall: AssistantToolCall, result: ToolExecutionResult, isError: boolean, errorDetails?: ToolResultError, options?: BuildToolResultOptions): ToolResultMessage;
|
|
14
|
+
//# sourceMappingURL=build-result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-result.d.ts","sourceRoot":"","sources":["../../src/tools/build-result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EAGjB,mBAAmB,EACnB,eAAe,EACf,iBAAiB,EAClB,MAAM,2BAA2B,CAAC;AAInC,MAAM,WAAW,sBAAsB;IACrC,yCAAyC;IACzC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,wCAAwC;IACxC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,iBAAiB,EAC3B,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,OAAO,EAChB,YAAY,CAAC,EAAE,eAAe,EAC9B,OAAO,GAAE,sBAA2B,GACnC,iBAAiB,CAyBnB"}
|