@animalabs/agent-framework 0.1.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.
Files changed (212) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +233 -0
  3. package/dist/src/agent.d.ts +130 -0
  4. package/dist/src/agent.d.ts.map +1 -0
  5. package/dist/src/agent.js +406 -0
  6. package/dist/src/agent.js.map +1 -0
  7. package/dist/src/api/mcp-server.d.ts +37 -0
  8. package/dist/src/api/mcp-server.d.ts.map +1 -0
  9. package/dist/src/api/mcp-server.js +598 -0
  10. package/dist/src/api/mcp-server.js.map +1 -0
  11. package/dist/src/api/server.d.ts +83 -0
  12. package/dist/src/api/server.d.ts.map +1 -0
  13. package/dist/src/api/server.js +785 -0
  14. package/dist/src/api/server.js.map +1 -0
  15. package/dist/src/api/types.d.ts +395 -0
  16. package/dist/src/api/types.d.ts.map +1 -0
  17. package/dist/src/api/types.js +5 -0
  18. package/dist/src/api/types.js.map +1 -0
  19. package/dist/src/framework.d.ts +309 -0
  20. package/dist/src/framework.d.ts.map +1 -0
  21. package/dist/src/framework.js +2309 -0
  22. package/dist/src/framework.js.map +1 -0
  23. package/dist/src/gate/event-gate.d.ts +75 -0
  24. package/dist/src/gate/event-gate.d.ts.map +1 -0
  25. package/dist/src/gate/event-gate.js +503 -0
  26. package/dist/src/gate/event-gate.js.map +1 -0
  27. package/dist/src/gate/index.d.ts +3 -0
  28. package/dist/src/gate/index.d.ts.map +1 -0
  29. package/dist/src/gate/index.js +2 -0
  30. package/dist/src/gate/index.js.map +1 -0
  31. package/dist/src/gate/types.d.ts +82 -0
  32. package/dist/src/gate/types.d.ts.map +1 -0
  33. package/dist/src/gate/types.js +9 -0
  34. package/dist/src/gate/types.js.map +1 -0
  35. package/dist/src/index.d.ts +17 -0
  36. package/dist/src/index.d.ts.map +1 -0
  37. package/dist/src/index.js +18 -0
  38. package/dist/src/index.js.map +1 -0
  39. package/dist/src/mcpl/channel-registry.d.ts +141 -0
  40. package/dist/src/mcpl/channel-registry.d.ts.map +1 -0
  41. package/dist/src/mcpl/channel-registry.js +647 -0
  42. package/dist/src/mcpl/channel-registry.js.map +1 -0
  43. package/dist/src/mcpl/checkpoint-manager.d.ts +79 -0
  44. package/dist/src/mcpl/checkpoint-manager.d.ts.map +1 -0
  45. package/dist/src/mcpl/checkpoint-manager.js +430 -0
  46. package/dist/src/mcpl/checkpoint-manager.js.map +1 -0
  47. package/dist/src/mcpl/errors.d.ts +52 -0
  48. package/dist/src/mcpl/errors.d.ts.map +1 -0
  49. package/dist/src/mcpl/errors.js +93 -0
  50. package/dist/src/mcpl/errors.js.map +1 -0
  51. package/dist/src/mcpl/feature-set-manager.d.ts +91 -0
  52. package/dist/src/mcpl/feature-set-manager.d.ts.map +1 -0
  53. package/dist/src/mcpl/feature-set-manager.js +250 -0
  54. package/dist/src/mcpl/feature-set-manager.js.map +1 -0
  55. package/dist/src/mcpl/hook-orchestrator.d.ts +51 -0
  56. package/dist/src/mcpl/hook-orchestrator.d.ts.map +1 -0
  57. package/dist/src/mcpl/hook-orchestrator.js +217 -0
  58. package/dist/src/mcpl/hook-orchestrator.js.map +1 -0
  59. package/dist/src/mcpl/index.d.ts +14 -0
  60. package/dist/src/mcpl/index.d.ts.map +1 -0
  61. package/dist/src/mcpl/index.js +21 -0
  62. package/dist/src/mcpl/index.js.map +1 -0
  63. package/dist/src/mcpl/inference-router.d.ts +67 -0
  64. package/dist/src/mcpl/inference-router.d.ts.map +1 -0
  65. package/dist/src/mcpl/inference-router.js +249 -0
  66. package/dist/src/mcpl/inference-router.js.map +1 -0
  67. package/dist/src/mcpl/push-handler.d.ts +60 -0
  68. package/dist/src/mcpl/push-handler.d.ts.map +1 -0
  69. package/dist/src/mcpl/push-handler.js +162 -0
  70. package/dist/src/mcpl/push-handler.js.map +1 -0
  71. package/dist/src/mcpl/scope-manager.d.ts +76 -0
  72. package/dist/src/mcpl/scope-manager.d.ts.map +1 -0
  73. package/dist/src/mcpl/scope-manager.js +213 -0
  74. package/dist/src/mcpl/scope-manager.js.map +1 -0
  75. package/dist/src/mcpl/server-connection.d.ts +174 -0
  76. package/dist/src/mcpl/server-connection.d.ts.map +1 -0
  77. package/dist/src/mcpl/server-connection.js +559 -0
  78. package/dist/src/mcpl/server-connection.js.map +1 -0
  79. package/dist/src/mcpl/server-registry.d.ts +63 -0
  80. package/dist/src/mcpl/server-registry.d.ts.map +1 -0
  81. package/dist/src/mcpl/server-registry.js +119 -0
  82. package/dist/src/mcpl/server-registry.js.map +1 -0
  83. package/dist/src/mcpl/types.d.ts +755 -0
  84. package/dist/src/mcpl/types.d.ts.map +1 -0
  85. package/dist/src/mcpl/types.js +48 -0
  86. package/dist/src/mcpl/types.js.map +1 -0
  87. package/dist/src/module-registry.d.ts +93 -0
  88. package/dist/src/module-registry.d.ts.map +1 -0
  89. package/dist/src/module-registry.js +386 -0
  90. package/dist/src/module-registry.js.map +1 -0
  91. package/dist/src/modules/api/index.d.ts +22 -0
  92. package/dist/src/modules/api/index.d.ts.map +1 -0
  93. package/dist/src/modules/api/index.js +63 -0
  94. package/dist/src/modules/api/index.js.map +1 -0
  95. package/dist/src/modules/discord/discord-js-client.d.ts +58 -0
  96. package/dist/src/modules/discord/discord-js-client.d.ts.map +1 -0
  97. package/dist/src/modules/discord/discord-js-client.js +273 -0
  98. package/dist/src/modules/discord/discord-js-client.js.map +1 -0
  99. package/dist/src/modules/discord/index.d.ts +82 -0
  100. package/dist/src/modules/discord/index.d.ts.map +1 -0
  101. package/dist/src/modules/discord/index.js +959 -0
  102. package/dist/src/modules/discord/index.js.map +1 -0
  103. package/dist/src/modules/discord/types.d.ts +327 -0
  104. package/dist/src/modules/discord/types.d.ts.map +1 -0
  105. package/dist/src/modules/discord/types.js +5 -0
  106. package/dist/src/modules/discord/types.js.map +1 -0
  107. package/dist/src/modules/index.d.ts +11 -0
  108. package/dist/src/modules/index.d.ts.map +1 -0
  109. package/dist/src/modules/index.js +7 -0
  110. package/dist/src/modules/index.js.map +1 -0
  111. package/dist/src/modules/workspace/index.d.ts +59 -0
  112. package/dist/src/modules/workspace/index.d.ts.map +1 -0
  113. package/dist/src/modules/workspace/index.js +831 -0
  114. package/dist/src/modules/workspace/index.js.map +1 -0
  115. package/dist/src/modules/workspace/sync.d.ts +48 -0
  116. package/dist/src/modules/workspace/sync.d.ts.map +1 -0
  117. package/dist/src/modules/workspace/sync.js +253 -0
  118. package/dist/src/modules/workspace/sync.js.map +1 -0
  119. package/dist/src/modules/workspace/types.d.ts +166 -0
  120. package/dist/src/modules/workspace/types.d.ts.map +1 -0
  121. package/dist/src/modules/workspace/types.js +5 -0
  122. package/dist/src/modules/workspace/types.js.map +1 -0
  123. package/dist/src/modules/workspace/watcher.d.ts +43 -0
  124. package/dist/src/modules/workspace/watcher.d.ts.map +1 -0
  125. package/dist/src/modules/workspace/watcher.js +120 -0
  126. package/dist/src/modules/workspace/watcher.js.map +1 -0
  127. package/dist/src/queue.d.ts +50 -0
  128. package/dist/src/queue.d.ts.map +1 -0
  129. package/dist/src/queue.js +95 -0
  130. package/dist/src/queue.js.map +1 -0
  131. package/dist/src/safe-slice.d.ts +8 -0
  132. package/dist/src/safe-slice.d.ts.map +1 -0
  133. package/dist/src/safe-slice.js +18 -0
  134. package/dist/src/safe-slice.js.map +1 -0
  135. package/dist/src/types/agent.d.ts +118 -0
  136. package/dist/src/types/agent.d.ts.map +1 -0
  137. package/dist/src/types/agent.js +2 -0
  138. package/dist/src/types/agent.js.map +1 -0
  139. package/dist/src/types/events.d.ts +220 -0
  140. package/dist/src/types/events.d.ts.map +1 -0
  141. package/dist/src/types/events.js +2 -0
  142. package/dist/src/types/events.js.map +1 -0
  143. package/dist/src/types/framework.d.ts +184 -0
  144. package/dist/src/types/framework.d.ts.map +1 -0
  145. package/dist/src/types/framework.js +2 -0
  146. package/dist/src/types/framework.js.map +1 -0
  147. package/dist/src/types/index.d.ts +7 -0
  148. package/dist/src/types/index.d.ts.map +1 -0
  149. package/dist/src/types/index.js +7 -0
  150. package/dist/src/types/index.js.map +1 -0
  151. package/dist/src/types/inference-log.d.ts +64 -0
  152. package/dist/src/types/inference-log.d.ts.map +1 -0
  153. package/dist/src/types/inference-log.js +5 -0
  154. package/dist/src/types/inference-log.js.map +1 -0
  155. package/dist/src/types/module.d.ts +302 -0
  156. package/dist/src/types/module.d.ts.map +1 -0
  157. package/dist/src/types/module.js +2 -0
  158. package/dist/src/types/module.js.map +1 -0
  159. package/dist/src/types/trace.d.ts +173 -0
  160. package/dist/src/types/trace.d.ts.map +1 -0
  161. package/dist/src/types/trace.js +2 -0
  162. package/dist/src/types/trace.js.map +1 -0
  163. package/dist/test/event-gate.test.d.ts +2 -0
  164. package/dist/test/event-gate.test.d.ts.map +1 -0
  165. package/dist/test/event-gate.test.js +535 -0
  166. package/dist/test/event-gate.test.js.map +1 -0
  167. package/dist/test/framework.test.d.ts +5 -0
  168. package/dist/test/framework.test.d.ts.map +1 -0
  169. package/dist/test/framework.test.js +738 -0
  170. package/dist/test/framework.test.js.map +1 -0
  171. package/dist/tsconfig.tsbuildinfo +1 -0
  172. package/package.json +58 -0
  173. package/src/agent.ts +512 -0
  174. package/src/api/mcp-server.ts +687 -0
  175. package/src/api/server.ts +1023 -0
  176. package/src/api/types.ts +542 -0
  177. package/src/framework.ts +2784 -0
  178. package/src/gate/event-gate.ts +623 -0
  179. package/src/gate/index.ts +12 -0
  180. package/src/gate/types.ts +112 -0
  181. package/src/index.ts +63 -0
  182. package/src/mcpl/channel-registry.ts +824 -0
  183. package/src/mcpl/checkpoint-manager.ts +545 -0
  184. package/src/mcpl/errors.ts +114 -0
  185. package/src/mcpl/feature-set-manager.ts +333 -0
  186. package/src/mcpl/hook-orchestrator.ts +281 -0
  187. package/src/mcpl/index.ts +134 -0
  188. package/src/mcpl/inference-router.ts +335 -0
  189. package/src/mcpl/push-handler.ts +232 -0
  190. package/src/mcpl/scope-manager.ts +262 -0
  191. package/src/mcpl/server-connection.ts +694 -0
  192. package/src/mcpl/server-registry.ts +144 -0
  193. package/src/mcpl/types.ts +991 -0
  194. package/src/module-registry.ts +547 -0
  195. package/src/modules/api/index.ts +94 -0
  196. package/src/modules/discord/discord-js-client.ts +353 -0
  197. package/src/modules/discord/index.ts +1146 -0
  198. package/src/modules/discord/types.ts +465 -0
  199. package/src/modules/index.ts +43 -0
  200. package/src/modules/workspace/index.ts +969 -0
  201. package/src/modules/workspace/sync.ts +314 -0
  202. package/src/modules/workspace/types.ts +202 -0
  203. package/src/modules/workspace/watcher.ts +142 -0
  204. package/src/queue.ts +106 -0
  205. package/src/safe-slice.ts +18 -0
  206. package/src/types/agent.ts +119 -0
  207. package/src/types/events.ts +252 -0
  208. package/src/types/framework.ts +211 -0
  209. package/src/types/index.ts +6 -0
  210. package/src/types/inference-log.ts +64 -0
  211. package/src/types/module.ts +367 -0
  212. package/src/types/trace.ts +196 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 antra-tess
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @connectome/agent-framework
2
+
3
+ Multi-agent framework with pluggable modules, persistent state, streaming inference, and concurrent tool execution.
4
+
5
+ ## Overview
6
+
7
+ Agent Framework orchestrates one or more LLM-powered agents that interact with the world through **modules** (pluggable capability providers). It handles the full lifecycle: event processing, context compilation, inference (streaming or request/response), tool dispatch, and state persistence via [Chronicle](https://github.com/antra-tess/chronicle).
8
+
9
+ ```
10
+ External events (Discord, API, MCPL servers, timers)
11
+ ↓
12
+ ProcessQueue → Module.onProcess() → EventResponse
13
+ ↓ ↓
14
+ Inference trigger Messages / state updates
15
+ ↓
16
+ Context compilation (with injections from modules + MCPL hooks)
17
+ ↓
18
+ Membrane (LLM abstraction) → YieldingStream
19
+ ↓
20
+ Tool calls → Module.handleToolCall() → results → stream resumes
21
+ ↓
22
+ Agent speech → Module.onAgentSpeech() → external delivery
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import { AgentFramework, ApiModule, ApiServer } from '@connectome/agent-framework';
29
+ import { Membrane, AnthropicAdapter } from 'membrane';
30
+
31
+ const membrane = new Membrane({ adapter: new AnthropicAdapter() });
32
+
33
+ const framework = await AgentFramework.create({
34
+ storePath: './data/store',
35
+ membrane,
36
+ agents: [{
37
+ name: 'assistant',
38
+ model: 'claude-sonnet-4-20250514',
39
+ systemPrompt: 'You are a helpful assistant.',
40
+ }],
41
+ modules: [new ApiModule()],
42
+ });
43
+
44
+ framework.start();
45
+
46
+ const server = new ApiServer(framework, { port: 8765 });
47
+ await server.start();
48
+ ```
49
+
50
+ ## Core Concepts
51
+
52
+ ### Agents
53
+
54
+ An agent wraps an LLM identity: model, system prompt, context strategy, and tool permissions. Multiple agents can coexist, each with independent context and inference state.
55
+
56
+ ```typescript
57
+ {
58
+ name: 'researcher',
59
+ model: 'claude-opus-4-20250514',
60
+ systemPrompt: '...',
61
+ strategy: new AutobiographicalStrategy({ recentWindowTokens: 30000 }),
62
+ allowedTools: ['search', 'read', 'write'], // or 'all'
63
+ triggerSources: ['discord'], // or 'all'
64
+ maxTokens: 8192,
65
+ maxStreamTokens: 150_000, // input token budget before stream restart
66
+ }
67
+ ```
68
+
69
+ **Inference modes:**
70
+ - **Streaming** (default in framework): `startStreamWithInjections()` returns a `YieldingStream` that emits tokens, tool calls, and completion events. The framework drives the stream, dispatches tools, and resumes automatically.
71
+ - **Request/response**: `runInferenceWithInjections()` for simple complete-and-return usage with optional `AbortSignal` for cancellation.
72
+
73
+ **State machine:**
74
+ ```
75
+ idle → inferring → streaming ⇄ waiting_for_tools → ready → streaming → ... → idle
76
+ ↘ (abort) → idle
77
+ ```
78
+
79
+ ### Modules
80
+
81
+ Modules are pluggable capability providers. They process events, expose tools, deliver speech, and optionally inject context before inference.
82
+
83
+ ```typescript
84
+ interface Module {
85
+ readonly name: string;
86
+
87
+ start(ctx: ModuleContext): Promise<void>;
88
+ stop(): Promise<void>;
89
+
90
+ getTools(): ToolDefinition[];
91
+ handleToolCall(call: ToolCall): Promise<ToolResult>;
92
+ onProcess(event: ProcessEvent, state: ProcessState): Promise<EventResponse>;
93
+
94
+ // Optional
95
+ onAgentSpeech?(agentName: string, content: ContentBlock[], context: SpeechContext): Promise<void>;
96
+ gatherContext?(agentName: string): Promise<ContextInjection[]>;
97
+ }
98
+ ```
99
+
100
+ An `EventResponse` can add/edit/remove messages, request inference, signal tool changes, and atomically update module state:
101
+
102
+ ```typescript
103
+ return {
104
+ addMessages: [{ participant: 'user', content: [{ type: 'text', text: '...' }] }],
105
+ requestInference: true,
106
+ stateUpdate: { lastProcessed: Date.now() },
107
+ };
108
+ ```
109
+
110
+ **Built-in modules:**
111
+ - **DiscordModule** - Discord bot integration (messages, reactions, threads, typing indicators)
112
+ - **ApiModule** - WebSocket API event processing
113
+ - **WorkspaceModule** - Mountable filesystem abstraction with Chronicle tree state, filesystem watching, and materialization
114
+
115
+ ### Event Processing
116
+
117
+ The framework runs an event loop over a `ProcessQueue`. Each event is processed by all modules, which return `EventResponse` objects. If any response requests inference, the framework compiles context and starts a stream.
118
+
119
+ **Event types:** `ExternalMessageEvent`, `ToolCallEvent`, `ToolResultEvent`, `InferenceRequestEvent`, `McplPushEvent`, `McplChannelIncomingEvent`, `TimerFiredEvent`, `ApiMessageEvent`, `ModuleEvent`, `CustomEvent`
120
+
121
+ ### Context Management
122
+
123
+ Each agent has a `ContextManager` (from `@connectome/context-manager`) that maintains conversation history with optional compression strategies:
124
+
125
+ - **PassthroughStrategy** - No compression, raw message replay up to budget
126
+ - **AutobiographicalStrategy** - Chunks old messages and summarizes them into diary entries, preserving recent context uncompressed
127
+
128
+ Before inference, modules and MCPL hooks can inject additional context via `gatherContext()`.
129
+
130
+ ### Persistence
131
+
132
+ All state is persisted in a [Chronicle](https://github.com/antra-tess/chronicle) store:
133
+
134
+ | State | Strategy | Description |
135
+ |-------|----------|-------------|
136
+ | `framework/state` | snapshot | Agent configs |
137
+ | `framework/inference-log` | append_log | Raw LLM requests/responses |
138
+ | `messages` | (context-manager) | Shared conversation log |
139
+ | `agents/{name}/context` | (context-manager) | Per-agent context log |
140
+ | `modules/{name}/state` | snapshot | Per-module persistent state |
141
+
142
+ Chronicle's branching support enables time-travel and what-if exploration across all state.
143
+
144
+ ### MCPL (MCP Live)
145
+
146
+ Optional host-side implementation of the MCP Live protocol. External servers (game engines, dev tools, etc.) can:
147
+ - Push events into agent context
148
+ - Hook into inference lifecycle (beforeInference/afterInference)
149
+ - Initiate inference requests
150
+ - Publish and observe channels
151
+ - Provide tools (namespaced automatically)
152
+
153
+ ```typescript
154
+ {
155
+ mcplServers: [{
156
+ id: 'game-engine',
157
+ command: 'node',
158
+ args: ['./game-server.js'],
159
+ toolPrefix: 'game',
160
+ }],
161
+ }
162
+ ```
163
+
164
+ ### Streaming Lifecycle
165
+
166
+ 1. **Start**: Framework calls `agent.startStreamWithInjections()` → `YieldingStream`
167
+ 2. **Drive**: Framework iterates stream events (tokens, tool-calls, complete, error)
168
+ 3. **Tool yield**: Stream yields tool calls → framework dispatches concurrently → collects results
169
+ 4. **Resume**: All results in → stream resumes with tool results
170
+ 5. **Budget restart**: If input tokens exceed `maxStreamTokens`, the stream is cancelled, context is recompressed, and a fresh stream starts
171
+ 6. **Complete**: Final response saved to context, agent returns to idle
172
+
173
+ ### Abort / Cancellation
174
+
175
+ ```typescript
176
+ // Cancel in-flight inference for an agent
177
+ framework.abortInference('assistant', 'user requested stop');
178
+ ```
179
+
180
+ The non-streaming path uses `AbortSignal` forwarded to `membrane.stream()`, which returns an `AbortedResponse` with partial content. The streaming path cancels the `YieldingStream` directly.
181
+
182
+ ## API Server
183
+
184
+ WebSocket server for external clients (UIs, scripts, other agents).
185
+
186
+ ```typescript
187
+ const server = new ApiServer(framework, { port: 8765 });
188
+ await server.start();
189
+ ```
190
+
191
+ **Commands:** `message.send`, `message.list`, `inference.request`, `inference.abort`, `branch.*`, `agent.*`, `module.*`, `store.*`, `inference.tail/inspect/search`, `events.*`
192
+
193
+ Also available as an MCP server via the `agent-framework-mcp` binary.
194
+
195
+ ## Observability
196
+
197
+ Subscribe to trace events for logging, UI updates, or debugging:
198
+
199
+ ```typescript
200
+ framework.onTrace((event) => {
201
+ // inference:started, inference:completed, inference:aborted
202
+ // tool:started, tool:completed, tool:failed
203
+ // process:received, process:completed
204
+ // message:added, module:added, module:removed
205
+ console.log(event.type, event);
206
+ });
207
+ ```
208
+
209
+ Query raw inference logs:
210
+
211
+ ```typescript
212
+ const logs = framework.queryInferenceLogs({ agentName: 'assistant', limit: 10 });
213
+ ```
214
+
215
+ ## Dependencies
216
+
217
+ | Package | Role |
218
+ |---------|------|
219
+ | [membrane](https://github.com/antra-tess/membrane) | LLM provider abstraction (Anthropic, Bedrock, OpenRouter) |
220
+ | [@connectome/context-manager](https://github.com/anima-research/context-manager) | Context window management and compression |
221
+ | [chronicle](https://github.com/antra-tess/chronicle) | Branchable persistent event store (Rust + N-API) |
222
+
223
+ ## Development
224
+
225
+ ```bash
226
+ npm install
227
+ npm run build # TypeScript compilation
228
+ npm run dev # Watch mode
229
+ npm test # Run tests
230
+ npm run typecheck # Type-check without emitting
231
+ ```
232
+
233
+ Requires Node.js >= 20. Chronicle requires a Rust toolchain for native module compilation (`npm run build` in the chronicle directory).
@@ -0,0 +1,130 @@
1
+ import type { Membrane, NormalizedRequest, ContentBlock, YieldingStream } from '@animalabs/membrane';
2
+ export interface StartStreamResult {
3
+ stream: YieldingStream;
4
+ request: NormalizedRequest;
5
+ }
6
+ import type { ContextManager, TokenBudget, ContextInjection, CompileResult } from '@animalabs/context-manager';
7
+ import type { AgentConfig, AgentState, ToolCallId, ToolCall, ToolResult, ToolDefinition, AgentInfo, InferenceResult, InferenceOptions } from './types/index.js';
8
+ /**
9
+ * An agent wraps a context manager and manages inference state.
10
+ */
11
+ export declare class Agent {
12
+ readonly name: string;
13
+ readonly model: string;
14
+ readonly systemPrompt: string;
15
+ readonly allowedTools: 'all' | string[];
16
+ readonly triggerSources: 'all' | string[];
17
+ readonly maxTokens: number;
18
+ readonly temperature: number;
19
+ private _state;
20
+ private _inferenceStartedAt;
21
+ private _streamId;
22
+ lastStreamInputTokens: number;
23
+ maxStreamTokens: number;
24
+ private contextManager;
25
+ private membrane;
26
+ constructor(config: AgentConfig, contextManager: ContextManager, membrane: Membrane);
27
+ /**
28
+ * Get current agent state.
29
+ */
30
+ get state(): AgentState;
31
+ /**
32
+ * Monotonically increasing stream generation counter.
33
+ * Used to guard stale driveStream handlers after a budget restart.
34
+ */
35
+ get streamId(): number;
36
+ /**
37
+ * Get agent info.
38
+ */
39
+ get info(): AgentInfo;
40
+ /**
41
+ * Check if agent can use a specific tool.
42
+ */
43
+ canUseTool(toolName: string): boolean;
44
+ /**
45
+ * Check if a source can trigger inference for this agent.
46
+ */
47
+ canBeTriggeredBy(source: string): boolean;
48
+ /**
49
+ * Compile the context without injections.
50
+ * Step 1 of the inference pipeline — callers can inspect/modify the result
51
+ * before building a request.
52
+ */
53
+ compileContext(budget?: TokenBudget): Promise<CompileResult>;
54
+ /**
55
+ * Compile the context with injections.
56
+ * Same as compileContext but forwards injections (e.g. from MCPL servers)
57
+ * to the context manager so they are merged into the compiled messages.
58
+ */
59
+ compileWithInjections(budget?: TokenBudget, injections?: ContextInjection[]): Promise<CompileResult>;
60
+ /**
61
+ * Run inference and return result with tool calls and speech content.
62
+ * Updates agent state during execution.
63
+ */
64
+ runInference(availableTools: ToolDefinition[], budget?: TokenBudget, options?: InferenceOptions): Promise<InferenceResult>;
65
+ /**
66
+ * Run inference with context injections.
67
+ * Same as runInference but passes injections through to compile.
68
+ */
69
+ runInferenceWithInjections(availableTools: ToolDefinition[], injections?: ContextInjection[], budget?: TokenBudget, options?: InferenceOptions): Promise<InferenceResult>;
70
+ /**
71
+ * Provide a tool result.
72
+ */
73
+ provideToolResult(callId: ToolCallId, result: ToolResult): void;
74
+ /**
75
+ * Start a yielding stream for inference.
76
+ * Returns the stream — the caller (framework) iterates it.
77
+ */
78
+ startStream(availableTools: ToolDefinition[], budget?: TokenBudget): Promise<StartStreamResult>;
79
+ /**
80
+ * Start a yielding stream with context injections.
81
+ * Same as startStream but passes injections through to compile.
82
+ */
83
+ startStreamWithInjections(availableTools: ToolDefinition[], injections?: ContextInjection[], budget?: TokenBudget): Promise<StartStreamResult>;
84
+ /**
85
+ * Transition to waiting_for_tools when stream yields tool calls.
86
+ * Called by framework's driveStream.
87
+ */
88
+ enterWaitingForTools(calls: ToolCall[], stream: YieldingStream): void;
89
+ /**
90
+ * Add an assistant response to context.
91
+ * Called by framework when stream completes.
92
+ */
93
+ addAssistantResponse(content: ContentBlock[]): void;
94
+ /**
95
+ * Transition back to streaming state after tool results are provided.
96
+ */
97
+ setStreaming(stream: YieldingStream): void;
98
+ /**
99
+ * Cancel any active stream and reset to idle.
100
+ */
101
+ cancelStream(): void;
102
+ /**
103
+ * Check if agent has pending tool calls.
104
+ */
105
+ hasPendingTools(): boolean;
106
+ /**
107
+ * Get pending tool call IDs.
108
+ */
109
+ getPendingToolIds(): ToolCallId[];
110
+ /**
111
+ * Reset agent to idle state.
112
+ */
113
+ reset(): void;
114
+ /**
115
+ * Get the context manager.
116
+ */
117
+ getContextManager(): ContextManager;
118
+ /**
119
+ * Build the effective system prompt, appending any system-position injections.
120
+ */
121
+ private buildSystemPrompt;
122
+ abortInference(reason?: string): {
123
+ aborted: true;
124
+ durationMs: number;
125
+ } | false;
126
+ private doInference;
127
+ private extractToolCallsAndSpeech;
128
+ private buildToolResultMessages;
129
+ }
130
+ //# 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,EAAE,QAAQ,EAAqB,iBAAiB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAGxH,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,iBAAiB,CAAC;CAC5B;AACD,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC/G,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EAGV,UAAU,EACV,QAAQ,EACR,UAAU,EACV,cAAc,EACd,SAAS,EACT,eAAe,EACf,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,qBAAa,KAAK;IAChB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,YAAY,EAAE,KAAK,GAAG,MAAM,EAAE,CAAC;IACxC,QAAQ,CAAC,cAAc,EAAE,KAAK,GAAG,MAAM,EAAE,CAAC;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CA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