@trillet-ai/web-sdk 1.7.1 → 1.9.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 CHANGED
@@ -20,6 +20,38 @@ npm install @trillet-ai/web-sdk
20
20
  yarn add @trillet-ai/web-sdk
21
21
  ```
22
22
 
23
+ ## Secure call flow (recommended)
24
+
25
+ Keep your API key on your server. Mint the room on your backend with `createCall`
26
+ (from the Node-safe `@trillet-ai/web-sdk/server` entry point), return the
27
+ `connection` object to the browser, and join with `joinRoom` — the API key, workspace
28
+ ID, and agent ID never reach the browser.
29
+
30
+ ```typescript
31
+ // --- Backend (Node, API key stays here) ---
32
+ import { createCall } from '@trillet-ai/web-sdk/server';
33
+
34
+ const roomDetails = await createCall({
35
+ apiKey: process.env.TRILLET_API_KEY!, // server-side only — never ship to the browser
36
+ agentId: 'your-agent-id',
37
+ mode: 'voice',
38
+ });
39
+ // Send roomDetails (or just roomDetails.connection) to your frontend over HTTPS.
40
+ ```
41
+
42
+ ```typescript
43
+ // --- Frontend (browser, no secret) ---
44
+ import { TrilletAgent } from '@trillet-ai/web-sdk';
45
+
46
+ const agent = new TrilletAgent({ agentId: 'your-agent-id', mode: 'voice' });
47
+
48
+ // roomDetails fetched from your backend above
49
+ await agent.joinRoom(roomDetails); // connects to LiveKit; makes no Trillet API call
50
+ ```
51
+
52
+ > `startCall()` and `createCall()` are **server-side only** — they require an API key
53
+ > and must not be called from the browser.
54
+
23
55
  ## Quick Start
24
56
 
25
57
  ```typescript
@@ -116,6 +148,34 @@ The SDK supports two types of calls:
116
148
  await agent.startPublicCall();
