@golba98/codexa 1.0.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 (226) hide show
  1. package/README.md +320 -0
  2. package/bin/codexa.js +445 -0
  3. package/package.json +45 -0
  4. package/scripts/audit-codexa-capabilities.mjs +466 -0
  5. package/scripts/smoke-terminal-bench.mjs +35 -0
  6. package/src/app.tsx +4561 -0
  7. package/src/appRenderStability.test.ts +131 -0
  8. package/src/commands/handler.test.ts +643 -0
  9. package/src/commands/handler.ts +875 -0
  10. package/src/config/launchArgs.test.ts +158 -0
  11. package/src/config/launchArgs.ts +186 -0
  12. package/src/config/layeredConfig.test.ts +143 -0
  13. package/src/config/layeredConfig.ts +836 -0
  14. package/src/config/persistence.test.ts +110 -0
  15. package/src/config/persistence.ts +311 -0
  16. package/src/config/runtimeConfig.test.ts +218 -0
  17. package/src/config/runtimeConfig.ts +554 -0
  18. package/src/config/settings.test.ts +155 -0
  19. package/src/config/settings.ts +401 -0
  20. package/src/config/toml-serialize.ts +98 -0
  21. package/src/config/trustStore.test.ts +29 -0
  22. package/src/config/trustStore.ts +68 -0
  23. package/src/core/attachments.test.ts +155 -0
  24. package/src/core/attachments.ts +71 -0
  25. package/src/core/auth/codexAuth.test.ts +68 -0
  26. package/src/core/auth/codexAuth.ts +359 -0
  27. package/src/core/cleanupFastFail.test.ts +76 -0
  28. package/src/core/cleanupFastFail.ts +67 -0
  29. package/src/core/clipboard.ts +24 -0
  30. package/src/core/codex.ts +124 -0
  31. package/src/core/codexExecArgs.test.ts +195 -0
  32. package/src/core/codexExecArgs.ts +152 -0
  33. package/src/core/codexLaunch.test.ts +205 -0
  34. package/src/core/codexLaunch.ts +162 -0
  35. package/src/core/codexPrompt.test.ts +252 -0
  36. package/src/core/codexPrompt.ts +428 -0
  37. package/src/core/executables/claudeExecutable.ts +63 -0
  38. package/src/core/executables/codexExecutable.test.ts +212 -0
  39. package/src/core/executables/codexExecutable.ts +159 -0
  40. package/src/core/executables/executableResolver.test.ts +129 -0
  41. package/src/core/executables/executableResolver.ts +138 -0
  42. package/src/core/executables/geminiExecutable.test.ts +116 -0
  43. package/src/core/executables/geminiExecutable.ts +78 -0
  44. package/src/core/executables/pathSanityScan.test.ts +47 -0
  45. package/src/core/githubDiagnostics.test.ts +92 -0
  46. package/src/core/githubDiagnostics.ts +222 -0
  47. package/src/core/hollowResponseFormat.test.ts +58 -0
  48. package/src/core/hollowResponseFormat.ts +39 -0
  49. package/src/core/inputDebug.ts +51 -0
  50. package/src/core/launchContext.test.ts +157 -0
  51. package/src/core/launchContext.ts +266 -0
  52. package/src/core/models/codexCapabilities.test.ts +45 -0
  53. package/src/core/models/codexCapabilities.ts +95 -0
  54. package/src/core/models/codexModelCapabilities.test.ts +246 -0
  55. package/src/core/models/codexModelCapabilities.ts +571 -0
  56. package/src/core/models/modelSpecs.test.ts +283 -0
  57. package/src/core/models/modelSpecs.ts +300 -0
  58. package/src/core/perf/profiler.ts +125 -0
  59. package/src/core/perf/renderDebug.test.ts +230 -0
  60. package/src/core/perf/renderDebug.ts +373 -0
  61. package/src/core/planStorage.test.ts +143 -0
  62. package/src/core/planStorage.ts +141 -0
  63. package/src/core/process/CommandRunner.test.ts +105 -0
  64. package/src/core/process/CommandRunner.ts +269 -0
  65. package/src/core/process/processValidation.ts +101 -0
  66. package/src/core/projectInstructions.test.ts +50 -0
  67. package/src/core/projectInstructions.ts +54 -0
  68. package/src/core/providerLauncher/launcher.test.ts +238 -0
  69. package/src/core/providerLauncher/launcher.ts +203 -0
  70. package/src/core/providerLauncher/registry.test.ts +324 -0
  71. package/src/core/providerLauncher/registry.ts +253 -0
  72. package/src/core/providerLauncher/types.ts +84 -0
  73. package/src/core/providerLauncher/workspaceConfig.test.ts +638 -0
  74. package/src/core/providerLauncher/workspaceConfig.ts +407 -0
  75. package/src/core/providerRuntime/anthropic.test.ts +1120 -0
  76. package/src/core/providerRuntime/anthropic.ts +576 -0
  77. package/src/core/providerRuntime/capabilityProfile.test.ts +311 -0
  78. package/src/core/providerRuntime/capabilityProfile.ts +288 -0
  79. package/src/core/providerRuntime/claudeCodeDiscovery.ts +446 -0
  80. package/src/core/providerRuntime/contextMetadata.test.ts +468 -0
  81. package/src/core/providerRuntime/contextMetadata.ts +409 -0
  82. package/src/core/providerRuntime/gemini.test.ts +437 -0
  83. package/src/core/providerRuntime/gemini.ts +784 -0
  84. package/src/core/providerRuntime/lmstudio.test.ts +168 -0
  85. package/src/core/providerRuntime/lmstudio.ts +118 -0
  86. package/src/core/providerRuntime/local.test.ts +787 -0
  87. package/src/core/providerRuntime/local.ts +754 -0
  88. package/src/core/providerRuntime/models.ts +150 -0
  89. package/src/core/providerRuntime/reasoning.ts +17 -0
  90. package/src/core/providerRuntime/registry.test.ts +233 -0
  91. package/src/core/providerRuntime/registry.ts +203 -0
  92. package/src/core/providerRuntime/types.ts +103 -0
  93. package/src/core/providers/codexJsonStream.test.ts +148 -0
  94. package/src/core/providers/codexJsonStream.ts +305 -0
  95. package/src/core/providers/codexSubprocess.test.ts +68 -0
  96. package/src/core/providers/codexSubprocess.ts +372 -0
  97. package/src/core/providers/codexTranscript.test.ts +284 -0
  98. package/src/core/providers/codexTranscript.ts +695 -0
  99. package/src/core/providers/openaiNative.ts +13 -0
  100. package/src/core/providers/registry.ts +21 -0
  101. package/src/core/providers/types.ts +59 -0
  102. package/src/core/terminal/terminalCapabilities.test.ts +93 -0
  103. package/src/core/terminal/terminalCapabilities.ts +100 -0
  104. package/src/core/terminal/terminalControl.test.ts +75 -0
  105. package/src/core/terminal/terminalControl.ts +147 -0
  106. package/src/core/terminal/terminalSanitize.test.ts +22 -0
  107. package/src/core/terminal/terminalSanitize.ts +147 -0
  108. package/src/core/terminal/terminalSelection.test.ts +42 -0
  109. package/src/core/terminal/terminalSelection.ts +66 -0
  110. package/src/core/terminal/terminalTitle.test.ts +328 -0
  111. package/src/core/terminal/terminalTitle.ts +483 -0
  112. package/src/core/workspaceActivity.test.ts +163 -0
  113. package/src/core/workspaceActivity.ts +380 -0
  114. package/src/core/workspaceGuard.test.ts +151 -0
  115. package/src/core/workspaceGuard.ts +288 -0
  116. package/src/core/workspaceRoot.test.ts +23 -0
  117. package/src/core/workspaceRoot.ts +47 -0
  118. package/src/exec.test.ts +13 -0
  119. package/src/exec.ts +72 -0
  120. package/src/headless/execArgs.test.ts +147 -0
  121. package/src/headless/execArgs.ts +294 -0
  122. package/src/headless/execRunner.test.ts +434 -0
  123. package/src/headless/execRunner.ts +304 -0
  124. package/src/index.test.tsx +618 -0
  125. package/src/index.tsx +296 -0
  126. package/src/session/appSession.test.ts +897 -0
  127. package/src/session/appSession.ts +761 -0
  128. package/src/session/chatLifecycle.test.ts +64 -0
  129. package/src/session/chatLifecycle.ts +951 -0
  130. package/src/session/liveRenderScheduler.test.ts +201 -0
  131. package/src/session/liveRenderScheduler.ts +214 -0
  132. package/src/session/planFlow.test.ts +103 -0
  133. package/src/session/planFlow.ts +149 -0
  134. package/src/session/planTranscript.test.ts +65 -0
  135. package/src/session/planTranscript.ts +15 -0
  136. package/src/session/promptRunSchedule.test.ts +36 -0
  137. package/src/session/promptRunSchedule.ts +26 -0
  138. package/src/session/types.ts +228 -0
  139. package/src/test/runtimeTestUtils.ts +14 -0
  140. package/src/types/react-dom.d.ts +3 -0
  141. package/src/ui/ActionRequiredBlock.tsx +38 -0
  142. package/src/ui/ActivityBars.tsx +68 -0
  143. package/src/ui/ActivityIndicator.test.tsx +58 -0
  144. package/src/ui/ActivityIndicator.tsx +58 -0
  145. package/src/ui/AgentBlock.test.ts +6 -0
  146. package/src/ui/AgentBlock.tsx +130 -0
  147. package/src/ui/AnimatedStatusText.test.ts +16 -0
  148. package/src/ui/AnimatedStatusText.tsx +69 -0
  149. package/src/ui/AppShell.test.tsx +1739 -0
  150. package/src/ui/AppShell.tsx +698 -0
  151. package/src/ui/AttachmentImportPanel.test.tsx +204 -0
  152. package/src/ui/AttachmentImportPanel.tsx +98 -0
  153. package/src/ui/AuthPanel.tsx +113 -0
  154. package/src/ui/BackendPicker.tsx +28 -0
  155. package/src/ui/BottomComposer.test.ts +674 -0
  156. package/src/ui/BottomComposer.tsx +1028 -0
  157. package/src/ui/CodexLogo.tsx +55 -0
  158. package/src/ui/DashCard.tsx +82 -0
  159. package/src/ui/Markdown.test.ts +157 -0
  160. package/src/ui/Markdown.tsx +310 -0
  161. package/src/ui/ModePicker.tsx +27 -0
  162. package/src/ui/ModelPicker.tsx +31 -0
  163. package/src/ui/ModelPickerProviderScope.test.tsx +411 -0
  164. package/src/ui/ModelPickerScreen.test.tsx +99 -0
  165. package/src/ui/ModelPickerScreen.tsx +416 -0
  166. package/src/ui/ModelPickerState.test.tsx +151 -0
  167. package/src/ui/ModelReasoningPicker.test.tsx +447 -0
  168. package/src/ui/ModelReasoningPicker.tsx +458 -0
  169. package/src/ui/Panel.tsx +51 -0
  170. package/src/ui/PermissionsPanel.tsx +78 -0
  171. package/src/ui/PlanActionPicker.tsx +119 -0
  172. package/src/ui/PlanReviewPanel.test.tsx +267 -0
  173. package/src/ui/PlanReviewPanel.tsx +212 -0
  174. package/src/ui/PromptCardBorder.test.tsx +161 -0
  175. package/src/ui/ProviderPicker.test.tsx +289 -0
  176. package/src/ui/ProviderPicker.tsx +321 -0
  177. package/src/ui/ProviderShortcut.test.tsx +143 -0
  178. package/src/ui/ReasoningPicker.tsx +46 -0
  179. package/src/ui/RunFooter.tsx +65 -0
  180. package/src/ui/SelectionPanel.tsx +67 -0
  181. package/src/ui/SettingsPanel.test.tsx +233 -0
  182. package/src/ui/SettingsPanel.tsx +156 -0
  183. package/src/ui/Spinner.tsx +25 -0
  184. package/src/ui/StaticIntroItem.tsx +54 -0
  185. package/src/ui/StaticTranscriptItem.tsx +56 -0
  186. package/src/ui/TextEntryPanel.tsx +139 -0
  187. package/src/ui/ThemePicker.tsx +31 -0
  188. package/src/ui/ThinkingBlock.tsx +100 -0
  189. package/src/ui/Timeline.test.ts +2067 -0
  190. package/src/ui/Timeline.tsx +1472 -0
  191. package/src/ui/TimelineNavigation.test.tsx +201 -0
  192. package/src/ui/TopHeader.test.tsx +239 -0
  193. package/src/ui/TopHeader.tsx +257 -0
  194. package/src/ui/TurnGroup.test.tsx +365 -0
  195. package/src/ui/TurnGroup.tsx +657 -0
  196. package/src/ui/busyStatusAnimation.test.ts +30 -0
  197. package/src/ui/busyStatusAnimation.ts +11 -0
  198. package/src/ui/commandNormalize.test.ts +142 -0
  199. package/src/ui/commandNormalize.ts +66 -0
  200. package/src/ui/diffRenderer.test.ts +102 -0
  201. package/src/ui/diffRenderer.ts +116 -0
  202. package/src/ui/focus.ts +61 -0
  203. package/src/ui/focusFlow.test.tsx +1098 -0
  204. package/src/ui/inputBuffer.test.ts +151 -0
  205. package/src/ui/inputBuffer.ts +203 -0
  206. package/src/ui/layout.test.ts +145 -0
  207. package/src/ui/layout.ts +287 -0
  208. package/src/ui/modeDisplay.test.ts +42 -0
  209. package/src/ui/modeDisplay.ts +52 -0
  210. package/src/ui/outputPipeline.ts +64 -0
  211. package/src/ui/progressEntries.ts +156 -0
  212. package/src/ui/runActivityView.test.ts +89 -0
  213. package/src/ui/runActivityView.ts +37 -0
  214. package/src/ui/runLifecycleView.test.tsx +237 -0
  215. package/src/ui/slashCommands.ts +41 -0
  216. package/src/ui/statusRenderIsolation.test.tsx +654 -0
  217. package/src/ui/terminalAnswerFormat.test.ts +19 -0
  218. package/src/ui/terminalAnswerFormat.ts +128 -0
  219. package/src/ui/textLayout.test.ts +18 -0
  220. package/src/ui/textLayout.ts +338 -0
  221. package/src/ui/theme.tsx +395 -0
  222. package/src/ui/themeFlow.test.ts +53 -0
  223. package/src/ui/themeFlow.ts +41 -0
  224. package/src/ui/timelineMeasure.ts +3088 -0
  225. package/src/ui/timelineMeasureCache.test.ts +986 -0
  226. package/src/ui/useThrottledValue.ts +31 -0
