@mizchi/vlmkit-ai 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/errors.d.mts +1 -1
- package/dist/llm-client.d.mts +2 -2
- package/dist/llm-client.mjs +64 -13
- package/dist/reasoning-pipeline.d.mts +1 -1
- package/dist/reasoning-pipeline.mjs +1 -1
- package/dist/vlm-client.d.mts +10 -1
- package/dist/vlm-client.mjs +17 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
VLM / LLM clients and reasoning pipeline used by VRT to interpret diff
|
|
4
4
|
images and natural-language intent.
|
|
5
5
|
|
|
6
|
-
Part of the [`vlmkit`](https://github.com/mizchi/
|
|
6
|
+
Part of the [`vlmkit`](https://github.com/mizchi/vlmkit) monorepo.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -41,9 +41,9 @@ const reasoning = await runReasoning({ /* diff context */ });
|
|
|
41
41
|
|
|
42
42
|
| Variable | Purpose |
|
|
43
43
|
|---|---|
|
|
44
|
-
| `
|
|
45
|
-
| `
|
|
46
|
-
| `
|
|
44
|
+
| `VLMKIT_LLM_PROVIDER` | `gemini` (default) \| `anthropic` |
|
|
45
|
+
| `VLMKIT_LLM_MODEL` | Override provider default model |
|
|
46
|
+
| `VLMKIT_VLM_MODEL` | OpenRouter VLM model id |
|
|
47
47
|
| `OPENROUTER_API_KEY` | VLM access |
|
|
48
48
|
| `GEMINI_API_KEY` | Gemini LLM access |
|
|
49
49
|
| `ANTHROPIC_API_KEY` | Anthropic LLM access |
|
package/dist/errors.d.mts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* Codes are stable; new ones can only be added (never renumbered or
|
|
16
16
|
* removed) in minor versions.
|
|
17
17
|
*/
|
|
18
|
-
type VrtConfigErrorCode = /** Required env var (`OPENROUTER_API_KEY`, `GEMINI_API_KEY`, `ANTHROPIC_API_KEY`) not set. */"MISSING_KEY" /** Provider/model id doesn't resolve. */ | "INVALID_MODEL" /** Model substring matches more than one candidate. */ | "MULTIPLE_MATCHES" /** `
|
|
18
|
+
type VrtConfigErrorCode = /** Required env var (`OPENROUTER_API_KEY`, `GEMINI_API_KEY`, `ANTHROPIC_API_KEY`) not set. */"MISSING_KEY" /** Provider/model id doesn't resolve. */ | "INVALID_MODEL" /** Model substring matches more than one candidate. */ | "MULTIPLE_MATCHES" /** `VLMKIT_LLM_PROVIDER` value is not one of the known providers. */ | "INVALID_PROVIDER" /** No provider in the fallback chain has an API key configured. */ | "NO_PROVIDER_AVAILABLE" /** An optional runtime dependency (e.g. `@google/generative-ai`) failed to import. */ | "MISSING_DEPENDENCY" /** Caller-supplied request parameters are malformed (empty prompt, out-of-range size, etc.). */ | "INVALID_REQUEST";
|
|
19
19
|
declare class VrtConfigError extends Error {
|
|
20
20
|
readonly code: VrtConfigErrorCode;
|
|
21
21
|
constructor(code: VrtConfigErrorCode, message: string);
|
package/dist/llm-client.d.mts
CHANGED
|
@@ -58,7 +58,7 @@ type LLMProviderName = "gemini" | "anthropic" | "openrouter";
|
|
|
58
58
|
/**
|
|
59
59
|
* Create a unified LLM client.
|
|
60
60
|
*
|
|
61
|
-
* Provider is set via `
|
|
61
|
+
* Provider is set via `VLMKIT_LLM_PROVIDER` env var (default: `gemini`).
|
|
62
62
|
*
|
|
63
63
|
* Throws `VrtConfigError` when the resolved provider has no API key
|
|
64
64
|
* (`code: "MISSING_KEY"`) or the provider value is unknown
|
|
@@ -75,7 +75,7 @@ interface CreateLLMProviderOptions {
|
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Backwards-compatible: returns the existing `LLMProvider` interface.
|
|
78
|
-
* Uses the provider specified by `
|
|
78
|
+
* Uses the provider specified by `VLMKIT_LLM_PROVIDER`. Falls back to
|
|
79
79
|
* other providers if the requested key is missing.
|
|
80
80
|
*
|
|
81
81
|
* Throws `VrtConfigError` (`code: "NO_PROVIDER_AVAILABLE"`) when no
|
package/dist/llm-client.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { VrtConfigError } from "./errors.mjs";
|
|
2
|
-
import { readEnv } from "@mizchi/vlmkit-core/
|
|
2
|
+
import { readEnv } from "@mizchi/vlmkit-core/project-config.ts";
|
|
3
3
|
//#region src/llm-client.ts
|
|
4
4
|
function createAnthropicClient(apiKey, model) {
|
|
5
5
|
const modelId = model ?? "claude-sonnet-4-6";
|
|
@@ -219,21 +219,44 @@ const VALID_PROVIDERS = new Set([
|
|
|
219
219
|
/**
|
|
220
220
|
* Resolve provider and key from environment variables.
|
|
221
221
|
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
222
|
+
* VLMKIT_LLM_PROVIDER: gemini (default) | anthropic | openrouter
|
|
223
|
+
* VLMKIT_LLM_MODEL: model ID (defaults to provider's default)
|
|
224
224
|
*
|
|
225
225
|
* Returns a config, or describes why no config was available. The
|
|
226
226
|
* factory decides whether to throw based on `throwIfMissing`.
|
|
227
|
+
*
|
|
228
|
+
* ## An unrequested provider is chosen from the keys you have
|
|
229
|
+
*
|
|
230
|
+
* This used to be `options?.provider ?? readEnv("LLM_PROVIDER") ?? "gemini"` with no
|
|
231
|
+
* regard for which keys existed, so a caller that expressed no preference got gemini and
|
|
232
|
+
* then a `MISSING_KEY` naming a key it did not need. Measured with exactly one key set:
|
|
233
|
+
*
|
|
234
|
+
* ANTHROPIC_API_KEY only → MISSING_KEY: GEMINI_API_KEY (or GOOGLE_AI_API_KEY) is required
|
|
235
|
+
* OPENROUTER_API_KEY only → MISSING_KEY: GEMINI_API_KEY (or GOOGLE_AI_API_KEY) is required
|
|
236
|
+
*
|
|
237
|
+
* while `createLLMProvider`, twenty lines further down this same file, returned a working
|
|
238
|
+
* client for both. Being told to obtain a key you do not need, while the one you have is
|
|
239
|
+
* ignored, sends the reader to the wrong fix.
|
|
240
|
+
*
|
|
241
|
+
* Every caller had already worked around it, in four different ways, which is what made
|
|
242
|
+
* this worth changing rather than leaving: `createLLMProvider` and
|
|
243
|
+
* `reasoning-pipeline.ts` each carry a byte-identical `["gemini", "anthropic",
|
|
244
|
+
* "openrouter"]` retry loop, and `vlmkit-plan` and `vlmkit-generate` each carry a
|
|
245
|
+
* byte-identical `resolveDefaultProvider` that picks by key presence before calling in.
|
|
246
|
+
* Four workarounds for one missing behaviour is the shared function being wrong, and the
|
|
247
|
+
* fifth caller is the one that pays for it — the same shape as eight modules hand-rolling
|
|
248
|
+
* `isUrlSource`.
|
|
249
|
+
*
|
|
250
|
+
* **An explicit request is still honoured exactly.** `{ provider: "gemini" }` or
|
|
251
|
+
* `VLMKIT_LLM_PROVIDER=gemini` with no Gemini key is a `MISSING_KEY` for *gemini*, not a
|
|
252
|
+
* silent substitution — asking for a specific provider and quietly getting another is its
|
|
253
|
+
* own kind of wrong answer. `createLLMProvider` deliberately does substitute, which is
|
|
254
|
+
* why its retry loop survives this change and is not redundant.
|
|
255
|
+
*
|
|
256
|
+
* The documented default is unchanged: gemini is first in the preference order, so a
|
|
257
|
+
* Gemini key present still means gemini.
|
|
227
258
|
*/
|
|
228
259
|
function resolveProviderConfig(options) {
|
|
229
|
-
const rawProvider = options?.provider ?? readEnv("LLM_PROVIDER") ?? "gemini";
|
|
230
|
-
if (!VALID_PROVIDERS.has(rawProvider)) return {
|
|
231
|
-
ok: false,
|
|
232
|
-
code: "INVALID_PROVIDER",
|
|
233
|
-
message: `Unknown VRT_LLM_PROVIDER "${rawProvider}". Expected: gemini | anthropic | openrouter.`
|
|
234
|
-
};
|
|
235
|
-
const provider = rawProvider;
|
|
236
|
-
const model = options?.model ?? readEnv("LLM_MODEL") ?? void 0;
|
|
237
260
|
const keyMap = {
|
|
238
261
|
gemini: process.env.GEMINI_API_KEY ?? process.env.GOOGLE_AI_API_KEY,
|
|
239
262
|
anthropic: process.env.ANTHROPIC_API_KEY,
|
|
@@ -244,6 +267,34 @@ function resolveProviderConfig(options) {
|
|
|
244
267
|
anthropic: "ANTHROPIC_API_KEY",
|
|
245
268
|
openrouter: "OPENROUTER_API_KEY"
|
|
246
269
|
};
|
|
270
|
+
/** Gemini first, so the documented default wins whenever its key is present. */
|
|
271
|
+
const PREFERENCE = [
|
|
272
|
+
"gemini",
|
|
273
|
+
"anthropic",
|
|
274
|
+
"openrouter"
|
|
275
|
+
];
|
|
276
|
+
const requested = options?.provider ?? readEnv("LLM_PROVIDER");
|
|
277
|
+
const model = options?.model ?? readEnv("LLM_MODEL") ?? void 0;
|
|
278
|
+
if (requested === void 0) {
|
|
279
|
+
const provider = PREFERENCE.find((p) => keyMap[p]);
|
|
280
|
+
if (!provider) return {
|
|
281
|
+
ok: false,
|
|
282
|
+
code: "MISSING_KEY",
|
|
283
|
+
message: `No LLM API key found. Set one of: ${PREFERENCE.map((p) => envVarName[p]).join(", ")} — or pick a provider explicitly with VLMKIT_LLM_PROVIDER.`
|
|
284
|
+
};
|
|
285
|
+
return {
|
|
286
|
+
ok: true,
|
|
287
|
+
provider,
|
|
288
|
+
key: keyMap[provider],
|
|
289
|
+
model
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
if (!VALID_PROVIDERS.has(requested)) return {
|
|
293
|
+
ok: false,
|
|
294
|
+
code: "INVALID_PROVIDER",
|
|
295
|
+
message: `Unknown VLMKIT_LLM_PROVIDER "${requested}". Expected: gemini | anthropic | openrouter.`
|
|
296
|
+
};
|
|
297
|
+
const provider = requested;
|
|
247
298
|
const key = keyMap[provider];
|
|
248
299
|
if (!key) return {
|
|
249
300
|
ok: false,
|
|
@@ -260,7 +311,7 @@ function resolveProviderConfig(options) {
|
|
|
260
311
|
/**
|
|
261
312
|
* Create a unified LLM client.
|
|
262
313
|
*
|
|
263
|
-
* Provider is set via `
|
|
314
|
+
* Provider is set via `VLMKIT_LLM_PROVIDER` env var (default: `gemini`).
|
|
264
315
|
*
|
|
265
316
|
* Throws `VrtConfigError` when the resolved provider has no API key
|
|
266
317
|
* (`code: "MISSING_KEY"`) or the provider value is unknown
|
|
@@ -282,7 +333,7 @@ function createUnifiedLLMClient(options) {
|
|
|
282
333
|
}
|
|
283
334
|
/**
|
|
284
335
|
* Backwards-compatible: returns the existing `LLMProvider` interface.
|
|
285
|
-
* Uses the provider specified by `
|
|
336
|
+
* Uses the provider specified by `VLMKIT_LLM_PROVIDER`. Falls back to
|
|
286
337
|
* other providers if the requested key is missing.
|
|
287
338
|
*
|
|
288
339
|
* Throws `VrtConfigError` (`code: "NO_PROVIDER_AVAILABLE"`) when no
|
|
@@ -35,7 +35,7 @@ interface FixSuggestion {
|
|
|
35
35
|
interface PipelineConfig {
|
|
36
36
|
/** Stage 1: VLM for image analysis (default: qwen3-vl-8b via OpenRouter) */
|
|
37
37
|
vlmModel?: string;
|
|
38
|
-
/** Stage 2: LLM for code generation (default: from
|
|
38
|
+
/** Stage 2: LLM for code generation (default: from VLMKIT_LLM_PROVIDER) */
|
|
39
39
|
llmProvider?: "gemini" | "anthropic" | "openrouter";
|
|
40
40
|
llmModel?: string;
|
|
41
41
|
/** Image resolution (default: medium = 400x300) */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { VrtConfigError } from "./errors.mjs";
|
|
2
2
|
import { createUnifiedLLMClient } from "./llm-client.mjs";
|
|
3
3
|
import { createVlmClient, resolveModel } from "./vlm-client.mjs";
|
|
4
|
-
import { readEnv } from "@mizchi/vlmkit-core/
|
|
4
|
+
import { readEnv } from "@mizchi/vlmkit-core/project-config.ts";
|
|
5
5
|
import { resizeBase64Png } from "@mizchi/vlmkit-core/image-resize.ts";
|
|
6
6
|
//#region src/reasoning-pipeline.ts
|
|
7
7
|
/**
|
package/dist/vlm-client.d.mts
CHANGED
|
@@ -28,6 +28,15 @@ interface VlmClient {
|
|
|
28
28
|
maxTokens?: number;
|
|
29
29
|
}): Promise<VlmResponse>;
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Drop the cached OpenRouter model list.
|
|
33
|
+
*
|
|
34
|
+
* The cache is process-wide and had no way out, which matters beyond tests: the API server
|
|
35
|
+
* (`src/api/`) and a `--loop` run are long-lived, so a model added or repriced upstream is
|
|
36
|
+
* invisible to them until restart. Named after `resetGateRegistryCache`, which exists for the
|
|
37
|
+
* same reason on the same kind of lazily-built module state.
|
|
38
|
+
*/
|
|
39
|
+
declare function resetVisionModelCache(): void;
|
|
31
40
|
declare function fetchVisionModels(): Promise<VlmModel[]>;
|
|
32
41
|
declare function listModels(options?: {
|
|
33
42
|
maxCost?: number;
|
|
@@ -60,4 +69,4 @@ interface CreateVlmClientOptions {
|
|
|
60
69
|
*/
|
|
61
70
|
declare function createVlmClient(model: VlmModel, options?: CreateVlmClientOptions): Promise<VlmClient | null>;
|
|
62
71
|
//#endregion
|
|
63
|
-
export { CreateVlmClientOptions, VlmClient, VlmModel, VlmResponse, createVlmClient, fetchVisionModels, isClaudeDirectModel, isGeminiDirectModel, listClaudeModels, listGeminiModels, listModels, resolveClaudeModel, resolveGeminiModel, resolveModel };
|
|
72
|
+
export { CreateVlmClientOptions, VlmClient, VlmModel, VlmResponse, createVlmClient, fetchVisionModels, isClaudeDirectModel, isGeminiDirectModel, listClaudeModels, listGeminiModels, listModels, resetVisionModelCache, resolveClaudeModel, resolveGeminiModel, resolveModel };
|
package/dist/vlm-client.mjs
CHANGED
|
@@ -11,6 +11,17 @@ import { readFile } from "node:fs/promises";
|
|
|
11
11
|
* Model list is fetched dynamically from the API.
|
|
12
12
|
*/
|
|
13
13
|
let _cachedModels = null;
|
|
14
|
+
/**
|
|
15
|
+
* Drop the cached OpenRouter model list.
|
|
16
|
+
*
|
|
17
|
+
* The cache is process-wide and had no way out, which matters beyond tests: the API server
|
|
18
|
+
* (`src/api/`) and a `--loop` run are long-lived, so a model added or repriced upstream is
|
|
19
|
+
* invisible to them until restart. Named after `resetGateRegistryCache`, which exists for the
|
|
20
|
+
* same reason on the same kind of lazily-built module state.
|
|
21
|
+
*/
|
|
22
|
+
function resetVisionModelCache() {
|
|
23
|
+
_cachedModels = null;
|
|
24
|
+
}
|
|
14
25
|
async function fetchVisionModels() {
|
|
15
26
|
if (_cachedModels) return _cachedModels;
|
|
16
27
|
const res = await fetch("https://openrouter.ai/api/v1/models");
|
|
@@ -50,9 +61,9 @@ async function resolveModel(idOrIndex) {
|
|
|
50
61
|
const partial = models.filter((m) => m.id.includes(idOrIndex));
|
|
51
62
|
if (partial.length === 1) return partial[0];
|
|
52
63
|
if (partial.length > 1) {
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
55
|
-
throw new VrtConfigError("MULTIPLE_MATCHES", `Ambiguous model "${idOrIndex}". Matches: ${partial.slice(0, 5).map((m) => m.id).join(", ")}\nTip: use more specific ID, e.g. "${partial[0].id}"`);
|
|
64
|
+
const segmentMatches = partial.filter((m) => m.id.split("/").includes(idOrIndex));
|
|
65
|
+
if (segmentMatches.length === 1) return segmentMatches[0];
|
|
66
|
+
throw new VrtConfigError("MULTIPLE_MATCHES", `Ambiguous model "${idOrIndex}". Matches: ${(segmentMatches.length > 1 ? segmentMatches : partial).slice(0, 5).map((m) => m.id).join(", ")}\nTip: use more specific ID, e.g. "${(segmentMatches[0] ?? partial[0]).id}"`);
|
|
56
67
|
}
|
|
57
68
|
const idx = parseInt(idOrIndex, 10);
|
|
58
69
|
if (!isNaN(idx) && idx >= 0 && idx < models.length) return models[idx];
|
|
@@ -364,8 +375,7 @@ async function createVlmClient(model, options) {
|
|
|
364
375
|
const latencyMs = Date.now() - start;
|
|
365
376
|
const usage = data.usage ?? {
|
|
366
377
|
prompt_tokens: 0,
|
|
367
|
-
completion_tokens: 0
|
|
368
|
-
total_tokens: 0
|
|
378
|
+
completion_tokens: 0
|
|
369
379
|
};
|
|
370
380
|
const costUsd = usage.prompt_tokens / 1e3 * model.promptCostPer1k + usage.completion_tokens / 1e3 * model.completionCostPer1k;
|
|
371
381
|
return {
|
|
@@ -373,7 +383,7 @@ async function createVlmClient(model, options) {
|
|
|
373
383
|
model: model.id,
|
|
374
384
|
promptTokens: usage.prompt_tokens,
|
|
375
385
|
completionTokens: usage.completion_tokens,
|
|
376
|
-
totalTokens: usage.total_tokens,
|
|
386
|
+
totalTokens: usage.total_tokens ?? usage.prompt_tokens + usage.completion_tokens,
|
|
377
387
|
costUsd,
|
|
378
388
|
latencyMs
|
|
379
389
|
};
|
|
@@ -427,4 +437,4 @@ async function createVlmClient(model, options) {
|
|
|
427
437
|
return orClient;
|
|
428
438
|
}
|
|
429
439
|
//#endregion
|
|
430
|
-
export { createVlmClient, fetchVisionModels, isClaudeDirectModel, isGeminiDirectModel, listClaudeModels, listGeminiModels, listModels, resolveClaudeModel, resolveGeminiModel, resolveModel };
|
|
440
|
+
export { createVlmClient, fetchVisionModels, isClaudeDirectModel, isGeminiDirectModel, listClaudeModels, listGeminiModels, listModels, resetVisionModelCache, resolveClaudeModel, resolveGeminiModel, resolveModel };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mizchi/vlmkit-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "VLM / LLM clients, reasoning pipeline, and NLP helpers used by VRT to interpret diff images and natural-language intent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "mizchi",
|
|
@@ -37,10 +37,10 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@google/generative-ai": "^0.24.1",
|
|
40
|
-
"@mizchi/vlmkit-core": "0.
|
|
40
|
+
"@mizchi/vlmkit-core": "0.11.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "pnpm --dir ../.. exec tsdown --config tsdown.packages.config.ts --filter @mizchi/vlmkit-ai",
|
|
44
|
-
"test": "
|
|
44
|
+
"test": "pnpm --dir ../.. exec vitest run packages/vlmkit-ai/src"
|
|
45
45
|
}
|
|
46
46
|
}
|