@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,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `file_changed` events for a session: the watcher and what it fires.
|
|
3
|
+
*
|
|
4
|
+
* The files watched are the ones named in the matchers of the `file_changed`
|
|
5
|
+
* hooks the host attached, plus the list hooks have handed back with
|
|
6
|
+
* `hookSpecificOutput.watchPaths`. A new list from any hook of `session_start`,
|
|
7
|
+
* `file_changed` or `cwd_changed` replaces the one handed back before; the
|
|
8
|
+
* matcher paths stay. With neither, no watcher exists.
|
|
9
|
+
*
|
|
10
|
+
* A hook of `file_changed` cannot refuse anything and gives the model nothing:
|
|
11
|
+
* what it may answer is a line for the person (`systemMessage`, passed to
|
|
12
|
+
* `onNotice`) and the next list of paths.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { SessionId } from '../types/ids/index.js'
|
|
16
|
+
import type { PluginHookResult } from '../types/plugin/index.js'
|
|
17
|
+
import { FileChangeWatcher, type FileChangeWatcherOptions } from './file-change-watch.js'
|
|
18
|
+
import type { PluginLifecycleManager } from './lifecycle.js'
|
|
19
|
+
|
|
20
|
+
export interface FileChangeHooksOptions {
|
|
21
|
+
readonly manager: Pick<PluginLifecycleManager, 'executeHooks'> &
|
|
22
|
+
Partial<Pick<PluginLifecycleManager, 'listHooks' | 'onHooksChanged'>>
|
|
23
|
+
/** Relative paths are under this folder. */
|
|
24
|
+
readonly cwd: string
|
|
25
|
+
/** The session now; asked at each event, because a host's session can change. */
|
|
26
|
+
readonly sessionId: () => string
|
|
27
|
+
/**
|
|
28
|
+
* Paths the `file_changed` matchers name, from the host's hook configuration.
|
|
29
|
+
* The matchers of hooks attached to the manager (a plugin's, a skill's, an
|
|
30
|
+
* agent file's) are read from it and added, now and whenever they change.
|
|
31
|
+
*/
|
|
32
|
+
readonly matcherPaths: readonly string[]
|
|
33
|
+
/** A line a hook gave for the person. */
|
|
34
|
+
readonly onNotice?: (line: string) => void
|
|
35
|
+
/** A watch that failed or was cut short, once per distinct sentence. */
|
|
36
|
+
readonly onProblem?: (line: string) => void
|
|
37
|
+
/** Seams for tests. */
|
|
38
|
+
readonly watcher?: Pick<
|
|
39
|
+
FileChangeWatcherOptions,
|
|
40
|
+
'settleMs' | 'maxPaths' | 'watchDirectory' | 'exists'
|
|
41
|
+
>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The paths a set of answers hand back: the union, or `undefined` when no answer carried the field. */
|
|
45
|
+
export function watchPathsOf(results: readonly PluginHookResult[]): readonly string[] | undefined {
|
|
46
|
+
let seen = false
|
|
47
|
+
const paths: string[] = []
|
|
48
|
+
for (const result of results) {
|
|
49
|
+
if (!('watchPaths' in result) || result.watchPaths === undefined) continue
|
|
50
|
+
seen = true
|
|
51
|
+
for (const path of result.watchPaths) if (!paths.includes(path)) paths.push(path)
|
|
52
|
+
}
|
|
53
|
+
return seen ? paths : undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class FileChangeHooks {
|
|
57
|
+
private handedBack: readonly string[] = []
|
|
58
|
+
private readonly watcher: FileChangeWatcher
|
|
59
|
+
private closed = false
|
|
60
|
+
private readonly stopListening: (() => void) | undefined
|
|
61
|
+
|
|
62
|
+
constructor(private readonly options: FileChangeHooksOptions) {
|
|
63
|
+
this.watcher = new FileChangeWatcher({
|
|
64
|
+
...options.watcher,
|
|
65
|
+
...(options.onProblem ? { onProblem: options.onProblem } : {}),
|
|
66
|
+
onChange: (change) => {
|
|
67
|
+
void this.fire(change.path, change.change)
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
this.stopListening = options.manager.onHooksChanged?.(() => {
|
|
71
|
+
if (!this.closed) this.refresh()
|
|
72
|
+
})
|
|
73
|
+
this.refresh()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The files watched now, absolute. */
|
|
77
|
+
get watching(): readonly string[] {
|
|
78
|
+
return this.watcher.watching
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** What the answers of a `session_start`, `file_changed` or `cwd_changed` run said: notices and a new list. */
|
|
82
|
+
absorb(results: readonly PluginHookResult[]): void {
|
|
83
|
+
if (this.closed) return
|
|
84
|
+
for (const result of results) {
|
|
85
|
+
const notice = 'notice' in result ? result.notice : undefined
|
|
86
|
+
if (notice !== undefined && notice.trim() !== '') this.options.onNotice?.(notice.trim())
|
|
87
|
+
}
|
|
88
|
+
const next = watchPathsOf(results)
|
|
89
|
+
if (next === undefined) return
|
|
90
|
+
this.handedBack = next
|
|
91
|
+
this.refresh()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
close(): void {
|
|
95
|
+
this.closed = true
|
|
96
|
+
this.stopListening?.()
|
|
97
|
+
this.watcher.close()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private refresh(): void {
|
|
101
|
+
const all = [...this.options.matcherPaths]
|
|
102
|
+
for (const hook of this.options.manager.listHooks?.() ?? []) {
|
|
103
|
+
if (hook.event !== 'file_changed') continue
|
|
104
|
+
for (const path of hook.watches ?? []) if (!all.includes(path)) all.push(path)
|
|
105
|
+
}
|
|
106
|
+
for (const path of this.handedBack) if (!all.includes(path)) all.push(path)
|
|
107
|
+
this.watcher.setPaths(all, this.options.cwd)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private async fire(path: string, change: 'change' | 'add' | 'unlink'): Promise<void> {
|
|
111
|
+
if (this.closed) return
|
|
112
|
+
// What the hook writes to this very file is not a new change: without
|
|
113
|
+
// this a hook that touches the file it watches would start itself again
|
|
114
|
+
// every half second for as long as the session lives.
|
|
115
|
+
this.watcher.hold(path)
|
|
116
|
+
try {
|
|
117
|
+
const results = await this.options.manager.executeHooks('file_changed', {
|
|
118
|
+
sessionId: this.options.sessionId() as SessionId,
|
|
119
|
+
filePath: path,
|
|
120
|
+
change,
|
|
121
|
+
})
|
|
122
|
+
this.absorb(results)
|
|
123
|
+
} catch (error) {
|
|
124
|
+
this.options.onProblem?.(
|
|
125
|
+
`A file_changed hook could not run for ${path}: ${error instanceof Error ? error.message : String(error)}.`,
|
|
126
|
+
)
|
|
127
|
+
} finally {
|
|
128
|
+
this.watcher.release(path)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watching the files hooks asked about, and nothing else.
|
|
3
|
+
*
|
|
4
|
+
* A `file_changed` hook names the files it cares about in its matcher
|
|
5
|
+
* (`.envrc|package.json`), and may hand back more (`watchPaths`). This file
|
|
6
|
+
* turns those paths into events. No watcher exists until a path is given, and
|
|
7
|
+
* `setPaths([])` closes them all.
|
|
8
|
+
*
|
|
9
|
+
* ## How
|
|
10
|
+
*
|
|
11
|
+
* The platform's own `fs.watch`, on the folder that holds each file rather
|
|
12
|
+
* than on the file: an editor that saves by writing a new file and renaming
|
|
13
|
+
* it over the old one would leave a watch on the file pointing at a file that
|
|
14
|
+
* is gone, and a watch on a folder also sees a file appear and disappear.
|
|
15
|
+
* One watch serves every file in a folder.
|
|
16
|
+
*
|
|
17
|
+
* ## Settling
|
|
18
|
+
*
|
|
19
|
+
* What the platform reports is noisy: an editor's save is several events, and
|
|
20
|
+
* the names differ between systems. Every event for a watched file only
|
|
21
|
+
* restarts a timer; when the file has been still for `settleMs` (half a
|
|
22
|
+
* second) the file is looked at once, and the event is `add` (it did not exist
|
|
23
|
+
* and does now), `unlink` (it did and does not) or `change` (it exists, and
|
|
24
|
+
* did). A file that did not exist before and does not now is no event.
|
|
25
|
+
*
|
|
26
|
+
* ## Limits
|
|
27
|
+
*
|
|
28
|
+
* At most {@link MAX_WATCHED_PATHS} files are watched. A longer list is cut
|
|
29
|
+
* (the first ones are kept, in the order given) and the cut is reported once.
|
|
30
|
+
* A watch that cannot be started or breaks (the folder does not exist, no
|
|
31
|
+
* permission, the system's watch limit) is reported once for that folder and
|
|
32
|
+
* the others carry on; the watch is not retried until the list is set again.
|
|
33
|
+
* Watches never keep the process alive.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { type FSWatcher, statSync, watch } from 'node:fs'
|
|
37
|
+
import { basename, dirname, isAbsolute, resolve } from 'node:path'
|
|
38
|
+
|
|
39
|
+
import type { PluginFileChange } from '../types/plugin/index.js'
|
|
40
|
+
|
|
41
|
+
/** The most files watched at once, matcher paths and handed-back paths together. */
|
|
42
|
+
export const MAX_WATCHED_PATHS = 64
|
|
43
|
+
/** How long a file must be still before its change is reported, in milliseconds. */
|
|
44
|
+
export const FILE_SETTLE_MS = 500
|
|
45
|
+
|
|
46
|
+
export interface FileChange {
|
|
47
|
+
readonly path: string
|
|
48
|
+
readonly change: PluginFileChange
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The platform's folder watch, as far as this file uses it. */
|
|
52
|
+
export type WatchDirectory = (
|
|
53
|
+
directory: string,
|
|
54
|
+
listener: (name: string | null) => void,
|
|
55
|
+
onError: (error: Error) => void,
|
|
56
|
+
) => { close(): void }
|
|
57
|
+
|
|
58
|
+
export interface FileChangeWatcherOptions {
|
|
59
|
+
readonly onChange: (change: FileChange) => void
|
|
60
|
+
/** Called once for each distinct problem, in a sentence. */
|
|
61
|
+
readonly onProblem?: (message: string) => void
|
|
62
|
+
readonly settleMs?: number
|
|
63
|
+
readonly maxPaths?: number
|
|
64
|
+
/** A seam for tests; default `fs.watch`, not persistent. */
|
|
65
|
+
readonly watchDirectory?: WatchDirectory
|
|
66
|
+
/** A seam for tests; default a `stat`. */
|
|
67
|
+
readonly exists?: (path: string) => boolean
|
|
68
|
+
/**
|
|
69
|
+
* A seam for tests: a string that differs whenever the file's content or
|
|
70
|
+
* identity does (default a `stat`'s inode, size and modification time;
|
|
71
|
+
* `'absent'` when the file is not there).
|
|
72
|
+
*/
|
|
73
|
+
readonly signature?: (path: string) => string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const defaultWatchDirectory: WatchDirectory = (directory, listener, onError) => {
|
|
77
|
+
const watcher: FSWatcher = watch(directory, { persistent: false }, (_type, name) => {
|
|
78
|
+
listener(name === null || name === undefined ? null : String(name))
|
|
79
|
+
})
|
|
80
|
+
watcher.on('error', onError)
|
|
81
|
+
return watcher
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const defaultExists = (path: string): boolean => {
|
|
85
|
+
try {
|
|
86
|
+
statSync(path)
|
|
87
|
+
return true
|
|
88
|
+
} catch {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const defaultSignature = (path: string): string => {
|
|
94
|
+
try {
|
|
95
|
+
const stat = statSync(path)
|
|
96
|
+
return `${stat.ino}:${stat.size}:${stat.mtimeMs}`
|
|
97
|
+
} catch {
|
|
98
|
+
return 'absent'
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The case-insensitive file systems compare names without regard to case. */
|
|
103
|
+
const FOLDS_CASE = process.platform === 'win32' || process.platform === 'darwin'
|
|
104
|
+
const fold = (name: string): string => (FOLDS_CASE ? name.toLowerCase() : name)
|
|
105
|
+
|
|
106
|
+
interface Watched {
|
|
107
|
+
existed: boolean
|
|
108
|
+
timer: ReturnType<typeof setTimeout> | undefined
|
|
109
|
+
/** Runs of a hook for this file now under way; its own writes are not news. */
|
|
110
|
+
holding: number
|
|
111
|
+
/** What the file looked like when the last hook run for it ended. */
|
|
112
|
+
acknowledged: string | undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export class FileChangeWatcher {
|
|
116
|
+
private readonly files = new Map<string, Watched>()
|
|
117
|
+
private readonly folders = new Map<string, { close(): void }>()
|
|
118
|
+
private readonly failed = new Set<string>()
|
|
119
|
+
/** Folders that did not exist when asked for, each with a watch on the nearest folder that does. */
|
|
120
|
+
private readonly waiting = new Map<string, { close(): void } | undefined>()
|
|
121
|
+
private readonly reported = new Set<string>()
|
|
122
|
+
private closed = false
|
|
123
|
+
|
|
124
|
+
constructor(private readonly options: FileChangeWatcherOptions) {}
|
|
125
|
+
|
|
126
|
+
/** The files watched now, absolute. */
|
|
127
|
+
get watching(): readonly string[] {
|
|
128
|
+
return [...this.files.keys()]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Watch exactly these files from now on. A relative path is under `base`.
|
|
133
|
+
* A file already watched keeps its state; one no longer named is let go.
|
|
134
|
+
*/
|
|
135
|
+
setPaths(paths: readonly string[], base: string): void {
|
|
136
|
+
if (this.closed) return
|
|
137
|
+
const limit = this.options.maxPaths ?? MAX_WATCHED_PATHS
|
|
138
|
+
const wanted: string[] = []
|
|
139
|
+
for (const path of paths) {
|
|
140
|
+
const absolute = isAbsolute(path) ? resolve(path) : resolve(base, path)
|
|
141
|
+
if (!wanted.includes(absolute)) wanted.push(absolute)
|
|
142
|
+
}
|
|
143
|
+
if (wanted.length > limit) {
|
|
144
|
+
this.report(
|
|
145
|
+
`Hooks asked to watch ${wanted.length} files; only the first ${limit} are watched.`,
|
|
146
|
+
)
|
|
147
|
+
wanted.length = limit
|
|
148
|
+
}
|
|
149
|
+
const exists = this.options.exists ?? defaultExists
|
|
150
|
+
for (const [path, state] of this.files) {
|
|
151
|
+
if (wanted.includes(path)) continue
|
|
152
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
153
|
+
this.files.delete(path)
|
|
154
|
+
}
|
|
155
|
+
for (const path of wanted) {
|
|
156
|
+
if (!this.files.has(path)) {
|
|
157
|
+
this.files.set(path, {
|
|
158
|
+
existed: exists(path),
|
|
159
|
+
timer: undefined,
|
|
160
|
+
holding: 0,
|
|
161
|
+
acknowledged: undefined,
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const folders = new Set(wanted.map((path) => dirname(path)))
|
|
166
|
+
for (const [folder, handle] of this.folders) {
|
|
167
|
+
if (folders.has(folder)) continue
|
|
168
|
+
handle.close()
|
|
169
|
+
this.folders.delete(folder)
|
|
170
|
+
}
|
|
171
|
+
for (const [folder] of this.waiting) {
|
|
172
|
+
if (!folders.has(folder)) this.stopWaiting(folder)
|
|
173
|
+
}
|
|
174
|
+
this.failed.clear()
|
|
175
|
+
for (const folder of folders) {
|
|
176
|
+
if (!this.folders.has(folder) && !this.waiting.has(folder)) this.start(folder)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A hook run for `path` begins: what it writes to that file is its own, not
|
|
182
|
+
* a change to report. Paired with {@link release}.
|
|
183
|
+
*/
|
|
184
|
+
hold(path: string): void {
|
|
185
|
+
const state = this.files.get(path)
|
|
186
|
+
if (state === undefined) return
|
|
187
|
+
state.holding += 1
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The hook run for `path` ended. The file as it is now is what that run
|
|
192
|
+
* left, so a settle that finds it unchanged is the run's own write and
|
|
193
|
+
* reports nothing; a change by anyone after this moment is reported.
|
|
194
|
+
*/
|
|
195
|
+
release(path: string): void {
|
|
196
|
+
const state = this.files.get(path)
|
|
197
|
+
if (state === undefined) return
|
|
198
|
+
state.holding = Math.max(0, state.holding - 1)
|
|
199
|
+
if (state.holding > 0) return
|
|
200
|
+
state.acknowledged = (this.options.signature ?? this.defaultSignatureFor())(path)
|
|
201
|
+
state.existed = (this.options.exists ?? defaultExists)(path)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private defaultSignatureFor(): (path: string) => string {
|
|
205
|
+
const exists = this.options.exists
|
|
206
|
+
return exists === undefined
|
|
207
|
+
? defaultSignature
|
|
208
|
+
: (path) => (exists(path) ? `present:${path}` : 'absent')
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
close(): void {
|
|
212
|
+
this.closed = true
|
|
213
|
+
for (const state of this.files.values()) {
|
|
214
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
215
|
+
}
|
|
216
|
+
this.files.clear()
|
|
217
|
+
for (const handle of this.folders.values()) handle.close()
|
|
218
|
+
this.folders.clear()
|
|
219
|
+
for (const [folder] of [...this.waiting]) this.stopWaiting(folder)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private report(message: string): void {
|
|
223
|
+
if (this.reported.has(message)) return
|
|
224
|
+
this.reported.add(message)
|
|
225
|
+
this.options.onProblem?.(message)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private start(folder: string): void {
|
|
229
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
230
|
+
try {
|
|
231
|
+
const handle = watchDirectory(
|
|
232
|
+
folder,
|
|
233
|
+
(name) => this.touched(folder, name),
|
|
234
|
+
(error) => {
|
|
235
|
+
this.drop(folder)
|
|
236
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`)
|
|
237
|
+
},
|
|
238
|
+
)
|
|
239
|
+
this.folders.set(folder, handle)
|
|
240
|
+
} catch (error) {
|
|
241
|
+
this.failed.add(folder)
|
|
242
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
243
|
+
const missing = /ENOENT/.test(reason)
|
|
244
|
+
this.report(
|
|
245
|
+
`Files in ${folder} cannot be watched${missing ? ' yet' : ''} (${reason})${missing ? '; they are watched once the folder exists' : ''}.`,
|
|
246
|
+
)
|
|
247
|
+
if (missing) this.waitFor(folder)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A folder that does not exist yet: watch the nearest one that does, and
|
|
253
|
+
* try the folder again whenever something happens there. When the folder
|
|
254
|
+
* appears its files are looked at, so a file made right after it is seen.
|
|
255
|
+
*/
|
|
256
|
+
private waitFor(folder: string): void {
|
|
257
|
+
this.stopWaiting(folder)
|
|
258
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
259
|
+
let handle: { close(): void } | undefined
|
|
260
|
+
let ancestor = dirname(folder)
|
|
261
|
+
while (handle === undefined && ancestor !== folder) {
|
|
262
|
+
try {
|
|
263
|
+
handle = watchDirectory(
|
|
264
|
+
ancestor,
|
|
265
|
+
() => this.retry(folder),
|
|
266
|
+
() => undefined,
|
|
267
|
+
)
|
|
268
|
+
} catch {
|
|
269
|
+
const up = dirname(ancestor)
|
|
270
|
+
if (up === ancestor) break
|
|
271
|
+
ancestor = up
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this.waiting.set(folder, handle)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private stopWaiting(folder: string): void {
|
|
278
|
+
const handle = this.waiting.get(folder)
|
|
279
|
+
this.waiting.delete(folder)
|
|
280
|
+
try {
|
|
281
|
+
handle?.close()
|
|
282
|
+
} catch {
|
|
283
|
+
// Already closed.
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private retry(folder: string): void {
|
|
288
|
+
if (this.closed || !this.waiting.has(folder)) return
|
|
289
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
290
|
+
let handle: { close(): void }
|
|
291
|
+
try {
|
|
292
|
+
handle = watchDirectory(
|
|
293
|
+
folder,
|
|
294
|
+
(name) => this.touched(folder, name),
|
|
295
|
+
(error) => {
|
|
296
|
+
this.drop(folder)
|
|
297
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`)
|
|
298
|
+
},
|
|
299
|
+
)
|
|
300
|
+
} catch {
|
|
301
|
+
// Not there yet: move the watch down to the nearest folder that exists now.
|
|
302
|
+
this.waitFor(folder)
|
|
303
|
+
return
|
|
304
|
+
}
|
|
305
|
+
this.stopWaiting(folder)
|
|
306
|
+
this.folders.set(folder, handle)
|
|
307
|
+
for (const path of this.files.keys()) {
|
|
308
|
+
if (dirname(path) === folder) this.schedule(path)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private drop(folder: string): void {
|
|
313
|
+
const handle = this.folders.get(folder)
|
|
314
|
+
this.folders.delete(folder)
|
|
315
|
+
try {
|
|
316
|
+
handle?.close()
|
|
317
|
+
} catch {
|
|
318
|
+
// Already closed by the error that brought us here.
|
|
319
|
+
}
|
|
320
|
+
this.failed.add(folder)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** An event in `folder`: for one name, or for every file in it when the system gave no name. */
|
|
324
|
+
private touched(folder: string, name: string | null): void {
|
|
325
|
+
if (this.closed) return
|
|
326
|
+
for (const path of this.files.keys()) {
|
|
327
|
+
if (dirname(path) !== folder) continue
|
|
328
|
+
if (name !== null && fold(basename(path)) !== fold(name)) continue
|
|
329
|
+
this.schedule(path)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private schedule(path: string): void {
|
|
334
|
+
const state = this.files.get(path)
|
|
335
|
+
if (state === undefined) return
|
|
336
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
337
|
+
const timer = setTimeout(() => this.settle(path), this.options.settleMs ?? FILE_SETTLE_MS)
|
|
338
|
+
timer.unref?.()
|
|
339
|
+
state.timer = timer
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private settle(path: string): void {
|
|
343
|
+
const state = this.files.get(path)
|
|
344
|
+
if (state === undefined || this.closed) return
|
|
345
|
+
state.timer = undefined
|
|
346
|
+
// A hook run for this file is under way: whatever it writes is its own.
|
|
347
|
+
// `release` looks at the file when the run ends.
|
|
348
|
+
if (state.holding > 0) return
|
|
349
|
+
const exists = (this.options.exists ?? defaultExists)(path)
|
|
350
|
+
const before = state.existed
|
|
351
|
+
state.existed = exists
|
|
352
|
+
if (state.acknowledged !== undefined) {
|
|
353
|
+
const now = (this.options.signature ?? this.defaultSignatureFor())(path)
|
|
354
|
+
if (now === state.acknowledged) return
|
|
355
|
+
state.acknowledged = undefined
|
|
356
|
+
}
|
|
357
|
+
const change: PluginFileChange | undefined =
|
|
358
|
+
before && exists
|
|
359
|
+
? 'change'
|
|
360
|
+
: !before && exists
|
|
361
|
+
? 'add'
|
|
362
|
+
: before && !exists
|
|
363
|
+
? 'unlink'
|
|
364
|
+
: undefined
|
|
365
|
+
if (change !== undefined) this.options.onChange({ path, change })
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -56,3 +56,49 @@ export function sessionStartHookContext(results: readonly PluginHookResult[]): s
|
|
|
56
56
|
}
|
|
57
57
|
return texts.length === 0 ? undefined : boundHookContext(texts.join('\n\n'))
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
/** The most characters of the model's last message a `turn_end` or `subagent_stop` hook receives. */
|
|
61
|
+
export const MAX_LAST_MESSAGE_HOOK_CHARS = 8_000
|
|
62
|
+
|
|
63
|
+
/** The most characters of a compaction summary a `post_compact` hook receives. */
|
|
64
|
+
export const MAX_COMPACTION_SUMMARY_HOOK_CHARS = 8_000
|
|
65
|
+
|
|
66
|
+
/** `text` cut to `limit` characters, with a mark when it was cut. */
|
|
67
|
+
function boundText(text: string, limit: number): string {
|
|
68
|
+
return text.length > limit ? `${text.slice(0, limit)}\n[truncated]` : text
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The model's last message as a stop hook receives it; `undefined` when it holds no text. */
|
|
72
|
+
export function boundLastAssistantMessage(text: string | null | undefined): string | undefined {
|
|
73
|
+
if (text === undefined || text === null || text.trim().length === 0) return undefined
|
|
74
|
+
return boundText(text, MAX_LAST_MESSAGE_HOOK_CHARS)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A compaction summary as a `post_compact` hook receives it; `undefined` when empty. */
|
|
78
|
+
export function boundCompactionSummary(text: string | null | undefined): string | undefined {
|
|
79
|
+
if (text === undefined || text === null || text.trim().length === 0) return undefined
|
|
80
|
+
return boundText(text, MAX_COMPACTION_SUMMARY_HOOK_CHARS)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* What `pre_compact` hooks said the summary should keep, joined in answer
|
|
85
|
+
* order and bounded to {@link MAX_HOOK_CONTEXT_CHARS}; `undefined` when none
|
|
86
|
+
* said anything. A hook gives it as `annotate`, as `continue` with `context`,
|
|
87
|
+
* or as `context` beside a `skip` (a refusal the pass overrides, as for a
|
|
88
|
+
* pass the provider forced, still has its words heard).
|
|
89
|
+
*/
|
|
90
|
+
export function compactionHookInstructions(
|
|
91
|
+
results: readonly PluginHookResult[],
|
|
92
|
+
): string | undefined {
|
|
93
|
+
const texts: string[] = []
|
|
94
|
+
for (const result of results) {
|
|
95
|
+
const text =
|
|
96
|
+
result.action === 'annotate'
|
|
97
|
+
? result.text
|
|
98
|
+
: result.action === 'continue' || result.action === 'skip'
|
|
99
|
+
? result.context
|
|
100
|
+
: undefined
|
|
101
|
+
if (text !== undefined && text.trim().length > 0) texts.push(text.trim())
|
|
102
|
+
}
|
|
103
|
+
return texts.length === 0 ? undefined : boundHookContext(texts.join('\n\n'))
|
|
104
|
+
}
|
package/src/plugin/index.ts
CHANGED
|
@@ -9,14 +9,19 @@ export { definePlugin } from './define.js'
|
|
|
9
9
|
export type { DefinedPlugin, DefinePluginOptions } from './define.js'
|
|
10
10
|
export type { PluginDiscoveryOptions, PluginEnablementCapabilities } from './loader.js'
|
|
11
11
|
export {
|
|
12
|
+
boundCompactionSummary,
|
|
12
13
|
boundHookContext,
|
|
13
14
|
boundHookReplacement,
|
|
15
|
+
boundLastAssistantMessage,
|
|
16
|
+
compactionHookInstructions,
|
|
14
17
|
frameOperatorHookContext,
|
|
18
|
+
MAX_COMPACTION_SUMMARY_HOOK_CHARS,
|
|
15
19
|
MAX_HOOK_CONTEXT_CHARS,
|
|
16
20
|
MAX_HOOK_REPLACEMENT_CHARS,
|
|
21
|
+
MAX_LAST_MESSAGE_HOOK_CHARS,
|
|
17
22
|
sessionStartHookContext,
|
|
18
23
|
} from './hook-context.js'
|
|
19
|
-
export { PluginLifecycleManager } from './lifecycle.js'
|
|
24
|
+
export { isHookEntriesPath, PluginLifecycleManager } from './lifecycle.js'
|
|
20
25
|
export { PluginResolver } from './resolver.js'
|
|
21
26
|
export {
|
|
22
27
|
attachShellHooks,
|
|
@@ -25,11 +30,30 @@ export {
|
|
|
25
30
|
MAX_SHELL_HOOK_TIMEOUT_MS,
|
|
26
31
|
runShellHook,
|
|
27
32
|
SHELL_HOOK_EVENTS,
|
|
33
|
+
SHELL_HOOK_PASCAL_EVENT_NAMES,
|
|
28
34
|
SHELL_HOOKS_PLUGIN_ID,
|
|
29
35
|
shellHookIfMatches,
|
|
36
|
+
shellHookIfProblem,
|
|
37
|
+
shellHookMatcherProblem,
|
|
30
38
|
shellHookMatches,
|
|
31
39
|
shellHookVerdict,
|
|
32
40
|
} from './shell-hook.js'
|
|
41
|
+
export {
|
|
42
|
+
assignedWithin,
|
|
43
|
+
isPluginFolderVariable,
|
|
44
|
+
isShellOwnVariable,
|
|
45
|
+
MOVED_FOLDER_ENDINGS,
|
|
46
|
+
referencedVariables,
|
|
47
|
+
SHELL_HOOK_RESERVED_ENV,
|
|
48
|
+
shellHookEnvNameProblem,
|
|
49
|
+
shellHookEnvProblems,
|
|
50
|
+
substitutePluginFolders,
|
|
51
|
+
} from './shell-hook-env.js'
|
|
52
|
+
export {
|
|
53
|
+
SHELL_HOOK_WRITTEN_TOOL_NAMES,
|
|
54
|
+
type ShellHookToolNames,
|
|
55
|
+
shellHookToolName,
|
|
56
|
+
} from './shell-hook-tool-names.js'
|
|
33
57
|
export type {
|
|
34
58
|
HttpHookEntry,
|
|
35
59
|
PromptHookEntry,
|
|
@@ -38,9 +62,37 @@ export type {
|
|
|
38
62
|
ShellHookHandler,
|
|
39
63
|
ShellHookJudge,
|
|
40
64
|
ShellHookOnFailure,
|
|
65
|
+
ShellHookCall,
|
|
41
66
|
ShellHookInput,
|
|
42
67
|
ShellHookOptions,
|
|
43
68
|
ShellHookPathFilter,
|
|
44
69
|
ShellHookOutcome,
|
|
45
70
|
ShellHooksConfig,
|
|
46
71
|
} from './shell-hook.js'
|
|
72
|
+
export {
|
|
73
|
+
fileChangedMatcherNote,
|
|
74
|
+
fileChangedMatcherPaths,
|
|
75
|
+
fileChangedMatches,
|
|
76
|
+
} from './shell-hook.js'
|
|
77
|
+
export {
|
|
78
|
+
HOOK_ENV_EVENTS,
|
|
79
|
+
MAX_HOOK_ENV_FILE_BYTES,
|
|
80
|
+
MAX_HOOK_ENV_VALUE_CHARS,
|
|
81
|
+
MAX_HOOK_ENV_VARIABLES,
|
|
82
|
+
refusedHookEnvName,
|
|
83
|
+
SessionHookEnv,
|
|
84
|
+
} from './session-hook-env.js'
|
|
85
|
+
export type { HookEnvEvent, SessionHookEnvOptions } from './session-hook-env.js'
|
|
86
|
+
export { FILE_SETTLE_MS, FileChangeWatcher, MAX_WATCHED_PATHS } from './file-change-watch.js'
|
|
87
|
+
export type {
|
|
88
|
+
FileChange,
|
|
89
|
+
FileChangeWatcherOptions,
|
|
90
|
+
WatchDirectory,
|
|
91
|
+
} from './file-change-watch.js'
|
|
92
|
+
export { FileChangeHooks } from './file-change-hooks.js'
|
|
93
|
+
export type { FileChangeHooksOptions } from './file-change-hooks.js'
|
|
94
|
+
export {
|
|
95
|
+
WORKTREE_HOOK_SHOWN_CHARS,
|
|
96
|
+
worktreeHookPrinted,
|
|
97
|
+
worktreeHookVerdict,
|
|
98
|
+
} from './worktree-hook-answer.js'
|