@voicethere/agent 0.12.1 → 0.14.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/llm/agent/config.d.ts +123 -0
- package/dist/llm/agent/config.d.ts.map +1 -1
- package/dist/llm/agent/config.js +170 -1
- package/dist/llm/agent/config.js.map +1 -1
- package/dist/llm/agent/create-llm-agent.d.ts +5 -1
- package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
- package/dist/llm/agent/create-llm-agent.js +194 -10
- package/dist/llm/agent/create-llm-agent.js.map +1 -1
- package/dist/llm/agent/prompt-guidance.d.ts +7 -0
- package/dist/llm/agent/prompt-guidance.d.ts.map +1 -0
- package/dist/llm/agent/prompt-guidance.js +15 -0
- package/dist/llm/agent/prompt-guidance.js.map +1 -0
- package/dist/llm/anthropic.d.ts +6 -0
- package/dist/llm/anthropic.d.ts.map +1 -0
- package/dist/llm/anthropic.js +8 -0
- package/dist/llm/anthropic.js.map +1 -0
- package/dist/llm/gemini.d.ts +6 -0
- package/dist/llm/gemini.d.ts.map +1 -0
- package/dist/llm/gemini.js +8 -0
- package/dist/llm/gemini.js.map +1 -0
- package/dist/llm/index.d.ts +3 -2
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +1 -0
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/openai.d.ts +6 -0
- package/dist/llm/openai.d.ts.map +1 -0
- package/dist/llm/openai.js +12 -0
- package/dist/llm/openai.js.map +1 -0
- package/dist/llm/providers.d.ts +4 -2
- package/dist/llm/providers.d.ts.map +1 -1
- package/dist/llm/providers.js +20 -22
- package/dist/llm/providers.js.map +1 -1
- package/dist/llm/types.d.ts +10 -0
- package/dist/llm/types.d.ts.map +1 -1
- package/dist/templates/llm-agent/agent.js +26244 -100530
- package/eject/llm/loop.ts +236 -16
- package/package.json +19 -1
- package/schema/voicethere.llm.schema.json +93 -0
- package/templates/llm-agent/README.md +49 -0
- package/templates/llm-agent/agent.ts +9 -1
- package/templates/llm-agent/prompt.md +10 -0
- package/templates/llm-agent/voicethere.llm.json +18 -2
package/eject/llm/loop.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copied from @voicethere/agent 0.
|
|
1
|
+
// Copied from @voicethere/agent 0.14.0. Package updates no longer change this file.
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import {
|
|
4
4
|
type AgentHandlers,
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
type LlmMessage,
|
|
24
24
|
type LlmProvider,
|
|
25
25
|
LlmProviderError,
|
|
26
|
+
type LlmProviderModule,
|
|
26
27
|
type LlmStreamEvent,
|
|
27
28
|
type LlmToolResult,
|
|
28
29
|
McpToolSource,
|
|
@@ -48,12 +49,22 @@ export interface CreateLlmAgentOptions {
|
|
|
48
49
|
env?: () => Record<string, string | undefined>;
|
|
49
50
|
/** Test injection. */
|
|
50
51
|
provider?: LlmProvider;
|
|
52
|
+
/** Provider SDK modules this agent may use, e.g. `[openai]` from `@voicethere/agent/llm/openai`. Must include the one `config.provider` selects. */
|
|
53
|
+
providers?: readonly LlmProviderModule[];
|
|
51
54
|
now?: () => number;
|
|
55
|
+
/** Test injection: random number in [0, 1) used to pick a filler phrase. */
|
|
56
|
+
random?: () => number;
|
|
52
57
|
/** Test injection for MCP tool sources. */
|
|
53
58
|
sourceFactory?: (opts: McpToolSourceOptions) => McpToolSource;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
type Source = "voice" | "chat";
|
|
62
|
+
type FillerKind = "thinking" | "toolCall" | "toolWait";
|
|
63
|
+
|
|
64
|
+
const MAX_FILLERS_PER_TURN = 3;
|
|
65
|
+
const MAX_TOOL_WAIT_FILLERS = 2;
|
|
66
|
+
const TOOL_CALL_GAP_MS = 2_000;
|
|
67
|
+
const COALESCE_MAX_CHARS = 400;
|
|
57
68
|
|
|
58
69
|
interface TurnState {
|
|
59
70
|
turnId: string;
|
|
@@ -74,6 +85,19 @@ interface TurnState {
|
|
|
74
85
|
playedOverride?: string;
|
|
75
86
|
spokeFirst: boolean;
|
|
76
87
|
fillerSpoken: boolean;
|
|
88
|
+
/** Fillers from the `fillers` block spoken in this turn (cap 2). */
|
|
89
|
+
fillersSpoken: number;
|
|
90
|
+
thinkingSpoken: boolean;
|
|
91
|
+
toolCallSpoken: boolean;
|
|
92
|
+
toolWaitSpoken: number;
|
|
93
|
+
lastFillerAt?: number;
|
|
94
|
+
/** True once reply speech started after the last filler. */
|
|
95
|
+
replySinceFiller: boolean;
|
|
96
|
+
fillerTimers: Map<FillerKind, ReturnType<typeof setTimeout>>;
|
|
97
|
+
/** Sentences completed but not yet spoken (speech coalescing). */
|
|
98
|
+
pending: string[];
|
|
99
|
+
pendingChars: number;
|
|
100
|
+
coalesceTimer?: ReturnType<typeof setTimeout>;
|
|
77
101
|
fallbackSpoken: boolean;
|
|
78
102
|
status: TurnSummary["status"];
|
|
79
103
|
}
|
|
@@ -90,6 +114,7 @@ interface SessionState {
|
|
|
90
114
|
active?: ActiveTurn;
|
|
91
115
|
queued?: { text: string; source: Source };
|
|
92
116
|
announcing: number;
|
|
117
|
+
lastFillerPhrase?: string;
|
|
93
118
|
chain: Promise<void>;
|
|
94
119
|
lastInterruption?: {
|
|
95
120
|
turnId: string;
|
|
@@ -132,8 +157,21 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
132
157
|
);
|
|
133
158
|
}
|
|
134
159
|
const config: LlmAgentConfig = validated.config;
|
|
160
|
+
if (
|
|
161
|
+
!opts.provider &&
|
|
162
|
+
!(opts.providers ?? []).some((m) => m.ids.includes(config.provider))
|
|
163
|
+
) {
|
|
164
|
+
// Quotes are interpolated so the eject copy's import scan sees no specifier here.
|
|
165
|
+
const sub = config.provider === "openrouter" ? "openai" : config.provider;
|
|
166
|
+
const q = '"';
|
|
167
|
+
throw configError(
|
|
168
|
+
`voicethere.llm.json selects provider ${q}${config.provider}${q}, but agent.ts does not register it. Add: import { ${sub} } from ${q}@voicethere/agent/llm/${sub}${q}; and pass providers: [${sub}]`,
|
|
169
|
+
"LLM_CONFIG_INVALID",
|
|
170
|
+
);
|
|
171
|
+
}
|
|
135
172
|
const hooks = opts.hooks ?? {};
|
|
136
173
|
const now = opts.now ?? Date.now;
|
|
174
|
+
const random = opts.random ?? Math.random;
|
|
137
175
|
const getEnv = opts.env ?? (() => process.env);
|
|
138
176
|
const makeSource =
|
|
139
177
|
opts.sourceFactory ?? ((o: McpToolSourceOptions) => new McpToolSource(o));
|
|
@@ -147,6 +185,19 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
147
185
|
const useNote = config.history?.interruptionNote ?? D.interruptionNote;
|
|
148
186
|
const historyTurns = config.history?.turns ?? D.historyTurns;
|
|
149
187
|
const serviceWaitMs = config.serviceWaitMs ?? D.serviceWaitMs;
|
|
188
|
+
const fillerCfg = config.fillers;
|
|
189
|
+
const thinkingPhrases = fillerCfg?.thinking?.phrases ?? [];
|
|
190
|
+
const toolCallPhrases = fillerCfg?.toolCall?.phrases ?? [];
|
|
191
|
+
const toolWaitPhrases = fillerCfg?.toolWait?.phrases ?? [];
|
|
192
|
+
const thinkingAfterMs = fillerCfg?.thinking?.afterMs ?? D.thinkingAfterMs;
|
|
193
|
+
const toolWaitAfterMs = fillerCfg?.toolWait?.afterMs ?? D.toolWaitAfterMs;
|
|
194
|
+
const fillersInterruptible =
|
|
195
|
+
fillerCfg?.interruptible ?? D.fillersInterruptible;
|
|
196
|
+
const coalesceMs = config.speech?.coalesceMs ?? D.speechCoalesceMs;
|
|
197
|
+
const legacyToolFiller =
|
|
198
|
+
toolCallPhrases.length === 0 && toolWaitPhrases.length === 0
|
|
199
|
+
? config.tools?.filler
|
|
200
|
+
: undefined;
|
|
150
201
|
|
|
151
202
|
let provider: LlmProvider | undefined = opts.provider;
|
|
152
203
|
let registry: ToolRegistry | undefined;
|
|
@@ -191,11 +242,9 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
191
242
|
text: string,
|
|
192
243
|
interruptible = true,
|
|
193
244
|
): void {
|
|
245
|
+
flushSpeech(s, t);
|
|
194
246
|
t.spoken.push(text);
|
|
195
|
-
|
|
196
|
-
t.spokeFirst = true;
|
|
197
|
-
setAgentState(s.sessionId, "speaking");
|
|
198
|
-
}
|
|
247
|
+
markReplyStarted(s, t);
|
|
199
248
|
if (t.source === "chat") {
|
|
200
249
|
sendToClient(s.sessionId, {
|
|
201
250
|
type: "assistant_delta",
|
|
@@ -226,6 +275,147 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
226
275
|
}
|
|
227
276
|
}
|
|
228
277
|
|
|
278
|
+
// ---- fillers -----------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
function clearFillerTimer(t: TurnState, kind?: FillerKind): void {
|
|
281
|
+
for (const [k, timer] of t.fillerTimers) {
|
|
282
|
+
if (kind && k !== kind) continue;
|
|
283
|
+
clearTimeout(timer);
|
|
284
|
+
t.fillerTimers.delete(k);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function phrasesFor(kind: FillerKind): string[] {
|
|
289
|
+
return kind === "thinking"
|
|
290
|
+
? thinkingPhrases
|
|
291
|
+
: kind === "toolCall"
|
|
292
|
+
? toolCallPhrases
|
|
293
|
+
: toolWaitPhrases;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function pickFiller(s: SessionState, kind: FillerKind): string {
|
|
297
|
+
const all = phrasesFor(kind);
|
|
298
|
+
const pool =
|
|
299
|
+
all.length > 1 ? all.filter((p) => p !== s.lastFillerPhrase) : all;
|
|
300
|
+
const i = Math.min(pool.length - 1, Math.floor(random() * pool.length));
|
|
301
|
+
return pool[i];
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Speaks a filler of `kind` now, unless the turn's limits say no. Returns
|
|
306
|
+
* the ms to wait when the gap to the previous filler is too short.
|
|
307
|
+
*/
|
|
308
|
+
function tryFiller(
|
|
309
|
+
s: SessionState,
|
|
310
|
+
t: TurnState,
|
|
311
|
+
kind: FillerKind,
|
|
312
|
+
afterMs: number,
|
|
313
|
+
): { wait: number } | undefined {
|
|
314
|
+
if (t.controller.signal.aborted || s.ended) return undefined;
|
|
315
|
+
if (t.source === "chat" && !speakChat) return undefined;
|
|
316
|
+
if (phrasesFor(kind).length === 0) return undefined;
|
|
317
|
+
if (t.fillersSpoken >= MAX_FILLERS_PER_TURN) return undefined;
|
|
318
|
+
if (kind === "thinking" && (t.thinkingSpoken || t.spokeFirst)) {
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
if (kind === "toolCall" && (t.toolCallSpoken || t.spokeFirst)) {
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|
|
324
|
+
if (kind === "toolWait" && t.toolWaitSpoken >= MAX_TOOL_WAIT_FILLERS) {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
if (t.lastFillerAt !== undefined && !t.replySinceFiller) {
|
|
328
|
+
const gap = kind === "toolCall" ? TOOL_CALL_GAP_MS : afterMs;
|
|
329
|
+
const rest = gap - (now() - t.lastFillerAt);
|
|
330
|
+
if (rest > 0) return kind === "toolCall" ? undefined : { wait: rest };
|
|
331
|
+
}
|
|
332
|
+
const text = pickFiller(s, kind);
|
|
333
|
+
s.lastFillerPhrase = text;
|
|
334
|
+
t.fillersSpoken += 1;
|
|
335
|
+
t.lastFillerAt = now();
|
|
336
|
+
t.replySinceFiller = false;
|
|
337
|
+
if (kind === "thinking") t.thinkingSpoken = true;
|
|
338
|
+
if (kind === "toolCall") t.toolCallSpoken = true;
|
|
339
|
+
if (kind === "toolWait") t.toolWaitSpoken += 1;
|
|
340
|
+
speakAnnouncement(s, t.turnId, text, fillersInterruptible);
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function armFiller(
|
|
345
|
+
s: SessionState,
|
|
346
|
+
t: TurnState,
|
|
347
|
+
kind: FillerKind,
|
|
348
|
+
delayMs: number,
|
|
349
|
+
afterMs: number,
|
|
350
|
+
): void {
|
|
351
|
+
if (phrasesFor(kind).length === 0 || s.ended) return;
|
|
352
|
+
clearFillerTimer(t, kind);
|
|
353
|
+
const timer = setTimeout(() => {
|
|
354
|
+
t.fillerTimers.delete(kind);
|
|
355
|
+
const r = tryFiller(s, t, kind, afterMs);
|
|
356
|
+
if (r) armFiller(s, t, kind, r.wait, afterMs);
|
|
357
|
+
}, delayMs);
|
|
358
|
+
t.fillerTimers.set(kind, timer);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ---- speech coalescing -------------------------------------------------
|
|
362
|
+
|
|
363
|
+
function dropPending(t: TurnState): void {
|
|
364
|
+
if (t.coalesceTimer) clearTimeout(t.coalesceTimer);
|
|
365
|
+
t.coalesceTimer = undefined;
|
|
366
|
+
t.pending = [];
|
|
367
|
+
t.pendingChars = 0;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** Speaks the buffered sentences as one utterance. */
|
|
371
|
+
function flushSpeech(s: SessionState, t: TurnState): void {
|
|
372
|
+
if (t.coalesceTimer) clearTimeout(t.coalesceTimer);
|
|
373
|
+
t.coalesceTimer = undefined;
|
|
374
|
+
if (t.pending.length === 0) return;
|
|
375
|
+
const text = t.pending.join(" ");
|
|
376
|
+
t.pending = [];
|
|
377
|
+
t.pendingChars = 0;
|
|
378
|
+
if (t.controller.signal.aborted || s.ended) return;
|
|
379
|
+
speak(s.sessionId, text, { turnId: t.turnId });
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** A sentence of the model's reply: chat delta now, speech coalesced. */
|
|
383
|
+
function deliverReply(s: SessionState, t: TurnState, text: string): void {
|
|
384
|
+
if (coalesceMs <= 0) {
|
|
385
|
+
deliver(s, t, text);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
markReplyStarted(s, t);
|
|
389
|
+
t.spoken.push(text);
|
|
390
|
+
if (t.source === "chat") {
|
|
391
|
+
sendToClient(s.sessionId, {
|
|
392
|
+
type: "assistant_delta",
|
|
393
|
+
turnId: t.turnId,
|
|
394
|
+
text,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
if (t.source !== "voice" && !speakChat) return;
|
|
398
|
+
t.pending.push(text);
|
|
399
|
+
t.pendingChars += text.length;
|
|
400
|
+
if (t.pendingChars >= COALESCE_MAX_CHARS) {
|
|
401
|
+
flushSpeech(s, t);
|
|
402
|
+
} else if (!t.coalesceTimer) {
|
|
403
|
+
t.coalesceTimer = setTimeout(() => {
|
|
404
|
+
t.coalesceTimer = undefined;
|
|
405
|
+
flushSpeech(s, t);
|
|
406
|
+
}, coalesceMs);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function markReplyStarted(s: SessionState, t: TurnState): void {
|
|
411
|
+
t.replySinceFiller = true;
|
|
412
|
+
if (!t.spokeFirst) {
|
|
413
|
+
t.spokeFirst = true;
|
|
414
|
+
clearFillerTimer(t, "thinking");
|
|
415
|
+
setAgentState(s.sessionId, "speaking");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
229
419
|
// ---- tools -------------------------------------------------------------
|
|
230
420
|
|
|
231
421
|
function buildRegistry(env: Record<string, string | undefined>): void {
|
|
@@ -390,7 +580,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
390
580
|
}
|
|
391
581
|
if (t.controller.signal.aborted) return;
|
|
392
582
|
t.roundText += `${t.roundText ? " " : ""}${out}`;
|
|
393
|
-
|
|
583
|
+
deliverReply(s, t, out);
|
|
394
584
|
}
|
|
395
585
|
|
|
396
586
|
async function runRound(
|
|
@@ -414,6 +604,9 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
414
604
|
];
|
|
415
605
|
}
|
|
416
606
|
t.llmCalls += 1;
|
|
607
|
+
if (t.llmCalls === 1 && !t.spokeFirst) {
|
|
608
|
+
armFiller(s, t, "thinking", thinkingAfterMs, thinkingAfterMs);
|
|
609
|
+
}
|
|
417
610
|
t.roundOpen = true;
|
|
418
611
|
t.roundText = "";
|
|
419
612
|
const splitter = new SentenceSplitter();
|
|
@@ -448,6 +641,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
448
641
|
for (const sentence of splitter.end()) {
|
|
449
642
|
await emitSentence(s, t, ctx, sentence);
|
|
450
643
|
}
|
|
644
|
+
flushSpeech(s, t);
|
|
451
645
|
if (done.usage) {
|
|
452
646
|
if (done.usage.inputTokens !== undefined) {
|
|
453
647
|
t.inputTokens = (t.inputTokens ?? 0) + done.usage.inputTokens;
|
|
@@ -559,18 +753,21 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
559
753
|
.join(",")
|
|
560
754
|
.slice(0, 64),
|
|
561
755
|
});
|
|
562
|
-
|
|
563
|
-
|
|
756
|
+
clearFillerTimer(t, "thinking");
|
|
757
|
+
tryFiller(s, t, "toolCall", 0);
|
|
758
|
+
armFiller(s, t, "toolWait", toolWaitAfterMs, toolWaitAfterMs);
|
|
759
|
+
const fillerLegacy = legacyToolFiller;
|
|
760
|
+
if (fillerLegacy && !t.fillerSpoken) {
|
|
564
761
|
filler = setTimeout(() => {
|
|
565
762
|
if (t.fillerSpoken || signal.aborted) return;
|
|
566
763
|
t.fillerSpoken = true;
|
|
567
764
|
speakAnnouncement(
|
|
568
765
|
s,
|
|
569
766
|
t.turnId,
|
|
570
|
-
|
|
571
|
-
|
|
767
|
+
fillerLegacy.text,
|
|
768
|
+
fillerLegacy.interruptible ?? D.fillerInterruptible,
|
|
572
769
|
);
|
|
573
|
-
},
|
|
770
|
+
}, fillerLegacy.afterMs ?? D.fillerAfterMs);
|
|
574
771
|
}
|
|
575
772
|
let results: LlmToolResult[];
|
|
576
773
|
try {
|
|
@@ -578,6 +775,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
578
775
|
} finally {
|
|
579
776
|
if (filler) clearTimeout(filler);
|
|
580
777
|
filler = undefined;
|
|
778
|
+
clearFillerTimer(t);
|
|
581
779
|
}
|
|
582
780
|
s.history.addToolResults(results, { turnId: t.turnId });
|
|
583
781
|
if (signal.aborted)
|
|
@@ -609,6 +807,8 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
609
807
|
return summarize(t);
|
|
610
808
|
} finally {
|
|
611
809
|
if (filler) clearTimeout(filler);
|
|
810
|
+
clearFillerTimer(t);
|
|
811
|
+
if (signal.aborted) dropPending(t);
|
|
612
812
|
}
|
|
613
813
|
}
|
|
614
814
|
|
|
@@ -701,6 +901,14 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
701
901
|
roundText: "",
|
|
702
902
|
spokeFirst: false,
|
|
703
903
|
fillerSpoken: false,
|
|
904
|
+
fillersSpoken: 0,
|
|
905
|
+
thinkingSpoken: false,
|
|
906
|
+
toolCallSpoken: false,
|
|
907
|
+
toolWaitSpoken: 0,
|
|
908
|
+
replySinceFiller: false,
|
|
909
|
+
fillerTimers: new Map(),
|
|
910
|
+
pending: [],
|
|
911
|
+
pendingChars: 0,
|
|
704
912
|
fallbackSpoken: false,
|
|
705
913
|
status: "completed",
|
|
706
914
|
};
|
|
@@ -783,6 +991,8 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
783
991
|
cancelSpeech(s.sessionId, { turnId: active.turnId });
|
|
784
992
|
active.state.abortStatus = "cancelled";
|
|
785
993
|
active.state.controller.abort();
|
|
994
|
+
clearFillerTimer(active.state);
|
|
995
|
+
dropPending(active.state);
|
|
786
996
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
787
997
|
await Promise.race([
|
|
788
998
|
active.promise,
|
|
@@ -813,6 +1023,9 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
813
1023
|
|
|
814
1024
|
return {
|
|
815
1025
|
async onAgentStart() {
|
|
1026
|
+
for (const w of validated.warnings) {
|
|
1027
|
+
agentLog("warn", `config warning: ${w.path} ${w.message}`);
|
|
1028
|
+
}
|
|
816
1029
|
const env = getEnv();
|
|
817
1030
|
const apiKey = env[config.apiKeySecret];
|
|
818
1031
|
if (!nonEmpty(apiKey)) {
|
|
@@ -822,11 +1035,14 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
822
1035
|
);
|
|
823
1036
|
}
|
|
824
1037
|
if (!provider) {
|
|
825
|
-
provider = createLlmProvider(
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
1038
|
+
provider = createLlmProvider(
|
|
1039
|
+
{
|
|
1040
|
+
provider: config.provider,
|
|
1041
|
+
apiKey,
|
|
1042
|
+
baseUrl: config.baseUrl,
|
|
1043
|
+
},
|
|
1044
|
+
opts.providers ?? [],
|
|
1045
|
+
);
|
|
830
1046
|
}
|
|
831
1047
|
buildRegistry(env);
|
|
832
1048
|
},
|
|
@@ -883,6 +1099,8 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
883
1099
|
const active = s.active;
|
|
884
1100
|
if (active && active.turnId === ctx.turnId) {
|
|
885
1101
|
const t = active.state;
|
|
1102
|
+
clearFillerTimer(t);
|
|
1103
|
+
dropPending(t);
|
|
886
1104
|
if (t.roundOpen) {
|
|
887
1105
|
t.playedOverride = ctx.playedText;
|
|
888
1106
|
if (note) s.history.setPendingNote(note);
|
|
@@ -906,6 +1124,8 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
906
1124
|
if (s.active) {
|
|
907
1125
|
s.active.state.abortStatus = "cancelled";
|
|
908
1126
|
s.active.state.controller.abort();
|
|
1127
|
+
clearFillerTimer(s.active.state);
|
|
1128
|
+
dropPending(s.active.state);
|
|
909
1129
|
}
|
|
910
1130
|
sessions.delete(ctx.sessionId);
|
|
911
1131
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voicethere/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -22,6 +22,24 @@
|
|
|
22
22
|
"require": "./dist/host/index.js",
|
|
23
23
|
"default": "./dist/host/index.js"
|
|
24
24
|
},
|
|
25
|
+
"./llm/openai": {
|
|
26
|
+
"types": "./dist/llm/openai.d.ts",
|
|
27
|
+
"import": "./dist/llm/openai.js",
|
|
28
|
+
"require": "./dist/llm/openai.js",
|
|
29
|
+
"default": "./dist/llm/openai.js"
|
|
30
|
+
},
|
|
31
|
+
"./llm/anthropic": {
|
|
32
|
+
"types": "./dist/llm/anthropic.d.ts",
|
|
33
|
+
"import": "./dist/llm/anthropic.js",
|
|
34
|
+
"require": "./dist/llm/anthropic.js",
|
|
35
|
+
"default": "./dist/llm/anthropic.js"
|
|
36
|
+
},
|
|
37
|
+
"./llm/gemini": {
|
|
38
|
+
"types": "./dist/llm/gemini.d.ts",
|
|
39
|
+
"import": "./dist/llm/gemini.js",
|
|
40
|
+
"require": "./dist/llm/gemini.js",
|
|
41
|
+
"default": "./dist/llm/gemini.js"
|
|
42
|
+
},
|
|
25
43
|
"./llm": {
|
|
26
44
|
"types": "./dist/llm/index.d.ts",
|
|
27
45
|
"import": "./dist/llm/index.js",
|
|
@@ -135,6 +135,8 @@
|
|
|
135
135
|
},
|
|
136
136
|
"filler": {
|
|
137
137
|
"type": "object",
|
|
138
|
+
"deprecated": true,
|
|
139
|
+
"description": "Deprecated: use the top-level fillers block. Ignored when fillers is set.",
|
|
138
140
|
"additionalProperties": false,
|
|
139
141
|
"required": [
|
|
140
142
|
"text"
|
|
@@ -159,6 +161,97 @@
|
|
|
159
161
|
}
|
|
160
162
|
}
|
|
161
163
|
},
|
|
164
|
+
"fillers": {
|
|
165
|
+
"type": "object",
|
|
166
|
+
"description": "Short sentences the agent speaks while the caller waits. Off when absent. At most 3 fillers per turn, never back to back.",
|
|
167
|
+
"additionalProperties": false,
|
|
168
|
+
"properties": {
|
|
169
|
+
"thinking": {
|
|
170
|
+
"type": "object",
|
|
171
|
+
"description": "Spoken when no sentence of the reply was spoken afterMs after the model call started. At most once per turn.",
|
|
172
|
+
"additionalProperties": false,
|
|
173
|
+
"properties": {
|
|
174
|
+
"phrases": {
|
|
175
|
+
"type": "array",
|
|
176
|
+
"description": "Phrases to pick from at random, never the same as the previous filler of the session. Empty or absent turns this situation off.",
|
|
177
|
+
"maxItems": 20,
|
|
178
|
+
"items": {
|
|
179
|
+
"type": "string",
|
|
180
|
+
"minLength": 1,
|
|
181
|
+
"maxLength": 60
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
"afterMs": {
|
|
185
|
+
"type": "integer",
|
|
186
|
+
"description": "Wait for the first sentence of the reply, in milliseconds.",
|
|
187
|
+
"minimum": 300,
|
|
188
|
+
"maximum": 10000,
|
|
189
|
+
"default": 1500
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
"toolCall": {
|
|
194
|
+
"type": "object",
|
|
195
|
+
"description": "Spoken right when the model requests a tool call, only if nothing was spoken in the turn yet. At most once per turn.",
|
|
196
|
+
"additionalProperties": false,
|
|
197
|
+
"properties": {
|
|
198
|
+
"phrases": {
|
|
199
|
+
"type": "array",
|
|
200
|
+
"description": "Phrases to pick from at random, never the same as the previous filler of the session. Empty or absent turns this situation off.",
|
|
201
|
+
"maxItems": 20,
|
|
202
|
+
"items": {
|
|
203
|
+
"type": "string",
|
|
204
|
+
"minLength": 1,
|
|
205
|
+
"maxLength": 120
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
"toolWait": {
|
|
211
|
+
"type": "object",
|
|
212
|
+
"description": "Spoken when a tool round is still running afterMs after it started. At most once per tool round, twice per turn.",
|
|
213
|
+
"additionalProperties": false,
|
|
214
|
+
"properties": {
|
|
215
|
+
"phrases": {
|
|
216
|
+
"type": "array",
|
|
217
|
+
"description": "Phrases to pick from at random, never the same as the previous filler of the session. Empty or absent turns this situation off.",
|
|
218
|
+
"maxItems": 20,
|
|
219
|
+
"items": {
|
|
220
|
+
"type": "string",
|
|
221
|
+
"minLength": 1,
|
|
222
|
+
"maxLength": 120
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
"afterMs": {
|
|
226
|
+
"type": "integer",
|
|
227
|
+
"description": "How long a tool round runs before the filler, in milliseconds.",
|
|
228
|
+
"minimum": 1000,
|
|
229
|
+
"maximum": 30000,
|
|
230
|
+
"default": 4000
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
"interruptible": {
|
|
235
|
+
"type": "boolean",
|
|
236
|
+
"description": "Whether barge-in may cut a filler.",
|
|
237
|
+
"default": true
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
"speech": {
|
|
242
|
+
"type": "object",
|
|
243
|
+
"description": "How the reply is spoken.",
|
|
244
|
+
"additionalProperties": false,
|
|
245
|
+
"properties": {
|
|
246
|
+
"coalesceMs": {
|
|
247
|
+
"type": "integer",
|
|
248
|
+
"description": "Sentences that complete within this window are spoken as one utterance, so a reply that arrives at once has no gap between sentences. 0 speaks every sentence on its own.",
|
|
249
|
+
"minimum": 0,
|
|
250
|
+
"maximum": 1000,
|
|
251
|
+
"default": 150
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
},
|
|
162
255
|
"toolSources": {
|
|
163
256
|
"type": "array",
|
|
164
257
|
"maxItems": 8,
|
|
@@ -15,10 +15,59 @@ A voice and chat agent powered by an LLM, with custom tools and MCP servers. Sou
|
|
|
15
15
|
|
|
16
16
|
The default provider is OpenAI. Add a project secret named `AGENT_OPENAI_API_KEY` with your key, then deploy. The key is read by name from `apiKeySecret` in `voicethere.llm.json`; it is never stored in the file. Only project secrets whose names start with `AGENT_` reach the agent.
|
|
17
17
|
|
|
18
|
+
## Filler phrases
|
|
19
|
+
|
|
20
|
+
The `fillers` block in `voicethere.llm.json` lists short sentences the agent speaks while the caller waits. Each situation has its own phrases; leave a situation out, or give it an empty list, to turn it off. The whole block is off when absent.
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
"fillers": {
|
|
24
|
+
"thinking": { "phrases": ["Hmm.", "Okay.", "Mm-hm.", "Right."], "afterMs": 1500 },
|
|
25
|
+
"toolCall": { "phrases": ["Let me check that for you.", "One moment, I'll look that up."] },
|
|
26
|
+
"toolWait": { "phrases": ["Still checking, one moment.", "Almost there."], "afterMs": 4000 },
|
|
27
|
+
"interruptible": true
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- `thinking`: no sentence of the reply was spoken `afterMs` (300 to 10000, default 1500) after the model call started. Phrases up to 60 characters. Once per turn.
|
|
32
|
+
- `toolCall`: the model asked for a tool and nothing has been spoken in this turn yet (no reply sentence, and no `thinking` filler in the last 2 seconds). It is spoken at once, before the tool runs. Phrases up to 120 characters. Once per turn.
|
|
33
|
+
- `toolWait`: a tool round is still running `afterMs` (1000 to 30000, default 4000) after it started. Once per tool round, twice per turn.
|
|
34
|
+
- At most 3 fillers per turn. Two fillers never play back to back unless the reply spoke in between or `afterMs` passed. Phrases are picked at random and never repeat the previous filler of the session. Each list takes up to 20 phrases.
|
|
35
|
+
- Callers can cut a filler by speaking (`interruptible`, default true). If the answer arrives while a filler plays, the answer follows it.
|
|
36
|
+
- Fillers are speech only. They do not appear in the conversation the model sees or in chat text.
|
|
37
|
+
- `tools.filler` (a single text) is deprecated. It still works when `toolCall` and `toolWait` have no phrases; otherwise `fillers` wins and a warning is logged.
|
|
38
|
+
|
|
39
|
+
## Speech coalescing
|
|
40
|
+
|
|
41
|
+
`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.
|
|
42
|
+
|
|
43
|
+
## Prompt guidance for phone calls
|
|
44
|
+
|
|
45
|
+
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`:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { CALL_AGENT_PROMPT_GUIDANCE } from "@voicethere/agent/llm";
|
|
49
|
+
|
|
50
|
+
createLlmAgent({
|
|
51
|
+
systemPrompt: `${prompt}\n\n${CALL_AGENT_PROMPT_GUIDANCE}` /* ... */,
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
It is never added automatically.
|
|
56
|
+
|
|
18
57
|
## Switch providers
|
|
19
58
|
|
|
20
59
|
Change `provider`, `model` and `apiKeySecret` in `voicethere.llm.json`. For example, `anthropic` with `AGENT_ANTHROPIC_API_KEY`, `gemini` with `AGENT_GEMINI_API_KEY`, or `openrouter` with `AGENT_OPENROUTER_API_KEY`. Create the matching project secret before you deploy.
|
|
21
60
|
|
|
61
|
+
## Switching provider
|
|
62
|
+
|
|
63
|
+
Only the provider you import in `agent.ts` is bundled. When you change `provider` in `voicethere.llm.json`, also change the import and the `providers` list:
|
|
64
|
+
|
|
65
|
+
- `anthropic`: `import { anthropic } from "@voicethere/agent/llm/anthropic";` and `providers: [anthropic]`
|
|
66
|
+
- `gemini`: `import { gemini } from "@voicethere/agent/llm/gemini";` and `providers: [gemini]`
|
|
67
|
+
- `openrouter` uses the `openai` module.
|
|
68
|
+
|
|
69
|
+
If they do not match, `createLlmAgent` throws when the bundle loads, and the deploy check reports it.
|
|
70
|
+
|
|
22
71
|
## Learn more
|
|
23
72
|
|
|
24
73
|
See the docs page `/docs/llm-agents` for the full list of settings, tools, MCP servers and hooks.
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { defineAgent } from "@voicethere/agent";
|
|
2
2
|
import { createLlmAgent } from "@voicethere/agent/llm";
|
|
3
|
+
import { openai } from "@voicethere/agent/llm/openai";
|
|
3
4
|
import config from "./voicethere.llm.json";
|
|
4
5
|
import systemPrompt from "./prompt.md";
|
|
5
6
|
import { tools } from "./tools";
|
|
6
7
|
import { hooks } from "./hooks";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
// Only the provider you import here is bundled. To switch provider, change
|
|
10
|
+
// "provider" in voicethere.llm.json and import its module instead:
|
|
11
|
+
// anthropic → import { anthropic } from "@voicethere/agent/llm/anthropic";
|
|
12
|
+
// gemini → import { gemini } from "@voicethere/agent/llm/gemini";
|
|
13
|
+
// openrouter uses the openai module.
|
|
14
|
+
defineAgent(
|
|
15
|
+
createLlmAgent({ config, systemPrompt, tools, hooks, providers: [openai] }),
|
|
16
|
+
);
|
|
@@ -9,3 +9,13 @@ If you do not know something, say so plainly instead of guessing. Offer to help
|
|
|
9
9
|
Use your tools when they help. For example, call get_current_time when someone asks what time or date it is, and pass the time zone if they name one.
|
|
10
10
|
|
|
11
11
|
Keep numbers and dates easy to say. Say "March third" instead of "03/03", and round long numbers when the exact value does not matter.
|
|
12
|
+
|
|
13
|
+
## Voice call
|
|
14
|
+
|
|
15
|
+
You are on a live phone call. Always answer right away; never leave the caller in silence.
|
|
16
|
+
|
|
17
|
+
Before you call a tool, say one short sentence that you are looking it up (for example "Let me check that for you."), then call the tool.
|
|
18
|
+
|
|
19
|
+
Keep sentences short; one idea per sentence. No lists, no markdown, no emojis; say numbers and dates the way a person would.
|
|
20
|
+
|
|
21
|
+
If you cannot help, say so in one sentence and offer what you can do.
|
|
@@ -11,10 +11,26 @@
|
|
|
11
11
|
},
|
|
12
12
|
"bargeIn": { "mode": "project" },
|
|
13
13
|
"history": { "turns": 20, "interruptionNote": true },
|
|
14
|
+
"fillers": {
|
|
15
|
+
"thinking": {
|
|
16
|
+
"phrases": ["Hmm.", "Okay.", "Mm-hm.", "Right."],
|
|
17
|
+
"afterMs": 1500
|
|
18
|
+
},
|
|
19
|
+
"toolCall": {
|
|
20
|
+
"phrases": [
|
|
21
|
+
"Let me check that for you.",
|
|
22
|
+
"One moment, I'll look that up."
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"toolWait": {
|
|
26
|
+
"phrases": ["Still checking, one moment.", "Almost there."],
|
|
27
|
+
"afterMs": 4000
|
|
28
|
+
},
|
|
29
|
+
"interruptible": true
|
|
30
|
+
},
|
|
14
31
|
"tools": {
|
|
15
32
|
"maxRounds": 5,
|
|
16
|
-
"timeoutMs": 15000
|
|
17
|
-
"filler": { "text": "One moment." }
|
|
33
|
+
"timeoutMs": 15000
|
|
18
34
|
},
|
|
19
35
|
"toolSources": [],
|
|
20
36
|
"interruptPolicy": "cancel"
|