@knightcodeai/cli 0.17.4 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1303 -0
- package/README.md +2 -2
- package/dist/bundle/chunks/anthropic-messages-A2IH2IY3.js +6 -0
- package/dist/bundle/chunks/anthropic.js +101 -0
- package/dist/bundle/chunks/azure-openai-responses-XZILQZOG.js +2 -0
- package/dist/bundle/chunks/bedrock-converse-stream.js +89 -0
- package/dist/bundle/chunks/chunk-253N4337.js +7 -0
- package/dist/bundle/chunks/chunk-2QIKI63P.js +108 -0
- package/dist/bundle/chunks/chunk-3LX7U45C.js +130 -0
- package/dist/bundle/chunks/chunk-4CRI7BMY.js +2 -0
- package/dist/bundle/chunks/chunk-57BPR4HF.js +2 -0
- package/dist/bundle/chunks/chunk-A5LJINMT.js +2 -0
- package/dist/bundle/chunks/chunk-AANBXVWQ.js +2 -0
- package/dist/bundle/chunks/chunk-AHB7JV5B.js +2 -0
- package/dist/bundle/chunks/chunk-B2FT7RH5.js +2 -0
- package/dist/bundle/chunks/chunk-BPNRVFVQ.js +2 -0
- package/dist/bundle/chunks/chunk-BRKUE2MD.js +2 -0
- package/dist/bundle/chunks/chunk-BXZL5C3N.js +58 -0
- package/dist/bundle/chunks/chunk-DGACKL4S.js +30 -0
- package/dist/bundle/chunks/chunk-DKOURXGU.js +2 -0
- package/dist/bundle/chunks/chunk-DVFCXHT4.js +2 -0
- package/dist/bundle/chunks/chunk-ETOMYYZC.js +2 -0
- package/dist/bundle/chunks/chunk-FA32X2UJ.js +7 -0
- package/dist/bundle/chunks/chunk-FHM6KUY3.js +44 -0
- package/dist/bundle/chunks/chunk-FO7CEQB3.js +11 -0
- package/dist/bundle/chunks/chunk-FXS5MU6B.js +2 -0
- package/dist/bundle/chunks/chunk-HHWP56PR.js +2 -0
- package/dist/bundle/chunks/chunk-HSL72VZI.js +2 -0
- package/dist/bundle/chunks/chunk-HUQB436V.js +2 -0
- package/dist/bundle/chunks/chunk-HWXGL4YB.js +2 -0
- package/dist/bundle/chunks/chunk-KQKUEQJA.js +6 -0
- package/dist/bundle/chunks/chunk-LROWZ6UF.js +1333 -0
- package/dist/bundle/chunks/chunk-MOALMQWH.js +9 -0
- package/dist/bundle/chunks/chunk-MXIWLMNT.js +8 -0
- package/dist/bundle/chunks/chunk-N6VDWLND.js +2 -0
- package/dist/bundle/chunks/chunk-NCIBB66G.js +2 -0
- package/dist/bundle/chunks/chunk-NDQ5OW6U.js +2 -0
- package/dist/bundle/chunks/chunk-O6JSP6YG.js +6 -0
- package/dist/bundle/chunks/chunk-OFST7FDG.js +2 -0
- package/dist/bundle/chunks/chunk-SPAUY3RD.js +12 -0
- package/dist/bundle/chunks/chunk-UZWP3NHW.js +2 -0
- package/dist/bundle/chunks/chunk-V5ANFNKF.js +11 -0
- package/dist/bundle/chunks/chunk-VJJ6J35N.js +20 -0
- package/dist/bundle/chunks/chunk-ZW7FVRHT.js +31 -0
- package/dist/bundle/chunks/cli-DFJTSQI6.js +51 -0
- package/dist/bundle/chunks/cloudflare-workers-ai-system-one-6E2UM7IG.js +2 -0
- package/dist/bundle/chunks/codemode-worker.js +478 -0
- package/dist/bundle/chunks/dist-DKI7Q6GY.js +13 -0
- package/dist/bundle/chunks/easter-egg-3d-7O2AHLAQ.js +2 -0
- package/dist/bundle/chunks/execute-4CLOTQIS.js +499 -0
- package/dist/bundle/chunks/github-copilot.js +2 -0
- package/dist/bundle/chunks/google-generative-ai-CSWR3ORZ.js +2 -0
- package/dist/bundle/chunks/google-vertex-QIDRFOFB.js +2 -0
- package/dist/bundle/chunks/image-resize-worker.js +2 -0
- package/dist/bundle/chunks/jiti-loader-BXVX3Y2R.js +21 -0
- package/dist/bundle/chunks/jiti-static-loader-NNBCFAO5.js +2 -0
- package/dist/bundle/chunks/kimi-coding.js +2 -0
- package/dist/bundle/chunks/knightcode-messages-3GNOTMSI.js +8 -0
- package/dist/bundle/chunks/lib-IHWMWJDR.js +9 -0
- package/dist/bundle/chunks/llama-cpp-classify-HRPRKOZA.js +22 -0
- package/dist/bundle/chunks/meta.js +2 -0
- package/dist/bundle/chunks/mistral-conversations-46TK5OBA.js +5 -0
- package/dist/bundle/chunks/multipart-parser-VTTMF2EO.js +3 -0
- package/dist/bundle/chunks/node-7S3GFMOZ.js +16 -0
- package/dist/bundle/chunks/node-MAEICQNS.js +2 -0
- package/dist/bundle/chunks/openai-chatgpt.js +101 -0
- package/dist/bundle/chunks/openai-codex-responses-IT7VEKUU.js +10 -0
- package/dist/bundle/chunks/openai-codex.js +101 -0
- package/dist/bundle/chunks/openai-completions-LOKIW33V.js +7 -0
- package/dist/bundle/chunks/openai-decisions-4F3LWHMG.js +5 -0
- package/dist/bundle/chunks/openai-responses-OK36GJXP.js +3 -0
- package/dist/bundle/chunks/openrouter-images-ZI7LLZ4Y.js +2 -0
- package/dist/bundle/chunks/openrouter.js +101 -0
- package/dist/bundle/chunks/radius.js +101 -0
- package/dist/bundle/chunks/runtime-IN7ZPKFY.js +2 -0
- package/dist/bundle/chunks/src-ETRMT73D.js +4 -0
- package/dist/bundle/chunks/typesafe-system-one-J2CJBWEH.js +2 -0
- package/dist/bundle/chunks/virtual-modules-7VXI3TVN.js +2 -0
- package/dist/bundle/chunks/xai.js +2 -0
- package/dist/bundle/cli-runtime.js +3 -0
- package/dist/bundle/cli.js +16 -0
- package/dist/bundle/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
- package/dist/bundle/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
- package/dist/bundle/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
- package/dist/bundle/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
- package/dist/bundle/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
- package/dist/bundle/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
- package/dist/core/export-html/template.css +1015 -0
- package/dist/core/export-html/template.html +55 -0
- package/dist/core/export-html/template.js +1929 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +78 -0
- package/dist/modes/interactive/theme/dark.json +74 -0
- package/dist/modes/interactive/theme/light.json +74 -0
- package/dist/modes/interactive/theme/system-theme.ts +579 -0
- package/dist/modes/interactive/theme/theme-controller.ts +253 -0
- package/dist/modes/interactive/theme/theme-schema.ts +145 -0
- package/dist/modes/interactive/theme/theme-tokens.ts +145 -0
- package/dist/modes/interactive/theme/theme.ts +1076 -0
- package/docs/app-server.md +479 -0
- package/docs/app-server.schema.json +8128 -0
- package/docs/cli-integration.md +106 -0
- package/docs/cli.md +393 -0
- package/docs/codemode.md +230 -0
- package/docs/compaction.md +463 -0
- package/docs/configuration.md +48 -0
- package/docs/containerization.md +143 -0
- package/docs/custom-provider.md +225 -0
- package/docs/desktop.md +73 -0
- package/docs/docs.json +236 -0
- package/docs/environment-variables.md +138 -0
- package/docs/extensions.md +420 -0
- package/docs/how-knightcode-works.md +49 -0
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/index.md +39 -0
- package/docs/json.md +226 -0
- package/docs/keybindings.md +197 -0
- package/docs/llama-cpp.md +121 -0
- package/docs/mcp.md +272 -0
- package/docs/message-types.md +279 -0
- package/docs/models-configuration.md +187 -0
- package/docs/models.md +221 -0
- package/docs/nvidia-rtx-llama-cpp.md +96 -0
- package/docs/packages.md +129 -0
- package/docs/plan-mode.md +121 -0
- package/docs/prompt-templates.md +59 -0
- package/docs/providers.md +282 -0
- package/docs/quickstart.md +128 -0
- package/docs/remote.md +87 -0
- package/docs/rpc-commands.md +860 -0
- package/docs/rpc-extension-ui.md +214 -0
- package/docs/rpc.md +193 -0
- package/docs/sdk.md +197 -0
- package/docs/security.md +99 -0
- package/docs/session-format.md +358 -0
- package/docs/sessions.md +135 -0
- package/docs/settings.md +220 -0
- package/docs/shell-aliases.md +93 -0
- package/docs/skills.md +93 -0
- package/docs/slash-commands.md +78 -0
- package/docs/terminal-setup.md +242 -0
- package/docs/termux.md +117 -0
- package/docs/themes.md +140 -0
- package/docs/tmux.md +55 -0
- package/docs/tui.md +195 -0
- package/docs/usage.md +167 -0
- package/docs/virtual-models.md +114 -0
- package/docs/windows.md +67 -0
- package/examples/README.md +36 -0
- package/examples/extensions/README.md +214 -0
- package/examples/extensions/auto-commit-on-exit.ts +55 -0
- package/examples/extensions/bash-spawn-hook.ts +30 -0
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/border-status-editor.ts +151 -0
- package/examples/extensions/built-in-tool-renderer.ts +225 -0
- package/examples/extensions/claude-rules.ts +89 -0
- package/examples/extensions/commands.ts +72 -0
- package/examples/extensions/confirm-destructive.ts +60 -0
- package/examples/extensions/custom-compaction.ts +118 -0
- package/examples/extensions/custom-footer.ts +66 -0
- package/examples/extensions/custom-header.ts +73 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +617 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +399 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/debug-provider.ts +90 -0
- package/examples/extensions/dirty-repo-guard.ts +56 -0
- package/examples/extensions/doom-overlay/README.md +46 -0
- package/examples/extensions/doom-overlay/doom/build.sh +152 -0
- package/examples/extensions/doom-overlay/doom/doomgeneric_knightcode.c +72 -0
- package/examples/extensions/doom-overlay/doom-component.ts +132 -0
- package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
- package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
- package/examples/extensions/doom-overlay/index.ts +74 -0
- package/examples/extensions/doom-overlay/wad-finder.ts +55 -0
- package/examples/extensions/dynamic-resources/SKILL.md +8 -0
- package/examples/extensions/dynamic-resources/dynamic.json +75 -0
- package/examples/extensions/dynamic-resources/dynamic.md +5 -0
- package/examples/extensions/dynamic-resources/index.ts +15 -0
- package/examples/extensions/dynamic-tools.ts +74 -0
- package/examples/extensions/entry-renderer.ts +41 -0
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/file-trigger.ts +41 -0
- package/examples/extensions/git-checkpoint.ts +53 -0
- package/examples/extensions/git-merge-and-resolve.ts +115 -0
- package/examples/extensions/github-issue-autocomplete.ts +185 -0
- package/examples/extensions/gondolin/index.ts +531 -0
- package/examples/extensions/gondolin/package-lock.json +185 -0
- package/examples/extensions/gondolin/package.json +19 -0
- package/examples/extensions/handoff.ts +190 -0
- package/examples/extensions/hello.ts +26 -0
- package/examples/extensions/hidden-thinking-label.ts +53 -0
- package/examples/extensions/inline-bash.ts +94 -0
- package/examples/extensions/input-transform-streaming.ts +39 -0
- package/examples/extensions/input-transform.ts +43 -0
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/jev-router.ts +113 -0
- package/examples/extensions/mac-system-theme.ts +47 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/minimal-mode.ts +353 -0
- package/examples/extensions/modal-editor.ts +85 -0
- package/examples/extensions/model-status.ts +31 -0
- package/examples/extensions/notify.ts +57 -0
- package/examples/extensions/overlay-qa-tests.ts +1450 -0
- package/examples/extensions/overlay-test.ts +153 -0
- package/examples/extensions/permission-gate.ts +34 -0
- package/examples/extensions/pirate.ts +47 -0
- package/examples/extensions/plan-mode/README.md +66 -0
- package/examples/extensions/plan-mode/index.ts +387 -0
- package/examples/extensions/plan-mode/utils.ts +168 -0
- package/examples/extensions/preset.ts +436 -0
- package/examples/extensions/project-trust.ts +64 -0
- package/examples/extensions/prompt-customizer.ts +49 -0
- package/examples/extensions/protected-paths.ts +30 -0
- package/examples/extensions/provider-payload.ts +18 -0
- package/examples/extensions/qna.ts +118 -0
- package/examples/extensions/question.ts +278 -0
- package/examples/extensions/questionnaire.ts +440 -0
- package/examples/extensions/rainbow-editor.ts +88 -0
- package/examples/extensions/reload-runtime.ts +37 -0
- package/examples/extensions/rpc-demo.ts +118 -0
- package/examples/extensions/sandbox/index.ts +321 -0
- package/examples/extensions/sandbox/package-lock.json +92 -0
- package/examples/extensions/sandbox/package.json +19 -0
- package/examples/extensions/send-user-message.ts +97 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/shutdown-command.ts +63 -0
- package/examples/extensions/snake.ts +343 -0
- package/examples/extensions/space-invaders.ts +560 -0
- package/examples/extensions/ssh.ts +220 -0
- package/examples/extensions/status-line.ts +32 -0
- package/examples/extensions/structured-output.ts +65 -0
- package/examples/extensions/subagent/README.md +177 -0
- package/examples/extensions/subagent/agents/planner.md +37 -0
- package/examples/extensions/subagent/agents/reviewer.md +35 -0
- package/examples/extensions/subagent/agents/scout.md +50 -0
- package/examples/extensions/subagent/agents/worker.md +24 -0
- package/examples/extensions/subagent/agents.ts +157 -0
- package/examples/extensions/subagent/index.ts +1010 -0
- package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
- package/examples/extensions/subagent/prompts/implement.md +10 -0
- package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
- package/examples/extensions/summarize.ts +199 -0
- package/examples/extensions/system-prompt-header.ts +17 -0
- package/examples/extensions/tic-tac-toe.ts +1004 -0
- package/examples/extensions/timed-confirm.ts +70 -0
- package/examples/extensions/titlebar-spinner.ts +58 -0
- package/examples/extensions/todo.ts +294 -0
- package/examples/extensions/tool-override.ts +144 -0
- package/examples/extensions/tools.ts +146 -0
- package/examples/extensions/trigger-compact.ts +50 -0
- package/examples/extensions/truncated-tool.ts +195 -0
- package/examples/extensions/widget-placement.ts +9 -0
- package/examples/extensions/with-deps/index.ts +32 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +22 -0
- package/examples/extensions/working-indicator.ts +121 -0
- package/examples/extensions/working-message-test.ts +25 -0
- package/examples/rpc-client.ts +35 -0
- package/examples/rpc-extension-ui.ts +661 -0
- package/examples/sdk/01-minimal.ts +26 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +70 -0
- package/examples/sdk/04-skills.ts +55 -0
- package/examples/sdk/05-tools.ts +48 -0
- package/examples/sdk/06-extensions.ts +94 -0
- package/examples/sdk/07-context-files.ts +42 -0
- package/examples/sdk/08-prompt-templates.ts +51 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +34 -0
- package/examples/sdk/10-settings.ts +53 -0
- package/examples/sdk/11-sessions.ts +52 -0
- package/examples/sdk/12-full-control.ts +74 -0
- package/examples/sdk/13-session-runtime.ts +67 -0
- package/examples/sdk/14-codemode-mcp.ts +61 -0
- package/examples/sdk/README.md +141 -0
- package/package.json +15 -15
- package/bin/knightcode +0 -62
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive Shell Commands Extension
|
|
3
|
+
*
|
|
4
|
+
* Enables running interactive commands (vim, git rebase -i, htop, etc.)
|
|
5
|
+
* with full terminal access. The TUI suspends while they run.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* knightcode -e examples/extensions/interactive-shell.ts
|
|
9
|
+
*
|
|
10
|
+
* !vim file.txt # Auto-detected as interactive
|
|
11
|
+
* !i any-command # Force interactive mode with !i prefix
|
|
12
|
+
* !git rebase -i HEAD~3
|
|
13
|
+
* !htop
|
|
14
|
+
*
|
|
15
|
+
* Configuration via environment variables:
|
|
16
|
+
* INTERACTIVE_COMMANDS - Additional commands (comma-separated)
|
|
17
|
+
* INTERACTIVE_EXCLUDE - Commands to exclude (comma-separated)
|
|
18
|
+
*
|
|
19
|
+
* Note: This only intercepts user `!` commands, not agent bash tool calls.
|
|
20
|
+
* If the agent runs an interactive command, it will fail (which is fine).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { spawnSync } from "node:child_process";
|
|
24
|
+
import type { ExtensionAPI } from "@knightcodeai/cli";
|
|
25
|
+
|
|
26
|
+
// Default interactive commands - editors, pagers, git ops, TUIs
|
|
27
|
+
const DEFAULT_INTERACTIVE_COMMANDS = [
|
|
28
|
+
// Editors
|
|
29
|
+
"vim",
|
|
30
|
+
"nvim",
|
|
31
|
+
"vi",
|
|
32
|
+
"nano",
|
|
33
|
+
"emacs",
|
|
34
|
+
"pico",
|
|
35
|
+
"micro",
|
|
36
|
+
"helix",
|
|
37
|
+
"hx",
|
|
38
|
+
"kak",
|
|
39
|
+
// Pagers
|
|
40
|
+
"less",
|
|
41
|
+
"more",
|
|
42
|
+
"most",
|
|
43
|
+
// Git interactive
|
|
44
|
+
"git commit",
|
|
45
|
+
"git rebase",
|
|
46
|
+
"git merge",
|
|
47
|
+
"git cherry-pick",
|
|
48
|
+
"git revert",
|
|
49
|
+
"git add -p",
|
|
50
|
+
"git add --patch",
|
|
51
|
+
"git add -i",
|
|
52
|
+
"git add --interactive",
|
|
53
|
+
"git stash -p",
|
|
54
|
+
"git stash --patch",
|
|
55
|
+
"git reset -p",
|
|
56
|
+
"git reset --patch",
|
|
57
|
+
"git checkout -p",
|
|
58
|
+
"git checkout --patch",
|
|
59
|
+
"git difftool",
|
|
60
|
+
"git mergetool",
|
|
61
|
+
// System monitors
|
|
62
|
+
"htop",
|
|
63
|
+
"top",
|
|
64
|
+
"btop",
|
|
65
|
+
"glances",
|
|
66
|
+
// File managers
|
|
67
|
+
"ranger",
|
|
68
|
+
"nnn",
|
|
69
|
+
"lf",
|
|
70
|
+
"mc",
|
|
71
|
+
"vifm",
|
|
72
|
+
// Git TUIs
|
|
73
|
+
"tig",
|
|
74
|
+
"lazygit",
|
|
75
|
+
"gitui",
|
|
76
|
+
// Fuzzy finders
|
|
77
|
+
"fzf",
|
|
78
|
+
"sk",
|
|
79
|
+
// Remote sessions
|
|
80
|
+
"ssh",
|
|
81
|
+
"telnet",
|
|
82
|
+
"mosh",
|
|
83
|
+
// Database clients
|
|
84
|
+
"psql",
|
|
85
|
+
"mysql",
|
|
86
|
+
"sqlite3",
|
|
87
|
+
"mongosh",
|
|
88
|
+
"redis-cli",
|
|
89
|
+
// Kubernetes/Docker
|
|
90
|
+
"kubectl edit",
|
|
91
|
+
"kubectl exec -it",
|
|
92
|
+
"docker exec -it",
|
|
93
|
+
"docker run -it",
|
|
94
|
+
// Other
|
|
95
|
+
"tmux",
|
|
96
|
+
"screen",
|
|
97
|
+
"ncdu",
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
function getInteractiveCommands(): string[] {
|
|
101
|
+
const additional =
|
|
102
|
+
process.env.INTERACTIVE_COMMANDS?.split(",")
|
|
103
|
+
.map((s) => s.trim())
|
|
104
|
+
.filter(Boolean) ?? [];
|
|
105
|
+
const excluded = new Set(process.env.INTERACTIVE_EXCLUDE?.split(",").map((s) => s.trim().toLowerCase()) ?? []);
|
|
106
|
+
return [...DEFAULT_INTERACTIVE_COMMANDS, ...additional].filter((cmd) => !excluded.has(cmd.toLowerCase()));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isInteractiveCommand(command: string): boolean {
|
|
110
|
+
const trimmed = command.trim().toLowerCase();
|
|
111
|
+
const commands = getInteractiveCommands();
|
|
112
|
+
|
|
113
|
+
for (const cmd of commands) {
|
|
114
|
+
const cmdLower = cmd.toLowerCase();
|
|
115
|
+
// Match at start
|
|
116
|
+
if (trimmed === cmdLower || trimmed.startsWith(`${cmdLower} `) || trimmed.startsWith(`${cmdLower}\t`)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
// Match after pipe: "cat file | less"
|
|
120
|
+
const pipeIdx = trimmed.lastIndexOf("|");
|
|
121
|
+
if (pipeIdx !== -1) {
|
|
122
|
+
const afterPipe = trimmed.slice(pipeIdx + 1).trim();
|
|
123
|
+
if (afterPipe === cmdLower || afterPipe.startsWith(`${cmdLower} `)) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default function (knightcode: ExtensionAPI) {
|
|
132
|
+
knightcode.on("user_bash", async (event, ctx) => {
|
|
133
|
+
let command = event.command;
|
|
134
|
+
let forceInteractive = false;
|
|
135
|
+
|
|
136
|
+
// Check for !i prefix (command comes without the leading !)
|
|
137
|
+
// The prefix parsing happens before this event, so we check if command starts with "i "
|
|
138
|
+
if (command.startsWith("i ") || command.startsWith("i\t")) {
|
|
139
|
+
forceInteractive = true;
|
|
140
|
+
command = command.slice(2).trim();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const shouldBeInteractive = forceInteractive || isInteractiveCommand(command);
|
|
144
|
+
if (!shouldBeInteractive) {
|
|
145
|
+
return; // Let normal handling proceed
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// No UI available (print mode, RPC, etc.)
|
|
149
|
+
if (ctx.mode !== "tui") {
|
|
150
|
+
return {
|
|
151
|
+
result: { output: "(interactive commands require TUI)", exitCode: 1, cancelled: false, truncated: false },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Use ctx.ui.custom() to get TUI access, then run the command
|
|
156
|
+
const exitCode = await ctx.ui.custom<number | null>((tui, _theme, _kb, done) => {
|
|
157
|
+
// Stop TUI to release terminal
|
|
158
|
+
tui.stop();
|
|
159
|
+
|
|
160
|
+
// Clear screen
|
|
161
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
162
|
+
|
|
163
|
+
// Run command with full terminal access
|
|
164
|
+
const shell = process.env.SHELL || "/bin/sh";
|
|
165
|
+
const result = spawnSync(shell, ["-c", command], {
|
|
166
|
+
stdio: "inherit",
|
|
167
|
+
env: process.env,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Restart TUI
|
|
171
|
+
tui.start();
|
|
172
|
+
tui.requestRender(true);
|
|
173
|
+
|
|
174
|
+
// Signal completion
|
|
175
|
+
done(result.status);
|
|
176
|
+
|
|
177
|
+
// Return empty component (immediately disposed since done() was called)
|
|
178
|
+
return { render: () => [], invalidate: () => {} };
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Return result to prevent default bash handling
|
|
182
|
+
const output =
|
|
183
|
+
exitCode === 0
|
|
184
|
+
? "(interactive command completed successfully)"
|
|
185
|
+
: `(interactive command exited with code ${exitCode})`;
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
result: {
|
|
189
|
+
output,
|
|
190
|
+
exitCode: exitCode ?? 1,
|
|
191
|
+
cancelled: false,
|
|
192
|
+
truncated: false,
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Jev router - a virtual model that plans on a strong model and implements on a cheap one.
|
|
3
|
+
*
|
|
4
|
+
* Registers `jev/auto`, which routes between three OpenAI Codex models:
|
|
5
|
+
*
|
|
6
|
+
* - Planning: GPT-5.6 Sol for complex work, GPT-5.6 Terra otherwise. The Jev classifier rates the
|
|
7
|
+
* first user message; planning stays on the chosen model.
|
|
8
|
+
* - Implementation: GPT-5.6 Luna.
|
|
9
|
+
*
|
|
10
|
+
* The planning model explores, plans, and makes the first edit. After the first successful `edit`
|
|
11
|
+
* or `write` tool call, the next request of the same turn goes to Luna, and the session stays
|
|
12
|
+
* there. A session therefore switches models once and accepts a single prompt-cache miss.
|
|
13
|
+
*
|
|
14
|
+
* The phase is router state: KnightCode stores it on the session branch, so it follows the session tree
|
|
15
|
+
* and survives compaction. The selected thinking level passes through as the reasoning effort of
|
|
16
|
+
* the chosen model. Requests outside the agent loop, such as compaction summaries, go to Luna.
|
|
17
|
+
*
|
|
18
|
+
* Requires TypeSafe credentials (TYPESAFE_API_KEY) and an OpenAI Codex login.
|
|
19
|
+
* Usage: KnightCode -e ./jev-router.ts --model jev/auto
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { Message } from "@knightcode/ai";
|
|
23
|
+
import type { ExtensionAPI, ExtensionContext, ModelRoute, ModelRouteRequest } from "@knightcodeai/cli";
|
|
24
|
+
|
|
25
|
+
const PROVIDER = "openai-codex";
|
|
26
|
+
const SOL = "gpt-5.6-sol";
|
|
27
|
+
const TERRA = "gpt-5.6-terra";
|
|
28
|
+
const LUNA = "gpt-5.6-luna";
|
|
29
|
+
|
|
30
|
+
/** Tools whose successful result means implementation has started. */
|
|
31
|
+
const EDIT_TOOLS = new Set(["edit", "write"]);
|
|
32
|
+
|
|
33
|
+
interface JevState {
|
|
34
|
+
phase: "planning" | "implementation";
|
|
35
|
+
/** OpenAI Codex model for this phase. */
|
|
36
|
+
model: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type JevRequest = ModelRouteRequest<JevState>;
|
|
40
|
+
|
|
41
|
+
function routeTo(request: JevRequest, ctx: ExtensionContext, id: string, state?: JevState): ModelRoute<JevState> {
|
|
42
|
+
const model = ctx.modelRegistry.find(PROVIDER, id);
|
|
43
|
+
if (!model) throw new Error(`Model ${PROVIDER}/${id} is not in the catalog`);
|
|
44
|
+
return { model, thinkingLevel: request.thinkingLevel, state };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function lastUserText(messages: readonly Message[]): string {
|
|
48
|
+
const content = messages.filter((message) => message.role === "user").at(-1)?.content ?? "";
|
|
49
|
+
if (typeof content === "string") return content;
|
|
50
|
+
return content.flatMap((block) => (block.type === "text" ? [block.text] : [])).join("\n");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether a tool call since the last user message edited a file successfully. */
|
|
54
|
+
function editedThisTurn(messages: readonly Message[]): boolean {
|
|
55
|
+
const lastUser = messages.findLastIndex((message) => message.role === "user");
|
|
56
|
+
return messages
|
|
57
|
+
.slice(lastUser + 1)
|
|
58
|
+
.some((message) => message.role === "toolResult" && EDIT_TOOLS.has(message.toolName) && !message.isError);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Planning model for a new session: Sol for complex work, Terra otherwise or when Jev is unavailable. */
|
|
62
|
+
async function choosePlanningModel(request: JevRequest, ctx: ExtensionContext): Promise<string> {
|
|
63
|
+
// Keep a planning model the session already uses, so switching to jev/auto costs no cache miss.
|
|
64
|
+
const previous = request.previous?.model;
|
|
65
|
+
if (previous?.provider === PROVIDER && (previous.id === SOL || previous.id === TERRA)) return previous.id;
|
|
66
|
+
|
|
67
|
+
const jev = ctx.modelRegistry.findOfType("classifier", "typesafe", "jev-latest");
|
|
68
|
+
if (!jev) return TERRA;
|
|
69
|
+
const result = await ctx.modelRegistry.classify(
|
|
70
|
+
jev,
|
|
71
|
+
{
|
|
72
|
+
state: { prompt: lastUserText(request.messages).slice(0, 16_000) },
|
|
73
|
+
questions: {
|
|
74
|
+
complexity: {
|
|
75
|
+
type: "choice",
|
|
76
|
+
instructions: "How demanding is the software engineering work requested in `prompt`?",
|
|
77
|
+
criteria: {
|
|
78
|
+
standard: "Ordinary features, fixes, reviews, or questions",
|
|
79
|
+
complex: "Subtle design, cross-cutting changes, or hard debugging",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{ signal: request.signal },
|
|
85
|
+
);
|
|
86
|
+
const answer = result.stopReason === "stop" ? result.answers.complexity : undefined;
|
|
87
|
+
return answer?.type === "choice" && (answer.probabilities.complex ?? 0) >= 0.5 ? SOL : TERRA;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default function (knightcode: ExtensionAPI) {
|
|
91
|
+
knightcode.registerVirtualModel<JevState>({
|
|
92
|
+
provider: "jev",
|
|
93
|
+
id: "auto",
|
|
94
|
+
name: "Auto (Jev)",
|
|
95
|
+
thinkingLevels: ["low", "medium", "high", "xhigh"],
|
|
96
|
+
// Shared by all three models; shown before the first response.
|
|
97
|
+
contextWindow: 272_000,
|
|
98
|
+
maxTokens: 128_000,
|
|
99
|
+
async route(request, ctx) {
|
|
100
|
+
if (request.reason === "direct") return routeTo(request, ctx, LUNA);
|
|
101
|
+
const state = request.state;
|
|
102
|
+
if (!state) {
|
|
103
|
+
const model = await choosePlanningModel(request, ctx);
|
|
104
|
+
return routeTo(request, ctx, model, { phase: "planning", model });
|
|
105
|
+
}
|
|
106
|
+
// The planning model made the first edit: hand the rest of the work to Luna.
|
|
107
|
+
if (state.phase === "planning" && editedThisTurn(request.messages)) {
|
|
108
|
+
return routeTo(request, ctx, LUNA, { phase: "implementation", model: LUNA });
|
|
109
|
+
}
|
|
110
|
+
return routeTo(request, ctx, state.model);
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syncs knightcode theme with macOS system appearance (dark/light mode).
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* knightcode -e examples/extensions/mac-system-theme.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { exec } from "node:child_process";
|
|
9
|
+
import { promisify } from "node:util";
|
|
10
|
+
import type { ExtensionAPI } from "@knightcodeai/cli";
|
|
11
|
+
|
|
12
|
+
const execAsync = promisify(exec);
|
|
13
|
+
|
|
14
|
+
async function isDarkMode(): Promise<boolean> {
|
|
15
|
+
try {
|
|
16
|
+
const { stdout } = await execAsync(
|
|
17
|
+
"osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'",
|
|
18
|
+
);
|
|
19
|
+
return stdout.trim() === "true";
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function (knightcode: ExtensionAPI) {
|
|
26
|
+
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
27
|
+
|
|
28
|
+
knightcode.on("session_start", async (_event, ctx) => {
|
|
29
|
+
let currentTheme = (await isDarkMode()) ? "dark" : "light";
|
|
30
|
+
ctx.ui.setTheme(currentTheme);
|
|
31
|
+
|
|
32
|
+
intervalId = setInterval(async () => {
|
|
33
|
+
const newTheme = (await isDarkMode()) ? "dark" : "light";
|
|
34
|
+
if (newTheme !== currentTheme) {
|
|
35
|
+
currentTheme = newTheme;
|
|
36
|
+
ctx.ui.setTheme(currentTheme);
|
|
37
|
+
}
|
|
38
|
+
}, 2000);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
knightcode.on("session_shutdown", () => {
|
|
42
|
+
if (intervalId) {
|
|
43
|
+
clearInterval(intervalId);
|
|
44
|
+
intervalId = null;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom message rendering example.
|
|
3
|
+
*
|
|
4
|
+
* Shows how to use registerMessageRenderer to control how custom messages
|
|
5
|
+
* appear in the TUI, with colors, formatting, and expandable details.
|
|
6
|
+
*
|
|
7
|
+
* Usage: /status [message] - sends a status message with custom rendering
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI } from "@knightcodeai/cli";
|
|
11
|
+
import { Box, Text } from "@knightcode/tui";
|
|
12
|
+
|
|
13
|
+
export default function (knightcode: ExtensionAPI) {
|
|
14
|
+
// Register custom renderer for "status-update" messages
|
|
15
|
+
knightcode.registerMessageRenderer("status-update", (message, { expanded, outputPad }, theme) => {
|
|
16
|
+
const details = message.details as { level: string; timestamp: number } | undefined;
|
|
17
|
+
const level = details?.level ?? "info";
|
|
18
|
+
|
|
19
|
+
// Color based on level
|
|
20
|
+
const color = level === "error" ? "error" : level === "warn" ? "warning" : "success";
|
|
21
|
+
const prefix = theme.fg(color, `[${level.toUpperCase()}]`);
|
|
22
|
+
|
|
23
|
+
let text = `${prefix} ${message.content}`;
|
|
24
|
+
|
|
25
|
+
// Show timestamp when expanded
|
|
26
|
+
if (expanded && details?.timestamp) {
|
|
27
|
+
const time = new Date(details.timestamp).toLocaleTimeString();
|
|
28
|
+
text += `\n${theme.fg("dim", ` at ${time}`)}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Use Box with customMessageBg for consistent styling
|
|
32
|
+
const box = new Box(outputPad, 1, (t) => theme.bg("customMessageBg", t));
|
|
33
|
+
box.addChild(new Text(text, 0, 0));
|
|
34
|
+
return box;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Command to send status messages
|
|
38
|
+
knightcode.registerCommand("status", {
|
|
39
|
+
description: "Send a status message (usage: /status [warn|error] message)",
|
|
40
|
+
handler: async (args, _ctx) => {
|
|
41
|
+
const parts = args.trim().split(/\s+/);
|
|
42
|
+
let level = "info";
|
|
43
|
+
let content = args.trim();
|
|
44
|
+
|
|
45
|
+
// Check for level prefix
|
|
46
|
+
if (parts[0] === "warn" || parts[0] === "error") {
|
|
47
|
+
level = parts[0];
|
|
48
|
+
content = parts.slice(1).join(" ") || "Status update";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
knightcode.sendMessage({
|
|
52
|
+
customType: "status-update",
|
|
53
|
+
content,
|
|
54
|
+
display: true,
|
|
55
|
+
details: { level, timestamp: Date.now() },
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|