opencode-rag-plugin 2.11.2 → 2.12.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/ReadMe.md +386 -222
- package/dist/cli/commands/backend-detect.d.ts +1 -1
- package/dist/cli/commands/backend-detect.js +1 -1
- package/dist/cli/commands/index.d.ts +0 -1
- package/dist/cli/commands/index.js +0 -1
- package/dist/cli/commands/setup.js +344 -85
- package/dist/cli/commands/{init-helpers.d.ts → workspace-helpers.d.ts} +10 -7
- package/dist/cli/commands/{init-helpers.js → workspace-helpers.js} +15 -62
- package/dist/cli/index.js +1 -2
- package/dist/cli/ollama.d.ts +12 -0
- package/dist/cli/ollama.js +26 -0
- package/dist/cli/prompt.d.ts +121 -0
- package/dist/cli/prompt.js +309 -0
- package/dist/cli/types.d.ts +31 -19
- package/dist/cli/wizard/apply.d.ts +60 -0
- package/dist/cli/wizard/apply.js +243 -0
- package/dist/cli/wizard/flow.d.ts +121 -0
- package/dist/cli/wizard/flow.js +904 -0
- package/dist/cli/wizard/model-catalog.d.ts +80 -0
- package/dist/cli/wizard/model-catalog.js +414 -0
- package/dist/cli/wizard/presets.d.ts +55 -0
- package/dist/cli/wizard/presets.js +70 -0
- package/dist/cli/wizard/repair.d.ts +54 -0
- package/dist/cli/wizard/repair.js +266 -0
- package/dist/cli/wizard/types.d.ts +99 -0
- package/dist/cli/wizard/types.js +5 -0
- package/dist/cli/workspace.d.ts +57 -0
- package/dist/cli/workspace.js +407 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1 -1
- package/dist/core/auto-scaffold.d.ts +21 -0
- package/dist/core/auto-scaffold.js +80 -0
- package/dist/core/config.d.ts +13 -2
- package/dist/core/config.js +13 -3
- package/dist/core/global-plugin.d.ts +48 -0
- package/dist/core/global-plugin.js +114 -0
- package/dist/core/provider-defaults.d.ts +2 -0
- package/dist/core/provider-defaults.js +4 -0
- package/dist/core/setup-runtime.js +40 -26
- package/dist/embedder/health.js +44 -2
- package/dist/eval/fast-index.js +1 -1
- package/dist/opencode/system-guidance.d.ts +1 -1
- package/dist/opencode/system-guidance.js +1 -1
- package/dist/tui.d.ts +11 -0
- package/dist/tui.js +12 -1
- package/dist/v2/v2-adapter.js +9 -0
- package/package.json +3 -2
- package/scripts/postinstall-setup.js +16 -0
- package/dist/cli/commands/init.d.ts +0 -51
- package/dist/cli/commands/init.js +0 -508
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Model discovery for the `setup` wizard.
|
|
3
|
+
*
|
|
4
|
+
* The wizard offers model pickers instead of asking the user to type names
|
|
5
|
+
* from memory. Discovery is provider-specific:
|
|
6
|
+
*
|
|
7
|
+
* - Ollama: `GET /api/tags` for the list, `POST /api/show` for per-model
|
|
8
|
+
* capabilities (`embedding`, `vision`, `completion`).
|
|
9
|
+
* - OpenAI-compatible providers: `GET /v1/models` (ids only, so capability is
|
|
10
|
+
* inferred from the model name).
|
|
11
|
+
* - Cohere: `GET /v1/models` (endpoints include `embed` / `chat`).
|
|
12
|
+
* - Anthropic / Google: `GET /v1/models`, chat/vision only (no embedding API).
|
|
13
|
+
*
|
|
14
|
+
* Everything is best-effort: an unreachable host returns `reachable: false`
|
|
15
|
+
* and the wizard falls back to manual model entry. No call throws.
|
|
16
|
+
*/
|
|
17
|
+
import type { ProxyConfig } from "../../core/config.js";
|
|
18
|
+
/** Capability buckets the wizard asks for. */
|
|
19
|
+
export type ModelCapability = "embedding" | "chat" | "vision";
|
|
20
|
+
/** A model discovered on the configured host. */
|
|
21
|
+
export interface ModelInfo {
|
|
22
|
+
/** Model identifier as the provider expects it (e.g. `qwen3-embedding:0.6b`). */
|
|
23
|
+
id: string;
|
|
24
|
+
/** Capabilities the provider reported, or inferred from the name when unknown. */
|
|
25
|
+
capabilities: ModelCapability[];
|
|
26
|
+
/** Optional provider detail such as parameter size or family. */
|
|
27
|
+
detail?: string;
|
|
28
|
+
}
|
|
29
|
+
/** Result of probing a model host. */
|
|
30
|
+
export interface ModelCatalog {
|
|
31
|
+
/** Provider id (`ollama`, `openai`, `cohere`, …). */
|
|
32
|
+
provider: string;
|
|
33
|
+
/** Base URL as it should be written into the config (Ollama keeps `/api`). */
|
|
34
|
+
baseUrl: string;
|
|
35
|
+
/** Whether the host answered the list request. */
|
|
36
|
+
reachable: boolean;
|
|
37
|
+
/** Human-readable failure reason when `reachable` is false. */
|
|
38
|
+
error?: string;
|
|
39
|
+
/** Discovered models (possibly empty). */
|
|
40
|
+
models: ModelInfo[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Normalize an Ollama base URL to the API root (`http://host:port`).
|
|
44
|
+
*
|
|
45
|
+
* The config convention is `…/api` because the health checks append `/embed`
|
|
46
|
+
* and `/chat`; the discovery endpoints live at the root (`/api/tags`).
|
|
47
|
+
*/
|
|
48
|
+
export declare function ollamaRoot(baseUrl: string): string;
|
|
49
|
+
/** Normalize an OpenAI-compatible base URL (`…/v1`). */
|
|
50
|
+
export declare function openAiBase(baseUrl: string): string;
|
|
51
|
+
/** Infer capabilities from a model id when the provider reports none. */
|
|
52
|
+
export declare function inferCapabilities(id: string): ModelCapability[];
|
|
53
|
+
/**
|
|
54
|
+
* Discover the models available on a host.
|
|
55
|
+
*
|
|
56
|
+
* Never throws: a failed probe returns `reachable: false` plus an error that
|
|
57
|
+
* the wizard prints before falling back to manual entry.
|
|
58
|
+
*/
|
|
59
|
+
export declare function fetchModelCatalog(options: {
|
|
60
|
+
provider: string;
|
|
61
|
+
baseUrl: string;
|
|
62
|
+
apiKey?: string;
|
|
63
|
+
proxy?: ProxyConfig;
|
|
64
|
+
}): Promise<ModelCatalog>;
|
|
65
|
+
/**
|
|
66
|
+
* Probe an embedding model and return its vector dimension.
|
|
67
|
+
*
|
|
68
|
+
* This is what keeps `vectorDimension` honest: writing the wrong dimension
|
|
69
|
+
* makes LanceDB silently pad/truncate embeddings and the store unreachable.
|
|
70
|
+
* Returns `undefined` when the probe fails (the wizard then omits the value
|
|
71
|
+
* and the runtime probe decides).
|
|
72
|
+
*/
|
|
73
|
+
export declare function probeEmbeddingDimension(options: {
|
|
74
|
+
provider: string;
|
|
75
|
+
baseUrl: string;
|
|
76
|
+
model: string;
|
|
77
|
+
apiKey?: string;
|
|
78
|
+
proxy?: ProxyConfig;
|
|
79
|
+
timeoutMs?: number;
|
|
80
|
+
}): Promise<number | undefined>;
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Model discovery for the `setup` wizard.
|
|
3
|
+
*
|
|
4
|
+
* The wizard offers model pickers instead of asking the user to type names
|
|
5
|
+
* from memory. Discovery is provider-specific:
|
|
6
|
+
*
|
|
7
|
+
* - Ollama: `GET /api/tags` for the list, `POST /api/show` for per-model
|
|
8
|
+
* capabilities (`embedding`, `vision`, `completion`).
|
|
9
|
+
* - OpenAI-compatible providers: `GET /v1/models` (ids only, so capability is
|
|
10
|
+
* inferred from the model name).
|
|
11
|
+
* - Cohere: `GET /v1/models` (endpoints include `embed` / `chat`).
|
|
12
|
+
* - Anthropic / Google: `GET /v1/models`, chat/vision only (no embedding API).
|
|
13
|
+
*
|
|
14
|
+
* Everything is best-effort: an unreachable host returns `reachable: false`
|
|
15
|
+
* and the wizard falls back to manual model entry. No call throws.
|
|
16
|
+
*/
|
|
17
|
+
import { fetchWithProxy, postJson } from "../../embedder/http.js";
|
|
18
|
+
/** Request timeout for discovery calls. Kept short so setup stays snappy. */
|
|
19
|
+
const LIST_TIMEOUT_MS = 6_000;
|
|
20
|
+
/** Total budget for the per-model Ollama capability probes. */
|
|
21
|
+
const CAPABILITY_BUDGET_MS = 5_000;
|
|
22
|
+
/** Parallel `/api/show` requests. */
|
|
23
|
+
const CAPABILITY_CONCURRENCY = 8;
|
|
24
|
+
/**
|
|
25
|
+
* Normalize an Ollama base URL to the API root (`http://host:port`).
|
|
26
|
+
*
|
|
27
|
+
* The config convention is `…/api` because the health checks append `/embed`
|
|
28
|
+
* and `/chat`; the discovery endpoints live at the root (`/api/tags`).
|
|
29
|
+
*/
|
|
30
|
+
export function ollamaRoot(baseUrl) {
|
|
31
|
+
return baseUrl.replace(/\/+$/, "").replace(/\/(api|v1)$/i, "");
|
|
32
|
+
}
|
|
33
|
+
/** Normalize an OpenAI-compatible base URL (`…/v1`). */
|
|
34
|
+
export function openAiBase(baseUrl) {
|
|
35
|
+
return baseUrl.replace(/\/+$/, "");
|
|
36
|
+
}
|
|
37
|
+
/** Build the auth header for a provider, when it needs one. */
|
|
38
|
+
function authHeaders(provider, apiKey) {
|
|
39
|
+
if (!apiKey)
|
|
40
|
+
return {};
|
|
41
|
+
if (provider === "anthropic") {
|
|
42
|
+
return { "x-api-key": apiKey, "anthropic-version": "2023-06-01" };
|
|
43
|
+
}
|
|
44
|
+
return { Authorization: `Bearer ${apiKey}` };
|
|
45
|
+
}
|
|
46
|
+
/** Name heuristics used when the provider does not report capabilities. */
|
|
47
|
+
const EMBEDDING_NAME = /(embed|bge[-_]|e5[-_]|gte[-_]|jina|nomic|mxbai|text-embedding|voyage)/i;
|
|
48
|
+
const VISION_NAME = /(vision|llava|vl[-_]|minicpm[-_]v|pixtral|molmo|idefics|internvl|gpt-4o|gpt-4\.1|claude|gemini|gemma3)/i;
|
|
49
|
+
const NON_CHAT_NAME = /(whisper|tts|rerank|moderation|dall-e|stable-diffusion|flux|clip)/i;
|
|
50
|
+
/** Infer capabilities from a model id when the provider reports none. */
|
|
51
|
+
export function inferCapabilities(id) {
|
|
52
|
+
const capabilities = [];
|
|
53
|
+
if (EMBEDDING_NAME.test(id)) {
|
|
54
|
+
capabilities.push("embedding");
|
|
55
|
+
}
|
|
56
|
+
if (!EMBEDDING_NAME.test(id) && !NON_CHAT_NAME.test(id)) {
|
|
57
|
+
capabilities.push("chat");
|
|
58
|
+
}
|
|
59
|
+
if (VISION_NAME.test(id) && !EMBEDDING_NAME.test(id)) {
|
|
60
|
+
capabilities.push("vision");
|
|
61
|
+
}
|
|
62
|
+
return capabilities;
|
|
63
|
+
}
|
|
64
|
+
/** GET JSON with a bounded timeout and proxy support. Returns `undefined` on failure. */
|
|
65
|
+
async function getJson(url, headers, proxy) {
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetchWithProxy(url, { method: "GET", headers, signal: AbortSignal.timeout(LIST_TIMEOUT_MS) }, proxy);
|
|
68
|
+
if (!response.ok)
|
|
69
|
+
return undefined;
|
|
70
|
+
return await response.json();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Extract a human-readable error from a failed list call (best-effort). */
|
|
77
|
+
async function describeListFailure(url, headers, proxy) {
|
|
78
|
+
try {
|
|
79
|
+
const response = await fetchWithProxy(url, { method: "GET", headers, signal: AbortSignal.timeout(LIST_TIMEOUT_MS) }, proxy);
|
|
80
|
+
if (response.ok)
|
|
81
|
+
return "The model list endpoint returned an unexpected response.";
|
|
82
|
+
if (response.status === 401 || response.status === 403) {
|
|
83
|
+
return `HTTP ${response.status}: authentication failed — check the API key.`;
|
|
84
|
+
}
|
|
85
|
+
return `HTTP ${response.status} from ${url}`;
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
const message = err.message || String(err);
|
|
89
|
+
if (/ECONNREFUSED|fetch failed|connect/i.test(message)) {
|
|
90
|
+
return `Cannot reach ${url} — is the server running?`;
|
|
91
|
+
}
|
|
92
|
+
return message.slice(0, 200);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Read an array-valued property from an unknown JSON object. */
|
|
96
|
+
function asArray(value) {
|
|
97
|
+
return Array.isArray(value) ? value : [];
|
|
98
|
+
}
|
|
99
|
+
/** Read a string property from an unknown record. */
|
|
100
|
+
function asString(value) {
|
|
101
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
102
|
+
}
|
|
103
|
+
/** Read a record property from an unknown value. */
|
|
104
|
+
function asRecord(value) {
|
|
105
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
106
|
+
? value
|
|
107
|
+
: undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Probe Ollama model capabilities with `POST /api/show`.
|
|
111
|
+
*
|
|
112
|
+
* Runs with bounded concurrency and a hard total budget so a slow remote host
|
|
113
|
+
* cannot stall setup. Models whose probe did not finish keep name-inferred
|
|
114
|
+
* capabilities.
|
|
115
|
+
*/
|
|
116
|
+
async function probeOllamaCapabilities(root, models, apiKey, proxy) {
|
|
117
|
+
const deadline = Date.now() + CAPABILITY_BUDGET_MS;
|
|
118
|
+
const headers = authHeaders("ollama", apiKey);
|
|
119
|
+
let cursor = 0;
|
|
120
|
+
const worker = async () => {
|
|
121
|
+
for (;;) {
|
|
122
|
+
if (Date.now() > deadline)
|
|
123
|
+
return;
|
|
124
|
+
const index = cursor;
|
|
125
|
+
cursor += 1;
|
|
126
|
+
const model = models[index];
|
|
127
|
+
if (!model)
|
|
128
|
+
return;
|
|
129
|
+
try {
|
|
130
|
+
const response = await postJson(`${root}/api/show`, { model: model.id }, headers, Math.min(LIST_TIMEOUT_MS, Math.max(1_000, deadline - Date.now())), proxy);
|
|
131
|
+
if (!response.ok)
|
|
132
|
+
continue;
|
|
133
|
+
const body = asRecord(await response.json());
|
|
134
|
+
const reported = asArray(body?.capabilities).filter((value) => typeof value === "string");
|
|
135
|
+
if (reported.length > 0) {
|
|
136
|
+
const capabilities = [];
|
|
137
|
+
if (reported.includes("embedding"))
|
|
138
|
+
capabilities.push("embedding");
|
|
139
|
+
if (reported.includes("completion") || reported.includes("tools"))
|
|
140
|
+
capabilities.push("chat");
|
|
141
|
+
if (reported.includes("vision")) {
|
|
142
|
+
capabilities.push("vision");
|
|
143
|
+
if (!capabilities.includes("chat"))
|
|
144
|
+
capabilities.push("chat");
|
|
145
|
+
}
|
|
146
|
+
if (capabilities.length > 0)
|
|
147
|
+
model.capabilities = capabilities;
|
|
148
|
+
}
|
|
149
|
+
const details = asRecord(body?.details);
|
|
150
|
+
const family = asString(details?.family);
|
|
151
|
+
const parameterSize = asString(details?.parameter_size);
|
|
152
|
+
if (family && parameterSize)
|
|
153
|
+
model.detail = `${family} · ${parameterSize}`;
|
|
154
|
+
else if (family)
|
|
155
|
+
model.detail = family;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// Probe is optional — keep inferred capabilities.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
await Promise.all(Array.from({ length: Math.min(CAPABILITY_CONCURRENCY, models.length) }, () => worker()));
|
|
163
|
+
}
|
|
164
|
+
/** Discover Ollama models. */
|
|
165
|
+
async function fetchOllamaCatalog(baseUrl, apiKey, proxy) {
|
|
166
|
+
const root = ollamaRoot(baseUrl);
|
|
167
|
+
const configBaseUrl = /\/api$/i.test(baseUrl.replace(/\/+$/, ""))
|
|
168
|
+
? baseUrl.replace(/\/+$/, "")
|
|
169
|
+
: /\/v1$/i.test(baseUrl.replace(/\/+$/, ""))
|
|
170
|
+
? `${root}/api`
|
|
171
|
+
: `${root}/api`;
|
|
172
|
+
const headers = authHeaders("ollama", apiKey);
|
|
173
|
+
const body = asRecord(await getJson(`${root}/api/tags`, headers, proxy));
|
|
174
|
+
if (!body) {
|
|
175
|
+
return {
|
|
176
|
+
provider: "ollama",
|
|
177
|
+
baseUrl: configBaseUrl,
|
|
178
|
+
reachable: false,
|
|
179
|
+
error: await describeListFailure(`${root}/api/tags`, headers, proxy),
|
|
180
|
+
models: [],
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const models = [];
|
|
184
|
+
for (const entry of asArray(body.models)) {
|
|
185
|
+
const record = asRecord(entry);
|
|
186
|
+
const id = asString(record?.name) ?? asString(record?.model);
|
|
187
|
+
if (!id)
|
|
188
|
+
continue;
|
|
189
|
+
if (models.some((model) => model.id === id))
|
|
190
|
+
continue;
|
|
191
|
+
const details = asRecord(record?.details);
|
|
192
|
+
const family = asString(details?.family);
|
|
193
|
+
const parameterSize = asString(details?.parameter_size);
|
|
194
|
+
models.push({
|
|
195
|
+
id,
|
|
196
|
+
capabilities: inferCapabilities(id),
|
|
197
|
+
...(family || parameterSize
|
|
198
|
+
? { detail: [family, parameterSize].filter(Boolean).join(" · ") }
|
|
199
|
+
: {}),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
await probeOllamaCapabilities(root, models, apiKey, proxy);
|
|
203
|
+
return { provider: "ollama", baseUrl: configBaseUrl, reachable: true, models };
|
|
204
|
+
}
|
|
205
|
+
/** Discover OpenAI-compatible models (`GET {baseUrl}/models`). */
|
|
206
|
+
async function fetchOpenAiCatalog(baseUrl, provider, apiKey, proxy) {
|
|
207
|
+
const base = openAiBase(baseUrl);
|
|
208
|
+
const url = `${base}/models`;
|
|
209
|
+
const headers = authHeaders(provider, apiKey);
|
|
210
|
+
const body = asRecord(await getJson(url, headers, proxy));
|
|
211
|
+
if (!body) {
|
|
212
|
+
return {
|
|
213
|
+
provider,
|
|
214
|
+
baseUrl: base,
|
|
215
|
+
reachable: false,
|
|
216
|
+
error: await describeListFailure(url, headers, proxy),
|
|
217
|
+
models: [],
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const models = [];
|
|
221
|
+
for (const entry of asArray(body.data)) {
|
|
222
|
+
const record = asRecord(entry);
|
|
223
|
+
const id = asString(record?.id);
|
|
224
|
+
if (!id || models.some((model) => model.id === id))
|
|
225
|
+
continue;
|
|
226
|
+
models.push({ id, capabilities: inferCapabilities(id) });
|
|
227
|
+
}
|
|
228
|
+
models.sort((a, b) => a.id.localeCompare(b.id));
|
|
229
|
+
return { provider, baseUrl: base, reachable: true, models };
|
|
230
|
+
}
|
|
231
|
+
/** Discover Cohere models (`GET {baseUrl}/models`, endpoints carry capabilities). */
|
|
232
|
+
async function fetchCohereCatalog(baseUrl, apiKey, proxy) {
|
|
233
|
+
const base = openAiBase(baseUrl);
|
|
234
|
+
const url = `${base}/models`;
|
|
235
|
+
const headers = authHeaders("cohere", apiKey);
|
|
236
|
+
const body = asRecord(await getJson(url, headers, proxy));
|
|
237
|
+
if (!body) {
|
|
238
|
+
return {
|
|
239
|
+
provider: "cohere",
|
|
240
|
+
baseUrl: base,
|
|
241
|
+
reachable: false,
|
|
242
|
+
error: await describeListFailure(url, headers, proxy),
|
|
243
|
+
models: [],
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const models = [];
|
|
247
|
+
for (const entry of asArray(body.models)) {
|
|
248
|
+
const record = asRecord(entry);
|
|
249
|
+
const id = asString(record?.name);
|
|
250
|
+
if (!id || models.some((model) => model.id === id))
|
|
251
|
+
continue;
|
|
252
|
+
const endpoints = asArray(record?.endpoints)
|
|
253
|
+
.filter((value) => typeof value === "string")
|
|
254
|
+
.map((value) => value.toLowerCase());
|
|
255
|
+
const capabilities = [];
|
|
256
|
+
if (endpoints.includes("embed") || inferCapabilities(id).includes("embedding")) {
|
|
257
|
+
capabilities.push("embedding");
|
|
258
|
+
}
|
|
259
|
+
if (endpoints.includes("chat") || endpoints.includes("generate")) {
|
|
260
|
+
capabilities.push("chat");
|
|
261
|
+
}
|
|
262
|
+
if (capabilities.length === 0)
|
|
263
|
+
capabilities.push(...inferCapabilities(id));
|
|
264
|
+
models.push({ id, capabilities });
|
|
265
|
+
}
|
|
266
|
+
models.sort((a, b) => a.id.localeCompare(b.id));
|
|
267
|
+
return { provider: "cohere", baseUrl: base, reachable: true, models };
|
|
268
|
+
}
|
|
269
|
+
/** Discover Anthropic models (`GET {baseUrl}/models`, chat + vision). */
|
|
270
|
+
async function fetchAnthropicCatalog(baseUrl, apiKey, proxy) {
|
|
271
|
+
const base = openAiBase(baseUrl);
|
|
272
|
+
const url = `${base}/models`;
|
|
273
|
+
const headers = authHeaders("anthropic", apiKey);
|
|
274
|
+
const body = asRecord(await getJson(url, headers, proxy));
|
|
275
|
+
if (!body) {
|
|
276
|
+
return {
|
|
277
|
+
provider: "anthropic",
|
|
278
|
+
baseUrl: base,
|
|
279
|
+
reachable: false,
|
|
280
|
+
error: await describeListFailure(url, headers, proxy),
|
|
281
|
+
models: [],
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const models = [];
|
|
285
|
+
for (const entry of asArray(body.data)) {
|
|
286
|
+
const record = asRecord(entry);
|
|
287
|
+
const id = asString(record?.id);
|
|
288
|
+
if (!id || models.some((model) => model.id === id))
|
|
289
|
+
continue;
|
|
290
|
+
models.push({ id, capabilities: ["chat", "vision"] });
|
|
291
|
+
}
|
|
292
|
+
return { provider: "anthropic", baseUrl: base, reachable: true, models };
|
|
293
|
+
}
|
|
294
|
+
/** Discover Google models (`GET {baseUrl}/models?key=...`). */
|
|
295
|
+
async function fetchGoogleCatalog(baseUrl, apiKey, proxy) {
|
|
296
|
+
const base = openAiBase(baseUrl);
|
|
297
|
+
const url = apiKey ? `${base}/models?key=${encodeURIComponent(apiKey)}` : `${base}/models`;
|
|
298
|
+
const body = asRecord(await getJson(url, {}, proxy));
|
|
299
|
+
if (!body) {
|
|
300
|
+
return {
|
|
301
|
+
provider: "google",
|
|
302
|
+
baseUrl: base,
|
|
303
|
+
reachable: false,
|
|
304
|
+
error: await describeListFailure(url, {}, proxy),
|
|
305
|
+
models: [],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
const models = [];
|
|
309
|
+
for (const entry of asArray(body.models)) {
|
|
310
|
+
const record = asRecord(entry);
|
|
311
|
+
const rawName = asString(record?.name);
|
|
312
|
+
if (!rawName)
|
|
313
|
+
continue;
|
|
314
|
+
const id = rawName.replace(/^models\//, "");
|
|
315
|
+
if (models.some((model) => model.id === id))
|
|
316
|
+
continue;
|
|
317
|
+
const methods = asArray(record?.supportedGenerationMethods)
|
|
318
|
+
.filter((value) => typeof value === "string")
|
|
319
|
+
.map((value) => value.toLowerCase());
|
|
320
|
+
const capabilities = [];
|
|
321
|
+
if (methods.includes("embedcontent"))
|
|
322
|
+
capabilities.push("embedding");
|
|
323
|
+
if (methods.includes("generatecontent")) {
|
|
324
|
+
capabilities.push("chat");
|
|
325
|
+
if (VISION_NAME.test(id))
|
|
326
|
+
capabilities.push("vision");
|
|
327
|
+
}
|
|
328
|
+
if (capabilities.length === 0)
|
|
329
|
+
capabilities.push(...inferCapabilities(id));
|
|
330
|
+
models.push({ id, capabilities });
|
|
331
|
+
}
|
|
332
|
+
return { provider: "google", baseUrl: base, reachable: true, models };
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Discover the models available on a host.
|
|
336
|
+
*
|
|
337
|
+
* Never throws: a failed probe returns `reachable: false` plus an error that
|
|
338
|
+
* the wizard prints before falling back to manual entry.
|
|
339
|
+
*/
|
|
340
|
+
export async function fetchModelCatalog(options) {
|
|
341
|
+
const { provider, baseUrl, apiKey, proxy } = options;
|
|
342
|
+
if (!baseUrl.trim()) {
|
|
343
|
+
return {
|
|
344
|
+
provider,
|
|
345
|
+
baseUrl,
|
|
346
|
+
reachable: false,
|
|
347
|
+
error: "No base URL configured.",
|
|
348
|
+
models: [],
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
if (provider === "ollama")
|
|
352
|
+
return fetchOllamaCatalog(baseUrl, apiKey, proxy);
|
|
353
|
+
if (provider === "cohere")
|
|
354
|
+
return fetchCohereCatalog(baseUrl, apiKey, proxy);
|
|
355
|
+
if (provider === "anthropic")
|
|
356
|
+
return fetchAnthropicCatalog(baseUrl, apiKey, proxy);
|
|
357
|
+
if (provider === "google")
|
|
358
|
+
return fetchGoogleCatalog(baseUrl, apiKey, proxy);
|
|
359
|
+
return fetchOpenAiCatalog(baseUrl, provider, apiKey, proxy);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Probe an embedding model and return its vector dimension.
|
|
363
|
+
*
|
|
364
|
+
* This is what keeps `vectorDimension` honest: writing the wrong dimension
|
|
365
|
+
* makes LanceDB silently pad/truncate embeddings and the store unreachable.
|
|
366
|
+
* Returns `undefined` when the probe fails (the wizard then omits the value
|
|
367
|
+
* and the runtime probe decides).
|
|
368
|
+
*/
|
|
369
|
+
export async function probeEmbeddingDimension(options) {
|
|
370
|
+
const { provider, baseUrl, model, apiKey, proxy, timeoutMs = 15_000 } = options;
|
|
371
|
+
const headers = authHeaders(provider, apiKey);
|
|
372
|
+
const text = "dimension probe";
|
|
373
|
+
const readLength = (value) => {
|
|
374
|
+
const arrays = asArray(value);
|
|
375
|
+
const first = asArray(arrays[0]);
|
|
376
|
+
return first.length > 0 ? first.length : undefined;
|
|
377
|
+
};
|
|
378
|
+
try {
|
|
379
|
+
if (provider === "cohere") {
|
|
380
|
+
const response = await postJson(`${openAiBase(baseUrl)}/embed`, { model, texts: [text], input_type: "search_document" }, headers, timeoutMs, proxy);
|
|
381
|
+
if (!response.ok)
|
|
382
|
+
return undefined;
|
|
383
|
+
return readLength(asRecord(await response.json())?.embeddings);
|
|
384
|
+
}
|
|
385
|
+
if (provider === "ollama") {
|
|
386
|
+
const root = ollamaRoot(baseUrl);
|
|
387
|
+
const response = await postJson(`${root}/api/embed`, { model, input: [text] }, headers, timeoutMs, proxy);
|
|
388
|
+
if (response.ok) {
|
|
389
|
+
return readLength(asRecord(await response.json())?.embeddings);
|
|
390
|
+
}
|
|
391
|
+
// Some Ollama builds only expose the OpenAI-compatible endpoint.
|
|
392
|
+
const fallback = await postJson(`${root}/v1/embeddings`, { model, input: [text] }, headers, timeoutMs, proxy);
|
|
393
|
+
if (!fallback.ok)
|
|
394
|
+
return undefined;
|
|
395
|
+
const body = asRecord(await fallback.json());
|
|
396
|
+
const data = asRecord(asArray(body?.data)[0]);
|
|
397
|
+
const embedding = asArray(data?.embedding);
|
|
398
|
+
return embedding.length > 0 ? embedding.length : undefined;
|
|
399
|
+
}
|
|
400
|
+
if (provider === "anthropic" || provider === "google")
|
|
401
|
+
return undefined;
|
|
402
|
+
const response = await postJson(`${openAiBase(baseUrl)}/embeddings`, { model, input: [text] }, headers, timeoutMs, proxy);
|
|
403
|
+
if (!response.ok)
|
|
404
|
+
return undefined;
|
|
405
|
+
const body = asRecord(await response.json());
|
|
406
|
+
const data = asRecord(asArray(body?.data)[0]);
|
|
407
|
+
const embedding = asArray(data?.embedding);
|
|
408
|
+
return embedding.length > 0 ? embedding.length : undefined;
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
return undefined;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
//# sourceMappingURL=model-catalog.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Recommended model presets for one-question `setup`.
|
|
3
|
+
*
|
|
4
|
+
* The presets are the "press Enter" path: they map a detected Ollama backend
|
|
5
|
+
* (CPU vs GPU) to a small, well-tested model set so a first-time user does not
|
|
6
|
+
* have to choose four models. The full wizard remains available as "Custom
|
|
7
|
+
* setup" and via `--reconfigure`.
|
|
8
|
+
*/
|
|
9
|
+
import type { ModelCatalog } from "./model-catalog.js";
|
|
10
|
+
/** Preset identifiers. */
|
|
11
|
+
export type PresetId = "ollama-cpu" | "ollama-gpu" | "openai";
|
|
12
|
+
/** One recommended model set. */
|
|
13
|
+
export interface Preset {
|
|
14
|
+
/** Stable identifier. */
|
|
15
|
+
id: PresetId;
|
|
16
|
+
/** Human-readable label. */
|
|
17
|
+
label: string;
|
|
18
|
+
/** One-line description shown in the prompt. */
|
|
19
|
+
description: string;
|
|
20
|
+
/** Provider that serves the models. */
|
|
21
|
+
provider: "ollama" | "openai";
|
|
22
|
+
/** Embedding model (required). */
|
|
23
|
+
embedding: string;
|
|
24
|
+
/** Description model, when the preset enables descriptions. */
|
|
25
|
+
descriptionModel?: string;
|
|
26
|
+
/** Vision model, when the preset enables image descriptions. */
|
|
27
|
+
visionModel?: string;
|
|
28
|
+
}
|
|
29
|
+
/** CPU-friendly Ollama preset (small models, fast on consumer hardware). */
|
|
30
|
+
export declare const OLLAMA_CPU_PRESET: Preset;
|
|
31
|
+
/** GPU Ollama preset (larger embedding model for better recall). */
|
|
32
|
+
export declare const OLLAMA_GPU_PRESET: Preset;
|
|
33
|
+
/** OpenAI preset (cloud; needs OPENAI_API_KEY). */
|
|
34
|
+
export declare const OPENAI_PRESET: Preset;
|
|
35
|
+
/** All presets, in display order. */
|
|
36
|
+
export declare const PRESETS: readonly Preset[];
|
|
37
|
+
/** Pick the Ollama preset matching a detected backend. */
|
|
38
|
+
export declare function presetForBackend(backend: "gpu" | "cpu" | "unreachable" | "unknown"): Preset;
|
|
39
|
+
/** A model from a preset plus whether the host already has it. */
|
|
40
|
+
export interface PresetModelStatus {
|
|
41
|
+
/** Model id. */
|
|
42
|
+
id: string;
|
|
43
|
+
/** Whether the host listed the model. */
|
|
44
|
+
installed: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* List the preset's models with their installation status.
|
|
48
|
+
*
|
|
49
|
+
* An unreachable catalog reports every model as not installed (the caller
|
|
50
|
+
* only uses this for display after a successful probe, but being defensive
|
|
51
|
+
* keeps the helper usable from tests).
|
|
52
|
+
*/
|
|
53
|
+
export declare function presetModelStatus(preset: Preset, catalog?: ModelCatalog): PresetModelStatus[];
|
|
54
|
+
/** Render the preset's model list for the confirmation prompt. */
|
|
55
|
+
export declare function describePreset(preset: Preset, catalog?: ModelCatalog): string[];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Recommended model presets for one-question `setup`.
|
|
3
|
+
*
|
|
4
|
+
* The presets are the "press Enter" path: they map a detected Ollama backend
|
|
5
|
+
* (CPU vs GPU) to a small, well-tested model set so a first-time user does not
|
|
6
|
+
* have to choose four models. The full wizard remains available as "Custom
|
|
7
|
+
* setup" and via `--reconfigure`.
|
|
8
|
+
*/
|
|
9
|
+
/** CPU-friendly Ollama preset (small models, fast on consumer hardware). */
|
|
10
|
+
export const OLLAMA_CPU_PRESET = {
|
|
11
|
+
id: "ollama-cpu",
|
|
12
|
+
label: "Ollama · CPU",
|
|
13
|
+
description: "Small models that stay fast without a dedicated GPU",
|
|
14
|
+
provider: "ollama",
|
|
15
|
+
embedding: "qwen3-embedding:0.6b",
|
|
16
|
+
descriptionModel: "qwen2.5:3b",
|
|
17
|
+
visionModel: "minicpm-v4.6",
|
|
18
|
+
};
|
|
19
|
+
/** GPU Ollama preset (larger embedding model for better recall). */
|
|
20
|
+
export const OLLAMA_GPU_PRESET = {
|
|
21
|
+
id: "ollama-gpu",
|
|
22
|
+
label: "Ollama · GPU",
|
|
23
|
+
description: "Larger embedding model for better recall",
|
|
24
|
+
provider: "ollama",
|
|
25
|
+
embedding: "qwen3-embedding:1.7b",
|
|
26
|
+
descriptionModel: "qwen2.5:3b",
|
|
27
|
+
visionModel: "minicpm-v4.6",
|
|
28
|
+
};
|
|
29
|
+
/** OpenAI preset (cloud; needs OPENAI_API_KEY). */
|
|
30
|
+
export const OPENAI_PRESET = {
|
|
31
|
+
id: "openai",
|
|
32
|
+
label: "OpenAI",
|
|
33
|
+
description: "Cloud models (requires OPENAI_API_KEY)",
|
|
34
|
+
provider: "openai",
|
|
35
|
+
embedding: "text-embedding-3-small",
|
|
36
|
+
descriptionModel: "gpt-4o-mini",
|
|
37
|
+
visionModel: "gpt-4o-mini",
|
|
38
|
+
};
|
|
39
|
+
/** All presets, in display order. */
|
|
40
|
+
export const PRESETS = [OLLAMA_CPU_PRESET, OLLAMA_GPU_PRESET, OPENAI_PRESET];
|
|
41
|
+
/** Pick the Ollama preset matching a detected backend. */
|
|
42
|
+
export function presetForBackend(backend) {
|
|
43
|
+
return backend === "gpu" ? OLLAMA_GPU_PRESET : OLLAMA_CPU_PRESET;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether a model id is present in the catalog.
|
|
47
|
+
*
|
|
48
|
+
* Ollama lists the implicit `:latest` tag explicitly, so `minicpm-v4.6` and
|
|
49
|
+
* `minicpm-v4.6:latest` are the same model for the "installed" display.
|
|
50
|
+
*/
|
|
51
|
+
function isInstalled(installed, id) {
|
|
52
|
+
return installed.has(id) || installed.has(`${id.replace(/:latest$/i, "")}:latest`);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* List the preset's models with their installation status.
|
|
56
|
+
*
|
|
57
|
+
* An unreachable catalog reports every model as not installed (the caller
|
|
58
|
+
* only uses this for display after a successful probe, but being defensive
|
|
59
|
+
* keeps the helper usable from tests).
|
|
60
|
+
*/
|
|
61
|
+
export function presetModelStatus(preset, catalog) {
|
|
62
|
+
const installed = new Set(catalog?.models.map((model) => model.id) ?? []);
|
|
63
|
+
const ids = [preset.embedding, preset.descriptionModel, preset.visionModel].filter((id) => typeof id === "string" && id.length > 0);
|
|
64
|
+
return ids.map((id) => ({ id, installed: isInstalled(installed, id) }));
|
|
65
|
+
}
|
|
66
|
+
/** Render the preset's model list for the confirmation prompt. */
|
|
67
|
+
export function describePreset(preset, catalog) {
|
|
68
|
+
return presetModelStatus(preset, catalog).map((model) => `${model.id}${model.installed ? " (installed)" : " (will be pulled)"}`);
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=presets.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Repair flow for models that fail the post-setup health check.
|
|
3
|
+
*
|
|
4
|
+
* `setup` probes the configured providers after applying the configuration.
|
|
5
|
+
* When a model answers with an error (expired key, wrong URL, unreachable
|
|
6
|
+
* server) the run must not stop at "Some providers are not reachable": this
|
|
7
|
+
* module asks whether to fix the section, then collects provider, base URL,
|
|
8
|
+
* API key and model (the same questions as the wizard) and writes the result
|
|
9
|
+
* into the config layer that already owns the section.
|
|
10
|
+
*
|
|
11
|
+
* The health check resolves API keys the same way the runtime does
|
|
12
|
+
* (`resolveApiKey`), so a key stored in OpenCode's auth store is used instead
|
|
13
|
+
* of the indexing section's key — an expired Zen key then reports itself as
|
|
14
|
+
* expired rather than as a generic 401.
|
|
15
|
+
*/
|
|
16
|
+
import { type ProxyConfig } from "../../core/config.js";
|
|
17
|
+
import { type HealthCheckResult } from "../../embedder/health.js";
|
|
18
|
+
import type { Prompter } from "../prompt.js";
|
|
19
|
+
/** Options for {@link offerHealthRepairs}. */
|
|
20
|
+
export interface HealthRepairOptions {
|
|
21
|
+
/** Workspace root (used to reload the effective config and detect sections). */
|
|
22
|
+
cwd: string;
|
|
23
|
+
/** Existing workspace config path (`setup` already resolved it). */
|
|
24
|
+
workspaceConfigPath: string;
|
|
25
|
+
/** Machine-global config path. */
|
|
26
|
+
globalConfigPath: string;
|
|
27
|
+
prompter: Prompter;
|
|
28
|
+
/** Results of the health check that just printed its report. */
|
|
29
|
+
results: readonly HealthCheckResult[];
|
|
30
|
+
proxy?: ProxyConfig;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Pick the config file that owns a section.
|
|
34
|
+
*
|
|
35
|
+
* The section is repaired where the user already put it, so the write is
|
|
36
|
+
* visible immediately instead of being shadowed by another layer.
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveRepairLayer(workspaceConfigPath: string, globalConfigPath: string, section: string, sub?: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Build the sparse patch for a repaired section.
|
|
41
|
+
*
|
|
42
|
+
* `apiKey: null` deletes a stale key (so an environment variable or the
|
|
43
|
+
* OpenCode auth store can take over); `vectorDimension: null` does the same
|
|
44
|
+
* for an embedding model that may have a different dimension.
|
|
45
|
+
*/
|
|
46
|
+
export declare function buildRepairPatch(section: "embedding" | "description" | "imageDescription" | "reranking", sub: "onDemand" | undefined, choice: Record<string, unknown>): Record<string, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* Offer to repair every model that failed the connectivity check.
|
|
49
|
+
*
|
|
50
|
+
* Only runs for an interactive prompter: `--yes` / non-TTY runs keep the
|
|
51
|
+
* current report-and-continue behaviour. Returns the number of sections that
|
|
52
|
+
* were written.
|
|
53
|
+
*/
|
|
54
|
+
export declare function offerHealthRepairs(options: HealthRepairOptions): Promise<number>;
|