The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { stripDisplayedPlanMarkdown, proposedPlanTitle } from "@t3tools/shared/proposedPlanText"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "./i18n/messages"; |
| 5 | |
| 6 | export function buildCollapsedProposedPlanPreviewMarkdown( |
| 7 | planMarkdown: string, |
| 8 | options?: { |
| 9 | maxLines?: number; |
| 10 | }, |
| 11 | resolve: MessageResolver = m, |
| 12 | ): string { |
| 13 | const maxLines = options?.maxLines ?? 8; |
| 14 | const lines = stripDisplayedPlanMarkdown(planMarkdown) |
| 15 | .trimEnd() |
| 16 | .split(/\r?\n/) |
| 17 | .map((line) => line.trimEnd()); |
| 18 | const previewLines: string[] = []; |
| 19 | let visibleLineCount = 0; |
| 20 | let hasMoreContent = false; |
| 21 | |
| 22 | for (const line of lines) { |
| 23 | const isVisibleLine = line.trim().length > 0; |
| 24 | if (isVisibleLine && visibleLineCount >= maxLines) { |
| 25 | hasMoreContent = true; |
| 26 | break; |
| 27 | } |
| 28 | previewLines.push(line); |
| 29 | if (isVisibleLine) { |
| 30 | visibleLineCount += 1; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | while (previewLines.length > 0 && previewLines.at(-1)?.trim().length === 0) { |
| 35 | previewLines.pop(); |
| 36 | } |
| 37 | |
| 38 | if (previewLines.length === 0) { |
| 39 | return proposedPlanTitle(planMarkdown) ?? resolve(msg("Plan preview unavailable.")); |
| 40 | } |
| 41 | |
| 42 | if (hasMoreContent) { |
| 43 | previewLines.push("", "..."); |
| 44 | } |
| 45 | |
| 46 | return previewLines.join("\n"); |
| 47 | } |
| 48 | |
| 49 | function sanitizePlanFileSegment(input: string): string { |
| 50 | const sanitized = input |
| 51 | .toLowerCase() |
| 52 | .replace(/[`'".,!?()[\]{}]+/g, "") |
| 53 | .replace(/[^a-z0-9]+/g, "-") |
| 54 | .replace(/^-+|-+$/g, ""); |
| 55 | return sanitized.length > 0 ? sanitized : "plan"; |
| 56 | } |
| 57 | |
| 58 | export const PLAN_IMPLEMENTATION_PROMPT_PREFIX = "PLEASE IMPLEMENT THIS PLAN:\n"; |
| 59 | |
| 60 | export function buildProposedPlanMarkdownFilename(planMarkdown: string): string { |
| 61 | const title = proposedPlanTitle(planMarkdown); |
| 62 | return `${sanitizePlanFileSegment(title ?? "plan")}.md`; |
| 63 | } |
| 64 | |
| 65 | export function normalizePlanMarkdownForExport(planMarkdown: string): string { |
| 66 | return `${planMarkdown.trimEnd()}\n`; |
| 67 | } |
| 68 | |
| 69 | export function downloadPlanAsTextFile(filename: string, contents: string): void { |
| 70 | const blob = new Blob([contents], { type: "text/markdown;charset=utf-8" }); |
| 71 | const url = URL.createObjectURL(blob); |
| 72 | const anchor = document.createElement("a"); |
| 73 | anchor.href = url; |
| 74 | anchor.download = filename; |
| 75 | anchor.click(); |
| 76 | window.setTimeout(() => { |
| 77 | URL.revokeObjectURL(url); |
| 78 | }, 0); |
| 79 | } |