@voicethere/agent 0.28.1 → 0.29.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.
Files changed (44) hide show
  1. package/README.md +40 -17
  2. package/dist/agent.js +85 -3
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
  8. package/dist/llm/agent/create-llm-agent.js +9 -30
  9. package/dist/llm/agent/create-llm-agent.js.map +1 -1
  10. package/dist/protocol.d.ts +95 -2
  11. package/dist/protocol.d.ts.map +1 -1
  12. package/dist/protocol.js.map +1 -1
  13. package/dist/runtime.d.ts +51 -1
  14. package/dist/runtime.d.ts.map +1 -1
  15. package/dist/runtime.js +184 -1
  16. package/dist/runtime.js.map +1 -1
  17. package/dist/templates/echo/agent.js +84 -3
  18. package/dist/templates/echo-dc/agent.js +84 -3
  19. package/dist/templates/flow-agent/agent.js +84 -3
  20. package/dist/templates/game-sync/agent.js +84 -3
  21. package/dist/templates/language-switch/agent.js +84 -3
  22. package/dist/templates/llm-agent/agent.js +91 -30
  23. package/dist/templates/llm-docs-rag/agent.js +91 -30
  24. package/dist/templates/llm-mcp-service/agent.js +91 -30
  25. package/dist/templates/llm-support-sticky-user/agent.js +91 -30
  26. package/dist/templates/positional-tts/agent.js +84 -3
  27. package/dist/templates/recording-consent/agent.js +84 -3
  28. package/dist/templates/registry.d.ts.map +1 -1
  29. package/dist/templates/registry.js +11 -0
  30. package/dist/templates/registry.js.map +1 -1
  31. package/dist/templates/spatial-showcase/agent.js +84 -3
  32. package/dist/templates/speaker-gate/agent.js +1575 -0
  33. package/dist/templates/voice-showcase/agent.js +84 -3
  34. package/dist/templates/voice-starter/agent.js +84 -3
  35. package/dist/templates/webhooks/agent.js +84 -3
  36. package/dist/templates/webhooks-redis/agent.js +84 -3
  37. package/dist/templates/world-sync/agent.js +84 -3
  38. package/dist/templates/world-sync-binary/agent.js +84 -3
  39. package/eject/llm/loop.ts +11 -37
  40. package/package.json +1 -1
  41. package/templates/README.md +24 -23
  42. package/templates/llm-agent/README.md +4 -0
  43. package/templates/speaker-gate/README.md +48 -0
  44. package/templates/speaker-gate/agent.ts +168 -0
