Open History to see the last commit
1export function proposedPlanTitle(planMarkdown: string): string | null {
2 const heading = planMarkdown.match(/^\s{0,3}#{1,6}\s+(.+)$/m)?.[1]?.trim();
3 return heading && heading.length > 0 ? heading : null;
4}
5
6export function stripDisplayedPlanMarkdown(planMarkdown: string): string {
7 const lines = planMarkdown.trimEnd().split(/\r?\n/);
8 const sourceLines = lines[0] && /^\s{0,3}#{1,6}\s+/.test(lines[0]) ? lines.slice(1) : [...lines];
9 while (sourceLines[0]?.trim().length === 0) {
10 sourceLines.shift();
11 }
12 const firstHeadingMatch = sourceLines[0]?.match(/^\s{0,3}#{1,6}\s+(.+)$/);
13 if (firstHeadingMatch?.[1]?.trim().toLowerCase() === "summary") {
14 sourceLines.shift();
15 while (sourceLines[0]?.trim().length === 0) {
16 sourceLines.shift();
17 }
18 }
19 return sourceLines.join("\n");
20}
21
22export function buildCollapsedProposedPlanPreviewMarkdown(
23 planMarkdown: string,
24 options?: {
25 maxLines?: number;
26 },
27): string {
28 const maxLines = options?.maxLines ?? 8;
29 const lines = stripDisplayedPlanMarkdown(planMarkdown)
30 .trimEnd()
31 .split(/\r?\n/)
32 .map((line) => line.trimEnd());
33 const previewLines: string[] = [];
34 let visibleLineCount = 0;
35 let hasMoreContent = false;
36
37 for (const line of lines) {
38 const isVisibleLine = line.trim().length > 0;
39 if (isVisibleLine && visibleLineCount >= maxLines) {
40 hasMoreContent = true;
41 break;
42 }
43 previewLines.push(line);
44 if (isVisibleLine) {
45 visibleLineCount += 1;
46 }
47 }
48
49 while (previewLines.length > 0 && previewLines.at(-1)?.trim().length === 0) {
50 previewLines.pop();
51 }
52
53 if (previewLines.length === 0) {
54 return proposedPlanTitle(planMarkdown) ?? "Plan preview unavailable.";
55 }
56
57 if (hasMoreContent) {
58 previewLines.push("", "...");
59 }
60
61 return previewLines.join("\n");
62}
63
64function sanitizePlanFileSegment(input: string): string {
65 const sanitized = input
66 .toLowerCase()
67 .replace(/[`'".,!?()[\]{}]+/g, "")
68 .replace(/[^a-z0-9]+/g, "-")
69 .replace(/^-+|-+$/g, "");
70 return sanitized.length > 0 ? sanitized : "plan";
71}
72
73/** Prefix of the message the app sends when the user approves a plan. */
74export const PLAN_IMPLEMENTATION_PROMPT_PREFIX = "PLEASE IMPLEMENT THIS PLAN:\n";
75
76export function buildPlanImplementationPrompt(planMarkdown: string): string {
77 return `${PLAN_IMPLEMENTATION_PROMPT_PREFIX}${planMarkdown.trim()}`;
78}
79
80export function resolvePlanFollowUpSubmission(input: { draftText: string; planMarkdown: string }): {
81 text: string;
82 interactionMode: "default" | "plan";
83} {
84 const trimmedDraftText = input.draftText.trim();
85 if (trimmedDraftText.length > 0) {
86 return {
87 text: trimmedDraftText,
88 interactionMode: "plan",
89 };
90 }
91
92 return {
93 text: buildPlanImplementationPrompt(input.planMarkdown),
94 interactionMode: "default",
95 };
96}
97
98export function buildPlanImplementationThreadTitle(planMarkdown: string): string {
99 const title = proposedPlanTitle(planMarkdown);
100 if (!title) {
101 return "Implement plan";
102 }
103 return `Implement ${title}`;
104}
105
106export function buildProposedPlanMarkdownFilename(planMarkdown: string): string {
107 const title = proposedPlanTitle(planMarkdown);
108 return `${sanitizePlanFileSegment(title ?? "plan")}.md`;
109}
110
111export function normalizePlanMarkdownForExport(planMarkdown: string): string {
112 return `${planMarkdown.trimEnd()}\n`;
113}
114
115export function downloadPlanAsTextFile(filename: string, contents: string): void {
116 const blob = new Blob([contents], { type: "text/markdown;charset=utf-8" });
117 const url = URL.createObjectURL(blob);
118 const anchor = document.createElement("a");
119 anchor.href = url;
120 anchor.download = filename;
121 anchor.click();
122 window.setTimeout(() => {
123 URL.revokeObjectURL(url);
124 }, 0);
125}