Skip to content
Source
Sign inSign up
Open History to see the last commit

TranscriptionPostProcessing.ts

39 lines2.3 KB

1const quoteCue = (cue: string) => JSON.stringify(cue);
2​
3/** Builds the cleanup prompt: instructions, the transcript, and the draft around the insertion point. */
4export function buildTranscriptionPostProcessingPrompt(
5 instructions: string,
6 transcript: string,
7 draft: {
8 readonly text: string;
9 readonly selection: { readonly start: number; readonly end: number };
10 },
11 correctionCue = "",
12): string {
13 const cue = correctionCue.trim();
14 const cueInstructions = cue
15 ? `\n\nCorrection cue: ${quoteCue(cue)}. Only when this cue clearly marks a spoken self-correction, apply the correction the speaker made and omit the cue from the result. The correction may revise, add to, or retract earlier speech. Preserve everything else. If the cue is an intended part of the sentence, keep it. Use the surrounding context to decide; do not assume every occurrence is a correction.`
16 : "";
17 const cursor = Math.max(0, Math.min(draft.selection.start, draft.text.length));
18 const selectionEnd = Math.max(cursor, Math.min(draft.selection.end, draft.text.length));
19 const replacesSelection = selectionEnd > cursor;
20 const placement = replacesSelection
21 ? "The transcript will replace composer_selection between composer_before and composer_after. Use the selected text only as context for terminology and references; it will not remain in the draft. Do not preserve or copy it into the result unless the new speech itself says it."
22 : "The transcript will be inserted at the cursor between composer_before and composer_after.";
23 const selectionBlock = replacesSelection
24 ? `\n<composer_selection>\n${draft.text.slice(cursor, selectionEnd)}\n</composer_selection>`
25 : "";
26 return `${instructions}${cueInstructions}
27​
28<transcript>
29${transcript}
30</transcript>
31​
32${placement} Use the composer draft and insertion position to resolve ambiguity and correct likely recognition errors in the transcript. The draft can clarify the topic, references, terminology, and how the new speech fits with nearby text. Prefer an interpretation supported by the draft when the speech plausibly matches it. Do not invent content, change clear speech, or include existing composer text in the result. Return only the cleaned transcript.
33<composer_before>
34${draft.text.slice(0, cursor)}
35</composer_before>${selectionBlock}
36<composer_after>
37${draft.text.slice(selectionEnd)}
38</composer_after>`;