@aomi-labs/client 0.1.0 → 0.1.2

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/index.d.ts CHANGED
@@ -28,9 +28,9 @@ interface AomiMessage {
28
28
  * GET /api/state
29
29
  * Fetches current session state including messages and processing status
30
30
  */
31
- interface ApiStateResponse {
31
+ interface AomiStateResponse {
32
32
  messages?: AomiMessage[] | null;
33
- system_events?: ApiSystemEvent[] | null;
33
+ system_events?: AomiSystemEvent[] | null;
34
34
  title?: string | null;
35
35
  is_processing?: boolean;
36
36
  }
@@ -38,9 +38,9 @@ interface ApiStateResponse {
38
38
  * POST /api/chat
39
39
  * Sends a chat message and returns updated session state
40
40
  */
41
- interface ApiChatResponse {
41
+ interface AomiChatResponse {
42
42
  messages?: AomiMessage[] | null;
43
- system_events?: ApiSystemEvent[] | null;
43
+ system_events?: AomiSystemEvent[] | null;
44
44
  title?: string | null;
45
45
  is_processing?: boolean;
46
46
  }
@@ -48,19 +48,19 @@ interface ApiChatResponse {
48
48
  * POST /api/system
49
49
  * Sends a system message and returns the response message
50
50
  */
51
- interface ApiSystemResponse {
51
+ interface AomiSystemResponse {
52
52
  res?: AomiMessage | null;
53
53
  }
54
54
  /**
55
55
  * POST /api/interrupt
56
56
  * Interrupts current processing and returns updated session state
57
57
  */
58
- type ApiInterruptResponse = ApiChatResponse;
58
+ type AomiInterruptResponse = AomiChatResponse;
59
59
  /**
60
60
  * GET /api/sessions
61
- * Returns array of ApiThread
61
+ * Returns array of AomiThread
62
62
  */
63
- interface ApiThread {
63
+ interface AomiThread {
64
64
  session_id: string;
65
65
  title: string;
66
66
  is_archived?: boolean;
@@ -69,20 +69,20 @@ interface ApiThread {
69
69
  * POST /api/sessions
70
70
  * Creates a new thread/session
71
71
  */
72
- interface ApiCreateThreadResponse {
72
+ interface AomiCreateThreadResponse {
73
73
  session_id: string;
74
74
  title?: string;
75
75
  }
76
76
  /**
77
77
  * Base SSE event - all events have session_id and type
78
78
  */
79
- type ApiSSEEvent = {
79
+ type AomiSSEEvent = {
80
80
  type: "title_changed" | "tool_update" | "tool_complete" | "system_notice" | string;
81
81
  session_id: string;
82
82
  new_title?: string;
83
83
  [key: string]: unknown;
84
84
  };
85
- type ApiSSEEventType = "title_changed" | "tool_update" | "tool_complete" | "system_notice";
85
+ type AomiSSEEventType = "title_changed" | "tool_update" | "tool_complete" | "system_notice";
86
86
  /**
87
87
  * Backend SystemEvent enum serializes as tagged JSON:
88
88
  * - InlineCall: {"InlineCall": {"type": "wallet_tx_request", "payload": {...}}}
@@ -90,7 +90,7 @@ type ApiSSEEventType = "title_changed" | "tool_update" | "tool_complete" | "syst
90
90
  * - SystemError: {"SystemError": "message"}
91
91
  * - AsyncCallback: {"AsyncCallback": {...}} (not sent over HTTP)
92
92
  */
93
- type ApiSystemEvent = {
93
+ type AomiSystemEvent = {
94
94
  InlineCall: {
95
95
  type: string;
96
96
  payload?: unknown;
@@ -103,19 +103,19 @@ type ApiSystemEvent = {
103
103
  } | {
104
104
  AsyncCallback: Record<string, unknown>;
105
105
  };
106
- declare function isInlineCall(event: ApiSystemEvent): event is {
106
+ declare function isInlineCall(event: AomiSystemEvent): event is {
107
107
  InlineCall: {
108
108
  type: string;
109
109
  payload?: unknown;
110
110
  };
111
111
  };
112
- declare function isSystemNotice(event: ApiSystemEvent): event is {
112
+ declare function isSystemNotice(event: AomiSystemEvent): event is {
113
113
  SystemNotice: string;
114
114
  };
115
- declare function isSystemError(event: ApiSystemEvent): event is {
115
+ declare function isSystemError(event: AomiSystemEvent): event is {
116
116
  SystemError: string;
117
117
  };
118
- declare function isAsyncCallback(event: ApiSystemEvent): event is {
118
+ declare function isAsyncCallback(event: AomiSystemEvent): event is {
119
119
  AsyncCallback: Record<string, unknown>;
120
120
  };
121
121
 
@@ -128,7 +128,7 @@ declare class AomiClient {
128
128
  /**
129
129
  * Fetch current session state (messages, processing status, title).
130
130
  */
131
- fetchState(sessionId: string, userState?: UserState): Promise<ApiStateResponse>;
131
+ fetchState(sessionId: string, userState?: UserState): Promise<AomiStateResponse>;
132
132
  /**
133
133
  * Send a chat message and return updated session state.
134
134
  */
@@ -137,33 +137,33 @@ declare class AomiClient {
137
137
  publicKey?: string;
138
138
  apiKey?: string;
139
139
  userState?: UserState;
140
- }): Promise<ApiChatResponse>;
140
+ }): Promise<AomiChatResponse>;
141
141
  /**
142
142
  * Send a system-level message (e.g. wallet state changes, context switches).
143
143
  */
144
- sendSystemMessage(sessionId: string, message: string): Promise<ApiSystemResponse>;
144
+ sendSystemMessage(sessionId: string, message: string): Promise<AomiSystemResponse>;
145
145
  /**
146
146
  * Interrupt the AI's current response.
147
147
  */
148
- interrupt(sessionId: string): Promise<ApiInterruptResponse>;
148
+ interrupt(sessionId: string): Promise<AomiInterruptResponse>;
149
149
  /**
150
150
  * Subscribe to real-time SSE updates for a session.
151
151
  * Automatically reconnects with exponential backoff on disconnects.
152
152
  * Returns an unsubscribe function.
153
153
  */
154
- subscribeSSE(sessionId: string, onUpdate: (event: ApiSSEEvent) => void, onError?: (error: unknown) => void): () => void;
154
+ subscribeSSE(sessionId: string, onUpdate: (event: AomiSSEEvent) => void, onError?: (error: unknown) => void): () => void;
155
155
  /**
156
156
  * List all threads for a wallet address.
157
157
  */
158
- listThreads(publicKey: string): Promise<ApiThread[]>;
158
+ listThreads(publicKey: string): Promise<AomiThread[]>;
159
159
  /**
160
160
  * Get a single thread by ID.
161
161
  */
162
- getThread(sessionId: string): Promise<ApiThread>;
162
+ getThread(sessionId: string): Promise<AomiThread>;
163
163
  /**
164
164
  * Create a new thread. The client generates the session ID.
165
165
  */
166
- createThread(threadId: string, publicKey?: string): Promise<ApiCreateThreadResponse>;
166
+ createThread(threadId: string, publicKey?: string): Promise<AomiCreateThreadResponse>;
167
167
  /**
168
168
  * Delete a thread by ID.
169
169
  */
@@ -183,7 +183,7 @@ declare class AomiClient {
183
183
  /**
184
184
  * Get system events for a session.
185
185
  */
186
- getSystemEvents(sessionId: string, count?: number): Promise<ApiSystemEvent[]>;
186
+ getSystemEvents(sessionId: string, count?: number): Promise<AomiSystemEvent[]>;
187
187
  /**
188
188
  * Get available namespaces.
189
189
  */
@@ -194,7 +194,9 @@ declare class AomiClient {
194
194
  /**
195
195
  * Get available models.
196
196
  */
197
- getModels(sessionId: string): Promise<string[]>;
197
+ getModels(sessionId: string, options?: {
198
+ apiKey?: string;
199
+ }): Promise<string[]>;
198
200
  /**
199
201
  * Set the model for a session.
200
202
  */
@@ -209,4 +211,233 @@ declare class AomiClient {
209
211
  }>;
210
212
  }
211
213
 
212
- export { AomiClient, type AomiClientOptions, type AomiMessage, type ApiChatResponse, type ApiCreateThreadResponse, type ApiInterruptResponse, type ApiSSEEvent, type ApiSSEEventType, type ApiStateResponse, type ApiSystemEvent, type ApiSystemResponse, type ApiThread, type Logger, type UserState, isAsyncCallback, isInlineCall, isSystemError, isSystemNotice };
214
+ type Listener<T = unknown> = (payload: T) => void;
215
+ /**
216
+ * Minimal typed event emitter with wildcard support.
217
+ *
218
+ * ```ts
219
+ * type Events = { message: string; error: { code: number } };
220
+ * const ee = new TypedEventEmitter<Events>();
221
+ * ee.on("message", (msg) => console.log(msg));
222
+ * ee.emit("message", "hello");
223
+ * ```
224
+ */
225
+ declare class TypedEventEmitter<EventMap extends Record<string, unknown> = Record<string, unknown>> {
226
+ private listeners;
227
+ /**
228
+ * Subscribe to an event type. Returns an unsubscribe function.
229
+ */
230
+ on<K extends keyof EventMap & string>(type: K, handler: Listener<EventMap[K]>): () => void;
231
+ /**
232
+ * Subscribe to an event type for a single emission, then auto-unsubscribe.
233
+ */
234
+ once<K extends keyof EventMap & string>(type: K, handler: Listener<EventMap[K]>): () => void;
235
+ /**
236
+ * Emit an event to all listeners of `type` and wildcard `"*"` listeners.
237
+ */
238
+ emit<K extends keyof EventMap & string>(type: K, payload: EventMap[K]): void;
239
+ /**
240
+ * Remove a specific handler from an event type.
241
+ */
242
+ off<K extends keyof EventMap & string>(type: K, handler: Listener<EventMap[K]>): void;
243
+ /**
244
+ * Remove all listeners for all event types.
245
+ */
246
+ removeAllListeners(): void;
247
+ }
248
+
249
+ type WalletTxPayload = {
250
+ to: string;
251
+ value?: string;
252
+ data?: string;
253
+ chainId?: number;
254
+ };
255
+ type WalletEip712Payload = {
256
+ typed_data?: {
257
+ domain?: {
258
+ chainId?: number | string;
259
+ };
260
+ types?: Record<string, Array<{
261
+ name: string;
262
+ type: string;
263
+ }>>;
264
+ primaryType?: string;
265
+ message?: Record<string, unknown>;
266
+ };
267
+ description?: string;
268
+ };
269
+ /**
270
+ * Normalize a wallet_tx_request payload into a consistent shape.
271
+ * Returns `null` if the payload is missing the required `to` field.
272
+ */
273
+ declare function normalizeTxPayload(payload: unknown): WalletTxPayload | null;
274
+ /**
275
+ * Normalize an EIP-712 signing request payload.
276
+ */
277
+ declare function normalizeEip712Payload(payload: unknown): WalletEip712Payload;
278
+
279
+ type WalletRequestKind = "transaction" | "eip712_sign";
280
+ type WalletRequest = {
281
+ id: string;
282
+ kind: WalletRequestKind;
283
+ payload: WalletTxPayload | WalletEip712Payload;
284
+ timestamp: number;
285
+ };
286
+ type WalletRequestResult = {
287
+ txHash?: string;
288
+ signature?: string;
289
+ amount?: string;
290
+ };
291
+ type SendResult = {
292
+ messages: AomiMessage[];
293
+ title?: string;
294
+ };
295
+ type SessionOptions = {
296
+ /** Session ID. Auto-generated (crypto.randomUUID) if omitted. */
297
+ sessionId?: string;
298
+ /** Namespace for chat messages. Default: "default" */
299
+ namespace?: string;
300
+ /** User public key (wallet address). */
301
+ publicKey?: string;
302
+ /** API key override. */
303
+ apiKey?: string;
304
+ /** User state to send with requests (wallet connection info, etc). */
305
+ userState?: UserState;
306
+ /** Polling interval in ms. Default: 500 */
307
+ pollIntervalMs?: number;
308
+ /** Logger for debug output. Pass `console` for verbose logging. */
309
+ logger?: {
310
+ debug: (...args: unknown[]) => void;
311
+ };
312
+ };
313
+ /** Events emitted by Session. */
314
+ type SessionEventMap = {
315
+ /** A transaction signing request arrived from the backend. */
316
+ wallet_tx_request: WalletRequest;
317
+ /** An EIP-712 signing request arrived from the backend. */
318
+ wallet_eip712_request: WalletRequest;
319
+ /** A system notice from the backend. */
320
+ system_notice: {
321
+ message: string;
322
+ };
323
+ /** A system error from the backend. */
324
+ system_error: {
325
+ message: string;
326
+ };
327
+ /** An async callback event. */
328
+ async_callback: Record<string, unknown>;
329
+ /** SSE: tool execution in progress. */
330
+ tool_update: AomiSSEEvent;
331
+ /** SSE: tool execution completed. */
332
+ tool_complete: AomiSSEEvent;
333
+ /** Session title changed. */
334
+ title_changed: {
335
+ title: string;
336
+ };
337
+ /** Messages updated (new messages from poll or send response). */
338
+ messages: AomiMessage[];
339
+ /** AI started processing. */
340
+ processing_start: undefined;
341
+ /** AI finished processing. */
342
+ processing_end: undefined;
343
+ /** An error occurred during polling or SSE. */
344
+ error: {
345
+ error: unknown;
346
+ };
347
+ /** Wildcard: receives all events as { type, payload }. */
348
+ "*": {
349
+ type: string;
350
+ payload: unknown;
351
+ };
352
+ };
353
+ declare class Session extends TypedEventEmitter<SessionEventMap> {
354
+ /** The underlying low-level client. */
355
+ readonly client: AomiClient;
356
+ /** The session (thread) ID. */
357
+ readonly sessionId: string;
358
+ private namespace;
359
+ private publicKey?;
360
+ private apiKey?;
361
+ private userState?;
362
+ private pollIntervalMs;
363
+ private logger?;
364
+ private pollTimer;
365
+ private unsubscribeSSE;
366
+ private _isProcessing;
367
+ private walletRequests;
368
+ private walletRequestNextId;
369
+ private _messages;
370
+ private _title?;
371
+ private closed;
372
+ private pendingResolve;
373
+ constructor(clientOrOptions: AomiClient | AomiClientOptions, sessionOptions?: SessionOptions);
374
+ /**
375
+ * Send a message and wait for the AI to finish processing.
376
+ *
377
+ * The returned promise resolves when `is_processing` becomes `false` AND
378
+ * there are no pending wallet requests. If a wallet request arrives
379
+ * mid-processing, polling continues but the promise pauses until the
380
+ * request is resolved or rejected via `resolve()` / `reject()`.
381
+ */
382
+ send(message: string): Promise<SendResult>;
383
+ /**
384
+ * Send a message without waiting for completion.
385
+ * Polling starts in the background; listen to events for updates.
386
+ */
387
+ sendAsync(message: string): Promise<AomiChatResponse>;
388
+ /**
389
+ * Resolve a pending wallet request (transaction or EIP-712 signing).
390
+ * Sends the result to the backend and resumes polling.
391
+ */
392
+ resolve(requestId: string, result: WalletRequestResult): Promise<void>;
393
+ /**
394
+ * Reject a pending wallet request.
395
+ * Sends an error to the backend and resumes polling.
396
+ */
397
+ reject(requestId: string, reason?: string): Promise<void>;
398
+ /**
399
+ * Cancel the AI's current response.
400
+ */
401
+ interrupt(): Promise<void>;
402
+ /**
403
+ * Close the session. Stops polling, unsubscribes SSE, removes all listeners.
404
+ * The session cannot be used after closing.
405
+ */
406
+ close(): void;
407
+ /** Current messages in the session. */
408
+ getMessages(): AomiMessage[];
409
+ /** Current session title. */
410
+ getTitle(): string | undefined;
411
+ /** Pending wallet requests waiting for resolve/reject. */
412
+ getPendingRequests(): WalletRequest[];
413
+ /** Whether the AI is currently processing. */
414
+ getIsProcessing(): boolean;
415
+ private startPolling;
416
+ private stopPolling;
417
+ private pollTick;
418
+ private applyState;
419
+ private dispatchSystemEvents;
420
+ private handleSSEEvent;
421
+ private enqueueWalletRequest;
422
+ private removeWalletRequest;
423
+ private sendSystemEvent;
424
+ private resolvePending;
425
+ private assertOpen;
426
+ }
427
+
428
+ type UnwrappedEvent = {
429
+ type: string;
430
+ payload: unknown;
431
+ };
432
+ /**
433
+ * Unwrap a tagged-enum AomiSystemEvent from the backend into a flat event.
434
+ *
435
+ * ```ts
436
+ * const event: AomiSystemEvent = { InlineCall: { type: "wallet_tx_request", payload: { to: "0x..." } } };
437
+ * const unwrapped = unwrapSystemEvent(event);
438
+ * // => { type: "wallet_tx_request", payload: { to: "0x..." } }
439
+ * ```
440
+ */
441
+ declare function unwrapSystemEvent(event: AomiSystemEvent): UnwrappedEvent | null;
442
+
443
+ export { type AomiChatResponse, AomiClient, type AomiClientOptions, type AomiCreateThreadResponse, type AomiInterruptResponse, type AomiMessage, type AomiSSEEvent, type AomiSSEEventType, type AomiStateResponse, type AomiSystemEvent, type AomiSystemResponse, type AomiThread, type Logger, type SendResult, Session, type SessionEventMap, type SessionOptions, TypedEventEmitter, type UnwrappedEvent, type UserState, type WalletEip712Payload, type WalletRequest, type WalletRequestKind, type WalletRequestResult, type WalletTxPayload, isAsyncCallback, isInlineCall, isSystemError, isSystemNotice, normalizeEip712Payload, normalizeTxPayload, unwrapSystemEvent };