Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(live): change the Fish agent's language one call start at a time

AdityaUmalec2e876c0cd71
diff --git a/server/live-session-wiring.test.ts b/server/live-session-wiring.test.ts
index ccd1468c07..322c19f143 100644
--- a/server/live-session-wiring.test.ts
+++ b/server/live-session-wiring.test.ts
@@ -85,6 +85,29 @@ describe("a Fish Audio call's session", () => {
expect(moved.saveAgent).not.toHaveBeenCalled();
});
+ it("changes the agent's language one call start at a time, so the app's latest language is the one left saved", async () => {
+ let releaseFirst!: () => void;
+ const firstHeld = new Promise<void>((resolve) => { releaseFirst = resolve; });
+ const fish = fakeFish();
+ let patches = 0;
+ const fetchImpl = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {
+ if (init!.method === "PATCH" && patches++ === 0) await firstHeld;
+ return fish(url, init);
+ });
+ const speech: { language?: string; fishAgentLanguage?: string } = { language: "de", fishAgentLanguage: "en" };
+ const w = wiring(fetchImpl as unknown as typeof fetch, {}, speech);
+ w.saveAgent.mockImplementation((agent: { fishAgentLanguage: string }) => { speech.fishAgentLanguage = agent.fishAgentLanguage; });
+ const endpoint = fishLiveEndpoint("agent_saved", {});
+ const first = w.calls.createSession({ ...start, endpoint });
+ speech.language = "fr";
+ const second = w.calls.createSession({ ...start, endpoint });
+ releaseFirst();
+ await Promise.all([first, second]);
+ const languages = fish.mock.calls.filter(([, init]) => init!.method === "PATCH").map(([, init]) => JSON.parse(String(init!.body)).voice.speaking_language);
+ expect(languages).toEqual(["de", "fr"]);
+ expect(speech.fishAgentLanguage).toBe("fr");
+ });
+
it("still starts the call when Fish won't change the agent's language", async () => {
const fetchImpl = fakeFish({ refuseConfig: true });
const w = wiring(fetchImpl, {}, { language: "vi" });
diff --git a/server/live-session-wiring.ts b/server/live-session-wiring.ts
index e1d184f6eb..936498d8aa 100644
--- a/server/live-session-wiring.ts
+++ b/server/live-session-wiring.ts
@@ -46,14 +46,20 @@ export function liveSessionWiring(input: LiveSessionWiringInput): {
};
// Moves the saved agent the harness set up to the app's language; if Fish refuses, the call goes ahead in the old one.
- const speakLanguage = async (key: string, endpoint: LiveEndpoint, language: string): Promise<void> => {
- if (operatorAgent() || (input.fishAgentLanguage?.() ?? "en") === language) return;
- try {
- await setFishAgentLanguage(key, endpoint, endpoint.model, language, fetchImpl);
- } catch {
- return;
- }
- input.saveAgent({ fishAgentId: endpoint.model, fishAgentFor: fishKeyPrint(key), fishAgentLanguage: language });
+ // One change at a time, even past a cancelled call start, so a slower earlier start never saves over a later one.
+ let languageChange: Promise<void> = Promise.resolve();
+ const speakLanguage = (key: string, endpoint: LiveEndpoint, language: string): Promise<void> => {
+ const change = languageChange.then(async () => {
+ if (operatorAgent() || (input.fishAgentLanguage?.() ?? "en") === language) return;
+ try {
+ await setFishAgentLanguage(key, endpoint, endpoint.model, language, fetchImpl);
+ } catch {
+ return;
+ }
+ input.saveAgent({ fishAgentId: endpoint.model, fishAgentFor: fishKeyPrint(key), fishAgentLanguage: language });
+ });
+ languageChange = change.catch(() => undefined);
+ return change;
};
const startFish = async (key: string, endpoint: LiveEndpoint, botId: string, threadId: string) => {