@voicethere/agent 0.18.2 → 0.20.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/README.md +32 -0
- package/dist/agent.js +89 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
- package/dist/llm/agent/create-llm-agent.js +44 -25
- package/dist/llm/agent/create-llm-agent.js.map +1 -1
- package/dist/protocol.d.ts +58 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +48 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +178 -0
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +85 -3
- package/dist/templates/echo-dc/agent.js +85 -3
- package/dist/templates/game-sync/agent.js +85 -3
- package/dist/templates/language-switch/agent.js +85 -3
- package/dist/templates/llm-agent/agent.js +122 -29
- package/dist/templates/llm-mcp-service/agent.js +122 -29
- package/dist/templates/positional-tts/agent.js +85 -3
- package/dist/templates/recording-consent/agent.js +85 -3
- package/dist/templates/spatial-showcase/agent.js +85 -3
- package/dist/templates/voice-showcase/agent.js +85 -3
- package/dist/templates/voice-starter/agent.js +85 -3
- package/dist/templates/webhooks/agent.js +85 -3
- package/dist/templates/webhooks-redis/agent.js +85 -3
- package/dist/templates/world-sync/agent.js +85 -3
- package/dist/templates/world-sync-binary/agent.js +85 -3
- package/eject/llm/loop.ts +63 -26
- package/package.json +1 -1
- package/templates/llm-agent/README.md +5 -1
- package/templates/llm-agent/voicethere.llm.json +1 -1
- package/templates/llm-mcp-service/README.md +1 -1
- package/templates/llm-mcp-service/voicethere.llm.json +1 -1
package/eject/llm/loop.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copied from @voicethere/agent 0.
|
|
1
|
+
// Copied from @voicethere/agent 0.20.0. Package updates no longer change this file.
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import {
|
|
4
4
|
type AgentHandlers,
|
|
@@ -96,6 +96,8 @@ interface TurnState {
|
|
|
96
96
|
controller: AbortController;
|
|
97
97
|
startedAt: number;
|
|
98
98
|
spoken: string[];
|
|
99
|
+
/** Utterance id of the caller final this voice turn answers. */
|
|
100
|
+
inReplyTo?: string;
|
|
99
101
|
toolCalls: TurnSummary["toolCalls"];
|
|
100
102
|
llmCalls: number;
|
|
101
103
|
firstTokenMs?: number;
|
|
@@ -137,7 +139,7 @@ interface SessionState {
|
|
|
137
139
|
sessionId: string;
|
|
138
140
|
history: ConversationHistory;
|
|
139
141
|
active?: ActiveTurn;
|
|
140
|
-
queued?: { text: string; source: Source };
|
|
142
|
+
queued?: { text: string; source: Source; utteranceId?: string };
|
|
141
143
|
announcing: number;
|
|
142
144
|
lastFillerPhrase?: string;
|
|
143
145
|
chain: Promise<void>;
|
|
@@ -281,6 +283,31 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
281
283
|
);
|
|
282
284
|
}
|
|
283
285
|
|
|
286
|
+
function replyOpts(t: TurnState): { turnId: string; inReplyTo?: string } {
|
|
287
|
+
return t.inReplyTo
|
|
288
|
+
? { turnId: t.turnId, inReplyTo: t.inReplyTo }
|
|
289
|
+
: { turnId: t.turnId };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The one place where reply text reaches the client as `assistant_delta`.
|
|
294
|
+
* Chat deltas are unchanged; voice turns add `spoken: true`.
|
|
295
|
+
*/
|
|
296
|
+
function sendReplyDelta(s: SessionState, t: TurnState, text: string): void {
|
|
297
|
+
sendToClient(
|
|
298
|
+
s.sessionId,
|
|
299
|
+
t.source === "chat"
|
|
300
|
+
? { type: "assistant_delta", turnId: t.turnId, text }
|
|
301
|
+
: {
|
|
302
|
+
type: "assistant_delta",
|
|
303
|
+
turnId: t.turnId,
|
|
304
|
+
text,
|
|
305
|
+
spoken: true,
|
|
306
|
+
...(t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}),
|
|
307
|
+
},
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
284
311
|
/** The one place where a sentence of a reply leaves the agent. */
|
|
285
312
|
function deliver(
|
|
286
313
|
s: SessionState,
|
|
@@ -291,18 +318,12 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
291
318
|
flushSpeech(s, t);
|
|
292
319
|
t.spoken.push(text);
|
|
293
320
|
markReplyStarted(s, t);
|
|
294
|
-
|
|
295
|
-
sendToClient(s.sessionId, {
|
|
296
|
-
type: "assistant_delta",
|
|
297
|
-
turnId: t.turnId,
|
|
298
|
-
text,
|
|
299
|
-
});
|
|
300
|
-
}
|
|
321
|
+
sendReplyDelta(s, t, text);
|
|
301
322
|
if (t.source === "voice" || speakChat) {
|
|
302
323
|
if (interruptible) {
|
|
303
|
-
speak(s.sessionId, text,
|
|
324
|
+
speak(s.sessionId, text, replyOpts(t));
|
|
304
325
|
} else {
|
|
305
|
-
const h = announce(s.sessionId, text,
|
|
326
|
+
const h = announce(s.sessionId, text, replyOpts(t));
|
|
306
327
|
trackAnnouncement(s, h.done);
|
|
307
328
|
}
|
|
308
329
|
}
|
|
@@ -422,7 +443,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
422
443
|
t.pending = [];
|
|
423
444
|
t.pendingChars = 0;
|
|
424
445
|
if (t.controller.signal.aborted || s.ended) return;
|
|
425
|
-
speak(s.sessionId, text,
|
|
446
|
+
speak(s.sessionId, text, replyOpts(t));
|
|
426
447
|
}
|
|
427
448
|
|
|
428
449
|
/** A sentence of the model's reply: chat delta now, speech coalesced. */
|
|
@@ -433,13 +454,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
433
454
|
}
|
|
434
455
|
markReplyStarted(s, t);
|
|
435
456
|
t.spoken.push(text);
|
|
436
|
-
|
|
437
|
-
sendToClient(s.sessionId, {
|
|
438
|
-
type: "assistant_delta",
|
|
439
|
-
turnId: t.turnId,
|
|
440
|
-
text,
|
|
441
|
-
});
|
|
442
|
-
}
|
|
457
|
+
sendReplyDelta(s, t, text);
|
|
443
458
|
if (t.source !== "voice" && !speakChat) return;
|
|
444
459
|
t.pending.push(text);
|
|
445
460
|
t.pendingChars += text.length;
|
|
@@ -1095,6 +1110,16 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1095
1110
|
});
|
|
1096
1111
|
sendToClient(s.sessionId, { type: "chat_reply", text });
|
|
1097
1112
|
}
|
|
1113
|
+
if (t.source === "voice" && t.spoken.length > 0) {
|
|
1114
|
+
sendToClient(s.sessionId, {
|
|
1115
|
+
type: "assistant_done",
|
|
1116
|
+
turnId: t.turnId,
|
|
1117
|
+
text: t.spoken.join(" "),
|
|
1118
|
+
spoken: true,
|
|
1119
|
+
status: summary.status,
|
|
1120
|
+
...(t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}),
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1098
1123
|
setAgentState(s.sessionId, "listening");
|
|
1099
1124
|
if (hooks.onTurnEnd) {
|
|
1100
1125
|
await callHook("onTurnEnd", s.sessionId, () =>
|
|
@@ -1118,7 +1143,12 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1118
1143
|
);
|
|
1119
1144
|
}
|
|
1120
1145
|
|
|
1121
|
-
function startTurn(
|
|
1146
|
+
function startTurn(
|
|
1147
|
+
s: SessionState,
|
|
1148
|
+
text: string,
|
|
1149
|
+
source: Source,
|
|
1150
|
+
utteranceId?: string,
|
|
1151
|
+
): void {
|
|
1122
1152
|
const turnId = randomUUID();
|
|
1123
1153
|
const controller = new AbortController();
|
|
1124
1154
|
const t: TurnState = {
|
|
@@ -1128,6 +1158,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1128
1158
|
controller,
|
|
1129
1159
|
startedAt: now(),
|
|
1130
1160
|
spoken: [],
|
|
1161
|
+
...(source === "voice" && utteranceId ? { inReplyTo: utteranceId } : {}),
|
|
1131
1162
|
toolCalls: [],
|
|
1132
1163
|
llmCalls: 0,
|
|
1133
1164
|
roundOpen: false,
|
|
@@ -1175,7 +1206,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1175
1206
|
const q = s.queued;
|
|
1176
1207
|
if (q && !s.ended && !s.active) {
|
|
1177
1208
|
s.queued = undefined;
|
|
1178
|
-
startTurn(s, q.text, q.source);
|
|
1209
|
+
startTurn(s, q.text, q.source, q.utteranceId);
|
|
1179
1210
|
}
|
|
1180
1211
|
}
|
|
1181
1212
|
})();
|
|
@@ -1185,6 +1216,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1185
1216
|
s: SessionState,
|
|
1186
1217
|
text: string,
|
|
1187
1218
|
source: Source,
|
|
1219
|
+
utteranceId?: string,
|
|
1188
1220
|
): Promise<void> {
|
|
1189
1221
|
if (s.ended) return;
|
|
1190
1222
|
if (
|
|
@@ -1219,7 +1251,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1219
1251
|
const active = s.active;
|
|
1220
1252
|
if (active) {
|
|
1221
1253
|
if (policy === "finish") {
|
|
1222
|
-
s.queued = { text, source };
|
|
1254
|
+
s.queued = { text, source, utteranceId };
|
|
1223
1255
|
return;
|
|
1224
1256
|
}
|
|
1225
1257
|
cancelSpeech(s.sessionId, { turnId: active.turnId });
|
|
@@ -1237,16 +1269,21 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1237
1269
|
if (timer) clearTimeout(timer);
|
|
1238
1270
|
if (s.ended) return;
|
|
1239
1271
|
}
|
|
1240
|
-
startTurn(s, text, source);
|
|
1272
|
+
startTurn(s, text, source, utteranceId);
|
|
1241
1273
|
}
|
|
1242
1274
|
|
|
1243
1275
|
/** Never awaits the LLM: the session's serial queue must stay free. */
|
|
1244
|
-
function submit(
|
|
1276
|
+
function submit(
|
|
1277
|
+
sessionId: string,
|
|
1278
|
+
text: string,
|
|
1279
|
+
source: Source,
|
|
1280
|
+
utteranceId?: string,
|
|
1281
|
+
): void {
|
|
1245
1282
|
const s = sessions.get(sessionId);
|
|
1246
1283
|
const trimmed = text.trim();
|
|
1247
1284
|
if (!s || !trimmed) return;
|
|
1248
1285
|
s.chain = s.chain
|
|
1249
|
-
.then(() => handleInput(s, trimmed, source))
|
|
1286
|
+
.then(() => handleInput(s, trimmed, source, utteranceId))
|
|
1250
1287
|
.catch((err) => {
|
|
1251
1288
|
const message = err instanceof Error ? err.message : String(err);
|
|
1252
1289
|
agentLog("error", `llm input failed: ${message}`, sessionId);
|
|
@@ -1312,7 +1349,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
1312
1349
|
},
|
|
1313
1350
|
|
|
1314
1351
|
onUserSpeechFinal(ctx) {
|
|
1315
|
-
submit(ctx.sessionId, ctx.text, "voice");
|
|
1352
|
+
submit(ctx.sessionId, ctx.text, "voice", ctx.utteranceId);
|
|
1316
1353
|
},
|
|
1317
1354
|
|
|
1318
1355
|
onDataChannelMessage(ctx) {
|
package/package.json
CHANGED
|
@@ -22,7 +22,7 @@ The `fillers` block in `voicethere.llm.json` lists short sentences the agent spe
|
|
|
22
22
|
|
|
23
23
|
```json
|
|
24
24
|
"fillers": {
|
|
25
|
-
"thinking": { "phrases": ["
|
|
25
|
+
"thinking": { "phrases": ["Okay.", "Right.", "Sure.", "Let me see."], "afterMs": 1500 },
|
|
26
26
|
"toolCall": { "phrases": ["Let me check that for you.", "One moment, I'll look that up."] },
|
|
27
27
|
"toolWait": { "phrases": ["Still checking, one moment.", "Almost there."], "afterMs": 4000 },
|
|
28
28
|
"interruptible": true
|
|
@@ -41,6 +41,10 @@ The `fillers` block in `voicethere.llm.json` lists short sentences the agent spe
|
|
|
41
41
|
|
|
42
42
|
`speech.coalesceMs` (0 to 1000, default 150) sets how long the agent holds a finished sentence to join it with the next one. Sentences that complete within the window are spoken as one utterance, so a reply the model returns at once has no gap between its sentences. The buffer is also spoken when the stream ends, at 400 characters, or when a tool call starts. Chat `assistant_delta` messages are not delayed. `0` speaks every sentence on its own.
|
|
43
43
|
|
|
44
|
+
## Client messages for voice replies
|
|
45
|
+
|
|
46
|
+
A voice reply also reaches the client as text. Each spoken sentence arrives as `{ "type": "assistant_delta", "turnId", "text", "spoken": true }`, and the turn ends with one `{ "type": "assistant_done", "turnId", "text", "spoken": true, "status" }`. `status` is `completed`, `failed`, `interrupted` or `cancelled`, and `text` holds the sentences delivered so far. When the runner sends an utterance id for the caller's final (`SpeechContext.utteranceId` in `onUserSpeechFinal`), both messages also carry `inReplyTo`, the caller utterance the reply answers. Render one line per `turnId` and reveal it as the speech plays. Typed (chat) turns keep sending `assistant_delta`, `assistant_done` and `chat_reply` without `spoken`. Fillers are never sent as text.
|
|
47
|
+
|
|
44
48
|
## Prompt guidance for phone calls
|
|
45
49
|
|
|
46
50
|
The template prompt ends with a "Voice call" section. In your own agent you can append the same text with `CALL_AGENT_PROMPT_GUIDANCE` from `@voicethere/agent/llm`:
|
|
@@ -50,7 +50,7 @@ The `fillers` block in `voicethere.llm.json` lists short sentences the agent spe
|
|
|
50
50
|
|
|
51
51
|
```json
|
|
52
52
|
"fillers": {
|
|
53
|
-
"thinking": { "phrases": ["
|
|
53
|
+
"thinking": { "phrases": ["Okay.", "Right.", "Sure.", "Let me see."], "afterMs": 1500 },
|
|
54
54
|
"toolCall": { "phrases": ["Let me check that for you.", "One moment, I'll look that up."] },
|
|
55
55
|
"toolWait": { "phrases": ["Still checking, one moment.", "Almost there."], "afterMs": 4000 },
|
|
56
56
|
"interruptible": true
|