@odysx/codemind 0.0.0-stage → 0.1.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.
- package/README.md +293 -2
- package/dist/commands/agents.d.ts +2 -0
- package/dist/commands/agents.d.ts.map +1 -0
- package/dist/commands/agents.js +91 -0
- package/dist/commands/agents.js.map +1 -0
- package/dist/commands/auth.d.ts +2 -0
- package/dist/commands/auth.d.ts.map +1 -0
- package/dist/commands/auth.js +35 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/chat.d.ts +2 -0
- package/dist/commands/chat.d.ts.map +1 -0
- package/dist/commands/chat.js +180 -0
- package/dist/commands/chat.js.map +1 -0
- package/dist/commands/keys.d.ts +2 -0
- package/dist/commands/keys.d.ts.map +1 -0
- package/dist/commands/keys.js +81 -0
- package/dist/commands/keys.js.map +1 -0
- package/dist/commands/local.d.ts +2 -0
- package/dist/commands/local.d.ts.map +1 -0
- package/dist/commands/local.js +325 -0
- package/dist/commands/local.js.map +1 -0
- package/dist/commands/mcp.d.ts +2 -0
- package/dist/commands/mcp.d.ts.map +1 -0
- package/dist/commands/mcp.js +73 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/prompts.d.ts +2 -0
- package/dist/commands/prompts.d.ts.map +1 -0
- package/dist/commands/prompts.js +59 -0
- package/dist/commands/prompts.js.map +1 -0
- package/dist/commands/skills.d.ts +2 -0
- package/dist/commands/skills.d.ts.map +1 -0
- package/dist/commands/skills.js +41 -0
- package/dist/commands/skills.js.map +1 -0
- package/dist/core/auth/index.d.ts +10 -0
- package/dist/core/auth/index.d.ts.map +1 -0
- package/dist/core/auth/index.js +82 -0
- package/dist/core/auth/index.js.map +1 -0
- package/dist/core/config/index.d.ts +47 -0
- package/dist/core/config/index.d.ts.map +1 -0
- package/dist/core/config/index.js +118 -0
- package/dist/core/config/index.js.map +1 -0
- package/dist/core/history.d.ts +14 -0
- package/dist/core/history.d.ts.map +1 -0
- package/dist/core/history.js +80 -0
- package/dist/core/history.js.map +1 -0
- package/dist/core/markdown-renderer.d.ts +47 -0
- package/dist/core/markdown-renderer.d.ts.map +1 -0
- package/dist/core/markdown-renderer.js +385 -0
- package/dist/core/markdown-renderer.js.map +1 -0
- package/dist/core/permissions/index.d.ts +31 -0
- package/dist/core/permissions/index.d.ts.map +1 -0
- package/dist/core/permissions/index.js +64 -0
- package/dist/core/permissions/index.js.map +1 -0
- package/dist/core/provider/external.d.ts +28 -0
- package/dist/core/provider/external.d.ts.map +1 -0
- package/dist/core/provider/external.js +248 -0
- package/dist/core/provider/external.js.map +1 -0
- package/dist/core/provider/index.d.ts +96 -0
- package/dist/core/provider/index.d.ts.map +1 -0
- package/dist/core/provider/index.js +423 -0
- package/dist/core/provider/index.js.map +1 -0
- package/dist/core/registry/index.d.ts +25 -0
- package/dist/core/registry/index.d.ts.map +1 -0
- package/dist/core/registry/index.js +216 -0
- package/dist/core/registry/index.js.map +1 -0
- package/dist/core/schema/types.d.ts +93 -0
- package/dist/core/schema/types.d.ts.map +1 -0
- package/dist/core/schema/types.js +12 -0
- package/dist/core/schema/types.js.map +1 -0
- package/dist/core/terminal.d.ts +40 -0
- package/dist/core/terminal.d.ts.map +1 -0
- package/dist/core/terminal.js +134 -0
- package/dist/core/terminal.js.map +1 -0
- package/dist/core/workspace/index.d.ts +35 -0
- package/dist/core/workspace/index.d.ts.map +1 -0
- package/dist/core/workspace/index.js +194 -0
- package/dist/core/workspace/index.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +393 -0
- package/dist/index.js.map +1 -0
- package/dist/ui/app.d.ts +3 -0
- package/dist/ui/app.d.ts.map +1 -0
- package/dist/ui/app.js +554 -0
- package/dist/ui/app.js.map +1 -0
- package/dist/ui/commands/cases/catalog.d.ts +11 -0
- package/dist/ui/commands/cases/catalog.d.ts.map +1 -0
- package/dist/ui/commands/cases/catalog.js +62 -0
- package/dist/ui/commands/cases/catalog.js.map +1 -0
- package/dist/ui/commands/cases/misc.d.ts +35 -0
- package/dist/ui/commands/cases/misc.d.ts.map +1 -0
- package/dist/ui/commands/cases/misc.js +245 -0
- package/dist/ui/commands/cases/misc.js.map +1 -0
- package/dist/ui/commands/cases/tasks.d.ts +13 -0
- package/dist/ui/commands/cases/tasks.d.ts.map +1 -0
- package/dist/ui/commands/cases/tasks.js +91 -0
- package/dist/ui/commands/cases/tasks.js.map +1 -0
- package/dist/ui/commands/runCommand.d.ts +22 -0
- package/dist/ui/commands/runCommand.d.ts.map +1 -0
- package/dist/ui/commands/runCommand.js +136 -0
- package/dist/ui/commands/runCommand.js.map +1 -0
- package/dist/ui/components/AgentPicker.d.ts +11 -0
- package/dist/ui/components/AgentPicker.d.ts.map +1 -0
- package/dist/ui/components/AgentPicker.js +7 -0
- package/dist/ui/components/AgentPicker.js.map +1 -0
- package/dist/ui/components/AuthPicker.d.ts +14 -0
- package/dist/ui/components/AuthPicker.d.ts.map +1 -0
- package/dist/ui/components/AuthPicker.js +7 -0
- package/dist/ui/components/AuthPicker.js.map +1 -0
- package/dist/ui/components/Banner.d.ts +8 -0
- package/dist/ui/components/Banner.d.ts.map +1 -0
- package/dist/ui/components/Banner.js +24 -0
- package/dist/ui/components/Banner.js.map +1 -0
- package/dist/ui/components/CommandMenu.d.ts +17 -0
- package/dist/ui/components/CommandMenu.d.ts.map +1 -0
- package/dist/ui/components/CommandMenu.js +21 -0
- package/dist/ui/components/CommandMenu.js.map +1 -0
- package/dist/ui/components/ErrorBoundary.d.ts +24 -0
- package/dist/ui/components/ErrorBoundary.d.ts.map +1 -0
- package/dist/ui/components/ErrorBoundary.js +51 -0
- package/dist/ui/components/ErrorBoundary.js.map +1 -0
- package/dist/ui/components/InputBar.d.ts +28 -0
- package/dist/ui/components/InputBar.d.ts.map +1 -0
- package/dist/ui/components/InputBar.js +108 -0
- package/dist/ui/components/InputBar.js.map +1 -0
- package/dist/ui/components/KeysPanel.d.ts +12 -0
- package/dist/ui/components/KeysPanel.d.ts.map +1 -0
- package/dist/ui/components/KeysPanel.js +25 -0
- package/dist/ui/components/KeysPanel.js.map +1 -0
- package/dist/ui/components/MessageView.d.ts +29 -0
- package/dist/ui/components/MessageView.d.ts.map +1 -0
- package/dist/ui/components/MessageView.js +107 -0
- package/dist/ui/components/MessageView.js.map +1 -0
- package/dist/ui/components/PermissionPrompt.d.ts +10 -0
- package/dist/ui/components/PermissionPrompt.d.ts.map +1 -0
- package/dist/ui/components/PermissionPrompt.js +4 -0
- package/dist/ui/components/PermissionPrompt.js.map +1 -0
- package/dist/ui/components/Pickers.d.ts +43 -0
- package/dist/ui/components/Pickers.d.ts.map +1 -0
- package/dist/ui/components/Pickers.js +33 -0
- package/dist/ui/components/Pickers.js.map +1 -0
- package/dist/ui/components/SearchOverlay.d.ts +21 -0
- package/dist/ui/components/SearchOverlay.d.ts.map +1 -0
- package/dist/ui/components/SearchOverlay.js +14 -0
- package/dist/ui/components/SearchOverlay.js.map +1 -0
- package/dist/ui/components/SessionPicker.d.ts +12 -0
- package/dist/ui/components/SessionPicker.d.ts.map +1 -0
- package/dist/ui/components/SessionPicker.js +8 -0
- package/dist/ui/components/SessionPicker.js.map +1 -0
- package/dist/ui/components/WorkingIndicator.d.ts +10 -0
- package/dist/ui/components/WorkingIndicator.d.ts.map +1 -0
- package/dist/ui/components/WorkingIndicator.js +23 -0
- package/dist/ui/components/WorkingIndicator.js.map +1 -0
- package/dist/ui/controls/ApiKeyEntry.d.ts +8 -0
- package/dist/ui/controls/ApiKeyEntry.d.ts.map +1 -0
- package/dist/ui/controls/ApiKeyEntry.js +5 -0
- package/dist/ui/controls/ApiKeyEntry.js.map +1 -0
- package/dist/ui/controls/MultiSelect.d.ts +18 -0
- package/dist/ui/controls/MultiSelect.d.ts.map +1 -0
- package/dist/ui/controls/MultiSelect.js +9 -0
- package/dist/ui/controls/MultiSelect.js.map +1 -0
- package/dist/ui/hooks/useAgentPicker.d.ts +14 -0
- package/dist/ui/hooks/useAgentPicker.d.ts.map +1 -0
- package/dist/ui/hooks/useAgentPicker.js +53 -0
- package/dist/ui/hooks/useAgentPicker.js.map +1 -0
- package/dist/ui/hooks/useAuthFlow.d.ts +14 -0
- package/dist/ui/hooks/useAuthFlow.d.ts.map +1 -0
- package/dist/ui/hooks/useAuthFlow.js +31 -0
- package/dist/ui/hooks/useAuthFlow.js.map +1 -0
- package/dist/ui/hooks/useKeysFlow.d.ts +17 -0
- package/dist/ui/hooks/useKeysFlow.d.ts.map +1 -0
- package/dist/ui/hooks/useKeysFlow.js +130 -0
- package/dist/ui/hooks/useKeysFlow.js.map +1 -0
- package/dist/ui/hooks/useSessionPicker.d.ts +29 -0
- package/dist/ui/hooks/useSessionPicker.d.ts.map +1 -0
- package/dist/ui/hooks/useSessionPicker.js +58 -0
- package/dist/ui/hooks/useSessionPicker.js.map +1 -0
- package/dist/ui/hooks/useTerminalSize.d.ts +6 -0
- package/dist/ui/hooks/useTerminalSize.d.ts.map +1 -0
- package/dist/ui/hooks/useTerminalSize.js +29 -0
- package/dist/ui/hooks/useTerminalSize.js.map +1 -0
- package/dist/ui/input/keyRouter.d.ts +125 -0
- package/dist/ui/input/keyRouter.d.ts.map +1 -0
- package/dist/ui/input/keyRouter.js +574 -0
- package/dist/ui/input/keyRouter.js.map +1 -0
- package/dist/ui/layouts/ScreenLayout.d.ts +98 -0
- package/dist/ui/layouts/ScreenLayout.d.ts.map +1 -0
- package/dist/ui/layouts/ScreenLayout.js +56 -0
- package/dist/ui/layouts/ScreenLayout.js.map +1 -0
- package/dist/ui/shared/types.d.ts +143 -0
- package/dist/ui/shared/types.d.ts.map +1 -0
- package/dist/ui/shared/types.js +38 -0
- package/dist/ui/shared/types.js.map +1 -0
- package/dist/ui/theme.d.ts +29 -0
- package/dist/ui/theme.d.ts.map +1 -0
- package/dist/ui/theme.js +54 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/ui/utils/markdown.d.ts +74 -0
- package/dist/ui/utils/markdown.d.ts.map +1 -0
- package/dist/ui/utils/markdown.js +187 -0
- package/dist/ui/utils/markdown.js.map +1 -0
- package/dist/ui/utils/mentions.d.ts +12 -0
- package/dist/ui/utils/mentions.d.ts.map +1 -0
- package/dist/ui/utils/mentions.js +28 -0
- package/dist/ui/utils/mentions.js.map +1 -0
- package/dist/ui/utils/terminal-text.d.ts +33 -0
- package/dist/ui/utils/terminal-text.d.ts.map +1 -0
- package/dist/ui/utils/terminal-text.js +123 -0
- package/dist/ui/utils/terminal-text.js.map +1 -0
- package/dist/ui/utils/text.d.ts +72 -0
- package/dist/ui/utils/text.d.ts.map +1 -0
- package/dist/ui/utils/text.js +171 -0
- package/dist/ui/utils/text.js.map +1 -0
- package/eslint.config.js +40 -0
- package/graphify-out/GRAPH_REPORT.md +140 -0
- package/graphify-out/cache/1236b1806943f8eb5c64fece9fb448149370f4a1b52b32f0977ad528d7911291.json +1 -0
- package/graphify-out/cache/15cbf641dcc0fd641c18da872214918203a1f64baa7ac8430e4702ef32d18b02.json +1 -0
- package/graphify-out/cache/1abc52745bb46f6cc5a528530e2bc1f368c26e348dff85cdde5cad6f275d7177.json +1 -0
- package/graphify-out/cache/1c7815c37c13177e98895e272285f6151a8d0807478fc843ec452634b3e9912d.json +1 -0
- package/graphify-out/cache/1ec38c39f044f329c78642b61d6394fb8e15d0014335f32947d1f5c7b88c02bc.json +1 -0
- package/graphify-out/cache/20c9b57ff76dbb9d57cbd00659b0a42d2b28dd2abacf1ef4143cc67a5b6fe7f4.json +1 -0
- package/graphify-out/cache/285e11876884eb619e779d540cd957b33e70f14c67ecc26c9b9755aa9cedd717.json +1 -0
- package/graphify-out/cache/2c0a9c49ab51f4e0f07543b4e941bf802b7db31fa607c4cee8e595575be68fe7.json +1 -0
- package/graphify-out/cache/3025e6c2756c79ccaadd51d611f25a8b6630a6a11ce1b4e6e261b3e033dc194f.json +1 -0
- package/graphify-out/cache/4801a686ddfa0a52824a4fc3d65443cbb0ab9f0b030af67d02fccb520c584a08.json +1 -0
- package/graphify-out/cache/4f8fd96f38aa7d0293b4fab60c4581c4f3e68f4533c664c9cd78d1c53bea1cba.json +1 -0
- package/graphify-out/cache/5928e562b0f5700184b810e2403f4aa685e2b93c3c584c10460562b514c40fdd.json +1 -0
- package/graphify-out/cache/66132486efb0a4cfaeea766448f1f15bb33635808714052e7e24e5b00fbc984f.json +1 -0
- package/graphify-out/cache/66ab27669e7ec685130063662af9f30018c267f7639b4af4b0504a98282c383a.json +1 -0
- package/graphify-out/cache/6ff6b3dd433b5ffe96e166263a203dd622397e419dc00bb676824988b7defeab.json +1 -0
- package/graphify-out/cache/7a00a49932177a6215e2590b1ddfd6f1d8494792f693068e9d427d16b165d370.json +1 -0
- package/graphify-out/cache/7b681bc5182f40f707f1c0b16b78af38efffa62a1df315d10878f4de888408ef.json +1 -0
- package/graphify-out/cache/81196d44e50ac0b276d826a531ed10c425a528b298e3fe18e8d5a3eec996e9d5.json +1 -0
- package/graphify-out/cache/8934e9b32dea6e66fdc34fa1681b287ff1335c389917e0fc6ee2fc573e23e87b.json +1 -0
- package/graphify-out/cache/975f6c99807f86b79c80ca40c783618e2080a927eaebe6ea3db37b869f048851.json +1 -0
- package/graphify-out/cache/9ab98f08626fd8190462595e38de828293ad184db8df5e1177b05a8fe290faa1.json +1 -0
- package/graphify-out/cache/9c434a6796d7932b6e87a908c155c09cb2b8d097fc55f141f0302b6600f94749.json +1 -0
- package/graphify-out/cache/ac5cf4c489dc417c1f6ea728d0b6ba12ac7087bc6c2e736697ff3024b4d4e534.json +1 -0
- package/graphify-out/cache/ae5083b5c1dca3e5c6af485e7f6eb8a08ff8b382cdecf92ab5e0902053442afe.json +1 -0
- package/graphify-out/cache/b274057fc2e0118cf2746d1d8060841122bb2a2b0305566fc3f32d3a51e08bcc.json +1 -0
- package/graphify-out/cache/b39beef31da682387f030c0d149f6cd5b3e66effbf936cc8430db15360759dda.json +1 -0
- package/graphify-out/cache/bf3c6a36bb72820bc11cbf4a5c49067d1f62013d4686c019a4dbd9951b1e980e.json +1 -0
- package/graphify-out/cache/c034e156028d1a58b89bce659726e9a4065ddc4d2b560d6bce7a206fdff8dad3.json +1 -0
- package/graphify-out/cache/c619b5bbe8d1f427fbd7816d1e9fcfaf90d7201b020ae6b578166aeb9cb20683.json +1 -0
- package/graphify-out/cache/c8db77a9da0b0f601acfa50a620f31155cdda0d0cc0f2285adcea19e77194ee6.json +1 -0
- package/graphify-out/cache/ca664791ec7741a2077d9e7c10384142d30e7a3b77b388df719c315da608d57d.json +1 -0
- package/graphify-out/cache/cba43a116186bd55c8a63368dea4eee4a8123b2bec0a42df6efb3254f89f9a7a.json +1 -0
- package/graphify-out/cache/cd51cbb3dad1cf34417b9761fe2eb184a9f57d23dc86e45630e63bfa2ecafddc.json +1 -0
- package/graphify-out/cache/d06738cde193b34a136b94104220c17e0212b814b3e4d7ff399105635cae1a03.json +1 -0
- package/graphify-out/cache/d093c19f0169c0398eeb1360895b1c16ecc50abc59f4be8eec3f6fd27c89a6e5.json +1 -0
- package/graphify-out/cache/da8f75c438531d232fe114d5cbf2374576ce842a5fb87c7e5a88b78fdb334e5e.json +1 -0
- package/graphify-out/cache/e35798fa04ddede548a6953bdea506bd7d0788dfed4e8a8ea6ae77b1b60ef5e0.json +1 -0
- package/graphify-out/cache/e5a38c254719000487098782125e15e2e982c07d66b99d359ed1781f30f9fc8c.json +1 -0
- package/graphify-out/cache/e6e4c4a5a7200ee0154585b5d6538f699b0a746dbded3164f8391f06ce7cb212.json +1 -0
- package/graphify-out/cache/ea8d0838df6309b673b031f616b253af957898ee7289d25a541fa6183b732e4e.json +1 -0
- package/graphify-out/cache/ebaee92b0e881efd7a08f3143e2c1803310396b4651d4f77532003c699cc6836.json +1 -0
- package/graphify-out/cache/edf8543318a9e60d56d04704078da6c37202ddd6fdfc83144fe2b94fe19b80ef.json +1 -0
- package/graphify-out/cache/efca19d3668035f11126fc9eccbeab290abfa94babf3f9a8706de81d17aba263.json +1 -0
- package/graphify-out/cache/f1480bb642025ebd6d99986cdd8a7d5726897ec038300e4b095c9c938435dfad.json +1 -0
- package/graphify-out/cache/f459f21277f03865f732affb175b1b092954dae5967f27b3cd1864466e3e24f4.json +1 -0
- package/graphify-out/cache/f899e37e864010b30f4c196e86082d7bd022a0df032005e0407883ebcec36514.json +1 -0
- package/graphify-out/cache/fb3cbe07fbcd97c536cff9c6bc343c35f22d49613f05c53ad30ef67a75c5d93e.json +1 -0
- package/graphify-out/cache/fd3dc682b1ca2f5ae03b50d6b0e045ae50f5701c0a21c5a388d77ef482939d3a.json +1 -0
- package/graphify-out/graph.html +257 -0
- package/graphify-out/graph.json +4396 -0
- package/package.json +45 -6
- package/scripts/e2e-external.mjs +65 -0
- package/scripts/live-agents.mjs +6 -0
- package/scripts/live-stream.mjs +51 -0
- package/src/commands/agents.ts +91 -0
- package/src/commands/auth.ts +39 -0
- package/src/commands/chat.ts +198 -0
- package/src/commands/keys.ts +83 -0
- package/src/commands/local.ts +302 -0
- package/src/commands/mcp.ts +74 -0
- package/src/commands/prompts.ts +58 -0
- package/src/commands/skills.ts +43 -0
- package/src/core/auth/index.ts +101 -0
- package/src/core/config/index.ts +152 -0
- package/src/core/history.ts +79 -0
- package/src/core/markdown-renderer.ts +420 -0
- package/src/core/permissions/index.ts +99 -0
- package/src/core/provider/external.ts +312 -0
- package/src/core/provider/index.ts +517 -0
- package/src/core/registry/index.ts +250 -0
- package/src/core/schema/types.ts +106 -0
- package/src/core/terminal.ts +124 -0
- package/src/core/workspace/index.ts +216 -0
- package/src/index.ts +401 -0
- package/src/ui/app.tsx +644 -0
- package/src/ui/commands/cases/catalog.ts +76 -0
- package/src/ui/commands/cases/misc.ts +293 -0
- package/src/ui/commands/cases/tasks.ts +115 -0
- package/src/ui/commands/runCommand.ts +178 -0
- package/src/ui/components/Banner.tsx +63 -0
- package/src/ui/components/CommandMenu.tsx +63 -0
- package/src/ui/components/ErrorBoundary.tsx +75 -0
- package/src/ui/components/InputBar.tsx +227 -0
- package/src/ui/components/KeysPanel.tsx +115 -0
- package/src/ui/components/MessageView.tsx +226 -0
- package/src/ui/components/Pickers.tsx +234 -0
- package/src/ui/components/SearchOverlay.tsx +65 -0
- package/src/ui/components/WorkingIndicator.tsx +35 -0
- package/src/ui/controls/ApiKeyEntry.tsx +29 -0
- package/src/ui/controls/MultiSelect.tsx +55 -0
- package/src/ui/hooks/useAgentPicker.ts +64 -0
- package/src/ui/hooks/useAuthFlow.ts +47 -0
- package/src/ui/hooks/useKeysFlow.ts +157 -0
- package/src/ui/hooks/useSessionPicker.ts +84 -0
- package/src/ui/hooks/useTerminalSize.ts +36 -0
- package/src/ui/input/keyRouter.ts +627 -0
- package/src/ui/layouts/ScreenLayout.tsx +202 -0
- package/src/ui/shared/types.ts +160 -0
- package/src/ui/theme.ts +53 -0
- package/src/ui/utils/markdown.ts +255 -0
- package/src/ui/utils/mentions.ts +40 -0
- package/src/ui/utils/terminal-text.ts +130 -0
- package/src/ui/utils/text.ts +174 -0
- package/test/commands.test.mjs +127 -0
- package/test/config.test.mjs +123 -0
- package/test/history.test.mjs +42 -0
- package/test/keyrouter.test.mjs +324 -0
- package/test/layout.test.mjs +271 -0
- package/test/markdown.test.mjs +92 -0
- package/test/mentions.test.mjs +51 -0
- package/test/permissions.test.mjs +130 -0
- package/test/provider.test.mjs +287 -0
- package/test/terminal-text.test.mjs +116 -0
- package/test/text.test.mjs +134 -0
- package/test/workspace.test.mjs +101 -0
- package/tsconfig.json +32 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config management — persists auth + settings to ~/.odysx/config.json.
|
|
3
|
+
*
|
|
4
|
+
* Two rules shape this module:
|
|
5
|
+
* 1. Values that came from the environment are never written back. A CI or
|
|
6
|
+
* ephemeral shell may export ODYSX_API_KEY; any later `updateConfig`
|
|
7
|
+
* (a mere `/model gpt-x`) used to serialise the whole merged object and
|
|
8
|
+
* leave that secret in plaintext on disk.
|
|
9
|
+
* 2. Writes are atomic and owner-only. A truncated write would lose the
|
|
10
|
+
* user's session, and the default umask leaves the file world-readable.
|
|
11
|
+
*/
|
|
12
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
|
|
16
|
+
/** External LLM provider endpoint (bring-your-own key). See
|
|
17
|
+
* core/provider/external.ts for the wire protocols. */
|
|
18
|
+
export interface ExternalProviderConfig {
|
|
19
|
+
type: "openai" | "anthropic" | "gemini";
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
model: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface OdysxConfig {
|
|
26
|
+
apiUrl: string;
|
|
27
|
+
token: string | null;
|
|
28
|
+
apiKey: string | null;
|
|
29
|
+
username: string | null;
|
|
30
|
+
defaultOutput: string;
|
|
31
|
+
telemetry: boolean;
|
|
32
|
+
model: string | null;
|
|
33
|
+
/** Workspace root for local file/shell ops (defaults to launch cwd). */
|
|
34
|
+
workspace?: string | null;
|
|
35
|
+
/** Active external provider name; absent/null/"odysx" = OdysX platform. */
|
|
36
|
+
provider?: string | null;
|
|
37
|
+
/** Permission posture for workspace operations. See core/permissions. */
|
|
38
|
+
permissionMode?: string | null;
|
|
39
|
+
/**
|
|
40
|
+
* Local label of the API key selected in /keys (derived from `key_prefix`).
|
|
41
|
+
* The key secret itself stays in `apiKey`; this is only a human-readable
|
|
42
|
+
* marker, because the backend never returns a created key's secret twice.
|
|
43
|
+
*/
|
|
44
|
+
activeKeyLabel?: string | null;
|
|
45
|
+
/** Named provider endpoints usable via /provider <name>. */
|
|
46
|
+
providers?: Record<string, ExternalProviderConfig>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const CONFIG_DIR = join(homedir(), ".odysx");
|
|
50
|
+
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
51
|
+
|
|
52
|
+
const DEFAULT_CONFIG: OdysxConfig = {
|
|
53
|
+
apiUrl: "http://localhost:8000",
|
|
54
|
+
token: null,
|
|
55
|
+
apiKey: null,
|
|
56
|
+
username: null,
|
|
57
|
+
defaultOutput: "table",
|
|
58
|
+
telemetry: true,
|
|
59
|
+
model: null,
|
|
60
|
+
provider: null,
|
|
61
|
+
providers: {},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
let cachedConfig: OdysxConfig | null = null;
|
|
65
|
+
/** The last state known to be on disk — the source of truth for what to write.
|
|
66
|
+
* Kept separate from `cachedConfig`, which also carries env overrides. */
|
|
67
|
+
let diskConfig: OdysxConfig | null = null;
|
|
68
|
+
|
|
69
|
+
/** Fields that an environment variable may override, mapped to that variable. */
|
|
70
|
+
const ENV_OVERRIDES: Array<{ key: keyof OdysxConfig; env: string }> = [
|
|
71
|
+
{ key: "apiUrl", env: "ODYSX_API_URL" },
|
|
72
|
+
{ key: "apiKey", env: "ODYSX_API_KEY" },
|
|
73
|
+
{ key: "token", env: "ODYSX_TOKEN" },
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
function loadFromDisk(): OdysxConfig {
|
|
77
|
+
if (!existsSync(CONFIG_FILE)) return { ...DEFAULT_CONFIG };
|
|
78
|
+
try {
|
|
79
|
+
const raw = readFileSync(CONFIG_FILE, "utf-8");
|
|
80
|
+
return { ...DEFAULT_CONFIG, ...JSON.parse(raw) };
|
|
81
|
+
} catch (err) {
|
|
82
|
+
// Don't silently pretend the user has no config: a corrupt file means
|
|
83
|
+
// they are about to be treated as logged out, which is worth saying.
|
|
84
|
+
process.stderr.write(
|
|
85
|
+
`⚠ ${CONFIG_FILE} could not be read (${err instanceof Error ? err.message : String(err)}); using defaults\n`,
|
|
86
|
+
);
|
|
87
|
+
return { ...DEFAULT_CONFIG };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Load config from disk (or defaults), with environment overrides applied. */
|
|
92
|
+
export function getConfig(): OdysxConfig {
|
|
93
|
+
if (cachedConfig) return cachedConfig;
|
|
94
|
+
|
|
95
|
+
const base = loadFromDisk();
|
|
96
|
+
diskConfig = { ...base };
|
|
97
|
+
const merged = { ...base } as unknown as Record<string, unknown>;
|
|
98
|
+
for (const { key, env } of ENV_OVERRIDES) {
|
|
99
|
+
const value = process.env[env];
|
|
100
|
+
if (value) merged[key] = value;
|
|
101
|
+
}
|
|
102
|
+
cachedConfig = merged as unknown as OdysxConfig;
|
|
103
|
+
return cachedConfig;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Save config to disk.
|
|
108
|
+
*
|
|
109
|
+
* Environment-derived values are written back as whatever was on disk before,
|
|
110
|
+
* so exporting a key never causes it to be persisted by an unrelated update.
|
|
111
|
+
* The write goes to a sibling temp file that is chmod'd 0600 and renamed into
|
|
112
|
+
* place: rename is atomic, so a crash can't leave a half-written config.
|
|
113
|
+
*/
|
|
114
|
+
export function saveConfig(config: OdysxConfig): void {
|
|
115
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
116
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const toWrite: OdysxConfig = { ...config };
|
|
120
|
+
const onDisk = diskConfig ?? DEFAULT_CONFIG;
|
|
121
|
+
const writable = toWrite as unknown as Record<string, unknown>;
|
|
122
|
+
for (const { key, env } of ENV_OVERRIDES) {
|
|
123
|
+
if (process.env[env]) writable[key] = (onDisk as unknown as Record<string, unknown>)[key];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const tmp = `${CONFIG_FILE}.${process.pid}.tmp`;
|
|
127
|
+
try {
|
|
128
|
+
writeFileSync(tmp, JSON.stringify(toWrite, null, 2), { encoding: "utf-8", mode: 0o600 });
|
|
129
|
+
// mode on create is subject to umask; chmod makes it explicit.
|
|
130
|
+
try { chmodSync(tmp, 0o600); } catch { /* not POSIX, or FS without modes */ }
|
|
131
|
+
renameSync(tmp, CONFIG_FILE);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
try { if (existsSync(tmp)) unlinkSync(tmp); } catch { /* best effort */ }
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
diskConfig = { ...toWrite };
|
|
138
|
+
cachedConfig = config; // the caller's view still includes env overrides
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Update a config field and save */
|
|
142
|
+
export function updateConfig(updates: Partial<OdysxConfig>): OdysxConfig {
|
|
143
|
+
const config = getConfig();
|
|
144
|
+
const updated = { ...config, ...updates };
|
|
145
|
+
saveConfig(updated);
|
|
146
|
+
return updated;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Clear auth tokens (logout) */
|
|
150
|
+
export function clearAuth(): void {
|
|
151
|
+
updateConfig({ token: null, apiKey: null });
|
|
152
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt history persistence.
|
|
3
|
+
*
|
|
4
|
+
* The REPL's ↑ recall was in-memory only, so every launch started with an
|
|
5
|
+
* empty history — the single most jarring difference from a real shell.
|
|
6
|
+
* History is appended to ~/.odysx/history.json (deduped, capped) and loaded
|
|
7
|
+
* once at startup. All failures are silent: a corrupt or unwritable history
|
|
8
|
+
* file must never stop the CLI from starting.
|
|
9
|
+
*/
|
|
10
|
+
import { chmodSync, existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
11
|
+
import { join, dirname } from "node:path";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
|
|
14
|
+
const HISTORY_FILE = join(homedir(), ".odysx", "history.json");
|
|
15
|
+
const MAX_ENTRIES = 500;
|
|
16
|
+
|
|
17
|
+
/** Flags whose following value is a credential or private content. */
|
|
18
|
+
const SECRET_FLAGS = [
|
|
19
|
+
"--content", "--password", "--api-key", "--apiKey", "--token",
|
|
20
|
+
"--secret", "--key", "--value",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** Anything that looks like a provider key, wherever it appears. */
|
|
24
|
+
const SECRET_TOKEN = /\b(sk-[A-Za-z0-9_-]{12,}|AIza[A-Za-z0-9_-]{20,}|gsk_[A-Za-z0-9]{12,})\b/g;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Strip credentials before a line is written to disk.
|
|
28
|
+
*
|
|
29
|
+
* History is a plaintext file that outlives the session, and every submitted
|
|
30
|
+
* line used to be stored verbatim — including `local write --content <secret>`
|
|
31
|
+
* and pasted API keys. Redacting the value keeps recall useful without
|
|
32
|
+
* turning the history file into a credential store.
|
|
33
|
+
*/
|
|
34
|
+
export const redact = (entry: string): string => {
|
|
35
|
+
let out = entry;
|
|
36
|
+
for (const flag of SECRET_FLAGS) {
|
|
37
|
+
const escaped = flag.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
38
|
+
out = out.replace(
|
|
39
|
+
new RegExp(`(${escaped})(\\s+|=)(\"[^\"]*\"|'[^']*'|\\S+)`, "gi"),
|
|
40
|
+
"$1$2[redacted]",
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
return out.replace(SECRET_TOKEN, "[redacted]");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export function loadHistory(): string[] {
|
|
47
|
+
try {
|
|
48
|
+
if (!existsSync(HISTORY_FILE)) return [];
|
|
49
|
+
const parsed: unknown = JSON.parse(readFileSync(HISTORY_FILE, "utf-8"));
|
|
50
|
+
if (!Array.isArray(parsed)) return [];
|
|
51
|
+
return parsed.filter((entry): entry is string => typeof entry === "string").slice(-MAX_ENTRIES);
|
|
52
|
+
} catch {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function saveHistory(entries: string[]): void {
|
|
58
|
+
try {
|
|
59
|
+
const dir = dirname(HISTORY_FILE);
|
|
60
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
61
|
+
writeFileSync(HISTORY_FILE, JSON.stringify(entries.slice(-MAX_ENTRIES), null, 0), {
|
|
62
|
+
encoding: "utf-8",
|
|
63
|
+
mode: 0o600,
|
|
64
|
+
});
|
|
65
|
+
// mode on create is subject to umask; make it explicit where supported.
|
|
66
|
+
try { chmodSync(HISTORY_FILE, 0o600); } catch { /* not POSIX */ }
|
|
67
|
+
} catch { /* history is a convenience, never fatal */ }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Append an entry, dropping consecutive duplicates, and persist. */
|
|
71
|
+
export function appendHistory(entries: string[], entry: string): string[] {
|
|
72
|
+
const trimmed = entry.trim();
|
|
73
|
+
if (!trimmed) return entries;
|
|
74
|
+
const safe = redact(trimmed);
|
|
75
|
+
if (entries[entries.length - 1] === safe) return entries;
|
|
76
|
+
const next = [...entries, safe].slice(-MAX_ENTRIES);
|
|
77
|
+
saveHistory(next);
|
|
78
|
+
return next;
|
|
79
|
+
}
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Renderer for Console Chat
|
|
3
|
+
*
|
|
4
|
+
* Transforms streaming markdown content into beautifully formatted console output
|
|
5
|
+
* with syntax highlighting, colorization, and proper code block handling.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import markdownIt from "markdown-it";
|
|
9
|
+
import chalk from "chalk";
|
|
10
|
+
|
|
11
|
+
// Initialize markdown-it with common options
|
|
12
|
+
const md = markdownIt({
|
|
13
|
+
html: true,
|
|
14
|
+
linkify: true,
|
|
15
|
+
typographer: true,
|
|
16
|
+
// Disabled markdown-it code highlighting - using custom renderer for better control
|
|
17
|
+
// highlight: (str, lang) => {
|
|
18
|
+
// // Simple syntax highlighting for common languages
|
|
19
|
+
// if (lang) {
|
|
20
|
+
// return highlightCode(str, lang);
|
|
21
|
+
// }
|
|
22
|
+
// return str;
|
|
23
|
+
// }
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Simple code highlighting using chalk
|
|
28
|
+
*/
|
|
29
|
+
function highlightCode(code: string, _lang: string): string {
|
|
30
|
+
const lines = code.split('\n');
|
|
31
|
+
let inString = false;
|
|
32
|
+
let stringChar = '';
|
|
33
|
+
let escaped = false;
|
|
34
|
+
|
|
35
|
+
return lines.map((line, idx) => {
|
|
36
|
+
let result = '';
|
|
37
|
+
let i = 0;
|
|
38
|
+
|
|
39
|
+
while (i < line.length) {
|
|
40
|
+
const char = line[i];
|
|
41
|
+
|
|
42
|
+
// Handle strings
|
|
43
|
+
if (!inString && (char === '"' || char === "'" || char === '`')) {
|
|
44
|
+
inString = true;
|
|
45
|
+
stringChar = char;
|
|
46
|
+
result += chalk.yellow(char);
|
|
47
|
+
} else if (inString && char === stringChar && !escaped) {
|
|
48
|
+
inString = false;
|
|
49
|
+
result += chalk.yellow(char);
|
|
50
|
+
} else if (inString && char === '\\' && !escaped) {
|
|
51
|
+
escaped = true;
|
|
52
|
+
result += chalk.yellow(char);
|
|
53
|
+
} else {
|
|
54
|
+
escaped = false;
|
|
55
|
+
|
|
56
|
+
if (inString) {
|
|
57
|
+
result += chalk.yellow(char);
|
|
58
|
+
} else {
|
|
59
|
+
// Keywords
|
|
60
|
+
const keywords = ['const', 'let', 'var', 'function', 'return', 'if', 'else', 'for', 'while', 'class', 'import', 'export', 'from', 'async', 'await', 'try', 'catch', 'throw', 'new', 'this', 'typeof', 'instanceof', 'def', 'pass', 'lambda', 'yield', 'with', 'as', 'is', 'not', 'and', 'or', 'in', 'True', 'False', 'None'];
|
|
61
|
+
const nextWord = line.slice(i).match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)/);
|
|
62
|
+
if (nextWord && keywords.includes(nextWord[1])) {
|
|
63
|
+
result += chalk.blue(nextWord[1]);
|
|
64
|
+
i += nextWord[1].length - 1;
|
|
65
|
+
} else if (char.match(/[{}()[\];,]/)) {
|
|
66
|
+
result += chalk.gray(char);
|
|
67
|
+
} else if (char === '#' && i === 0) {
|
|
68
|
+
// Python comment
|
|
69
|
+
result += chalk.green(line.slice(i));
|
|
70
|
+
break;
|
|
71
|
+
} else if (char === '/' && i + 1 < line.length && line[i + 1] === '/') {
|
|
72
|
+
// JS/TS comment
|
|
73
|
+
result += chalk.green(line.slice(i));
|
|
74
|
+
break;
|
|
75
|
+
} else {
|
|
76
|
+
result += chalk.white(char);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return chalk.gray(`${String(idx + 1).padStart(3, ' ')} | `) + result;
|
|
84
|
+
}).join('\n');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Render a full markdown string to console with colors
|
|
89
|
+
*/
|
|
90
|
+
export function renderMarkdown(markdown: string): void {
|
|
91
|
+
if (!markdown || markdown.trim() === '') return;
|
|
92
|
+
|
|
93
|
+
const rendered = md.render(markdown);
|
|
94
|
+
|
|
95
|
+
// Parse the HTML output and convert to chalk-styled output
|
|
96
|
+
const lines = rendered.split('\n');
|
|
97
|
+
|
|
98
|
+
for (const line of lines) {
|
|
99
|
+
const trimmed = line.trim();
|
|
100
|
+
|
|
101
|
+
if (!trimmed) {
|
|
102
|
+
console.log('');
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Headings
|
|
107
|
+
const headingMatch = trimmed.match(/^<h([1-6])>(.*?)<\/h[1-6]>$/);
|
|
108
|
+
if (headingMatch) {
|
|
109
|
+
const level = parseInt(headingMatch[1]);
|
|
110
|
+
const text = stripHtml(headingMatch[2]);
|
|
111
|
+
const prefix = '#'.repeat(level) + ' ';
|
|
112
|
+
console.log(chalk.bold(chalk.rgb(255, 165, 0)(prefix + text)));
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Paragraphs
|
|
117
|
+
if (trimmed.startsWith('<p>') && trimmed.endsWith('</p>')) {
|
|
118
|
+
const text = stripHtml(trimmed.slice(3, -4));
|
|
119
|
+
console.log(chalk.gray(text));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Lists
|
|
124
|
+
if (trimmed.startsWith('<ul>') || trimmed.startsWith('<ol>')) {
|
|
125
|
+
// Skip list container tags
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (trimmed.startsWith('</ul>') || trimmed.startsWith('</ol>')) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const liMatch = trimmed.match(/^<li>(.*?)<\/li>$/);
|
|
132
|
+
if (liMatch) {
|
|
133
|
+
const text = stripHtml(liMatch[1]);
|
|
134
|
+
console.log(chalk.green(' • ') + chalk.white(text));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Code blocks
|
|
139
|
+
if (trimmed.startsWith('<pre><code') && trimmed.endsWith('</code></pre>')) {
|
|
140
|
+
// Extract code content
|
|
141
|
+
const codeMatch = trimmed.match(/<pre><code[^>]*>([\s\S]*)<\/code><\/pre>/);
|
|
142
|
+
if (codeMatch) {
|
|
143
|
+
console.log(chalk.cyan('┌─ Code Block ─'));
|
|
144
|
+
console.log(codeMatch[1]);
|
|
145
|
+
console.log(chalk.cyan('└───────────────'));
|
|
146
|
+
}
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Inline code
|
|
151
|
+
if (trimmed.includes('<code>') && !trimmed.includes('<pre>')) {
|
|
152
|
+
let text = trimmed.replace(/<code>(.*?)<\/code>/g, (_, code) => chalk.cyan('`' + stripHtml(code) + '`'));
|
|
153
|
+
text = stripHtml(text);
|
|
154
|
+
console.log(chalk.gray(text));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Blockquotes
|
|
159
|
+
if (trimmed.startsWith('<blockquote>') && trimmed.endsWith('</blockquote>')) {
|
|
160
|
+
const text = stripHtml(trimmed.slice(12, -13));
|
|
161
|
+
console.log(chalk.magenta('│ ') + chalk.gray(text));
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Horizontal rule
|
|
166
|
+
if (trimmed === '<hr>') {
|
|
167
|
+
console.log(chalk.gray('─'.repeat(60)));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Links
|
|
172
|
+
if (trimmed.includes('<a href=')) {
|
|
173
|
+
let text = trimmed.replace(/<a href="([^"]*)">([^<]*)<\/a>/g, (_, url, linkText) =>
|
|
174
|
+
chalk.underline(chalk.blue(linkText)) + chalk.dim(` (${url})`)
|
|
175
|
+
);
|
|
176
|
+
text = stripHtml(text);
|
|
177
|
+
console.log(chalk.gray(text));
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Bold/Italic
|
|
182
|
+
let text = trimmed
|
|
183
|
+
.replace(/<strong>(.*?)<\/strong>/g, (_, t) => chalk.bold(stripHtml(t)))
|
|
184
|
+
.replace(/<b>(.*?)<\/b>/g, (_, t) => chalk.bold(stripHtml(t)))
|
|
185
|
+
.replace(/<em>(.*?)<\/em>/g, (_, t) => chalk.italic(stripHtml(t)))
|
|
186
|
+
.replace(/<i>(.*?)<\/i>/g, (_, t) => chalk.italic(stripHtml(t)));
|
|
187
|
+
|
|
188
|
+
text = stripHtml(text);
|
|
189
|
+
if (text.trim()) {
|
|
190
|
+
console.log(chalk.gray(text));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Strip HTML tags from string
|
|
197
|
+
*/
|
|
198
|
+
function stripHtml(html: string): string {
|
|
199
|
+
return html
|
|
200
|
+
.replace(/<[^>]*>/g, '')
|
|
201
|
+
.replace(/</g, '<')
|
|
202
|
+
.replace(/>/g, '>')
|
|
203
|
+
.replace(/&/g, '&')
|
|
204
|
+
.replace(/"/g, '"')
|
|
205
|
+
.replace(/'/g, "'")
|
|
206
|
+
.replace(/ /g, ' ');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Streaming markdown renderer - processes tokens as they arrive
|
|
211
|
+
*/
|
|
212
|
+
export class StreamingMarkdownRenderer {
|
|
213
|
+
private buffer: string = '';
|
|
214
|
+
private inCodeBlock = false;
|
|
215
|
+
private codeBlockLang = '';
|
|
216
|
+
private codeBuffer: string[] = [];
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Process a chunk of streamed markdown
|
|
220
|
+
*/
|
|
221
|
+
processChunk(chunk: string): void {
|
|
222
|
+
this.buffer += chunk;
|
|
223
|
+
|
|
224
|
+
// Check for code block start
|
|
225
|
+
if (!this.inCodeBlock) {
|
|
226
|
+
const codeBlockStart = this.buffer.match(/```(\w*)\n/);
|
|
227
|
+
if (codeBlockStart && codeBlockStart.index !== undefined) {
|
|
228
|
+
this.inCodeBlock = true;
|
|
229
|
+
this.codeBlockLang = codeBlockStart[1] || '';
|
|
230
|
+
this.codeBuffer = [];
|
|
231
|
+
|
|
232
|
+
// Print everything before the code block
|
|
233
|
+
const beforeCode = this.buffer.slice(0, codeBlockStart.index);
|
|
234
|
+
if (beforeCode.trim()) {
|
|
235
|
+
this.renderInline(beforeCode);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
this.buffer = this.buffer.slice(codeBlockStart.index + codeBlockStart[0].length);
|
|
239
|
+
console.log(chalk.cyan('\n┌─ Code Block (' + (this.codeBlockLang || 'text') + ') ─'));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
// Check for code block end
|
|
244
|
+
const codeBlockEnd = this.buffer.indexOf('\n```');
|
|
245
|
+
if (codeBlockEnd !== -1) {
|
|
246
|
+
const codeContent = this.buffer.slice(0, codeBlockEnd);
|
|
247
|
+
this.codeBuffer.push(codeContent);
|
|
248
|
+
this.buffer = this.buffer.slice(codeBlockEnd + 4);
|
|
249
|
+
this.inCodeBlock = false;
|
|
250
|
+
|
|
251
|
+
// Render the code block with highlighting
|
|
252
|
+
const fullCode = this.codeBuffer.join('\n');
|
|
253
|
+
console.log(highlightCode(fullCode, this.codeBlockLang));
|
|
254
|
+
console.log(chalk.cyan('└──────────────────────────\n'));
|
|
255
|
+
this.codeBuffer = [];
|
|
256
|
+
} else {
|
|
257
|
+
// Still in code block, accumulate
|
|
258
|
+
this.codeBuffer.push(this.buffer);
|
|
259
|
+
this.buffer = '';
|
|
260
|
+
}
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Render non-code content as we go
|
|
265
|
+
this.renderInline(this.buffer);
|
|
266
|
+
this.buffer = '';
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Render inline markdown (non-code-block content)
|
|
271
|
+
*/
|
|
272
|
+
private renderInline(text: string): void {
|
|
273
|
+
if (!text.trim()) return;
|
|
274
|
+
|
|
275
|
+
// Split by newlines and process each line
|
|
276
|
+
const lines = text.split('\n');
|
|
277
|
+
for (const line of lines) {
|
|
278
|
+
if (!line.trim()) continue;
|
|
279
|
+
|
|
280
|
+
// Handle headings
|
|
281
|
+
const headingMatch = line.match(/^(#{1,6})\s+(.*)/);
|
|
282
|
+
if (headingMatch) {
|
|
283
|
+
const level = headingMatch[1].length;
|
|
284
|
+
const text = headingMatch[2];
|
|
285
|
+
const prefix = '#'.repeat(level) + ' ';
|
|
286
|
+
console.log(chalk.bold(chalk.rgb(255, 165, 0)(prefix + text)));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Handle list items
|
|
291
|
+
const listMatch = line.match(/^[\s]*[-*+]\s+(.*)/);
|
|
292
|
+
if (listMatch) {
|
|
293
|
+
console.log(chalk.green(' • ') + chalk.white(this.formatInline(listMatch[1])));
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Handle numbered lists
|
|
298
|
+
const numMatch = line.match(/^[\s]*\d+\.\s+(.*)/);
|
|
299
|
+
if (numMatch) {
|
|
300
|
+
console.log(chalk.green(' 1. ') + chalk.white(this.formatInline(numMatch[1])));
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Handle blockquotes
|
|
305
|
+
const quoteMatch = line.match(/^>\s*(.*)/);
|
|
306
|
+
if (quoteMatch) {
|
|
307
|
+
console.log(chalk.magenta('│ ') + chalk.gray(this.formatInline(quoteMatch[1])));
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Handle horizontal rule
|
|
312
|
+
if (line.match(/^[-*_]{3,}$/)) {
|
|
313
|
+
console.log(chalk.gray('─'.repeat(60)));
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Regular text with inline formatting
|
|
318
|
+
console.log(chalk.gray(this.formatInline(line)));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Format inline markdown elements (bold, italic, code, links)
|
|
324
|
+
*/
|
|
325
|
+
private formatInline(text: string): string {
|
|
326
|
+
return text
|
|
327
|
+
.replace(/\*\*(.*?)\*\*/g, (_, t) => chalk.bold(t))
|
|
328
|
+
.replace(/__(.*?)__/g, (_, t) => chalk.bold(t))
|
|
329
|
+
.replace(/\*(.*?)\*/g, (_, t) => chalk.italic(t))
|
|
330
|
+
.replace(/_(.*?)_/g, (_, t) => chalk.italic(t))
|
|
331
|
+
.replace(/`([^`]+)`/g, (_, t) => chalk.cyan('`' + t + '`'))
|
|
332
|
+
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_, linkText, url) =>
|
|
333
|
+
chalk.underline(chalk.blue(linkText)) + chalk.dim(` (${url})`)
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Flush any remaining buffer
|
|
339
|
+
*/
|
|
340
|
+
flush(): void {
|
|
341
|
+
if (this.buffer.trim()) {
|
|
342
|
+
this.renderInline(this.buffer);
|
|
343
|
+
this.buffer = '';
|
|
344
|
+
}
|
|
345
|
+
if (this.inCodeBlock && this.codeBuffer.length > 0) {
|
|
346
|
+
const fullCode = this.codeBuffer.join('\n');
|
|
347
|
+
console.log(highlightCode(fullCode, this.codeBlockLang));
|
|
348
|
+
console.log(chalk.cyan('└──────────────────────────\n'));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Pure markdown → plain-text-with-unicode-markers converter for the Ink UI.
|
|
355
|
+
*
|
|
356
|
+
* Returns a string (Ink <Text> renders ANSI-free text, so styling is expressed
|
|
357
|
+
* with unicode markers instead of chalk escapes — chalk output would leak raw
|
|
358
|
+
* escape codes into Ink's reconciled frame).
|
|
359
|
+
*/
|
|
360
|
+
export function markdownToText(markdown: string): string {
|
|
361
|
+
if (!markdown || !markdown.trim()) return "";
|
|
362
|
+
const lines = md.render(markdown).split("\n");
|
|
363
|
+
const out: string[] = [];
|
|
364
|
+
|
|
365
|
+
for (const raw of lines) {
|
|
366
|
+
const line = raw.trim();
|
|
367
|
+
if (!line) continue;
|
|
368
|
+
|
|
369
|
+
// Headings: underline with ─ instead of #
|
|
370
|
+
const h = line.match(/^<h([1-6])>(.*?)<\/h[1-6]>$/);
|
|
371
|
+
if (h) {
|
|
372
|
+
const text = stripTags(h[2]);
|
|
373
|
+
out.push("", text.toUpperCase(), "─".repeat(Math.min(text.length, 60)));
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
// Paragraph
|
|
377
|
+
if (line.startsWith("<p>") && line.endsWith("</p>")) {
|
|
378
|
+
out.push(stripTags(line.slice(3, -4)));
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
// List item → bullet
|
|
382
|
+
const li = line.match(/^<li>(.*?)<\/li>$/);
|
|
383
|
+
if (li) {
|
|
384
|
+
out.push(` • ${stripTags(li[1])}`);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
// Blockquote
|
|
388
|
+
if (line.startsWith("<blockquote>")) {
|
|
389
|
+
out.push(`│ ${stripTags(line.replace(/<\/?blockquote>/g, ""))}`);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
// Code block (rendered HTML keeps newlines inside <code>)
|
|
393
|
+
if (line.startsWith("<pre>") || line.startsWith("<code>")) {
|
|
394
|
+
out.push(...stripTags(line).split("\n").map((l) => ` ${l}`));
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
// Horizontal rule
|
|
398
|
+
if (/^<(hr|hr\s)/.test(line)) {
|
|
399
|
+
out.push("─".repeat(60));
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
out.push(stripTags(line));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return out.join("\n");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Strip remaining HTML tags and decode a handful of common entities. */
|
|
409
|
+
function stripTags(html: string): string {
|
|
410
|
+
return html
|
|
411
|
+
.replace(/<br\s*\/?>/g, "\n")
|
|
412
|
+
.replace(/<[^>]+>/g, "")
|
|
413
|
+
.replace(/&/g, "&")
|
|
414
|
+
.replace(/</g, "<")
|
|
415
|
+
.replace(/>/g, ">")
|
|
416
|
+
.replace(/"/g, '"')
|
|
417
|
+
.replace(/'/g, "'");
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export { md };
|