Open History to see the last commit
1import { TRIAGE_PROMPT_FILE_NAME } from "@t3tools/contracts";
2
3const PLAYBOOK_LINK_START = `[${TRIAGE_PROMPT_FILE_NAME}](`;
4
5/** A draft is a triage draft while its message still links the playbook file. */
6export const isTriagePrompt = (prompt: string) => prompt.includes(PLAYBOOK_LINK_START);
7
8// `serializeComposerFileLink` encodes `)` and whitespace, so the first `)` closes the link.
9const playbookLinkEnd = (text: string, start: number) => {
10 const close = text.indexOf(")", start + PLAYBOOK_LINK_START.length);
11 return close === -1 ? null : close + 1;
12};
13
14/**
15 * Moves a triage draft to the session another machine prepared: each playbook
16 * link in `prompt` becomes the link in `preparedPrompt`, and the rest of the
17 * text stays. A prompt without the link comes back unchanged.
18 */
19export function replaceTriagePlaybookLink(prompt: string, preparedPrompt: string): string {
20 const preparedStart = preparedPrompt.indexOf(PLAYBOOK_LINK_START);
21 const preparedEnd = preparedStart === -1 ? null : playbookLinkEnd(preparedPrompt, preparedStart);
22 if (preparedEnd === null) return prompt;
23 const link = preparedPrompt.slice(preparedStart, preparedEnd);
24 let next = "";
25 let cursor = 0;
26 while (true) {
27 const start = prompt.indexOf(PLAYBOOK_LINK_START, cursor);
28 const end = start === -1 ? null : playbookLinkEnd(prompt, start);
29 if (end === null) return next + prompt.slice(cursor);
30 next += prompt.slice(cursor, start) + link;
31 cursor = end;
32 }
33}