@dirgha/code 1.2.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/LICENSE +101 -0
- package/README.md +674 -0
- package/dist/dirgha.mjs +2185 -0
- package/dist_v2/acp/index.d.ts +1 -0
- package/dist_v2/acp/index.js +2 -0
- package/dist_v2/acp/server.d.ts +31 -0
- package/dist_v2/acp/server.js +137 -0
- package/dist_v2/cli/auth-cmd.d.ts +10 -0
- package/dist_v2/cli/auth-cmd.js +69 -0
- package/dist_v2/cli/config.d.ts +46 -0
- package/dist_v2/cli/config.js +70 -0
- package/dist_v2/cli/flags.d.ts +10 -0
- package/dist_v2/cli/flags.js +49 -0
- package/dist_v2/cli/flows/full-cycle.d.ts +30 -0
- package/dist_v2/cli/flows/full-cycle.js +39 -0
- package/dist_v2/cli/flows/index.d.ts +2 -0
- package/dist_v2/cli/flows/index.js +3 -0
- package/dist_v2/cli/flows/tarball.d.ts +10 -0
- package/dist_v2/cli/flows/tarball.js +47 -0
- package/dist_v2/cli/index.d.ts +4 -0
- package/dist_v2/cli/index.js +5 -0
- package/dist_v2/cli/interactive.d.ts +21 -0
- package/dist_v2/cli/interactive.js +207 -0
- package/dist_v2/cli/main.d.ts +10 -0
- package/dist_v2/cli/main.js +173 -0
- package/dist_v2/cli/models-cmd.d.ts +6 -0
- package/dist_v2/cli/models-cmd.js +50 -0
- package/dist_v2/cli/setup.d.ts +13 -0
- package/dist_v2/cli/setup.js +128 -0
- package/dist_v2/cli/slash/account.d.ts +10 -0
- package/dist_v2/cli/slash/account.js +59 -0
- package/dist_v2/cli/slash/clear.d.ts +8 -0
- package/dist_v2/cli/slash/clear.js +15 -0
- package/dist_v2/cli/slash/compact.d.ts +7 -0
- package/dist_v2/cli/slash/compact.js +13 -0
- package/dist_v2/cli/slash/config.d.ts +8 -0
- package/dist_v2/cli/slash/config.js +49 -0
- package/dist_v2/cli/slash/exit.d.ts +7 -0
- package/dist_v2/cli/slash/exit.js +16 -0
- package/dist_v2/cli/slash/fleet.d.ts +7 -0
- package/dist_v2/cli/slash/fleet.js +22 -0
- package/dist_v2/cli/slash/help.d.ts +9 -0
- package/dist_v2/cli/slash/help.js +31 -0
- package/dist_v2/cli/slash/history.d.ts +7 -0
- package/dist_v2/cli/slash/history.js +42 -0
- package/dist_v2/cli/slash/index.d.ts +30 -0
- package/dist_v2/cli/slash/index.js +52 -0
- package/dist_v2/cli/slash/init.d.ts +8 -0
- package/dist_v2/cli/slash/init.js +57 -0
- package/dist_v2/cli/slash/keys.d.ts +8 -0
- package/dist_v2/cli/slash/keys.js +101 -0
- package/dist_v2/cli/slash/login.d.ts +10 -0
- package/dist_v2/cli/slash/login.js +47 -0
- package/dist_v2/cli/slash/memory.d.ts +8 -0
- package/dist_v2/cli/slash/memory.js +101 -0
- package/dist_v2/cli/slash/mode.d.ts +8 -0
- package/dist_v2/cli/slash/mode.js +30 -0
- package/dist_v2/cli/slash/models.d.ts +8 -0
- package/dist_v2/cli/slash/models.js +73 -0
- package/dist_v2/cli/slash/resume.d.ts +8 -0
- package/dist_v2/cli/slash/resume.js +24 -0
- package/dist_v2/cli/slash/session.d.ts +8 -0
- package/dist_v2/cli/slash/session.js +74 -0
- package/dist_v2/cli/slash/setup.d.ts +8 -0
- package/dist_v2/cli/slash/setup.js +50 -0
- package/dist_v2/cli/slash/status.d.ts +7 -0
- package/dist_v2/cli/slash/status.js +37 -0
- package/dist_v2/cli/slash/theme.d.ts +9 -0
- package/dist_v2/cli/slash/theme.js +53 -0
- package/dist_v2/cli/slash/types.d.ts +14 -0
- package/dist_v2/cli/slash/types.js +9 -0
- package/dist_v2/cli/slash/upgrade.d.ts +9 -0
- package/dist_v2/cli/slash/upgrade.js +66 -0
- package/dist_v2/cli/slash.d.ts +72 -0
- package/dist_v2/cli/slash.js +77 -0
- package/dist_v2/cli/subcommands/ask.d.ts +17 -0
- package/dist_v2/cli/subcommands/ask.js +76 -0
- package/dist_v2/cli/subcommands/audit.d.ts +15 -0
- package/dist_v2/cli/subcommands/audit.js +128 -0
- package/dist_v2/cli/subcommands/chat.d.ts +13 -0
- package/dist_v2/cli/subcommands/chat.js +72 -0
- package/dist_v2/cli/subcommands/compact.d.ts +14 -0
- package/dist_v2/cli/subcommands/compact.js +114 -0
- package/dist_v2/cli/subcommands/doctor.d.ts +11 -0
- package/dist_v2/cli/subcommands/doctor.js +103 -0
- package/dist_v2/cli/subcommands/export-session.d.ts +10 -0
- package/dist_v2/cli/subcommands/export-session.js +60 -0
- package/dist_v2/cli/subcommands/import-session.d.ts +11 -0
- package/dist_v2/cli/subcommands/import-session.js +77 -0
- package/dist_v2/cli/subcommands/index.d.ts +40 -0
- package/dist_v2/cli/subcommands/index.js +38 -0
- package/dist_v2/cli/subcommands/init.d.ts +10 -0
- package/dist_v2/cli/subcommands/init.js +74 -0
- package/dist_v2/cli/subcommands/keys.d.ts +12 -0
- package/dist_v2/cli/subcommands/keys.js +136 -0
- package/dist_v2/cli/subcommands/login.d.ts +10 -0
- package/dist_v2/cli/subcommands/login.js +60 -0
- package/dist_v2/cli/subcommands/logout.d.ts +10 -0
- package/dist_v2/cli/subcommands/logout.js +21 -0
- package/dist_v2/cli/subcommands/models.d.ts +14 -0
- package/dist_v2/cli/subcommands/models.js +144 -0
- package/dist_v2/cli/subcommands/setup.d.ts +11 -0
- package/dist_v2/cli/subcommands/setup.js +107 -0
- package/dist_v2/cli/subcommands/stats.d.ts +13 -0
- package/dist_v2/cli/subcommands/stats.js +148 -0
- package/dist_v2/cli/subcommands/status.d.ts +14 -0
- package/dist_v2/cli/subcommands/status.js +91 -0
- package/dist_v2/cli/submit-paper.d.ts +15 -0
- package/dist_v2/cli/submit-paper.js +68 -0
- package/dist_v2/context/_fts.d.ts +62 -0
- package/dist_v2/context/_fts.js +195 -0
- package/dist_v2/context/branch.d.ts +18 -0
- package/dist_v2/context/branch.js +66 -0
- package/dist_v2/context/compaction.d.ts +26 -0
- package/dist_v2/context/compaction.js +120 -0
- package/dist_v2/context/index.d.ts +5 -0
- package/dist_v2/context/index.js +6 -0
- package/dist_v2/context/knowledge.d.ts +38 -0
- package/dist_v2/context/knowledge.js +146 -0
- package/dist_v2/context/memory.d.ts +80 -0
- package/dist_v2/context/memory.js +276 -0
- package/dist_v2/context/mode.d.ts +25 -0
- package/dist_v2/context/mode.js +81 -0
- package/dist_v2/context/session.d.ts +56 -0
- package/dist_v2/context/session.js +108 -0
- package/dist_v2/daemon/index.d.ts +2 -0
- package/dist_v2/daemon/index.js +3 -0
- package/dist_v2/daemon/protocol.d.ts +73 -0
- package/dist_v2/daemon/protocol.js +6 -0
- package/dist_v2/daemon/server.d.ts +35 -0
- package/dist_v2/daemon/server.js +191 -0
- package/dist_v2/eval/index.d.ts +5 -0
- package/dist_v2/eval/index.js +6 -0
- package/dist_v2/eval/regression.d.ts +14 -0
- package/dist_v2/eval/regression.js +106 -0
- package/dist_v2/eval/reporter.d.ts +7 -0
- package/dist_v2/eval/reporter.js +34 -0
- package/dist_v2/eval/swe-bench.d.ts +18 -0
- package/dist_v2/eval/swe-bench.js +23 -0
- package/dist_v2/eval/terminal-bench.d.ts +13 -0
- package/dist_v2/eval/terminal-bench.js +19 -0
- package/dist_v2/eval/types.d.ts +36 -0
- package/dist_v2/eval/types.js +5 -0
- package/dist_v2/fleet/apply-back.d.ts +27 -0
- package/dist_v2/fleet/apply-back.js +191 -0
- package/dist_v2/fleet/cli-command.d.ts +61 -0
- package/dist_v2/fleet/cli-command.js +329 -0
- package/dist_v2/fleet/index.d.ts +21 -0
- package/dist_v2/fleet/index.js +21 -0
- package/dist_v2/fleet/runner.d.ts +28 -0
- package/dist_v2/fleet/runner.js +347 -0
- package/dist_v2/fleet/tripleshot.d.ts +21 -0
- package/dist_v2/fleet/tripleshot.js +222 -0
- package/dist_v2/fleet/types.d.ts +193 -0
- package/dist_v2/fleet/types.js +24 -0
- package/dist_v2/fleet/worktree.d.ts +51 -0
- package/dist_v2/fleet/worktree.js +176 -0
- package/dist_v2/hooks/index.d.ts +1 -0
- package/dist_v2/hooks/index.js +2 -0
- package/dist_v2/hooks/registry.d.ts +21 -0
- package/dist_v2/hooks/registry.js +40 -0
- package/dist_v2/index.d.ts +12 -0
- package/dist_v2/index.js +11 -0
- package/dist_v2/integrations/arniko-client.d.ts +42 -0
- package/dist_v2/integrations/arniko-client.js +86 -0
- package/dist_v2/integrations/auth.d.ts +45 -0
- package/dist_v2/integrations/auth.js +54 -0
- package/dist_v2/integrations/billing.d.ts +71 -0
- package/dist_v2/integrations/billing.js +107 -0
- package/dist_v2/integrations/bucky-client.d.ts +105 -0
- package/dist_v2/integrations/bucky-client.js +76 -0
- package/dist_v2/integrations/deploy-client.d.ts +60 -0
- package/dist_v2/integrations/deploy-client.js +87 -0
- package/dist_v2/integrations/device-auth.d.ts +48 -0
- package/dist_v2/integrations/device-auth.js +192 -0
- package/dist_v2/integrations/entitlements.d.ts +34 -0
- package/dist_v2/integrations/entitlements.js +43 -0
- package/dist_v2/integrations/http.d.ts +22 -0
- package/dist_v2/integrations/http.js +114 -0
- package/dist_v2/integrations/index.d.ts +8 -0
- package/dist_v2/integrations/index.js +15 -0
- package/dist_v2/intelligence/cost.d.ts +28 -0
- package/dist_v2/intelligence/cost.js +58 -0
- package/dist_v2/intelligence/error-classifier.d.ts +8 -0
- package/dist_v2/intelligence/error-classifier.js +71 -0
- package/dist_v2/intelligence/index.d.ts +5 -0
- package/dist_v2/intelligence/index.js +6 -0
- package/dist_v2/intelligence/prices.d.ts +13 -0
- package/dist_v2/intelligence/prices.js +26 -0
- package/dist_v2/intelligence/smart-router.d.ts +21 -0
- package/dist_v2/intelligence/smart-router.js +59 -0
- package/dist_v2/intelligence/telemetry.d.ts +22 -0
- package/dist_v2/intelligence/telemetry.js +31 -0
- package/dist_v2/kernel/agent-loop.d.ts +32 -0
- package/dist_v2/kernel/agent-loop.js +151 -0
- package/dist_v2/kernel/event-stream.d.ts +18 -0
- package/dist_v2/kernel/event-stream.js +102 -0
- package/dist_v2/kernel/index.d.ts +4 -0
- package/dist_v2/kernel/index.js +5 -0
- package/dist_v2/kernel/message.d.ts +29 -0
- package/dist_v2/kernel/message.js +118 -0
- package/dist_v2/kernel/types.d.ts +206 -0
- package/dist_v2/kernel/types.js +9 -0
- package/dist_v2/mcp/client.d.ts +44 -0
- package/dist_v2/mcp/client.js +63 -0
- package/dist_v2/mcp/index.d.ts +3 -0
- package/dist_v2/mcp/index.js +4 -0
- package/dist_v2/mcp/tool-bridge.d.ts +11 -0
- package/dist_v2/mcp/tool-bridge.js +33 -0
- package/dist_v2/mcp/transport.d.ts +29 -0
- package/dist_v2/mcp/transport.js +73 -0
- package/dist_v2/parity/index.d.ts +3 -0
- package/dist_v2/parity/index.js +4 -0
- package/dist_v2/parity/mock-openai-compat.d.ts +15 -0
- package/dist_v2/parity/mock-openai-compat.js +35 -0
- package/dist_v2/parity/runner.d.ts +21 -0
- package/dist_v2/parity/runner.js +76 -0
- package/dist_v2/parity/scenarios.d.ts +20 -0
- package/dist_v2/parity/scenarios.js +70 -0
- package/dist_v2/providers/anthropic.d.ts +21 -0
- package/dist_v2/providers/anthropic.js +191 -0
- package/dist_v2/providers/dispatch.d.ts +11 -0
- package/dist_v2/providers/dispatch.js +45 -0
- package/dist_v2/providers/fireworks.d.ts +18 -0
- package/dist_v2/providers/fireworks.js +44 -0
- package/dist_v2/providers/gemini.d.ts +19 -0
- package/dist_v2/providers/gemini.js +188 -0
- package/dist_v2/providers/http.d.ts +41 -0
- package/dist_v2/providers/http.js +188 -0
- package/dist_v2/providers/iface.d.ts +18 -0
- package/dist_v2/providers/iface.js +18 -0
- package/dist_v2/providers/index.d.ts +39 -0
- package/dist_v2/providers/index.js +51 -0
- package/dist_v2/providers/nvidia.d.ts +27 -0
- package/dist_v2/providers/nvidia.js +142 -0
- package/dist_v2/providers/ollama.d.ts +15 -0
- package/dist_v2/providers/ollama.js +37 -0
- package/dist_v2/providers/openai-compat.d.ts +29 -0
- package/dist_v2/providers/openai-compat.js +239 -0
- package/dist_v2/providers/openai.d.ts +20 -0
- package/dist_v2/providers/openai.js +107 -0
- package/dist_v2/providers/openrouter.d.ts +24 -0
- package/dist_v2/providers/openrouter.js +69 -0
- package/dist_v2/safety/approval-bus.d.ts +26 -0
- package/dist_v2/safety/approval-bus.js +48 -0
- package/dist_v2/safety/audit-log.d.ts +27 -0
- package/dist_v2/safety/audit-log.js +71 -0
- package/dist_v2/safety/index.d.ts +4 -0
- package/dist_v2/safety/index.js +5 -0
- package/dist_v2/safety/policy.d.ts +32 -0
- package/dist_v2/safety/policy.js +131 -0
- package/dist_v2/safety/sandbox/bwrap.d.ts +11 -0
- package/dist_v2/safety/sandbox/bwrap.js +52 -0
- package/dist_v2/safety/sandbox/iface.d.ts +29 -0
- package/dist_v2/safety/sandbox/iface.js +8 -0
- package/dist_v2/safety/sandbox/index.d.ts +7 -0
- package/dist_v2/safety/sandbox/index.js +8 -0
- package/dist_v2/safety/sandbox/landlock.d.ts +17 -0
- package/dist_v2/safety/sandbox/landlock.js +26 -0
- package/dist_v2/safety/sandbox/noop.d.ts +12 -0
- package/dist_v2/safety/sandbox/noop.js +52 -0
- package/dist_v2/safety/sandbox/seatbelt.d.ts +11 -0
- package/dist_v2/safety/sandbox/seatbelt.js +49 -0
- package/dist_v2/safety/sandbox/select.d.ts +6 -0
- package/dist_v2/safety/sandbox/select.js +42 -0
- package/dist_v2/safety/sandbox/windows.d.ts +13 -0
- package/dist_v2/safety/sandbox/windows.js +22 -0
- package/dist_v2/skills/index.d.ts +3 -0
- package/dist_v2/skills/index.js +4 -0
- package/dist_v2/skills/loader.d.ts +38 -0
- package/dist_v2/skills/loader.js +102 -0
- package/dist_v2/skills/matcher.d.ts +14 -0
- package/dist_v2/skills/matcher.js +72 -0
- package/dist_v2/skills/runtime.d.ts +13 -0
- package/dist_v2/skills/runtime.js +40 -0
- package/dist_v2/subagents/delegator.d.ts +36 -0
- package/dist_v2/subagents/delegator.js +82 -0
- package/dist_v2/subagents/index.d.ts +2 -0
- package/dist_v2/subagents/index.js +3 -0
- package/dist_v2/subagents/pool.d.ts +18 -0
- package/dist_v2/subagents/pool.js +31 -0
- package/dist_v2/tools/browser.d.ts +27 -0
- package/dist_v2/tools/browser.js +179 -0
- package/dist_v2/tools/checkpoint.d.ts +25 -0
- package/dist_v2/tools/checkpoint.js +222 -0
- package/dist_v2/tools/cron.d.ts +32 -0
- package/dist_v2/tools/cron.js +160 -0
- package/dist_v2/tools/diff.d.ts +18 -0
- package/dist_v2/tools/diff.js +117 -0
- package/dist_v2/tools/exec.d.ts +19 -0
- package/dist_v2/tools/exec.js +49 -0
- package/dist_v2/tools/fs-edit.d.ts +11 -0
- package/dist_v2/tools/fs-edit.js +81 -0
- package/dist_v2/tools/fs-ls.d.ts +7 -0
- package/dist_v2/tools/fs-ls.js +57 -0
- package/dist_v2/tools/fs-read.d.ts +7 -0
- package/dist_v2/tools/fs-read.js +60 -0
- package/dist_v2/tools/fs-write.d.ts +8 -0
- package/dist_v2/tools/fs-write.js +46 -0
- package/dist_v2/tools/git.d.ts +8 -0
- package/dist_v2/tools/git.js +57 -0
- package/dist_v2/tools/index.d.ts +25 -0
- package/dist_v2/tools/index.js +39 -0
- package/dist_v2/tools/multimodal.d.ts +41 -0
- package/dist_v2/tools/multimodal.js +381 -0
- package/dist_v2/tools/permission.d.ts +27 -0
- package/dist_v2/tools/permission.js +43 -0
- package/dist_v2/tools/registry.d.ts +44 -0
- package/dist_v2/tools/registry.js +62 -0
- package/dist_v2/tools/search-glob.d.ts +7 -0
- package/dist_v2/tools/search-glob.js +98 -0
- package/dist_v2/tools/search-grep.d.ts +7 -0
- package/dist_v2/tools/search-grep.js +109 -0
- package/dist_v2/tools/shell.d.ts +10 -0
- package/dist_v2/tools/shell.js +76 -0
- package/dist_v2/tools/task.d.ts +8 -0
- package/dist_v2/tools/task.js +37 -0
- package/dist_v2/tui/approval.d.ts +7 -0
- package/dist_v2/tui/approval.js +55 -0
- package/dist_v2/tui/index.d.ts +4 -0
- package/dist_v2/tui/index.js +5 -0
- package/dist_v2/tui/ink/App.d.ts +39 -0
- package/dist_v2/tui/ink/App.js +206 -0
- package/dist_v2/tui/ink/components/AtFileComplete.d.ts +25 -0
- package/dist_v2/tui/ink/components/AtFileComplete.js +158 -0
- package/dist_v2/tui/ink/components/HelpOverlay.d.ts +24 -0
- package/dist_v2/tui/ink/components/HelpOverlay.js +130 -0
- package/dist_v2/tui/ink/components/InputBox.d.ts +35 -0
- package/dist_v2/tui/ink/components/InputBox.js +153 -0
- package/dist_v2/tui/ink/components/Logo.d.ts +12 -0
- package/dist_v2/tui/ink/components/Logo.js +27 -0
- package/dist_v2/tui/ink/components/ModelPicker.d.ts +26 -0
- package/dist_v2/tui/ink/components/ModelPicker.js +80 -0
- package/dist_v2/tui/ink/components/PasteCollapse.d.ts +36 -0
- package/dist_v2/tui/ink/components/PasteCollapse.js +59 -0
- package/dist_v2/tui/ink/components/StatusBar.d.ts +18 -0
- package/dist_v2/tui/ink/components/StatusBar.js +41 -0
- package/dist_v2/tui/ink/components/StreamingText.d.ts +14 -0
- package/dist_v2/tui/ink/components/StreamingText.js +11 -0
- package/dist_v2/tui/ink/components/ThinkingBlock.d.ts +13 -0
- package/dist_v2/tui/ink/components/ThinkingBlock.js +13 -0
- package/dist_v2/tui/ink/components/ToolBox.d.ts +20 -0
- package/dist_v2/tui/ink/components/ToolBox.js +63 -0
- package/dist_v2/tui/ink/components/vim-bindings.d.ts +43 -0
- package/dist_v2/tui/ink/components/vim-bindings.js +135 -0
- package/dist_v2/tui/ink/index.d.ts +39 -0
- package/dist_v2/tui/ink/index.js +47 -0
- package/dist_v2/tui/ink/use-event-projection.d.ts +51 -0
- package/dist_v2/tui/ink/use-event-projection.js +126 -0
- package/dist_v2/tui/ink/use-overlays.d.ts +24 -0
- package/dist_v2/tui/ink/use-overlays.js +50 -0
- package/dist_v2/tui/renderer.d.ts +19 -0
- package/dist_v2/tui/renderer.js +64 -0
- package/dist_v2/tui/status-bar.d.ts +12 -0
- package/dist_v2/tui/status-bar.js +16 -0
- package/dist_v2/tui/theme.d.ts +27 -0
- package/dist_v2/tui/theme.js +56 -0
- package/dist_v2/utils/json-repair.d.ts +7 -0
- package/dist_v2/utils/json-repair.js +45 -0
- package/package.json +113 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive REPL. A thin readline loop that routes user input either
|
|
3
|
+
* to the agent loop (regular prompts) or to the slash registry
|
|
4
|
+
* (commands starting with /). Streaming output is rendered via the TUI
|
|
5
|
+
* renderer subscribed to the shared event stream.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
import { createInterface } from 'node:readline';
|
|
9
|
+
import { createEventStream } from '../kernel/event-stream.js';
|
|
10
|
+
import { runAgentLoop } from '../kernel/agent-loop.js';
|
|
11
|
+
import { createToolExecutor } from '../tools/exec.js';
|
|
12
|
+
import { renderStreamingEvents } from '../tui/renderer.js';
|
|
13
|
+
import { getTheme, style } from '../tui/theme.js';
|
|
14
|
+
import { createTuiApprovalBus } from '../tui/approval.js';
|
|
15
|
+
import { maybeCompact } from '../context/compaction.js';
|
|
16
|
+
import { createDefaultSlashRegistry, registerBuiltinSlashCommands } from './slash.js';
|
|
17
|
+
import { loadToken, migrateLegacyAuth } from '../integrations/device-auth.js';
|
|
18
|
+
import { applyMode, resolveMode } from '../context/mode.js';
|
|
19
|
+
export async function runInteractive(opts) {
|
|
20
|
+
const sessionId = randomUUID();
|
|
21
|
+
const session = await opts.sessions.create(sessionId);
|
|
22
|
+
const events = createEventStream();
|
|
23
|
+
const slash = createDefaultSlashRegistry();
|
|
24
|
+
await registerBuiltinSlashCommands(slash);
|
|
25
|
+
const approvalBus = createTuiApprovalBus(new Set(opts.config.autoApproveTools));
|
|
26
|
+
const render = renderStreamingEvents({ showThinking: opts.config.showThinking });
|
|
27
|
+
events.subscribe(render);
|
|
28
|
+
let currentModel = opts.config.model;
|
|
29
|
+
let currentMode = await resolveMode();
|
|
30
|
+
let currentThemeName = (opts.config.theme ?? 'dark');
|
|
31
|
+
let currentTheme = getTheme(currentThemeName);
|
|
32
|
+
const initial = [...(opts.initialMessages ?? [])];
|
|
33
|
+
// System prompt is rebuilt per turn below so mode changes apply live.
|
|
34
|
+
const history = [...initial];
|
|
35
|
+
const totals = { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 };
|
|
36
|
+
events.subscribe(ev => {
|
|
37
|
+
if (ev.type === 'usage') {
|
|
38
|
+
totals.inputTokens += ev.inputTokens;
|
|
39
|
+
totals.outputTokens += ev.outputTokens;
|
|
40
|
+
totals.cachedTokens += ev.cachedTokens ?? 0;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
// Canonical auth lives in device-auth.ts. Migrate any legacy blob
|
|
44
|
+
// synchronously-before-REPL-start, then prime the slash-command token.
|
|
45
|
+
await migrateLegacyAuth().catch(() => undefined);
|
|
46
|
+
let currentToken = await loadToken();
|
|
47
|
+
process.stdout.write(style(currentTheme.accent, '\ndirgha-cli interactive mode. /help for commands.\n\n'));
|
|
48
|
+
const rl = createInterface({
|
|
49
|
+
input: process.stdin,
|
|
50
|
+
output: process.stdout,
|
|
51
|
+
terminal: true,
|
|
52
|
+
prompt: style(currentTheme.userPrompt, '❯ '),
|
|
53
|
+
});
|
|
54
|
+
rl.prompt();
|
|
55
|
+
rl.on('line', line => { void handleLine(line); });
|
|
56
|
+
rl.on('close', () => process.exit(0));
|
|
57
|
+
const emitStatus = (message) => {
|
|
58
|
+
process.stdout.write(style(currentTheme.muted, `\n${message}\n`));
|
|
59
|
+
rl.prompt();
|
|
60
|
+
};
|
|
61
|
+
/** Build the per-turn system prompt from options + current mode. */
|
|
62
|
+
const buildSystem = () => {
|
|
63
|
+
const base = opts.systemPrompt;
|
|
64
|
+
return applyMode(base, currentMode);
|
|
65
|
+
};
|
|
66
|
+
const handleLine = async (raw) => {
|
|
67
|
+
const line = raw.trim();
|
|
68
|
+
if (line.length === 0) {
|
|
69
|
+
rl.prompt();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (line.startsWith('/')) {
|
|
73
|
+
const ctx = buildSlashCtx({
|
|
74
|
+
session,
|
|
75
|
+
opts,
|
|
76
|
+
modelRef: { get model() { return currentModel; }, set model(v) { currentModel = v; } },
|
|
77
|
+
totals,
|
|
78
|
+
clearHistory: () => { history.length = 0; history.push(...initial); },
|
|
79
|
+
exit: () => rl.close(),
|
|
80
|
+
tokenRef: { get token() { return currentToken; }, set token(v) { currentToken = v; } },
|
|
81
|
+
status: emitStatus,
|
|
82
|
+
modeRef: { get mode() { return currentMode; }, set mode(v) { currentMode = v; } },
|
|
83
|
+
themeRef: {
|
|
84
|
+
get name() { return currentThemeName; },
|
|
85
|
+
set name(v) {
|
|
86
|
+
currentThemeName = v;
|
|
87
|
+
currentTheme = getTheme(v);
|
|
88
|
+
rl.setPrompt(style(currentTheme.userPrompt, '❯ '));
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
providerForCurrent: () => opts.providers.forModel(currentModel),
|
|
92
|
+
summaryModel: opts.config.summaryModel,
|
|
93
|
+
});
|
|
94
|
+
const result = await slash.dispatch(line, ctx);
|
|
95
|
+
if (result.output)
|
|
96
|
+
process.stdout.write(`${result.output}\n`);
|
|
97
|
+
rl.prompt();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// Rebuild system prompt per turn so /mode changes apply immediately.
|
|
101
|
+
const system = buildSystem();
|
|
102
|
+
const turnHistory = system ? [{ role: 'system', content: system }, ...history] : [...history];
|
|
103
|
+
turnHistory.push({ role: 'user', content: line });
|
|
104
|
+
history.push({ role: 'user', content: line });
|
|
105
|
+
await session.append({ type: 'message', ts: new Date().toISOString(), message: { role: 'user', content: line } });
|
|
106
|
+
const executor = createToolExecutor({ registry: opts.registry, cwd: opts.cwd, sessionId });
|
|
107
|
+
const sanitized = opts.registry.sanitize({ descriptionLimit: 200 });
|
|
108
|
+
const provider = opts.providers.forModel(currentModel);
|
|
109
|
+
try {
|
|
110
|
+
const result = await runAgentLoop({
|
|
111
|
+
sessionId,
|
|
112
|
+
model: currentModel,
|
|
113
|
+
messages: turnHistory,
|
|
114
|
+
tools: sanitized.definitions,
|
|
115
|
+
maxTurns: opts.config.maxTurns,
|
|
116
|
+
provider,
|
|
117
|
+
toolExecutor: executor,
|
|
118
|
+
approvalBus,
|
|
119
|
+
events,
|
|
120
|
+
contextTransform: async (msgs) => (await maybeCompact(msgs, {
|
|
121
|
+
triggerTokens: opts.config.compaction.triggerTokens,
|
|
122
|
+
preserveLastTurns: opts.config.compaction.preserveLastTurns,
|
|
123
|
+
summarizer: provider,
|
|
124
|
+
summaryModel: opts.config.summaryModel,
|
|
125
|
+
}, session)).messages,
|
|
126
|
+
});
|
|
127
|
+
// Persist only the non-system portion so /mode changes don't
|
|
128
|
+
// calcify a stale preamble into the history.
|
|
129
|
+
history.length = 0;
|
|
130
|
+
history.push(...result.messages.filter(m => m.role !== 'system'));
|
|
131
|
+
for (const msg of result.messages.slice(-4)) {
|
|
132
|
+
await session.append({ type: 'message', ts: new Date().toISOString(), message: msg });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
process.stdout.write(style(currentTheme.danger, `\n[fatal] ${err instanceof Error ? err.message : String(err)}\n`));
|
|
137
|
+
}
|
|
138
|
+
rl.prompt();
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function buildSlashCtx(a) {
|
|
142
|
+
const apiBase = () => process.env['DIRGHA_API_BASE'] ?? process.env['DIRGHA_GATEWAY_URL'] ?? 'https://api.dirgha.ai';
|
|
143
|
+
const upgradeUrl = () => process.env['DIRGHA_UPGRADE_URL'] ?? 'https://dirgha.ai/billing/upgrade';
|
|
144
|
+
return {
|
|
145
|
+
get model() { return a.modelRef.model; },
|
|
146
|
+
get sessionId() { return a.session.id; },
|
|
147
|
+
setModel(value) { a.modelRef.model = value; },
|
|
148
|
+
showHelp() {
|
|
149
|
+
return [
|
|
150
|
+
'Slash commands:',
|
|
151
|
+
' /help Show this message',
|
|
152
|
+
' /model [id] Show or switch model',
|
|
153
|
+
' /mode [plan|act|verify] Change execution mode',
|
|
154
|
+
' /theme [dark|light|none] Change TUI theme',
|
|
155
|
+
' /compact Manually compact history',
|
|
156
|
+
' /clear Clear transcript (keeps session on disk)',
|
|
157
|
+
' /session list List saved sessions',
|
|
158
|
+
' /session load <id> Load a session',
|
|
159
|
+
' /session branch <name> Branch current session with a summary',
|
|
160
|
+
' /skills List skills available for this turn',
|
|
161
|
+
' /cost Show cumulative usage',
|
|
162
|
+
' /login Sign in via device-code flow',
|
|
163
|
+
' /account Show billing tier, balance, limits',
|
|
164
|
+
' /upgrade Upgrade to a paid tier',
|
|
165
|
+
' /exit, /quit Exit',
|
|
166
|
+
].join('\n');
|
|
167
|
+
},
|
|
168
|
+
async compact() {
|
|
169
|
+
return '(compact runs automatically each turn; use /cost to inspect usage.)';
|
|
170
|
+
},
|
|
171
|
+
clear() {
|
|
172
|
+
a.clearHistory();
|
|
173
|
+
process.stdout.write(style(a.themeRef.name === 'none' ? '' : '\x1b[32m', '(transcript cleared)\n'));
|
|
174
|
+
},
|
|
175
|
+
async listSessions() {
|
|
176
|
+
const ids = await a.opts.sessions.list();
|
|
177
|
+
return ids.length === 0 ? '(no saved sessions)' : ids.map(id => `- ${id}`).join('\n');
|
|
178
|
+
},
|
|
179
|
+
async loadSession(id) {
|
|
180
|
+
const next = await a.opts.sessions.open(id);
|
|
181
|
+
if (!next)
|
|
182
|
+
return `Session ${id} not found.`;
|
|
183
|
+
return `Loaded ${id}. Previous messages: ${(await next.messages()).length}.`;
|
|
184
|
+
},
|
|
185
|
+
async listSkills() {
|
|
186
|
+
return '(skills system is wired; run `dirgha skills` for full detail.)';
|
|
187
|
+
},
|
|
188
|
+
showCost() {
|
|
189
|
+
return `tokens in=${a.totals.inputTokens} out=${a.totals.outputTokens} cached=${a.totals.cachedTokens} cost=$${a.totals.costUsd.toFixed(4)}`;
|
|
190
|
+
},
|
|
191
|
+
exit(code = 0) { a.exit(); process.exit(code); },
|
|
192
|
+
getToken() { return a.tokenRef.token; },
|
|
193
|
+
setToken(value) { a.tokenRef.token = value; },
|
|
194
|
+
apiBase,
|
|
195
|
+
upgradeUrl,
|
|
196
|
+
status: a.status,
|
|
197
|
+
getMode() { return a.modeRef.mode; },
|
|
198
|
+
setMode(value) { a.modeRef.mode = value; },
|
|
199
|
+
getTheme() { return a.themeRef.name; },
|
|
200
|
+
setTheme(value) { a.themeRef.name = value; },
|
|
201
|
+
getSession() { return a.session; },
|
|
202
|
+
getSessionStore() { return a.opts.sessions; },
|
|
203
|
+
getProvider() { return a.providerForCurrent(); },
|
|
204
|
+
getSummaryModel() { return a.summaryModel; },
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=interactive.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI entry point.
|
|
4
|
+
*
|
|
5
|
+
* Default behaviour: when a prompt is provided on the command line, run
|
|
6
|
+
* a single turn non-interactively and exit. When no prompt is provided
|
|
7
|
+
* and stdin is a TTY, enter the interactive REPL. When stdin is piped,
|
|
8
|
+
* read the prompt from stdin and run non-interactively.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI entry point.
|
|
4
|
+
*
|
|
5
|
+
* Default behaviour: when a prompt is provided on the command line, run
|
|
6
|
+
* a single turn non-interactively and exit. When no prompt is provided
|
|
7
|
+
* and stdin is a TTY, enter the interactive REPL. When stdin is piped,
|
|
8
|
+
* read the prompt from stdin and run non-interactively.
|
|
9
|
+
*/
|
|
10
|
+
import { argv, cwd, exit, stdin, stdout } from 'node:process';
|
|
11
|
+
import { randomUUID } from 'node:crypto';
|
|
12
|
+
import { createEventStream } from '../kernel/event-stream.js';
|
|
13
|
+
import { runAgentLoop } from '../kernel/agent-loop.js';
|
|
14
|
+
import { ProviderRegistry } from '../providers/index.js';
|
|
15
|
+
import { builtInTools, createToolExecutor, createToolRegistry } from '../tools/index.js';
|
|
16
|
+
import { loadConfig } from './config.js';
|
|
17
|
+
import { parseFlags } from './flags.js';
|
|
18
|
+
import { runInteractive } from './interactive.js';
|
|
19
|
+
import { runInkTUI } from '../tui/ink/index.js';
|
|
20
|
+
import { renderStreamingEvents } from '../tui/renderer.js';
|
|
21
|
+
import { createSessionStore } from '../context/session.js';
|
|
22
|
+
import { runSubmitPaper } from './submit-paper.js';
|
|
23
|
+
import { runLogin, runLogout, runSetup, findSubcommand } from './subcommands/index.js';
|
|
24
|
+
async function main() {
|
|
25
|
+
const { flags, positionals } = parseFlags(argv.slice(2));
|
|
26
|
+
if (flags.help || flags.h) {
|
|
27
|
+
printHelp();
|
|
28
|
+
exit(0);
|
|
29
|
+
}
|
|
30
|
+
// Subcommand dispatch (positional 0 as verb).
|
|
31
|
+
if (positionals[0] === 'submit-paper') {
|
|
32
|
+
const doi = positionals[1];
|
|
33
|
+
if (!doi) {
|
|
34
|
+
stdout.write('usage: dirgha submit-paper <doi> [--open-pr]\n');
|
|
35
|
+
exit(1);
|
|
36
|
+
}
|
|
37
|
+
const code = await runSubmitPaper({ doi, openPr: flags['open-pr'] === true });
|
|
38
|
+
exit(code);
|
|
39
|
+
}
|
|
40
|
+
if (positionals[0] === 'login')
|
|
41
|
+
exit(await runLogin(positionals.slice(1)));
|
|
42
|
+
if (positionals[0] === 'logout')
|
|
43
|
+
exit(await runLogout(positionals.slice(1)));
|
|
44
|
+
if (positionals[0] === 'setup')
|
|
45
|
+
exit(await runSetup(positionals.slice(1)));
|
|
46
|
+
// Generic subcommand dispatch. Covers: doctor, audit, stats, status,
|
|
47
|
+
// init, keys, models, chat, ask, compact, export-session,
|
|
48
|
+
// import-session (plus anything the auth agent adds to the barrel).
|
|
49
|
+
//
|
|
50
|
+
// We pass the raw argv tail (unparsed) so each subcommand can re-run
|
|
51
|
+
// parseFlags with its own conventions. The tail starts after the
|
|
52
|
+
// first occurrence of the verb in argv.
|
|
53
|
+
{
|
|
54
|
+
const verb = positionals[0];
|
|
55
|
+
const cmd = verb ? findSubcommand(verb) : undefined;
|
|
56
|
+
if (cmd) {
|
|
57
|
+
const rawArgs = argv.slice(2);
|
|
58
|
+
const verbIdx = rawArgs.indexOf(verb);
|
|
59
|
+
const tail = verbIdx >= 0 ? rawArgs.slice(verbIdx + 1) : positionals.slice(1);
|
|
60
|
+
const code = await cmd.run(tail, { cwd: cwd() });
|
|
61
|
+
exit(code);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const config = await loadConfig(cwd());
|
|
65
|
+
const model = (typeof flags.model === 'string' ? flags.model : (typeof flags.m === 'string' ? flags.m : config.model));
|
|
66
|
+
const json = flags.json === true;
|
|
67
|
+
const print = flags.print === true;
|
|
68
|
+
const system = typeof flags.system === 'string' ? flags.system : (typeof flags.s === 'string' ? flags.s : undefined);
|
|
69
|
+
const maxTurns = typeof flags['max-turns'] === 'string' ? Number.parseInt(flags['max-turns'], 10) : config.maxTurns;
|
|
70
|
+
const providers = new ProviderRegistry();
|
|
71
|
+
const registry = createToolRegistry(builtInTools);
|
|
72
|
+
const sessions = createSessionStore();
|
|
73
|
+
let prompt = positionals.join(' ').trim();
|
|
74
|
+
if (!prompt && !stdin.isTTY)
|
|
75
|
+
prompt = (await readAllStdin()).trim();
|
|
76
|
+
if (!prompt) {
|
|
77
|
+
if (print || json) {
|
|
78
|
+
printHelp();
|
|
79
|
+
exit(1);
|
|
80
|
+
}
|
|
81
|
+
// Ink TUI is the default interactive renderer. Fall back to the
|
|
82
|
+
// readline REPL when DIRGHA_NO_INK=1 (diagnostic / CI escape hatch).
|
|
83
|
+
const useInk = process.env['DIRGHA_NO_INK'] !== '1' && stdin.isTTY;
|
|
84
|
+
if (useInk) {
|
|
85
|
+
await runInkTUI({ registry, providers, sessions, config, cwd: cwd(), systemPrompt: system });
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
await runInteractive({ registry, providers, sessions, config, cwd: cwd(), systemPrompt: system });
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const sessionId = randomUUID();
|
|
93
|
+
const events = createEventStream();
|
|
94
|
+
if (json) {
|
|
95
|
+
events.subscribe(ev => { stdout.write(`${JSON.stringify(ev)}\n`); });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
events.subscribe(renderStreamingEvents({ showThinking: config.showThinking }));
|
|
99
|
+
}
|
|
100
|
+
const executor = createToolExecutor({ registry, cwd: cwd(), sessionId });
|
|
101
|
+
const sanitized = registry.sanitize({ descriptionLimit: 200 });
|
|
102
|
+
const messages = [];
|
|
103
|
+
if (system)
|
|
104
|
+
messages.push({ role: 'system', content: system });
|
|
105
|
+
messages.push({ role: 'user', content: prompt });
|
|
106
|
+
const result = await runAgentLoop({
|
|
107
|
+
sessionId,
|
|
108
|
+
model,
|
|
109
|
+
messages,
|
|
110
|
+
tools: sanitized.definitions,
|
|
111
|
+
maxTurns,
|
|
112
|
+
provider: providers.forModel(model),
|
|
113
|
+
toolExecutor: executor,
|
|
114
|
+
events,
|
|
115
|
+
});
|
|
116
|
+
if (!json)
|
|
117
|
+
stdout.write('\n');
|
|
118
|
+
if (result.stopReason === 'error')
|
|
119
|
+
exit(2);
|
|
120
|
+
}
|
|
121
|
+
function readAllStdin() {
|
|
122
|
+
return new Promise(resolve => {
|
|
123
|
+
const chunks = [];
|
|
124
|
+
stdin.setEncoding('utf8');
|
|
125
|
+
stdin.on('data', c => chunks.push(typeof c === 'string' ? c : c.toString('utf8')));
|
|
126
|
+
stdin.on('end', () => resolve(chunks.join('')));
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function printHelp() {
|
|
130
|
+
stdout.write(`dirgha — coding agent CLI
|
|
131
|
+
|
|
132
|
+
Usage:
|
|
133
|
+
dirgha Interactive REPL
|
|
134
|
+
dirgha "prompt" One-shot non-interactive (equivalent to dirgha ask)
|
|
135
|
+
echo prompt | dirgha --print Stdin prompt → stdout answer
|
|
136
|
+
dirgha --json "prompt" NDJSON event stream
|
|
137
|
+
|
|
138
|
+
Subcommands:
|
|
139
|
+
ask "<prompt>" Headless agent (tools, --max-turns 30 default)
|
|
140
|
+
chat "<prompt>" Pure LLM call, no tools
|
|
141
|
+
doctor [--json] Environment diagnostics
|
|
142
|
+
status [--json] Account / model / providers / sessions
|
|
143
|
+
stats [today|week|month|all] Usage aggregates
|
|
144
|
+
audit [list|tail|search <q>] Local audit log
|
|
145
|
+
init [path] [--force] Scaffold DIRGHA.md
|
|
146
|
+
keys <list|set|get|clear> ... BYOK key store (~/.dirgha/keys.json)
|
|
147
|
+
models <list|default|info> ... Model catalogue + default
|
|
148
|
+
compact [sessionId] Force-compact a session on disk
|
|
149
|
+
export-session <id> [path|-] Dump session JSON
|
|
150
|
+
import-session <path> Load session JSON into the store
|
|
151
|
+
login / logout / setup Auth + first-run wizard
|
|
152
|
+
submit-paper <doi> Fetch Crossref metadata, emit JSON
|
|
153
|
+
|
|
154
|
+
Options:
|
|
155
|
+
-m, --model <id> Model id (default from config)
|
|
156
|
+
-t, --temperature <n> Sampling temperature
|
|
157
|
+
-s, --system <text> System prompt
|
|
158
|
+
--max-turns <n> Max agent turns
|
|
159
|
+
--print Non-interactive; text output
|
|
160
|
+
--json NDJSON event output
|
|
161
|
+
-h, --help This help
|
|
162
|
+
|
|
163
|
+
Environment:
|
|
164
|
+
DIRGHA_MODEL, DIRGHA_MAX_TURNS, DIRGHA_SHOW_THINKING
|
|
165
|
+
NVIDIA_API_KEY, OPENROUTER_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY,
|
|
166
|
+
GEMINI_API_KEY / GOOGLE_API_KEY
|
|
167
|
+
`);
|
|
168
|
+
}
|
|
169
|
+
main().catch((err) => {
|
|
170
|
+
stdout.write(`\nFatal: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
171
|
+
exit(2);
|
|
172
|
+
});
|
|
173
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dirgha models` subcommand. Enumerates the model catalogue grouped
|
|
3
|
+
* by provider, marking which providers are currently configured
|
|
4
|
+
* (either via env var or a stored auth token).
|
|
5
|
+
*/
|
|
6
|
+
import { PRICES } from '../intelligence/prices.js';
|
|
7
|
+
import { style, defaultTheme } from '../tui/theme.js';
|
|
8
|
+
const ENV_FOR_PROVIDER = {
|
|
9
|
+
anthropic: 'ANTHROPIC_API_KEY',
|
|
10
|
+
openai: 'OPENAI_API_KEY',
|
|
11
|
+
gemini: 'GEMINI_API_KEY',
|
|
12
|
+
nvidia: 'NVIDIA_API_KEY',
|
|
13
|
+
openrouter: 'OPENROUTER_API_KEY',
|
|
14
|
+
ollama: '',
|
|
15
|
+
};
|
|
16
|
+
export function runModels() {
|
|
17
|
+
const rows = PRICES.map(p => ({
|
|
18
|
+
provider: p.provider,
|
|
19
|
+
model: p.model,
|
|
20
|
+
inputUsd: p.inputPerM === 0 ? 'free' : `$${p.inputPerM.toFixed(2)}/M`,
|
|
21
|
+
outputUsd: p.outputPerM === 0 ? 'free' : `$${p.outputPerM.toFixed(2)}/M`,
|
|
22
|
+
configured: isConfigured(p.provider),
|
|
23
|
+
}));
|
|
24
|
+
const byProvider = new Map();
|
|
25
|
+
for (const row of rows) {
|
|
26
|
+
const list = byProvider.get(row.provider) ?? [];
|
|
27
|
+
list.push(row);
|
|
28
|
+
byProvider.set(row.provider, list);
|
|
29
|
+
}
|
|
30
|
+
process.stdout.write(style(defaultTheme.accent, '\nModel catalogue\n'));
|
|
31
|
+
for (const [provider, list] of byProvider) {
|
|
32
|
+
const env = ENV_FOR_PROVIDER[provider] ?? '';
|
|
33
|
+
const marker = list[0].configured
|
|
34
|
+
? style(defaultTheme.success, 'configured')
|
|
35
|
+
: style(defaultTheme.muted, env ? `set ${env} to enable` : 'no key required');
|
|
36
|
+
process.stdout.write(`\n${style(defaultTheme.userPrompt, provider)} (${marker})\n`);
|
|
37
|
+
for (const row of list) {
|
|
38
|
+
process.stdout.write(` ${row.model.padEnd(44)} in ${row.inputUsd.padEnd(12)} out ${row.outputUsd}\n`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
process.stdout.write('\nUse `-m <model>` on the command line or `/model <id>` inside the REPL.\n');
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
function isConfigured(provider) {
|
|
45
|
+
const env = ENV_FOR_PROVIDER[provider];
|
|
46
|
+
if (!env)
|
|
47
|
+
return true;
|
|
48
|
+
return Boolean(process.env[env] && process.env[env].length > 0);
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=models-cmd.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-run interactive setup wizard.
|
|
3
|
+
*
|
|
4
|
+
* Prompts the user for provider API keys (BYOK), picks a default
|
|
5
|
+
* model, and persists the result to `~/.dirgha/config.json` plus a
|
|
6
|
+
* sibling `~/.dirgha/env` file that exports the keys for future
|
|
7
|
+
* invocations. The wizard never overwrites an existing non-empty value
|
|
8
|
+
* without confirmation.
|
|
9
|
+
*/
|
|
10
|
+
export interface SetupOptions {
|
|
11
|
+
home?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function runSetup(opts?: SetupOptions): Promise<void>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-run interactive setup wizard.
|
|
3
|
+
*
|
|
4
|
+
* Prompts the user for provider API keys (BYOK), picks a default
|
|
5
|
+
* model, and persists the result to `~/.dirgha/config.json` plus a
|
|
6
|
+
* sibling `~/.dirgha/env` file that exports the keys for future
|
|
7
|
+
* invocations. The wizard never overwrites an existing non-empty value
|
|
8
|
+
* without confirmation.
|
|
9
|
+
*/
|
|
10
|
+
import { chmod, mkdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { createInterface } from 'node:readline';
|
|
13
|
+
import { dirname, join } from 'node:path';
|
|
14
|
+
import { DEFAULT_CONFIG } from './config.js';
|
|
15
|
+
import { style, defaultTheme } from '../tui/theme.js';
|
|
16
|
+
const PROVIDERS = [
|
|
17
|
+
{ label: 'NVIDIA NIM', env: 'NVIDIA_API_KEY', helpUrl: 'https://build.nvidia.com/settings/api-keys', suggested: ['moonshotai/kimi-k2-instruct', 'qwen/qwen3-next-80b-a3b-instruct', 'meta/llama-3.3-70b-instruct'] },
|
|
18
|
+
{ label: 'OpenRouter', env: 'OPENROUTER_API_KEY', helpUrl: 'https://openrouter.ai/keys', suggested: ['inclusionai/ling-2.6-1t:free', 'anthropic/claude-sonnet-4-6', 'openai/gpt-5-mini'] },
|
|
19
|
+
{ label: 'Anthropic', env: 'ANTHROPIC_API_KEY', helpUrl: 'https://console.anthropic.com/settings/keys', suggested: ['claude-opus-4-7', 'claude-sonnet-4-6', 'claude-haiku-4-5'] },
|
|
20
|
+
{ label: 'OpenAI', env: 'OPENAI_API_KEY', helpUrl: 'https://platform.openai.com/api-keys', suggested: ['gpt-5', 'gpt-5-mini', 'gpt-4o', 'gpt-4o-mini', 'o1'] },
|
|
21
|
+
{ label: 'Google AI', env: 'GEMINI_API_KEY', helpUrl: 'https://aistudio.google.com/apikey', suggested: ['gemini-2.5-pro', 'gemini-2.5-flash'] },
|
|
22
|
+
];
|
|
23
|
+
export async function runSetup(opts = {}) {
|
|
24
|
+
const home = opts.home ?? homedir();
|
|
25
|
+
const dir = join(home, '.dirgha');
|
|
26
|
+
const configPath = join(dir, 'config.json');
|
|
27
|
+
const envPath = join(dir, 'env');
|
|
28
|
+
await mkdir(dir, { recursive: true });
|
|
29
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
30
|
+
const ask = (question, defaultValue) => new Promise(resolve => {
|
|
31
|
+
const suffix = defaultValue !== undefined && defaultValue.length > 0 ? ` [${maskIfSecret(defaultValue)}]` : '';
|
|
32
|
+
rl.question(`${question}${suffix}: `, answer => resolve(answer.trim() || defaultValue || ''));
|
|
33
|
+
});
|
|
34
|
+
print(`\n${style(defaultTheme.accent, 'dirgha-cli setup')} — bring your own keys + pick a default model.\n`);
|
|
35
|
+
print(`Config file: ${configPath}`);
|
|
36
|
+
print(`Env file: ${envPath}\n`);
|
|
37
|
+
const existing = await readJson(configPath);
|
|
38
|
+
const existingEnv = await readEnvFile(envPath);
|
|
39
|
+
const collectedKeys = { ...existingEnv };
|
|
40
|
+
for (const provider of PROVIDERS) {
|
|
41
|
+
const current = existingEnv[provider.env] ?? process.env[provider.env] ?? '';
|
|
42
|
+
print(style(defaultTheme.userPrompt, `\n${provider.label}`) + style(defaultTheme.muted, ` (${provider.helpUrl})`));
|
|
43
|
+
const answer = await ask(` ${provider.env}`, current);
|
|
44
|
+
if (answer)
|
|
45
|
+
collectedKeys[provider.env] = answer;
|
|
46
|
+
}
|
|
47
|
+
print('\nChoose a default model.');
|
|
48
|
+
const suggestions = PROVIDERS.flatMap(p => p.suggested);
|
|
49
|
+
suggestions.forEach((m, i) => { print(` ${i + 1}. ${m}`); });
|
|
50
|
+
const selection = await ask(' choose by number or paste a model id', existing.model ?? DEFAULT_CONFIG.model);
|
|
51
|
+
const defaultModel = resolveModel(selection, suggestions, existing.model ?? DEFAULT_CONFIG.model);
|
|
52
|
+
const showThinkingAns = (await ask('\n Show model thinking when available? (y/N)', existing.showThinking ? 'y' : 'n')).toLowerCase();
|
|
53
|
+
const showThinking = showThinkingAns === 'y' || showThinkingAns === 'yes';
|
|
54
|
+
rl.close();
|
|
55
|
+
const config = {
|
|
56
|
+
...existing,
|
|
57
|
+
model: defaultModel,
|
|
58
|
+
showThinking,
|
|
59
|
+
};
|
|
60
|
+
await writeFile(configPath, JSON.stringify(config, null, 2) + '\n', 'utf8');
|
|
61
|
+
await writeEnvFile(envPath, collectedKeys);
|
|
62
|
+
try {
|
|
63
|
+
await chmod(envPath, 0o600);
|
|
64
|
+
}
|
|
65
|
+
catch { /* non-POSIX fs */ }
|
|
66
|
+
print(style(defaultTheme.success, '\nSetup complete.'));
|
|
67
|
+
print('\nTo load keys into your shell, add to ~/.bashrc or ~/.zshrc:');
|
|
68
|
+
print(` ${style(defaultTheme.muted, `set -a && source ${envPath} && set +a`)}`);
|
|
69
|
+
print(`Or run dirgha from a shell that already has those env vars exported.`);
|
|
70
|
+
print(`\nDefault model: ${style(defaultTheme.accent, defaultModel)} (change with /model inside the REPL or --model on the command line)`);
|
|
71
|
+
}
|
|
72
|
+
function resolveModel(selection, suggestions, fallback) {
|
|
73
|
+
if (/^\d+$/.test(selection)) {
|
|
74
|
+
const idx = Number.parseInt(selection, 10) - 1;
|
|
75
|
+
if (idx >= 0 && idx < suggestions.length)
|
|
76
|
+
return suggestions[idx];
|
|
77
|
+
}
|
|
78
|
+
return selection || fallback;
|
|
79
|
+
}
|
|
80
|
+
async function readJson(path) {
|
|
81
|
+
const text = await readFile(path, 'utf8').catch(() => '');
|
|
82
|
+
if (!text)
|
|
83
|
+
return {};
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(text);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return {};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function readEnvFile(path) {
|
|
92
|
+
const text = await readFile(path, 'utf8').catch(() => '');
|
|
93
|
+
const out = {};
|
|
94
|
+
for (const line of text.split('\n')) {
|
|
95
|
+
const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/);
|
|
96
|
+
if (!m)
|
|
97
|
+
continue;
|
|
98
|
+
let value = m[2];
|
|
99
|
+
if (value.startsWith('"') && value.endsWith('"'))
|
|
100
|
+
value = value.slice(1, -1);
|
|
101
|
+
if (value.startsWith("'") && value.endsWith("'"))
|
|
102
|
+
value = value.slice(1, -1);
|
|
103
|
+
out[m[1]] = value;
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
async function writeEnvFile(path, values) {
|
|
108
|
+
await mkdir(dirname(path), { recursive: true });
|
|
109
|
+
const lines = Object.entries(values)
|
|
110
|
+
.filter(([, value]) => value)
|
|
111
|
+
.map(([key, value]) => `export ${key}=${quoteValue(value)}`);
|
|
112
|
+
await writeFile(path, lines.join('\n') + '\n', 'utf8');
|
|
113
|
+
}
|
|
114
|
+
function quoteValue(value) {
|
|
115
|
+
if (/^[A-Za-z0-9_\-./:]+$/.test(value))
|
|
116
|
+
return value;
|
|
117
|
+
return `"${value.replace(/"/g, '\\"')}"`;
|
|
118
|
+
}
|
|
119
|
+
function maskIfSecret(value) {
|
|
120
|
+
if (value.length < 10)
|
|
121
|
+
return value;
|
|
122
|
+
return `${value.slice(0, 4)}…${value.slice(-4)}`;
|
|
123
|
+
}
|
|
124
|
+
function print(line) {
|
|
125
|
+
process.stdout.write(line + '\n');
|
|
126
|
+
}
|
|
127
|
+
void stat;
|
|
128
|
+
//# sourceMappingURL=setup.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /account — billing + entitlements snapshot.
|
|
3
|
+
*
|
|
4
|
+
* Requires an active token (loaded from `credentials.json` at REPL
|
|
5
|
+
* start). Calls `/api/billing/account` for tier + balance + limits and
|
|
6
|
+
* `/api/billing/entitlements` (via `checkEntitlement`) to show whether
|
|
7
|
+
* Fleet is unlocked.
|
|
8
|
+
*/
|
|
9
|
+
import type { SlashCommand } from './types.js';
|
|
10
|
+
export declare const accountCommand: SlashCommand;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /account — billing + entitlements snapshot.
|
|
3
|
+
*
|
|
4
|
+
* Requires an active token (loaded from `credentials.json` at REPL
|
|
5
|
+
* start). Calls `/api/billing/account` for tier + balance + limits and
|
|
6
|
+
* `/api/billing/entitlements` (via `checkEntitlement`) to show whether
|
|
7
|
+
* Fleet is unlocked.
|
|
8
|
+
*/
|
|
9
|
+
import { getAccountStatus } from '../../integrations/billing.js';
|
|
10
|
+
import { checkEntitlement } from '../../integrations/entitlements.js';
|
|
11
|
+
import { loadToken } from '../../integrations/device-auth.js';
|
|
12
|
+
function fmtUsd(n) {
|
|
13
|
+
return `$${n.toFixed(2)}`;
|
|
14
|
+
}
|
|
15
|
+
function fmtTokens(used, limit) {
|
|
16
|
+
if (!limit)
|
|
17
|
+
return `${used.toLocaleString()}`;
|
|
18
|
+
return `${used.toLocaleString()} / ${limit.toLocaleString()}`;
|
|
19
|
+
}
|
|
20
|
+
function render(status, fleet) {
|
|
21
|
+
return [
|
|
22
|
+
'Account:',
|
|
23
|
+
` user : ${status.email} (${status.userId})`,
|
|
24
|
+
` tier : ${status.tier}`,
|
|
25
|
+
` balance : ${fmtUsd(status.balanceUsd)}`,
|
|
26
|
+
` daily : ${fmtTokens(status.limits.dailyTokensUsed, status.limits.dailyTokens)} tokens`,
|
|
27
|
+
` monthly : ${fmtTokens(status.limits.monthlyTokensUsed, status.limits.monthlyTokens)} tokens`,
|
|
28
|
+
` resets at : ${status.resetAt || 'unknown'}`,
|
|
29
|
+
'',
|
|
30
|
+
` Fleet access: ${fleet ? '✓' : '✗'}`,
|
|
31
|
+
].join('\n');
|
|
32
|
+
}
|
|
33
|
+
export const accountCommand = {
|
|
34
|
+
name: 'account',
|
|
35
|
+
description: 'Show billing tier, balance, and limits',
|
|
36
|
+
async execute(_args, ctx) {
|
|
37
|
+
// Fall back to re-reading in case another REPL shell signed in.
|
|
38
|
+
let token = ctx.getToken();
|
|
39
|
+
if (!token) {
|
|
40
|
+
token = await loadToken();
|
|
41
|
+
if (token)
|
|
42
|
+
ctx.setToken(token);
|
|
43
|
+
}
|
|
44
|
+
if (!token) {
|
|
45
|
+
return 'Not signed in. Run /login first.';
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const [status, fleet] = await Promise.all([
|
|
49
|
+
getAccountStatus(token.token, ctx.apiBase()),
|
|
50
|
+
checkEntitlement(token.token, 'fleet', { baseUrl: ctx.apiBase() }),
|
|
51
|
+
]);
|
|
52
|
+
return render(status, fleet);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
return `Account lookup failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=account.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /clear — clear the in-memory transcript. Delegates to the core
|
|
3
|
+
* SlashContext.clear() which is already wired to drop the history
|
|
4
|
+
* array and print an acknowledgement. The on-disk session is kept so
|
|
5
|
+
* the conversation remains replayable later.
|
|
6
|
+
*/
|
|
7
|
+
import type { SlashCommand } from './types.js';
|
|
8
|
+
export declare const clearCommand: SlashCommand;
|