typed-agents 1.0.0
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/LICENSE +19 -0
- package/README.md +201 -0
- package/dist/agents/agent.decorator.d.ts +19 -0
- package/dist/agents/agent.decorator.d.ts.map +1 -0
- package/dist/agents/agent.decorator.js +38 -0
- package/dist/agents/agent.decorator.js.map +1 -0
- package/dist/agents/agent.runtime.d.ts +133 -0
- package/dist/agents/agent.runtime.d.ts.map +1 -0
- package/dist/agents/agent.runtime.js +593 -0
- package/dist/agents/agent.runtime.js.map +1 -0
- package/dist/agents/agent.types.d.ts +245 -0
- package/dist/agents/agent.types.d.ts.map +1 -0
- package/dist/agents/agent.types.js +27 -0
- package/dist/agents/agent.types.js.map +1 -0
- package/dist/agents/behavior-engine/behavior.types.d.ts +23 -0
- package/dist/agents/behavior-engine/behavior.types.d.ts.map +1 -0
- package/dist/agents/behavior-engine/behavior.types.js +3 -0
- package/dist/agents/behavior-engine/behavior.types.js.map +1 -0
- package/dist/agents/behavior-engine/build-system-prompt.d.ts +12 -0
- package/dist/agents/behavior-engine/build-system-prompt.d.ts.map +1 -0
- package/dist/agents/behavior-engine/build-system-prompt.js +29 -0
- package/dist/agents/behavior-engine/build-system-prompt.js.map +1 -0
- package/dist/agents/behavior-engine/index.d.ts +7 -0
- package/dist/agents/behavior-engine/index.d.ts.map +1 -0
- package/dist/agents/behavior-engine/index.js +23 -0
- package/dist/agents/behavior-engine/index.js.map +1 -0
- package/dist/agents/behavior-engine/load-behavior.d.ts +16 -0
- package/dist/agents/behavior-engine/load-behavior.d.ts.map +1 -0
- package/dist/agents/behavior-engine/load-behavior.js +36 -0
- package/dist/agents/behavior-engine/load-behavior.js.map +1 -0
- package/dist/agents/behavior-engine/merge-behavior.d.ts +12 -0
- package/dist/agents/behavior-engine/merge-behavior.d.ts.map +1 -0
- package/dist/agents/behavior-engine/merge-behavior.js +33 -0
- package/dist/agents/behavior-engine/merge-behavior.js.map +1 -0
- package/dist/agents/behavior-engine/parse-behavior.d.ts +10 -0
- package/dist/agents/behavior-engine/parse-behavior.d.ts.map +1 -0
- package/dist/agents/behavior-engine/parse-behavior.js +66 -0
- package/dist/agents/behavior-engine/parse-behavior.js.map +1 -0
- package/dist/agents/behavior-engine/watch-behavior.d.ts +10 -0
- package/dist/agents/behavior-engine/watch-behavior.d.ts.map +1 -0
- package/dist/agents/behavior-engine/watch-behavior.js +34 -0
- package/dist/agents/behavior-engine/watch-behavior.js.map +1 -0
- package/dist/agents/compose.d.ts +41 -0
- package/dist/agents/compose.d.ts.map +1 -0
- package/dist/agents/compose.js +29 -0
- package/dist/agents/compose.js.map +1 -0
- package/dist/agents/index.d.ts +7 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +25 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/runtime-engine/build-tool-map.d.ts +16 -0
- package/dist/agents/runtime-engine/build-tool-map.d.ts.map +1 -0
- package/dist/agents/runtime-engine/build-tool-map.js +88 -0
- package/dist/agents/runtime-engine/build-tool-map.js.map +1 -0
- package/dist/agents/runtime-engine/convert-zod-to-tool-schema.d.ts +13 -0
- package/dist/agents/runtime-engine/convert-zod-to-tool-schema.d.ts.map +1 -0
- package/dist/agents/runtime-engine/convert-zod-to-tool-schema.js +26 -0
- package/dist/agents/runtime-engine/convert-zod-to-tool-schema.js.map +1 -0
- package/dist/agents/runtime-engine/execute-tool.d.ts +19 -0
- package/dist/agents/runtime-engine/execute-tool.d.ts.map +1 -0
- package/dist/agents/runtime-engine/execute-tool.js +100 -0
- package/dist/agents/runtime-engine/execute-tool.js.map +1 -0
- package/dist/agents/runtime-engine/index.d.ts +7 -0
- package/dist/agents/runtime-engine/index.d.ts.map +1 -0
- package/dist/agents/runtime-engine/index.js +23 -0
- package/dist/agents/runtime-engine/index.js.map +1 -0
- package/dist/agents/runtime-engine/parse-tool-arguments.d.ts +12 -0
- package/dist/agents/runtime-engine/parse-tool-arguments.d.ts.map +1 -0
- package/dist/agents/runtime-engine/parse-tool-arguments.js +42 -0
- package/dist/agents/runtime-engine/parse-tool-arguments.js.map +1 -0
- package/dist/agents/runtime-engine/retry.utils.d.ts +26 -0
- package/dist/agents/runtime-engine/retry.utils.d.ts.map +1 -0
- package/dist/agents/runtime-engine/retry.utils.js +55 -0
- package/dist/agents/runtime-engine/retry.utils.js.map +1 -0
- package/dist/agents/runtime-engine/state-persistence.d.ts +56 -0
- package/dist/agents/runtime-engine/state-persistence.d.ts.map +1 -0
- package/dist/agents/runtime-engine/state-persistence.js +70 -0
- package/dist/agents/runtime-engine/state-persistence.js.map +1 -0
- package/dist/clients/index.d.ts +3 -0
- package/dist/clients/index.d.ts.map +1 -0
- package/dist/clients/index.js +19 -0
- package/dist/clients/index.js.map +1 -0
- package/dist/clients/llm.client.d.ts +48 -0
- package/dist/clients/llm.client.d.ts.map +1 -0
- package/dist/clients/llm.client.js +3 -0
- package/dist/clients/llm.client.js.map +1 -0
- package/dist/clients/ollama.client.d.ts +20 -0
- package/dist/clients/ollama.client.d.ts.map +1 -0
- package/dist/clients/ollama.client.js +85 -0
- package/dist/clients/ollama.client.js.map +1 -0
- package/dist/core/logger/index.d.ts +2 -0
- package/dist/core/logger/index.d.ts.map +1 -0
- package/dist/core/logger/index.js +18 -0
- package/dist/core/logger/index.js.map +1 -0
- package/dist/core/logger/logger.d.ts +7 -0
- package/dist/core/logger/logger.d.ts.map +1 -0
- package/dist/core/logger/logger.js +19 -0
- package/dist/core/logger/logger.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/agent.runner.d.ts +29 -0
- package/dist/runtime/agent.runner.d.ts.map +1 -0
- package/dist/runtime/agent.runner.js +39 -0
- package/dist/runtime/agent.runner.js.map +1 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +18 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +20 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +128 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +140 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/tool.decorator.d.ts +71 -0
- package/dist/tools/tool.decorator.d.ts.map +1 -0
- package/dist/tools/tool.decorator.js +98 -0
- package/dist/tools/tool.decorator.js.map +1 -0
- package/dist/tools/tool.types.d.ts +231 -0
- package/dist/tools/tool.types.d.ts.map +1 -0
- package/dist/tools/tool.types.js +3 -0
- package/dist/tools/tool.types.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
Copyright 2026 typed-agents
|
|
8
|
+
|
|
9
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
you may not use this file except in compliance with the License.
|
|
11
|
+
You may obtain a copy of the License at
|
|
12
|
+
|
|
13
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
|
|
15
|
+
Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
See the License for the specific language governing permissions and
|
|
19
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🤖 typed-agents
|
|
4
|
+
|
|
5
|
+
> A TypeScript framework for building AI Agents and Tools with decorators, behavior files, composable sub-agents, streaming, and persistent state.
|
|
6
|
+
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://nodejs.org/)
|
|
10
|
+
[](https://ollama.com/)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## ✨ Features
|
|
17
|
+
|
|
18
|
+
| | |
|
|
19
|
+
|---|---|
|
|
20
|
+
| 🎨 **Decorator-based** | Define agents and tools with `@agent` and `@tool` — zero boilerplate |
|
|
21
|
+
| 🔁 **Multi-step reasoning** | Agents loop through LLM calls and tool executions automatically |
|
|
22
|
+
| 🧩 **Agent composition** | Nest agents as tools inside other agents via `compose()` |
|
|
23
|
+
| 📄 **Behavior engine** | Define agent personality and rules in plain `.md` files |
|
|
24
|
+
| 💾 **State persistence** | Resume interrupted runs from memory or disk |
|
|
25
|
+
| ⚡ **Sub-agent caching** | Avoid redundant sub-agent calls with built-in SHA-256 caching |
|
|
26
|
+
| 🔄 **Retry & backoff** | Automatic exponential backoff for LLM and tool calls |
|
|
27
|
+
| 📡 **Event system** | Subscribe to lifecycle events at every stage of a run |
|
|
28
|
+
| 🔌 **Pluggable clients** | Ships with `OllamaClient`, extendable via `LLMClient` interface |
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🚀 Quick Start
|
|
33
|
+
|
|
34
|
+
### 1. Define a Tool
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { tool } from "./tools/toolDecorator";
|
|
38
|
+
import { z } from "zod";
|
|
39
|
+
|
|
40
|
+
class MyTools {
|
|
41
|
+
@tool({
|
|
42
|
+
name: "get_weather",
|
|
43
|
+
description: "Returns the weather for a given city",
|
|
44
|
+
inputSchema: z.object({ city: z.string() }),
|
|
45
|
+
})
|
|
46
|
+
async getWeather({ city }: { city: string }) {
|
|
47
|
+
return `Sunny in ${city}, 24°C`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Define an Agent
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { agent } from "./agents/agentDecorator";
|
|
56
|
+
import { OllamaClient } from "./clients/ollamaClient";
|
|
57
|
+
|
|
58
|
+
@agent({
|
|
59
|
+
name: "weather-agent",
|
|
60
|
+
llmClient: new OllamaClient("granite4:3b"),
|
|
61
|
+
tools: [MyTools],
|
|
62
|
+
maxSteps: 5,
|
|
63
|
+
})
|
|
64
|
+
class WeatherAgent {}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 3. Run it
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { runAgent } from "./runtime/agentRunner";
|
|
71
|
+
|
|
72
|
+
const result = await runAgent(WeatherAgent, "What's the weather in Rome?");
|
|
73
|
+
console.log(result);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 🧩 Agent Composition
|
|
79
|
+
|
|
80
|
+
Agents can be used as tools inside other agents. The orchestrator delegates tasks to specialized sub-agents automatically.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
@agent({ name: "researcher", llmClient: new OllamaClient("granite4:3b") })
|
|
84
|
+
class Researcher {}
|
|
85
|
+
|
|
86
|
+
@agent({
|
|
87
|
+
name: "orchestrator",
|
|
88
|
+
llmClient: new OllamaClient("granite4:3b"),
|
|
89
|
+
tools: [
|
|
90
|
+
"auto",
|
|
91
|
+
compose(Researcher, { description: "Deep-dive research on any topic" }),
|
|
92
|
+
],
|
|
93
|
+
maxSteps: 8,
|
|
94
|
+
})
|
|
95
|
+
class Orchestrator {}
|
|
96
|
+
|
|
97
|
+
await runAgent(Orchestrator, "Research the future of AI in Italy");
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
> `"auto"` automatically registers all tools in the global registry.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 📄 Behavior Files
|
|
105
|
+
|
|
106
|
+
Skip writing raw system prompts. Define how your agent thinks and speaks in a plain `.md` file:
|
|
107
|
+
|
|
108
|
+
```md
|
|
109
|
+
# role
|
|
110
|
+
You are a strict and concise research assistant.
|
|
111
|
+
|
|
112
|
+
# rules
|
|
113
|
+
- Never make up information
|
|
114
|
+
- Always cite sources when possible
|
|
115
|
+
|
|
116
|
+
# planning
|
|
117
|
+
Break down complex tasks before answering.
|
|
118
|
+
|
|
119
|
+
# answer
|
|
120
|
+
Respond in bullet points. Be brief and factual.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
@agent({
|
|
125
|
+
name: "researcher",
|
|
126
|
+
llmClient: new OllamaClient("granite4:3b"),
|
|
127
|
+
behavior: "./behaviors/researcher.md", // or an array of files
|
|
128
|
+
})
|
|
129
|
+
class Researcher {}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Behavior files support **`extends`** for inheritance and can be **merged** from multiple sources.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## 💾 State Persistence
|
|
137
|
+
|
|
138
|
+
Run long tasks without fear of losing progress. Persist state to disk and resume anytime.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const result = await runAgent(MyAgent, "Do something long", true, undefined, {
|
|
142
|
+
persist: true, // saves to ./.agent-runs/
|
|
143
|
+
persistCache: true, // persists sub-agent result cache too
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// Resume a previous run with its ID
|
|
149
|
+
await runAgent(MyAgent, "", true, undefined, {
|
|
150
|
+
resumeId: "your-run-id",
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
| Mode | Description |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `"none"` | No persistence *(default)* |
|
|
157
|
+
| `"memory"` | In-memory, lost on restart |
|
|
158
|
+
| `"file"` | Written to disk, fully resumable |
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## 📡 Event System
|
|
163
|
+
|
|
164
|
+
Hook into every stage of an agent's lifecycle for logging, monitoring, or custom side effects.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
const runtime = new AgentRuntime(true);
|
|
168
|
+
|
|
169
|
+
runtime.on("agent:start", (e) => console.log("▶️ Started:", e.payload.agent));
|
|
170
|
+
runtime.on("tool:call", (e) => console.log("🔧 Tool:", e.payload.name));
|
|
171
|
+
runtime.on("tool:result", (e) => console.log("✅ Result:", e.payload.result));
|
|
172
|
+
runtime.on("agent:finish", (e) => console.log("🏁 Done:", e.payload.output));
|
|
173
|
+
|
|
174
|
+
await runtime.run(MyAgent, "Hello!");
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**Available events:** `agent:start` · `agent:step` · `agent:finish` · `agent:max_steps` · `agent:resume` · `llm:response` · `tool:call` · `tool:result` · `retry:attempt` · `state:saved`
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## ⚙️ Requirements
|
|
182
|
+
|
|
183
|
+
- **Node.js** 18+
|
|
184
|
+
- **TypeScript** with `experimentalDecorators` and `emitDecoratorMetadata` enabled
|
|
185
|
+
- **[Ollama](https://ollama.com)** running locally — or bring your own `LLMClient` implementation
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
// tsconfig.json
|
|
189
|
+
{
|
|
190
|
+
"compilerOptions": {
|
|
191
|
+
"experimentalDecorators": true,
|
|
192
|
+
"emitDecoratorMetadata": true
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## 📜 License
|
|
200
|
+
|
|
201
|
+
Distributed under the **Apache License 2.0**. See [`LICENSE`](LICENSE) for details.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { AgentOptions, AgentMetadata } from "./agent.types";
|
|
3
|
+
/**
|
|
4
|
+
* Class decorator to declare an agent and attach normalized metadata.
|
|
5
|
+
*
|
|
6
|
+
* This decorator stores the agent settings (name, model, tools, etc.) as
|
|
7
|
+
* metadata so that runtime factory code can discover an agent class.
|
|
8
|
+
*
|
|
9
|
+
* @param options - User-provided agent configuration.
|
|
10
|
+
*/
|
|
11
|
+
export declare function agent(options: AgentOptions): <T extends new (...args: never[]) => object>(constructor: T) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Reads agent metadata attached to a class or instance by the `@agent()` decorator.
|
|
14
|
+
*
|
|
15
|
+
* @param target - Constructor or object to inspect for metadata.
|
|
16
|
+
* @returns The agent metadata, or undefined if not decorated.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getAgentMetadata<T extends object>(target: T): AgentMetadata | undefined;
|
|
19
|
+
//# sourceMappingURL=agent.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.decorator.d.ts","sourceRoot":"","sources":["../../src/agents/agent.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAI5D;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY,IACxB,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,MAAM,EAAE,aAAa,CAAC,UAa5E;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,MAAM,EAAE,CAAC,GACR,aAAa,GAAG,SAAS,CAI3B"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.agent = agent;
|
|
4
|
+
exports.getAgentMetadata = getAgentMetadata;
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
const AGENT_METADATA_KEY = Symbol("typed-agents:agent");
|
|
7
|
+
/**
|
|
8
|
+
* Class decorator to declare an agent and attach normalized metadata.
|
|
9
|
+
*
|
|
10
|
+
* This decorator stores the agent settings (name, model, tools, etc.) as
|
|
11
|
+
* metadata so that runtime factory code can discover an agent class.
|
|
12
|
+
*
|
|
13
|
+
* @param options - User-provided agent configuration.
|
|
14
|
+
*/
|
|
15
|
+
function agent(options) {
|
|
16
|
+
return function (constructor) {
|
|
17
|
+
const metadata = {
|
|
18
|
+
name: options.name,
|
|
19
|
+
model: options.model,
|
|
20
|
+
llmClient: options.llmClient,
|
|
21
|
+
tools: options.tools ?? [],
|
|
22
|
+
maxSteps: options.maxSteps ?? 5,
|
|
23
|
+
systemPrompt: options.systemPrompt,
|
|
24
|
+
behavior: options.behavior,
|
|
25
|
+
};
|
|
26
|
+
Reflect.defineMetadata(AGENT_METADATA_KEY, metadata, constructor);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Reads agent metadata attached to a class or instance by the `@agent()` decorator.
|
|
31
|
+
*
|
|
32
|
+
* @param target - Constructor or object to inspect for metadata.
|
|
33
|
+
* @returns The agent metadata, or undefined if not decorated.
|
|
34
|
+
*/
|
|
35
|
+
function getAgentMetadata(target) {
|
|
36
|
+
return Reflect.getMetadata(AGENT_METADATA_KEY, target);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=agent.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.decorator.js","sourceRoot":"","sources":["../../src/agents/agent.decorator.ts"],"names":[],"mappings":";;AAaA,sBAcC;AAQD,4CAMC;AAzCD,4BAA0B;AAG1B,MAAM,kBAAkB,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAExD;;;;;;;GAOG;AACH,SAAgB,KAAK,CAAC,OAAqB;IACzC,OAAO,UAAsD,WAAc;QACzE,MAAM,QAAQ,GAAkB;YAC9B,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;YAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,CAAC;YAC/B,YAAY,EAAE,OAAO,CAAC,YAAY;YAClC,QAAQ,EAAE,OAAO,CAAC,QAAQ;SAC3B,CAAC;QAEF,OAAO,CAAC,cAAc,CAAC,kBAAkB,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;IACpE,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAC9B,MAAS;IAET,OAAO,OAAO,CAAC,WAAW,CAAC,kBAAkB,EAAE,MAAM,CAExC,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { LLMClient, StreamChunk } from "../clients/llm.client";
|
|
2
|
+
import { type AgentEvent, type RunOptions, type PersistConfig } from "./agent.types";
|
|
3
|
+
/**
|
|
4
|
+
* Core runtime engine for executing agent classes decorated with `@agent()`.
|
|
5
|
+
*
|
|
6
|
+
* Manages the full agent lifecycle: initialization, ReAct loop, tool execution,
|
|
7
|
+
* persistence, planning/reflection, and event emission. Supports both buffered
|
|
8
|
+
* and streaming execution modes.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AgentRuntime {
|
|
11
|
+
private showLogs;
|
|
12
|
+
private emitter;
|
|
13
|
+
private subAgentCache;
|
|
14
|
+
private persister;
|
|
15
|
+
private cacheDir;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new AgentRuntime instance with optional persistence configuration.
|
|
18
|
+
*
|
|
19
|
+
* @param showLogs - Whether to enable console logging for events and operations.
|
|
20
|
+
* @param persistConfig - Configuration for state persistence (none, memory, or file).
|
|
21
|
+
*/
|
|
22
|
+
constructor(showLogs: boolean, persistConfig?: PersistConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Clears the internal cache of sub-agent results.
|
|
25
|
+
* Useful for resetting state between runs or when tools change.
|
|
26
|
+
*/
|
|
27
|
+
clearSubAgentCache(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Registers an event handler for a specific agent event type.
|
|
30
|
+
*
|
|
31
|
+
* @param event - The event type to listen for (e.g., "agent:start", "tool:call").
|
|
32
|
+
* @param handler - Callback function invoked when the event is emitted.
|
|
33
|
+
*/
|
|
34
|
+
on<K extends AgentEvent["type"]>(event: K, handler: (event: Extract<AgentEvent, {
|
|
35
|
+
type: K;
|
|
36
|
+
}>) => void): void;
|
|
37
|
+
/**
|
|
38
|
+
* Emits an agent event to all registered listeners and optionally logs it.
|
|
39
|
+
*
|
|
40
|
+
* @param event - The event object to emit.
|
|
41
|
+
*/
|
|
42
|
+
private emit;
|
|
43
|
+
/**
|
|
44
|
+
* Saves the current agent state to the configured persister.
|
|
45
|
+
*
|
|
46
|
+
* @param runId - Unique identifier for the run.
|
|
47
|
+
* @param context - Current agent context to serialize.
|
|
48
|
+
* @param isFinished - Whether the run is complete.
|
|
49
|
+
*/
|
|
50
|
+
private saveState;
|
|
51
|
+
/**
|
|
52
|
+
* Saves the sub-agent cache to a JSON file in the cache directory.
|
|
53
|
+
*
|
|
54
|
+
* @param runId - Run ID used to name the cache file.
|
|
55
|
+
*/
|
|
56
|
+
private saveCache;
|
|
57
|
+
/**
|
|
58
|
+
* Loads the sub-agent cache from a JSON file if it exists.
|
|
59
|
+
*
|
|
60
|
+
* @param runId - Run ID to identify the cache file.
|
|
61
|
+
*/
|
|
62
|
+
private loadCache;
|
|
63
|
+
/**
|
|
64
|
+
* Resolves the LLM client to use, prioritizing injected client, then metadata client, then creating one from model.
|
|
65
|
+
*
|
|
66
|
+
* @param metadata - Agent metadata containing client or model info.
|
|
67
|
+
* @param injected - Optionally injected client (overrides others).
|
|
68
|
+
* @returns The resolved LLMClient instance.
|
|
69
|
+
* @throws Error if no client or model is available.
|
|
70
|
+
*/
|
|
71
|
+
private resolveClient;
|
|
72
|
+
/**
|
|
73
|
+
* Builds the initial message array for the agent conversation.
|
|
74
|
+
*
|
|
75
|
+
* @param systemPrompt - The system prompt to include.
|
|
76
|
+
* @param prompt - The user input prompt.
|
|
77
|
+
* @returns Array of LLMMessage objects.
|
|
78
|
+
*/
|
|
79
|
+
private buildInitialMessages;
|
|
80
|
+
/**
|
|
81
|
+
* Asks the LLM to produce a plan before the main execution loop.
|
|
82
|
+
* Best-effort: a failure here does not abort the run.
|
|
83
|
+
*
|
|
84
|
+
* @param context - Current agent context.
|
|
85
|
+
* @param client - LLM client to use for planning.
|
|
86
|
+
* @param options - Run options (for temperature, etc.).
|
|
87
|
+
*/
|
|
88
|
+
private executePlanningStep;
|
|
89
|
+
/**
|
|
90
|
+
* After a tool result, asks the LLM to self-reflect and optionally adjust.
|
|
91
|
+
* Only triggered in "reflect" planning mode.
|
|
92
|
+
*
|
|
93
|
+
* @param context - Current agent context.
|
|
94
|
+
* @param client - LLM client to use for reflection.
|
|
95
|
+
*/
|
|
96
|
+
private executeReflectionStep;
|
|
97
|
+
/**
|
|
98
|
+
* Executes an agent class in buffered mode, returning the final output string.
|
|
99
|
+
*
|
|
100
|
+
* This method handles the full ReAct loop: planning (if enabled), LLM calls,
|
|
101
|
+
* tool execution, reflection, persistence, and event emission. Supports resuming
|
|
102
|
+
* from a previous run ID.
|
|
103
|
+
*
|
|
104
|
+
* @param agentClass - The agent class decorated with `@agent()`.
|
|
105
|
+
* @param prompt - The user input prompt to start the conversation.
|
|
106
|
+
* @param injectedClient - Optional LLM client to override the agent's default.
|
|
107
|
+
* @param options - Runtime options (maxSteps, temperature, persistence, etc.).
|
|
108
|
+
* @returns The final output string from the agent.
|
|
109
|
+
* @throws Error if agent metadata is missing or run resumption fails.
|
|
110
|
+
*/
|
|
111
|
+
run(agentClass: new () => object, prompt: string, injectedClient?: LLMClient, options?: RunOptions): Promise<string>;
|
|
112
|
+
/**
|
|
113
|
+
* Streaming variant of `run`. Yields:
|
|
114
|
+
* - AgentEvent for every lifecycle event (steps, tool calls, planning…)
|
|
115
|
+
* - StreamChunk { type: "text_delta" } for incremental LLM text tokens
|
|
116
|
+
*
|
|
117
|
+
* Falls back to buffered `invoke` if the client does not implement `invokeStream`.
|
|
118
|
+
*
|
|
119
|
+
* Usage example:
|
|
120
|
+
* for await (const event of runtime.runStream(MyAgent, prompt)) {
|
|
121
|
+
* if (event.type === "text_delta") process.stdout.write(event.delta);
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* @param agentClass - The agent class decorated with `@agent()`.
|
|
125
|
+
* @param prompt - The user input prompt.
|
|
126
|
+
* @param injectedClient - Optional LLM client override.
|
|
127
|
+
* @param options - Runtime options.
|
|
128
|
+
* @returns Async generator yielding events and stream chunks.
|
|
129
|
+
* @throws Error if agent metadata is missing.
|
|
130
|
+
*/
|
|
131
|
+
runStream(agentClass: new () => object, prompt: string, injectedClient?: LLMClient, options?: RunOptions): AsyncGenerator<AgentEvent | StreamChunk>;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=agent.runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.runtime.d.ts","sourceRoot":"","sources":["../../src/agents/agent.runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAc,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAK3E,OAAO,EAEL,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,KAAK,aAAa,EAEnB,MAAM,eAAe,CAAC;AAsCvB;;;;;;GAMG;AACH,qBAAa,YAAY;IAarB,OAAO,CAAC,QAAQ;IAZlB,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,aAAa,CAA6B;IAClD,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,QAAQ,CAAuB;IAEvC;;;;;OAKG;gBAEO,QAAQ,EAAE,OAAO,EACzB,aAAa,GAAE,aAAgC;IAcjD;;;OAGG;IACI,kBAAkB,IAAI,IAAI;IAKjC;;;;;OAKG;IACH,EAAE,CAAC,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,IAAI,GACzD,IAAI;IAIP;;;;OAIG;IACH,OAAO,CAAC,IAAI;IAOZ;;;;;;OAMG;YACW,SAAS;IAsBvB;;;;OAIG;YACW,SAAS;IASvB;;;;OAIG;YACW,SAAS;IAcvB;;;;;;;OAOG;IACH,OAAO,CAAC,aAAa;IAarB;;;;;;OAMG;IACH,OAAO,CAAC,oBAAoB;IAS5B;;;;;;;OAOG;YACW,mBAAmB;IA0BjC;;;;;;OAMG;YACW,qBAAqB;IA0BnC;;;;;;;;;;;;;OAaG;IACG,GAAG,CACP,UAAU,EAAE,UAAU,MAAM,EAC5B,MAAM,EAAE,MAAM,EACd,cAAc,CAAC,EAAE,SAAS,EAC1B,OAAO,GAAE,UAAe,GACvB,OAAO,CAAC,MAAM,CAAC;IA0MlB;;;;;;;;;;;;;;;;;;OAkBG;IACI,SAAS,CACd,UAAU,EAAE,UAAU,MAAM,EAC5B,MAAM,EAAE,MAAM,EACd,cAAc,CAAC,EAAE,SAAS,EAC1B,OAAO,GAAE,UAAe,GACvB,cAAc,CAAC,UAAU,GAAG,WAAW,CAAC;CAiL5C"}
|