@anam-ai/agent-widget 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/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # @anam-ai/agent-widget
2
+
3
+ Embeddable video agent widget for [Anam AI](https://anam.ai). Add a conversational AI avatar to any website with a single HTML snippet.
4
+
5
+ ## Quick Start
6
+
7
+ Add two lines to your HTML:
8
+
9
+ ```html
10
+ <anam-agent agent-id="your-persona-id"></anam-agent>
11
+ <script src="https://unpkg.com/@anam-ai/agent-widget" async></script>
12
+ ```
13
+
14
+ The widget auto-registers the `<anam-agent>` custom element on load. No build step required.
15
+
16
+ > **Allowed origins:** Your persona must have an allowed origin configured in [Anam Lab](https://lab.anam.ai) widget settings for the domain you're embedding on, or "Allow everywhere" must be enabled. Without this, the widget will be blocked by default.
17
+
18
+ ## Installation
19
+
20
+ ### CDN (recommended for embedding)
21
+
22
+ ```html
23
+ <script src="https://unpkg.com/@anam-ai/agent-widget" async></script>
24
+ ```
25
+
26
+ Also available via jsDelivr:
27
+
28
+ ```html
29
+ <script src="https://cdn.jsdelivr.net/npm/@anam-ai/agent-widget" async></script>
30
+ ```
31
+
32
+ ### npm (for bundlers)
33
+
34
+ ```bash
35
+ npm install @anam-ai/agent-widget
36
+ ```
37
+
38
+ ```typescript
39
+ import { registerWidget } from '@anam-ai/agent-widget';
40
+
41
+ registerWidget(); // registers <anam-agent>
42
+ registerWidget('my-agent'); // or use a custom tag name
43
+ ```
44
+
45
+ ## Attributes
46
+
47
+ Configure the widget entirely via HTML attributes. All are optional except `agent-id`.
48
+
49
+ | Attribute | Type | Default | Description |
50
+ |-----------|------|---------|-------------|
51
+ | `agent-id` | string | — | Persona ID from Anam Lab. Required for streaming. |
52
+ | `session-token` | string | — | Pre-created session token. Skips the `/v1/auth/widget` call when set (used by Lab preview). |
53
+ | `api-base-url` | string | `https://api.anam.ai` | API endpoint override. |
54
+ | `layout` | `inline` \| `floating` | `floating` | Inline renders in-flow; floating overlays the page. |
55
+ | `initial-state` | `minimized` \| `expanded` | `expanded` | Floating layout only. Start as orb or expanded sheet. |
56
+ | `position` | string | `bottom-right` | Corner preset (`bottom-right`, `bottom-left`, `top-right`, `top-left`) or custom bracket syntax. |
57
+ | `ui-mute-button` | `true` \| `false` | `true` | Show microphone mute toggle. |
58
+ | `ui-text-input` | `true` \| `false` | `true` | Show text message input. |
59
+ | `ui-captions` | `true` \| `false` | `false` | Show conversation captions. |
60
+ | `call-to-action` | string | `Talk to our assistant` | Text on the start button. |
61
+ | `avatar-url` | string | — | Override avatar thumbnail image URL. |
62
+ | `avatar-video-url` | string | — | Override avatar preview video URL. |
63
+ | `agent-name` | string | — | Override persona name displayed in the CTA. |
64
+
65
+ ### Custom Positioning
66
+
67
+ Beyond the four corner presets, use bracket syntax for pixel-level control:
68
+
69
+ ```html
70
+ <anam-agent agent-id="..." position="[top-20,right-0]"></anam-agent>
71
+ <anam-agent agent-id="..." position="[bottom-100,left-40]"></anam-agent>
72
+ ```
73
+
74
+ ## Layout Modes
75
+
76
+ ### Floating (default)
77
+
78
+ Renders as a fixed-position overlay. Starts expanded or as a clickable orb depending on `initial-state`.
79
+
80
+ ```html
81
+ <anam-agent
82
+ agent-id="your-persona-id"
83
+ layout="floating"
84
+ initial-state="minimized"
85
+ position="bottom-right"
86
+ ></anam-agent>
87
+ ```
88
+
89
+ ### Inline
90
+
91
+ Renders in the normal document flow, filling its parent container.
92
+
93
+ ```html
94
+ <div style="width: 400px; height: 600px;">
95
+ <anam-agent agent-id="your-persona-id" layout="inline"></anam-agent>
96
+ </div>
97
+ ```
98
+
99
+ ## Configuration Priority
100
+
101
+ The widget merges configuration from three sources (highest priority wins):
102
+
103
+ 1. **HTML attributes** — per-instance overrides set by the embedder
104
+ 2. **Server config** — persona settings from Anam Lab (fetched via API)
105
+ 3. **Widget defaults** — built-in fallbacks
106
+
107
+ ## How It Works
108
+
109
+ 1. **On load:** The widget fetches persona metadata (avatar image, name, display config) from `GET /v1/personas/:id/widget`
110
+ 2. **On user click:** Requests a session token from `POST /v1/auth/widget`, then starts a WebRTC stream via the Anam JS SDK
111
+ 3. **During session:** Real-time video avatar with voice input, optional text input, and mute controls
112
+ 4. **On close:** Stops the stream and cleans up resources
113
+
114
+ ## Events
115
+
116
+ The widget dispatches custom DOM events on the `<anam-agent>` element:
117
+
118
+ | Event | Payload | Description |
119
+ |-------|---------|-------------|
120
+ | `anam-agent:session-started` | `{ sessionId }` | Stream connected |
121
+ | `anam-agent:session-ended` | `{ sessionId, reason }` | Stream ended |
122
+ | `anam-agent:message-received` | `{ role, content }` | Message from user or assistant |
123
+ | `anam-agent:message-sent` | `{ content }` | Text message sent |
124
+ | `anam-agent:expanded` | `{}` | Widget expanded from orb |
125
+ | `anam-agent:collapsed` | `{}` | Widget collapsed to orb |
126
+ | `anam-agent:error` | `{ code, message }` | Error occurred |
127
+ | `anam-agent:mic-muted` | `{}` | Microphone muted |
128
+ | `anam-agent:mic-unmuted` | `{}` | Microphone unmuted |
129
+
130
+ ```javascript
131
+ document.querySelector('anam-agent').addEventListener('anam-agent:session-started', (e) => {
132
+ console.log('Session started:', e.detail.sessionId);
133
+ });
134
+ ```
135
+
136
+ ## Development
137
+
138
+ ### Prerequisites
139
+
140
+ - Node.js 18+
141
+ - pnpm 10+
142
+
143
+ ### Setup
144
+
145
+ ```bash
146
+ pnpm install
147
+ ```
148
+
149
+ ### Dev Server
150
+
151
+ ```bash
152
+ pnpm dev
153
+ ```
154
+
155
+ Opens the playground at `http://localhost:5173/playground/` with HMR. The playground provides a sidebar with controls for all widget attributes and a live preview area.
156
+
157
+ Set `VITE_ANAM_AGENT_ID` in `.env` to pre-fill the agent ID input.
158
+
159
+ ### Build
160
+
161
+ ```bash
162
+ pnpm build
163
+ ```
164
+
165
+ Outputs dual-format bundles to `dist/`:
166
+ - `dist/index.js` — ESM (for bundlers)
167
+ - `dist/index.umd.js` — UMD (for script tags)
168
+
169
+ All dependencies are bundled into the output (zero runtime externals).
170
+
171
+ ### Preview Production Build
172
+
173
+ ```bash
174
+ pnpm preview
175
+ ```
176
+
177
+ Builds and serves at `http://localhost:4173/playground/preview.html` using the UMD bundle.
178
+
179
+ ### Type Check
180
+
181
+ ```bash
182
+ pnpm typecheck
183
+ ```
184
+
185
+ ### Lint
186
+
187
+ ```bash
188
+ pnpm lint
189
+ ```
190
+
191
+ ## Architecture
192
+
193
+ - **Preact** — lightweight UI rendering (~4KB)
194
+ - **Preact Signals** — fine-grained reactive state for session lifecycle
195
+ - **Shadow DOM** — full CSS isolation from host page styles
196
+ - **Web Components** — `<anam-agent>` custom element via `preact-custom-element`
197
+ - **Tailwind CSS** — compiled and inlined into the Shadow DOM (no external stylesheets)
198
+ - **Vite** — library-mode build with esbuild minification
199
+
200
+ ## Browser Support
201
+
202
+ Modern browsers with Web Components and WebRTC support:
203
+ - Chrome / Edge 80+
204
+ - Firefox 63+
205
+ - Safari 13.1+