@namzu/sdk 69.0.0 → 69.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (316) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/dist/agents/QueryAgent.d.ts.map +1 -1
  3. package/dist/agents/QueryAgent.js +2 -0
  4. package/dist/agents/QueryAgent.js.map +1 -1
  5. package/dist/authorization/skill-grant.d.ts.map +1 -1
  6. package/dist/authorization/skill-grant.js +1 -0
  7. package/dist/authorization/skill-grant.js.map +1 -1
  8. package/dist/bridge/acp/permission.d.ts +10 -0
  9. package/dist/bridge/acp/permission.d.ts.map +1 -1
  10. package/dist/bridge/acp/permission.js.map +1 -1
  11. package/dist/bridge/acp/server.d.ts.map +1 -1
  12. package/dist/bridge/acp/server.js +1 -0
  13. package/dist/bridge/acp/server.js.map +1 -1
  14. package/dist/connector/index.d.ts +2 -2
  15. package/dist/connector/index.d.ts.map +1 -1
  16. package/dist/connector/index.js +1 -1
  17. package/dist/connector/index.js.map +1 -1
  18. package/dist/connector/mcp/client.d.ts +29 -0
  19. package/dist/connector/mcp/client.d.ts.map +1 -1
  20. package/dist/connector/mcp/client.js +59 -2
  21. package/dist/connector/mcp/client.js.map +1 -1
  22. package/dist/connector/mcp/errors.d.ts +7 -0
  23. package/dist/connector/mcp/errors.d.ts.map +1 -1
  24. package/dist/connector/mcp/errors.js +14 -0
  25. package/dist/connector/mcp/errors.js.map +1 -1
  26. package/dist/connector/mcp/index.d.ts +5 -1
  27. package/dist/connector/mcp/index.d.ts.map +1 -1
  28. package/dist/connector/mcp/index.js +3 -1
  29. package/dist/connector/mcp/index.js.map +1 -1
  30. package/dist/connector/mcp/skill-source.d.ts +144 -0
  31. package/dist/connector/mcp/skill-source.d.ts.map +1 -0
  32. package/dist/connector/mcp/skill-source.js +375 -0
  33. package/dist/connector/mcp/skill-source.js.map +1 -0
  34. package/dist/connector/mcp/skills.d.ts +137 -0
  35. package/dist/connector/mcp/skills.d.ts.map +1 -0
  36. package/dist/connector/mcp/skills.js +303 -0
  37. package/dist/connector/mcp/skills.js.map +1 -0
  38. package/dist/network/audit.d.ts +1 -1
  39. package/dist/network/audit.d.ts.map +1 -1
  40. package/dist/network/audit.js +2 -0
  41. package/dist/network/audit.js.map +1 -1
  42. package/dist/network/computer.d.ts +107 -0
  43. package/dist/network/computer.d.ts.map +1 -0
  44. package/dist/network/computer.js +250 -0
  45. package/dist/network/computer.js.map +1 -0
  46. package/dist/network/index.d.ts +2 -0
  47. package/dist/network/index.d.ts.map +1 -1
  48. package/dist/network/index.js +1 -0
  49. package/dist/network/index.js.map +1 -1
  50. package/dist/network/join.d.ts +3 -1
  51. package/dist/network/join.d.ts.map +1 -1
  52. package/dist/network/join.js +6 -3
  53. package/dist/network/join.js.map +1 -1
  54. package/dist/network/roster.d.ts +5 -0
  55. package/dist/network/roster.d.ts.map +1 -1
  56. package/dist/network/roster.js.map +1 -1
  57. package/dist/network/service.d.ts +12 -2
  58. package/dist/network/service.d.ts.map +1 -1
  59. package/dist/network/service.js +377 -52
  60. package/dist/network/service.js.map +1 -1
  61. package/dist/network/store.d.ts +10 -0
  62. package/dist/network/store.d.ts.map +1 -1
  63. package/dist/network/store.js +25 -3
  64. package/dist/network/store.js.map +1 -1
  65. package/dist/network/wire.d.ts +2 -0
  66. package/dist/network/wire.d.ts.map +1 -1
  67. package/dist/network/wire.js +5 -0
  68. package/dist/network/wire.js.map +1 -1
  69. package/dist/persona/assembler.d.ts.map +1 -1
  70. package/dist/persona/assembler.js +7 -1
  71. package/dist/persona/assembler.js.map +1 -1
  72. package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
  73. package/dist/plugin/shell-hook-tool-names.js +1 -0
  74. package/dist/plugin/shell-hook-tool-names.js.map +1 -1
  75. package/dist/plugin/shell-hook.d.ts +8 -3
  76. package/dist/plugin/shell-hook.d.ts.map +1 -1
  77. package/dist/plugin/shell-hook.js +132 -7
  78. package/dist/plugin/shell-hook.js.map +1 -1
  79. package/dist/provider/fallback.d.ts.map +1 -1
  80. package/dist/provider/fallback.js +4 -0
  81. package/dist/provider/fallback.js.map +1 -1
  82. package/dist/provider/idle-timeout.d.ts.map +1 -1
  83. package/dist/provider/idle-timeout.js +3 -0
  84. package/dist/provider/idle-timeout.js.map +1 -1
  85. package/dist/provider/model-window-suffix.d.ts.map +1 -1
  86. package/dist/provider/model-window-suffix.js +1 -0
  87. package/dist/provider/model-window-suffix.js.map +1 -1
  88. package/dist/provider/retry.d.ts.map +1 -1
  89. package/dist/provider/retry.js +3 -0
  90. package/dist/provider/retry.js.map +1 -1
  91. package/dist/public-runtime.d.ts +9 -6
  92. package/dist/public-runtime.d.ts.map +1 -1
  93. package/dist/public-runtime.js +11 -4
  94. package/dist/public-runtime.js.map +1 -1
  95. package/dist/public-tools.d.ts +7 -4
  96. package/dist/public-tools.d.ts.map +1 -1
  97. package/dist/public-tools.js +9 -3
  98. package/dist/public-tools.js.map +1 -1
  99. package/dist/public-types.d.ts +3 -1
  100. package/dist/public-types.d.ts.map +1 -1
  101. package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
  102. package/dist/runtime/query/executor/tool-call-admission.js +10 -1
  103. package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
  104. package/dist/runtime/query/executor.d.ts +12 -0
  105. package/dist/runtime/query/executor.d.ts.map +1 -1
  106. package/dist/runtime/query/executor.js +43 -9
  107. package/dist/runtime/query/executor.js.map +1 -1
  108. package/dist/runtime/query/index.d.ts +4 -0
  109. package/dist/runtime/query/index.d.ts.map +1 -1
  110. package/dist/runtime/query/index.js +32 -0
  111. package/dist/runtime/query/index.js.map +1 -1
  112. package/dist/runtime/query/review-policy.d.ts +2 -2
  113. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  114. package/dist/runtime/query/review-policy.js +2 -2
  115. package/dist/runtime/query/review-policy.js.map +1 -1
  116. package/dist/runtime/query/shell-line-escalation.d.ts +24 -0
  117. package/dist/runtime/query/shell-line-escalation.d.ts.map +1 -0
  118. package/dist/runtime/query/shell-line-escalation.js +38 -0
  119. package/dist/runtime/query/shell-line-escalation.js.map +1 -0
  120. package/dist/runtime/query/tooling.d.ts +7 -0
  121. package/dist/runtime/query/tooling.d.ts.map +1 -1
  122. package/dist/runtime/query/tooling.js +2 -0
  123. package/dist/runtime/query/tooling.js.map +1 -1
  124. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  125. package/dist/runtime/query/turn/outcome-ledger.js +11 -0
  126. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  127. package/dist/scheduler/local.d.ts.map +1 -1
  128. package/dist/scheduler/local.js +3 -1
  129. package/dist/scheduler/local.js.map +1 -1
  130. package/dist/skills/index.d.ts +4 -0
  131. package/dist/skills/index.d.ts.map +1 -1
  132. package/dist/skills/index.js +2 -0
  133. package/dist/skills/index.js.map +1 -1
  134. package/dist/skills/inline-shell-host.d.ts +103 -0
  135. package/dist/skills/inline-shell-host.d.ts.map +1 -0
  136. package/dist/skills/inline-shell-host.js +238 -0
  137. package/dist/skills/inline-shell-host.js.map +1 -0
  138. package/dist/skills/inline-shell.d.ts +176 -0
  139. package/dist/skills/inline-shell.d.ts.map +1 -0
  140. package/dist/skills/inline-shell.js +646 -0
  141. package/dist/skills/inline-shell.js.map +1 -0
  142. package/dist/store/index.d.ts +2 -0
  143. package/dist/store/index.d.ts.map +1 -1
  144. package/dist/store/index.js +1 -0
  145. package/dist/store/index.js.map +1 -1
  146. package/dist/store/task/file.d.ts +124 -0
  147. package/dist/store/task/file.d.ts.map +1 -0
  148. package/dist/store/task/file.js +498 -0
  149. package/dist/store/task/file.js.map +1 -0
  150. package/dist/teams/backend.d.ts +28 -0
  151. package/dist/teams/backend.d.ts.map +1 -0
  152. package/dist/teams/backend.js +2 -0
  153. package/dist/teams/backend.js.map +1 -0
  154. package/dist/teams/fs-sync.d.ts +19 -0
  155. package/dist/teams/fs-sync.d.ts.map +1 -0
  156. package/dist/teams/fs-sync.js +81 -0
  157. package/dist/teams/fs-sync.js.map +1 -0
  158. package/dist/teams/index.d.ts +14 -0
  159. package/dist/teams/index.d.ts.map +1 -0
  160. package/dist/teams/index.js +11 -0
  161. package/dist/teams/index.js.map +1 -0
  162. package/dist/teams/names.d.ts +15 -0
  163. package/dist/teams/names.d.ts.map +1 -0
  164. package/dist/teams/names.js +43 -0
  165. package/dist/teams/names.js.map +1 -0
  166. package/dist/teams/store.d.ts +133 -0
  167. package/dist/teams/store.d.ts.map +1 -0
  168. package/dist/teams/store.js +536 -0
  169. package/dist/teams/store.js.map +1 -0
  170. package/dist/teams/types.d.ts +97 -0
  171. package/dist/teams/types.d.ts.map +1 -0
  172. package/dist/teams/types.js +70 -0
  173. package/dist/teams/types.js.map +1 -0
  174. package/dist/tools/admit-facts.d.ts.map +1 -1
  175. package/dist/tools/admit-facts.js +9 -1
  176. package/dist/tools/admit-facts.js.map +1 -1
  177. package/dist/tools/builtins/atomic-write-file.d.ts +19 -3
  178. package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
  179. package/dist/tools/builtins/atomic-write-file.js +56 -3
  180. package/dist/tools/builtins/atomic-write-file.js.map +1 -1
  181. package/dist/tools/builtins/computer-use.d.ts +56 -2
  182. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  183. package/dist/tools/builtins/computer-use.js +604 -90
  184. package/dist/tools/builtins/computer-use.js.map +1 -1
  185. package/dist/tools/builtins/index.d.ts +1 -1
  186. package/dist/tools/builtins/index.d.ts.map +1 -1
  187. package/dist/tools/builtins/index.js.map +1 -1
  188. package/dist/tools/builtins/skill.d.ts +31 -0
  189. package/dist/tools/builtins/skill.d.ts.map +1 -1
  190. package/dist/tools/builtins/skill.js +232 -88
  191. package/dist/tools/builtins/skill.js.map +1 -1
  192. package/dist/tools/task/create.d.ts.map +1 -1
  193. package/dist/tools/task/create.js +11 -2
  194. package/dist/tools/task/create.js.map +1 -1
  195. package/dist/tools/task/get.d.ts +8 -0
  196. package/dist/tools/task/get.d.ts.map +1 -0
  197. package/dist/tools/task/get.js +51 -0
  198. package/dist/tools/task/get.js.map +1 -0
  199. package/dist/tools/task/index.d.ts +7 -0
  200. package/dist/tools/task/index.d.ts.map +1 -1
  201. package/dist/tools/task/index.js +10 -2
  202. package/dist/tools/task/index.js.map +1 -1
  203. package/dist/tools/task/list.d.ts.map +1 -1
  204. package/dist/tools/task/list.js +10 -2
  205. package/dist/tools/task/list.js.map +1 -1
  206. package/dist/tools/task/number.d.ts +13 -0
  207. package/dist/tools/task/number.d.ts.map +1 -0
  208. package/dist/tools/task/number.js +30 -0
  209. package/dist/tools/task/number.js.map +1 -0
  210. package/dist/tools/task/present.d.ts.map +1 -1
  211. package/dist/tools/task/present.js +17 -3
  212. package/dist/tools/task/present.js.map +1 -1
  213. package/dist/tools/task/update.d.ts +2 -1
  214. package/dist/tools/task/update.d.ts.map +1 -1
  215. package/dist/tools/task/update.js +86 -19
  216. package/dist/tools/task/update.js.map +1 -1
  217. package/dist/types/acp/index.d.ts +10 -0
  218. package/dist/types/acp/index.d.ts.map +1 -1
  219. package/dist/types/agent/base.d.ts +4 -0
  220. package/dist/types/agent/base.d.ts.map +1 -1
  221. package/dist/types/agent/scheduler.d.ts +9 -0
  222. package/dist/types/agent/scheduler.d.ts.map +1 -1
  223. package/dist/types/computer-use/index.d.ts +7 -0
  224. package/dist/types/computer-use/index.d.ts.map +1 -1
  225. package/dist/types/computer-use/index.js.map +1 -1
  226. package/dist/types/connector/mcp.d.ts +10 -0
  227. package/dist/types/connector/mcp.d.ts.map +1 -1
  228. package/dist/types/permission/index.d.ts +20 -0
  229. package/dist/types/permission/index.d.ts.map +1 -1
  230. package/dist/types/permission/index.js +25 -0
  231. package/dist/types/permission/index.js.map +1 -1
  232. package/dist/types/plugin/index.d.ts +47 -3
  233. package/dist/types/plugin/index.d.ts.map +1 -1
  234. package/dist/types/plugin/index.js +4 -0
  235. package/dist/types/plugin/index.js.map +1 -1
  236. package/dist/types/provider/interface.d.ts +6 -0
  237. package/dist/types/provider/interface.d.ts.map +1 -1
  238. package/dist/types/skills/index.d.ts +7 -0
  239. package/dist/types/skills/index.d.ts.map +1 -1
  240. package/dist/types/skills/index.js.map +1 -1
  241. package/dist/types/tool/index.d.ts +28 -2
  242. package/dist/types/tool/index.d.ts.map +1 -1
  243. package/dist/types/tool/index.js.map +1 -1
  244. package/dist/types/tool/presentation.d.ts +4 -0
  245. package/dist/types/tool/presentation.d.ts.map +1 -1
  246. package/package.json +1 -1
  247. package/src/agents/QueryAgent.ts +2 -0
  248. package/src/authorization/skill-grant.ts +1 -0
  249. package/src/bridge/acp/permission.ts +11 -1
  250. package/src/bridge/acp/server.ts +1 -0
  251. package/src/connector/index.ts +27 -0
  252. package/src/connector/mcp/client.ts +71 -2
  253. package/src/connector/mcp/errors.ts +14 -0
  254. package/src/connector/mcp/index.ts +36 -0
  255. package/src/connector/mcp/skill-source.ts +499 -0
  256. package/src/connector/mcp/skills.ts +367 -0
  257. package/src/network/audit.ts +2 -0
  258. package/src/network/computer.ts +347 -0
  259. package/src/network/index.ts +20 -0
  260. package/src/network/join.ts +4 -2
  261. package/src/network/roster.ts +5 -0
  262. package/src/network/service.ts +492 -75
  263. package/src/network/store.ts +29 -3
  264. package/src/network/wire.ts +5 -0
  265. package/src/persona/assembler.ts +7 -1
  266. package/src/plugin/shell-hook-tool-names.ts +1 -0
  267. package/src/plugin/shell-hook.ts +144 -7
  268. package/src/provider/fallback.ts +4 -0
  269. package/src/provider/idle-timeout.ts +3 -0
  270. package/src/provider/model-window-suffix.ts +1 -0
  271. package/src/provider/retry.ts +3 -0
  272. package/src/public-runtime.ts +99 -0
  273. package/src/public-tools.ts +15 -0
  274. package/src/public-types.ts +28 -0
  275. package/src/runtime/query/executor/tool-call-admission.ts +10 -1
  276. package/src/runtime/query/executor.ts +57 -8
  277. package/src/runtime/query/index.ts +43 -0
  278. package/src/runtime/query/review-policy.ts +7 -1
  279. package/src/runtime/query/shell-line-escalation.ts +49 -0
  280. package/src/runtime/query/tooling.ts +6 -0
  281. package/src/runtime/query/turn/outcome-ledger.ts +11 -0
  282. package/src/scheduler/local.ts +3 -1
  283. package/src/skills/index.ts +43 -0
  284. package/src/skills/inline-shell-host.ts +303 -0
  285. package/src/skills/inline-shell.ts +765 -0
  286. package/src/store/index.ts +8 -0
  287. package/src/store/task/file.ts +641 -0
  288. package/src/teams/backend.ts +29 -0
  289. package/src/teams/fs-sync.ts +91 -0
  290. package/src/teams/index.ts +47 -0
  291. package/src/teams/names.ts +49 -0
  292. package/src/teams/store.ts +607 -0
  293. package/src/teams/types.ts +102 -0
  294. package/src/tools/admit-facts.ts +9 -1
  295. package/src/tools/builtins/atomic-write-file.ts +73 -5
  296. package/src/tools/builtins/computer-use.ts +689 -95
  297. package/src/tools/builtins/index.ts +1 -0
  298. package/src/tools/builtins/skill.ts +291 -95
  299. package/src/tools/task/create.ts +11 -2
  300. package/src/tools/task/get.ts +53 -0
  301. package/src/tools/task/index.ts +14 -2
  302. package/src/tools/task/list.ts +10 -2
  303. package/src/tools/task/number.ts +32 -0
  304. package/src/tools/task/present.ts +18 -3
  305. package/src/tools/task/update.ts +93 -19
  306. package/src/types/acp/index.ts +11 -1
  307. package/src/types/agent/base.ts +6 -0
  308. package/src/types/agent/scheduler.ts +9 -0
  309. package/src/types/computer-use/index.ts +7 -0
  310. package/src/types/connector/mcp.ts +10 -0
  311. package/src/types/permission/index.ts +29 -0
  312. package/src/types/plugin/index.ts +95 -39
  313. package/src/types/provider/interface.ts +7 -0
  314. package/src/types/skills/index.ts +8 -0
  315. package/src/types/tool/index.ts +23 -2
  316. package/src/types/tool/presentation.ts +2 -0
