@alexeiled/claude-router 0.8.0 → 1.0.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.
Files changed (44) hide show
  1. package/.claude-plugin/plugin.json +10 -6
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +51 -100
  4. package/docs/architecture.md +55 -419
  5. package/docs/configuration.md +86 -182
  6. package/docs/evaluation.md +62 -81
  7. package/docs/native-router.md +79 -0
  8. package/docs/user-guide.md +103 -172
  9. package/hooks/hooks.json +1 -26
  10. package/hooks/native-router.mjs +635 -0
  11. package/lib/config.mjs +20 -58
  12. package/lib/{facts.mjs → facts-pure.mjs} +2 -23
  13. package/lib/{jev.mjs → jev-contract.mjs} +17 -38
  14. package/lib/native-cost.mjs +88 -0
  15. package/lib/native-display.mjs +75 -0
  16. package/lib/native-jev.mjs +125 -0
  17. package/lib/native-panel.mjs +150 -0
  18. package/lib/native-router.mjs +216 -0
  19. package/lib/policy.mjs +17 -21
  20. package/package.json +9 -8
  21. package/scripts/migrate-config.mjs +57 -0
  22. package/types/index.d.ts +95 -0
  23. package/lib/cost.mjs +0 -93
  24. package/lib/gateway.mjs +0 -221
  25. package/lib/idle.mjs +0 -41
  26. package/lib/rewrite.mjs +0 -130
  27. package/lib/router.mjs +0 -276
  28. package/lib/runtime.mjs +0 -78
  29. package/lib/sse.mjs +0 -57
  30. package/lib/status.mjs +0 -152
  31. package/lib/store.mjs +0 -76
  32. package/scripts/ensure-gateway.mjs +0 -143
  33. package/scripts/gateway.mjs +0 -81
  34. package/scripts/git-hooks/pre-commit +0 -41
  35. package/scripts/git-hooks/pre-push +0 -34
  36. package/scripts/status.mjs +0 -20
  37. package/scripts/statusline.mjs +0 -45
  38. package/scripts/transcript-models.sh +0 -4
  39. package/skills/high/SKILL.md +0 -9
  40. package/skills/low/SKILL.md +0 -7
  41. package/skills/medium/SKILL.md +0 -8
  42. package/skills/micro/SKILL.md +0 -7
  43. package/skills/setup/SKILL.md +0 -31
  44. package/skills/status/SKILL.md +0 -12
