@korallis/k-pi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/CHANGELOG.md +5619 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +66 -0
  4. package/README.md +723 -0
  5. package/dist/bundle/chunks/anthropic-messages-IBCPCIJO.js +45 -0
  6. package/dist/bundle/chunks/anthropic.js +75 -0
  7. package/dist/bundle/chunks/azure-openai-responses-GUUG57CZ.js +2 -0
  8. package/dist/bundle/chunks/bedrock-converse-stream.js +74 -0
  9. package/dist/bundle/chunks/chunk-4L3WN2XY.js +2 -0
  10. package/dist/bundle/chunks/chunk-6CYCI3EJ.js +12 -0
  11. package/dist/bundle/chunks/chunk-6R7UI4IY.js +2 -0
  12. package/dist/bundle/chunks/chunk-AXIIZGTV.js +2 -0
  13. package/dist/bundle/chunks/chunk-CO7LIZG5.js +2 -0
  14. package/dist/bundle/chunks/chunk-DRQDKNXA.js +2 -0
  15. package/dist/bundle/chunks/chunk-GMWCTUPB.js +2 -0
  16. package/dist/bundle/chunks/chunk-HOFU6X66.js +50 -0
  17. package/dist/bundle/chunks/chunk-JUUC5AZY.js +2 -0
  18. package/dist/bundle/chunks/chunk-L2ZPNLYD.js +2 -0
  19. package/dist/bundle/chunks/chunk-MNAIPA3J.js +2 -0
  20. package/dist/bundle/chunks/chunk-NBBFIJUL.js +11 -0
  21. package/dist/bundle/chunks/chunk-NUHFSC37.js +25 -0
  22. package/dist/bundle/chunks/chunk-PGXDEOVV.js +2 -0
  23. package/dist/bundle/chunks/chunk-PJACZCIR.js +2 -0
  24. package/dist/bundle/chunks/chunk-PRRNXB7S.js +2 -0
  25. package/dist/bundle/chunks/chunk-TEPMHNKQ.js +2 -0
  26. package/dist/bundle/chunks/chunk-TRDNDS6A.js +4 -0
  27. package/dist/bundle/chunks/chunk-UAQELI3K.js +2 -0
  28. package/dist/bundle/chunks/chunk-X5QHCKY4.js +1653 -0
  29. package/dist/bundle/chunks/chunk-XDUWOHPL.js +6 -0
  30. package/dist/bundle/chunks/github-copilot.js +2 -0
  31. package/dist/bundle/chunks/google-generative-ai-YRXF7C4I.js +2 -0
  32. package/dist/bundle/chunks/google-vertex-BVZ6VBRQ.js +2 -0
  33. package/dist/bundle/chunks/https-proxy-agent-2VXB7436.js +13 -0
  34. package/dist/bundle/chunks/image-resize-worker.js +2 -0
  35. package/dist/bundle/chunks/kimi-coding.js +2 -0
  36. package/dist/bundle/chunks/lib-HGQMNYVT.js +9 -0
  37. package/dist/bundle/chunks/mistral-conversations-Q3AWZJAZ.js +5 -0
  38. package/dist/bundle/chunks/multipart-parser-SQ6GDJL7.js +3 -0
  39. package/dist/bundle/chunks/openai-codex-responses-GJVBJXLB.js +8 -0
  40. package/dist/bundle/chunks/openai-codex.js +75 -0
  41. package/dist/bundle/chunks/openai-completions-OICQQ5JB.js +7 -0
  42. package/dist/bundle/chunks/openai-responses-P5LIZTXX.js +2 -0
  43. package/dist/bundle/chunks/openrouter-images-XTKS2AS3.js +2 -0
  44. package/dist/bundle/chunks/openrouter.js +75 -0
  45. package/dist/bundle/chunks/pi-messages-TPFH44NP.js +8 -0
  46. package/dist/bundle/chunks/radius.js +75 -0
  47. package/dist/bundle/chunks/src-UK77SYZE.js +4 -0
  48. package/dist/bundle/chunks/xai.js +2 -0
  49. package/dist/bundle/cli.js +3 -0
  50. package/dist/bundle/client.js +2 -0
  51. package/dist/bundle/index.js +2 -0
  52. package/dist/bundle/rpc-entry.js +3 -0
  53. package/dist/core/export-html/template.css +1066 -0
  54. package/dist/core/export-html/template.html +55 -0
  55. package/dist/core/export-html/template.js +1864 -0
  56. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  57. package/dist/core/export-html/vendor/marked.min.js +78 -0
  58. package/dist/kpi/graphs/coding-loop.auto.json +377 -0
  59. package/dist/kpi/graphs/coding-loop.gated.json +417 -0
  60. package/dist/kpi/graphs/spec-first.json +40 -0
  61. package/dist/kpi/kstack/NOTICE +5 -0
  62. package/dist/kpi/kstack/UPSTREAM.md +23 -0
  63. package/dist/kpi/kstack/generated/LICENSE +21 -0
  64. package/dist/kpi/kstack/generated/skills/architect/SKILL.md +83 -0
  65. package/dist/kpi/kstack/generated/skills/architect/references/design-red-flags.md +33 -0
  66. package/dist/kpi/kstack/generated/skills/architect/references/rationale-template.md +35 -0
  67. package/dist/kpi/kstack/generated/skills/architect/references/runner-prompt.md +20 -0
  68. package/dist/kpi/kstack/generated/skills/arena/SKILL.md +29 -0
  69. package/dist/kpi/kstack/generated/skills/blast-radius/SKILL.md +50 -0
  70. package/dist/kpi/kstack/generated/skills/bro/SKILL.md +7 -0
  71. package/dist/kpi/kstack/generated/skills/create-verification-skill/SKILL.md +44 -0
  72. package/dist/kpi/kstack/generated/skills/create-verification-skill/references/feature-map-example/README.md +47 -0
  73. package/dist/kpi/kstack/generated/skills/create-verification-skill/references/feature-map-example/create-note.md +39 -0
  74. package/dist/kpi/kstack/generated/skills/create-verification-skill/references/feature-map-example/search.md +45 -0
  75. package/dist/kpi/kstack/generated/skills/figure-it-out/SKILL.md +55 -0
  76. package/dist/kpi/kstack/generated/skills/how/SKILL.md +135 -0
  77. package/dist/kpi/kstack/generated/skills/how/references/critic-prompt.md +59 -0
  78. package/dist/kpi/kstack/generated/skills/how/references/critique-rubric.md +58 -0
  79. package/dist/kpi/kstack/generated/skills/how/references/explainer-prompt.md +55 -0
  80. package/dist/kpi/kstack/generated/skills/how/references/explorer-prompt.md +52 -0
  81. package/dist/kpi/kstack/generated/skills/interrogate/SKILL.md +113 -0
  82. package/dist/kpi/kstack/generated/skills/interrogate/references/code-quality-review.md +47 -0
  83. package/dist/kpi/kstack/generated/skills/interrogate/references/lead-judgment.md +58 -0
  84. package/dist/kpi/kstack/generated/skills/interrogate/references/reviewer-prompt.md +72 -0
  85. package/dist/kpi/kstack/generated/skills/interrogate/references/rubric.md +77 -0
  86. package/dist/kpi/kstack/generated/skills/k-agent/SKILL.md +17 -0
  87. package/dist/kpi/kstack/generated/skills/k-mode/SKILL.md +64 -0
  88. package/dist/kpi/kstack/generated/skills/k-stack-principles/SKILL.md +24 -0
  89. package/dist/kpi/kstack/generated/skills/maintain-verification-skill/SKILL.md +39 -0
  90. package/dist/kpi/kstack/generated/skills/no-comments/SKILL.md +24 -0
  91. package/dist/kpi/kstack/generated/skills/playbook-arena/SKILL.md +20 -0
  92. package/dist/kpi/kstack/generated/skills/playbook-autonomous-run/SKILL.md +22 -0
  93. package/dist/kpi/kstack/generated/skills/playbook-autopilot-full/SKILL.md +20 -0
  94. package/dist/kpi/kstack/generated/skills/playbook-autopilot-stack/SKILL.md +20 -0
  95. package/dist/kpi/kstack/generated/skills/playbook-bug-fix/SKILL.md +21 -0
  96. package/dist/kpi/kstack/generated/skills/playbook-feature/SKILL.md +22 -0
  97. package/dist/kpi/kstack/generated/skills/playbook-investigation/SKILL.md +21 -0
  98. package/dist/kpi/kstack/generated/skills/playbook-shipping/SKILL.md +18 -0
  99. package/dist/kpi/kstack/generated/skills/playbook-swarm/SKILL.md +20 -0
  100. package/dist/kpi/kstack/generated/skills/principle-boundary-discipline/SKILL.md +34 -0
  101. package/dist/kpi/kstack/generated/skills/principle-build-the-lever/SKILL.md +23 -0
  102. package/dist/kpi/kstack/generated/skills/principle-encode-lessons-in-structure/SKILL.md +31 -0
  103. package/dist/kpi/kstack/generated/skills/principle-exhaust-the-design-space/SKILL.md +21 -0
  104. package/dist/kpi/kstack/generated/skills/principle-experience-first/SKILL.md +19 -0
  105. package/dist/kpi/kstack/generated/skills/principle-fix-root-causes/SKILL.md +23 -0
  106. package/dist/kpi/kstack/generated/skills/principle-foundational-thinking/SKILL.md +21 -0
  107. package/dist/kpi/kstack/generated/skills/principle-guard-the-context-window/SKILL.md +17 -0
  108. package/dist/kpi/kstack/generated/skills/principle-laziness-protocol/SKILL.md +18 -0
  109. package/dist/kpi/kstack/generated/skills/principle-make-operations-idempotent/SKILL.md +24 -0
  110. package/dist/kpi/kstack/generated/skills/principle-migrate-callers-then-delete-legacy-apis/SKILL.md +22 -0
  111. package/dist/kpi/kstack/generated/skills/principle-minimize-reader-load/SKILL.md +23 -0
  112. package/dist/kpi/kstack/generated/skills/principle-model-the-domain/SKILL.md +26 -0
  113. package/dist/kpi/kstack/generated/skills/principle-never-block-on-the-human/SKILL.md +23 -0
  114. package/dist/kpi/kstack/generated/skills/principle-outcome-oriented-execution/SKILL.md +22 -0
  115. package/dist/kpi/kstack/generated/skills/principle-prove-it-works/SKILL.md +33 -0
  116. package/dist/kpi/kstack/generated/skills/principle-redesign-from-first-principles/SKILL.md +16 -0
  117. package/dist/kpi/kstack/generated/skills/principle-separate-before-serializing-shared-state/SKILL.md +16 -0
  118. package/dist/kpi/kstack/generated/skills/principle-sequence-verifiable-units/SKILL.md +22 -0
  119. package/dist/kpi/kstack/generated/skills/principle-subtract-before-you-add/SKILL.md +22 -0
  120. package/dist/kpi/kstack/generated/skills/principle-type-system-discipline/SKILL.md +31 -0
  121. package/dist/kpi/kstack/generated/skills/recall/SKILL.md +35 -0
  122. package/dist/kpi/kstack/generated/skills/reflect/SKILL.md +77 -0
  123. package/dist/kpi/kstack/generated/skills/reflect/references/divergent-reviewer.md +43 -0
  124. package/dist/kpi/kstack/generated/skills/reflect/references/judgment-reviewer.md +42 -0
  125. package/dist/kpi/kstack/generated/skills/reflect/references/synthesizer.md +56 -0
  126. package/dist/kpi/kstack/generated/skills/reflect/references/tooling-reviewer.md +57 -0
  127. package/dist/kpi/kstack/generated/skills/setup-kstack/SKILL.md +48 -0
  128. package/dist/kpi/kstack/generated/skills/show-me-your-work/SKILL.md +82 -0
  129. package/dist/kpi/kstack/generated/skills/show-me-your-work/references/decision-log-template.tsv +1 -0
  130. package/dist/kpi/kstack/generated/skills/show-me-your-work/scripts/log.sh +40 -0
  131. package/dist/kpi/kstack/generated/skills/swarm/SKILL.md +27 -0
  132. package/dist/kpi/kstack/generated/skills/tdd/SKILL.md +44 -0
  133. package/dist/kpi/kstack/generated/skills/teach/SKILL.md +21 -0
  134. package/dist/kpi/kstack/generated/skills/technical-writing/SKILL.md +130 -0
  135. package/dist/kpi/kstack/generated/skills/typescript-best-practices/SKILL.md +30 -0
  136. package/dist/kpi/kstack/generated/skills/typescript-best-practices/references/patterns.md +313 -0
  137. package/dist/kpi/kstack/generated/skills/unslop/SKILL.md +81 -0
  138. package/dist/kpi/kstack/generated/skills/why/SKILL.md +230 -0
  139. package/dist/kpi/kstack/generated/skills/why/references/epistemics.md +144 -0
  140. package/dist/kpi/kstack/generated/skills/why/references/investigator-prompt.md +103 -0
  141. package/dist/kpi/kstack/generated/skills/why/references/source-playbook.md +17 -0
  142. package/dist/kpi/kstack/generated/skills/why/references/sources/code-archaeology.md +88 -0
  143. package/dist/kpi/kstack/generated/skills/why/references/sources/databricks.md +70 -0
  144. package/dist/kpi/kstack/generated/skills/why/references/sources/datadog.md +99 -0
  145. package/dist/kpi/kstack/generated/skills/why/references/sources/incident-postmortem.md +15 -0
  146. package/dist/kpi/kstack/generated/skills/why/references/sources/linear.md +48 -0
  147. package/dist/kpi/kstack/generated/skills/why/references/sources/notion.md +55 -0
  148. package/dist/kpi/kstack/generated/skills/why/references/sources/sentry.md +100 -0
  149. package/dist/kpi/kstack/generated/skills/why/references/sources/slack.md +54 -0
  150. package/dist/kpi/kstack/generated/skills/why/references/synthesizer-prompt.md +135 -0
  151. package/dist/kpi/kstack/model-ladder.md +50 -0
  152. package/dist/kpi/kstack/provenance.json +17 -0
  153. package/dist/kpi/prompts/implement.md +6 -0
  154. package/dist/kpi/prompts/plan.md +6 -0
  155. package/dist/kpi/prompts/review.md +6 -0
  156. package/dist/kpi/prompts/ship.md +6 -0
  157. package/dist/kpi/prompts/specify.md +6 -0
  158. package/dist/kpi/prompts/verify.md +6 -0
  159. package/dist/kpi/schemas/event.schema.json +1314 -0
  160. package/dist/kpi/schemas/evidence.schema.json +43 -0
  161. package/dist/kpi/schemas/stack.schema.json +102 -0
  162. package/dist/kpi/schemas/task.schema.json +165 -0
  163. package/dist/kpi/schemas/verdict.schema.json +44 -0
  164. package/dist/kpi/skills/concise-output/SKILL.md +10 -0
  165. package/dist/kpi/skills/context-pack/SKILL.md +10 -0
  166. package/dist/kpi/skills/conventional-commit/SKILL.md +12 -0
  167. package/dist/kpi/skills/isolated-review/SKILL.md +10 -0
  168. package/dist/kpi/skills/kg-claim/SKILL.md +26 -0
  169. package/dist/kpi/skills/minimalist/SKILL.md +24 -0
  170. package/dist/kpi/skills/quality-gates/SKILL.md +10 -0
  171. package/dist/kpi/skills/spec-first/SKILL.md +10 -0
  172. package/dist/kpi/skills/tdd-cycle/SKILL.md +10 -0
  173. package/dist/kpi/templates/AGENTS.md +31 -0
  174. package/dist/kpi/templates/APPEND_SYSTEM.md +13 -0
  175. package/dist/kpi/templates/context-pack/product.md +7 -0
  176. package/dist/kpi/templates/context-pack/structure.md +7 -0
  177. package/dist/kpi/templates/context-pack/tech.md +9 -0
  178. package/dist/kpi/templates/policy.json +17 -0
  179. package/dist/kpi/themes/loop-amber.json +82 -0
  180. package/dist/kpi/themes/protocol-blue.json +82 -0
  181. package/dist/modes/interactive/assets/clankolas.png +0 -0
  182. package/dist/modes/interactive/theme/dark.json +90 -0
  183. package/dist/modes/interactive/theme/light.json +89 -0
  184. package/dist/modes/interactive/theme/theme-schema.json +352 -0
  185. package/docs/compaction.md +418 -0
  186. package/docs/containerization.md +111 -0
  187. package/docs/custom-provider.md +777 -0
  188. package/docs/development.md +71 -0
  189. package/docs/docs.json +156 -0
  190. package/docs/environment-variables.md +97 -0
  191. package/docs/extensions.md +3020 -0
  192. package/docs/images/doom-extension.png +0 -0
  193. package/docs/images/exy.png +0 -0
  194. package/docs/images/interactive-mode.png +0 -0
  195. package/docs/images/tree-view.png +0 -0
  196. package/docs/index.md +84 -0
  197. package/docs/json.md +98 -0
  198. package/docs/keybindings.md +236 -0
  199. package/docs/llama-cpp.md +101 -0
  200. package/docs/models.md +571 -0
  201. package/docs/packages.md +228 -0
  202. package/docs/prompt-templates.md +96 -0
  203. package/docs/providers.md +317 -0
  204. package/docs/quickstart.md +167 -0
  205. package/docs/rpc.md +1618 -0
  206. package/docs/sdk.md +1219 -0
  207. package/docs/security.md +59 -0
  208. package/docs/session-format.md +438 -0
  209. package/docs/sessions.md +145 -0
  210. package/docs/settings.md +369 -0
  211. package/docs/shell-aliases.md +13 -0
  212. package/docs/skills.md +232 -0
  213. package/docs/terminal-setup.md +177 -0
  214. package/docs/termux.md +127 -0
  215. package/docs/themes.md +320 -0
  216. package/docs/tmux.md +63 -0
  217. package/docs/tui.md +942 -0
  218. package/docs/usage.md +311 -0
  219. package/docs/windows.md +39 -0
  220. package/examples/README.md +25 -0
  221. package/examples/extensions/README.md +213 -0
  222. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  223. package/examples/extensions/bash-spawn-hook.ts +30 -0
  224. package/examples/extensions/bookmark.ts +50 -0
  225. package/examples/extensions/border-status-editor.ts +150 -0
  226. package/examples/extensions/built-in-tool-renderer.ts +249 -0
  227. package/examples/extensions/claude-rules.ts +86 -0
  228. package/examples/extensions/commands.ts +72 -0
  229. package/examples/extensions/confirm-destructive.ts +59 -0
  230. package/examples/extensions/custom-compaction.ts +117 -0
  231. package/examples/extensions/custom-footer.ts +64 -0
  232. package/examples/extensions/custom-header.ts +73 -0
  233. package/examples/extensions/custom-provider-anthropic/index.ts +611 -0
  234. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  235. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  236. package/examples/extensions/custom-provider-gitlab-duo/index.ts +405 -0
  237. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  238. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  239. package/examples/extensions/dirty-repo-guard.ts +56 -0
  240. package/examples/extensions/doom-overlay/README.md +46 -0
  241. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  242. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  243. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  244. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  245. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  246. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  247. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  248. package/examples/extensions/doom-overlay/index.ts +74 -0
  249. package/examples/extensions/doom-overlay/wad-finder.ts +55 -0
  250. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  251. package/examples/extensions/dynamic-resources/dynamic.json +79 -0
  252. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  253. package/examples/extensions/dynamic-resources/index.ts +15 -0
  254. package/examples/extensions/dynamic-tools.ts +74 -0
  255. package/examples/extensions/entry-renderer.ts +41 -0
  256. package/examples/extensions/event-bus.ts +43 -0
  257. package/examples/extensions/file-trigger.ts +41 -0
  258. package/examples/extensions/git-checkpoint.ts +53 -0
  259. package/examples/extensions/git-merge-and-resolve.ts +115 -0
  260. package/examples/extensions/github-issue-autocomplete.ts +185 -0
  261. package/examples/extensions/gondolin/index.ts +531 -0
  262. package/examples/extensions/gondolin/package-lock.json +185 -0
  263. package/examples/extensions/gondolin/package.json +19 -0
  264. package/examples/extensions/handoff.ts +190 -0
  265. package/examples/extensions/hello.ts +26 -0
  266. package/examples/extensions/hidden-thinking-label.ts +53 -0
  267. package/examples/extensions/inline-bash.ts +94 -0
  268. package/examples/extensions/input-transform-streaming.ts +39 -0
  269. package/examples/extensions/input-transform.ts +43 -0
  270. package/examples/extensions/interactive-shell.ts +196 -0
  271. package/examples/extensions/kimi-deferred-tools.ts +61 -0
  272. package/examples/extensions/mac-system-theme.ts +47 -0
  273. package/examples/extensions/message-renderer.ts +59 -0
  274. package/examples/extensions/minimal-mode.ts +426 -0
  275. package/examples/extensions/modal-editor.ts +85 -0
  276. package/examples/extensions/model-status.ts +31 -0
  277. package/examples/extensions/notify.ts +57 -0
  278. package/examples/extensions/overlay-qa-tests.ts +1450 -0
  279. package/examples/extensions/overlay-test.ts +153 -0
  280. package/examples/extensions/permission-gate.ts +34 -0
  281. package/examples/extensions/pirate.ts +47 -0
  282. package/examples/extensions/plan-mode/README.md +66 -0
  283. package/examples/extensions/plan-mode/index.ts +390 -0
  284. package/examples/extensions/plan-mode/utils.ts +168 -0
  285. package/examples/extensions/preset.ts +436 -0
  286. package/examples/extensions/project-trust.ts +64 -0
  287. package/examples/extensions/prompt-customizer.ts +97 -0
  288. package/examples/extensions/protected-paths.ts +30 -0
  289. package/examples/extensions/provider-payload.ts +18 -0
  290. package/examples/extensions/qna.ts +118 -0
  291. package/examples/extensions/question.ts +286 -0
  292. package/examples/extensions/questionnaire.ts +448 -0
  293. package/examples/extensions/rainbow-editor.ts +88 -0
  294. package/examples/extensions/reload-runtime.ts +37 -0
  295. package/examples/extensions/rpc-demo.ts +118 -0
  296. package/examples/extensions/sandbox/index.ts +321 -0
  297. package/examples/extensions/sandbox/package-lock.json +92 -0
  298. package/examples/extensions/sandbox/package.json +19 -0
  299. package/examples/extensions/send-user-message.ts +97 -0
  300. package/examples/extensions/session-name.ts +27 -0
  301. package/examples/extensions/shutdown-command.ts +63 -0
  302. package/examples/extensions/snake.ts +343 -0
  303. package/examples/extensions/space-invaders.ts +560 -0
  304. package/examples/extensions/ssh.ts +220 -0
  305. package/examples/extensions/status-line.ts +32 -0
  306. package/examples/extensions/structured-output.ts +65 -0
  307. package/examples/extensions/subagent/README.md +177 -0
  308. package/examples/extensions/subagent/agents/planner.md +37 -0
  309. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  310. package/examples/extensions/subagent/agents/scout.md +50 -0
  311. package/examples/extensions/subagent/agents/worker.md +24 -0
  312. package/examples/extensions/subagent/agents.ts +157 -0
  313. package/examples/extensions/subagent/index.ts +1038 -0
  314. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  315. package/examples/extensions/subagent/prompts/implement.md +10 -0
  316. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  317. package/examples/extensions/summarize.ts +199 -0
  318. package/examples/extensions/system-prompt-header.ts +17 -0
  319. package/examples/extensions/tic-tac-toe.ts +1008 -0
  320. package/examples/extensions/timed-confirm.ts +70 -0
  321. package/examples/extensions/titlebar-spinner.ts +58 -0
  322. package/examples/extensions/todo.ts +297 -0
  323. package/examples/extensions/tool-override.ts +144 -0
  324. package/examples/extensions/tools.ts +146 -0
  325. package/examples/extensions/trigger-compact.ts +50 -0
  326. package/examples/extensions/truncated-tool.ts +195 -0
  327. package/examples/extensions/widget-placement.ts +9 -0
  328. package/examples/extensions/with-deps/index.ts +32 -0
  329. package/examples/extensions/with-deps/package-lock.json +31 -0
  330. package/examples/extensions/with-deps/package.json +22 -0
  331. package/examples/extensions/working-indicator.ts +123 -0
  332. package/examples/extensions/working-message-test.ts +25 -0
  333. package/examples/rpc-extension-ui.ts +641 -0
  334. package/examples/sdk/01-minimal.ts +26 -0
  335. package/examples/sdk/02-custom-model.ts +49 -0
  336. package/examples/sdk/03-custom-prompt.ts +75 -0
  337. package/examples/sdk/04-skills.ts +55 -0
  338. package/examples/sdk/05-tools.ts +48 -0
  339. package/examples/sdk/06-extensions.ts +99 -0
  340. package/examples/sdk/07-context-files.ts +47 -0
  341. package/examples/sdk/08-prompt-templates.ts +51 -0
  342. package/examples/sdk/09-api-keys-and-oauth.ts +34 -0
  343. package/examples/sdk/10-settings.ts +53 -0
  344. package/examples/sdk/11-sessions.ts +52 -0
  345. package/examples/sdk/12-full-control.ts +74 -0
  346. package/examples/sdk/13-session-runtime.ts +67 -0
  347. package/examples/sdk/README.md +140 -0
  348. package/package.json +55 -0
