@odysx/codemind 0.1.0 → 0.1.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/dist/core/provider/index.d.ts +52 -10
  2. package/dist/core/provider/index.js +99 -12
  3. package/package.json +6 -1
  4. package/dist/commands/agents.d.ts.map +0 -1
  5. package/dist/commands/agents.js.map +0 -1
  6. package/dist/commands/auth.d.ts.map +0 -1
  7. package/dist/commands/auth.js.map +0 -1
  8. package/dist/commands/chat.d.ts.map +0 -1
  9. package/dist/commands/chat.js.map +0 -1
  10. package/dist/commands/keys.d.ts.map +0 -1
  11. package/dist/commands/keys.js.map +0 -1
  12. package/dist/commands/local.d.ts.map +0 -1
  13. package/dist/commands/local.js.map +0 -1
  14. package/dist/commands/mcp.d.ts.map +0 -1
  15. package/dist/commands/mcp.js.map +0 -1
  16. package/dist/commands/prompts.d.ts.map +0 -1
  17. package/dist/commands/prompts.js.map +0 -1
  18. package/dist/commands/skills.d.ts.map +0 -1
  19. package/dist/commands/skills.js.map +0 -1
  20. package/dist/core/auth/index.d.ts.map +0 -1
  21. package/dist/core/auth/index.js.map +0 -1
  22. package/dist/core/config/index.d.ts.map +0 -1
  23. package/dist/core/config/index.js.map +0 -1
  24. package/dist/core/history.d.ts.map +0 -1
  25. package/dist/core/history.js.map +0 -1
  26. package/dist/core/markdown-renderer.d.ts.map +0 -1
  27. package/dist/core/markdown-renderer.js.map +0 -1
  28. package/dist/core/permissions/index.d.ts.map +0 -1
  29. package/dist/core/permissions/index.js.map +0 -1
  30. package/dist/core/provider/external.d.ts.map +0 -1
  31. package/dist/core/provider/external.js.map +0 -1
  32. package/dist/core/provider/index.d.ts.map +0 -1
  33. package/dist/core/provider/index.js.map +0 -1
  34. package/dist/core/registry/index.d.ts.map +0 -1
  35. package/dist/core/registry/index.js.map +0 -1
  36. package/dist/core/schema/types.d.ts.map +0 -1
  37. package/dist/core/schema/types.js.map +0 -1
  38. package/dist/core/terminal.d.ts.map +0 -1
  39. package/dist/core/terminal.js.map +0 -1
  40. package/dist/core/workspace/index.d.ts.map +0 -1
  41. package/dist/core/workspace/index.js.map +0 -1
  42. package/dist/index.d.ts.map +0 -1
  43. package/dist/index.js.map +0 -1
  44. package/dist/ui/app.d.ts.map +0 -1
  45. package/dist/ui/app.js.map +0 -1
  46. package/dist/ui/commands/cases/catalog.d.ts.map +0 -1
  47. package/dist/ui/commands/cases/catalog.js.map +0 -1
  48. package/dist/ui/commands/cases/misc.d.ts.map +0 -1
  49. package/dist/ui/commands/cases/misc.js.map +0 -1
  50. package/dist/ui/commands/cases/tasks.d.ts.map +0 -1
  51. package/dist/ui/commands/cases/tasks.js.map +0 -1
  52. package/dist/ui/commands/runCommand.d.ts.map +0 -1
  53. package/dist/ui/commands/runCommand.js.map +0 -1
  54. package/dist/ui/components/AgentPicker.d.ts.map +0 -1
  55. package/dist/ui/components/AgentPicker.js.map +0 -1
  56. package/dist/ui/components/AuthPicker.d.ts.map +0 -1
  57. package/dist/ui/components/AuthPicker.js.map +0 -1
  58. package/dist/ui/components/Banner.d.ts.map +0 -1
  59. package/dist/ui/components/Banner.js.map +0 -1
  60. package/dist/ui/components/CommandMenu.d.ts.map +0 -1
  61. package/dist/ui/components/CommandMenu.js.map +0 -1
  62. package/dist/ui/components/ErrorBoundary.d.ts.map +0 -1
  63. package/dist/ui/components/ErrorBoundary.js.map +0 -1
  64. package/dist/ui/components/InputBar.d.ts.map +0 -1
  65. package/dist/ui/components/InputBar.js.map +0 -1
  66. package/dist/ui/components/KeysPanel.d.ts.map +0 -1
  67. package/dist/ui/components/KeysPanel.js.map +0 -1
  68. package/dist/ui/components/MessageView.d.ts.map +0 -1
  69. package/dist/ui/components/MessageView.js.map +0 -1
  70. package/dist/ui/components/PermissionPrompt.d.ts.map +0 -1
  71. package/dist/ui/components/PermissionPrompt.js.map +0 -1
  72. package/dist/ui/components/Pickers.d.ts.map +0 -1
  73. package/dist/ui/components/Pickers.js.map +0 -1
  74. package/dist/ui/components/SearchOverlay.d.ts.map +0 -1
  75. package/dist/ui/components/SearchOverlay.js.map +0 -1
  76. package/dist/ui/components/SessionPicker.d.ts.map +0 -1
  77. package/dist/ui/components/SessionPicker.js.map +0 -1
  78. package/dist/ui/components/WorkingIndicator.d.ts.map +0 -1
  79. package/dist/ui/components/WorkingIndicator.js.map +0 -1
  80. package/dist/ui/controls/ApiKeyEntry.d.ts.map +0 -1
  81. package/dist/ui/controls/ApiKeyEntry.js.map +0 -1
  82. package/dist/ui/controls/MultiSelect.d.ts.map +0 -1
  83. package/dist/ui/controls/MultiSelect.js.map +0 -1
  84. package/dist/ui/hooks/useAgentPicker.d.ts.map +0 -1
  85. package/dist/ui/hooks/useAgentPicker.js.map +0 -1
  86. package/dist/ui/hooks/useAuthFlow.d.ts.map +0 -1
  87. package/dist/ui/hooks/useAuthFlow.js.map +0 -1
  88. package/dist/ui/hooks/useKeysFlow.d.ts.map +0 -1
  89. package/dist/ui/hooks/useKeysFlow.js.map +0 -1
  90. package/dist/ui/hooks/useSessionPicker.d.ts.map +0 -1
  91. package/dist/ui/hooks/useSessionPicker.js.map +0 -1
  92. package/dist/ui/hooks/useTerminalSize.d.ts.map +0 -1
  93. package/dist/ui/hooks/useTerminalSize.js.map +0 -1
  94. package/dist/ui/input/keyRouter.d.ts.map +0 -1
  95. package/dist/ui/input/keyRouter.js.map +0 -1
  96. package/dist/ui/layouts/ScreenLayout.d.ts.map +0 -1
  97. package/dist/ui/layouts/ScreenLayout.js.map +0 -1
  98. package/dist/ui/shared/types.d.ts.map +0 -1
  99. package/dist/ui/shared/types.js.map +0 -1
  100. package/dist/ui/theme.d.ts.map +0 -1
  101. package/dist/ui/theme.js.map +0 -1
  102. package/dist/ui/utils/markdown.d.ts.map +0 -1
  103. package/dist/ui/utils/markdown.js.map +0 -1
  104. package/dist/ui/utils/mentions.d.ts.map +0 -1
  105. package/dist/ui/utils/mentions.js.map +0 -1
  106. package/dist/ui/utils/terminal-text.d.ts.map +0 -1
  107. package/dist/ui/utils/terminal-text.js.map +0 -1
  108. package/dist/ui/utils/text.d.ts.map +0 -1
  109. package/dist/ui/utils/text.js.map +0 -1
  110. package/eslint.config.js +0 -40
  111. package/graphify-out/GRAPH_REPORT.md +0 -140
  112. package/graphify-out/cache/1236b1806943f8eb5c64fece9fb448149370f4a1b52b32f0977ad528d7911291.json +0 -1
  113. package/graphify-out/cache/15cbf641dcc0fd641c18da872214918203a1f64baa7ac8430e4702ef32d18b02.json +0 -1
  114. package/graphify-out/cache/1abc52745bb46f6cc5a528530e2bc1f368c26e348dff85cdde5cad6f275d7177.json +0 -1
  115. package/graphify-out/cache/1c7815c37c13177e98895e272285f6151a8d0807478fc843ec452634b3e9912d.json +0 -1
  116. package/graphify-out/cache/1ec38c39f044f329c78642b61d6394fb8e15d0014335f32947d1f5c7b88c02bc.json +0 -1
  117. package/graphify-out/cache/20c9b57ff76dbb9d57cbd00659b0a42d2b28dd2abacf1ef4143cc67a5b6fe7f4.json +0 -1
  118. package/graphify-out/cache/285e11876884eb619e779d540cd957b33e70f14c67ecc26c9b9755aa9cedd717.json +0 -1
  119. package/graphify-out/cache/2c0a9c49ab51f4e0f07543b4e941bf802b7db31fa607c4cee8e595575be68fe7.json +0 -1
  120. package/graphify-out/cache/3025e6c2756c79ccaadd51d611f25a8b6630a6a11ce1b4e6e261b3e033dc194f.json +0 -1
  121. package/graphify-out/cache/4801a686ddfa0a52824a4fc3d65443cbb0ab9f0b030af67d02fccb520c584a08.json +0 -1
  122. package/graphify-out/cache/4f8fd96f38aa7d0293b4fab60c4581c4f3e68f4533c664c9cd78d1c53bea1cba.json +0 -1
  123. package/graphify-out/cache/5928e562b0f5700184b810e2403f4aa685e2b93c3c584c10460562b514c40fdd.json +0 -1
  124. package/graphify-out/cache/66132486efb0a4cfaeea766448f1f15bb33635808714052e7e24e5b00fbc984f.json +0 -1
  125. package/graphify-out/cache/66ab27669e7ec685130063662af9f30018c267f7639b4af4b0504a98282c383a.json +0 -1
  126. package/graphify-out/cache/6ff6b3dd433b5ffe96e166263a203dd622397e419dc00bb676824988b7defeab.json +0 -1
  127. package/graphify-out/cache/7a00a49932177a6215e2590b1ddfd6f1d8494792f693068e9d427d16b165d370.json +0 -1
  128. package/graphify-out/cache/7b681bc5182f40f707f1c0b16b78af38efffa62a1df315d10878f4de888408ef.json +0 -1
  129. package/graphify-out/cache/81196d44e50ac0b276d826a531ed10c425a528b298e3fe18e8d5a3eec996e9d5.json +0 -1
  130. package/graphify-out/cache/8934e9b32dea6e66fdc34fa1681b287ff1335c389917e0fc6ee2fc573e23e87b.json +0 -1
  131. package/graphify-out/cache/975f6c99807f86b79c80ca40c783618e2080a927eaebe6ea3db37b869f048851.json +0 -1
  132. package/graphify-out/cache/9ab98f08626fd8190462595e38de828293ad184db8df5e1177b05a8fe290faa1.json +0 -1
  133. package/graphify-out/cache/9c434a6796d7932b6e87a908c155c09cb2b8d097fc55f141f0302b6600f94749.json +0 -1
  134. package/graphify-out/cache/ac5cf4c489dc417c1f6ea728d0b6ba12ac7087bc6c2e736697ff3024b4d4e534.json +0 -1
  135. package/graphify-out/cache/ae5083b5c1dca3e5c6af485e7f6eb8a08ff8b382cdecf92ab5e0902053442afe.json +0 -1
  136. package/graphify-out/cache/b274057fc2e0118cf2746d1d8060841122bb2a2b0305566fc3f32d3a51e08bcc.json +0 -1
  137. package/graphify-out/cache/b39beef31da682387f030c0d149f6cd5b3e66effbf936cc8430db15360759dda.json +0 -1
  138. package/graphify-out/cache/bf3c6a36bb72820bc11cbf4a5c49067d1f62013d4686c019a4dbd9951b1e980e.json +0 -1
  139. package/graphify-out/cache/c034e156028d1a58b89bce659726e9a4065ddc4d2b560d6bce7a206fdff8dad3.json +0 -1
  140. package/graphify-out/cache/c619b5bbe8d1f427fbd7816d1e9fcfaf90d7201b020ae6b578166aeb9cb20683.json +0 -1
  141. package/graphify-out/cache/c8db77a9da0b0f601acfa50a620f31155cdda0d0cc0f2285adcea19e77194ee6.json +0 -1
  142. package/graphify-out/cache/ca664791ec7741a2077d9e7c10384142d30e7a3b77b388df719c315da608d57d.json +0 -1
  143. package/graphify-out/cache/cba43a116186bd55c8a63368dea4eee4a8123b2bec0a42df6efb3254f89f9a7a.json +0 -1
  144. package/graphify-out/cache/cd51cbb3dad1cf34417b9761fe2eb184a9f57d23dc86e45630e63bfa2ecafddc.json +0 -1
  145. package/graphify-out/cache/d06738cde193b34a136b94104220c17e0212b814b3e4d7ff399105635cae1a03.json +0 -1
  146. package/graphify-out/cache/d093c19f0169c0398eeb1360895b1c16ecc50abc59f4be8eec3f6fd27c89a6e5.json +0 -1
  147. package/graphify-out/cache/da8f75c438531d232fe114d5cbf2374576ce842a5fb87c7e5a88b78fdb334e5e.json +0 -1
  148. package/graphify-out/cache/e35798fa04ddede548a6953bdea506bd7d0788dfed4e8a8ea6ae77b1b60ef5e0.json +0 -1
  149. package/graphify-out/cache/e5a38c254719000487098782125e15e2e982c07d66b99d359ed1781f30f9fc8c.json +0 -1
  150. package/graphify-out/cache/e6e4c4a5a7200ee0154585b5d6538f699b0a746dbded3164f8391f06ce7cb212.json +0 -1
  151. package/graphify-out/cache/ea8d0838df6309b673b031f616b253af957898ee7289d25a541fa6183b732e4e.json +0 -1
  152. package/graphify-out/cache/ebaee92b0e881efd7a08f3143e2c1803310396b4651d4f77532003c699cc6836.json +0 -1
  153. package/graphify-out/cache/edf8543318a9e60d56d04704078da6c37202ddd6fdfc83144fe2b94fe19b80ef.json +0 -1
  154. package/graphify-out/cache/efca19d3668035f11126fc9eccbeab290abfa94babf3f9a8706de81d17aba263.json +0 -1
  155. package/graphify-out/cache/f1480bb642025ebd6d99986cdd8a7d5726897ec038300e4b095c9c938435dfad.json +0 -1
  156. package/graphify-out/cache/f459f21277f03865f732affb175b1b092954dae5967f27b3cd1864466e3e24f4.json +0 -1
  157. package/graphify-out/cache/f899e37e864010b30f4c196e86082d7bd022a0df032005e0407883ebcec36514.json +0 -1
  158. package/graphify-out/cache/fb3cbe07fbcd97c536cff9c6bc343c35f22d49613f05c53ad30ef67a75c5d93e.json +0 -1
  159. package/graphify-out/cache/fd3dc682b1ca2f5ae03b50d6b0e045ae50f5701c0a21c5a388d77ef482939d3a.json +0 -1
  160. package/graphify-out/graph.html +0 -257
  161. package/graphify-out/graph.json +0 -4396
  162. package/scripts/e2e-external.mjs +0 -65
  163. package/scripts/live-agents.mjs +0 -6
  164. package/scripts/live-stream.mjs +0 -51
  165. package/src/commands/agents.ts +0 -91
  166. package/src/commands/auth.ts +0 -39
  167. package/src/commands/chat.ts +0 -198
  168. package/src/commands/keys.ts +0 -83
  169. package/src/commands/local.ts +0 -302
  170. package/src/commands/mcp.ts +0 -74
  171. package/src/commands/prompts.ts +0 -58
  172. package/src/commands/skills.ts +0 -43
  173. package/src/core/auth/index.ts +0 -101
  174. package/src/core/config/index.ts +0 -152
  175. package/src/core/history.ts +0 -79
  176. package/src/core/markdown-renderer.ts +0 -420
  177. package/src/core/permissions/index.ts +0 -99
  178. package/src/core/provider/external.ts +0 -312
  179. package/src/core/provider/index.ts +0 -517
  180. package/src/core/registry/index.ts +0 -250
  181. package/src/core/schema/types.ts +0 -106
  182. package/src/core/terminal.ts +0 -124
  183. package/src/core/workspace/index.ts +0 -216
  184. package/src/index.ts +0 -401
  185. package/src/ui/app.tsx +0 -644
  186. package/src/ui/commands/cases/catalog.ts +0 -76
  187. package/src/ui/commands/cases/misc.ts +0 -293
  188. package/src/ui/commands/cases/tasks.ts +0 -115
  189. package/src/ui/commands/runCommand.ts +0 -178
  190. package/src/ui/components/Banner.tsx +0 -63
  191. package/src/ui/components/CommandMenu.tsx +0 -63
  192. package/src/ui/components/ErrorBoundary.tsx +0 -75
  193. package/src/ui/components/InputBar.tsx +0 -227
  194. package/src/ui/components/KeysPanel.tsx +0 -115
  195. package/src/ui/components/MessageView.tsx +0 -226
  196. package/src/ui/components/Pickers.tsx +0 -234
  197. package/src/ui/components/SearchOverlay.tsx +0 -65
  198. package/src/ui/components/WorkingIndicator.tsx +0 -35
  199. package/src/ui/controls/ApiKeyEntry.tsx +0 -29
  200. package/src/ui/controls/MultiSelect.tsx +0 -55
  201. package/src/ui/hooks/useAgentPicker.ts +0 -64
  202. package/src/ui/hooks/useAuthFlow.ts +0 -47
  203. package/src/ui/hooks/useKeysFlow.ts +0 -157
  204. package/src/ui/hooks/useSessionPicker.ts +0 -84
  205. package/src/ui/hooks/useTerminalSize.ts +0 -36
  206. package/src/ui/input/keyRouter.ts +0 -627
  207. package/src/ui/layouts/ScreenLayout.tsx +0 -202
  208. package/src/ui/shared/types.ts +0 -160
  209. package/src/ui/theme.ts +0 -53
  210. package/src/ui/utils/markdown.ts +0 -255
  211. package/src/ui/utils/mentions.ts +0 -40
  212. package/src/ui/utils/terminal-text.ts +0 -130
  213. package/src/ui/utils/text.ts +0 -174
  214. package/test/commands.test.mjs +0 -127
  215. package/test/config.test.mjs +0 -123
  216. package/test/history.test.mjs +0 -42
  217. package/test/keyrouter.test.mjs +0 -324
  218. package/test/layout.test.mjs +0 -271
  219. package/test/markdown.test.mjs +0 -92
  220. package/test/mentions.test.mjs +0 -51
  221. package/test/permissions.test.mjs +0 -130
  222. package/test/provider.test.mjs +0 -287
  223. package/test/terminal-text.test.mjs +0 -116
  224. package/test/text.test.mjs +0 -134
  225. package/test/workspace.test.mjs +0 -101
  226. package/tsconfig.json +0 -32
