@useago/sdk 0.4.7 → 1.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.
@@ -0,0 +1,482 @@
1
+ /**
2
+ * Agent information
3
+ */
4
+ declare interface AgoAgent {
5
+ id: string;
6
+ name: string;
7
+ displayName?: string;
8
+ }
9
+
10
+ /**
11
+ * Main SDK client for AGO Chat integration
12
+ */
13
+ declare class AgoClient {
14
+ private httpClient;
15
+ private functionRegistry;
16
+ private contextRegistry;
17
+ private eventEmitter;
18
+ private config;
19
+ constructor(config: AgoConfig);
20
+ /**
21
+ * Send a message and receive a streaming response
22
+ */
23
+ sendMessage(content: string, options?: SendMessageOptions): Promise<AgoMessage>;
24
+ private processSSEResponse;
25
+ private handleClientFunctionInvocation;
26
+ /**
27
+ * Get list of conversations
28
+ */
29
+ getConversations(): Promise<Conversation[]>;
30
+ /**
31
+ * Get a specific conversation with messages
32
+ */
33
+ getConversation(conversationId: string): Promise<Conversation>;
34
+ /**
35
+ * Map the backend's snake_case `tool_call_data` entries onto the camelCase
36
+ * {@link ToolCallData} shape used everywhere else in the SDK. Mirrors
37
+ * `SSEHandler.parseToolCall` so persisted and streamed tool calls match.
38
+ */
39
+ private static mapPersistedToolCalls;
40
+ /**
41
+ * Get messages for a conversation
42
+ */
43
+ getMessages(conversationId: string): Promise<AgoMessage[]>;
44
+ /**
45
+ * Submit form data for a tool call
46
+ */
47
+ submitToolCallForm(toolCallId: string, formData: Record<string, unknown>): Promise<void>;
48
+ /**
49
+ * Confirm a tool call
50
+ */
51
+ confirmToolCall(toolCallId: string): Promise<void>;
52
+ /**
53
+ * Reject a tool call
54
+ */
55
+ rejectToolCall(toolCallId: string): Promise<void>;
56
+ /**
57
+ * Relay a completed form collector's values to a server-configured destination.
58
+ *
59
+ * Used by `createFormCollector` / `useFormCollector` in `{ via: "backend" }` mode:
60
+ * the browser never holds the destination URL or its secret — the backend resolves
61
+ * `destination` (a named, opted-in tool) and forwards the values from the server.
62
+ */
63
+ submitFormCollector(destination: string, values: Record<string, unknown>): Promise<unknown>;
64
+ /**
65
+ * Submit feedback for a message
66
+ */
67
+ submitFeedback(messageId: string, rating: "positive" | "negative"): Promise<void>;
68
+ /**
69
+ * Register a client-side function that AGO can call.
70
+ * Accepts either a definition object or (name, handler, schema) args.
71
+ *
72
+ * ```ts
73
+ * // Single-object (preferred)
74
+ * client.registerFunction({
75
+ * name: "lookupOrder",
76
+ * description: "Look up an order",
77
+ * parameters: { type: "object", properties: { id: { type: "string" } } },
78
+ * handler: async (args) => fetchOrder(args.id),
79
+ * });
80
+ *
81
+ * // Classic 3-arg form
82
+ * client.registerFunction("lookupOrder", handler, schema);
83
+ * ```
84
+ */
85
+ registerFunction(definition: ClientFunctionDefinition): void;
86
+ registerFunction(name: string, handler: ClientFunctionHandler, schema: Omit<ClientFunctionSchema, "name">): void;
87
+ /**
88
+ * Short alias for `registerFunction`. Also accepts an array of definitions.
89
+ *
90
+ * ```ts
91
+ * client.register(lookupOrder);
92
+ * client.register([lookupOrder, cancelOrder]);
93
+ * ```
94
+ */
95
+ register(definition: ClientFunctionDefinition | ClientFunctionDefinition[]): void;
96
+ /**
97
+ * Unregister a client-side function
98
+ */
99
+ unregisterFunction(name: string): boolean;
100
+ /**
101
+ * Get all registered function schemas
102
+ */
103
+ getRegisteredFunctions(): ClientFunctionSchema[];
104
+ /**
105
+ * Register a navigation function that lets AGO navigate users to different pages.
106
+ * @param navigate - A callback that performs the navigation (e.g. react-router's navigate)
107
+ * @param routes - Map of route names to paths, with descriptions for the LLM
108
+ */
109
+ registerNavigationFunction(navigate: (path: string) => void, routes: Array<{
110
+ name: string;
111
+ path: string;
112
+ description: string;
113
+ }>): void;
114
+ /**
115
+ * Register or update a piece of client-side context.
116
+ * This context is sent with every message so the AI understands the user's situation.
117
+ *
118
+ * ```ts
119
+ * client.setContext("order-page", {
120
+ * name: "Order detail",
121
+ * description: "User is viewing order #123",
122
+ * data: { orderId: "123", status: "shipped" },
123
+ * });
124
+ * ```
125
+ */
126
+ setContext(key: string, entry: ContextEntry): void;
127
+ /**
128
+ * Remove a previously registered context entry.
129
+ */
130
+ removeContext(key: string): boolean;
131
+ /**
132
+ * Register a dynamic context provider. The callback is invoked every time
133
+ * a message is sent, so the AI always gets the freshest data.
134
+ *
135
+ * Use this for context that lives outside React state — global stores,
136
+ * refs, or computed values.
137
+ *
138
+ * ```ts
139
+ * client.addDynamicContext("cart", () => ({
140
+ * name: "Checkout",
141
+ * data: { itemCount: cart.items.length, total: cart.total },
142
+ * }));
143
+ * ```
144
+ */
145
+ addDynamicContext(key: string, provider: DynamicContextProvider): void;
146
+ /**
147
+ * Remove a dynamic context provider.
148
+ */
149
+ removeDynamicContext(key: string): boolean;
150
+ /**
151
+ * Broadcast that the client-side context changed by emitting `context:changed`
152
+ * with the fresh snapshot. The context-mutating methods call this automatically;
153
+ * stateful helpers whose context lives in a store (e.g. `createFormCollector`)
154
+ * call it when their store updates so observers like the dev panel stay live.
155
+ */
156
+ notifyContextChanged(): void;
157
+ /**
158
+ * Enable automatic capture of the current browser page (URL + title).
159
+ * Injected as a dynamic context entry named `browser-page`.
160
+ */
161
+ enableAutoPageContext(): void;
162
+ /**
163
+ * Get the current context snapshot.
164
+ */
165
+ getContextSnapshot(): ContextSnapshot | null;
166
+ /**
167
+ * Subscribe to an event
168
+ */
169
+ on<K extends AgoEventName>(event: K, handler: AgoEventHandler<K>): void;
170
+ /**
171
+ * Unsubscribe from an event
172
+ */
173
+ off<K extends AgoEventName>(event: K, handler: AgoEventHandler<K>): void;
174
+ /**
175
+ * Subscribe to an event once — auto-unsubscribes after the first call.
176
+ */
177
+ once<K extends AgoEventName>(event: K, handler: AgoEventHandler<K>): void;
178
+ /**
179
+ * Returns a Promise that resolves the next time `event` fires.
180
+ *
181
+ * ```ts
182
+ * const msg = await client.waitFor("message:complete", { timeout: 10000 });
183
+ * ```
184
+ */
185
+ waitFor<K extends AgoEventName>(event: K, options?: {
186
+ timeout?: number;
187
+ }): Promise<AgoClientEvents[K]>;
188
+ /**
189
+ * Update client configuration
190
+ */
191
+ updateConfig(config: Partial<AgoConfig>): void;
192
+ /**
193
+ * Clean up resources
194
+ */
195
+ destroy(): void;
196
+ }
197
+
198
+ /**
199
+ * SDK Events
200
+ */
201
+ declare interface AgoClientEvents {
202
+ /**
203
+ * The client-side context changed: a context entry or dynamic provider was
204
+ * added/removed, or a stateful helper (e.g. `createFormCollector`) updated its
205
+ * store. Carries the fresh snapshot so observers — notably the dev panel — can
206
+ * repaint without waiting for the next message. Fires for the form collector's
207
+ * initial install too, so its missing fields show from the start of a conversation.
208
+ */
209
+ "context:changed": ContextSnapshot | null;
210
+ "message:start": {
211
+ conversationId: string;
212
+ messageId: string;
213
+ };
214
+ "message:chunk": {
215
+ content: string;
216
+ conversationId: string;
217
+ messageId: string;
218
+ };
219
+ /**
220
+ * The main answer text is done (backend emitted `status: "DONE"`), but follow-up
221
+ * replies may still be pending. Fires once, before `message:complete`.
222
+ */
223
+ "message:answer-complete": AgoMessage;
224
+ "message:complete": AgoMessage;
225
+ "message:error": {
226
+ error: string;
227
+ conversationId?: string;
228
+ messageId?: string;
229
+ };
230
+ /**
231
+ * A full conversation was loaded from the server (e.g. after a page reload).
232
+ * Carries the messages and their persisted tool calls so stateful helpers —
233
+ * notably `createFormCollector` — can replay them to restore their state.
234
+ */
235
+ "conversation:loaded": Conversation;
236
+ "toolCall:received": ToolCallData;
237
+ "toolCall:form": ToolCallData;
238
+ "function:invoke": ClientFunctionInvocation;
239
+ "function:result": {
240
+ invocationId: string;
241
+ result: unknown;
242
+ error?: string;
243
+ };
244
+ "connection:status": {
245
+ connected: boolean;
246
+ };
247
+ }
248
+
249
+ /**
250
+ * SDK Configuration
251
+ */
252
+ declare interface AgoConfig {
253
+ /** API base URL (e.g., https://YOUR-DOMAIN.useago.com) */
254
+ baseUrl: string;
255
+ /** Widget ID (X-Widget-Id header). Auto-generated if not provided. */
256
+ widgetId?: string;
257
+ /** Default agent (id or slug) for new conversations. Shorthand for `defaultAgentId`. */
258
+ agent?: string;
259
+ /** Default agent ID for new conversations. Prefer `agent`. */
260
+ defaultAgentId?: string;
261
+ /** Permission name to apply to all requests (sent as `X-Widget-Permission`). Mirrors the widget's `window.AGO.permission`. */
262
+ permission?: string;
263
+ /** User email for identification */
264
+ userEmail?: string;
265
+ /** JWT token for authenticated users */
266
+ userJwt?: string;
267
+ /** Enable debug logging */
268
+ debug?: boolean;
269
+ }
270
+
271
+ declare type AgoEventHandler<K extends AgoEventName> = (data: AgoClientEvents[K]) => void;
272
+
273
+ declare type AgoEventName = keyof AgoClientEvents;
274
+
275
+ /**
276
+ * Message from AGO
277
+ */
278
+ declare interface AgoMessage {
279
+ id: string;
280
+ conversationId: string;
281
+ content: string;
282
+ role: "user" | "assistant";
283
+ status: MessageStatus;
284
+ agent?: AgoAgent;
285
+ sources?: AgoSource[];
286
+ toolCalls?: ToolCallData[];
287
+ followUpReplies?: string[];
288
+ createdAt: Date;
289
+ }
290
+
291
+ /**
292
+ * Knowledge source citation
293
+ */
294
+ declare interface AgoSource {
295
+ id: string;
296
+ title: string;
297
+ url?: string;
298
+ }
299
+
300
+ /**
301
+ * Single-object function definition — combines name, schema, and handler.
302
+ *
303
+ * ```ts
304
+ * const fn = defineFunction({
305
+ * name: "lookupOrder",
306
+ * description: "Look up an order by ID",
307
+ * parameters: { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
308
+ * handler: async (args) => fetchOrder(args.id as string),
309
+ * });
310
+ * client.registerFunction(fn);
311
+ * ```
312
+ */
313
+ declare interface ClientFunctionDefinition {
314
+ name: string;
315
+ description: string;
316
+ parameters: ClientFunctionSchema["parameters"];
317
+ handler: ClientFunctionHandler;
318
+ }
319
+
320
+ /**
321
+ * Function handler type
322
+ */
323
+ declare type ClientFunctionHandler = (args: Record<string, unknown>) => Promise<unknown> | unknown;
324
+
325
+ declare interface ClientFunctionInvocation {
326
+ invocationId: string;
327
+ functionName: string;
328
+ arguments: Record<string, unknown>;
329
+ conversationId: string;
330
+ }
331
+
332
+ /**
333
+ * Schema for a client-side function that AGO can call
334
+ */
335
+ declare interface ClientFunctionSchema {
336
+ /** Function name (must be unique) */
337
+ name: string;
338
+ /** Description for the LLM to understand when to call this function */
339
+ description: string;
340
+ /** JSON Schema for function parameters */
341
+ parameters: {
342
+ type: "object";
343
+ properties: Record<string, {
344
+ type: string;
345
+ description?: string;
346
+ enum?: string[];
347
+ default?: unknown;
348
+ }>;
349
+ required?: string[];
350
+ };
351
+ }
352
+
353
+ /**
354
+ * A single piece of context the client wants the LLM to know about.
355
+ */
356
+ declare interface ContextEntry {
357
+ /** Human-readable label (e.g. "Order detail", "Current user") */
358
+ name?: string;
359
+ /** Short description of what this context represents */
360
+ description?: string;
361
+ /** Arbitrary structured data the AI should know about */
362
+ data?: Record<string, unknown>;
363
+ }
364
+
365
+ /**
366
+ * Serialised client context sent with each message.
367
+ */
368
+ declare interface ContextSnapshot {
369
+ /** All active context entries keyed by their registration key */
370
+ entries: Record<string, ContextEntry>;
371
+ }
372
+
373
+ /**
374
+ * Conversation/Thread
375
+ */
376
+ declare interface Conversation {
377
+ id: string;
378
+ title: string;
379
+ lastMessageDate: Date;
380
+ messages?: AgoMessage[];
381
+ }
382
+
383
+ /**
384
+ * Create a mock AgoClient for testing. Works with any framework.
385
+ *
386
+ * ```ts
387
+ * import { createMockClient } from "@useago/sdk/testing";
388
+ *
389
+ * const mock = createMockClient();
390
+ * mock.__emitEvent("message:complete", someMessage);
391
+ * expect(mock.__callsFor("sendMessage")).toHaveLength(1);
392
+ * ```
393
+ */
394
+ export declare function createMockClient(options?: MockAgoClientOptions): MockAgoClient;
395
+
396
+ /**
397
+ * A function that returns a fresh context entry on demand.
398
+ * Evaluated every time a message is sent — use it to pull data from stores,
399
+ * refs, or any source that isn't captured by React state.
400
+ */
401
+ declare type DynamicContextProvider = () => ContextEntry | null | undefined;
402
+
403
+ declare interface FormField {
404
+ type: "string" | "number" | "boolean" | "array";
405
+ title?: string;
406
+ description?: string;
407
+ enum?: string[];
408
+ default?: unknown;
409
+ }
410
+
411
+ /**
412
+ * Form schema for tool calls requiring user input
413
+ */
414
+ declare interface FormSchema {
415
+ type: "object";
416
+ properties: Record<string, FormField>;
417
+ required?: string[];
418
+ }
419
+
420
+ declare type MessageStatus = "IN_PROGRESS" | "DONE" | "ERROR" | "TODO" | "CANCELED";
421
+
422
+ export declare interface MockAgoClient extends AgoClient {
423
+ /**
424
+ * Simulate a server-pushed event.
425
+ * ```ts
426
+ * mock.__emitEvent("message:complete", { id: "1", content: "hi", ... });
427
+ * ```
428
+ */
429
+ __emitEvent<K extends AgoEventName>(event: K, data: AgoClientEvents[K]): void;
430
+ /**
431
+ * All recorded method calls: `[methodName, ...args][]`
432
+ */
433
+ __calls: Array<{
434
+ method: string;
435
+ args: unknown[];
436
+ }>;
437
+ /**
438
+ * Get calls for a specific method.
439
+ */
440
+ __callsFor(method: string): Array<{
441
+ method: string;
442
+ args: unknown[];
443
+ }>;
444
+ }
445
+
446
+ export declare interface MockAgoClientOptions {
447
+ overrides?: Partial<Record<string, MockFn>>;
448
+ }
449
+
450
+ declare type MockFn = (...args: any[]) => any;
451
+
452
+ /**
453
+ * Options for sending a message
454
+ */
455
+ declare interface SendMessageOptions {
456
+ /** Existing conversation ID */
457
+ conversationId?: string;
458
+ /** Override default agent */
459
+ agentId?: string;
460
+ /** File attachments */
461
+ files?: File[];
462
+ }
463
+
464
+ /**
465
+ * Tool call data from SSE stream
466
+ */
467
+ declare interface ToolCallData {
468
+ id: string;
469
+ type: ToolCallType;
470
+ status: string;
471
+ toolName: string;
472
+ toolDisplayName?: string;
473
+ message?: string;
474
+ formSchema?: FormSchema;
475
+ data?: Record<string, unknown>;
476
+ functionName?: string;
477
+ arguments?: Record<string, unknown>;
478
+ }
479
+
480
+ declare type ToolCallType = "form" | "confirmation_input" | "status_message" | "progress_indicator" | "client_function" | "reasoning" | "mcp_ui_resource";
481
+
482
+ export { }