@@ -0,0 +1,448 @@
1
+ /**
2
+ * Questionnaire Tool - Unified tool for asking single or multiple questions
3
+ *
4
+ * Single question: simple options list
5
+ * Multiple questions: tab bar navigation between questions
6
+ */
7
+
8
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
9
+ import {
10
+ Editor,
11
+ type EditorTheme,
12
+ Key,
13
+ matchesKey,
14
+ Text,
15
+ visibleWidth,
16
+ wrapTextWithAnsi,
17
+ } from "@earendil-works/pi-tui";
18
+ import { Type } from "typebox";
19
+
20
+ // Types
21
+ interface QuestionOption {
22
+ value: string;
23
+ label: string;
24
+ description?: string;
25
+ }
26
+
27
+ type RenderOption = QuestionOption & { isOther?: boolean };
28
+
29
+ interface Question {
30
+ id: string;
31
+ label: string;
32
+ prompt: string;
33
+ options: QuestionOption[];
34
+ allowOther: boolean;
35
+ }
36
+
37
+ interface Answer {
38
+ id: string;
39
+ value: string;
40
+ label: string;
41
+ wasCustom: boolean;
42
+ index?: number;
43
+ }
44
+
45
+ interface QuestionnaireResult {
46
+ questions: Question[];
47
+ answers: Answer[];
48
+ cancelled: boolean;
49
+ }
50
+
51
+ // Schema
52
+ const QuestionOptionSchema = Type.Object({
53
+ value: Type.String({ description: "The value returned when selected" }),
54
+ label: Type.String({ description: "Display label for the option" }),
55
+ description: Type.Optional(Type.String({ description: "Optional description shown below label" })),
56
+ });
57
+
58
+ const QuestionSchema = Type.Object({
59
+ id: Type.String({ description: "Unique identifier for this question" }),
60
+ label: Type.Optional(
61
+ Type.String({
62
+ description: "Short contextual label for tab bar, e.g. 'Scope', 'Priority' (defaults to Q1, Q2)",
63
+ }),
64
+ ),
65
+ prompt: Type.String({ description: "The full question text to display" }),
66
+ options: Type.Array(QuestionOptionSchema, { description: "Available options to choose from" }),
67
+ allowOther: Type.Optional(Type.Boolean({ description: "Allow 'Type something' option (default: true)" })),
68
+ });
69
+
70
+ const QuestionnaireParams = Type.Object({
71
+ questions: Type.Array(QuestionSchema, { description: "Questions to ask the user" }),
72
+ });
73
+
74
+ function errorResult(
75
+ message: string,
76
+ questions: Question[] = [],
77
+ ): { content: { type: "text"; text: string }[]; details: QuestionnaireResult } {
78
+ return {
79
+ content: [{ type: "text", text: message }],
80
+ details: { questions, answers: [], cancelled: true },
81
+ };
82
+ }
83
+
84
+ export default function questionnaire(pi: ExtensionAPI) {
85
+ pi.registerTool({
86
+ name: "questionnaire",
87
+ label: "Questionnaire",
88
+ description:
89
+ "Ask the user one or more questions. Use for clarifying requirements, getting preferences, or confirming decisions. For single questions, shows a simple option list. For multiple questions, shows a tab-based interface.",
90
+ parameters: QuestionnaireParams,
91
+
92
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
93
+ if (ctx.mode !== "tui") {
94
+ return errorResult("Error: UI not available (running in non-interactive mode)");
95
+ }
96
+ if (params.questions.length === 0) {
97
+ return errorResult("Error: No questions provided");
98
+ }
99
+
100
+ // Normalize questions with defaults
101
+ const questions: Question[] = params.questions.map((q, i) => ({
102
+ ...q,
103
+ label: q.label || `Q${i + 1}`,
104
+ allowOther: q.allowOther !== false,
105
+ }));
106
+
107
+ const isMulti = questions.length > 1;
108
+ const totalTabs = questions.length + 1; // questions + Submit
109
+
110
+ const result = await ctx.ui.custom<QuestionnaireResult>((tui, theme, _kb, done) => {
111
+ // State
112
+ let currentTab = 0;
113
+ let optionIndex = 0;
114
+ let inputMode = false;
115
+ let inputQuestionId: string | null = null;
116
+ let cachedLines: string[] | undefined;
117
+ const answers = new Map<string, Answer>();
118
+
119
+ // Editor for "Type something" option
120
+ const editorTheme: EditorTheme = {
121
+ borderColor: (s) => theme.fg("accent", s),
122
+ selectList: {
123
+ selectedPrefix: (t) => theme.fg("accent", t),
124
+ selectedText: (t) => theme.fg("accent", t),
125
+ description: (t) => theme.fg("muted", t),
126
+ scrollInfo: (t) => theme.fg("dim", t),
127
+ noMatch: (t) => theme.fg("warning", t),
128
+ },
129
+ };
130
+ const editor = new Editor(tui, editorTheme);
131
+
132
+ // Helpers
133
+ function refresh() {
134
+ cachedLines = undefined;
135
+ tui.requestRender();
136
+ }
137
+
138
+ function submit(cancelled: boolean) {
139
+ done({ questions, answers: Array.from(answers.values()), cancelled });
140
+ }
141
+
142
+ function currentQuestion(): Question | undefined {
143
+ return questions[currentTab];
144
+ }
145
+
146
+ function currentOptions(): RenderOption[] {
147
+ const q = currentQuestion();
148
+ if (!q) return [];
149
+ const opts: RenderOption[] = [...q.options];
150
+ if (q.allowOther) {
151
+ opts.push({ value: "__other__", label: "Type something.", isOther: true });
152
+ }
153
+ return opts;
154
+ }
155
+
156
+ function allAnswered(): boolean {
157
+ return questions.every((q) => answers.has(q.id));
158
+ }
159
+
160
+ function advanceAfterAnswer() {
161
+ if (!isMulti) {
162
+ submit(false);
163
+ return;
164
+ }
165
+ if (currentTab < questions.length - 1) {
166
+ currentTab++;
167
+ } else {
168
+ currentTab = questions.length; // Submit tab
169
+ }
170
+ optionIndex = 0;
171
+ refresh();
172
+ }
173
+
174
+ function saveAnswer(questionId: string, value: string, label: string, wasCustom: boolean, index?: number) {
175
+ answers.set(questionId, { id: questionId, value, label, wasCustom, index });
176
+ }
177
+
178
+ // Editor submit callback
179
+ editor.onSubmit = (value) => {
180
+ if (!inputQuestionId) return;
181
+ const trimmed = value.trim() || "(no response)";
182
+ saveAnswer(inputQuestionId, trimmed, trimmed, true);
183
+ inputMode = false;
184
+ inputQuestionId = null;
185
+ editor.setText("");
186
+ advanceAfterAnswer();
187
+ };
188
+
189
+ function handleInput(data: string) {
190
+ // Input mode: route to editor
191
+ if (inputMode) {
192
+ if (matchesKey(data, Key.escape)) {
193
+ inputMode = false;
194
+ inputQuestionId = null;
195
+ editor.setText("");
196
+ refresh();
197
+ return;
198
+ }
199
+ editor.handleInput(data);
200
+ refresh();
201
+ return;
202
+ }
203
+
204
+ const q = currentQuestion();
205
+ const opts = currentOptions();
206
+
207
+ // Tab navigation (multi-question only)
208
+ if (isMulti) {
209
+ if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
210
+ currentTab = (currentTab + 1) % totalTabs;
211
+ optionIndex = 0;
212
+ refresh();
213
+ return;
214
+ }
215
+ if (matchesKey(data, Key.shift("tab")) || matchesKey(data, Key.left)) {
216
+ currentTab = (currentTab - 1 + totalTabs) % totalTabs;
217
+ optionIndex = 0;
218
+ refresh();
219
+ return;
220
+ }
221
+ }
222
+
223
+ // Submit tab
224
+ if (currentTab === questions.length) {
225
+ if (matchesKey(data, Key.enter) && allAnswered()) {
226
+ submit(false);
227
+ } else if (matchesKey(data, Key.escape)) {
228
+ submit(true);
229
+ }
230
+ return;
231
+ }
232
+
233
+ // Option navigation
234
+ if (matchesKey(data, Key.up)) {
235
+ optionIndex = Math.max(0, optionIndex - 1);
236
+ refresh();
237
+ return;
238
+ }
239
+ if (matchesKey(data, Key.down)) {
240
+ optionIndex = Math.min(opts.length - 1, optionIndex + 1);
241
+ refresh();
242
+ return;
243
+ }
244
+
245
+ // Select option
246
+ if (matchesKey(data, Key.enter) && q) {
247
+ const opt = opts[optionIndex];
248
+ if (opt.isOther) {
249
+ inputMode = true;
250
+ inputQuestionId = q.id;
251
+ editor.setText("");
252
+ refresh();
253
+ return;
254
+ }
255
+ saveAnswer(q.id, opt.value, opt.label, false, optionIndex + 1);
256
+ advanceAfterAnswer();
257
+ return;
258
+ }
259
+
260
+ // Cancel
261
+ if (matchesKey(data, Key.escape)) {
262
+ submit(true);
263
+ }
264
+ }
265
+
266
+ function render(width: number): string[] {
267
+ if (cachedLines) return cachedLines;
268
+
269
+ const lines: string[] = [];
270
+ const renderWidth = Math.max(1, width);
271
+ const q = currentQuestion();
272
+ const opts = currentOptions();
273
+
274
+ function addWrapped(text: string) {
275
+ lines.push(...wrapTextWithAnsi(text, renderWidth));
276
+ }
277
+
278
+ function addWrappedWithPrefix(prefix: string, text: string) {
279
+ const prefixWidth = visibleWidth(prefix);
280
+ if (prefixWidth >= renderWidth) {
281
+ addWrapped(prefix + text);
282
+ return;
283
+ }
284
+ const wrapped = wrapTextWithAnsi(text, renderWidth - prefixWidth);
285
+ const continuationPrefix = " ".repeat(prefixWidth);
286
+ for (let i = 0; i < wrapped.length; i++) {
287
+ lines.push(`${i === 0 ? prefix : continuationPrefix}${wrapped[i]}`);
288
+ }
289
+ }
290
+
291
+ lines.push(theme.fg("accent", "─".repeat(renderWidth)));
292
+
293
+ // Tab bar (multi-question only)
294
+ if (isMulti) {
295
+ const tabs: string[] = ["← "];
296
+ for (let i = 0; i < questions.length; i++) {
297
+ const isActive = i === currentTab;
298
+ const isAnswered = answers.has(questions[i].id);
299
+ const lbl = questions[i].label;
300
+ const box = isAnswered ? "■" : "□";
301
+ const color = isAnswered ? "success" : "muted";
302
+ const text = ` ${box} ${lbl} `;
303
+ const styled = isActive ? theme.bg("selectedBg", theme.fg("text", text)) : theme.fg(color, text);
304
+ tabs.push(`${styled} `);
305
+ }
306
+ const canSubmit = allAnswered();
307
+ const isSubmitTab = currentTab === questions.length;
308
+ const submitText = " ✓ Submit ";
309
+ const submitStyled = isSubmitTab
310
+ ? theme.bg("selectedBg", theme.fg("text", submitText))
311
+ : theme.fg(canSubmit ? "success" : "dim", submitText);
312
+ tabs.push(`${submitStyled} →`);
313
+ addWrappedWithPrefix(" ", tabs.join(""));
314
+ lines.push("");
315
+ }
316
+
317
+ // Helper to render options list
318
+ function renderOptions() {
319
+ for (let i = 0; i < opts.length; i++) {
320
+ const opt = opts[i];
321
+ const selected = i === optionIndex;
322
+ const isOther = opt.isOther === true;
323
+ const prefix = selected ? theme.fg("accent", "> ") : " ";
324
+ const label = `${i + 1}. ${opt.label}${isOther && inputMode ? " ✎" : ""}`;
325
+ const color = selected || (isOther && inputMode) ? "accent" : "text";
326
+
327
+ addWrappedWithPrefix(prefix, theme.fg(color, label));
328
+ if (opt.description) {
329
+ addWrappedWithPrefix(" ", theme.fg("muted", opt.description));
330
+ }
331
+ }
332
+ }
333
+
334
+ // Content
335
+ if (inputMode && q) {
336
+ addWrappedWithPrefix(" ", theme.fg("text", q.prompt));
337
+ lines.push("");
338
+ // Show options for reference
339
+ renderOptions();
340
+ lines.push("");
341
+ addWrappedWithPrefix(" ", theme.fg("muted", "Your answer:"));
342
+ for (const line of editor.render(Math.max(1, renderWidth - 2))) {
343
+ lines.push(` ${line}`);
344
+ }
345
+ lines.push("");
346
+ addWrappedWithPrefix(" ", theme.fg("dim", "Enter to submit • Esc to cancel"));
347
+ } else if (currentTab === questions.length) {
348
+ addWrappedWithPrefix(" ", theme.fg("accent", theme.bold("Ready to submit")));
349
+ lines.push("");
350
+ for (const question of questions) {
351
+ const answer = answers.get(question.id);
352
+ if (answer) {
353
+ const prefix = answer.wasCustom ? "(wrote) " : "";
354
+ const summary = `${theme.fg("muted", `${question.label}: `)}${theme.fg("text", prefix + answer.label)}`;
355
+ addWrappedWithPrefix(" ", summary);
356
+ }
357
+ }
358
+ lines.push("");
359
+ if (allAnswered()) {
360
+ addWrappedWithPrefix(" ", theme.fg("success", "Press Enter to submit"));
361
+ } else {
362
+ const missing = questions
363
+ .filter((q) => !answers.has(q.id))
364
+ .map((q) => q.label)
365
+ .join(", ");
366
+ addWrappedWithPrefix(" ", theme.fg("warning", `Unanswered: ${missing}`));
367
+ }
368
+ } else if (q) {
369
+ addWrappedWithPrefix(" ", theme.fg("text", q.prompt));
370
+ lines.push("");
371
+ renderOptions();
372
+ }
373
+
374
+ lines.push("");
375
+ if (!inputMode) {
376
+ const help = isMulti
377
+ ? "Tab/←→ navigate • ↑↓ select • Enter confirm • Esc cancel"
378
+ : "↑↓ navigate • Enter select • Esc cancel";
379
+ addWrappedWithPrefix(" ", theme.fg("dim", help));
380
+ }
381
+ lines.push(theme.fg("accent", "─".repeat(renderWidth)));
382
+
383
+ cachedLines = lines;
384
+ return lines;
385
+ }
386
+
387
+ return {
388
+ render,
389
+ invalidate: () => {
390
+ cachedLines = undefined;
391
+ },
392
+ handleInput,
393
+ };
394
+ });
395
+
396
+ if (result.cancelled) {
397
+ return {
398
+ content: [{ type: "text", text: "User cancelled the questionnaire" }],
399
+ details: result,
400
+ };
401
+ }
402
+
403
+ const answerLines = result.answers.map((a) => {
404
+ const qLabel = questions.find((q) => q.id === a.id)?.label || a.id;
405
+ if (a.wasCustom) {
406
+ return `${qLabel}: user wrote: ${a.label}`;
407
+ }
408
+ return `${qLabel}: user selected: ${a.index}. ${a.label}`;
409
+ });
410
+
411
+ return {
412
+ content: [{ type: "text", text: answerLines.join("\n") }],
413
+ details: result,
414
+ };
415
+ },
416
+
417
+ renderCall(args, theme, _context) {
418
+ const qs = (args.questions as Question[]) || [];
419
+ const count = qs.length;
420
+ const labels = qs.map((q) => q.label || q.id).join(", ");
421
+ let text = theme.fg("toolTitle", theme.bold("questionnaire "));
422
+ text += theme.fg("muted", `${count} question${count !== 1 ? "s" : ""}`);
423
+ if (labels) {
424
+ text += theme.fg("dim", ` (${labels})`);
425
+ }
426
+ return new Text(text, 0, 0);
427
+ },
428
+
429
+ renderResult(result, _options, theme, _context) {
430
+ const details = result.details as QuestionnaireResult | undefined;
431
+ if (!details) {
432
+ const text = result.content[0];
433
+ return new Text(text?.type === "text" ? text.text : "", 0, 0);
434
+ }
435
+ if (details.cancelled) {
436
+ return new Text(theme.fg("warning", "Cancelled"), 0, 0);
437
+ }
438
+ const lines = details.answers.map((a) => {
439
+ if (a.wasCustom) {
440
+ return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${theme.fg("muted", "(wrote) ")}${a.label}`;
441
+ }
442
+ const display = a.index ? `${a.index}. ${a.label}` : a.label;
443
+ return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${display}`;
444
+ });
445
+ return new Text(lines.join("\n"), 0, 0);
446
+ },
447
+ });
448
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Rainbow Editor - highlights "ultrathink" with animated shine effect
3
+ *
4
+ * Usage: pi --extension ./examples/extensions/rainbow-editor.ts
5
+ */
6
+
7
+ import { CustomEditor, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
8
+
9
+ // Base colors (coral → yellow → green → teal → blue → purple → pink)
10
+ const COLORS: [number, number, number][] = [
11
+ [233, 137, 115], // coral
12
+ [228, 186, 103], // yellow
13
+ [141, 192, 122], // green
14
+ [102, 194, 179], // teal
15
+ [121, 157, 207], // blue
16
+ [157, 134, 195], // purple
17
+ [206, 130, 172], // pink
18
+ ];
19
+ const RESET = "\x1b[0m";
20
+
21
+ function brighten(rgb: [number, number, number], factor: number): string {
22
+ const [r, g, b] = rgb.map((c) => Math.round(c + (255 - c) * factor));
23
+ return `\x1b[38;2;${r};${g};${b}m`;
24
+ }
25
+
26
+ function colorize(text: string, shinePos: number): string {
27
+ return (
28
+ [...text]
29
+ .map((c, i) => {
30
+ const baseColor = COLORS[i % COLORS.length]!;
31
+ // 3-letter shine: center bright, adjacent dimmer
32
+ let factor = 0;
33
+ if (shinePos >= 0) {
34
+ const dist = Math.abs(i - shinePos);
35
+ if (dist === 0) factor = 0.7;
36
+ else if (dist === 1) factor = 0.35;
37
+ }
38
+ return `${brighten(baseColor, factor)}${c}`;
39
+ })
40
+ .join("") + RESET
41
+ );
42
+ }
43
+
44
+ class RainbowEditor extends CustomEditor {
45
+ private animationTimer?: ReturnType<typeof setInterval>;
46
+ private frame = 0;
47
+
48
+ private hasUltrathink(): boolean {
49
+ return /ultrathink/i.test(this.getText());
50
+ }
51
+
52
+ private startAnimation(): void {
53
+ if (this.animationTimer) return;
54
+ this.animationTimer = setInterval(() => {
55
+ this.frame++;
56
+ this.tui.requestRender();
57
+ }, 60);
58
+ }
59
+
60
+ private stopAnimation(): void {
61
+ if (this.animationTimer) {
62
+ clearInterval(this.animationTimer);
63
+ this.animationTimer = undefined;
64
+ }
65
+ }
66
+
67
+ handleInput(data: string): void {
68
+ super.handleInput(data);
69
+ if (this.hasUltrathink()) {
70
+ this.startAnimation();
71
+ } else {
72
+ this.stopAnimation();
73
+ }
74
+ }
75
+
76
+ render(width: number): string[] {
77
+ // Cycle: 10 shine positions + 10 pause frames
78
+ const cycle = this.frame % 20;
79
+ const shinePos = cycle < 10 ? cycle : -1; // -1 means no shine (pause)
80
+ return super.render(width).map((line) => line.replace(/ultrathink/gi, (m) => colorize(m, shinePos)));
81
+ }
82
+ }
83
+
84
+ export default function (pi: ExtensionAPI) {
85
+ pi.on("session_start", (_event, ctx) => {
86
+ ctx.ui.setEditorComponent((tui, theme, kb) => new RainbowEditor(tui, theme, kb));
87
+ });
88
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Reload Runtime Extension
3
+ *
4
+ * Demonstrates ctx.reload() from ExtensionCommandContext and an LLM-callable
5
+ * tool that queues a follow-up command to trigger reload.
6
+ */
7
+
8
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
9
+ import { Type } from "typebox";
10
+
11
+ export default function (pi: ExtensionAPI) {
12
+ // Command entrypoint for reload.
13
+ // Treat reload as terminal for this handler.
14
+ pi.registerCommand("reload-runtime", {
15
+ description: "Reload extensions, skills, prompts, themes, and context files",
16
+ handler: async (_args, ctx) => {
17
+ await ctx.reload();
18
+ return;
19
+ },
20
+ });
21
+
22
+ // LLM-callable tool. Tools get ExtensionContext, so they cannot call ctx.reload() directly.
23
+ // Instead, queue a follow-up user command that executes the command above.
24
+ pi.registerTool({
25
+ name: "reload_runtime",
26
+ label: "Reload Runtime",
27
+ description: "Reload extensions, skills, prompts, themes, and context files",
28
+ parameters: Type.Object({}),
29
+ async execute() {
30
+ pi.sendUserMessage("/reload-runtime", { deliverAs: "followUp" });
31
+ return {
32
+ content: [{ type: "text", text: "Queued /reload-runtime as a follow-up command." }],
33
+ details: {},
34
+ };
35
+ },
36
+ });
37
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * RPC Extension UI Demo
3
+ *
4
+ * Purpose-built extension that exercises all RPC-supported extension UI methods.
5
+ * Designed to be loaded alongside the rpc-extension-ui-example.ts script to
6
+ * demonstrate the full extension UI protocol.
7
+ *
8
+ * UI methods exercised:
9
+ * - select() - on tool_call for dangerous bash commands
10
+ * - confirm() - on session_before_switch
11
+ * - input() - via /rpc-input command
12
+ * - editor() - via /rpc-editor command
13
+ * - notify() - after each dialog completes
14
+ * - setStatus() - on turn_start/turn_end
15
+ * - setWidget() - on session_start
16
+ * - setTitle() - on session_start
17
+ * - setEditorText() - via /rpc-prefill command
18
+ */
19
+
20
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
21
+
22
+ export default function (pi: ExtensionAPI) {
23
+ let turnCount = 0;
24
+
25
+ // -- setTitle, setWidget, setStatus on session lifecycle --
26
+
27
+ pi.on("session_start", async (event, ctx) => {
28
+ ctx.ui.setTitle(event.reason === "new" ? "pi RPC Demo (new session)" : "pi RPC Demo");
29
+ ctx.ui.setWidget("rpc-demo", ["--- RPC Extension UI Demo ---", "Loaded and ready."]);
30
+ ctx.ui.setStatus("rpc-demo", `Turns: ${turnCount}`);
31
+ });
32
+
33
+ // -- setStatus on turn lifecycle --
34
+
35
+ pi.on("turn_start", async (_event, ctx) => {
36
+ turnCount++;
37
+ ctx.ui.setStatus("rpc-demo", `Turn ${turnCount} running...`);
38
+ });
39
+
40
+ pi.on("turn_end", async (_event, ctx) => {
41
+ ctx.ui.setStatus("rpc-demo", `Turn ${turnCount} done`);
42
+ });
43
+
44
+ // -- select on dangerous tool calls --
45
+
46
+ pi.on("tool_call", async (event, ctx) => {
47
+ if (event.toolName !== "bash") return undefined;
48
+
49
+ const command = event.input.command as string;
50
+ const isDangerous = /\brm\s+(-rf?|--recursive)/i.test(command) || /\bsudo\b/i.test(command);
51
+
52
+ if (isDangerous) {
53
+ if (!ctx.hasUI) {
54
+ return { block: true, reason: "Dangerous command blocked (no UI)" };
55
+ }
56
+
57
+ const choice = await ctx.ui.select(`Dangerous command: ${command}`, ["Allow", "Block"]);
58
+ if (choice !== "Allow") {
59
+ ctx.ui.notify("Command blocked by user", "warning");
60
+ return { block: true, reason: "Blocked by user" };
61
+ }
62
+ ctx.ui.notify("Command allowed", "info");
63
+ }
64
+
65
+ return undefined;
66
+ });
67
+
68
+ // -- confirm on session clear --
69
+
70
+ pi.on("session_before_switch", async (event, ctx) => {
71
+ if (event.reason !== "new") return;
72
+ if (!ctx.hasUI) return;
73
+
74
+ const confirmed = await ctx.ui.confirm("Clear session?", "All messages will be lost.");
75
+ if (!confirmed) {
76
+ ctx.ui.notify("Clear cancelled", "info");
77
+ return { cancel: true };
78
+ }
79
+ });
80
+
81
+ // -- input via command --
82
+
83
+ pi.registerCommand("rpc-input", {
84
+ description: "Prompt for text input (demonstrates ctx.ui.input in RPC)",
85
+ handler: async (_args, ctx) => {
86
+ const value = await ctx.ui.input("Enter a value", "type something...");
87
+ if (value) {
88
+ ctx.ui.notify(`You entered: ${value}`, "info");
89
+ } else {
90
+ ctx.ui.notify("Input cancelled", "info");
91
+ }
92
+ },
93
+ });
94
+
95
+ // -- editor via command --
96
+
97
+ pi.registerCommand("rpc-editor", {
98
+ description: "Open multi-line editor (demonstrates ctx.ui.editor in RPC)",
99
+ handler: async (_args, ctx) => {
100
+ const text = await ctx.ui.editor("Edit some text", "Line 1\nLine 2\nLine 3");
101
+ if (text) {
102
+ ctx.ui.notify(`Editor submitted (${text.split("\n").length} lines)`, "info");
103
+ } else {
104
+ ctx.ui.notify("Editor cancelled", "info");
105
+ }
106
+ },
107
+ });
108
+
109
+ // -- setEditorText via command --
110
+
111
+ pi.registerCommand("rpc-prefill", {
112
+ description: "Prefill the input editor (demonstrates ctx.ui.setEditorText in RPC)",
113
+ handler: async (_args, ctx) => {
114
+ ctx.ui.setEditorText("This text was set by the rpc-demo extension.");
115
+ ctx.ui.notify("Editor prefilled", "info");
116
+ },
117
+ });
118
+ }