@devvibex/aiflow 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devvibex/aiflow",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "First-party LLM gateway SDK for apps generated on the VibeX platform — chat, streaming, image generation and embeddings without shipping any third-party API key.",
5
5
  "keywords": [
6
6
  "ai",
package/src/index.d.ts CHANGED
@@ -93,6 +93,8 @@ export interface ImageRequest {
93
93
  */
94
94
  language?: string;
95
95
  signal?: AbortSignal;
96
+ /** Per-call timeout (ms). Defaults to 180000 — image edits exceed the 60s client default. */
97
+ timeout?: number;
96
98
  }
97
99
 
98
100
  export interface GeneratedImage {
@@ -106,6 +108,56 @@ export interface ImageResponse {
106
108
  usage: { creditsUsed: number };
107
109
  }
108
110
 
111
+ // ─── Batch image ────────────────────────────────────────────────────────────
112
+
113
+ /**
114
+ * One page in a batch image request. Mirrors {@link ImageRequest} but `prompt`
115
+ * is optional — it can be supplied once at the batch level and shared across
116
+ * every page (the common case for translating all pages of a PDF).
117
+ */
118
+ export interface ImageBatchPage {
119
+ prompt?: string;
120
+ model?: string;
121
+ size?: '512x512' | '1024x1024' | '1024x1792' | string;
122
+ aspectRatio?: string;
123
+ n?: number;
124
+ image?: string;
125
+ images?: string[];
126
+ system?: string;
127
+ language?: string;
128
+ }
129
+
130
+ export interface ImageBatchRequest {
131
+ /** Pages to process (1–20). */
132
+ pages: ImageBatchPage[];
133
+ /** Shared defaults applied to any page that omits the field. */
134
+ prompt?: string;
135
+ model?: string;
136
+ system?: string;
137
+ language?: string;
138
+ size?: '512x512' | '1024x1024' | '1024x1792' | string;
139
+ /** Max pages processed simultaneously (1–4, default 3 server-side). */
140
+ concurrency?: number;
141
+ signal?: AbortSignal;
142
+ /** Per-call timeout (ms). Defaults to 600000 (10 min). */
143
+ timeout?: number;
144
+ }
145
+
146
+ export interface ImageBatchPageResult {
147
+ index: number;
148
+ status: 'success' | 'error';
149
+ images?: GeneratedImage[];
150
+ creditsUsed?: number;
151
+ error?: string;
152
+ }
153
+
154
+ export interface ImageBatchResponse {
155
+ results: ImageBatchPageResult[];
156
+ successCount: number;
157
+ errorCount: number;
158
+ totalCreditsUsed: number;
159
+ }
160
+
109
161
  // ─── Video ────────────────────────────────────────────────────────────────────
110
162
 
111
163
  export interface VideoRequest {
@@ -215,6 +267,7 @@ export class AIFlowClient {
215
267
  chat(request: ChatRequest): Promise<ChatResponse>;
216
268
  chatStream(request: ChatRequest): AsyncGenerator<ChatStreamChunk, void, void>;
217
269
  generateImage(request: ImageRequest): Promise<ImageResponse>;
270
+ generateImageBatch(request: ImageBatchRequest): Promise<ImageBatchResponse>;
218
271
  generateVideo(request: VideoRequest): Promise<VideoResponse>;
219
272
  embed(request: EmbedRequest): Promise<EmbedResponse>;
220
273
  vision(request: VisionRequest): Promise<VisionResponse>;
package/src/index.js CHANGED
@@ -249,6 +249,7 @@ export class AIFlowClient {
249
249
  system,
250
250
  language,
251
251
  signal,
252
+ timeout,
252
253
  } = {}) {
253
254
  if (!prompt || typeof prompt !== 'string') {
254
255
  throw new AIFlowError('prompt is required');
@@ -271,7 +272,57 @@ export class AIFlowClient {
271
272
  system: resolvedSystem,
272
273
  language,
273
274
  },
274
- { signal },
275
+ // Image edits (esp. document translation) routinely exceed the 60s
276
+ // client default — a single 2K edit was observed at ~63s. Default
277
+ // to 3 minutes so the SDK doesn't abort a request the server is
278
+ // still completing.
279
+ { signal, timeout: timeout ?? 180_000 },
280
+ );
281
+ return await res.json();
282
+ }
283
+
284
+ /**
285
+ * Batch image generation/editing — send many pages (e.g. every page of a
286
+ * translated PDF) in ONE call. The server processes them with bounded
287
+ * concurrency and isolates per-page failures, so one page hitting
288
+ * MAX_TOKENS or a rate-limit never fails the whole job. Top-level
289
+ * `prompt`/`model`/`system`/`language`/`size` are shared defaults applied
290
+ * to any page that omits them — handy for translating all pages with the
291
+ * same instruction while varying only the image.
292
+ *
293
+ * @returns {Promise<{results: Array<{index:number,status:'success'|'error',images?:Array<{url?:string,b64?:string,mimeType:string}>,creditsUsed?:number,error?:string}>, successCount:number, errorCount:number, totalCreditsUsed:number}>}
294
+ */
295
+ async generateImageBatch({
296
+ pages,
297
+ prompt,
298
+ model,
299
+ system,
300
+ language,
301
+ size,
302
+ concurrency,
303
+ signal,
304
+ timeout,
305
+ } = {}) {
306
+ if (!Array.isArray(pages) || pages.length === 0) {
307
+ throw new AIFlowError('pages must be a non-empty array');
308
+ }
309
+ const resolvedModel =
310
+ model || (await this._resolveDefaultModel('image'));
311
+ const resolvedSystem = await this._resolveSystemPrompt(system);
312
+ const res = await this._request(
313
+ '/image/batch',
314
+ {
315
+ pages,
316
+ prompt,
317
+ model: resolvedModel,
318
+ system: resolvedSystem,
319
+ language,
320
+ size,
321
+ concurrency,
322
+ },
323
+ // A batch of multi-minute edits needs far longer than a single
324
+ // request — default to 10 minutes, matching the server socket.
325
+ { signal, timeout: timeout ?? 600_000 },
275
326
  );
276
327
  return await res.json();
277
328
  }
@@ -356,20 +407,23 @@ export class AIFlowClient {
356
407
  // Internals
357
408
  // ────────────────────────────────────────────────────────────────────────
358
409
 
359
- async _request(path, body, { signal, stream = false } = {}) {
410
+ async _request(path, body, { signal, stream = false, timeout } = {}) {
360
411
  const url = `${this.baseUrl}${path}`;
361
412
  const headers = {
362
413
  'Content-Type': 'application/json',
363
414
  'x-app-id': this.appId,
364
415
  Accept: stream ? 'text/event-stream' : 'application/json',
365
416
  };
417
+ // Per-call timeout override — image/video calls run far longer than the
418
+ // 60s client default, so they pass a larger value here.
419
+ const effectiveTimeout = timeout ?? this.timeout;
366
420
 
367
421
  let attempt = 0;
368
422
  while (true) {
369
423
  const timeoutCtrl = new AbortController();
370
424
  const timer = setTimeout(
371
425
  () => timeoutCtrl.abort(),
372
- this.timeout,
426
+ effectiveTimeout,
373
427
  );
374
428
  const composedSignal = anySignal(signal, timeoutCtrl.signal);
375
429