@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,111 @@
|
|
|
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
|
+
import { FileChangeWatcher } from './file-change-watch.js';
|
|
15
|
+
/** The paths a set of answers hand back: the union, or `undefined` when no answer carried the field. */
|
|
16
|
+
export function watchPathsOf(results) {
|
|
17
|
+
let seen = false;
|
|
18
|
+
const paths = [];
|
|
19
|
+
for (const result of results) {
|
|
20
|
+
if (!('watchPaths' in result) || result.watchPaths === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
seen = true;
|
|
23
|
+
for (const path of result.watchPaths)
|
|
24
|
+
if (!paths.includes(path))
|
|
25
|
+
paths.push(path);
|
|
26
|
+
}
|
|
27
|
+
return seen ? paths : undefined;
|
|
28
|
+
}
|
|
29
|
+
export class FileChangeHooks {
|
|
30
|
+
options;
|
|
31
|
+
handedBack = [];
|
|
32
|
+
watcher;
|
|
33
|
+
closed = false;
|
|
34
|
+
stopListening;
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.options = options;
|
|
37
|
+
this.watcher = new FileChangeWatcher({
|
|
38
|
+
...options.watcher,
|
|
39
|
+
...(options.onProblem ? { onProblem: options.onProblem } : {}),
|
|
40
|
+
onChange: (change) => {
|
|
41
|
+
void this.fire(change.path, change.change);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
this.stopListening = options.manager.onHooksChanged?.(() => {
|
|
45
|
+
if (!this.closed)
|
|
46
|
+
this.refresh();
|
|
47
|
+
});
|
|
48
|
+
this.refresh();
|
|
49
|
+
}
|
|
50
|
+
/** The files watched now, absolute. */
|
|
51
|
+
get watching() {
|
|
52
|
+
return this.watcher.watching;
|
|
53
|
+
}
|
|
54
|
+
/** What the answers of a `session_start`, `file_changed` or `cwd_changed` run said: notices and a new list. */
|
|
55
|
+
absorb(results) {
|
|
56
|
+
if (this.closed)
|
|
57
|
+
return;
|
|
58
|
+
for (const result of results) {
|
|
59
|
+
const notice = 'notice' in result ? result.notice : undefined;
|
|
60
|
+
if (notice !== undefined && notice.trim() !== '')
|
|
61
|
+
this.options.onNotice?.(notice.trim());
|
|
62
|
+
}
|
|
63
|
+
const next = watchPathsOf(results);
|
|
64
|
+
if (next === undefined)
|
|
65
|
+
return;
|
|
66
|
+
this.handedBack = next;
|
|
67
|
+
this.refresh();
|
|
68
|
+
}
|
|
69
|
+
close() {
|
|
70
|
+
this.closed = true;
|
|
71
|
+
this.stopListening?.();
|
|
72
|
+
this.watcher.close();
|
|
73
|
+
}
|
|
74
|
+
refresh() {
|
|
75
|
+
const all = [...this.options.matcherPaths];
|
|
76
|
+
for (const hook of this.options.manager.listHooks?.() ?? []) {
|
|
77
|
+
if (hook.event !== 'file_changed')
|
|
78
|
+
continue;
|
|
79
|
+
for (const path of hook.watches ?? [])
|
|
80
|
+
if (!all.includes(path))
|
|
81
|
+
all.push(path);
|
|
82
|
+
}
|
|
83
|
+
for (const path of this.handedBack)
|
|
84
|
+
if (!all.includes(path))
|
|
85
|
+
all.push(path);
|
|
86
|
+
this.watcher.setPaths(all, this.options.cwd);
|
|
87
|
+
}
|
|
88
|
+
async fire(path, change) {
|
|
89
|
+
if (this.closed)
|
|
90
|
+
return;
|
|
91
|
+
// What the hook writes to this very file is not a new change: without
|
|
92
|
+
// this a hook that touches the file it watches would start itself again
|
|
93
|
+
// every half second for as long as the session lives.
|
|
94
|
+
this.watcher.hold(path);
|
|
95
|
+
try {
|
|
96
|
+
const results = await this.options.manager.executeHooks('file_changed', {
|
|
97
|
+
sessionId: this.options.sessionId(),
|
|
98
|
+
filePath: path,
|
|
99
|
+
change,
|
|
100
|
+
});
|
|
101
|
+
this.absorb(results);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
this.options.onProblem?.(`A file_changed hook could not run for ${path}: ${error instanceof Error ? error.message : String(error)}.`);
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
this.watcher.release(path);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=file-change-hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-change-hooks.js","sourceRoot":"","sources":["../../src/plugin/file-change-hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,OAAO,EAAE,iBAAiB,EAAiC,MAAM,wBAAwB,CAAA;AA2BzF,wGAAwG;AACxG,MAAM,UAAU,YAAY,CAAC,OAAoC;IAChE,IAAI,IAAI,GAAG,KAAK,CAAA;IAChB,MAAM,KAAK,GAAa,EAAE,CAAA;IAC1B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC9B,IAAI,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS;YAAE,SAAQ;QAC1E,IAAI,GAAG,IAAI,CAAA;QACX,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,UAAU;YAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;gBAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClF,CAAC;IACD,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAChC,CAAC;AAED,MAAM,OAAO,eAAe;IAME;IALrB,UAAU,GAAsB,EAAE,CAAA;IACzB,OAAO,CAAmB;IACnC,MAAM,GAAG,KAAK,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MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,cAAc,EAAE;gBACvE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAe;gBAChD,QAAQ,EAAE,IAAI;gBACd,MAAM;aACN,CAAC,CAAA;YACF,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QACrB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CACvB,yCAAyC,IAAI,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAC3G,CAAA;QACF,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
import type { PluginFileChange } from '../types/plugin/index.js';
|
|
36
|
+
/** The most files watched at once, matcher paths and handed-back paths together. */
|
|
37
|
+
export declare const MAX_WATCHED_PATHS = 64;
|
|
38
|
+
/** How long a file must be still before its change is reported, in milliseconds. */
|
|
39
|
+
export declare const FILE_SETTLE_MS = 500;
|
|
40
|
+
export interface FileChange {
|
|
41
|
+
readonly path: string;
|
|
42
|
+
readonly change: PluginFileChange;
|
|
43
|
+
}
|
|
44
|
+
/** The platform's folder watch, as far as this file uses it. */
|
|
45
|
+
export type WatchDirectory = (directory: string, listener: (name: string | null) => void, onError: (error: Error) => void) => {
|
|
46
|
+
close(): void;
|
|
47
|
+
};
|
|
48
|
+
export interface FileChangeWatcherOptions {
|
|
49
|
+
readonly onChange: (change: FileChange) => void;
|
|
50
|
+
/** Called once for each distinct problem, in a sentence. */
|
|
51
|
+
readonly onProblem?: (message: string) => void;
|
|
52
|
+
readonly settleMs?: number;
|
|
53
|
+
readonly maxPaths?: number;
|
|
54
|
+
/** A seam for tests; default `fs.watch`, not persistent. */
|
|
55
|
+
readonly watchDirectory?: WatchDirectory;
|
|
56
|
+
/** A seam for tests; default a `stat`. */
|
|
57
|
+
readonly exists?: (path: string) => boolean;
|
|
58
|
+
/**
|
|
59
|
+
* A seam for tests: a string that differs whenever the file's content or
|
|
60
|
+
* identity does (default a `stat`'s inode, size and modification time;
|
|
61
|
+
* `'absent'` when the file is not there).
|
|
62
|
+
*/
|
|
63
|
+
readonly signature?: (path: string) => string;
|
|
64
|
+
}
|
|
65
|
+
export declare class FileChangeWatcher {
|
|
66
|
+
private readonly options;
|
|
67
|
+
private readonly files;
|
|
68
|
+
private readonly folders;
|
|
69
|
+
private readonly failed;
|
|
70
|
+
/** Folders that did not exist when asked for, each with a watch on the nearest folder that does. */
|
|
71
|
+
private readonly waiting;
|
|
72
|
+
private readonly reported;
|
|
73
|
+
private closed;
|
|
74
|
+
constructor(options: FileChangeWatcherOptions);
|
|
75
|
+
/** The files watched now, absolute. */
|
|
76
|
+
get watching(): readonly string[];
|
|
77
|
+
/**
|
|
78
|
+
* Watch exactly these files from now on. A relative path is under `base`.
|
|
79
|
+
* A file already watched keeps its state; one no longer named is let go.
|
|
80
|
+
*/
|
|
81
|
+
setPaths(paths: readonly string[], base: string): void;
|
|
82
|
+
/**
|
|
83
|
+
* A hook run for `path` begins: what it writes to that file is its own, not
|
|
84
|
+
* a change to report. Paired with {@link release}.
|
|
85
|
+
*/
|
|
86
|
+
hold(path: string): void;
|
|
87
|
+
/**
|
|
88
|
+
* The hook run for `path` ended. The file as it is now is what that run
|
|
89
|
+
* left, so a settle that finds it unchanged is the run's own write and
|
|
90
|
+
* reports nothing; a change by anyone after this moment is reported.
|
|
91
|
+
*/
|
|
92
|
+
release(path: string): void;
|
|
93
|
+
private defaultSignatureFor;
|
|
94
|
+
close(): void;
|
|
95
|
+
private report;
|
|
96
|
+
private start;
|
|
97
|
+
/**
|
|
98
|
+
* A folder that does not exist yet: watch the nearest one that does, and
|
|
99
|
+
* try the folder again whenever something happens there. When the folder
|
|
100
|
+
* appears its files are looked at, so a file made right after it is seen.
|
|
101
|
+
*/
|
|
102
|
+
private waitFor;
|
|
103
|
+
private stopWaiting;
|
|
104
|
+
private retry;
|
|
105
|
+
private drop;
|
|
106
|
+
/** An event in `folder`: for one name, or for every file in it when the system gave no name. */
|
|
107
|
+
private touched;
|
|
108
|
+
private schedule;
|
|
109
|
+
private settle;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=file-change-watch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-change-watch.d.ts","sourceRoot":"","sources":["../../src/plugin/file-change-watch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAEhE,oFAAoF;AACpF,eAAO,MAAM,iBAAiB,KAAK,CAAA;AACnC,oFAAoF;AACpF,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAA;CACjC;AAED,gEAAgE;AAChE,MAAM,MAAM,cAAc,GAAG,CAC5B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,EACvC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,KAC3B;IAAE,KAAK,IAAI,IAAI,CAAA;CAAE,CAAA;AAEtB,MAAM,WAAW,wBAAwB;IACxC,QAAQ,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,CAAA;IAC/C,4DAA4D;IAC5D,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IAC9C,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,4DAA4D;IAC5D,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,CAAA;IACxC,0CAA0C;IAC1C,QAAQ,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
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
|
+
import { statSync, watch } from 'node:fs';
|
|
36
|
+
import { basename, dirname, isAbsolute, resolve } from 'node:path';
|
|
37
|
+
/** The most files watched at once, matcher paths and handed-back paths together. */
|
|
38
|
+
export const MAX_WATCHED_PATHS = 64;
|
|
39
|
+
/** How long a file must be still before its change is reported, in milliseconds. */
|
|
40
|
+
export const FILE_SETTLE_MS = 500;
|
|
41
|
+
const defaultWatchDirectory = (directory, listener, onError) => {
|
|
42
|
+
const watcher = watch(directory, { persistent: false }, (_type, name) => {
|
|
43
|
+
listener(name === null || name === undefined ? null : String(name));
|
|
44
|
+
});
|
|
45
|
+
watcher.on('error', onError);
|
|
46
|
+
return watcher;
|
|
47
|
+
};
|
|
48
|
+
const defaultExists = (path) => {
|
|
49
|
+
try {
|
|
50
|
+
statSync(path);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const defaultSignature = (path) => {
|
|
58
|
+
try {
|
|
59
|
+
const stat = statSync(path);
|
|
60
|
+
return `${stat.ino}:${stat.size}:${stat.mtimeMs}`;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return 'absent';
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/** The case-insensitive file systems compare names without regard to case. */
|
|
67
|
+
const FOLDS_CASE = process.platform === 'win32' || process.platform === 'darwin';
|
|
68
|
+
const fold = (name) => (FOLDS_CASE ? name.toLowerCase() : name);
|
|
69
|
+
export class FileChangeWatcher {
|
|
70
|
+
options;
|
|
71
|
+
files = new Map();
|
|
72
|
+
folders = new Map();
|
|
73
|
+
failed = new Set();
|
|
74
|
+
/** Folders that did not exist when asked for, each with a watch on the nearest folder that does. */
|
|
75
|
+
waiting = new Map();
|
|
76
|
+
reported = new Set();
|
|
77
|
+
closed = false;
|
|
78
|
+
constructor(options) {
|
|
79
|
+
this.options = options;
|
|
80
|
+
}
|
|
81
|
+
/** The files watched now, absolute. */
|
|
82
|
+
get watching() {
|
|
83
|
+
return [...this.files.keys()];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Watch exactly these files from now on. A relative path is under `base`.
|
|
87
|
+
* A file already watched keeps its state; one no longer named is let go.
|
|
88
|
+
*/
|
|
89
|
+
setPaths(paths, base) {
|
|
90
|
+
if (this.closed)
|
|
91
|
+
return;
|
|
92
|
+
const limit = this.options.maxPaths ?? MAX_WATCHED_PATHS;
|
|
93
|
+
const wanted = [];
|
|
94
|
+
for (const path of paths) {
|
|
95
|
+
const absolute = isAbsolute(path) ? resolve(path) : resolve(base, path);
|
|
96
|
+
if (!wanted.includes(absolute))
|
|
97
|
+
wanted.push(absolute);
|
|
98
|
+
}
|
|
99
|
+
if (wanted.length > limit) {
|
|
100
|
+
this.report(`Hooks asked to watch ${wanted.length} files; only the first ${limit} are watched.`);
|
|
101
|
+
wanted.length = limit;
|
|
102
|
+
}
|
|
103
|
+
const exists = this.options.exists ?? defaultExists;
|
|
104
|
+
for (const [path, state] of this.files) {
|
|
105
|
+
if (wanted.includes(path))
|
|
106
|
+
continue;
|
|
107
|
+
if (state.timer !== undefined)
|
|
108
|
+
clearTimeout(state.timer);
|
|
109
|
+
this.files.delete(path);
|
|
110
|
+
}
|
|
111
|
+
for (const path of wanted) {
|
|
112
|
+
if (!this.files.has(path)) {
|
|
113
|
+
this.files.set(path, {
|
|
114
|
+
existed: exists(path),
|
|
115
|
+
timer: undefined,
|
|
116
|
+
holding: 0,
|
|
117
|
+
acknowledged: undefined,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const folders = new Set(wanted.map((path) => dirname(path)));
|
|
122
|
+
for (const [folder, handle] of this.folders) {
|
|
123
|
+
if (folders.has(folder))
|
|
124
|
+
continue;
|
|
125
|
+
handle.close();
|
|
126
|
+
this.folders.delete(folder);
|
|
127
|
+
}
|
|
128
|
+
for (const [folder] of this.waiting) {
|
|
129
|
+
if (!folders.has(folder))
|
|
130
|
+
this.stopWaiting(folder);
|
|
131
|
+
}
|
|
132
|
+
this.failed.clear();
|
|
133
|
+
for (const folder of folders) {
|
|
134
|
+
if (!this.folders.has(folder) && !this.waiting.has(folder))
|
|
135
|
+
this.start(folder);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A hook run for `path` begins: what it writes to that file is its own, not
|
|
140
|
+
* a change to report. Paired with {@link release}.
|
|
141
|
+
*/
|
|
142
|
+
hold(path) {
|
|
143
|
+
const state = this.files.get(path);
|
|
144
|
+
if (state === undefined)
|
|
145
|
+
return;
|
|
146
|
+
state.holding += 1;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The hook run for `path` ended. The file as it is now is what that run
|
|
150
|
+
* left, so a settle that finds it unchanged is the run's own write and
|
|
151
|
+
* reports nothing; a change by anyone after this moment is reported.
|
|
152
|
+
*/
|
|
153
|
+
release(path) {
|
|
154
|
+
const state = this.files.get(path);
|
|
155
|
+
if (state === undefined)
|
|
156
|
+
return;
|
|
157
|
+
state.holding = Math.max(0, state.holding - 1);
|
|
158
|
+
if (state.holding > 0)
|
|
159
|
+
return;
|
|
160
|
+
state.acknowledged = (this.options.signature ?? this.defaultSignatureFor())(path);
|
|
161
|
+
state.existed = (this.options.exists ?? defaultExists)(path);
|
|
162
|
+
}
|
|
163
|
+
defaultSignatureFor() {
|
|
164
|
+
const exists = this.options.exists;
|
|
165
|
+
return exists === undefined
|
|
166
|
+
? defaultSignature
|
|
167
|
+
: (path) => (exists(path) ? `present:${path}` : 'absent');
|
|
168
|
+
}
|
|
169
|
+
close() {
|
|
170
|
+
this.closed = true;
|
|
171
|
+
for (const state of this.files.values()) {
|
|
172
|
+
if (state.timer !== undefined)
|
|
173
|
+
clearTimeout(state.timer);
|
|
174
|
+
}
|
|
175
|
+
this.files.clear();
|
|
176
|
+
for (const handle of this.folders.values())
|
|
177
|
+
handle.close();
|
|
178
|
+
this.folders.clear();
|
|
179
|
+
for (const [folder] of [...this.waiting])
|
|
180
|
+
this.stopWaiting(folder);
|
|
181
|
+
}
|
|
182
|
+
report(message) {
|
|
183
|
+
if (this.reported.has(message))
|
|
184
|
+
return;
|
|
185
|
+
this.reported.add(message);
|
|
186
|
+
this.options.onProblem?.(message);
|
|
187
|
+
}
|
|
188
|
+
start(folder) {
|
|
189
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory;
|
|
190
|
+
try {
|
|
191
|
+
const handle = watchDirectory(folder, (name) => this.touched(folder, name), (error) => {
|
|
192
|
+
this.drop(folder);
|
|
193
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`);
|
|
194
|
+
});
|
|
195
|
+
this.folders.set(folder, handle);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
this.failed.add(folder);
|
|
199
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
200
|
+
const missing = /ENOENT/.test(reason);
|
|
201
|
+
this.report(`Files in ${folder} cannot be watched${missing ? ' yet' : ''} (${reason})${missing ? '; they are watched once the folder exists' : ''}.`);
|
|
202
|
+
if (missing)
|
|
203
|
+
this.waitFor(folder);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A folder that does not exist yet: watch the nearest one that does, and
|
|
208
|
+
* try the folder again whenever something happens there. When the folder
|
|
209
|
+
* appears its files are looked at, so a file made right after it is seen.
|
|
210
|
+
*/
|
|
211
|
+
waitFor(folder) {
|
|
212
|
+
this.stopWaiting(folder);
|
|
213
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory;
|
|
214
|
+
let handle;
|
|
215
|
+
let ancestor = dirname(folder);
|
|
216
|
+
while (handle === undefined && ancestor !== folder) {
|
|
217
|
+
try {
|
|
218
|
+
handle = watchDirectory(ancestor, () => this.retry(folder), () => undefined);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
const up = dirname(ancestor);
|
|
222
|
+
if (up === ancestor)
|
|
223
|
+
break;
|
|
224
|
+
ancestor = up;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
this.waiting.set(folder, handle);
|
|
228
|
+
}
|
|
229
|
+
stopWaiting(folder) {
|
|
230
|
+
const handle = this.waiting.get(folder);
|
|
231
|
+
this.waiting.delete(folder);
|
|
232
|
+
try {
|
|
233
|
+
handle?.close();
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
// Already closed.
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
retry(folder) {
|
|
240
|
+
if (this.closed || !this.waiting.has(folder))
|
|
241
|
+
return;
|
|
242
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory;
|
|
243
|
+
let handle;
|
|
244
|
+
try {
|
|
245
|
+
handle = watchDirectory(folder, (name) => this.touched(folder, name), (error) => {
|
|
246
|
+
this.drop(folder);
|
|
247
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
// Not there yet: move the watch down to the nearest folder that exists now.
|
|
252
|
+
this.waitFor(folder);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.stopWaiting(folder);
|
|
256
|
+
this.folders.set(folder, handle);
|
|
257
|
+
for (const path of this.files.keys()) {
|
|
258
|
+
if (dirname(path) === folder)
|
|
259
|
+
this.schedule(path);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
drop(folder) {
|
|
263
|
+
const handle = this.folders.get(folder);
|
|
264
|
+
this.folders.delete(folder);
|
|
265
|
+
try {
|
|
266
|
+
handle?.close();
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
// Already closed by the error that brought us here.
|
|
270
|
+
}
|
|
271
|
+
this.failed.add(folder);
|
|
272
|
+
}
|
|
273
|
+
/** An event in `folder`: for one name, or for every file in it when the system gave no name. */
|
|
274
|
+
touched(folder, name) {
|
|
275
|
+
if (this.closed)
|
|
276
|
+
return;
|
|
277
|
+
for (const path of this.files.keys()) {
|
|
278
|
+
if (dirname(path) !== folder)
|
|
279
|
+
continue;
|
|
280
|
+
if (name !== null && fold(basename(path)) !== fold(name))
|
|
281
|
+
continue;
|
|
282
|
+
this.schedule(path);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
schedule(path) {
|
|
286
|
+
const state = this.files.get(path);
|
|
287
|
+
if (state === undefined)
|
|
288
|
+
return;
|
|
289
|
+
if (state.timer !== undefined)
|
|
290
|
+
clearTimeout(state.timer);
|
|
291
|
+
const timer = setTimeout(() => this.settle(path), this.options.settleMs ?? FILE_SETTLE_MS);
|
|
292
|
+
timer.unref?.();
|
|
293
|
+
state.timer = timer;
|
|
294
|
+
}
|
|
295
|
+
settle(path) {
|
|
296
|
+
const state = this.files.get(path);
|
|
297
|
+
if (state === undefined || this.closed)
|
|
298
|
+
return;
|
|
299
|
+
state.timer = undefined;
|
|
300
|
+
// A hook run for this file is under way: whatever it writes is its own.
|
|
301
|
+
// `release` looks at the file when the run ends.
|
|
302
|
+
if (state.holding > 0)
|
|
303
|
+
return;
|
|
304
|
+
const exists = (this.options.exists ?? defaultExists)(path);
|
|
305
|
+
const before = state.existed;
|
|
306
|
+
state.existed = exists;
|
|
307
|
+
if (state.acknowledged !== undefined) {
|
|
308
|
+
const now = (this.options.signature ?? this.defaultSignatureFor())(path);
|
|
309
|
+
if (now === state.acknowledged)
|
|
310
|
+
return;
|
|
311
|
+
state.acknowledged = undefined;
|
|
312
|
+
}
|
|
313
|
+
const change = before && exists
|
|
314
|
+
? 'change'
|
|
315
|
+
: !before && exists
|
|
316
|
+
? 'add'
|
|
317
|
+
: before && !exists
|
|
318
|
+
? 'unlink'
|
|
319
|
+
: undefined;
|
|
320
|
+
if (change !== undefined)
|
|
321
|
+
this.options.onChange({ path, change });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
//# sourceMappingURL=file-change-watch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-change-watch.js","sourceRoot":"","sources":["../../src/plugin/file-change-watch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAkB,QAAQ,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,U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AmB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAA;YACxE,IAAI,GAAG,KAAK,KAAK,CAAC,YAAY;gBAAE,OAAM;YACtC,KAAK,CAAC,YAAY,GAAG,SAAS,CAAA;QAC/B,CAAC;QACD,MAAM,MAAM,GACX,MAAM,IAAI,MAAM;YACf,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,CAAC,MAAM,IAAI,MAAM;gBAClB,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,MAAM,IAAI,CAAC,MAAM;oBAClB,CAAC,CAAC,QAAQ;oBACV,CAAC,CAAC,SAAS,CAAA;QACf,IAAI,MAAM,KAAK,SAAS;YAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAA;IAClE,CAAC;CACD"}
|
|
@@ -27,4 +27,20 @@ export declare function frameOperatorHookContext(texts: readonly string[]): stri
|
|
|
27
27
|
* `continue` with `context` or as `annotate`.
|
|
28
28
|
*/
|
|
29
29
|
export declare function sessionStartHookContext(results: readonly PluginHookResult[]): string | undefined;
|
|
30
|
+
/** The most characters of the model's last message a `turn_end` or `subagent_stop` hook receives. */
|
|
31
|
+
export declare const MAX_LAST_MESSAGE_HOOK_CHARS = 8000;
|
|
32
|
+
/** The most characters of a compaction summary a `post_compact` hook receives. */
|
|
33
|
+
export declare const MAX_COMPACTION_SUMMARY_HOOK_CHARS = 8000;
|
|
34
|
+
/** The model's last message as a stop hook receives it; `undefined` when it holds no text. */
|
|
35
|
+
export declare function boundLastAssistantMessage(text: string | null | undefined): string | undefined;
|
|
36
|
+
/** A compaction summary as a `post_compact` hook receives it; `undefined` when empty. */
|
|
37
|
+
export declare function boundCompactionSummary(text: string | null | undefined): string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* What `pre_compact` hooks said the summary should keep, joined in answer
|
|
40
|
+
* order and bounded to {@link MAX_HOOK_CONTEXT_CHARS}; `undefined` when none
|
|
41
|
+
* said anything. A hook gives it as `annotate`, as `continue` with `context`,
|
|
42
|
+
* or as `context` beside a `skip` (a refusal the pass overrides, as for a
|
|
43
|
+
* pass the provider forced, still has its words heard).
|
|
44
|
+
*/
|
|
45
|
+
export declare function compactionHookInstructions(results: readonly PluginHookResult[]): string | undefined;
|
|
30
46
|
//# sourceMappingURL=hook-context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hook-context.d.ts","sourceRoot":"","sources":["../../src/plugin/hook-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAEhE,0EAA0E;AAC1E,eAAO,MAAM,sBAAsB,OAAQ,CAAA;AAE3C,iFAAiF;AACjF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAIrD;AAED,kFAAkF;AAClF,eAAO,MAAM,0BAA0B,QAAS,CAAA;AAEhD,mGAAmG;AACnG,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAIzD;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,GAAG,MAAM,GAAG,SAAS,CAYhG"}
|
|
1
|
+
{"version":3,"file":"hook-context.d.ts","sourceRoot":"","sources":["../../src/plugin/hook-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAEhE,0EAA0E;AAC1E,eAAO,MAAM,sBAAsB,OAAQ,CAAA;AAE3C,iFAAiF;AACjF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAIrD;AAED,kFAAkF;AAClF,eAAO,MAAM,0BAA0B,QAAS,CAAA;AAEhD,mGAAmG;AACnG,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAIzD;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,GAAG,MAAM,GAAG,SAAS,CAYhG;AAED,qGAAqG;AACrG,eAAO,MAAM,2BAA2B,OAAQ,CAAA;AAEhD,kFAAkF;AAClF,eAAO,MAAM,iCAAiC,OAAQ,CAAA;AAOtD,8FAA8F;AAC9F,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAG7F;AAED,yFAAyF;AACzF,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAG1F;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACzC,OAAO,EAAE,SAAS,gBAAgB,EAAE,GAClC,MAAM,GAAG,SAAS,CAYpB"}
|
|
@@ -48,4 +48,44 @@ export function sessionStartHookContext(results) {
|
|
|
48
48
|
}
|
|
49
49
|
return texts.length === 0 ? undefined : boundHookContext(texts.join('\n\n'));
|
|
50
50
|
}
|
|
51
|
+
/** The most characters of the model's last message a `turn_end` or `subagent_stop` hook receives. */
|
|
52
|
+
export const MAX_LAST_MESSAGE_HOOK_CHARS = 8_000;
|
|
53
|
+
/** The most characters of a compaction summary a `post_compact` hook receives. */
|
|
54
|
+
export const MAX_COMPACTION_SUMMARY_HOOK_CHARS = 8_000;
|
|
55
|
+
/** `text` cut to `limit` characters, with a mark when it was cut. */
|
|
56
|
+
function boundText(text, limit) {
|
|
57
|
+
return text.length > limit ? `${text.slice(0, limit)}\n[truncated]` : text;
|
|
58
|
+
}
|
|
59
|
+
/** The model's last message as a stop hook receives it; `undefined` when it holds no text. */
|
|
60
|
+
export function boundLastAssistantMessage(text) {
|
|
61
|
+
if (text === undefined || text === null || text.trim().length === 0)
|
|
62
|
+
return undefined;
|
|
63
|
+
return boundText(text, MAX_LAST_MESSAGE_HOOK_CHARS);
|
|
64
|
+
}
|
|
65
|
+
/** A compaction summary as a `post_compact` hook receives it; `undefined` when empty. */
|
|
66
|
+
export function boundCompactionSummary(text) {
|
|
67
|
+
if (text === undefined || text === null || text.trim().length === 0)
|
|
68
|
+
return undefined;
|
|
69
|
+
return boundText(text, MAX_COMPACTION_SUMMARY_HOOK_CHARS);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What `pre_compact` hooks said the summary should keep, joined in answer
|
|
73
|
+
* order and bounded to {@link MAX_HOOK_CONTEXT_CHARS}; `undefined` when none
|
|
74
|
+
* said anything. A hook gives it as `annotate`, as `continue` with `context`,
|
|
75
|
+
* or as `context` beside a `skip` (a refusal the pass overrides, as for a
|
|
76
|
+
* pass the provider forced, still has its words heard).
|
|
77
|
+
*/
|
|
78
|
+
export function compactionHookInstructions(results) {
|
|
79
|
+
const texts = [];
|
|
80
|
+
for (const result of results) {
|
|
81
|
+
const text = result.action === 'annotate'
|
|
82
|
+
? result.text
|
|
83
|
+
: result.action === 'continue' || result.action === 'skip'
|
|
84
|
+
? result.context
|
|
85
|
+
: undefined;
|
|
86
|
+
if (text !== undefined && text.trim().length > 0)
|
|
87
|
+
texts.push(text.trim());
|
|
88
|
+
}
|
|
89
|
+
return texts.length === 0 ? undefined : boundHookContext(texts.join('\n\n'));
|
|
90
|
+
}
|
|
51
91
|
//# sourceMappingURL=hook-context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hook-context.js","sourceRoot":"","sources":["../../src/plugin/hook-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,0EAA0E;AAC1E,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,CAAA;AAE3C,iFAAiF;AACjF,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