noonsight 0.0.1 → 0.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/.claude-plugin/marketplace.json +19 -0
- package/.claude-plugin/plugin.json +14 -0
- package/README.md +271 -9
- package/dist/src/adapters/claude-code/classifier.js +138 -0
- package/dist/src/adapters/claude-code/code-edit.js +108 -0
- package/dist/src/adapters/claude-code/compat.js +31 -0
- package/dist/src/adapters/claude-code/definitions.js +347 -0
- package/dist/src/adapters/claude-code/fork.js +132 -0
- package/dist/src/adapters/claude-code/hook.js +652 -0
- package/dist/src/adapters/claude-code/install.js +186 -0
- package/dist/src/adapters/claude-code/launch.js +270 -0
- package/dist/src/adapters/claude-code/launcher.js +252 -0
- package/dist/src/adapters/claude-code/locations.js +115 -0
- package/dist/src/adapters/claude-code/managed.js +75 -0
- package/dist/src/adapters/claude-code/plugin-files.js +88 -0
- package/dist/src/adapters/claude-code/plugins.js +117 -0
- package/dist/src/adapters/claude-code/pointer.js +48 -0
- package/dist/src/adapters/claude-code/predict.js +271 -0
- package/dist/src/adapters/claude-code/routing.js +88 -0
- package/dist/src/adapters/claude-code/settings.js +189 -0
- package/dist/src/adapters/claude-code/subagents.js +666 -0
- package/dist/src/adapters/claude-code/triggers.js +88 -0
- package/dist/src/cli.js +157 -0
- package/dist/src/commands/accept.js +115 -0
- package/dist/src/commands/check.js +529 -0
- package/dist/src/commands/hook.js +73 -0
- package/dist/src/commands/init.js +29 -0
- package/dist/src/commands/install.js +370 -0
- package/dist/src/commands/models.js +213 -0
- package/dist/src/commands/receives.js +84 -0
- package/dist/src/commands/report.js +386 -0
- package/dist/src/commands/resolve.js +32 -0
- package/dist/src/commands/run.js +168 -0
- package/dist/src/commands/trust.js +70 -0
- package/dist/src/commands/usage.js +10 -0
- package/dist/src/commands/watch.js +154 -0
- package/dist/src/config/create.js +51 -0
- package/dist/src/config/json-schema.js +207 -0
- package/dist/src/config/load.js +758 -0
- package/dist/src/config/paths.js +42 -0
- package/dist/src/config/preset-changes.js +190 -0
- package/dist/src/config/project.js +354 -0
- package/dist/src/config/rewrite.js +112 -0
- package/dist/src/core/backtracking.js +330 -0
- package/dist/src/core/classify.js +66 -0
- package/dist/src/core/normalize.js +24 -0
- package/dist/src/core/overlap.js +39 -0
- package/dist/src/core/regex-sample.js +278 -0
- package/dist/src/core/resolve.js +201 -0
- package/dist/src/core/route.js +93 -0
- package/dist/src/core/scope.js +77 -0
- package/dist/src/core/triggers.js +30 -0
- package/dist/src/core/types.js +74 -0
- package/dist/src/state/agents.js +200 -0
- package/dist/src/state/claims.js +80 -0
- package/dist/src/state/files.js +84 -0
- package/dist/src/state/installs.js +29 -0
- package/dist/src/state/log.js +90 -0
- package/dist/src/state/node.js +35 -0
- package/dist/src/state/sessions.js +109 -0
- package/dist/src/state/triggers.js +57 -0
- package/dist/src/version.js +24 -0
- package/dist/src/watch/service.js +172 -0
- package/dist/src/watch/watcher.js +130 -0
- package/dist/src/wording.js +11 -0
- package/hooks/hooks.json +118 -0
- package/package.json +57 -7
- package/presets/claude/SOURCES.md +485 -0
- package/presets/claude/preset.json +432 -0
- package/presets/claude/rules/fable-5-2/code-ownership.md +1 -0
- package/presets/claude/rules/fable-5-2/finish-work.md +1 -0
- package/presets/claude/rules/fable-5-2/low-context-report.md +1 -0
- package/presets/claude/rules/fable-5-2/progress-updates.md +1 -0
- package/presets/claude/rules/haiku-5-5/carry-through.md +1 -0
- package/presets/claude/rules/haiku-5-5/search-recent-facts.md +1 -0
- package/presets/claude/rules/haiku-5-5/verify-changes.md +1 -0
- package/presets/claude/rules/opus-5-5/explore-before-acting.md +1 -0
- package/presets/claude/rules/opus-5-5/progress-updates.md +1 -0
- package/presets/claude/rules/opus-5-5/unattended-run.md +1 -0
- package/presets/claude/rules/sonnet-5-5/carry-through.md +1 -0
- package/presets/claude/rules/sonnet-5-5/check-current-facts.md +1 -0
- package/presets/claude/rules/sonnet-5-5/ideas-first.md +1 -0
- package/presets/claude/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
- package/presets/claude/rules/sonnet-5-5/stay-in-scope.md +1 -0
- package/presets/claude/rules/sonnet-5-5/verify-changes.md +1 -0
- package/presets/claude/versions/1.3.0/preset.json +432 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/code-ownership.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/finish-work.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/low-context-report.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/progress-updates.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/carry-through.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/search-recent-facts.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/verify-changes.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/explore-before-acting.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/progress-updates.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/unattended-run.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/carry-through.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/check-current-facts.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/ideas-first.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/stay-in-scope.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/verify-changes.md +1 -0
- package/schema/noonsight-project.schema.json +394 -0
- package/schema/noonsight.schema.json +425 -0
- package/schema/preset.schema.json +194 -0
- package/scripts/noonsight +68 -0
- package/skills/accept/SKILL.md +11 -0
- package/skills/check/SKILL.md +11 -0
- package/skills/models/SKILL.md +11 -0
- package/skills/report/SKILL.md +11 -0
- package/skills/resolve/SKILL.md +12 -0
- package/skills/setup/SKILL.md +15 -0
- package/skills/trust/SKILL.md +12 -0
- package/skills/untrust/SKILL.md +12 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
// The Claude Code hook adapter: SessionStart and PostModelSwitch for the session itself, PostToolUse
|
|
2
|
+
// on Edit and Write for the triggers the session reveals (triggers.ts), and PreToolUse, PermissionRequest
|
|
3
|
+
// and PostToolUse on the agent tool and SubagentStart for the subagents it starts (subagents.ts).
|
|
4
|
+
//
|
|
5
|
+
// Contract (docs/hooks.md has the full table):
|
|
6
|
+
// - Always exits 0 and never holds the session beyond a bound: it reads local files only and never
|
|
7
|
+
// the network itself. It starts no process, except `ps` on macOS to read the Claude Code command
|
|
8
|
+
// line when the input does not name the session's model and, except at a resume, no record does
|
|
9
|
+
// (launch.ts), when `noonsight run` started the session (launcher.ts), or when an agent call's
|
|
10
|
+
// subagent may take its model from a definition (definitions.ts); `git ls-files`, stopped
|
|
11
|
+
// after 3 seconds, for an edit of a code file until the session reveals code-edit (code-edit.ts);
|
|
12
|
+
// and, with classifier.mode on, the classifier for an agent call, stopped after
|
|
13
|
+
// classifier.timeout_ms (classifier.ts).
|
|
14
|
+
// - Prints nothing, or one JSON object: hookSpecificOutput for Claude Code (session or subagent
|
|
15
|
+
// context, or a subagent call routing changed), systemMessage for the person.
|
|
16
|
+
// - Writes one line to the decision log for every decision it makes.
|
|
17
|
+
// - A configuration with any problem is refused whole: nothing is delivered, the person is warned,
|
|
18
|
+
// and the log records config-invalid.
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import { writeConfig } from '../../config/create.js';
|
|
21
|
+
import { ConfigError, loadConfigDetails } from '../../config/load.js';
|
|
22
|
+
import { changesDelivery, comparePresets, describeChanges } from '../../config/preset-changes.js';
|
|
23
|
+
import { configPath, stateDir } from '../../config/paths.js';
|
|
24
|
+
import { projectFields, projectFor, projectWarning } from '../../config/project.js';
|
|
25
|
+
import { normalizeModel } from '../../core/normalize.js';
|
|
26
|
+
import { joinDelivery, launcherNote, resolve, resolveAdded, stampDelivery, switchNote } from '../../core/resolve.js';
|
|
27
|
+
import { sessionPlace, sessionScope } from '../../core/scope.js';
|
|
28
|
+
import { ANY_MODEL } from '../../core/types.js';
|
|
29
|
+
import { claimOccurrence } from '../../state/claims.js';
|
|
30
|
+
import { appendDecision } from '../../state/log.js';
|
|
31
|
+
import { heldModels, processFile, pruneState, readState, sessionFile, writeState } from '../../state/sessions.js';
|
|
32
|
+
import { forPlugin } from '../../wording.js';
|
|
33
|
+
import { inheritTriggers, sessionTriggers } from '../../state/triggers.js';
|
|
34
|
+
import { forkOrigin } from './fork.js';
|
|
35
|
+
import { launchFields, launchModel, readCommandLine, resolveModelValue, resolveValue } from './launch.js';
|
|
36
|
+
import { launcherPrompt } from './launcher.js';
|
|
37
|
+
import { conversationResponse } from './predict.js';
|
|
38
|
+
import { HOOK_HANDLERS } from './settings.js';
|
|
39
|
+
import { notRouted } from './routing.js';
|
|
40
|
+
import { agentCall, agentHeld, agentResult, choiceFields, decisionFields, lateRules, subagentStart } from './subagents.js';
|
|
41
|
+
import { fileEdit, mayReveal } from './triggers.js';
|
|
42
|
+
/**
|
|
43
|
+
* Run one hook event. Returns what to print on stdout (possibly nothing). Never throws. When another
|
|
44
|
+
* kind of install runs the same event, only the copy that claims it runs (state/claims.ts).
|
|
45
|
+
*/
|
|
46
|
+
export function runHook(arg, rawInput, ctx) {
|
|
47
|
+
const out = runEvent(arg, rawInput, ctx, true);
|
|
48
|
+
return ctx.install === 'plugin' ? withMessages(out, forPlugin) : out;
|
|
49
|
+
}
|
|
50
|
+
/** `late` is false only for the event's own run after rules a subagent was owed have been given (see below). */
|
|
51
|
+
function runEvent(arg, rawInput, ctx, late) {
|
|
52
|
+
if (ctx.env.NOONSIGHT_DISABLE === '1')
|
|
53
|
+
return '';
|
|
54
|
+
const spec = HOOK_HANDLERS.find((h) => h.arg === arg);
|
|
55
|
+
if (!spec)
|
|
56
|
+
return '';
|
|
57
|
+
const event = spec.event;
|
|
58
|
+
const input = parseInput(rawInput);
|
|
59
|
+
// Anything that is not this Claude Code event (another tool reading the same settings, a hand
|
|
60
|
+
// run, a renamed event) is left alone, silently.
|
|
61
|
+
if (!input || input.hook_event_name !== event)
|
|
62
|
+
return '';
|
|
63
|
+
const toolEvent = spec.tools !== undefined;
|
|
64
|
+
const editEvent = arg === 'file-edit';
|
|
65
|
+
const subagentEvent = (toolEvent && !editEvent) || event === 'SubagentStart';
|
|
66
|
+
// Each settings entry only matches its own tools; a hand-edited or widened matcher must not reach other tools.
|
|
67
|
+
if (spec.tools !== undefined && !spec.tools.includes(str(input.tool_name) ?? ''))
|
|
68
|
+
return '';
|
|
69
|
+
const state = stateDir(ctx.home);
|
|
70
|
+
const record = {
|
|
71
|
+
ts: ctx.now.toISOString(),
|
|
72
|
+
version: ctx.version,
|
|
73
|
+
event: arg,
|
|
74
|
+
session_id: str(input.session_id),
|
|
75
|
+
...(toolEvent
|
|
76
|
+
? {
|
|
77
|
+
tool_name: str(input.tool_name),
|
|
78
|
+
tool_use_id: str(input.tool_use_id),
|
|
79
|
+
...(str(input.agent_id) !== null ? { agent_id: str(input.agent_id) } : {}),
|
|
80
|
+
permission_mode: str(input.permission_mode),
|
|
81
|
+
}
|
|
82
|
+
: event === 'SubagentStart'
|
|
83
|
+
? { agent_id: str(input.agent_id) }
|
|
84
|
+
: { source: str(input.source) }),
|
|
85
|
+
install: ctx.install,
|
|
86
|
+
};
|
|
87
|
+
const log = (fields) => {
|
|
88
|
+
try {
|
|
89
|
+
appendDecision(state, { ...record, ...fields });
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// A log that cannot be written must not cost the session its rules.
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
// A machine-wide install reaches every account; an account that never set noonsight up is left alone.
|
|
96
|
+
// Installing the plugin is setting it up, so the first session that starts under it writes the
|
|
97
|
+
// configuration, as `noonsight install` does.
|
|
98
|
+
const unset = isUnset(ctx);
|
|
99
|
+
if (unset && !(ctx.install === 'plugin' && event === 'SessionStart'))
|
|
100
|
+
return '';
|
|
101
|
+
// Claimed only by a copy that will run: one that returned above left the event to another install.
|
|
102
|
+
const claimed = once(() => !late || ctx.install === null || claimOccurrence(state, ctx.install, { event: arg, parentPid: ctx.parentPid, input: rawInput }, ctx.now.getTime()));
|
|
103
|
+
// A result or an edit inside a subagent whose start could not tell its call: the subagent's own call is
|
|
104
|
+
// known by now, and its rules ride on this hook's context. The event itself then runs as usual, and
|
|
105
|
+
// its own output is added.
|
|
106
|
+
if (late && (arg === 'agent-result' || editEvent) && str(input.agent_id) !== null) {
|
|
107
|
+
if (!claimed())
|
|
108
|
+
return '';
|
|
109
|
+
let owed = null;
|
|
110
|
+
try {
|
|
111
|
+
owed = lateRules(input, { state, now: ctx.now });
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// The subagent keeps going without them, as it started.
|
|
115
|
+
}
|
|
116
|
+
if (owed !== null) {
|
|
117
|
+
log(owed.record);
|
|
118
|
+
return withContext(runEvent(arg, rawInput, ctx, false), event, String(owed.specific?.additionalContext ?? ''));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// Every edit runs this hook; one that cannot reveal a trigger ends here, before the configuration is read.
|
|
122
|
+
if (editEvent && !mayReveal(input, state))
|
|
123
|
+
return '';
|
|
124
|
+
if (!claimed())
|
|
125
|
+
return '';
|
|
126
|
+
try {
|
|
127
|
+
const file = configPath(ctx.env, ctx.home);
|
|
128
|
+
record.config = file;
|
|
129
|
+
const wrote = unset ? writeConfig(ctx.env, ctx.home) : null;
|
|
130
|
+
if (wrote !== null)
|
|
131
|
+
record.config_written = true;
|
|
132
|
+
// The session's project: the directory Claude Code names for its hooks, else the input's cwd.
|
|
133
|
+
const project = projectFor(ctx.env.CLAUDE_PROJECT_DIR || str(input.cwd), file, state);
|
|
134
|
+
let details;
|
|
135
|
+
try {
|
|
136
|
+
details = loadConfigDetails(file, { project }, ctx.env);
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
if (!(e instanceof ConfigError))
|
|
140
|
+
throw e;
|
|
141
|
+
// The call that started the subagent already warned, and logged that nothing was delivered; the
|
|
142
|
+
// session's start already warned that the session got no rules; the call a permission prompt holds
|
|
143
|
+
// already warned.
|
|
144
|
+
if (event === 'SubagentStart' || event === 'PermissionRequest' || editEvent)
|
|
145
|
+
return '';
|
|
146
|
+
// The call this result belongs to already warned.
|
|
147
|
+
if (event === 'PostToolUse') {
|
|
148
|
+
log({ status: 'config-invalid', problems: e.problems.length, ...notRouted('config-invalid'), warned: false });
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
log({ status: 'config-invalid', problems: e.problems.length, ...(event === 'PreToolUse' ? notRouted('config-invalid') : {}), delivered: 'nothing', warned: true });
|
|
152
|
+
return output([
|
|
153
|
+
`noonsight: the configuration at ${file} was refused (${plural(e.problems.length, 'problem')}), ` +
|
|
154
|
+
'so no rules were delivered. Run `noonsight check` to see the problems.',
|
|
155
|
+
], null);
|
|
156
|
+
}
|
|
157
|
+
const config = details.config;
|
|
158
|
+
Object.assign(record, projectFields(details.project));
|
|
159
|
+
if (subagentEvent) {
|
|
160
|
+
const sub = { env: ctx.env, home: ctx.home, now: ctx.now, state, parentPid: ctx.parentPid, file };
|
|
161
|
+
const outcome = event === 'PreToolUse'
|
|
162
|
+
? agentCall(input, config, sub)
|
|
163
|
+
: event === 'SubagentStart'
|
|
164
|
+
? subagentStart(input, config, sub)
|
|
165
|
+
: event === 'PermissionRequest'
|
|
166
|
+
? agentHeld(input, config, sub)
|
|
167
|
+
: agentResult(input, config, sub);
|
|
168
|
+
if (outcome === null)
|
|
169
|
+
return '';
|
|
170
|
+
log(outcome.record);
|
|
171
|
+
return output(outcome.messages, outcome.specific);
|
|
172
|
+
}
|
|
173
|
+
const sessionId = str(input.session_id) ?? '';
|
|
174
|
+
const sessionState = sessionFile(state, sessionId);
|
|
175
|
+
if (editEvent) {
|
|
176
|
+
const outcome = fileEdit(input, config, { env: ctx.env, home: ctx.home, now: ctx.now, state, parentPid: ctx.parentPid });
|
|
177
|
+
if (outcome === null)
|
|
178
|
+
return '';
|
|
179
|
+
log(outcome.record);
|
|
180
|
+
if (outcome.state)
|
|
181
|
+
try {
|
|
182
|
+
// The conversation still holds what it held, and a delivery that may have been lost stays so.
|
|
183
|
+
const before = readState(sessionState);
|
|
184
|
+
const held = [...new Set([...heldModels(before), ...heldModels(outcome.state)])];
|
|
185
|
+
writeState(sessionState, {
|
|
186
|
+
...outcome.state,
|
|
187
|
+
held,
|
|
188
|
+
...(before?.unconfirmed !== undefined ? { unconfirmed: before.unconfirmed } : {}),
|
|
189
|
+
...(before?.chosen !== undefined ? { chosen: before.chosen } : {}),
|
|
190
|
+
// A resume's delivery is confirmed by a response after it, so its time is kept.
|
|
191
|
+
at: before?.unconfirmed === 'resume' ? before.at : ctx.now.toISOString(),
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
// Without the record, a later resume delivers again: the safe direction.
|
|
196
|
+
}
|
|
197
|
+
return output([], outcome.context ? { hookEventName: event, additionalContext: outcome.context } : null);
|
|
198
|
+
}
|
|
199
|
+
const processState = processFile(state, ctx.parentPid);
|
|
200
|
+
const commandLine = once(() => readCommandLine(ctx.parentPid, process.platform));
|
|
201
|
+
const own = readState(sessionState);
|
|
202
|
+
// A fork starts with a copy of another conversation under a new id that has no record (fork.ts).
|
|
203
|
+
const fork = event === 'SessionStart' && str(input.source) === 'fork' && own === null ? forkContext(input, state, ctx.env, commandLine) : null;
|
|
204
|
+
if (fork?.origin)
|
|
205
|
+
inheritTriggers(state, fork.origin.sessionId, sessionId);
|
|
206
|
+
// Both come from the environment Claude Code runs in, which its hooks inherit.
|
|
207
|
+
const where = {
|
|
208
|
+
scope: sessionScope(ctx.env),
|
|
209
|
+
place: sessionPlace(config, ctx.env),
|
|
210
|
+
triggers: sessionTriggers(state, str(input.session_id)),
|
|
211
|
+
};
|
|
212
|
+
const response = once(() => conversationResponse(str(input.transcript_path)));
|
|
213
|
+
const result = event === 'SessionStart'
|
|
214
|
+
? sessionStart(input, config, file, where, own, readState(processState), (sources) => launchModel({ env: ctx.env, home: ctx.home, pid: ctx.parentPid, cwd: str(input.cwd), commandLine }, sources), response, () => launcherPrompt(ctx.env, commandLine), fork)
|
|
215
|
+
: modelSwitch(input, config, file, where, readState(sessionState), ctx.env);
|
|
216
|
+
const startup = event === 'SessionStart' && input.source === 'startup';
|
|
217
|
+
const update = startup ? presetUpdate(details, result.state?.model ?? null, file, ctx.env) : null;
|
|
218
|
+
if (update !== null) {
|
|
219
|
+
result.record.preset_update = update.fields;
|
|
220
|
+
result.record.warned = true;
|
|
221
|
+
}
|
|
222
|
+
// A project left out is reported once, as the session starts.
|
|
223
|
+
const projectNote = startup ? projectWarning(details.project) : null;
|
|
224
|
+
if (projectNote !== null)
|
|
225
|
+
result.record.warned = true;
|
|
226
|
+
if (wrote !== null)
|
|
227
|
+
result.record.warned = true;
|
|
228
|
+
log(result.record);
|
|
229
|
+
if (result.state) {
|
|
230
|
+
const saved = { ...result.state, at: ctx.now.toISOString() };
|
|
231
|
+
try {
|
|
232
|
+
writeState(sessionState, saved);
|
|
233
|
+
writeState(processState, saved);
|
|
234
|
+
if (event === 'SessionStart' && input.source === 'startup')
|
|
235
|
+
pruneState(state, ctx.now.getTime());
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
// Without the record, a later resume delivers again: the safe direction.
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Claude Code leaves out a resume's or a fork's context when an earlier start of the conversation
|
|
242
|
+
// added the same text, so each such delivery carries its time (docs/hooks.md, "SessionStart").
|
|
243
|
+
const resumedStart = event === 'SessionStart' && (input.source === 'resume' || input.source === 'fork');
|
|
244
|
+
const context = result.context !== null && resumedStart ? stampDelivery(result.context, ctx.now.toISOString()) : result.context;
|
|
245
|
+
return output([...(wrote !== null ? [firstConfigMessage(file)] : []), ...result.messages, ...(update !== null ? [update.message] : []), ...(projectNote !== null ? [projectNote] : [])], context ? { hookEventName: event, additionalContext: context } : null);
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
249
|
+
// The edit itself is done, and a warning after every edit would only repeat; the log keeps the error.
|
|
250
|
+
if (editEvent) {
|
|
251
|
+
log({ status: 'error', error: message, delivered: 'nothing', warned: false });
|
|
252
|
+
return '';
|
|
253
|
+
}
|
|
254
|
+
if (event === 'PostToolUse') {
|
|
255
|
+
log({ status: 'error', error: message, ...notRouted('error'), warned: true });
|
|
256
|
+
return output([`noonsight: the hook failed (${message}), so the subagent's model was not checked.`], null);
|
|
257
|
+
}
|
|
258
|
+
// A permission request is answered by the person; the call it holds may then start too late for its rules.
|
|
259
|
+
if (event === 'PermissionRequest') {
|
|
260
|
+
log({ status: 'error', error: message, warned: false });
|
|
261
|
+
return '';
|
|
262
|
+
}
|
|
263
|
+
if (event === 'SubagentStart') {
|
|
264
|
+
log({ status: 'error', error: message, delivered: 'nothing', warned: true });
|
|
265
|
+
return output([`noonsight: the hook failed (${message}), so the subagent that started got no rules.`], null);
|
|
266
|
+
}
|
|
267
|
+
log({ status: 'error', error: message, ...(event === 'PreToolUse' ? notRouted('error') : {}), delivered: 'nothing', warned: true });
|
|
268
|
+
return output([`noonsight: the hook failed (${message}), so no rules were delivered.`], null);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function forkContext(input, state, env, commandLine) {
|
|
272
|
+
const origin = forkOrigin(str(input.transcript_path), env, commandLine);
|
|
273
|
+
return {
|
|
274
|
+
origin,
|
|
275
|
+
record: origin !== null ? readState(sessionFile(state, origin.sessionId)) : null,
|
|
276
|
+
response: once(() => conversationResponse(origin?.transcript ?? null)),
|
|
277
|
+
launch: origin?.model !== undefined ? resolveValue(origin.model, 'claude-args', '--model of the background job', env) : null,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The record of a conversation nothing is known of: it may hold the rules of any model, so the next
|
|
282
|
+
* delivery says that those of every model but the session's own no longer apply.
|
|
283
|
+
*/
|
|
284
|
+
const UNKNOWN_HELD = { model: null, status: 'none', sha256: null, held: [ANY_MODEL], at: new Date(0).toISOString() };
|
|
285
|
+
/**
|
|
286
|
+
* SessionStart, by how the session started:
|
|
287
|
+
* startup deliver
|
|
288
|
+
* clear, compact deliver again: the earlier context, and the rules in it, are gone
|
|
289
|
+
* resume, fork deliver only when the model or the rules changed since this session's record, or
|
|
290
|
+
* when its last delivery may never have arrived: a /model switch's, or a resume's
|
|
291
|
+
* the conversation has not answered since
|
|
292
|
+
* anything else deliver: delivering twice costs little, missing a delivery is the defect
|
|
293
|
+
*
|
|
294
|
+
* The model comes from the input. When Claude Code leaves it out, after /clear and compaction it comes
|
|
295
|
+
* from the record of the same session or of the same Claude Code process, which keeps the model a
|
|
296
|
+
* /model since the launch chose; and when neither knows it, as in every `claude -p` run, from what the
|
|
297
|
+
* session was started with (launch.ts). A resumed or forked conversation runs in a new Claude Code
|
|
298
|
+
* process, which takes its model from --model, else ANTHROPIC_MODEL, else the model of the
|
|
299
|
+
* conversation's last response since its last compaction, else the model setting; so there those come
|
|
300
|
+
* first, in that order, and the session's record after them. When the model is not the one the record
|
|
301
|
+
* holds, the conversation still holds that model's rules, and as at a /model switch a note says they no
|
|
302
|
+
* longer apply.
|
|
303
|
+
*
|
|
304
|
+
* A session `noonsight run` started already holds rules in its system prompt. Claude Code renders the
|
|
305
|
+
* system prompt from the current launch for a new conversation, after /clear and after compaction,
|
|
306
|
+
* so at those starts the rules are not delivered again when the launcher's text holds them. A resumed
|
|
307
|
+
* or forked conversation keeps the system prompt it was first sent with, so the session record decides.
|
|
308
|
+
* When the launcher's rules are for another model than the session's, a note says they do not apply.
|
|
309
|
+
*
|
|
310
|
+
* The rules are resolved with the triggers the session has revealed (triggers.ts), which its id keeps:
|
|
311
|
+
* after compaction, the same session gets the rules a trigger added again; after /clear, a new session
|
|
312
|
+
* starts with none.
|
|
313
|
+
*/
|
|
314
|
+
function sessionStart(input, config, file, where, session, process, readLaunch, ownConversation, readLauncher, fork = null) {
|
|
315
|
+
const source = str(input.source);
|
|
316
|
+
const resumed = source === 'resume' || source === 'fork';
|
|
317
|
+
// A fork's own id has no record yet: the conversation it copied stands in for it, as that
|
|
318
|
+
// conversation's record and last response; with nothing known of it, it may hold any model's rules.
|
|
319
|
+
const known = fork !== null ? (fork.record ?? UNKNOWN_HELD) : session;
|
|
320
|
+
const readConversation = fork !== null ? () => ownConversation() ?? fork.response() : ownConversation;
|
|
321
|
+
// A background job runs on the model it was started with, which its own command line does not show.
|
|
322
|
+
const readStart = fork?.launch ? (sources) => (sources === 'settings' ? readLaunch(sources) : fork.launch) : readLaunch;
|
|
323
|
+
const reported = normalizeModel(str(input.model));
|
|
324
|
+
const recorded = known?.model ?? (source === 'clear' || source === 'compact' ? process?.model : null) ?? null;
|
|
325
|
+
// Claude Code may run another model in place of the one the person chose, as it runs Sonnet in plan
|
|
326
|
+
// mode for Haiku; the start of a compaction, /resume or /branch in that process then reports the
|
|
327
|
+
// chosen model, and the process keeps running the other, so the record of the same process, which
|
|
328
|
+
// the switch to it wrote, decides. A resume or fork that reports a model runs in the same process;
|
|
329
|
+
// one in a new process reports none, and a new process starts on the model it is given.
|
|
330
|
+
const sameProcess = source === 'compact' || source === 'clear' || (resumed && !!reported);
|
|
331
|
+
const carried = sameProcess ? (process ?? (source === 'compact' ? known : null)) : null;
|
|
332
|
+
const substituted = carried?.chosen !== undefined && carried.model !== null && (!reported || reported === carried.chosen) ? carried : null;
|
|
333
|
+
const chosen = substituted
|
|
334
|
+
? { model: substituted.model, source: substituted === process ? 'process-record' : 'session-record', launch: null }
|
|
335
|
+
: reported
|
|
336
|
+
? { model: reported, source: 'hook-input', launch: null }
|
|
337
|
+
: resumed
|
|
338
|
+
? resumedModel(recorded, readStart, () => readConversation()?.model ?? null)
|
|
339
|
+
: recorded
|
|
340
|
+
? { model: recorded, source: session?.model ? 'session-record' : 'process-record', launch: null }
|
|
341
|
+
: launchChoice(readLaunch('all'), null);
|
|
342
|
+
const { model, launch } = chosen;
|
|
343
|
+
const request = { model, scope: where.scope.scope, place: where.place.place, triggers: where.triggers };
|
|
344
|
+
const decision = resolve(config, request);
|
|
345
|
+
const sha = decision.status === 'matched' ? decision.sha256 : null;
|
|
346
|
+
const record = resumed && known !== null ? confirmed(known, fork !== null ? fork.response : readConversation) : known;
|
|
347
|
+
// A delivery that may never have reached the conversation is made again.
|
|
348
|
+
const unconfirmed = resumed && record?.unconfirmed !== undefined;
|
|
349
|
+
const repeat = resumed && record !== null && !unconfirmed && record.model === decision.model && record.sha256 === sha;
|
|
350
|
+
// A resumed conversation still holds the rules it held; those of another model no longer apply.
|
|
351
|
+
const kept = resumed ? heldModels(record) : [];
|
|
352
|
+
const retired = repeat || decision.model === null ? [] : kept.filter((m) => m !== decision.model);
|
|
353
|
+
const changedFrom = resumed && record?.model && decision.model !== null && record.model !== decision.model ? record.model : null;
|
|
354
|
+
const launcher = source === 'startup' || source === 'clear' || source === 'compact' ? readLauncher() : null;
|
|
355
|
+
// The launcher's system prompt holds the rules for the session's scope; it never holds those a
|
|
356
|
+
// trigger the session revealed later added, which are delivered here.
|
|
357
|
+
const forLauncher = where.triggers.length ? resolve(config, { ...request, triggers: [] }) : decision;
|
|
358
|
+
const inPrompt = launcher !== null && forLauncher.status === 'matched' && launcher.content.includes(forLauncher.text);
|
|
359
|
+
const added = inPrompt && where.triggers.length ? resolveAdded(config, request, []) : null;
|
|
360
|
+
const sent = inPrompt ? (added?.status === 'matched' ? added : null) : decision.status === 'matched' ? decision : null;
|
|
361
|
+
const otherModel = launcher !== null && !inPrompt && launcher.model !== null && launcher.model !== decision.model ? launcher.model : null;
|
|
362
|
+
const rules = sent !== null && !repeat ? sent.text : null;
|
|
363
|
+
const note = retired.length
|
|
364
|
+
? switchNote(changedFrom, decision.model ?? '', retired)
|
|
365
|
+
: otherModel !== null
|
|
366
|
+
? launcherNote(otherModel, decision.model ?? '')
|
|
367
|
+
: null;
|
|
368
|
+
const parts = [...(note ? [note] : []), ...(rules ? [rules] : [])];
|
|
369
|
+
const messages = repeat
|
|
370
|
+
? []
|
|
371
|
+
: [
|
|
372
|
+
...(otherModel !== null
|
|
373
|
+
? [
|
|
374
|
+
`noonsight: noonsight run put the rules for ${otherModel} in the system prompt, but this session runs on ${decision.model ?? 'a model Claude Code did not report'}; ` +
|
|
375
|
+
'the session was told that those rules do not apply.',
|
|
376
|
+
]
|
|
377
|
+
: []),
|
|
378
|
+
...undeclaredMessages(decision, file, launch),
|
|
379
|
+
];
|
|
380
|
+
// A session start's delivery, once it reaches the conversation, leaves only this model's rules
|
|
381
|
+
// standing: those it delivers or finds, or, when it has none now, those it held before. That of a
|
|
382
|
+
// startup, /clear or compaction is part of the conversation from its start; what a resume or a fork
|
|
383
|
+
// returns reaches it with the process's first request only, so until a response shows it arrived,
|
|
384
|
+
// the conversation may still hold every model's rules it held.
|
|
385
|
+
const holds = decision.model !== null && (decision.status === 'matched' || kept.includes(decision.model));
|
|
386
|
+
const own = holds ? [decision.model] : [];
|
|
387
|
+
const pending = resumed && parts.length > 0;
|
|
388
|
+
const held = repeat ? kept : pending ? [...new Set([...kept, ...own])] : own;
|
|
389
|
+
return {
|
|
390
|
+
record: {
|
|
391
|
+
// Rules sent beside the launcher's are only those a trigger added, so the line names those.
|
|
392
|
+
...decisionFields(sent !== null && sent !== decision && !repeat ? sent : decision),
|
|
393
|
+
...choiceFields(where.scope, where.place),
|
|
394
|
+
...triggerFields(where.triggers),
|
|
395
|
+
model_source: chosen.source,
|
|
396
|
+
...(substituted !== null ? { chosen_model: substituted.chosen, ...(reported ? { reported_model: reported } : {}) } : {}),
|
|
397
|
+
...(fork !== null ? { fork_origin: fork.origin?.via ?? 'unknown', ...(fork.origin !== null ? { forked_from: fork.origin.sessionId } : {}) } : {}),
|
|
398
|
+
...(changedFrom !== null ? { from_model: changedFrom } : {}),
|
|
399
|
+
...launchFields(launch),
|
|
400
|
+
...(launcher !== null ? { launch_file: launcher.file, launch_model: launcher.model } : {}),
|
|
401
|
+
delivered: repeat ? 'already-delivered' : rules ? (note ? 'rules+note' : 'rules') : inPrompt ? 'launcher' : note ? 'note' : 'nothing',
|
|
402
|
+
warned: messages.length > 0,
|
|
403
|
+
},
|
|
404
|
+
context: parts.length ? joinDelivery(parts) : null,
|
|
405
|
+
messages,
|
|
406
|
+
state: decision.model === null
|
|
407
|
+
? null
|
|
408
|
+
: { model: decision.model, status: decision.status, sha256: sha, held, ...(pending ? { unconfirmed: 'resume' } : {}), ...(substituted?.chosen ? { chosen: substituted.chosen } : {}) },
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* A resumed conversation's record as the conversation stands. A resume's delivery goes with the first
|
|
413
|
+
* request its process sends, so when the conversation has answered since that delivery, it arrived,
|
|
414
|
+
* and its note left only the record's model's rules standing. Otherwise the record stands as written.
|
|
415
|
+
*/
|
|
416
|
+
function confirmed(session, readConversation) {
|
|
417
|
+
if (session.unconfirmed !== 'resume')
|
|
418
|
+
return session;
|
|
419
|
+
const delivered = Date.parse(session.at);
|
|
420
|
+
const answered = readConversation()?.at ?? null;
|
|
421
|
+
if (answered === null || Number.isNaN(delivered) || answered <= delivered)
|
|
422
|
+
return session;
|
|
423
|
+
const { unconfirmed: _, ...rest } = session;
|
|
424
|
+
return { ...rest, held: heldModels(session).filter((m) => m === session.model) };
|
|
425
|
+
}
|
|
426
|
+
function launchChoice(launch, fallback) {
|
|
427
|
+
if (launch.kind === 'found')
|
|
428
|
+
return { model: launch.model, source: launch.source, launch };
|
|
429
|
+
return { model: fallback?.model ?? null, source: fallback?.source ?? null, launch };
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The model of a resumed or forked conversation, which Claude Code reports no model for, in the order
|
|
433
|
+
* Claude Code itself takes it: --model, else ANTHROPIC_MODEL, else the model of the conversation's
|
|
434
|
+
* last response since its last compaction, else the model setting. A value noonsight cannot tell
|
|
435
|
+
* which model it selects leaves the conversation to decide, and below the setting the session's record
|
|
436
|
+
* does: Claude Code then runs its default model, which noonsight cannot tell either.
|
|
437
|
+
*
|
|
438
|
+
* One setting outranks the conversation: `haiku`, over a last response from Haiku or Sonnet
|
|
439
|
+
* (keepsHaikuSetting).
|
|
440
|
+
*/
|
|
441
|
+
function resumedModel(recorded, readLaunch, readConversation) {
|
|
442
|
+
const explicit = readLaunch('explicit');
|
|
443
|
+
if (explicit.kind === 'found')
|
|
444
|
+
return launchChoice(explicit, null);
|
|
445
|
+
// An explicit value noonsight cannot pin down outranks the setting, as it does in Claude Code.
|
|
446
|
+
const settings = explicit.kind === 'none' ? readLaunch('settings') : explicit;
|
|
447
|
+
const conversation = readConversation();
|
|
448
|
+
if (conversation !== null && !keepsHaikuSetting(settings, conversation))
|
|
449
|
+
return { model: conversation, source: 'session-transcript', launch: explicit.kind === 'none' ? null : explicit };
|
|
450
|
+
if (settings.kind === 'found')
|
|
451
|
+
return launchChoice(settings, null);
|
|
452
|
+
return launchChoice(settings, recorded !== null ? { model: recorded, source: 'session-record' } : null);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Whether Claude Code resumes on the model setting rather than on the conversation's last response:
|
|
456
|
+
* it does when the setting is exactly `haiku` and that response came from Haiku or Sonnet. Under any
|
|
457
|
+
* other setting, `opusplan` and `Haiku` included, and under `haiku` after a response from Opus or
|
|
458
|
+
* Fable, the conversation keeps its own model (docs/hooks.md, "SessionStart").
|
|
459
|
+
*/
|
|
460
|
+
function keepsHaikuSetting(settings, conversation) {
|
|
461
|
+
return settings.kind === 'found' && settings.source === 'settings' && settings.alias === 'haiku' && (conversation.includes('haiku') || conversation.includes('sonnet'));
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* PostModelSwitch: deliver the new model's rules. The rules delivered for earlier models stay in the
|
|
465
|
+
* conversation, so when the conversation may hold any, a note says they no longer apply. Claude Code
|
|
466
|
+
* adds what this hook returns to the next request only, and loses it when another switch or the end
|
|
467
|
+
* of the session comes first, so a switch's delivery stays unconfirmed: the next switch's note again
|
|
468
|
+
* names every model whose rules the conversation may hold, and the next resume delivers again.
|
|
469
|
+
*/
|
|
470
|
+
function modelSwitch(input, config, file, where, session, env) {
|
|
471
|
+
const to = normalizeModel(str(input.to_model));
|
|
472
|
+
const from = normalizeModel(str(input.from_model));
|
|
473
|
+
const chosen = chosenModel(input, from, to, session, env);
|
|
474
|
+
const request = { scope: where.scope.scope, place: where.place.place, triggers: where.triggers };
|
|
475
|
+
const decision = resolve(config, { model: to, ...request });
|
|
476
|
+
const sha = decision.status === 'matched' ? decision.sha256 : null;
|
|
477
|
+
// A switch right after a delivery that may have been lost makes it again.
|
|
478
|
+
const unconfirmed = session?.unconfirmed !== undefined;
|
|
479
|
+
const base = {
|
|
480
|
+
...decisionFields(decision),
|
|
481
|
+
...choiceFields(where.scope, where.place),
|
|
482
|
+
...triggerFields(where.triggers),
|
|
483
|
+
model_source: 'hook-input',
|
|
484
|
+
from_model: from || null,
|
|
485
|
+
...(chosen !== null ? { chosen_model: chosen } : {}),
|
|
486
|
+
};
|
|
487
|
+
// Without a record, the previous model's rules are those the conversation may hold.
|
|
488
|
+
const kept = session
|
|
489
|
+
? heldModels(session)
|
|
490
|
+
: from && resolve(config, { model: from, ...request }).status === 'matched'
|
|
491
|
+
? [from]
|
|
492
|
+
: [];
|
|
493
|
+
const held = [...new Set([...kept, ...(decision.status === 'matched' && decision.model !== null ? [decision.model] : [])])];
|
|
494
|
+
// Resuming a session restores its model and reports it as a switch; the rules may already be there.
|
|
495
|
+
if (session !== null && !unconfirmed && session.model === decision.model && session.sha256 === sha)
|
|
496
|
+
return {
|
|
497
|
+
record: { ...base, delivered: 'already-delivered', warned: false },
|
|
498
|
+
context: null,
|
|
499
|
+
messages: [],
|
|
500
|
+
state: decision.model === null ? null : { model: decision.model, status: decision.status, sha256: sha, held, ...(chosen !== null ? { chosen } : {}) },
|
|
501
|
+
};
|
|
502
|
+
const retired = kept.filter((m) => m !== decision.model);
|
|
503
|
+
const note = retired.length ? switchNote(from || session?.model || null, to, retired) : null;
|
|
504
|
+
const parts = [...(note ? [note] : []), ...(decision.status === 'matched' ? [decision.text] : [])];
|
|
505
|
+
const messages = undeclaredMessages(decision, file);
|
|
506
|
+
return {
|
|
507
|
+
record: {
|
|
508
|
+
...base,
|
|
509
|
+
delivered: decision.status === 'matched' ? (note ? 'rules+note' : 'rules') : note ? 'note' : 'nothing',
|
|
510
|
+
warned: messages.length > 0,
|
|
511
|
+
},
|
|
512
|
+
context: parts.length ? joinDelivery(parts) : null,
|
|
513
|
+
messages,
|
|
514
|
+
state: decision.model === null
|
|
515
|
+
? null
|
|
516
|
+
: { model: decision.model, status: decision.status, sha256: sha, held, ...(parts.length ? { unconfirmed: 'switch' } : {}), ...(chosen !== null ? { chosen } : {}) },
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* The model the person chose, when the switch leaves Claude Code running another in its place, else
|
|
521
|
+
* null. A switch Claude Code makes by itself (`source: "auto"`, as when plan mode on Haiku runs Sonnet)
|
|
522
|
+
* keeps the model chosen before it, until a switch back to that model; a `/model` whose
|
|
523
|
+
* `requested_model` selects another model than the one it switched to was substituted the same way.
|
|
524
|
+
*/
|
|
525
|
+
function chosenModel(input, from, to, session, env) {
|
|
526
|
+
if (str(input.source) === 'auto') {
|
|
527
|
+
const before = session?.chosen ?? (from || null);
|
|
528
|
+
return before !== null && before !== to ? before : null;
|
|
529
|
+
}
|
|
530
|
+
const requested = str(input.requested_model);
|
|
531
|
+
const asked = requested !== null ? resolveModelValue(requested, env) : null;
|
|
532
|
+
return asked?.kind === 'found' && asked.model !== to ? asked.model : null;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* The warning a session gets as it starts when the installed preset is not the version the
|
|
536
|
+
* configuration accepted and that changes the session's model's rules: under "warn", what the
|
|
537
|
+
* session receives now that it did not; under "hold", what accepting the installed version would
|
|
538
|
+
* change. A failure here never costs the session its rules: `noonsight check` reports it.
|
|
539
|
+
*/
|
|
540
|
+
function presetUpdate(details, model, file, env) {
|
|
541
|
+
const v = details.presetVersions;
|
|
542
|
+
if (model === null || v.accepted === null || v.installed === null || v.accepted === v.installed.version)
|
|
543
|
+
return null;
|
|
544
|
+
try {
|
|
545
|
+
const c = comparePresets(v, details.declared, {}, env, model);
|
|
546
|
+
const changes = c?.models[0];
|
|
547
|
+
if (!c || c.from === null || changes === undefined || !changesDelivery(changes))
|
|
548
|
+
return null;
|
|
549
|
+
const to = c.installed.version;
|
|
550
|
+
const what = describeChanges(changes, c.held);
|
|
551
|
+
const message = c.held
|
|
552
|
+
? `noonsight: preset ${c.name} ${to} is installed, and this session keeps the rules of ${c.accepted}, held in ${file}; ` +
|
|
553
|
+
`${to} would change those of ${model}: ${what}. Run \`noonsight accept\` to take ${to}.`
|
|
554
|
+
: `noonsight: preset ${c.name} ${to} changed the rules of ${model} since ${c.accepted}, the version accepted in ${file}: ${what}. ` +
|
|
555
|
+
`Run \`noonsight check\` for every model, and \`noonsight accept\` to accept ${to}.`;
|
|
556
|
+
return { message, fields: { accepted: c.accepted, installed: to, held: c.held } };
|
|
557
|
+
}
|
|
558
|
+
catch {
|
|
559
|
+
return null;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
/** The triggers a session has revealed, for its log line; nothing when it has revealed none. */
|
|
563
|
+
function triggerFields(triggers) {
|
|
564
|
+
return triggers.length ? { triggers } : {};
|
|
565
|
+
}
|
|
566
|
+
function undeclaredMessages(decision, file, launch = null) {
|
|
567
|
+
if (decision.status !== 'undeclared' || decision.warning === undefined)
|
|
568
|
+
return [];
|
|
569
|
+
if (decision.reason !== 'model-not-reported')
|
|
570
|
+
return [
|
|
571
|
+
`noonsight: ${decision.model} is not declared in ${file}, so this session gets no rules. ` +
|
|
572
|
+
'Declare it under "models" (with "use": "none" if it needs no rules).',
|
|
573
|
+
];
|
|
574
|
+
if (launch?.kind === 'unresolved')
|
|
575
|
+
return [
|
|
576
|
+
`noonsight: this session's model is set to "${launch.value}" (${launch.from}), and noonsight cannot tell which model that is ` +
|
|
577
|
+
`(${launch.why}), so no rules were delivered.`,
|
|
578
|
+
];
|
|
579
|
+
return [
|
|
580
|
+
"noonsight: Claude Code did not report this session's model, and neither its command line, ANTHROPIC_MODEL nor a model setting names one, " +
|
|
581
|
+
'so no rules were delivered.',
|
|
582
|
+
];
|
|
583
|
+
}
|
|
584
|
+
function output(messages, specific) {
|
|
585
|
+
if (!specific && messages.length === 0)
|
|
586
|
+
return '';
|
|
587
|
+
return JSON.stringify({
|
|
588
|
+
...(messages.length ? { systemMessage: messages.join('\n') } : {}),
|
|
589
|
+
...(specific ? { hookSpecificOutput: specific } : {}),
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
/** Whether the account never set noonsight up: no configuration at the default path, and none named. */
|
|
593
|
+
function isUnset(ctx) {
|
|
594
|
+
try {
|
|
595
|
+
return !ctx.env.NOONSIGHT_CONFIG && !fs.existsSync(configPath(ctx.env, ctx.home));
|
|
596
|
+
}
|
|
597
|
+
catch {
|
|
598
|
+
// The event's own run meets the failure, and reports it.
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** What the plugin's first session says once it has written the configuration. */
|
|
603
|
+
function firstConfigMessage(file) {
|
|
604
|
+
return (`noonsight: no configuration was found, so ${file} was written from the preset, and sessions and subagents now receive its rules for their model. ` +
|
|
605
|
+
'Run `noonsight check` to see what each model receives and how to change it.');
|
|
606
|
+
}
|
|
607
|
+
/** A hook's output with each message for the person reworded; its context is left as it is. */
|
|
608
|
+
function withMessages(out, word) {
|
|
609
|
+
if (out === '')
|
|
610
|
+
return out;
|
|
611
|
+
const parsed = JSON.parse(out);
|
|
612
|
+
return typeof parsed.systemMessage === 'string' ? JSON.stringify({ ...parsed, systemMessage: word(parsed.systemMessage) }) : out;
|
|
613
|
+
}
|
|
614
|
+
/** A hook's output with more context added to it, for the event given. */
|
|
615
|
+
function withContext(out, event, context) {
|
|
616
|
+
if (context === '')
|
|
617
|
+
return out;
|
|
618
|
+
let parsed = {};
|
|
619
|
+
try {
|
|
620
|
+
if (out !== '')
|
|
621
|
+
parsed = JSON.parse(out);
|
|
622
|
+
}
|
|
623
|
+
catch {
|
|
624
|
+
parsed = {};
|
|
625
|
+
}
|
|
626
|
+
const specific = (typeof parsed.hookSpecificOutput === 'object' && parsed.hookSpecificOutput !== null ? parsed.hookSpecificOutput : {});
|
|
627
|
+
const earlier = typeof specific.additionalContext === 'string' && specific.additionalContext !== '' ? specific.additionalContext : null;
|
|
628
|
+
return JSON.stringify({
|
|
629
|
+
...parsed,
|
|
630
|
+
hookSpecificOutput: { ...specific, hookEventName: event, additionalContext: earlier === null ? context : `${context}\n\n${earlier}` },
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
function parseInput(raw) {
|
|
634
|
+
try {
|
|
635
|
+
const value = JSON.parse(raw);
|
|
636
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? value : null;
|
|
637
|
+
}
|
|
638
|
+
catch {
|
|
639
|
+
return null;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function str(value) {
|
|
643
|
+
return typeof value === 'string' && value !== '' ? value : null;
|
|
644
|
+
}
|
|
645
|
+
function once(read) {
|
|
646
|
+
let cached = null;
|
|
647
|
+
return () => (cached ??= { value: read() }).value;
|
|
648
|
+
}
|
|
649
|
+
function plural(n, word) {
|
|
650
|
+
return `${n} ${word}${n === 1 ? '' : 's'}`;
|
|
651
|
+
}
|
|
652
|
+
//# sourceMappingURL=hook.js.map
|