@izkac/forgekit 0.3.11 → 0.3.13
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/bin/forge.mjs +107 -107
- package/bin/forgekit.mjs +83 -83
- package/bin/review.mjs +81 -81
- package/package.json +1 -1
- package/scripts/prepack.mjs +78 -78
- package/scripts/run-tests.mjs +50 -50
- package/src/adr.mjs +236 -236
- package/src/adr.test.mjs +170 -170
- package/src/change.mjs +327 -234
- package/src/change.test.mjs +145 -83
- package/src/cleanup-sessions.mjs +84 -84
- package/src/config.mjs +103 -103
- package/src/defer.mjs +75 -75
- package/src/doctor.mjs +350 -341
- package/src/doctor.test.mjs +114 -114
- package/src/init.mjs +680 -621
- package/src/install.mjs +815 -815
- package/src/install.test.mjs +180 -180
- package/src/integrity-check.mjs +60 -60
- package/src/integrity.mjs +682 -682
- package/src/integrity.test.mjs +566 -566
- package/src/lib.mjs +143 -143
- package/src/models.defaults.json +41 -41
- package/src/new-session.mjs +99 -99
- package/src/openspec-overlays/README.md +19 -19
- package/src/openspec-overlays/openspec-apply-change-footer.md +14 -14
- package/src/openspec-overlays/opsx-apply-completion-step.md +1 -1
- package/src/openspec-overlays/opsx-apply-implement-step.md +11 -11
- package/src/paths.mjs +92 -92
- package/src/plan-engine.mjs +321 -278
- package/src/plan-engine.test.mjs +447 -283
- package/src/preferences.defaults.json +78 -78
- package/src/preferences.mjs +438 -438
- package/src/preferences.test.mjs +174 -174
- package/src/record-evidence.mjs +204 -204
- package/src/resolve-model.mjs +312 -312
- package/src/resolve-model.test.mjs +194 -194
- package/src/review/cli.test.mjs +117 -117
- package/src/review/export.mjs +172 -172
- package/src/review/export.test.mjs +197 -197
- package/src/review/fixtures/valid-review.json +42 -42
- package/src/review/lib.mjs +894 -894
- package/src/review/lib.test.mjs +266 -266
- package/src/review/schema.json +196 -196
- package/src/review/signals.test.mjs +62 -62
- package/src/score-cli.mjs +68 -68
- package/src/score.mjs +568 -566
- package/src/score.test.mjs +366 -340
- package/src/session-reminder.mjs +207 -207
- package/src/session-status.mjs +70 -70
- package/src/set-models.mjs +186 -186
- package/src/set-phase.mjs +205 -205
- package/src/set-prefs.mjs +294 -294
- package/src/specs-sync.mjs +234 -0
- package/src/specs-sync.test.mjs +114 -0
- package/src/spine.mjs +93 -93
- package/src/triage-prompt.mjs +175 -175
- package/src/triage-prompt.test.mjs +50 -50
- package/src/vendor-openspec-overlays.mjs +176 -176
- package/src/vendor-openspec-overlays.test.mjs +62 -62
- package/vendor/skills/archive-to-adr/SKILL.md +149 -149
- package/vendor/skills/forge/SKILL.md +136 -136
- package/vendor/skills/forge/docs/forge.md +650 -647
- package/vendor/skills/forge/phases/brainstorm.md +23 -23
- package/vendor/skills/forge/phases/finish.md +90 -87
- package/vendor/skills/forge/phases/implement.md +77 -77
- package/vendor/skills/forge/phases/plan-openspec.md +60 -60
- package/vendor/skills/forge/phases/plan-specs.md +163 -117
- package/vendor/skills/forge/phases/review.md +25 -25
- package/vendor/skills/forge/phases/verify.md +124 -124
- package/vendor/skills/forge/references/forge-layout.md +85 -85
- package/vendor/skills/forge/references/pace.md +115 -115
- package/vendor/skills/forge/references/plan-routing.md +52 -51
- package/vendor/skills/forge/references/runtime-integrity.md +232 -225
- package/vendor/skills/forge/references/substantial-work.md +37 -37
- package/vendor/skills/forge/references/test-evidence.md +30 -30
- package/vendor/skills/forge/references/test-strategy.md +68 -68
- package/vendor/skills/forge/skills/subagent-driven-development/SKILL.md +87 -87
- package/vendor/skills/forge/subagents/final-reviewer-prompt.md +56 -56
- package/vendor/skills/forge/subagents/implementer-prompt.md +38 -38
- package/vendor/skills/git-resolve-adr-conflict/SKILL.md +132 -132
- package/vendor/skills/thorough-code-review/SKILL.md +290 -290
- package/vendor/skills/thorough-code-review/examples.md +133 -133
- package/vendor/skills/thorough-code-review/reference/accepted-risks.md +26 -26
- package/vendor/skills/thorough-code-review/reference/lenses.md +96 -96
- package/vendor/skills/thorough-code-review/reference/phase1-scout.md +62 -62
- package/vendor/skills/thorough-code-review/reference/phase2-skeptic.md +105 -105
- package/vendor/skills/thorough-code-review/reference/report-schema.json +222 -222
- package/vendor/skills/thorough-code-review/reference/report-template.md +115 -115
- package/vendor/skills/thorough-code-review/reference/severity-rubric.md +49 -49
- package/vendor/skills/thorough-code-review/reference/signals-preflight.md +55 -55
- package/vendor/templates/adr/README.md +7 -7
- package/vendor/templates/adr/decisions.md +141 -141
- package/vendor/templates/adr/hooks/check-pending-adrs.mjs +74 -74
- package/vendor/templates/adr/hooks/check-pending-adrs.sh +3 -3
- package/vendor/templates/adr/hooks/openspec-archive-agent-message.mjs +52 -52
- package/vendor/templates/adr/hooks/openspec-archive-agent-message.sh +3 -3
- package/vendor/templates/project/claude/commands/forge-apply.md +75 -75
- package/vendor/templates/project/claude/commands/forge-brainstorm.md +7 -7
- package/vendor/templates/project/claude/commands/forge-build.md +17 -17
- package/vendor/templates/project/claude/commands/forge-plan.md +12 -12
- package/vendor/templates/project/claude/commands/forge-skip.md +14 -14
- package/vendor/templates/project/claude/commands/forge-status.md +16 -16
- package/vendor/templates/project/claude/commands/forge.md +16 -16
- package/vendor/templates/project/claude/hooks/forge-prompt-hook.mjs +73 -73
- package/vendor/templates/project/claude/hooks/forge-session-start.mjs +19 -19
- package/vendor/templates/project/claude/hooks/forge-triage-hook.mjs +77 -77
- package/vendor/templates/project/cursor/commands/forge-apply.md +75 -75
- package/vendor/templates/project/cursor/commands/forge-brainstorm.md +10 -10
- package/vendor/templates/project/cursor/commands/forge-build.md +17 -17
- package/vendor/templates/project/cursor/commands/forge-plan.md +15 -15
- package/vendor/templates/project/cursor/commands/forge-skip.md +14 -14
- package/vendor/templates/project/cursor/commands/forge-status.md +16 -16
- package/vendor/templates/project/cursor/commands/forge.md +16 -16
- package/vendor/templates/project/cursor/hooks/forge-session-start.mjs +30 -30
- package/vendor/templates/project/cursor/hooks/forge-session-start.sh +3 -3
package/src/resolve-model.mjs
CHANGED
|
@@ -1,312 +1,312 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Resolve Forge subagent model from capability tier × billing lane.
|
|
4
|
-
*
|
|
5
|
-
* Usage:
|
|
6
|
-
* forge resolve-model --tier <fast|standard|capable> [options]
|
|
7
|
-
*
|
|
8
|
-
* Options:
|
|
9
|
-
* --tier <tier> Capability tier (required)
|
|
10
|
-
* --billing <lane> included | metered (default: merged config)
|
|
11
|
-
* --agent <name> cursor | claude-code | codex (default: detect)
|
|
12
|
-
* --defaults <path> Override defaults JSON path
|
|
13
|
-
* --forge-dir <path> Forge root (default: .forge under cwd)
|
|
14
|
-
* --help Show help
|
|
15
|
-
*
|
|
16
|
-
* Stdout: single JSON object { agent, billing, tier, model, omitModel, source }
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import fs from 'node:fs';
|
|
20
|
-
import path from 'node:path';
|
|
21
|
-
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
-
|
|
23
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
24
|
-
|
|
25
|
-
export const TIERS = Object.freeze(['fast', 'standard', 'capable']);
|
|
26
|
-
export const BILLING_LANES = Object.freeze(['included', 'metered']);
|
|
27
|
-
export const AGENTS = Object.freeze(['cursor', 'claude-code', 'codex']);
|
|
28
|
-
|
|
29
|
-
export const DEFAULTS_PATH = path.join(__dirname, 'models.defaults.json');
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @param {string[]} argv
|
|
33
|
-
*/
|
|
34
|
-
export function parseArgs(argv) {
|
|
35
|
-
const opts = {
|
|
36
|
-
tier: null,
|
|
37
|
-
billing: null,
|
|
38
|
-
agent: null,
|
|
39
|
-
defaults: null,
|
|
40
|
-
forgeDir: null,
|
|
41
|
-
help: false,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
45
|
-
const arg = argv[i];
|
|
46
|
-
if (arg === '--tier') opts.tier = argv[++i];
|
|
47
|
-
else if (arg === '--billing') opts.billing = argv[++i];
|
|
48
|
-
else if (arg === '--agent') opts.agent = argv[++i];
|
|
49
|
-
else if (arg === '--defaults') opts.defaults = argv[++i];
|
|
50
|
-
else if (arg === '--forge-dir') opts.forgeDir = argv[++i];
|
|
51
|
-
else if (arg === '--help' || arg === '-h') opts.help = true;
|
|
52
|
-
else if (arg === '--') continue;
|
|
53
|
-
else throw new Error(`Unknown argument: ${arg}`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return opts;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @param {NodeJS.ProcessEnv} [env]
|
|
61
|
-
* @returns {{ agent: string, detected: boolean }}
|
|
62
|
-
*/
|
|
63
|
-
export function detectAgent(env = process.env) {
|
|
64
|
-
if (env.CURSOR_AGENT === '1' || env.CURSOR_TRACE_ID || env.CURSOR_SESSION_ID) {
|
|
65
|
-
return { agent: 'cursor', detected: true };
|
|
66
|
-
}
|
|
67
|
-
if (env.CLAUDE_CODE === '1' || env.CLAUDECODE || env.CLAUDE_PROJECT_DIR) {
|
|
68
|
-
return { agent: 'claude-code', detected: true };
|
|
69
|
-
}
|
|
70
|
-
if (env.CODEX_HOME || env.CODEX_CI || env.CODEX_SANDBOX) {
|
|
71
|
-
return { agent: 'codex', detected: true };
|
|
72
|
-
}
|
|
73
|
-
// Cursor Agent often sets these; fall back when running inside Cursor IDE shell.
|
|
74
|
-
if (env.TERM_PROGRAM === 'vscode' && (env.VSCODE_GIT_IPC_HANDLE || env.VSCODE_PID)) {
|
|
75
|
-
return { agent: 'cursor', detected: true };
|
|
76
|
-
}
|
|
77
|
-
return { agent: 'cursor', detected: false };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @param {unknown} value
|
|
82
|
-
* @returns {value is Record<string, unknown>}
|
|
83
|
-
*/
|
|
84
|
-
function isPlainObject(value) {
|
|
85
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Deep-merge local overlay onto defaults (objects only; arrays/scalars replaced).
|
|
90
|
-
* @param {Record<string, unknown>} base
|
|
91
|
-
* @param {Record<string, unknown>} overlay
|
|
92
|
-
*/
|
|
93
|
-
export function deepMerge(base, overlay) {
|
|
94
|
-
/** @type {Record<string, unknown>} */
|
|
95
|
-
const out = { ...base };
|
|
96
|
-
for (const [key, value] of Object.entries(overlay)) {
|
|
97
|
-
if (isPlainObject(value) && isPlainObject(out[key])) {
|
|
98
|
-
out[key] = deepMerge(
|
|
99
|
-
/** @type {Record<string, unknown>} */ (out[key]),
|
|
100
|
-
/** @type {Record<string, unknown>} */ (value),
|
|
101
|
-
);
|
|
102
|
-
} else {
|
|
103
|
-
out[key] = value;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return out;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @param {string} filePath
|
|
111
|
-
* @returns {Record<string, unknown>}
|
|
112
|
-
*/
|
|
113
|
-
export function loadJsonFile(filePath) {
|
|
114
|
-
if (!fs.existsSync(filePath)) {
|
|
115
|
-
throw new Error(`Missing config file: ${filePath}`);
|
|
116
|
-
}
|
|
117
|
-
let parsed;
|
|
118
|
-
try {
|
|
119
|
-
parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
120
|
-
} catch (err) {
|
|
121
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
122
|
-
throw new Error(`Invalid JSON in ${filePath}: ${msg}`);
|
|
123
|
-
}
|
|
124
|
-
if (!isPlainObject(parsed)) {
|
|
125
|
-
throw new Error(`Config root must be an object: ${filePath}`);
|
|
126
|
-
}
|
|
127
|
-
return parsed;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @param {{ defaultsPath?: string, forgeDir?: string, cwd?: string }} [paths]
|
|
132
|
-
*/
|
|
133
|
-
export function loadMergedConfig(paths = {}) {
|
|
134
|
-
const defaultsPath = paths.defaultsPath ?? DEFAULTS_PATH;
|
|
135
|
-
const cwd = paths.cwd ?? process.cwd();
|
|
136
|
-
const forgeDir = paths.forgeDir ?? path.join(cwd, '.forge');
|
|
137
|
-
const localPath = path.join(forgeDir, 'models.local.json');
|
|
138
|
-
|
|
139
|
-
const defaults = loadJsonFile(defaultsPath);
|
|
140
|
-
/** @type {string[]} */
|
|
141
|
-
const sources = [defaultsPath];
|
|
142
|
-
|
|
143
|
-
let merged = defaults;
|
|
144
|
-
if (fs.existsSync(localPath)) {
|
|
145
|
-
const local = loadJsonFile(localPath);
|
|
146
|
-
merged = deepMerge(defaults, local);
|
|
147
|
-
sources.push(localPath);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return { config: merged, sources, localPath };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* @param {Record<string, unknown>} config
|
|
155
|
-
* @param {{ agent: string, billing: string, tier: string }} sel
|
|
156
|
-
*/
|
|
157
|
-
export function resolveFromConfig(config, sel) {
|
|
158
|
-
const { agent, billing, tier } = sel;
|
|
159
|
-
if (!AGENTS.includes(agent)) {
|
|
160
|
-
throw new Error(`Unknown agent "${agent}". Expected one of: ${AGENTS.join(', ')}`);
|
|
161
|
-
}
|
|
162
|
-
if (!BILLING_LANES.includes(billing)) {
|
|
163
|
-
throw new Error(`Unknown billing "${billing}". Expected one of: ${BILLING_LANES.join(', ')}`);
|
|
164
|
-
}
|
|
165
|
-
if (!TIERS.includes(tier)) {
|
|
166
|
-
throw new Error(`Unknown tier "${tier}". Expected one of: ${TIERS.join(', ')}`);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const agents = config.agents;
|
|
170
|
-
if (!isPlainObject(agents)) {
|
|
171
|
-
throw new Error('Config missing agents object');
|
|
172
|
-
}
|
|
173
|
-
const agentMap = agents[agent];
|
|
174
|
-
if (!isPlainObject(agentMap)) {
|
|
175
|
-
throw new Error(`Missing agent map for "${agent}"`);
|
|
176
|
-
}
|
|
177
|
-
const laneMap = agentMap[billing];
|
|
178
|
-
if (!isPlainObject(laneMap)) {
|
|
179
|
-
throw new Error(`Missing billing map for (${agent}, ${billing})`);
|
|
180
|
-
}
|
|
181
|
-
const cell = laneMap[tier];
|
|
182
|
-
if (cell === undefined || cell === null || cell === '') {
|
|
183
|
-
throw new Error(`Missing cell for (${agent}, ${billing}, ${tier})`);
|
|
184
|
-
}
|
|
185
|
-
if (typeof cell !== 'string') {
|
|
186
|
-
throw new Error(`Cell for (${agent}, ${billing}, ${tier}) must be a string`);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const inherit = cell === 'inherit';
|
|
190
|
-
return {
|
|
191
|
-
agent,
|
|
192
|
-
billing,
|
|
193
|
-
tier,
|
|
194
|
-
model: inherit ? null : cell,
|
|
195
|
-
omitModel: inherit,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* @param {{
|
|
201
|
-
* tier: string,
|
|
202
|
-
* billing?: string | null,
|
|
203
|
-
* agent?: string | null,
|
|
204
|
-
* defaultsPath?: string,
|
|
205
|
-
* forgeDir?: string,
|
|
206
|
-
* cwd?: string,
|
|
207
|
-
* env?: NodeJS.ProcessEnv,
|
|
208
|
-
* }} opts
|
|
209
|
-
*/
|
|
210
|
-
export function resolveModel(opts) {
|
|
211
|
-
if (!opts.tier) {
|
|
212
|
-
throw new Error('--tier is required (fast | standard | capable)');
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const { config, sources } = loadMergedConfig({
|
|
216
|
-
defaultsPath: opts.defaultsPath,
|
|
217
|
-
forgeDir: opts.forgeDir,
|
|
218
|
-
cwd: opts.cwd,
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
let agent = opts.agent ?? null;
|
|
222
|
-
let agentDetected = Boolean(agent);
|
|
223
|
-
if (!agent) {
|
|
224
|
-
const detected = detectAgent(opts.env ?? process.env);
|
|
225
|
-
agent = detected.agent;
|
|
226
|
-
agentDetected = detected.detected;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const defaultBilling =
|
|
230
|
-
typeof config.billing === 'string' && BILLING_LANES.includes(config.billing)
|
|
231
|
-
? config.billing
|
|
232
|
-
: 'included';
|
|
233
|
-
const billing = opts.billing ?? defaultBilling;
|
|
234
|
-
|
|
235
|
-
const resolved = resolveFromConfig(config, {
|
|
236
|
-
agent,
|
|
237
|
-
billing,
|
|
238
|
-
tier: opts.tier,
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
...resolved,
|
|
243
|
-
source: sources,
|
|
244
|
-
agentDetected,
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function printHelp() {
|
|
249
|
-
process.stdout.write(`Usage: forge resolve-model --tier <fast|standard|capable> [options]
|
|
250
|
-
|
|
251
|
-
Resolve a subagent model for Forge (capability × billing).
|
|
252
|
-
|
|
253
|
-
Options:
|
|
254
|
-
--tier <tier> fast | standard | capable (required)
|
|
255
|
-
--billing <lane> included | metered (default: from config, else included)
|
|
256
|
-
--agent <name> cursor | claude-code | codex (default: detect)
|
|
257
|
-
--defaults <path> Path to defaults JSON
|
|
258
|
-
--forge-dir <path> Forge directory containing models.local.json
|
|
259
|
-
--help Show help
|
|
260
|
-
|
|
261
|
-
Stdout: JSON { agent, billing, tier, model, omitModel, source, agentDetected }
|
|
262
|
-
`);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* @param {string[]} argv
|
|
267
|
-
* @param {{ cwd?: string, env?: NodeJS.ProcessEnv, stdout?: NodeJS.WritableStream, stderr?: NodeJS.WritableStream }} [io]
|
|
268
|
-
*/
|
|
269
|
-
export function runResolveModel(argv, io = {}) {
|
|
270
|
-
const stdout = io.stdout ?? process.stdout;
|
|
271
|
-
const stderr = io.stderr ?? process.stderr;
|
|
272
|
-
|
|
273
|
-
let opts;
|
|
274
|
-
try {
|
|
275
|
-
opts = parseArgs(argv);
|
|
276
|
-
} catch (err) {
|
|
277
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
278
|
-
stderr.write(`${msg}\n`);
|
|
279
|
-
return 2;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (opts.help) {
|
|
283
|
-
printHelp();
|
|
284
|
-
return 0;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
try {
|
|
288
|
-
const result = resolveModel({
|
|
289
|
-
tier: opts.tier,
|
|
290
|
-
billing: opts.billing,
|
|
291
|
-
agent: opts.agent,
|
|
292
|
-
defaultsPath: opts.defaults ?? undefined,
|
|
293
|
-
forgeDir: opts.forgeDir ?? undefined,
|
|
294
|
-
cwd: io.cwd,
|
|
295
|
-
env: io.env,
|
|
296
|
-
});
|
|
297
|
-
stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
298
|
-
return 0;
|
|
299
|
-
} catch (err) {
|
|
300
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
301
|
-
stderr.write(`${msg}\n`);
|
|
302
|
-
return 1;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const isMain =
|
|
307
|
-
process.argv[1] &&
|
|
308
|
-
import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href;
|
|
309
|
-
|
|
310
|
-
if (isMain) {
|
|
311
|
-
process.exitCode = runResolveModel(process.argv.slice(2));
|
|
312
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Resolve Forge subagent model from capability tier × billing lane.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* forge resolve-model --tier <fast|standard|capable> [options]
|
|
7
|
+
*
|
|
8
|
+
* Options:
|
|
9
|
+
* --tier <tier> Capability tier (required)
|
|
10
|
+
* --billing <lane> included | metered (default: merged config)
|
|
11
|
+
* --agent <name> cursor | claude-code | codex (default: detect)
|
|
12
|
+
* --defaults <path> Override defaults JSON path
|
|
13
|
+
* --forge-dir <path> Forge root (default: .forge under cwd)
|
|
14
|
+
* --help Show help
|
|
15
|
+
*
|
|
16
|
+
* Stdout: single JSON object { agent, billing, tier, model, omitModel, source }
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
+
|
|
23
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
24
|
+
|
|
25
|
+
export const TIERS = Object.freeze(['fast', 'standard', 'capable']);
|
|
26
|
+
export const BILLING_LANES = Object.freeze(['included', 'metered']);
|
|
27
|
+
export const AGENTS = Object.freeze(['cursor', 'claude-code', 'codex']);
|
|
28
|
+
|
|
29
|
+
export const DEFAULTS_PATH = path.join(__dirname, 'models.defaults.json');
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string[]} argv
|
|
33
|
+
*/
|
|
34
|
+
export function parseArgs(argv) {
|
|
35
|
+
const opts = {
|
|
36
|
+
tier: null,
|
|
37
|
+
billing: null,
|
|
38
|
+
agent: null,
|
|
39
|
+
defaults: null,
|
|
40
|
+
forgeDir: null,
|
|
41
|
+
help: false,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
45
|
+
const arg = argv[i];
|
|
46
|
+
if (arg === '--tier') opts.tier = argv[++i];
|
|
47
|
+
else if (arg === '--billing') opts.billing = argv[++i];
|
|
48
|
+
else if (arg === '--agent') opts.agent = argv[++i];
|
|
49
|
+
else if (arg === '--defaults') opts.defaults = argv[++i];
|
|
50
|
+
else if (arg === '--forge-dir') opts.forgeDir = argv[++i];
|
|
51
|
+
else if (arg === '--help' || arg === '-h') opts.help = true;
|
|
52
|
+
else if (arg === '--') continue;
|
|
53
|
+
else throw new Error(`Unknown argument: ${arg}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return opts;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {NodeJS.ProcessEnv} [env]
|
|
61
|
+
* @returns {{ agent: string, detected: boolean }}
|
|
62
|
+
*/
|
|
63
|
+
export function detectAgent(env = process.env) {
|
|
64
|
+
if (env.CURSOR_AGENT === '1' || env.CURSOR_TRACE_ID || env.CURSOR_SESSION_ID) {
|
|
65
|
+
return { agent: 'cursor', detected: true };
|
|
66
|
+
}
|
|
67
|
+
if (env.CLAUDE_CODE === '1' || env.CLAUDECODE || env.CLAUDE_PROJECT_DIR) {
|
|
68
|
+
return { agent: 'claude-code', detected: true };
|
|
69
|
+
}
|
|
70
|
+
if (env.CODEX_HOME || env.CODEX_CI || env.CODEX_SANDBOX) {
|
|
71
|
+
return { agent: 'codex', detected: true };
|
|
72
|
+
}
|
|
73
|
+
// Cursor Agent often sets these; fall back when running inside Cursor IDE shell.
|
|
74
|
+
if (env.TERM_PROGRAM === 'vscode' && (env.VSCODE_GIT_IPC_HANDLE || env.VSCODE_PID)) {
|
|
75
|
+
return { agent: 'cursor', detected: true };
|
|
76
|
+
}
|
|
77
|
+
return { agent: 'cursor', detected: false };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {unknown} value
|
|
82
|
+
* @returns {value is Record<string, unknown>}
|
|
83
|
+
*/
|
|
84
|
+
function isPlainObject(value) {
|
|
85
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Deep-merge local overlay onto defaults (objects only; arrays/scalars replaced).
|
|
90
|
+
* @param {Record<string, unknown>} base
|
|
91
|
+
* @param {Record<string, unknown>} overlay
|
|
92
|
+
*/
|
|
93
|
+
export function deepMerge(base, overlay) {
|
|
94
|
+
/** @type {Record<string, unknown>} */
|
|
95
|
+
const out = { ...base };
|
|
96
|
+
for (const [key, value] of Object.entries(overlay)) {
|
|
97
|
+
if (isPlainObject(value) && isPlainObject(out[key])) {
|
|
98
|
+
out[key] = deepMerge(
|
|
99
|
+
/** @type {Record<string, unknown>} */ (out[key]),
|
|
100
|
+
/** @type {Record<string, unknown>} */ (value),
|
|
101
|
+
);
|
|
102
|
+
} else {
|
|
103
|
+
out[key] = value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @param {string} filePath
|
|
111
|
+
* @returns {Record<string, unknown>}
|
|
112
|
+
*/
|
|
113
|
+
export function loadJsonFile(filePath) {
|
|
114
|
+
if (!fs.existsSync(filePath)) {
|
|
115
|
+
throw new Error(`Missing config file: ${filePath}`);
|
|
116
|
+
}
|
|
117
|
+
let parsed;
|
|
118
|
+
try {
|
|
119
|
+
parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
120
|
+
} catch (err) {
|
|
121
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
122
|
+
throw new Error(`Invalid JSON in ${filePath}: ${msg}`);
|
|
123
|
+
}
|
|
124
|
+
if (!isPlainObject(parsed)) {
|
|
125
|
+
throw new Error(`Config root must be an object: ${filePath}`);
|
|
126
|
+
}
|
|
127
|
+
return parsed;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {{ defaultsPath?: string, forgeDir?: string, cwd?: string }} [paths]
|
|
132
|
+
*/
|
|
133
|
+
export function loadMergedConfig(paths = {}) {
|
|
134
|
+
const defaultsPath = paths.defaultsPath ?? DEFAULTS_PATH;
|
|
135
|
+
const cwd = paths.cwd ?? process.cwd();
|
|
136
|
+
const forgeDir = paths.forgeDir ?? path.join(cwd, '.forge');
|
|
137
|
+
const localPath = path.join(forgeDir, 'models.local.json');
|
|
138
|
+
|
|
139
|
+
const defaults = loadJsonFile(defaultsPath);
|
|
140
|
+
/** @type {string[]} */
|
|
141
|
+
const sources = [defaultsPath];
|
|
142
|
+
|
|
143
|
+
let merged = defaults;
|
|
144
|
+
if (fs.existsSync(localPath)) {
|
|
145
|
+
const local = loadJsonFile(localPath);
|
|
146
|
+
merged = deepMerge(defaults, local);
|
|
147
|
+
sources.push(localPath);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { config: merged, sources, localPath };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {Record<string, unknown>} config
|
|
155
|
+
* @param {{ agent: string, billing: string, tier: string }} sel
|
|
156
|
+
*/
|
|
157
|
+
export function resolveFromConfig(config, sel) {
|
|
158
|
+
const { agent, billing, tier } = sel;
|
|
159
|
+
if (!AGENTS.includes(agent)) {
|
|
160
|
+
throw new Error(`Unknown agent "${agent}". Expected one of: ${AGENTS.join(', ')}`);
|
|
161
|
+
}
|
|
162
|
+
if (!BILLING_LANES.includes(billing)) {
|
|
163
|
+
throw new Error(`Unknown billing "${billing}". Expected one of: ${BILLING_LANES.join(', ')}`);
|
|
164
|
+
}
|
|
165
|
+
if (!TIERS.includes(tier)) {
|
|
166
|
+
throw new Error(`Unknown tier "${tier}". Expected one of: ${TIERS.join(', ')}`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const agents = config.agents;
|
|
170
|
+
if (!isPlainObject(agents)) {
|
|
171
|
+
throw new Error('Config missing agents object');
|
|
172
|
+
}
|
|
173
|
+
const agentMap = agents[agent];
|
|
174
|
+
if (!isPlainObject(agentMap)) {
|
|
175
|
+
throw new Error(`Missing agent map for "${agent}"`);
|
|
176
|
+
}
|
|
177
|
+
const laneMap = agentMap[billing];
|
|
178
|
+
if (!isPlainObject(laneMap)) {
|
|
179
|
+
throw new Error(`Missing billing map for (${agent}, ${billing})`);
|
|
180
|
+
}
|
|
181
|
+
const cell = laneMap[tier];
|
|
182
|
+
if (cell === undefined || cell === null || cell === '') {
|
|
183
|
+
throw new Error(`Missing cell for (${agent}, ${billing}, ${tier})`);
|
|
184
|
+
}
|
|
185
|
+
if (typeof cell !== 'string') {
|
|
186
|
+
throw new Error(`Cell for (${agent}, ${billing}, ${tier}) must be a string`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const inherit = cell === 'inherit';
|
|
190
|
+
return {
|
|
191
|
+
agent,
|
|
192
|
+
billing,
|
|
193
|
+
tier,
|
|
194
|
+
model: inherit ? null : cell,
|
|
195
|
+
omitModel: inherit,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* @param {{
|
|
201
|
+
* tier: string,
|
|
202
|
+
* billing?: string | null,
|
|
203
|
+
* agent?: string | null,
|
|
204
|
+
* defaultsPath?: string,
|
|
205
|
+
* forgeDir?: string,
|
|
206
|
+
* cwd?: string,
|
|
207
|
+
* env?: NodeJS.ProcessEnv,
|
|
208
|
+
* }} opts
|
|
209
|
+
*/
|
|
210
|
+
export function resolveModel(opts) {
|
|
211
|
+
if (!opts.tier) {
|
|
212
|
+
throw new Error('--tier is required (fast | standard | capable)');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const { config, sources } = loadMergedConfig({
|
|
216
|
+
defaultsPath: opts.defaultsPath,
|
|
217
|
+
forgeDir: opts.forgeDir,
|
|
218
|
+
cwd: opts.cwd,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
let agent = opts.agent ?? null;
|
|
222
|
+
let agentDetected = Boolean(agent);
|
|
223
|
+
if (!agent) {
|
|
224
|
+
const detected = detectAgent(opts.env ?? process.env);
|
|
225
|
+
agent = detected.agent;
|
|
226
|
+
agentDetected = detected.detected;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const defaultBilling =
|
|
230
|
+
typeof config.billing === 'string' && BILLING_LANES.includes(config.billing)
|
|
231
|
+
? config.billing
|
|
232
|
+
: 'included';
|
|
233
|
+
const billing = opts.billing ?? defaultBilling;
|
|
234
|
+
|
|
235
|
+
const resolved = resolveFromConfig(config, {
|
|
236
|
+
agent,
|
|
237
|
+
billing,
|
|
238
|
+
tier: opts.tier,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
...resolved,
|
|
243
|
+
source: sources,
|
|
244
|
+
agentDetected,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function printHelp() {
|
|
249
|
+
process.stdout.write(`Usage: forge resolve-model --tier <fast|standard|capable> [options]
|
|
250
|
+
|
|
251
|
+
Resolve a subagent model for Forge (capability × billing).
|
|
252
|
+
|
|
253
|
+
Options:
|
|
254
|
+
--tier <tier> fast | standard | capable (required)
|
|
255
|
+
--billing <lane> included | metered (default: from config, else included)
|
|
256
|
+
--agent <name> cursor | claude-code | codex (default: detect)
|
|
257
|
+
--defaults <path> Path to defaults JSON
|
|
258
|
+
--forge-dir <path> Forge directory containing models.local.json
|
|
259
|
+
--help Show help
|
|
260
|
+
|
|
261
|
+
Stdout: JSON { agent, billing, tier, model, omitModel, source, agentDetected }
|
|
262
|
+
`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @param {string[]} argv
|
|
267
|
+
* @param {{ cwd?: string, env?: NodeJS.ProcessEnv, stdout?: NodeJS.WritableStream, stderr?: NodeJS.WritableStream }} [io]
|
|
268
|
+
*/
|
|
269
|
+
export function runResolveModel(argv, io = {}) {
|
|
270
|
+
const stdout = io.stdout ?? process.stdout;
|
|
271
|
+
const stderr = io.stderr ?? process.stderr;
|
|
272
|
+
|
|
273
|
+
let opts;
|
|
274
|
+
try {
|
|
275
|
+
opts = parseArgs(argv);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
278
|
+
stderr.write(`${msg}\n`);
|
|
279
|
+
return 2;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (opts.help) {
|
|
283
|
+
printHelp();
|
|
284
|
+
return 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
const result = resolveModel({
|
|
289
|
+
tier: opts.tier,
|
|
290
|
+
billing: opts.billing,
|
|
291
|
+
agent: opts.agent,
|
|
292
|
+
defaultsPath: opts.defaults ?? undefined,
|
|
293
|
+
forgeDir: opts.forgeDir ?? undefined,
|
|
294
|
+
cwd: io.cwd,
|
|
295
|
+
env: io.env,
|
|
296
|
+
});
|
|
297
|
+
stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
298
|
+
return 0;
|
|
299
|
+
} catch (err) {
|
|
300
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
301
|
+
stderr.write(`${msg}\n`);
|
|
302
|
+
return 1;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const isMain =
|
|
307
|
+
process.argv[1] &&
|
|
308
|
+
import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href;
|
|
309
|
+
|
|
310
|
+
if (isMain) {
|
|
311
|
+
process.exitCode = runResolveModel(process.argv.slice(2));
|
|
312
|
+
}
|