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

proposedPlan.ts

122 lines3.5 KB

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");
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");
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";
72​
73export function buildPlanImplementationPrompt(planMarkdown: string): string {
74 return `PLEASE IMPLEMENT THIS PLAN:\n${planMarkdown.trim()}`;
76​
77export function resolvePlanFollowUpSubmission(input: { draftText: string; planMarkdown: string }): {
78 text: string;
79 interactionMode: "default" | "plan";
80} {
81 const trimmedDraftText = input.draftText.trim();
82 if (trimmedDraftText.length > 0) {
83 return {
84 text: trimmedDraftText,
85 interactionMode: "plan",
86 };
87 }
88​
89 return {
90 text: buildPlanImplementationPrompt(input.planMarkdown),
91 interactionMode: "default",
92 };
94​
95export function buildPlanImplementationThreadTitle(planMarkdown: string): string {
96 const title = proposedPlanTitle(planMarkdown);
97 if (!title) {
98 return "Implement plan";
99 }
100 return `Implement ${title}`;
102​
103export function buildProposedPlanMarkdownFilename(planMarkdown: string): string {
104 const title = proposedPlanTitle(planMarkdown);
105 return `${sanitizePlanFileSegment(title ?? "plan")}.md`;
107​
108export function normalizePlanMarkdownForExport(planMarkdown: string): string {
109 return `${planMarkdown.trimEnd()}\n`;
111​
112export function downloadPlanAsTextFile(filename: string, contents: string): void {
113 const blob = new Blob([contents], { type: "text/markdown;charset=utf-8" });
114 const url = URL.createObjectURL(blob);
115 const anchor = document.createElement("a");
116 anchor.href = url;
117 anchor.download = filename;
118 anchor.click();
119 window.setTimeout(() => {
120 URL.revokeObjectURL(url);
121 }, 0);