@oh-my-pi/pi-coding-agent 1.337.0

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 (224) hide show
  1. package/CHANGELOG.md +1228 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/custom-tools.md +541 -0
  5. package/docs/extension-loading.md +1004 -0
  6. package/docs/hooks.md +867 -0
  7. package/docs/rpc.md +1040 -0
  8. package/docs/sdk.md +994 -0
  9. package/docs/session-tree-plan.md +441 -0
  10. package/docs/session.md +240 -0
  11. package/docs/skills.md +290 -0
  12. package/docs/theme.md +637 -0
  13. package/docs/tree.md +197 -0
  14. package/docs/tui.md +341 -0
  15. package/examples/README.md +21 -0
  16. package/examples/custom-tools/README.md +124 -0
  17. package/examples/custom-tools/hello/index.ts +20 -0
  18. package/examples/custom-tools/question/index.ts +84 -0
  19. package/examples/custom-tools/subagent/README.md +172 -0
  20. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  21. package/examples/custom-tools/subagent/agents/reviewer.md +35 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +81 -0
  57. package/src/cli/args.ts +246 -0
  58. package/src/cli/file-processor.ts +72 -0
  59. package/src/cli/list-models.ts +104 -0
  60. package/src/cli/plugin-cli.ts +650 -0
  61. package/src/cli/session-picker.ts +41 -0
  62. package/src/cli.ts +10 -0
  63. package/src/commands/init.md +20 -0
  64. package/src/config.ts +159 -0
  65. package/src/core/agent-session.ts +1900 -0
  66. package/src/core/auth-storage.ts +236 -0
  67. package/src/core/bash-executor.ts +196 -0
  68. package/src/core/compaction/branch-summarization.ts +343 -0
  69. package/src/core/compaction/compaction.ts +742 -0
  70. package/src/core/compaction/index.ts +7 -0
  71. package/src/core/compaction/utils.ts +154 -0
  72. package/src/core/custom-tools/index.ts +21 -0
  73. package/src/core/custom-tools/loader.ts +248 -0
  74. package/src/core/custom-tools/types.ts +169 -0
  75. package/src/core/custom-tools/wrapper.ts +28 -0
  76. package/src/core/exec.ts +129 -0
  77. package/src/core/export-html/index.ts +211 -0
  78. package/src/core/export-html/template.css +781 -0
  79. package/src/core/export-html/template.html +54 -0
  80. package/src/core/export-html/template.js +1185 -0
  81. package/src/core/export-html/vendor/highlight.min.js +1213 -0
  82. package/src/core/export-html/vendor/marked.min.js +6 -0
  83. package/src/core/hooks/index.ts +16 -0
  84. package/src/core/hooks/loader.ts +312 -0
  85. package/src/core/hooks/runner.ts +434 -0
  86. package/src/core/hooks/tool-wrapper.ts +99 -0
  87. package/src/core/hooks/types.ts +773 -0
  88. package/src/core/index.ts +52 -0
  89. package/src/core/mcp/client.ts +158 -0
  90. package/src/core/mcp/config.ts +154 -0
  91. package/src/core/mcp/index.ts +45 -0
  92. package/src/core/mcp/loader.ts +68 -0
  93. package/src/core/mcp/manager.ts +181 -0
  94. package/src/core/mcp/tool-bridge.ts +148 -0
  95. package/src/core/mcp/transports/http.ts +316 -0
  96. package/src/core/mcp/transports/index.ts +6 -0
  97. package/src/core/mcp/transports/stdio.ts +252 -0
  98. package/src/core/mcp/types.ts +220 -0
  99. package/src/core/messages.ts +189 -0
  100. package/src/core/model-registry.ts +317 -0
  101. package/src/core/model-resolver.ts +393 -0
  102. package/src/core/plugins/doctor.ts +59 -0
  103. package/src/core/plugins/index.ts +38 -0
  104. package/src/core/plugins/installer.ts +189 -0
  105. package/src/core/plugins/loader.ts +338 -0
  106. package/src/core/plugins/manager.ts +672 -0
  107. package/src/core/plugins/parser.ts +105 -0
  108. package/src/core/plugins/paths.ts +32 -0
  109. package/src/core/plugins/types.ts +190 -0
  110. package/src/core/sdk.ts +760 -0
  111. package/src/core/session-manager.ts +1128 -0
  112. package/src/core/settings-manager.ts +443 -0
  113. package/src/core/skills.ts +437 -0
  114. package/src/core/slash-commands.ts +248 -0
  115. package/src/core/system-prompt.ts +439 -0
  116. package/src/core/timings.ts +25 -0
  117. package/src/core/tools/ask.ts +211 -0
  118. package/src/core/tools/bash-interceptor.ts +120 -0
  119. package/src/core/tools/bash.ts +250 -0
  120. package/src/core/tools/context.ts +32 -0
  121. package/src/core/tools/edit-diff.ts +475 -0
  122. package/src/core/tools/edit.ts +208 -0
  123. package/src/core/tools/exa/company.ts +59 -0
  124. package/src/core/tools/exa/index.ts +64 -0
  125. package/src/core/tools/exa/linkedin.ts +59 -0
  126. package/src/core/tools/exa/logger.ts +56 -0
  127. package/src/core/tools/exa/mcp-client.ts +368 -0
  128. package/src/core/tools/exa/render.ts +196 -0
  129. package/src/core/tools/exa/researcher.ts +90 -0
  130. package/src/core/tools/exa/search.ts +337 -0
  131. package/src/core/tools/exa/types.ts +168 -0
  132. package/src/core/tools/exa/websets.ts +248 -0
  133. package/src/core/tools/find.ts +261 -0
  134. package/src/core/tools/grep.ts +555 -0
  135. package/src/core/tools/index.ts +202 -0
  136. package/src/core/tools/ls.ts +140 -0
  137. package/src/core/tools/lsp/client.ts +605 -0
  138. package/src/core/tools/lsp/config.ts +147 -0
  139. package/src/core/tools/lsp/edits.ts +101 -0
  140. package/src/core/tools/lsp/index.ts +804 -0
  141. package/src/core/tools/lsp/render.ts +447 -0
  142. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  143. package/src/core/tools/lsp/types.ts +463 -0
  144. package/src/core/tools/lsp/utils.ts +486 -0
  145. package/src/core/tools/notebook.ts +229 -0
  146. package/src/core/tools/path-utils.ts +61 -0
  147. package/src/core/tools/read.ts +240 -0
  148. package/src/core/tools/renderers.ts +540 -0
  149. package/src/core/tools/task/agents.ts +153 -0
  150. package/src/core/tools/task/artifacts.ts +114 -0
  151. package/src/core/tools/task/bundled-agents/browser.md +71 -0
  152. package/src/core/tools/task/bundled-agents/explore.md +82 -0
  153. package/src/core/tools/task/bundled-agents/plan.md +54 -0
  154. package/src/core/tools/task/bundled-agents/reviewer.md +59 -0
  155. package/src/core/tools/task/bundled-agents/task.md +53 -0
  156. package/src/core/tools/task/bundled-commands/architect-plan.md +10 -0
  157. package/src/core/tools/task/bundled-commands/implement-with-critic.md +11 -0
  158. package/src/core/tools/task/bundled-commands/implement.md +11 -0
  159. package/src/core/tools/task/commands.ts +213 -0
  160. package/src/core/tools/task/discovery.ts +208 -0
  161. package/src/core/tools/task/executor.ts +367 -0
  162. package/src/core/tools/task/index.ts +388 -0
  163. package/src/core/tools/task/model-resolver.ts +115 -0
  164. package/src/core/tools/task/parallel.ts +38 -0
  165. package/src/core/tools/task/render.ts +232 -0
  166. package/src/core/tools/task/types.ts +99 -0
  167. package/src/core/tools/truncate.ts +265 -0
  168. package/src/core/tools/web-fetch.ts +2370 -0
  169. package/src/core/tools/web-search/auth.ts +193 -0
  170. package/src/core/tools/web-search/index.ts +537 -0
  171. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  172. package/src/core/tools/web-search/providers/exa.ts +302 -0
  173. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  174. package/src/core/tools/web-search/render.ts +182 -0
  175. package/src/core/tools/web-search/types.ts +180 -0
  176. package/src/core/tools/write.ts +99 -0
  177. package/src/index.ts +176 -0
  178. package/src/main.ts +464 -0
  179. package/src/migrations.ts +135 -0
  180. package/src/modes/index.ts +43 -0
  181. package/src/modes/interactive/components/armin.ts +382 -0
  182. package/src/modes/interactive/components/assistant-message.ts +86 -0
  183. package/src/modes/interactive/components/bash-execution.ts +196 -0
  184. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  185. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  186. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  187. package/src/modes/interactive/components/custom-editor.ts +122 -0
  188. package/src/modes/interactive/components/diff.ts +147 -0
  189. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  190. package/src/modes/interactive/components/footer.ts +381 -0
  191. package/src/modes/interactive/components/hook-editor.ts +117 -0
  192. package/src/modes/interactive/components/hook-input.ts +64 -0
  193. package/src/modes/interactive/components/hook-message.ts +96 -0
  194. package/src/modes/interactive/components/hook-selector.ts +91 -0
  195. package/src/modes/interactive/components/model-selector.ts +247 -0
  196. package/src/modes/interactive/components/oauth-selector.ts +120 -0
  197. package/src/modes/interactive/components/plugin-settings.ts +479 -0
  198. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  199. package/src/modes/interactive/components/session-selector.ts +204 -0
  200. package/src/modes/interactive/components/settings-selector.ts +453 -0
  201. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  202. package/src/modes/interactive/components/theme-selector.ts +62 -0
  203. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  204. package/src/modes/interactive/components/tool-execution.ts +675 -0
  205. package/src/modes/interactive/components/tree-selector.ts +866 -0
  206. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  207. package/src/modes/interactive/components/user-message.ts +18 -0
  208. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  209. package/src/modes/interactive/components/welcome.ts +183 -0
  210. package/src/modes/interactive/interactive-mode.ts +2516 -0
  211. package/src/modes/interactive/theme/dark.json +101 -0
  212. package/src/modes/interactive/theme/light.json +98 -0
  213. package/src/modes/interactive/theme/theme-schema.json +308 -0
  214. package/src/modes/interactive/theme/theme.ts +998 -0
  215. package/src/modes/print-mode.ts +128 -0
  216. package/src/modes/rpc/rpc-client.ts +527 -0
  217. package/src/modes/rpc/rpc-mode.ts +483 -0
  218. package/src/modes/rpc/rpc-types.ts +203 -0
  219. package/src/utils/changelog.ts +99 -0
  220. package/src/utils/clipboard.ts +265 -0
  221. package/src/utils/fuzzy.ts +108 -0
  222. package/src/utils/mime.ts +30 -0
  223. package/src/utils/shell.ts +276 -0
  224. package/src/utils/tools-manager.ts +274 -0
