@devvibex/aiflow 1.0.7 → 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.7",
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
@@ -81,7 +81,20 @@ export interface ImageRequest {
81
81
  n?: number;
82
82
  image?: string;
83
83
  images?: string[];
84
+ /**
85
+ * System prompt for image generation/editing (e.g. a translation
86
+ * instruction). When omitted, the server-configured `systemPrompt` is used.
87
+ * Prepended to `prompt` server-side since image models take a single prompt.
88
+ */
89
+ system?: string;
90
+ /**
91
+ * Target language for translation-style image editing. Replaces any
92
+ * `{{language}}` placeholder in the system/user prompt server-side.
93
+ */
94
+ language?: string;
84
95
  signal?: AbortSignal;
96
+ /** Per-call timeout (ms). Defaults to 180000 — image edits exceed the 60s client default. */
97
+ timeout?: number;
85
98
  }
86
99
 
87
100
  export interface GeneratedImage {
@@ -95,6 +108,56 @@ export interface ImageResponse {
95
108
  usage: { creditsUsed: number };
96
109
  }
97
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
+
98
161
  // ─── Video ────────────────────────────────────────────────────────────────────
99
162
 
100
163
  export interface VideoRequest {
@@ -184,6 +247,8 @@ export interface AiflowConfig {
184
247
  maxEmbedInputs: number;
185
248
  };
186
249
  creditValue: number;
250
+ /** Module-configured default system prompt, or null if none. */
251
+ systemPrompt?: string | null;
187
252
  }
188
253
 
189
254
  export class AIFlowClient {
@@ -202,6 +267,7 @@ export class AIFlowClient {
202
267
  chat(request: ChatRequest): Promise<ChatResponse>;
203
268
  chatStream(request: ChatRequest): AsyncGenerator<ChatStreamChunk, void, void>;
204
269
  generateImage(request: ImageRequest): Promise<ImageResponse>;
270
+ generateImageBatch(request: ImageBatchRequest): Promise<ImageBatchResponse>;
205
271
  generateVideo(request: VideoRequest): Promise<VideoResponse>;
206
272
  embed(request: EmbedRequest): Promise<EmbedResponse>;
207
273
  vision(request: VisionRequest): Promise<VisionResponse>;
package/src/index.js CHANGED
@@ -246,13 +246,20 @@ export class AIFlowClient {
246
246
  n = 1,
247
247
  image,
248
248
  images,
249
+ system,
250
+ language,
249
251
  signal,
252
+ timeout,
250
253
  } = {}) {
251
254
  if (!prompt || typeof prompt !== 'string') {
252
255
  throw new AIFlowError('prompt is required');
253
256
  }
254
257
  const resolvedModel =
255
258
  model || (await this._resolveDefaultModel('image'));
259
+ // Resolve the system prompt: user-provided > server config > null,
260
+ // mirroring chat(). The server prepends it to the prompt and injects
261
+ // the dynamic `language` into any {{language}} placeholder.
262
+ const resolvedSystem = await this._resolveSystemPrompt(system);
256
263
  const res = await this._request(
257
264
  '/image',
258
265
  {
@@ -262,8 +269,60 @@ export class AIFlowClient {
262
269
  n,
263
270
  image,
264
271
  images,
272
+ system: resolvedSystem,
273
+ language,
265
274
  },
266
- { 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 },
267
326
  );
268
327
  return await res.json();
269
328
  }
@@ -348,20 +407,23 @@ export class AIFlowClient {
348
407
  // Internals
349
408
  // ────────────────────────────────────────────────────────────────────────
350
409
 
351
- async _request(path, body, { signal, stream = false } = {}) {
410
+ async _request(path, body, { signal, stream = false, timeout } = {}) {
352
411
  const url = `${this.baseUrl}${path}`;
353
412
  const headers = {
354
413
  'Content-Type': 'application/json',
355
414
  'x-app-id': this.appId,
356
415
  Accept: stream ? 'text/event-stream' : 'application/json',
357
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;
358
420
 
359
421
  let attempt = 0;
360
422
  while (true) {
361
423
  const timeoutCtrl = new AbortController();
362
424
  const timer = setTimeout(
363
425
  () => timeoutCtrl.abort(),
364
- this.timeout,
426
+ effectiveTimeout,
365
427
  );
366
428
  const composedSignal = anySignal(signal, timeoutCtrl.signal);
367
429