Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
fix(voice-runtime): keep the spoken language when a document changes language
muterCcc1a1e31304d
diff --git a/apps/voice-runtime/src/live-worker.test.ts b/apps/voice-runtime/src/live-worker.test.tsindex a2330798..98093874 100644--- a/apps/voice-runtime/src/live-worker.test.ts+++ b/apps/voice-runtime/src/live-worker.test.ts@@ -112,6 +112,7 @@ vi.mock("@livekit/rtc-node", () => ({import { LiveSessionError, type LiveEvent } from "./live-session.js";import {+ afterCall,liveDataTopic,liveReplyEventId,liveTurnId,@@ -406,6 +407,17 @@ async function startTask(test: Test, goal = "Research three vendors' pricing") {);}+describe("afterCall", () => {+ it("keeps the words after a tool call apart from the sentence before it", () => {+ expect(+ afterCall("Switching everything to English.", "All English now"),+ ).toBe(" All English now");+ expect(afterCall("改好了。", "主题是周五会议")).toBe("主题是周五会议");+ expect(afterCall("", "Done.")).toBe("Done.");+ expect(afterCall("Done. ", "Next")).toBe("Next");+ });+});+describe("parseLiveClientEvent", () => {const encode = (value: unknown) =>new TextEncoder().encode(JSON.stringify(value));diff --git a/apps/voice-runtime/src/live-worker.ts b/apps/voice-runtime/src/live-worker.tsindex 847f90a2..9cd007c7 100644--- a/apps/voice-runtime/src/live-worker.ts+++ b/apps/voice-runtime/src/live-worker.ts@@ -197,6 +197,8 @@ interface Reply {endRequested?: boolean;/** Tool results not yet returned; the reply continues after them. */toolsPending: number;+ /** A tool was called; the next words start a new sentence after it. */+ called?: boolean;interrupted: boolean;/** A pause of it ended unconfirmed; only server VAD stops it now. */pauseDeclined?: boolean;@@ -789,23 +791,31 @@ export class LiveVoiceWorker {if (!reply || reply.interrupted || !event.text) return;reply.outputStarted = true;const turn = reply.turn;+ let text = event.text;+ if (reply.called) {+ reply.called = false;+ text = afterCall(+ reply.speech ? (reply.spokenText ?? "") : turn.text,+ text,+ );+ }if (reply.speech) {- reply.spokenText = (reply.spokenText ?? "") + event.text;+ reply.spokenText = (reply.spokenText ?? "") + text;if (reply.speechStarted)this.publish({type: "reply_delta",turn_id: reply.speech.turnId,speech_id: reply.speech.speechId,- text: event.text,+ text,});return;}turn.firstTextAt ??= Date.now();- turn.text += event.text;+ turn.text += text;this.publish({type: "reply_delta",turn_id: turn.id,- text: event.text,+ text,});return;}@@ -826,6 +836,7 @@ export class LiveVoiceWorker {: this.replyFor(false);if (!reply) return;reply.toolsPending += 1;+ reply.called = true;const calledAt = Date.now();let answered = true;this.track(@@ -2396,3 +2407,14 @@ export function withoutRepeat(said: string, text: string) {const kept = text.slice(tokens[cut - 1]!.end).replace(/^[\s\p{P}]+/u, "");return kept || text;}++/**+ * The words said after a tool call come from a new provider response that+ * starts right after the sentence before the call ("…to English.All English+ * now"); a space keeps them apart where a Latin word or number follows.+ */+export function afterCall(before: string, text: string) {+ if (!before || /\s$/.test(before) || !/^[\p{Script=Latin}\p{N}]/u.test(text))+ return text;+ return ` ${text}`;+}diff --git a/apps/voice-runtime/src/realtime.test.ts b/apps/voice-runtime/src/realtime.test.tsindex 62092f21..d216b0b7 100644--- a/apps/voice-runtime/src/realtime.test.ts+++ b/apps/voice-runtime/src/realtime.test.ts@@ -49,6 +49,9 @@ describe("realtime session rules", () => {expect(rules).toContain("\n- LANGUAGE: Speak only the language of the user's latest words.",);+ expect(rules).toContain(+ "changes only that text, never the language you speak",+ );expect(rules).toContain("\n- LENGTH: ");expect(rules.indexOf("## Conversation Flow")).toBeLessThan(rules.indexOf("## CRITICAL INSTRUCTIONS"),diff --git a/apps/voice-runtime/src/realtime.ts b/apps/voice-runtime/src/realtime.tsindex 1a86f5bf..c2fce22e 100644--- a/apps/voice-runtime/src/realtime.ts+++ b/apps/voice-runtime/src/realtime.ts@@ -32,7 +32,7 @@ export function historyPairs(* language these instructions, tools and results happen to be written in.*/const languageRule =- "LANGUAGE: Speak only the language of the user's latest words. When they speak Chinese, even with a few English words in it, every word you say is Chinese, except names of people, companies, products and apps: say an English word the user or a result used by its Chinese equivalent (the Chinese for deadline, follow-up, job offer, meeting, update, draft, onboarding), restate any message, email or reminder you are about to send in Chinese, translate English results, meeting names and titles into Chinese, and read numbers, dates and versions the Chinese way. When they speak English, answer in English, with no word of another language in it. Titles and text you write into a document, email or note are in the user's language too, unless they ask for another. The language of these instructions, the tools and the results never decides the language you speak, and your reasoning is never spoken.";+ "LANGUAGE: Speak only the language of the user's latest words. When they speak Chinese, even with a few English words in it, every word you say is Chinese, except names of people, companies, products and apps: say an English word the user or a result used by its Chinese equivalent (the Chinese for deadline, follow-up, job offer, meeting, update, draft, onboarding), restate any message, email or reminder you are about to send in Chinese, translate English results, meeting names and titles into Chinese, and read numbers, dates and versions the Chinese way. When they speak English, answer in English, with no word of another language in it. Titles and text you write into a document, email or note are in the user's language too, unless they ask for another; asking for text in another language, or for a translation, changes only that text, never the language you speak. The language of these instructions, the tools and the results never decides the language you speak, and your reasoning is never spoken.";/** Said first and last, right after the language: how long one answer is. */const lengthRule =