@mizchi/vlmkit-ai 0.6.0 → 0.8.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/package.json +2 -2
- package/src/errors.ts +3 -1
- package/src/image-gen-client.ts +245 -0
- package/src/index.ts +22 -0
- package/src/llm-client.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mizchi/vlmkit-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@google/generative-ai": "^0.24.1",
|
|
29
|
-
"@mizchi/vlmkit-core": "0.
|
|
29
|
+
"@mizchi/vlmkit-core": "0.8.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"test": "node --test 'src/**/*.test.ts'"
|
package/src/errors.ts
CHANGED
|
@@ -26,7 +26,9 @@ export type VrtConfigErrorCode =
|
|
|
26
26
|
/** No provider in the fallback chain has an API key configured. */
|
|
27
27
|
| "NO_PROVIDER_AVAILABLE"
|
|
28
28
|
/** An optional runtime dependency (e.g. `@google/generative-ai`) failed to import. */
|
|
29
|
-
| "MISSING_DEPENDENCY"
|
|
29
|
+
| "MISSING_DEPENDENCY"
|
|
30
|
+
/** Caller-supplied request parameters are malformed (empty prompt, out-of-range size, etc.). */
|
|
31
|
+
| "INVALID_REQUEST";
|
|
30
32
|
|
|
31
33
|
export class VrtConfigError extends Error {
|
|
32
34
|
readonly code: VrtConfigErrorCode;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image generation client (OpenAI gpt-image-2).
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the vlm-client / llm-client split: a small registry of known
|
|
5
|
+
* models, pure builders + parsers that can be unit-tested without a network
|
|
6
|
+
* call, and a `createImageGenClient` factory that wires them to the actual
|
|
7
|
+
* OpenAI Images API.
|
|
8
|
+
*
|
|
9
|
+
* Used by:
|
|
10
|
+
* - `design-runs/game-assets-20260520/run-gpt-image-2.mjs` (dogfood driver)
|
|
11
|
+
* - vlmkit-markup pipelines that need to synthesize reference imagery
|
|
12
|
+
* - downstream callers in `src/cli/workflow/` (added separately)
|
|
13
|
+
*
|
|
14
|
+
* The OpenAI Images endpoint returns base64-encoded PNGs by default; we
|
|
15
|
+
* surface them as `Uint8Array`. Reference-image inputs (edit mode via
|
|
16
|
+
* `/v1/images/edits`) are intentionally out of scope here; add a separate
|
|
17
|
+
* `editImage` method if/when a caller needs it.
|
|
18
|
+
*/
|
|
19
|
+
import { VrtConfigError } from "./errors.ts";
|
|
20
|
+
|
|
21
|
+
// ---- Types ----
|
|
22
|
+
|
|
23
|
+
export type ImageGenSize = "1024x1024" | "1024x1536" | "1536x1024" | "auto";
|
|
24
|
+
export type ImageGenQuality = "low" | "medium" | "high" | "auto";
|
|
25
|
+
export type ImageGenOutputFormat = "png" | "jpeg" | "webp";
|
|
26
|
+
export type ImageGenBackground = "opaque" | "transparent" | "auto";
|
|
27
|
+
|
|
28
|
+
export interface ImageGenModel {
|
|
29
|
+
id: string;
|
|
30
|
+
provider: "openai";
|
|
31
|
+
/** USD per 1M tokens (matches OpenAI's published pricing table). */
|
|
32
|
+
costPer1MInputTextTokens: number;
|
|
33
|
+
costPer1MInputImageTokens: number;
|
|
34
|
+
costPer1MOutputImageTokens: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ImageGenRequest {
|
|
38
|
+
prompt: string;
|
|
39
|
+
size?: ImageGenSize;
|
|
40
|
+
quality?: ImageGenQuality;
|
|
41
|
+
n?: number;
|
|
42
|
+
outputFormat?: ImageGenOutputFormat;
|
|
43
|
+
background?: ImageGenBackground;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ImageGenUsage {
|
|
47
|
+
inputTextTokens: number;
|
|
48
|
+
inputImageTokens: number;
|
|
49
|
+
outputTokens: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ImageGenResponse {
|
|
53
|
+
model: string;
|
|
54
|
+
images: Uint8Array[];
|
|
55
|
+
usage: ImageGenUsage | null;
|
|
56
|
+
costUsd: number;
|
|
57
|
+
latencyMs: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ImageGenRequestBody {
|
|
61
|
+
model: string;
|
|
62
|
+
prompt: string;
|
|
63
|
+
size: ImageGenSize;
|
|
64
|
+
quality: ImageGenQuality;
|
|
65
|
+
n: number;
|
|
66
|
+
output_format: ImageGenOutputFormat;
|
|
67
|
+
background: ImageGenBackground;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ImageGenClient {
|
|
71
|
+
model: ImageGenModel;
|
|
72
|
+
generate(req: ImageGenRequest): Promise<ImageGenResponse>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ---- Registry ----
|
|
76
|
+
|
|
77
|
+
const IMAGE_GEN_MODELS: ImageGenModel[] = [
|
|
78
|
+
{
|
|
79
|
+
id: "gpt-image-2",
|
|
80
|
+
provider: "openai",
|
|
81
|
+
costPer1MInputTextTokens: 5,
|
|
82
|
+
costPer1MInputImageTokens: 8,
|
|
83
|
+
costPer1MOutputImageTokens: 30,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: "gpt-image-2-2026-04-21",
|
|
87
|
+
provider: "openai",
|
|
88
|
+
costPer1MInputTextTokens: 5,
|
|
89
|
+
costPer1MInputImageTokens: 8,
|
|
90
|
+
costPer1MOutputImageTokens: 30,
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
const VALID_SIZES: ReadonlySet<ImageGenSize> = new Set(["1024x1024", "1024x1536", "1536x1024", "auto"]);
|
|
95
|
+
const VALID_QUALITIES: ReadonlySet<ImageGenQuality> = new Set(["low", "medium", "high", "auto"]);
|
|
96
|
+
const VALID_OUTPUT_FORMATS: ReadonlySet<ImageGenOutputFormat> = new Set(["png", "jpeg", "webp"]);
|
|
97
|
+
const VALID_BACKGROUNDS: ReadonlySet<ImageGenBackground> = new Set(["opaque", "transparent", "auto"]);
|
|
98
|
+
|
|
99
|
+
export function listImageGenModels(): ImageGenModel[] {
|
|
100
|
+
return IMAGE_GEN_MODELS.map((m) => ({ ...m }));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function resolveImageGenModel(id: string): ImageGenModel {
|
|
104
|
+
const hit = IMAGE_GEN_MODELS.find((m) => m.id === id);
|
|
105
|
+
if (!hit) {
|
|
106
|
+
throw new VrtConfigError(
|
|
107
|
+
"INVALID_MODEL",
|
|
108
|
+
`Unknown image generation model: "${id}". Known: ${IMAGE_GEN_MODELS.map((m) => m.id).join(", ")}`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return { ...hit };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---- Pure builders / parsers ----
|
|
115
|
+
|
|
116
|
+
export function buildGenerationBody(model: ImageGenModel, req: ImageGenRequest): ImageGenRequestBody {
|
|
117
|
+
if (!req.prompt || !req.prompt.trim()) {
|
|
118
|
+
throw new VrtConfigError("INVALID_REQUEST", "image-gen: prompt must be a non-empty string");
|
|
119
|
+
}
|
|
120
|
+
const size: ImageGenSize = req.size ?? "1024x1024";
|
|
121
|
+
const quality: ImageGenQuality = req.quality ?? "medium";
|
|
122
|
+
const outputFormat: ImageGenOutputFormat = req.outputFormat ?? "png";
|
|
123
|
+
const background: ImageGenBackground = req.background ?? "opaque";
|
|
124
|
+
const n = req.n ?? 1;
|
|
125
|
+
if (!VALID_SIZES.has(size)) {
|
|
126
|
+
throw new VrtConfigError("INVALID_REQUEST", `image-gen: invalid size "${size}"`);
|
|
127
|
+
}
|
|
128
|
+
if (!VALID_QUALITIES.has(quality)) {
|
|
129
|
+
throw new VrtConfigError("INVALID_REQUEST", `image-gen: invalid quality "${quality}"`);
|
|
130
|
+
}
|
|
131
|
+
if (!VALID_OUTPUT_FORMATS.has(outputFormat)) {
|
|
132
|
+
throw new VrtConfigError("INVALID_REQUEST", `image-gen: invalid output_format "${outputFormat}"`);
|
|
133
|
+
}
|
|
134
|
+
if (!VALID_BACKGROUNDS.has(background)) {
|
|
135
|
+
throw new VrtConfigError("INVALID_REQUEST", `image-gen: invalid background "${background}"`);
|
|
136
|
+
}
|
|
137
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
138
|
+
throw new VrtConfigError("INVALID_REQUEST", `image-gen: n must be a positive integer, got ${n}`);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
model: model.id,
|
|
142
|
+
prompt: req.prompt,
|
|
143
|
+
size,
|
|
144
|
+
quality,
|
|
145
|
+
n,
|
|
146
|
+
output_format: outputFormat,
|
|
147
|
+
background,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface RawGenerationResponse {
|
|
152
|
+
data?: Array<{ b64_json?: string; url?: string }>;
|
|
153
|
+
usage?: {
|
|
154
|
+
input_tokens?: number;
|
|
155
|
+
output_tokens?: number;
|
|
156
|
+
total_tokens?: number;
|
|
157
|
+
input_tokens_details?: { text_tokens?: number; image_tokens?: number };
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface ParsedGeneration {
|
|
162
|
+
images: Uint8Array[];
|
|
163
|
+
usage: ImageGenUsage | null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function parseGenerationResponse(json: RawGenerationResponse): ParsedGeneration {
|
|
167
|
+
const images: Uint8Array[] = [];
|
|
168
|
+
for (const entry of json.data ?? []) {
|
|
169
|
+
if (!entry || typeof entry.b64_json !== "string") continue;
|
|
170
|
+
images.push(new Uint8Array(Buffer.from(entry.b64_json, "base64")));
|
|
171
|
+
}
|
|
172
|
+
const u = json.usage;
|
|
173
|
+
const usage: ImageGenUsage | null = u
|
|
174
|
+
? {
|
|
175
|
+
inputTextTokens: u.input_tokens_details?.text_tokens ?? u.input_tokens ?? 0,
|
|
176
|
+
inputImageTokens: u.input_tokens_details?.image_tokens ?? 0,
|
|
177
|
+
outputTokens: u.output_tokens ?? 0,
|
|
178
|
+
}
|
|
179
|
+
: null;
|
|
180
|
+
return { images, usage };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function estimateImageGenCost(model: ImageGenModel, usage: ImageGenUsage | null): number {
|
|
184
|
+
if (!usage) return 0;
|
|
185
|
+
const cost = (usage.inputTextTokens * model.costPer1MInputTextTokens
|
|
186
|
+
+ usage.inputImageTokens * model.costPer1MInputImageTokens
|
|
187
|
+
+ usage.outputTokens * model.costPer1MOutputImageTokens) / 1_000_000;
|
|
188
|
+
return Math.round(cost * 1_000_000) / 1_000_000;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---- Client factory ----
|
|
192
|
+
|
|
193
|
+
export interface CreateImageGenClientOptions {
|
|
194
|
+
apiKey?: string;
|
|
195
|
+
throwIfMissing?: boolean;
|
|
196
|
+
/** Override the API base URL (for tests / proxies). */
|
|
197
|
+
baseUrl?: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function createImageGenClient(
|
|
201
|
+
modelOrId: ImageGenModel | string,
|
|
202
|
+
options?: CreateImageGenClientOptions,
|
|
203
|
+
): ImageGenClient {
|
|
204
|
+
const model = typeof modelOrId === "string" ? resolveImageGenModel(modelOrId) : modelOrId;
|
|
205
|
+
const throwIfMissing = options?.throwIfMissing ?? true;
|
|
206
|
+
const apiKey = options?.apiKey ?? process.env.OPENAI_API_KEY;
|
|
207
|
+
if (!apiKey && throwIfMissing) {
|
|
208
|
+
throw new VrtConfigError("MISSING_KEY", `OPENAI_API_KEY is required for ${model.id}`);
|
|
209
|
+
}
|
|
210
|
+
const baseUrl = options?.baseUrl ?? "https://api.openai.com";
|
|
211
|
+
return {
|
|
212
|
+
model,
|
|
213
|
+
async generate(req: ImageGenRequest): Promise<ImageGenResponse> {
|
|
214
|
+
const body = buildGenerationBody(model, req);
|
|
215
|
+
const started = Date.now();
|
|
216
|
+
const res = await fetch(`${baseUrl}/v1/images/generations`, {
|
|
217
|
+
method: "POST",
|
|
218
|
+
headers: {
|
|
219
|
+
Authorization: `Bearer ${apiKey ?? ""}`,
|
|
220
|
+
"Content-Type": "application/json",
|
|
221
|
+
},
|
|
222
|
+
body: JSON.stringify(body),
|
|
223
|
+
});
|
|
224
|
+
const text = await res.text();
|
|
225
|
+
let json: RawGenerationResponse & { error?: { message?: string } };
|
|
226
|
+
try {
|
|
227
|
+
json = JSON.parse(text);
|
|
228
|
+
} catch {
|
|
229
|
+
throw new Error(`image-gen: non-JSON response from OpenAI (status ${res.status}): ${text.slice(0, 200)}`);
|
|
230
|
+
}
|
|
231
|
+
if (!res.ok) {
|
|
232
|
+
const message = json.error?.message ?? text.slice(0, 200);
|
|
233
|
+
throw new Error(`image-gen ${model.id} failed (${res.status}): ${message}`);
|
|
234
|
+
}
|
|
235
|
+
const parsed = parseGenerationResponse(json);
|
|
236
|
+
return {
|
|
237
|
+
model: model.id,
|
|
238
|
+
images: parsed.images,
|
|
239
|
+
usage: parsed.usage,
|
|
240
|
+
costUsd: estimateImageGenCost(model, parsed.usage),
|
|
241
|
+
latencyMs: Date.now() - started,
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -38,6 +38,28 @@ export {
|
|
|
38
38
|
type VlmResponse,
|
|
39
39
|
} from "./vlm-client.ts";
|
|
40
40
|
|
|
41
|
+
// ---- Image generation clients ----
|
|
42
|
+
export {
|
|
43
|
+
buildGenerationBody,
|
|
44
|
+
createImageGenClient,
|
|
45
|
+
estimateImageGenCost,
|
|
46
|
+
listImageGenModels,
|
|
47
|
+
parseGenerationResponse,
|
|
48
|
+
resolveImageGenModel,
|
|
49
|
+
type CreateImageGenClientOptions,
|
|
50
|
+
type ImageGenBackground,
|
|
51
|
+
type ImageGenClient,
|
|
52
|
+
type ImageGenModel,
|
|
53
|
+
type ImageGenOutputFormat,
|
|
54
|
+
type ImageGenQuality,
|
|
55
|
+
type ImageGenRequest,
|
|
56
|
+
type ImageGenRequestBody,
|
|
57
|
+
type ImageGenResponse,
|
|
58
|
+
type ImageGenSize,
|
|
59
|
+
type ImageGenUsage,
|
|
60
|
+
type ParsedGeneration,
|
|
61
|
+
} from "./image-gen-client.ts";
|
|
62
|
+
|
|
41
63
|
// ---- Reasoning ----
|
|
42
64
|
export {
|
|
43
65
|
createReasoningPipeline,
|
package/src/llm-client.ts
CHANGED
|
@@ -74,7 +74,7 @@ export interface UnifiedLLMClient {
|
|
|
74
74
|
// ---- Anthropic ----
|
|
75
75
|
|
|
76
76
|
function createAnthropicClient(apiKey: string, model?: string): UnifiedLLMClient {
|
|
77
|
-
const modelId = model ?? "claude-sonnet-4-
|
|
77
|
+
const modelId = model ?? "claude-sonnet-4-6";
|
|
78
78
|
|
|
79
79
|
async function call(content: MessageContent, maxTokens: number): Promise<LLMResponse> {
|
|
80
80
|
const start = Date.now();
|