@axiom-lattice/client-sdk 0.1.1

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 ADDED
@@ -0,0 +1,163 @@
1
+ # Axiom Lattice Client SDK
2
+
3
+ A TypeScript client for the Axiom Lattice Agent Service API.
4
+
5
+ ## Overview
6
+
7
+ The Client SDK provides a simple interface for interacting with the Axiom Lattice Agent Service API. It handles authentication, request/response formatting, and error handling.
8
+
9
+ ## Features
10
+
11
+ - Create and manage threads
12
+ - Send messages and receive responses
13
+ - Stream responses with event-based callbacks
14
+ - Register client-side tools
15
+ - Handle tool calls and responses
16
+ - Process streaming message chunks with ChunkMessageMerger
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @axiom-lattice/client-sdk
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```typescript
27
+ import { Client, createSimpleMessageMerger } from "@axiom-lattice/client-sdk";
28
+
29
+ // Create a client
30
+ const client = new Client({
31
+ baseURL: "https://api.example.com",
32
+ apiKey: "your-api-key",
33
+ assistantId: "your-assistant-id",
34
+ transport: "sse",
35
+ });
36
+
37
+ // Create a thread
38
+ const threadId = await client.createThread({
39
+ metadata: { user: "user123" },
40
+ });
41
+
42
+ // Send a message
43
+ const response = await client.chat.send({
44
+ threadId,
45
+ messages: [
46
+ {
47
+ role: "user",
48
+ content: "Hello, how can you help me?",
49
+ id: "msg-1",
50
+ },
51
+ ],
52
+ });
53
+
54
+ // Stream a message
55
+ const stopStreaming = client.chat.stream(
56
+ {
57
+ threadId,
58
+ messages: [
59
+ {
60
+ role: "user",
61
+ content: "Tell me a story",
62
+ id: "msg-2",
63
+ },
64
+ ],
65
+ },
66
+ (event) => {
67
+ console.log("Event:", event);
68
+ },
69
+ () => {
70
+ console.log("Stream completed");
71
+ },
72
+ (error) => {
73
+ console.error("Stream error:", error);
74
+ }
75
+ );
76
+
77
+ // Stop streaming if needed
78
+ // stopStreaming();
79
+
80
+ // Use ChunkMessageMerger to process streaming chunks
81
+ const merger = createSimpleMessageMerger();
82
+
83
+ // Process user message
84
+ merger.push({
85
+ type: "human",
86
+ data: {
87
+ id: "user-1",
88
+ content: "Hello",
89
+ },
90
+ });
91
+
92
+ // Process AI message with tool calls
93
+ merger.push({
94
+ type: "ai",
95
+ data: {
96
+ id: "ai-1",
97
+ content: "I need to search for that.",
98
+ tool_calls: [
99
+ {
100
+ name: "search",
101
+ args: { query: "typescript" },
102
+ id: "search-1",
103
+ },
104
+ ],
105
+ },
106
+ });
107
+
108
+ // Process tool response
109
+ merger.push({
110
+ type: "tool",
111
+ data: {
112
+ id: "tool-1",
113
+ content: "Search results: ...",
114
+ tool_call_id: "search-1",
115
+ },
116
+ });
117
+
118
+ // Get merged messages
119
+ const messages = merger.getMessages();
120
+ ```
121
+
122
+ ## Migration Notes
123
+
124
+ ### ChunkMessageMerger
125
+
126
+ The `ChunkMessageMerger` module has been moved from the web project to the client-sdk package. It provides functionality for processing streaming message chunks and merging them into complete messages.
127
+
128
+ To use it:
129
+
130
+ ```typescript
131
+ import { createSimpleMessageMerger } from "@axiom-lattice/client-sdk";
132
+
133
+ const merger = createSimpleMessageMerger();
134
+ // Use merger methods: push, getMessages, reset, etc.
135
+ ```
136
+
137
+ ### Future Migration Plans
138
+
139
+ The current web project uses its own implementation of `useChat` that is tightly integrated with the web app's API and message format. In the future, we plan to migrate the web project to use the react-sdk's `useChat` hook, which provides a more standardized interface for chat interactions.
140
+
141
+ Steps for future migration:
142
+
143
+ 1. Update the web project's message types to align with the react-sdk's message format
144
+ 2. Replace direct API calls with client-sdk methods
145
+ 3. Integrate the react-sdk's useChat hook
146
+ 4. Update UI components to work with the new message format
147
+
148
+ ## API Reference
149
+
150
+ ### Client
151
+
152
+ The main client class for interacting with the Axiom Lattice Agent Service API.
153
+
154
+ ### ChunkMessageMerger
155
+
156
+ A utility for processing streaming message chunks and merging them into complete messages.
157
+
158
+ - `createSimpleMessageMerger()`: Creates a new message merger instance
159
+ - `push(chunk)`: Processes a message chunk
160
+ - `getMessages()`: Gets all messages with tool calls merged
161
+ - `getMessagesWithoutToolCalls()`: Gets messages without tool calls
162
+ - `initialMessages(msgs)`: Initializes with existing messages
163
+ - `reset()`: Resets the merger state
@@ -0,0 +1,388 @@
1
+ import { Message, MessageChunk, AssistantMessage } from '@axiom-lattice/protocols';
2
+
3
+ /**
4
+ * Core types for the Axiom Lattice Client SDK
5
+ */
6
+
7
+ /**
8
+ * Transport method for client-server communication
9
+ * Note: Currently only "ws" is used for tool registration
10
+ */
11
+ type Transport = "sse" | "ws";
12
+ /**
13
+ * Configuration options for the Client
14
+ */
15
+ interface ClientConfig {
16
+ /**
17
+ * Base URL for the API
18
+ */
19
+ baseURL: string;
20
+ /**
21
+ * API key for authentication
22
+ */
23
+ apiKey: string;
24
+ /**
25
+ * Assistant identifier
26
+ */
27
+ assistantId: string;
28
+ /**
29
+ * Transport method (Server-Sent Events or WebSocket)
30
+ */
31
+ transport: Transport;
32
+ /**
33
+ * Request timeout in milliseconds (optional, defaults to 30000)
34
+ */
35
+ timeout?: number;
36
+ /**
37
+ * Additional headers to include in requests (optional)
38
+ */
39
+ headers?: Record<string, string>;
40
+ /**
41
+ * Configuration for retry behavior (optional)
42
+ */
43
+ retry?: RetryConfig;
44
+ }
45
+ /**
46
+ * Configuration for retry behavior
47
+ */
48
+ interface RetryConfig {
49
+ /**
50
+ * Maximum number of retry attempts
51
+ */
52
+ maxRetries: number;
53
+ /**
54
+ * Initial delay between retries in milliseconds
55
+ */
56
+ initialDelayMs: number;
57
+ /**
58
+ * Maximum delay between retries in milliseconds
59
+ */
60
+ maxDelayMs: number;
61
+ /**
62
+ * Factor to multiply delay by after each retry attempt
63
+ */
64
+ backoffFactor: number;
65
+ }
66
+ /**
67
+ * Thread creation options
68
+ */
69
+ interface CreateThreadOptions {
70
+ /**
71
+ * Optional metadata for the thread
72
+ */
73
+ metadata?: Record<string, any>;
74
+ }
75
+ /**
76
+ * Thread information
77
+ */
78
+ interface Thread {
79
+ /**
80
+ * Thread identifier
81
+ */
82
+ id: string;
83
+ /**
84
+ * Thread metadata
85
+ */
86
+ metadata?: Record<string, any>;
87
+ /**
88
+ * Thread creation timestamp
89
+ */
90
+ createdAt: string;
91
+ }
92
+ /**
93
+ * Chat send options
94
+ */
95
+ interface ChatSendOptions {
96
+ /**
97
+ * Thread identifier
98
+ */
99
+ threadId: string;
100
+ /**
101
+ * Messages to send
102
+ */
103
+ messages: Message[];
104
+ }
105
+ /**
106
+ * Chat stream options
107
+ */
108
+ interface ChatStreamOptions extends ChatSendOptions {
109
+ /**
110
+ * Whether to run in background (optional)
111
+ */
112
+ background?: boolean;
113
+ }
114
+ /**
115
+ * Stream callbacks interface
116
+ */
117
+ interface StreamCallbacks {
118
+ /**
119
+ * Called when a stream event is received
120
+ */
121
+ onEvent: (event: MessageChunk) => void;
122
+ /**
123
+ * Called when the stream completes
124
+ */
125
+ onComplete?: () => void;
126
+ /**
127
+ * Called when an error occurs
128
+ */
129
+ onError?: (error: Error) => void;
130
+ }
131
+ /**
132
+ * Chat response
133
+ */
134
+ interface ChatResponse {
135
+ /**
136
+ * Response message
137
+ */
138
+ message: AssistantMessage;
139
+ /**
140
+ * Trace ID for debugging
141
+ */
142
+ traceId: string;
143
+ }
144
+ /**
145
+ * Tool registration options
146
+ */
147
+ interface RegisterToolOptions {
148
+ /**
149
+ * Tool name
150
+ */
151
+ name: string;
152
+ /**
153
+ * Tool parameter schema
154
+ */
155
+ schema: object;
156
+ /**
157
+ * Tool handler function
158
+ */
159
+ handler: (params: any) => Promise<any>;
160
+ }
161
+ /**
162
+ * Get messages options
163
+ */
164
+ interface GetMessagesOptions {
165
+ /**
166
+ * Thread identifier
167
+ */
168
+ threadId: string;
169
+ /**
170
+ * Maximum number of messages to return
171
+ */
172
+ limit?: number;
173
+ /**
174
+ * Message ID to start from (for pagination)
175
+ */
176
+ after?: string;
177
+ /**
178
+ * Whether to return messages in reverse chronological order
179
+ */
180
+ reverse?: boolean;
181
+ }
182
+ /**
183
+ * Thread list options
184
+ */
185
+ interface ListThreadsOptions {
186
+ /**
187
+ * Maximum number of threads to return
188
+ */
189
+ limit?: number;
190
+ /**
191
+ * Offset for pagination
192
+ */
193
+ offset?: number;
194
+ }
195
+ /**
196
+ * Run options
197
+ */
198
+ interface RunOptions {
199
+ /**
200
+ * Thread identifier
201
+ */
202
+ threadId: string;
203
+ /**
204
+ * Message to send
205
+ */
206
+ message: string;
207
+ /**
208
+ * Files to attach (optional)
209
+ */
210
+ files?: any[];
211
+ /**
212
+ * Command to execute (optional)
213
+ */
214
+ command?: any;
215
+ /**
216
+ * Whether to stream the response (optional)
217
+ */
218
+ streaming?: boolean;
219
+ /**
220
+ * Whether to run in background (optional)
221
+ */
222
+ background?: boolean;
223
+ }
224
+ /**
225
+ * Agent state
226
+ */
227
+ interface AgentState {
228
+ values: {
229
+ messages: Message[];
230
+ [key: string]: any;
231
+ };
232
+ tasks: Array<{
233
+ interrupts: Array<{
234
+ value: any;
235
+ }>;
236
+ }>;
237
+ }
238
+ /**
239
+ * Custom error classes
240
+ */
241
+ declare class ApiError extends Error {
242
+ statusCode: number;
243
+ responseBody?: any | undefined;
244
+ constructor(message: string, statusCode: number, responseBody?: any | undefined);
245
+ }
246
+ declare class NetworkError extends Error {
247
+ constructor(message: string);
248
+ }
249
+ declare class AuthenticationError extends Error {
250
+ constructor(message: string);
251
+ }
252
+
253
+ /**
254
+ * Main client class for interacting with the Axiom Lattice Agent Service API
255
+ */
256
+ declare class Client {
257
+ private client;
258
+ private config;
259
+ private assistantId;
260
+ private tenantId;
261
+ private registeredTools;
262
+ /**
263
+ * Creates a new Client instance
264
+ * @param config - Configuration options for the client
265
+ */
266
+ constructor(config: ClientConfig);
267
+ /**
268
+ * Sets up axios interceptors for error handling
269
+ * @private
270
+ */
271
+ private setupInterceptors;
272
+ /**
273
+ * Set tenant ID for multi-tenant environments
274
+ * @param tenantId - Tenant identifier
275
+ */
276
+ setTenantId(tenantId: string): void;
277
+ /**
278
+ * Creates a new thread
279
+ * @param options - Options for creating a thread
280
+ * @returns A promise that resolves to the thread ID
281
+ */
282
+ createThread(options: CreateThreadOptions): Promise<string>;
283
+ /**
284
+ * Retrieves thread information
285
+ * @param threadId - Thread identifier
286
+ * @returns A promise that resolves to the thread information
287
+ */
288
+ getThread(threadId: string): Promise<Thread>;
289
+ /**
290
+ * Lists all threads
291
+ * @param options - Options for listing threads
292
+ * @returns A promise that resolves to an array of threads
293
+ */
294
+ listThreads(options?: ListThreadsOptions): Promise<Thread[]>;
295
+ /**
296
+ * Deletes a thread
297
+ * @param threadId - Thread identifier
298
+ * @returns A promise that resolves when the thread is deleted
299
+ */
300
+ deleteThread(threadId: string): Promise<void>;
301
+ /**
302
+ * Retrieves messages from a thread
303
+ * @param options - Options for retrieving messages
304
+ * @returns A promise that resolves to an array of messages
305
+ */
306
+ getMessages(options: GetMessagesOptions): Promise<Message[]>;
307
+ /**
308
+ * Retrieves agent state
309
+ * @param threadId - Thread identifier
310
+ * @returns A promise that resolves to the agent state
311
+ */
312
+ getAgentState(threadId: string): Promise<AgentState>;
313
+ /**
314
+ * Gets agent graph visualization
315
+ * @returns A promise that resolves to the graph visualization data
316
+ */
317
+ getAgentGraph(): Promise<string>;
318
+ /**
319
+ * Run agent with options
320
+ * @param options - Options for running the agent
321
+ * @returns A promise that resolves to the run result or a stream
322
+ */
323
+ run(options: RunOptions): Promise<any>;
324
+ /**
325
+ * Stream run results
326
+ * @param options - Options for streaming run results
327
+ * @param onEvent - Callback function that receives stream events
328
+ * @param onComplete - Optional callback function called when streaming completes
329
+ * @param onError - Optional callback function called when an error occurs
330
+ * @returns A function that can be called to stop the stream
331
+ */
332
+ private streamRun;
333
+ /**
334
+ * Chat namespace for sending messages and streaming responses
335
+ */
336
+ chat: {
337
+ /**
338
+ * Sends a message to a thread and receives a response
339
+ * @param options - Options for sending a message
340
+ * @returns A promise that resolves to the chat response
341
+ */
342
+ send: (options: ChatSendOptions) => Promise<ChatResponse>;
343
+ /**
344
+ * Sends a message to a thread and streams the response
345
+ * @param options - Options for streaming a message
346
+ * @param onEvent - Callback function that receives stream events
347
+ * @param onComplete - Optional callback function called when streaming completes
348
+ * @param onError - Optional callback function called when an error occurs
349
+ * @returns A function that can be called to stop the stream
350
+ */
351
+ stream: (options: ChatStreamOptions, onEvent: (event: MessageChunk) => void, onComplete?: () => void, onError?: (error: Error) => void) => (() => void);
352
+ };
353
+ /**
354
+ * Tools namespace for registering and unregistering client-side tools
355
+ */
356
+ tools: {
357
+ /**
358
+ * Registers a client-side tool
359
+ * @param options - Options for registering a tool
360
+ */
361
+ register: (options: RegisterToolOptions) => void;
362
+ /**
363
+ * Unregisters a client-side tool
364
+ * @param name - Tool name
365
+ */
366
+ unregister: (name: string) => void;
367
+ };
368
+ }
369
+
370
+ /**
371
+ * ChunkMessageMerger
372
+ *
373
+ * A utility for handling streaming message chunks and merging them into complete messages.
374
+ */
375
+
376
+ /**
377
+ * Creates a simple message merger for handling streaming message chunks
378
+ * @returns An object with methods to push chunks and retrieve merged messages
379
+ */
380
+ declare function createSimpleMessageMerger(): {
381
+ push: (chunk: MessageChunk) => void;
382
+ initialMessages: (msgs: any[]) => void;
383
+ getMessages: () => any[];
384
+ getMessagesWithoutToolCalls: () => any[];
385
+ reset: () => void;
386
+ };
387
+
388
+ export { AgentState, ApiError, AuthenticationError, ChatResponse, ChatSendOptions, ChatStreamOptions, Client, ClientConfig, CreateThreadOptions, GetMessagesOptions, ListThreadsOptions, NetworkError, RegisterToolOptions, RetryConfig, RunOptions, StreamCallbacks, Thread, Transport, createSimpleMessageMerger };