Skip to content

Commit a96c29b

Browse files
committed
fix(mcp): read the project again when a waiting transcription starts
Part of #1180
1 parent 73827d7 commit a96c29b

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎electron/mcp/openscreen-mcp-server.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,20 @@ export function createToolRunner(deps: McpToolDeps): McpToolRunner {
682682
}
683683
const whisper = deps.transcribe;
684684
if (!whisper) return textResult("Transcription is not available here.", true);
685-
const started = whisperQueue.then(() =>
686-
whisper({
687-
sourcePath: asset.originalPath,
685+
const started = whisperQueue.then(async () => {
686+
// Read again once it is this call's turn: a vocabulary set while it waited counts.
687+
const current = await deps.projects
688+
.getProjectForUpdate(targetId)
689+
.then((read) => read.document)
690+
.catch(() => document);
691+
const media = current.assets.find((a) => a.id === asset.id);
692+
if (!media) throw new Error(`Asset ${asset.id} was removed from the project.`);
693+
return whisper({
694+
sourcePath: media.originalPath,
688695
language: language === "auto" ? undefined : language,
689-
prompt: document.project.vocabulary,
690-
}),
691-
);
696+
prompt: current.project.vocabulary,
697+
});
698+
});
692699
whisperQueue = started.catch(() => undefined);
693700
let response: SttTranscribeResponse;
694701
try {

‎electron/mcp/transcribe-tool.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ describe("transcribe, on a project's file", () => {
169169
expect((await pending).isError).toBeUndefined();
170170
});
171171

172+
it("primes a waiting transcription with the vocabulary set while it waited", async () => {
173+
await projects.saveProject(fixtureDocument("proj_1"));
174+
const running: Array<(response: SttTranscribeResponse) => void> = [];
175+
const { run, transcribe } = runnerWith(
176+
() =>
177+
new Promise((resolve) => {
178+
running.push(resolve);
179+
}),
180+
);
181+
const first = run(TRANSCRIBE_TOOL, { projectId: "proj_1" });
182+
await vi.waitFor(() => expect(running).toHaveLength(1));
183+
const second = run(TRANSCRIBE_TOOL, { projectId: "proj_1", language: "fr" });
184+
await run("createCheckpoint", { projectId: "proj_1" });
185+
await run("setVocabulary", { projectId: "proj_1", vocabulary: "Vulkan" });
186+
running.shift()?.(SPEECH);
187+
await first;
188+
await vi.waitFor(() => expect(running).toHaveLength(1));
189+
expect(transcribe).toHaveBeenLastCalledWith(expect.objectContaining({ prompt: "Vulkan" }));
190+
running.shift()?.(SPEECH);
191+
await second;
192+
});
193+
172194
it("survives a restore to a checkpoint made before it", async () => {
173195
await projects.saveProject(fixtureDocument("proj_1"));
174196
const { run } = runnerWith(async () => SPEECH);

0 commit comments

Comments
 (0)