@voicethere/agent 0.35.1 → 0.35.3

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.
@@ -60725,7 +60725,7 @@ var voicethere_llm_default = {
60725
60725
  $schema: "./node_modules/@voicethere/agent/schema/voicethere.llm.schema.json",
60726
60726
  version: 1,
60727
60727
  provider: "openrouter",
60728
- model: "amazon/nova-micro-v1",
60728
+ model: "anthropic/claude-haiku-5.5",
60729
60729
  apiKeySecret: "AGENT_OPENROUTER_API_KEY",
60730
60730
  maxOutputTokens: 256,
60731
60731
  greeting: {
@@ -60799,7 +60799,57 @@ function unsupportedNotice(heard, current) {
60799
60799
  return `${text.heard(languageName(heard, lang))} ${text.can(list)}`;
60800
60800
  }
60801
60801
  function withLanguageNote(messages, code) {
60802
- const note = languageNote(code);
60802
+ return withNote(messages, languageNote(code));
60803
+ }
60804
+ var CHAT_NOTE = "Reply in the language this message is written in, even if the earlier conversation used another language.";
60805
+ var STOPWORDS = {
60806
+ en: "the is are what where how can you your i my to of and for with from this that it do does please".split(
60807
+ " "
60808
+ ),
60809
+ de: "der die das ist sind wo wie kann ich du sie ein eine und nicht mit von zu bitte wann welche auf nach".split(
60810
+ " "
60811
+ ),
60812
+ es: "el los las es son d\xF3nde donde c\xF3mo como puedo puede yo t\xFA un una y no con por para hola desde al qu\xE9 que cu\xE1l".split(
60813
+ " "
60814
+ ),
60815
+ fr: "le les est sont o\xF9 ou comment puis je tu vous un une et pas avec pour bonjour depuis au quel quelle que qu'est-ce".split(
60816
+ " "
60817
+ )
60818
+ };
60819
+ var MARKS = {
60820
+ es: "\xBF\xA1\xF1",
60821
+ de: "\xDF\xE4\xF6\xFC",
60822
+ fr: "\xE7\xE0\xE8\xF9\xEA\xEE\xF4\xFB"
60823
+ };
60824
+ function guessChatLanguage(text) {
60825
+ const words = text.toLowerCase().match(/[\p{L}'-]+/gu) ?? [];
60826
+ const scores = SUPPORTED_LANGUAGES.map((code) => {
60827
+ const stop = new Set(STOPWORDS[code] ?? []);
60828
+ let hits = words.filter((w) => stop.has(w)).length;
60829
+ for (const mark of MARKS[code] ?? "") {
60830
+ if (text.toLowerCase().includes(mark)) hits += 1;
60831
+ }
60832
+ return { code, hits };
60833
+ }).sort((a, b) => b.hits - a.hits);
60834
+ const best = scores[0];
60835
+ const next = scores[1];
60836
+ if (!best || best.hits < 2 || best.hits - (next?.hits ?? 0) < 2) {
60837
+ return void 0;
60838
+ }
60839
+ return best.code;
60840
+ }
60841
+ function chatLanguageNote(code) {
60842
+ const name = languageName(code, "en");
60843
+ return `The caller typed this message in ${name} (${code}). Reply only in ${name}.`;
60844
+ }
60845
+ var INTERRUPTION_NOTE = /^\(The caller interrupted your previous reply after: [\s\S]*?\.\)\n/;
60846
+ function withChatNote(messages) {
60847
+ const last = [...messages].reverse().find((m) => m.role === "user");
60848
+ const typed = last?.text.replace(INTERRUPTION_NOTE, "");
60849
+ const code = typed ? guessChatLanguage(typed) : void 0;
60850
+ return withNote(messages, code ? chatLanguageNote(code) : CHAT_NOTE);
60851
+ }
60852
+ function withNote(messages, note) {
60803
60853
  for (let i = messages.length - 1; i >= 0; i -= 1) {
60804
60854
  const m = messages[i];
60805
60855
  if (m.role === "user") {
@@ -60820,7 +60870,7 @@ function languageSwitchAgent(create) {
60820
60870
  const sessions = /* @__PURE__ */ new Map();
60821
60871
  const llm = create({
60822
60872
  onBeforeLlmCall(messages, ctx) {
60823
- if (ctx.source === "chat") return;
60873
+ if (ctx.source === "chat") return withChatNote(messages);
60824
60874
  return withLanguageNote(
60825
60875
  messages,
60826
60876
  replyLanguage(sessions.get(ctx.sessionId))
package/eject/llm/loop.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copied from @voicethere/agent 0.35.1. Package updates no longer change this file.
1
+ // Copied from @voicethere/agent 0.35.3. Package updates no longer change this file.
2
2
  import { randomUUID } from "node:crypto";
3
3
  import {
4
4
  type AgentHandlers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicethere/agent",
3
- "version": "0.35.1",
3
+ "version": "0.35.3",
4
4
  "description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,13 +17,13 @@ The runner owns the switch. The agent never calls `setVoiceLanguage`.
17
17
  - `onVoiceLanguageChanged` records the committed language and sends `{ "type": "language", "language": "de" }` to the client. The runner sends it after it has replayed the switching utterance, so the replayed final (`replay: true`) is answered in the language detected for it; every other final is answered in the committed language. The agent appends one line ("The caller now speaks German (de). Reply only in German.") to a copy of the last caller message on every LLM call, so it never piles up in the history.
18
18
  - `onUserLanguage` only reports what was heard: `{ "type": "language_detected", "language": "it", "supported": false }`. The runner may still reject a detection, so a detection alone does not change the LLM note: only the replay that follows a switch uses it.
19
19
  - When the detected language is not in `SUPPORTED_LANGUAGES`, the runner does not switch and the transcript comes from the wrong language's speech model. The agent skips the LLM for that utterance and says, in the current language, "I heard Italian. I can talk with you in English, German, Spanish or French." That notice is not added to the LLM history.
20
- - Typed chat messages are answered in the language they are written in (the prompt says so); chat does not change the voice.
20
+ - Typed chat messages are answered in the language they are written in (the agent guesses the language from common words and characters, limited to the supported languages, and adds a one-line note naming it to the chat message, for example "The caller typed this message in Spanish (es). Reply only in Spanish."; when the text is short or mixed it adds a general "reply in the language of this message" note instead. The note is not stored in the history, so the reply does not follow the voice language); chat does not change the voice.
21
21
 
22
22
  ## Files
23
23
 
24
24
  - `agent.ts` wires `createLlmAgent` into `languageSwitchAgent`.
25
25
  - `language.ts` holds `SUPPORTED_LANGUAGES`, the language note and the unsupported-language notice. Keep `SUPPORTED_LANGUAGES` equal to the project's `voice.allowedLanguages`.
26
- - `voicethere.llm.json` holds the model, greeting and limits.
26
+ - `voicethere.llm.json` holds the model (`anthropic/claude-haiku-5.5` on OpenRouter), greeting and limits.
27
27
  - `prompt.md` is the system prompt.
28
28
  - `md.d.ts` lets TypeScript accept `import x from "./x.md"`.
29
29
 
@@ -99,7 +99,84 @@ export function withLanguageNote(
99
99
  messages: LlmMessage[],
100
100
  code: string,
101
101
  ): LlmMessage[] {
102
- const note = languageNote(code);
102
+ return withNote(messages, languageNote(code));
103
+ }
104
+
105
+ /** The fallback line when the language of a typed message is not clear. */
106
+ export const CHAT_NOTE =
107
+ "Reply in the language this message is written in, even if the earlier conversation used another language.";
108
+
109
+ /** Common words per language; ambiguous ones (`de`, `la`, `en`, `a`) are left out. */
110
+ const STOPWORDS: Record<string, readonly string[]> = {
111
+ en: "the is are what where how can you your i my to of and for with from this that it do does please".split(
112
+ " ",
113
+ ),
114
+ de: "der die das ist sind wo wie kann ich du sie ein eine und nicht mit von zu bitte wann welche auf nach".split(
115
+ " ",
116
+ ),
117
+ es: "el los las es son dónde donde cómo como puedo puede yo tú un una y no con por para hola desde al qué que cuál".split(
118
+ " ",
119
+ ),
120
+ fr: "le les est sont où ou comment puis je tu vous un une et pas avec pour bonjour depuis au quel quelle que qu'est-ce".split(
121
+ " ",
122
+ ),
123
+ };
124
+
125
+ /** Characters that mark one language among the supported ones. */
126
+ const MARKS: Record<string, string> = {
127
+ es: "¿¡ñ",
128
+ de: "ßäöü",
129
+ fr: "çàèùêîôû",
130
+ };
131
+
132
+ /**
133
+ * Guesses the language of a typed message from stopwords and marks, limited
134
+ * to `SUPPORTED_LANGUAGES`. Returns a code only when the best language has at
135
+ * least 2 hits and leads the next one by at least 2; short, mixed or unknown
136
+ * text returns undefined. A guess only words the chat note: it never changes
137
+ * the session language or the voice.
138
+ */
139
+ export function guessChatLanguage(text: string): string | undefined {
140
+ const words = text.toLowerCase().match(/[\p{L}'-]+/gu) ?? [];
141
+ const scores = SUPPORTED_LANGUAGES.map((code) => {
142
+ const stop = new Set(STOPWORDS[code] ?? []);
143
+ let hits = words.filter((w) => stop.has(w)).length;
144
+ for (const mark of MARKS[code] ?? "") {
145
+ if (text.toLowerCase().includes(mark)) hits += 1;
146
+ }
147
+ return { code, hits };
148
+ }).sort((a, b) => b.hits - a.hits);
149
+ const best = scores[0];
150
+ const next = scores[1];
151
+ if (!best || best.hits < 2 || best.hits - (next?.hits ?? 0) < 2) {
152
+ return undefined;
153
+ }
154
+ return best.code;
155
+ }
156
+
157
+ /** The chat note naming the language of a typed message. */
158
+ export function chatLanguageNote(code: string): string {
159
+ const name = languageName(code, "en");
160
+ return `The caller typed this message in ${name} (${code}). Reply only in ${name}.`;
161
+ }
162
+
163
+ const INTERRUPTION_NOTE =
164
+ /^\(The caller interrupted your previous reply after: [\s\S]*?\.\)\n/;
165
+
166
+ /**
167
+ * Same as `withLanguageNote`, for a typed chat message: names the guessed
168
+ * language, or falls back to `CHAT_NOTE` when the guess is not clear.
169
+ */
170
+ export function withChatNote(messages: LlmMessage[]): LlmMessage[] {
171
+ const last = [...messages].reverse().find((m) => m.role === "user");
172
+ // The runtime may put its English interruption note before the message;
173
+ // it is not the caller's text and must not count towards the guess.
174
+ const typed = last?.text.replace(INTERRUPTION_NOTE, "");
175
+ const code = typed ? guessChatLanguage(typed) : undefined;
176
+ return withNote(messages, code ? chatLanguageNote(code) : CHAT_NOTE);
177
+ }
178
+
179
+ function withNote(messages: LlmMessage[], note: string): LlmMessage[] {
103
180
  for (let i = messages.length - 1; i >= 0; i -= 1) {
104
181
  const m = messages[i]!;
105
182
  if (m.role === "user") {
@@ -146,8 +223,9 @@ export function languageSwitchAgent(
146
223
 
147
224
  const llm = create({
148
225
  onBeforeLlmCall(messages, ctx) {
149
- // Typed chat is answered in the language it is written in.
150
- if (ctx.source === "chat") return;
226
+ // Typed chat is answered in the language it is written in, not the
227
+ // call's voice language (the history may be in another language).
228
+ if (ctx.source === "chat") return withChatNote(messages);
151
229
  return withLanguageNote(
152
230
  messages,
153
231
  replyLanguage(sessions.get(ctx.sessionId)),
@@ -2,7 +2,7 @@
2
2
  "$schema": "./node_modules/@voicethere/agent/schema/voicethere.llm.schema.json",
3
3
  "version": 1,
4
4
  "provider": "openrouter",
5
- "model": "amazon/nova-micro-v1",
5
+ "model": "anthropic/claude-haiku-5.5",
6
6
  "apiKeySecret": "AGENT_OPENROUTER_API_KEY",
7
7
  "maxOutputTokens": 256,
8
8
  "greeting": {