117
149
  ```
118
150
 
151
+ ## Gating a call (`onBeforeCall`)
152
+
153
+ `onBeforeCall` is awaited once at the top of every call-start path — `startCall()`,
154
+ `startPublicCall()` and `joinRoom()` — before any network request or LiveKit connection.
155
+ Throw to abort: nothing is minted and nothing connects.
156
+
157
+ ```typescript
158
+ const agent = new TrilletAgent({
159
+ agentId: 'your-agent-id',
160
+ workspaceId: 'your-workspace-id',
161
+ onBeforeCall: async () => {
162
+ // Your own challenge, login check, or quota lookup.
163
+ const ok = await fetch('/api/verify-human').then((r) => r.json());
164
+ if (!ok.allowed) throw new Error('Verification failed');
165
+ },
166
+ });
167
+
168
+ await agent.startPublicCall(); // never runs if the hook throws
169
+ ```
170
+
171
+ This is the right place for a challenge you actually verify on your own backend. (The
172
+ `<trillet-widget>` `recaptcha-site-key` attribute is deliberately *not* that — its token
173
+ is never verified, so it is friction only.)
174
+
175
+ Note that a public call is additionally subject to server-side controls you configure in
176
+ the portal: your workspace's domain allowlist (Settings → Domains) and rate limits on the
177
+ public endpoint.
178
+
119
179
  ## Events
120
180
 
121
181
  The SDK emits various events that you can listen to:
@@ -0,0 +1,15 @@
1
+ interface ToolUsed {
2
+ name: string;
3
+ args?: any;
4
+ status?: 'pending' | 'success' | 'error';
5
+ }
6
+ interface Transcript {
7
+ role: 'user' | 'assistant';
8
+ text: string;
9
+ isFinal: boolean;
10
+ timestamp: Date;
11
+ participantId?: string;
12
+ toolUsed?: ToolUsed;
13
+ }
14
+
15
+ export type { Transcript as T };
@@ -0,0 +1,46 @@
1
+ // src/createCall.ts
2
+ var TRILLET_API_URL = {
3
+ CONNECT: "https://api.trillet.ai/v1/api/web-call",
4
+ PUBLIC_CONNECT: "https://api.trillet.ai/v1/api/web-call/public"
5
+ };
6
+ async function createCall(config) {
7
+ var _a, _b;
8
+ const response = await fetch(TRILLET_API_URL.CONNECT, {
9
+ method: "POST",
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ Accept: "application/json",
13
+ "X-API-KEY": config.apiKey
14
+ },
15
+ body: JSON.stringify({
16
+ call_agent_id: config.agentId,
17
+ mode: config.mode,
18
+ dynamic_variables: config.variables || {},
19
+ callback_url: config.callbackUrl,
20
+ // browser_fingerprint is a browser-only concern; omitted here so this runs
21
+ // cleanly server-side. The backend degrades to IP-based tracking.
22
+ browser_fingerprint: null
23
+ })
24
+ });
25
+ if (!response.ok) {
26
+ let errorMessage;
27
+ try {
28
+ const errorData = await response.json();
29
+ errorMessage = errorData.message || errorData.error || "Unknown error";
30
+ } catch (e) {
31
+ errorMessage = await response.text();
32
+ }
33
+ throw new Error(`API Error: ${errorMessage}`);
34
+ }
35
+ const data = await response.json();
36
+ if (!((_a = data.connection) == null ? void 0 : _a.url) || !((_b = data.connection) == null ? void 0 : _b.token)) {
37
+ throw new Error("Invalid response from server: missing connection url or token");
38
+ }
39
+ return data;
40
+ }
41
+
42
+ export {
43
+ TRILLET_API_URL,
44
+ createCall
45
+ };
46
+ //# sourceMappingURL=chunk-3MH6DJZ6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/createCall.ts"],"sourcesContent":["// Shared, browser-agnostic call-creation logic.\n//\n// This module performs ONLY the authenticated `POST /v1/api/web-call` fetch and\n// returns the room details keyed off the `connection` object that the backend\n// returns. It deliberately contains no `window`/DOM access and no secure-context\n// guard, so it can run in Node (the server entry point) as well as in the browser\n// (the `TrilletAgent.startCall` internals). Keeping this here means index.tsx and\n// server.ts share one request/response implementation and cannot drift.\n\nexport const TRILLET_API_URL = {\n CONNECT: 'https://api.trillet.ai/v1/api/web-call',\n PUBLIC_CONNECT: 'https://api.trillet.ai/v1/api/web-call/public',\n};\n\n/**\n * Room connection details returned by the Trillet web-call endpoints and consumed\n * by `TrilletAgent.joinRoom`. The `connection` object is the stable contract that\n * both the authenticated and public endpoints return.\n */\nexport interface TrilletRoomDetails {\n connection: { url: string; token: string };\n call_id?: string;\n room_name?: string;\n status?: string;\n /**\n * Human-facing identity of who the caller is talking to. Returned by both the\n * authenticated and public web-call endpoints. All three keys are always present;\n * any individual value may be `null`. Consumed by the widget header.\n */\n display?: {\n agent_name?: string | null;\n call_flow_name?: string | null;\n workspace_name?: string | null;\n };\n}\n\nexport interface CreateCallConfig {\n apiKey: string;\n agentId: string;\n mode?: 'voice' | 'text';\n variables?: Record<string, any>;\n callbackUrl?: string;\n}\n\n/**\n * **Server-side only — requires an API key, do not call from the browser.**\n *\n * Performs the authenticated `POST /v1/api/web-call` request with the `X-API-KEY`\n * header and returns the room {@link TrilletRoomDetails}. Makes no LiveKit\n * connection, requests no microphone, and touches no browser APIs — safe to run\n * in Node on the customer's backend. Pass the returned `connection` object to the\n * browser and call `agent.joinRoom(roomDetails)` there.\n */\nexport async function createCall(config: CreateCallConfig): Promise<TrilletRoomDetails> {\n const response = await fetch(TRILLET_API_URL.CONNECT, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'X-API-KEY': config.apiKey,\n },\n body: JSON.stringify({\n call_agent_id: config.agentId,\n mode: config.mode,\n dynamic_variables: config.variables || {},\n callback_url: config.callbackUrl,\n // browser_fingerprint is a browser-only concern; omitted here so this runs\n // cleanly server-side. The backend degrades to IP-based tracking.\n browser_fingerprint: null,\n }),\n });\n\n if (!response.ok) {\n let errorMessage;\n try {\n const errorData = await response.json();\n errorMessage = errorData.message || errorData.error || 'Unknown error';\n } catch {\n errorMessage = await response.text();\n }\n throw new Error(`API Error: ${errorMessage}`);\n }\n\n const data: TrilletRoomDetails = await response.json();\n\n if (!data.connection?.url || !data.connection?.token) {\n throw new Error('Invalid response from server: missing connection url or token');\n }\n\n return data;\n}\n"],"mappings":";AASO,IAAM,kBAAkB;AAAA,EAC7B,SAAS;AAAA,EACT,gBAAgB;AAClB;AAyCA,eAAsB,WAAW,QAAuD;AArDxF;AAsDE,QAAM,WAAW,MAAM,MAAM,gBAAgB,SAAS;AAAA,IACpD,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,QAAQ;AAAA,MACR,aAAa,OAAO;AAAA,IACtB;AAAA,IACA,MAAM,KAAK,UAAU;AAAA,MACnB,eAAe,OAAO;AAAA,MACtB,MAAM,OAAO;AAAA,MACb,mBAAmB,OAAO,aAAa,CAAC;AAAA,MACxC,cAAc,OAAO;AAAA;AAAA;AAAA,MAGrB,qBAAqB;AAAA,IACvB,CAAC;AAAA,EACH,CAAC;AAED,MAAI,CAAC,SAAS,IAAI;AAChB,QAAI;AACJ,QAAI;AACF,YAAM,YAAY,MAAM,SAAS,KAAK;AACtC,qBAAe,UAAU,WAAW,UAAU,SAAS;AAAA,IACzD,SAAQ;AACN,qBAAe,MAAM,SAAS,KAAK;AAAA,IACrC;AACA,UAAM,IAAI,MAAM,cAAc,YAAY,EAAE;AAAA,EAC9C;AAEA,QAAM,OAA2B,MAAM,SAAS,KAAK;AAErD,MAAI,GAAC,UAAK,eAAL,mBAAiB,QAAO,GAAC,UAAK,eAAL,mBAAiB,QAAO;AACpD,UAAM,IAAI,MAAM,+DAA+D;AAAA,EACjF;AAEA,SAAO;AACT;","names":[]}