agentxjs 1.9.9-dev → 2.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/README.md +257 -0
- package/dist/index.d.ts +223 -96
- package/dist/index.js +559 -289
- package/dist/index.js.map +1 -1
- package/package.json +12 -4
- package/src/LocalClient.ts +89 -0
- package/src/RemoteClient.ts +27 -135
- package/src/index.ts +117 -38
- package/src/namespaces/agents.ts +121 -0
- package/src/namespaces/containers.ts +68 -0
- package/src/namespaces/images.ts +180 -0
- package/src/namespaces/presentations.ts +22 -0
- package/src/namespaces/sessions.ts +60 -0
- package/src/presentation/Presentation.ts +14 -10
- package/src/presentation/index.ts +2 -0
- package/src/presentation/reducer.ts +272 -103
- package/src/presentation/types.ts +10 -0
- package/src/types.ts +200 -61
package/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# agentxjs
|
|
2
|
+
|
|
3
|
+
Client SDK for building AI agent applications. Supports local (embedded) and remote (WebSocket) modes through a single unified API.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`agentxjs` is the main SDK that developers use directly. It auto-detects the mode based on config: provide `apiKey` for local mode (no server needed), or `serverUrl` for remote mode (connects to an AgentX server over WebSocket).
|
|
8
|
+
|
|
9
|
+
## Quick Start
|
|
10
|
+
|
|
11
|
+
### When to use which mode?
|
|
12
|
+
|
|
13
|
+
| | Local Mode | Remote Mode |
|
|
14
|
+
| ------------- | ----------------------------------------------- | ------------------------------------------------------------ |
|
|
15
|
+
| **Use when** | Prototyping, CLI tools, single-user apps, tests | Multi-tenant apps, shared infrastructure, horizontal scaling |
|
|
16
|
+
| **Trade-off** | Simpler setup, no server to manage | Centralized state, multiple clients can share agents |
|
|
17
|
+
| **Data** | In-process SQLite (or `:memory:`) | Server-managed persistent storage |
|
|
18
|
+
| **Config** | `apiKey` | `serverUrl` |
|
|
19
|
+
|
|
20
|
+
**Start with Local mode.** Switch to Remote when you need multiple processes or users sharing the same agents.
|
|
21
|
+
|
|
22
|
+
### Local Mode (Embedded)
|
|
23
|
+
|
|
24
|
+
Runs agents directly in your process. No server required.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { createAgentX } from "agentxjs";
|
|
28
|
+
|
|
29
|
+
const agentx = await createAgentX({
|
|
30
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
31
|
+
provider: "anthropic",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await agentx.containers.create("my-app");
|
|
35
|
+
|
|
36
|
+
const { record: image } = await agentx.images.create({
|
|
37
|
+
containerId: "my-app",
|
|
38
|
+
systemPrompt: "You are a helpful assistant.",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const { agentId } = await agentx.agents.create({ imageId: image.imageId });
|
|
42
|
+
|
|
43
|
+
agentx.on("text_delta", (e) => process.stdout.write(e.data.text));
|
|
44
|
+
await agentx.sessions.send(agentId, "Hello!");
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Remote Mode (WebSocket)
|
|
48
|
+
|
|
49
|
+
Connects to a running AgentX server. Same API surface.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { createAgentX } from "agentxjs";
|
|
53
|
+
|
|
54
|
+
const agentx = await createAgentX({
|
|
55
|
+
serverUrl: "ws://localhost:5200",
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Identical API as local mode
|
|
59
|
+
await agentx.containers.create("my-app");
|
|
60
|
+
const { record: image } = await agentx.images.create({
|
|
61
|
+
containerId: "my-app",
|
|
62
|
+
systemPrompt: "You are a helpful assistant.",
|
|
63
|
+
});
|
|
64
|
+
const { agentId } = await agentx.agents.create({ imageId: image.imageId });
|
|
65
|
+
|
|
66
|
+
agentx.on("text_delta", (e) => process.stdout.write(e.data.text));
|
|
67
|
+
await agentx.sessions.send(agentId, "Hello!");
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## API Reference
|
|
71
|
+
|
|
72
|
+
### `createAgentX(config: AgentXConfig): Promise<AgentX>`
|
|
73
|
+
|
|
74
|
+
Creates an AgentX client. Mode is auto-detected:
|
|
75
|
+
|
|
76
|
+
| Config field | Mode | Trigger |
|
|
77
|
+
| ---------------- | ------ | --------------------------------------- |
|
|
78
|
+
| `serverUrl` | Remote | Connects via WebSocket |
|
|
79
|
+
| `apiKey` | Local | Starts embedded runtime with MonoDriver |
|
|
80
|
+
| `createDriver` | Local | Uses custom driver factory |
|
|
81
|
+
| `customPlatform` | Local | Uses custom platform |
|
|
82
|
+
|
|
83
|
+
### AgentX Interface
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
interface AgentX {
|
|
87
|
+
readonly connected: boolean;
|
|
88
|
+
readonly events: EventBus;
|
|
89
|
+
|
|
90
|
+
// Namespaced operations
|
|
91
|
+
readonly containers: ContainerNamespace;
|
|
92
|
+
readonly images: ImageNamespace;
|
|
93
|
+
readonly agents: AgentNamespace;
|
|
94
|
+
readonly sessions: SessionNamespace;
|
|
95
|
+
readonly presentations: PresentationNamespace;
|
|
96
|
+
|
|
97
|
+
// Event subscription
|
|
98
|
+
on<T extends string>(type: T, handler: BusEventHandler): Unsubscribe;
|
|
99
|
+
onAny(handler: BusEventHandler): Unsubscribe;
|
|
100
|
+
subscribe(sessionId: string): void;
|
|
101
|
+
|
|
102
|
+
// Lifecycle
|
|
103
|
+
disconnect(): Promise<void>;
|
|
104
|
+
dispose(): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Namespace Operations
|
|
109
|
+
|
|
110
|
+
**containers**:
|
|
111
|
+
|
|
112
|
+
- `create(containerId: string): Promise<ContainerCreateResponse>`
|
|
113
|
+
- `get(containerId: string): Promise<ContainerGetResponse>`
|
|
114
|
+
- `list(): Promise<ContainerListResponse>`
|
|
115
|
+
|
|
116
|
+
**images**:
|
|
117
|
+
|
|
118
|
+
- `create(params: { containerId, name?, description?, systemPrompt?, mcpServers? }): Promise<ImageCreateResponse>`
|
|
119
|
+
- `get(imageId: string): Promise<ImageGetResponse>`
|
|
120
|
+
- `list(containerId?: string): Promise<ImageListResponse>`
|
|
121
|
+
- `delete(imageId: string): Promise<BaseResponse>`
|
|
122
|
+
|
|
123
|
+
**agents**:
|
|
124
|
+
|
|
125
|
+
- `create(params: { imageId, agentId? }): Promise<AgentCreateResponse>`
|
|
126
|
+
- `get(agentId: string): Promise<AgentGetResponse>`
|
|
127
|
+
- `list(containerId?: string): Promise<AgentListResponse>`
|
|
128
|
+
- `destroy(agentId: string): Promise<BaseResponse>`
|
|
129
|
+
|
|
130
|
+
**sessions**:
|
|
131
|
+
|
|
132
|
+
- `send(agentId: string, content: string | unknown[]): Promise<MessageSendResponse>`
|
|
133
|
+
- `interrupt(agentId: string): Promise<BaseResponse>`
|
|
134
|
+
|
|
135
|
+
**presentations**:
|
|
136
|
+
|
|
137
|
+
- `create(agentId: string, options?: PresentationOptions): Presentation`
|
|
138
|
+
|
|
139
|
+
### Stream Events
|
|
140
|
+
|
|
141
|
+
| Event | Data | Description |
|
|
142
|
+
| ------------------ | -------------------------- | ---------------------- |
|
|
143
|
+
| `message_start` | `{ messageId, model }` | Response begins |
|
|
144
|
+
| `text_delta` | `{ text }` | Incremental text chunk |
|
|
145
|
+
| `tool_use_start` | `{ toolCallId, toolName }` | Tool call begins |
|
|
146
|
+
| `input_json_delta` | `{ partialJson }` | Incremental tool input |
|
|
147
|
+
| `tool_result` | `{ toolCallId, result }` | Tool execution result |
|
|
148
|
+
| `message_stop` | `{ stopReason }` | Response complete |
|
|
149
|
+
| `error` | `{ message }` | Error occurred |
|
|
150
|
+
|
|
151
|
+
### Presentation API
|
|
152
|
+
|
|
153
|
+
High-level UI state management. Aggregates raw stream events into a structured conversation state.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const presentation = await agentx.presentations.create(agentId, {
|
|
157
|
+
onUpdate: (state: PresentationState) => renderUI(state),
|
|
158
|
+
onError: (error) => console.error(error),
|
|
159
|
+
});
|
|
160
|
+
// For existing sessions, getState() already contains conversation history
|
|
161
|
+
|
|
162
|
+
await presentation.send("What is the weather?");
|
|
163
|
+
const state = presentation.getState();
|
|
164
|
+
// state.conversations -- completed conversations (includes history)
|
|
165
|
+
// state.streaming -- current streaming response (or null)
|
|
166
|
+
// state.status -- "idle" | "thinking" | "responding" | "executing"
|
|
167
|
+
|
|
168
|
+
presentation.dispose();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**PresentationState**:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
interface PresentationState {
|
|
175
|
+
conversations: Conversation[]; // UserConversation | AssistantConversation | ErrorConversation
|
|
176
|
+
streaming: AssistantConversation | null;
|
|
177
|
+
status: "idle" | "thinking" | "responding" | "executing";
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Block types**: `TextBlock { type: "text", content }`, `ToolBlock { type: "tool", toolName, status, ... }`, `ImageBlock { type: "image", url }`
|
|
182
|
+
|
|
183
|
+
For custom state management, use the exported reducer:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { presentationReducer, createInitialState, addUserConversation } from "agentxjs";
|
|
187
|
+
|
|
188
|
+
let state = createInitialState();
|
|
189
|
+
state = addUserConversation(state, "Hello");
|
|
190
|
+
state = presentationReducer(state, event); // pure function
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Configuration
|
|
194
|
+
|
|
195
|
+
### AgentXConfig
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
interface AgentXConfig {
|
|
199
|
+
// --- Local Mode ---
|
|
200
|
+
apiKey?: string; // LLM provider API key
|
|
201
|
+
provider?: LLMProvider; // default: "anthropic"
|
|
202
|
+
model?: string; // e.g. "claude-sonnet-4-20250514"
|
|
203
|
+
baseUrl?: string; // custom API endpoint
|
|
204
|
+
dataPath?: string; // default: ":memory:"
|
|
205
|
+
createDriver?: CreateDriver; // custom driver factory (advanced)
|
|
206
|
+
customPlatform?: AgentXPlatform; // custom platform (advanced)
|
|
207
|
+
|
|
208
|
+
// --- Remote Mode ---
|
|
209
|
+
serverUrl?: string; // WebSocket URL
|
|
210
|
+
headers?: MaybeAsync<Record<string, string>>; // auth headers
|
|
211
|
+
context?: MaybeAsync<Record<string, unknown>>; // business context
|
|
212
|
+
|
|
213
|
+
// --- Common ---
|
|
214
|
+
timeout?: number; // default: 30000 ms
|
|
215
|
+
debug?: boolean;
|
|
216
|
+
autoReconnect?: boolean; // default: true (remote only)
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
| Field | Type | Default | Description |
|
|
221
|
+
| --------------- | ------------------------------------- | ---------------- | ----------------------------------------------------------------------------------- |
|
|
222
|
+
| `apiKey` | `string` | -- | LLM provider API key (triggers local mode) |
|
|
223
|
+
| `provider` | `LLMProvider` | `"anthropic"` | `"anthropic"` \| `"openai"` \| `"google"` \| `"xai"` \| `"deepseek"` \| `"mistral"` |
|
|
224
|
+
| `model` | `string` | provider default | Model identifier |
|
|
225
|
+
| `baseUrl` | `string` | -- | Custom API endpoint |
|
|
226
|
+
| `dataPath` | `string` | `":memory:"` | SQLite path or `:memory:` |
|
|
227
|
+
| `serverUrl` | `string` | -- | WebSocket URL (triggers remote mode) |
|
|
228
|
+
| `headers` | `MaybeAsync<Record<string, string>>` | -- | Static, dynamic, or async auth headers |
|
|
229
|
+
| `context` | `MaybeAsync<Record<string, unknown>>` | -- | Business context (userId, tenantId, etc.) |
|
|
230
|
+
| `timeout` | `number` | `30000` | Request timeout in ms |
|
|
231
|
+
| `debug` | `boolean` | `false` | Enable debug logging |
|
|
232
|
+
| `autoReconnect` | `boolean` | `true` | Auto reconnect on connection loss (remote only) |
|
|
233
|
+
|
|
234
|
+
### MaybeAsync
|
|
235
|
+
|
|
236
|
+
`headers` and `context` accept static values, functions, or async functions:
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
type MaybeAsync<T> = T | (() => T) | (() => Promise<T>);
|
|
240
|
+
|
|
241
|
+
// Static
|
|
242
|
+
headers: {
|
|
243
|
+
Authorization: "Bearer sk-xxx";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Dynamic
|
|
247
|
+
headers: () => ({ Authorization: `Bearer ${getToken()}` });
|
|
248
|
+
|
|
249
|
+
// Async
|
|
250
|
+
headers: async () => ({ Authorization: `Bearer ${await refreshToken()}` });
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Environment Variables
|
|
254
|
+
|
|
255
|
+
| Variable | Description |
|
|
256
|
+
| ------------------- | -------------------------------------- |
|
|
257
|
+
| `ANTHROPIC_API_KEY` | Default API key for Anthropic provider |
|