@craftedxp/voice-js 0.5.4 → 0.6.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/browser.d.mts +65 -1
- package/dist/browser.d.ts +467 -502
- package/dist/browser.js +921 -836
- package/dist/browser.js.map +1 -1
- package/dist/browser.mjs +93 -1
- package/dist/browser.mjs.map +1 -1
- package/dist/embed.iife.js +67 -23358
- package/dist/node.d.mts +63 -0
- package/dist/node.d.ts +462 -483
- package/dist/node.js +453 -467
- package/dist/node.js.map +1 -1
- package/dist/node.mjs.map +1 -1
- package/package.json +2 -1
package/dist/browser.mjs
CHANGED
|
@@ -1190,6 +1190,90 @@ var joinRoom = async (opts) => {
|
|
|
1190
1190
|
};
|
|
1191
1191
|
};
|
|
1192
1192
|
|
|
1193
|
+
// src/textSession.ts
|
|
1194
|
+
async function startTextSession(opts) {
|
|
1195
|
+
const f = opts.fetch ?? fetch;
|
|
1196
|
+
const tokenQs = `?token=${encodeURIComponent(opts.token)}`;
|
|
1197
|
+
const startUrl = `${opts.baseUrl}/v1/agents/${opts.agentId}/chat${tokenQs}`;
|
|
1198
|
+
const startBody = opts.text ? JSON.stringify({ text: opts.text }) : "{}";
|
|
1199
|
+
const res = await f(startUrl, {
|
|
1200
|
+
method: "POST",
|
|
1201
|
+
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
1202
|
+
body: startBody
|
|
1203
|
+
});
|
|
1204
|
+
if (!res.ok || !res.body) {
|
|
1205
|
+
const text = await res.text().catch(() => "");
|
|
1206
|
+
throw new Error(`startTextSession failed: ${res.status} ${text}`);
|
|
1207
|
+
}
|
|
1208
|
+
const iter = parseSse(res.body);
|
|
1209
|
+
let chatId = "";
|
|
1210
|
+
let callId = "";
|
|
1211
|
+
const buffered = [];
|
|
1212
|
+
const it = iter[Symbol.asyncIterator]();
|
|
1213
|
+
while (true) {
|
|
1214
|
+
const { value, done } = await it.next();
|
|
1215
|
+
if (done) break;
|
|
1216
|
+
if (value.type === "chat.started") {
|
|
1217
|
+
chatId = value.chatId;
|
|
1218
|
+
callId = value.callId;
|
|
1219
|
+
break;
|
|
1220
|
+
}
|
|
1221
|
+
buffered.push(value);
|
|
1222
|
+
}
|
|
1223
|
+
return {
|
|
1224
|
+
id: chatId,
|
|
1225
|
+
callId,
|
|
1226
|
+
greeting: replayThen(buffered, { [Symbol.asyncIterator]: () => it }),
|
|
1227
|
+
async send(text) {
|
|
1228
|
+
const r = await f(`${opts.baseUrl}/v1/chats/${chatId}/messages${tokenQs}`, {
|
|
1229
|
+
method: "POST",
|
|
1230
|
+
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
1231
|
+
body: JSON.stringify({ text })
|
|
1232
|
+
});
|
|
1233
|
+
if (!r.ok || !r.body) {
|
|
1234
|
+
const errText = await r.text().catch(() => "");
|
|
1235
|
+
throw new Error(`send failed: ${r.status} ${errText}`);
|
|
1236
|
+
}
|
|
1237
|
+
return parseSse(r.body);
|
|
1238
|
+
},
|
|
1239
|
+
async end() {
|
|
1240
|
+
await f(`${opts.baseUrl}/v1/calls/${callId}`, { method: "DELETE" });
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
async function* parseSse(body) {
|
|
1245
|
+
const reader = body.getReader();
|
|
1246
|
+
const decoder = new TextDecoder();
|
|
1247
|
+
let buf = "";
|
|
1248
|
+
while (true) {
|
|
1249
|
+
const { value, done } = await reader.read();
|
|
1250
|
+
if (done) return;
|
|
1251
|
+
buf += decoder.decode(value, { stream: true });
|
|
1252
|
+
let idx;
|
|
1253
|
+
while ((idx = buf.indexOf("\n\n")) >= 0) {
|
|
1254
|
+
const chunk = buf.slice(0, idx);
|
|
1255
|
+
buf = buf.slice(idx + 2);
|
|
1256
|
+
let event = "message";
|
|
1257
|
+
let data = "";
|
|
1258
|
+
for (const line of chunk.split("\n")) {
|
|
1259
|
+
if (line.startsWith(":")) continue;
|
|
1260
|
+
if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
1261
|
+
else if (line.startsWith("data:")) data += line.slice(5).trim();
|
|
1262
|
+
}
|
|
1263
|
+
if (!data) continue;
|
|
1264
|
+
try {
|
|
1265
|
+
const parsed = JSON.parse(data);
|
|
1266
|
+
yield { type: event, ...parsed };
|
|
1267
|
+
} catch {
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
async function* replayThen(buffered, rest) {
|
|
1273
|
+
for (const x of buffered) yield x;
|
|
1274
|
+
for await (const x of rest) yield x;
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1193
1277
|
// src/incomingCall.ts
|
|
1194
1278
|
var parseIncomingCall = (raw) => {
|
|
1195
1279
|
if (typeof raw !== "object" || raw === null) {
|
|
@@ -1278,6 +1362,13 @@ var BrowserVoiceFactory = class {
|
|
|
1278
1362
|
// endpoint — the opaque code is the only credential), then connects
|
|
1279
1363
|
// to LiveKit and returns a typed event surface.
|
|
1280
1364
|
this.joinRoom = (opts) => joinRoom({ apiBase: this.config.apiBase, ...opts });
|
|
1365
|
+
// Text-channel chat session (no microphone / audio).
|
|
1366
|
+
// Mint a `ct_` token with `channel: 'text'` on your backend, then call
|
|
1367
|
+
// this to open an SSE stream against the chat API.
|
|
1368
|
+
this.startTextSession = (opts) => startTextSession({
|
|
1369
|
+
...opts,
|
|
1370
|
+
baseUrl: this.config.apiBase
|
|
1371
|
+
});
|
|
1281
1372
|
this.config = config;
|
|
1282
1373
|
}
|
|
1283
1374
|
};
|
|
@@ -1293,6 +1384,7 @@ export {
|
|
|
1293
1384
|
createReconnectingWebSocket,
|
|
1294
1385
|
handleServerMessage,
|
|
1295
1386
|
joinRoom,
|
|
1296
|
-
parseIncomingCall
|
|
1387
|
+
parseIncomingCall,
|
|
1388
|
+
startTextSession
|
|
1297
1389
|
};
|
|
1298
1390
|
//# sourceMappingURL=browser.mjs.map
|