@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.
Files changed (281) hide show
  1. package/CHANGELOG.md +1303 -0
  2. package/README.md +2 -2
  3. package/dist/bundle/chunks/anthropic-messages-A2IH2IY3.js +6 -0
  4. package/dist/bundle/chunks/anthropic.js +101 -0
  5. package/dist/bundle/chunks/azure-openai-responses-XZILQZOG.js +2 -0
  6. package/dist/bundle/chunks/bedrock-converse-stream.js +89 -0
  7. package/dist/bundle/chunks/chunk-253N4337.js +7 -0
  8. package/dist/bundle/chunks/chunk-2QIKI63P.js +108 -0
  9. package/dist/bundle/chunks/chunk-3LX7U45C.js +130 -0
  10. package/dist/bundle/chunks/chunk-4CRI7BMY.js +2 -0
  11. package/dist/bundle/chunks/chunk-57BPR4HF.js +2 -0
  12. package/dist/bundle/chunks/chunk-A5LJINMT.js +2 -0
  13. package/dist/bundle/chunks/chunk-AANBXVWQ.js +2 -0
  14. package/dist/bundle/chunks/chunk-AHB7JV5B.js +2 -0
  15. package/dist/bundle/chunks/chunk-B2FT7RH5.js +2 -0
  16. package/dist/bundle/chunks/chunk-BPNRVFVQ.js +2 -0
  17. package/dist/bundle/chunks/chunk-BRKUE2MD.js +2 -0
  18. package/dist/bundle/chunks/chunk-BXZL5C3N.js +58 -0
  19. package/dist/bundle/chunks/chunk-DGACKL4S.js +30 -0
  20. package/dist/bundle/chunks/chunk-DKOURXGU.js +2 -0
  21. package/dist/bundle/chunks/chunk-DVFCXHT4.js +2 -0
  22. package/dist/bundle/chunks/chunk-ETOMYYZC.js +2 -0
  23. package/dist/bundle/chunks/chunk-FA32X2UJ.js +7 -0
  24. package/dist/bundle/chunks/chunk-FHM6KUY3.js +44 -0
  25. package/dist/bundle/chunks/chunk-FO7CEQB3.js +11 -0
  26. package/dist/bundle/chunks/chunk-FXS5MU6B.js +2 -0
  27. package/dist/bundle/chunks/chunk-HHWP56PR.js +2 -0
  28. package/dist/bundle/chunks/chunk-HSL72VZI.js +2 -0
  29. package/dist/bundle/chunks/chunk-HUQB436V.js +2 -0
  30. package/dist/bundle/chunks/chunk-HWXGL4YB.js +2 -0
  31. package/dist/bundle/chunks/chunk-KQKUEQJA.js +6 -0
  32. package/dist/bundle/chunks/chunk-LROWZ6UF.js +1333 -0
  33. package/dist/bundle/chunks/chunk-MOALMQWH.js +9 -0
  34. package/dist/bundle/chunks/chunk-MXIWLMNT.js +8 -0
  35. package/dist/bundle/chunks/chunk-N6VDWLND.js +2 -0
  36. package/dist/bundle/chunks/chunk-NCIBB66G.js +2 -0
  37. package/dist/bundle/chunks/chunk-NDQ5OW6U.js +2 -0
  38. package/dist/bundle/chunks/chunk-O6JSP6YG.js +6 -0
  39. package/dist/bundle/chunks/chunk-OFST7FDG.js +2 -0
  40. package/dist/bundle/chunks/chunk-SPAUY3RD.js +12 -0
  41. package/dist/bundle/chunks/chunk-UZWP3NHW.js +2 -0
  42. package/dist/bundle/chunks/chunk-V5ANFNKF.js +11 -0
  43. package/dist/bundle/chunks/chunk-VJJ6J35N.js +20 -0
  44. package/dist/bundle/chunks/chunk-ZW7FVRHT.js +31 -0
  45. package/dist/bundle/chunks/cli-DFJTSQI6.js +51 -0
  46. package/dist/bundle/chunks/cloudflare-workers-ai-system-one-6E2UM7IG.js +2 -0
  47. package/dist/bundle/chunks/codemode-worker.js +478 -0
  48. package/dist/bundle/chunks/dist-DKI7Q6GY.js +13 -0
  49. package/dist/bundle/chunks/easter-egg-3d-7O2AHLAQ.js +2 -0
  50. package/dist/bundle/chunks/execute-4CLOTQIS.js +499 -0
  51. package/dist/bundle/chunks/github-copilot.js +2 -0
  52. package/dist/bundle/chunks/google-generative-ai-CSWR3ORZ.js +2 -0
  53. package/dist/bundle/chunks/google-vertex-QIDRFOFB.js +2 -0
  54. package/dist/bundle/chunks/image-resize-worker.js +2 -0
  55. package/dist/bundle/chunks/jiti-loader-BXVX3Y2R.js +21 -0
  56. package/dist/bundle/chunks/jiti-static-loader-NNBCFAO5.js +2 -0
  57. package/dist/bundle/chunks/kimi-coding.js +2 -0
  58. package/dist/bundle/chunks/knightcode-messages-3GNOTMSI.js +8 -0
  59. package/dist/bundle/chunks/lib-IHWMWJDR.js +9 -0
  60. package/dist/bundle/chunks/llama-cpp-classify-HRPRKOZA.js +22 -0
  61. package/dist/bundle/chunks/meta.js +2 -0
  62. package/dist/bundle/chunks/mistral-conversations-46TK5OBA.js +5 -0
  63. package/dist/bundle/chunks/multipart-parser-VTTMF2EO.js +3 -0
  64. package/dist/bundle/chunks/node-7S3GFMOZ.js +16 -0
  65. package/dist/bundle/chunks/node-MAEICQNS.js +2 -0
  66. package/dist/bundle/chunks/openai-chatgpt.js +101 -0
  67. package/dist/bundle/chunks/openai-codex-responses-IT7VEKUU.js +10 -0
  68. package/dist/bundle/chunks/openai-codex.js +101 -0
  69. package/dist/bundle/chunks/openai-completions-LOKIW33V.js +7 -0
  70. package/dist/bundle/chunks/openai-decisions-4F3LWHMG.js +5 -0
  71. package/dist/bundle/chunks/openai-responses-OK36GJXP.js +3 -0
  72. package/dist/bundle/chunks/openrouter-images-ZI7LLZ4Y.js +2 -0
  73. package/dist/bundle/chunks/openrouter.js +101 -0
  74. package/dist/bundle/chunks/radius.js +101 -0
  75. package/dist/bundle/chunks/runtime-IN7ZPKFY.js +2 -0
  76. package/dist/bundle/chunks/src-ETRMT73D.js +4 -0
  77. package/dist/bundle/chunks/typesafe-system-one-J2CJBWEH.js +2 -0
  78. package/dist/bundle/chunks/virtual-modules-7VXI3TVN.js +2 -0
  79. package/dist/bundle/chunks/xai.js +2 -0
  80. package/dist/bundle/cli-runtime.js +3 -0
  81. package/dist/bundle/cli.js +16 -0
  82. package/dist/bundle/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  83. package/dist/bundle/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  84. package/dist/bundle/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  85. package/dist/bundle/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  86. package/dist/bundle/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  87. package/dist/bundle/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  88. package/dist/core/export-html/template.css +1015 -0
  89. package/dist/core/export-html/template.html +55 -0
  90. package/dist/core/export-html/template.js +1929 -0
  91. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  92. package/dist/core/export-html/vendor/marked.min.js +78 -0
  93. package/dist/modes/interactive/theme/dark.json +74 -0
  94. package/dist/modes/interactive/theme/light.json +74 -0
  95. package/dist/modes/interactive/theme/system-theme.ts +579 -0
  96. package/dist/modes/interactive/theme/theme-controller.ts +253 -0
  97. package/dist/modes/interactive/theme/theme-schema.ts +145 -0
  98. package/dist/modes/interactive/theme/theme-tokens.ts +145 -0
  99. package/dist/modes/interactive/theme/theme.ts +1076 -0
  100. package/docs/app-server.md +479 -0
  101. package/docs/app-server.schema.json +8128 -0
  102. package/docs/cli-integration.md +106 -0
  103. package/docs/cli.md +393 -0
  104. package/docs/codemode.md +230 -0
  105. package/docs/compaction.md +463 -0
  106. package/docs/configuration.md +48 -0
  107. package/docs/containerization.md +143 -0
  108. package/docs/custom-provider.md +225 -0
  109. package/docs/desktop.md +73 -0
  110. package/docs/docs.json +236 -0
  111. package/docs/environment-variables.md +138 -0
  112. package/docs/extensions.md +420 -0
  113. package/docs/how-knightcode-works.md +49 -0
  114. package/docs/images/doom-extension.png +0 -0
  115. package/docs/images/interactive-mode.png +0 -0
  116. package/docs/images/tree-view.png +0 -0
  117. package/docs/index.md +39 -0
  118. package/docs/json.md +226 -0
  119. package/docs/keybindings.md +197 -0
  120. package/docs/llama-cpp.md +121 -0
  121. package/docs/mcp.md +272 -0
  122. package/docs/message-types.md +279 -0
  123. package/docs/models-configuration.md +187 -0
  124. package/docs/models.md +221 -0
  125. package/docs/nvidia-rtx-llama-cpp.md +96 -0
  126. package/docs/packages.md +129 -0
  127. package/docs/plan-mode.md +121 -0
  128. package/docs/prompt-templates.md +59 -0
  129. package/docs/providers.md +282 -0
  130. package/docs/quickstart.md +128 -0
  131. package/docs/remote.md +87 -0
  132. package/docs/rpc-commands.md +860 -0
  133. package/docs/rpc-extension-ui.md +214 -0
  134. package/docs/rpc.md +193 -0
  135. package/docs/sdk.md +197 -0
  136. package/docs/security.md +99 -0
  137. package/docs/session-format.md +358 -0
  138. package/docs/sessions.md +135 -0
  139. package/docs/settings.md +220 -0
  140. package/docs/shell-aliases.md +93 -0
  141. package/docs/skills.md +93 -0
  142. package/docs/slash-commands.md +78 -0
  143. package/docs/terminal-setup.md +242 -0
  144. package/docs/termux.md +117 -0
  145. package/docs/themes.md +140 -0
  146. package/docs/tmux.md +55 -0
  147. package/docs/tui.md +195 -0
  148. package/docs/usage.md +167 -0
  149. package/docs/virtual-models.md +114 -0
  150. package/docs/windows.md +67 -0
  151. package/examples/README.md +36 -0
  152. package/examples/extensions/README.md +214 -0
  153. package/examples/extensions/auto-commit-on-exit.ts +55 -0
  154. package/examples/extensions/bash-spawn-hook.ts +30 -0
  155. package/examples/extensions/bookmark.ts +50 -0
  156. package/examples/extensions/border-status-editor.ts +151 -0
  157. package/examples/extensions/built-in-tool-renderer.ts +225 -0
  158. package/examples/extensions/claude-rules.ts +89 -0
  159. package/examples/extensions/commands.ts +72 -0
  160. package/examples/extensions/confirm-destructive.ts +60 -0
  161. package/examples/extensions/custom-compaction.ts +118 -0
  162. package/examples/extensions/custom-footer.ts +66 -0
  163. package/examples/extensions/custom-header.ts +73 -0
  164. package/examples/extensions/custom-provider-anthropic/index.ts +617 -0
  165. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  166. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  167. package/examples/extensions/custom-provider-gitlab-duo/index.ts +399 -0
  168. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  169. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  170. package/examples/extensions/debug-provider.ts +90 -0
  171. package/examples/extensions/dirty-repo-guard.ts +56 -0
  172. package/examples/extensions/doom-overlay/README.md +46 -0
  173. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  174. package/examples/extensions/doom-overlay/doom/doomgeneric_knightcode.c +72 -0
  175. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  176. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  177. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  178. package/examples/extensions/doom-overlay/index.ts +74 -0
  179. package/examples/extensions/doom-overlay/wad-finder.ts +55 -0
  180. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  181. package/examples/extensions/dynamic-resources/dynamic.json +75 -0
  182. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  183. package/examples/extensions/dynamic-resources/index.ts +15 -0
  184. package/examples/extensions/dynamic-tools.ts +74 -0
  185. package/examples/extensions/entry-renderer.ts +41 -0
  186. package/examples/extensions/event-bus.ts +43 -0
  187. package/examples/extensions/file-trigger.ts +41 -0
  188. package/examples/extensions/git-checkpoint.ts +53 -0
  189. package/examples/extensions/git-merge-and-resolve.ts +115 -0
  190. package/examples/extensions/github-issue-autocomplete.ts +185 -0
  191. package/examples/extensions/gondolin/index.ts +531 -0
  192. package/examples/extensions/gondolin/package-lock.json +185 -0
  193. package/examples/extensions/gondolin/package.json +19 -0
  194. package/examples/extensions/handoff.ts +190 -0
  195. package/examples/extensions/hello.ts +26 -0
  196. package/examples/extensions/hidden-thinking-label.ts +53 -0
  197. package/examples/extensions/inline-bash.ts +94 -0
  198. package/examples/extensions/input-transform-streaming.ts +39 -0
  199. package/examples/extensions/input-transform.ts +43 -0
  200. package/examples/extensions/interactive-shell.ts +196 -0
  201. package/examples/extensions/jev-router.ts +113 -0
  202. package/examples/extensions/mac-system-theme.ts +47 -0
  203. package/examples/extensions/message-renderer.ts +59 -0
  204. package/examples/extensions/minimal-mode.ts +353 -0
  205. package/examples/extensions/modal-editor.ts +85 -0
  206. package/examples/extensions/model-status.ts +31 -0
  207. package/examples/extensions/notify.ts +57 -0
  208. package/examples/extensions/overlay-qa-tests.ts +1450 -0
  209. package/examples/extensions/overlay-test.ts +153 -0
  210. package/examples/extensions/permission-gate.ts +34 -0
  211. package/examples/extensions/pirate.ts +47 -0
  212. package/examples/extensions/plan-mode/README.md +66 -0
  213. package/examples/extensions/plan-mode/index.ts +387 -0
  214. package/examples/extensions/plan-mode/utils.ts +168 -0
  215. package/examples/extensions/preset.ts +436 -0
  216. package/examples/extensions/project-trust.ts +64 -0
  217. package/examples/extensions/prompt-customizer.ts +49 -0
  218. package/examples/extensions/protected-paths.ts +30 -0
  219. package/examples/extensions/provider-payload.ts +18 -0
  220. package/examples/extensions/qna.ts +118 -0
  221. package/examples/extensions/question.ts +278 -0
  222. package/examples/extensions/questionnaire.ts +440 -0
  223. package/examples/extensions/rainbow-editor.ts +88 -0
  224. package/examples/extensions/reload-runtime.ts +37 -0
  225. package/examples/extensions/rpc-demo.ts +118 -0
  226. package/examples/extensions/sandbox/index.ts +321 -0
  227. package/examples/extensions/sandbox/package-lock.json +92 -0
  228. package/examples/extensions/sandbox/package.json +19 -0
  229. package/examples/extensions/send-user-message.ts +97 -0
  230. package/examples/extensions/session-name.ts +27 -0
  231. package/examples/extensions/shutdown-command.ts +63 -0
  232. package/examples/extensions/snake.ts +343 -0
  233. package/examples/extensions/space-invaders.ts +560 -0
  234. package/examples/extensions/ssh.ts +220 -0
  235. package/examples/extensions/status-line.ts +32 -0
  236. package/examples/extensions/structured-output.ts +65 -0
  237. package/examples/extensions/subagent/README.md +177 -0
  238. package/examples/extensions/subagent/agents/planner.md +37 -0
  239. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  240. package/examples/extensions/subagent/agents/scout.md +50 -0
  241. package/examples/extensions/subagent/agents/worker.md +24 -0
  242. package/examples/extensions/subagent/agents.ts +157 -0
  243. package/examples/extensions/subagent/index.ts +1010 -0
  244. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  245. package/examples/extensions/subagent/prompts/implement.md +10 -0
  246. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  247. package/examples/extensions/summarize.ts +199 -0
  248. package/examples/extensions/system-prompt-header.ts +17 -0
  249. package/examples/extensions/tic-tac-toe.ts +1004 -0
  250. package/examples/extensions/timed-confirm.ts +70 -0
  251. package/examples/extensions/titlebar-spinner.ts +58 -0
  252. package/examples/extensions/todo.ts +294 -0
  253. package/examples/extensions/tool-override.ts +144 -0
  254. package/examples/extensions/tools.ts +146 -0
  255. package/examples/extensions/trigger-compact.ts +50 -0
  256. package/examples/extensions/truncated-tool.ts +195 -0
  257. package/examples/extensions/widget-placement.ts +9 -0
  258. package/examples/extensions/with-deps/index.ts +32 -0
  259. package/examples/extensions/with-deps/package-lock.json +31 -0
  260. package/examples/extensions/with-deps/package.json +22 -0
  261. package/examples/extensions/working-indicator.ts +121 -0
  262. package/examples/extensions/working-message-test.ts +25 -0
  263. package/examples/rpc-client.ts +35 -0
  264. package/examples/rpc-extension-ui.ts +661 -0
  265. package/examples/sdk/01-minimal.ts +26 -0
  266. package/examples/sdk/02-custom-model.ts +49 -0
  267. package/examples/sdk/03-custom-prompt.ts +70 -0
  268. package/examples/sdk/04-skills.ts +55 -0
  269. package/examples/sdk/05-tools.ts +48 -0
  270. package/examples/sdk/06-extensions.ts +94 -0
  271. package/examples/sdk/07-context-files.ts +42 -0
  272. package/examples/sdk/08-prompt-templates.ts +51 -0
  273. package/examples/sdk/09-api-keys-and-oauth.ts +34 -0
  274. package/examples/sdk/10-settings.ts +53 -0
  275. package/examples/sdk/11-sessions.ts +52 -0
  276. package/examples/sdk/12-full-control.ts +74 -0
  277. package/examples/sdk/13-session-runtime.ts +67 -0
  278. package/examples/sdk/14-codemode-mcp.ts +61 -0
  279. package/examples/sdk/README.md +141 -0
  280. package/package.json +15 -15
  281. package/bin/knightcode +0 -62
