@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.
- package/CHANGELOG.md +68 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +2 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/authorization/skill-grant.d.ts.map +1 -1
- package/dist/authorization/skill-grant.js +1 -0
- package/dist/authorization/skill-grant.js.map +1 -1
- package/dist/bridge/acp/permission.d.ts +10 -0
- package/dist/bridge/acp/permission.d.ts.map +1 -1
- package/dist/bridge/acp/permission.js.map +1 -1
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +1 -0
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/connector/index.d.ts +2 -2
- package/dist/connector/index.d.ts.map +1 -1
- package/dist/connector/index.js +1 -1
- package/dist/connector/index.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +29 -0
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +59 -2
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/mcp/errors.d.ts +7 -0
- package/dist/connector/mcp/errors.d.ts.map +1 -1
- package/dist/connector/mcp/errors.js +14 -0
- package/dist/connector/mcp/errors.js.map +1 -1
- package/dist/connector/mcp/index.d.ts +5 -1
- package/dist/connector/mcp/index.d.ts.map +1 -1
- package/dist/connector/mcp/index.js +3 -1
- package/dist/connector/mcp/index.js.map +1 -1
- package/dist/connector/mcp/skill-source.d.ts +144 -0
- package/dist/connector/mcp/skill-source.d.ts.map +1 -0
- package/dist/connector/mcp/skill-source.js +375 -0
- package/dist/connector/mcp/skill-source.js.map +1 -0
- package/dist/connector/mcp/skills.d.ts +137 -0
- package/dist/connector/mcp/skills.d.ts.map +1 -0
- package/dist/connector/mcp/skills.js +303 -0
- package/dist/connector/mcp/skills.js.map +1 -0
- package/dist/network/audit.d.ts +1 -1
- package/dist/network/audit.d.ts.map +1 -1
- package/dist/network/audit.js +2 -0
- package/dist/network/audit.js.map +1 -1
- package/dist/network/computer.d.ts +107 -0
- package/dist/network/computer.d.ts.map +1 -0
- package/dist/network/computer.js +250 -0
- package/dist/network/computer.js.map +1 -0
- package/dist/network/index.d.ts +2 -0
- package/dist/network/index.d.ts.map +1 -1
- package/dist/network/index.js +1 -0
- package/dist/network/index.js.map +1 -1
- package/dist/network/join.d.ts +3 -1
- package/dist/network/join.d.ts.map +1 -1
- package/dist/network/join.js +6 -3
- package/dist/network/join.js.map +1 -1
- package/dist/network/roster.d.ts +5 -0
- package/dist/network/roster.d.ts.map +1 -1
- package/dist/network/roster.js.map +1 -1
- package/dist/network/service.d.ts +12 -2
- package/dist/network/service.d.ts.map +1 -1
- package/dist/network/service.js +377 -52
- package/dist/network/service.js.map +1 -1
- package/dist/network/store.d.ts +10 -0
- package/dist/network/store.d.ts.map +1 -1
- package/dist/network/store.js +25 -3
- package/dist/network/store.js.map +1 -1
- package/dist/network/wire.d.ts +2 -0
- package/dist/network/wire.d.ts.map +1 -1
- package/dist/network/wire.js +5 -0
- package/dist/network/wire.js.map +1 -1
- package/dist/persona/assembler.d.ts.map +1 -1
- package/dist/persona/assembler.js +7 -1
- package/dist/persona/assembler.js.map +1 -1
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
- package/dist/plugin/shell-hook-tool-names.js +1 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +8 -3
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +132 -7
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +4 -0
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/idle-timeout.d.ts.map +1 -1
- package/dist/provider/idle-timeout.js +3 -0
- package/dist/provider/idle-timeout.js.map +1 -1
- package/dist/provider/model-window-suffix.d.ts.map +1 -1
- package/dist/provider/model-window-suffix.js +1 -0
- package/dist/provider/model-window-suffix.js.map +1 -1
- package/dist/provider/retry.d.ts.map +1 -1
- package/dist/provider/retry.js +3 -0
- package/dist/provider/retry.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -6
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +11 -4
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +7 -4
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +9 -3
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +3 -1
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.js +10 -1
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +12 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +43 -9
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +4 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +32 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +2 -2
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +2 -2
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/shell-line-escalation.d.ts +24 -0
- package/dist/runtime/query/shell-line-escalation.d.ts.map +1 -0
- package/dist/runtime/query/shell-line-escalation.js +38 -0
- package/dist/runtime/query/shell-line-escalation.js.map +1 -0
- package/dist/runtime/query/tooling.d.ts +7 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +2 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +11 -0
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/scheduler/local.d.ts.map +1 -1
- package/dist/scheduler/local.js +3 -1
- package/dist/scheduler/local.js.map +1 -1
- package/dist/skills/index.d.ts +4 -0
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +2 -0
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/inline-shell-host.d.ts +103 -0
- package/dist/skills/inline-shell-host.d.ts.map +1 -0
- package/dist/skills/inline-shell-host.js +238 -0
- package/dist/skills/inline-shell-host.js.map +1 -0
- package/dist/skills/inline-shell.d.ts +176 -0
- package/dist/skills/inline-shell.d.ts.map +1 -0
- package/dist/skills/inline-shell.js +646 -0
- package/dist/skills/inline-shell.js.map +1 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +1 -0
- package/dist/store/index.js.map +1 -1
- package/dist/store/task/file.d.ts +124 -0
- package/dist/store/task/file.d.ts.map +1 -0
- package/dist/store/task/file.js +498 -0
- package/dist/store/task/file.js.map +1 -0
- package/dist/teams/backend.d.ts +28 -0
- package/dist/teams/backend.d.ts.map +1 -0
- package/dist/teams/backend.js +2 -0
- package/dist/teams/backend.js.map +1 -0
- package/dist/teams/fs-sync.d.ts +19 -0
- package/dist/teams/fs-sync.d.ts.map +1 -0
- package/dist/teams/fs-sync.js +81 -0
- package/dist/teams/fs-sync.js.map +1 -0
- package/dist/teams/index.d.ts +14 -0
- package/dist/teams/index.d.ts.map +1 -0
- package/dist/teams/index.js +11 -0
- package/dist/teams/index.js.map +1 -0
- package/dist/teams/names.d.ts +15 -0
- package/dist/teams/names.d.ts.map +1 -0
- package/dist/teams/names.js +43 -0
- package/dist/teams/names.js.map +1 -0
- package/dist/teams/store.d.ts +133 -0
- package/dist/teams/store.d.ts.map +1 -0
- package/dist/teams/store.js +536 -0
- package/dist/teams/store.js.map +1 -0
- package/dist/teams/types.d.ts +97 -0
- package/dist/teams/types.d.ts.map +1 -0
- package/dist/teams/types.js +70 -0
- package/dist/teams/types.js.map +1 -0
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +9 -1
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/atomic-write-file.d.ts +19 -3
- package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
- package/dist/tools/builtins/atomic-write-file.js +56 -3
- package/dist/tools/builtins/atomic-write-file.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +56 -2
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +604 -90
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +1 -1
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts +31 -0
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +232 -88
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/task/create.d.ts.map +1 -1
- package/dist/tools/task/create.js +11 -2
- package/dist/tools/task/create.js.map +1 -1
- package/dist/tools/task/get.d.ts +8 -0
- package/dist/tools/task/get.d.ts.map +1 -0
- package/dist/tools/task/get.js +51 -0
- package/dist/tools/task/get.js.map +1 -0
- package/dist/tools/task/index.d.ts +7 -0
- package/dist/tools/task/index.d.ts.map +1 -1
- package/dist/tools/task/index.js +10 -2
- package/dist/tools/task/index.js.map +1 -1
- package/dist/tools/task/list.d.ts.map +1 -1
- package/dist/tools/task/list.js +10 -2
- package/dist/tools/task/list.js.map +1 -1
- package/dist/tools/task/number.d.ts +13 -0
- package/dist/tools/task/number.d.ts.map +1 -0
- package/dist/tools/task/number.js +30 -0
- package/dist/tools/task/number.js.map +1 -0
- package/dist/tools/task/present.d.ts.map +1 -1
- package/dist/tools/task/present.js +17 -3
- package/dist/tools/task/present.js.map +1 -1
- package/dist/tools/task/update.d.ts +2 -1
- package/dist/tools/task/update.d.ts.map +1 -1
- package/dist/tools/task/update.js +86 -19
- package/dist/tools/task/update.js.map +1 -1
- package/dist/types/acp/index.d.ts +10 -0
- package/dist/types/acp/index.d.ts.map +1 -1
- package/dist/types/agent/base.d.ts +4 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/agent/scheduler.d.ts +9 -0
- package/dist/types/agent/scheduler.d.ts.map +1 -1
- package/dist/types/computer-use/index.d.ts +7 -0
- package/dist/types/computer-use/index.d.ts.map +1 -1
- package/dist/types/computer-use/index.js.map +1 -1
- package/dist/types/connector/mcp.d.ts +10 -0
- package/dist/types/connector/mcp.d.ts.map +1 -1
- package/dist/types/permission/index.d.ts +20 -0
- package/dist/types/permission/index.d.ts.map +1 -1
- package/dist/types/permission/index.js +25 -0
- package/dist/types/permission/index.js.map +1 -1
- package/dist/types/plugin/index.d.ts +47 -3
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +4 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/provider/interface.d.ts +6 -0
- package/dist/types/provider/interface.d.ts.map +1 -1
- package/dist/types/skills/index.d.ts +7 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/dist/types/skills/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +28 -2
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +4 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +2 -0
- package/src/authorization/skill-grant.ts +1 -0
- package/src/bridge/acp/permission.ts +11 -1
- package/src/bridge/acp/server.ts +1 -0
- package/src/connector/index.ts +27 -0
- package/src/connector/mcp/client.ts +71 -2
- package/src/connector/mcp/errors.ts +14 -0
- package/src/connector/mcp/index.ts +36 -0
- package/src/connector/mcp/skill-source.ts +499 -0
- package/src/connector/mcp/skills.ts +367 -0
- package/src/network/audit.ts +2 -0
- package/src/network/computer.ts +347 -0
- package/src/network/index.ts +20 -0
- package/src/network/join.ts +4 -2
- package/src/network/roster.ts +5 -0
- package/src/network/service.ts +492 -75
- package/src/network/store.ts +29 -3
- package/src/network/wire.ts +5 -0
- package/src/persona/assembler.ts +7 -1
- package/src/plugin/shell-hook-tool-names.ts +1 -0
- package/src/plugin/shell-hook.ts +144 -7
- package/src/provider/fallback.ts +4 -0
- package/src/provider/idle-timeout.ts +3 -0
- package/src/provider/model-window-suffix.ts +1 -0
- package/src/provider/retry.ts +3 -0
- package/src/public-runtime.ts +99 -0
- package/src/public-tools.ts +15 -0
- package/src/public-types.ts +28 -0
- package/src/runtime/query/executor/tool-call-admission.ts +10 -1
- package/src/runtime/query/executor.ts +57 -8
- package/src/runtime/query/index.ts +43 -0
- package/src/runtime/query/review-policy.ts +7 -1
- package/src/runtime/query/shell-line-escalation.ts +49 -0
- package/src/runtime/query/tooling.ts +6 -0
- package/src/runtime/query/turn/outcome-ledger.ts +11 -0
- package/src/scheduler/local.ts +3 -1
- package/src/skills/index.ts +43 -0
- package/src/skills/inline-shell-host.ts +303 -0
- package/src/skills/inline-shell.ts +765 -0
- package/src/store/index.ts +8 -0
- package/src/store/task/file.ts +641 -0
- package/src/teams/backend.ts +29 -0
- package/src/teams/fs-sync.ts +91 -0
- package/src/teams/index.ts +47 -0
- package/src/teams/names.ts +49 -0
- package/src/teams/store.ts +607 -0
- package/src/teams/types.ts +102 -0
- package/src/tools/admit-facts.ts +9 -1
- package/src/tools/builtins/atomic-write-file.ts +73 -5
- package/src/tools/builtins/computer-use.ts +689 -95
- package/src/tools/builtins/index.ts +1 -0
- package/src/tools/builtins/skill.ts +291 -95
- package/src/tools/task/create.ts +11 -2
- package/src/tools/task/get.ts +53 -0
- package/src/tools/task/index.ts +14 -2
- package/src/tools/task/list.ts +10 -2
- package/src/tools/task/number.ts +32 -0
- package/src/tools/task/present.ts +18 -3
- package/src/tools/task/update.ts +93 -19
- package/src/types/acp/index.ts +11 -1
- package/src/types/agent/base.ts +6 -0
- package/src/types/agent/scheduler.ts +9 -0
- package/src/types/computer-use/index.ts +7 -0
- package/src/types/connector/mcp.ts +10 -0
- package/src/types/permission/index.ts +29 -0
- package/src/types/plugin/index.ts +95 -39
- package/src/types/provider/interface.ts +7 -0
- package/src/types/skills/index.ts +8 -0
- package/src/types/tool/index.ts +23 -2
- 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
|
+
}
|