@abacus-ai/cli 1.106.25008 → 2.0.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/.oxlintrc.json +8 -0
  2. package/dist/index.mjs +12823 -0
  3. package/package.json +7 -39
  4. package/resources/abacus.ico +0 -0
  5. package/resources/entitlements.plist +9 -0
  6. package/src/__e2e__/README.md +196 -0
  7. package/src/__e2e__/agent-interactions.e2e.test.tsx +61 -0
  8. package/src/__e2e__/cli-commands.e2e.test.tsx +77 -0
  9. package/src/__e2e__/conversation-throttle.e2e.test.ts +453 -0
  10. package/src/__e2e__/conversation.e2e.test.tsx +56 -0
  11. package/src/__e2e__/diff-preview.e2e.test.tsx +3399 -0
  12. package/src/__e2e__/file-creation.e2e.test.tsx +149 -0
  13. package/src/__e2e__/helpers/test-helpers.ts +449 -0
  14. package/src/__e2e__/keyboard-navigation.e2e.test.tsx +34 -0
  15. package/src/__e2e__/llm-models.e2e.test.ts +402 -0
  16. package/src/__e2e__/mcp/mcp-callback-flow.e2e.test.tsx +71 -0
  17. package/src/__e2e__/mcp/mcp-full-app-ui.e2e.test.tsx +167 -0
  18. package/src/__e2e__/mcp/mcp-ui-rendering.e2e.test.tsx +185 -0
  19. package/src/__e2e__/repl.e2e.test.tsx +78 -0
  20. package/src/__e2e__/shell-compatibility.e2e.test.tsx +76 -0
  21. package/src/__e2e__/theme-mcp.e2e.test.tsx +98 -0
  22. package/src/__e2e__/tool-permissions.e2e.test.tsx +66 -0
  23. package/src/args.ts +22 -0
  24. package/src/components/__tests__/react-compiler.test.tsx +78 -0
  25. package/src/components/__tests__/status-indicator.test.tsx +403 -0
  26. package/src/components/composer/__tests__/bash-runner.test.tsx +263 -0
  27. package/src/components/composer/agent-mode-indicator.tsx +63 -0
  28. package/src/components/composer/bash-runner.tsx +54 -0
  29. package/src/components/composer/commands/default-commands.tsx +615 -0
  30. package/src/components/composer/commands/handler.tsx +59 -0
  31. package/src/components/composer/commands/picker.tsx +273 -0
  32. package/src/components/composer/commands/registry.ts +233 -0
  33. package/src/components/composer/commands/types.ts +33 -0
  34. package/src/components/composer/context.tsx +88 -0
  35. package/src/components/composer/file-mention-picker.tsx +83 -0
  36. package/src/components/composer/help.tsx +44 -0
  37. package/src/components/composer/index.tsx +1007 -0
  38. package/src/components/composer/mentions.ts +57 -0
  39. package/src/components/composer/message-queue.tsx +70 -0
  40. package/src/components/composer/mode-panel.tsx +35 -0
  41. package/src/components/composer/modes/__tests__/bash-handler.test.tsx +755 -0
  42. package/src/components/composer/modes/__tests__/bash-renderer.test.tsx +1108 -0
  43. package/src/components/composer/modes/bash-handler.tsx +132 -0
  44. package/src/components/composer/modes/bash-renderer.tsx +175 -0
  45. package/src/components/composer/modes/default-handlers.tsx +33 -0
  46. package/src/components/composer/modes/index.ts +41 -0
  47. package/src/components/composer/modes/types.ts +21 -0
  48. package/src/components/composer/persistent-shell.ts +283 -0
  49. package/src/components/composer/process.ts +65 -0
  50. package/src/components/composer/types.ts +9 -0
  51. package/src/components/composer/use-mention-search.ts +68 -0
  52. package/src/components/error-boundry.tsx +60 -0
  53. package/src/components/exit-message.tsx +29 -0
  54. package/src/components/expanded-view.tsx +74 -0
  55. package/src/components/file-completion.tsx +127 -0
  56. package/src/components/header.tsx +47 -0
  57. package/src/components/logo.tsx +37 -0
  58. package/src/components/segments.tsx +356 -0
  59. package/src/components/status-indicator.tsx +306 -0
  60. package/src/components/tool-group-summary.tsx +263 -0
  61. package/src/components/tool-permissions/ask-user-question-permission-ui.tsx +319 -0
  62. package/src/components/tool-permissions/diff-preview.tsx +359 -0
  63. package/src/components/tool-permissions/index.ts +5 -0
  64. package/src/components/tool-permissions/permission-options.tsx +401 -0
  65. package/src/components/tool-permissions/permission-preview-header.tsx +57 -0
  66. package/src/components/tool-permissions/tool-permission-ui.tsx +420 -0
  67. package/src/components/tools/agent/ask-user-question.tsx +107 -0
  68. package/src/components/tools/agent/enter-plan-mode.tsx +55 -0
  69. package/src/components/tools/agent/exit-plan-mode.tsx +83 -0
  70. package/src/components/tools/agent/handoff-to-main.tsx +27 -0
  71. package/src/components/tools/agent/subagent.tsx +37 -0
  72. package/src/components/tools/agent/todo-write.tsx +104 -0
  73. package/src/components/tools/browser/close-tab.tsx +58 -0
  74. package/src/components/tools/browser/computer.tsx +70 -0
  75. package/src/components/tools/browser/get-interactive-elements.tsx +54 -0
  76. package/src/components/tools/browser/get-tab-content.tsx +51 -0
  77. package/src/components/tools/browser/navigate-to.tsx +59 -0
  78. package/src/components/tools/browser/new-tab.tsx +60 -0
  79. package/src/components/tools/browser/perform-action.tsx +63 -0
  80. package/src/components/tools/browser/refresh-tab.tsx +43 -0
  81. package/src/components/tools/browser/switch-tab.tsx +58 -0
  82. package/src/components/tools/filesystem/delete-file.tsx +104 -0
  83. package/src/components/tools/filesystem/edit.tsx +220 -0
  84. package/src/components/tools/filesystem/list-dir.tsx +78 -0
  85. package/src/components/tools/filesystem/read-file.tsx +180 -0
  86. package/src/components/tools/filesystem/upload-image.tsx +76 -0
  87. package/src/components/tools/ide/ide-diagnostics.tsx +62 -0
  88. package/src/components/tools/index.ts +91 -0
  89. package/src/components/tools/mcp/mcp-tool.tsx +158 -0
  90. package/src/components/tools/search/fetch-url.tsx +73 -0
  91. package/src/components/tools/search/file-search.tsx +78 -0
  92. package/src/components/tools/search/grep.tsx +90 -0
  93. package/src/components/tools/search/semantic-search.tsx +66 -0
  94. package/src/components/tools/search/web-search.tsx +71 -0
  95. package/src/components/tools/shared/index.tsx +48 -0
  96. package/src/components/tools/shared/zod-coercion.ts +35 -0
  97. package/src/components/tools/terminal/bash-tool-output.tsx +188 -0
  98. package/src/components/tools/terminal/get-terminal-output.tsx +91 -0
  99. package/src/components/tools/terminal/run-in-terminal.tsx +131 -0
  100. package/src/components/tools/types.ts +16 -0
  101. package/src/components/tools.tsx +68 -0
  102. package/src/components/ui/__tests__/divider.test.tsx +61 -0
  103. package/src/components/ui/__tests__/gradient.test.tsx +125 -0
  104. package/src/components/ui/__tests__/input.test.tsx +166 -0
  105. package/src/components/ui/__tests__/select.test.tsx +273 -0
  106. package/src/components/ui/__tests__/shimmer.test.tsx +99 -0
  107. package/src/components/ui/blinking-indicator.tsx +27 -0
  108. package/src/components/ui/divider.tsx +162 -0
  109. package/src/components/ui/gradient.tsx +56 -0
  110. package/src/components/ui/input.tsx +228 -0
  111. package/src/components/ui/select.tsx +151 -0
  112. package/src/components/ui/shimmer.tsx +76 -0
  113. package/src/context/agent-mode.tsx +95 -0
  114. package/src/context/extension-file.tsx +136 -0
  115. package/src/context/network-activity.tsx +45 -0
  116. package/src/context/notification.tsx +62 -0
  117. package/src/context/shell-size.tsx +49 -0
  118. package/src/context/shell-title.tsx +38 -0
  119. package/src/entrypoints/print-mode.ts +312 -0
  120. package/src/entrypoints/repl.tsx +389 -0
  121. package/src/hooks/use-agent.ts +15 -0
  122. package/src/hooks/use-api-client.ts +1 -0
  123. package/src/hooks/use-available-height.ts +8 -0
  124. package/src/hooks/use-cleanup.ts +29 -0
  125. package/src/hooks/use-interrupt-manager.ts +242 -0
  126. package/src/hooks/use-models.ts +22 -0
  127. package/src/index.ts +217 -0
  128. package/src/lib/__tests__/ansi.test.ts +255 -0
  129. package/src/lib/__tests__/cli.test.ts +122 -0
  130. package/src/lib/__tests__/commands.test.ts +325 -0
  131. package/src/lib/__tests__/constants.test.ts +15 -0
  132. package/src/lib/__tests__/focusables.test.ts +25 -0
  133. package/src/lib/__tests__/fs.test.ts +231 -0
  134. package/src/lib/__tests__/markdown.test.tsx +348 -0
  135. package/src/lib/__tests__/mcpCommandHandler.test.ts +173 -0
  136. package/src/lib/__tests__/mcpManagement.test.ts +38 -0
  137. package/src/lib/__tests__/path-paste.test.ts +144 -0
  138. package/src/lib/__tests__/path.test.ts +300 -0
  139. package/src/lib/__tests__/queries.test.ts +39 -0
  140. package/src/lib/__tests__/standaloneMcpService.test.ts +71 -0
  141. package/src/lib/__tests__/text-buffer.test.ts +328 -0
  142. package/src/lib/__tests__/text-utils.test.ts +32 -0
  143. package/src/lib/__tests__/timing.test.ts +78 -0
  144. package/src/lib/__tests__/utils.test.ts +238 -0
  145. package/src/lib/__tests__/vim-buffer-actions.test.ts +154 -0
  146. package/src/lib/ansi.ts +150 -0
  147. package/src/lib/cli-push-server.ts +112 -0
  148. package/src/lib/cli.ts +44 -0
  149. package/src/lib/clipboard.ts +226 -0
  150. package/src/lib/command-utils.ts +93 -0
  151. package/src/lib/commands.ts +270 -0
  152. package/src/lib/constants.ts +3 -0
  153. package/src/lib/extension-connection.ts +181 -0
  154. package/src/lib/focusables.ts +7 -0
  155. package/src/lib/fs.ts +533 -0
  156. package/src/lib/markdown/code-block.tsx +63 -0
  157. package/src/lib/markdown/index.ts +4 -0
  158. package/src/lib/markdown/link.tsx +19 -0
  159. package/src/lib/markdown/markdown.tsx +372 -0
  160. package/src/lib/markdown/types.ts +15 -0
  161. package/src/lib/mcpCommandHandler.ts +121 -0
  162. package/src/lib/mcpManagement.ts +44 -0
  163. package/src/lib/path-paste.ts +185 -0
  164. package/src/lib/path.ts +179 -0
  165. package/src/lib/queries.ts +15 -0
  166. package/src/lib/standaloneMcpService.ts +688 -0
  167. package/src/lib/status-utils.ts +237 -0
  168. package/src/lib/test-utils.tsx +72 -0
  169. package/src/lib/text-buffer.ts +2415 -0
  170. package/src/lib/text-utils.ts +272 -0
  171. package/src/lib/timing.ts +63 -0
  172. package/src/lib/types.ts +295 -0
  173. package/src/lib/utils.ts +182 -0
  174. package/src/lib/vim-buffer-actions.ts +732 -0
  175. package/src/providers/agent.tsx +1063 -0
  176. package/src/providers/api-client.tsx +43 -0
  177. package/src/services/logger.ts +85 -0
  178. package/src/terminal/detection.ts +187 -0
  179. package/src/terminal/exit.ts +279 -0
  180. package/src/terminal/notification.ts +83 -0
  181. package/src/terminal/progress.ts +201 -0
  182. package/src/terminal/setup.ts +797 -0
  183. package/src/terminal/types.ts +51 -0
  184. package/src/theme/context.tsx +57 -0
  185. package/src/theme/index.ts +4 -0
  186. package/src/theme/themed.tsx +35 -0
  187. package/src/theme/themes.json +546 -0
  188. package/src/theme/types.ts +110 -0
  189. package/src/tools/types.ts +59 -0
  190. package/src/tools/utils/__tests__/zod-coercion.test.ts +33 -0
  191. package/src/tools/utils/tool-ui-components.tsx +649 -0
  192. package/src/tools/utils/zod-coercion.ts +35 -0
  193. package/tsconfig.json +16 -0
  194. package/tsconfig.node.json +29 -0
  195. package/tsconfig.test.json +27 -0
  196. package/tsdown.config.ts +17 -0
  197. package/vitest.config.ts +76 -0
  198. package/README.md +0 -28
  199. package/dist/index.js +0 -26
