@earendil-works/pi-coding-agent 0.99.1 → 1.0.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.
Files changed (221) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/dist/bundle/chunks/anthropic-messages-UID2PALB.js +6 -0
  3. package/dist/bundle/chunks/anthropic.js +2 -2
  4. package/dist/bundle/chunks/{azure-openai-responses-EN5WOBET.js → azure-openai-responses-AFMVM6NP.js} +1 -1
  5. package/dist/bundle/chunks/bedrock-converse-stream.js +1 -1
  6. package/dist/bundle/chunks/{chunk-AXPY26X7.js → chunk-2KTBZM5G.js} +14 -28
  7. package/dist/bundle/chunks/{chunk-LRS5S6EK.js → chunk-2VNGUWBO.js} +2 -2
  8. package/dist/bundle/chunks/chunk-33XOIQ5N.js +1204 -0
  9. package/dist/bundle/chunks/{chunk-W2DOMZSC.js → chunk-3LH3ZQKY.js} +1 -1
  10. package/dist/bundle/chunks/chunk-4KU537KD.js +2 -0
  11. package/dist/bundle/chunks/chunk-6D7LHLKG.js +2 -0
  12. package/dist/bundle/chunks/chunk-7FH3BDRG.js +82 -0
  13. package/dist/bundle/chunks/{chunk-LLMKQWZS.js → chunk-IBCPYVFK.js} +7 -9
  14. package/dist/bundle/chunks/chunk-KDN6RCEQ.js +4 -0
  15. package/dist/bundle/chunks/chunk-LD2P6SFZ.js +2 -0
  16. package/dist/bundle/chunks/{chunk-PEHWOV5U.js → chunk-LTXEMVMA.js} +18 -78
  17. package/dist/bundle/chunks/{chunk-J4CWPC4N.js → chunk-OSKPX3Z5.js} +1 -1
  18. package/dist/bundle/chunks/chunk-PRD6WYBT.js +16 -0
  19. package/dist/bundle/chunks/chunk-VIV3CWXG.js +2 -0
  20. package/dist/bundle/chunks/{cli-AZXNGIZH.js → cli-QOMC26IJ.js} +8 -6
  21. package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-XJQQYGGU.js → cloudflare-workers-ai-system-one-NPQN263E.js} +1 -1
  22. package/dist/bundle/chunks/codemode-worker.js +67 -8
  23. package/dist/bundle/chunks/execute-GVRSEGHL.js +367 -0
  24. package/dist/bundle/chunks/github-copilot.js +1 -1
  25. package/dist/bundle/chunks/{google-generative-ai-CQAKKJTA.js → google-generative-ai-APTPZOQL.js} +1 -1
  26. package/dist/bundle/chunks/{google-vertex-AE36YXJU.js → google-vertex-MXCOSOY3.js} +1 -1
  27. package/dist/bundle/chunks/{llama-cpp-classify-PJJYKPLD.js → llama-cpp-classify-XT44PI3C.js} +1 -1
  28. package/dist/bundle/chunks/{mistral-conversations-KEYTXJM7.js → mistral-conversations-DKVFNQL6.js} +1 -1
  29. package/dist/bundle/chunks/openai-chatgpt.js +1 -1
  30. package/dist/bundle/chunks/{openai-codex-responses-JQTCOVEG.js → openai-codex-responses-GK67DQQJ.js} +1 -1
  31. package/dist/bundle/chunks/openai-codex.js +1 -1
  32. package/dist/bundle/chunks/{openai-completions-XW2Q5HVC.js → openai-completions-JXDDPZ23.js} +1 -1
  33. package/dist/bundle/chunks/{openai-responses-6AXWFL75.js → openai-responses-CQDDLDQF.js} +1 -1
  34. package/dist/bundle/chunks/{openrouter-images-TPQ4V6OR.js → openrouter-images-ZVXWCSIM.js} +1 -1
  35. package/dist/bundle/chunks/openrouter.js +1 -1
  36. package/dist/bundle/chunks/pi-logo-animation-MPKZE4EK.js +2 -0
  37. package/dist/bundle/chunks/radius.js +1 -1
  38. package/dist/bundle/chunks/runtime-2SO2JDWM.js +2 -0
  39. package/dist/bundle/chunks/{typesafe-system-one-E7A4HNJI.js → typesafe-system-one-A4CZTQWZ.js} +1 -1
  40. package/dist/bundle/chunks/virtual-modules-6ZMK7GDO.js +2 -0
  41. package/dist/bundle/cli-runtime.js +1 -1
  42. package/dist/bundle/index.js +1 -1
  43. package/dist/bundle/rpc-entry.js +1 -1
  44. package/dist/cli/args.js +2 -2
  45. package/dist/cli/args.js.map +1 -1
  46. package/dist/config.d.ts +5 -8
  47. package/dist/config.d.ts.map +1 -1
  48. package/dist/config.js +16 -13
  49. package/dist/config.js.map +1 -1
  50. package/dist/core/agent-session.d.ts +15 -1
  51. package/dist/core/agent-session.d.ts.map +1 -1
  52. package/dist/core/agent-session.js +51 -10
  53. package/dist/core/agent-session.js.map +1 -1
  54. package/dist/core/extensions/loader.d.ts.map +1 -1
  55. package/dist/core/extensions/loader.js +13 -1
  56. package/dist/core/extensions/loader.js.map +1 -1
  57. package/dist/core/extensions/types.d.ts +6 -1
  58. package/dist/core/extensions/types.d.ts.map +1 -1
  59. package/dist/core/extensions/types.js.map +1 -1
  60. package/dist/core/mcp-servers.d.ts +34 -7
  61. package/dist/core/mcp-servers.d.ts.map +1 -1
  62. package/dist/core/mcp-servers.js +52 -11
  63. package/dist/core/mcp-servers.js.map +1 -1
  64. package/dist/core/model-registry.d.ts +3 -1
  65. package/dist/core/model-registry.d.ts.map +1 -1
  66. package/dist/core/model-registry.js +4 -0
  67. package/dist/core/model-registry.js.map +1 -1
  68. package/dist/core/model-runtime.d.ts +6 -0
  69. package/dist/core/model-runtime.d.ts.map +1 -1
  70. package/dist/core/model-runtime.js +22 -18
  71. package/dist/core/model-runtime.js.map +1 -1
  72. package/dist/core/radius.d.ts +2 -0
  73. package/dist/core/radius.d.ts.map +1 -1
  74. package/dist/core/radius.js +2 -0
  75. package/dist/core/radius.js.map +1 -1
  76. package/dist/core/remote-catalog-provider.d.ts.map +1 -1
  77. package/dist/core/remote-catalog-provider.js +4 -9
  78. package/dist/core/remote-catalog-provider.js.map +1 -1
  79. package/dist/core/sdk.d.ts.map +1 -1
  80. package/dist/core/sdk.js +1 -0
  81. package/dist/core/sdk.js.map +1 -1
  82. package/dist/core/settings-manager.d.ts +7 -5
  83. package/dist/core/settings-manager.d.ts.map +1 -1
  84. package/dist/core/settings-manager.js +3 -2
  85. package/dist/core/settings-manager.js.map +1 -1
  86. package/dist/core/system-prompt.js +1 -1
  87. package/dist/core/system-prompt.js.map +1 -1
  88. package/dist/core/tools/bash.d.ts.map +1 -1
  89. package/dist/core/tools/bash.js +3 -5
  90. package/dist/core/tools/bash.js.map +1 -1
  91. package/dist/core/tools/renderers/bash.d.ts.map +1 -1
  92. package/dist/core/tools/renderers/bash.js +11 -31
  93. package/dist/core/tools/renderers/bash.js.map +1 -1
  94. package/dist/core/virtual-models.d.ts +2 -0
  95. package/dist/core/virtual-models.d.ts.map +1 -1
  96. package/dist/core/virtual-models.js +19 -12
  97. package/dist/core/virtual-models.js.map +1 -1
  98. package/dist/extensions/codemode/execute.d.ts.map +1 -1
  99. package/dist/extensions/codemode/execute.js +197 -51
  100. package/dist/extensions/codemode/execute.js.map +1 -1
  101. package/dist/extensions/codemode/renderer.d.ts.map +1 -1
  102. package/dist/extensions/codemode/renderer.js +43 -25
  103. package/dist/extensions/codemode/renderer.js.map +1 -1
  104. package/dist/extensions/codemode/tool.d.ts +12 -11
  105. package/dist/extensions/codemode/tool.d.ts.map +1 -1
  106. package/dist/extensions/codemode/tool.js +73 -129
  107. package/dist/extensions/codemode/tool.js.map +1 -1
  108. package/dist/extensions/codemode/worker.d.ts +1 -1
  109. package/dist/extensions/codemode/worker.js +1 -1
  110. package/dist/extensions/codemode/worker.js.map +1 -1
  111. package/dist/extensions/mcp/cli.d.ts.map +1 -1
  112. package/dist/extensions/mcp/cli.js +20 -5
  113. package/dist/extensions/mcp/cli.js.map +1 -1
  114. package/dist/extensions/mcp/config.d.ts +4 -2
  115. package/dist/extensions/mcp/config.d.ts.map +1 -1
  116. package/dist/extensions/mcp/config.js +14 -2
  117. package/dist/extensions/mcp/config.js.map +1 -1
  118. package/dist/extensions/mcp/index.d.ts +32 -8
  119. package/dist/extensions/mcp/index.d.ts.map +1 -1
  120. package/dist/extensions/mcp/index.js +220 -56
  121. package/dist/extensions/mcp/index.js.map +1 -1
  122. package/dist/extensions/mcp/oauth.d.ts +10 -7
  123. package/dist/extensions/mcp/oauth.d.ts.map +1 -1
  124. package/dist/extensions/mcp/oauth.js +56 -31
  125. package/dist/extensions/mcp/oauth.js.map +1 -1
  126. package/dist/extensions/mcp/runtime.d.ts +2 -0
  127. package/dist/extensions/mcp/runtime.d.ts.map +1 -1
  128. package/dist/extensions/mcp/runtime.js +16 -9
  129. package/dist/extensions/mcp/runtime.js.map +1 -1
  130. package/dist/extensions/mcp/tools.d.ts +5 -4
  131. package/dist/extensions/mcp/tools.d.ts.map +1 -1
  132. package/dist/extensions/mcp/tools.js +33 -15
  133. package/dist/extensions/mcp/tools.js.map +1 -1
  134. package/dist/extensions/mcp/ui.d.ts.map +1 -1
  135. package/dist/extensions/mcp/ui.js +4 -2
  136. package/dist/extensions/mcp/ui.js.map +1 -1
  137. package/dist/extensions/tool-search/tool.d.ts +5 -4
  138. package/dist/extensions/tool-search/tool.d.ts.map +1 -1
  139. package/dist/extensions/tool-search/tool.js +7 -28
  140. package/dist/extensions/tool-search/tool.js.map +1 -1
  141. package/dist/index.d.ts +1 -1
  142. package/dist/index.d.ts.map +1 -1
  143. package/dist/index.js.map +1 -1
  144. package/dist/main.d.ts.map +1 -1
  145. package/dist/main.js +6 -0
  146. package/dist/main.js.map +1 -1
  147. package/dist/modes/interactive/components/oauth-selector.d.ts +8 -2
  148. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  149. package/dist/modes/interactive/components/oauth-selector.js +24 -21
  150. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  151. package/dist/modes/interactive/components/pi-logo-animation.d.ts +68 -0
  152. package/dist/modes/interactive/components/pi-logo-animation.d.ts.map +1 -0
  153. package/dist/modes/interactive/components/pi-logo-animation.js +1086 -0
  154. package/dist/modes/interactive/components/pi-logo-animation.js.map +1 -0
  155. package/dist/modes/interactive/components/pi-logo-animation.lazy.d.ts +7 -0
  156. package/dist/modes/interactive/components/pi-logo-animation.lazy.d.ts.map +1 -0
  157. package/dist/modes/interactive/components/pi-logo-animation.lazy.js +12 -0
  158. package/dist/modes/interactive/components/pi-logo-animation.lazy.js.map +1 -0
  159. package/dist/modes/interactive/components/pi-logo.d.ts +7 -0
  160. package/dist/modes/interactive/components/pi-logo.d.ts.map +1 -1
  161. package/dist/modes/interactive/components/pi-logo.js +13 -1
  162. package/dist/modes/interactive/components/pi-logo.js.map +1 -1
  163. package/dist/modes/interactive/components/radius-login-selector.d.ts +15 -0
  164. package/dist/modes/interactive/components/radius-login-selector.d.ts.map +1 -0
  165. package/dist/modes/interactive/components/radius-login-selector.js +85 -0
  166. package/dist/modes/interactive/components/radius-login-selector.js.map +1 -0
  167. package/dist/modes/interactive/components/settings-selector.d.ts +3 -3
  168. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  169. package/dist/modes/interactive/components/settings-selector.js +5 -5
  170. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  171. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  172. package/dist/modes/interactive/components/user-message.js +5 -4
  173. package/dist/modes/interactive/components/user-message.js.map +1 -1
  174. package/dist/modes/interactive/components/visual-truncate.d.ts +27 -3
  175. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -1
  176. package/dist/modes/interactive/components/visual-truncate.js +39 -6
  177. package/dist/modes/interactive/components/visual-truncate.js.map +1 -1
  178. package/dist/modes/interactive/interactive-mode.d.ts +10 -0
  179. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  180. package/dist/modes/interactive/interactive-mode.js +153 -40
  181. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  182. package/dist/modes/interactive/theme/system-theme.d.ts +2 -1
  183. package/dist/modes/interactive/theme/system-theme.d.ts.map +1 -1
  184. package/dist/modes/interactive/theme/system-theme.js +16 -5
  185. package/dist/modes/interactive/theme/system-theme.js.map +1 -1
  186. package/docs/cli.md +8 -18
  187. package/docs/codemode.md +207 -0
  188. package/docs/docs.json +5 -1
  189. package/docs/environment-variables.md +2 -2
  190. package/docs/extensions.md +3 -3
  191. package/docs/index.md +1 -1
  192. package/docs/mcp.md +142 -82
  193. package/docs/models.md +22 -1
  194. package/docs/providers.md +18 -6
  195. package/docs/sdk.md +2 -2
  196. package/docs/settings.md +5 -3
  197. package/docs/usage.md +1 -1
  198. package/examples/extensions/built-in-tool-renderer.ts +16 -40
  199. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  200. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  201. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  202. package/examples/extensions/gondolin/package-lock.json +2 -2
  203. package/examples/extensions/gondolin/package.json +1 -1
  204. package/examples/extensions/minimal-mode.ts +24 -96
  205. package/examples/extensions/sandbox/package-lock.json +2 -2
  206. package/examples/extensions/sandbox/package.json +1 -1
  207. package/examples/extensions/with-deps/package-lock.json +2 -2
  208. package/examples/extensions/with-deps/package.json +1 -1
  209. package/examples/sdk/14-codemode-mcp.ts +2 -2
  210. package/npm-shrinkwrap.json +25 -30
  211. package/package.json +10 -10
  212. package/dist/bundle/chunks/anthropic-messages-A6EXUBG6.js +0 -6
  213. package/dist/bundle/chunks/chunk-GOPCREV2.js +0 -4
  214. package/dist/bundle/chunks/chunk-GUORCHFS.js +0 -1435
  215. package/dist/bundle/chunks/chunk-HSPCVFST.js +0 -2
  216. package/dist/bundle/chunks/chunk-IMTXZUO7.js +0 -82
  217. package/dist/bundle/chunks/chunk-SLDA2X2H.js +0 -2
  218. package/dist/bundle/chunks/chunk-XNGRGP62.js +0 -2
  219. package/dist/bundle/chunks/execute-7XXPZAWW.js +0 -308
  220. package/dist/bundle/chunks/runtime-G5WJPRBM.js +0 -2
  221. package/dist/bundle/chunks/virtual-modules-CSWRO37N.js +0 -2