package/src/index.ts DELETED
@@ -1,401 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * OdysX CLI — Codemind
4
- * Interactive agent chat.
5
- */
6
- import { Command } from "commander";
7
- import { buildProgram } from "./core/registry/index.js";
8
- import { getConfig, updateConfig } from "./core/config/index.js";
9
- import { getPermissionMode, isPermissionMode } from "./core/permissions/index.js";
10
- import { getWorkspaceRoot, isFolderTrusted, trustFolder } from "./core/workspace/index.js";
11
- import { streamTurn } from "./core/provider/index.js";
12
- import { writeFileSync } from "node:fs";
13
- import { randomUUID } from "node:crypto";
14
- import { enterAltScreen, restoreScreen, exitWith } from "./core/terminal.js";
15
-
16
- // Command registration is a side effect of importing each module — these must
17
- // run before buildProgram() so the registry knows about every command.
18
- import "./commands/agents.js";
19
- import "./commands/auth.js";
20
- import "./commands/chat.js";
21
- import "./commands/keys.js";
22
- import "./commands/local.js";
23
- import "./commands/mcp.js";
24
- import "./commands/prompts.js";
25
- import "./commands/skills.js";
26
-
27
- // ── Colour ─────────────────────────────────────────────────────────────────
28
- // chalk, not literal escape codes: chalk normalizes every style to a no-op at
29
- // level 0, so piped output (`odysx | tee`, CI) and NO_COLOR stay clean without
30
- // a conditional at each call site. The level is set by the Ink theme, which
31
- // shares this module and applies the same policy; this path may run before it
32
- // (a one-shot subcommand never imports the theme), so apply it here too.
33
- import chalk from "chalk";
34
- import { colorEnabled, clearViewport } from "./core/terminal.js";
35
-
36
- if (!colorEnabled()) chalk.level = 0;
37
-
38
- const C = {
39
- bold: chalk.bold,
40
- dim: chalk.dim,
41
- user: chalk.bgHex("#5f87af").white,
42
- assistant: chalk.bgHex("#303030").hex("#d0d0d0"),
43
- };
44
-
45
- function promptStr(): string {
46
- return "❯ ";
47
- }
48
-
49
- function header(config: ReturnType<typeof getConfig>): string {
50
- const isAuth = config.apiKey || config.token;
51
- const status = isAuth ? "✓ auth" : "○ no auth";
52
- const agent = config.model ?? "master";
53
- return `
54
- ${C.bold(`Codemind v0.1.0 • agent ${agent} | ${status} | ${getPermissionMode()} mode`)}
55
- `;
56
- }
57
-
58
- function helpText(): string {
59
- return `
60
- ${C.bold("Commands:")}
61
- ${C.dim("/help")} Show help
62
- ${C.dim("/clear")} Clear screen
63
- ${C.dim("/status")} Session info
64
- ${C.dim("/agent")} ${C.dim("<id>")} Switch agent
65
- ${C.dim("/model")} ${C.dim("<id>")} Switch model
66
- ${C.dim("/quit")} Exit
67
- `;
68
- }
69
-
70
- function formatMessage(role: "user" | "assistant", text: string): string {
71
- return role === "user" ? C.user(text) : C.assistant(text);
72
- }
73
-
74
- const useLegacyRepl = process.argv.includes("--legacy-repl");
75
- if (useLegacyRepl) {
76
- // Strip the flag so commander (below) never sees an unknown option, and
77
- // drop the extra argv entry so hasArgs reflects the remaining args.
78
- process.argv = process.argv.filter((a) => a !== "--legacy-repl");
79
- }
80
- // Colour flags are consumed by terminal.ts (colorEnabled) for every writer;
81
- // strip them here too so commander doesn't reject them as unknown options.
82
- const COLOR_FLAGS = ["--color", "--colors", "--no-color", "--no-colors"];
83
- if (process.argv.some((a) => COLOR_FLAGS.includes(a))) {
84
- process.argv = process.argv.filter((a) => !COLOR_FLAGS.includes(a));
85
- }
86
- // --mode [plan|guide|manual|auto] sets the persisted permission posture. It is
87
- // consumed here (before commander) so the flag works both with the REPL and
88
- // with one-shot subcommands; an invalid value fails early with a clear list.
89
- let modeFlag: string | null = null;
90
- const MODE_FLAG = "--mode";
91
- const modeIdx = process.argv.indexOf(MODE_FLAG);
92
- if (modeIdx !== -1) {
93
- const given = process.argv[modeIdx + 1] ?? null;
94
- process.argv = process.argv.filter((_, i) => i !== modeIdx && i !== modeIdx + 1);
95
- if (!given || !isPermissionMode(given)) {
96
- process.stderr.write(`Invalid --mode. Choose: plan, guide, manual, auto.\n`);
97
- process.exit(1);
98
- }
99
- modeFlag = given;
100
- // Persist only after the REPL's trust gate has passed — an untrusted piped
101
- // invocation must not silently change the user's saved permission mode.
102
- }
103
- const hasArgs = process.argv.length > 2;
104
- // Ink needs a real TTY for raw-mode key handling, and it draws with cursor
105
- // control that only makes sense on a screen. Require BOTH ends: a TTY stdin
106
- // with redirected stdout (`odysx > transcript.txt`) used to run the full TUI
107
- // and write escape sequences into the file. Piped stdin (CI, `echo "/help" |
108
- // odysx`) falls back to readline instead of crashing on raw mode.
109
- const canRunInk = Boolean(process.stdin.isTTY) && Boolean(process.stdout.isTTY);
110
- const interactive = !hasArgs && canRunInk;
111
-
112
- // The legacy/piped path installs no handler of its own; a rejected promise in
113
- // an async slash handler would otherwise terminate Node with a raw stack.
114
- process.on("unhandledRejection", (reason) => {
115
- const text = reason instanceof Error ? reason.message : String(reason);
116
- try {
117
- process.stdout.write(`\nError: ${text}\n`);
118
- } catch { /* stream gone */ }
119
- });
120
-
121
- if (!hasArgs && interactive && !useLegacyRepl) {
122
- // ── Interactive Ink UI ───────────────────────────────────────────────────
123
- if (modeFlag) updateConfig({ permissionMode: modeFlag });
124
- const { runApp } = await import("./ui/app.js");
125
- runApp();
126
- } else if (!hasArgs) {
127
- // ── Legacy readline REPL (odysx --legacy-repl, or non-TTY stdin) ────────
128
- // Same alternate-screen behavior as the Ink UI: hide the tab's prior
129
- // content while running, restore the primary buffer on exit. Signal
130
- // handlers re-raise so Ctrl+C/kill actually terminates.
131
- enterAltScreen();
132
-
133
- const config = getConfig();
134
- let agentId: string = config.model ?? "master";
135
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
136
- const sessionId: string = randomUUID();
137
- const messages: Array<{role: "user" | "assistant", text: string}> = [];
138
- // External providers need the transcript on every turn; the platform keeps
139
- // its own history keyed by session id.
140
- const wireHistory = () => messages.map((m) => ({ role: m.role, content: m.text }));
141
-
142
- const readline = await import("node:readline");
143
- const rl = readline.createInterface({
144
- input: process.stdin,
145
- output: process.stdout,
146
- prompt: promptStr(),
147
- historySize: 200,
148
- });
149
-
150
- // Piped stdin (CI, `printf … | odysx`) reaches EOF immediately. If lines are
151
- // handled as they arrive, an async handler is still running when `close`
152
- // fires and its output is lost. Buffer every line and drain them in order.
153
- const piped = !process.stdin.isTTY;
154
- const pendingLines: string[] = [];
155
- if (piped) rl.removeAllListeners("line");
156
-
157
- // Prompt function — meaningless without a terminal, so piped mode prints a
158
- // plain marker only when it is actually waiting on the user.
159
- const promptUser = () => {
160
- if (!piped) rl.prompt();
161
- };
162
-
163
- // Startup — the alt-buffer switch above already gave us a clean slate, so
164
- // just print the header (no \x1Bc full-terminal reset: it would re-enable
165
- // the hardware cursor and clear modes).
166
- process.stdout.write(header(config) + "\n\n");
167
- process.stdout.write(helpText());
168
-
169
- if (!config.apiKey && !config.token) {
170
- messages.push({
171
- role: "assistant",
172
- text: "Welcome. I'm your agent. Set your API key with:\n odysx auth set-key <key>\n\nOr type /help for commands.",
173
- });
174
- messages.forEach(m => {
175
- process.stdout.write(formatMessage(m.role, m.text) + "\n");
176
- });
177
- }
178
-
179
- // Piped stdin delivers every buffered line at once, so handlers can overlap
180
- // the async exit; `closing` stops work once a quit is in flight.
181
- let closing = false;
182
-
183
- // Trust gate for the legacy/piped path. Piped stdin has no one to ask, so an
184
- // untrusted workspace refuses rather than silently granting access. A TTY
185
- // gets the same y/N question as the Ink UI; a declined answer exits.
186
- if (!isFolderTrusted()) {
187
- const root = getWorkspaceRoot();
188
- if (piped) {
189
- process.stdout.write(`Untrusted workspace: ${root}\nRun \`odysx\` in a terminal to trust it.\n`);
190
- exitWith(1);
191
- }
192
- const answer = await new Promise<string>((resolve) => {
193
- rl.question(
194
- `Trust this folder? Codemind can read, edit, and run commands here.\n ${root}\n[1] Yes, I trust this folder [2] No, exit (1/2): `,
195
- resolve,
196
- );
197
- });
198
- if (answer.trim() !== "1" && answer.trim().toLowerCase() !== "y") {
199
- process.stdout.write("No — exiting.\n");
200
- rl.close();
201
- exitWith(0);
202
- }
203
- try {
204
- trustFolder();
205
- } catch (err) {
206
- process.stdout.write(`Could not save folder trust: ${err instanceof Error ? err.message : String(err)}\n`);
207
- rl.close();
208
- exitWith(1);
209
- }
210
- }
211
- if (modeFlag) updateConfig({ permissionMode: modeFlag });
212
- // Token accounting, shared with /usage.
213
- const usageRef = { current: { requests: 0, promptTokens: 0, completionTokens: 0 } };
214
-
215
- // Helper to handle a user line asynchronously
216
- const handleLine = async (input: string) => {
217
- if (closing) return;
218
- const msg = input.trim();
219
- if (!msg) { promptUser(); return; }
220
-
221
- if (msg === "/quit" || msg === "/exit" || msg === "/q") {
222
- // `closing` guards a second "line" event racing the async flush in
223
- // exitWith (piped input delivers every line at once).
224
- closing = true;
225
- rl.close();
226
- exitWith(0);
227
- return;
228
- }
229
- if (msg === "/help" || msg === "?") {
230
- process.stdout.write(helpText() + "\n");
231
- promptUser();
232
- return;
233
- }
234
- if (msg === "/clear") {
235
- clearViewport(); // viewport wipe only — no mode resets; a no-op when piped
236
- process.stdout.write(header(config) + "\n\n");
237
- process.stdout.write(helpText());
238
- messages.length = 0;
239
- if (!config.apiKey && !config.token) {
240
- messages.push({ role: "assistant", text: "Welcome. Set your API key..." });
241
- messages.forEach(m => process.stdout.write(formatMessage(m.role, m.text) + "\n"));
242
- }
243
- promptUser();
244
- return;
245
- }
246
- if (msg === "/status") {
247
- process.stdout.write(`Session: ${sessionId}\nAgent: ${agentId}\nAuth: ${config.apiKey ? "api-key" : config.token ? "jwt" : "none"}\n`);
248
- promptUser();
249
- return;
250
- }
251
- if (msg.startsWith("/agent ")) {
252
- agentId = msg.split(" ")[1];
253
- process.stdout.write(`Agent set to: ${agentId}\n`);
254
- promptUser();
255
- return;
256
- }
257
- if (msg.startsWith("/model ")) {
258
- updateConfig({ model: msg.split(" ")[1] });
259
- process.stdout.write(`Model updated\n`);
260
- promptUser();
261
- return;
262
- }
263
- if (msg === "/compact") {
264
- messages.length = 0;
265
- process.stdout.write("Context cleared\n");
266
- promptUser();
267
- return;
268
- }
269
- if (msg === "/export") {
270
- const fn = `odysx-session-${sessionId.slice(0, 8)}.json`;
271
- writeFileSync(fn, JSON.stringify({ sessionId, agentId, messages }, null, 2));
272
- process.stdout.write(`Exported to: ${fn}\n`);
273
- promptUser();
274
- return;
275
- }
276
-
277
- // Any other slash command goes through the same table the Ink UI uses, so
278
- // the two frontends never drift. Commands that need an overlay say so
279
- // instead of being sent to the model as prose.
280
- if (msg.startsWith("/")) {
281
- const { runCommand, DISPATCHABLE_COMMANDS } = await import("./ui/commands/runCommand.js");
282
- const name = msg.slice(1).split(/\s+/)[0]!.toLowerCase();
283
- if (!DISPATCHABLE_COMMANDS.includes(name)) {
284
- process.stdout.write(`Unknown command: /${name}. Try /help.\n`);
285
- promptUser();
286
- return;
287
- }
288
- // These render overlays or use state that lives in the Ink UI. /agent and
289
- // /sessions still work with an argument, so only block the bare form.
290
- // /mcp, /prompts, /skills and /tasks/ /ralph print to the transcript and
291
- // work fine here.
292
- const overlayOnly = ["signin", "keys"];
293
- const needsUi = overlayOnly.includes(name) || (["agent", "sessions"].includes(name) && !msg.includes(" "));
294
- if (needsUi) {
295
- process.stdout.write(`/${name} needs the interactive UI — run \`odysx\` in a terminal.\n`);
296
- promptUser();
297
- return;
298
- }
299
- const ctx = {
300
- addMessage: (text: string) => process.stdout.write(text + "\n"),
301
- setMessages: () => { },
302
- sessionId: () => sessionId,
303
- agentId,
304
- config: { apiKey: config.apiKey ?? undefined, token: config.token ?? undefined, apiUrl: config.apiUrl },
305
- openAgentPicker: () => process.stdout.write("/agent needs the interactive UI\n"),
306
- setAgentId: (id: string) => { agentId = id; },
307
- resumeSession: () => process.stdout.write("/sessions needs the interactive UI\n"),
308
- openSessionPicker: () => process.stdout.write("/sessions needs the interactive UI\n"),
309
- openKeysPanel: async () => process.stdout.write("/keys needs the interactive UI\n"),
310
- setPendingOp: () => { },
311
- resetTranscript: () => { messages.length = 0; },
312
- newSession: () => {
313
- messages.length = 0;
314
- process.stdout.write("New session requested — restart `odysx` for a fresh server conversation.\n");
315
- },
316
- usageRef,
317
- abortStream: () => { },
318
- requestExit: () => { closing = true; rl.close(); exitWith(0); },
319
- setAuthMode: () => process.stdout.write("/signin needs the interactive UI\n"),
320
- setAuthIndex: () => { },
321
- };
322
- // Awaited: the handlers hit the network, and the piped drain loop below
323
- // exits the process as soon as this returns.
324
- await runCommand(msg, ctx as never, messages.map((m) => ({ ...m })));
325
- promptUser();
326
- return;
327
- }
328
-
329
- if (!config.apiKey && !config.token) {
330
- process.stdout.write("Authentication required. Run: odysx auth set-key <key>\n");
331
- promptUser();
332
- return;
333
- }
334
-
335
- messages.push({ role: "user", text: msg });
336
- process.stdout.write(formatMessage("user", msg) + "\n\n");
337
- process.stdout.write("Agent is typing...\n\n");
338
-
339
- try {
340
- usageRef.current.requests += 1;
341
- let reply = "";
342
- await streamTurn(msg, {
343
- agentId,
344
- sessionId,
345
- history: wireHistory(), // before this turn's user message is appended
346
- onToken: (token: string) => {
347
- reply += token;
348
- process.stdout.write(token);
349
- },
350
- onUsage: (u) => {
351
- usageRef.current.promptTokens += u.promptTokens;
352
- usageRef.current.completionTokens += u.completionTokens;
353
- },
354
- });
355
- messages.push({ role: "assistant", text: reply });
356
- process.stdout.write("\n\n");
357
- promptUser();
358
- } catch (err) {
359
- process.stdout.write(`\nError: ${err}\n\n`);
360
- promptUser();
361
- }
362
- };
363
-
364
- if (piped) {
365
- rl.on("line", (line: string) => pendingLines.push(line));
366
- rl.on("close", () => {
367
- // Drain sequentially; each handler awaits its own work, so output order
368
- // matches input order and nothing is truncated by the process exiting.
369
- void (async () => {
370
- for (const line of pendingLines) {
371
- if (closing) break;
372
- await handleLine(line);
373
- }
374
- exitWith(0);
375
- })();
376
- });
377
- } else {
378
- rl.on("line", handleLine);
379
- rl.on("close", () => exitWith(0));
380
- promptUser();
381
- }
382
- } else {
383
- // ── CLI routing ──────────────────────────────────────────────────────────
384
- if (modeFlag) updateConfig({ permissionMode: modeFlag });
385
- const program = buildProgram();
386
- program.addCommand(
387
- new Command("help")
388
- .description("Show help")
389
- .argument("[command]", "Command to get help for")
390
- .action((cmdName) => {
391
- if (cmdName) {
392
- const cmd = program.commands.find(
393
- (c) => c.name() === cmdName || c.aliases().includes(cmdName)
394
- );
395
- if (cmd) cmd.help();
396
- else { console.error("Unknown: " + cmdName); exitWith(1); }
397
- } else { program.help(); }
398
- }),
399
- );
400
- program.parse(process.argv);
401
- }