|
| 1 | +import { pathToFiletype } from "@opentui/core"; |
| 2 | +import React from "react"; |
| 3 | +import { COLORS, REASONING_MARKDOWN_STYLE } from "../../../ui/constants"; |
| 4 | +import { registerToolLayout } from "../registry"; |
| 5 | +import type { ToolHeader, ToolLayoutConfig, ToolLayoutRenderProps } from "../types"; |
| 6 | + |
| 7 | +type UnknownRecord = Record<string, unknown>; |
| 8 | + |
| 9 | +function isRecord(value: unknown): value is UnknownRecord { |
| 10 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 11 | +} |
| 12 | + |
| 13 | +function extractPath(input: unknown): string | null { |
| 14 | + if (!isRecord(input)) return null; |
| 15 | + if ("path" in input && typeof input.path === "string") { |
| 16 | + return input.path; |
| 17 | + } |
| 18 | + return null; |
| 19 | +} |
| 20 | + |
| 21 | +function extractContent(input: unknown): string | null { |
| 22 | + if (!isRecord(input)) return null; |
| 23 | + if ("content" in input && typeof input.content === "string") { |
| 24 | + return input.content; |
| 25 | + } |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +function extractAppend(input: unknown): boolean { |
| 30 | + if (!isRecord(input)) return false; |
| 31 | + if ("append" in input && typeof input.append === "boolean") { |
| 32 | + return input.append; |
| 33 | + } |
| 34 | + return false; |
| 35 | +} |
| 36 | + |
| 37 | +function WriteFileBody({ call, result }: ToolLayoutRenderProps) { |
| 38 | + if (!isRecord(result)) return null; |
| 39 | + if (result.success === false && typeof result.error === "string") { |
| 40 | + return ( |
| 41 | + <box paddingLeft={2}> |
| 42 | + <text> |
| 43 | + <span fg={COLORS.STATUS_FAILED}>{`error: ${result.error}`}</span> |
| 44 | + </text> |
| 45 | + </box> |
| 46 | + ); |
| 47 | + } |
| 48 | + if (result.success !== true) return null; |
| 49 | + |
| 50 | + const content = extractContent(call.input) ?? ""; |
| 51 | + const path = extractPath(call.input) ?? ""; |
| 52 | + |
| 53 | + // Detect filetype from path for syntax highlighting |
| 54 | + const filetype = pathToFiletype(path); |
| 55 | + |
| 56 | + // Format content preview |
| 57 | + let previewContent = ""; |
| 58 | + if (content.trim()) { |
| 59 | + const MAX_LINES = 4; |
| 60 | + const MAX_CHARS = 160; |
| 61 | + const contentLines = content |
| 62 | + .split("\n") |
| 63 | + .slice(0, MAX_LINES) |
| 64 | + .map((line) => (line.length > MAX_CHARS ? `${line.slice(0, MAX_CHARS - 1)}…` : line)); |
| 65 | + |
| 66 | + const totalLines = content.split("\n").length; |
| 67 | + if (totalLines > MAX_LINES) { |
| 68 | + contentLines.push(`... (${totalLines - MAX_LINES} more lines)`); |
| 69 | + } |
| 70 | + previewContent = contentLines.join("\n"); |
| 71 | + } else { |
| 72 | + previewContent = "(empty file)"; |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <box flexDirection="column" paddingLeft={2} marginTop={0}> |
| 77 | + <box |
| 78 | + borderStyle="single" |
| 79 | + borderColor={COLORS.TOOL_INPUT_BORDER} |
| 80 | + paddingLeft={1} |
| 81 | + paddingRight={1} |
| 82 | + paddingTop={0} |
| 83 | + paddingBottom={0} |
| 84 | + > |
| 85 | + <code |
| 86 | + content={previewContent} |
| 87 | + filetype={filetype} |
| 88 | + syntaxStyle={REASONING_MARKDOWN_STYLE} |
| 89 | + conceal={true} |
| 90 | + drawUnstyledText={false} |
| 91 | + /> |
| 92 | + </box> |
| 93 | + </box> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +export const writeFileLayout: ToolLayoutConfig = { |
| 98 | + abbreviation: "write", |
| 99 | + |
| 100 | + getHeader: (input): ToolHeader | null => { |
| 101 | + const path = extractPath(input); |
| 102 | + if (!path) return null; |
| 103 | + const append = extractAppend(input); |
| 104 | + const filetype = pathToFiletype(path); |
| 105 | + |
| 106 | + const parts: string[] = []; |
| 107 | + if (filetype) parts.push(filetype); |
| 108 | + if (append) parts.push("append"); |
| 109 | + |
| 110 | + const secondary = parts.length > 0 ? parts.join(" · ") : undefined; |
| 111 | + return { primary: path, secondary, secondaryStyle: "dim" }; |
| 112 | + }, |
| 113 | + |
| 114 | + renderBody: WriteFileBody, |
| 115 | +}; |
| 116 | + |
| 117 | +registerToolLayout("writeFile", writeFileLayout); |
0 commit comments