pantheon-opencode 1.6.2-beta.3 → 1.6.2-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +38 -1
- package/bin/pantheon-init.mjs +17 -0
- package/package.json +9 -3
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router-budget.mjs +781 -0
- package/scripts/decision-router.mjs +297 -0
- package/scripts/install/decision-router-config.mjs +432 -0
- package/scripts/install/interactive.mjs +76 -5
- package/scripts/install/opencode.mjs +12 -0
- package/scripts/install/state.mjs +5 -0
- package/scripts/install/strings.mjs +4 -0
- package/src/agents/zeus.md +12 -0
- package/src/pantheon/local-decision-agents.ts +170 -0
- package/src/pantheon/local-decision-budget.ts +1043 -0
- package/src/pantheon/local-decision-egress-gate.ts +264 -0
- package/src/pantheon/local-decision-prompt.ts +123 -0
- package/src/pantheon/local-decision-provider-matrix.ts +92 -0
- package/src/pantheon/local-decision-provider-protocol.ts +335 -0
- package/src/pantheon/local-decision-provider.internal.ts +260 -0
- package/src/pantheon/local-decision-router.ts +225 -0
- package/src/pantheon/local-decision-skills.ts +171 -0
- package/src/pantheon/local-decision-tool.ts +126 -0
- package/src/pantheon/local-decision-turn.ts +338 -0
- package/src/pantheon/local-decision-usage-evidence.internal.ts +207 -0
- package/src/pantheon/local-orchestration.ts +322 -0
- package/src/pantheon/pantheon-decision-catalog.ts +101 -0
- package/src/pantheon/v2-tools.ts +15 -14
- package/src/plugin-v2.ts +52 -19
- package/src/plugin.ts +4 -0
- package/src/plugins/tui/dist/tui-v2.js +3 -3
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
4
|
+
// Prebuilt bundle: Node refuses to strip TypeScript types under node_modules,
|
|
5
|
+
// so the published CLI cannot import the ledger source directly. Regenerate
|
|
6
|
+
// with `npm run build:decision-router`; `--check` guards against drift.
|
|
7
|
+
import {
|
|
8
|
+
openLocalDecisionBudgetLedger,
|
|
9
|
+
resolveLocalDecisionBudgetDbPath,
|
|
10
|
+
} from './decision-router-budget.mjs'
|
|
11
|
+
import {
|
|
12
|
+
DECISION_PROVIDER_BUDGET_MAPPING,
|
|
13
|
+
DECISION_ROUTER_MODELS,
|
|
14
|
+
readUserDecisionRouterConfig,
|
|
15
|
+
resolveUserDecisionRouterConfigPath,
|
|
16
|
+
updateUserDecisionRouterConfig,
|
|
17
|
+
writeUserDecisionRouterConfig,
|
|
18
|
+
} from './install/decision-router-config.mjs'
|
|
19
|
+
|
|
20
|
+
const BUDGET_PROVIDERS = DECISION_PROVIDER_BUDGET_MAPPING
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Malformed command line (wrong flags, unknown provider, invalid value).
|
|
24
|
+
* Callers report it as a short message; a stack implies a bug, not bad input.
|
|
25
|
+
*/
|
|
26
|
+
export class DecisionRouterUsageError extends TypeError {
|
|
27
|
+
name = 'DecisionRouterUsageError'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Run an explicit, non-secret administrative command for the user-level router config. */
|
|
31
|
+
export async function runDecisionRouterCommand(
|
|
32
|
+
args,
|
|
33
|
+
{ env = process.env, stdout = console.log } = {},
|
|
34
|
+
) {
|
|
35
|
+
const [command, ...rest] = args
|
|
36
|
+
if (command === 'status' && rest.length === 0) {
|
|
37
|
+
const config = readUserDecisionRouterConfig({ env })
|
|
38
|
+
const ledger = openLedger(env)
|
|
39
|
+
try {
|
|
40
|
+
stdout(`Config: ${resolveUserDecisionRouterConfigPath(env)}`)
|
|
41
|
+
stdout(`Enabled: ${config.enabled ? 'yes' : 'no'}`)
|
|
42
|
+
stdout(`Selection: ${formatSelection(config.selection)}`)
|
|
43
|
+
stdout(`Egress consent: ${config.egress_consent ? 'yes' : 'no'}`)
|
|
44
|
+
stdout(
|
|
45
|
+
`Enabled candidates: ${config.enabled_candidates.length ? config.enabled_candidates.map(formatCandidate).join(', ') : 'none'}`,
|
|
46
|
+
)
|
|
47
|
+
stdout(`Active budget period: ${ledger.getActiveBudgetPeriod() ?? 'unconfirmed'}`)
|
|
48
|
+
} finally {
|
|
49
|
+
ledger.close()
|
|
50
|
+
}
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (command === 'select') {
|
|
55
|
+
const target = rest[0]
|
|
56
|
+
if (rest.length !== 1 || typeof target !== 'string')
|
|
57
|
+
throw new DecisionRouterUsageError('Usage: decision-router select local|auto|provider/model')
|
|
58
|
+
let selection
|
|
59
|
+
if (target === 'local' || target === 'auto') {
|
|
60
|
+
selection = { mode: target }
|
|
61
|
+
} else {
|
|
62
|
+
const [provider, model, ...extra] = target.split('/')
|
|
63
|
+
if (extra.length || !isKnownCandidate(provider, model)) {
|
|
64
|
+
throw new DecisionRouterUsageError('Selection must name one supported provider/model pair')
|
|
65
|
+
}
|
|
66
|
+
selection = { mode: 'explicit', provider, model }
|
|
67
|
+
}
|
|
68
|
+
saveConfig(env, { selection })
|
|
69
|
+
stdout(`Selection saved: ${formatSelection(selection)}. Consent and enablement are unchanged.`)
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (command === 'enable' || command === 'disable') {
|
|
74
|
+
const [provider, model, ...extra] = rest
|
|
75
|
+
if (extra.length) {
|
|
76
|
+
throw new DecisionRouterUsageError(`Usage: decision-router ${command} [<provider> <model>]`)
|
|
77
|
+
}
|
|
78
|
+
if (provider === undefined && model === undefined) {
|
|
79
|
+
saveConfig(env, { enabled: command === 'enable' })
|
|
80
|
+
stdout(
|
|
81
|
+
`Turn selection ${command === 'enable' ? 'enabled' : 'disabled'}. Provider consent and candidate enablement are unchanged.`,
|
|
82
|
+
)
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
if (provider === undefined || !isKnownCandidate(provider, model)) {
|
|
86
|
+
throw new DecisionRouterUsageError(`Usage: decision-router ${command} <provider> <model>`)
|
|
87
|
+
}
|
|
88
|
+
const config = readUserDecisionRouterConfig({ env })
|
|
89
|
+
const key = candidateKey(provider, model)
|
|
90
|
+
const candidates = config.enabled_candidates.filter(
|
|
91
|
+
(candidate) => candidateKey(candidate.provider, candidate.model) !== key,
|
|
92
|
+
)
|
|
93
|
+
if (command === 'enable') candidates.push({ provider, model })
|
|
94
|
+
saveConfig(env, { enabled_candidates: candidates })
|
|
95
|
+
stdout(`${command === 'enable' ? 'Enabled' : 'Disabled'} ${provider}/${model}.`)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (command === 'consent') {
|
|
100
|
+
const value = rest[0]
|
|
101
|
+
if (rest.length !== 1 || (value !== 'yes' && value !== 'no')) {
|
|
102
|
+
throw new DecisionRouterUsageError('Usage: decision-router consent yes|no')
|
|
103
|
+
}
|
|
104
|
+
saveConfig(env, { egress_consent: value === 'yes' })
|
|
105
|
+
stdout(`Egress consent saved: ${value}.`)
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (command === 'caps') {
|
|
110
|
+
const [scope, ...scopeArgs] = rest
|
|
111
|
+
const { monthly, perDecision, rest: targetArgs } = parseCaps(scopeArgs)
|
|
112
|
+
const ledger = openLedger(env)
|
|
113
|
+
try {
|
|
114
|
+
if (scope === 'user' && targetArgs.length === 0) {
|
|
115
|
+
ledger.configureUserCaps({ monthlyCapMicros: monthly, perDecisionCapMicros: perDecision })
|
|
116
|
+
} else if (scope === 'provider' && targetArgs.length === 1) {
|
|
117
|
+
const budgetProvider = Object.hasOwn(BUDGET_PROVIDERS, targetArgs[0])
|
|
118
|
+
? BUDGET_PROVIDERS[targetArgs[0]]
|
|
119
|
+
: undefined
|
|
120
|
+
if (!budgetProvider) throw new DecisionRouterUsageError('Unsupported budget provider')
|
|
121
|
+
ledger.configureProviderCaps(budgetProvider, {
|
|
122
|
+
monthlyCapMicros: monthly,
|
|
123
|
+
perDecisionCapMicros: perDecision,
|
|
124
|
+
})
|
|
125
|
+
} else if (scope === 'model' && targetArgs.length === 2) {
|
|
126
|
+
const [provider, model] = targetArgs
|
|
127
|
+
if (!isKnownCandidate(provider, model))
|
|
128
|
+
throw new DecisionRouterUsageError('Unsupported provider/model pair')
|
|
129
|
+
ledger.configureModelCaps(BUDGET_PROVIDERS[provider], model, {
|
|
130
|
+
monthlyCapMicros: monthly,
|
|
131
|
+
perDecisionCapMicros: perDecision,
|
|
132
|
+
})
|
|
133
|
+
} else {
|
|
134
|
+
throw new DecisionRouterUsageError(
|
|
135
|
+
'Usage: decision-router caps user|provider <provider>|model <provider> <model> --monthly-usd N --per-decision-usd N',
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
} finally {
|
|
139
|
+
ledger.close()
|
|
140
|
+
}
|
|
141
|
+
stdout(
|
|
142
|
+
`Configured ${scope} admission caps. Caps limit local admission; they are not invoice guarantees.`,
|
|
143
|
+
)
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (command === 'holds' && rest.length === 0) {
|
|
148
|
+
if (!existsSync(resolveLocalDecisionBudgetDbPath(env))) {
|
|
149
|
+
stdout('No unresolved provider holds.')
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
const ledger = openLedger(env)
|
|
153
|
+
try {
|
|
154
|
+
const holds = ledger.listUncertainReservations()
|
|
155
|
+
if (holds.length === 0) {
|
|
156
|
+
stdout('No unresolved provider holds.')
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
for (const hold of holds) {
|
|
160
|
+
stdout(
|
|
161
|
+
`Hold ${hold.reservationId}: status=${hold.state} provider=${hold.provider} model=${hold.model} period=${hold.period} reserved_micros=${hold.reservedMicros}`,
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
stdout(
|
|
165
|
+
'This listing does not reserve, reconcile, release, or prune holds. ' +
|
|
166
|
+
'Opening an existing ledger may apply a forward-only schema migration. ' +
|
|
167
|
+
'Unresolved holds are never auto-released; reconcile or resolve them ' +
|
|
168
|
+
'explicitly before pruning their period.',
|
|
169
|
+
)
|
|
170
|
+
} finally {
|
|
171
|
+
ledger.close()
|
|
172
|
+
}
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (command === 'period') {
|
|
177
|
+
const [action, ...periodArgs] = rest
|
|
178
|
+
if (action !== 'confirm')
|
|
179
|
+
throw new DecisionRouterUsageError(
|
|
180
|
+
'Usage: decision-router period confirm --current none|YYYY-MM --next YYYY-MM',
|
|
181
|
+
)
|
|
182
|
+
const options = parseNamedOptions(periodArgs, ['--current', '--next'])
|
|
183
|
+
const expected = options['--current'] === 'none' ? null : options['--current']
|
|
184
|
+
const target = options['--next']
|
|
185
|
+
if ((expected !== null && !isMonth(expected)) || !isMonth(target)) {
|
|
186
|
+
throw new DecisionRouterUsageError(
|
|
187
|
+
'Budget periods must use YYYY-MM; initial current period is `none`',
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
const ledger = openLedger(env)
|
|
191
|
+
try {
|
|
192
|
+
const result = ledger.confirmBudgetPeriod(expected, target)
|
|
193
|
+
if (result.status !== 'confirmed')
|
|
194
|
+
throw new Error(`Budget period confirmation denied: ${result.reason}`)
|
|
195
|
+
stdout(`Explicit period confirmed: ${result.activePeriod}. Automatic rollover is disabled.`)
|
|
196
|
+
} finally {
|
|
197
|
+
ledger.close()
|
|
198
|
+
}
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
throw new DecisionRouterUsageError(
|
|
203
|
+
'Usage: decision-router status|select|enable|disable|consent|caps|period|holds',
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function saveConfig(env, update) {
|
|
208
|
+
const current = readUserDecisionRouterConfig({ env })
|
|
209
|
+
const next = updateUserDecisionRouterConfig(current, update)
|
|
210
|
+
writeUserDecisionRouterConfig(next, { env })
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function openLedger(env) {
|
|
214
|
+
return openLocalDecisionBudgetLedger({ dbPath: resolveLocalDecisionBudgetDbPath(env) })
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function parseCaps(args) {
|
|
218
|
+
const firstFlag = args.findIndex((arg) => arg.startsWith('--'))
|
|
219
|
+
const targetArgs = firstFlag === -1 ? args : args.slice(0, firstFlag)
|
|
220
|
+
const optionArgs = firstFlag === -1 ? [] : args.slice(firstFlag)
|
|
221
|
+
const missing = ['--monthly-usd', '--per-decision-usd'].filter(
|
|
222
|
+
(flag) => !optionArgs.includes(flag),
|
|
223
|
+
)
|
|
224
|
+
if (missing.length > 0) {
|
|
225
|
+
throw new DecisionRouterUsageError('Both --monthly-usd and --per-decision-usd are required')
|
|
226
|
+
}
|
|
227
|
+
const flags = parseNamedOptions(optionArgs, ['--monthly-usd', '--per-decision-usd'])
|
|
228
|
+
return {
|
|
229
|
+
monthly: parseUsdMicros(flags['--monthly-usd']),
|
|
230
|
+
perDecision: parseUsdMicros(flags['--per-decision-usd']),
|
|
231
|
+
rest: targetArgs,
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function parseNamedOptions(args, expected) {
|
|
236
|
+
if (args.length !== expected.length * 2)
|
|
237
|
+
throw new DecisionRouterUsageError('Missing or extra command options')
|
|
238
|
+
const result = Object.create(null)
|
|
239
|
+
for (let index = 0; index < args.length; index += 2) {
|
|
240
|
+
const key = args[index]
|
|
241
|
+
const value = args[index + 1]
|
|
242
|
+
if (!expected.includes(key) || Object.hasOwn(result, key) || !value || value.startsWith('--')) {
|
|
243
|
+
throw new DecisionRouterUsageError('Invalid or duplicate command option')
|
|
244
|
+
}
|
|
245
|
+
result[key] = value
|
|
246
|
+
}
|
|
247
|
+
if (expected.some((key) => !Object.hasOwn(result, key)))
|
|
248
|
+
throw new DecisionRouterUsageError('Missing command option')
|
|
249
|
+
return result
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function parseUsdMicros(value) {
|
|
253
|
+
if (typeof value !== 'string' || !/^(0|[1-9]\d*)(?:\.\d{1,6})?$/.test(value)) {
|
|
254
|
+
throw new DecisionRouterUsageError(
|
|
255
|
+
'USD cap must be a non-negative decimal with at most six fractional digits',
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
const [whole, fraction = ''] = value.split('.')
|
|
259
|
+
const micros = Number(whole) * 1_000_000 + Number(fraction.padEnd(6, '0'))
|
|
260
|
+
if (!Number.isSafeInteger(micros))
|
|
261
|
+
throw new DecisionRouterUsageError('USD cap exceeds the supported range')
|
|
262
|
+
return micros
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function isKnownCandidate(provider, model) {
|
|
266
|
+
return (
|
|
267
|
+
typeof provider === 'string' &&
|
|
268
|
+
typeof model === 'string' &&
|
|
269
|
+
DECISION_ROUTER_MODELS[provider]?.includes(model)
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function isMonth(value) {
|
|
274
|
+
return typeof value === 'string' && /^\d{4}-(0[1-9]|1[0-2])$/.test(value)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function candidateKey(provider, model) {
|
|
278
|
+
return `${provider}/${model}`
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function formatSelection(selection) {
|
|
282
|
+
if (selection.mode === 'explicit') return `${selection.provider}/${selection.model}`
|
|
283
|
+
return selection.mode
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function formatCandidate(candidate) {
|
|
287
|
+
return `${candidate.provider}/${candidate.model}`
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
291
|
+
runDecisionRouterCommand(process.argv.slice(2)).catch((error) => {
|
|
292
|
+
console.error(
|
|
293
|
+
`Error: ${error instanceof Error ? error.message : 'Decision-router command failed'}`,
|
|
294
|
+
)
|
|
295
|
+
process.exitCode = 1
|
|
296
|
+
})
|
|
297
|
+
}
|