@namzu/sdk 63.1.0 → 64.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.
- package/CHANGELOG.md +96 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +2 -1
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/agents/file-definitions.d.ts +10 -0
- package/dist/agents/file-definitions.d.ts.map +1 -1
- package/dist/agents/file-definitions.js +10 -2
- package/dist/agents/file-definitions.js.map +1 -1
- package/dist/compaction/hooks.d.ts +57 -0
- package/dist/compaction/hooks.d.ts.map +1 -0
- package/dist/compaction/hooks.js +44 -0
- package/dist/compaction/hooks.js.map +1 -0
- package/dist/compaction/manual.d.ts +9 -0
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +74 -5
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/model-summary.d.ts +6 -0
- package/dist/compaction/model-summary.d.ts.map +1 -1
- package/dist/compaction/model-summary.js +13 -3
- package/dist/compaction/model-summary.js.map +1 -1
- package/dist/compaction/verifier.d.ts +8 -2
- package/dist/compaction/verifier.d.ts.map +1 -1
- package/dist/compaction/verifier.js +23 -7
- package/dist/compaction/verifier.js.map +1 -1
- package/dist/plugin/file-change-hooks.d.ts +53 -0
- package/dist/plugin/file-change-hooks.d.ts.map +1 -0
- package/dist/plugin/file-change-hooks.js +111 -0
- package/dist/plugin/file-change-hooks.js.map +1 -0
- package/dist/plugin/file-change-watch.d.ts +111 -0
- package/dist/plugin/file-change-watch.d.ts.map +1 -0
- package/dist/plugin/file-change-watch.js +324 -0
- package/dist/plugin/file-change-watch.js.map +1 -0
- package/dist/plugin/hook-context.d.ts +16 -0
- package/dist/plugin/hook-context.d.ts.map +1 -1
- package/dist/plugin/hook-context.js +40 -0
- package/dist/plugin/hook-context.js.map +1 -1
- package/dist/plugin/index.d.ts +14 -4
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +10 -3
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +60 -1
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +118 -7
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +24 -1
- package/dist/plugin/loader.js.map +1 -1
- package/dist/plugin/session-hook-env.d.ts +140 -0
- package/dist/plugin/session-hook-env.d.ts.map +1 -0
- package/dist/plugin/session-hook-env.js +458 -0
- package/dist/plugin/session-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook-command.d.ts +25 -0
- package/dist/plugin/shell-hook-command.d.ts.map +1 -0
- package/dist/plugin/shell-hook-command.js +218 -0
- package/dist/plugin/shell-hook-command.js.map +1 -0
- package/dist/plugin/shell-hook-env.d.ts +107 -0
- package/dist/plugin/shell-hook-env.d.ts.map +1 -0
- package/dist/plugin/shell-hook-env.js +272 -0
- package/dist/plugin/shell-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook-tool-names.d.ts +20 -0
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -0
- package/dist/plugin/shell-hook-tool-names.js +43 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -0
- package/dist/plugin/shell-hook.d.ts +167 -22
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +651 -106
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/plugin/worktree-hook-answer.d.ts +26 -0
- package/dist/plugin/worktree-hook-answer.d.ts.map +1 -0
- package/dist/plugin/worktree-hook-answer.js +132 -0
- package/dist/plugin/worktree-hook-answer.js.map +1 -0
- package/dist/public-runtime.d.ts +5 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +4 -3
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +9 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/compact-session.d.ts +7 -1
- package/dist/runtime/query/compact-session.d.ts.map +1 -1
- package/dist/runtime/query/compact-session.js +8 -3
- package/dist/runtime/query/compact-session.js.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.js +4 -3
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +5 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +8 -3
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/finalize-turn.d.ts.map +1 -1
- package/dist/runtime/query/finalize-turn.js +9 -2
- package/dist/runtime/query/finalize-turn.js.map +1 -1
- package/dist/runtime/query/index.d.ts +9 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +3 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +2 -1
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +24 -12
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +2 -0
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/plugin-hooks.d.ts +13 -0
- package/dist/runtime/query/plugin-hooks.d.ts.map +1 -1
- package/dist/runtime/query/plugin-hooks.js +29 -2
- package/dist/runtime/query/plugin-hooks.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +5 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +3 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/session/workspace/hooked-driver.d.ts +72 -0
- package/dist/session/workspace/hooked-driver.d.ts.map +1 -0
- package/dist/session/workspace/hooked-driver.js +204 -0
- package/dist/session/workspace/hooked-driver.js.map +1 -0
- package/dist/session/workspace/index.d.ts +2 -0
- package/dist/session/workspace/index.d.ts.map +1 -1
- package/dist/session/workspace/index.js +1 -0
- package/dist/session/workspace/index.js.map +1 -1
- package/dist/skills/index.d.ts +1 -1
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +1 -1
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/loader.d.ts +22 -0
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +41 -0
- package/dist/skills/loader.js.map +1 -1
- package/dist/tools/builtins/bash-cwd.d.ts +6 -1
- package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
- package/dist/tools/builtins/bash-cwd.js +27 -2
- package/dist/tools/builtins/bash-cwd.js.map +1 -1
- package/dist/tools/builtins/bash.js +1 -1
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +93 -16
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/types/agent/query.d.ts +8 -0
- package/dist/types/agent/query.d.ts.map +1 -1
- package/dist/types/plugin/index.d.ts +130 -13
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +5 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/skills/index.d.ts +8 -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 +68 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/workspace/ref.d.ts +6 -0
- package/dist/types/workspace/ref.d.ts.map +1 -1
- package/dist/utils/frontmatter.d.ts +16 -0
- package/dist/utils/frontmatter.d.ts.map +1 -1
- package/dist/utils/frontmatter.js +31 -1
- package/dist/utils/frontmatter.js.map +1 -1
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +2 -1
- package/src/agents/file-definitions.ts +17 -2
- package/src/compaction/hooks.ts +104 -0
- package/src/compaction/manual.ts +117 -4
- package/src/compaction/model-summary.ts +19 -3
- package/src/compaction/verifier.ts +33 -3
- package/src/plugin/file-change-hooks.ts +131 -0
- package/src/plugin/file-change-watch.ts +367 -0
- package/src/plugin/hook-context.ts +46 -0
- package/src/plugin/index.ts +53 -1
- package/src/plugin/lifecycle.ts +162 -8
- package/src/plugin/loader.ts +22 -1
- package/src/plugin/session-hook-env.ts +475 -0
- package/src/plugin/shell-hook-command.ts +215 -0
- package/src/plugin/shell-hook-env.ts +335 -0
- package/src/plugin/shell-hook-tool-names.ts +46 -0
- package/src/plugin/shell-hook.ts +818 -100
- package/src/plugin/worktree-hook-answer.ts +142 -0
- package/src/public-runtime.ts +44 -0
- package/src/public-types.ts +22 -1
- package/src/runtime/query/compact-session.ts +16 -4
- package/src/runtime/query/executor/tool-call-admission.ts +10 -2
- package/src/runtime/query/executor.ts +13 -2
- package/src/runtime/query/finalize-turn.ts +9 -2
- package/src/runtime/query/index.ts +12 -0
- package/src/runtime/query/iteration/index.ts +2 -1
- package/src/runtime/query/iteration/phases/compaction.ts +31 -12
- package/src/runtime/query/iteration/phases/tool-review.ts +2 -0
- package/src/runtime/query/plugin-hooks.ts +32 -2
- package/src/runtime/query/tooling.ts +8 -0
- package/src/session/workspace/hooked-driver.ts +272 -0
- package/src/session/workspace/index.ts +7 -0
- package/src/skills/index.ts +3 -0
- package/src/skills/loader.ts +47 -0
- package/src/tools/builtins/bash-cwd.ts +25 -1
- package/src/tools/builtins/bash.ts +1 -1
- package/src/tools/builtins/skill.ts +115 -26
- package/src/types/agent/query.ts +9 -0
- package/src/types/plugin/index.ts +150 -23
- package/src/types/skills/index.ts +9 -0
- package/src/types/tool/index.ts +74 -0
- package/src/types/workspace/ref.ts +6 -0
- package/src/utils/frontmatter.ts +52 -1
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment a shell hook command is started with, beyond what the
|
|
3
|
+
* kernel always sets: the entry's own `env` map, and variables a moved
|
|
4
|
+
* command expects.
|
|
5
|
+
*
|
|
6
|
+
* A hooks block brought from elsewhere often starts a command with a variable
|
|
7
|
+
* for the project folder, named for the tool it was written for. That tool's
|
|
8
|
+
* name is not ours to know, so the rule here reads the shape of the name: a
|
|
9
|
+
* variable the command or its arguments refer to, that is not set, and whose
|
|
10
|
+
* name ends in one of {@link MOVED_FOLDER_ENDINGS}, is set to that folder.
|
|
11
|
+
* Nothing already set is ever replaced.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** A name a hook is started with that `env` may not replace: the kernel sets these itself. */
|
|
15
|
+
export const SHELL_HOOK_RESERVED_ENV: readonly string[] = [
|
|
16
|
+
'NAMZU_HOOK_EVENT',
|
|
17
|
+
'NAMZU_SESSION_ID',
|
|
18
|
+
'NAMZU_PROJECT_DIR',
|
|
19
|
+
'NAMZU_TURN_ID',
|
|
20
|
+
'NAMZU_TOOL_NAME',
|
|
21
|
+
'NAMZU_TOOL_PATH',
|
|
22
|
+
'NAMZU_ENV_FILE',
|
|
23
|
+
'NAMZU_STARTED_BY_SESSION',
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
/** The folders a name's ending can stand for. */
|
|
27
|
+
export type MovedFolder = 'project' | 'plugin' | 'pluginData' | 'skill' | 'envFile'
|
|
28
|
+
|
|
29
|
+
/** Endings read by shape; the name looked for is the whole variable name. */
|
|
30
|
+
export const MOVED_FOLDER_ENDINGS: readonly {
|
|
31
|
+
readonly ending: string
|
|
32
|
+
readonly folder: MovedFolder
|
|
33
|
+
}[] = [
|
|
34
|
+
{ ending: '_PROJECT_DIR', folder: 'project' },
|
|
35
|
+
// A plugin's own folder and the folder kept for its data. These are set only
|
|
36
|
+
// for a hook entry a plugin ships; a name like this in a config file's entry
|
|
37
|
+
// has no folder to stand for and is left unset (and reported).
|
|
38
|
+
{ ending: '_PLUGIN_ROOT', folder: 'plugin' },
|
|
39
|
+
{ ending: '_PLUGIN_DATA', folder: 'pluginData' },
|
|
40
|
+
// A skill's own folder, for a hook that came from a skill. A skill moved as
|
|
41
|
+
// written names its folder `..._PLUGIN_ROOT` (see `movedFolderFor`); it has
|
|
42
|
+
// no data folder, so `_PLUGIN_DATA` is never given to one.
|
|
43
|
+
{ ending: '_SKILL_ROOT', folder: 'skill' },
|
|
44
|
+
// The file a hook may append `export NAME=value` lines to, on the events
|
|
45
|
+
// that have one (`session_start`, `cwd_changed`, `file_changed`): the same
|
|
46
|
+
// file `NAMZU_ENV_FILE` names. On any other event there is no such file and
|
|
47
|
+
// the name is left unset (and reported).
|
|
48
|
+
{ ending: '_ENV_FILE', folder: 'envFile' },
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The folder a variable name stands for, or `undefined` when it has none here.
|
|
53
|
+
* A skill's folder answers `_SKILL_ROOT` and also `_PLUGIN_ROOT`, unless the
|
|
54
|
+
* entry belongs to a plugin (then `_PLUGIN_ROOT` is the plugin's folder).
|
|
55
|
+
*/
|
|
56
|
+
export function movedFolderFor(
|
|
57
|
+
name: string,
|
|
58
|
+
folders: Readonly<Partial<Record<MovedFolder, string>>>,
|
|
59
|
+
): string | undefined {
|
|
60
|
+
const moved = MOVED_FOLDER_ENDINGS.find((each) => name.endsWith(each.ending))
|
|
61
|
+
if (moved === undefined) return undefined
|
|
62
|
+
if (moved.folder === 'plugin') return folders.plugin ?? folders.skill
|
|
63
|
+
return folders[moved.folder]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The folders a plugin-shipped entry is given. */
|
|
67
|
+
export interface PluginFolders {
|
|
68
|
+
/** The plugin's own folder. */
|
|
69
|
+
readonly root: string
|
|
70
|
+
/** The folder kept for the plugin's data, under the Namzu home and never inside `root`. */
|
|
71
|
+
readonly dataDir: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Whether a variable name stands for a plugin folder by its ending. */
|
|
75
|
+
export function isPluginFolderVariable(name: string): boolean {
|
|
76
|
+
return MOVED_FOLDER_ENDINGS.some(
|
|
77
|
+
(each) =>
|
|
78
|
+
(each.folder === 'plugin' || each.folder === 'pluginData') && name.endsWith(each.ending),
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* `${NAME}` and `$NAME` in a command or argument, for a name that ends in
|
|
84
|
+
* `_PLUGIN_ROOT` or `_PLUGIN_DATA`, become the folder's path, so an entry in
|
|
85
|
+
* the exec form (which no shell reads) finds its own folder too. A reference
|
|
86
|
+
* with a modifier (`${NAME:-x}`) is left to the shell, which has the variable.
|
|
87
|
+
*/
|
|
88
|
+
export function substitutePluginFolders(text: string, folders: PluginFolders | undefined): string {
|
|
89
|
+
if (folders === undefined) return text
|
|
90
|
+
return text.replace(
|
|
91
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g,
|
|
92
|
+
(whole: string, braced: string | undefined, bare: string | undefined) => {
|
|
93
|
+
const name = (braced ?? bare) as string
|
|
94
|
+
const moved = MOVED_FOLDER_ENDINGS.find((each) => name.endsWith(each.ending))
|
|
95
|
+
if (moved === undefined) return whole
|
|
96
|
+
if (moved.folder === 'plugin') return folders.root
|
|
97
|
+
if (moved.folder === 'pluginData') return folders.dataDir
|
|
98
|
+
return whole
|
|
99
|
+
},
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* For the exec form, which no shell reads: `${NAME}` and `$NAME` for a name
|
|
105
|
+
* that ends in one of {@link MOVED_FOLDER_ENDINGS} become the value the hook is
|
|
106
|
+
* started with (`env` is the environment {@link movedEnvironment} built, so a
|
|
107
|
+
* value the entry or the host already set wins). A reference with a modifier is
|
|
108
|
+
* left as written, as is a name with no value.
|
|
109
|
+
*/
|
|
110
|
+
export function substituteMovedFolders(
|
|
111
|
+
text: string,
|
|
112
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
113
|
+
): string {
|
|
114
|
+
return text.replace(
|
|
115
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g,
|
|
116
|
+
(whole: string, braced: string | undefined, bare: string | undefined) => {
|
|
117
|
+
const name = (braced ?? bare) as string
|
|
118
|
+
if (!MOVED_FOLDER_ENDINGS.some((each) => name.endsWith(each.ending))) return whole
|
|
119
|
+
return env[name] ?? whole
|
|
120
|
+
},
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The program that reads a shell hook's command, found on the host's own
|
|
126
|
+
* `PATH`. An entry may set `PATH` in its `env` to something that does not hold
|
|
127
|
+
* `sh`; the command is still read by it, so the shell is looked up before that
|
|
128
|
+
* `env` is applied. `undefined` when no change is needed or none is found.
|
|
129
|
+
*/
|
|
130
|
+
export function shellPathOutsideEnv(
|
|
131
|
+
name: string,
|
|
132
|
+
hostPath: string | undefined,
|
|
133
|
+
startedWith: string | undefined,
|
|
134
|
+
isExecutable: (path: string) => boolean,
|
|
135
|
+
separator: string,
|
|
136
|
+
): string | undefined {
|
|
137
|
+
if (hostPath === undefined || hostPath === startedWith) return undefined
|
|
138
|
+
for (const dir of hostPath.split(separator)) {
|
|
139
|
+
if (dir === '') continue
|
|
140
|
+
const candidate = `${dir.replace(/[\\/]$/, '')}/${name}`
|
|
141
|
+
if (isExecutable(candidate)) return candidate
|
|
142
|
+
}
|
|
143
|
+
return undefined
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
147
|
+
|
|
148
|
+
/** What is wrong with an `env` name, or `undefined` when it can be used. */
|
|
149
|
+
export function shellHookEnvNameProblem(name: string): string | undefined {
|
|
150
|
+
if (!NAME.test(name)) {
|
|
151
|
+
return 'is not a valid variable name (letters, digits and underscores, not starting with a digit)'
|
|
152
|
+
}
|
|
153
|
+
if (SHELL_HOOK_RESERVED_ENV.includes(name)) {
|
|
154
|
+
return 'is set by Namzu for every hook and cannot be replaced'
|
|
155
|
+
}
|
|
156
|
+
return undefined
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The problems in an `env` value as a whole, each naming the variable. Empty when it is usable. */
|
|
160
|
+
export function shellHookEnvProblems(env: unknown): string[] {
|
|
161
|
+
if (env === null || typeof env !== 'object' || Array.isArray(env)) {
|
|
162
|
+
return ['must be a mapping of variable names to text']
|
|
163
|
+
}
|
|
164
|
+
const problems: string[] = []
|
|
165
|
+
for (const [name, value] of Object.entries(env)) {
|
|
166
|
+
const bad = shellHookEnvNameProblem(name)
|
|
167
|
+
if (bad !== undefined) problems.push(`${JSON.stringify(name)} ${bad}`)
|
|
168
|
+
else if (typeof value !== 'string') problems.push(`${JSON.stringify(name)} must be text`)
|
|
169
|
+
}
|
|
170
|
+
return problems
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// `$NAME`, `${NAME}` and `${NAME<modifier>}`: group 2 is the modifier text.
|
|
174
|
+
const REFERENCE = /\$(?:\{([A-Za-z_][A-Za-z0-9_]*)([^}]*)\}|([A-Za-z_][A-Za-z0-9_]*))/g
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The variables the texts refer to, in order of first appearance. A reference
|
|
178
|
+
* with a default (`${NAME:-x}`, `${NAME-x}`, `${NAME:+x}`, `${NAME=x}`) is
|
|
179
|
+
* left out unless `withDefaults` is set: it works without the variable.
|
|
180
|
+
*/
|
|
181
|
+
export function referencedVariables(
|
|
182
|
+
texts: readonly string[],
|
|
183
|
+
options: { readonly withDefaults?: boolean } = {},
|
|
184
|
+
): string[] {
|
|
185
|
+
const seen = new Set<string>()
|
|
186
|
+
for (const text of texts) {
|
|
187
|
+
for (const match of text.matchAll(REFERENCE)) {
|
|
188
|
+
const name = match[1] ?? match[3]
|
|
189
|
+
if (name === undefined) continue
|
|
190
|
+
const modifier = match[2] ?? ''
|
|
191
|
+
const hasDefault = /^:?[-+=]/.test(modifier)
|
|
192
|
+
if (hasDefault && options.withDefaults !== true) continue
|
|
193
|
+
seen.add(name)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return [...seen]
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Whether the command text itself gives the variable a value (`NAME=...`,
|
|
201
|
+
* `for NAME in`, `select NAME in`, `read NAME`, `getopts opts NAME`).
|
|
202
|
+
*/
|
|
203
|
+
export function assignedWithin(texts: readonly string[], name: string): boolean {
|
|
204
|
+
const assign = new RegExp(`(?<![A-Za-z0-9_$])${name}=`)
|
|
205
|
+
const loop = new RegExp(`\\b(?:for|select)\\s+${name}\\b`)
|
|
206
|
+
const read = new RegExp(`\\bread\\s+(?:-\\w+\\s+)*(?:[A-Za-z_][A-Za-z0-9_]*\\s+)*${name}\\b`)
|
|
207
|
+
const opts = new RegExp(`\\bgetopts\\s+\\S+\\s+${name}\\b`)
|
|
208
|
+
return texts.some(
|
|
209
|
+
(text) => assign.test(text) || loop.test(text) || read.test(text) || opts.test(text),
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Variables a shell sets or keeps for itself; a command that reads one is not missing a value. */
|
|
214
|
+
const SHELL_OWN_VARIABLES: ReadonlySet<string> = new Set([
|
|
215
|
+
'BASH',
|
|
216
|
+
'BASHOPTS',
|
|
217
|
+
'BASHPID',
|
|
218
|
+
'BASH_ARGC',
|
|
219
|
+
'BASH_ARGV',
|
|
220
|
+
'BASH_COMMAND',
|
|
221
|
+
'BASH_LINENO',
|
|
222
|
+
'BASH_REMATCH',
|
|
223
|
+
'BASH_SOURCE',
|
|
224
|
+
'BASH_SUBSHELL',
|
|
225
|
+
'BASH_VERSINFO',
|
|
226
|
+
'BASH_VERSION',
|
|
227
|
+
'COLUMNS',
|
|
228
|
+
'EUID',
|
|
229
|
+
'FUNCNAME',
|
|
230
|
+
'GROUPS',
|
|
231
|
+
'HOSTNAME',
|
|
232
|
+
'HOSTTYPE',
|
|
233
|
+
'IFS',
|
|
234
|
+
'LINENO',
|
|
235
|
+
'LINES',
|
|
236
|
+
'MACHTYPE',
|
|
237
|
+
'OLDPWD',
|
|
238
|
+
'OPTARG',
|
|
239
|
+
'OPTERR',
|
|
240
|
+
'OPTIND',
|
|
241
|
+
'OSTYPE',
|
|
242
|
+
'PIPESTATUS',
|
|
243
|
+
'PPID',
|
|
244
|
+
'PS1',
|
|
245
|
+
'PS2',
|
|
246
|
+
'PS4',
|
|
247
|
+
'PWD',
|
|
248
|
+
'RANDOM',
|
|
249
|
+
'REPLY',
|
|
250
|
+
'SECONDS',
|
|
251
|
+
'SHELLOPTS',
|
|
252
|
+
'SHLVL',
|
|
253
|
+
'UID',
|
|
254
|
+
])
|
|
255
|
+
|
|
256
|
+
/** Whether the name is one a shell keeps for itself (`RANDOM`, `PPID`, `PWD`, ...). */
|
|
257
|
+
export function isShellOwnVariable(name: string): boolean {
|
|
258
|
+
return SHELL_OWN_VARIABLES.has(name)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** `$NAME` and `${NAME}` in an `env` value become the value the hook already has; any other text stays as written. */
|
|
262
|
+
export function expandHookEnvValue(
|
|
263
|
+
value: string,
|
|
264
|
+
have: Readonly<Record<string, string | undefined>>,
|
|
265
|
+
): string {
|
|
266
|
+
return value.replace(
|
|
267
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g,
|
|
268
|
+
(whole: string, braced: string | undefined, bare: string | undefined) => {
|
|
269
|
+
const found = have[(braced ?? bare) as string]
|
|
270
|
+
return found === undefined ? whole : found
|
|
271
|
+
},
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface MovedEnvironment {
|
|
276
|
+
/** The environment to start the command with. */
|
|
277
|
+
readonly env: Record<string, string | undefined>
|
|
278
|
+
/** Variables the command refers to that are set nowhere (and not by the command itself). */
|
|
279
|
+
readonly unset: readonly string[]
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Layer the entry's `env` map and the by-shape variables over `base` (the
|
|
284
|
+
* inherited environment with the kernel's own variables). `kernel` holds the
|
|
285
|
+
* variables the kernel always sets; they are applied last and win.
|
|
286
|
+
*/
|
|
287
|
+
export function movedEnvironment(
|
|
288
|
+
entry: { readonly command: string; readonly args?: readonly string[]; readonly env?: unknown },
|
|
289
|
+
base: Readonly<Record<string, string | undefined>>,
|
|
290
|
+
kernel: Readonly<Record<string, string | undefined>>,
|
|
291
|
+
folders: Readonly<Partial<Record<MovedFolder, string>>>,
|
|
292
|
+
): MovedEnvironment {
|
|
293
|
+
const env: Record<string, string | undefined> = { ...base }
|
|
294
|
+
if (entry.env !== null && typeof entry.env === 'object' && !Array.isArray(entry.env)) {
|
|
295
|
+
for (const [name, value] of Object.entries(entry.env)) {
|
|
296
|
+
if (shellHookEnvNameProblem(name) !== undefined || typeof value !== 'string') continue
|
|
297
|
+
env[name] = expandHookEnvValue(value, base)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
Object.assign(env, kernel)
|
|
301
|
+
const texts = [entry.command, ...(entry.args ?? [])]
|
|
302
|
+
const unset: string[] = []
|
|
303
|
+
for (const name of referencedVariables(texts)) {
|
|
304
|
+
if (env[name] !== undefined) continue
|
|
305
|
+
const folder = movedFolderFor(name, folders)
|
|
306
|
+
if (folder !== undefined) env[name] = folder
|
|
307
|
+
else if (!assignedWithin(texts, name) && !isShellOwnVariable(name)) unset.push(name)
|
|
308
|
+
}
|
|
309
|
+
return { env, unset }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The sentence added to a hook's failure when it could not find what it ran
|
|
314
|
+
* and its command refers to variables that are set nowhere; `undefined` when
|
|
315
|
+
* the failure does not look like a missing program or file, or nothing is unset.
|
|
316
|
+
*/
|
|
317
|
+
export function hookUnsetVariableHint(outcome: {
|
|
318
|
+
readonly exitCode: number | null
|
|
319
|
+
readonly stderr: string
|
|
320
|
+
readonly spawnError?: string
|
|
321
|
+
readonly unsetVariables?: readonly string[]
|
|
322
|
+
}): string | undefined {
|
|
323
|
+
const unset = outcome.unsetVariables ?? []
|
|
324
|
+
if (unset.length === 0) return undefined
|
|
325
|
+
const notFound =
|
|
326
|
+
(outcome.spawnError !== undefined && /ENOENT/.test(outcome.spawnError)) ||
|
|
327
|
+
outcome.exitCode === 127 ||
|
|
328
|
+
/no such file|not found/i.test(outcome.stderr)
|
|
329
|
+
if (!notFound) return undefined
|
|
330
|
+
const shown = unset.slice(0, 3).map((name) => `$${name}`)
|
|
331
|
+
const more = unset.length > 3 ? ` and ${unset.length - 3} more` : ''
|
|
332
|
+
return `its command refers to ${shown.join(', ')}${more}, which ${
|
|
333
|
+
unset.length === 1 ? 'is' : 'are'
|
|
334
|
+
} not set for hooks: give ${unset.length === 1 ? 'it' : 'each'} a value with the entry's "env" key`
|
|
335
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names a shell hook may be sent for a tool.
|
|
3
|
+
*
|
|
4
|
+
* A hook sees the tool's own name (`bash`, `web_fetch`) unless its entry asks
|
|
5
|
+
* for the written one (`toolNames: 'written'`), the capitalised name a person
|
|
6
|
+
* writes in a matcher for that tool (`Bash`, `WebFetch`). An entry read from
|
|
7
|
+
* the common hook contract asks for it by default.
|
|
8
|
+
*
|
|
9
|
+
* ONE table, from the name a tool is registered under to its written name.
|
|
10
|
+
* A tool with no common name is not in it and keeps its own; a tool-server
|
|
11
|
+
* tool (`mcp__<server>__<tool>`) is never in it. The table is documented in
|
|
12
|
+
* `docs/sdk/hooks.md`, and a test holds the two equal.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Which spelling of a tool's name an entry receives. */
|
|
16
|
+
export type ShellHookToolNames = 'namzu' | 'written'
|
|
17
|
+
|
|
18
|
+
/** Our tool name to the name written for it. Every key is lower case. */
|
|
19
|
+
export const SHELL_HOOK_WRITTEN_TOOL_NAMES: Readonly<Record<string, string>> = Object.freeze({
|
|
20
|
+
Agent: 'Agent',
|
|
21
|
+
ask_user_question: 'AskUserQuestion',
|
|
22
|
+
bash: 'Bash',
|
|
23
|
+
edit: 'Edit',
|
|
24
|
+
glob: 'Glob',
|
|
25
|
+
grep: 'Grep',
|
|
26
|
+
ls: 'LS',
|
|
27
|
+
lsp: 'LSP',
|
|
28
|
+
monitor: 'Monitor',
|
|
29
|
+
read: 'Read',
|
|
30
|
+
search_tools: 'ToolSearch',
|
|
31
|
+
skill: 'Skill',
|
|
32
|
+
task_create: 'TaskCreate',
|
|
33
|
+
task_list: 'TaskList',
|
|
34
|
+
task_update: 'TaskUpdate',
|
|
35
|
+
web_fetch: 'WebFetch',
|
|
36
|
+
web_search: 'WebSearch',
|
|
37
|
+
write: 'Write',
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
/** The name a hook is sent for `toolName` under the entry's `toolNames`. */
|
|
41
|
+
export function shellHookToolName(toolName: string, mode: ShellHookToolNames | undefined): string {
|
|
42
|
+
if (mode !== 'written') return toolName
|
|
43
|
+
return Object.hasOwn(SHELL_HOOK_WRITTEN_TOOL_NAMES, toolName)
|
|
44
|
+
? (SHELL_HOOK_WRITTEN_TOOL_NAMES[toolName] as string)
|
|
45
|
+
: toolName
|
|
46
|
+
}
|