@@ -0,0 +1,649 @@
1
+ import { View, Text } from "@codellm/jar";
2
+ import { renderMermaidAscii } from "beautiful-mermaid";
3
+ import { highlight, supportsLanguage } from "cli-highlight";
4
+ import { structuredPatch } from "diff";
5
+ import { extname } from "path";
6
+ import React, { useEffect, useState, useMemo, createContext, use } from "react";
7
+
8
+ import type { ToolStatus } from "../types.js";
9
+
10
+ import { useTheme } from "../../theme/index.js";
11
+
12
+ /**
13
+ * Context for the currently pending tool call ID (awaiting permission).
14
+ * Provided at top level; consumed by ToolHeader to suppress blinking.
15
+ */
16
+ export const PendingToolCallIdContext = createContext<string | null>(null);
17
+
18
+ /**
19
+ * Diff line type for condensed diff preview
20
+ */
21
+ export interface CondensedDiffLine {
22
+ type: "added" | "removed" | "unchanged";
23
+ content: string;
24
+ lineNumber: number;
25
+ }
26
+
27
+ /**
28
+ * Format bytes for display
29
+ */
30
+ export function formatBytes(bytes: number): string {
31
+ if (bytes < 1024) {
32
+ return `${bytes}B`;
33
+ }
34
+ if (bytes < 1024 * 1024) {
35
+ return `${(bytes / 1024).toFixed(1)}KB`;
36
+ }
37
+ return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
38
+ }
39
+
40
+ /**
41
+ * Format duration in milliseconds for display
42
+ */
43
+ export function formatDuration(ms: number): string {
44
+ if (ms < 1000) {
45
+ return `${ms}ms`;
46
+ }
47
+ return `${(ms / 1000).toFixed(1)}s`;
48
+ }
49
+
50
+ /**
51
+ * Truncate long file paths for display
52
+ */
53
+ export function truncatePath(path: string, maxLen: number = 50): string {
54
+ if (path.length <= maxLen) {
55
+ return path;
56
+ }
57
+ const parts = path.split("/");
58
+ if (parts.length === 1) {
59
+ return path.length > maxLen ? `...${path.slice(-maxLen + 3)}` : path;
60
+ }
61
+ const filename = parts[parts.length - 1];
62
+ const remaining = maxLen - filename.length - 3;
63
+ if (remaining < 0) {
64
+ return `...${filename.slice(-maxLen + 3)}`;
65
+ }
66
+ const prefix = parts.slice(0, -1).join("/");
67
+ if (prefix.length <= remaining) {
68
+ return path;
69
+ }
70
+ return `...${prefix.slice(-remaining)}/${filename}`;
71
+ }
72
+
73
+ /**
74
+ * Get color for tool status
75
+ */
76
+ export function getStatusColor(status: ToolStatus): string {
77
+ switch (status) {
78
+ case "success":
79
+ return "green";
80
+ case "error":
81
+ return "red";
82
+ case "rejected":
83
+ case "interrupted":
84
+ return "yellow";
85
+ case "pending":
86
+ case "executing":
87
+ default:
88
+ return "white";
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Blinking dot animation for pending status - grey fade effect
94
+ */
95
+ export function BlinkingDot() {
96
+ const [visible, setVisible] = useState(true);
97
+
98
+ useEffect(() => {
99
+ const interval = setInterval(() => {
100
+ setVisible((prev) => !prev);
101
+ }, 500);
102
+
103
+ return () => clearInterval(interval);
104
+ }, []);
105
+
106
+ return (
107
+ <Text color="gray" dimColor={!visible}>
108
+
109
+ </Text>
110
+ );
111
+ }
112
+
113
+ /**
114
+ * Tool header component - displays tool name, params, and status
115
+ */
116
+ export interface ToolHeaderProps {
117
+ name: string;
118
+ params?: string;
119
+ status?: ToolStatus;
120
+ showSpinner?: boolean;
121
+ /** When true, shows a static yellow ◌ instead of blinking dot (permission wait state) */
122
+ isAwaitingPermission?: boolean;
123
+ /** Tool call ID — used to check against PendingToolCallIdContext for auto awaiting-permission detection */
124
+ toolCallId?: string;
125
+ /** When set, syntax-highlight params using this language via cli-highlight */
126
+ paramsLanguage?: string;
127
+ }
128
+
129
+ export function ToolHeader({
130
+ name,
131
+ params,
132
+ status = "pending",
133
+ showSpinner,
134
+ isAwaitingPermission,
135
+ toolCallId,
136
+ paramsLanguage,
137
+ }: ToolHeaderProps) {
138
+ const color = getStatusColor(status);
139
+
140
+ // Auto-detect awaiting permission via context:
141
+ // When a tool call ID matches the pending permission, or when showSpinner is true
142
+ // and there's any pending permission (only one at a time), show static indicator.
143
+ const pendingToolCallId = use(PendingToolCallIdContext);
144
+ const awaiting =
145
+ isAwaitingPermission ??
146
+ (pendingToolCallId != null &&
147
+ ((toolCallId != null && toolCallId === pendingToolCallId) ||
148
+ (toolCallId == null && showSpinner)));
149
+
150
+ let indicator: React.ReactNode;
151
+ if (awaiting) {
152
+ indicator = <Text color="yellow">◌</Text>;
153
+ } else if (showSpinner) {
154
+ indicator = <BlinkingDot />;
155
+ } else {
156
+ indicator = <Text color={color}>⏺</Text>;
157
+ }
158
+
159
+ let paramsContent: React.ReactNode = null;
160
+ if (params) {
161
+ let highlighted = params;
162
+ if (paramsLanguage && supportsLanguage(paramsLanguage)) {
163
+ try {
164
+ highlighted = highlight(params, { language: paramsLanguage });
165
+ } catch {
166
+ // fallback to plain text
167
+ }
168
+ }
169
+ paramsContent = paramsLanguage ? (
170
+ <Text>({highlighted})</Text>
171
+ ) : (
172
+ <Text dimColor>({params})</Text>
173
+ );
174
+ }
175
+
176
+ return (
177
+ <View flexDirection="row" alignItems="center" gap={1}>
178
+ {indicator}
179
+ <Text color="cyan">{name}</Text>
180
+ {paramsContent}
181
+ </View>
182
+ );
183
+ }
184
+
185
+ /**
186
+ * Tool result component - displays tool output with indentation
187
+ */
188
+ export interface ToolResultProps {
189
+ children: React.ReactNode;
190
+ color?: string;
191
+ indent?: number;
192
+ }
193
+
194
+ export function ToolResult({ children, color = "gray", indent = 2 }: ToolResultProps) {
195
+ return (
196
+ <View marginLeft={indent} flexDirection="row" alignItems="flex-start" gap={1}>
197
+ <Text color={color} dimColor>
198
+
199
+ </Text>
200
+ <Text color={color} dimColor>
201
+ {children}
202
+ </Text>
203
+ </View>
204
+ );
205
+ }
206
+
207
+ /**
208
+ * Wrapper for preview content areas (diffs, code blocks, terminal output).
209
+ * Provides consistent indentation below the result line.
210
+ */
211
+ export interface PreviewContentProps {
212
+ children: React.ReactNode;
213
+ indent?: number;
214
+ }
215
+
216
+ export function PreviewContent({ children, indent = 4 }: PreviewContentProps) {
217
+ return (
218
+ <View flexDirection="column" paddingLeft={indent}>
219
+ {children}
220
+ </View>
221
+ );
222
+ }
223
+
224
+ /**
225
+ * Parse tool result from JSON string
226
+ */
227
+ export function parseToolResult(content: string): any {
228
+ try {
229
+ const parsed = JSON.parse(content);
230
+ if (typeof parsed !== "object" || parsed === null) {
231
+ return { output: content, type: "text" };
232
+ }
233
+ return parsed;
234
+ } catch {
235
+ const jsonMatch = content.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
236
+ if (jsonMatch && jsonMatch[1]) {
237
+ try {
238
+ const parsed = JSON.parse(jsonMatch[1].trim());
239
+ if (typeof parsed !== "object" || parsed === null) {
240
+ return { output: jsonMatch[1].trim(), type: "text" };
241
+ }
242
+ return parsed;
243
+ } catch {
244
+ return { output: jsonMatch[1].trim(), type: "text" };
245
+ }
246
+ }
247
+ return { output: content, type: "text" };
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Compute expected final content after edit operation
253
+ * Used for diff preview
254
+ */
255
+ export function computeExpectedFinalContentForDiff(
256
+ currentContent: string,
257
+ codeEdit: string,
258
+ overwriteFile: boolean,
259
+ startLine?: number,
260
+ endLine?: number,
261
+ instructions?: string,
262
+ ): string {
263
+ if (overwriteFile) {
264
+ return codeEdit;
265
+ }
266
+
267
+ if (startLine !== undefined && endLine !== undefined) {
268
+ const lines = currentContent.split("\n");
269
+ const totalLines = lines.length;
270
+ const validStartLine = Math.max(1, Math.min(startLine, totalLines));
271
+ const validEndLine = Math.max(validStartLine, Math.min(endLine, totalLines));
272
+
273
+ const beforeLines = lines.slice(0, validStartLine - 1);
274
+ const afterLines = lines.slice(validEndLine);
275
+ const newLines = codeEdit.split("\n");
276
+
277
+ return [...beforeLines, ...newLines, ...afterLines].join("\n");
278
+ }
279
+
280
+ if (instructions) {
281
+ return currentContent + "\n" + codeEdit;
282
+ }
283
+
284
+ const codeEditLines = codeEdit.split("\n");
285
+ const currentLines = currentContent.split("\n");
286
+
287
+ if (codeEditLines.length >= currentLines.length * 0.8) {
288
+ return codeEdit;
289
+ }
290
+
291
+ if (currentContent.includes(codeEdit)) {
292
+ return currentContent;
293
+ }
294
+
295
+ return currentContent + "\n" + codeEdit;
296
+ }
297
+
298
+ /**
299
+ * Wrap long lines for terminal display
300
+ */
301
+ export function wrapLine(content: string, maxWidth: number, lineNumberWidth: number): string[] {
302
+ if (!content) return [""];
303
+
304
+ // Account for line number, gutter (` - ` or ` + `), and some margin
305
+ const availableWidth = maxWidth - lineNumberWidth - 4 - 2; // 4 for gutter, 2 for margins
306
+
307
+ if (content.length <= availableWidth) {
308
+ return [content];
309
+ }
310
+
311
+ const wrapped: string[] = [];
312
+ let remaining = content;
313
+
314
+ while (remaining.length > 0) {
315
+ if (remaining.length <= availableWidth) {
316
+ wrapped.push(remaining);
317
+ break;
318
+ }
319
+
320
+ // Try to break at a space near the limit
321
+ let breakPoint = remaining.lastIndexOf(" ", availableWidth);
322
+ if (breakPoint === -1 || breakPoint < availableWidth * 0.5) {
323
+ breakPoint = availableWidth;
324
+ }
325
+
326
+ wrapped.push(remaining.slice(0, breakPoint));
327
+ remaining = remaining.slice(breakPoint).trimStart();
328
+ }
329
+
330
+ return wrapped;
331
+ }
332
+
333
+ /**
334
+ * Get language from file extension for syntax highlighting
335
+ */
336
+ function getLanguageFromPath(filePath: string): string | undefined {
337
+ const ext = extname(filePath).toLowerCase().slice(1);
338
+ const langMap: Record<string, string> = {
339
+ ts: "typescript",
340
+ tsx: "typescript",
341
+ js: "javascript",
342
+ jsx: "javascript",
343
+ py: "python",
344
+ rb: "ruby",
345
+ rs: "rust",
346
+ go: "go",
347
+ java: "java",
348
+ c: "c",
349
+ cpp: "cpp",
350
+ h: "c",
351
+ hpp: "cpp",
352
+ cs: "csharp",
353
+ php: "php",
354
+ swift: "swift",
355
+ kt: "kotlin",
356
+ scala: "scala",
357
+ sh: "bash",
358
+ bash: "bash",
359
+ zsh: "bash",
360
+ json: "json",
361
+ yaml: "yaml",
362
+ yml: "yaml",
363
+ xml: "xml",
364
+ html: "html",
365
+ css: "css",
366
+ scss: "scss",
367
+ less: "less",
368
+ sql: "sql",
369
+ md: "markdown",
370
+ markdown: "markdown",
371
+ txt: "plaintext",
372
+ mmd: "mermaid",
373
+ mermaid: "mermaid",
374
+ };
375
+ return langMap[ext];
376
+ }
377
+
378
+ /**
379
+ * Try to render mermaid diagram as ASCII art
380
+ * Returns null if parsing fails
381
+ */
382
+ function tryRenderMermaid(content: string): string | null {
383
+ try {
384
+ return renderMermaidAscii(content);
385
+ } catch {
386
+ return null;
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Code preview component for new files
392
+ * Shows syntax-highlighted code block with line numbers
393
+ */
394
+ export interface CodePreviewProps {
395
+ content: string;
396
+ filePath?: string;
397
+ /** First line number to display (1-based). Defaults to 1. */
398
+ startLine?: number;
399
+ maxLines?: number;
400
+ dimmed?: boolean;
401
+ }
402
+
403
+ export function CodePreview({
404
+ content,
405
+ filePath,
406
+ startLine = 1,
407
+ maxLines = 4,
408
+ dimmed = false,
409
+ }: CodePreviewProps) {
410
+ const { displayContent, totalLines, hasMore, displayLineCount } = useMemo(() => {
411
+ const allLines = content.split("\n");
412
+ const total = allLines.length;
413
+ const lines = allLines.slice(0, maxLines);
414
+ return {
415
+ displayContent: lines.join("\n"),
416
+ displayLineCount: lines.length,
417
+ totalLines: total,
418
+ hasMore: total > maxLines,
419
+ };
420
+ }, [content, maxLines]);
421
+
422
+ const highlightedContent = useMemo(() => {
423
+ if (!displayContent) return displayContent;
424
+
425
+ const language = filePath ? getLanguageFromPath(filePath) : undefined;
426
+
427
+ // Try to render mermaid diagrams as ASCII art
428
+ if (language === "mermaid") {
429
+ const mermaidOutput = tryRenderMermaid(content);
430
+ if (mermaidOutput !== null) {
431
+ return mermaidOutput;
432
+ }
433
+ // If mermaid parsing fails, fall through to regular rendering
434
+ }
435
+
436
+ if (language && supportsLanguage(language)) {
437
+ try {
438
+ return highlight(displayContent, { language });
439
+ } catch {
440
+ // Fallback to plain text
441
+ }
442
+ }
443
+ return displayContent;
444
+ }, [displayContent, filePath, content]);
445
+
446
+ if (!displayContent) {
447
+ return null;
448
+ }
449
+
450
+ const lastLineNum = startLine + totalLines - 1;
451
+ const maxLineNumWidth = lastLineNum.toString().length;
452
+ const lines = highlightedContent.split("\n");
453
+
454
+ return (
455
+ <View flexDirection="column">
456
+ {lines.map((line, idx) => {
457
+ const lineNum = startLine + idx;
458
+ const paddedNum = String(lineNum).padStart(maxLineNumWidth, " ");
459
+ return (
460
+ <Text key={idx} dimColor={dimmed}>
461
+ <Text dimColor>{paddedNum} │ </Text>
462
+ {line}
463
+ </Text>
464
+ );
465
+ })}
466
+ {hasMore && <Text dimColor> ... {totalLines - displayLineCount} more lines</Text>}
467
+ </View>
468
+ );
469
+ }
470
+
471
+ /**
472
+ * Highlight full file content and return per-line highlighted strings.
473
+ * Highlights the whole file (not per-line) to preserve multi-line syntax context.
474
+ * Returns plain split lines if language is unsupported or highlight throws.
475
+ */
476
+ export function highlightLines(content: string, filePath: string): string[] {
477
+ const language = getLanguageFromPath(filePath);
478
+ if (language && language !== "mermaid" && supportsLanguage(language)) {
479
+ try {
480
+ return highlight(content, { language }).split("\n");
481
+ } catch {}
482
+ }
483
+ return content.split("\n");
484
+ }
485
+
486
+ /**
487
+ * Condensed diff preview component
488
+ * Shows a limited number of changed lines with context
489
+ */
490
+ export interface CondensedDiffPreviewProps {
491
+ originalContent: string;
492
+ newContent: string;
493
+ filePath?: string;
494
+ maxLines?: number;
495
+ dimmed?: boolean;
496
+ }
497
+
498
+ export function CondensedDiffPreview({
499
+ originalContent,
500
+ newContent,
501
+ filePath,
502
+ maxLines = 4,
503
+ dimmed = false,
504
+ }: CondensedDiffPreviewProps) {
505
+ const { colors } = useTheme();
506
+
507
+ const { diffLines, addedCount, removedCount, highlightedOrigLines, highlightedNewLines } =
508
+ useMemo(() => {
509
+ const hlOrig = filePath ? highlightLines(originalContent, filePath) : null;
510
+ const hlNew = filePath ? highlightLines(newContent, filePath) : null;
511
+
512
+ const patch = structuredPatch(
513
+ "file",
514
+ "file",
515
+ originalContent,
516
+ newContent,
517
+ undefined,
518
+ undefined,
519
+ { context: 1 },
520
+ );
521
+ const lines: CondensedDiffLine[] = [];
522
+ let added = 0;
523
+ let removed = 0;
524
+
525
+ for (const hunk of patch.hunks) {
526
+ let oldLineNum = hunk.oldStart;
527
+ let newLineNum = hunk.newStart;
528
+
529
+ for (const line of hunk.lines) {
530
+ if (lines.length >= maxLines) break;
531
+
532
+ const prefix = line[0];
533
+ const content = line.slice(1);
534
+
535
+ if (prefix === "+") {
536
+ lines.push({ type: "added", content, lineNumber: newLineNum++ });
537
+ added++;
538
+ } else if (prefix === "-") {
539
+ lines.push({ type: "removed", content, lineNumber: oldLineNum++ });
540
+ removed++;
541
+ } else {
542
+ lines.push({ type: "unchanged", content, lineNumber: oldLineNum });
543
+ oldLineNum++;
544
+ newLineNum++;
545
+ }
546
+ }
547
+ if (lines.length >= maxLines) break;
548
+ }
549
+
550
+ return {
551
+ diffLines: lines.slice(0, maxLines),
552
+ addedCount: added,
553
+ removedCount: removed,
554
+ highlightedOrigLines: hlOrig,
555
+ highlightedNewLines: hlNew,
556
+ };
557
+ }, [originalContent, newContent, filePath, maxLines]);
558
+
559
+ if (diffLines.length === 0) {
560
+ return null;
561
+ }
562
+
563
+ const maxLineNumWidth = Math.max(...diffLines.map((l) => l.lineNumber)).toString().length;
564
+
565
+ // Build summary text
566
+ const summaryParts = [];
567
+ if (addedCount > 0) summaryParts.push(`+${addedCount}`);
568
+ if (removedCount > 0) summaryParts.push(`-${removedCount}`);
569
+ const summary = summaryParts.join(" ");
570
+
571
+ return (
572
+ <View flexDirection="column" paddingLeft={1}>
573
+ {summary && (
574
+ <View marginBottom={0}>
575
+ <Text dimColor>{summary} lines</Text>
576
+ </View>
577
+ )}
578
+ {diffLines.map((line, idx) => {
579
+ const paddedNum = String(line.lineNumber).padStart(maxLineNumWidth, " ");
580
+ // Pick syntax-highlighted content when available
581
+ const hlContent =
582
+ line.type === "removed"
583
+ ? (highlightedOrigLines?.[line.lineNumber - 1] ?? line.content)
584
+ : line.type === "added"
585
+ ? (highlightedNewLines?.[line.lineNumber - 1] ?? line.content)
586
+ : (highlightedOrigLines?.[line.lineNumber - 1] ?? line.content);
587
+
588
+ if (line.type === "removed") {
589
+ return (
590
+ <View key={idx} flexDirection="row" backgroundColor={colors.diffRemoved}>
591
+ <Text
592
+ backgroundColor={colors.diffRemoved}
593
+ color={colors.diffRemovedText}
594
+ dimColor={dimmed}
595
+ >
596
+ {paddedNum} -{" "}
597
+ </Text>
598
+ <View flexShrink={1}>
599
+ <Text
600
+ backgroundColor={colors.diffRemoved}
601
+ color={colors.diffRemovedText}
602
+ dimColor={dimmed}
603
+ wrap="wrap"
604
+ >
605
+ {hlContent}
606
+ </Text>
607
+ </View>
608
+ </View>
609
+ );
610
+ }
611
+
612
+ if (line.type === "added") {
613
+ return (
614
+ <View key={idx} flexDirection="row" backgroundColor={colors.diffAdded}>
615
+ <Text
616
+ backgroundColor={colors.diffAdded}
617
+ color={colors.diffAddedText}
618
+ dimColor={dimmed}
619
+ >
620
+ {paddedNum} +{" "}
621
+ </Text>
622
+ <View flexShrink={1}>
623
+ <Text
624
+ backgroundColor={colors.diffAdded}
625
+ color={colors.diffAddedText}
626
+ dimColor={dimmed}
627
+ wrap="wrap"
628
+ >
629
+ {hlContent}
630
+ </Text>
631
+ </View>
632
+ </View>
633
+ );
634
+ }
635
+
636
+ return (
637
+ <View key={idx} flexDirection="row">
638
+ <Text dimColor>{paddedNum} </Text>
639
+ <View flexShrink={1}>
640
+ <Text dimColor wrap="wrap">
641
+ {hlContent}
642
+ </Text>
643
+ </View>
644
+ </View>
645
+ );
646
+ })}
647
+ </View>
648
+ );
649
+ }
@@ -0,0 +1,35 @@
1
+ import { z } from "zod";
2
+
3
+ function normalizeNumberInput(value: unknown): unknown {
4
+ if (typeof value !== "string") {
5
+ return value;
6
+ }
7
+
8
+ const trimmed = value.trim();
9
+ if (!trimmed) {
10
+ return Number.NaN;
11
+ }
12
+
13
+ return Number(trimmed);
14
+ }
15
+
16
+ function normalizeBooleanInput(value: unknown): unknown {
17
+ if (typeof value !== "string") {
18
+ return value;
19
+ }
20
+
21
+ const normalized = value.trim().toLowerCase();
22
+ if (["true", "1", "yes", "y"].includes(normalized)) {
23
+ return true;
24
+ }
25
+ if (["false", "0", "no", "n"].includes(normalized)) {
26
+ return false;
27
+ }
28
+
29
+ return value;
30
+ }
31
+
32
+ export const coerceNumber = z.preprocess(normalizeNumberInput, z.number());
33
+ export const coerceOptionalNumber = z.preprocess(normalizeNumberInput, z.number().optional());
34
+ export const coerceBoolean = z.preprocess(normalizeBooleanInput, z.boolean());
35
+ export const coerceOptionalBoolean = z.preprocess(normalizeBooleanInput, z.boolean().optional());
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "files": [],
3
+ "references": [
4
+ {
5
+ "path": "./tsconfig.node.json"
6
+ },
7
+ {
8
+ "path": "./tsconfig.test.json"
9
+ }
10
+ ],
11
+ "compilerOptions": {
12
+ "paths": {
13
+ "@/package.json": ["./package.json"]
14
+ }
15
+ }
16
+ }