avin-ai 0.1.10 → 0.2.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/dist/index.d.ts CHANGED
@@ -2,6 +2,117 @@ export declare interface Agent {
2
2
  tts_voice?: TTSVoice;
3
3
  }
4
4
 
5
+ export declare class AvinAI {
6
+ private config;
7
+ private _status;
8
+ private client;
9
+ private listeners;
10
+ constructor(config: AvinAIConfig);
11
+ /** Current connection status */
12
+ get status(): AvinAIStatus;
13
+ /** Shorthand: true when status is 'connected' */
14
+ get isConnected(): boolean;
15
+ /**
16
+ * Register an event listener.
17
+ * @param event - Event name
18
+ * @param callback - Handler function
19
+ * @returns `this` for chaining
20
+ */
21
+ on<K extends keyof EventMap>(event: K, callback: EventCallback<EventMap[K]>): this;
22
+ /**
23
+ * Remove a previously registered event listener.
24
+ * @param event - Event name
25
+ * @param callback - The exact same function reference that was registered
26
+ */
27
+ off<K extends keyof EventMap>(event: K, callback: EventCallback<EventMap[K]>): this;
28
+ private emit;
29
+ /**
30
+ * Connect to the Voice AI agent and start the call.
31
+ * Requests microphone permission, establishes WebRTC, and opens the data channel.
32
+ */
33
+ start(): Promise<void>;
34
+ /**
35
+ * Disconnect the call and release all resources (microphone, WebRTC connection).
36
+ */
37
+ stop(): void;
38
+ /**
39
+ * Send the result of a **blocking** client-side tool back to the AI.
40
+ * The AI is paused and will resume once it receives this.
41
+ *
42
+ * @param toolCallId - The `tool_call_id` from the `tool_call` event
43
+ * @param result - Arbitrary JSON payload the AI needs to continue
44
+ *
45
+ * @example
46
+ * client.sendToolResult(tool_call_id, { selected_store: 'Store #5' });
47
+ */
48
+ sendToolResult(toolCallId: string, result: Record<string, any>): void;
49
+ /**
50
+ * Send an error result for a **blocking** client-side tool.
51
+ * Use when the user cancelled or the UI crashed.
52
+ *
53
+ * @param toolCallId - The `tool_call_id` from the `tool_call` event
54
+ * @param errorMessage - Human-readable error description
55
+ */
56
+ sendToolError(toolCallId: string, errorMessage: string): void;
57
+ /**
58
+ * Silently update the AI's context with what the user is currently doing on screen.
59
+ * Does NOT interrupt the AI's current speech.
60
+ *
61
+ * @example
62
+ * client.updateContext({ active_page: 'Checkout', item: 'Nike Shoes', price: 149.99 });
63
+ */
64
+ updateContext(context: Record<string, any>): void;
65
+ /**
66
+ * Force the AI to stop speaking and immediately react to a UI event.
67
+ * Use when the user performs a significant action (e.g., clicks a button, opens a page).
68
+ *
69
+ * @param actionName - A descriptive name for the action (e.g., 'user_clicked_cart')
70
+ * @param data - Optional context about the action
71
+ *
72
+ * @example
73
+ * client.triggerInterrupt('user_opened_checkout', { cart_value: 299 });
74
+ */
75
+ triggerInterrupt(actionName: string, data?: Record<string, any>): void;
76
+ private _setStatus;
77
+ private _inferServerUrl;
78
+ private _assertConnected;
79
+ }
80
+
81
+ export declare interface AvinAIConfig {
82
+ /** Your Ovin agent ID from the dashboard */
83
+ agentId: string;
84
+ /** Bearer token for authentication. Required for ICE server fetching. */
85
+ token?: string;
86
+ /** Override the WebRTC server URL. Auto-detected if omitted. */
87
+ serverUrl?: string;
88
+ /** Override the GraphQL endpoint for config fetching. */
89
+ graphqlEndpoint?: string;
90
+ /** Optional: pre-supply your own ICE servers (skips auto-fetch). */
91
+ iceServers?: RTCIceServer[];
92
+ /** Optional call ID override. Auto-generated if omitted. */
93
+ callId?: string;
94
+ }
95
+
96
+ /**
97
+ * AvinAI SDK
98
+ * ============
99
+ * A simple, developer-friendly Voice AI client.
100
+ * Hides all WebRTC complexity behind a clean event-emitter API.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const client = new AvinAI({ agentId: 'your-agent-id', token: 'Bearer your-token' });
105
+ * client.on('tool_call', ({ name, arguments: args, tool_call_id, execution_mode }) => {
106
+ * // render your UI component here
107
+ * if (execution_mode === 'blocking') {
108
+ * client.sendToolResult(tool_call_id, { success: true });
109
+ * }
110
+ * });
111
+ * await client.start();
112
+ * ```
113
+ */
114
+ export declare type AvinAIStatus = 'idle' | 'connecting' | 'connected' | 'disconnected' | 'error';
115
+
5
116
  export declare class AvinWidget extends HTMLElement {
6
117
  private shadow;
7
118
  private provider;
@@ -29,12 +140,27 @@ declare interface ChatMessage {
29
140
  }
30
141
 
31
142
  export declare class ChatService {
143
+ static setChatUrl(url: string): void;
32
144
  static createChatProspect(prospectGroupId: string): Promise<string>;
33
- static fetchWelcomeMessage(agentId: string, prospectId: string): Promise<ChatMessage | null>;
145
+ static fetchWelcomeMessage(agentId: string, prospectId: string): Promise<ChatMessage & {
146
+ chatId?: string;
147
+ }>;
34
148
  static sendChatMessage(agentId: string, prospectId: string, message: string, chatId: string | null, onChunk: (text: string) => void, onWidget: (widget: any) => void, onDone: (newChatId: string | null) => void): Promise<void>;
149
+ static listenForAdminReplies(inboxId: string, sender: string, onMessage: (content: string) => void): EventSource;
35
150
  static createCall(agentId: string, prospectId: string | null, widgetMode: string): Promise<string>;
36
151
  }
37
152
 
153
+ export declare interface ClientToolCall {
154
+ /** The ref_code of the triggered action */
155
+ name: string;
156
+ /** The mapped payload from client_action_fields */
157
+ arguments: Record<string, any>;
158
+ /** Unique ID for this execution. Required when sending a result back for blocking tools. */
159
+ tool_call_id: string;
160
+ /** Whether the AI is paused waiting for result ('blocking') or keeps talking ('fire_and_forget') */
161
+ execution_mode: 'blocking' | 'fire_and_forget';
162
+ }
163
+
38
164
  export declare class ConfigService {
39
165
  static fetchWidgetConfig(widgetId: string): Promise<WidgetConfig>;
40
166
  }
@@ -44,6 +170,28 @@ export declare interface ControlEvent {
44
170
  text?: string;
45
171
  isFinal?: boolean;
46
172
  name?: string;
173
+ tool_call?: any;
174
+ data?: any;
175
+ }
176
+
177
+ declare type EventCallback<T> = T extends void ? () => void : (payload: T) => void;
178
+
179
+ declare type EventMap = {
180
+ /** Fired when WebRTC connection is fully established */
181
+ connected: void;
182
+ /** Fired when the connection is lost or explicitly disconnected */
183
+ disconnected: void;
184
+ /** Fired on connection or protocol error. Payload is an error message string. */
185
+ error: string;
186
+ /** Fired whenever the AI transcribes speech (both interim and final) */
187
+ transcription: TranscriptionEvent;
188
+ /** Fired when the AI triggers a client-side tool (action) */
189
+ tool_call: ClientToolCall;
190
+ };
191
+
192
+ export declare interface TranscriptionEvent {
193
+ text: string;
194
+ isFinal: boolean;
47
195
  }
48
196
 
49
197
  export declare interface TTSProvider {
@@ -64,6 +212,7 @@ export declare class WebRTCClient {
64
212
  private onError;
65
213
  private onTranscription;
66
214
  private onRemoteTrack;
215
+ private onClientToolCall;
67
216
  private pc;
68
217
  private dataChannel;
69
218
  private audioElement;
@@ -95,6 +244,20 @@ export declare class WebRTCClient {
95
244
  * Generate unique call ID
96
245
  */
97
246
  generateCallId(): string;
247
+ /**
248
+ * Send client-side tool execution result back to the AI server.
249
+ * Use this to unblock a 'blocking' execution mode tool.
250
+ */
251
+ sendToolResult(callId: string, result: any): void;
252
+ /**
253
+ * Silently update the AI's context with what the user is currently viewing.
254
+ * Does NOT interrupt the AI's current speech.
255
+ */
256
+ sendSilentContext(context: string): void;
257
+ /**
258
+ * Trigger a client-side interrupt — cuts AI audio and forces it to react to a UI event.
259
+ */
260
+ triggerActionInterrupt(): void;
98
261
  /**
99
262
  * Static helper to fetch ICE servers from Hasura
100
263
  */
@@ -110,6 +273,7 @@ export declare interface WebRTCClientConfig {
110
273
  onError?: (error: any) => void;
111
274
  onTranscription?: (text: string, isFinal: boolean) => void;
112
275
  onRemoteTrack?: (track: MediaStreamTrack, stream: MediaStream) => void;
276
+ onClientToolCall?: (toolCall: any) => void;
113
277
  iceServers?: RTCIceServer[];
114
278
  token?: string;
115
279
  }
package/package.json CHANGED
@@ -1,7 +1,23 @@
1
1
  {
2
2
  "name": "avin-ai",
3
- "version": "0.1.10",
4
- "description": "AvinAI Voice Widget SDK",
3
+ "version": "0.2.0",
4
+ "description": "AvinAI Voice & Chat Widget SDK — embed conversational AI into any website with one script tag",
5
+ "author": "AvinAI <dev@avin.ai>",
6
+ "license": "MIT",
7
+ "homepage": "https://avin.ai",
8
+ "keywords": [
9
+ "voice-ai",
10
+ "chatbot",
11
+ "widget",
12
+ "sdk",
13
+ "webrtc",
14
+ "ai-assistant",
15
+ "avin"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/avin-ai/sdk"
20
+ },
5
21
  "type": "module",
6
22
  "main": "./dist/avin-ai.umd.js",
7
23
  "module": "./dist/avin-ai.es.js",