@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.
- package/LICENSE +21 -0
- package/README.md +233 -0
- package/dist/src/agent.d.ts +130 -0
- package/dist/src/agent.d.ts.map +1 -0
- package/dist/src/agent.js +406 -0
- package/dist/src/agent.js.map +1 -0
- package/dist/src/api/mcp-server.d.ts +37 -0
- package/dist/src/api/mcp-server.d.ts.map +1 -0
- package/dist/src/api/mcp-server.js +598 -0
- package/dist/src/api/mcp-server.js.map +1 -0
- package/dist/src/api/server.d.ts +83 -0
- package/dist/src/api/server.d.ts.map +1 -0
- package/dist/src/api/server.js +785 -0
- package/dist/src/api/server.js.map +1 -0
- package/dist/src/api/types.d.ts +395 -0
- package/dist/src/api/types.d.ts.map +1 -0
- package/dist/src/api/types.js +5 -0
- package/dist/src/api/types.js.map +1 -0
- package/dist/src/framework.d.ts +309 -0
- package/dist/src/framework.d.ts.map +1 -0
- package/dist/src/framework.js +2309 -0
- package/dist/src/framework.js.map +1 -0
- package/dist/src/gate/event-gate.d.ts +75 -0
- package/dist/src/gate/event-gate.d.ts.map +1 -0
- package/dist/src/gate/event-gate.js +503 -0
- package/dist/src/gate/event-gate.js.map +1 -0
- package/dist/src/gate/index.d.ts +3 -0
- package/dist/src/gate/index.d.ts.map +1 -0
- package/dist/src/gate/index.js +2 -0
- package/dist/src/gate/index.js.map +1 -0
- package/dist/src/gate/types.d.ts +82 -0
- package/dist/src/gate/types.d.ts.map +1 -0
- package/dist/src/gate/types.js +9 -0
- package/dist/src/gate/types.js.map +1 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +18 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mcpl/channel-registry.d.ts +141 -0
- package/dist/src/mcpl/channel-registry.d.ts.map +1 -0
- package/dist/src/mcpl/channel-registry.js +647 -0
- package/dist/src/mcpl/channel-registry.js.map +1 -0
- package/dist/src/mcpl/checkpoint-manager.d.ts +79 -0
- package/dist/src/mcpl/checkpoint-manager.d.ts.map +1 -0
- package/dist/src/mcpl/checkpoint-manager.js +430 -0
- package/dist/src/mcpl/checkpoint-manager.js.map +1 -0
- package/dist/src/mcpl/errors.d.ts +52 -0
- package/dist/src/mcpl/errors.d.ts.map +1 -0
- package/dist/src/mcpl/errors.js +93 -0
- package/dist/src/mcpl/errors.js.map +1 -0
- package/dist/src/mcpl/feature-set-manager.d.ts +91 -0
- package/dist/src/mcpl/feature-set-manager.d.ts.map +1 -0
- package/dist/src/mcpl/feature-set-manager.js +250 -0
- package/dist/src/mcpl/feature-set-manager.js.map +1 -0
- package/dist/src/mcpl/hook-orchestrator.d.ts +51 -0
- package/dist/src/mcpl/hook-orchestrator.d.ts.map +1 -0
- package/dist/src/mcpl/hook-orchestrator.js +217 -0
- package/dist/src/mcpl/hook-orchestrator.js.map +1 -0
- package/dist/src/mcpl/index.d.ts +14 -0
- package/dist/src/mcpl/index.d.ts.map +1 -0
- package/dist/src/mcpl/index.js +21 -0
- package/dist/src/mcpl/index.js.map +1 -0
- package/dist/src/mcpl/inference-router.d.ts +67 -0
- package/dist/src/mcpl/inference-router.d.ts.map +1 -0
- package/dist/src/mcpl/inference-router.js +249 -0
- package/dist/src/mcpl/inference-router.js.map +1 -0
- package/dist/src/mcpl/push-handler.d.ts +60 -0
- package/dist/src/mcpl/push-handler.d.ts.map +1 -0
- package/dist/src/mcpl/push-handler.js +162 -0
- package/dist/src/mcpl/push-handler.js.map +1 -0
- package/dist/src/mcpl/scope-manager.d.ts +76 -0
- package/dist/src/mcpl/scope-manager.d.ts.map +1 -0
- package/dist/src/mcpl/scope-manager.js +213 -0
- package/dist/src/mcpl/scope-manager.js.map +1 -0
- package/dist/src/mcpl/server-connection.d.ts +174 -0
- package/dist/src/mcpl/server-connection.d.ts.map +1 -0
- package/dist/src/mcpl/server-connection.js +559 -0
- package/dist/src/mcpl/server-connection.js.map +1 -0
- package/dist/src/mcpl/server-registry.d.ts +63 -0
- package/dist/src/mcpl/server-registry.d.ts.map +1 -0
- package/dist/src/mcpl/server-registry.js +119 -0
- package/dist/src/mcpl/server-registry.js.map +1 -0
- package/dist/src/mcpl/types.d.ts +755 -0
- package/dist/src/mcpl/types.d.ts.map +1 -0
- package/dist/src/mcpl/types.js +48 -0
- package/dist/src/mcpl/types.js.map +1 -0
- package/dist/src/module-registry.d.ts +93 -0
- package/dist/src/module-registry.d.ts.map +1 -0
- package/dist/src/module-registry.js +386 -0
- package/dist/src/module-registry.js.map +1 -0
- package/dist/src/modules/api/index.d.ts +22 -0
- package/dist/src/modules/api/index.d.ts.map +1 -0
- package/dist/src/modules/api/index.js +63 -0
- package/dist/src/modules/api/index.js.map +1 -0
- package/dist/src/modules/discord/discord-js-client.d.ts +58 -0
- package/dist/src/modules/discord/discord-js-client.d.ts.map +1 -0
- package/dist/src/modules/discord/discord-js-client.js +273 -0
- package/dist/src/modules/discord/discord-js-client.js.map +1 -0
- package/dist/src/modules/discord/index.d.ts +82 -0
- package/dist/src/modules/discord/index.d.ts.map +1 -0
- package/dist/src/modules/discord/index.js +959 -0
- package/dist/src/modules/discord/index.js.map +1 -0
- package/dist/src/modules/discord/types.d.ts +327 -0
- package/dist/src/modules/discord/types.d.ts.map +1 -0
- package/dist/src/modules/discord/types.js +5 -0
- package/dist/src/modules/discord/types.js.map +1 -0
- package/dist/src/modules/index.d.ts +11 -0
- package/dist/src/modules/index.d.ts.map +1 -0
- package/dist/src/modules/index.js +7 -0
- package/dist/src/modules/index.js.map +1 -0
- package/dist/src/modules/workspace/index.d.ts +59 -0
- package/dist/src/modules/workspace/index.d.ts.map +1 -0
- package/dist/src/modules/workspace/index.js +831 -0
- package/dist/src/modules/workspace/index.js.map +1 -0
- package/dist/src/modules/workspace/sync.d.ts +48 -0
- package/dist/src/modules/workspace/sync.d.ts.map +1 -0
- package/dist/src/modules/workspace/sync.js +253 -0
- package/dist/src/modules/workspace/sync.js.map +1 -0
- package/dist/src/modules/workspace/types.d.ts +166 -0
- package/dist/src/modules/workspace/types.d.ts.map +1 -0
- package/dist/src/modules/workspace/types.js +5 -0
- package/dist/src/modules/workspace/types.js.map +1 -0
- package/dist/src/modules/workspace/watcher.d.ts +43 -0
- package/dist/src/modules/workspace/watcher.d.ts.map +1 -0
- package/dist/src/modules/workspace/watcher.js +120 -0
- package/dist/src/modules/workspace/watcher.js.map +1 -0
- package/dist/src/queue.d.ts +50 -0
- package/dist/src/queue.d.ts.map +1 -0
- package/dist/src/queue.js +95 -0
- package/dist/src/queue.js.map +1 -0
- package/dist/src/safe-slice.d.ts +8 -0
- package/dist/src/safe-slice.d.ts.map +1 -0
- package/dist/src/safe-slice.js +18 -0
- package/dist/src/safe-slice.js.map +1 -0
- package/dist/src/types/agent.d.ts +118 -0
- package/dist/src/types/agent.d.ts.map +1 -0
- package/dist/src/types/agent.js +2 -0
- package/dist/src/types/agent.js.map +1 -0
- package/dist/src/types/events.d.ts +220 -0
- package/dist/src/types/events.d.ts.map +1 -0
- package/dist/src/types/events.js +2 -0
- package/dist/src/types/events.js.map +1 -0
- package/dist/src/types/framework.d.ts +184 -0
- package/dist/src/types/framework.d.ts.map +1 -0
- package/dist/src/types/framework.js +2 -0
- package/dist/src/types/framework.js.map +1 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +7 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/inference-log.d.ts +64 -0
- package/dist/src/types/inference-log.d.ts.map +1 -0
- package/dist/src/types/inference-log.js +5 -0
- package/dist/src/types/inference-log.js.map +1 -0
- package/dist/src/types/module.d.ts +302 -0
- package/dist/src/types/module.d.ts.map +1 -0
- package/dist/src/types/module.js +2 -0
- package/dist/src/types/module.js.map +1 -0
- package/dist/src/types/trace.d.ts +173 -0
- package/dist/src/types/trace.d.ts.map +1 -0
- package/dist/src/types/trace.js +2 -0
- package/dist/src/types/trace.js.map +1 -0
- package/dist/test/event-gate.test.d.ts +2 -0
- package/dist/test/event-gate.test.d.ts.map +1 -0
- package/dist/test/event-gate.test.js +535 -0
- package/dist/test/event-gate.test.js.map +1 -0
- package/dist/test/framework.test.d.ts +5 -0
- package/dist/test/framework.test.d.ts.map +1 -0
- package/dist/test/framework.test.js +738 -0
- package/dist/test/framework.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
- package/src/agent.ts +512 -0
- package/src/api/mcp-server.ts +687 -0
- package/src/api/server.ts +1023 -0
- package/src/api/types.ts +542 -0
- package/src/framework.ts +2784 -0
- package/src/gate/event-gate.ts +623 -0
- package/src/gate/index.ts +12 -0
- package/src/gate/types.ts +112 -0
- package/src/index.ts +63 -0
- package/src/mcpl/channel-registry.ts +824 -0
- package/src/mcpl/checkpoint-manager.ts +545 -0
- package/src/mcpl/errors.ts +114 -0
- package/src/mcpl/feature-set-manager.ts +333 -0
- package/src/mcpl/hook-orchestrator.ts +281 -0
- package/src/mcpl/index.ts +134 -0
- package/src/mcpl/inference-router.ts +335 -0
- package/src/mcpl/push-handler.ts +232 -0
- package/src/mcpl/scope-manager.ts +262 -0
- package/src/mcpl/server-connection.ts +694 -0
- package/src/mcpl/server-registry.ts +144 -0
- package/src/mcpl/types.ts +991 -0
- package/src/module-registry.ts +547 -0
- package/src/modules/api/index.ts +94 -0
- package/src/modules/discord/discord-js-client.ts +353 -0
- package/src/modules/discord/index.ts +1146 -0
- package/src/modules/discord/types.ts +465 -0
- package/src/modules/index.ts +43 -0
- package/src/modules/workspace/index.ts +969 -0
- package/src/modules/workspace/sync.ts +314 -0
- package/src/modules/workspace/types.ts +202 -0
- package/src/modules/workspace/watcher.ts +142 -0
- package/src/queue.ts +106 -0
- package/src/safe-slice.ts +18 -0
- package/src/types/agent.ts +119 -0
- package/src/types/events.ts +252 -0
- package/src/types/framework.ts +211 -0
- package/src/types/index.ts +6 -0
- package/src/types/inference-log.ts +64 -0
- package/src/types/module.ts +367 -0
- 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":"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"}
|