@telsoncc/opencode-commandcode 1.2.33 → 1.2.35
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 -222
- 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 +3447 -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 +155 -42
- 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,179 @@
|
|
|
1
|
+
import { type ModelEntry } from "./catalog.js";
|
|
2
|
+
export declare const COMMANDCODE_PROVIDER_ID = "commandcode";
|
|
3
|
+
export declare const COMMANDCODE_INTEGRATION_ID = "commandcode";
|
|
4
|
+
export declare const COMMANDCODE_PROVIDER_NAME = "Command Code";
|
|
5
|
+
export declare const COMMANDCODE_BASE_URL = "https://api.commandcode.ai/provider/v1";
|
|
6
|
+
export declare const COMMANDCODE_PACKAGE = "@opencode/ai/providers/openai-compatible";
|
|
7
|
+
/**
|
|
8
|
+
* Anthropic Messages runtime. Models the gateway serves only on `/messages`
|
|
9
|
+
* cannot go through the OpenAI-compatible runtime, so they carry this package
|
|
10
|
+
* on the model entry and it overrides the provider-wide default.
|
|
11
|
+
*/
|
|
12
|
+
export declare const COMMANDCODE_ANTHROPIC_PACKAGE = "@opencode/ai/providers/anthropic-compatible";
|
|
13
|
+
export declare const COMMANDCODE_ENV_NAMES: string[];
|
|
14
|
+
export type V2Cost = {
|
|
15
|
+
tier?: {
|
|
16
|
+
type: "context";
|
|
17
|
+
size: number;
|
|
18
|
+
};
|
|
19
|
+
input: number;
|
|
20
|
+
output: number;
|
|
21
|
+
cache: {
|
|
22
|
+
read: number;
|
|
23
|
+
write: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export type V2Variant = {
|
|
27
|
+
id: string;
|
|
28
|
+
settings?: Record<string, unknown>;
|
|
29
|
+
};
|
|
30
|
+
export type V2ModelInfo = {
|
|
31
|
+
id: string;
|
|
32
|
+
/** Wire id sent to the provider. Lets one upstream model back one entry. */
|
|
33
|
+
modelID: string;
|
|
34
|
+
providerID: string;
|
|
35
|
+
/**
|
|
36
|
+
* Provider runtime for this model. Overrides the provider-wide `package`
|
|
37
|
+
* — Anthropic Messages models are rejected by the OpenAI-compatible
|
|
38
|
+
* runtime, so they carry `@opencode/ai/providers/anthropic-compatible`.
|
|
39
|
+
*/
|
|
40
|
+
package?: string;
|
|
41
|
+
name: string;
|
|
42
|
+
capabilities: {
|
|
43
|
+
tools: boolean;
|
|
44
|
+
input: string[];
|
|
45
|
+
output: string[];
|
|
46
|
+
};
|
|
47
|
+
limit: {
|
|
48
|
+
context: number;
|
|
49
|
+
output: number;
|
|
50
|
+
};
|
|
51
|
+
variants: V2Variant[];
|
|
52
|
+
status: "active";
|
|
53
|
+
enabled: true;
|
|
54
|
+
time: {
|
|
55
|
+
released: number;
|
|
56
|
+
};
|
|
57
|
+
cost: V2Cost[];
|
|
58
|
+
settings?: Record<string, unknown>;
|
|
59
|
+
};
|
|
60
|
+
export type V2ProviderInfo = {
|
|
61
|
+
id: string;
|
|
62
|
+
name: string;
|
|
63
|
+
activation: "enabled";
|
|
64
|
+
package: string;
|
|
65
|
+
integrationID: string;
|
|
66
|
+
settings: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
export interface V2ProviderEditor {
|
|
69
|
+
add(input: {
|
|
70
|
+
info: V2ProviderInfo;
|
|
71
|
+
models: readonly V2ModelInfo[];
|
|
72
|
+
sourceConnection?: unknown;
|
|
73
|
+
}): void;
|
|
74
|
+
}
|
|
75
|
+
export interface V2IntegrationDraft {
|
|
76
|
+
update(id: string, update: (integration: {
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
}) => void): void;
|
|
80
|
+
readonly method: {
|
|
81
|
+
update(input: {
|
|
82
|
+
readonly integrationID: string;
|
|
83
|
+
readonly method: {
|
|
84
|
+
type: "key";
|
|
85
|
+
label?: string;
|
|
86
|
+
};
|
|
87
|
+
} | {
|
|
88
|
+
readonly integrationID: string;
|
|
89
|
+
readonly method: {
|
|
90
|
+
type: "env";
|
|
91
|
+
names: string[];
|
|
92
|
+
};
|
|
93
|
+
}): void;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export interface V2SetupContext {
|
|
97
|
+
readonly provider: {
|
|
98
|
+
readonly transform: (callback: (editor: V2ProviderEditor) => void) => Promise<{
|
|
99
|
+
dispose: () => Promise<void>;
|
|
100
|
+
}>;
|
|
101
|
+
readonly reload: () => Promise<void>;
|
|
102
|
+
};
|
|
103
|
+
readonly integration: {
|
|
104
|
+
readonly transform: (callback: (draft: V2IntegrationDraft) => void) => Promise<{
|
|
105
|
+
dispose: () => Promise<void>;
|
|
106
|
+
}>;
|
|
107
|
+
readonly connection: {
|
|
108
|
+
readonly active: (integrationID: string) => Promise<unknown>;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
readonly event?: {
|
|
112
|
+
readonly subscribe: (...args: unknown[]) => AsyncIterable<{
|
|
113
|
+
type?: unknown;
|
|
114
|
+
}>;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Provider runtime for one model: Anthropic Messages models need the
|
|
119
|
+
* anthropic-compatible runtime, everything else the OpenAI-compatible one.
|
|
120
|
+
* The provider-wide `package` stays the OpenAI default and is only a fallback
|
|
121
|
+
* for entries that carry no `package` of their own.
|
|
122
|
+
*/
|
|
123
|
+
export declare function modelPackage(entry: ModelEntry): string;
|
|
124
|
+
export declare function modelEntryToV2(entry: ModelEntry): V2ModelInfo;
|
|
125
|
+
export declare function buildV2Models(entries: ModelEntry[]): V2ModelInfo[];
|
|
126
|
+
export declare function buildProviderInfo(): V2ProviderInfo;
|
|
127
|
+
/**
|
|
128
|
+
* Publish the catalog into the live provider inventory. A no-op while empty
|
|
129
|
+
* so a failed load keeps the last successful inventory (same convention as
|
|
130
|
+
* the cursor provider plugin).
|
|
131
|
+
*/
|
|
132
|
+
export declare function applyProviderInventory(editor: V2ProviderEditor, models: ModelEntry[], sourceConnection?: unknown): void;
|
|
133
|
+
/** Register the `commandcode` integration: API-key entry plus env fallback. */
|
|
134
|
+
export declare function applyIntegration(draft: V2IntegrationDraft): void;
|
|
135
|
+
export type V2CatalogSource = "bundled" | "cache" | "opt-in-local" | "remote";
|
|
136
|
+
export interface LoadedV2Catalog {
|
|
137
|
+
models: ModelEntry[];
|
|
138
|
+
source: V2CatalogSource;
|
|
139
|
+
commandCodeVersion: string | null;
|
|
140
|
+
degraded: boolean;
|
|
141
|
+
degradedReason: string | null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Catalog resolution shared with the V1 `config` hook in `plugin.ts`:
|
|
145
|
+
* opt-in local `command-code` scrape first, then the bundled `models.json`,
|
|
146
|
+
* then the last-good cache. Always refreshes the cache and `startup.json`
|
|
147
|
+
* when models resolve. Keep the two in sync when this changes.
|
|
148
|
+
*/
|
|
149
|
+
export declare function loadCatalogForV2(): LoadedV2Catalog;
|
|
150
|
+
export declare const REMOTE_SYNC_TIMEOUT_MS = 15000;
|
|
151
|
+
export declare const REMOTE_SYNC_INTERVAL_MS: number;
|
|
152
|
+
export interface RemoteRefreshResult {
|
|
153
|
+
models: ModelEntry[];
|
|
154
|
+
unavailable: string[];
|
|
155
|
+
pendingNew: string[];
|
|
156
|
+
remoteCount: number;
|
|
157
|
+
/** Provisional entries published with models.dev estimates. */
|
|
158
|
+
estimated: ModelEntry[];
|
|
159
|
+
}
|
|
160
|
+
/** Live availability ids from the Provider API. No auth required. */
|
|
161
|
+
export declare function fetchRemoteAvailability(timeoutMs?: number): Promise<string[]>;
|
|
162
|
+
/**
|
|
163
|
+
* Reconcile a local catalog with live availability: keep the ids the API
|
|
164
|
+
* still serves, report retired ones, and list remote ids missing locally.
|
|
165
|
+
* Missing ids stay pending here — refreshCatalogFromRemote tries to publish
|
|
166
|
+
* them with models.dev estimates first.
|
|
167
|
+
*/
|
|
168
|
+
export declare function applyRemoteAvailability(base: ModelEntry[], remoteIds: string[]): RemoteRefreshResult;
|
|
169
|
+
export declare function refreshCatalogFromRemote(base: ModelEntry[], timeoutMs?: number): Promise<RemoteRefreshResult>;
|
|
170
|
+
export declare function isRemoteSyncDisabled(): boolean;
|
|
171
|
+
/** Overwrite the last-good disk cache with a refreshed inventory. */
|
|
172
|
+
export declare function persistRefreshedModels(models: ModelEntry[]): void;
|
|
173
|
+
export declare function writeRemoteRefreshSummary(result: RemoteRefreshResult, commandCodeVersion: string | null): void;
|
|
174
|
+
export declare function remoteRefreshRunnerCount(): number;
|
|
175
|
+
/**
|
|
176
|
+
* Share one interval across setups in the process. Returns a detach
|
|
177
|
+
* function; the timer stops after the last runner detaches.
|
|
178
|
+
*/
|
|
179
|
+
export declare function attachRemoteRefreshRunner(run: () => Promise<void>, intervalMs?: number): () => void;
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { join, dirname } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { MODELS_API_URL, filterCatalogByAvailability, modelTransport, parseAvailabilityIds, toConfigKey, loadCatalogFromLocalCommandCode, } from "./catalog.js";
|
|
6
|
+
import { buildEstimatedEntry, loadModelsDevRows } from "./costs-models-dev.js";
|
|
7
|
+
import { readCatalogCache, writeCatalogCache, writeStartupSummary, pluginStateDir, } from "./startup.js";
|
|
8
|
+
// OpenCode 2.0 support: in-memory provider inventory + integration.
|
|
9
|
+
//
|
|
10
|
+
// The 2.0 host API is duck-typed on purpose (mirrors
|
|
11
|
+
// oakimov/cursor-opencode-provider `src/opencode2/types.ts`): importing
|
|
12
|
+
// `@opencode/plugin` as a runtime dependency would force a plugin bump on
|
|
13
|
+
// every OpenCode release. These shapes only cover what this plugin publishes
|
|
14
|
+
// or calls; extra host fields are ignored at runtime.
|
|
15
|
+
export const COMMANDCODE_PROVIDER_ID = "commandcode";
|
|
16
|
+
export const COMMANDCODE_INTEGRATION_ID = "commandcode";
|
|
17
|
+
export const COMMANDCODE_PROVIDER_NAME = "Command Code";
|
|
18
|
+
export const COMMANDCODE_BASE_URL = "https://api.commandcode.ai/provider/v1";
|
|
19
|
+
export const COMMANDCODE_PACKAGE = "@opencode/ai/providers/openai-compatible";
|
|
20
|
+
/**
|
|
21
|
+
* Anthropic Messages runtime. Models the gateway serves only on `/messages`
|
|
22
|
+
* cannot go through the OpenAI-compatible runtime, so they carry this package
|
|
23
|
+
* on the model entry and it overrides the provider-wide default.
|
|
24
|
+
*/
|
|
25
|
+
export const COMMANDCODE_ANTHROPIC_PACKAGE = "@opencode/ai/providers/anthropic-compatible";
|
|
26
|
+
export const COMMANDCODE_ENV_NAMES = ["COMMANDCODE_API_KEY"];
|
|
27
|
+
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
// Repo layout: src/../ is the package root. Published builds: dist/src/../ is
|
|
29
|
+
// dist/ (catalog copied at build time) with the package root one level above.
|
|
30
|
+
function resolveAsset(name) {
|
|
31
|
+
for (const dir of [join(MODULE_DIR, ".."), join(MODULE_DIR, "..", "..")]) {
|
|
32
|
+
const candidate = join(dir, name);
|
|
33
|
+
if (existsSync(candidate))
|
|
34
|
+
return candidate;
|
|
35
|
+
}
|
|
36
|
+
return join(MODULE_DIR, "..", name);
|
|
37
|
+
}
|
|
38
|
+
const MODELS_PATH = resolveAsset("models.json");
|
|
39
|
+
const VERSION_PATH = resolveAsset("_version.txt");
|
|
40
|
+
const MANIFEST_PATH = resolveAsset("manifest.json");
|
|
41
|
+
function loadPluginFileConfig() {
|
|
42
|
+
const dir = join(homedir(), ".config", "opencode");
|
|
43
|
+
const configPath = [
|
|
44
|
+
join(dir, "opencode-commandcode.json"),
|
|
45
|
+
join(dir, "commandcode-go-opencode-provider.json"),
|
|
46
|
+
].find((p) => existsSync(p));
|
|
47
|
+
if (!configPath)
|
|
48
|
+
return {};
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(readFileSync(configPath, "utf-8"));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Provider runtime for one model: Anthropic Messages models need the
|
|
58
|
+
* anthropic-compatible runtime, everything else the OpenAI-compatible one.
|
|
59
|
+
* The provider-wide `package` stays the OpenAI default and is only a fallback
|
|
60
|
+
* for entries that carry no `package` of their own.
|
|
61
|
+
*/
|
|
62
|
+
export function modelPackage(entry) {
|
|
63
|
+
return modelTransport(entry) === "messages" ? COMMANDCODE_ANTHROPIC_PACKAGE : COMMANDCODE_PACKAGE;
|
|
64
|
+
}
|
|
65
|
+
export function modelEntryToV2(entry) {
|
|
66
|
+
const rawInput = entry.modalities?.input?.filter((m) => typeof m === "string" && m.length > 0) ?? ["text"];
|
|
67
|
+
const input = rawInput.length > 0 ? [...rawInput] : ["text"];
|
|
68
|
+
const variants = (entry.reasoningEfforts ?? []).map((effort) => ({
|
|
69
|
+
id: effort,
|
|
70
|
+
settings: { reasoningEffort: effort },
|
|
71
|
+
}));
|
|
72
|
+
return {
|
|
73
|
+
id: toConfigKey(entry.id),
|
|
74
|
+
modelID: entry.id,
|
|
75
|
+
providerID: COMMANDCODE_PROVIDER_ID,
|
|
76
|
+
package: modelPackage(entry),
|
|
77
|
+
name: entry.name,
|
|
78
|
+
capabilities: { tools: entry.tool_call, input, output: ["text"] },
|
|
79
|
+
limit: { context: entry.limit.context, output: entry.limit.output },
|
|
80
|
+
variants,
|
|
81
|
+
status: "active",
|
|
82
|
+
enabled: true,
|
|
83
|
+
// The bundled catalog carries no release dates; 0 sorts these last in the
|
|
84
|
+
// picker, so filter by provider "Command Code" (same convention as the
|
|
85
|
+
// cursor provider plugin).
|
|
86
|
+
time: { released: 0 },
|
|
87
|
+
cost: [
|
|
88
|
+
{
|
|
89
|
+
input: entry.cost.input,
|
|
90
|
+
output: entry.cost.output,
|
|
91
|
+
cache: { read: entry.cost.cache_read ?? 0, write: entry.cost.cache_write ?? 0 },
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export function buildV2Models(entries) {
|
|
97
|
+
return entries.map(modelEntryToV2);
|
|
98
|
+
}
|
|
99
|
+
export function buildProviderInfo() {
|
|
100
|
+
return {
|
|
101
|
+
id: COMMANDCODE_PROVIDER_ID,
|
|
102
|
+
name: COMMANDCODE_PROVIDER_NAME,
|
|
103
|
+
activation: "enabled",
|
|
104
|
+
package: COMMANDCODE_PACKAGE,
|
|
105
|
+
integrationID: COMMANDCODE_INTEGRATION_ID,
|
|
106
|
+
settings: { baseURL: COMMANDCODE_BASE_URL },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Publish the catalog into the live provider inventory. A no-op while empty
|
|
111
|
+
* so a failed load keeps the last successful inventory (same convention as
|
|
112
|
+
* the cursor provider plugin).
|
|
113
|
+
*/
|
|
114
|
+
export function applyProviderInventory(editor, models, sourceConnection) {
|
|
115
|
+
if (models.length === 0)
|
|
116
|
+
return;
|
|
117
|
+
editor.add({
|
|
118
|
+
info: buildProviderInfo(),
|
|
119
|
+
models: buildV2Models(models),
|
|
120
|
+
...(sourceConnection === undefined ? {} : { sourceConnection }),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/** Register the `commandcode` integration: API-key entry plus env fallback. */
|
|
124
|
+
export function applyIntegration(draft) {
|
|
125
|
+
draft.update(COMMANDCODE_INTEGRATION_ID, (integration) => {
|
|
126
|
+
integration.id = COMMANDCODE_INTEGRATION_ID;
|
|
127
|
+
integration.name = COMMANDCODE_PROVIDER_NAME;
|
|
128
|
+
});
|
|
129
|
+
draft.method.update({
|
|
130
|
+
integrationID: COMMANDCODE_INTEGRATION_ID,
|
|
131
|
+
method: { type: "key", label: "API Key" },
|
|
132
|
+
});
|
|
133
|
+
draft.method.update({
|
|
134
|
+
integrationID: COMMANDCODE_INTEGRATION_ID,
|
|
135
|
+
method: { type: "env", names: [...COMMANDCODE_ENV_NAMES] },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function loadBundledModels() {
|
|
139
|
+
try {
|
|
140
|
+
const parsed = JSON.parse(readFileSync(MODELS_PATH, "utf-8"));
|
|
141
|
+
if (!Array.isArray(parsed) || parsed.length === 0)
|
|
142
|
+
return null;
|
|
143
|
+
return parsed;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function readBundledManifest() {
|
|
150
|
+
if (!existsSync(MANIFEST_PATH))
|
|
151
|
+
return null;
|
|
152
|
+
try {
|
|
153
|
+
return JSON.parse(readFileSync(MANIFEST_PATH, "utf-8"));
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function readBundledVersion() {
|
|
160
|
+
const manifest = readBundledManifest();
|
|
161
|
+
if (manifest?.commandCodeVersion)
|
|
162
|
+
return manifest.commandCodeVersion;
|
|
163
|
+
if (!existsSync(VERSION_PATH))
|
|
164
|
+
return null;
|
|
165
|
+
try {
|
|
166
|
+
const first = readFileSync(VERSION_PATH, "utf-8").split("\n")[0]?.trim();
|
|
167
|
+
return first || null;
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Catalog resolution shared with the V1 `config` hook in `plugin.ts`:
|
|
175
|
+
* opt-in local `command-code` scrape first, then the bundled `models.json`,
|
|
176
|
+
* then the last-good cache. Always refreshes the cache and `startup.json`
|
|
177
|
+
* when models resolve. Keep the two in sync when this changes.
|
|
178
|
+
*/
|
|
179
|
+
export function loadCatalogForV2() {
|
|
180
|
+
const pluginCfg = loadPluginFileConfig();
|
|
181
|
+
const debug = pluginCfg.debugStartupLogs === true;
|
|
182
|
+
const override = pluginCfg.commandCodePackagePath?.trim() || process.env.COMMANDCODE_PACKAGE_PATH?.trim() || "";
|
|
183
|
+
let models = [];
|
|
184
|
+
// Mirrors the V1 `config` hook: stays "bundled" when nothing resolves.
|
|
185
|
+
let source = "bundled";
|
|
186
|
+
let commandCodeVersion = null;
|
|
187
|
+
let degraded = false;
|
|
188
|
+
let degradedReason = null;
|
|
189
|
+
if (override) {
|
|
190
|
+
const localCatalog = loadCatalogFromLocalCommandCode({ packagePath: override });
|
|
191
|
+
if (localCatalog && localCatalog.models.length > 0) {
|
|
192
|
+
models = localCatalog.models;
|
|
193
|
+
source = "opt-in-local";
|
|
194
|
+
commandCodeVersion = localCatalog.version;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (models.length === 0) {
|
|
198
|
+
const bundled = loadBundledModels();
|
|
199
|
+
if (bundled) {
|
|
200
|
+
models = bundled;
|
|
201
|
+
source = "bundled";
|
|
202
|
+
commandCodeVersion = readBundledVersion();
|
|
203
|
+
const manifest = readBundledManifest();
|
|
204
|
+
if (manifest?.status === "degraded" || manifest?.status === "broken") {
|
|
205
|
+
degraded = true;
|
|
206
|
+
degradedReason =
|
|
207
|
+
manifest.status === "broken"
|
|
208
|
+
? "bundled catalog marked broken"
|
|
209
|
+
: "bundled catalog has models with no listed price";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
const cached = readCatalogCache();
|
|
214
|
+
if (cached) {
|
|
215
|
+
models = cached;
|
|
216
|
+
source = "cache";
|
|
217
|
+
degraded = true;
|
|
218
|
+
degradedReason = "bundled models.json unreadable; using last-good cache";
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
degraded = true;
|
|
222
|
+
degradedReason = "no bundled catalog and no cache";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (models.length > 0) {
|
|
227
|
+
try {
|
|
228
|
+
writeCatalogCache(pluginStateDir(), models);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// ignore cache write
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const summary = {
|
|
235
|
+
catalogSource: source,
|
|
236
|
+
commandCodeVersion,
|
|
237
|
+
modelCount: models.length,
|
|
238
|
+
reasoningModelCount: models.filter((m) => m.reasoning).length,
|
|
239
|
+
degraded,
|
|
240
|
+
degradedReason,
|
|
241
|
+
};
|
|
242
|
+
try {
|
|
243
|
+
writeStartupSummary(pluginStateDir(), summary);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// ignore
|
|
247
|
+
}
|
|
248
|
+
if (debug) {
|
|
249
|
+
console.warn("[commandcode]", JSON.stringify(summary));
|
|
250
|
+
}
|
|
251
|
+
return { models, source, commandCodeVersion, degraded, degradedReason };
|
|
252
|
+
}
|
|
253
|
+
export const REMOTE_SYNC_TIMEOUT_MS = 15_000;
|
|
254
|
+
export const REMOTE_SYNC_INTERVAL_MS = 6 * 3600_000;
|
|
255
|
+
/** Live availability ids from the Provider API. No auth required. */
|
|
256
|
+
export async function fetchRemoteAvailability(timeoutMs = REMOTE_SYNC_TIMEOUT_MS) {
|
|
257
|
+
const response = await fetch(MODELS_API_URL, {
|
|
258
|
+
headers: { accept: "application/json" },
|
|
259
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
260
|
+
});
|
|
261
|
+
if (!response.ok) {
|
|
262
|
+
throw new Error(`availability request failed: HTTP ${response.status}`);
|
|
263
|
+
}
|
|
264
|
+
return parseAvailabilityIds(await response.json());
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Reconcile a local catalog with live availability: keep the ids the API
|
|
268
|
+
* still serves, report retired ones, and list remote ids missing locally.
|
|
269
|
+
* Missing ids stay pending here — refreshCatalogFromRemote tries to publish
|
|
270
|
+
* them with models.dev estimates first.
|
|
271
|
+
*/
|
|
272
|
+
export function applyRemoteAvailability(base, remoteIds) {
|
|
273
|
+
const { retained, unavailable } = filterCatalogByAvailability(base, remoteIds);
|
|
274
|
+
const known = new Set(base.map((m) => m.id));
|
|
275
|
+
const pendingNew = [...new Set(remoteIds)].filter((id) => !known.has(id)).sort();
|
|
276
|
+
return {
|
|
277
|
+
models: retained,
|
|
278
|
+
unavailable,
|
|
279
|
+
pendingNew,
|
|
280
|
+
remoteCount: remoteIds.length,
|
|
281
|
+
estimated: [],
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
export async function refreshCatalogFromRemote(base, timeoutMs = REMOTE_SYNC_TIMEOUT_MS) {
|
|
285
|
+
const result = applyRemoteAvailability(base, await fetchRemoteAvailability(timeoutMs));
|
|
286
|
+
if (result.pendingNew.length === 0)
|
|
287
|
+
return result;
|
|
288
|
+
let rows = [];
|
|
289
|
+
try {
|
|
290
|
+
rows = await loadModelsDevRows();
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
if (rows.length === 0)
|
|
296
|
+
return result;
|
|
297
|
+
const usedKeys = new Set(base.map((m) => toConfigKey(m.id)));
|
|
298
|
+
const estimated = [];
|
|
299
|
+
const stillPending = [];
|
|
300
|
+
for (const id of result.pendingNew) {
|
|
301
|
+
const key = toConfigKey(id);
|
|
302
|
+
if (usedKeys.has(key)) {
|
|
303
|
+
stillPending.push(id);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const entry = buildEstimatedEntry(id, rows);
|
|
307
|
+
if (!entry) {
|
|
308
|
+
stillPending.push(id);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
usedKeys.add(key);
|
|
312
|
+
estimated.push(entry);
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
models: [...result.models, ...estimated],
|
|
316
|
+
unavailable: result.unavailable,
|
|
317
|
+
pendingNew: stillPending,
|
|
318
|
+
remoteCount: result.remoteCount,
|
|
319
|
+
estimated,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
export function isRemoteSyncDisabled() {
|
|
323
|
+
return loadPluginFileConfig().disableModelSync === true;
|
|
324
|
+
}
|
|
325
|
+
/** Overwrite the last-good disk cache with a refreshed inventory. */
|
|
326
|
+
export function persistRefreshedModels(models) {
|
|
327
|
+
if (models.length === 0)
|
|
328
|
+
return;
|
|
329
|
+
try {
|
|
330
|
+
writeCatalogCache(pluginStateDir(), models);
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
// ignore cache write
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
export function writeRemoteRefreshSummary(result, commandCodeVersion) {
|
|
337
|
+
const summary = {
|
|
338
|
+
catalogSource: "remote",
|
|
339
|
+
commandCodeVersion,
|
|
340
|
+
modelCount: result.models.length,
|
|
341
|
+
reasoningModelCount: result.models.filter((m) => m.reasoning).length,
|
|
342
|
+
degraded: result.models.length === 0,
|
|
343
|
+
degradedReason: result.models.length === 0 ? "remote availability matched no local models" : null,
|
|
344
|
+
pendingNewCount: result.pendingNew.length,
|
|
345
|
+
estimatedCount: result.estimated.length,
|
|
346
|
+
};
|
|
347
|
+
try {
|
|
348
|
+
writeStartupSummary(pluginStateDir(), summary);
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
// ignore
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const remoteRefreshRunners = new Set();
|
|
355
|
+
let remoteRefreshTimer = null;
|
|
356
|
+
export function remoteRefreshRunnerCount() {
|
|
357
|
+
return remoteRefreshRunners.size;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Share one interval across setups in the process. Returns a detach
|
|
361
|
+
* function; the timer stops after the last runner detaches.
|
|
362
|
+
*/
|
|
363
|
+
export function attachRemoteRefreshRunner(run, intervalMs = REMOTE_SYNC_INTERVAL_MS) {
|
|
364
|
+
remoteRefreshRunners.add(run);
|
|
365
|
+
if (!remoteRefreshTimer) {
|
|
366
|
+
remoteRefreshTimer = setInterval(() => {
|
|
367
|
+
for (const runner of remoteRefreshRunners) {
|
|
368
|
+
void runner().catch(() => {
|
|
369
|
+
// A failed refresh keeps the last inventory; the next tick retries.
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}, intervalMs);
|
|
373
|
+
remoteRefreshTimer.unref?.();
|
|
374
|
+
}
|
|
375
|
+
let detached = false;
|
|
376
|
+
return () => {
|
|
377
|
+
if (detached)
|
|
378
|
+
return;
|
|
379
|
+
detached = true;
|
|
380
|
+
remoteRefreshRunners.delete(run);
|
|
381
|
+
if (remoteRefreshRunners.size === 0 && remoteRefreshTimer) {
|
|
382
|
+
clearInterval(remoteRefreshTimer);
|
|
383
|
+
remoteRefreshTimer = null;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract between `package.json` and the files a `npm pack` tarball actually
|
|
3
|
+
* ships. The published entrypoints point at compiled output (`dist/`), so a
|
|
4
|
+
* pack that skips the build — or a `files` list that drops `dist/` — produces
|
|
5
|
+
* a package whose imports fail at runtime on hosts without a TypeScript
|
|
6
|
+
* loader (the opencode IDE server runs plain Node inside Electron).
|
|
7
|
+
*/
|
|
8
|
+
export type PackPackageJson = {
|
|
9
|
+
main?: string;
|
|
10
|
+
types?: string;
|
|
11
|
+
exports?: Record<string, string | {
|
|
12
|
+
import?: string;
|
|
13
|
+
types?: string;
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
/** Normalize `tar -tzf` output, which uses CRLF on Windows and LF on Linux. */
|
|
17
|
+
export declare function parseTarListing(listing: string): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Every file `package.json` promises to resolve, relative to the package root.
|
|
20
|
+
* `main` and each `exports` target are included so a build regression cannot
|
|
21
|
+
* ship silently.
|
|
22
|
+
*/
|
|
23
|
+
export declare function declaredEntrypoints(pkg: PackPackageJson): string[];
|
|
24
|
+
/** Entrypoints declared by `package.json` but absent from the tarball. */
|
|
25
|
+
export declare function missingEntrypoints(packageJson: PackPackageJson, packed: string[]): string[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract between `package.json` and the files a `npm pack` tarball actually
|
|
3
|
+
* ships. The published entrypoints point at compiled output (`dist/`), so a
|
|
4
|
+
* pack that skips the build — or a `files` list that drops `dist/` — produces
|
|
5
|
+
* a package whose imports fail at runtime on hosts without a TypeScript
|
|
6
|
+
* loader (the opencode IDE server runs plain Node inside Electron).
|
|
7
|
+
*/
|
|
8
|
+
/** Normalize `tar -tzf` output, which uses CRLF on Windows and LF on Linux. */
|
|
9
|
+
export function parseTarListing(listing) {
|
|
10
|
+
return listing
|
|
11
|
+
.split(/\r?\n/)
|
|
12
|
+
.filter(Boolean)
|
|
13
|
+
.map((entry) => entry.replace(/^package\//, ""));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Every file `package.json` promises to resolve, relative to the package root.
|
|
17
|
+
* `main` and each `exports` target are included so a build regression cannot
|
|
18
|
+
* ship silently.
|
|
19
|
+
*/
|
|
20
|
+
export function declaredEntrypoints(pkg) {
|
|
21
|
+
const targets = [];
|
|
22
|
+
const push = (value) => {
|
|
23
|
+
if (typeof value !== "string")
|
|
24
|
+
return;
|
|
25
|
+
targets.push(value.replace(/^\.\//, ""));
|
|
26
|
+
};
|
|
27
|
+
push(pkg.main);
|
|
28
|
+
for (const entry of Object.values(pkg.exports ?? {})) {
|
|
29
|
+
if (typeof entry === "string") {
|
|
30
|
+
push(entry);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
push(entry.import);
|
|
34
|
+
push(entry.types);
|
|
35
|
+
}
|
|
36
|
+
return [...new Set(targets)];
|
|
37
|
+
}
|
|
38
|
+
/** Entrypoints declared by `package.json` but absent from the tarball. */
|
|
39
|
+
export function missingEntrypoints(packageJson, packed) {
|
|
40
|
+
const present = new Set(packed);
|
|
41
|
+
return declaredEntrypoints(packageJson).filter((entry) => !present.has(entry));
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type CatalogSyncDecision = {
|
|
2
|
+
extract: boolean;
|
|
3
|
+
publishRetry: boolean;
|
|
4
|
+
exit: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type PublishDecision = "publish" | "skip-no-token" | "skip-already-published";
|
|
7
|
+
export declare function decideCatalogSync(input: {
|
|
8
|
+
force: boolean;
|
|
9
|
+
latestCommandCodeVersion: string;
|
|
10
|
+
bundledCommandCodeVersion: string | null;
|
|
11
|
+
pluginVersion: string;
|
|
12
|
+
publishedPluginVersions: string[];
|
|
13
|
+
}): CatalogSyncDecision;
|
|
14
|
+
export declare function decidePublish(input: {
|
|
15
|
+
pluginVersion: string;
|
|
16
|
+
publishedPluginVersions: string[];
|
|
17
|
+
npmTokenSet: boolean;
|
|
18
|
+
}): PublishDecision;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function decideCatalogSync(input) {
|
|
2
|
+
const published = input.publishedPluginVersions.includes(input.pluginVersion);
|
|
3
|
+
if (input.force || input.latestCommandCodeVersion !== input.bundledCommandCodeVersion) {
|
|
4
|
+
return { extract: true, publishRetry: false, exit: false };
|
|
5
|
+
}
|
|
6
|
+
if (!published)
|
|
7
|
+
return { extract: false, publishRetry: true, exit: false };
|
|
8
|
+
return { extract: false, publishRetry: false, exit: true };
|
|
9
|
+
}
|
|
10
|
+
export function decidePublish(input) {
|
|
11
|
+
if (input.publishedPluginVersions.includes(input.pluginVersion))
|
|
12
|
+
return "skip-already-published";
|
|
13
|
+
if (!input.npmTokenSet)
|
|
14
|
+
return "skip-no-token";
|
|
15
|
+
return "publish";
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModelEntry } from "./catalog.js";
|
|
2
|
+
export type { ModelEntry } from "./catalog.js";
|
|
3
|
+
export type StartupSummary = {
|
|
4
|
+
catalogSource: "bundled" | "cache" | "opt-in-local" | "remote";
|
|
5
|
+
commandCodeVersion: string | null;
|
|
6
|
+
modelCount: number;
|
|
7
|
+
reasoningModelCount: number;
|
|
8
|
+
degraded: boolean;
|
|
9
|
+
degradedReason: string | null;
|
|
10
|
+
/** Remote ids with no local cost data yet — next catalog sync picks them up. */
|
|
11
|
+
pendingNewCount?: number;
|
|
12
|
+
/** Provisional entries published with models.dev estimates. */
|
|
13
|
+
estimatedCount?: number;
|
|
14
|
+
};
|
|
15
|
+
export declare function pluginStateDir(): string;
|
|
16
|
+
export declare function readCatalogCache(dir?: string): ModelEntry[] | null;
|
|
17
|
+
export declare function writeCatalogCache(dir: string, models: ModelEntry[]): void;
|
|
18
|
+
export declare function writeStartupSummary(dir: string, summary: StartupSummary): void;
|