@@ -0,0 +1,289 @@
1
+ import assert from "node:assert/strict";
2
+ import { PassThrough } from "node:stream";
3
+ import test from "node:test";
4
+ import React from "react";
5
+ import { Box, Text, render } from "ink";
6
+ import { buildProviderRegistry } from "../core/providerLauncher/registry.js";
7
+ import type { ProviderId, ProviderPickerAction } from "../core/providerLauncher/types.js";
8
+ import { createLayoutSnapshot } from "./layout.js";
9
+ import { ProviderPicker } from "./ProviderPicker.js";
10
+ import { ThemeProvider } from "./theme.js";
11
+
12
+ class TestInput extends PassThrough {
13
+ readonly isTTY = true;
14
+
15
+ setRawMode(): this {
16
+ return this;
17
+ }
18
+
19
+ override resume(): this {
20
+ return this;
21
+ }
22
+
23
+ override pause(): this {
24
+ return this;
25
+ }
26
+
27
+ ref(): this {
28
+ return this;
29
+ }
30
+
31
+ unref(): this {
32
+ return this;
33
+ }
34
+ }
35
+
36
+ class TestOutput extends PassThrough {
37
+ readonly isTTY = true;
38
+ columns = 120;
39
+ rows = 40;
40
+ }
41
+
42
+ function stripAnsi(value: string): string {
43
+ return value.replace(/\u001B\[[0-?]*[ -/]*[@-~]/g, "");
44
+ }
45
+
46
+ function sleep(ms = 50): Promise<void> {
47
+ return new Promise((resolve) => setTimeout(resolve, ms));
48
+ }
49
+
50
+ function createInkHarness(node: React.ReactElement) {
51
+ const stdin = new TestInput();
52
+ const stdout = new TestOutput();
53
+ let output = "";
54
+
55
+ stdout.on("data", (chunk) => {
56
+ output += chunk.toString();
57
+ });
58
+
59
+ const instance = render(node, {
60
+ stdin: stdin as unknown as NodeJS.ReadStream,
61
+ stdout: stdout as unknown as NodeJS.WriteStream,
62
+ stderr: stdout as unknown as NodeJS.WriteStream,
63
+ debug: true,
64
+ exitOnCtrlC: false,
65
+ patchConsole: false,
66
+ });
67
+
68
+ return {
69
+ stdin,
70
+ getOutput(): string {
71
+ return stripAnsi(output);
72
+ },
73
+ async cleanup() {
74
+ instance.cleanup();
75
+ await sleep(20);
76
+ },
77
+ };
78
+ }
79
+
80
+ function ProviderPickerHarness() {
81
+ const [action, setAction] = React.useState("none");
82
+ const providers = buildProviderRegistry({
83
+ activeModel: "gpt-5.4",
84
+ workspaceConfig: { workspaceDefaultProviderId: "openai" },
85
+ });
86
+
87
+ return (
88
+ <ThemeProvider theme="purple">
89
+ <Box flexDirection="column">
90
+ <ProviderPicker
91
+ layout={createLayoutSnapshot(120, 40)}
92
+ providers={providers}
93
+ onAction={(providerId: ProviderId, nextAction: ProviderPickerAction) => {
94
+ setAction(`${providerId}:${nextAction}`);
95
+ }}
96
+ onCancel={() => setAction("cancel")}
97
+ />
98
+ <Text>{`action:${action}`}</Text>
99
+ </Box>
100
+ </ThemeProvider>
101
+ );
102
+ }
103
+
104
+ test("provider picker renders compact aligned provider rows", async () => {
105
+ const harness = createInkHarness(<ProviderPickerHarness />);
106
+
107
+ try {
108
+ await sleep(80);
109
+ const output = harness.getOutput();
110
+ assert.match(output, /Providers/);
111
+ assert.match(output, /Enter = select, U = use, S = set default, Esc = cancel/);
112
+ assert.match(output, /OpenAI/);
113
+ assert.match(output, /Anthropic/);
114
+ assert.match(output, /Google/);
115
+ assert.match(output, /Local/);
116
+ assert.doesNotMatch(output, /Antigravity/);
117
+ assert.match(output, /Context/);
118
+ assert.match(output, /Tool/);
119
+ assert.match(output, /Strm/);
120
+ assert.match(output, /Unknown/);
121
+ assert.match(output, /\?/);
122
+ assert.match(output, /Disabled/);
123
+ assert.doesNotMatch(output, /0\/unknown/);
124
+ } finally {
125
+ await harness.cleanup();
126
+ }
127
+ });
128
+
129
+ test("provider picker stays readable in a cramped terminal layout", async () => {
130
+ const providers = buildProviderRegistry({ activeModel: "gpt-5.4-mini" });
131
+ const harness = createInkHarness(
132
+ <ThemeProvider theme="purple">
133
+ <ProviderPicker
134
+ layout={createLayoutSnapshot(44, 18)}
135
+ providers={providers}
136
+ onAction={() => {}}
137
+ onCancel={() => {}}
138
+ />
139
+ </ThemeProvider>,
140
+ );
141
+
142
+ try {
143
+ await sleep(80);
144
+ const output = harness.getOutput();
145
+ assert.match(output, /Providers/);
146
+ assert.match(output, /Enter select/);
147
+ assert.doesNotMatch(output, /Gemini CLIEnabled/);
148
+ assert.match(output, /OpenAI/);
149
+ assert.match(output, /Local/);
150
+ assert.match(output, /Disabled/);
151
+ assert.doesNotMatch(output, /undefined/);
152
+ } finally {
153
+ await harness.cleanup();
154
+ }
155
+ });
156
+
157
+ test("provider picker supports setting default with S", async () => {
158
+ const harness = createInkHarness(<ProviderPickerHarness />);
159
+
160
+ try {
161
+ await sleep(80);
162
+ harness.stdin.write("\u001b[B");
163
+ await sleep(40);
164
+ harness.stdin.write("s");
165
+ await sleep(80);
166
+
167
+ assert.match(harness.getOutput(), /action:anthropic:set-default/);
168
+ } finally {
169
+ await harness.cleanup();
170
+ }
171
+ });
172
+
173
+ test("provider picker opens action menu and selects launch", async () => {
174
+ const harness = createInkHarness(<ProviderPickerHarness />);
175
+
176
+ try {
177
+ await sleep(80);
178
+ harness.stdin.write("\u001b[B");
179
+ await sleep(40);
180
+ harness.stdin.write("\r");
181
+ await sleep(40);
182
+ assert.match(harness.getOutput(), /Provider action: Anthropic/);
183
+ assert.match(harness.getOutput(), /Use in Codexa/);
184
+ assert.match(harness.getOutput(), /Launch external CLI/);
185
+ harness.stdin.write("\u001b[B");
186
+ await sleep(40);
187
+ harness.stdin.write("\u001b[B");
188
+ await sleep(40);
189
+ harness.stdin.write("\u001b[B");
190
+ await sleep(40);
191
+ harness.stdin.write("\r");
192
+ await sleep(80);
193
+
194
+ assert.match(harness.getOutput(), /action:anthropic:launch/);
195
+ } finally {
196
+ await harness.cleanup();
197
+ }
198
+ });
199
+
200
+ test("provider picker reports Anthropic in-Codexa route actions without launching", async () => {
201
+ const harness = createInkHarness(<ProviderPickerHarness />);
202
+
203
+ try {
204
+ await sleep(80);
205
+ harness.stdin.write("\u001b[B");
206
+ await sleep(40);
207
+ harness.stdin.write("\r");
208
+ await sleep(40);
209
+ harness.stdin.write("\r");
210
+ await sleep(80);
211
+
212
+ assert.match(harness.getOutput(), /action:anthropic:use-in-codexa/);
213
+ } finally {
214
+ await harness.cleanup();
215
+ }
216
+ });
217
+
218
+ test("provider picker exposes Gemini diagnostics action", async () => {
219
+ const harness = createInkHarness(<ProviderPickerHarness />);
220
+
221
+ try {
222
+ await sleep(80);
223
+ harness.stdin.write("\u001b[B");
224
+ await sleep(40);
225
+ harness.stdin.write("\u001b[B");
226
+ await sleep(40);
227
+ harness.stdin.write("\r");
228
+ await sleep(40);
229
+ assert.match(harness.getOutput(), /Provider action: Google/);
230
+ assert.match(harness.getOutput(), /Run Gemini diagnostics/);
231
+ harness.stdin.write("\u001b[B");
232
+ await sleep(40);
233
+ harness.stdin.write("\u001b[B");
234
+ await sleep(40);
235
+ harness.stdin.write("\u001b[B");
236
+ await sleep(40);
237
+ harness.stdin.write("\r");
238
+ await sleep(80);
239
+
240
+ assert.match(harness.getOutput(), /action:google:run-diagnostics/);
241
+ } finally {
242
+ await harness.cleanup();
243
+ }
244
+ });
245
+
246
+ test("provider picker cancels from provider list with Esc", async () => {
247
+ const harness = createInkHarness(<ProviderPickerHarness />);
248
+
249
+ try {
250
+ await sleep(80);
251
+ harness.stdin.write("\u001b");
252
+ await sleep(80);
253
+
254
+ assert.match(harness.getOutput(), /action:cancel/);
255
+ } finally {
256
+ await harness.cleanup();
257
+ }
258
+ });
259
+
260
+ test('pressing U fires use-in-codexa for the selected provider', async () => {
261
+ const harness = createInkHarness(<ProviderPickerHarness />);
262
+
263
+ try {
264
+ await sleep(80);
265
+ harness.stdin.write('u');
266
+ await sleep(80);
267
+
268
+ assert.match(harness.getOutput(), /action:openai:use-in-codexa/);
269
+ } finally {
270
+ await harness.cleanup();
271
+ }
272
+ });
273
+
274
+ test('pressing U after navigating down fires use-in-codexa for the selected provider', async () => {
275
+ const harness = createInkHarness(<ProviderPickerHarness />);
276
+
277
+ try {
278
+ await sleep(80);
279
+ harness.stdin.write('j'); // down to Anthropic
280
+ await sleep(40);
281
+ harness.stdin.write('u');
282
+ await sleep(80);
283
+
284
+ assert.match(harness.getOutput(), /action:anthropic:use-in-codexa/);
285
+ } finally {
286
+ await harness.cleanup();
287
+ }
288
+ });
289
+
@@ -0,0 +1,321 @@
1
+ import React, { useMemo, useState } from "react";
2
+ import { Box, Text, useFocus, useInput } from "ink";
3
+ import type { ProviderConfig, ProviderId, ProviderPickerAction } from "../core/providerLauncher/types.js";
4
+ import { traceInputDebug } from "../core/inputDebug.js";
5
+ import { FOCUS_IDS } from "./focus.js";
6
+ import { clampVisualText, getShellWidth, type Layout } from "./layout.js";
7
+ import { useTheme } from "./theme.js";
8
+
9
+ // ─── Types & helpers ─────────────────────────────────────────────────────────
10
+
11
+ interface ProviderPickerProps {
12
+ layout: Layout;
13
+ providers: readonly ProviderConfig[];
14
+ onAction: (providerId: ProviderId, action: ProviderPickerAction) => void;
15
+ onCancel: () => void;
16
+ /** When set, the picker mounts directly at this provider's action panel. */
17
+ initialProviderId?: ProviderId;
18
+ }
19
+
20
+ interface ProviderActionItem {
21
+ value: ProviderPickerAction;
22
+ label: string;
23
+ disabledReason?: string | null;
24
+ }
25
+
26
+ function clampIndex(index: number, length: number): number {
27
+ if (length <= 0) return 0;
28
+ return Math.max(0, Math.min(length - 1, index));
29
+ }
30
+
31
+ // ─── Component ────────────────────────────────────────────────────────────────
32
+
33
+ export function ProviderPicker({ layout, providers, onAction, onCancel, initialProviderId }: ProviderPickerProps) {
34
+ const theme = useTheme();
35
+ const { isFocused } = useFocus({ id: FOCUS_IDS.providerPicker, autoFocus: true });
36
+ const initialIndex = initialProviderId
37
+ ? Math.max(0, providers.findIndex((p) => p.id === initialProviderId))
38
+ : 0;
39
+ const [providerIndex, setProviderIndex] = useState(initialIndex);
40
+ const [mode, setMode] = useState<"providers" | "actions">(
41
+ initialProviderId ? "actions" : "providers",
42
+ );
43
+ const [actionIndex, setActionIndex] = useState(0);
44
+
45
+ const selectedProvider = providers[clampIndex(providerIndex, providers.length)];
46
+ const shellWidth = getShellWidth(layout.cols);
47
+ const panelWidth = Math.max(42, Math.min(shellWidth - 2, layout.mode === "full" ? 86 : 72));
48
+ const innerWidth = Math.max(30, panelWidth - 4);
49
+ const markerWidth = 2;
50
+ const columnGaps = 4;
51
+ const columnWidthBudget = Math.max(26, innerWidth - markerWidth - columnGaps);
52
+ const statusWidth = Math.min(8, Math.max(6, columnWidthBudget - 20));
53
+ const toolsWidth = 4;
54
+ const streamWidth = 4;
55
+ const contextWidth = Math.min(11, Math.max(7, columnWidthBudget - statusWidth - toolsWidth - streamWidth - 16));
56
+ const providerNameWidth = Math.min(layout.mode === "micro" ? 10 : 14, Math.max(8, columnWidthBudget - statusWidth - toolsWidth - streamWidth - contextWidth - 8));
57
+ const modelWidth = Math.max(5, columnWidthBudget - providerNameWidth - contextWidth - toolsWidth - streamWidth - statusWidth);
58
+
59
+ const helpText = layout.mode === "micro"
60
+ ? "Enter select U use S default Esc"
61
+ : "Enter = select, U = use, S = set default, Esc = cancel";
62
+ const title = mode === "actions" && selectedProvider
63
+ ? `Provider action: ${selectedProvider.displayName}`
64
+ : "Providers";
65
+ const actions = useMemo<ProviderActionItem[]>(() => {
66
+ const routeUnavailable = selectedProvider?.routeMode === "in-codexa"
67
+ ? null
68
+ : selectedProvider?.routeUnavailableReason ?? "In-Codexa routing is not configured yet.";
69
+
70
+ return [
71
+ { value: "use-in-codexa", label: "Use in Codexa", disabledReason: routeUnavailable },
72
+ { value: "select-model", label: "Select model", disabledReason: routeUnavailable },
73
+ { value: "refresh-models", label: selectedProvider?.id === "anthropic" ? "Refresh Claude capabilities" : selectedProvider?.id === "local" ? "Refresh LM Studio metadata" : "Refresh models", disabledReason: routeUnavailable },
74
+ ...(selectedProvider?.id === "google" || selectedProvider?.id === "local"
75
+ ? [{ value: "run-diagnostics" as const, label: selectedProvider.id === "local" ? "Run Local diagnostics" : "Run Gemini diagnostics" }]
76
+ : []),
77
+ { value: "launch", label: "Launch external CLI" },
78
+ { value: "set-default", label: "Set as workspace default" },
79
+ { value: "cancel", label: "Cancel" },
80
+ ];
81
+ }, [selectedProvider]);
82
+
83
+ useInput((input, key) => {
84
+ traceInputDebug("provider_picker_input", {
85
+ handler: "ProviderPicker.useInput",
86
+ input,
87
+ return: Boolean(key.return),
88
+ escape: Boolean(key.escape),
89
+ upArrow: Boolean(key.upArrow),
90
+ downArrow: Boolean(key.downArrow),
91
+ mode,
92
+ providerIndex,
93
+ actionIndex,
94
+ });
95
+
96
+ if (key.ctrl && (input === "c" || input === "q")) {
97
+ onCancel();
98
+ return;
99
+ }
100
+
101
+ if (key.escape) {
102
+ if (mode === "actions") {
103
+ setMode("providers");
104
+ setActionIndex(0);
105
+ return;
106
+ }
107
+ onCancel();
108
+ return;
109
+ }
110
+
111
+ if (mode === "providers") {
112
+ if (key.upArrow || input === "k") {
113
+ setProviderIndex((current) => clampIndex(current - 1, providers.length));
114
+ return;
115
+ }
116
+ if (key.downArrow || input === "j") {
117
+ setProviderIndex((current) => clampIndex(current + 1, providers.length));
118
+ return;
119
+ }
120
+ if (input.toLowerCase() === "s" && selectedProvider) {
121
+ onAction(selectedProvider.id, "set-default");
122
+ return;
123
+ }
124
+ if (input.toLowerCase() === "u" && selectedProvider) {
125
+ onAction(selectedProvider.id, "use-in-codexa");
126
+ return;
127
+ }
128
+ if (key.return && selectedProvider) {
129
+ setMode("actions");
130
+ setActionIndex(0);
131
+ }
132
+ return;
133
+ }
134
+
135
+ if (key.upArrow || input === "k") {
136
+ setActionIndex((current) => clampIndex(current - 1, actions.length));
137
+ return;
138
+ }
139
+ if (key.downArrow || input === "j") {
140
+ setActionIndex((current) => clampIndex(current + 1, actions.length));
141
+ return;
142
+ }
143
+ if (key.return && selectedProvider) {
144
+ onAction(selectedProvider.id, actions[actionIndex]?.value ?? "cancel");
145
+ }
146
+ }, { isActive: isFocused });
147
+
148
+ const body = useMemo(() => {
149
+ if (mode === "actions" && selectedProvider) {
150
+ const inCodexaAvailable = selectedProvider.routeMode === "in-codexa";
151
+ const isConfigured = inCodexaAvailable && !selectedProvider.routeUnavailableReason;
152
+ const inCodexaStatusText = !inCodexaAvailable ? "Unavailable" : isConfigured ? "Available" : "Needs configuration";
153
+ const inCodexaStatusColor = !inCodexaAvailable ? theme.ERROR : isConfigured ? theme.SUCCESS : theme.WARNING;
154
+
155
+ return (
156
+ <Box flexDirection="column">
157
+ <Box marginBottom={1} flexDirection="column" paddingX={2}>
158
+ <Text color={theme.DIM}>Status: <Text color={theme.TEXT}>{selectedProvider.routeUnavailableReason ?? "Ready"}</Text></Text>
159
+ <Text color={theme.DIM}>Backend: <Text color={theme.TEXT}>{selectedProvider.backendType}</Text></Text>
160
+ <Text color={theme.DIM}>Use in Codexa: <Text color={inCodexaStatusColor}>{inCodexaStatusText}</Text></Text>
161
+ </Box>
162
+ {actions.map((action, index) => (
163
+ <ActionRow
164
+ key={action.value}
165
+ label={action.label}
166
+ disabledReason={action.disabledReason}
167
+ isHighlighted={index === actionIndex}
168
+ width={innerWidth}
169
+ />
170
+ ))}
171
+ </Box>
172
+ );
173
+ }
174
+
175
+ return providers.map((provider, index) => (
176
+ <ProviderRow
177
+ key={provider.id}
178
+ provider={provider}
179
+ isHighlighted={index === providerIndex}
180
+ widths={{ providerNameWidth, modelWidth, contextWidth, toolsWidth, streamWidth, statusWidth }}
181
+ />
182
+ ));
183
+ }, [actionIndex, actions, contextWidth, innerWidth, mode, modelWidth, providerIndex, providerNameWidth, providers, streamWidth, toolsWidth, statusWidth]);
184
+
185
+ return (
186
+ <Box flexDirection="column" width={panelWidth}>
187
+ <Box
188
+ borderStyle="round"
189
+ borderColor={theme.PROMPT}
190
+ paddingX={1}
191
+ paddingY={0}
192
+ width={panelWidth}
193
+ flexDirection="column"
194
+ >
195
+ <Box width="100%" overflow="hidden">
196
+ <Text color={theme.ACCENT} bold>
197
+ {clampVisualText(`${title} ${helpText}`, innerWidth)}
198
+ </Text>
199
+ </Box>
200
+
201
+ {mode === "providers" && (
202
+ <Box width="100%" overflow="hidden">
203
+ <Text color={theme.DIM}>
204
+ {" "}
205
+ {clampVisualText("Provider", providerNameWidth)}
206
+ {" "}
207
+ {clampVisualText("Model", modelWidth)}
208
+ {" "}
209
+ {clampVisualText("Context", contextWidth)}
210
+ {" "}
211
+ {clampVisualText("Tool", toolsWidth)}
212
+ {" "}
213
+ {clampVisualText("Strm", streamWidth)}
214
+ {" "}
215
+ {clampVisualText("Status", statusWidth)}
216
+ </Text>
217
+ </Box>
218
+ )}
219
+
220
+ <Box flexDirection="column" marginTop={0} width="100%">
221
+ {body}
222
+ </Box>
223
+ </Box>
224
+ </Box>
225
+ );
226
+ }
227
+
228
+ // ─── Subcomponents ───────────────────────────────────────────────────────────
229
+
230
+ function capabilityFlag(value: boolean | null | undefined): string {
231
+ if (value === true) return "Y";
232
+ if (value === false) return "N";
233
+ return "?";
234
+ }
235
+
236
+ function ProviderRow({
237
+ provider,
238
+ isHighlighted,
239
+ widths,
240
+ }: {
241
+ provider: ProviderConfig;
242
+ isHighlighted: boolean;
243
+ widths: {
244
+ providerNameWidth: number;
245
+ modelWidth: number;
246
+ contextWidth: number;
247
+ toolsWidth: number;
248
+ streamWidth: number;
249
+ statusWidth: number;
250
+ };
251
+ }) {
252
+ const theme = useTheme();
253
+ const statusColor = provider.isActiveRoute
254
+ ? theme.SUCCESS
255
+ : provider.enabled && !provider.routeUnavailableReason
256
+ ? theme.SUCCESS
257
+ : theme.WARNING;
258
+ const marker = isHighlighted ? ">" : " ";
259
+ const defaultMark = provider.isActiveRoute ? "@" : provider.isDefault ? "*" : " ";
260
+ const statusText = provider.isActiveRoute ? "Active" : provider.statusLabel;
261
+
262
+ return (
263
+ <Box width="100%" overflow="hidden">
264
+ <Box width={2} flexShrink={0}>
265
+ <Text color={isHighlighted ? theme.ACCENT : theme.DIM}>{marker}{defaultMark}</Text>
266
+ </Box>
267
+ <Box width={widths.providerNameWidth} flexShrink={0} overflow="hidden">
268
+ <Text color={isHighlighted ? theme.TEXT : theme.MUTED} bold={isHighlighted}>
269
+ {clampVisualText(provider.displayName, widths.providerNameWidth)}
270
+ </Text>
271
+ </Box>
272
+ <Text> </Text>
273
+ <Box width={widths.modelWidth} flexShrink={0} overflow="hidden">
274
+ <Text color={theme.MUTED}>{clampVisualText(provider.currentModel, widths.modelWidth)}</Text>
275
+ </Box>
276
+ <Text> </Text>
277
+ <Box width={widths.contextWidth} flexShrink={0} overflow="hidden">
278
+ <Text color={theme.MUTED}>{clampVisualText(provider.contextLengthLabel ?? "Unknown", widths.contextWidth)}</Text>
279
+ </Box>
280
+ <Text> </Text>
281
+ <Box width={widths.toolsWidth} flexShrink={0} overflow="hidden">
282
+ <Text color={theme.MUTED}>{clampVisualText(capabilityFlag(provider.capabilityProfile?.supportsToolCalls), widths.toolsWidth)}</Text>
283
+ </Box>
284
+ <Text> </Text>
285
+ <Box width={widths.streamWidth} flexShrink={0} overflow="hidden">
286
+ <Text color={theme.MUTED}>{clampVisualText(capabilityFlag(provider.capabilityProfile?.supportsStreaming), widths.streamWidth)}</Text>
287
+ </Box>
288
+ <Text> </Text>
289
+ <Box width={widths.statusWidth} flexShrink={0} overflow="hidden">
290
+ <Text color={statusColor}>{clampVisualText(statusText, widths.statusWidth)}</Text>
291
+ </Box>
292
+ </Box>
293
+ );
294
+ }
295
+
296
+ function ActionRow({
297
+ label,
298
+ disabledReason,
299
+ isHighlighted,
300
+ width,
301
+ }: {
302
+ label: string;
303
+ disabledReason?: string | null;
304
+ isHighlighted: boolean;
305
+ width: number;
306
+ }) {
307
+ const theme = useTheme();
308
+ const text = disabledReason ? `${label} unavailable` : label;
309
+ return (
310
+ <Box width="100%" overflow="hidden">
311
+ <Box width={2} flexShrink={0}>
312
+ <Text color={isHighlighted ? theme.ACCENT : theme.DIM}>{isHighlighted ? ">" : " "}</Text>
313
+ </Box>
314
+ <Box width={Math.max(10, width - 2)} flexShrink={0} overflow="hidden">
315
+ <Text color={disabledReason ? theme.DIM : isHighlighted ? theme.TEXT : theme.MUTED} bold={isHighlighted && !disabledReason}>
316
+ {clampVisualText(text, Math.max(10, width - 2))}
317
+ </Text>
318
+ </Box>
319
+ </Box>
320
+ );
321
+ }