@@ -0,0 +1,353 @@
1
+ /**
2
+ * Minimal Mode Example - Demonstrates a "minimal" tool display mode
3
+ *
4
+ * This extension overrides built-in tools to provide custom rendering:
5
+ * - Collapsed mode: Only shows the tool call (command/path), no output
6
+ * - Expanded mode: Shows full output like the built-in renderers
7
+ *
8
+ * This demonstrates how a "minimal mode" could work, where ctrl+o cycles through:
9
+ * - Standard: Shows truncated output (current default)
10
+ * - Expanded: Shows full output (current expanded)
11
+ * - Minimal: Shows only tool call, no output (this extension's collapsed mode)
12
+ *
13
+ * Usage:
14
+ * knightcode -e ./minimal-mode.ts
15
+ *
16
+ * Then use ctrl+o to toggle between minimal (collapsed) and full (expanded) views.
17
+ */
18
+
19
+ import type { ExtensionAPI } from "@knightcodeai/cli";
20
+ import {
21
+ createBashToolDefinition,
22
+ createEditToolDefinition,
23
+ createFindToolDefinition,
24
+ createGrepToolDefinition,
25
+ createLsToolDefinition,
26
+ createReadToolDefinition,
27
+ createWriteToolDefinition,
28
+ formatReadLineRange,
29
+ } from "@knightcodeai/cli";
30
+ import { Text } from "@knightcode/tui";
31
+ import { homedir } from "os";
32
+
33
+ /**
34
+ * Shorten a path by replacing home directory with ~
35
+ */
36
+ function shortenPath(path: string): string {
37
+ const home = homedir();
38
+ if (path.startsWith(home)) {
39
+ return `~${path.slice(home.length)}`;
40
+ }
41
+ return path;
42
+ }
43
+
44
+ function createBuiltInTools(cwd: string) {
45
+ return {
46
+ read: createReadToolDefinition(cwd),
47
+ bash: createBashToolDefinition(cwd),
48
+ edit: createEditToolDefinition(cwd),
49
+ write: createWriteToolDefinition(cwd),
50
+ find: createFindToolDefinition(cwd),
51
+ grep: createGrepToolDefinition(cwd),
52
+ ls: createLsToolDefinition(cwd),
53
+ };
54
+ }
55
+
56
+ export default function (knightcode: ExtensionAPI) {
57
+ const tools = createBuiltInTools(process.cwd());
58
+
59
+ // =========================================================================
60
+ // Read Tool
61
+ // =========================================================================
62
+ knightcode.registerTool({
63
+ ...tools.read,
64
+
65
+ renderCall(args, theme, _context) {
66
+ const path = shortenPath(args.path || "");
67
+ let pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
68
+
69
+ const lineRange = formatReadLineRange(args.offset, args.limit);
70
+ if (lineRange) {
71
+ pathDisplay += theme.fg("warning", lineRange);
72
+ }
73
+
74
+ return new Text(`${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`, 0, 0);
75
+ },
76
+
77
+ renderResult(result, { expanded }, theme, _context) {
78
+ // Minimal mode: show nothing in collapsed state
79
+ if (!expanded) {
80
+ return new Text("", 0, 0);
81
+ }
82
+
83
+ // Expanded mode: show full output
84
+ const textContent = result.content.find((c) => c.type === "text");
85
+ if (!textContent || textContent.type !== "text") {
86
+ return new Text("", 0, 0);
87
+ }
88
+
89
+ const lines = textContent.text.split("\n");
90
+ const output = lines.map((line) => theme.fg("toolOutput", line)).join("\n");
91
+ return new Text(`\n${output}`, 0, 0);
92
+ },
93
+ });
94
+
95
+ // =========================================================================
96
+ // Bash Tool
97
+ // =========================================================================
98
+ knightcode.registerTool({
99
+ ...tools.bash,
100
+
101
+ renderCall(args, theme, _context) {
102
+ const command = args.command || "...";
103
+ const timeout = args.timeout as number | undefined;
104
+ const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
105
+
106
+ return new Text(theme.fg("toolTitle", theme.bold(`$ ${command}`)) + timeoutSuffix, 0, 0);
107
+ },
108
+
109
+ renderResult(result, { expanded }, theme, _context) {
110
+ // Minimal mode: show nothing in collapsed state
111
+ if (!expanded) {
112
+ return new Text("", 0, 0);
113
+ }
114
+
115
+ // Expanded mode: show full output
116
+ const textContent = result.content.find((c) => c.type === "text");
117
+ if (!textContent || textContent.type !== "text") {
118
+ return new Text("", 0, 0);
119
+ }
120
+
121
+ const output = textContent.text
122
+ .trim()
123
+ .split("\n")
124
+ .map((line) => theme.fg("toolOutput", line))
125
+ .join("\n");
126
+
127
+ if (!output) {
128
+ return new Text("", 0, 0);
129
+ }
130
+
131
+ return new Text(`\n${output}`, 0, 0);
132
+ },
133
+ });
134
+
135
+ // =========================================================================
136
+ // Write Tool
137
+ // =========================================================================
138
+ knightcode.registerTool({
139
+ ...tools.write,
140
+
141
+ renderCall(args, theme, _context) {
142
+ const path = shortenPath(args.path || "");
143
+ const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
144
+ const lineCount = args.content ? args.content.split("\n").length : 0;
145
+ const lineInfo = lineCount > 0 ? theme.fg("muted", ` (${lineCount} lines)`) : "";
146
+
147
+ return new Text(`${theme.fg("toolTitle", theme.bold("write"))} ${pathDisplay}${lineInfo}`, 0, 0);
148
+ },
149
+
150
+ renderResult(result, { expanded }, theme, _context) {
151
+ // Minimal mode: show nothing (file was written)
152
+ if (!expanded) {
153
+ return new Text("", 0, 0);
154
+ }
155
+
156
+ // Expanded mode: show error if any
157
+ if (result.content.some((c) => c.type === "text" && c.text)) {
158
+ const textContent = result.content.find((c) => c.type === "text");
159
+ if (textContent?.type === "text" && textContent.text) {
160
+ return new Text(`\n${theme.fg("error", textContent.text)}`, 0, 0);
161
+ }
162
+ }
163
+
164
+ return new Text("", 0, 0);
165
+ },
166
+ });
167
+
168
+ // =========================================================================
169
+ // Edit Tool
170
+ // =========================================================================
171
+ knightcode.registerTool({
172
+ ...tools.edit,
173
+ renderShell: "default",
174
+
175
+ renderCall(args, theme, _context) {
176
+ const path = shortenPath(args.path || "");
177
+ const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
178
+
179
+ return new Text(`${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`, 0, 0);
180
+ },
181
+
182
+ renderResult(result, { expanded }, theme, _context) {
183
+ // Minimal mode: show nothing in collapsed state
184
+ if (!expanded) {
185
+ return new Text("", 0, 0);
186
+ }
187
+
188
+ // Expanded mode: show diff or error
189
+ const textContent = result.content.find((c) => c.type === "text");
190
+ if (!textContent || textContent.type !== "text") {
191
+ return new Text("", 0, 0);
192
+ }
193
+
194
+ // For errors, show the error message
195
+ const text = textContent.text;
196
+ if (text.includes("Error") || text.includes("error")) {
197
+ return new Text(`\n${theme.fg("error", text)}`, 0, 0);
198
+ }
199
+
200
+ // Otherwise show the text (would be nice to show actual diff here)
201
+ return new Text(`\n${theme.fg("toolOutput", text)}`, 0, 0);
202
+ },
203
+ });
204
+
205
+ // =========================================================================
206
+ // Find Tool
207
+ // =========================================================================
208
+ knightcode.registerTool({
209
+ ...tools.find,
210
+
211
+ renderCall(args, theme, _context) {
212
+ const pattern = args.pattern || "";
213
+ const path = shortenPath(args.path || ".");
214
+ const limit = args.limit;
215
+
216
+ let text = `${theme.fg("toolTitle", theme.bold("find"))} ${theme.fg("accent", pattern)}`;
217
+ text += theme.fg("toolOutput", ` in ${path}`);
218
+ if (limit !== undefined) {
219
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
220
+ }
221
+
222
+ return new Text(text, 0, 0);
223
+ },
224
+
225
+ renderResult(result, { expanded }, theme, _context) {
226
+ if (!expanded) {
227
+ // Minimal: just show count
228
+ const textContent = result.content.find((c) => c.type === "text");
229
+ if (textContent?.type === "text") {
230
+ const count = textContent.text.trim().split("\n").filter(Boolean).length;
231
+ if (count > 0) {
232
+ return new Text(theme.fg("muted", ` → ${count} files`), 0, 0);
233
+ }
234
+ }
235
+ return new Text("", 0, 0);
236
+ }
237
+
238
+ // Expanded: show full results
239
+ const textContent = result.content.find((c) => c.type === "text");
240
+ if (!textContent || textContent.type !== "text") {
241
+ return new Text("", 0, 0);
242
+ }
243
+
244
+ const output = textContent.text
245
+ .trim()
246
+ .split("\n")
247
+ .map((line) => theme.fg("toolOutput", line))
248
+ .join("\n");
249
+
250
+ return new Text(`\n${output}`, 0, 0);
251
+ },
252
+ });
253
+
254
+ // =========================================================================
255
+ // Grep Tool
256
+ // =========================================================================
257
+ knightcode.registerTool({
258
+ ...tools.grep,
259
+
260
+ renderCall(args, theme, _context) {
261
+ const pattern = args.pattern || "";
262
+ const path = shortenPath(args.path || ".");
263
+ const glob = args.glob;
264
+ const limit = args.limit;
265
+
266
+ let text = `${theme.fg("toolTitle", theme.bold("grep"))} ${theme.fg("accent", `/${pattern}/`)}`;
267
+ text += theme.fg("toolOutput", ` in ${path}`);
268
+ if (glob) {
269
+ text += theme.fg("toolOutput", ` (${glob})`);
270
+ }
271
+ if (limit !== undefined) {
272
+ text += theme.fg("toolOutput", ` limit ${limit}`);
273
+ }
274
+
275
+ return new Text(text, 0, 0);
276
+ },
277
+
278
+ renderResult(result, { expanded }, theme, _context) {
279
+ if (!expanded) {
280
+ // Minimal: just show match count
281
+ const textContent = result.content.find((c) => c.type === "text");
282
+ if (textContent?.type === "text") {
283
+ const count = textContent.text.trim().split("\n").filter(Boolean).length;
284
+ if (count > 0) {
285
+ return new Text(theme.fg("muted", ` → ${count} matches`), 0, 0);
286
+ }
287
+ }
288
+ return new Text("", 0, 0);
289
+ }
290
+
291
+ // Expanded: show full results
292
+ const textContent = result.content.find((c) => c.type === "text");
293
+ if (!textContent || textContent.type !== "text") {
294
+ return new Text("", 0, 0);
295
+ }
296
+
297
+ const output = textContent.text
298
+ .trim()
299
+ .split("\n")
300
+ .map((line) => theme.fg("toolOutput", line))
301
+ .join("\n");
302
+
303
+ return new Text(`\n${output}`, 0, 0);
304
+ },
305
+ });
306
+
307
+ // =========================================================================
308
+ // Ls Tool
309
+ // =========================================================================
310
+ knightcode.registerTool({
311
+ ...tools.ls,
312
+
313
+ renderCall(args, theme, _context) {
314
+ const path = shortenPath(args.path || ".");
315
+ const limit = args.limit;
316
+
317
+ let text = `${theme.fg("toolTitle", theme.bold("ls"))} ${theme.fg("accent", path)}`;
318
+ if (limit !== undefined) {
319
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
320
+ }
321
+
322
+ return new Text(text, 0, 0);
323
+ },
324
+
325
+ renderResult(result, { expanded }, theme, _context) {
326
+ if (!expanded) {
327
+ // Minimal: just show entry count
328
+ const textContent = result.content.find((c) => c.type === "text");
329
+ if (textContent?.type === "text") {
330
+ const count = textContent.text.trim().split("\n").filter(Boolean).length;
331
+ if (count > 0) {
332
+ return new Text(theme.fg("muted", ` → ${count} entries`), 0, 0);
333
+ }
334
+ }
335
+ return new Text("", 0, 0);
336
+ }
337
+
338
+ // Expanded: show full listing
339
+ const textContent = result.content.find((c) => c.type === "text");
340
+ if (!textContent || textContent.type !== "text") {
341
+ return new Text("", 0, 0);
342
+ }
343
+
344
+ const output = textContent.text
345
+ .trim()
346
+ .split("\n")
347
+ .map((line) => theme.fg("toolOutput", line))
348
+ .join("\n");
349
+
350
+ return new Text(`\n${output}`, 0, 0);
351
+ },
352
+ });
353
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Modal Editor - vim-like modal editing example
3
+ *
4
+ * Usage: knightcode --extension ./examples/extensions/modal-editor.ts
5
+ *
6
+ * - Escape: insert → normal mode (in normal mode, aborts agent)
7
+ * - i: normal → insert mode
8
+ * - hjkl: navigation in normal mode
9
+ * - ctrl+c, ctrl+d, etc. work in both modes
10
+ */
11
+
12
+ import { CustomEditor, type ExtensionAPI } from "@knightcodeai/cli";
13
+ import { matchesKey, truncateToWidth, visibleWidth } from "@knightcode/tui";
14
+
15
+ // Normal mode key mappings: key -> escape sequence (or null for mode switch)
16
+ const NORMAL_KEYS: Record<string, string | null> = {
17
+ h: "\x1b[D", // left
18
+ j: "\x1b[B", // down
19
+ k: "\x1b[A", // up
20
+ l: "\x1b[C", // right
21
+ "0": "\x01", // line start
22
+ $: "\x05", // line end
23
+ x: "\x1b[3~", // delete char
24
+ i: null, // insert mode
25
+ a: null, // append (insert + right)
26
+ };
27
+
28
+ class ModalEditor extends CustomEditor {
29
+ private mode: "normal" | "insert" = "insert";
30
+
31
+ handleInput(data: string): void {
32
+ // Escape toggles to normal mode, or passes through for app handling
33
+ if (matchesKey(data, "escape")) {
34
+ if (this.mode === "insert") {
35
+ this.mode = "normal";
36
+ } else {
37
+ super.handleInput(data); // abort agent, etc.
38
+ }
39
+ return;
40
+ }
41
+
42
+ // Insert mode: pass everything through
43
+ if (this.mode === "insert") {
44
+ super.handleInput(data);
45
+ return;
46
+ }
47
+
48
+ // Normal mode: check mapped keys
49
+ if (data in NORMAL_KEYS) {
50
+ const seq = NORMAL_KEYS[data];
51
+ if (data === "i") {
52
+ this.mode = "insert";
53
+ } else if (data === "a") {
54
+ this.mode = "insert";
55
+ super.handleInput("\x1b[C"); // move right first
56
+ } else if (seq) {
57
+ super.handleInput(seq);
58
+ }
59
+ return;
60
+ }
61
+
62
+ // Pass control sequences (ctrl+c, etc.) to super, ignore printable chars
63
+ if (data.length === 1 && data.charCodeAt(0) >= 32) return;
64
+ super.handleInput(data);
65
+ }
66
+
67
+ render(width: number): string[] {
68
+ const lines = super.render(width);
69
+ if (lines.length === 0) return lines;
70
+
71
+ // Add mode indicator to bottom border
72
+ const label = this.mode === "normal" ? " NORMAL " : " INSERT ";
73
+ const last = lines.length - 1;
74
+ if (visibleWidth(lines[last]!) >= label.length) {
75
+ lines[last] = truncateToWidth(lines[last]!, width - label.length, "") + label;
76
+ }
77
+ return lines;
78
+ }
79
+ }
80
+
81
+ export default function (knightcode: ExtensionAPI) {
82
+ knightcode.on("session_start", (_event, ctx) => {
83
+ ctx.ui.setEditorComponent((tui, theme, kb) => new ModalEditor(tui, theme, kb));
84
+ });
85
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Model status extension - shows model changes in the status bar.
3
+ *
4
+ * Demonstrates the `model_select` hook which fires when the model changes
5
+ * via /model command, Ctrl+P cycling, or session restore.
6
+ *
7
+ * Usage: knightcode -e ./model-status.ts
8
+ */
9
+
10
+ import type { ExtensionAPI } from "@knightcodeai/cli";
11
+
12
+ export default function (knightcode: ExtensionAPI) {
13
+ knightcode.on("model_select", async (event, ctx) => {
14
+ const { model, previousModel, source } = event;
15
+
16
+ // Format model identifiers
17
+ const next = `${model.provider}/${model.id}`;
18
+ const prev = previousModel ? `${previousModel.provider}/${previousModel.id}` : "none";
19
+
20
+ // Show notification on change
21
+ if (source !== "restore") {
22
+ ctx.ui.notify(`Model: ${next}`, "info");
23
+ }
24
+
25
+ // Update status bar with current model
26
+ ctx.ui.setStatus("model", `🤖 ${model.id}`);
27
+
28
+ // Log change details (visible in debug output)
29
+ console.log(`[model_select] ${prev} → ${next} (${source})`);
30
+ });
31
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * KnightCode Notify Extension
3
+ *
4
+ * Sends a native terminal notification when KnightCode agent is done and waiting for input.
5
+ * Supports multiple terminal protocols:
6
+ * - OSC 777: Ghostty, iTerm2, WezTerm, rxvt-unicode
7
+ * - OSC 99: Kitty
8
+ * - Windows toast: Windows Terminal (WSL)
9
+ */
10
+
11
+ import type { ExtensionAPI } from "@knightcodeai/cli";
12
+
13
+ function windowsToastScript(title: string, body: string): string {
14
+ const type = "Windows.UI.Notifications";
15
+ const mgr = `[${type}.ToastNotificationManager, ${type}, ContentType = WindowsRuntime]`;
16
+ const template = `[${type}.ToastTemplateType]::ToastText01`;
17
+ const toast = `[${type}.ToastNotification]::new($xml)`;
18
+ return [
19
+ `${mgr} > $null`,
20
+ `$xml = [${type}.ToastNotificationManager]::GetTemplateContent(${template})`,
21
+ `$xml.GetElementsByTagName('text')[0].AppendChild($xml.CreateTextNode('${body}')) > $null`,
22
+ `[${type}.ToastNotificationManager]::CreateToastNotifier('${title}').Show(${toast})`,
23
+ ].join("; ");
24
+ }
25
+
26
+ function notifyOSC777(title: string, body: string): void {
27
+ process.stdout.write(`\x1b]777;notify;${title};${body}\x07`);
28
+ }
29
+
30
+ function notifyOSC99(title: string, body: string): void {
31
+ // Kitty OSC 99: i=notification id, d=0 means not done yet, p=body for second part
32
+ process.stdout.write(`\x1b]99;i=1:d=0;${title}\x1b\\`);
33
+ process.stdout.write(`\x1b]99;i=1:p=body;${body}\x1b\\`);
34
+ }
35
+
36
+ function notifyWindows(title: string, body: string): void {
37
+ const { execFile } = require("child_process");
38
+ execFile("powershell.exe", ["-NoProfile", "-Command", windowsToastScript(title, body)]);
39
+ }
40
+
41
+ function notify(title: string, body: string): void {
42
+ if (process.env.WT_SESSION) {
43
+ notifyWindows(title, body);
44
+ } else if (process.env.KITTY_WINDOW_ID) {
45
+ notifyOSC99(title, body);
46
+ } else {
47
+ notifyOSC777(title, body);
48
+ }
49
+ }
50
+
51
+ export default function (knightcode: ExtensionAPI) {
52
+ // `agent_end` fires after each low-level run; KnightCode may still retry, compact,
53
+ // or continue with queued follow-ups. Notify only after the full run settles.
54
+ knightcode.on("agent_settled", async () => {
55
+ notify("KnightCode", "Ready for input");
56
+ });
57
+ }