anva-sdk 0.1.0 → 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/index.js +45 -22
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* import { Anva } from "anva";
|
|
5
5
|
* const client = new Anva(process.env.ANVA_KEY);
|
|
6
|
-
* const session = await client.createSession({
|
|
6
|
+
* const session = await client.createSession({ presetId: "..." });
|
|
7
7
|
* // put session.embed_url in an <iframe allow="camera; microphone; autoplay">
|
|
8
8
|
*
|
|
9
9
|
* Works in Node 18+ (global fetch/WebSocket) and modern browsers — but keep
|
|
@@ -36,43 +36,60 @@ export class Anva {
|
|
|
36
36
|
// -- sessions -------------------------------------------------------------
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* Create a live session
|
|
40
|
-
*
|
|
39
|
+
* Create a live session, one of two ways:
|
|
40
|
+
* - Embed tier: pass `presetId` (a saved preset from the Playground).
|
|
41
|
+
* - Advanced tier: pass `avatarId` + the persona (`systemPrompt`, `voiceId`,
|
|
42
|
+
* `languageCode`) directly — nothing is stored server-side.
|
|
43
|
+
* Returns { session_id, session_token, embed_url, events_ws_url, expires_at,
|
|
44
|
+
* instance_id, preset_id, avatar_id, ... }.
|
|
45
|
+
*
|
|
46
|
+
* @param {{presetId?: string, avatarId?: string, systemPrompt?: string,
|
|
47
|
+
* voiceId?: string, languageCode?: string, llmMode?: string,
|
|
48
|
+
* webhookUrl?: string, webhookSecret?: string}} params
|
|
41
49
|
*/
|
|
42
|
-
createSession(
|
|
43
|
-
const
|
|
50
|
+
createSession(params = {}) {
|
|
51
|
+
const { presetId, avatarId, systemPrompt, voiceId, languageCode, llmMode, webhookUrl, webhookSecret } = params;
|
|
52
|
+
if (!presetId && !avatarId) {
|
|
53
|
+
throw new Error("createSession requires either presetId (embed) or avatarId (advanced persona)");
|
|
54
|
+
}
|
|
55
|
+
const body = {};
|
|
56
|
+
if (presetId) body.preset_id = presetId;
|
|
57
|
+
if (avatarId) body.avatar_id = avatarId;
|
|
58
|
+
if (systemPrompt) body.system_prompt = systemPrompt;
|
|
59
|
+
if (voiceId) body.voice_id = voiceId;
|
|
60
|
+
if (languageCode) body.language_code = languageCode;
|
|
44
61
|
if (llmMode) body.llm_mode = llmMode;
|
|
45
62
|
if (webhookUrl) body.webhook_url = webhookUrl;
|
|
46
63
|
if (webhookSecret) body.webhook_secret = webhookSecret;
|
|
47
|
-
return this._request("POST", "/api/
|
|
64
|
+
return this._request("POST", "/api/v2/sessions", body);
|
|
48
65
|
}
|
|
49
66
|
|
|
50
67
|
getSession(sessionId) {
|
|
51
|
-
return this._request("GET", `/api/
|
|
68
|
+
return this._request("GET", `/api/v2/sessions/${enc(sessionId)}`);
|
|
52
69
|
}
|
|
53
70
|
|
|
54
71
|
endSession(sessionId) {
|
|
55
|
-
return this._request("DELETE", `/api/
|
|
72
|
+
return this._request("DELETE", `/api/v2/sessions/${enc(sessionId)}`);
|
|
56
73
|
}
|
|
57
74
|
|
|
58
75
|
/** Have the avatar speak `text` to the user. */
|
|
59
76
|
sendMessage(sessionId, text) {
|
|
60
|
-
return this._request("POST", `/api/
|
|
77
|
+
return this._request("POST", `/api/v2/sessions/${enc(sessionId)}/messages`, { text });
|
|
61
78
|
}
|
|
62
79
|
|
|
63
80
|
/** Stop the avatar mid-sentence. */
|
|
64
81
|
interrupt(sessionId) {
|
|
65
|
-
return this._request("POST", `/api/
|
|
82
|
+
return this._request("POST", `/api/v2/sessions/${enc(sessionId)}/interrupt`, {});
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
triggerAction(sessionId, name) {
|
|
69
|
-
return this._request("POST", `/api/
|
|
86
|
+
return this._request("POST", `/api/v2/sessions/${enc(sessionId)}/actions`, { name });
|
|
70
87
|
}
|
|
71
88
|
|
|
72
89
|
/** The authenticated WebSocket URL for the session's event stream. */
|
|
73
90
|
eventsUrl(sessionId) {
|
|
74
91
|
const ws = this.baseUrl.replace(/^http/, "ws");
|
|
75
|
-
return `${ws}/api/
|
|
92
|
+
return `${ws}/api/v2/sessions/${enc(sessionId)}/events?api_key=${encodeURIComponent(this.apiKey)}`;
|
|
76
93
|
}
|
|
77
94
|
|
|
78
95
|
/**
|
|
@@ -124,27 +141,33 @@ export class Anva {
|
|
|
124
141
|
}
|
|
125
142
|
}
|
|
126
143
|
|
|
127
|
-
// --
|
|
144
|
+
// -- presets --------------------------------------------------------------
|
|
128
145
|
|
|
129
|
-
|
|
130
|
-
return this._request("GET", "/api/
|
|
146
|
+
listPresets() {
|
|
147
|
+
return this._request("GET", "/api/v2/presets");
|
|
131
148
|
}
|
|
132
149
|
|
|
133
|
-
|
|
150
|
+
createPreset({ name, visualCharacterId, systemPrompt, voiceId, languageCode }) {
|
|
134
151
|
const body = { name };
|
|
135
152
|
if (visualCharacterId) body.visual_character_id = visualCharacterId;
|
|
136
153
|
if (systemPrompt) body.system_prompt = systemPrompt;
|
|
137
154
|
if (voiceId) body.voice_id = voiceId;
|
|
138
155
|
if (languageCode) body.language_code = languageCode;
|
|
139
|
-
return this._request("POST", "/api/
|
|
156
|
+
return this._request("POST", "/api/v2/presets", body);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getPreset(presetId) {
|
|
160
|
+
return this._request("GET", `/api/v2/presets/${enc(presetId)}`);
|
|
140
161
|
}
|
|
141
162
|
|
|
142
|
-
|
|
143
|
-
return this._request("
|
|
163
|
+
deletePreset(presetId) {
|
|
164
|
+
return this._request("DELETE", `/api/v2/presets/${enc(presetId)}`);
|
|
144
165
|
}
|
|
145
166
|
|
|
146
|
-
|
|
147
|
-
|
|
167
|
+
// -- instances ------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
listInstances() {
|
|
170
|
+
return this._request("GET", "/api/v2/instances");
|
|
148
171
|
}
|
|
149
172
|
|
|
150
173
|
// -- plumbing -------------------------------------------------------------
|
|
@@ -155,7 +178,7 @@ export class Anva {
|
|
|
155
178
|
headers: {
|
|
156
179
|
Authorization: `Bearer ${this.apiKey}`,
|
|
157
180
|
"Content-Type": "application/json",
|
|
158
|
-
"User-Agent": "anva-js/0.
|
|
181
|
+
"User-Agent": "anva-js/0.2.0",
|
|
159
182
|
},
|
|
160
183
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
161
184
|
});
|