@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
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@ank1015/agents-runtime` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## 0.1.11
|
|
6
|
+
|
|
7
|
+
- First public npm release of the generic agent loop runtime.
|
|
8
|
+
- Depends on `@ank1015/agents-contracts@^0.1.11`.
|
|
9
|
+
|
|
10
|
+
## 0.1.1
|
|
11
|
+
|
|
12
|
+
- Internal workspace package version prior to first public publish.
|
|
13
|
+
|
|
14
|
+
## 0.1.0 - Initial Release
|
|
15
|
+
|
|
16
|
+
- Added the `Agent` container with transport, tool registry, plugin interceptors, and config.
|
|
17
|
+
- Added per-run `AgentRun` handles with event streaming, `abort()`, and `send()` steering.
|
|
18
|
+
- Added `defineTool`, tool registry, and argument validation helpers.
|
|
19
|
+
- Added interceptor-based plugin installation across loop stages.
|
|
20
|
+
- Added utility helpers for ids, time, content, usage, errors, and abort.
|
|
21
|
+
- Added grouped package exports for `tools` and `utils`.
|
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# @ank1015/agents-runtime
|
|
2
|
+
|
|
3
|
+
Generic agent loop runtime for the `@ank1015/agents` packages.
|
|
4
|
+
|
|
5
|
+
This package turns an injected `LLMTransport` into a multi-turn, tool-using agent. Extensibility is a single interceptor primitive that can observe, transform, wrap, replace, or re-run any stage of the loop.
|
|
6
|
+
|
|
7
|
+
It depends only on `@ank1015/agents-contracts` (and `zod`). Hand it any transport — `createAdapterTransport`, `GatewayTransport`, or your own.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @ank1015/agents-runtime
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What This Package Provides
|
|
16
|
+
|
|
17
|
+
- `Agent` — durable container for transport, tools, plugins, and config
|
|
18
|
+
- `AgentRun` — per-run event stream with `abort()` and `send()` steering
|
|
19
|
+
- `defineTool` / tool registry helpers
|
|
20
|
+
- Utility helpers for ids, time, content, usage, errors, and abort
|
|
21
|
+
|
|
22
|
+
## Import Paths
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { Agent, defineTool, createEventStream } from '@ank1015/agents-runtime';
|
|
26
|
+
import { defineTool as defineToolFromTools } from '@ank1015/agents-runtime/tools';
|
|
27
|
+
import { createId, now } from '@ank1015/agents-runtime/utils';
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { Agent, defineTool } from '@ank1015/agents-runtime';
|
|
34
|
+
import { createAdapterTransport } from '@ank1015/agents-core/transport';
|
|
35
|
+
import { createAnthropicProviderAdapter } from '@ank1015/agents-provider-anthropic';
|
|
36
|
+
import { z } from 'zod';
|
|
37
|
+
|
|
38
|
+
const transport = createAdapterTransport([
|
|
39
|
+
createAnthropicProviderAdapter({
|
|
40
|
+
provider: 'anthropic',
|
|
41
|
+
apiKey: { type: 'env', name: 'ANTHROPIC_API_KEY' },
|
|
42
|
+
}),
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const agent = new Agent({
|
|
46
|
+
transport,
|
|
47
|
+
config: {
|
|
48
|
+
provider: 'anthropic',
|
|
49
|
+
modelId: 'claude-sonnet-4-6',
|
|
50
|
+
instructions: 'You are a helpful coding assistant.',
|
|
51
|
+
},
|
|
52
|
+
tools: [
|
|
53
|
+
defineTool({
|
|
54
|
+
name: 'echo',
|
|
55
|
+
description: 'Echo a string back',
|
|
56
|
+
parameters: z.object({ text: z.string() }),
|
|
57
|
+
execute: async ({ args }) => ({
|
|
58
|
+
content: [{ type: 'text', text: args.text }],
|
|
59
|
+
}),
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const run = agent.run({
|
|
65
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: 'Say hello via echo.' }] }],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
for await (const event of run) {
|
|
69
|
+
console.log(event.type);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const result = await run.result();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Plugins
|
|
76
|
+
|
|
77
|
+
Plugins install interceptors on loop stages (`input`, `request`, `model`, `response`, `tool`, `result`, `finish`). Add them at construction or with `agent.use(plugin)` before the first run.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
agent.use({
|
|
81
|
+
name: 'log-requests',
|
|
82
|
+
setup(api) {
|
|
83
|
+
api.before('request', (ctx) => {
|
|
84
|
+
console.log('request', ctx.request.modelId);
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Versioning
|
|
91
|
+
|
|
92
|
+
This package is currently `0.1.11`. Until `1.0.0`, public runtime APIs may still evolve as the agent contract and plugin surface settle.
|
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AgentConfig, AgentEventStream, AgentInput, AgentPlugin, ExecutableTool, LLMTransport, Message, Provider, ToolRegistry } from '@ank1015/agents-contracts';
|
|
2
|
+
export interface AgentOptions<P extends Provider = Provider> {
|
|
3
|
+
transport: LLMTransport;
|
|
4
|
+
config: AgentConfig<P>;
|
|
5
|
+
tools?: readonly ExecutableTool[];
|
|
6
|
+
plugins?: readonly AgentPlugin[];
|
|
7
|
+
deps?: {
|
|
8
|
+
createId?: (prefix?: string) => string;
|
|
9
|
+
now?: () => number;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A run handle: the loop's event stream (`for await` / `result()` / `drain()`)
|
|
14
|
+
* plus out-of-band control — `abort()` cancels the run, `send()` injects a
|
|
15
|
+
* message that the loop drains at the top of the next turn (steering /
|
|
16
|
+
* side-agents).
|
|
17
|
+
*/
|
|
18
|
+
export interface AgentRun<P extends Provider = Provider> extends AgentEventStream<P> {
|
|
19
|
+
abort(): void;
|
|
20
|
+
send(message: Message): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The durable, reusable container. Owns the transport, tool registry, plugin
|
|
24
|
+
* interceptors, and resolved config. `run()` starts an isolated run (fresh
|
|
25
|
+
* state per call) and returns an {@link AgentRun}.
|
|
26
|
+
*/
|
|
27
|
+
export declare class Agent<P extends Provider = Provider> {
|
|
28
|
+
#private;
|
|
29
|
+
constructor(options: AgentOptions<P>);
|
|
30
|
+
/** The tool registry, so callers can register tools after construction. */
|
|
31
|
+
get tools(): ToolRegistry;
|
|
32
|
+
use(plugin: AgentPlugin): this;
|
|
33
|
+
tool(tool: ExecutableTool): this;
|
|
34
|
+
/**
|
|
35
|
+
* Starts an isolated run. With no `input.model`, the run uses the agent's
|
|
36
|
+
* default config and is typed `AgentRun<P>`. With `input.model`, the whole run
|
|
37
|
+
* uses that {@link ModelSelection} — possibly a different provider — and the
|
|
38
|
+
* run is typed to that provider (`Q`). Plugins still adjust the model per turn.
|
|
39
|
+
*/
|
|
40
|
+
run<Q extends Provider = P>(input: AgentInput<Q>): AgentRun<Q>;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAEX,gBAAgB,EAEhB,UAAU,EACV,WAAW,EAEX,cAAc,EACd,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,YAAY,EACb,MAAM,2BAA2B,CAAC;AAQnC,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;IACzD,SAAS,EAAE,YAAY,CAAC;IACxB,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACvB,KAAK,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;IAClC,OAAO,CAAC,EAAE,SAAS,WAAW,EAAE,CAAC;IACjC,IAAI,CAAC,EAAE;QACL,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;QACvC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;KACpB,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IAClF,KAAK,IAAI,IAAI,CAAC;IACd,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9B;AAED;;;;GAIG;AACH,qBAAa,KAAK,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;;gBAWlC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC;IAWpC,2EAA2E;IAC3E,IAAI,KAAK,IAAI,YAAY,CAExB;IAED,GAAG,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAQ9B,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI;IAKhC;;;;;OAKG;IACH,GAAG,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC;CA+B/D"}
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createEventStream } from '@ank1015/agents-contracts';
|
|
2
|
+
import { AgentLoop } from './kernel/driver.js';
|
|
3
|
+
import { createInterceptorStore, installPlugins, makePluginApi } from './plugins/registry.js';
|
|
4
|
+
import { createToolRegistry } from './tools/registry.js';
|
|
5
|
+
import { createId as defaultCreateId, now as defaultNow } from './utils/index.js';
|
|
6
|
+
/**
|
|
7
|
+
* The durable, reusable container. Owns the transport, tool registry, plugin
|
|
8
|
+
* interceptors, and resolved config. `run()` starts an isolated run (fresh
|
|
9
|
+
* state per call) and returns an {@link AgentRun}.
|
|
10
|
+
*/
|
|
11
|
+
export class Agent {
|
|
12
|
+
#transport;
|
|
13
|
+
#config;
|
|
14
|
+
#tools;
|
|
15
|
+
#interceptors;
|
|
16
|
+
#plugins;
|
|
17
|
+
#api;
|
|
18
|
+
#createId;
|
|
19
|
+
#now;
|
|
20
|
+
#ready;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.#transport = options.transport;
|
|
23
|
+
this.#config = options.config;
|
|
24
|
+
this.#tools = createToolRegistry(options.tools ?? []);
|
|
25
|
+
this.#interceptors = createInterceptorStore();
|
|
26
|
+
this.#api = makePluginApi(this.#interceptors, this.#tools);
|
|
27
|
+
this.#plugins = [...(options.plugins ?? [])];
|
|
28
|
+
this.#createId = options.deps?.createId ?? defaultCreateId;
|
|
29
|
+
this.#now = options.deps?.now ?? defaultNow;
|
|
30
|
+
}
|
|
31
|
+
/** The tool registry, so callers can register tools after construction. */
|
|
32
|
+
get tools() {
|
|
33
|
+
return this.#tools;
|
|
34
|
+
}
|
|
35
|
+
use(plugin) {
|
|
36
|
+
if (this.#ready !== undefined) {
|
|
37
|
+
throw new Error('Cannot add plugins after the first run has started.');
|
|
38
|
+
}
|
|
39
|
+
this.#plugins.push(plugin);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
tool(tool) {
|
|
43
|
+
this.#tools.register(tool);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Starts an isolated run. With no `input.model`, the run uses the agent's
|
|
48
|
+
* default config and is typed `AgentRun<P>`. With `input.model`, the whole run
|
|
49
|
+
* uses that {@link ModelSelection} — possibly a different provider — and the
|
|
50
|
+
* run is typed to that provider (`Q`). Plugins still adjust the model per turn.
|
|
51
|
+
*/
|
|
52
|
+
run(input) {
|
|
53
|
+
this.#ready ??= installPlugins(this.#plugins, this.#api);
|
|
54
|
+
// Resolve the run's config: a per-run override replaces the model selection
|
|
55
|
+
// (provider/modelId/providerOptions) entirely while keeping the agent's
|
|
56
|
+
// instructions. The transport routes by provider at runtime, which the static
|
|
57
|
+
// generic can't track — hence the cast on the no-override branch, where `Q`
|
|
58
|
+
// resolves to `P` at the call site.
|
|
59
|
+
let config;
|
|
60
|
+
if (input.model) {
|
|
61
|
+
config = { ...input.model };
|
|
62
|
+
if (this.#config.instructions !== undefined) {
|
|
63
|
+
config.instructions = this.#config.instructions;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
config = this.#config;
|
|
68
|
+
}
|
|
69
|
+
const source = createEventStream();
|
|
70
|
+
const loop = new AgentLoop({
|
|
71
|
+
transport: this.#transport,
|
|
72
|
+
tools: this.#tools,
|
|
73
|
+
interceptors: this.#interceptors,
|
|
74
|
+
config,
|
|
75
|
+
ready: this.#ready,
|
|
76
|
+
createId: this.#createId,
|
|
77
|
+
now: this.#now,
|
|
78
|
+
});
|
|
79
|
+
void loop.drive(input, source);
|
|
80
|
+
return makeAgentRun(source, loop);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function makeAgentRun(source, loop) {
|
|
84
|
+
return {
|
|
85
|
+
[Symbol.asyncIterator]() {
|
|
86
|
+
return source[Symbol.asyncIterator]();
|
|
87
|
+
},
|
|
88
|
+
result() {
|
|
89
|
+
return source.result();
|
|
90
|
+
},
|
|
91
|
+
drain() {
|
|
92
|
+
return source.drain();
|
|
93
|
+
},
|
|
94
|
+
abort() {
|
|
95
|
+
loop.abort();
|
|
96
|
+
},
|
|
97
|
+
send(message) {
|
|
98
|
+
loop.send(message);
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,GAAG,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAwBlF;;;;GAIG;AACH,MAAM,OAAO,KAAK;IACP,UAAU,CAAe;IACzB,OAAO,CAAiB;IACxB,MAAM,CAAe;IACrB,aAAa,CAAmB;IAChC,QAAQ,CAAgB;IACxB,IAAI,CAAmC;IACvC,SAAS,CAA8B;IACvC,IAAI,CAAe;IAC5B,MAAM,CAA4B;IAElC,YAAY,OAAwB;QAClC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAA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AC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AgentConfig, AgentEvent, AgentState, AssistantToolCall, LLMRequest, LLMTransport, Message, Provider, Stage, StageContext, ToolRegistry } from '@ank1015/agents-contracts';
|
|
2
|
+
/** Operation-local data the kernel attaches for the node stages. */
|
|
3
|
+
export interface StageExtras<P extends Provider = Provider> {
|
|
4
|
+
request?: LLMRequest<P>;
|
|
5
|
+
call?: AssistantToolCall;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Live wiring the kernel hands to {@link makeStageContext}. The context is a
|
|
9
|
+
* thin view over these: state/config are the loop's mutable objects, the rest
|
|
10
|
+
* are bound callbacks, so every interceptor sees the current run.
|
|
11
|
+
*/
|
|
12
|
+
export interface KernelBindings<P extends Provider = Provider> {
|
|
13
|
+
state: AgentState<P>;
|
|
14
|
+
getConfig: () => AgentConfig<P>;
|
|
15
|
+
setConfig: (config: AgentConfig<P>) => void;
|
|
16
|
+
signal: AbortSignal | undefined;
|
|
17
|
+
metadata: Record<string, unknown>;
|
|
18
|
+
transport: LLMTransport;
|
|
19
|
+
tools: ToolRegistry;
|
|
20
|
+
emit: (event: AgentEvent<P>) => void;
|
|
21
|
+
addMessage: (message: Message) => void;
|
|
22
|
+
updateMessages: (update: (messages: Message[]) => Message[]) => void;
|
|
23
|
+
requestContinue: () => void;
|
|
24
|
+
requestStop: (reason?: string) => void;
|
|
25
|
+
}
|
|
26
|
+
/** Builds the {@link StageContext} an interceptor receives for one stage. */
|
|
27
|
+
export declare function makeStageContext<S extends Stage, P extends Provider = Provider>(bindings: KernelBindings<P>, stage: S, extras?: StageExtras<P>): StageContext<S, P>;
|
|
28
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/kernel/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAEX,UAAU,EACV,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,KAAK,EACL,YAAY,EACZ,YAAY,EACb,MAAM,2BAA2B,CAAC;AAEnC,oEAAoE;AACpE,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;IACxD,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IACxB,IAAI,CAAC,EAAE,iBAAiB,CAAC;CAC1B;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;IAC3D,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IACrB,SAAS,EAAE,MAAM,WAAW,CAAC,CAAC,CAAC,CAAC;IAChC,SAAS,EAAE,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;IAC5C,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,SAAS,EAAE,YAAY,CAAC;IACxB,KAAK,EAAE,YAAY,CAAC;IACpB,IAAI,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;IACrC,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACvC,cAAc,EAAE,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,IAAI,CAAC;IACrE,eAAe,EAAE,MAAM,IAAI,CAAC;IAC5B,WAAW,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;CACxC;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAC7E,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC,EAC3B,KAAK,EAAE,CAAC,EACR,MAAM,GAAE,WAAW,CAAC,CAAC,CAAM,GAC1B,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAiCpB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Builds the {@link StageContext} an interceptor receives for one stage. */
|
|
2
|
+
export function makeStageContext(bindings, stage, extras = {}) {
|
|
3
|
+
const ctx = {
|
|
4
|
+
stage,
|
|
5
|
+
state: bindings.state,
|
|
6
|
+
signal: bindings.signal,
|
|
7
|
+
metadata: bindings.metadata,
|
|
8
|
+
get config() {
|
|
9
|
+
return bindings.getConfig();
|
|
10
|
+
},
|
|
11
|
+
set config(value) {
|
|
12
|
+
bindings.setConfig(value);
|
|
13
|
+
},
|
|
14
|
+
transport: bindings.transport,
|
|
15
|
+
tools: bindings.tools,
|
|
16
|
+
emit: bindings.emit,
|
|
17
|
+
addMessage: bindings.addMessage,
|
|
18
|
+
updateMessages: bindings.updateMessages,
|
|
19
|
+
continue: bindings.requestContinue,
|
|
20
|
+
stop: bindings.requestStop,
|
|
21
|
+
};
|
|
22
|
+
const node = ctx;
|
|
23
|
+
if (extras.request !== undefined) {
|
|
24
|
+
node.request = extras.request;
|
|
25
|
+
}
|
|
26
|
+
if (extras.call !== undefined) {
|
|
27
|
+
node.call = extras.call;
|
|
28
|
+
}
|
|
29
|
+
return ctx;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/kernel/context.ts"],"names":[],"mappings":"AAyCA,6EAA6E;AAC7E,MAAM,UAAU,gBAAgB,CAC9B,QAA2B,EAC3B,KAAQ,EACR,SAAyB,EAAE;IAE3B,MAAM,GAAG,GAAoB;QAC3B,KAAK;QACL,KAAK,EAAE,QAAQ,CAAC,KAAK;QACrB,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,QAAQ,EAAE,QAAQ,CAAC,QAAQ;QAC3B,IAAI,MAAM;YACR,OAAO,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC9B,CAAC;QACD,IAAI,MAAM,CAAC,KAAqB;YAC9B,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC;QACD,SAAS,EAAE,QAAQ,CAAC,SAAS;QAC7B,KAAK,EAAE,QAAQ,CAAC,KAAK;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,UAAU,EAAE,QAAQ,CAAC,UAAU;QAC/B,cAAc,EAAE,QAAQ,CAAC,cAAc;QACvC,QAAQ,EAAE,QAAQ,CAAC,eAAe;QAClC,IAAI,EAAE,QAAQ,CAAC,WAAW;KAC3B,CAAC;IAEF,MAAM,IAAI,GAAG,GAGZ,CAAC;IACF,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAChC,CAAC;IACD,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,GAAyB,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AgentConfig, AgentEventStreamSource, AgentInput, LLMTransport, Message, Provider, ToolRegistry } from '@ank1015/agents-contracts';
|
|
2
|
+
import type { InterceptorStore } from '../plugins/registry.js';
|
|
3
|
+
export interface AgentLoopDeps<P extends Provider = Provider> {
|
|
4
|
+
transport: LLMTransport;
|
|
5
|
+
tools: ToolRegistry;
|
|
6
|
+
interceptors: InterceptorStore;
|
|
7
|
+
config: AgentConfig<P>;
|
|
8
|
+
/** Resolves once all plugin `setup` calls have completed. */
|
|
9
|
+
ready: Promise<void>;
|
|
10
|
+
createId: (prefix?: string) => string;
|
|
11
|
+
now: () => number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Per-run executor of the 5-edge / 2-node loop. Every stage is dispatched
|
|
15
|
+
* through {@link runStage}, which composes the stage's interceptors as an onion
|
|
16
|
+
* around the stage's real work. The loop owns the run invariants (one terminal
|
|
17
|
+
* event, tool-call/result pairing, abort propagation, continue ownership);
|
|
18
|
+
* everything else is a plugin's job.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AgentLoop<P extends Provider = Provider> {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(deps: AgentLoopDeps<P>);
|
|
23
|
+
abort(): void;
|
|
24
|
+
send(message: Message): void;
|
|
25
|
+
drive(input: AgentInput, source: AgentEventStreamSource<P>): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=driver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.d.ts","sourceRoot":"","sources":["../../src/kernel/driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAGX,sBAAsB,EAEtB,UAAU,EAOV,YAAY,EACZ,OAAO,EACP,QAAQ,EAKR,YAAY,EAGb,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAgB/D,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;IAC1D,SAAS,EAAE,YAAY,CAAC;IACxB,KAAK,EAAE,YAAY,CAAC;IACpB,YAAY,EAAE,gBAAgB,CAAC;IAC/B,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACvB,6DAA6D;IAC7D,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrB,QAAQ,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACtC,GAAG,EAAE,MAAM,MAAM,CAAC;CACnB;AAED;;;;;;GAMG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ;;gBAwBtC,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC;IAYlC,KAAK,IAAI,IAAI;IAIb,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAMtB,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,sBAAsB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAmcjF"}
|