@@ -17,10 +17,10 @@ import {
17
17
 
18
18
  ## Product vs e2e
19
19
 
20
- | Kind | Dashboard create | Prebuilt seed bundle | Typical consumer |
21
- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ | ----------------------- |
22
- | **product** | Yes (`echo`, `echo-dc`, `voice-starter`, `language-switch`, `world-sync`, `world-sync-binary`, `game-sync`, `voice-showcase`, `recording-consent`, `positional-tts`, `spatial-showcase`, `webhooks`, `webhooks-redis`) | Yes — `dist/templates/<id>/agent.js` | Platform project create |
23
- | **e2e** | No | No — build from sources at test time | `voicethere/e2e` smokes |
20
+ | Kind | Dashboard create | Prebuilt seed bundle | Typical consumer |
21
+ | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ | ----------------------- |
22
+ | **product** | Yes (`echo`, `echo-dc`, `voice-starter`, `language-switch`, `speaker-gate`, `world-sync`, `world-sync-binary`, `game-sync`, `voice-showcase`, `recording-consent`, `positional-tts`, `spatial-showcase`, `webhooks`, `webhooks-redis`) | Yes — `dist/templates/<id>/agent.js` | Platform project create |
23
+ | **e2e** | No | No — build from sources at test time | `voicethere/e2e` smokes |
24
24
 
25
25
  Product templates always set `seedOnCreate: true`. CI fails if a product template is missing its prebuilt bundle after `npm run build`.
26
26
 
@@ -59,25 +59,26 @@ See each folder README for the wire format.
59
59
 
60
60
  Each folder has its own README. Summary:
61
61
 
62
- | Id | Folder | Summary |
63
- | ------------------- | -------------------- | ----------------------------------------------------------------- |
64
- | `echo` | `echo/` | Voice + chat echo |
65
- | `echo-dc` | `echo-dc/` | Data-channel echo, no TTS |
66
- | `voice-starter` | `voice-starter/` | Every speech event |
67
- | `language-switch` | `language-switch/` | Manual `setVoiceLanguage`, or runner auto-switch with a localized echo of the recognized text |
68
- | `world-sync` | `world-sync/` | JSON pose broadcast |
69
- | `world-sync-binary` | `world-sync-binary/` | Binary pose `ArrayBuffer` |
70
- | `game-sync` | `game-sync/` | Authoritative sim, Redis + binary snapshots |
71
- | `voice-showcase` | `voice-showcase/` | Conversational landing demo |
72
- | `recording-consent` | `recording-consent/` | Recording consent flow |
73
- | `positional-tts` | `positional-tts/` | Orbiting TTS |
74
- | `spatial-showcase` | `spatial-showcase/` | Orbit / soundboard / proximity |
75
- | `webhooks` | `webhooks/` | Inbound HMAC webhooks |
76
- | `webhooks-redis` | `webhooks-redis/` | Webhooks plus Redis counter |
77
- | `llm-agent` | `llm-agent/` | LLM voice and chat agent, tools, MCP, optional knowledge (RAG) |
78
- | `llm-mcp-service` | `llm-mcp-service/` | `llm-agent` plus a project service that serves MCP tools |
79
- | `llm-docs-rag` | `llm-docs-rag/` | `llm-agent` answering from your docs via the `docs-rag-mcp` image |
80
- | `llm-support-sticky-user` | `llm-support-sticky-user/` | `llm-agent` for support: verified users get their tickets and earlier conversations |
62
+ | Id | Folder | Summary |
63
+ | ------------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
64
+ | `echo` | `echo/` | Voice + chat echo |
65
+ | `echo-dc` | `echo-dc/` | Data-channel echo, no TTS |
66
+ | `voice-starter` | `voice-starter/` | Every speech event |
67
+ | `language-switch` | `language-switch/` | Manual `setVoiceLanguage`, or runner auto-switch with a localized echo of the recognized text |
68
+ | `speaker-gate` | `speaker-gate/` | Caller speaker gate: `auto` listens to the enrolled caller only, `manual` labels every speaker; switch with `setSpeakerGate` |
69
+ | `world-sync` | `world-sync/` | JSON pose broadcast |
70
+ | `world-sync-binary` | `world-sync-binary/` | Binary pose `ArrayBuffer` |
71
+ | `game-sync` | `game-sync/` | Authoritative sim, Redis + binary snapshots |
72
+ | `voice-showcase` | `voice-showcase/` | Conversational landing demo |
73
+ | `recording-consent` | `recording-consent/` | Recording consent flow |
74
+ | `positional-tts` | `positional-tts/` | Orbiting TTS |
75
+ | `spatial-showcase` | `spatial-showcase/` | Orbit / soundboard / proximity |
76
+ | `webhooks` | `webhooks/` | Inbound HMAC webhooks |
77
+ | `webhooks-redis` | `webhooks-redis/` | Webhooks plus Redis counter |
78
+ | `llm-agent` | `llm-agent/` | LLM voice and chat agent, tools, MCP, optional knowledge (RAG) |
79
+ | `llm-mcp-service` | `llm-mcp-service/` | `llm-agent` plus a project service that serves MCP tools |
80
+ | `llm-docs-rag` | `llm-docs-rag/` | `llm-agent` answering from your docs via the `docs-rag-mcp` image |
81
+ | `llm-support-sticky-user` | `llm-support-sticky-user/` | `llm-agent` for support: verified users get their tickets and earlier conversations |
81
82
 
82
83
  ## E2e templates
83
84
 
@@ -16,6 +16,10 @@ A voice and chat agent powered by an LLM, with custom tools and MCP servers. Sou
16
16
 
17
17
  The template starts on OpenRouter with the model `anthropic/claude-haiku-5.5`. Create a key at openrouter.ai, add it as a project secret named `AGENT_OPENROUTER_API_KEY`, then deploy. For production, pick the model from OpenRouter's list yourself, checking cost, latency and tool support. 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.
18
18
 
19
+ ## Greeting
20
+
21
+ The greeting is sent like every spoken turn: `assistant_delta` and `assistant_done` (`status: "completed"`) go out as soon as its text is complete, before the audio plays. `assistant_done` does not wait for playback. If the caller cuts the greeting off, that arrives as `barge_in` / `agent_interrupted`, the same as for a reply.
22
+
19
23
  ## Filler phrases
20
24
 
21
25
  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.
@@ -0,0 +1,48 @@
1
+ # speaker-gate
2
+
3
+ A phone on a table picks up everyone in the room. The speaker gate learns the first voice of the call, the caller's, and then tells that voice apart from the rest. This template shows the three modes and how to switch between them while the call is running.
4
+
5
+ The project's voice settings pick the mode the call starts in (`speakerGate.mode`). Details and the full settings list are on the docs page at `/docs/speaker-gate`.
6
+
7
+ ## Modes
8
+
9
+ | Mode | What reaches the agent |
10
+ | ---------- | ------------------------------------------------------------------------------------------------------------------------------ |
11
+ | `disabled` | Every final. No speaker fields. |
12
+ | `auto` | Only the enrolled caller's finals, as a normal `onUserSpeechFinal`. A bystander arrives as `onUserSpeechIgnored` with no text. |
13
+ | `manual` | Every final, with `speaker` set to `caller`, `other` or `unknown`. The agent decides what to do. |
14
+
15
+ `speakerUnknownReason`, `speakerCheckWindows` and `speakerRechecked` come along when the runner sets them. A separate `bargeIn` policy says who may interrupt the agent: `caller_only` (default) or `anyone`.
16
+
17
+ ## What the template does
18
+
19
+ - In `manual` it answers `caller` and `unknown` finals. When it hears `other` it says one short line, at most once every 30 seconds, and otherwise stays quiet.
20
+ - Every event writes one agent log line with a counter `n` that starts at 1 and counts all lines of the session:
21
+ - `speaker_gate.mode n=1 mode=auto` at connect and after each applied mode command;
22
+ - `speaker_gate.final n=2 speaker=caller|other|unknown|none text="..."` for every final (`none` when the event has no verdict). This line includes the transcript, so treat these logs as call content;
23
+ - `speaker_gate.ignored n=3 utteranceId=<id> reason=speaker_mismatch` for each utterance `auto` mode drops;
24
+ - `speaker_gate.reset n=4` after an enrollment reset.
25
+ - A bystander's voice cannot change the mode: spoken commands are ignored when the verdict is `other`.
26
+
27
+ ## Switching the mode
28
+
29
+ Send a data-channel chat message, `{ "type": "chat", "text": "speaker-gate:auto" }`:
30
+
31
+ | Text | Effect |
32
+ | ----------------------- | ----------------------------------------------------------- |
33
+ | `speaker-gate:auto` | `setSpeakerGate(sessionId, { mode: "auto" })` |
34
+ | `speaker-gate:manual` | `setSpeakerGate(sessionId, { mode: "manual" })` |
35
+ | `speaker-gate:disabled` | `setSpeakerGate(sessionId, { mode: "disabled" })` |
36
+ | `speaker-gate:reset` | `resetSpeakerEnrollment(sessionId)`: learn the caller again |
37
+
38
+ Or say it: "only listen to me" switches to `auto`, "listen to everyone" switches to `manual`.
39
+
40
+ A switch applies from the next utterance. The enrolled caller is kept across switches, so going from `auto` to `disabled` and back does not enroll again.
41
+
42
+ ## When the gate is not available
43
+
44
+ `ctx.speakerGate.available` is `false` on older runners and on nodes without the speaker model. `setSpeakerGate` and `resetSpeakerEnrollment` then resolve `{ ok: false, reason: "not_available" }` and the agent hears every voice, as in `disabled`.
45
+
46
+ ## Sources
47
+
48
+ - `agent.ts` (`templates/speaker-gate/agent.ts`)
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Caller speaker gate: decide whose voice the agent listens to, and change that
3
+ * while the call stays up.
4
+ *
5
+ * Modes (set in the project's voice settings, switchable here with `setSpeakerGate`):
6
+ * disabled every final reaches the agent, no speaker verdict.
7
+ * auto only the enrolled caller's finals reach `onUserSpeechFinal`;
8
+ * a bystander arrives as `onUserSpeechIgnored` (no text).
9
+ * manual every final carries `speaker`: "caller" | "other" | "unknown",
10
+ * and this agent decides what to do with it.
11
+ *
12
+ * Switch from the data channel with a chat message (`speaker-gate:auto`,
13
+ * `speaker-gate:manual`, `speaker-gate:disabled`, `speaker-gate:reset`) or by
14
+ * voice ("only listen to me", "listen to everyone"). Each event writes one `speaker_gate.<kind> n=<n> ...` log line
15
+ * (the e2e smoke reads them; `final` lines include the transcript).
16
+ */
17
+
18
+ import {
19
+ agentLog,
20
+ defineAgent,
21
+ parseChatText,
22
+ resetSpeakerEnrollment,
23
+ setSpeakerGate,
24
+ speak,
25
+ type SpeakerGateMode,
26
+ type SpeakerVerdict,
27
+ } from "@voicethere/agent";
28
+
29
+ export type SpeakerGateCommand =
30
+ { kind: "mode"; mode: SpeakerGateMode } | { kind: "reset" };
31
+
32
+ /** Data-channel chat command: `speaker-gate:auto|manual|disabled|reset`. */
33
+ export function parseSpeakerGateCommand(
34
+ text: string,
35
+ ): SpeakerGateCommand | null {
36
+ const match = /^speaker-gate:(auto|manual|disabled|reset)$/i.exec(
37
+ text.trim(),
38
+ );
39
+ if (!match) return null;
40
+ const word = match[1].toLowerCase();
41
+ if (word === "reset") return { kind: "reset" };
42
+ return { kind: "mode", mode: word as SpeakerGateMode };
43
+ }
44
+
45
+ function normalizeSpoken(text: string): string {
46
+ return text
47
+ .toLowerCase()
48
+ .replace(/[^\p{L}\p{N}\s]/gu, " ")
49
+ .replace(/\s+/g, " ")
50
+ .trim();
51
+ }
52
+
53
+ /** Spoken command: "only listen to me" -> auto, "listen to everyone" -> manual. */
54
+ export function parseSpokenCommand(text: string): SpeakerGateCommand | null {
55
+ const phrase = normalizeSpoken(text);
56
+ if (phrase === "only listen to me") return { kind: "mode", mode: "auto" };
57
+ if (phrase === "listen to everyone") {
58
+ return { kind: "mode", mode: "manual" };
59
+ }
60
+ return null;
61
+ }
62
+
63
+ /** In manual mode the agent answers the caller and unverified speech, not `other`. */
64
+ export function shouldAnswer(speaker: SpeakerVerdict | undefined): boolean {
65
+ return speaker !== "other";
66
+ }
67
+
68
+ /** At most one "I heard someone else" line per session in this window. */
69
+ export const OTHER_NOTICE_INTERVAL_MS = 30_000;
70
+
71
+ const lastOtherNoticeAt = new Map<string, number>();
72
+
73
+ const counters = new Map<string, number>();
74
+
75
+ /** One log line per event; `n` counts up from 1 across all lines of a session. */
76
+ export function logLine(
77
+ sessionId: string,
78
+ kind: "mode" | "final" | "ignored" | "reset",
79
+ fields: string,
80
+ ): string {
81
+ const n = (counters.get(sessionId) ?? 0) + 1;
82
+ counters.set(sessionId, n);
83
+ const line = `speaker_gate.${kind} n=${n}${fields ? ` ${fields}` : ""}`;
84
+ agentLog("info", line, sessionId);
85
+ return line;
86
+ }
87
+
88
+ async function runCommand(
89
+ sessionId: string,
90
+ command: SpeakerGateCommand,
91
+ ): Promise<void> {
92
+ if (command.kind === "reset") {
93
+ const result = await resetSpeakerEnrollment(sessionId);
94
+ if (!result.ok) return;
95
+ logLine(sessionId, "reset", "");
96
+ speak(sessionId, "Okay, I will learn your voice again.");
97
+ return;
98
+ }
99
+ const result = await setSpeakerGate(sessionId, { mode: command.mode });
100
+ if (!result.ok) return;
101
+ logLine(sessionId, "mode", `mode=${result.mode}`);
102
+ speak(
103
+ sessionId,
104
+ result.mode === "auto"
105
+ ? "Okay, I only listen to you now."
106
+ : result.mode === "manual"
107
+ ? "Okay, I listen to everyone and tell voices apart."
108
+ : "Okay, I listen to every voice.",
109
+ );
110
+ }
111
+
112
+ defineAgent({
113
+ onSessionStart(ctx) {
114
+ logLine(
115
+ ctx.sessionId,
116
+ "mode",
117
+ `mode=${ctx.speakerGate?.mode ?? "disabled"}`,
118
+ );
119
+ },
120
+
121
+ async onUserSpeechFinal(ctx) {
122
+ logLine(
123
+ ctx.sessionId,
124
+ "final",
125
+ `speaker=${ctx.speaker ?? "none"} text=${JSON.stringify(ctx.text)}`,
126
+ );
127
+ const spoken = parseSpokenCommand(ctx.text);
128
+ // A bystander cannot change the mode in manual mode.
129
+ if (spoken && ctx.speaker !== "other") {
130
+ await runCommand(ctx.sessionId, spoken);
131
+ return;
132
+ }
133
+ if (!shouldAnswer(ctx.speaker)) {
134
+ const now = Date.now();
135
+ const last = lastOtherNoticeAt.get(ctx.sessionId) ?? 0;
136
+ if (now - last >= OTHER_NOTICE_INTERVAL_MS) {
137
+ lastOtherNoticeAt.set(ctx.sessionId, now);
138
+ speak(
139
+ ctx.sessionId,
140
+ "I heard someone else. I am listening to the caller.",
141
+ );
142
+ }
143
+ return;
144
+ }
145
+ speak(ctx.sessionId, `You said: ${ctx.text}`);
146
+ },
147
+
148
+ onUserSpeechIgnored(ctx) {
149
+ logLine(
150
+ ctx.sessionId,
151
+ "ignored",
152
+ `utteranceId=${ctx.utteranceId} reason=${ctx.reason}`,
153
+ );
154
+ },
155
+
156
+ async onDataChannelMessage(ctx) {
157
+ const text = parseChatText(ctx.message);
158
+ if (!text) return;
159
+ const command = parseSpeakerGateCommand(text);
160
+ if (!command) return;
161
+ await runCommand(ctx.sessionId, command);
162
+ },
163
+
164
+ onSessionEnd(ctx) {
165
+ lastOtherNoticeAt.delete(ctx.sessionId);
166
+ counters.delete(ctx.sessionId);
167
+ },
168
+ });