@voicethere/agent 0.35.0 → 0.35.2

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.
@@ -60799,7 +60799,13 @@ 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
+ function withChatNote(messages) {
60806
+ return withNote(messages, CHAT_NOTE);
60807
+ }
60808
+ function withNote(messages, note) {
60803
60809
  for (let i = messages.length - 1; i >= 0; i -= 1) {
60804
60810
  const m = messages[i];
60805
60811
  if (m.role === "user") {
@@ -60813,13 +60819,18 @@ function withLanguageNote(messages, code) {
60813
60819
  return messages;
60814
60820
  }
60815
60821
  var NOTICE_TURN_PREFIX = "lang-notice-";
60822
+ function replyLanguage(state2) {
60823
+ return state2?.turn ?? state2?.current ?? DEFAULT_LANGUAGE;
60824
+ }
60816
60825
  function languageSwitchAgent(create) {
60817
60826
  const sessions = /* @__PURE__ */ new Map();
60818
60827
  const llm = create({
60819
60828
  onBeforeLlmCall(messages, ctx) {
60820
- if (ctx.source === "chat") return;
60821
- const code = sessions.get(ctx.sessionId)?.current ?? DEFAULT_LANGUAGE;
60822
- return withLanguageNote(messages, code);
60829
+ if (ctx.source === "chat") return withChatNote(messages);
60830
+ return withLanguageNote(
60831
+ messages,
60832
+ replyLanguage(sessions.get(ctx.sessionId))
60833
+ );
60823
60834
  }
60824
60835
  });
60825
60836
  return {
@@ -60849,7 +60860,10 @@ function languageSwitchAgent(create) {
60849
60860
  supported
60850
60861
  });
60851
60862
  const state2 = sessions.get(ctx.sessionId);
60852
- if (state2) state2.unsupported = supported ? void 0 : language;
60863
+ if (state2) {
60864
+ state2.unsupported = supported ? void 0 : language;
60865
+ state2.detected = supported ? language : void 0;
60866
+ }
60853
60867
  },
60854
60868
  onVoiceLanguageChanged(ctx) {
60855
60869
  const language = normalizeLanguage(ctx.language);
@@ -60858,11 +60872,16 @@ function languageSwitchAgent(create) {
60858
60872
  if (state2) {
60859
60873
  state2.current = language;
60860
60874
  state2.unsupported = void 0;
60875
+ state2.detected = void 0;
60876
+ state2.turn = void 0;
60861
60877
  }
60862
60878
  sendToClient(ctx.sessionId, { type: "language", language });
60863
60879
  },
60864
60880
  onUserSpeechFinal(ctx) {
60865
60881
  const state2 = sessions.get(ctx.sessionId);
60882
+ if (state2) {
60883
+ state2.turn = ctx.replay ? state2.detected ?? state2.current : void 0;
60884
+ }
60866
60885
  if (state2?.unsupported && !ctx.replay) {
60867
60886
  const text = unsupportedNotice(state2.unsupported, state2.current);
60868
60887
  state2.unsupported = void 0;
package/eject/llm/loop.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copied from @voicethere/agent 0.35.0. Package updates no longer change this file.
1
+ // Copied from @voicethere/agent 0.35.2. 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.0",
3
+ "version": "0.35.2",
4
4
  "description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -14,10 +14,10 @@ A small travel-help assistant that follows the caller when they change language
14
14
 
15
15
  The runner owns the switch. The agent never calls `setVoiceLanguage`.
16
16
 
17
- - `onVoiceLanguageChanged` records the committed language and sends `{ "type": "language", "language": "de" }` to the client. Only a committed language changes what the LLM is told. 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
- - `onUserLanguage` only reports what was heard: `{ "type": "language_detected", "language": "it", "supported": false }`. The runner may still reject a detection, so the LLM note does not change on it.
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
+ - `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 adds a one-line note to the chat message, 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
 
@@ -99,7 +99,19 @@ 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 one line that tells the LLM to answer a typed message in its own language. */
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
+ /** Same as `withLanguageNote`, for a typed chat message. */
110
+ export function withChatNote(messages: LlmMessage[]): LlmMessage[] {
111
+ return withNote(messages, CHAT_NOTE);
112
+ }
113
+
114
+ function withNote(messages: LlmMessage[], note: string): LlmMessage[] {
103
115
  for (let i = messages.length - 1; i >= 0; i -= 1) {
104
116
  const m = messages[i]!;
105
117
  if (m.role === "user") {
@@ -119,6 +131,20 @@ interface SessionLanguage {
119
131
  current: string;
120
132
  /** Detected language outside the list, waiting for its final. */
121
133
  unsupported?: string;
134
+ /** Last detected language in the list, until it is committed. */
135
+ detected?: string;
136
+ /**
137
+ * Language of the turn the LLM is answering now. Set by a replayed final:
138
+ * the runner replays only after it has swapped to the detected language,
139
+ * and it sends `voice_language_changed` after the replay, so the commit can
140
+ * arrive after the LLM call starts.
141
+ */
142
+ turn?: string;
143
+ }
144
+
145
+ /** The language the LLM is told for the current turn. */
146
+ function replyLanguage(state: SessionLanguage | undefined): string {
147
+ return state?.turn ?? state?.current ?? DEFAULT_LANGUAGE;
122
148
  }
123
149
 
124
150
  /**
@@ -132,10 +158,13 @@ export function languageSwitchAgent(
132
158
 
133
159
  const llm = create({
134
160
  onBeforeLlmCall(messages, ctx) {
135
- // Typed chat is answered in the language it is written in.
136
- if (ctx.source === "chat") return;
137
- const code = sessions.get(ctx.sessionId)?.current ?? DEFAULT_LANGUAGE;
138
- return withLanguageNote(messages, code);
161
+ // Typed chat is answered in the language it is written in, not the
162
+ // call's voice language (the history may be in another language).
163
+ if (ctx.source === "chat") return withChatNote(messages);
164
+ return withLanguageNote(
165
+ messages,
166
+ replyLanguage(sessions.get(ctx.sessionId)),
167
+ );
139
168
  },
140
169
  });
141
170
 
@@ -168,7 +197,10 @@ export function languageSwitchAgent(
168
197
  supported,
169
198
  });
170
199
  const state = sessions.get(ctx.sessionId);
171
- if (state) state.unsupported = supported ? undefined : language;
200
+ if (state) {
201
+ state.unsupported = supported ? undefined : language;
202
+ state.detected = supported ? language : undefined;
203
+ }
172
204
  },
173
205
 
174
206
  onVoiceLanguageChanged(ctx) {
@@ -178,12 +210,20 @@ export function languageSwitchAgent(
178
210
  if (state) {
179
211
  state.current = language;
180
212
  state.unsupported = undefined;
213
+ state.detected = undefined;
214
+ state.turn = undefined;
181
215
  }
182
216
  sendToClient(ctx.sessionId, { type: "language", language });
183
217
  },
184
218
 
185
219
  onUserSpeechFinal(ctx) {
186
220
  const state = sessions.get(ctx.sessionId);
221
+ if (state) {
222
+ // A replayed final is the switching utterance re-decoded by the new
223
+ // language's STT; the commit follows it. Any other final is answered
224
+ // in the committed language.
225
+ state.turn = ctx.replay ? (state.detected ?? state.current) : undefined;
226
+ }
187
227
  if (state?.unsupported && !ctx.replay) {
188
228
  // The transcript came from the wrong language's STT: do not ask the
189
229
  // LLM, say what was heard in the current language instead.