@telsoncc/opencode-commandcode 1.2.32 → 1.2.34
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 +6 -220
- package/_version.txt +1 -1
- package/dist/_version.txt +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +19 -0
- package/dist/manifest.json +28 -0
- package/dist/models.json +3334 -0
- package/dist/plugin-opencode2.d.ts +30 -0
- package/dist/plugin-opencode2.js +127 -0
- package/dist/plugin.d.ts +27 -0
- package/dist/plugin.js +192 -0
- package/dist/src/auth.d.ts +5 -0
- package/dist/src/auth.js +38 -0
- package/dist/src/catalog-break.d.ts +12 -0
- package/dist/src/catalog-break.js +33 -0
- package/dist/src/catalog-release-notes.d.ts +30 -0
- package/dist/src/catalog-release-notes.js +106 -0
- package/dist/src/catalog.d.ts +160 -0
- package/dist/src/catalog.js +786 -0
- package/dist/src/convert.d.ts +71 -0
- package/dist/src/convert.js +154 -0
- package/dist/src/costs-docs.d.ts +13 -0
- package/dist/src/costs-docs.js +170 -0
- package/dist/src/costs-models-dev.d.ts +68 -0
- package/dist/src/costs-models-dev.js +303 -0
- package/dist/src/cut-changelog.d.ts +1 -0
- package/dist/src/cut-changelog.js +43 -0
- package/dist/src/manifest.d.ts +72 -0
- package/dist/src/manifest.js +111 -0
- package/dist/src/model.d.ts +18 -0
- package/dist/src/model.js +138 -0
- package/dist/src/opencode2.d.ts +179 -0
- package/dist/src/opencode2.js +386 -0
- package/dist/src/pack-contract.d.ts +25 -0
- package/dist/src/pack-contract.js +42 -0
- package/dist/src/publish-policy.d.ts +18 -0
- package/dist/src/publish-policy.js +16 -0
- package/dist/src/startup.d.ts +18 -0
- package/dist/src/startup.js +31 -0
- package/dist/src/stream.d.ts +2 -0
- package/dist/src/stream.js +203 -0
- package/manifest.json +7 -7
- package/models.json +38 -66
- package/package.json +12 -7
- package/plugin.ts +14 -3
- package/src/cut-changelog.ts +6 -1
- package/src/opencode2.ts +14 -3
- package/src/pack-contract.ts +51 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { pluginStateDir } from "./startup.js";
|
|
4
|
+
export const MODELS_DEV_URL = "https://models.dev/api.json";
|
|
5
|
+
export const MODELS_DEV_TIMEOUT_MS = 30_000;
|
|
6
|
+
export const MODELS_DEV_CACHE_TTL_MS = 24 * 3600_000;
|
|
7
|
+
export const MODELS_DEV_CACHE_FILE = "models-dev-cache.json";
|
|
8
|
+
export const ESTIMATED_CONTEXT_TOKENS = 200000;
|
|
9
|
+
export const ESTIMATED_OUTPUT_TOKENS = 65536;
|
|
10
|
+
export const FREE_COST = { input: 0, output: 0 };
|
|
11
|
+
export const TEXT_ONLY_MODALITIES = { input: ["text"], output: ["text"] };
|
|
12
|
+
function lastSegment(id) {
|
|
13
|
+
const i = id.lastIndexOf("/");
|
|
14
|
+
return i >= 0 ? id.slice(i + 1) : id;
|
|
15
|
+
}
|
|
16
|
+
export function isFreeSku(model) {
|
|
17
|
+
if (/-free$/i.test(model.id))
|
|
18
|
+
return true;
|
|
19
|
+
return /\bfree\b/i.test(`${model.id} ${model.name}`);
|
|
20
|
+
}
|
|
21
|
+
export function parseModelsDev(json) {
|
|
22
|
+
const data = JSON.parse(json);
|
|
23
|
+
const rows = [];
|
|
24
|
+
const seen = new Set();
|
|
25
|
+
for (const provider of Object.keys(data).sort()) {
|
|
26
|
+
const models = data[provider]?.models ?? {};
|
|
27
|
+
for (const model of Object.values(models)) {
|
|
28
|
+
if (!model?.id || model.cost?.input === undefined || model.cost?.output === undefined)
|
|
29
|
+
continue;
|
|
30
|
+
const key = model.id.toLowerCase();
|
|
31
|
+
if (seen.has(key))
|
|
32
|
+
continue;
|
|
33
|
+
seen.add(key);
|
|
34
|
+
const cost = { input: model.cost.input, output: model.cost.output };
|
|
35
|
+
if (model.cost.cache_read !== undefined)
|
|
36
|
+
cost.cache_read = model.cost.cache_read;
|
|
37
|
+
if (model.cost.cache_write !== undefined)
|
|
38
|
+
cost.cache_write = model.cost.cache_write;
|
|
39
|
+
const row = { id: model.id, name: model.name ?? model.id, cost };
|
|
40
|
+
if (typeof model.attachment === "boolean")
|
|
41
|
+
row.attachment = model.attachment;
|
|
42
|
+
if (typeof model.reasoning === "boolean")
|
|
43
|
+
row.reasoning = model.reasoning;
|
|
44
|
+
const efforts = extractEffortValues(model.reasoning_options);
|
|
45
|
+
if (efforts)
|
|
46
|
+
row.reasoningEfforts = efforts;
|
|
47
|
+
if (typeof model.tool_call === "boolean")
|
|
48
|
+
row.toolCall = model.tool_call;
|
|
49
|
+
const limit = extractLimit(model.limit);
|
|
50
|
+
if (limit)
|
|
51
|
+
row.limit = limit;
|
|
52
|
+
const input = model.modalities?.input?.filter((x) => typeof x === "string");
|
|
53
|
+
const output = model.modalities?.output?.filter((x) => typeof x === "string");
|
|
54
|
+
if (input?.length || output?.length) {
|
|
55
|
+
row.modalities = {
|
|
56
|
+
input: input?.length ? input : [...TEXT_ONLY_MODALITIES.input],
|
|
57
|
+
output: output?.length ? output : [...TEXT_ONLY_MODALITIES.output],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
rows.push(row);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return rows;
|
|
64
|
+
}
|
|
65
|
+
function indexRows(rows) {
|
|
66
|
+
const byId = new Map();
|
|
67
|
+
const bySegment = new Map();
|
|
68
|
+
const byName = new Map();
|
|
69
|
+
for (const row of rows) {
|
|
70
|
+
const idKey = row.id.toLowerCase();
|
|
71
|
+
if (!byId.has(idKey))
|
|
72
|
+
byId.set(idKey, row);
|
|
73
|
+
const segment = lastSegment(row.id).toLowerCase();
|
|
74
|
+
if (!bySegment.has(segment))
|
|
75
|
+
bySegment.set(segment, row);
|
|
76
|
+
const nameKey = row.name.toLowerCase();
|
|
77
|
+
if (!byName.has(nameKey))
|
|
78
|
+
byName.set(nameKey, row);
|
|
79
|
+
}
|
|
80
|
+
return { byId, bySegment, byName };
|
|
81
|
+
}
|
|
82
|
+
function findRow(model, index) {
|
|
83
|
+
return (index.byId.get(model.id.toLowerCase()) ??
|
|
84
|
+
index.bySegment.get(lastSegment(model.id).toLowerCase()) ??
|
|
85
|
+
index.byName.get(model.name.toLowerCase()));
|
|
86
|
+
}
|
|
87
|
+
export function applyFreeCosts(models, skipIds, filledIds) {
|
|
88
|
+
let filled = 0;
|
|
89
|
+
for (const model of models) {
|
|
90
|
+
if (skipIds.has(model.id) || !isFreeSku(model))
|
|
91
|
+
continue;
|
|
92
|
+
model.cost = { ...FREE_COST };
|
|
93
|
+
filledIds?.add(model.id);
|
|
94
|
+
filled++;
|
|
95
|
+
}
|
|
96
|
+
return filled;
|
|
97
|
+
}
|
|
98
|
+
function textOnly() {
|
|
99
|
+
return { input: [...TEXT_ONLY_MODALITIES.input], output: [...TEXT_ONLY_MODALITIES.output] };
|
|
100
|
+
}
|
|
101
|
+
function extractEffortValues(options) {
|
|
102
|
+
if (!Array.isArray(options))
|
|
103
|
+
return undefined;
|
|
104
|
+
const values = options.flatMap((o) => o?.type === "effort" && Array.isArray(o.values) ? o.values : []);
|
|
105
|
+
const unique = [
|
|
106
|
+
...new Set(values.filter((v) => typeof v === "string" && v.length > 0)),
|
|
107
|
+
];
|
|
108
|
+
return unique.length > 0 ? unique : undefined;
|
|
109
|
+
}
|
|
110
|
+
function extractLimit(limit) {
|
|
111
|
+
if (!limit || typeof limit.context !== "number" || typeof limit.output !== "number") {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (!(limit.context > 0) || !(limit.output > 0))
|
|
115
|
+
return undefined;
|
|
116
|
+
return { context: limit.context, output: limit.output };
|
|
117
|
+
}
|
|
118
|
+
function prettifyModelId(id) {
|
|
119
|
+
return lastSegment(id)
|
|
120
|
+
.replace(/[-_]+/g, " ")
|
|
121
|
+
.replace(/\s+/g, " ")
|
|
122
|
+
.trim()
|
|
123
|
+
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
124
|
+
}
|
|
125
|
+
/** Match a models.dev row by exact id, last path segment, or display name. */
|
|
126
|
+
export function findModelsDevRow(rows, id, name) {
|
|
127
|
+
const index = indexRows(rows);
|
|
128
|
+
return (index.byId.get(id.toLowerCase()) ??
|
|
129
|
+
index.bySegment.get(lastSegment(id).toLowerCase()) ??
|
|
130
|
+
(name ? index.byName.get(name.toLowerCase()) : undefined));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Build a provisional catalog entry for a remote id missing locally.
|
|
134
|
+
* Costs/capabilities come from models.dev (list prices, close to but not
|
|
135
|
+
* equal to Command Code billing); the entry is replaced on the next catalog
|
|
136
|
+
* sync. Returns null when nothing can ground an estimate.
|
|
137
|
+
*/
|
|
138
|
+
export function buildEstimatedEntry(id, rows) {
|
|
139
|
+
const displayName = prettifyModelId(id);
|
|
140
|
+
if (isFreeSku({ id, name: displayName })) {
|
|
141
|
+
return {
|
|
142
|
+
id,
|
|
143
|
+
name: `${displayName} (est.)`,
|
|
144
|
+
tier: "open-source",
|
|
145
|
+
reasoning: false,
|
|
146
|
+
tool_call: true,
|
|
147
|
+
cost: { ...FREE_COST },
|
|
148
|
+
limit: { context: ESTIMATED_CONTEXT_TOKENS, output: ESTIMATED_OUTPUT_TOKENS },
|
|
149
|
+
attachment: false,
|
|
150
|
+
modalities: textOnly(),
|
|
151
|
+
estimated: true,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const row = findModelsDevRow(rows, id);
|
|
155
|
+
if (!row)
|
|
156
|
+
return null;
|
|
157
|
+
const cost = { input: row.cost.input, output: row.cost.output };
|
|
158
|
+
if (row.cost.cache_read !== undefined)
|
|
159
|
+
cost.cache_read = row.cost.cache_read;
|
|
160
|
+
if (row.cost.cache_write !== undefined)
|
|
161
|
+
cost.cache_write = row.cost.cache_write;
|
|
162
|
+
const modalities = row.modalities
|
|
163
|
+
? { input: [...row.modalities.input], output: [...row.modalities.output] }
|
|
164
|
+
: textOnly();
|
|
165
|
+
return {
|
|
166
|
+
id,
|
|
167
|
+
name: `${row.name} (est.)`,
|
|
168
|
+
tier: "open-source",
|
|
169
|
+
reasoning: row.reasoning ?? (row.reasoningEfforts?.length ?? 0) > 0,
|
|
170
|
+
...(row.reasoningEfforts ? { reasoningEfforts: [...row.reasoningEfforts] } : {}),
|
|
171
|
+
tool_call: row.toolCall ?? true,
|
|
172
|
+
cost,
|
|
173
|
+
limit: row.limit ?? {
|
|
174
|
+
context: ESTIMATED_CONTEXT_TOKENS,
|
|
175
|
+
output: ESTIMATED_OUTPUT_TOKENS,
|
|
176
|
+
},
|
|
177
|
+
attachment: row.attachment ?? modalities.input.includes("image"),
|
|
178
|
+
modalities,
|
|
179
|
+
estimated: true,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function readModelsDevCache(cacheDir) {
|
|
183
|
+
const path = join(cacheDir, MODELS_DEV_CACHE_FILE);
|
|
184
|
+
if (!existsSync(path))
|
|
185
|
+
return null;
|
|
186
|
+
try {
|
|
187
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
188
|
+
if (typeof parsed.json !== "string" || typeof parsed.fetchedAt !== "number")
|
|
189
|
+
return null;
|
|
190
|
+
return { fetchedAt: parsed.fetchedAt, json: parsed.json };
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function writeModelsDevCache(cacheDir, json) {
|
|
197
|
+
try {
|
|
198
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
199
|
+
writeFileSync(join(cacheDir, MODELS_DEV_CACHE_FILE), JSON.stringify({ fetchedAt: Date.now(), json }), "utf-8");
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// ignore cache write
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Load models.dev rows, cached on disk for a day. Falls back to stale cache
|
|
207
|
+
* when the network fails; returns [] only when nothing is available.
|
|
208
|
+
*/
|
|
209
|
+
export async function loadModelsDevRows(options = {}) {
|
|
210
|
+
const cacheDir = options.cacheDir ?? pluginStateDir();
|
|
211
|
+
const maxAgeMs = options.maxAgeMs ?? MODELS_DEV_CACHE_TTL_MS;
|
|
212
|
+
const cached = readModelsDevCache(cacheDir);
|
|
213
|
+
if (cached && Date.now() - cached.fetchedAt < maxAgeMs) {
|
|
214
|
+
try {
|
|
215
|
+
return parseModelsDev(cached.json);
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// fall through to refetch
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
const json = await fetchModelsDevJson(AbortSignal.timeout(options.timeoutMs ?? MODELS_DEV_TIMEOUT_MS));
|
|
223
|
+
writeModelsDevCache(cacheDir, json);
|
|
224
|
+
return parseModelsDev(json);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
if (cached) {
|
|
228
|
+
try {
|
|
229
|
+
return parseModelsDev(cached.json);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
// fall through to []
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return [];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
export function applyModelsDevModalities(models, rows) {
|
|
239
|
+
const index = indexRows(rows);
|
|
240
|
+
let filled = 0;
|
|
241
|
+
for (const model of models) {
|
|
242
|
+
const row = findRow(model, index);
|
|
243
|
+
const current = model.modalities;
|
|
244
|
+
if (current && model.attachment !== undefined) {
|
|
245
|
+
const extra = row?.modalities?.input?.filter((x) => !current.input.includes(x)) ?? [];
|
|
246
|
+
if (extra.length > 0) {
|
|
247
|
+
model.modalities = {
|
|
248
|
+
input: [...current.input, ...extra],
|
|
249
|
+
output: [...current.output],
|
|
250
|
+
};
|
|
251
|
+
if (model.modalities.input.includes("image"))
|
|
252
|
+
model.attachment = true;
|
|
253
|
+
filled++;
|
|
254
|
+
}
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (row && (row.modalities || row.attachment !== undefined)) {
|
|
258
|
+
const modalities = row.modalities
|
|
259
|
+
? { input: [...row.modalities.input], output: [...row.modalities.output] }
|
|
260
|
+
: textOnly();
|
|
261
|
+
model.modalities = modalities;
|
|
262
|
+
model.attachment = row.attachment ?? modalities.input.includes("image");
|
|
263
|
+
filled++;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
model.attachment = false;
|
|
267
|
+
model.modalities = textOnly();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return filled;
|
|
271
|
+
}
|
|
272
|
+
export function applyModelsDevCosts(models, rows, skipIds, filledIds) {
|
|
273
|
+
const index = indexRows(rows);
|
|
274
|
+
let filled = 0;
|
|
275
|
+
for (const model of models) {
|
|
276
|
+
if (skipIds.has(model.id) || isFreeSku(model))
|
|
277
|
+
continue;
|
|
278
|
+
const row = findRow(model, index);
|
|
279
|
+
if (!row)
|
|
280
|
+
continue;
|
|
281
|
+
model.cost = { input: row.cost.input, output: row.cost.output };
|
|
282
|
+
if (row.cost.cache_read !== undefined)
|
|
283
|
+
model.cost.cache_read = row.cost.cache_read;
|
|
284
|
+
if (row.cost.cache_write !== undefined)
|
|
285
|
+
model.cost.cache_write = row.cost.cache_write;
|
|
286
|
+
filledIds?.add(model.id);
|
|
287
|
+
filled++;
|
|
288
|
+
}
|
|
289
|
+
return filled;
|
|
290
|
+
}
|
|
291
|
+
export async function fetchModelsDevJson(signal) {
|
|
292
|
+
const resp = await fetch(MODELS_DEV_URL, {
|
|
293
|
+
headers: {
|
|
294
|
+
// ponytail: models.dev returns 403 without a browser-like UA; upgrade if they add a real API token
|
|
295
|
+
"User-Agent": "Mozilla/5.0 (compatible; opencode-commandcode/0.5; +https://github.com/telsoncc/opencode-commandcode)",
|
|
296
|
+
Accept: "application/json",
|
|
297
|
+
},
|
|
298
|
+
...(signal ? { signal } : {}),
|
|
299
|
+
});
|
|
300
|
+
if (!resp.ok)
|
|
301
|
+
throw new Error(`models.dev returned ${resp.status}`);
|
|
302
|
+
return resp.text();
|
|
303
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cutKeepAChangelog(markdown: string, version: string, date: string): string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const UNRELEASED = /^## \[Unreleased\]\s*$/;
|
|
2
|
+
const VERSION = /^## \[/;
|
|
3
|
+
export function cutKeepAChangelog(markdown, version, date) {
|
|
4
|
+
if (new RegExp(`^## \\[${escapeRegExp(version)}\\]`, "m").test(markdown))
|
|
5
|
+
return markdown;
|
|
6
|
+
const lines = markdown.split(/(?<=\n)/);
|
|
7
|
+
let start = null;
|
|
8
|
+
for (let i = 0; i < lines.length; i++) {
|
|
9
|
+
const line = lines[i];
|
|
10
|
+
if (line !== undefined && UNRELEASED.test(line.replace(/\n$/, ""))) {
|
|
11
|
+
start = i;
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (start === null)
|
|
16
|
+
throw new Error("CHANGELOG.md has no ## [Unreleased] heading");
|
|
17
|
+
let end = lines.length;
|
|
18
|
+
for (let j = start + 1; j < lines.length; j++) {
|
|
19
|
+
const line = lines[j];
|
|
20
|
+
if (line === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
const stripped = line.replace(/\n$/, "");
|
|
23
|
+
if (VERSION.test(stripped) && !UNRELEASED.test(stripped)) {
|
|
24
|
+
end = j;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const body = lines
|
|
29
|
+
.slice(start + 1, end)
|
|
30
|
+
.join("")
|
|
31
|
+
.replace(/^\n+/, "\n");
|
|
32
|
+
// Nothing was written under [Unreleased] since the last cut: emitting a
|
|
33
|
+
// heading here would only add an empty version section. Releases are also
|
|
34
|
+
// described on GitHub (semantic-release catalog notes), so skip the cut.
|
|
35
|
+
if (body.trim() === "")
|
|
36
|
+
return markdown;
|
|
37
|
+
const before = lines.slice(0, start + 1).join("");
|
|
38
|
+
const after = lines.slice(end).join("");
|
|
39
|
+
return `${before}\n## [${version}] - ${date}\n${body}${after}`;
|
|
40
|
+
}
|
|
41
|
+
function escapeRegExp(value) {
|
|
42
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
43
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type CatalogStatus = "healthy" | "degraded" | "broken";
|
|
2
|
+
export type CostCatalogBest = "cli" | "docs" | "thirdParty" | "free" | "fallback" | "missing";
|
|
3
|
+
export type CostSources = {
|
|
4
|
+
cli: number;
|
|
5
|
+
officialDocs: number;
|
|
6
|
+
thirdParty: number;
|
|
7
|
+
free: number;
|
|
8
|
+
fallback: number;
|
|
9
|
+
unmatched: number;
|
|
10
|
+
};
|
|
11
|
+
export type UnavailableEntry = {
|
|
12
|
+
id: string;
|
|
13
|
+
reason: "not-listed-by-provider-api";
|
|
14
|
+
};
|
|
15
|
+
export type CatalogReview = {
|
|
16
|
+
thirdParty: string[];
|
|
17
|
+
free: string[];
|
|
18
|
+
unmatched: string[];
|
|
19
|
+
unavailable?: UnavailableEntry[];
|
|
20
|
+
};
|
|
21
|
+
export type CatalogManifest = {
|
|
22
|
+
schemaVersion: 1;
|
|
23
|
+
generatedAt: string;
|
|
24
|
+
pluginVersion: string;
|
|
25
|
+
commandCodeVersion: string;
|
|
26
|
+
commandCodeTarball: string;
|
|
27
|
+
modelCount: number;
|
|
28
|
+
reasoningModelCount: number;
|
|
29
|
+
extraction: {
|
|
30
|
+
modelCatalog: "ok" | "failed";
|
|
31
|
+
costCatalog: CostCatalogBest;
|
|
32
|
+
costCatalogError: string | null;
|
|
33
|
+
};
|
|
34
|
+
costSources: CostSources;
|
|
35
|
+
review?: CatalogReview;
|
|
36
|
+
status: CatalogStatus;
|
|
37
|
+
};
|
|
38
|
+
export type BuildManifestInput = {
|
|
39
|
+
pluginVersion: string;
|
|
40
|
+
commandCodeVersion: string;
|
|
41
|
+
commandCodeTarball: string;
|
|
42
|
+
modelCount: number;
|
|
43
|
+
reasoningModelCount: number;
|
|
44
|
+
modelCatalogOk: boolean;
|
|
45
|
+
costSources: CostSources;
|
|
46
|
+
review?: CatalogReview;
|
|
47
|
+
generatedAt: string;
|
|
48
|
+
costCatalogError?: string | null;
|
|
49
|
+
};
|
|
50
|
+
export declare function withUnavailableIds(review: CatalogReview | undefined, unavailable: string[]): CatalogReview | undefined;
|
|
51
|
+
/** Update only the embedded plugin version; reject a missing target version. */
|
|
52
|
+
export declare function withPluginVersion(manifest: CatalogManifest, version: string): CatalogManifest;
|
|
53
|
+
export declare function bumpPatch(version: string): string;
|
|
54
|
+
export declare function meetsModelCountFloor(modelCount: number, lastSuccessful: number | null): boolean;
|
|
55
|
+
export declare function bestCostCatalog(sources: CostSources): CostCatalogBest;
|
|
56
|
+
export declare function catalogStatus(modelCatalogOk: boolean, sources: CostSources): CatalogStatus;
|
|
57
|
+
export declare function commandCodeTarballUrl(version: string): string;
|
|
58
|
+
export declare function buildManifest(input: BuildManifestInput): CatalogManifest;
|
|
59
|
+
export declare function writeManifest(path: string, manifest: CatalogManifest): void;
|
|
60
|
+
export declare function lastSuccessfulModelCount(manifest: CatalogManifest | null): number | null;
|
|
61
|
+
export declare function countCostSources(input: {
|
|
62
|
+
modelIds: string[];
|
|
63
|
+
cliIds: Set<string>;
|
|
64
|
+
officialDocIds: Set<string>;
|
|
65
|
+
thirdPartyIds: Set<string>;
|
|
66
|
+
freeIds: Set<string>;
|
|
67
|
+
fallbackIds?: Set<string>;
|
|
68
|
+
}): CostSources;
|
|
69
|
+
export declare function bumpPackageVersionField(pkgJson: string): {
|
|
70
|
+
json: string;
|
|
71
|
+
version: string;
|
|
72
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { writeFileSync } from "fs";
|
|
2
|
+
export function withUnavailableIds(review, unavailable) {
|
|
3
|
+
const entries = [...unavailable]
|
|
4
|
+
.sort()
|
|
5
|
+
.map((id) => ({ id, reason: "not-listed-by-provider-api" }));
|
|
6
|
+
if (entries.length === 0)
|
|
7
|
+
return review;
|
|
8
|
+
return { ...(review ?? { thirdParty: [], free: [], unmatched: [] }), unavailable: entries };
|
|
9
|
+
}
|
|
10
|
+
/** Update only the embedded plugin version; reject a missing target version. */
|
|
11
|
+
export function withPluginVersion(manifest, version) {
|
|
12
|
+
if (!version || typeof version !== "string" || version.trim().length === 0) {
|
|
13
|
+
throw new Error("target plugin version must be a non-empty string");
|
|
14
|
+
}
|
|
15
|
+
return { ...manifest, pluginVersion: version };
|
|
16
|
+
}
|
|
17
|
+
export function bumpPatch(version) {
|
|
18
|
+
const parts = version.split(".");
|
|
19
|
+
const patch = Number(parts[2] ?? "0");
|
|
20
|
+
return `${parts[0] ?? "0"}.${parts[1] ?? "0"}.${patch + 1}`;
|
|
21
|
+
}
|
|
22
|
+
export function meetsModelCountFloor(modelCount, lastSuccessful) {
|
|
23
|
+
const floor = Math.max(20, Math.floor((lastSuccessful ?? 20) * 0.5));
|
|
24
|
+
// ponytail: when there is no prior catalog, lastSuccessful is null and the floor is 20
|
|
25
|
+
return modelCount >= (lastSuccessful === null ? 20 : floor);
|
|
26
|
+
}
|
|
27
|
+
export function bestCostCatalog(sources) {
|
|
28
|
+
if (sources.cli > 0)
|
|
29
|
+
return "cli";
|
|
30
|
+
if (sources.officialDocs > 0)
|
|
31
|
+
return "docs";
|
|
32
|
+
if (sources.thirdParty > 0)
|
|
33
|
+
return "thirdParty";
|
|
34
|
+
if (sources.free > 0)
|
|
35
|
+
return "free";
|
|
36
|
+
if (sources.fallback > 0)
|
|
37
|
+
return "fallback";
|
|
38
|
+
return "missing";
|
|
39
|
+
}
|
|
40
|
+
export function catalogStatus(modelCatalogOk, sources) {
|
|
41
|
+
if (!modelCatalogOk)
|
|
42
|
+
return "broken";
|
|
43
|
+
if (sources.unmatched > 0)
|
|
44
|
+
return "degraded";
|
|
45
|
+
return "healthy";
|
|
46
|
+
}
|
|
47
|
+
export function commandCodeTarballUrl(version) {
|
|
48
|
+
return `https://registry.npmjs.org/command-code/-/command-code-${version}.tgz`;
|
|
49
|
+
}
|
|
50
|
+
export function buildManifest(input) {
|
|
51
|
+
const status = catalogStatus(input.modelCatalogOk, input.costSources);
|
|
52
|
+
return {
|
|
53
|
+
schemaVersion: 1,
|
|
54
|
+
generatedAt: input.generatedAt,
|
|
55
|
+
pluginVersion: input.pluginVersion,
|
|
56
|
+
commandCodeVersion: input.commandCodeVersion,
|
|
57
|
+
commandCodeTarball: input.commandCodeTarball,
|
|
58
|
+
modelCount: input.modelCount,
|
|
59
|
+
reasoningModelCount: input.reasoningModelCount,
|
|
60
|
+
extraction: {
|
|
61
|
+
modelCatalog: input.modelCatalogOk ? "ok" : "failed",
|
|
62
|
+
costCatalog: bestCostCatalog(input.costSources),
|
|
63
|
+
costCatalogError: input.costCatalogError ?? null,
|
|
64
|
+
},
|
|
65
|
+
costSources: { ...input.costSources },
|
|
66
|
+
...(input.review ? { review: input.review } : {}),
|
|
67
|
+
status,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function writeManifest(path, manifest) {
|
|
71
|
+
writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`, "utf-8");
|
|
72
|
+
}
|
|
73
|
+
export function lastSuccessfulModelCount(manifest) {
|
|
74
|
+
if (!manifest)
|
|
75
|
+
return null;
|
|
76
|
+
if (manifest.status === "broken")
|
|
77
|
+
return null;
|
|
78
|
+
return manifest.modelCount;
|
|
79
|
+
}
|
|
80
|
+
export function countCostSources(input) {
|
|
81
|
+
const fallbackIds = input.fallbackIds ?? new Set();
|
|
82
|
+
const sources = {
|
|
83
|
+
cli: 0,
|
|
84
|
+
officialDocs: 0,
|
|
85
|
+
thirdParty: 0,
|
|
86
|
+
free: 0,
|
|
87
|
+
fallback: 0,
|
|
88
|
+
unmatched: 0,
|
|
89
|
+
};
|
|
90
|
+
for (const id of input.modelIds) {
|
|
91
|
+
if (input.cliIds.has(id))
|
|
92
|
+
sources.cli++;
|
|
93
|
+
else if (input.officialDocIds.has(id))
|
|
94
|
+
sources.officialDocs++;
|
|
95
|
+
else if (input.thirdPartyIds.has(id))
|
|
96
|
+
sources.thirdParty++;
|
|
97
|
+
else if (input.freeIds.has(id))
|
|
98
|
+
sources.free++;
|
|
99
|
+
else if (fallbackIds.has(id))
|
|
100
|
+
sources.fallback++;
|
|
101
|
+
else
|
|
102
|
+
sources.unmatched++;
|
|
103
|
+
}
|
|
104
|
+
return sources;
|
|
105
|
+
}
|
|
106
|
+
export function bumpPackageVersionField(pkgJson) {
|
|
107
|
+
const pkg = JSON.parse(pkgJson);
|
|
108
|
+
const version = bumpPatch(pkg.version);
|
|
109
|
+
const json = pkgJson.replace(/("version"\s*:\s*")([^"]+)(")/, `$1${version}$3`);
|
|
110
|
+
return { json, version };
|
|
111
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3StreamResult, LanguageModelV3GenerateResult } from "@ai-sdk/provider";
|
|
2
|
+
export interface CommandCodeModelOptions {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseURL?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export declare class CommandCodeLanguageModel implements LanguageModelV3 {
|
|
8
|
+
readonly specificationVersion: "v3";
|
|
9
|
+
readonly provider = "commandcode";
|
|
10
|
+
readonly modelId: string;
|
|
11
|
+
supportedUrls: Record<string, RegExp[]>;
|
|
12
|
+
private opts;
|
|
13
|
+
constructor(modelId: string, opts: CommandCodeModelOptions);
|
|
14
|
+
private get baseURL();
|
|
15
|
+
private buildHeaders;
|
|
16
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
17
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { buildRequest } from "./convert.js";
|
|
2
|
+
import { parseStreamEvents } from "./stream.js";
|
|
3
|
+
const DEFAULT_BASE_URL = "https://api.commandcode.ai";
|
|
4
|
+
// x-command-code-version must match the Command Code CLI version for API compatibility
|
|
5
|
+
const CC_VERSION = "0.26.20";
|
|
6
|
+
export class CommandCodeLanguageModel {
|
|
7
|
+
specificationVersion = "v3";
|
|
8
|
+
provider = "commandcode";
|
|
9
|
+
modelId;
|
|
10
|
+
supportedUrls = {};
|
|
11
|
+
opts;
|
|
12
|
+
constructor(modelId, opts) {
|
|
13
|
+
this.modelId = modelId;
|
|
14
|
+
this.opts = opts;
|
|
15
|
+
}
|
|
16
|
+
get baseURL() {
|
|
17
|
+
return this.opts.baseURL ?? DEFAULT_BASE_URL;
|
|
18
|
+
}
|
|
19
|
+
buildHeaders() {
|
|
20
|
+
return {
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
Authorization: `Bearer ${this.opts.apiKey}`,
|
|
23
|
+
"x-command-code-version": CC_VERSION,
|
|
24
|
+
"x-cli-environment": "production",
|
|
25
|
+
"x-project-slug": "opencode",
|
|
26
|
+
...this.opts.headers,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async doStream(options) {
|
|
30
|
+
const body = buildRequest(this.modelId, options);
|
|
31
|
+
const requestBody = JSON.stringify(body);
|
|
32
|
+
const controller = new AbortController();
|
|
33
|
+
const timeout = setTimeout(() => controller.abort(new Error("Request timed out after 5 minutes")), 300_000);
|
|
34
|
+
const userSignal = options.abortSignal;
|
|
35
|
+
if (userSignal) {
|
|
36
|
+
const onAbort = () => controller.abort(userSignal.reason);
|
|
37
|
+
userSignal.addEventListener("abort", onAbort, { once: true });
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const response = await fetch(`${this.baseURL}/alpha/generate`, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: this.buildHeaders(),
|
|
43
|
+
body: requestBody,
|
|
44
|
+
signal: controller.signal,
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
const errorBody = await response.text().catch(() => "");
|
|
48
|
+
let errorMessage = `Command Code API error: ${response.status} ${response.statusText}`;
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(errorBody);
|
|
51
|
+
if (parsed.error?.message)
|
|
52
|
+
errorMessage = parsed.error.message;
|
|
53
|
+
else if (parsed.message)
|
|
54
|
+
errorMessage = parsed.message;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// intentionally silent: error body is not JSON
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`${errorMessage} [model=${this.modelId}]`);
|
|
60
|
+
}
|
|
61
|
+
if (!response.body) {
|
|
62
|
+
throw new Error(`Command Code API returned no body [model=${this.modelId}]`);
|
|
63
|
+
}
|
|
64
|
+
const responseHeaders = {};
|
|
65
|
+
response.headers.forEach((v, k) => {
|
|
66
|
+
responseHeaders[k] = v;
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
stream: parseStreamEvents(response.body),
|
|
70
|
+
request: { body: requestBody },
|
|
71
|
+
response: { headers: responseHeaders },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
clearTimeout(timeout);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async doGenerate(options) {
|
|
79
|
+
const { stream } = await this.doStream(options);
|
|
80
|
+
const textParts = [];
|
|
81
|
+
const reasoningParts = [];
|
|
82
|
+
const content = [];
|
|
83
|
+
let finishReason = { unified: "stop", raw: "stop" };
|
|
84
|
+
let usage = {
|
|
85
|
+
inputTokens: {
|
|
86
|
+
total: undefined,
|
|
87
|
+
noCache: undefined,
|
|
88
|
+
cacheRead: undefined,
|
|
89
|
+
cacheWrite: undefined,
|
|
90
|
+
},
|
|
91
|
+
outputTokens: { total: undefined, text: undefined, reasoning: undefined },
|
|
92
|
+
};
|
|
93
|
+
const reader = stream.getReader();
|
|
94
|
+
try {
|
|
95
|
+
while (true) {
|
|
96
|
+
const { done, value } = await reader.read();
|
|
97
|
+
if (done)
|
|
98
|
+
break;
|
|
99
|
+
switch (value.type) {
|
|
100
|
+
case "text-delta":
|
|
101
|
+
textParts.push(value.delta);
|
|
102
|
+
break;
|
|
103
|
+
case "reasoning-delta":
|
|
104
|
+
reasoningParts.push(value.delta);
|
|
105
|
+
break;
|
|
106
|
+
case "tool-call":
|
|
107
|
+
content.push({
|
|
108
|
+
type: "tool-call",
|
|
109
|
+
toolCallId: value.toolCallId,
|
|
110
|
+
toolName: value.toolName,
|
|
111
|
+
input: value.input,
|
|
112
|
+
});
|
|
113
|
+
break;
|
|
114
|
+
case "finish":
|
|
115
|
+
finishReason = value.finishReason;
|
|
116
|
+
usage = value.usage;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
reader.releaseLock();
|
|
123
|
+
stream.cancel();
|
|
124
|
+
}
|
|
125
|
+
const text = textParts.join("");
|
|
126
|
+
if (text)
|
|
127
|
+
content.unshift({ type: "text", text });
|
|
128
|
+
const reasoning = reasoningParts.join("");
|
|
129
|
+
if (reasoning)
|
|
130
|
+
content.unshift({ type: "reasoning", text: reasoning });
|
|
131
|
+
return {
|
|
132
|
+
content,
|
|
133
|
+
finishReason,
|
|
134
|
+
usage,
|
|
135
|
+
warnings: [],
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|