@@ -6,9 +6,10 @@
6
6
  * header. Nested calls are not
7
7
  * separate tool rows because they never reach the model as tool calls.
8
8
  */
9
- import { Text } from "@earendil-works/pi-tui";
9
+ import { Container, Spacer, Text } from "@earendil-works/pi-tui";
10
10
  import { getTextOutput, replaceTabs, str } from "../../core/tools/render-utils.js";
11
11
  import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";
12
+ import { VisualLinePreview } from "../../modes/interactive/components/visual-truncate.js";
12
13
  import { highlightCode } from "../../modes/interactive/theme/theme.js";
13
14
  const CODE_PREVIEW_LINES = 10;
14
15
  const CALL_PREVIEW_COUNT = 8;
@@ -59,23 +60,30 @@ export const codemodeRenderers = {
59
60
  renderCall(args, theme, context) {
60
61
  // The code includes the `// @options:` line, so options show as part of the script.
61
62
  const code = str(args?.code);
62
- let text = theme.fg("toolTitle", theme.bold("codemode"));
63
+ const title = theme.fg("toolTitle", theme.bold("codemode"));
64
+ const component = context.lastComponent ?? new Container();
65
+ component.clear();
63
66
  if (code === null) {
64
- text += ` ${theme.fg("error", "[invalid arg]")}`;
67
+ component.addChild(new Text(`${title} ${theme.fg("error", "[invalid arg]")}`, 0, 0));
68
+ return component;
65
69
  }
66
- else if (code) {
67
- const lines = highlightCode(replaceTabs(code.replace(/\r/g, "").trimEnd()), "javascript");
68
- const shown = context.expanded ? lines : lines.slice(0, CODE_PREVIEW_LINES);
69
- text += `\n${shown.join("\n")}`;
70
- if (shown.length < lines.length)
71
- text += `\n${expandHint(theme, lines.length - shown.length, "lines")}`;
70
+ component.addChild(new Text(title, 0, 0));
71
+ if (code) {
72
+ const highlighted = highlightCode(replaceTabs(code.replace(/\r/g, "").trimEnd()), "javascript").join("\n");
73
+ component.addChild(context.expanded
74
+ ? new Text(highlighted, 0, 0)
75
+ : new VisualLinePreview({
76
+ text: highlighted,
77
+ maxVisualLines: CODE_PREVIEW_LINES,
78
+ keep: "start",
79
+ formatHint: (hidden) => expandHint(theme, hidden, "lines"),
80
+ }));
72
81
  }
73
- const component = context.lastComponent ?? new Text("", 0, 0);
74
- component.setText(text);
75
82
  return component;
76
83
  },
77
84
  renderResult(result, options, theme, context) {
78
- const sections = [];
85
+ const component = context.lastComponent ?? new Container();
86
+ component.clear();
79
87
  const calls = result.details?.calls ?? [];
80
88
  if (calls.length > 0) {
81
89
  const shown = options.expanded ? calls : calls.slice(-CALL_PREVIEW_COUNT);
@@ -89,7 +97,8 @@ export const codemodeRenderers = {
89
97
  const total = priced.reduce((sum, call) => sum + (call.cost ?? 0), 0);
90
98
  lines.push(theme.fg("muted", `Model calls: ${formatCost(total)}`));
91
99
  }
92
- sections.push(lines.join("\n"));
100
+ component.addChild(new Spacer(1));
101
+ component.addChild(new Text(lines.join("\n"), 0, 0));
93
102
  }
94
103
  // Drop the "Script completed\nWall time ...\nOutput:\n" header. Rejected input (invalid options)
95
104
  // has no header.
@@ -100,19 +109,28 @@ export const codemodeRenderers = {
100
109
  : getTextOutput({ ...result, content: hasHeader ? rest : result.content }, context.showImages).trim();
101
110
  if (output) {
102
111
  const color = context.isError ? "error" : "toolOutput";
103
- const lines = replaceTabs(output).split("\n");
104
- const shown = options.expanded ? lines : lines.slice(0, OUTPUT_PREVIEW_LINES);
105
- let text = shown.map((line) => theme.fg(color, line)).join("\n");
106
- if (shown.length < lines.length)
107
- text += `\n${expandHint(theme, lines.length - shown.length, "lines")}`;
108
- // The collapsed preview hides the truncation notice at the end, so name the file here.
109
- const fullOutputPath = result.details?.fullOutputPath;
110
- if (fullOutputPath && !options.expanded)
111
- text += `\n${theme.fg("muted", `Full output: ${fullOutputPath}`)}`;
112
- sections.push(text);
112
+ const styled = replaceTabs(output)
113
+ .split("\n")
114
+ .map((line) => theme.fg(color, line))
115
+ .join("\n");
116
+ component.addChild(new Spacer(1));
117
+ if (options.expanded) {
118
+ component.addChild(new Text(styled, 0, 0));
119
+ }
120
+ else {
121
+ // Limit wrapped lines, not logical ones: script output is often one long JSON line.
122
+ component.addChild(new VisualLinePreview({
123
+ text: styled,
124
+ maxVisualLines: OUTPUT_PREVIEW_LINES,
125
+ keep: "start",
126
+ formatHint: (hidden) => expandHint(theme, hidden, "lines"),
127
+ }));
128
+ // The collapsed preview hides the truncation notice at the end, so name the file here.
129
+ const fullOutputPath = result.details?.fullOutputPath;
130
+ if (fullOutputPath)
131
+ component.addChild(new Text(theme.fg("muted", `Full output: ${fullOutputPath}`), 0, 0));
132
+ }
113
133
  }
114
- const component = context.lastComponent ?? new Text("", 0, 0);
115
- component.setText(sections.length > 0 ? `\n${sections.join("\n\n")}` : "");
116
134
  return component;
117
135
  },
118
136
  };
@@ -1 +1 @@
1
- {"version":3,"file":"renderer.js","sourceRoot":"","sources":["../../../src/extensions/codemode/renderer.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,aAAa,EAAc,MAAM,wCAAwC,CAAC;AAGnF,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAC9B,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAC7B,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,aAAa,GAAG,kEAAkE,CAAC;AAEzF,SAAS,UAAU,CAAC,KAAY,EAAE,MAAc,EAAE,IAAY;IAC7D,OAAO,GAAG,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,QAAQ,MAAM,SAAS,IAAI,GAAG,CAAC,IAAI,OAAO,CAAC,kBAAkB,EAAE,WAAW,CAAC,GAAG,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC;AACpI,CAAC;AAED,SAAS,cAAc,CAAC,EAAsB;IAC7C,IAAI,EAAE,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IAChC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;AACzE,CAAC;AAED,0GAA0G;AAC1G,SAAS,UAAU,CAAC,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their status as they\n * run and the cost of its model calls, followed by the script output without the \"Script completed\"\n * header. Nested calls are not\n * separate tool rows because they never reach the model as tool calls.\n */\n\nimport { Text } from \"@earendil-works/pi-tui\";\nimport type { ToolDefinition } from \"../../core/extensions/types.ts\";\nimport { getTextOutput, replaceTabs, str } from \"../../core/tools/render-utils.ts\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { highlightCode, type Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport type { CodemodeNestedCall, CodemodeToolDetails } from \"./tool.ts\";\n\nconst CODE_PREVIEW_LINES = 10;\nconst CALL_PREVIEW_COUNT = 8;\nconst OUTPUT_PREVIEW_LINES = 5;\nconst COLLAPSED_ARGS_CHARS = 80;\nconst SCRIPT_HEADER = /^Script (completed|failed)\\nWall time [\\d.]+ seconds\\nOutput:\\n$/;\n\nfunction expandHint(theme: Theme, hidden: number, noun: string): string {\n\treturn `${theme.fg(\"muted\", `... (${hidden} more ${noun},`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`;\n}\n\nfunction formatDuration(ms: number | undefined): string {\n\tif (ms === undefined) return \"\";\n\treturn ms < 1000 ? `${Math.round(ms)}ms` : `${(ms / 1000).toFixed(1)}s`;\n}\n\n/** Cents for larger amounts, two significant digits for the fractions of a cent classifier calls cost. */\nfunction formatCost(cost: number): string {\n\treturn `$${cost >= 0.01 ? cost.toFixed(2) : cost.toPrecision(2)}`;\n}\n\nfunction statusIcon(call: CodemodeNestedCall, theme: Theme): string {\n\tswitch (call.status) {\n\t\tcase \"running\":\n\t\t\treturn theme.fg(\"warning\", \"…\");\n\t\tcase \"ok\":\n\t\t\treturn theme.fg(\"success\", \"✓\");\n\t\tcase \"error\":\n\t\t\treturn theme.fg(\"error\", \"✗\");\n\t\tcase \"cancelled\":\n\t\t\treturn theme.fg(\"muted\", \"⊘\");\n\t}\n}\n\nfunction formatCall(call: CodemodeNestedCall, theme: Theme, expanded: boolean): string {\n\tconst args =\n\t\t!expanded && call.args.length > COLLAPSED_ARGS_CHARS\n\t\t\t? `${call.args.slice(0, COLLAPSED_ARGS_CHARS - 3)}...`\n\t\t\t: call.args;\n\tconst duration = formatDuration(call.durationMs);\n\tlet line = `${statusIcon(call, theme)} ${theme.fg(\"toolTitle\", call.name)}`;\n\tif (args) line += ` ${theme.fg(\"muted\", args)}`;\n\tif (duration) line += ` ${theme.fg(\"dim\", duration)}`;\n\tif (call.cost) line += ` ${theme.fg(\"dim\", formatCost(call.cost))}`;\n\tif (expanded && call.error) line += `\\n ${theme.fg(\"error\", call.error.split(\"\\n\").join(\"\\n \"))}`;\n\treturn line;\n}\n\nexport const codemodeRenderers: Pick<\n\tToolDefinition<any, CodemodeToolDetails | undefined>,\n\t\"renderCall\" | \"renderResult\"\n> = {\n\trenderCall(args, theme, context) {\n\t\t// The code includes the `// @options:` line, so options show as part of the script.\n\t\tconst code = str((args as { code?: unknown } | undefined)?.code);\n\t\tlet text = theme.fg(\"toolTitle\", theme.bold(\"codemode\"));\n\t\tif (code === null) {\n\t\t\ttext += ` ${theme.fg(\"error\", \"[invalid arg]\")}`;\n\t\t} else if (code) {\n\t\t\tconst lines = highlightCode(replaceTabs(code.replace(/\\r/g, \"\").trimEnd()), \"javascript\");\n\t\t\tconst shown = context.expanded ? lines : lines.slice(0, CODE_PREVIEW_LINES);\n\t\t\ttext += `\\n${shown.join(\"\\n\")}`;\n\t\t\tif (shown.length < lines.length) text += `\\n${expandHint(theme, lines.length - shown.length, \"lines\")}`;\n\t\t}\n\t\tconst component = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\tcomponent.setText(text);\n\t\treturn component;\n\t},\n\trenderResult(result, options, theme, context) {\n\t\tconst sections: string[] = [];\n\t\tconst calls = result.details?.calls ?? [];\n\t\tif (calls.length > 0) {\n\t\t\tconst shown = options.expanded ? calls : calls.slice(-CALL_PREVIEW_COUNT);\n\t\t\tconst lines = shown.map((call) => formatCall(call, theme, options.expanded));\n\t\t\tif (shown.length < calls.length) {\n\t\t\t\tlines.unshift(\n\t\t\t\t\t`${theme.fg(\"muted\", `... (${calls.length - shown.length} earlier calls,`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\t// Collapsed rows hide earlier calls, so the total covers every call.\n\t\t\tconst priced = calls.filter((call) => call.cost);\n\t\t\tif (priced.length > 1) {\n\t\t\t\tconst total = priced.reduce((sum, call) => sum + (call.cost ?? 0), 0);\n\t\t\t\tlines.push(theme.fg(\"muted\", `Model calls: ${formatCost(total)}`));\n\t\t\t}\n\t\t\tsections.push(lines.join(\"\\n\"));\n\t\t}\n\n\t\t// Drop the \"Script completed\\nWall time ...\\nOutput:\\n\" header. Rejected input (invalid options)\n\t\t// has no header.\n\t\tconst [first, ...rest] = result.content;\n\t\tconst hasHeader = first?.type === \"text\" && SCRIPT_HEADER.test(first.text);\n\t\tconst output = options.isPartial\n\t\t\t? \"\"\n\t\t\t: getTextOutput({ ...result, content: hasHeader ? rest : result.content }, context.showImages).trim();\n\t\tif (output) {\n\t\t\tconst color = context.isError ? \"error\" : \"toolOutput\";\n\t\t\tconst lines = replaceTabs(output).split(\"\\n\");\n\t\t\tconst shown = options.expanded ? lines : lines.slice(0, OUTPUT_PREVIEW_LINES);\n\t\t\tlet text = shown.map((line) => theme.fg(color, line)).join(\"\\n\");\n\t\t\tif (shown.length < lines.length) text += `\\n${expandHint(theme, lines.length - shown.length, \"lines\")}`;\n\t\t\t// The collapsed preview hides the truncation notice at the end, so name the file here.\n\t\t\tconst fullOutputPath = result.details?.fullOutputPath;\n\t\t\tif (fullOutputPath && !options.expanded) text += `\\n${theme.fg(\"muted\", `Full output: ${fullOutputPath}`)}`;\n\t\t\tsections.push(text);\n\t\t}\n\n\t\tconst component = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\tcomponent.setText(sections.length > 0 ? `\\n${sections.join(\"\\n\\n\")}` : \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AC,OAAO,EAAE,gBAAgB,cAAc,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAC7G,CAAC;QACF,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;CACD,CAAC","sourcesContent":["/**\n * Presentation for the codemode tool.\n *\n * The call shows the script; the result lists the nested tool calls with their status as they\n * run and the cost of its model calls, followed by the script output without the \"Script completed\"\n * header. Nested calls are not\n * separate tool rows because they never reach the model as tool calls.\n */\n\nimport { Container, Spacer, Text } from \"@earendil-works/pi-tui\";\nimport type { ToolDefinition } from \"../../core/extensions/types.ts\";\nimport { getTextOutput, replaceTabs, str } from \"../../core/tools/render-utils.ts\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { VisualLinePreview } from \"../../modes/interactive/components/visual-truncate.ts\";\nimport { highlightCode, type Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport type { CodemodeNestedCall, CodemodeToolDetails } from \"./tool.ts\";\n\nconst CODE_PREVIEW_LINES = 10;\nconst CALL_PREVIEW_COUNT = 8;\nconst OUTPUT_PREVIEW_LINES = 5;\nconst COLLAPSED_ARGS_CHARS = 80;\nconst SCRIPT_HEADER = /^Script (completed|failed)\\nWall time [\\d.]+ seconds\\nOutput:\\n$/;\n\nfunction expandHint(theme: Theme, hidden: number, noun: string): string {\n\treturn `${theme.fg(\"muted\", `... (${hidden} more ${noun},`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`;\n}\n\nfunction formatDuration(ms: number | undefined): string {\n\tif (ms === undefined) return \"\";\n\treturn ms < 1000 ? `${Math.round(ms)}ms` : `${(ms / 1000).toFixed(1)}s`;\n}\n\n/** Cents for larger amounts, two significant digits for the fractions of a cent classifier calls cost. */\nfunction formatCost(cost: number): string {\n\treturn `$${cost >= 0.01 ? cost.toFixed(2) : cost.toPrecision(2)}`;\n}\n\nfunction statusIcon(call: CodemodeNestedCall, theme: Theme): string {\n\tswitch (call.status) {\n\t\tcase \"running\":\n\t\t\treturn theme.fg(\"warning\", \"…\");\n\t\tcase \"ok\":\n\t\t\treturn theme.fg(\"success\", \"✓\");\n\t\tcase \"error\":\n\t\t\treturn theme.fg(\"error\", \"✗\");\n\t\tcase \"cancelled\":\n\t\t\treturn theme.fg(\"muted\", \"⊘\");\n\t}\n}\n\nfunction formatCall(call: CodemodeNestedCall, theme: Theme, expanded: boolean): string {\n\tconst args =\n\t\t!expanded && call.args.length > COLLAPSED_ARGS_CHARS\n\t\t\t? `${call.args.slice(0, COLLAPSED_ARGS_CHARS - 3)}...`\n\t\t\t: call.args;\n\tconst duration = formatDuration(call.durationMs);\n\tlet line = `${statusIcon(call, theme)} ${theme.fg(\"toolTitle\", call.name)}`;\n\tif (args) line += ` ${theme.fg(\"muted\", args)}`;\n\tif (duration) line += ` ${theme.fg(\"dim\", duration)}`;\n\tif (call.cost) line += ` ${theme.fg(\"dim\", formatCost(call.cost))}`;\n\tif (expanded && call.error) line += `\\n ${theme.fg(\"error\", call.error.split(\"\\n\").join(\"\\n \"))}`;\n\treturn line;\n}\n\nexport const codemodeRenderers: Pick<\n\tToolDefinition<any, CodemodeToolDetails | undefined>,\n\t\"renderCall\" | \"renderResult\"\n> = {\n\trenderCall(args, theme, context) {\n\t\t// The code includes the `// @options:` line, so options show as part of the script.\n\t\tconst code = str((args as { code?: unknown } | undefined)?.code);\n\t\tconst title = theme.fg(\"toolTitle\", theme.bold(\"codemode\"));\n\t\tconst component = (context.lastComponent as Container | undefined) ?? new Container();\n\t\tcomponent.clear();\n\t\tif (code === null) {\n\t\t\tcomponent.addChild(new Text(`${title} ${theme.fg(\"error\", \"[invalid arg]\")}`, 0, 0));\n\t\t\treturn component;\n\t\t}\n\t\tcomponent.addChild(new Text(title, 0, 0));\n\t\tif (code) {\n\t\t\tconst highlighted = highlightCode(replaceTabs(code.replace(/\\r/g, \"\").trimEnd()), \"javascript\").join(\"\\n\");\n\t\t\tcomponent.addChild(\n\t\t\t\tcontext.expanded\n\t\t\t\t\t? new Text(highlighted, 0, 0)\n\t\t\t\t\t: new VisualLinePreview({\n\t\t\t\t\t\t\ttext: highlighted,\n\t\t\t\t\t\t\tmaxVisualLines: CODE_PREVIEW_LINES,\n\t\t\t\t\t\t\tkeep: \"start\",\n\t\t\t\t\t\t\tformatHint: (hidden) => expandHint(theme, hidden, \"lines\"),\n\t\t\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\treturn component;\n\t},\n\trenderResult(result, options, theme, context) {\n\t\tconst component = (context.lastComponent as Container | undefined) ?? new Container();\n\t\tcomponent.clear();\n\t\tconst calls = result.details?.calls ?? [];\n\t\tif (calls.length > 0) {\n\t\t\tconst shown = options.expanded ? calls : calls.slice(-CALL_PREVIEW_COUNT);\n\t\t\tconst lines = shown.map((call) => formatCall(call, theme, options.expanded));\n\t\t\tif (shown.length < calls.length) {\n\t\t\t\tlines.unshift(\n\t\t\t\t\t`${theme.fg(\"muted\", `... (${calls.length - shown.length} earlier calls,`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\t// Collapsed rows hide earlier calls, so the total covers every call.\n\t\t\tconst priced = calls.filter((call) => call.cost);\n\t\t\tif (priced.length > 1) {\n\t\t\t\tconst total = priced.reduce((sum, call) => sum + (call.cost ?? 0), 0);\n\t\t\t\tlines.push(theme.fg(\"muted\", `Model calls: ${formatCost(total)}`));\n\t\t\t}\n\t\t\tcomponent.addChild(new Spacer(1));\n\t\t\tcomponent.addChild(new Text(lines.join(\"\\n\"), 0, 0));\n\t\t}\n\n\t\t// Drop the \"Script completed\\nWall time ...\\nOutput:\\n\" header. Rejected input (invalid options)\n\t\t// has no header.\n\t\tconst [first, ...rest] = result.content;\n\t\tconst hasHeader = first?.type === \"text\" && SCRIPT_HEADER.test(first.text);\n\t\tconst output = options.isPartial\n\t\t\t? \"\"\n\t\t\t: getTextOutput({ ...result, content: hasHeader ? rest : result.content }, context.showImages).trim();\n\t\tif (output) {\n\t\t\tconst color = context.isError ? \"error\" : \"toolOutput\";\n\t\t\tconst styled = replaceTabs(output)\n\t\t\t\t.split(\"\\n\")\n\t\t\t\t.map((line) => theme.fg(color, line))\n\t\t\t\t.join(\"\\n\");\n\t\t\tcomponent.addChild(new Spacer(1));\n\t\t\tif (options.expanded) {\n\t\t\t\tcomponent.addChild(new Text(styled, 0, 0));\n\t\t\t} else {\n\t\t\t\t// Limit wrapped lines, not logical ones: script output is often one long JSON line.\n\t\t\t\tcomponent.addChild(\n\t\t\t\t\tnew VisualLinePreview({\n\t\t\t\t\t\ttext: styled,\n\t\t\t\t\t\tmaxVisualLines: OUTPUT_PREVIEW_LINES,\n\t\t\t\t\t\tkeep: \"start\",\n\t\t\t\t\t\tformatHint: (hidden) => expandHint(theme, hidden, \"lines\"),\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t\t// The collapsed preview hides the truncation notice at the end, so name the file here.\n\t\t\t\tconst fullOutputPath = result.details?.fullOutputPath;\n\t\t\t\tif (fullOutputPath) component.addChild(new Text(theme.fg(\"muted\", `Full output: ${fullOutputPath}`), 0, 0));\n\t\t\t}\n\t\t}\n\t\treturn component;\n\t},\n};\n"]}
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The `codemode` tool: the model writes JavaScript that calls other tools. Scripts use `tools`,
3
3
  * `ALL_TOOLS`, `text()`, `image()`, `exit()`, `store()`/`load()`, `console.*`, and `return <value>`,
4
- * may start with a `// @options:` line, and reach the model catalog and classifiers through
5
- * `models.*`. Results start with a "Script completed" or "Script failed" header.
4
+ * may start with a `// @options:` line, and reach the model catalog, classifiers, and image models
5
+ * through `models.*`. Results start with a "Script completed" or "Script failed" header.
6
6
  *
7
7
  * Scripts can call the agent loop's nested tools: active `direct` tools and every `codemode` or
8
8
  * `deferred` tool. Nested calls run through the agent loop's tool pipeline (`ctx.executeTool`), so
@@ -34,7 +34,7 @@ export interface CodemodeStoreEntryData {
34
34
  delete: string[];
35
35
  }
36
36
  /** The part of the model registry that scripts reach through `models`. */
37
- export type CodemodeModelRuntime = Pick<ModelRegistry, "getModelsOfType" | "getAvailableOfType" | "getModelOfType" | "classify">;
37
+ export type CodemodeModelRuntime = Pick<ModelRegistry, "getModelsOfType" | "getAvailableOfType" | "getModelOfType" | "classify" | "generateImages">;
38
38
  export interface CodemodeToolOptions {
39
39
  /** Namespace of a tool, for `searchTools()` ranking and its `namespace` filter. */
40
40
  getToolNamespace?: (toolName: string) => ToolNamespace | undefined;
@@ -82,11 +82,11 @@ export interface CodemodeToolDetails {
82
82
  fullOutputPath?: string;
83
83
  }
84
84
  export declare const codemodeToolSystemPromptContribution: {
85
- readonly snippet: "Run JavaScript that calls other tools (chains, loops, Promise.all, filtering large results)";
86
- readonly guidelines: readonly ["Use codemode to batch or chain several tool calls, or to filter large tool output down to what you need, instead of issuing many individual tool calls. Batch independent calls in one codemode call using await Promise.allSettled([...])."];
85
+ readonly snippet: "Run JavaScript that calls other tools";
86
+ readonly guidelines: readonly ["Use codemode to batch independent tool calls (Promise.allSettled), chain them, or filter large output, instead of many separate calls."];
87
87
  };
88
- /** Declarations of the `models` globals; codemode-execute.ts implements them. */
89
- export declare const MODEL_GLOBAL_DECLARATIONS: readonly Omit<CodemodeTool, "execute">[];
88
+ /** The reference for scripts: globals, tool results, `store()`, the `models` API, and limits. */
89
+ export declare const CODEMODE_DOCS_PATH: string;
90
90
  /** Default for {@link CodemodeDescriptionOptions.inlineBudget}, in estimated tokens. */
91
91
  export declare const DEFAULT_CODEMODE_INLINE_BUDGET = 3000;
92
92
  /** What a script sees of a tool. Tools without an output schema resolve to their text output. */
@@ -107,10 +107,11 @@ export interface CodemodeDescriptionOptions {
107
107
  inlineBudget?: number;
108
108
  }
109
109
  /**
110
- * Model-facing description: the helper list, guidance for omitted tools, the shared MCP types when
111
- * MCP tools are callable, the `models` API, and one section per tool, grouped by namespace. Tool
112
- * sections are limited to `inlineBudget`; every namespace is listed with its tool count either
113
- * way, and the listing states whether it is complete.
110
+ * Model-facing description: the helper list, guidance for finding tools that are not listed, the
111
+ * shared MCP types when listed tools need them, the `models` API, and one section per listed tool,
112
+ * grouped by namespace. Deferred tools are never listed and do not affect the description at all, so
113
+ * it stays the same while MCP servers connect or change their tools. Tool sections are limited to
114
+ * `inlineBudget`.
114
115
  */
115
116
  export declare function createCodemodeDescription(tools: readonly AgentTool<any>[], options?: CodemodeDescriptionOptions): string;
116
117
  export declare function createCodemodeToolDefinition(options?: CodemodeToolOptions): ToolDefinition<typeof codemodeSchema, CodemodeToolDetails | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/extensions/codemode/tool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,EAAsB,YAAY,EAAE,MAAM,6BAA6B,CAAC;AASpF,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EACX,cAAc,EACd,QAAQ,EAGR,aAAa,EACb,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAKnE,eAAO,MAAM,kBAAkB,aAAa,CAAC;AAE7C,+FAA+F;AAC/F,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,MAAM,WAAW,sBAAsB;IACtC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7B,MAAM,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,0EAA0E;AAC1E,MAAM,MAAM,oBAAoB,GAAG,IAAI,CACtC,aAAa,EACb,iBAAiB,GAAG,oBAAoB,GAAG,gBAAgB,GAAG,UAAU,CACxE,CAAC;AAEF,MAAM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,CAAC;IACjB,iGAAiG;IACjG,UAAU,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAChD,6FAA6F;IAC7F,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC/B;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AA6CD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACxC,KAAK,EAAE,SAAS,SAAS,CAAC,GAAG,CAAC,EAAE,EAChC,OAAO,GAAE,0BAA+B,GACtC,MAAM,CA+CR;AA8ED,wBAAgB,4BAA4B,CAC3C,OAAO,GAAE,mBAAwB,GAC/B,cAAc,CAAC,OAAO,cAAc,EAAE,mBAAmB,GAAG,SAAS,CAAC,CAmBxE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,GAAE,SAAS,SAAS,CAAC,GAAG,CAAC,EAAO,EACrC,OAAO,GAAE,mBAAwB,GAC/B,SAAS,CAAC,OAAO,cAAc,CAAC,CASlC"}
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The `codemode` tool: the model writes JavaScript that calls other tools. Scripts use `tools`,
3
3
  * `ALL_TOOLS`, `text()`, `image()`, `exit()`, `store()`/`load()`, `console.*`, and `return <value>`,
4
- * may start with a `// @options:` line, and reach the model catalog and classifiers through
5
- * `models.*`. Results start with a "Script completed" or "Script failed" header.
4
+ * may start with a `// @options:` line, and reach the model catalog, classifiers, and image models
5
+ * through `models.*`. Results start with a "Script completed" or "Script failed" header.
6
6
  *
7
7
  * Scripts can call the agent loop's nested tools: active `direct` tools and every `codemode` or
8
8
  * `deferred` tool. Nested calls run through the agent loop's tool pipeline (`ctx.executeTool`), so
@@ -20,9 +20,11 @@
20
20
  * calls; successful scripts append their writes to the session as `codemode-store` custom entries,
21
21
  * so each branch sees the values written on its own path.
22
22
  */
23
- import { MCP_TYPESCRIPT_PREAMBLE, mcpStructuredContentSchema, renderDeclarations, renderToolSample, toCodemodeIdentifier, } from "@earendil-works/pi-codemode/declarations";
23
+ import { join } from "node:path";
24
+ import { MCP_TYPESCRIPT_PREAMBLE, mcpStructuredContentSchema, renderToolOutputType, renderToolSample, toCodemodeIdentifier, } from "@earendil-works/pi-codemode/declarations";
24
25
  import { CODEMODE_SOURCE_GRAMMAR } from "@earendil-works/pi-codemode/source";
25
26
  import { Type } from "typebox";
27
+ import { getDocsPath } from "../../config.js";
26
28
  import { wrapToolDefinition } from "../../core/tools/tool-definition-wrapper.js";
27
29
  import { loadCodemodeExecutor } from "./execute.lazy.js";
28
30
  import { codemodeRenderers } from "./renderer.js";
@@ -32,7 +34,7 @@ export const CODEMODE_STORE_ENTRY_TYPE = "codemode-store";
32
34
  const TEXT_OUTPUT_SCHEMA = { type: "string" };
33
35
  export const codemodeSchema = Type.Object({
34
36
  code: Type.String({
35
- description: 'Raw JavaScript source. Top-level await and return work. May start with a `// @options: {"max_output_tokens": 1000}` line.',
37
+ description: "Raw JavaScript source.",
36
38
  }),
37
39
  });
38
40
  /**
@@ -43,97 +45,29 @@ export function isCodemodeTool(tool) {
43
45
  return tool.name === CODEMODE_TOOL_NAME && tool.parameters === codemodeSchema;
44
46
  }
45
47
  export const codemodeToolSystemPromptContribution = {
46
- snippet: "Run JavaScript that calls other tools (chains, loops, Promise.all, filtering large results)",
48
+ snippet: "Run JavaScript that calls other tools",
47
49
  guidelines: [
48
- "Use codemode to batch or chain several tool calls, or to filter large tool output down to what you need, instead of issuing many individual tool calls. Batch independent calls in one codemode call using await Promise.allSettled([...]).",
50
+ "Use codemode to batch independent tool calls (Promise.allSettled), chain them, or filter large output, instead of many separate calls.",
49
51
  ],
50
52
  };
51
- const DESCRIPTION_INTRO = `Run JavaScript code to orchestrate/compose tool calls
52
- - Evaluates the provided JavaScript code in a fresh QuickJS sandbox as the body of an async function: top-level \`await\` and \`return\` work.
53
- - All nested tools are available on the global \`tools\` object, for example \`await tools.read(...)\`. Tool names are exposed as normalized JavaScript identifiers, for example \`await tools.mcp__ologs__get_profile(...)\`.
54
- - Nested tool methods take an object as their input argument.
55
- - Nested tools return either an object or a string, based on the description.
56
- - A nested tool call that fails, is blocked, or gets invalid arguments rejects with an Error carrying the tool's error text.
57
- - Runs raw JavaScript -- no Node, no file system, no network access, no timers.
58
- - Accepts raw JavaScript source text, not JSON, quoted strings, or markdown code fences.
59
- - You may optionally start the tool input with a first line like \`// @options: {"max_output_tokens": 1000, "timeout_ms": 60000}\`.
60
- - \`max_output_tokens\` sets the token budget for the script's output. Defaults to 10000 tokens.
61
- - \`timeout_ms\` sets a hard deadline for the whole script. By default there is none.
62
- - When the JS code is fully evaluated, calls that are still running are cancelled and unawaited promises are silently discarded.
63
- - Tool calls are real and have side effects. If the script fails partway, earlier calls are not undone.
64
- - Scripts have a 256 MB memory limit; exceeding it throws \`InternalError: out of memory\`. Filter or aggregate large data instead of accumulating it.
65
-
66
- - Global helpers:
67
- - \`exit()\`: Immediately ends the current script successfully (like an early return from the top level).
68
- - \`text(value: string | number | boolean | undefined | null)\`: Appends a text item. Non-string values are stringified with \`JSON.stringify(...)\` when possible.
69
- - \`image(imageUrlOrItem: string | { image_url: string } | ImageContent)\`: Appends an image item. \`image_url\` should be a base64-encoded \`data:\` URL. To forward an MCP tool image, pass an individual \`ImageContent\` block from \`result.content\`, for example \`image(result.content[0])\`.
70
- - \`store(key: string, value: any)\`: stores a serializable value under a string key for later \`codemode\` calls in the same session. Storing \`undefined\` deletes the key. Writes are kept only if the script succeeds.
71
- - \`load(key: string)\`: returns the stored value for a string key, or \`undefined\` if it is missing.
72
- - \`ALL_TOOLS\`: metadata for the enabled nested tools as \`{ name, description }\` entries.
73
- - \`searchTools(query: string, options?: { limit?: number; namespace?: string })\`: resolves to the nested tools that best match the query (BM25, default limit 8), as \`{ name, description }\` entries like \`ALL_TOOLS\`.
74
- - \`describeTool(name: string)\`: resolves to the description and declaration of a nested tool, or \`undefined\`.
75
- - \`console.log(...)\` and the other \`console\` methods append a text item like \`text()\`.
76
- - \`return value\` at the top level appends the value like \`text()\`.`;
77
- const MODEL_TYPES = `type ModelType = "chat" | "image" | "classifier";
78
- /** A model catalog entry. \`provider\` and \`id\` identify it; the other fields depend on the type. */
79
- interface ModelInfo {
80
- type?: ModelType;
81
- provider: string;
82
- id: string;
83
- name: string;
84
- api: string;
85
- input: ("text" | "image")[];
86
- contextWindow?: number;
87
- [key: string]: unknown;
88
- }
89
- type ClassifierQuestion =
90
- | { type: "choice"; instructions: string; criteria: Record<string, string> }
91
- | { type: "score"; instructions: string; criteria: string[] }
92
- | { type: "bool"; instructions: string; criteria: { true: string; false: string } };
93
- type ClassifierAnswer =
94
- | { type: "choice"; choice: string; probabilities: Record<string, number>; confidence: number }
95
- | { type: "score"; score: number; confidence: number }
96
- | { type: "bool"; probability: number };
97
- interface ClassifierContext {
98
- state: Record<string, unknown>;
99
- questions: Record<string, ClassifierQuestion>;
53
+ /** The reference for scripts: globals, tool results, `store()`, the `models` API, and limits. */
54
+ export const CODEMODE_DOCS_PATH = join(getDocsPath(), "codemode.md");
55
+ const DESCRIPTION_INTRO = `Run JavaScript that calls other tools. The input is raw JavaScript (not JSON, no code fence), run as an async function body in a QuickJS sandbox: top-level \`await\` and \`return\` work. No Node, file system, network, or timers.
56
+ - \`await tools.<name>({ ...args })\` resolves to a string, or an object if the tool's declaration says so, and rejects with an Error on failure. Calls still running when the script ends are cancelled.
57
+ - Optional first line: \`// @options: {"max_output_tokens": 10000, "timeout_ms": 60000}\``;
58
+ /** One line per global. The details live in {@link CODEMODE_DOCS_PATH}. */
59
+ function describeGlobals(models) {
60
+ const lines = [
61
+ "Globals:",
62
+ "- `text(value)`, `image(dataUrlOrImageBlock)`, `console.log(...)`, and top-level `return` add output; `exit()` ends the script.",
63
+ "- `store(key, value)` and `load(key)` keep JSON values across codemode calls.",
64
+ "- `ALL_TOOLS`, `searchTools(query, { limit?, namespace? })`, `describeTool(name)`, `describeNamespace(name)`: find unlisted tools, such as MCP tools.",
65
+ ];
66
+ if (models) {
67
+ lines.push(`- \`models\`: classifiers and image generation. Read ${CODEMODE_DOCS_PATH} first.`);
68
+ }
69
+ return lines.join("\n");
100
70
  }
101
- interface ClassifierResult {
102
- api: string;
103
- provider: string;
104
- model: string;
105
- answers: Record<string, ClassifierAnswer>;
106
- /** Set when the service reports token counts. Cost is in USD. */
107
- usage?: { input: number; output: number; totalTokens: number; cost: { total: number } };
108
- stopReason: "stop" | "error" | "aborted";
109
- errorMessage?: string;
110
- timestamp: number;
111
- }`;
112
- /** Declarations of the `models` globals; codemode-execute.ts implements them. */
113
- export const MODEL_GLOBAL_DECLARATIONS = [
114
- {
115
- name: "models.getModelsOfType",
116
- description: "Every known model of a type, optionally for one provider.",
117
- signature: "(type: ModelType, provider?: string): Promise<ModelInfo[]>",
118
- },
119
- {
120
- name: "models.getAvailableOfType",
121
- description: "Models of a type whose provider has working credentials.",
122
- signature: "(type: ModelType, provider?: string): Promise<ModelInfo[]>",
123
- },
124
- {
125
- name: "models.getModelOfType",
126
- description: "One catalog entry, or undefined.",
127
- signature: "(type: ModelType, provider: string, id: string): Promise<ModelInfo | undefined>",
128
- },
129
- {
130
- name: "models.classify",
131
- description: "Run a classifier model on one state. Only `provider` and `id` of `model` are used. Provider errors do not throw: check `stopReason` and `errorMessage`.",
132
- signature: "(model: ModelInfo, context: ClassifierContext): Promise<ClassifierResult>",
133
- },
134
- ];
135
- const DEFERRED_TOOLS_GUIDANCE = `Some deferred nested tools may be omitted from this description. They are still available on the global \`tools\` object and listed in \`ALL_TOOLS\`.
136
- To find one, call \`await searchTools(query)\`, or filter \`ALL_TOOLS\` by \`name\` and \`description\`.`;
137
71
  /** Default for {@link CodemodeDescriptionOptions.inlineBudget}, in estimated tokens. */
138
72
  export const DEFAULT_CODEMODE_INLINE_BUDGET = 3000;
139
73
  /** Characters per token when estimating the cost of a tool section. */
@@ -164,10 +98,9 @@ function renderToolSection(declaration) {
164
98
  * represented before any namespace is complete.
165
99
  */
166
100
  function selectCatalog(groups, budget) {
167
- const listable = groups.map((group) => group.entries.filter((entry) => !entry.deferred));
168
101
  if (budget === undefined)
169
- return new Set(listable.flat().map((entry) => entry.name));
170
- const queues = listable.map((entries) => [...entries].sort((a, b) => a.cost - b.cost));
102
+ return new Set(groups.flatMap((group) => group.entries.map((entry) => entry.name)));
103
+ const queues = groups.map((group) => [...group.entries].sort((a, b) => a.cost - b.cost));
171
104
  const shown = new Set();
172
105
  let remaining = budget;
173
106
  let active = queues.filter((queue) => queue.length > 0);
@@ -185,13 +118,16 @@ function selectCatalog(groups, budget) {
185
118
  return shown;
186
119
  }
187
120
  /**
188
- * Model-facing description: the helper list, guidance for omitted tools, the shared MCP types when
189
- * MCP tools are callable, the `models` API, and one section per tool, grouped by namespace. Tool
190
- * sections are limited to `inlineBudget`; every namespace is listed with its tool count either
191
- * way, and the listing states whether it is complete.
121
+ * Model-facing description: the helper list, guidance for finding tools that are not listed, the
122
+ * shared MCP types when listed tools need them, the `models` API, and one section per listed tool,
123
+ * grouped by namespace. Deferred tools are never listed and do not affect the description at all, so
124
+ * it stays the same while MCP servers connect or change their tools. Tool sections are limited to
125
+ * `inlineBudget`.
192
126
  */
193
127
  export function createCodemodeDescription(tools, options = {}) {
194
- const declarations = getCodemodeCallableTools(tools).map(toCodemodeDeclaration);
128
+ const declarations = getCodemodeCallableTools(tools)
129
+ .filter((tool) => !options.deferred?.has(tool.name))
130
+ .map(toCodemodeDeclaration);
195
131
  const groups = new Map([["", { namespace: undefined, entries: [] }]]);
196
132
  for (const declaration of declarations) {
197
133
  const namespace = options.namespaces?.get(declaration.name);
@@ -199,47 +135,28 @@ export function createCodemodeDescription(tools, options = {}) {
199
135
  const group = groups.get(key) ?? { namespace, entries: [] };
200
136
  groups.set(key, group);
201
137
  const section = renderToolSection(declaration);
202
- group.entries.push({
203
- name: declaration.name,
204
- section,
205
- cost: Math.ceil(section.length / CHARS_PER_TOKEN),
206
- deferred: options.deferred?.has(declaration.name) === true,
207
- });
138
+ group.entries.push({ name: declaration.name, section, cost: Math.ceil(section.length / CHARS_PER_TOKEN) });
208
139
  }
209
140
  const ordered = [...groups.values()].sort((a, b) => a.namespace === undefined ? -1 : b.namespace === undefined ? 1 : a.namespace.name.localeCompare(b.namespace.name));
210
141
  const shown = selectCatalog(ordered, options.inlineBudget);
211
- const complete = shown.size === declarations.length;
212
- const sections = [DESCRIPTION_INTRO];
213
- if (!complete)
214
- sections.push(DEFERRED_TOOLS_GUIDANCE);
215
- if (declarations.some((declaration) => mcpStructuredContentSchema(declaration.outputSchema) !== undefined)) {
142
+ const sections = [DESCRIPTION_INTRO, describeGlobals(options.models === true)];
143
+ if (declarations.some((declaration) => shown.has(declaration.name) && mcpStructuredContentSchema(declaration.outputSchema) !== undefined)) {
216
144
  sections.push(`Shared MCP Types:\n\`\`\`ts\n${MCP_TYPESCRIPT_PREAMBLE}\n\`\`\``);
217
145
  }
218
- if (options.models) {
219
- const noop = () => undefined;
220
- const models = renderDeclarations({
221
- globals: MODEL_GLOBAL_DECLARATIONS.map((global) => ({ ...global, execute: noop })),
222
- });
223
- sections.push(`Model API:\n\`\`\`ts\n${MODEL_TYPES}\n\n${models}\n\`\`\``);
224
- }
225
146
  if (declarations.length === 0)
226
147
  return sections.join("\n\n");
227
- const toolSections = [
228
- complete
229
- ? `Nested tools: COMPLETE list (${declarations.length} tool${declarations.length === 1 ? "" : "s"}).`
230
- : `Nested tools: PARTIAL - ${shown.size} of ${declarations.length} shown.`,
231
- ];
148
+ const toolSections = ["Nested tools:"];
232
149
  for (const { namespace, entries } of ordered) {
233
150
  const visible = entries.filter((entry) => shown.has(entry.name));
234
151
  if (namespace) {
235
- const count = `${entries.length} tool${entries.length === 1 ? "" : "s"}`;
236
- const suffix = visible.length === entries.length
152
+ // Only tools that did not fit the budget are counted as not listed here.
153
+ const listing = visible.length === entries.length
237
154
  ? ""
238
155
  : visible.length === 0
239
- ? ", none shown"
240
- : `, ${visible.length} shown`;
156
+ ? " (tools not listed)"
157
+ : " (some tools not listed)";
241
158
  const description = namespace.description?.trim();
242
- toolSections.push(`## ${namespace.name} (${count}${suffix})${description ? `\n${description}` : ""}`);
159
+ toolSections.push(`## ${namespace.name}${listing}${description ? `\n${description}` : ""}`);
243
160
  }
244
161
  for (const entry of visible)
245
162
  toolSections.push(entry.section);
@@ -247,9 +164,36 @@ export function createCodemodeDescription(tools, options = {}) {
247
164
  sections.push(toolSections.join("\n\n"));
248
165
  return sections.join("\n\n");
249
166
  }
167
+ /**
168
+ * What a script call resolves to, in one line: `a string`, the field names of an object
169
+ * (`{ output, exit_code, full_output_path? }`), or the rendered type for anything else.
170
+ */
171
+ function describeOutput(schema) {
172
+ const type = renderToolOutputType(schema);
173
+ if (type === "string")
174
+ return "a string";
175
+ const object = typeof schema === "object" ? schema : undefined;
176
+ const properties = object?.properties;
177
+ if (object?.type === "object" &&
178
+ typeof properties === "object" &&
179
+ properties !== null &&
180
+ mcpStructuredContentSchema(schema) === undefined) {
181
+ const required = new Set(Array.isArray(object.required) ? object.required : []);
182
+ const fields = Object.keys(properties).map((name) => (required.has(name) ? name : `${name}?`));
183
+ return `\`{ ${fields.join(", ")} }\``;
184
+ }
185
+ return `\`${type.replace(/\s+/g, " ")}\``;
186
+ }
187
+ /**
188
+ * A declared tool's description followed by how scripts call it and what the call resolves to. The
189
+ * arguments are the tool's declared parameters, so they are not repeated.
190
+ */
191
+ function describeScriptCall(tool) {
192
+ return `${tool.description.trim()}\n\nCodemode: \`tools.${toCodemodeIdentifier(tool.name)}(args)\` resolves to ${describeOutput(toCodemodeDeclaration(tool).outputSchema)}.`;
193
+ }
250
194
  /**
251
195
  * How the codemode tool presents tools that are both declared and callable from scripts:
252
- * - `on`: their descriptions get the codemode declaration appended, and the codemode description
196
+ * - `on`: their descriptions say how scripts call them, and the codemode description
253
197
  * lists only the callable tools without `direct` exposure.
254
198
  * - `only`: the codemode description lists every callable tool, and requests leave out the
255
199
  * declarations of active `direct` tools.
@@ -266,7 +210,7 @@ function prepareCodemodeLoadout(loadout, options) {
266
210
  if (mode === "on") {
267
211
  for (const tool of loadout.declared) {
268
212
  if (callableNames.has(tool.name))
269
- descriptions[tool.name] = renderToolSample(toCodemodeDeclaration(tool));
213
+ descriptions[tool.name] = describeScriptCall(tool);
270
214
  }
271
215
  }
272
216
  const listed = mode === "only" ? callable : callable.filter((tool) => !isDirect(tool));