@almadar/llm 2.49.0 → 2.50.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/dist/index.d.ts +34 -5
- package/dist/index.js +97 -16
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/image-client.ts +120 -17
- package/src/index.ts +2 -0
package/dist/index.d.ts
CHANGED
|
@@ -104,15 +104,17 @@ declare function cosineSimilarity(a: ReadonlyArray<number>, b: ReadonlyArray<num
|
|
|
104
104
|
/**
|
|
105
105
|
* Image Client
|
|
106
106
|
*
|
|
107
|
-
* Single-purpose client for
|
|
108
|
-
*
|
|
107
|
+
* Single-purpose client for image-generation APIs. `openrouter` posts to
|
|
108
|
+
* `POST {baseUrl}/images`; `local` posts an OpenAI-shaped body to
|
|
109
|
+
* `POST {baseUrl}/images/generations` (the MFLUX shim in
|
|
110
|
+
* tools/asset-workflow/python/image_server). Raw `fetch`, no SDK — mirrors
|
|
109
111
|
* `embedding-client.ts`'s shape (provider tables, retry policy, the
|
|
110
112
|
* three response-error surfaces).
|
|
111
113
|
*
|
|
112
114
|
* @packageDocumentation
|
|
113
115
|
*/
|
|
114
116
|
|
|
115
|
-
type ImageProvider = 'openrouter';
|
|
117
|
+
type ImageProvider = 'openrouter' | 'local';
|
|
116
118
|
/** Canonical image-provider resolution, mirrors `resolveDefaultEmbeddingProvider`. */
|
|
117
119
|
declare function resolveDefaultImageProvider(): ImageProvider;
|
|
118
120
|
declare const OPENROUTER_IMAGE_MODELS: {
|
|
@@ -126,6 +128,16 @@ declare const OPENROUTER_IMAGE_DEFAULTS: {
|
|
|
126
128
|
readonly sheet: "bytedance-seed/seedream-5-0-lite";
|
|
127
129
|
readonly preview: "google/gemini-3.1-flash-lite-image";
|
|
128
130
|
};
|
|
131
|
+
declare const LOCAL_IMAGE_MODELS: {
|
|
132
|
+
readonly Z_IMAGE_TURBO: "z-image-turbo";
|
|
133
|
+
readonly FLUX_SCHNELL: "flux-schnell";
|
|
134
|
+
readonly FLUX_DEV: "flux-dev";
|
|
135
|
+
};
|
|
136
|
+
declare const LOCAL_IMAGE_DEFAULTS: {
|
|
137
|
+
/** Z-Image-Turbo is the default: public on Hugging Face, no token required (FLUX is gated). */
|
|
138
|
+
readonly sheet: "z-image-turbo";
|
|
139
|
+
readonly preview: "z-image-turbo";
|
|
140
|
+
};
|
|
129
141
|
interface ImageClientOptions {
|
|
130
142
|
provider?: ImageProvider;
|
|
131
143
|
model?: string;
|
|
@@ -196,8 +208,25 @@ declare class ImageClient {
|
|
|
196
208
|
*/
|
|
197
209
|
generateChecked(opts: ImageGenerateOptions): Promise<ImageGenerateResult>;
|
|
198
210
|
private performGenerate;
|
|
199
|
-
/**
|
|
211
|
+
/**
|
|
212
|
+
* OpenAI-compatible local server (the MFLUX shim in tools/asset-workflow).
|
|
213
|
+
* OpenRouter-only fields are reported in `dropped`, never sent. References
|
|
214
|
+
* ARE sent (as `input_references`) — the shim forwards them to mflux's
|
|
215
|
+
* `image_path`/`image_strength` img2img conditioning.
|
|
216
|
+
*/
|
|
217
|
+
private performLocalGenerate;
|
|
218
|
+
private authHeaders;
|
|
219
|
+
private sendGenerate;
|
|
220
|
+
/** Retries on network failure or a 5xx status; any other response is returned as-is.
|
|
221
|
+
* The local provider never retries: a failed local generation (e.g. MLX OOM) cannot
|
|
222
|
+
* succeed on an immediate retry, and hammering a memory-pressured machine is harmful. */
|
|
200
223
|
private fetchWithRetry;
|
|
224
|
+
/** Local generations can exceed Node's built-in 300s headersTimeout (a single
|
|
225
|
+
* MFLUX image routinely takes minutes), so local calls ride npm-undici's own
|
|
226
|
+
* fetch with the transport timers disabled — mirrors `LLMClient.localDispatcher`. */
|
|
227
|
+
private static localDispatcher;
|
|
228
|
+
private static getLocalDispatcher;
|
|
229
|
+
private doFetch;
|
|
201
230
|
}
|
|
202
231
|
declare const IMAGE_PROVIDER_CATALOG: ReadonlyArray<{
|
|
203
232
|
provider: ImageProvider;
|
|
@@ -441,4 +470,4 @@ type LLMServiceActions = {
|
|
|
441
470
|
*/
|
|
442
471
|
type LLMServiceContract = ServiceContract<LLMServiceActions>;
|
|
443
472
|
|
|
444
|
-
export { type ContinuationOptions, type ContinuationResult, type EmbeddingBatchResult, EmbeddingClient, type EmbeddingClientOptions, type EmbeddingProvider, type EmbeddingResult, type EmbeddingUsage, type GeneratedImage, IMAGE_PROVIDER_CATALOG, ImageClient, type ImageClientOptions, type ImageGenerateOptions, type ImageGenerateResult, type ImageModelCapabilities, type ImageProvider, type ImageReference, LLMClient, LLMFinishReason, type LLMServiceActions, type LLMServiceContract, OPENROUTER_IMAGE_DEFAULTS, OPENROUTER_IMAGE_MODELS, type TruncationReason, type TruncationResult, buildGenericContinuationPrompt, callWithContinuation, cosineSimilarity, detectTruncation, findLastCompleteElement, isLikelyTruncated, mergeResponses, resolveDefaultEmbeddingProvider, resolveDefaultImageProvider, salvagePartialResponse };
|
|
473
|
+
export { type ContinuationOptions, type ContinuationResult, type EmbeddingBatchResult, EmbeddingClient, type EmbeddingClientOptions, type EmbeddingProvider, type EmbeddingResult, type EmbeddingUsage, type GeneratedImage, IMAGE_PROVIDER_CATALOG, ImageClient, type ImageClientOptions, type ImageGenerateOptions, type ImageGenerateResult, type ImageModelCapabilities, type ImageProvider, type ImageReference, LLMClient, LLMFinishReason, type LLMServiceActions, type LLMServiceContract, LOCAL_IMAGE_DEFAULTS, LOCAL_IMAGE_MODELS, OPENROUTER_IMAGE_DEFAULTS, OPENROUTER_IMAGE_MODELS, type TruncationReason, type TruncationResult, buildGenericContinuationPrompt, callWithContinuation, cosineSimilarity, detectTruncation, findLastCompleteElement, isLikelyTruncated, mergeResponses, resolveDefaultEmbeddingProvider, resolveDefaultImageProvider, salvagePartialResponse };
|
package/dist/index.js
CHANGED
|
@@ -199,7 +199,8 @@ function cosineSimilarity(a, b) {
|
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
// src/image-client.ts
|
|
202
|
-
|
|
202
|
+
import { Agent as UndiciAgent, fetch as undiciFetch } from "undici";
|
|
203
|
+
var IMAGE_PROVIDERS = ["openrouter", "local"];
|
|
203
204
|
function resolveDefaultImageProvider() {
|
|
204
205
|
const v = process.env.IMAGE_PROVIDER;
|
|
205
206
|
return v && IMAGE_PROVIDERS.includes(v) ? v : "openrouter";
|
|
@@ -215,16 +216,29 @@ var OPENROUTER_IMAGE_DEFAULTS = {
|
|
|
215
216
|
sheet: OPENROUTER_IMAGE_MODELS.SEEDREAM_5_LITE,
|
|
216
217
|
preview: OPENROUTER_IMAGE_MODELS.GEMINI_3_1_FLASH_LITE_IMAGE
|
|
217
218
|
};
|
|
219
|
+
var LOCAL_IMAGE_MODELS = {
|
|
220
|
+
Z_IMAGE_TURBO: "z-image-turbo",
|
|
221
|
+
FLUX_SCHNELL: "flux-schnell",
|
|
222
|
+
FLUX_DEV: "flux-dev"
|
|
223
|
+
};
|
|
224
|
+
var LOCAL_IMAGE_DEFAULTS = {
|
|
225
|
+
/** Z-Image-Turbo is the default: public on Hugging Face, no token required (FLUX is gated). */
|
|
226
|
+
sheet: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO,
|
|
227
|
+
preview: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO
|
|
228
|
+
};
|
|
218
229
|
var IMAGE_MAX_ATTEMPTS = 3;
|
|
219
230
|
var IMAGE_RETRY_BACKOFF_MS = [500, 2e3];
|
|
220
231
|
var DEFAULT_MODELS2 = {
|
|
221
|
-
openrouter: OPENROUTER_IMAGE_DEFAULTS.sheet
|
|
232
|
+
openrouter: OPENROUTER_IMAGE_DEFAULTS.sheet,
|
|
233
|
+
local: LOCAL_IMAGE_DEFAULTS.sheet
|
|
222
234
|
};
|
|
223
235
|
var DEFAULT_BASE_URLS2 = {
|
|
224
|
-
openrouter: "https://openrouter.ai/api/v1"
|
|
236
|
+
openrouter: "https://openrouter.ai/api/v1",
|
|
237
|
+
local: "http://127.0.0.1:8180/v1"
|
|
225
238
|
};
|
|
226
239
|
var API_KEY_ENV_VARS2 = {
|
|
227
|
-
openrouter: "OPEN_ROUTER_API_KEY"
|
|
240
|
+
openrouter: "OPEN_ROUTER_API_KEY",
|
|
241
|
+
local: "LOCAL_IMAGE_API_KEY"
|
|
228
242
|
};
|
|
229
243
|
var IMAGE_CHECKED_PARAM_NAMES = {
|
|
230
244
|
background: "background",
|
|
@@ -272,15 +286,15 @@ function collectSupportedParameters(body) {
|
|
|
272
286
|
}
|
|
273
287
|
return params;
|
|
274
288
|
}
|
|
275
|
-
var
|
|
289
|
+
var _ImageClient = class _ImageClient {
|
|
276
290
|
constructor(opts = {}) {
|
|
277
291
|
this.capabilitiesCache = /* @__PURE__ */ new Map();
|
|
278
|
-
this.provider = opts.provider ??
|
|
292
|
+
this.provider = opts.provider ?? resolveDefaultImageProvider();
|
|
279
293
|
this.model = opts.model ?? DEFAULT_MODELS2[this.provider];
|
|
280
294
|
this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URLS2[this.provider]).replace(/\/$/, "");
|
|
281
295
|
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
282
|
-
const apiKey = opts.apiKey ?? process.env[API_KEY_ENV_VARS2[this.provider]];
|
|
283
|
-
if (!apiKey) {
|
|
296
|
+
const apiKey = opts.apiKey ?? process.env[API_KEY_ENV_VARS2[this.provider]] ?? "";
|
|
297
|
+
if (!apiKey && this.provider !== "local") {
|
|
284
298
|
throw new Error(
|
|
285
299
|
`ImageClient: API key for provider '${this.provider}' not found. Set ${API_KEY_ENV_VARS2[this.provider]} in your environment or pass options.apiKey.`
|
|
286
300
|
);
|
|
@@ -300,9 +314,14 @@ var ImageClient = class {
|
|
|
300
314
|
const model = modelId ?? this.model;
|
|
301
315
|
const cached = this.capabilitiesCache.get(model);
|
|
302
316
|
if (cached) return cached;
|
|
317
|
+
if (this.provider === "local") {
|
|
318
|
+
const local = { model, supportedParameters: /* @__PURE__ */ new Set(["seed", IMAGE_CHECKED_PARAM_NAMES.references]), raw: {} };
|
|
319
|
+
this.capabilitiesCache.set(model, local);
|
|
320
|
+
return local;
|
|
321
|
+
}
|
|
303
322
|
const response = await this.fetchWithRetry(`${this.baseUrl}/images/models/${model}/endpoints`, {
|
|
304
323
|
method: "GET",
|
|
305
|
-
headers:
|
|
324
|
+
headers: this.authHeaders()
|
|
306
325
|
});
|
|
307
326
|
if (!response.ok) {
|
|
308
327
|
const errorText = await response.text().catch(() => "<no body>");
|
|
@@ -345,6 +364,9 @@ var ImageClient = class {
|
|
|
345
364
|
throw new Error("ImageClient.generate: `size` cannot be combined with `resolution`/`aspectRatio` \u2014 pass one or the other");
|
|
346
365
|
}
|
|
347
366
|
const model = opts.model ?? this.model;
|
|
367
|
+
if (this.provider === "local") {
|
|
368
|
+
return this.performLocalGenerate(opts, model, dropped);
|
|
369
|
+
}
|
|
348
370
|
const requestBody = { model, prompt: opts.prompt, output_format: "png" };
|
|
349
371
|
if (opts.n !== void 0) requestBody.n = opts.n;
|
|
350
372
|
if (opts.size !== void 0) requestBody.size = opts.size;
|
|
@@ -363,9 +385,35 @@ var ImageClient = class {
|
|
|
363
385
|
if (opts.provider.allowFallbacks !== void 0) providerField.allow_fallbacks = opts.provider.allowFallbacks;
|
|
364
386
|
requestBody.provider = providerField;
|
|
365
387
|
}
|
|
366
|
-
|
|
388
|
+
return this.sendGenerate(`${this.baseUrl}/images`, requestBody, model, dropped);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* OpenAI-compatible local server (the MFLUX shim in tools/asset-workflow).
|
|
392
|
+
* OpenRouter-only fields are reported in `dropped`, never sent. References
|
|
393
|
+
* ARE sent (as `input_references`) — the shim forwards them to mflux's
|
|
394
|
+
* `image_path`/`image_strength` img2img conditioning.
|
|
395
|
+
*/
|
|
396
|
+
async performLocalGenerate(opts, model, dropped) {
|
|
397
|
+
const requestBody = { model, prompt: opts.prompt };
|
|
398
|
+
if (opts.n !== void 0) requestBody.n = opts.n;
|
|
399
|
+
if (opts.size !== void 0) requestBody.size = opts.size;
|
|
400
|
+
if (opts.seed !== void 0) requestBody.seed = opts.seed;
|
|
401
|
+
if (opts.references !== void 0 && opts.references.length > 0) {
|
|
402
|
+
requestBody.input_references = opts.references.map((r) => ({ type: "image_url", image_url: { url: r.url } }));
|
|
403
|
+
}
|
|
404
|
+
for (const field of ["resolution", "aspectRatio", "quality", "background"]) {
|
|
405
|
+
if (opts[field] !== void 0) dropped.push(field);
|
|
406
|
+
}
|
|
407
|
+
if (opts.provider !== void 0) dropped.push("provider");
|
|
408
|
+
return this.sendGenerate(`${this.baseUrl}/images/generations`, requestBody, model, dropped);
|
|
409
|
+
}
|
|
410
|
+
authHeaders() {
|
|
411
|
+
return this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {};
|
|
412
|
+
}
|
|
413
|
+
async sendGenerate(url, requestBody, model, dropped) {
|
|
414
|
+
const response = await this.fetchWithRetry(url, {
|
|
367
415
|
method: "POST",
|
|
368
|
-
headers: { "Content-Type": "application/json",
|
|
416
|
+
headers: { "Content-Type": "application/json", ...this.authHeaders() },
|
|
369
417
|
body: JSON.stringify(requestBody)
|
|
370
418
|
});
|
|
371
419
|
if (!response.ok) {
|
|
@@ -403,31 +451,62 @@ var ImageClient = class {
|
|
|
403
451
|
};
|
|
404
452
|
return { images, usage, model, dropped };
|
|
405
453
|
}
|
|
406
|
-
/** Retries on network failure or a 5xx status; any other response is returned as-is.
|
|
454
|
+
/** Retries on network failure or a 5xx status; any other response is returned as-is.
|
|
455
|
+
* The local provider never retries: a failed local generation (e.g. MLX OOM) cannot
|
|
456
|
+
* succeed on an immediate retry, and hammering a memory-pressured machine is harmful. */
|
|
407
457
|
async fetchWithRetry(url, init) {
|
|
458
|
+
const maxAttempts = this.provider === "local" ? 1 : IMAGE_MAX_ATTEMPTS;
|
|
408
459
|
let lastNetworkError;
|
|
409
460
|
let response;
|
|
410
|
-
for (let attempt = 1; attempt <=
|
|
461
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
411
462
|
try {
|
|
412
|
-
response = await this.
|
|
463
|
+
response = await this.doFetch(url, init);
|
|
413
464
|
} catch (e) {
|
|
414
465
|
response = void 0;
|
|
415
466
|
lastNetworkError = e instanceof Error ? e : new Error(String(e));
|
|
416
467
|
}
|
|
417
468
|
if (response !== void 0 && response.status < 500) return response;
|
|
418
469
|
const backoff = IMAGE_RETRY_BACKOFF_MS[attempt - 1];
|
|
419
|
-
if (backoff === void 0) break;
|
|
470
|
+
if (backoff === void 0 || attempt >= maxAttempts) break;
|
|
420
471
|
await new Promise((r) => setTimeout(r, backoff));
|
|
421
472
|
}
|
|
422
473
|
if (response) return response;
|
|
423
|
-
throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${
|
|
474
|
+
throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${maxAttempts} attempts`);
|
|
475
|
+
}
|
|
476
|
+
static getLocalDispatcher() {
|
|
477
|
+
if (_ImageClient.localDispatcher === null) {
|
|
478
|
+
_ImageClient.localDispatcher = new UndiciAgent({ headersTimeout: 0, bodyTimeout: 0 });
|
|
479
|
+
}
|
|
480
|
+
return _ImageClient.localDispatcher;
|
|
481
|
+
}
|
|
482
|
+
async doFetch(url, init) {
|
|
483
|
+
if (this.provider === "local" && this.fetchImpl === fetch) {
|
|
484
|
+
const uinit = {
|
|
485
|
+
method: init.method,
|
|
486
|
+
headers: init.headers,
|
|
487
|
+
body: init.body,
|
|
488
|
+
dispatcher: _ImageClient.getLocalDispatcher()
|
|
489
|
+
};
|
|
490
|
+
return undiciFetch(url, uinit);
|
|
491
|
+
}
|
|
492
|
+
return this.fetchImpl(url, init);
|
|
424
493
|
}
|
|
425
494
|
};
|
|
495
|
+
/** Local generations can exceed Node's built-in 300s headersTimeout (a single
|
|
496
|
+
* MFLUX image routinely takes minutes), so local calls ride npm-undici's own
|
|
497
|
+
* fetch with the transport timers disabled — mirrors `LLMClient.localDispatcher`. */
|
|
498
|
+
_ImageClient.localDispatcher = null;
|
|
499
|
+
var ImageClient = _ImageClient;
|
|
426
500
|
var IMAGE_PROVIDER_CATALOG = [
|
|
427
501
|
{
|
|
428
502
|
provider: "openrouter",
|
|
429
503
|
models: Object.values(OPENROUTER_IMAGE_MODELS),
|
|
430
504
|
defaultModel: OPENROUTER_IMAGE_DEFAULTS.sheet
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
provider: "local",
|
|
508
|
+
models: Object.values(LOCAL_IMAGE_MODELS),
|
|
509
|
+
defaultModel: LOCAL_IMAGE_DEFAULTS.sheet
|
|
431
510
|
}
|
|
432
511
|
];
|
|
433
512
|
|
|
@@ -833,6 +912,8 @@ export {
|
|
|
833
912
|
ImageClient,
|
|
834
913
|
KIMI_MODELS,
|
|
835
914
|
LLMClient,
|
|
915
|
+
LOCAL_IMAGE_DEFAULTS,
|
|
916
|
+
LOCAL_IMAGE_MODELS,
|
|
836
917
|
MasarError,
|
|
837
918
|
MasarProvider,
|
|
838
919
|
OPENAI_MODELS,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/embedding-client.ts","../src/image-client.ts","../src/truncation-detector.ts","../src/continuation.ts"],"sourcesContent":["/**\n * Embedding Client\n *\n * Single-purpose client for text embeddings. Used by `@almadar-io/agent`'s\n * cosine-similarity catalog retrieval (rank organisms + atoms against the\n * user's request before rendering Stage A's prompt) and by\n * `@almadar/std`'s publish-time embedding bake step.\n *\n * Providers:\n * - `openai` (default model `text-embedding-3-small`, 1536-d) — requires\n * `OPENAI_API_KEY`.\n * - `openrouter` (default model `baai/bge-base-en-v1.5`, 768-d) — requires\n * `OPEN_ROUTER_API_KEY`. Same OpenAI-compatible request shape, just a\n * different base URL.\n * - `lmstudio` (default model `text-embedding-bge-base-en-v1.5`, same\n * bge-base-en-v1.5 weights as the openrouter default, run locally) —\n * local LM Studio server, no API key. Mirrors `client.ts`'s\n * `PROVIDER_CONFIGS.lmstudio`: base URL from `LMSTUDIO_URL` (default\n * `http://127.0.0.1:1234/v1`), model from `LMSTUDIO_EMBEDDING_MODEL`.\n *\n * All three providers return the same response envelope (`{data:[{embedding,index}]}`),\n * so the request/response code is shared.\n *\n * @packageDocumentation\n */\n\nexport type EmbeddingProvider = 'openai' | 'openrouter' | 'lmstudio';\n\nconst EMBEDDING_PROVIDERS: readonly EmbeddingProvider[] = ['openai', 'openrouter', 'lmstudio'];\n\n/**\n * Canonical embedding-provider resolution — the ONE place every downstream\n * consumer should defer to instead of hardcoding a literal provider.\n * Mirrors the `EMBEDDING_PROVIDER` env var `@almadar-io/integrations`'s\n * `RuntimeIntegrationManager`/`getEmbeddings()` already reads (its own\n * `?? 'openrouter'` fallback is the precedent this centralizes).\n *\n * Defaults to `openrouter` — UNCHANGED from every existing deployment's\n * behavior — so nothing (cloud/server included) switches to a local-only\n * provider unless an operator explicitly sets `EMBEDDING_PROVIDER=lmstudio`\n * in that specific environment (e.g. a dev machine running LM Studio).\n * Consumers: `@almadar-io/rabit`'s catalog narrower\n * (`runtime/services/embedding.ts`) and `@almadar/workspace`'s index\n * embedder (`workspace-index/embedder.ts`).\n */\nexport function resolveDefaultEmbeddingProvider(): EmbeddingProvider {\n const v = process.env.EMBEDDING_PROVIDER;\n return v && (EMBEDDING_PROVIDERS as readonly string[]).includes(v) ? (v as EmbeddingProvider) : 'openrouter';\n}\n\nexport interface EmbeddingClientOptions {\n provider: EmbeddingProvider;\n /** Defaults: openai → text-embedding-3-small, openrouter → baai/bge-base-en-v1.5, lmstudio → text-embedding-bge-base-en-v1.5. */\n model?: string;\n /** Override API key. Defaults to provider-specific env var; lmstudio needs none. */\n apiKey?: string;\n /** Override base URL. Defaults to provider canonical endpoint (lmstudio: `LMSTUDIO_URL`). */\n baseUrl?: string;\n}\n\nexport interface EmbeddingUsage {\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface EmbeddingResult {\n embedding: readonly number[];\n usage: EmbeddingUsage;\n}\n\nexport interface EmbeddingBatchResult {\n embeddings: readonly (readonly number[])[];\n usage: EmbeddingUsage;\n}\n\ninterface OpenAIEmbeddingApiResponse {\n data: Array<{ embedding: number[]; index: number }>;\n usage?: { prompt_tokens?: number; total_tokens?: number };\n /** OpenRouter reports provider hiccups as HTTP 200 with an error envelope and no `data`. */\n error?: { message?: string; code?: number | string };\n}\n\n/** Transient-failure retry policy: attempts and backoff (ms) between them. */\nconst EMBED_MAX_ATTEMPTS = 3;\nconst EMBED_RETRY_BACKOFF_MS = [500, 2000];\n\n/** Provider rejects an input that exceeds the model's context window (bge: 512 tokens). */\nconst TOKEN_OVERFLOW_RE =\n /context length|maximum input length|reduce the length of the input|input_tokens/i;\n\nconst DEFAULT_MODELS: Record<EmbeddingProvider, string> = {\n openai: 'text-embedding-3-small',\n openrouter: 'baai/bge-base-en-v1.5',\n lmstudio: 'text-embedding-bge-base-en-v1.5',\n};\n\nconst DEFAULT_BASE_URLS: Record<EmbeddingProvider, string> = {\n openai: 'https://api.openai.com/v1',\n openrouter: 'https://openrouter.ai/api/v1',\n lmstudio: 'http://127.0.0.1:1234/v1',\n};\n\nconst API_KEY_ENV_VARS: Partial<Record<EmbeddingProvider, string>> = {\n openai: 'OPENAI_API_KEY',\n openrouter: 'OPEN_ROUTER_API_KEY',\n};\n\nexport class EmbeddingClient {\n private readonly provider: EmbeddingProvider;\n private readonly model: string;\n private readonly apiKey: string;\n private readonly baseUrl: string;\n\n constructor(opts: EmbeddingClientOptions) {\n this.provider = opts.provider;\n this.model = opts.model ?? (this.provider === 'lmstudio' ? process.env.LMSTUDIO_EMBEDDING_MODEL ?? DEFAULT_MODELS.lmstudio : DEFAULT_MODELS[this.provider]);\n const defaultBaseUrl = this.provider === 'lmstudio' ? process.env.LMSTUDIO_URL ?? DEFAULT_BASE_URLS.lmstudio : DEFAULT_BASE_URLS[this.provider];\n this.baseUrl = (opts.baseUrl ?? defaultBaseUrl).replace(/\\/$/, '');\n\n if (this.provider === 'lmstudio') {\n // Local LM Studio server — no credential to check, mirrors client.ts's PROVIDER_CONFIGS.lmstudio.\n this.apiKey = opts.apiKey ?? 'not-needed';\n return;\n }\n const apiKey = opts.apiKey ?? this.resolveApiKey();\n if (!apiKey) {\n throw new Error(\n `EmbeddingClient: API key for provider '${this.provider}' not found. ` +\n `Set ${API_KEY_ENV_VARS[this.provider]} in your environment or pass options.apiKey.`,\n );\n }\n this.apiKey = apiKey;\n }\n\n /** Embed a single text. */\n async embed(text: string): Promise<EmbeddingResult> {\n const batch = await this.embedBatch([text]);\n const embedding = batch.embeddings[0];\n if (!embedding) {\n throw new Error('EmbeddingClient.embed: provider returned no embedding');\n }\n return { embedding, usage: batch.usage };\n }\n\n /**\n * Embed a batch of texts. Used by `@almadar/std`'s publish-time\n * `build-embeddings.ts` script — one batch call for the full catalog\n * (≈190 entries) rather than per-entry requests.\n */\n async embedBatch(texts: ReadonlyArray<string>): Promise<EmbeddingBatchResult> {\n if (texts.length === 0) {\n return { embeddings: [], usage: { promptTokens: 0, totalTokens: 0 } };\n }\n // bge-base-en-v1.5 has a 512-token context limit. Chars-per-token varies\n // with density (~4 for typical English, ~2.5-3 for dense technical/code\n // text), so no fixed char cap can guarantee a fit. Start at 2000 chars and,\n // when the provider rejects an input for exceeding the context window,\n // halve the cap and retry — the provider's own token count is the source\n // of truth, not a chars-per-token guess.\n let charCap = 2000;\n let lastError: Error = new Error('EmbeddingClient: unreachable');\n for (let attempt = 1; attempt <= EMBED_MAX_ATTEMPTS; attempt++) {\n const trimmed = texts.map((t) => (t.length > charCap ? t.slice(0, charCap) : t));\n try {\n return await this.embedBatchOnce(texts.length, trimmed);\n } catch (e) {\n lastError = e instanceof Error ? e : new Error(String(e));\n if (TOKEN_OVERFLOW_RE.test(lastError.message) && charCap > 500) {\n charCap = Math.floor(charCap / 2);\n continue;\n }\n const backoff = EMBED_RETRY_BACKOFF_MS[attempt - 1];\n if (backoff === undefined) break;\n await new Promise((r) => setTimeout(r, backoff));\n }\n }\n throw lastError;\n }\n\n private async embedBatchOnce(\n inputCount: number,\n trimmed: ReadonlyArray<string>,\n ): Promise<EmbeddingBatchResult> {\n const response = await fetch(`${this.baseUrl}/embeddings`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify({ model: this.model, input: trimmed }),\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `EmbeddingClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const rawBody = await response.text();\n let body: OpenAIEmbeddingApiResponse;\n try {\n body = JSON.parse(rawBody) as OpenAIEmbeddingApiResponse;\n } catch {\n throw new Error(\n `EmbeddingClient: ${this.provider} returned unparseable body: ${rawBody}`,\n );\n }\n if (!Array.isArray(body.data)) {\n // OpenRouter reports provider hiccups as 200 + `{error}` with no data —\n // surface the real body instead of a blind \"missing data\" (G9: this\n // error was undiagnosable for weeks because the body was discarded).\n const detail = body.error?.message ?? rawBody;\n throw new Error(\n `EmbeddingClient: ${this.provider} response has no \\`data\\` array: ${detail}`,\n );\n }\n const ordered = [...body.data].sort((a, b) => a.index - b.index);\n if (ordered.length !== inputCount) {\n throw new Error(\n `EmbeddingClient: provider returned ${ordered.length} embeddings for ${inputCount} inputs`,\n );\n }\n return {\n embeddings: ordered.map((d) => d.embedding),\n usage: {\n promptTokens: body.usage?.prompt_tokens ?? 0,\n totalTokens: body.usage?.total_tokens ?? 0,\n },\n };\n }\n\n private resolveApiKey(): string | undefined {\n const envVar = API_KEY_ENV_VARS[this.provider];\n return envVar ? process.env[envVar] : undefined;\n }\n}\n\n/**\n * Cosine similarity between two equal-length vectors. Returns a value in\n * [-1, 1]; higher means more similar. Used by `@almadar-io/agent`'s\n * catalog retrieval to rank organism/atom descriptions against the user\n * request embedding.\n *\n * Defensive: returns 0 when either vector is the zero vector (would\n * otherwise divide by zero).\n */\nexport function cosineSimilarity(a: ReadonlyArray<number>, b: ReadonlyArray<number>): number {\n if (a.length !== b.length) {\n throw new Error(`cosineSimilarity: vector length mismatch (${a.length} vs ${b.length})`);\n }\n let dot = 0;\n let normA = 0;\n let normB = 0;\n for (let i = 0; i < a.length; i++) {\n const av = a[i];\n const bv = b[i];\n dot += av * bv;\n normA += av * av;\n normB += bv * bv;\n }\n if (normA === 0 || normB === 0) return 0;\n return dot / (Math.sqrt(normA) * Math.sqrt(normB));\n}\n","/**\n * Image Client\n *\n * Single-purpose client for OpenRouter's image-generation API\n * (`POST {baseUrl}/images`). Raw `fetch`, no SDK — mirrors\n * `embedding-client.ts`'s shape (provider tables, retry policy, the\n * three response-error surfaces).\n *\n * @packageDocumentation\n */\n\nimport type { JsonValue } from '@almadar/core';\n\nexport type ImageProvider = 'openrouter';\n\nconst IMAGE_PROVIDERS: readonly ImageProvider[] = ['openrouter'];\n\n/** Canonical image-provider resolution, mirrors `resolveDefaultEmbeddingProvider`. */\nexport function resolveDefaultImageProvider(): ImageProvider {\n const v = process.env.IMAGE_PROVIDER;\n return v && (IMAGE_PROVIDERS as readonly string[]).includes(v) ? (v as ImageProvider) : 'openrouter';\n}\n\nexport const OPENROUTER_IMAGE_MODELS = {\n SEEDREAM_5_LITE: 'bytedance-seed/seedream-5-0-lite',\n SEEDREAM_4_5: 'bytedance-seed/seedream-4.5',\n GEMINI_3_1_FLASH_LITE_IMAGE: 'google/gemini-3.1-flash-lite-image',\n GEMINI_2_5_FLASH_IMAGE: 'google/gemini-2.5-flash-image',\n FLUX_2_PRO: 'black-forest-labs/flux.2-pro',\n} as const;\n\nexport const OPENROUTER_IMAGE_DEFAULTS = {\n sheet: OPENROUTER_IMAGE_MODELS.SEEDREAM_5_LITE,\n preview: OPENROUTER_IMAGE_MODELS.GEMINI_3_1_FLASH_LITE_IMAGE,\n} as const;\n\nexport interface ImageClientOptions {\n provider?: ImageProvider;\n model?: string;\n apiKey?: string;\n baseUrl?: string;\n fetchImpl?: typeof fetch;\n}\n\n/** An input reference image — `https:` or `data:` URL. Transport only, no fetching/validation. */\nexport interface ImageReference {\n url: string;\n}\n\nexport interface ImageGenerateOptions {\n prompt: string;\n model?: string;\n n?: number;\n size?: `${number}x${number}`;\n resolution?: '512' | '1K' | '2K' | '4K';\n aspectRatio?: string;\n quality?: 'auto' | 'low' | 'medium' | 'high';\n background?: 'auto' | 'transparent' | 'opaque';\n seed?: number;\n references?: ReadonlyArray<ImageReference>;\n provider?: { order?: string[]; only?: string[]; allowFallbacks?: boolean };\n}\n\nexport interface GeneratedImage {\n bytes: Uint8Array;\n mediaType: string;\n}\n\nexport interface ImageGenerateResult {\n images: GeneratedImage[];\n usage: { cost: number; totalTokens: number };\n model: string;\n dropped: string[];\n}\n\nexport interface ImageModelCapabilities {\n model: string;\n supportedParameters: ReadonlySet<string>;\n raw: JsonValue;\n}\n\n/** Transient-failure retry policy: attempts and backoff (ms) between them. */\nconst IMAGE_MAX_ATTEMPTS = 3;\nconst IMAGE_RETRY_BACKOFF_MS = [500, 2000];\n\nconst DEFAULT_MODELS: Record<ImageProvider, string> = {\n openrouter: OPENROUTER_IMAGE_DEFAULTS.sheet,\n};\n\nconst DEFAULT_BASE_URLS: Record<ImageProvider, string> = {\n openrouter: 'https://openrouter.ai/api/v1',\n};\n\nconst API_KEY_ENV_VARS: Record<ImageProvider, string> = {\n openrouter: 'OPEN_ROUTER_API_KEY',\n};\n\n/**\n * Request field → OpenRouter `supported_parameters` name. The single\n * place `generateChecked` reads to decide what to strip — change the\n * mapping here, not at each call site.\n */\nconst IMAGE_CHECKED_PARAM_NAMES = {\n background: 'background',\n seed: 'seed',\n references: 'input_references',\n} as const;\n\nfunction isJsonObject(value: JsonValue): value is { [key: string]: JsonValue } {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\nfunction asString(value: JsonValue | undefined): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nfunction asNumber(value: JsonValue | undefined): number | undefined {\n return typeof value === 'number' ? value : undefined;\n}\n\nfunction parseJsonBody(provider: ImageProvider, rawBody: string): JsonValue {\n try {\n return JSON.parse(rawBody) as JsonValue;\n } catch {\n throw new Error(`ImageClient: ${provider} returned unparseable body: ${rawBody}`);\n }\n}\n\nfunction decodeBase64(b64: string): Uint8Array {\n if (typeof globalThis.Buffer !== 'undefined') {\n return new Uint8Array(globalThis.Buffer.from(b64, 'base64'));\n }\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return bytes;\n}\n\n/**\n * Collects the union of `supported_parameters` across a `.../endpoints`\n * response's `data`/`endpoints` arrays. Returns an empty set (never\n * throws) when the shape doesn't match — the caller still gets `raw`.\n */\nfunction collectSupportedParameters(body: JsonValue): ReadonlySet<string> {\n const params = new Set<string>();\n if (!isJsonObject(body)) return params;\n const candidateArrays: JsonValue[][] = [];\n if (Array.isArray(body.data)) candidateArrays.push(body.data);\n if (Array.isArray(body.endpoints)) candidateArrays.push(body.endpoints);\n for (const arr of candidateArrays) {\n for (const entry of arr) {\n if (!isJsonObject(entry)) continue;\n const sp = entry.supported_parameters;\n if (!Array.isArray(sp)) continue;\n for (const p of sp) if (typeof p === 'string') params.add(p);\n }\n }\n return params;\n}\n\nexport class ImageClient {\n private readonly provider: ImageProvider;\n private readonly model: string;\n private readonly apiKey: string;\n private readonly baseUrl: string;\n private readonly fetchImpl: typeof fetch;\n private readonly capabilitiesCache = new Map<string, ImageModelCapabilities>();\n\n constructor(opts: ImageClientOptions = {}) {\n this.provider = opts.provider ?? 'openrouter';\n this.model = opts.model ?? DEFAULT_MODELS[this.provider];\n this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URLS[this.provider]).replace(/\\/$/, '');\n this.fetchImpl = opts.fetchImpl ?? fetch;\n\n const apiKey = opts.apiKey ?? process.env[API_KEY_ENV_VARS[this.provider]];\n if (!apiKey) {\n throw new Error(\n `ImageClient: API key for provider '${this.provider}' not found. ` +\n `Set ${API_KEY_ENV_VARS[this.provider]} in your environment or pass options.apiKey.`,\n );\n }\n this.apiKey = apiKey;\n }\n\n /** Generate images. Never strips request fields — `dropped` is always `[]`. */\n async generate(opts: ImageGenerateOptions): Promise<ImageGenerateResult> {\n return this.performGenerate(opts, []);\n }\n\n /**\n * Per-model `.../endpoints` capabilities, memoized per instance per\n * model. `supportedParameters` is the empty set (not a throw) when the\n * response shape doesn't match what's expected.\n */\n async capabilities(modelId?: string): Promise<ImageModelCapabilities> {\n const model = modelId ?? this.model;\n const cached = this.capabilitiesCache.get(model);\n if (cached) return cached;\n\n const response = await this.fetchWithRetry(`${this.baseUrl}/images/models/${model}/endpoints`, {\n method: 'GET',\n headers: { Authorization: `Bearer ${this.apiKey}` },\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `ImageClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const body = parseJsonBody(this.provider, await response.text());\n const result: ImageModelCapabilities = { model, supportedParameters: collectSupportedParameters(body), raw: body };\n this.capabilitiesCache.set(model, result);\n return result;\n }\n\n /**\n * Like `generate`, but first reads `capabilities(model)` and strips\n * any of `background`/`seed`/`references` the model doesn't list in\n * `supported_parameters` — their (OpenRouter) names come back in\n * `dropped` rather than being silently swallowed.\n */\n async generateChecked(opts: ImageGenerateOptions): Promise<ImageGenerateResult> {\n const model = opts.model ?? this.model;\n const caps = await this.capabilities(model);\n const dropped: string[] = [];\n const filtered: ImageGenerateOptions = { ...opts, model };\n\n if (filtered.background !== undefined && !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.background)) {\n delete filtered.background;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.background);\n }\n if (filtered.seed !== undefined && !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.seed)) {\n delete filtered.seed;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.seed);\n }\n if (\n filtered.references !== undefined &&\n filtered.references.length > 0 &&\n !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.references)\n ) {\n delete filtered.references;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.references);\n }\n\n return this.performGenerate(filtered, dropped);\n }\n\n private async performGenerate(opts: ImageGenerateOptions, dropped: string[]): Promise<ImageGenerateResult> {\n if (opts.size !== undefined && (opts.resolution !== undefined || opts.aspectRatio !== undefined)) {\n throw new Error('ImageClient.generate: `size` cannot be combined with `resolution`/`aspectRatio` — pass one or the other');\n }\n const model = opts.model ?? this.model;\n const requestBody: Record<string, JsonValue> = { model, prompt: opts.prompt, output_format: 'png' };\n if (opts.n !== undefined) requestBody.n = opts.n;\n if (opts.size !== undefined) requestBody.size = opts.size;\n if (opts.resolution !== undefined) requestBody.resolution = opts.resolution;\n if (opts.aspectRatio !== undefined) requestBody.aspect_ratio = opts.aspectRatio;\n if (opts.quality !== undefined) requestBody.quality = opts.quality;\n if (opts.background !== undefined) requestBody.background = opts.background;\n if (opts.seed !== undefined) requestBody.seed = opts.seed;\n if (opts.references !== undefined && opts.references.length > 0) {\n requestBody.input_references = opts.references.map((r) => ({ type: 'image_url', image_url: { url: r.url } }));\n }\n if (opts.provider !== undefined) {\n const providerField: Record<string, JsonValue> = {};\n if (opts.provider.order !== undefined) providerField.order = opts.provider.order;\n if (opts.provider.only !== undefined) providerField.only = opts.provider.only;\n if (opts.provider.allowFallbacks !== undefined) providerField.allow_fallbacks = opts.provider.allowFallbacks;\n requestBody.provider = providerField;\n }\n\n const response = await this.fetchWithRetry(`${this.baseUrl}/images`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.apiKey}` },\n body: JSON.stringify(requestBody),\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `ImageClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const rawBody = await response.text();\n const body = parseJsonBody(this.provider, rawBody);\n if (!isJsonObject(body)) {\n throw new Error(`ImageClient: ${this.provider} returned unparseable body: ${rawBody}`);\n }\n if (isJsonObject(body.error)) {\n const detail = asString(body.error.message) ?? rawBody;\n throw new Error(`ImageClient: ${this.provider} returned an error: ${detail}`);\n }\n if (!Array.isArray(body.data)) {\n throw new Error(`ImageClient: ${this.provider} response has no \\`data\\` array: ${rawBody}`);\n }\n\n const images: GeneratedImage[] = body.data.map((entry, i) => {\n if (!isJsonObject(entry)) {\n throw new Error(`ImageClient: ${this.provider} response data[${i}] is not an object: ${rawBody}`);\n }\n const b64 = asString(entry.b64_json);\n if (!b64) {\n throw new Error(`ImageClient: ${this.provider} response data[${i}] missing \\`b64_json\\`: ${rawBody}`);\n }\n return { bytes: decodeBase64(b64), mediaType: asString(entry.media_type) ?? 'image/png' };\n });\n\n const usageObj = isJsonObject(body.usage) ? body.usage : undefined;\n const usage = {\n cost: asNumber(usageObj?.cost) ?? 0,\n totalTokens: asNumber(usageObj?.total_tokens) ?? 0,\n };\n\n return { images, usage, model, dropped };\n }\n\n /** Retries on network failure or a 5xx status; any other response is returned as-is. */\n private async fetchWithRetry(url: string, init: RequestInit): Promise<Response> {\n let lastNetworkError: Error | undefined;\n let response: Response | undefined;\n for (let attempt = 1; attempt <= IMAGE_MAX_ATTEMPTS; attempt++) {\n try {\n response = await this.fetchImpl(url, init);\n } catch (e) {\n response = undefined;\n lastNetworkError = e instanceof Error ? e : new Error(String(e));\n }\n if (response !== undefined && response.status < 500) return response;\n const backoff = IMAGE_RETRY_BACKOFF_MS[attempt - 1];\n if (backoff === undefined) break;\n await new Promise((r) => setTimeout(r, backoff));\n }\n if (response) return response;\n throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${IMAGE_MAX_ATTEMPTS} attempts`);\n }\n}\n\nexport const IMAGE_PROVIDER_CATALOG: ReadonlyArray<{ provider: ImageProvider; models: string[]; defaultModel: string }> = [\n {\n provider: 'openrouter',\n models: Object.values(OPENROUTER_IMAGE_MODELS),\n defaultModel: OPENROUTER_IMAGE_DEFAULTS.sheet,\n },\n];\n","/**\n * Truncation Detector\n *\n * Utilities for detecting when LLM output has been truncated and\n * extracting usable content from partial responses.\n *\n * @packageDocumentation\n */\n\nimport type { LLMFinishReason } from './client.js';\nimport { autoCloseJson } from './json-parser.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport type TruncationReason =\n | 'finish_reason'\n | 'json_incomplete'\n | 'bracket_mismatch'\n | 'none';\n\nexport interface TruncationResult {\n isTruncated: boolean;\n reason: TruncationReason;\n partialContent?: string;\n lastCompleteElement?: unknown;\n missingCloseBrackets?: number;\n missingCloseBraces?: number;\n}\n\n// ============================================================================\n// Main Detection Function\n// ============================================================================\n\n/**\n * Detect if an LLM response has been truncated.\n *\n * Analyzes the response content and finish reason to determine if\n * the output was cut off due to token limits or other issues.\n *\n * @param {string} response - The LLM response text\n * @param {LLMFinishReason} finishReason - The finish reason from the LLM\n * @returns {TruncationResult} Detection result with truncation details\n */\nexport function detectTruncation(\n response: string,\n finishReason: LLMFinishReason,\n): TruncationResult {\n if (finishReason === 'length') {\n const bracketInfo = countBrackets(response);\n return {\n isTruncated: true,\n reason: 'finish_reason',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n try {\n JSON.parse(response);\n return { isTruncated: false, reason: 'none' };\n } catch {\n // JSON is invalid, check if due to truncation\n }\n\n if (finishReason === 'stop' || finishReason === null) {\n const trimmed = response.trim();\n\n const isMidContent =\n trimmed.endsWith(',') ||\n trimmed.endsWith(':') ||\n trimmed.endsWith('\": ') ||\n /:\\s*$/.test(trimmed) ||\n /,\\s*$/.test(trimmed);\n\n if (isMidContent) {\n const bracketInfo = countBrackets(response);\n return {\n isTruncated: true,\n reason: 'json_incomplete',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n try {\n const closed = autoCloseJson(trimmed);\n JSON.parse(closed);\n return { isTruncated: false, reason: 'none' };\n } catch {\n return { isTruncated: false, reason: 'none' };\n }\n }\n\n const bracketInfo = countBrackets(response);\n if (\n bracketInfo.missingCloseBrackets > 0 ||\n bracketInfo.missingCloseBraces > 0\n ) {\n return {\n isTruncated: true,\n reason: 'bracket_mismatch',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n return { isTruncated: false, reason: 'none' };\n}\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\nfunction countBrackets(json: string): {\n openBrackets: number;\n closeBrackets: number;\n openBraces: number;\n closeBraces: number;\n missingCloseBrackets: number;\n missingCloseBraces: number;\n} {\n let inString = false;\n let escaped = false;\n let openBrackets = 0;\n let closeBrackets = 0;\n let openBraces = 0;\n let closeBraces = 0;\n\n for (const char of json) {\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n switch (char) {\n case '[':\n openBrackets++;\n break;\n case ']':\n closeBrackets++;\n break;\n case '{':\n openBraces++;\n break;\n case '}':\n closeBraces++;\n break;\n }\n }\n\n return {\n openBrackets,\n closeBrackets,\n openBraces,\n closeBraces,\n missingCloseBrackets: Math.max(0, openBrackets - closeBrackets),\n missingCloseBraces: Math.max(0, openBraces - closeBraces),\n };\n}\n\nexport function findLastCompleteElement(json: string): unknown | null {\n const autoClosed = autoCloseJson(json);\n try {\n return JSON.parse(autoClosed);\n } catch {\n // Auto-close didn't work\n }\n\n const trimmed = json.trim();\n\n if (trimmed.startsWith('[')) {\n const lastCompleteIndex = findLastCompleteArrayElement(trimmed);\n if (lastCompleteIndex > 0) {\n const subset = trimmed.substring(0, lastCompleteIndex) + ']';\n try {\n return JSON.parse(subset);\n } catch {\n // Continue\n }\n }\n }\n\n if (trimmed.startsWith('{')) {\n const closed = autoCloseJson(trimmed);\n try {\n return JSON.parse(closed);\n } catch {\n const lastCompleteIndex = findLastCompleteObjectProperty(trimmed);\n if (lastCompleteIndex > 0) {\n const subset = trimmed.substring(0, lastCompleteIndex) + '}';\n try {\n return JSON.parse(subset);\n } catch {\n // Give up\n }\n }\n }\n }\n\n return null;\n}\n\nfunction findLastCompleteArrayElement(json: string): number {\n let depth = 0;\n let inString = false;\n let escaped = false;\n let lastCompleteElementEnd = -1;\n\n for (let i = 0; i < json.length; i++) {\n const char = json[i];\n\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n if (char === '[' || char === '{') {\n depth++;\n } else if (char === ']' || char === '}') {\n depth--;\n if (depth === 1) {\n lastCompleteElementEnd = i + 1;\n }\n } else if (char === ',' && depth === 1) {\n lastCompleteElementEnd = i;\n }\n }\n\n return lastCompleteElementEnd > 0 ? lastCompleteElementEnd : -1;\n}\n\nfunction findLastCompleteObjectProperty(json: string): number {\n let depth = 0;\n let inString = false;\n let escaped = false;\n let lastCommaIndex = -1;\n\n for (let i = 0; i < json.length; i++) {\n const char = json[i];\n\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n if (char === '[' || char === '{') {\n depth++;\n } else if (char === ']' || char === '}') {\n depth--;\n } else if (char === ',' && depth === 1) {\n lastCommaIndex = i;\n }\n }\n\n return lastCommaIndex > 0 ? lastCommaIndex : -1;\n}\n\nexport function isLikelyTruncated(content: string): boolean {\n const trimmed = content.trim();\n if (!trimmed) return false;\n\n const brackets = countBrackets(trimmed);\n if (\n brackets.missingCloseBrackets > 0 ||\n brackets.missingCloseBraces > 0\n ) {\n return true;\n }\n\n const abruptEndings = [\n /,\\s*$/,\n /:\\s*$/,\n /\"\\s*:\\s*$/,\n /\\[\\s*$/,\n /{\\s*$/,\n ];\n\n for (const pattern of abruptEndings) {\n if (pattern.test(trimmed)) return true;\n }\n\n return false;\n}\n","/**\n * LLM Continuation Utility\n *\n * Handles truncated LLM responses with automatic continuation.\n * - Detects truncation via finish_reason and JSON structure\n * - Automatically continues with full context\n * - Merges partial and continuation responses\n * - Salvages partial data if max continuations reached\n *\n * @packageDocumentation\n */\n\nimport { z } from 'zod';\nimport { LLMClient, type LLMFinishReason } from './client.js';\nimport { detectTruncation } from './truncation-detector.js';\nimport { extractJsonFromText, autoCloseJson, isValidJson } from './json-parser.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface ContinuationOptions<T> {\n client: LLMClient;\n systemPrompt: string;\n userPrompt: string;\n schema?: z.ZodSchema<T>;\n maxTokens?: number;\n maxContinuations?: number;\n maxRetries?: number;\n buildContinuationPrompt: (\n partialResponse: string,\n attempt: number,\n ) => string;\n continuationSystemPrompt?: string;\n}\n\nexport interface ContinuationResult<T> {\n data: T;\n raw: string;\n continuationCount: number;\n warnings: string[];\n wasSalvaged: boolean;\n}\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst DEFAULT_MAX_TOKENS = 8192;\nconst DEFAULT_MAX_CONTINUATIONS = 3;\n\n/**\n * Default continuation system prompt.\n * Used when no custom continuationSystemPrompt is provided.\n */\nconst DEFAULT_CONTINUATION_SYSTEM_PROMPT = `You are a JSON continuation assistant. Your ONLY job is to continue generating JSON from where the previous response was truncated.\n\nRules:\n1. Continue from EXACTLY where the previous output stopped\n2. Do NOT repeat any content already generated\n3. Complete the JSON structure properly with all closing brackets\n4. Do NOT wrap in markdown code blocks\n5. Output ONLY the continuation JSON, nothing else`;\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\n/**\n * Merge a previous partial response with a continuation.\n *\n * Handles JSON structure merging, removing markdown code blocks,\n * and ensuring proper comma separation between merged parts.\n *\n * @param {string} previous - The previous partial response\n * @param {string} continuation - The continuation to merge\n * @returns {string} The merged response\n */\nexport function mergeResponses(\n previous: string,\n continuation: string,\n): string {\n const trimmedPrev = previous.trimEnd();\n const trimmedCont = continuation.trimStart();\n\n let cleanedCont = trimmedCont\n .replace(/^```json?\\s*/i, '')\n .replace(/```\\s*$/i, '')\n .trim();\n\n if (cleanedCont.startsWith('{')) {\n try {\n const contParsed = JSON.parse(autoCloseJson(cleanedCont));\n const keys = Object.keys(contParsed);\n if (keys.length === 1 && Array.isArray(contParsed[keys[0]])) {\n cleanedCont = contParsed[keys[0]]\n .map((item: unknown) => JSON.stringify(item))\n .join(',\\n');\n }\n } catch {\n // Continue with original cleaning\n }\n }\n\n if (cleanedCont.startsWith('}') || cleanedCont.startsWith(']')) {\n return trimmedPrev + cleanedCont;\n }\n\n const prevEndsWithValue = /[\\}\\]\\\"\\d]$/.test(trimmedPrev);\n const contStartsWithValue = /^[\\{\\[\\\"]/.test(cleanedCont);\n\n if (prevEndsWithValue && contStartsWithValue) {\n return trimmedPrev + ',\\n' + cleanedCont;\n }\n\n return trimmedPrev + cleanedCont;\n}\n\nexport function salvagePartialResponse<T>(rawResponse: string): T | null {\n console.warn('[Continuation] Attempting to salvage partial response');\n\n try {\n const cleanedResponse = extractJsonFromText(rawResponse) || rawResponse;\n const closed = autoCloseJson(cleanedResponse);\n const parsed = JSON.parse(closed) as T;\n console.log('[Continuation] Successfully salvaged partial response');\n return parsed;\n } catch (error) {\n console.error('[Continuation] Could not salvage response:', error);\n }\n\n return null;\n}\n\n// ============================================================================\n// Main Function\n// ============================================================================\n\n/**\n * Call an LLM with automatic continuation handling.\n *\n * Manages token limits by detecting truncation and making continuation\n * calls until the complete response is received or max continuations reached.\n *\n * @template T - Expected response type\n * @param {ContinuationOptions<T>} options - Continuation call options\n * @returns {Promise<ContinuationResult<T>>} Result with data and continuation metadata\n */\nexport async function callWithContinuation<T>(\n options: ContinuationOptions<T>,\n): Promise<ContinuationResult<T>> {\n const {\n client,\n systemPrompt,\n userPrompt,\n schema,\n maxTokens = DEFAULT_MAX_TOKENS,\n maxContinuations = DEFAULT_MAX_CONTINUATIONS,\n buildContinuationPrompt,\n continuationSystemPrompt = DEFAULT_CONTINUATION_SYSTEM_PROMPT,\n } = options;\n\n let rawResponse = '';\n let continuationCount = 0;\n const warnings: string[] = [];\n let wasSalvaged = false;\n\n console.log('[Continuation] Starting LLM call with continuation support');\n console.log(\n `[Continuation] Max tokens: ${maxTokens}, Max continuations: ${maxContinuations}`,\n );\n\n try {\n const response = await client.callRawWithMetadata({\n systemPrompt,\n userPrompt,\n maxTokens,\n });\n\n rawResponse = extractJsonFromText(response.raw) || response.raw;\n\n console.log(\n `[Continuation] Initial response: ${rawResponse.length} chars, finish_reason: ${response.finishReason}`,\n );\n\n let truncation = detectTruncation(rawResponse, response.finishReason);\n\n while (truncation.isTruncated && continuationCount < maxContinuations) {\n continuationCount++;\n const warningMsg = `Response truncated (${truncation.reason}), continuing (attempt ${continuationCount}/${maxContinuations})`;\n console.log(`[Continuation] ${warningMsg}`);\n warnings.push(warningMsg);\n\n const contPrompt = buildContinuationPrompt(\n rawResponse,\n continuationCount,\n );\n\n const contResponse = await client.callRawWithMetadata({\n systemPrompt: continuationSystemPrompt,\n userPrompt: contPrompt,\n maxTokens,\n });\n\n console.log(\n `[Continuation] Continuation response: ${contResponse.raw.length} chars, finish_reason: ${contResponse.finishReason}`,\n );\n\n const cleanedContResponse =\n extractJsonFromText(contResponse.raw) || contResponse.raw;\n rawResponse = mergeResponses(rawResponse, cleanedContResponse);\n\n truncation = detectTruncation(rawResponse, contResponse.finishReason);\n }\n\n if (\n continuationCount >= maxContinuations &&\n truncation.isTruncated\n ) {\n console.warn(\n `[Continuation] Reached max continuations (${maxContinuations}), attempting to salvage...`,\n );\n warnings.push(\n `Reached max continuations - some content may be incomplete`,\n );\n wasSalvaged = true;\n }\n\n const cleanedResponse =\n extractJsonFromText(rawResponse) || rawResponse;\n let data: T;\n\n try {\n if (isValidJson(cleanedResponse)) {\n data = JSON.parse(cleanedResponse) as T;\n } else {\n const closed = autoCloseJson(cleanedResponse);\n data = JSON.parse(closed) as T;\n if (!wasSalvaged) {\n warnings.push('Response required auto-closing of JSON brackets');\n }\n }\n } catch (parseError) {\n const salvaged = salvagePartialResponse<T>(cleanedResponse);\n if (salvaged) {\n data = salvaged;\n wasSalvaged = true;\n warnings.push('Response was salvaged from partial data');\n } else {\n throw new Error(\n `Failed to parse response after ${continuationCount} continuations: ${parseError}`,\n );\n }\n }\n\n if (schema) {\n try {\n data = schema.parse(data);\n } catch (validationError) {\n console.warn(\n '[Continuation] Schema validation failed:',\n validationError,\n );\n warnings.push(`Schema validation issue: ${validationError}`);\n }\n }\n\n console.log(\n `[Continuation] Complete. Continuations: ${continuationCount}, Warnings: ${warnings.length}`,\n );\n\n return {\n data,\n raw: rawResponse,\n continuationCount,\n warnings,\n wasSalvaged,\n };\n } catch (error) {\n console.error('[Continuation] Error during LLM call:', error);\n throw error;\n }\n}\n\n/**\n * Build a generic continuation prompt.\n *\n * Creates a prompt that instructs the LLM to continue from where\n * a previous truncated response left off.\n *\n * @param {string} context - Original context/prompt\n * @param {string} partialResponse - The partial response generated so far\n * @param {number} attempt - Current continuation attempt number\n * @param {number} [maxAttempts] - Maximum allowed continuation attempts\n * @returns {string} The formatted continuation prompt\n */\nexport function buildGenericContinuationPrompt(\n context: string,\n partialResponse: string,\n attempt: number,\n maxAttempts: number = DEFAULT_MAX_CONTINUATIONS,\n): string {\n return `## CONTINUATION REQUEST (Attempt ${attempt}/${maxAttempts})\n\nYour previous response was truncated. Continue generating from where you left off.\n\n### ORIGINAL CONTEXT\n${context}\n\n### WHAT YOU GENERATED SO FAR\n\\`\\`\\`json\n${partialResponse}\n\\`\\`\\`\n\n### INSTRUCTIONS\n1. Continue from EXACTLY where the response was cut off\n2. Do NOT repeat any content already generated\n3. Complete the JSON structure properly\n4. Do NOT wrap your response in markdown code blocks\n\nContinue generating now:`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAM,sBAAoD,CA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MACF;AAAA,IACF,SAAS,YAAY;AACnB,YAAM,WAAW,uBAA0B,eAAe;AAC1D,UAAI,UAAU;AACZ,eAAO;AACP,sBAAc;AACd,iBAAS,KAAK,yCAAyC;AAAA,MACzD,OAAO;AACL,cAAM,IAAI;AAAA,UACR,kCAAkC,iBAAiB,mBAAmB,UAAU;AAAA,QAClF;AAAA,MACF;AAAA,IACF;AAEA,QAAI,QAAQ;AACV,UAAI;AACF,eAAO,OAAO,MAAM,IAAI;AAAA,MAC1B,SAAS,iBAAiB;AACxB,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,QACF;AACA,iBAAS,KAAK,4BAA4B,eAAe,EAAE;AAAA,MAC7D;AAAA,IACF;AAEA,YAAQ;AAAA,MACN,2CAA2C,iBAAiB,eAAe,SAAS,MAAM;AAAA,IAC5F;AAEA,WAAO;AAAA,MACL;AAAA,MACA,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,yCAAyC,KAAK;AAC5D,UAAM;AAAA,EACR;AACF;AAcO,SAAS,+BACd,SACA,iBACA,SACA,cAAsB,2BACd;AACR,SAAO,oCAAoC,OAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAKjE,OAAO;AAAA;AAAA;AAAA;AAAA,EAIP,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUjB;","names":["DEFAULT_MODELS","DEFAULT_BASE_URLS","API_KEY_ENV_VARS","bracketInfo"]}
|
|
1
|
+
{"version":3,"sources":["../src/embedding-client.ts","../src/image-client.ts","../src/truncation-detector.ts","../src/continuation.ts"],"sourcesContent":["/**\n * Embedding Client\n *\n * Single-purpose client for text embeddings. Used by `@almadar-io/agent`'s\n * cosine-similarity catalog retrieval (rank organisms + atoms against the\n * user's request before rendering Stage A's prompt) and by\n * `@almadar/std`'s publish-time embedding bake step.\n *\n * Providers:\n * - `openai` (default model `text-embedding-3-small`, 1536-d) — requires\n * `OPENAI_API_KEY`.\n * - `openrouter` (default model `baai/bge-base-en-v1.5`, 768-d) — requires\n * `OPEN_ROUTER_API_KEY`. Same OpenAI-compatible request shape, just a\n * different base URL.\n * - `lmstudio` (default model `text-embedding-bge-base-en-v1.5`, same\n * bge-base-en-v1.5 weights as the openrouter default, run locally) —\n * local LM Studio server, no API key. Mirrors `client.ts`'s\n * `PROVIDER_CONFIGS.lmstudio`: base URL from `LMSTUDIO_URL` (default\n * `http://127.0.0.1:1234/v1`), model from `LMSTUDIO_EMBEDDING_MODEL`.\n *\n * All three providers return the same response envelope (`{data:[{embedding,index}]}`),\n * so the request/response code is shared.\n *\n * @packageDocumentation\n */\n\nexport type EmbeddingProvider = 'openai' | 'openrouter' | 'lmstudio';\n\nconst EMBEDDING_PROVIDERS: readonly EmbeddingProvider[] = ['openai', 'openrouter', 'lmstudio'];\n\n/**\n * Canonical embedding-provider resolution — the ONE place every downstream\n * consumer should defer to instead of hardcoding a literal provider.\n * Mirrors the `EMBEDDING_PROVIDER` env var `@almadar-io/integrations`'s\n * `RuntimeIntegrationManager`/`getEmbeddings()` already reads (its own\n * `?? 'openrouter'` fallback is the precedent this centralizes).\n *\n * Defaults to `openrouter` — UNCHANGED from every existing deployment's\n * behavior — so nothing (cloud/server included) switches to a local-only\n * provider unless an operator explicitly sets `EMBEDDING_PROVIDER=lmstudio`\n * in that specific environment (e.g. a dev machine running LM Studio).\n * Consumers: `@almadar-io/rabit`'s catalog narrower\n * (`runtime/services/embedding.ts`) and `@almadar/workspace`'s index\n * embedder (`workspace-index/embedder.ts`).\n */\nexport function resolveDefaultEmbeddingProvider(): EmbeddingProvider {\n const v = process.env.EMBEDDING_PROVIDER;\n return v && (EMBEDDING_PROVIDERS as readonly string[]).includes(v) ? (v as EmbeddingProvider) : 'openrouter';\n}\n\nexport interface EmbeddingClientOptions {\n provider: EmbeddingProvider;\n /** Defaults: openai → text-embedding-3-small, openrouter → baai/bge-base-en-v1.5, lmstudio → text-embedding-bge-base-en-v1.5. */\n model?: string;\n /** Override API key. Defaults to provider-specific env var; lmstudio needs none. */\n apiKey?: string;\n /** Override base URL. Defaults to provider canonical endpoint (lmstudio: `LMSTUDIO_URL`). */\n baseUrl?: string;\n}\n\nexport interface EmbeddingUsage {\n promptTokens: number;\n totalTokens: number;\n}\n\nexport interface EmbeddingResult {\n embedding: readonly number[];\n usage: EmbeddingUsage;\n}\n\nexport interface EmbeddingBatchResult {\n embeddings: readonly (readonly number[])[];\n usage: EmbeddingUsage;\n}\n\ninterface OpenAIEmbeddingApiResponse {\n data: Array<{ embedding: number[]; index: number }>;\n usage?: { prompt_tokens?: number; total_tokens?: number };\n /** OpenRouter reports provider hiccups as HTTP 200 with an error envelope and no `data`. */\n error?: { message?: string; code?: number | string };\n}\n\n/** Transient-failure retry policy: attempts and backoff (ms) between them. */\nconst EMBED_MAX_ATTEMPTS = 3;\nconst EMBED_RETRY_BACKOFF_MS = [500, 2000];\n\n/** Provider rejects an input that exceeds the model's context window (bge: 512 tokens). */\nconst TOKEN_OVERFLOW_RE =\n /context length|maximum input length|reduce the length of the input|input_tokens/i;\n\nconst DEFAULT_MODELS: Record<EmbeddingProvider, string> = {\n openai: 'text-embedding-3-small',\n openrouter: 'baai/bge-base-en-v1.5',\n lmstudio: 'text-embedding-bge-base-en-v1.5',\n};\n\nconst DEFAULT_BASE_URLS: Record<EmbeddingProvider, string> = {\n openai: 'https://api.openai.com/v1',\n openrouter: 'https://openrouter.ai/api/v1',\n lmstudio: 'http://127.0.0.1:1234/v1',\n};\n\nconst API_KEY_ENV_VARS: Partial<Record<EmbeddingProvider, string>> = {\n openai: 'OPENAI_API_KEY',\n openrouter: 'OPEN_ROUTER_API_KEY',\n};\n\nexport class EmbeddingClient {\n private readonly provider: EmbeddingProvider;\n private readonly model: string;\n private readonly apiKey: string;\n private readonly baseUrl: string;\n\n constructor(opts: EmbeddingClientOptions) {\n this.provider = opts.provider;\n this.model = opts.model ?? (this.provider === 'lmstudio' ? process.env.LMSTUDIO_EMBEDDING_MODEL ?? DEFAULT_MODELS.lmstudio : DEFAULT_MODELS[this.provider]);\n const defaultBaseUrl = this.provider === 'lmstudio' ? process.env.LMSTUDIO_URL ?? DEFAULT_BASE_URLS.lmstudio : DEFAULT_BASE_URLS[this.provider];\n this.baseUrl = (opts.baseUrl ?? defaultBaseUrl).replace(/\\/$/, '');\n\n if (this.provider === 'lmstudio') {\n // Local LM Studio server — no credential to check, mirrors client.ts's PROVIDER_CONFIGS.lmstudio.\n this.apiKey = opts.apiKey ?? 'not-needed';\n return;\n }\n const apiKey = opts.apiKey ?? this.resolveApiKey();\n if (!apiKey) {\n throw new Error(\n `EmbeddingClient: API key for provider '${this.provider}' not found. ` +\n `Set ${API_KEY_ENV_VARS[this.provider]} in your environment or pass options.apiKey.`,\n );\n }\n this.apiKey = apiKey;\n }\n\n /** Embed a single text. */\n async embed(text: string): Promise<EmbeddingResult> {\n const batch = await this.embedBatch([text]);\n const embedding = batch.embeddings[0];\n if (!embedding) {\n throw new Error('EmbeddingClient.embed: provider returned no embedding');\n }\n return { embedding, usage: batch.usage };\n }\n\n /**\n * Embed a batch of texts. Used by `@almadar/std`'s publish-time\n * `build-embeddings.ts` script — one batch call for the full catalog\n * (≈190 entries) rather than per-entry requests.\n */\n async embedBatch(texts: ReadonlyArray<string>): Promise<EmbeddingBatchResult> {\n if (texts.length === 0) {\n return { embeddings: [], usage: { promptTokens: 0, totalTokens: 0 } };\n }\n // bge-base-en-v1.5 has a 512-token context limit. Chars-per-token varies\n // with density (~4 for typical English, ~2.5-3 for dense technical/code\n // text), so no fixed char cap can guarantee a fit. Start at 2000 chars and,\n // when the provider rejects an input for exceeding the context window,\n // halve the cap and retry — the provider's own token count is the source\n // of truth, not a chars-per-token guess.\n let charCap = 2000;\n let lastError: Error = new Error('EmbeddingClient: unreachable');\n for (let attempt = 1; attempt <= EMBED_MAX_ATTEMPTS; attempt++) {\n const trimmed = texts.map((t) => (t.length > charCap ? t.slice(0, charCap) : t));\n try {\n return await this.embedBatchOnce(texts.length, trimmed);\n } catch (e) {\n lastError = e instanceof Error ? e : new Error(String(e));\n if (TOKEN_OVERFLOW_RE.test(lastError.message) && charCap > 500) {\n charCap = Math.floor(charCap / 2);\n continue;\n }\n const backoff = EMBED_RETRY_BACKOFF_MS[attempt - 1];\n if (backoff === undefined) break;\n await new Promise((r) => setTimeout(r, backoff));\n }\n }\n throw lastError;\n }\n\n private async embedBatchOnce(\n inputCount: number,\n trimmed: ReadonlyArray<string>,\n ): Promise<EmbeddingBatchResult> {\n const response = await fetch(`${this.baseUrl}/embeddings`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify({ model: this.model, input: trimmed }),\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `EmbeddingClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const rawBody = await response.text();\n let body: OpenAIEmbeddingApiResponse;\n try {\n body = JSON.parse(rawBody) as OpenAIEmbeddingApiResponse;\n } catch {\n throw new Error(\n `EmbeddingClient: ${this.provider} returned unparseable body: ${rawBody}`,\n );\n }\n if (!Array.isArray(body.data)) {\n // OpenRouter reports provider hiccups as 200 + `{error}` with no data —\n // surface the real body instead of a blind \"missing data\" (G9: this\n // error was undiagnosable for weeks because the body was discarded).\n const detail = body.error?.message ?? rawBody;\n throw new Error(\n `EmbeddingClient: ${this.provider} response has no \\`data\\` array: ${detail}`,\n );\n }\n const ordered = [...body.data].sort((a, b) => a.index - b.index);\n if (ordered.length !== inputCount) {\n throw new Error(\n `EmbeddingClient: provider returned ${ordered.length} embeddings for ${inputCount} inputs`,\n );\n }\n return {\n embeddings: ordered.map((d) => d.embedding),\n usage: {\n promptTokens: body.usage?.prompt_tokens ?? 0,\n totalTokens: body.usage?.total_tokens ?? 0,\n },\n };\n }\n\n private resolveApiKey(): string | undefined {\n const envVar = API_KEY_ENV_VARS[this.provider];\n return envVar ? process.env[envVar] : undefined;\n }\n}\n\n/**\n * Cosine similarity between two equal-length vectors. Returns a value in\n * [-1, 1]; higher means more similar. Used by `@almadar-io/agent`'s\n * catalog retrieval to rank organism/atom descriptions against the user\n * request embedding.\n *\n * Defensive: returns 0 when either vector is the zero vector (would\n * otherwise divide by zero).\n */\nexport function cosineSimilarity(a: ReadonlyArray<number>, b: ReadonlyArray<number>): number {\n if (a.length !== b.length) {\n throw new Error(`cosineSimilarity: vector length mismatch (${a.length} vs ${b.length})`);\n }\n let dot = 0;\n let normA = 0;\n let normB = 0;\n for (let i = 0; i < a.length; i++) {\n const av = a[i];\n const bv = b[i];\n dot += av * bv;\n normA += av * av;\n normB += bv * bv;\n }\n if (normA === 0 || normB === 0) return 0;\n return dot / (Math.sqrt(normA) * Math.sqrt(normB));\n}\n","/**\n * Image Client\n *\n * Single-purpose client for image-generation APIs. `openrouter` posts to\n * `POST {baseUrl}/images`; `local` posts an OpenAI-shaped body to\n * `POST {baseUrl}/images/generations` (the MFLUX shim in\n * tools/asset-workflow/python/image_server). Raw `fetch`, no SDK — mirrors\n * `embedding-client.ts`'s shape (provider tables, retry policy, the\n * three response-error surfaces).\n *\n * @packageDocumentation\n */\n\nimport type { JsonValue } from '@almadar/core';\nimport { Agent as UndiciAgent, fetch as undiciFetch, type RequestInit as UndiciRequestInit, type Response as UndiciResponse } from 'undici';\n\nexport type ImageProvider = 'openrouter' | 'local';\n\nconst IMAGE_PROVIDERS: readonly ImageProvider[] = ['openrouter', 'local'];\n\n/** Canonical image-provider resolution, mirrors `resolveDefaultEmbeddingProvider`. */\nexport function resolveDefaultImageProvider(): ImageProvider {\n const v = process.env.IMAGE_PROVIDER;\n return v && (IMAGE_PROVIDERS as readonly string[]).includes(v) ? (v as ImageProvider) : 'openrouter';\n}\n\nexport const OPENROUTER_IMAGE_MODELS = {\n SEEDREAM_5_LITE: 'bytedance-seed/seedream-5-0-lite',\n SEEDREAM_4_5: 'bytedance-seed/seedream-4.5',\n GEMINI_3_1_FLASH_LITE_IMAGE: 'google/gemini-3.1-flash-lite-image',\n GEMINI_2_5_FLASH_IMAGE: 'google/gemini-2.5-flash-image',\n FLUX_2_PRO: 'black-forest-labs/flux.2-pro',\n} as const;\n\nexport const OPENROUTER_IMAGE_DEFAULTS = {\n sheet: OPENROUTER_IMAGE_MODELS.SEEDREAM_5_LITE,\n preview: OPENROUTER_IMAGE_MODELS.GEMINI_3_1_FLASH_LITE_IMAGE,\n} as const;\n\nexport const LOCAL_IMAGE_MODELS = {\n Z_IMAGE_TURBO: 'z-image-turbo',\n FLUX_SCHNELL: 'flux-schnell',\n FLUX_DEV: 'flux-dev',\n} as const;\n\nexport const LOCAL_IMAGE_DEFAULTS = {\n /** Z-Image-Turbo is the default: public on Hugging Face, no token required (FLUX is gated). */\n sheet: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO,\n preview: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO,\n} as const;\n\nexport interface ImageClientOptions {\n provider?: ImageProvider;\n model?: string;\n apiKey?: string;\n baseUrl?: string;\n fetchImpl?: typeof fetch;\n}\n\n/** An input reference image — `https:` or `data:` URL. Transport only, no fetching/validation. */\nexport interface ImageReference {\n url: string;\n}\n\nexport interface ImageGenerateOptions {\n prompt: string;\n model?: string;\n n?: number;\n size?: `${number}x${number}`;\n resolution?: '512' | '1K' | '2K' | '4K';\n aspectRatio?: string;\n quality?: 'auto' | 'low' | 'medium' | 'high';\n background?: 'auto' | 'transparent' | 'opaque';\n seed?: number;\n references?: ReadonlyArray<ImageReference>;\n provider?: { order?: string[]; only?: string[]; allowFallbacks?: boolean };\n}\n\nexport interface GeneratedImage {\n bytes: Uint8Array;\n mediaType: string;\n}\n\nexport interface ImageGenerateResult {\n images: GeneratedImage[];\n usage: { cost: number; totalTokens: number };\n model: string;\n dropped: string[];\n}\n\nexport interface ImageModelCapabilities {\n model: string;\n supportedParameters: ReadonlySet<string>;\n raw: JsonValue;\n}\n\n/** Transient-failure retry policy: attempts and backoff (ms) between them. */\nconst IMAGE_MAX_ATTEMPTS = 3;\nconst IMAGE_RETRY_BACKOFF_MS = [500, 2000];\n\nconst DEFAULT_MODELS: Record<ImageProvider, string> = {\n openrouter: OPENROUTER_IMAGE_DEFAULTS.sheet,\n local: LOCAL_IMAGE_DEFAULTS.sheet,\n};\n\nconst DEFAULT_BASE_URLS: Record<ImageProvider, string> = {\n openrouter: 'https://openrouter.ai/api/v1',\n local: 'http://127.0.0.1:8180/v1',\n};\n\nconst API_KEY_ENV_VARS: Record<ImageProvider, string> = {\n openrouter: 'OPEN_ROUTER_API_KEY',\n local: 'LOCAL_IMAGE_API_KEY',\n};\n\n/**\n * Request field → OpenRouter `supported_parameters` name. The single\n * place `generateChecked` reads to decide what to strip — change the\n * mapping here, not at each call site.\n */\nconst IMAGE_CHECKED_PARAM_NAMES = {\n background: 'background',\n seed: 'seed',\n references: 'input_references',\n} as const;\n\nfunction isJsonObject(value: JsonValue): value is { [key: string]: JsonValue } {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\nfunction asString(value: JsonValue | undefined): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nfunction asNumber(value: JsonValue | undefined): number | undefined {\n return typeof value === 'number' ? value : undefined;\n}\n\nfunction parseJsonBody(provider: ImageProvider, rawBody: string): JsonValue {\n try {\n return JSON.parse(rawBody) as JsonValue;\n } catch {\n throw new Error(`ImageClient: ${provider} returned unparseable body: ${rawBody}`);\n }\n}\n\nfunction decodeBase64(b64: string): Uint8Array {\n if (typeof globalThis.Buffer !== 'undefined') {\n return new Uint8Array(globalThis.Buffer.from(b64, 'base64'));\n }\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return bytes;\n}\n\n/**\n * Collects the union of `supported_parameters` across a `.../endpoints`\n * response's `data`/`endpoints` arrays. Returns an empty set (never\n * throws) when the shape doesn't match — the caller still gets `raw`.\n */\nfunction collectSupportedParameters(body: JsonValue): ReadonlySet<string> {\n const params = new Set<string>();\n if (!isJsonObject(body)) return params;\n const candidateArrays: JsonValue[][] = [];\n if (Array.isArray(body.data)) candidateArrays.push(body.data);\n if (Array.isArray(body.endpoints)) candidateArrays.push(body.endpoints);\n for (const arr of candidateArrays) {\n for (const entry of arr) {\n if (!isJsonObject(entry)) continue;\n const sp = entry.supported_parameters;\n if (!Array.isArray(sp)) continue;\n for (const p of sp) if (typeof p === 'string') params.add(p);\n }\n }\n return params;\n}\n\nexport class ImageClient {\n private readonly provider: ImageProvider;\n private readonly model: string;\n private readonly apiKey: string;\n private readonly baseUrl: string;\n private readonly fetchImpl: typeof fetch;\n private readonly capabilitiesCache = new Map<string, ImageModelCapabilities>();\n\n constructor(opts: ImageClientOptions = {}) {\n this.provider = opts.provider ?? resolveDefaultImageProvider();\n this.model = opts.model ?? DEFAULT_MODELS[this.provider];\n this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URLS[this.provider]).replace(/\\/$/, '');\n this.fetchImpl = opts.fetchImpl ?? fetch;\n\n // The local shim is unauthenticated; a key is required only for hosted providers.\n const apiKey = opts.apiKey ?? process.env[API_KEY_ENV_VARS[this.provider]] ?? '';\n if (!apiKey && this.provider !== 'local') {\n throw new Error(\n `ImageClient: API key for provider '${this.provider}' not found. ` +\n `Set ${API_KEY_ENV_VARS[this.provider]} in your environment or pass options.apiKey.`,\n );\n }\n this.apiKey = apiKey;\n }\n\n /** Generate images. Never strips request fields — `dropped` is always `[]`. */\n async generate(opts: ImageGenerateOptions): Promise<ImageGenerateResult> {\n return this.performGenerate(opts, []);\n }\n\n /**\n * Per-model `.../endpoints` capabilities, memoized per instance per\n * model. `supportedParameters` is the empty set (not a throw) when the\n * response shape doesn't match what's expected.\n */\n async capabilities(modelId?: string): Promise<ImageModelCapabilities> {\n const model = modelId ?? this.model;\n const cached = this.capabilitiesCache.get(model);\n if (cached) return cached;\n\n // The local shim serves no `.../endpoints` metadata; declare what it honors.\n if (this.provider === 'local') {\n const local: ImageModelCapabilities = { model, supportedParameters: new Set(['seed', IMAGE_CHECKED_PARAM_NAMES.references]), raw: {} };\n this.capabilitiesCache.set(model, local);\n return local;\n }\n\n const response = await this.fetchWithRetry(`${this.baseUrl}/images/models/${model}/endpoints`, {\n method: 'GET',\n headers: this.authHeaders(),\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `ImageClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const body = parseJsonBody(this.provider, await response.text());\n const result: ImageModelCapabilities = { model, supportedParameters: collectSupportedParameters(body), raw: body };\n this.capabilitiesCache.set(model, result);\n return result;\n }\n\n /**\n * Like `generate`, but first reads `capabilities(model)` and strips\n * any of `background`/`seed`/`references` the model doesn't list in\n * `supported_parameters` — their (OpenRouter) names come back in\n * `dropped` rather than being silently swallowed.\n */\n async generateChecked(opts: ImageGenerateOptions): Promise<ImageGenerateResult> {\n const model = opts.model ?? this.model;\n const caps = await this.capabilities(model);\n const dropped: string[] = [];\n const filtered: ImageGenerateOptions = { ...opts, model };\n\n if (filtered.background !== undefined && !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.background)) {\n delete filtered.background;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.background);\n }\n if (filtered.seed !== undefined && !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.seed)) {\n delete filtered.seed;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.seed);\n }\n if (\n filtered.references !== undefined &&\n filtered.references.length > 0 &&\n !caps.supportedParameters.has(IMAGE_CHECKED_PARAM_NAMES.references)\n ) {\n delete filtered.references;\n dropped.push(IMAGE_CHECKED_PARAM_NAMES.references);\n }\n\n return this.performGenerate(filtered, dropped);\n }\n\n private async performGenerate(opts: ImageGenerateOptions, dropped: string[]): Promise<ImageGenerateResult> {\n if (opts.size !== undefined && (opts.resolution !== undefined || opts.aspectRatio !== undefined)) {\n throw new Error('ImageClient.generate: `size` cannot be combined with `resolution`/`aspectRatio` — pass one or the other');\n }\n const model = opts.model ?? this.model;\n\n if (this.provider === 'local') {\n return this.performLocalGenerate(opts, model, dropped);\n }\n\n const requestBody: Record<string, JsonValue> = { model, prompt: opts.prompt, output_format: 'png' };\n if (opts.n !== undefined) requestBody.n = opts.n;\n if (opts.size !== undefined) requestBody.size = opts.size;\n if (opts.resolution !== undefined) requestBody.resolution = opts.resolution;\n if (opts.aspectRatio !== undefined) requestBody.aspect_ratio = opts.aspectRatio;\n if (opts.quality !== undefined) requestBody.quality = opts.quality;\n if (opts.background !== undefined) requestBody.background = opts.background;\n if (opts.seed !== undefined) requestBody.seed = opts.seed;\n if (opts.references !== undefined && opts.references.length > 0) {\n requestBody.input_references = opts.references.map((r) => ({ type: 'image_url', image_url: { url: r.url } }));\n }\n if (opts.provider !== undefined) {\n const providerField: Record<string, JsonValue> = {};\n if (opts.provider.order !== undefined) providerField.order = opts.provider.order;\n if (opts.provider.only !== undefined) providerField.only = opts.provider.only;\n if (opts.provider.allowFallbacks !== undefined) providerField.allow_fallbacks = opts.provider.allowFallbacks;\n requestBody.provider = providerField;\n }\n\n return this.sendGenerate(`${this.baseUrl}/images`, requestBody, model, dropped);\n }\n\n /**\n * OpenAI-compatible local server (the MFLUX shim in tools/asset-workflow).\n * OpenRouter-only fields are reported in `dropped`, never sent. References\n * ARE sent (as `input_references`) — the shim forwards them to mflux's\n * `image_path`/`image_strength` img2img conditioning.\n */\n private async performLocalGenerate(\n opts: ImageGenerateOptions,\n model: string,\n dropped: string[],\n ): Promise<ImageGenerateResult> {\n const requestBody: Record<string, JsonValue> = { model, prompt: opts.prompt };\n if (opts.n !== undefined) requestBody.n = opts.n;\n if (opts.size !== undefined) requestBody.size = opts.size;\n if (opts.seed !== undefined) requestBody.seed = opts.seed;\n if (opts.references !== undefined && opts.references.length > 0) {\n requestBody.input_references = opts.references.map((r) => ({ type: 'image_url', image_url: { url: r.url } }));\n }\n for (const field of ['resolution', 'aspectRatio', 'quality', 'background'] as const) {\n if (opts[field] !== undefined) dropped.push(field);\n }\n if (opts.provider !== undefined) dropped.push('provider');\n\n return this.sendGenerate(`${this.baseUrl}/images/generations`, requestBody, model, dropped);\n }\n\n private authHeaders(): Record<string, string> {\n return this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {};\n }\n\n private async sendGenerate(\n url: string,\n requestBody: Record<string, JsonValue>,\n model: string,\n dropped: string[],\n ): Promise<ImageGenerateResult> {\n const response = await this.fetchWithRetry(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', ...this.authHeaders() },\n body: JSON.stringify(requestBody),\n });\n if (!response.ok) {\n const errorText = await response.text().catch(() => '<no body>');\n throw new Error(\n `ImageClient: ${this.provider} returned ${response.status} ${response.statusText}: ${errorText}`,\n );\n }\n const rawBody = await response.text();\n const body = parseJsonBody(this.provider, rawBody);\n if (!isJsonObject(body)) {\n throw new Error(`ImageClient: ${this.provider} returned unparseable body: ${rawBody}`);\n }\n if (isJsonObject(body.error)) {\n const detail = asString(body.error.message) ?? rawBody;\n throw new Error(`ImageClient: ${this.provider} returned an error: ${detail}`);\n }\n if (!Array.isArray(body.data)) {\n throw new Error(`ImageClient: ${this.provider} response has no \\`data\\` array: ${rawBody}`);\n }\n\n const images: GeneratedImage[] = body.data.map((entry, i) => {\n if (!isJsonObject(entry)) {\n throw new Error(`ImageClient: ${this.provider} response data[${i}] is not an object: ${rawBody}`);\n }\n const b64 = asString(entry.b64_json);\n if (!b64) {\n throw new Error(`ImageClient: ${this.provider} response data[${i}] missing \\`b64_json\\`: ${rawBody}`);\n }\n return { bytes: decodeBase64(b64), mediaType: asString(entry.media_type) ?? 'image/png' };\n });\n\n const usageObj = isJsonObject(body.usage) ? body.usage : undefined;\n const usage = {\n cost: asNumber(usageObj?.cost) ?? 0,\n totalTokens: asNumber(usageObj?.total_tokens) ?? 0,\n };\n\n return { images, usage, model, dropped };\n }\n\n /** Retries on network failure or a 5xx status; any other response is returned as-is.\n * The local provider never retries: a failed local generation (e.g. MLX OOM) cannot\n * succeed on an immediate retry, and hammering a memory-pressured machine is harmful. */\n private async fetchWithRetry(url: string, init: RequestInit): Promise<Response | UndiciResponse> {\n const maxAttempts = this.provider === 'local' ? 1 : IMAGE_MAX_ATTEMPTS;\n let lastNetworkError: Error | undefined;\n let response: Response | UndiciResponse | undefined;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n response = await this.doFetch(url, init);\n } catch (e) {\n response = undefined;\n lastNetworkError = e instanceof Error ? e : new Error(String(e));\n }\n if (response !== undefined && response.status < 500) return response;\n const backoff = IMAGE_RETRY_BACKOFF_MS[attempt - 1];\n if (backoff === undefined || attempt >= maxAttempts) break;\n await new Promise((r) => setTimeout(r, backoff));\n }\n if (response) return response;\n throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${maxAttempts} attempts`);\n }\n\n /** Local generations can exceed Node's built-in 300s headersTimeout (a single\n * MFLUX image routinely takes minutes), so local calls ride npm-undici's own\n * fetch with the transport timers disabled — mirrors `LLMClient.localDispatcher`. */\n private static localDispatcher: UndiciAgent | null = null;\n\n private static getLocalDispatcher(): UndiciAgent {\n if (ImageClient.localDispatcher === null) {\n ImageClient.localDispatcher = new UndiciAgent({ headersTimeout: 0, bodyTimeout: 0 });\n }\n return ImageClient.localDispatcher;\n }\n\n private async doFetch(url: string, init: RequestInit): Promise<Response | UndiciResponse> {\n if (this.provider === 'local' && this.fetchImpl === fetch) {\n const uinit: UndiciRequestInit = {\n method: init.method,\n headers: init.headers as UndiciRequestInit['headers'],\n body: init.body as UndiciRequestInit['body'],\n dispatcher: ImageClient.getLocalDispatcher(),\n };\n return undiciFetch(url, uinit);\n }\n return this.fetchImpl(url, init);\n }\n}\n\nexport const IMAGE_PROVIDER_CATALOG: ReadonlyArray<{ provider: ImageProvider; models: string[]; defaultModel: string }> = [\n {\n provider: 'openrouter',\n models: Object.values(OPENROUTER_IMAGE_MODELS),\n defaultModel: OPENROUTER_IMAGE_DEFAULTS.sheet,\n },\n {\n provider: 'local',\n models: Object.values(LOCAL_IMAGE_MODELS),\n defaultModel: LOCAL_IMAGE_DEFAULTS.sheet,\n },\n];\n","/**\n * Truncation Detector\n *\n * Utilities for detecting when LLM output has been truncated and\n * extracting usable content from partial responses.\n *\n * @packageDocumentation\n */\n\nimport type { LLMFinishReason } from './client.js';\nimport { autoCloseJson } from './json-parser.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport type TruncationReason =\n | 'finish_reason'\n | 'json_incomplete'\n | 'bracket_mismatch'\n | 'none';\n\nexport interface TruncationResult {\n isTruncated: boolean;\n reason: TruncationReason;\n partialContent?: string;\n lastCompleteElement?: unknown;\n missingCloseBrackets?: number;\n missingCloseBraces?: number;\n}\n\n// ============================================================================\n// Main Detection Function\n// ============================================================================\n\n/**\n * Detect if an LLM response has been truncated.\n *\n * Analyzes the response content and finish reason to determine if\n * the output was cut off due to token limits or other issues.\n *\n * @param {string} response - The LLM response text\n * @param {LLMFinishReason} finishReason - The finish reason from the LLM\n * @returns {TruncationResult} Detection result with truncation details\n */\nexport function detectTruncation(\n response: string,\n finishReason: LLMFinishReason,\n): TruncationResult {\n if (finishReason === 'length') {\n const bracketInfo = countBrackets(response);\n return {\n isTruncated: true,\n reason: 'finish_reason',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n try {\n JSON.parse(response);\n return { isTruncated: false, reason: 'none' };\n } catch {\n // JSON is invalid, check if due to truncation\n }\n\n if (finishReason === 'stop' || finishReason === null) {\n const trimmed = response.trim();\n\n const isMidContent =\n trimmed.endsWith(',') ||\n trimmed.endsWith(':') ||\n trimmed.endsWith('\": ') ||\n /:\\s*$/.test(trimmed) ||\n /,\\s*$/.test(trimmed);\n\n if (isMidContent) {\n const bracketInfo = countBrackets(response);\n return {\n isTruncated: true,\n reason: 'json_incomplete',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n try {\n const closed = autoCloseJson(trimmed);\n JSON.parse(closed);\n return { isTruncated: false, reason: 'none' };\n } catch {\n return { isTruncated: false, reason: 'none' };\n }\n }\n\n const bracketInfo = countBrackets(response);\n if (\n bracketInfo.missingCloseBrackets > 0 ||\n bracketInfo.missingCloseBraces > 0\n ) {\n return {\n isTruncated: true,\n reason: 'bracket_mismatch',\n partialContent: response,\n lastCompleteElement: findLastCompleteElement(response),\n missingCloseBrackets: bracketInfo.missingCloseBrackets,\n missingCloseBraces: bracketInfo.missingCloseBraces,\n };\n }\n\n return { isTruncated: false, reason: 'none' };\n}\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\nfunction countBrackets(json: string): {\n openBrackets: number;\n closeBrackets: number;\n openBraces: number;\n closeBraces: number;\n missingCloseBrackets: number;\n missingCloseBraces: number;\n} {\n let inString = false;\n let escaped = false;\n let openBrackets = 0;\n let closeBrackets = 0;\n let openBraces = 0;\n let closeBraces = 0;\n\n for (const char of json) {\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n switch (char) {\n case '[':\n openBrackets++;\n break;\n case ']':\n closeBrackets++;\n break;\n case '{':\n openBraces++;\n break;\n case '}':\n closeBraces++;\n break;\n }\n }\n\n return {\n openBrackets,\n closeBrackets,\n openBraces,\n closeBraces,\n missingCloseBrackets: Math.max(0, openBrackets - closeBrackets),\n missingCloseBraces: Math.max(0, openBraces - closeBraces),\n };\n}\n\nexport function findLastCompleteElement(json: string): unknown | null {\n const autoClosed = autoCloseJson(json);\n try {\n return JSON.parse(autoClosed);\n } catch {\n // Auto-close didn't work\n }\n\n const trimmed = json.trim();\n\n if (trimmed.startsWith('[')) {\n const lastCompleteIndex = findLastCompleteArrayElement(trimmed);\n if (lastCompleteIndex > 0) {\n const subset = trimmed.substring(0, lastCompleteIndex) + ']';\n try {\n return JSON.parse(subset);\n } catch {\n // Continue\n }\n }\n }\n\n if (trimmed.startsWith('{')) {\n const closed = autoCloseJson(trimmed);\n try {\n return JSON.parse(closed);\n } catch {\n const lastCompleteIndex = findLastCompleteObjectProperty(trimmed);\n if (lastCompleteIndex > 0) {\n const subset = trimmed.substring(0, lastCompleteIndex) + '}';\n try {\n return JSON.parse(subset);\n } catch {\n // Give up\n }\n }\n }\n }\n\n return null;\n}\n\nfunction findLastCompleteArrayElement(json: string): number {\n let depth = 0;\n let inString = false;\n let escaped = false;\n let lastCompleteElementEnd = -1;\n\n for (let i = 0; i < json.length; i++) {\n const char = json[i];\n\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n if (char === '[' || char === '{') {\n depth++;\n } else if (char === ']' || char === '}') {\n depth--;\n if (depth === 1) {\n lastCompleteElementEnd = i + 1;\n }\n } else if (char === ',' && depth === 1) {\n lastCompleteElementEnd = i;\n }\n }\n\n return lastCompleteElementEnd > 0 ? lastCompleteElementEnd : -1;\n}\n\nfunction findLastCompleteObjectProperty(json: string): number {\n let depth = 0;\n let inString = false;\n let escaped = false;\n let lastCommaIndex = -1;\n\n for (let i = 0; i < json.length; i++) {\n const char = json[i];\n\n if (escaped) {\n escaped = false;\n continue;\n }\n if (char === '\\\\' && inString) {\n escaped = true;\n continue;\n }\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n if (inString) continue;\n\n if (char === '[' || char === '{') {\n depth++;\n } else if (char === ']' || char === '}') {\n depth--;\n } else if (char === ',' && depth === 1) {\n lastCommaIndex = i;\n }\n }\n\n return lastCommaIndex > 0 ? lastCommaIndex : -1;\n}\n\nexport function isLikelyTruncated(content: string): boolean {\n const trimmed = content.trim();\n if (!trimmed) return false;\n\n const brackets = countBrackets(trimmed);\n if (\n brackets.missingCloseBrackets > 0 ||\n brackets.missingCloseBraces > 0\n ) {\n return true;\n }\n\n const abruptEndings = [\n /,\\s*$/,\n /:\\s*$/,\n /\"\\s*:\\s*$/,\n /\\[\\s*$/,\n /{\\s*$/,\n ];\n\n for (const pattern of abruptEndings) {\n if (pattern.test(trimmed)) return true;\n }\n\n return false;\n}\n","/**\n * LLM Continuation Utility\n *\n * Handles truncated LLM responses with automatic continuation.\n * - Detects truncation via finish_reason and JSON structure\n * - Automatically continues with full context\n * - Merges partial and continuation responses\n * - Salvages partial data if max continuations reached\n *\n * @packageDocumentation\n */\n\nimport { z } from 'zod';\nimport { LLMClient, type LLMFinishReason } from './client.js';\nimport { detectTruncation } from './truncation-detector.js';\nimport { extractJsonFromText, autoCloseJson, isValidJson } from './json-parser.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface ContinuationOptions<T> {\n client: LLMClient;\n systemPrompt: string;\n userPrompt: string;\n schema?: z.ZodSchema<T>;\n maxTokens?: number;\n maxContinuations?: number;\n maxRetries?: number;\n buildContinuationPrompt: (\n partialResponse: string,\n attempt: number,\n ) => string;\n continuationSystemPrompt?: string;\n}\n\nexport interface ContinuationResult<T> {\n data: T;\n raw: string;\n continuationCount: number;\n warnings: string[];\n wasSalvaged: boolean;\n}\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst DEFAULT_MAX_TOKENS = 8192;\nconst DEFAULT_MAX_CONTINUATIONS = 3;\n\n/**\n * Default continuation system prompt.\n * Used when no custom continuationSystemPrompt is provided.\n */\nconst DEFAULT_CONTINUATION_SYSTEM_PROMPT = `You are a JSON continuation assistant. Your ONLY job is to continue generating JSON from where the previous response was truncated.\n\nRules:\n1. Continue from EXACTLY where the previous output stopped\n2. Do NOT repeat any content already generated\n3. Complete the JSON structure properly with all closing brackets\n4. Do NOT wrap in markdown code blocks\n5. Output ONLY the continuation JSON, nothing else`;\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\n/**\n * Merge a previous partial response with a continuation.\n *\n * Handles JSON structure merging, removing markdown code blocks,\n * and ensuring proper comma separation between merged parts.\n *\n * @param {string} previous - The previous partial response\n * @param {string} continuation - The continuation to merge\n * @returns {string} The merged response\n */\nexport function mergeResponses(\n previous: string,\n continuation: string,\n): string {\n const trimmedPrev = previous.trimEnd();\n const trimmedCont = continuation.trimStart();\n\n let cleanedCont = trimmedCont\n .replace(/^```json?\\s*/i, '')\n .replace(/```\\s*$/i, '')\n .trim();\n\n if (cleanedCont.startsWith('{')) {\n try {\n const contParsed = JSON.parse(autoCloseJson(cleanedCont));\n const keys = Object.keys(contParsed);\n if (keys.length === 1 && Array.isArray(contParsed[keys[0]])) {\n cleanedCont = contParsed[keys[0]]\n .map((item: unknown) => JSON.stringify(item))\n .join(',\\n');\n }\n } catch {\n // Continue with original cleaning\n }\n }\n\n if (cleanedCont.startsWith('}') || cleanedCont.startsWith(']')) {\n return trimmedPrev + cleanedCont;\n }\n\n const prevEndsWithValue = /[\\}\\]\\\"\\d]$/.test(trimmedPrev);\n const contStartsWithValue = /^[\\{\\[\\\"]/.test(cleanedCont);\n\n if (prevEndsWithValue && contStartsWithValue) {\n return trimmedPrev + ',\\n' + cleanedCont;\n }\n\n return trimmedPrev + cleanedCont;\n}\n\nexport function salvagePartialResponse<T>(rawResponse: string): T | null {\n console.warn('[Continuation] Attempting to salvage partial response');\n\n try {\n const cleanedResponse = extractJsonFromText(rawResponse) || rawResponse;\n const closed = autoCloseJson(cleanedResponse);\n const parsed = JSON.parse(closed) as T;\n console.log('[Continuation] Successfully salvaged partial response');\n return parsed;\n } catch (error) {\n console.error('[Continuation] Could not salvage response:', error);\n }\n\n return null;\n}\n\n// ============================================================================\n// Main Function\n// ============================================================================\n\n/**\n * Call an LLM with automatic continuation handling.\n *\n * Manages token limits by detecting truncation and making continuation\n * calls until the complete response is received or max continuations reached.\n *\n * @template T - Expected response type\n * @param {ContinuationOptions<T>} options - Continuation call options\n * @returns {Promise<ContinuationResult<T>>} Result with data and continuation metadata\n */\nexport async function callWithContinuation<T>(\n options: ContinuationOptions<T>,\n): Promise<ContinuationResult<T>> {\n const {\n client,\n systemPrompt,\n userPrompt,\n schema,\n maxTokens = DEFAULT_MAX_TOKENS,\n maxContinuations = DEFAULT_MAX_CONTINUATIONS,\n buildContinuationPrompt,\n continuationSystemPrompt = DEFAULT_CONTINUATION_SYSTEM_PROMPT,\n } = options;\n\n let rawResponse = '';\n let continuationCount = 0;\n const warnings: string[] = [];\n let wasSalvaged = false;\n\n console.log('[Continuation] Starting LLM call with continuation support');\n console.log(\n `[Continuation] Max tokens: ${maxTokens}, Max continuations: ${maxContinuations}`,\n );\n\n try {\n const response = await client.callRawWithMetadata({\n systemPrompt,\n userPrompt,\n maxTokens,\n });\n\n rawResponse = extractJsonFromText(response.raw) || response.raw;\n\n console.log(\n `[Continuation] Initial response: ${rawResponse.length} chars, finish_reason: ${response.finishReason}`,\n );\n\n let truncation = detectTruncation(rawResponse, response.finishReason);\n\n while (truncation.isTruncated && continuationCount < maxContinuations) {\n continuationCount++;\n const warningMsg = `Response truncated (${truncation.reason}), continuing (attempt ${continuationCount}/${maxContinuations})`;\n console.log(`[Continuation] ${warningMsg}`);\n warnings.push(warningMsg);\n\n const contPrompt = buildContinuationPrompt(\n rawResponse,\n continuationCount,\n );\n\n const contResponse = await client.callRawWithMetadata({\n systemPrompt: continuationSystemPrompt,\n userPrompt: contPrompt,\n maxTokens,\n });\n\n console.log(\n `[Continuation] Continuation response: ${contResponse.raw.length} chars, finish_reason: ${contResponse.finishReason}`,\n );\n\n const cleanedContResponse =\n extractJsonFromText(contResponse.raw) || contResponse.raw;\n rawResponse = mergeResponses(rawResponse, cleanedContResponse);\n\n truncation = detectTruncation(rawResponse, contResponse.finishReason);\n }\n\n if (\n continuationCount >= maxContinuations &&\n truncation.isTruncated\n ) {\n console.warn(\n `[Continuation] Reached max continuations (${maxContinuations}), attempting to salvage...`,\n );\n warnings.push(\n `Reached max continuations - some content may be incomplete`,\n );\n wasSalvaged = true;\n }\n\n const cleanedResponse =\n extractJsonFromText(rawResponse) || rawResponse;\n let data: T;\n\n try {\n if (isValidJson(cleanedResponse)) {\n data = JSON.parse(cleanedResponse) as T;\n } else {\n const closed = autoCloseJson(cleanedResponse);\n data = JSON.parse(closed) as T;\n if (!wasSalvaged) {\n warnings.push('Response required auto-closing of JSON brackets');\n }\n }\n } catch (parseError) {\n const salvaged = salvagePartialResponse<T>(cleanedResponse);\n if (salvaged) {\n data = salvaged;\n wasSalvaged = true;\n warnings.push('Response was salvaged from partial data');\n } else {\n throw new Error(\n `Failed to parse response after ${continuationCount} continuations: ${parseError}`,\n );\n }\n }\n\n if (schema) {\n try {\n data = schema.parse(data);\n } catch (validationError) {\n console.warn(\n '[Continuation] Schema validation failed:',\n validationError,\n );\n warnings.push(`Schema validation issue: ${validationError}`);\n }\n }\n\n console.log(\n `[Continuation] Complete. Continuations: ${continuationCount}, Warnings: ${warnings.length}`,\n );\n\n return {\n data,\n raw: rawResponse,\n continuationCount,\n warnings,\n wasSalvaged,\n };\n } catch (error) {\n console.error('[Continuation] Error during LLM call:', error);\n throw error;\n }\n}\n\n/**\n * Build a generic continuation prompt.\n *\n * Creates a prompt that instructs the LLM to continue from where\n * a previous truncated response left off.\n *\n * @param {string} context - Original context/prompt\n * @param {string} partialResponse - The partial response generated so far\n * @param {number} attempt - Current continuation attempt number\n * @param {number} [maxAttempts] - Maximum allowed continuation attempts\n * @returns {string} The formatted continuation prompt\n */\nexport function buildGenericContinuationPrompt(\n context: string,\n partialResponse: string,\n attempt: number,\n maxAttempts: number = DEFAULT_MAX_CONTINUATIONS,\n): string {\n return `## CONTINUATION REQUEST (Attempt ${attempt}/${maxAttempts})\n\nYour previous response was truncated. Continue generating from where you left off.\n\n### ORIGINAL CONTEXT\n${context}\n\n### WHAT YOU GENERATED SO FAR\n\\`\\`\\`json\n${partialResponse}\n\\`\\`\\`\n\n### INSTRUCTIONS\n1. Continue from EXACTLY where the response was cut off\n2. Do NOT repeat any content already generated\n3. Complete the JSON structure properly\n4. Do NOT wrap your response in markdown code blocks\n\nContinue generating now:`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAM,sBAAoD,CAAC,UAAU,cAAc,UAAU;AAiBtF,SAAS,kCAAqD;AACnE,QAAM,IAAI,QAAQ,IAAI;AACtB,SAAO,KAAM,oBAA0C,SAAS,CAAC,IAAK,IAA0B;AAClG;AAmCA,IAAM,qBAAqB;AAC3B,IAAM,yBAAyB,CAAC,KAAK,GAAI;AAGzC,IAAM,oBACJ;AAEF,IAAM,iBAAoD;AAAA,EACxD,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,UAAU;AACZ;AAEA,IAAM,oBAAuD;AAAA,EAC3D,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,UAAU;AACZ;AAEA,IAAM,mBAA+D;AAAA,EACnE,QAAQ;AAAA,EACR,YAAY;AACd;AAEO,IAAM,kBAAN,MAAsB;AAAA,EAM3B,YAAY,MAA8B;AACxC,SAAK,WAAW,KAAK;AACrB,SAAK,QAAQ,KAAK,UAAU,KAAK,aAAa,aAAa,QAAQ,IAAI,4BAA4B,eAAe,WAAW,eAAe,KAAK,QAAQ;AACzJ,UAAM,iBAAiB,KAAK,aAAa,aAAa,QAAQ,IAAI,gBAAgB,kBAAkB,WAAW,kBAAkB,KAAK,QAAQ;AAC9I,SAAK,WAAW,KAAK,WAAW,gBAAgB,QAAQ,OAAO,EAAE;AAEjE,QAAI,KAAK,aAAa,YAAY;AAEhC,WAAK,SAAS,KAAK,UAAU;AAC7B;AAAA,IACF;AACA,UAAM,SAAS,KAAK,UAAU,KAAK,cAAc;AACjD,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI;AAAA,QACR,0CAA0C,KAAK,QAAQ,oBAC9C,iBAAiB,KAAK,QAAQ,CAAC;AAAA,M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AM,WAAW,uBAA0B,eAAe;AAC1D,UAAI,UAAU;AACZ,eAAO;AACP,sBAAc;AACd,iBAAS,KAAK,yCAAyC;AAAA,MACzD,OAAO;AACL,cAAM,IAAI;AAAA,UACR,kCAAkC,iBAAiB,mBAAmB,UAAU;AAAA,QAClF;AAAA,MACF;AAAA,IACF;AAEA,QAAI,QAAQ;AACV,UAAI;AACF,eAAO,OAAO,MAAM,IAAI;AAAA,MAC1B,SAAS,iBAAiB;AACxB,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,QACF;AACA,iBAAS,KAAK,4BAA4B,eAAe,EAAE;AAAA,MAC7D;AAAA,IACF;AAEA,YAAQ;AAAA,MACN,2CAA2C,iBAAiB,eAAe,SAAS,MAAM;AAAA,IAC5F;AAEA,WAAO;AAAA,MACL;AAAA,MACA,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,yCAAyC,KAAK;AAC5D,UAAM;AAAA,EACR;AACF;AAcO,SAAS,+BACd,SACA,iBACA,SACA,cAAsB,2BACd;AACR,SAAO,oCAAoC,OAAO,IAAI,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAKjE,OAAO;AAAA;AAAA;AAAA;AAAA,EAIP,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUjB;","names":["DEFAULT_MODELS","DEFAULT_BASE_URLS","API_KEY_ENV_VARS","bracketInfo"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@almadar/llm",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.50.0",
|
|
4
4
|
"description": "Multi-provider LLM client with rate limiting, token tracking, structured outputs, and continuation handling",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
"zod": "^3.22.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@almadar/core": "^10.
|
|
50
|
+
"@almadar/core": "^10.88.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@almadar/core": "^10.
|
|
53
|
+
"@almadar/core": "^10.88.0",
|
|
54
54
|
"@almadar/eslint-plugin": "^2.18.0",
|
|
55
55
|
"@types/node": "^22.0.0",
|
|
56
56
|
"@typescript-eslint/parser": "8.65.0",
|
package/src/image-client.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Image Client
|
|
3
3
|
*
|
|
4
|
-
* Single-purpose client for
|
|
5
|
-
*
|
|
4
|
+
* Single-purpose client for image-generation APIs. `openrouter` posts to
|
|
5
|
+
* `POST {baseUrl}/images`; `local` posts an OpenAI-shaped body to
|
|
6
|
+
* `POST {baseUrl}/images/generations` (the MFLUX shim in
|
|
7
|
+
* tools/asset-workflow/python/image_server). Raw `fetch`, no SDK — mirrors
|
|
6
8
|
* `embedding-client.ts`'s shape (provider tables, retry policy, the
|
|
7
9
|
* three response-error surfaces).
|
|
8
10
|
*
|
|
@@ -10,10 +12,11 @@
|
|
|
10
12
|
*/
|
|
11
13
|
|
|
12
14
|
import type { JsonValue } from '@almadar/core';
|
|
15
|
+
import { Agent as UndiciAgent, fetch as undiciFetch, type RequestInit as UndiciRequestInit, type Response as UndiciResponse } from 'undici';
|
|
13
16
|
|
|
14
|
-
export type ImageProvider = 'openrouter';
|
|
17
|
+
export type ImageProvider = 'openrouter' | 'local';
|
|
15
18
|
|
|
16
|
-
const IMAGE_PROVIDERS: readonly ImageProvider[] = ['openrouter'];
|
|
19
|
+
const IMAGE_PROVIDERS: readonly ImageProvider[] = ['openrouter', 'local'];
|
|
17
20
|
|
|
18
21
|
/** Canonical image-provider resolution, mirrors `resolveDefaultEmbeddingProvider`. */
|
|
19
22
|
export function resolveDefaultImageProvider(): ImageProvider {
|
|
@@ -34,6 +37,18 @@ export const OPENROUTER_IMAGE_DEFAULTS = {
|
|
|
34
37
|
preview: OPENROUTER_IMAGE_MODELS.GEMINI_3_1_FLASH_LITE_IMAGE,
|
|
35
38
|
} as const;
|
|
36
39
|
|
|
40
|
+
export const LOCAL_IMAGE_MODELS = {
|
|
41
|
+
Z_IMAGE_TURBO: 'z-image-turbo',
|
|
42
|
+
FLUX_SCHNELL: 'flux-schnell',
|
|
43
|
+
FLUX_DEV: 'flux-dev',
|
|
44
|
+
} as const;
|
|
45
|
+
|
|
46
|
+
export const LOCAL_IMAGE_DEFAULTS = {
|
|
47
|
+
/** Z-Image-Turbo is the default: public on Hugging Face, no token required (FLUX is gated). */
|
|
48
|
+
sheet: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO,
|
|
49
|
+
preview: LOCAL_IMAGE_MODELS.Z_IMAGE_TURBO,
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
37
52
|
export interface ImageClientOptions {
|
|
38
53
|
provider?: ImageProvider;
|
|
39
54
|
model?: string;
|
|
@@ -85,14 +100,17 @@ const IMAGE_RETRY_BACKOFF_MS = [500, 2000];
|
|
|
85
100
|
|
|
86
101
|
const DEFAULT_MODELS: Record<ImageProvider, string> = {
|
|
87
102
|
openrouter: OPENROUTER_IMAGE_DEFAULTS.sheet,
|
|
103
|
+
local: LOCAL_IMAGE_DEFAULTS.sheet,
|
|
88
104
|
};
|
|
89
105
|
|
|
90
106
|
const DEFAULT_BASE_URLS: Record<ImageProvider, string> = {
|
|
91
107
|
openrouter: 'https://openrouter.ai/api/v1',
|
|
108
|
+
local: 'http://127.0.0.1:8180/v1',
|
|
92
109
|
};
|
|
93
110
|
|
|
94
111
|
const API_KEY_ENV_VARS: Record<ImageProvider, string> = {
|
|
95
112
|
openrouter: 'OPEN_ROUTER_API_KEY',
|
|
113
|
+
local: 'LOCAL_IMAGE_API_KEY',
|
|
96
114
|
};
|
|
97
115
|
|
|
98
116
|
/**
|
|
@@ -167,13 +185,14 @@ export class ImageClient {
|
|
|
167
185
|
private readonly capabilitiesCache = new Map<string, ImageModelCapabilities>();
|
|
168
186
|
|
|
169
187
|
constructor(opts: ImageClientOptions = {}) {
|
|
170
|
-
this.provider = opts.provider ??
|
|
188
|
+
this.provider = opts.provider ?? resolveDefaultImageProvider();
|
|
171
189
|
this.model = opts.model ?? DEFAULT_MODELS[this.provider];
|
|
172
190
|
this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URLS[this.provider]).replace(/\/$/, '');
|
|
173
191
|
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
174
192
|
|
|
175
|
-
|
|
176
|
-
|
|
193
|
+
// The local shim is unauthenticated; a key is required only for hosted providers.
|
|
194
|
+
const apiKey = opts.apiKey ?? process.env[API_KEY_ENV_VARS[this.provider]] ?? '';
|
|
195
|
+
if (!apiKey && this.provider !== 'local') {
|
|
177
196
|
throw new Error(
|
|
178
197
|
`ImageClient: API key for provider '${this.provider}' not found. ` +
|
|
179
198
|
`Set ${API_KEY_ENV_VARS[this.provider]} in your environment or pass options.apiKey.`,
|
|
@@ -197,9 +216,16 @@ export class ImageClient {
|
|
|
197
216
|
const cached = this.capabilitiesCache.get(model);
|
|
198
217
|
if (cached) return cached;
|
|
199
218
|
|
|
219
|
+
// The local shim serves no `.../endpoints` metadata; declare what it honors.
|
|
220
|
+
if (this.provider === 'local') {
|
|
221
|
+
const local: ImageModelCapabilities = { model, supportedParameters: new Set(['seed', IMAGE_CHECKED_PARAM_NAMES.references]), raw: {} };
|
|
222
|
+
this.capabilitiesCache.set(model, local);
|
|
223
|
+
return local;
|
|
224
|
+
}
|
|
225
|
+
|
|
200
226
|
const response = await this.fetchWithRetry(`${this.baseUrl}/images/models/${model}/endpoints`, {
|
|
201
227
|
method: 'GET',
|
|
202
|
-
headers:
|
|
228
|
+
headers: this.authHeaders(),
|
|
203
229
|
});
|
|
204
230
|
if (!response.ok) {
|
|
205
231
|
const errorText = await response.text().catch(() => '<no body>');
|
|
@@ -250,6 +276,11 @@ export class ImageClient {
|
|
|
250
276
|
throw new Error('ImageClient.generate: `size` cannot be combined with `resolution`/`aspectRatio` — pass one or the other');
|
|
251
277
|
}
|
|
252
278
|
const model = opts.model ?? this.model;
|
|
279
|
+
|
|
280
|
+
if (this.provider === 'local') {
|
|
281
|
+
return this.performLocalGenerate(opts, model, dropped);
|
|
282
|
+
}
|
|
283
|
+
|
|
253
284
|
const requestBody: Record<string, JsonValue> = { model, prompt: opts.prompt, output_format: 'png' };
|
|
254
285
|
if (opts.n !== undefined) requestBody.n = opts.n;
|
|
255
286
|
if (opts.size !== undefined) requestBody.size = opts.size;
|
|
@@ -269,9 +300,48 @@ export class ImageClient {
|
|
|
269
300
|
requestBody.provider = providerField;
|
|
270
301
|
}
|
|
271
302
|
|
|
272
|
-
|
|
303
|
+
return this.sendGenerate(`${this.baseUrl}/images`, requestBody, model, dropped);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* OpenAI-compatible local server (the MFLUX shim in tools/asset-workflow).
|
|
308
|
+
* OpenRouter-only fields are reported in `dropped`, never sent. References
|
|
309
|
+
* ARE sent (as `input_references`) — the shim forwards them to mflux's
|
|
310
|
+
* `image_path`/`image_strength` img2img conditioning.
|
|
311
|
+
*/
|
|
312
|
+
private async performLocalGenerate(
|
|
313
|
+
opts: ImageGenerateOptions,
|
|
314
|
+
model: string,
|
|
315
|
+
dropped: string[],
|
|
316
|
+
): Promise<ImageGenerateResult> {
|
|
317
|
+
const requestBody: Record<string, JsonValue> = { model, prompt: opts.prompt };
|
|
318
|
+
if (opts.n !== undefined) requestBody.n = opts.n;
|
|
319
|
+
if (opts.size !== undefined) requestBody.size = opts.size;
|
|
320
|
+
if (opts.seed !== undefined) requestBody.seed = opts.seed;
|
|
321
|
+
if (opts.references !== undefined && opts.references.length > 0) {
|
|
322
|
+
requestBody.input_references = opts.references.map((r) => ({ type: 'image_url', image_url: { url: r.url } }));
|
|
323
|
+
}
|
|
324
|
+
for (const field of ['resolution', 'aspectRatio', 'quality', 'background'] as const) {
|
|
325
|
+
if (opts[field] !== undefined) dropped.push(field);
|
|
326
|
+
}
|
|
327
|
+
if (opts.provider !== undefined) dropped.push('provider');
|
|
328
|
+
|
|
329
|
+
return this.sendGenerate(`${this.baseUrl}/images/generations`, requestBody, model, dropped);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private authHeaders(): Record<string, string> {
|
|
333
|
+
return this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private async sendGenerate(
|
|
337
|
+
url: string,
|
|
338
|
+
requestBody: Record<string, JsonValue>,
|
|
339
|
+
model: string,
|
|
340
|
+
dropped: string[],
|
|
341
|
+
): Promise<ImageGenerateResult> {
|
|
342
|
+
const response = await this.fetchWithRetry(url, {
|
|
273
343
|
method: 'POST',
|
|
274
|
-
headers: { 'Content-Type': 'application/json',
|
|
344
|
+
headers: { 'Content-Type': 'application/json', ...this.authHeaders() },
|
|
275
345
|
body: JSON.stringify(requestBody),
|
|
276
346
|
});
|
|
277
347
|
if (!response.ok) {
|
|
@@ -313,24 +383,52 @@ export class ImageClient {
|
|
|
313
383
|
return { images, usage, model, dropped };
|
|
314
384
|
}
|
|
315
385
|
|
|
316
|
-
/** Retries on network failure or a 5xx status; any other response is returned as-is.
|
|
317
|
-
|
|
386
|
+
/** Retries on network failure or a 5xx status; any other response is returned as-is.
|
|
387
|
+
* The local provider never retries: a failed local generation (e.g. MLX OOM) cannot
|
|
388
|
+
* succeed on an immediate retry, and hammering a memory-pressured machine is harmful. */
|
|
389
|
+
private async fetchWithRetry(url: string, init: RequestInit): Promise<Response | UndiciResponse> {
|
|
390
|
+
const maxAttempts = this.provider === 'local' ? 1 : IMAGE_MAX_ATTEMPTS;
|
|
318
391
|
let lastNetworkError: Error | undefined;
|
|
319
|
-
let response: Response | undefined;
|
|
320
|
-
for (let attempt = 1; attempt <=
|
|
392
|
+
let response: Response | UndiciResponse | undefined;
|
|
393
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
321
394
|
try {
|
|
322
|
-
response = await this.
|
|
395
|
+
response = await this.doFetch(url, init);
|
|
323
396
|
} catch (e) {
|
|
324
397
|
response = undefined;
|
|
325
398
|
lastNetworkError = e instanceof Error ? e : new Error(String(e));
|
|
326
399
|
}
|
|
327
400
|
if (response !== undefined && response.status < 500) return response;
|
|
328
401
|
const backoff = IMAGE_RETRY_BACKOFF_MS[attempt - 1];
|
|
329
|
-
if (backoff === undefined) break;
|
|
402
|
+
if (backoff === undefined || attempt >= maxAttempts) break;
|
|
330
403
|
await new Promise((r) => setTimeout(r, backoff));
|
|
331
404
|
}
|
|
332
405
|
if (response) return response;
|
|
333
|
-
throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${
|
|
406
|
+
throw lastNetworkError ?? new Error(`ImageClient: ${this.provider} request failed after ${maxAttempts} attempts`);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Local generations can exceed Node's built-in 300s headersTimeout (a single
|
|
410
|
+
* MFLUX image routinely takes minutes), so local calls ride npm-undici's own
|
|
411
|
+
* fetch with the transport timers disabled — mirrors `LLMClient.localDispatcher`. */
|
|
412
|
+
private static localDispatcher: UndiciAgent | null = null;
|
|
413
|
+
|
|
414
|
+
private static getLocalDispatcher(): UndiciAgent {
|
|
415
|
+
if (ImageClient.localDispatcher === null) {
|
|
416
|
+
ImageClient.localDispatcher = new UndiciAgent({ headersTimeout: 0, bodyTimeout: 0 });
|
|
417
|
+
}
|
|
418
|
+
return ImageClient.localDispatcher;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
private async doFetch(url: string, init: RequestInit): Promise<Response | UndiciResponse> {
|
|
422
|
+
if (this.provider === 'local' && this.fetchImpl === fetch) {
|
|
423
|
+
const uinit: UndiciRequestInit = {
|
|
424
|
+
method: init.method,
|
|
425
|
+
headers: init.headers as UndiciRequestInit['headers'],
|
|
426
|
+
body: init.body as UndiciRequestInit['body'],
|
|
427
|
+
dispatcher: ImageClient.getLocalDispatcher(),
|
|
428
|
+
};
|
|
429
|
+
return undiciFetch(url, uinit);
|
|
430
|
+
}
|
|
431
|
+
return this.fetchImpl(url, init);
|
|
334
432
|
}
|
|
335
433
|
}
|
|
336
434
|
|
|
@@ -340,4 +438,9 @@ export const IMAGE_PROVIDER_CATALOG: ReadonlyArray<{ provider: ImageProvider; mo
|
|
|
340
438
|
models: Object.values(OPENROUTER_IMAGE_MODELS),
|
|
341
439
|
defaultModel: OPENROUTER_IMAGE_DEFAULTS.sheet,
|
|
342
440
|
},
|
|
441
|
+
{
|
|
442
|
+
provider: 'local',
|
|
443
|
+
models: Object.values(LOCAL_IMAGE_MODELS),
|
|
444
|
+
defaultModel: LOCAL_IMAGE_DEFAULTS.sheet,
|
|
445
|
+
},
|
|
343
446
|
];
|