@@ -0,0 +1,998 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { EditorTheme, MarkdownTheme, SelectListTheme } from "@oh-my-pi/pi-tui";
4
+ import { type Static, Type } from "@sinclair/typebox";
5
+ import { TypeCompiler } from "@sinclair/typebox/compiler";
6
+ import chalk from "chalk";
7
+ import { highlight, supportsLanguage } from "cli-highlight";
8
+ import { getCustomThemesDir, getThemesDir } from "../../../config.js";
9
+
10
+ // ============================================================================
11
+ // Types & Schema
12
+ // ============================================================================
13
+
14
+ const ColorValueSchema = Type.Union([
15
+ Type.String(), // hex "#ff0000", var ref "primary", or empty ""
16
+ Type.Integer({ minimum: 0, maximum: 255 }), // 256-color index
17
+ ]);
18
+
19
+ type ColorValue = Static<typeof ColorValueSchema>;
20
+
21
+ const ThemeJsonSchema = Type.Object({
22
+ $schema: Type.Optional(Type.String()),
23
+ name: Type.String(),
24
+ vars: Type.Optional(Type.Record(Type.String(), ColorValueSchema)),
25
+ colors: Type.Object({
26
+ // Core UI (10 colors)
27
+ accent: ColorValueSchema,
28
+ border: ColorValueSchema,
29
+ borderAccent: ColorValueSchema,
30
+ borderMuted: ColorValueSchema,
31
+ success: ColorValueSchema,
32
+ error: ColorValueSchema,
33
+ warning: ColorValueSchema,
34
+ muted: ColorValueSchema,
35
+ dim: ColorValueSchema,
36
+ text: ColorValueSchema,
37
+ thinkingText: ColorValueSchema,
38
+ // Backgrounds & Content Text (11 colors)
39
+ selectedBg: ColorValueSchema,
40
+ userMessageBg: ColorValueSchema,
41
+ userMessageText: ColorValueSchema,
42
+ customMessageBg: ColorValueSchema,
43
+ customMessageText: ColorValueSchema,
44
+ customMessageLabel: ColorValueSchema,
45
+ toolPendingBg: ColorValueSchema,
46
+ toolSuccessBg: ColorValueSchema,
47
+ toolErrorBg: ColorValueSchema,
48
+ toolTitle: ColorValueSchema,
49
+ toolOutput: ColorValueSchema,
50
+ // Markdown (10 colors)
51
+ mdHeading: ColorValueSchema,
52
+ mdLink: ColorValueSchema,
53
+ mdLinkUrl: ColorValueSchema,
54
+ mdCode: ColorValueSchema,
55
+ mdCodeBlock: ColorValueSchema,
56
+ mdCodeBlockBorder: ColorValueSchema,
57
+ mdQuote: ColorValueSchema,
58
+ mdQuoteBorder: ColorValueSchema,
59
+ mdHr: ColorValueSchema,
60
+ mdListBullet: ColorValueSchema,
61
+ // Tool Diffs (3 colors)
62
+ toolDiffAdded: ColorValueSchema,
63
+ toolDiffRemoved: ColorValueSchema,
64
+ toolDiffContext: ColorValueSchema,
65
+ // Syntax Highlighting (9 colors)
66
+ syntaxComment: ColorValueSchema,
67
+ syntaxKeyword: ColorValueSchema,
68
+ syntaxFunction: ColorValueSchema,
69
+ syntaxVariable: ColorValueSchema,
70
+ syntaxString: ColorValueSchema,
71
+ syntaxNumber: ColorValueSchema,
72
+ syntaxType: ColorValueSchema,
73
+ syntaxOperator: ColorValueSchema,
74
+ syntaxPunctuation: ColorValueSchema,
75
+ // Thinking Level Borders (6 colors)
76
+ thinkingOff: ColorValueSchema,
77
+ thinkingMinimal: ColorValueSchema,
78
+ thinkingLow: ColorValueSchema,
79
+ thinkingMedium: ColorValueSchema,
80
+ thinkingHigh: ColorValueSchema,
81
+ thinkingXhigh: ColorValueSchema,
82
+ // Bash Mode (1 color)
83
+ bashMode: ColorValueSchema,
84
+ // Footer Status Line (10 colors)
85
+ footerIcon: ColorValueSchema,
86
+ footerSep: ColorValueSchema,
87
+ footerModel: ColorValueSchema,
88
+ footerPath: ColorValueSchema,
89
+ footerBranch: ColorValueSchema,
90
+ footerStaged: ColorValueSchema,
91
+ footerDirty: ColorValueSchema,
92
+ footerUntracked: ColorValueSchema,
93
+ footerInput: ColorValueSchema,
94
+ footerOutput: ColorValueSchema,
95
+ footerCacheRead: ColorValueSchema,
96
+ footerCacheWrite: ColorValueSchema,
97
+ footerCost: ColorValueSchema,
98
+ }),
99
+ export: Type.Optional(
100
+ Type.Object({
101
+ pageBg: Type.Optional(ColorValueSchema),
102
+ cardBg: Type.Optional(ColorValueSchema),
103
+ infoBg: Type.Optional(ColorValueSchema),
104
+ }),
105
+ ),
106
+ });
107
+
108
+ type ThemeJson = Static<typeof ThemeJsonSchema>;
109
+
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TypeBox CJS/ESM type mismatch
111
+ const validateThemeJson = TypeCompiler.Compile(ThemeJsonSchema as any);
112
+
113
+ export type ThemeColor =
114
+ | "accent"
115
+ | "border"
116
+ | "borderAccent"
117
+ | "borderMuted"
118
+ | "success"
119
+ | "error"
120
+ | "warning"
121
+ | "muted"
122
+ | "dim"
123
+ | "text"
124
+ | "thinkingText"
125
+ | "userMessageText"
126
+ | "customMessageText"
127
+ | "customMessageLabel"
128
+ | "toolTitle"
129
+ | "toolOutput"
130
+ | "mdHeading"
131
+ | "mdLink"
132
+ | "mdLinkUrl"
133
+ | "mdCode"
134
+ | "mdCodeBlock"
135
+ | "mdCodeBlockBorder"
136
+ | "mdQuote"
137
+ | "mdQuoteBorder"
138
+ | "mdHr"
139
+ | "mdListBullet"
140
+ | "toolDiffAdded"
141
+ | "toolDiffRemoved"
142
+ | "toolDiffContext"
143
+ | "syntaxComment"
144
+ | "syntaxKeyword"
145
+ | "syntaxFunction"
146
+ | "syntaxVariable"
147
+ | "syntaxString"
148
+ | "syntaxNumber"
149
+ | "syntaxType"
150
+ | "syntaxOperator"
151
+ | "syntaxPunctuation"
152
+ | "thinkingOff"
153
+ | "thinkingMinimal"
154
+ | "thinkingLow"
155
+ | "thinkingMedium"
156
+ | "thinkingHigh"
157
+ | "thinkingXhigh"
158
+ | "bashMode"
159
+ | "footerIcon"
160
+ | "footerSep"
161
+ | "footerModel"
162
+ | "footerPath"
163
+ | "footerBranch"
164
+ | "footerStaged"
165
+ | "footerDirty"
166
+ | "footerUntracked"
167
+ | "footerInput"
168
+ | "footerOutput"
169
+ | "footerCacheRead"
170
+ | "footerCacheWrite"
171
+ | "footerCost";
172
+
173
+ export type ThemeBg =
174
+ | "selectedBg"
175
+ | "userMessageBg"
176
+ | "customMessageBg"
177
+ | "toolPendingBg"
178
+ | "toolSuccessBg"
179
+ | "toolErrorBg";
180
+
181
+ type ColorMode = "truecolor" | "256color";
182
+
183
+ // ============================================================================
184
+ // Color Utilities
185
+ // ============================================================================
186
+
187
+ function detectColorMode(): ColorMode {
188
+ const colorterm = process.env.COLORTERM;
189
+ if (colorterm === "truecolor" || colorterm === "24bit") {
190
+ return "truecolor";
191
+ }
192
+ // Windows Terminal supports truecolor
193
+ if (process.env.WT_SESSION) {
194
+ return "truecolor";
195
+ }
196
+ const term = process.env.TERM || "";
197
+ if (term.includes("256color")) {
198
+ return "256color";
199
+ }
200
+ return "256color";
201
+ }
202
+
203
+ function hexToRgb(hex: string): { r: number; g: number; b: number } {
204
+ const cleaned = hex.replace("#", "");
205
+ if (cleaned.length !== 6) {
206
+ throw new Error(`Invalid hex color: ${hex}`);
207
+ }
208
+ const r = parseInt(cleaned.substring(0, 2), 16);
209
+ const g = parseInt(cleaned.substring(2, 4), 16);
210
+ const b = parseInt(cleaned.substring(4, 6), 16);
211
+ if (Number.isNaN(r) || Number.isNaN(g) || Number.isNaN(b)) {
212
+ throw new Error(`Invalid hex color: ${hex}`);
213
+ }
214
+ return { r, g, b };
215
+ }
216
+
217
+ // The 6x6x6 color cube channel values (indices 0-5)
218
+ const CUBE_VALUES = [0, 95, 135, 175, 215, 255];
219
+
220
+ // Grayscale ramp values (indices 232-255, 24 grays from 8 to 238)
221
+ const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10);
222
+
223
+ function findClosestCubeIndex(value: number): number {
224
+ let minDist = Infinity;
225
+ let minIdx = 0;
226
+ for (let i = 0; i < CUBE_VALUES.length; i++) {
227
+ const dist = Math.abs(value - CUBE_VALUES[i]);
228
+ if (dist < minDist) {
229
+ minDist = dist;
230
+ minIdx = i;
231
+ }
232
+ }
233
+ return minIdx;
234
+ }
235
+
236
+ function findClosestGrayIndex(gray: number): number {
237
+ let minDist = Infinity;
238
+ let minIdx = 0;
239
+ for (let i = 0; i < GRAY_VALUES.length; i++) {
240
+ const dist = Math.abs(gray - GRAY_VALUES[i]);
241
+ if (dist < minDist) {
242
+ minDist = dist;
243
+ minIdx = i;
244
+ }
245
+ }
246
+ return minIdx;
247
+ }
248
+
249
+ function colorDistance(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number): number {
250
+ // Weighted Euclidean distance (human eye is more sensitive to green)
251
+ const dr = r1 - r2;
252
+ const dg = g1 - g2;
253
+ const db = b1 - b2;
254
+ return dr * dr * 0.299 + dg * dg * 0.587 + db * db * 0.114;
255
+ }
256
+
257
+ function rgbTo256(r: number, g: number, b: number): number {
258
+ // Find closest color in the 6x6x6 cube
259
+ const rIdx = findClosestCubeIndex(r);
260
+ const gIdx = findClosestCubeIndex(g);
261
+ const bIdx = findClosestCubeIndex(b);
262
+ const cubeR = CUBE_VALUES[rIdx];
263
+ const cubeG = CUBE_VALUES[gIdx];
264
+ const cubeB = CUBE_VALUES[bIdx];
265
+ const cubeIndex = 16 + 36 * rIdx + 6 * gIdx + bIdx;
266
+ const cubeDist = colorDistance(r, g, b, cubeR, cubeG, cubeB);
267
+
268
+ // Find closest grayscale
269
+ const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
270
+ const grayIdx = findClosestGrayIndex(gray);
271
+ const grayValue = GRAY_VALUES[grayIdx];
272
+ const grayIndex = 232 + grayIdx;
273
+ const grayDist = colorDistance(r, g, b, grayValue, grayValue, grayValue);
274
+
275
+ // Check if color has noticeable saturation (hue matters)
276
+ // If max-min spread is significant, prefer cube to preserve tint
277
+ const maxC = Math.max(r, g, b);
278
+ const minC = Math.min(r, g, b);
279
+ const spread = maxC - minC;
280
+
281
+ // Only consider grayscale if color is nearly neutral (spread < 10)
282
+ // AND grayscale is actually closer
283
+ if (spread < 10 && grayDist < cubeDist) {
284
+ return grayIndex;
285
+ }
286
+
287
+ return cubeIndex;
288
+ }
289
+
290
+ function hexTo256(hex: string): number {
291
+ const { r, g, b } = hexToRgb(hex);
292
+ return rgbTo256(r, g, b);
293
+ }
294
+
295
+ function fgAnsi(color: string | number, mode: ColorMode): string {
296
+ if (color === "") return "\x1b[39m";
297
+ if (typeof color === "number") return `\x1b[38;5;${color}m`;
298
+ if (color.startsWith("#")) {
299
+ if (mode === "truecolor") {
300
+ const { r, g, b } = hexToRgb(color);
301
+ return `\x1b[38;2;${r};${g};${b}m`;
302
+ } else {
303
+ const index = hexTo256(color);
304
+ return `\x1b[38;5;${index}m`;
305
+ }
306
+ }
307
+ throw new Error(`Invalid color value: ${color}`);
308
+ }
309
+
310
+ function bgAnsi(color: string | number, mode: ColorMode): string {
311
+ if (color === "") return "\x1b[49m";
312
+ if (typeof color === "number") return `\x1b[48;5;${color}m`;
313
+ if (color.startsWith("#")) {
314
+ if (mode === "truecolor") {
315
+ const { r, g, b } = hexToRgb(color);
316
+ return `\x1b[48;2;${r};${g};${b}m`;
317
+ } else {
318
+ const index = hexTo256(color);
319
+ return `\x1b[48;5;${index}m`;
320
+ }
321
+ }
322
+ throw new Error(`Invalid color value: ${color}`);
323
+ }
324
+
325
+ function resolveVarRefs(
326
+ value: ColorValue,
327
+ vars: Record<string, ColorValue>,
328
+ visited = new Set<string>(),
329
+ ): string | number {
330
+ if (typeof value === "number" || value === "" || value.startsWith("#")) {
331
+ return value;
332
+ }
333
+ if (visited.has(value)) {
334
+ throw new Error(`Circular variable reference detected: ${value}`);
335
+ }
336
+ if (!(value in vars)) {
337
+ throw new Error(`Variable reference not found: ${value}`);
338
+ }
339
+ visited.add(value);
340
+ return resolveVarRefs(vars[value], vars, visited);
341
+ }
342
+
343
+ function resolveThemeColors<T extends Record<string, ColorValue>>(
344
+ colors: T,
345
+ vars: Record<string, ColorValue> = {},
346
+ ): Record<keyof T, string | number> {
347
+ const resolved: Record<string, string | number> = {};
348
+ for (const [key, value] of Object.entries(colors)) {
349
+ resolved[key] = resolveVarRefs(value, vars);
350
+ }
351
+ return resolved as Record<keyof T, string | number>;
352
+ }
353
+
354
+ // ============================================================================
355
+ // Theme Class
356
+ // ============================================================================
357
+
358
+ export class Theme {
359
+ private fgColors: Map<ThemeColor, string>;
360
+ private bgColors: Map<ThemeBg, string>;
361
+ private mode: ColorMode;
362
+
363
+ constructor(
364
+ fgColors: Record<ThemeColor, string | number>,
365
+ bgColors: Record<ThemeBg, string | number>,
366
+ mode: ColorMode,
367
+ ) {
368
+ this.mode = mode;
369
+ this.fgColors = new Map();
370
+ for (const [key, value] of Object.entries(fgColors) as [ThemeColor, string | number][]) {
371
+ this.fgColors.set(key, fgAnsi(value, mode));
372
+ }
373
+ this.bgColors = new Map();
374
+ for (const [key, value] of Object.entries(bgColors) as [ThemeBg, string | number][]) {
375
+ this.bgColors.set(key, bgAnsi(value, mode));
376
+ }
377
+ }
378
+
379
+ fg(color: ThemeColor, text: string): string {
380
+ const ansi = this.fgColors.get(color);
381
+ if (!ansi) throw new Error(`Unknown theme color: ${color}`);
382
+ return `${ansi}${text}\x1b[39m`; // Reset only foreground color
383
+ }
384
+
385
+ bg(color: ThemeBg, text: string): string {
386
+ const ansi = this.bgColors.get(color);
387
+ if (!ansi) throw new Error(`Unknown theme background color: ${color}`);
388
+ return `${ansi}${text}\x1b[49m`; // Reset only background color
389
+ }
390
+
391
+ bold(text: string): string {
392
+ return chalk.bold(text);
393
+ }
394
+
395
+ italic(text: string): string {
396
+ return chalk.italic(text);
397
+ }
398
+
399
+ underline(text: string): string {
400
+ return chalk.underline(text);
401
+ }
402
+
403
+ inverse(text: string): string {
404
+ return chalk.inverse(text);
405
+ }
406
+
407
+ getFgAnsi(color: ThemeColor): string {
408
+ const ansi = this.fgColors.get(color);
409
+ if (!ansi) throw new Error(`Unknown theme color: ${color}`);
410
+ return ansi;
411
+ }
412
+
413
+ getBgAnsi(color: ThemeBg): string {
414
+ const ansi = this.bgColors.get(color);
415
+ if (!ansi) throw new Error(`Unknown theme background color: ${color}`);
416
+ return ansi;
417
+ }
418
+
419
+ getColorMode(): ColorMode {
420
+ return this.mode;
421
+ }
422
+
423
+ getThinkingBorderColor(level: "off" | "minimal" | "low" | "medium" | "high" | "xhigh"): (str: string) => string {
424
+ // Map thinking levels to dedicated theme colors
425
+ switch (level) {
426
+ case "off":
427
+ return (str: string) => this.fg("thinkingOff", str);
428
+ case "minimal":
429
+ return (str: string) => this.fg("thinkingMinimal", str);
430
+ case "low":
431
+ return (str: string) => this.fg("thinkingLow", str);
432
+ case "medium":
433
+ return (str: string) => this.fg("thinkingMedium", str);
434
+ case "high":
435
+ return (str: string) => this.fg("thinkingHigh", str);
436
+ case "xhigh":
437
+ return (str: string) => this.fg("thinkingXhigh", str);
438
+ default:
439
+ return (str: string) => this.fg("thinkingOff", str);
440
+ }
441
+ }
442
+
443
+ getBashModeBorderColor(): (str: string) => string {
444
+ return (str: string) => this.fg("bashMode", str);
445
+ }
446
+ }
447
+
448
+ // ============================================================================
449
+ // Theme Loading
450
+ // ============================================================================
451
+
452
+ let BUILTIN_THEMES: Record<string, ThemeJson> | undefined;
453
+
454
+ function getBuiltinThemes(): Record<string, ThemeJson> {
455
+ if (!BUILTIN_THEMES) {
456
+ const themesDir = getThemesDir();
457
+ const darkPath = path.join(themesDir, "dark.json");
458
+ const lightPath = path.join(themesDir, "light.json");
459
+ BUILTIN_THEMES = {
460
+ dark: JSON.parse(fs.readFileSync(darkPath, "utf-8")) as ThemeJson,
461
+ light: JSON.parse(fs.readFileSync(lightPath, "utf-8")) as ThemeJson,
462
+ };
463
+ }
464
+ return BUILTIN_THEMES;
465
+ }
466
+
467
+ export function getAvailableThemes(): string[] {
468
+ const themes = new Set<string>(Object.keys(getBuiltinThemes()));
469
+ const customThemesDir = getCustomThemesDir();
470
+ if (fs.existsSync(customThemesDir)) {
471
+ const files = fs.readdirSync(customThemesDir);
472
+ for (const file of files) {
473
+ if (file.endsWith(".json")) {
474
+ themes.add(file.slice(0, -5));
475
+ }
476
+ }
477
+ }
478
+ return Array.from(themes).sort();
479
+ }
480
+
481
+ function loadThemeJson(name: string): ThemeJson {
482
+ const builtinThemes = getBuiltinThemes();
483
+ if (name in builtinThemes) {
484
+ return builtinThemes[name];
485
+ }
486
+ const customThemesDir = getCustomThemesDir();
487
+ const themePath = path.join(customThemesDir, `${name}.json`);
488
+ if (!fs.existsSync(themePath)) {
489
+ throw new Error(`Theme not found: ${name}`);
490
+ }
491
+ const content = fs.readFileSync(themePath, "utf-8");
492
+ let json: unknown;
493
+ try {
494
+ json = JSON.parse(content);
495
+ } catch (error) {
496
+ throw new Error(`Failed to parse theme ${name}: ${error}`);
497
+ }
498
+ if (!validateThemeJson.Check(json)) {
499
+ const errors = Array.from(validateThemeJson.Errors(json));
500
+ const missingColors: string[] = [];
501
+ const otherErrors: string[] = [];
502
+
503
+ for (const e of errors) {
504
+ // Check for missing required color properties
505
+ const match = e.path.match(/^\/colors\/(\w+)$/);
506
+ if (match && e.message.includes("Required")) {
507
+ missingColors.push(match[1]);
508
+ } else {
509
+ otherErrors.push(` - ${e.path}: ${e.message}`);
510
+ }
511
+ }
512
+
513
+ let errorMessage = `Invalid theme "${name}":\n`;
514
+ if (missingColors.length > 0) {
515
+ errorMessage += `\nMissing required color tokens:\n`;
516
+ errorMessage += missingColors.map((c) => ` - ${c}`).join("\n");
517
+ errorMessage += `\n\nPlease add these colors to your theme's "colors" object.`;
518
+ errorMessage += `\nSee the built-in themes (dark.json, light.json) for reference values.`;
519
+ }
520
+ if (otherErrors.length > 0) {
521
+ errorMessage += `\n\nOther errors:\n${otherErrors.join("\n")}`;
522
+ }
523
+
524
+ throw new Error(errorMessage);
525
+ }
526
+ return json as ThemeJson;
527
+ }
528
+
529
+ function createTheme(themeJson: ThemeJson, mode?: ColorMode): Theme {
530
+ const colorMode = mode ?? detectColorMode();
531
+ const resolvedColors = resolveThemeColors(themeJson.colors, themeJson.vars);
532
+ const fgColors: Record<ThemeColor, string | number> = {} as Record<ThemeColor, string | number>;
533
+ const bgColors: Record<ThemeBg, string | number> = {} as Record<ThemeBg, string | number>;
534
+ const bgColorKeys: Set<string> = new Set([
535
+ "selectedBg",
536
+ "userMessageBg",
537
+ "customMessageBg",
538
+ "toolPendingBg",
539
+ "toolSuccessBg",
540
+ "toolErrorBg",
541
+ ]);
542
+ for (const [key, value] of Object.entries(resolvedColors)) {
543
+ if (bgColorKeys.has(key)) {
544
+ bgColors[key as ThemeBg] = value;
545
+ } else {
546
+ fgColors[key as ThemeColor] = value;
547
+ }
548
+ }
549
+ return new Theme(fgColors, bgColors, colorMode);
550
+ }
551
+
552
+ function loadTheme(name: string, mode?: ColorMode): Theme {
553
+ const themeJson = loadThemeJson(name);
554
+ return createTheme(themeJson, mode);
555
+ }
556
+
557
+ function detectTerminalBackground(): "dark" | "light" {
558
+ const colorfgbg = process.env.COLORFGBG || "";
559
+ if (colorfgbg) {
560
+ const parts = colorfgbg.split(";");
561
+ if (parts.length >= 2) {
562
+ const bg = parseInt(parts[1], 10);
563
+ if (!Number.isNaN(bg)) {
564
+ const result = bg < 8 ? "dark" : "light";
565
+ return result;
566
+ }
567
+ }
568
+ }
569
+ return "dark";
570
+ }
571
+
572
+ function getDefaultTheme(): string {
573
+ return detectTerminalBackground();
574
+ }
575
+
576
+ // ============================================================================
577
+ // Global Theme Instance
578
+ // ============================================================================
579
+
580
+ export let theme: Theme;
581
+ let currentThemeName: string | undefined;
582
+ let themeWatcher: fs.FSWatcher | undefined;
583
+ let onThemeChangeCallback: (() => void) | undefined;
584
+
585
+ export function initTheme(themeName?: string, enableWatcher: boolean = false): void {
586
+ const name = themeName ?? getDefaultTheme();
587
+ currentThemeName = name;
588
+ try {
589
+ theme = loadTheme(name);
590
+ if (enableWatcher) {
591
+ startThemeWatcher();
592
+ }
593
+ } catch (_error) {
594
+ // Theme is invalid - fall back to dark theme silently
595
+ currentThemeName = "dark";
596
+ theme = loadTheme("dark");
597
+ // Don't start watcher for fallback theme
598
+ }
599
+ }
600
+
601
+ export function setTheme(name: string, enableWatcher: boolean = false): { success: boolean; error?: string } {
602
+ currentThemeName = name;
603
+ try {
604
+ theme = loadTheme(name);
605
+ if (enableWatcher) {
606
+ startThemeWatcher();
607
+ }
608
+ return { success: true };
609
+ } catch (error) {
610
+ // Theme is invalid - fall back to dark theme
611
+ currentThemeName = "dark";
612
+ theme = loadTheme("dark");
613
+ // Don't start watcher for fallback theme
614
+ return {
615
+ success: false,
616
+ error: error instanceof Error ? error.message : String(error),
617
+ };
618
+ }
619
+ }
620
+
621
+ export function onThemeChange(callback: () => void): void {
622
+ onThemeChangeCallback = callback;
623
+ }
624
+
625
+ function startThemeWatcher(): void {
626
+ // Stop existing watcher if any
627
+ if (themeWatcher) {
628
+ themeWatcher.close();
629
+ themeWatcher = undefined;
630
+ }
631
+
632
+ // Only watch if it's a custom theme (not built-in)
633
+ if (!currentThemeName || currentThemeName === "dark" || currentThemeName === "light") {
634
+ return;
635
+ }
636
+
637
+ const customThemesDir = getCustomThemesDir();
638
+ const themeFile = path.join(customThemesDir, `${currentThemeName}.json`);
639
+
640
+ // Only watch if the file exists
641
+ if (!fs.existsSync(themeFile)) {
642
+ return;
643
+ }
644
+
645
+ try {
646
+ themeWatcher = fs.watch(themeFile, (eventType) => {
647
+ if (eventType === "change") {
648
+ // Debounce rapid changes
649
+ setTimeout(() => {
650
+ try {
651
+ // Reload the theme
652
+ theme = loadTheme(currentThemeName!);
653
+ // Notify callback (to invalidate UI)
654
+ if (onThemeChangeCallback) {
655
+ onThemeChangeCallback();
656
+ }
657
+ } catch (_error) {
658
+ // Ignore errors (file might be in invalid state while being edited)
659
+ }
660
+ }, 100);
661
+ } else if (eventType === "rename") {
662
+ // File was deleted or renamed - fall back to default theme
663
+ setTimeout(() => {
664
+ if (!fs.existsSync(themeFile)) {
665
+ currentThemeName = "dark";
666
+ theme = loadTheme("dark");
667
+ if (themeWatcher) {
668
+ themeWatcher.close();
669
+ themeWatcher = undefined;
670
+ }
671
+ if (onThemeChangeCallback) {
672
+ onThemeChangeCallback();
673
+ }
674
+ }
675
+ }, 100);
676
+ }
677
+ });
678
+ } catch (_error) {
679
+ // Ignore errors starting watcher
680
+ }
681
+ }
682
+
683
+ export function stopThemeWatcher(): void {
684
+ if (themeWatcher) {
685
+ themeWatcher.close();
686
+ themeWatcher = undefined;
687
+ }
688
+ }
689
+
690
+ // ============================================================================
691
+ // HTML Export Helpers
692
+ // ============================================================================
693
+
694
+ /**
695
+ * Convert a 256-color index to hex string.
696
+ * Indices 0-15: basic colors (approximate)
697
+ * Indices 16-231: 6x6x6 color cube
698
+ * Indices 232-255: grayscale ramp
699
+ */
700
+ function ansi256ToHex(index: number): string {
701
+ // Basic colors (0-15) - approximate common terminal values
702
+ const basicColors = [
703
+ "#000000",
704
+ "#800000",
705
+ "#008000",
706
+ "#808000",
707
+ "#000080",
708
+ "#800080",
709
+ "#008080",
710
+ "#c0c0c0",
711
+ "#808080",
712
+ "#ff0000",
713
+ "#00ff00",
714
+ "#ffff00",
715
+ "#0000ff",
716
+ "#ff00ff",
717
+ "#00ffff",
718
+ "#ffffff",
719
+ ];
720
+ if (index < 16) {
721
+ return basicColors[index];
722
+ }
723
+
724
+ // Color cube (16-231): 6x6x6 = 216 colors
725
+ if (index < 232) {
726
+ const cubeIndex = index - 16;
727
+ const r = Math.floor(cubeIndex / 36);
728
+ const g = Math.floor((cubeIndex % 36) / 6);
729
+ const b = cubeIndex % 6;
730
+ const toHex = (n: number) => (n === 0 ? 0 : 55 + n * 40).toString(16).padStart(2, "0");
731
+ return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
732
+ }
733
+
734
+ // Grayscale (232-255): 24 shades
735
+ const gray = 8 + (index - 232) * 10;
736
+ const grayHex = gray.toString(16).padStart(2, "0");
737
+ return `#${grayHex}${grayHex}${grayHex}`;
738
+ }
739
+
740
+ /**
741
+ * Get resolved theme colors as CSS-compatible hex strings.
742
+ * Used by HTML export to generate CSS custom properties.
743
+ */
744
+ export function getResolvedThemeColors(themeName?: string): Record<string, string> {
745
+ const name = themeName ?? getDefaultTheme();
746
+ const isLight = name === "light";
747
+ const themeJson = loadThemeJson(name);
748
+ const resolved = resolveThemeColors(themeJson.colors, themeJson.vars);
749
+
750
+ // Default text color for empty values (terminal uses default fg color)
751
+ const defaultText = isLight ? "#000000" : "#e5e5e7";
752
+
753
+ const cssColors: Record<string, string> = {};
754
+ for (const [key, value] of Object.entries(resolved)) {
755
+ if (typeof value === "number") {
756
+ cssColors[key] = ansi256ToHex(value);
757
+ } else if (value === "") {
758
+ // Empty means default terminal color - use sensible fallback for HTML
759
+ cssColors[key] = defaultText;
760
+ } else {
761
+ cssColors[key] = value;
762
+ }
763
+ }
764
+ return cssColors;
765
+ }
766
+
767
+ /**
768
+ * Check if a theme is a "light" theme (for CSS that needs light/dark variants).
769
+ */
770
+ export function isLightTheme(themeName?: string): boolean {
771
+ // Currently just check the name - could be extended to analyze colors
772
+ return themeName === "light";
773
+ }
774
+
775
+ /**
776
+ * Get explicit export colors from theme JSON, if specified.
777
+ * Returns undefined for each color that isn't explicitly set.
778
+ */
779
+ export function getThemeExportColors(themeName?: string): {
780
+ pageBg?: string;
781
+ cardBg?: string;
782
+ infoBg?: string;
783
+ } {
784
+ const name = themeName ?? getDefaultTheme();
785
+ try {
786
+ const themeJson = loadThemeJson(name);
787
+ const exportSection = themeJson.export;
788
+ if (!exportSection) return {};
789
+
790
+ const vars = themeJson.vars ?? {};
791
+ const resolve = (value: string | number | undefined): string | undefined => {
792
+ if (value === undefined) return undefined;
793
+ if (typeof value === "number") return ansi256ToHex(value);
794
+ if (value.startsWith("$")) {
795
+ const resolved = vars[value];
796
+ if (resolved === undefined) return undefined;
797
+ if (typeof resolved === "number") return ansi256ToHex(resolved);
798
+ return resolved;
799
+ }
800
+ return value;
801
+ };
802
+
803
+ return {
804
+ pageBg: resolve(exportSection.pageBg),
805
+ cardBg: resolve(exportSection.cardBg),
806
+ infoBg: resolve(exportSection.infoBg),
807
+ };
808
+ } catch {
809
+ return {};
810
+ }
811
+ }
812
+
813
+ // ============================================================================
814
+ // TUI Helpers
815
+ // ============================================================================
816
+
817
+ type CliHighlightTheme = Record<string, (s: string) => string>;
818
+
819
+ let cachedHighlightThemeFor: Theme | undefined;
820
+ let cachedCliHighlightTheme: CliHighlightTheme | undefined;
821
+
822
+ function buildCliHighlightTheme(t: Theme): CliHighlightTheme {
823
+ return {
824
+ keyword: (s: string) => t.fg("syntaxKeyword", s),
825
+ built_in: (s: string) => t.fg("syntaxType", s),
826
+ literal: (s: string) => t.fg("syntaxNumber", s),
827
+ number: (s: string) => t.fg("syntaxNumber", s),
828
+ string: (s: string) => t.fg("syntaxString", s),
829
+ comment: (s: string) => t.fg("syntaxComment", s),
830
+ function: (s: string) => t.fg("syntaxFunction", s),
831
+ title: (s: string) => t.fg("syntaxFunction", s),
832
+ class: (s: string) => t.fg("syntaxType", s),
833
+ type: (s: string) => t.fg("syntaxType", s),
834
+ attr: (s: string) => t.fg("syntaxVariable", s),
835
+ variable: (s: string) => t.fg("syntaxVariable", s),
836
+ params: (s: string) => t.fg("syntaxVariable", s),
837
+ operator: (s: string) => t.fg("syntaxOperator", s),
838
+ punctuation: (s: string) => t.fg("syntaxPunctuation", s),
839
+ };
840
+ }
841
+
842
+ function getCliHighlightTheme(t: Theme): CliHighlightTheme {
843
+ if (cachedHighlightThemeFor !== t || !cachedCliHighlightTheme) {
844
+ cachedHighlightThemeFor = t;
845
+ cachedCliHighlightTheme = buildCliHighlightTheme(t);
846
+ }
847
+ return cachedCliHighlightTheme;
848
+ }
849
+
850
+ /**
851
+ * Highlight code with syntax coloring based on file extension or language.
852
+ * Returns array of highlighted lines.
853
+ */
854
+ export function highlightCode(code: string, lang?: string): string[] {
855
+ // Validate language before highlighting to avoid stderr spam from cli-highlight
856
+ const validLang = lang && supportsLanguage(lang) ? lang : undefined;
857
+ const opts = {
858
+ language: validLang,
859
+ ignoreIllegals: true,
860
+ theme: getCliHighlightTheme(theme),
861
+ };
862
+ try {
863
+ return highlight(code, opts).split("\n");
864
+ } catch {
865
+ return code.split("\n");
866
+ }
867
+ }
868
+
869
+ /**
870
+ * Get language identifier from file path extension.
871
+ */
872
+ export function getLanguageFromPath(filePath: string): string | undefined {
873
+ const ext = filePath.split(".").pop()?.toLowerCase();
874
+ if (!ext) return undefined;
875
+
876
+ const extToLang: Record<string, string> = {
877
+ ts: "typescript",
878
+ tsx: "typescript",
879
+ js: "javascript",
880
+ jsx: "javascript",
881
+ mjs: "javascript",
882
+ cjs: "javascript",
883
+ py: "python",
884
+ rb: "ruby",
885
+ rs: "rust",
886
+ go: "go",
887
+ java: "java",
888
+ kt: "kotlin",
889
+ swift: "swift",
890
+ c: "c",
891
+ h: "c",
892
+ cpp: "cpp",
893
+ cc: "cpp",
894
+ cxx: "cpp",
895
+ hpp: "cpp",
896
+ cs: "csharp",
897
+ php: "php",
898
+ sh: "bash",
899
+ bash: "bash",
900
+ zsh: "bash",
901
+ fish: "fish",
902
+ ps1: "powershell",
903
+ sql: "sql",
904
+ html: "html",
905
+ htm: "html",
906
+ css: "css",
907
+ scss: "scss",
908
+ sass: "sass",
909
+ less: "less",
910
+ json: "json",
911
+ yaml: "yaml",
912
+ yml: "yaml",
913
+ toml: "toml",
914
+ xml: "xml",
915
+ md: "markdown",
916
+ markdown: "markdown",
917
+ dockerfile: "dockerfile",
918
+ makefile: "makefile",
919
+ cmake: "cmake",
920
+ lua: "lua",
921
+ perl: "perl",
922
+ r: "r",
923
+ scala: "scala",
924
+ clj: "clojure",
925
+ ex: "elixir",
926
+ exs: "elixir",
927
+ erl: "erlang",
928
+ hs: "haskell",
929
+ ml: "ocaml",
930
+ vim: "vim",
931
+ graphql: "graphql",
932
+ proto: "protobuf",
933
+ tf: "hcl",
934
+ hcl: "hcl",
935
+ };
936
+
937
+ return extToLang[ext];
938
+ }
939
+
940
+ export function getMarkdownTheme(): MarkdownTheme {
941
+ return {
942
+ heading: (text: string) => theme.fg("mdHeading", text),
943
+ link: (text: string) => theme.fg("mdLink", text),
944
+ linkUrl: (text: string) => theme.fg("mdLinkUrl", text),
945
+ code: (text: string) => theme.fg("mdCode", text),
946
+ codeBlock: (text: string) => theme.fg("mdCodeBlock", text),
947
+ codeBlockBorder: (text: string) => theme.fg("mdCodeBlockBorder", text),
948
+ quote: (text: string) => theme.fg("mdQuote", text),
949
+ quoteBorder: (text: string) => theme.fg("mdQuoteBorder", text),
950
+ hr: (text: string) => theme.fg("mdHr", text),
951
+ listBullet: (text: string) => theme.fg("mdListBullet", text),
952
+ bold: (text: string) => theme.bold(text),
953
+ italic: (text: string) => theme.italic(text),
954
+ underline: (text: string) => theme.underline(text),
955
+ strikethrough: (text: string) => chalk.strikethrough(text),
956
+ highlightCode: (code: string, lang?: string): string[] => {
957
+ // Validate language before highlighting to avoid stderr spam from cli-highlight
958
+ const validLang = lang && supportsLanguage(lang) ? lang : undefined;
959
+ const opts = {
960
+ language: validLang,
961
+ ignoreIllegals: true,
962
+ theme: getCliHighlightTheme(theme),
963
+ };
964
+ try {
965
+ return highlight(code, opts).split("\n");
966
+ } catch {
967
+ return code.split("\n").map((line) => theme.fg("mdCodeBlock", line));
968
+ }
969
+ },
970
+ };
971
+ }
972
+
973
+ export function getSelectListTheme(): SelectListTheme {
974
+ return {
975
+ selectedPrefix: (text: string) => theme.fg("accent", text),
976
+ selectedText: (text: string) => theme.fg("accent", text),
977
+ description: (text: string) => theme.fg("muted", text),
978
+ scrollInfo: (text: string) => theme.fg("muted", text),
979
+ noMatch: (text: string) => theme.fg("muted", text),
980
+ };
981
+ }
982
+
983
+ export function getEditorTheme(): EditorTheme {
984
+ return {
985
+ borderColor: (text: string) => theme.fg("borderMuted", text),
986
+ selectList: getSelectListTheme(),
987
+ };
988
+ }
989
+
990
+ export function getSettingsListTheme(): import("@oh-my-pi/pi-tui").SettingsListTheme {
991
+ return {
992
+ label: (text: string, selected: boolean) => (selected ? theme.fg("accent", text) : text),
993
+ value: (text: string, selected: boolean) => (selected ? theme.fg("accent", text) : theme.fg("muted", text)),
994
+ description: (text: string) => theme.fg("dim", text),
995
+ cursor: theme.fg("accent", "→ "),
996
+ hint: (text: string) => theme.fg("dim", text),
997
+ };
998
+ }