modelpace 0.0.0-stage → 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/AGENTS.md +147 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +216 -2
- package/README.zh-CN.md +198 -0
- package/SECURITY.md +16 -0
- package/bin/modelpace-mcp.mjs +337 -0
- package/bin/modelpace.mjs +383 -0
- package/docs/SCHEMA.md +170 -0
- package/examples/multi-account.config.json +168 -0
- package/examples/multi-account.snapshot.json +227 -0
- package/examples/record.example.json +23 -0
- package/examples/sample-snapshot.json +173 -0
- package/llms.txt +26 -0
- package/package.json +56 -4
- package/skills/modelpace/SKILL.md +82 -0
- package/src/adapters/json-snapshot.mjs +360 -0
- package/src/adapters/t3-auto.mjs +160 -0
- package/src/adapters/t3-config.mjs +337 -0
- package/src/adapters/t3-rpc.mjs +275 -0
- package/src/context.mjs +73 -0
- package/src/core/decide.mjs +435 -0
- package/src/core/pacing.mjs +569 -0
- package/src/core/policy.mjs +252 -0
- package/src/core/scores.mjs +491 -0
- package/src/index.mjs +48 -0
- package/src/speed/collect.mjs +47 -0
- package/src/speed/histogram.mjs +170 -0
- package/src/speed/scanner.mjs +390 -0
- package/src/ui/client.js +522 -0
- package/src/ui/dashboard.css +106 -0
- package/src/ui/dashboard.mjs +150 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { DEFAULT_PACING_CONFIG } from './pacing.mjs';
|
|
2
|
+
import { DEFAULT_SCORING_CONFIG, TASKS } from './scores.mjs';
|
|
3
|
+
|
|
4
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
5
|
+
const isPercent = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 100;
|
|
6
|
+
const isPositive = (value) => typeof value === 'number' && Number.isFinite(value) && value > 0;
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_POLICY = Object.freeze({
|
|
9
|
+
schemaVersion: 2,
|
|
10
|
+
tiers: ['work', 'personal', 'public'],
|
|
11
|
+
defaultTier: null,
|
|
12
|
+
taskToSlot: {
|
|
13
|
+
general: 'worker',
|
|
14
|
+
research: 'worker',
|
|
15
|
+
review: 'review',
|
|
16
|
+
slice: 'worker',
|
|
17
|
+
long: 'heavy',
|
|
18
|
+
},
|
|
19
|
+
instances: {},
|
|
20
|
+
slots: {},
|
|
21
|
+
pacing: DEFAULT_PACING_CONFIG,
|
|
22
|
+
scoring: DEFAULT_SCORING_CONFIG,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 内置默认策略:只用官方 T3 Code 的默认实例 id(等于 driver 名),一种 driver 一个实例。
|
|
27
|
+
* 多账号、多订阅的组合见 examples/multi-account.config.json。
|
|
28
|
+
*/
|
|
29
|
+
export function starterPolicy() {
|
|
30
|
+
return {
|
|
31
|
+
schemaVersion: 2,
|
|
32
|
+
tiers: ['work', 'personal', 'public'],
|
|
33
|
+
defaultTier: null,
|
|
34
|
+
taskToSlot: {
|
|
35
|
+
general: 'worker',
|
|
36
|
+
research: 'worker',
|
|
37
|
+
review: 'review',
|
|
38
|
+
slice: 'worker',
|
|
39
|
+
long: 'heavy',
|
|
40
|
+
},
|
|
41
|
+
instances: {
|
|
42
|
+
codex: { driver: 'codex', shortWindowMaxUsedPercent: 90 },
|
|
43
|
+
claudeAgent: { driver: 'claudeAgent', shortWindowMaxUsedPercent: 90 },
|
|
44
|
+
antigravity: { driver: 'antigravity', shortWindowMaxUsedPercent: 90 },
|
|
45
|
+
},
|
|
46
|
+
slots: {
|
|
47
|
+
worker: {
|
|
48
|
+
candidates: [
|
|
49
|
+
{ id: 'codex-worker', instanceId: 'codex', model: 'gpt-5.3-codex', effort: 'high', options: { reasoningEffort: 'high' }, priority: 1 },
|
|
50
|
+
{ id: 'gemini-worker', instanceId: 'antigravity', model: 'gemini-4-hydrogen', pool: 'gemini', effort: 'high', priority: 2 },
|
|
51
|
+
{ id: 'claude-worker', instanceId: 'claudeAgent', model: 'claude-sonnet-4-5', effort: 'high', priority: 3 },
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
heavy: {
|
|
55
|
+
candidates: [
|
|
56
|
+
{ id: 'claude-opus-heavy', instanceId: 'claudeAgent', model: 'claude-opus-4-5', effort: 'high', priority: 1 },
|
|
57
|
+
{ id: 'codex-heavy', instanceId: 'codex', model: 'gpt-5.3-codex', effort: 'xhigh', options: { reasoningEffort: 'xhigh' }, priority: 2 },
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
review: {
|
|
61
|
+
candidates: [
|
|
62
|
+
{ id: 'claude-review', instanceId: 'claudeAgent', model: 'claude-sonnet-4-5', effort: 'high', tasks: ['review', 'research'], priority: 1 },
|
|
63
|
+
{ id: 'codex-review', instanceId: 'codex', model: 'gpt-5.3-codex', effort: 'high', priority: 2 },
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function normalizePolicy(rawPolicy) {
|
|
71
|
+
if (!isObject(rawPolicy)) {
|
|
72
|
+
throw new Error('Policy error: dispatch config must be an object');
|
|
73
|
+
}
|
|
74
|
+
if (rawPolicy.schemaVersion != null && rawPolicy.schemaVersion !== 2) {
|
|
75
|
+
throw new Error(`Policy error: unsupported schemaVersion ${rawPolicy.schemaVersion} (expected 2)`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const tiers = rawPolicy.tiers ?? DEFAULT_POLICY.tiers;
|
|
79
|
+
if (!Array.isArray(tiers) || !tiers.every((t) => typeof t === 'string' && t.trim())) {
|
|
80
|
+
throw new Error('Policy error: tiers must be an array of non-empty strings');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const defaultTier = rawPolicy.defaultTier ?? null;
|
|
84
|
+
if (defaultTier !== null && !tiers.includes(defaultTier)) {
|
|
85
|
+
throw new Error(`Policy error: defaultTier "${defaultTier}" must be listed in tiers`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const taskToSlot = {
|
|
89
|
+
...DEFAULT_POLICY.taskToSlot,
|
|
90
|
+
...(isObject(rawPolicy.taskToSlot) ? rawPolicy.taskToSlot : {}),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const pacing = {
|
|
94
|
+
...DEFAULT_PACING_CONFIG,
|
|
95
|
+
...(isObject(rawPolicy.pacing) ? rawPolicy.pacing : {}),
|
|
96
|
+
resetCredit: {
|
|
97
|
+
...DEFAULT_PACING_CONFIG.resetCredit,
|
|
98
|
+
...(isObject(rawPolicy.pacing?.resetCredit) ? rawPolicy.pacing.resetCredit : {}),
|
|
99
|
+
},
|
|
100
|
+
expiringBoost: rawPolicy.pacing?.expiringBoost === null
|
|
101
|
+
? null
|
|
102
|
+
: {
|
|
103
|
+
...DEFAULT_PACING_CONFIG.expiringBoost,
|
|
104
|
+
...(isObject(rawPolicy.pacing?.expiringBoost) ? rawPolicy.pacing.expiringBoost : {}),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
for (const key of ['paceMarginPercent', 'longTaskMinRemainingPercent', 'defaultMinRemainingPercent', 'shortWindowMaxUsedPercent']) {
|
|
109
|
+
if (!isPercent(pacing[key])) {
|
|
110
|
+
throw new Error(`Policy error: pacing.${key} must be 0..100`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (pacing.paceMarginPercent <= 0) {
|
|
114
|
+
throw new Error('Policy error: pacing.paceMarginPercent must be > 0');
|
|
115
|
+
}
|
|
116
|
+
for (const key of ['recheckAfterMinutes', 'maxSnapshotAgeMinutes']) {
|
|
117
|
+
if (!isPositive(pacing[key])) {
|
|
118
|
+
throw new Error(`Policy error: pacing.${key} must be > 0`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (!isPositive(pacing.resetCredit.skipIfResetWithinHours) || !isPositive(pacing.resetCredit.expiryWarnHours)) {
|
|
122
|
+
throw new Error('Policy error: pacing.resetCredit hours must be > 0');
|
|
123
|
+
}
|
|
124
|
+
if (pacing.expiringBoost !== null) {
|
|
125
|
+
if (!isPositive(pacing.expiringBoost.resetWithinHours)) {
|
|
126
|
+
throw new Error('Policy error: pacing.expiringBoost.resetWithinHours must be > 0');
|
|
127
|
+
}
|
|
128
|
+
if (!isPercent(pacing.expiringBoost.minRemainingPercent) || !isPercent(pacing.expiringBoost.maxShortUsedPercent)) {
|
|
129
|
+
throw new Error('Policy error: pacing.expiringBoost percentages must be 0..100');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const rawScoring = isObject(rawPolicy.scoring) ? rawPolicy.scoring : {};
|
|
134
|
+
const scoring = {
|
|
135
|
+
...DEFAULT_SCORING_CONFIG,
|
|
136
|
+
...rawScoring,
|
|
137
|
+
aliases: {
|
|
138
|
+
provider: {
|
|
139
|
+
...DEFAULT_SCORING_CONFIG.aliases.provider,
|
|
140
|
+
...(isObject(rawScoring.aliases?.provider) ? rawScoring.aliases.provider : {}),
|
|
141
|
+
},
|
|
142
|
+
tier: {
|
|
143
|
+
...DEFAULT_SCORING_CONFIG.aliases.tier,
|
|
144
|
+
...(isObject(rawScoring.aliases?.tier) ? rawScoring.aliases.tier : {}),
|
|
145
|
+
},
|
|
146
|
+
...(isObject(rawScoring.aliases?.model) ? { model: rawScoring.aliases.model } : {}),
|
|
147
|
+
...(isObject(rawScoring.aliases?.effort) ? { effort: rawScoring.aliases.effort } : {}),
|
|
148
|
+
},
|
|
149
|
+
pricing: {
|
|
150
|
+
...DEFAULT_SCORING_CONFIG.pricing,
|
|
151
|
+
...(isObject(rawScoring.pricing) ? rawScoring.pricing : {}),
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
if (!['demote', 'strict', 'advisory'].includes(scoring.mode)) {
|
|
156
|
+
throw new Error(`Policy error: scoring.mode must be demote, strict, or advisory`);
|
|
157
|
+
}
|
|
158
|
+
if (!Number.isFinite(scoring.minPassRate) || scoring.minPassRate < 0 || scoring.minPassRate > 1) {
|
|
159
|
+
throw new Error('Policy error: scoring.minPassRate must be in 0..1');
|
|
160
|
+
}
|
|
161
|
+
if (!Number.isSafeInteger(scoring.minSamples) || scoring.minSamples < 1) {
|
|
162
|
+
throw new Error('Policy error: scoring.minSamples must be a positive integer');
|
|
163
|
+
}
|
|
164
|
+
if (!Number.isFinite(scoring.speedTieBreakerMarginRatio) || scoring.speedTieBreakerMarginRatio < 0) {
|
|
165
|
+
throw new Error('Policy error: scoring.speedTieBreakerMarginRatio must be >= 0');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const instances = {};
|
|
169
|
+
if (isObject(rawPolicy.instances)) {
|
|
170
|
+
for (const [id, inst] of Object.entries(rawPolicy.instances)) {
|
|
171
|
+
if (!isObject(inst)) throw new Error(`Policy error: instances.${id} must be an object`);
|
|
172
|
+
if (inst.tiers != null && (!Array.isArray(inst.tiers) || !inst.tiers.every((t) => tiers.includes(t)))) {
|
|
173
|
+
throw new Error(`Policy error: instances.${id}.tiers contains unknown tier`);
|
|
174
|
+
}
|
|
175
|
+
if (inst.shortWindowMaxUsedPercent != null && !isPercent(inst.shortWindowMaxUsedPercent)) {
|
|
176
|
+
throw new Error(`Policy error: instances.${id}.shortWindowMaxUsedPercent must be 0..100`);
|
|
177
|
+
}
|
|
178
|
+
if (inst.weeklyMinRemainingPercent != null && !isPercent(inst.weeklyMinRemainingPercent)) {
|
|
179
|
+
throw new Error(`Policy error: instances.${id}.weeklyMinRemainingPercent must be 0..100`);
|
|
180
|
+
}
|
|
181
|
+
instances[id] = {
|
|
182
|
+
enabled: inst.enabled !== false,
|
|
183
|
+
...inst,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!isObject(rawPolicy.slots) || Object.keys(rawPolicy.slots).length === 0) {
|
|
189
|
+
throw new Error('Policy error: slots must define at least one slot');
|
|
190
|
+
}
|
|
191
|
+
const slots = {};
|
|
192
|
+
for (const [slotName, slotDef] of Object.entries(rawPolicy.slots)) {
|
|
193
|
+
if (!isObject(slotDef) || !Array.isArray(slotDef.candidates)) {
|
|
194
|
+
throw new Error(`Policy error: slots.${slotName}.candidates must be an array`);
|
|
195
|
+
}
|
|
196
|
+
const seenIds = new Set();
|
|
197
|
+
const candidates = slotDef.candidates.map((cand, index) => {
|
|
198
|
+
if (!isObject(cand)) {
|
|
199
|
+
throw new Error(`Policy error: slots.${slotName}.candidates[${index}] must be an object`);
|
|
200
|
+
}
|
|
201
|
+
if (typeof cand.instanceId !== 'string' || !cand.instanceId.trim()) {
|
|
202
|
+
throw new Error(`Policy error: slots.${slotName}.candidates[${index}].instanceId is required`);
|
|
203
|
+
}
|
|
204
|
+
if (typeof cand.model !== 'string' || !cand.model.trim()) {
|
|
205
|
+
throw new Error(`Policy error: slots.${slotName}.candidates[${index}].model is required`);
|
|
206
|
+
}
|
|
207
|
+
const id = typeof cand.id === 'string' && cand.id.trim() ? cand.id.trim() : `${cand.instanceId}:${cand.model}:${index}`;
|
|
208
|
+
if (seenIds.has(id)) {
|
|
209
|
+
throw new Error(`Policy error: duplicate candidate id "${id}" in slot "${slotName}"`);
|
|
210
|
+
}
|
|
211
|
+
seenIds.add(id);
|
|
212
|
+
if (cand.tiers != null && (!Array.isArray(cand.tiers) || !cand.tiers.every((t) => tiers.includes(t)))) {
|
|
213
|
+
throw new Error(`Policy error: candidate "${id}" tiers contains unknown tier`);
|
|
214
|
+
}
|
|
215
|
+
if (cand.tasks != null && (!Array.isArray(cand.tasks) || !cand.tasks.every((t) => TASKS.includes(t)))) {
|
|
216
|
+
throw new Error(`Policy error: candidate "${id}" tasks contains invalid task`);
|
|
217
|
+
}
|
|
218
|
+
if (cand.maxShortUsedPercent != null && !isPercent(cand.maxShortUsedPercent)) {
|
|
219
|
+
throw new Error(`Policy error: candidate "${id}" maxShortUsedPercent must be 0..100`);
|
|
220
|
+
}
|
|
221
|
+
if (cand.minRemainingPercent != null && !isPercent(cand.minRemainingPercent)) {
|
|
222
|
+
throw new Error(`Policy error: candidate "${id}" minRemainingPercent must be 0..100`);
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
...cand,
|
|
226
|
+
id,
|
|
227
|
+
enabled: cand.enabled !== false,
|
|
228
|
+
effort: typeof cand.effort === 'string' && cand.effort.trim() ? cand.effort.trim() : 'default',
|
|
229
|
+
priority: Number.isFinite(cand.priority) ? cand.priority : index + 1,
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
slots[slotName] = {
|
|
233
|
+
...slotDef,
|
|
234
|
+
candidates,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
schemaVersion: 2,
|
|
240
|
+
tiers: [...tiers],
|
|
241
|
+
defaultTier,
|
|
242
|
+
taskToSlot,
|
|
243
|
+
instances,
|
|
244
|
+
slots,
|
|
245
|
+
pacing,
|
|
246
|
+
scoring,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function validatePolicy(rawPolicy) {
|
|
251
|
+
return normalizePolicy(rawPolicy);
|
|
252
|
+
}
|