@herouucn/opencode-commandcode 0.1.0 → 0.1.1
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/LICENSE +21 -21
- package/_version.txt +1 -1
- package/index.ts +32 -32
- package/manifest.json +34 -34
- package/models.json +2182 -2182
- package/package.json +3 -3
- package/src/auth.ts +43 -43
- package/src/catalog-break.ts +41 -41
- package/src/catalog.ts +769 -769
- package/src/convert.ts +242 -242
- package/src/costs-docs.ts +179 -179
- package/src/costs-models-dev.ts +173 -173
- package/src/manifest.ts +134 -134
- package/src/model.ts +168 -168
- package/src/startup.ts +43 -43
- package/src/stream.ts +237 -237
package/src/costs-docs.ts
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
import type { ModelEntry } from "./catalog.js";
|
|
2
|
-
|
|
3
|
-
export type DocCostRow = {
|
|
4
|
-
name: string;
|
|
5
|
-
id?: string;
|
|
6
|
-
input: number;
|
|
7
|
-
output: number;
|
|
8
|
-
cache_read?: number;
|
|
9
|
-
cache_write?: number;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function parseMoneyCell(cell: string): number | undefined {
|
|
13
|
-
const trimmed = cell.trim();
|
|
14
|
-
if (!trimmed || trimmed === "—" || trimmed === "-") return undefined;
|
|
15
|
-
if (/^free$/i.test(trimmed)) return 0;
|
|
16
|
-
const amounts = [...trimmed.matchAll(/\$([0-9]+(?:\.[0-9]+)?)/g)].map((m) => Number(m[1]));
|
|
17
|
-
if (amounts.length === 0) return undefined;
|
|
18
|
-
return amounts[amounts.length - 1];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type CellRow = { cells: string[]; id?: string };
|
|
22
|
-
|
|
23
|
-
function decodeEntities(text: string): string {
|
|
24
|
-
return text
|
|
25
|
-
.replace(/&/g, "&")
|
|
26
|
-
.replace(/</g, "<")
|
|
27
|
-
.replace(/>/g, ">")
|
|
28
|
-
.replace(/"/g, '"')
|
|
29
|
-
.replace(/'/g, "'")
|
|
30
|
-
.replace(/ /g, " ");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function stripTags(html: string): string {
|
|
34
|
-
return decodeEntities(html.replace(/<[^>]+>/g, " "))
|
|
35
|
-
.replace(/\s+/g, " ")
|
|
36
|
-
.trim();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function visibleCellText(innerHtml: string): string {
|
|
40
|
-
const trunc = innerHtml.match(/<span class="truncate[^"]*"[^>]*>([^<]*)<\/span>/i);
|
|
41
|
-
if (trunc?.[1]) return decodeEntities(trunc[1]).trim();
|
|
42
|
-
return stripTags(innerHtml);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function headerName(cell: string): string {
|
|
46
|
-
return cell.replace(/↕/g, "").replace(/\s+/g, " ").trim().toLowerCase();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function colIndex(headers: string[], name: string): number {
|
|
50
|
-
return headers.findIndex((h) => h === name || h.startsWith(`${name} `) || h.startsWith(name));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function pipeRows(markdown: string): CellRow[] {
|
|
54
|
-
const rows: CellRow[] = [];
|
|
55
|
-
for (const line of markdown.split("\n")) {
|
|
56
|
-
if (!line.includes("|")) continue;
|
|
57
|
-
const cells = line
|
|
58
|
-
.split("|")
|
|
59
|
-
.map((c) => c.trim())
|
|
60
|
-
.filter((c, i, arr) => !(i === 0 && c === "") && !(i === arr.length - 1 && c === ""));
|
|
61
|
-
if (cells.length) rows.push({ cells });
|
|
62
|
-
}
|
|
63
|
-
return rows;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function htmlTableRows(html: string): CellRow[] {
|
|
67
|
-
const rows: CellRow[] = [];
|
|
68
|
-
const tables = html.match(/<table[\s\S]*?<\/table>/gi) ?? [];
|
|
69
|
-
for (const table of tables) {
|
|
70
|
-
const trs = table.match(/<tr[\s\S]*?<\/tr>/gi) ?? [];
|
|
71
|
-
for (const tr of trs) {
|
|
72
|
-
const rawCells = [...tr.matchAll(/<t[dh]\b[\s\S]*?<\/t[dh]>/gi)].map((m) => m[0]);
|
|
73
|
-
if (rawCells.length === 0) continue;
|
|
74
|
-
const cells = rawCells.map((raw) => {
|
|
75
|
-
const inner = raw.replace(/^<t[dh]\b[^>]*>/i, "").replace(/<\/t[dh]>$/i, "");
|
|
76
|
-
return visibleCellText(inner);
|
|
77
|
-
});
|
|
78
|
-
const href = tr.match(/href="\/models\/([^"?#]+)"/i)?.[1];
|
|
79
|
-
rows.push({ cells, ...(href ? { id: href } : {}) });
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return rows;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function rowsFromGroup(group: CellRow[]): DocCostRow[] {
|
|
86
|
-
if (group.length === 0) return [];
|
|
87
|
-
|
|
88
|
-
let modelI = 0;
|
|
89
|
-
let inputI = 2;
|
|
90
|
-
let outputI = 3;
|
|
91
|
-
let cacheReadI = 4;
|
|
92
|
-
let cacheWriteI = 5;
|
|
93
|
-
let start = 0;
|
|
94
|
-
|
|
95
|
-
const headerIdx = group.findIndex((r) => /^model\b/i.test(headerName(r.cells[0] ?? "")));
|
|
96
|
-
if (headerIdx >= 0) {
|
|
97
|
-
const headers = group[headerIdx]!.cells.map(headerName);
|
|
98
|
-
const foundInput = colIndex(headers, "input");
|
|
99
|
-
const foundOutput = colIndex(headers, "output");
|
|
100
|
-
if (foundInput >= 0 && foundOutput >= 0) {
|
|
101
|
-
const foundModel = colIndex(headers, "model");
|
|
102
|
-
if (foundModel >= 0) modelI = foundModel;
|
|
103
|
-
inputI = foundInput;
|
|
104
|
-
outputI = foundOutput;
|
|
105
|
-
cacheReadI = colIndex(headers, "cache read");
|
|
106
|
-
cacheWriteI = colIndex(headers, "cache write");
|
|
107
|
-
start = headerIdx + 1;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const rows: DocCostRow[] = [];
|
|
112
|
-
for (let i = start; i < group.length; i++) {
|
|
113
|
-
const { cells, id } = group[i]!;
|
|
114
|
-
if (cells.length < 2) continue;
|
|
115
|
-
const first = headerName(cells[0] ?? "");
|
|
116
|
-
if (first === "model" || (cells[0] ?? "").startsWith("---")) continue;
|
|
117
|
-
const input = parseMoneyCell(cells[inputI] ?? "");
|
|
118
|
-
const output = parseMoneyCell(cells[outputI] ?? "");
|
|
119
|
-
if (input === undefined || output === undefined) continue;
|
|
120
|
-
const name = (cells[modelI] ?? "").trim();
|
|
121
|
-
if (!name) continue;
|
|
122
|
-
const row: DocCostRow = { name, input, output };
|
|
123
|
-
if (id) row.id = id;
|
|
124
|
-
if (cacheReadI >= 0) {
|
|
125
|
-
const cacheRead = parseMoneyCell(cells[cacheReadI] ?? "");
|
|
126
|
-
if (cacheRead !== undefined) row.cache_read = cacheRead;
|
|
127
|
-
}
|
|
128
|
-
if (cacheWriteI >= 0) {
|
|
129
|
-
const cacheWrite = parseMoneyCell(cells[cacheWriteI] ?? "");
|
|
130
|
-
if (cacheWrite !== undefined) row.cache_write = cacheWrite;
|
|
131
|
-
}
|
|
132
|
-
rows.push(row);
|
|
133
|
-
}
|
|
134
|
-
return rows;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export function parseModelsTable(markdown: string): DocCostRow[] {
|
|
138
|
-
return [...rowsFromGroup(pipeRows(markdown)), ...rowsFromGroup(htmlTableRows(markdown))];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function applyDocCosts(
|
|
142
|
-
models: ModelEntry[],
|
|
143
|
-
rows: DocCostRow[],
|
|
144
|
-
cliIds: Set<string> = new Set(),
|
|
145
|
-
filledIds?: Set<string>,
|
|
146
|
-
): number {
|
|
147
|
-
const byName = new Map(rows.map((r) => [r.name.toLowerCase(), r]));
|
|
148
|
-
const byId = new Map(rows.filter((r) => r.id).map((r) => [r.id!.toLowerCase(), r]));
|
|
149
|
-
let filled = 0;
|
|
150
|
-
for (const model of models) {
|
|
151
|
-
if (cliIds.has(model.id)) continue;
|
|
152
|
-
const row = byId.get(model.id.toLowerCase()) ?? byName.get(model.name.toLowerCase());
|
|
153
|
-
if (!row) continue;
|
|
154
|
-
model.cost = { input: row.input, output: row.output };
|
|
155
|
-
if (row.cache_read !== undefined) model.cost.cache_read = row.cache_read;
|
|
156
|
-
if (row.cache_write !== undefined) model.cost.cache_write = row.cache_write;
|
|
157
|
-
filledIds?.add(model.id);
|
|
158
|
-
filled++;
|
|
159
|
-
}
|
|
160
|
-
return filled;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export async function fetchOfficialModelsMarkdown(): Promise<string> {
|
|
164
|
-
const urls = [
|
|
165
|
-
"https://commandcode.ai/models",
|
|
166
|
-
"https://commandcode.ai/docs/resources/pricing-limits",
|
|
167
|
-
];
|
|
168
|
-
for (const url of urls) {
|
|
169
|
-
try {
|
|
170
|
-
const resp = await fetch(url);
|
|
171
|
-
if (!resp.ok) continue;
|
|
172
|
-
const text = await resp.text();
|
|
173
|
-
if (parseModelsTable(text).length > 0) return text;
|
|
174
|
-
} catch {
|
|
175
|
-
// try next
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return "";
|
|
179
|
-
}
|
|
1
|
+
import type { ModelEntry } from "./catalog.js";
|
|
2
|
+
|
|
3
|
+
export type DocCostRow = {
|
|
4
|
+
name: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
input: number;
|
|
7
|
+
output: number;
|
|
8
|
+
cache_read?: number;
|
|
9
|
+
cache_write?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function parseMoneyCell(cell: string): number | undefined {
|
|
13
|
+
const trimmed = cell.trim();
|
|
14
|
+
if (!trimmed || trimmed === "—" || trimmed === "-") return undefined;
|
|
15
|
+
if (/^free$/i.test(trimmed)) return 0;
|
|
16
|
+
const amounts = [...trimmed.matchAll(/\$([0-9]+(?:\.[0-9]+)?)/g)].map((m) => Number(m[1]));
|
|
17
|
+
if (amounts.length === 0) return undefined;
|
|
18
|
+
return amounts[amounts.length - 1];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type CellRow = { cells: string[]; id?: string };
|
|
22
|
+
|
|
23
|
+
function decodeEntities(text: string): string {
|
|
24
|
+
return text
|
|
25
|
+
.replace(/&/g, "&")
|
|
26
|
+
.replace(/</g, "<")
|
|
27
|
+
.replace(/>/g, ">")
|
|
28
|
+
.replace(/"/g, '"')
|
|
29
|
+
.replace(/'/g, "'")
|
|
30
|
+
.replace(/ /g, " ");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function stripTags(html: string): string {
|
|
34
|
+
return decodeEntities(html.replace(/<[^>]+>/g, " "))
|
|
35
|
+
.replace(/\s+/g, " ")
|
|
36
|
+
.trim();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function visibleCellText(innerHtml: string): string {
|
|
40
|
+
const trunc = innerHtml.match(/<span class="truncate[^"]*"[^>]*>([^<]*)<\/span>/i);
|
|
41
|
+
if (trunc?.[1]) return decodeEntities(trunc[1]).trim();
|
|
42
|
+
return stripTags(innerHtml);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function headerName(cell: string): string {
|
|
46
|
+
return cell.replace(/↕/g, "").replace(/\s+/g, " ").trim().toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function colIndex(headers: string[], name: string): number {
|
|
50
|
+
return headers.findIndex((h) => h === name || h.startsWith(`${name} `) || h.startsWith(name));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pipeRows(markdown: string): CellRow[] {
|
|
54
|
+
const rows: CellRow[] = [];
|
|
55
|
+
for (const line of markdown.split("\n")) {
|
|
56
|
+
if (!line.includes("|")) continue;
|
|
57
|
+
const cells = line
|
|
58
|
+
.split("|")
|
|
59
|
+
.map((c) => c.trim())
|
|
60
|
+
.filter((c, i, arr) => !(i === 0 && c === "") && !(i === arr.length - 1 && c === ""));
|
|
61
|
+
if (cells.length) rows.push({ cells });
|
|
62
|
+
}
|
|
63
|
+
return rows;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function htmlTableRows(html: string): CellRow[] {
|
|
67
|
+
const rows: CellRow[] = [];
|
|
68
|
+
const tables = html.match(/<table[\s\S]*?<\/table>/gi) ?? [];
|
|
69
|
+
for (const table of tables) {
|
|
70
|
+
const trs = table.match(/<tr[\s\S]*?<\/tr>/gi) ?? [];
|
|
71
|
+
for (const tr of trs) {
|
|
72
|
+
const rawCells = [...tr.matchAll(/<t[dh]\b[\s\S]*?<\/t[dh]>/gi)].map((m) => m[0]);
|
|
73
|
+
if (rawCells.length === 0) continue;
|
|
74
|
+
const cells = rawCells.map((raw) => {
|
|
75
|
+
const inner = raw.replace(/^<t[dh]\b[^>]*>/i, "").replace(/<\/t[dh]>$/i, "");
|
|
76
|
+
return visibleCellText(inner);
|
|
77
|
+
});
|
|
78
|
+
const href = tr.match(/href="\/models\/([^"?#]+)"/i)?.[1];
|
|
79
|
+
rows.push({ cells, ...(href ? { id: href } : {}) });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return rows;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function rowsFromGroup(group: CellRow[]): DocCostRow[] {
|
|
86
|
+
if (group.length === 0) return [];
|
|
87
|
+
|
|
88
|
+
let modelI = 0;
|
|
89
|
+
let inputI = 2;
|
|
90
|
+
let outputI = 3;
|
|
91
|
+
let cacheReadI = 4;
|
|
92
|
+
let cacheWriteI = 5;
|
|
93
|
+
let start = 0;
|
|
94
|
+
|
|
95
|
+
const headerIdx = group.findIndex((r) => /^model\b/i.test(headerName(r.cells[0] ?? "")));
|
|
96
|
+
if (headerIdx >= 0) {
|
|
97
|
+
const headers = group[headerIdx]!.cells.map(headerName);
|
|
98
|
+
const foundInput = colIndex(headers, "input");
|
|
99
|
+
const foundOutput = colIndex(headers, "output");
|
|
100
|
+
if (foundInput >= 0 && foundOutput >= 0) {
|
|
101
|
+
const foundModel = colIndex(headers, "model");
|
|
102
|
+
if (foundModel >= 0) modelI = foundModel;
|
|
103
|
+
inputI = foundInput;
|
|
104
|
+
outputI = foundOutput;
|
|
105
|
+
cacheReadI = colIndex(headers, "cache read");
|
|
106
|
+
cacheWriteI = colIndex(headers, "cache write");
|
|
107
|
+
start = headerIdx + 1;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const rows: DocCostRow[] = [];
|
|
112
|
+
for (let i = start; i < group.length; i++) {
|
|
113
|
+
const { cells, id } = group[i]!;
|
|
114
|
+
if (cells.length < 2) continue;
|
|
115
|
+
const first = headerName(cells[0] ?? "");
|
|
116
|
+
if (first === "model" || (cells[0] ?? "").startsWith("---")) continue;
|
|
117
|
+
const input = parseMoneyCell(cells[inputI] ?? "");
|
|
118
|
+
const output = parseMoneyCell(cells[outputI] ?? "");
|
|
119
|
+
if (input === undefined || output === undefined) continue;
|
|
120
|
+
const name = (cells[modelI] ?? "").trim();
|
|
121
|
+
if (!name) continue;
|
|
122
|
+
const row: DocCostRow = { name, input, output };
|
|
123
|
+
if (id) row.id = id;
|
|
124
|
+
if (cacheReadI >= 0) {
|
|
125
|
+
const cacheRead = parseMoneyCell(cells[cacheReadI] ?? "");
|
|
126
|
+
if (cacheRead !== undefined) row.cache_read = cacheRead;
|
|
127
|
+
}
|
|
128
|
+
if (cacheWriteI >= 0) {
|
|
129
|
+
const cacheWrite = parseMoneyCell(cells[cacheWriteI] ?? "");
|
|
130
|
+
if (cacheWrite !== undefined) row.cache_write = cacheWrite;
|
|
131
|
+
}
|
|
132
|
+
rows.push(row);
|
|
133
|
+
}
|
|
134
|
+
return rows;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function parseModelsTable(markdown: string): DocCostRow[] {
|
|
138
|
+
return [...rowsFromGroup(pipeRows(markdown)), ...rowsFromGroup(htmlTableRows(markdown))];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function applyDocCosts(
|
|
142
|
+
models: ModelEntry[],
|
|
143
|
+
rows: DocCostRow[],
|
|
144
|
+
cliIds: Set<string> = new Set(),
|
|
145
|
+
filledIds?: Set<string>,
|
|
146
|
+
): number {
|
|
147
|
+
const byName = new Map(rows.map((r) => [r.name.toLowerCase(), r]));
|
|
148
|
+
const byId = new Map(rows.filter((r) => r.id).map((r) => [r.id!.toLowerCase(), r]));
|
|
149
|
+
let filled = 0;
|
|
150
|
+
for (const model of models) {
|
|
151
|
+
if (cliIds.has(model.id)) continue;
|
|
152
|
+
const row = byId.get(model.id.toLowerCase()) ?? byName.get(model.name.toLowerCase());
|
|
153
|
+
if (!row) continue;
|
|
154
|
+
model.cost = { input: row.input, output: row.output };
|
|
155
|
+
if (row.cache_read !== undefined) model.cost.cache_read = row.cache_read;
|
|
156
|
+
if (row.cache_write !== undefined) model.cost.cache_write = row.cache_write;
|
|
157
|
+
filledIds?.add(model.id);
|
|
158
|
+
filled++;
|
|
159
|
+
}
|
|
160
|
+
return filled;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function fetchOfficialModelsMarkdown(): Promise<string> {
|
|
164
|
+
const urls = [
|
|
165
|
+
"https://commandcode.ai/models",
|
|
166
|
+
"https://commandcode.ai/docs/resources/pricing-limits",
|
|
167
|
+
];
|
|
168
|
+
for (const url of urls) {
|
|
169
|
+
try {
|
|
170
|
+
const resp = await fetch(url);
|
|
171
|
+
if (!resp.ok) continue;
|
|
172
|
+
const text = await resp.text();
|
|
173
|
+
if (parseModelsTable(text).length > 0) return text;
|
|
174
|
+
} catch {
|
|
175
|
+
// try next
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return "";
|
|
179
|
+
}
|