package/lib/cost.mjs DELETED
@@ -1,93 +0,0 @@
1
- // Cache-aware cost of the next request. Pure arithmetic over configured prices and the gateway's memory.
2
- import { clampEffort } from './rewrite.mjs';
3
-
4
- // The prompt cache belongs to a model, and its messages part to the effort too: a top-level effort change rewrites
5
- // the messages cache. Sonnet at `high` and Sonnet at `xhigh` are two caches, not one.
6
- export function cacheKey(modelId, effort) {
7
- return effort ? `${modelId}@${effort}` : modelId;
8
- }
9
-
10
- // The cache a request routed to `tier` uses. `sentEffort` is what Claude Code sent, for a route that keeps it.
11
- export function routeCacheKey(config, tier, sentEffort) {
12
- const model = modelOf(config, tier);
13
- return cacheKey(model.id, clampEffort(config.routes[tier].effort ?? sentEffort, model.efforts));
14
- }
15
-
16
- export function isWarm(modelState, now, cache) {
17
- if (!modelState) return false;
18
- return now < modelState.lastAt + cache.ttlMs[modelState.ttl] - cache.warmMarginMs;
19
- }
20
-
21
- // For the logs: `unknown` means no response for this cache since the session started or its history broke. The cost
22
- // arithmetic treats `unknown` and `expired` alike, as a full write.
23
- export function cacheState(modelState, now, cache) {
24
- if (!modelState) return 'unknown';
25
- return isWarm(modelState, now, cache) ? 'warm' : 'expired';
26
- }
27
-
28
- function modelOf(config, tier) {
29
- return config.models[config.routes[tier].model];
30
- }
31
-
32
- function ttlFor(config, alias, facts) {
33
- if (config.models[alias].billing === 'credits') return '5m';
34
- return facts.lastRequest?.ttl ?? '5m';
35
- }
36
-
37
- // USD for the input side of one request routed to `tier` with `tokens` of context.
38
- export function inputCostUsd(config, tier, tokens, facts, now) {
39
- const alias = config.routes[tier].model;
40
- const model = config.models[alias];
41
- const state = facts.models[routeCacheKey(config, tier, facts.effort)];
42
- const reusable = isWarm(state, now, config.cache) ? Math.min(state.prefixTokens, tokens) : 0;
43
- const write = model.input * config.cache.writeMultiplier[ttlFor(config, alias, facts)];
44
- return (model.cacheRead * reusable + write * (tokens - reusable)) / 1e6;
45
- }
46
-
47
- export function coldWriteUsd(config, alias, tokens, facts) {
48
- const model = config.models[alias];
49
- return (model.input * config.cache.writeMultiplier[ttlFor(config, alias, facts)] * tokens) / 1e6;
50
- }
51
-
52
- // Estimated context of the next request: last request plus its output. Tool results and the new prompt are not counted.
53
- export function nextContextTokens(facts) {
54
- const last = facts.lastRequest;
55
- return last ? last.tokens + last.outputTokens : 0;
56
- }
57
-
58
- export function switchingTaxUsd(config, candidateTier, incumbentTier, facts, now) {
59
- const tokens = nextContextTokens(facts);
60
- return (
61
- inputCostUsd(config, candidateTier, tokens, facts, now) - inputCostUsd(config, incumbentTier, tokens, facts, now)
62
- );
63
- }
64
-
65
- // Shadow estimate for the decision log; the downgrade tax nets it over a horizon. At list prices, so for `plan` models
66
- // it is a list-price equivalent, not a charge. Output uses the last observed output size for both routes, although
67
- // effort changes how much a model writes.
68
- // - nextTurnUsd: candidate minus incumbent for the next request, input at the current cache state plus output.
69
- // - laterTurnUsd: the same difference for each later turn, once both caches are warm.
70
- // - paybackTurns: 0 when the switch is cheaper at once, n when later turns repay it after n turns, null when never.
71
- export function shadowEconomics(config, candidateTier, incumbentTier, facts, now) {
72
- const candidate = modelOf(config, candidateTier);
73
- const incumbent = modelOf(config, incumbentTier);
74
- if (candidate.output === undefined || incumbent.output === undefined) return null;
75
- const tokens = nextContextTokens(facts);
76
- const outputTokens = facts.lastRequest?.outputTokens ?? 0;
77
- const outputUsd = ((candidate.output - incumbent.output) * outputTokens) / 1e6;
78
- const nextTurnUsd = switchingTaxUsd(config, candidateTier, incumbentTier, facts, now) + outputUsd;
79
- const laterTurnUsd = ((candidate.cacheRead - incumbent.cacheRead) * tokens) / 1e6 + outputUsd;
80
- let paybackTurns = null;
81
- if (nextTurnUsd <= 0 && laterTurnUsd <= 0) paybackTurns = 0;
82
- else if (nextTurnUsd > 0 && laterTurnUsd < 0) paybackTurns = Math.ceil(nextTurnUsd / -laterTurnUsd);
83
- return { nextTurnUsd, laterTurnUsd, paybackTurns, outputTokens };
84
- }
85
-
86
- // A downgrade's tax over `turns` turns: the next one plus the later ones, output included. A missing or zero output
87
- // price is unknown, not free, so the tax then falls back to the next request's input alone.
88
- export function downgradeTaxUsd(config, candidateTier, incumbentTier, facts, now, turns) {
89
- const priced = [candidateTier, incumbentTier].every((tier) => modelOf(config, tier).output > 0);
90
- if (!priced) return Math.max(0, switchingTaxUsd(config, candidateTier, incumbentTier, facts, now));
91
- const { nextTurnUsd, laterTurnUsd } = shadowEconomics(config, candidateTier, incumbentTier, facts, now);
92
- return Math.max(0, nextTurnUsd + (turns - 1) * laterTurnUsd);
93
- }
package/lib/gateway.mjs DELETED
@@ -1,221 +0,0 @@
1
- // Local Anthropic Messages gateway: byte-for-byte passthrough, except a routed request gets its
2
- // model, effort and thinking rewritten. Never re-serializes responses; reads usage on a tee.
3
- // Upstream errors (429, 529, 5xx) pass through unchanged: Claude Code owns retries and backoff.
4
- import { createServer, request as httpRequest } from 'node:http';
5
- import { request as httpsRequest } from 'node:https';
6
- import { pipeline } from 'node:stream';
7
- import { IdleTracker } from './idle.mjs';
8
- import { adaptBetas } from './rewrite.mjs';
9
- import { isSideRequest } from './router.mjs';
10
- import { UsageReader } from './sse.mjs';
11
- import { ROUTER_DISPLAY_NAME, STATUS_PATH, statusSnapshot } from './status.mjs';
12
-
13
- const HOP_BY_HOP = new Set(['host', 'connection', 'content-length', 'accept-encoding', 'transfer-encoding']);
14
- const LOOPBACK = new Set(['127.0.0.1', 'localhost', '[::1]']);
15
- const MESSAGES_PATH = '/v1/messages';
16
-
17
- // The betas the routed model accepts: rewrite.mjs owns what each model takes.
18
- function withSupportedBetas(headers, modelId, config) {
19
- if (typeof headers['anthropic-beta'] !== 'string') return;
20
- const kept = adaptBetas(headers['anthropic-beta'], modelId, config);
21
- if (kept) headers['anthropic-beta'] = kept;
22
- else delete headers['anthropic-beta'];
23
- }
24
-
25
- export function createGateway({
26
- router,
27
- upstream = 'https://api.anthropic.com',
28
- onError = () => {},
29
- activity = new IdleTracker(),
30
- }) {
31
- const target = new URL(upstream);
32
- const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
33
-
34
- return createServer((req, res) => {
35
- activity.requestStarted();
36
- res.once('close', () => activity.requestEnded());
37
- if (!isLocalClient(req))
38
- return reply(res, 403, errorBody('permission_error', 'router: the gateway serves local clients only'));
39
- if (req.method === 'GET' && req.url.startsWith('/v1/models')) return discovery(router, res);
40
- if (req.method === 'GET' && req.url.startsWith(STATUS_PATH)) return status(router, req, res);
41
- let up = null;
42
- let closedEarly = false;
43
- // The client left before its response was complete (Esc in Claude Code, a client timeout). Before the upstream
44
- // answers, abort the upstream request here; after, pipeline() below does it. Either way the model stops generating.
45
- res.on('close', () => {
46
- if (res.writableFinished) return;
47
- closedEarly = true;
48
- if (!res.headersSent) up?.destroy();
49
- });
50
- const chunks = [];
51
- req.on('data', (c) => chunks.push(c));
52
- req.on('end', async () => {
53
- try {
54
- let body = Buffer.concat(chunks);
55
- const parsed = parseJson(body);
56
- const session = sessionKey(req, parsed);
57
- const turn = isMessagesCall(req);
58
- // Only the conversation itself waits for tool results; a side request on the same session says nothing about it.
59
- // Only the hint header decides here: by body shape a turn with thinking off looks like a side request, and
60
- // keeping a wait too long costs less than exiting under a permission prompt.
61
- const { requestClass } = routingHints(req);
62
- const side = Boolean(requestClass) && isSideRequest(parsed ?? {}, requestClass);
63
- if (turn && !side) activity.turnResumed(session);
64
- let routed = null;
65
- if (parsed && router.isRouted(parsed)) {
66
- if (turn) {
67
- try {
68
- routed = await router.route(parsed, { sessionId: session, ...routingHints(req) });
69
- } catch (error) {
70
- onError(error);
71
- routed = router.fallback(parsed, session, routingHints(req)); // never forward the alias upstream
72
- }
73
- } else routed = router.resolveModel(parsed, session);
74
- body = Buffer.from(JSON.stringify(routed.body));
75
- }
76
- if (closedEarly) return; // the client left while the route was decided
77
- const headers = {};
78
- for (const [name, value] of Object.entries(req.headers)) if (!HOP_BY_HOP.has(name)) headers[name] = value;
79
- headers.host = target.host;
80
- headers['content-length'] = String(body.length);
81
- if (routed) withSupportedBetas(headers, routed.body.model, router.config);
82
- up = send(
83
- { host: target.hostname, port: target.port || undefined, path: req.url, method: req.method, headers },
84
- (upRes) => relay(upRes, res, { routed, session, turn, clientLeft: () => closedEarly }),
85
- );
86
- up.on('error', (error) => {
87
- if (closedEarly) return; // aborted above because the client left
88
- fail(res, new Error(`upstream: ${error.message}`));
89
- });
90
- up.end(body);
91
- } catch (error) {
92
- fail(res, error);
93
- }
94
- });
95
- });
96
-
97
- function relay(upRes, res, { routed, session, turn, clientLeft }) {
98
- const reader = turn ? new UsageReader() : null;
99
- // pipeline() reports ECONNRESET for both kinds of failure; only the side that closed first says whose it was.
100
- let upstreamBroke = false;
101
- upRes.once('close', () => {
102
- upstreamBroke = !upRes.complete && !clientLeft();
103
- });
104
- res.writeHead(upRes.statusCode, upRes.headers);
105
- if (reader) upRes.on('data', (c) => reader.feed(c.toString('utf8')));
106
- // pipeline() tears down both sides on failure. An upstream reset reaches the client as a reset it retries at once,
107
- // where pipe() left it waiting for bytes that never came; a client that leaves stops the upstream response.
108
- pipeline(upRes, res, (error) => {
109
- if (error) {
110
- if (upstreamBroke) onError(new Error(`upstream stream: ${error.message}`));
111
- return;
112
- }
113
- if (!reader) return;
114
- if (upRes.statusCode >= 300) {
115
- if (routed) tryRecord(() => router.recordFailure(session, routed, upRes.statusCode));
116
- return;
117
- }
118
- const usage = reader.end();
119
- if (reader.stopReason === 'tool_use') activity.turnPaused(session);
120
- if (!routed || routed.auxiliary) return;
121
- tryRecord(() => router.recordResponse(session, routed.tier, usage, routed.effort));
122
- });
123
- }
124
-
125
- function tryRecord(record) {
126
- try {
127
- record();
128
- } catch (error) {
129
- onError(error);
130
- }
131
- }
132
-
133
- function fail(res, error) {
134
- onError(error);
135
- if (!res.headersSent) res.writeHead(502);
136
- res.end();
137
- }
138
- }
139
-
140
- // Claude Code sends its session id as a header; the metadata field is the fallback. A subagent's requests carry
141
- // its agent id too: it gets its own routing memory, so its turns do not mix with the main conversation's.
142
- export function sessionKey(req, body) {
143
- const agent = req.headers['x-claude-code-agent-id'];
144
- const session = sessionOf(req, body);
145
- return agent ? `${session}.${agent}` : session;
146
- }
147
-
148
- function sessionOf(req, body) {
149
- const header = req.headers['x-claude-code-session-id'];
150
- if (header) return String(header);
151
- try {
152
- return JSON.parse(body?.metadata?.user_id ?? '{}').session_id ?? 'unknown';
153
- } catch {
154
- return 'unknown';
155
- }
156
- }
157
-
158
- // Gateway hint headers, sent when CLAUDE_CODE_GATEWAY_HINT_HEADERS=1. Without them the router falls back to the body.
159
- function routingHints(req) {
160
- return {
161
- requestClass: req.headers['x-claude-code-request-class'] ?? null,
162
- agentType: req.headers['x-claude-code-agent-type'] ?? null,
163
- contextCompacted: req.headers['x-claude-code-context-compacted'] ?? null,
164
- };
165
- }
166
-
167
- // A web page can reach a loopback port too: by DNS rebinding (a foreign Host) or by a request from a page, which
168
- // always carries an Origin, even one served from another loopback port. Claude Code sends a loopback Host and no Origin.
169
- function isLocalClient(req) {
170
- return LOOPBACK.has(hostOf(req.headers.host)) && req.headers.origin === undefined;
171
- }
172
-
173
- function hostOf(host = '') {
174
- return host.replace(/:\d+$/, '').toLowerCase();
175
- }
176
-
177
- // Only a Messages call is a turn. Side endpoints such as count_tokens carry the alias too, but no decision.
178
- function isMessagesCall(req) {
179
- return req.method === 'POST' && req.url.split('?')[0] === MESSAGES_PATH;
180
- }
181
-
182
- function parseJson(buffer) {
183
- if (!buffer.length) return null;
184
- try {
185
- return JSON.parse(buffer.toString('utf8'));
186
- } catch {
187
- return null;
188
- }
189
- }
190
-
191
- function discovery(router, res) {
192
- const { alias } = router.config.gateway;
193
- reply(res, 200, {
194
- data: [{ id: alias, display_name: ROUTER_DISPLAY_NAME, description: routesDescription(router.config) }],
195
- });
196
- }
197
-
198
- function status(router, req, res) {
199
- const session = new URL(req.url, 'http://localhost').searchParams.get('session');
200
- const pausedUntil = router.jevPausedUntil > router.now() ? new Date(router.jevPausedUntil).toISOString() : null;
201
- reply(res, 200, {
202
- ...statusSnapshot(router.config, session ? router.memory(session) : null),
203
- pid: process.pid,
204
- jevPausedUntil: pausedUntil,
205
- });
206
- }
207
-
208
- function reply(res, statusCode, payload) {
209
- res.writeHead(statusCode, { 'content-type': 'application/json' });
210
- res.end(JSON.stringify(payload));
211
- }
212
-
213
- function errorBody(type, message) {
214
- return { type: 'error', error: { type, message } };
215
- }
216
-
217
- // "Picks claude-opus-5-5 / claude-sonnet-5-5 / claude-haiku-4-5 and the effort for each turn"
218
- export function routesDescription(config) {
219
- const ids = [...new Set(Object.values(config.routes).map((r) => config.models[r.model].id))];
220
- return `Picks ${ids.join(' / ')} and the effort for each turn`;
221
- }
package/lib/idle.mjs DELETED
@@ -1,41 +0,0 @@
1
- // When the gateway may exit on its own. Claude Code holds no connection open between requests, so an idle session
2
- // and a closed one look the same here: the signal is time without requests. Two cases must still keep the gateway:
3
- // a request in flight, and a turn that waits for a tool result (a permission prompt or a question nobody answered
4
- // yet), whose next request comes without a prompt and so without the hook that would start a gateway again.
5
- // A session killed mid-turn stops counting after TURN_WAIT_MS.
6
- export const TURN_WAIT_MS = 24 * 3_600_000;
7
-
8
- export class IdleTracker {
9
- constructor({ now = Date.now } = {}) {
10
- this.now = now;
11
- this.inFlight = 0;
12
- this.lastActive = now();
13
- this.waiting = new Map(); // session -> when its last response asked for a tool
14
- }
15
-
16
- requestStarted() {
17
- this.inFlight += 1;
18
- this.lastActive = this.now();
19
- }
20
-
21
- requestEnded() {
22
- this.inFlight -= 1;
23
- this.lastActive = this.now();
24
- }
25
-
26
- // A Messages request for the session: whatever it waited for has arrived.
27
- turnResumed(session) {
28
- this.waiting.delete(session);
29
- }
30
-
31
- turnPaused(session) {
32
- this.waiting.set(session, this.now());
33
- }
34
-
35
- idle(idleMs) {
36
- const now = this.now();
37
- if (this.inFlight > 0 || now - this.lastActive < idleMs) return false;
38
- for (const [session, at] of this.waiting) if (now - at >= TURN_WAIT_MS) this.waiting.delete(session);
39
- return this.waiting.size === 0;
40
- }
41
- }
package/lib/rewrite.mjs DELETED
@@ -1,130 +0,0 @@
1
- // Turn a request for the router alias into a request for a concrete model. `model`, `max_tokens`,
2
- // `output_config.effort`, `thinking` and thinking edits in `context_management` change. For a model without a
3
- // request feature that Claude Code uses (config FEATURES), the messages and the beta header lose that feature,
4
- // as Claude Code itself does when the model answers 400; system prompt and tools are never touched.
5
- import { EFFORTS } from './config.mjs';
6
-
7
- const TOOL_CHANGE_BLOCKS = new Set(['tool_addition', 'tool_removal']);
8
- const LONG_CONTEXT_BETA = /^context-1m-/;
9
- const LONG_CONTEXT_WINDOW = 1_000_000;
10
- // Each feature is a beta; a model without the feature answers the beta with 400.
11
- const FEATURE_BETAS = {
12
- 'mid-conversation-system': /^mid-conversation-system-/,
13
- 'per-turn-control': /^per-turn-control-/,
14
- 'mid-conversation-tool-changes': /^mid-conversation-tool-changes-/,
15
- };
16
-
17
- export function rewriteRequest(body, tier, config) {
18
- const route = config.routes[tier];
19
- const model = config.models[route.model];
20
- const out = { ...body, model: model.id };
21
- // A route's own effort also goes into per-turn control, which overrides the request's effort on the model.
22
- const routeEffort = route.effort ? clampEffort(route.effort, model.efforts) : null;
23
- if (Array.isArray(body.messages)) out.messages = adaptMessages(body.messages, model.features ?? [], routeEffort);
24
- if (model.maxOutput && out.max_tokens > model.maxOutput) out.max_tokens = model.maxOutput;
25
- if (model.efforts.length === 0) return withoutThinking(out, body.output_config);
26
- const effort = clampEffort(route.effort ?? body.output_config?.effort, model.efforts);
27
- if (effort) out.output_config = { ...(body.output_config ?? {}), effort };
28
- return out;
29
- }
30
-
31
- // The `anthropic-beta` header for the routed model, or null when nothing is left. `jev-router[1m]` makes Claude Code
32
- // send the 1M context beta on every request; a model with a smaller window answers it with 400.
33
- export function adaptBetas(beta, modelId, config) {
34
- const model = Object.values(config.models).find((m) => m.id === modelId);
35
- if (!model) return beta;
36
- const features = model.features ?? [];
37
- const dropped = Object.entries(FEATURE_BETAS)
38
- .filter(([feature]) => !features.includes(feature))
39
- .map(([, pattern]) => pattern);
40
- if (model.contextWindow < LONG_CONTEXT_WINDOW) dropped.push(LONG_CONTEXT_BETA);
41
- const kept = beta.split(',').filter((flag) => !dropped.some((pattern) => pattern.test(flag.trim())));
42
- return kept.length ? kept.join(',') : null;
43
- }
44
-
45
- // Pure and deterministic: the same history adapts the same way on every turn, so the cached prefix holds.
46
- function adaptMessages(messages, features, routeEffort) {
47
- let out = messages;
48
- if (!features.includes('per-turn-control')) out = out.map(withoutTurnControl).filter(Boolean);
49
- else if (routeEffort && out.some((m) => turnEffortOf(m) && turnEffortOf(m) !== routeEffort))
50
- out = out.map((m) => (turnEffortOf(m) ? { ...m, output_config: { ...m.output_config, effort: routeEffort } } : m));
51
- if (!features.includes('mid-conversation-tool-changes')) out = out.map(withoutToolChanges).filter(Boolean);
52
- if (!features.includes('mid-conversation-system')) out = foldSystemMessages(out);
53
- return out;
54
- }
55
-
56
- // Per-turn control is an `output_config` (the turn's effort) on a message, usually the system message after the prompt.
57
- // A message that carried only that is dropped: the API rejects empty content.
58
- function withoutTurnControl(message) {
59
- if (!message || !Object.hasOwn(message, 'output_config')) return message;
60
- const { output_config: _turn, ...rest } = message;
61
- return Array.isArray(rest.content) && rest.content.length === 0 ? null : rest;
62
- }
63
-
64
- function turnEffortOf(message) {
65
- return message?.output_config?.effort ?? null;
66
- }
67
-
68
- // Drops tool_addition and tool_removal blocks. A dropped block's cache breakpoint moves to the block before it, or the
69
- // turn loses its last breakpoint; a message left empty is dropped.
70
- function withoutToolChanges(message) {
71
- if (!Array.isArray(message.content) || !message.content.some((b) => TOOL_CHANGE_BLOCKS.has(b?.type))) return message;
72
- const content = [];
73
- for (const block of message.content) {
74
- if (!TOOL_CHANGE_BLOCKS.has(block?.type)) content.push(block);
75
- else if (block.cache_control && content.length && !content.at(-1).cache_control)
76
- content[content.length - 1] = { ...content.at(-1), cache_control: block.cache_control };
77
- }
78
- return content.length ? { ...message, content } : null;
79
- }
80
-
81
- // A `system` message becomes text in the user message next to it, wrapped as a system reminder: the form Claude Code
82
- // uses for hook output when the model takes no system messages.
83
- function foldSystemMessages(messages) {
84
- const out = [];
85
- let lastFolded = false;
86
- for (const message of messages) {
87
- const folded = message.role === 'system';
88
- const next = folded ? { role: 'user', content: blocks(message.content).map(asReminder) } : message;
89
- const prev = out.at(-1);
90
- if (prev?.role === 'user' && next.role === 'user' && (folded || lastFolded))
91
- out[out.length - 1] = { ...prev, content: [...blocks(prev.content), ...next.content] };
92
- else out.push(next);
93
- lastFolded = folded;
94
- }
95
- return out;
96
- }
97
-
98
- function blocks(content) {
99
- if (typeof content === 'string') return [{ type: 'text', text: content }];
100
- return Array.isArray(content) ? content : [];
101
- }
102
-
103
- function asReminder(block) {
104
- return block?.type === 'text' ? { ...block, text: `<system-reminder>\n${block.text}\n</system-reminder>` } : block;
105
- }
106
-
107
- // Highest supported level at or below the wanted one, as Claude Code does for its own models.
108
- export function clampEffort(wanted, supported) {
109
- if (!wanted || supported.length === 0) return null;
110
- if (supported.includes(wanted)) return wanted;
111
- for (let i = EFFORTS.indexOf(wanted); i >= 0; i -= 1) if (supported.includes(EFFORTS[i])) return EFFORTS[i];
112
- return supported[0];
113
- }
114
-
115
- // A family with no effort control has no adaptive thinking either: omit both and run without thinking.
116
- // The API rejects a clear_thinking edit without thinking, so drop those edits too.
117
- function withoutThinking(out, outputConfig) {
118
- delete out.thinking;
119
- const edits = out.context_management?.edits;
120
- if (edits) {
121
- const kept = edits.filter((e) => !e.type?.startsWith('clear_thinking'));
122
- if (kept.length) out.context_management = { ...out.context_management, edits: kept };
123
- else delete out.context_management;
124
- }
125
- if (!outputConfig) return out;
126
- const { effort: _dropped, ...rest } = outputConfig;
127
- if (Object.keys(rest).length) out.output_config = rest;
128
- else delete out.output_config;
129
- return out;
130
- }