@voicethere/agent 0.35.0 → 0.35.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.
@@ -60813,13 +60813,18 @@ function withLanguageNote(messages, code) {
60813
60813
  return messages;
60814
60814
  }
60815
60815
  var NOTICE_TURN_PREFIX = "lang-notice-";
60816
+ function replyLanguage(state2) {
60817
+ return state2?.turn ?? state2?.current ?? DEFAULT_LANGUAGE;
60818
+ }
60816
60819
  function languageSwitchAgent(create) {
60817
60820
  const sessions = /* @__PURE__ */ new Map();
60818
60821
  const llm = create({
60819
60822
  onBeforeLlmCall(messages, ctx) {
60820
60823
  if (ctx.source === "chat") return;
60821
- const code = sessions.get(ctx.sessionId)?.current ?? DEFAULT_LANGUAGE;
60822
- return withLanguageNote(messages, code);
60824
+ return withLanguageNote(
60825
+ messages,
60826
+ replyLanguage(sessions.get(ctx.sessionId))
60827
+ );
60823
60828
  }
60824
60829
  });
60825
60830
  return {
@@ -60849,7 +60854,10 @@ function languageSwitchAgent(create) {
60849
60854
  supported
60850
60855
  });
60851
60856
  const state2 = sessions.get(ctx.sessionId);
60852
- if (state2) state2.unsupported = supported ? void 0 : language;
60857
+ if (state2) {
60858
+ state2.unsupported = supported ? void 0 : language;
60859
+ state2.detected = supported ? language : void 0;
60860
+ }
60853
60861
  },
60854
60862
  onVoiceLanguageChanged(ctx) {
60855
60863
  const language = normalizeLanguage(ctx.language);
@@ -60858,11 +60866,16 @@ function languageSwitchAgent(create) {
60858
60866
  if (state2) {
60859
60867
  state2.current = language;
60860
60868
  state2.unsupported = void 0;
60869
+ state2.detected = void 0;
60870
+ state2.turn = void 0;
60861
60871
  }
60862
60872
  sendToClient(ctx.sessionId, { type: "language", language });
60863
60873
  },
60864
60874
  onUserSpeechFinal(ctx) {
60865
60875
  const state2 = sessions.get(ctx.sessionId);
60876
+ if (state2) {
60877
+ state2.turn = ctx.replay ? state2.detected ?? state2.current : void 0;
60878
+ }
60866
60879
  if (state2?.unsupported && !ctx.replay) {
60867
60880
  const text = unsupportedNotice(state2.unsupported, state2.current);
60868
60881
  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.1. 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.1",
4
4
  "description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -14,8 +14,8 @@ 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
20
  - Typed chat messages are answered in the language they are written in (the prompt says so); chat does not change the voice.
21
21
 
@@ -119,6 +119,20 @@ interface SessionLanguage {
119
119
  current: string;
120
120
  /** Detected language outside the list, waiting for its final. */
121
121
  unsupported?: string;
122
+ /** Last detected language in the list, until it is committed. */
123
+ detected?: string;
124
+ /**
125
+ * Language of the turn the LLM is answering now. Set by a replayed final:
126
+ * the runner replays only after it has swapped to the detected language,
127
+ * and it sends `voice_language_changed` after the replay, so the commit can
128
+ * arrive after the LLM call starts.
129
+ */
130
+ turn?: string;
131
+ }
132
+
133
+ /** The language the LLM is told for the current turn. */
134
+ function replyLanguage(state: SessionLanguage | undefined): string {
135
+ return state?.turn ?? state?.current ?? DEFAULT_LANGUAGE;
122
136
  }
123
137
 
124
138
  /**
@@ -134,8 +148,10 @@ export function languageSwitchAgent(
134
148
  onBeforeLlmCall(messages, ctx) {
135
149
  // Typed chat is answered in the language it is written in.
136
150
  if (ctx.source === "chat") return;
137
- const code = sessions.get(ctx.sessionId)?.current ?? DEFAULT_LANGUAGE;
138
- return withLanguageNote(messages, code);
151
+ return withLanguageNote(
152
+ messages,
153
+ replyLanguage(sessions.get(ctx.sessionId)),
154
+ );
139
155
  },
140
156
  });
141
157
 
@@ -168,7 +184,10 @@ export function languageSwitchAgent(
168
184
  supported,
169
185
  });
170
186
  const state = sessions.get(ctx.sessionId);
171
- if (state) state.unsupported = supported ? undefined : language;
187
+ if (state) {
188
+ state.unsupported = supported ? undefined : language;
189
+ state.detected = supported ? language : undefined;
190
+ }
172
191
  },
173
192
 
174
193
  onVoiceLanguageChanged(ctx) {
@@ -178,12 +197,20 @@ export function languageSwitchAgent(
178
197
  if (state) {
179
198
  state.current = language;
180
199
  state.unsupported = undefined;
200
+ state.detected = undefined;
201
+ state.turn = undefined;
181
202
  }
182
203
  sendToClient(ctx.sessionId, { type: "language", language });
183
204
  },
184
205
 
185
206
  onUserSpeechFinal(ctx) {
186
207
  const state = sessions.get(ctx.sessionId);
208
+ if (state) {
209
+ // A replayed final is the switching utterance re-decoded by the new
210
+ // language's STT; the commit follows it. Any other final is answered
211
+ // in the committed language.
212
+ state.turn = ctx.replay ? (state.detected ?? state.current) : undefined;
213
+ }
187
214
  if (state?.unsupported && !ctx.replay) {
188
215
  // The transcript came from the wrong language's STT: do not ask the
189
216
  // LLM, say what was heard in the current language instead.