@@ -0,0 +1,303 @@
1
+ /**
2
+ * How a host runs the commands a skill's text holds, through the machinery it
3
+ * already has.
4
+ *
5
+ * - {@link skillShellHookVerdict} reads what `pre_skill_shell_command` hooks
6
+ * answered into one verdict for one command.
7
+ * - {@link createSkillShellHook} asks those hooks, and answers `undefined`
8
+ * when none is attached so nothing is asked of nobody.
9
+ * - {@link createPreToolUseShellHook} asks the `pre_tool_use` hooks the shell
10
+ * tool's own calls are asked, for a load that has no turn to ask them in.
11
+ * - {@link createToolSkillShellHost} runs a command as a nested call of the
12
+ * shell tool from inside another tool (the `skill` tool): the same
13
+ * authorization gate, the same sandbox and the same tool a model's own call
14
+ * goes through, and no second executor.
15
+ * - {@link skillShellAllow} turns a skill's `allowed-tools` into a
16
+ * pre-approval that holds for the one expansion.
17
+ */
18
+
19
+ import { SkillGrantSet, compileSkillGrant } from '../authorization/skill-grant.js'
20
+ import type { PluginLifecycleManager } from '../plugin/lifecycle.js'
21
+ import type { SessionId, TurnId } from '../types/ids/index.js'
22
+ import type { PluginHookResult, PluginSkillShellInfo } from '../types/plugin/index.js'
23
+ import type { ToolContext, ToolDispatchOptions } from '../types/tool/index.js'
24
+ import {
25
+ INLINE_SHELL_REASON_CHARS,
26
+ type InlineShellHookVerdict,
27
+ type InlineShellHost,
28
+ type InlineShellOutcome,
29
+ splitShellToolOutput,
30
+ } from './inline-shell.js'
31
+
32
+ /** The tool a skill's commands run in. */
33
+ export const SKILL_SHELL_TOOL_NAME = 'bash'
34
+
35
+ /** The event a host fires before each command, after permission allowed it. */
36
+ export const PRE_SKILL_SHELL_COMMAND_EVENT = 'pre_skill_shell_command'
37
+
38
+ /** The event the shell tool's own calls are checked by; a load outside a turn asks it too. */
39
+ export const PRE_TOOL_USE_EVENT = 'pre_tool_use'
40
+
41
+ /** The deadline the lifecycle manager gives a hook before it counts as timed out. */
42
+ const MANAGER_TIMEOUT_MESSAGE = 'Hook timeout'
43
+
44
+ type HookManager = Pick<PluginLifecycleManager, 'executeHooks' | 'listHooks'>
45
+
46
+ /**
47
+ * One command's verdict from every hook's answer.
48
+ *
49
+ * - A stop (`halt`) outranks a refusal, which outranks everything else.
50
+ * - A `skip` refuses this command and nothing else.
51
+ * - An in-process hook that asks for approval, or rewrites the command, is
52
+ * refused: a load cannot wait for a person and never runs a changed command.
53
+ * - A hook that did not answer, that crashed or that ran out of time refuses
54
+ * the command: nothing says it is safe. This is how a `pre_tool_use` hook that
55
+ * times out treats the shell tool's own call.
56
+ */
57
+ export function skillShellHookVerdict(
58
+ results: readonly PluginHookResult[],
59
+ attached: number,
60
+ ): InlineShellHookVerdict {
61
+ if (attached > 0 && results.length === 0) {
62
+ return { kind: 'refuse', reason: "the hook didn't answer" }
63
+ }
64
+ const warnings: string[] = []
65
+ const refusals: string[] = []
66
+ let stop: string | undefined
67
+ for (const result of results) {
68
+ const notice = 'notice' in result ? result.notice : undefined
69
+ if (notice) warnings.push(notice)
70
+ switch (result.action) {
71
+ case 'skip':
72
+ refusals.push(result.reason)
73
+ break
74
+ case 'continue':
75
+ if (result.halt !== undefined && stop === undefined) stop = result.halt.reason
76
+ if (result.permission?.decision === 'ask') {
77
+ refusals.push("the hook asked for approval, which a skill can't wait for")
78
+ }
79
+ break
80
+ case 'modify':
81
+ refusals.push("the hook changed the command, which isn't supported here")
82
+ break
83
+ case 'error':
84
+ refusals.push(
85
+ result.message === MANAGER_TIMEOUT_MESSAGE
86
+ ? 'the hook timed out'
87
+ : "the hook couldn't run",
88
+ )
89
+ break
90
+ default:
91
+ break
92
+ }
93
+ }
94
+ const warning = warnings.length > 0 ? warnings.join('\n') : undefined
95
+ if (stop !== undefined) return { kind: 'stop', reason: stop }
96
+ if (refusals.length > 0) {
97
+ const reason = refusals.join('; ').slice(0, INLINE_SHELL_REASON_CHARS * 2)
98
+ return { kind: 'refuse', reason, ...(warning ? { warning } : {}) }
99
+ }
100
+ return { kind: 'run', ...(warning ? { warning } : {}) }
101
+ }
102
+
103
+ export interface SkillShellHookOptions {
104
+ readonly manager: HookManager | undefined
105
+ readonly sessionId: string
106
+ readonly turnId?: string
107
+ readonly signal?: AbortSignal
108
+ /** The skill or command, where it came from, and who asked for it. */
109
+ readonly info: PluginSkillShellInfo
110
+ /** The tool name the hook is told. Default `bash`. */
111
+ readonly toolName?: string
112
+ }
113
+
114
+ /**
115
+ * Ask the hooks about one command. `undefined` when the manager has none for
116
+ * the event, so a session with no such hook pays nothing and fires nothing.
117
+ */
118
+ export function createSkillShellHook(
119
+ options: SkillShellHookOptions,
120
+ ): ((command: string, toolUseId: string) => Promise<InlineShellHookVerdict>) | undefined {
121
+ return createShellEventHook(PRE_SKILL_SHELL_COMMAND_EVENT, options, {
122
+ skillShell: options.info,
123
+ })
124
+ }
125
+
126
+ export interface PreToolUseShellHookOptions {
127
+ readonly manager: HookManager | undefined
128
+ readonly sessionId: string
129
+ readonly turnId?: string
130
+ readonly signal?: AbortSignal
131
+ /** The tool name the hook is told. Default `bash`. */
132
+ readonly toolName?: string
133
+ }
134
+
135
+ /**
136
+ * Ask the session's `pre_tool_use` hooks about one command, as they are asked
137
+ * about the shell tool's own call. Inside a turn the executor does it for a
138
+ * nested call; a load outside a turn (a typed command, a headless prompt, a
139
+ * preload) has no executor, so the host asks here. A refusal, a stop, a request
140
+ * for approval, a rewrite, a timeout or a crash all refuse the command: a load
141
+ * cannot wait for a person and never runs a changed command. `undefined` when
142
+ * no such hook is attached.
143
+ */
144
+ export function createPreToolUseShellHook(
145
+ options: PreToolUseShellHookOptions,
146
+ ): ((command: string, toolUseId: string) => Promise<InlineShellHookVerdict>) | undefined {
147
+ return createShellEventHook(PRE_TOOL_USE_EVENT, options, {})
148
+ }
149
+
150
+ function createShellEventHook(
151
+ event: typeof PRE_SKILL_SHELL_COMMAND_EVENT | typeof PRE_TOOL_USE_EVENT,
152
+ options: PreToolUseShellHookOptions,
153
+ extra: { readonly skillShell?: PluginSkillShellInfo },
154
+ ): ((command: string, toolUseId: string) => Promise<InlineShellHookVerdict>) | undefined {
155
+ const manager = options.manager
156
+ if (manager === undefined) return undefined
157
+ const countAttached = (): number =>
158
+ manager.listHooks().filter((hook) => hook.event === event).length
159
+ if (countAttached() === 0) return undefined
160
+ const toolName = options.toolName ?? SKILL_SHELL_TOOL_NAME
161
+ return async (command, toolUseId) => {
162
+ // Read again for each command: a hook a skill attaches while it loads counts.
163
+ const attached = countAttached()
164
+ if (attached === 0) return { kind: 'run' }
165
+ try {
166
+ const results = await manager.executeHooks(event, {
167
+ sessionId: options.sessionId as SessionId,
168
+ ...(options.turnId !== undefined ? { turnId: options.turnId as TurnId } : {}),
169
+ toolName,
170
+ toolInput: { command },
171
+ toolCallId: toolUseId,
172
+ ...extra,
173
+ ...(options.signal ? { signal: options.signal } : {}),
174
+ })
175
+ return skillShellHookVerdict(results, attached)
176
+ } catch (error) {
177
+ if (options.signal?.aborted) throw error
178
+ return { kind: 'refuse', reason: "the hook couldn't run" }
179
+ }
180
+ }
181
+ }
182
+
183
+ /**
184
+ * The pre-approval a skill's `allowed-tools` gives to its own commands, for
185
+ * one expansion. Only entries for the shell tool count; any other entry is
186
+ * not read here. Absent when the skill grants the shell tool nothing.
187
+ */
188
+ export function skillShellAllow(
189
+ allowedTools: readonly string[] | undefined,
190
+ options: { readonly skillDirectory?: string; readonly toolName?: string } = {},
191
+ ): ToolDispatchOptions['allow'] | undefined {
192
+ if (allowedTools === undefined || allowedTools.length === 0) return undefined
193
+ const toolName = options.toolName ?? SKILL_SHELL_TOOL_NAME
194
+ const compiled = compileSkillGrant(allowedTools, {
195
+ resolveTool: (name) =>
196
+ name.toLowerCase() === toolName ? { name: toolName, commandArgument: 'command' } : undefined,
197
+ ...(options.skillDirectory ? { skillDirectory: options.skillDirectory } : {}),
198
+ })
199
+ if (compiled.entries.length === 0) return undefined
200
+ const held = new SkillGrantSet()
201
+ held.grant('inline-shell', compiled)
202
+ return (call) => held.coveringSkill(call) !== undefined
203
+ }
204
+
205
+ /** Strip the tool's closing `Error: ...` line off a failed result's text. */
206
+ function withoutErrorLine(text: string): string {
207
+ const at = text.lastIndexOf('\n\nError: ')
208
+ if (at !== -1) return text.slice(0, at)
209
+ return text.startsWith('Error: ') ? '' : text
210
+ }
211
+
212
+ export interface ToolSkillShellHostOptions {
213
+ readonly context: ToolContext
214
+ readonly allow?: ToolDispatchOptions['allow']
215
+ readonly toolName?: string
216
+ }
217
+
218
+ /**
219
+ * A host that runs each command by dispatching the shell tool from inside the
220
+ * tool that is loading the text. A command a rule does not allow is refused
221
+ * with the rule's reason, because a person cannot be asked from inside
222
+ * another tool's call.
223
+ */
224
+ export function createToolSkillShellHost(options: ToolSkillShellHostOptions): InlineShellHost {
225
+ const toolName = options.toolName ?? SKILL_SHELL_TOOL_NAME
226
+ const dispatch = options.context.dispatchTool
227
+ return {
228
+ async run(command, beforeRun) {
229
+ if (dispatch === undefined) {
230
+ return {
231
+ kind: 'denied',
232
+ reason: 'this turn cannot run a tool from inside another one',
233
+ }
234
+ }
235
+ let hook: Exclude<InlineShellHookVerdict, { kind: 'run' }> | undefined
236
+ let result: Awaited<ReturnType<typeof dispatch>>
237
+ try {
238
+ result = await dispatch(
239
+ toolName,
240
+ { command },
241
+ {
242
+ ...(options.allow ? { allow: options.allow } : {}),
243
+ beforeRun: async () => {
244
+ const verdict = await beforeRun()
245
+ if (verdict.kind === 'run') return undefined
246
+ hook = verdict
247
+ return 'refused by a hook'
248
+ },
249
+ },
250
+ )
251
+ } catch (error) {
252
+ if (options.context.abortSignal.aborted) {
253
+ return { kind: 'ran', stdout: '', stderr: '', exitCode: null, interrupted: true }
254
+ }
255
+ throw error
256
+ }
257
+ if (hook !== undefined) return { kind: 'hook', verdict: hook }
258
+ return outcomeOf(result)
259
+ },
260
+ }
261
+ }
262
+
263
+ /** A shell tool result as the host's outcome of one command. */
264
+ export function outcomeOf(result: {
265
+ readonly success: boolean
266
+ readonly output: string
267
+ readonly error?: string
268
+ readonly data?: unknown
269
+ }): InlineShellOutcome {
270
+ const data =
271
+ result.data !== null && typeof result.data === 'object'
272
+ ? (result.data as Record<string, unknown>)
273
+ : undefined
274
+ const exitCode = typeof data?.exitCode === 'number' ? data.exitCode : undefined
275
+ const ran = data !== undefined && ('exitCode' in data || 'timedOut' in data)
276
+ if (!ran && !result.success) {
277
+ return { kind: 'denied', reason: deniedReason(result.error ?? result.output) }
278
+ }
279
+ // Through a nested dispatch a failure's text is in `error` (output and the closing line
280
+ // joined); from the tool itself it is in `output`, with the closing line in `error`.
281
+ const text = result.success
282
+ ? result.output
283
+ : result.output !== ''
284
+ ? result.output
285
+ : withoutErrorLine(result.error ?? '')
286
+ const { stdout, stderr } = splitShellToolOutput(text)
287
+ if (result.success) {
288
+ return { kind: 'ran', stdout, stderr, exitCode: exitCode ?? 0 }
289
+ }
290
+ return {
291
+ kind: 'ran',
292
+ stdout,
293
+ stderr,
294
+ exitCode: exitCode ?? null,
295
+ }
296
+ }
297
+
298
+ /** The words after `Tool "x" was not executed.` when that is how the refusal begins. */
299
+ function deniedReason(text: string): string {
300
+ const trimmed = text.replace(/^Error:\s*/, '')
301
+ const match = /^Tool "[^"]*" was not executed\.\s*/.exec(trimmed)
302
+ return (match ? trimmed.slice(match[0].length) : trimmed).trim() || 'refused'
303
+ }