agentxjs 2.0.3 → 2.0.5
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/dist/chunk-ZFL27HK7.js +467 -0
- package/dist/chunk-ZFL27HK7.js.map +1 -0
- package/dist/index.d.ts +185 -140
- package/dist/index.js +124 -84
- package/dist/index.js.map +1 -1
- package/dist/server-IFVYHIJF.js +7 -0
- package/dist/server-IFVYHIJF.js.map +1 -0
- package/package.json +4 -6
- package/src/CommandHandler.ts +424 -0
- package/src/LocalClient.ts +10 -10
- package/src/RemoteClient.ts +14 -14
- package/src/index.ts +125 -110
- package/src/namespaces/images.ts +12 -0
- package/src/namespaces/presentations.ts +1 -1
- package/src/presentation/Presentation.ts +2 -2
- package/src/server.ts +346 -0
- package/src/types.ts +89 -137
package/src/index.ts
CHANGED
|
@@ -1,146 +1,158 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* agentxjs - AgentX Client SDK
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Fluent API supporting local, remote, and server modes.
|
|
5
5
|
*
|
|
6
|
-
* @example Local mode
|
|
6
|
+
* @example Local mode
|
|
7
7
|
* ```typescript
|
|
8
8
|
* import { createAgentX } from "agentxjs";
|
|
9
|
+
* import { node } from "@agentxjs/node-platform";
|
|
9
10
|
*
|
|
10
|
-
* const
|
|
11
|
-
*
|
|
12
|
-
* provider: "anthropic",
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* await agentx.containers.create("my-app");
|
|
16
|
-
* const { record: image } = await agentx.images.create({
|
|
17
|
-
* containerId: "my-app",
|
|
18
|
-
* systemPrompt: "You are helpful",
|
|
19
|
-
* });
|
|
20
|
-
* const { agentId } = await agentx.agents.create({ imageId: image.imageId });
|
|
21
|
-
*
|
|
22
|
-
* agentx.on("text_delta", (e) => process.stdout.write(e.data.text));
|
|
23
|
-
* await agentx.sessions.send(agentId, "Hello!");
|
|
11
|
+
* const ax = createAgentX(node({ createDriver }));
|
|
12
|
+
* await ax.agent.create({ imageId: "..." });
|
|
24
13
|
* ```
|
|
25
14
|
*
|
|
26
|
-
* @example Remote mode
|
|
15
|
+
* @example Remote mode
|
|
27
16
|
* ```typescript
|
|
28
|
-
*
|
|
17
|
+
* const ax = createAgentX();
|
|
18
|
+
* const client = await ax.connect("ws://localhost:5200");
|
|
19
|
+
* ```
|
|
29
20
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* });
|
|
21
|
+
* @example Server mode
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const ax = createAgentX(node({ createDriver }));
|
|
24
|
+
* const server = await ax.serve({ port: 5200 });
|
|
33
25
|
* ```
|
|
34
26
|
*/
|
|
35
27
|
|
|
28
|
+
import type { CreateDriver } from "@agentxjs/core/driver";
|
|
29
|
+
import type { AgentXPlatform } from "@agentxjs/core/runtime";
|
|
30
|
+
import { createAgentXRuntime } from "@agentxjs/core/runtime";
|
|
36
31
|
import { LocalClient } from "./LocalClient";
|
|
37
32
|
import { RemoteClient } from "./RemoteClient";
|
|
38
|
-
import type { AgentX,
|
|
33
|
+
import type { AgentX, AgentXBuilder, AgentXServer, ConnectOptions, ServeConfig } from "./types";
|
|
39
34
|
|
|
40
35
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* Mode detection:
|
|
44
|
-
* - `serverUrl` present → **Remote mode** (WebSocket client)
|
|
45
|
-
* - `apiKey` present → **Local mode** (embedded Runtime + MonoDriver)
|
|
46
|
-
*
|
|
47
|
-
* @param config - Client configuration
|
|
48
|
-
* @returns Connected AgentX client
|
|
36
|
+
* Platform configuration for createAgentX
|
|
49
37
|
*/
|
|
50
|
-
export
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const resolvedConfig = await resolvePlatformForRemote(config);
|
|
54
|
-
const client = new RemoteClient(resolvedConfig);
|
|
55
|
-
await client.connect();
|
|
56
|
-
return client;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (config.apiKey || config.createDriver || config.customPlatform) {
|
|
60
|
-
// Local mode
|
|
61
|
-
return createLocalClient(config);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
throw new Error(
|
|
65
|
-
"Invalid AgentX config: provide either 'serverUrl' (remote mode) or 'apiKey' (local mode)"
|
|
66
|
-
);
|
|
38
|
+
export interface PlatformConfig {
|
|
39
|
+
platform: AgentXPlatform;
|
|
40
|
+
createDriver: CreateDriver;
|
|
67
41
|
}
|
|
68
42
|
|
|
69
43
|
/**
|
|
70
|
-
*
|
|
44
|
+
* Create an AgentX builder
|
|
71
45
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
46
|
+
* @param config - Platform configuration (optional). Without it, only connect() is available.
|
|
47
|
+
* @returns AgentXBuilder — local AgentX + connect() + serve()
|
|
74
48
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return config;
|
|
78
|
-
}
|
|
49
|
+
export function createAgentX(config?: PlatformConfig): AgentXBuilder {
|
|
50
|
+
let localClient: LocalClient | null = null;
|
|
79
51
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
52
|
+
function getLocalClient(): LocalClient {
|
|
53
|
+
if (localClient) return localClient;
|
|
54
|
+
if (!config) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Local mode requires a platform. Pass a PlatformConfig to createAgentX(), or use connect() for remote mode."
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const runtime = createAgentXRuntime(config.platform, config.createDriver);
|
|
60
|
+
localClient = new LocalClient(runtime);
|
|
61
|
+
return localClient;
|
|
83
62
|
}
|
|
84
63
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const { createNodeWebSocket } = await import("@agentxjs/node-platform/network");
|
|
88
|
-
return {
|
|
89
|
-
...config,
|
|
90
|
-
customPlatform: {
|
|
91
|
-
...config.customPlatform,
|
|
92
|
-
channelClient: createNodeWebSocket,
|
|
93
|
-
} as any,
|
|
94
|
-
};
|
|
95
|
-
} catch {
|
|
96
|
-
// node-platform not available, fall back to global WebSocket
|
|
97
|
-
return config;
|
|
64
|
+
if (config) {
|
|
65
|
+
getLocalClient();
|
|
98
66
|
}
|
|
99
|
-
}
|
|
100
67
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
68
|
+
return {
|
|
69
|
+
get connected() {
|
|
70
|
+
return localClient?.connected ?? false;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
get events() {
|
|
74
|
+
return getLocalClient().events;
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
get container() {
|
|
78
|
+
return getLocalClient().container;
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
get image() {
|
|
82
|
+
return getLocalClient().image;
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
get agent() {
|
|
86
|
+
return getLocalClient().agent;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
get session() {
|
|
90
|
+
return getLocalClient().session;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
get presentation() {
|
|
94
|
+
return getLocalClient().presentation;
|
|
95
|
+
},
|
|
118
96
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
97
|
+
on(type, handler) {
|
|
98
|
+
return getLocalClient().on(type, handler);
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
onAny(handler) {
|
|
102
|
+
return getLocalClient().onAny(handler);
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
subscribe(sessionId) {
|
|
106
|
+
getLocalClient().subscribe(sessionId);
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
async disconnect() {
|
|
110
|
+
await localClient?.disconnect();
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
async dispose() {
|
|
114
|
+
await localClient?.dispose();
|
|
115
|
+
localClient = null;
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
async connect(serverUrl: string, options?: ConnectOptions): Promise<AgentX> {
|
|
119
|
+
const remoteClient = new RemoteClient({
|
|
120
|
+
serverUrl,
|
|
121
|
+
headers: options?.headers as Record<string, string> | undefined,
|
|
122
|
+
context: options?.context,
|
|
123
|
+
timeout: options?.timeout,
|
|
124
|
+
autoReconnect: options?.autoReconnect,
|
|
125
|
+
customPlatform: config?.platform,
|
|
134
126
|
});
|
|
135
|
-
|
|
136
|
-
|
|
127
|
+
await remoteClient.connect();
|
|
128
|
+
return remoteClient;
|
|
129
|
+
},
|
|
137
130
|
|
|
138
|
-
|
|
139
|
-
|
|
131
|
+
async serve(serveConfig?: ServeConfig): Promise<AgentXServer> {
|
|
132
|
+
if (!config) {
|
|
133
|
+
throw new Error("serve() requires a platform. Pass a PlatformConfig to createAgentX().");
|
|
134
|
+
}
|
|
135
|
+
if (!config.platform.channelServer) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
"serve() requires platform.channelServer. Ensure your platform supports server mode."
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
const { createServer } = await import("./server");
|
|
142
|
+
return createServer({
|
|
143
|
+
platform: config.platform,
|
|
144
|
+
createDriver: config.createDriver,
|
|
145
|
+
port: serveConfig?.port,
|
|
146
|
+
host: serveConfig?.host,
|
|
147
|
+
server: serveConfig?.server as any,
|
|
148
|
+
wsPath: serveConfig?.wsPath,
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
};
|
|
142
152
|
}
|
|
143
153
|
|
|
154
|
+
// Re-export server
|
|
155
|
+
export { CommandHandler } from "./CommandHandler";
|
|
144
156
|
// Re-export Presentation types and classes
|
|
145
157
|
export type {
|
|
146
158
|
AssistantConversation,
|
|
@@ -164,6 +176,7 @@ export {
|
|
|
164
176
|
Presentation,
|
|
165
177
|
presentationReducer,
|
|
166
178
|
} from "./presentation";
|
|
179
|
+
export { createServer, type ServerConfig } from "./server";
|
|
167
180
|
// Re-export types
|
|
168
181
|
export type {
|
|
169
182
|
AgentCreateResponse,
|
|
@@ -172,8 +185,10 @@ export type {
|
|
|
172
185
|
AgentListResponse,
|
|
173
186
|
AgentNamespace,
|
|
174
187
|
AgentX,
|
|
175
|
-
|
|
188
|
+
AgentXBuilder,
|
|
189
|
+
AgentXServer,
|
|
176
190
|
BaseResponse,
|
|
191
|
+
ConnectOptions,
|
|
177
192
|
ContainerCreateResponse,
|
|
178
193
|
ContainerGetResponse,
|
|
179
194
|
ContainerInfo,
|
|
@@ -184,9 +199,9 @@ export type {
|
|
|
184
199
|
ImageListResponse,
|
|
185
200
|
ImageNamespace,
|
|
186
201
|
ImageRecord,
|
|
187
|
-
LLMProvider,
|
|
188
202
|
MaybeAsync,
|
|
189
203
|
MessageSendResponse,
|
|
190
204
|
PresentationNamespace,
|
|
205
|
+
ServeConfig,
|
|
191
206
|
SessionNamespace,
|
|
192
207
|
} from "./types";
|
package/src/namespaces/images.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Image namespace factories
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import type { Message } from "@agentxjs/core/agent";
|
|
5
6
|
import type { RpcClient } from "@agentxjs/core/network";
|
|
6
7
|
import type { AgentXPlatform } from "@agentxjs/core/runtime";
|
|
7
8
|
import type {
|
|
@@ -100,6 +101,12 @@ export function createLocalImages(platform: AgentXPlatform): ImageNamespace {
|
|
|
100
101
|
|
|
101
102
|
return { requestId: "" };
|
|
102
103
|
},
|
|
104
|
+
|
|
105
|
+
async getMessages(imageId: string): Promise<Message[]> {
|
|
106
|
+
const imageRecord = await platform.imageRepository.findImageById(imageId);
|
|
107
|
+
if (!imageRecord) return [];
|
|
108
|
+
return platform.sessionRepository.getMessages(imageRecord.sessionId);
|
|
109
|
+
},
|
|
103
110
|
};
|
|
104
111
|
}
|
|
105
112
|
|
|
@@ -176,5 +183,10 @@ export function createRemoteImages(
|
|
|
176
183
|
const result = await rpcClient.call<BaseResponse>("image.delete", { imageId });
|
|
177
184
|
return { ...result, requestId: "" };
|
|
178
185
|
},
|
|
186
|
+
|
|
187
|
+
async getMessages(imageId: string): Promise<Message[]> {
|
|
188
|
+
const result = await rpcClient.call<{ messages: Message[] }>("image.messages", { imageId });
|
|
189
|
+
return result.messages ?? [];
|
|
190
|
+
},
|
|
179
191
|
};
|
|
180
192
|
}
|
|
@@ -14,7 +14,7 @@ import type { AgentX, PresentationNamespace } from "../types";
|
|
|
14
14
|
export function createPresentations(agentx: AgentX): PresentationNamespace {
|
|
15
15
|
return {
|
|
16
16
|
async create(agentId: string, options?: PresentationOptions): Promise<Presentation> {
|
|
17
|
-
const messages = await agentx.
|
|
17
|
+
const messages = await agentx.session.getMessages(agentId);
|
|
18
18
|
const conversations = messagesToConversations(messages);
|
|
19
19
|
return new Presentation(agentx, agentId, options, conversations);
|
|
20
20
|
},
|
|
@@ -110,7 +110,7 @@ export class Presentation {
|
|
|
110
110
|
|
|
111
111
|
try {
|
|
112
112
|
// Send message via agentx
|
|
113
|
-
await this.agentx.
|
|
113
|
+
await this.agentx.session.send(this.agentId, content);
|
|
114
114
|
} catch (error) {
|
|
115
115
|
this.notifyError(error instanceof Error ? error : new Error(String(error)));
|
|
116
116
|
}
|
|
@@ -121,7 +121,7 @@ export class Presentation {
|
|
|
121
121
|
*/
|
|
122
122
|
async interrupt(): Promise<void> {
|
|
123
123
|
try {
|
|
124
|
-
await this.agentx.
|
|
124
|
+
await this.agentx.session.interrupt(this.agentId);
|
|
125
125
|
} catch (error) {
|
|
126
126
|
this.notifyError(error instanceof Error ? error : new Error(String(error)));
|
|
127
127
|
}
|