@botpress/zai 2.4.1 → 2.5.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 CHANGED
@@ -41,18 +41,126 @@ declare abstract class Adapter {
41
41
  abstract saveExample<TInput, TOutput>(props: SaveExampleProps<TInput, TOutput>): Promise<void>;
42
42
  }
43
43
 
44
+ /**
45
+ * Active learning configuration for improving AI operations over time.
46
+ *
47
+ * When enabled, Zai stores successful operation results in a table and uses them as examples
48
+ * for future operations, improving accuracy and consistency.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const activeLearning = {
53
+ * enable: true,
54
+ * tableName: 'MyAppLearningTable',
55
+ * taskId: 'sentiment-analysis'
56
+ * }
57
+ * ```
58
+ */
44
59
  type ActiveLearning = {
60
+ /** Whether to enable active learning for this Zai instance */
45
61
  enable: boolean;
62
+ /** Name of the Botpress table to store learning examples (must end with 'Table') */
46
63
  tableName: string;
64
+ /** Unique identifier for this learning task */
47
65
  taskId: string;
48
66
  };
67
+ /**
68
+ * Configuration options for creating a Zai instance.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * import { Client } from '@botpress/client'
73
+ * import { Zai } from '@botpress/zai'
74
+ *
75
+ * const client = new Client({ token: 'your-token' })
76
+ * const config: ZaiConfig = {
77
+ * client,
78
+ * modelId: 'best', // Use the best available model
79
+ * userId: 'user-123',
80
+ * namespace: 'my-app',
81
+ * activeLearning: {
82
+ * enable: true,
83
+ * tableName: 'MyLearningTable',
84
+ * taskId: 'extraction'
85
+ * }
86
+ * }
87
+ * const zai = new Zai(config)
88
+ * ```
89
+ */
49
90
  type ZaiConfig = {
91
+ /** Botpress client or Cognitive client instance */
50
92
  client: BotpressClientLike | Cognitive;
93
+ /** Optional user ID for tracking and attribution */
51
94
  userId?: string;
95
+ /** Model to use: 'best' (default), 'fast', or specific model like 'openai:gpt-4' */
52
96
  modelId?: Models;
97
+ /** Active learning configuration to improve operations over time */
53
98
  activeLearning?: ActiveLearning;
99
+ /** Namespace for organizing tasks (default: 'zai') */
54
100
  namespace?: string;
55
101
  };
102
+ /**
103
+ * Zai - A type-safe LLM utility library for production-ready AI operations.
104
+ *
105
+ * Zai provides high-level abstractions for common AI tasks with built-in features like:
106
+ * - Active learning (learns from successful operations)
107
+ * - Automatic chunking for large inputs
108
+ * - Retry logic with error recovery
109
+ * - Usage tracking (tokens, cost, latency)
110
+ * - Type-safe schema validation with Zod
111
+ *
112
+ * @example Basic usage
113
+ * ```typescript
114
+ * import { Client } from '@botpress/client'
115
+ * import { Zai } from '@botpress/zai'
116
+ * import { z } from '@bpinternal/zui'
117
+ *
118
+ * const client = new Client({ token: process.env.BOTPRESS_TOKEN })
119
+ * const zai = new Zai({ client })
120
+ *
121
+ * // Extract structured data
122
+ * const schema = z.object({
123
+ * name: z.string(),
124
+ * age: z.number()
125
+ * })
126
+ * const person = await zai.extract('John is 30 years old', schema)
127
+ * // Output: { name: 'John', age: 30 }
128
+ *
129
+ * // Check conditions
130
+ * const isPositive = await zai.check('I love this product!', 'Is the sentiment positive?')
131
+ * // Output: true
132
+ *
133
+ * // Summarize text
134
+ * const summary = await zai.summarize(longDocument, { length: 100 })
135
+ * ```
136
+ *
137
+ * @example With active learning
138
+ * ```typescript
139
+ * const zai = new Zai({
140
+ * client,
141
+ * activeLearning: {
142
+ * enable: true,
143
+ * tableName: 'SentimentTable',
144
+ * taskId: 'product-reviews'
145
+ * }
146
+ * })
147
+ *
148
+ * // Enable learning for specific task
149
+ * const result = await zai.learn('sentiment').check(review, 'Is this positive?')
150
+ * // Future calls will use approved examples for better accuracy
151
+ * ```
152
+ *
153
+ * @example Chaining configuration
154
+ * ```typescript
155
+ * // Use fast model for quick operations
156
+ * const fastZai = zai.with({ modelId: 'fast' })
157
+ * await fastZai.check(text, 'Is this spam?')
158
+ *
159
+ * // Use specific model
160
+ * const gpt4Zai = zai.with({ modelId: 'openai:gpt-4' })
161
+ * await gpt4Zai.extract(document, complexSchema)
162
+ * ```
163
+ */
56
164
  declare class Zai {
57
165
  protected static tokenizer: TextTokenizer;
58
166
  protected client: Cognitive;
@@ -63,13 +171,119 @@ declare class Zai {
63
171
  protected namespace: string;
64
172
  protected adapter: Adapter;
65
173
  protected activeLearning: ActiveLearning;
174
+ /**
175
+ * Creates a new Zai instance with the specified configuration.
176
+ *
177
+ * @param config - Configuration object containing client, model, and learning settings
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * import { Client } from '@botpress/client'
182
+ * import { Zai } from '@botpress/zai'
183
+ *
184
+ * const client = new Client({ token: 'your-token' })
185
+ * const zai = new Zai({
186
+ * client,
187
+ * modelId: 'best',
188
+ * namespace: 'my-app',
189
+ * userId: 'user-123'
190
+ * })
191
+ * ```
192
+ *
193
+ * @throws {Error} If the configuration is invalid (e.g., invalid modelId format)
194
+ */
66
195
  constructor(config: ZaiConfig);
67
196
  /** @internal */
68
197
  protected callModel(props: Parameters<Cognitive['generateContent']>[0]): ReturnType<Cognitive['generateContent']>;
69
198
  protected getTokenizer(): Promise<TextTokenizer>;
70
199
  protected fetchModelDetails(): Promise<void>;
71
200
  protected get taskId(): string;
201
+ /**
202
+ * Creates a new Zai instance with merged configuration options.
203
+ *
204
+ * This method allows you to create variations of your Zai instance with different
205
+ * settings without modifying the original. Useful for switching models, namespaces,
206
+ * or other configuration on a per-operation basis.
207
+ *
208
+ * @param options - Partial configuration to override the current settings
209
+ * @returns A new Zai instance with the merged configuration
210
+ *
211
+ * @example Switch to a faster model
212
+ * ```typescript
213
+ * const zai = new Zai({ client })
214
+ *
215
+ * // Use fast model for simple operations
216
+ * const fastZai = zai.with({ modelId: 'fast' })
217
+ * await fastZai.check(text, 'Is this spam?')
218
+ *
219
+ * // Use best model for complex operations
220
+ * const bestZai = zai.with({ modelId: 'best' })
221
+ * await bestZai.extract(document, complexSchema)
222
+ * ```
223
+ *
224
+ * @example Change namespace
225
+ * ```typescript
226
+ * const customerZai = zai.with({ namespace: 'customer-support' })
227
+ * const salesZai = zai.with({ namespace: 'sales' })
228
+ * ```
229
+ *
230
+ * @example Use specific model
231
+ * ```typescript
232
+ * const gpt4 = zai.with({ modelId: 'openai:gpt-4' })
233
+ * const claude = zai.with({ modelId: 'anthropic:claude-3-5-sonnet-20241022' })
234
+ * ```
235
+ */
72
236
  with(options: Partial<ZaiConfig>): Zai;
237
+ /**
238
+ * Creates a new Zai instance with active learning enabled for a specific task.
239
+ *
240
+ * Active learning stores successful operation results and uses them as examples for
241
+ * future operations, improving accuracy and consistency over time. Each task ID
242
+ * maintains its own set of learned examples.
243
+ *
244
+ * @param taskId - Unique identifier for the learning task (alphanumeric, hyphens, underscores, slashes)
245
+ * @returns A new Zai instance with active learning enabled for the specified task
246
+ *
247
+ * @example Sentiment analysis with learning
248
+ * ```typescript
249
+ * const zai = new Zai({
250
+ * client,
251
+ * activeLearning: {
252
+ * enable: false,
253
+ * tableName: 'AppLearningTable',
254
+ * taskId: 'default'
255
+ * }
256
+ * })
257
+ *
258
+ * // Enable learning for sentiment analysis
259
+ * const sentimentZai = zai.learn('sentiment-analysis')
260
+ * const result = await sentimentZai.check(review, 'Is this review positive?')
261
+ *
262
+ * // Each successful call is stored and used to improve future calls
263
+ * ```
264
+ *
265
+ * @example Different tasks for different purposes
266
+ * ```typescript
267
+ * // Extract user info with learning
268
+ * const userExtractor = zai.learn('user-extraction')
269
+ * await userExtractor.extract(text, userSchema)
270
+ *
271
+ * // Extract product info with separate learning
272
+ * const productExtractor = zai.learn('product-extraction')
273
+ * await productExtractor.extract(text, productSchema)
274
+ *
275
+ * // Each task learns independently
276
+ * ```
277
+ *
278
+ * @example Combining with other configuration
279
+ * ```typescript
280
+ * // Use fast model + learning
281
+ * const fastLearner = zai.with({ modelId: 'fast' }).learn('quick-checks')
282
+ * await fastLearner.check(email, 'Is this spam?')
283
+ * ```
284
+ *
285
+ * @see {@link ZaiConfig.activeLearning} for configuration options
286
+ */
73
287
  learn(taskId: string): Zai;
74
288
  }
75
289
 
@@ -86,22 +300,54 @@ type ZaiContextProps = {
86
300
  adapter?: Adapter;
87
301
  source?: GenerateContentInput['meta'];
88
302
  };
303
+ /**
304
+ * Usage statistics tracking tokens, cost, and request metrics for an operation.
305
+ *
306
+ * This type is returned via Response events and the `.result()` method, providing
307
+ * real-time visibility into:
308
+ * - Token consumption (input/output/total)
309
+ * - Cost in USD (input/output/total)
310
+ * - Request statistics (count, errors, cache hits, progress percentage)
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * const { usage } = await zai.extract(text, schema).result()
315
+ *
316
+ * console.log(usage.tokens.total) // 1250
317
+ * console.log(usage.cost.total) // 0.0075 (USD)
318
+ * console.log(usage.requests.cached) // 0
319
+ * ```
320
+ */
89
321
  type Usage = {
322
+ /** Request statistics */
90
323
  requests: {
324
+ /** Total number of requests initiated */
91
325
  requests: number;
326
+ /** Number of requests that failed with errors */
92
327
  errors: number;
328
+ /** Number of successful responses received */
93
329
  responses: number;
330
+ /** Number of responses served from cache (no tokens used) */
94
331
  cached: number;
332
+ /** Operation progress as a decimal (0.0 to 1.0) */
95
333
  percentage: number;
96
334
  };
335
+ /** Cost statistics in USD */
97
336
  cost: {
337
+ /** Cost for input tokens */
98
338
  input: number;
339
+ /** Cost for output tokens */
99
340
  output: number;
341
+ /** Total cost (input + output) */
100
342
  total: number;
101
343
  };
344
+ /** Token usage statistics */
102
345
  tokens: {
346
+ /** Input tokens consumed */
103
347
  input: number;
348
+ /** Output tokens generated */
104
349
  output: number;
350
+ /** Total tokens (input + output) */
105
351
  total: number;
106
352
  };
107
353
  };
@@ -140,11 +386,109 @@ declare class ZaiContext {
140
386
  get usage(): Usage;
141
387
  }
142
388
 
389
+ /**
390
+ * Event types emitted during operation execution.
391
+ *
392
+ * @property progress - Emitted periodically with usage statistics (tokens, cost, requests)
393
+ * @property complete - Emitted when operation completes successfully with the result
394
+ * @property error - Emitted when operation fails with the error
395
+ */
143
396
  type ResponseEvents<TComplete = any> = {
397
+ /** Emitted during execution with updated usage statistics */
144
398
  progress: Usage;
399
+ /** Emitted when the operation completes with the full result */
145
400
  complete: TComplete;
401
+ /** Emitted when the operation fails with an error */
146
402
  error: unknown;
147
403
  };
404
+ /**
405
+ * Promise-like wrapper for Zai operations with observability and control.
406
+ *
407
+ * Response provides a dual-value system:
408
+ * - **Simplified value**: When awaited directly, returns a simplified result (e.g., boolean for `check()`)
409
+ * - **Full result**: Via `.result()` method, returns `{ output, usage, elapsed }`
410
+ *
411
+ * All Zai operations return a Response instance, allowing you to:
412
+ * - Track progress and usage in real-time
413
+ * - Abort operations
414
+ * - Bind to external abort signals
415
+ * - Get detailed cost and performance metrics
416
+ *
417
+ * @template T - The full output type
418
+ * @template S - The simplified output type (defaults to T)
419
+ *
420
+ * @example Basic usage (simplified)
421
+ * ```typescript
422
+ * // Simplified result (boolean)
423
+ * const isPositive = await zai.check(review, 'Is this positive?')
424
+ * console.log(isPositive) // true or false
425
+ * ```
426
+ *
427
+ * @example Full result with usage
428
+ * ```typescript
429
+ * const response = zai.check(review, 'Is this positive?')
430
+ * const { output, usage, elapsed } = await response.result()
431
+ *
432
+ * console.log(output.value) // true/false
433
+ * console.log(output.explanation) // "The review expresses satisfaction..."
434
+ * console.log(usage.tokens.total) // 150
435
+ * console.log(usage.cost.total) // 0.002
436
+ * console.log(elapsed) // 1234 (ms)
437
+ * ```
438
+ *
439
+ * @example Progress tracking
440
+ * ```typescript
441
+ * const response = zai.summarize(longDocument, { length: 500 })
442
+ *
443
+ * response.on('progress', (usage) => {
444
+ * console.log(`Progress: ${usage.requests.percentage * 100}%`)
445
+ * console.log(`Tokens: ${usage.tokens.total}`)
446
+ * console.log(`Cost: $${usage.cost.total}`)
447
+ * })
448
+ *
449
+ * const summary = await response
450
+ * ```
451
+ *
452
+ * @example Aborting operations
453
+ * ```typescript
454
+ * const response = zai.extract(hugeDocument, schema)
455
+ *
456
+ * // Abort after 5 seconds
457
+ * setTimeout(() => response.abort('Timeout'), 5000)
458
+ *
459
+ * try {
460
+ * const result = await response
461
+ * } catch (error) {
462
+ * console.log('Operation aborted:', error)
463
+ * }
464
+ * ```
465
+ *
466
+ * @example External abort signal
467
+ * ```typescript
468
+ * const controller = new AbortController()
469
+ * const response = zai.answer(documents, question).bindSignal(controller.signal)
470
+ *
471
+ * // User clicks cancel button
472
+ * cancelButton.onclick = () => controller.abort()
473
+ *
474
+ * const answer = await response
475
+ * ```
476
+ *
477
+ * @example Error handling
478
+ * ```typescript
479
+ * const response = zai.extract(text, schema)
480
+ *
481
+ * response.on('error', (error) => {
482
+ * console.error('Operation failed:', error)
483
+ * })
484
+ *
485
+ * try {
486
+ * const result = await response
487
+ * } catch (error) {
488
+ * // Handle error
489
+ * }
490
+ * ```
491
+ */
148
492
  declare class Response<T = any, S = T> implements PromiseLike<S> {
149
493
  private _promise;
150
494
  private _eventEmitter;
@@ -152,13 +496,179 @@ declare class Response<T = any, S = T> implements PromiseLike<S> {
152
496
  private _elasped;
153
497
  private _simplify;
154
498
  constructor(context: ZaiContext, promise: Promise<T>, simplify: (value: T) => S);
499
+ /**
500
+ * Subscribes to events emitted during operation execution.
501
+ *
502
+ * @param type - Event type: 'progress', 'complete', or 'error'
503
+ * @param listener - Callback function to handle the event
504
+ * @returns This Response instance for chaining
505
+ *
506
+ * @example Track progress
507
+ * ```typescript
508
+ * response.on('progress', (usage) => {
509
+ * console.log(`${usage.requests.percentage * 100}% complete`)
510
+ * console.log(`Cost: $${usage.cost.total}`)
511
+ * })
512
+ * ```
513
+ *
514
+ * @example Handle completion
515
+ * ```typescript
516
+ * response.on('complete', (result) => {
517
+ * console.log('Operation completed:', result)
518
+ * })
519
+ * ```
520
+ *
521
+ * @example Handle errors
522
+ * ```typescript
523
+ * response.on('error', (error) => {
524
+ * console.error('Operation failed:', error)
525
+ * })
526
+ * ```
527
+ */
155
528
  on<K extends keyof ResponseEvents<T>>(type: K, listener: (event: ResponseEvents<T>[K]) => void): this;
529
+ /**
530
+ * Unsubscribes from events.
531
+ *
532
+ * @param type - Event type to unsubscribe from
533
+ * @param listener - The exact listener function to remove
534
+ * @returns This Response instance for chaining
535
+ *
536
+ * @example
537
+ * ```typescript
538
+ * const progressHandler = (usage) => console.log(usage.tokens.total)
539
+ * response.on('progress', progressHandler)
540
+ * // Later...
541
+ * response.off('progress', progressHandler)
542
+ * ```
543
+ */
156
544
  off<K extends keyof ResponseEvents<T>>(type: K, listener: (event: ResponseEvents<T>[K]) => void): this;
545
+ /**
546
+ * Subscribes to an event for a single emission.
547
+ *
548
+ * The listener is automatically removed after being called once.
549
+ *
550
+ * @param type - Event type: 'progress', 'complete', or 'error'
551
+ * @param listener - Callback function to handle the event once
552
+ * @returns This Response instance for chaining
553
+ *
554
+ * @example
555
+ * ```typescript
556
+ * response.once('complete', (result) => {
557
+ * console.log('Finished:', result)
558
+ * })
559
+ * ```
560
+ */
157
561
  once<K extends keyof ResponseEvents<T>>(type: K, listener: (event: ResponseEvents<T>[K]) => void): this;
562
+ /**
563
+ * Binds an external AbortSignal to this operation.
564
+ *
565
+ * When the signal is aborted, the operation will be cancelled automatically.
566
+ * Useful for integrating with UI cancel buttons or request timeouts.
567
+ *
568
+ * @param signal - AbortSignal to bind
569
+ * @returns This Response instance for chaining
570
+ *
571
+ * @example With AbortController
572
+ * ```typescript
573
+ * const controller = new AbortController()
574
+ * const response = zai.extract(data, schema).bindSignal(controller.signal)
575
+ *
576
+ * // Cancel from elsewhere
577
+ * cancelButton.onclick = () => controller.abort()
578
+ * ```
579
+ *
580
+ * @example With timeout
581
+ * ```typescript
582
+ * const controller = new AbortController()
583
+ * setTimeout(() => controller.abort('Timeout'), 10000)
584
+ *
585
+ * const response = zai.answer(docs, question).bindSignal(controller.signal)
586
+ * ```
587
+ */
158
588
  bindSignal(signal: AbortSignal): this;
589
+ /**
590
+ * Aborts the operation in progress.
591
+ *
592
+ * The operation will be cancelled and throw an abort error.
593
+ * Any partial results will not be returned.
594
+ *
595
+ * @param reason - Optional reason for aborting (string or Error)
596
+ *
597
+ * @example
598
+ * ```typescript
599
+ * const response = zai.extract(largeDocument, schema)
600
+ *
601
+ * // Abort after 5 seconds
602
+ * setTimeout(() => response.abort('Operation timeout'), 5000)
603
+ *
604
+ * try {
605
+ * await response
606
+ * } catch (error) {
607
+ * console.log('Aborted:', error)
608
+ * }
609
+ * ```
610
+ */
159
611
  abort(reason?: string | Error): void;
612
+ /**
613
+ * Promise interface - allows awaiting the Response.
614
+ *
615
+ * When awaited, returns the simplified value (S).
616
+ * Use `.result()` for full output with usage statistics.
617
+ *
618
+ * @param onfulfilled - Success handler
619
+ * @param onrejected - Error handler
620
+ * @returns Promise resolving to simplified value
621
+ *
622
+ * @example
623
+ * ```typescript
624
+ * // Simplified value
625
+ * const isPositive = await zai.check(review, 'Is positive?')
626
+ * console.log(isPositive) // true
627
+ * ```
628
+ */
160
629
  then<TResult1 = S, TResult2 = never>(onfulfilled?: ((value: S) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
630
+ /**
631
+ * Promise interface - handles errors.
632
+ *
633
+ * @param onrejected - Error handler
634
+ * @returns Promise resolving to simplified value or error result
635
+ */
161
636
  catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): PromiseLike<S | TResult>;
637
+ /**
638
+ * Gets the full result with detailed usage statistics and timing.
639
+ *
640
+ * Unlike awaiting the Response directly (which returns simplified value),
641
+ * this method provides:
642
+ * - `output`: Full operation result (not simplified)
643
+ * - `usage`: Detailed token usage, cost, and request statistics
644
+ * - `elapsed`: Operation duration in milliseconds
645
+ *
646
+ * @returns Promise resolving to full result object
647
+ *
648
+ * @example
649
+ * ```typescript
650
+ * const { output, usage, elapsed } = await zai.check(text, condition).result()
651
+ *
652
+ * console.log(output.value) // true/false
653
+ * console.log(output.explanation) // "The text expresses..."
654
+ * console.log(usage.tokens.total) // 245
655
+ * console.log(usage.cost.total) // 0.0012
656
+ * console.log(elapsed) // 1523 (ms)
657
+ * ```
658
+ *
659
+ * @example Usage statistics breakdown
660
+ * ```typescript
661
+ * const { usage } = await response.result()
662
+ *
663
+ * console.log('Requests:', usage.requests.requests)
664
+ * console.log('Cached:', usage.requests.cached)
665
+ * console.log('Input tokens:', usage.tokens.input)
666
+ * console.log('Output tokens:', usage.tokens.output)
667
+ * console.log('Input cost:', usage.cost.input)
668
+ * console.log('Output cost:', usage.cost.output)
669
+ * console.log('Total cost:', usage.cost.total)
670
+ * ```
671
+ */
162
672
  result(): Promise<{
163
673
  output: T;
164
674
  usage: Usage;
@@ -166,14 +676,63 @@ declare class Response<T = any, S = T> implements PromiseLike<S> {
166
676
  }>;
167
677
  }
168
678
 
169
- type Options$a = {
679
+ type Options$b = {
170
680
  /** The maximum number of tokens to generate */
171
681
  length?: number;
172
682
  };
173
683
  declare module '@botpress/zai' {
174
684
  interface Zai {
175
- /** Generates a text of the desired length according to the prompt */
176
- text(prompt: string, options?: Options$a): Response<string>;
685
+ /**
686
+ * Generates text content based on a natural language prompt.
687
+ *
688
+ * This operation creates original text content using LLMs with optional length constraints.
689
+ * Perfect for generating descriptions, emails, articles, creative content, and more.
690
+ *
691
+ * @param prompt - Natural language description of what text to generate
692
+ * @param options - Optional configuration for text length
693
+ * @returns Response promise resolving to the generated text
694
+ *
695
+ * @example Product description
696
+ * ```typescript
697
+ * const description = await zai.text(
698
+ * 'Write a compelling product description for eco-friendly bamboo toothbrushes'
699
+ * )
700
+ * ```
701
+ *
702
+ * @example With length constraint
703
+ * ```typescript
704
+ * const tagline = await zai.text(
705
+ * 'Create a catchy tagline for a fitness app',
706
+ * { length: 10 } // ~10 tokens (7-8 words)
707
+ * )
708
+ * ```
709
+ *
710
+ * @example Email generation
711
+ * ```typescript
712
+ * const email = await zai.text(`
713
+ * Write a professional email to a customer explaining
714
+ * that their order will be delayed by 2 days due to weather.
715
+ * Apologize and offer a 10% discount on their next purchase.
716
+ * `, { length: 150 })
717
+ * ```
718
+ *
719
+ * @example Blog post
720
+ * ```typescript
721
+ * const blogPost = await zai.text(`
722
+ * Write an informative blog post about the benefits of meditation
723
+ * for software developers. Include practical tips and scientific research.
724
+ * `, { length: 500 })
725
+ * ```
726
+ *
727
+ * @example Social media content
728
+ * ```typescript
729
+ * const tweet = await zai.text(
730
+ * 'Write an engaging tweet announcing our new AI-powered chatbot feature',
731
+ * { length: 30 } // Twitter-friendly length
732
+ * )
733
+ * ```
734
+ */
735
+ text(prompt: string, options?: Options$b): Response<string>;
177
736
  }
178
737
  }
179
738
 
@@ -182,7 +741,7 @@ type Example$3 = {
182
741
  output: string;
183
742
  instructions?: string;
184
743
  };
185
- type Options$9 = {
744
+ type Options$a = {
186
745
  /** Examples to guide the rewriting */
187
746
  examples?: Array<Example$3>;
188
747
  /** The maximum number of tokens to generate */
@@ -190,12 +749,95 @@ type Options$9 = {
190
749
  };
191
750
  declare module '@botpress/zai' {
192
751
  interface Zai {
193
- /** Rewrites a string according to match the prompt */
194
- rewrite(original: string, prompt: string, options?: Options$9): Response<string>;
752
+ /**
753
+ * Rewrites text according to specific instructions while preserving the core meaning.
754
+ *
755
+ * This operation transforms text based on natural language instructions. Perfect for
756
+ * tone adjustment, format conversion, translation, simplification, and style changes.
757
+ *
758
+ * @param original - The text to rewrite
759
+ * @param prompt - Instructions describing how to transform the text
760
+ * @param options - Configuration for examples and length constraints
761
+ * @returns Response promise resolving to the rewritten text
762
+ *
763
+ * @example Change tone
764
+ * ```typescript
765
+ * const original = "Your request has been denied due to insufficient funds."
766
+ * const friendly = await zai.rewrite(
767
+ * original,
768
+ * 'Make this sound more friendly and empathetic'
769
+ * )
770
+ * // Result: "We appreciate your interest, but unfortunately we're unable to proceed..."
771
+ * ```
772
+ *
773
+ * @example Simplify technical content
774
+ * ```typescript
775
+ * const technical = "The API implements RESTful architecture with OAuth 2.0 authentication..."
776
+ * const simple = await zai.rewrite(
777
+ * technical,
778
+ * 'Explain this in simple terms for non-technical users'
779
+ * )
780
+ * ```
781
+ *
782
+ * @example Professional email conversion
783
+ * ```typescript
784
+ * const casual = "Hey, can you send me that report? Thanks!"
785
+ * const professional = await zai.rewrite(
786
+ * casual,
787
+ * 'Rewrite this as a formal business email'
788
+ * )
789
+ * // Result: "Dear colleague, I would appreciate it if you could share the report..."
790
+ * ```
791
+ *
792
+ * @example Format conversion
793
+ * ```typescript
794
+ * const paragraph = "First do this. Then do that. Finally do this other thing."
795
+ * const bullets = await zai.rewrite(
796
+ * paragraph,
797
+ * 'Convert this to a bulleted list'
798
+ * )
799
+ * // Result:
800
+ * // - First do this
801
+ * // - Then do that
802
+ * // - Finally do this other thing
803
+ * ```
804
+ *
805
+ * @example With examples for consistent style
806
+ * ```typescript
807
+ * const result = await zai.rewrite(
808
+ * original,
809
+ * 'Rewrite in our brand voice',
810
+ * {
811
+ * examples: [
812
+ * {
813
+ * input: 'We offer many products.',
814
+ * output: 'Discover our curated collection of innovative solutions.'
815
+ * },
816
+ * {
817
+ * input: 'Contact us for help.',
818
+ * output: "We're here to support your success. Let's connect!"
819
+ * }
820
+ * ],
821
+ * length: 200 // Limit output length
822
+ * }
823
+ * )
824
+ * ```
825
+ *
826
+ * @example Translation-like transformation
827
+ * ```typescript
828
+ * const code = "if (user.isActive && user.hasPermission) { allowAccess() }"
829
+ * const pseudocode = await zai.rewrite(
830
+ * code,
831
+ * 'Convert this code to natural language pseudocode'
832
+ * )
833
+ * // Result: "If the user is active AND has permission, then allow access"
834
+ * ```
835
+ */
836
+ rewrite(original: string, prompt: string, options?: Options$a): Response<string>;
195
837
  }
196
838
  }
197
839
 
198
- type Options$8 = {
840
+ type Options$9 = {
199
841
  /** What should the text be summarized to? */
200
842
  prompt?: string;
201
843
  /** How to format the example text */
@@ -214,8 +856,81 @@ type Options$8 = {
214
856
  };
215
857
  declare module '@botpress/zai' {
216
858
  interface Zai {
217
- /** Summarizes a text of any length to a summary of the desired length */
218
- summarize(original: string, options?: Options$8): Response<string>;
859
+ /**
860
+ * Summarizes text of any length to a target length using intelligent chunking strategies.
861
+ *
862
+ * This operation can handle documents from a few paragraphs to entire books. It uses
863
+ * two strategies based on document size:
864
+ * - **Sliding window**: For moderate documents, processes overlapping chunks iteratively
865
+ * - **Merge sort**: For very large documents, recursively summarizes and merges
866
+ *
867
+ * @param original - The text to summarize
868
+ * @param options - Configuration for length, focus, format, and chunking strategy
869
+ * @returns Response promise resolving to the summary text
870
+ *
871
+ * @example Basic summarization
872
+ * ```typescript
873
+ * const article = "Long article text here..."
874
+ * const summary = await zai.summarize(article, {
875
+ * length: 100 // Target 100 tokens (~75 words)
876
+ * })
877
+ * ```
878
+ *
879
+ * @example Custom focus and format
880
+ * ```typescript
881
+ * const meetingNotes = "... detailed meeting transcript ..."
882
+ * const summary = await zai.summarize(meetingNotes, {
883
+ * length: 200,
884
+ * prompt: 'Key decisions, action items, and next steps',
885
+ * format: 'Bullet points with clear sections for Decisions, Actions, and Next Steps'
886
+ * })
887
+ * ```
888
+ *
889
+ * @example Summarizing very large documents
890
+ * ```typescript
891
+ * const book = await readFile('large-book.txt', 'utf-8') // 100k+ tokens
892
+ * const summary = await zai.summarize(book, {
893
+ * length: 500,
894
+ * intermediateFactor: 4, // Intermediate summaries can be 4x target length
895
+ * prompt: 'Main themes, key events, and character development'
896
+ * })
897
+ * // Automatically uses merge-sort strategy for efficiency
898
+ * ```
899
+ *
900
+ * @example Technical documentation summary
901
+ * ```typescript
902
+ * const docs = "... API documentation ..."
903
+ * const summary = await zai.summarize(docs, {
904
+ * length: 300,
905
+ * prompt: 'Core API endpoints, authentication methods, and rate limits',
906
+ * format: 'Structured markdown with code examples where relevant'
907
+ * })
908
+ * ```
909
+ *
910
+ * @example Adjusting chunking strategy
911
+ * ```typescript
912
+ * const summary = await zai.summarize(document, {
913
+ * length: 150,
914
+ * sliding: {
915
+ * window: 30000, // Process 30k tokens at a time
916
+ * overlap: 500 // 500 token overlap between windows
917
+ * }
918
+ * })
919
+ * ```
920
+ *
921
+ * @example Progress tracking for long documents
922
+ * ```typescript
923
+ * const response = zai.summarize(veryLongDocument, { length: 400 })
924
+ *
925
+ * response.on('progress', (usage) => {
926
+ * console.log(`Progress: ${Math.round(usage.requests.percentage * 100)}%`)
927
+ * console.log(`Tokens used: ${usage.tokens.total}`)
928
+ * })
929
+ *
930
+ * const summary = await response
931
+ * ```
932
+ */
933
+ summarize(original: string, options?: Options$9): Response<string>;
219
934
  }
220
935
  }
221
936
 
@@ -225,14 +940,88 @@ type Example$2 = {
225
940
  reason?: string;
226
941
  condition?: string;
227
942
  };
228
- type Options$7 = {
943
+ type Options$8 = {
229
944
  /** Examples to check the condition against */
230
945
  examples?: Array<Example$2>;
231
946
  };
232
947
  declare module '@botpress/zai' {
233
948
  interface Zai {
234
- /** Checks wether a condition is true or not */
235
- check(input: unknown, condition: string, options?: Options$7): Response<{
949
+ /**
950
+ * Checks whether a condition is true for the given input, with an explanation.
951
+ *
952
+ * This operation evaluates natural language conditions against your input data,
953
+ * returning both a boolean result and a detailed explanation. Perfect for
954
+ * content moderation, sentiment analysis, quality checks, and business rule validation.
955
+ *
956
+ * @param input - The data to evaluate (text, object, or any value)
957
+ * @param condition - Natural language description of the condition to check
958
+ * @param options - Optional examples to guide the evaluation
959
+ * @returns Response with { value: boolean, explanation: string }, simplified to boolean when awaited
960
+ *
961
+ * @example Basic sentiment check
962
+ * ```typescript
963
+ * const review = "This product exceeded my expectations!"
964
+ * const isPositive = await zai.check(review, 'Is the sentiment positive?')
965
+ * // Result: true
966
+ *
967
+ * // Get full details
968
+ * const { value, explanation } = await zai.check(review, 'Is the sentiment positive?').result()
969
+ * // value: true
970
+ * // explanation: "The review expresses satisfaction and exceeded expectations..."
971
+ * ```
972
+ *
973
+ * @example Content moderation
974
+ * ```typescript
975
+ * const comment = "Great article! Very informative."
976
+ * const isSpam = await zai.check(comment, 'Is this spam or promotional content?')
977
+ * // Result: false
978
+ *
979
+ * const hasProfanity = await zai.check(comment, 'Does this contain profanity or offensive language?')
980
+ * // Result: false
981
+ * ```
982
+ *
983
+ * @example Business rules validation
984
+ * ```typescript
985
+ * const invoice = {
986
+ * total: 1500,
987
+ * items: ['laptop', 'mouse'],
988
+ * customer: 'Enterprise Corp'
989
+ * }
990
+ *
991
+ * const needsApproval = await zai.check(
992
+ * invoice,
993
+ * 'Does this invoice require manager approval? (over $1000 or enterprise customer)'
994
+ * )
995
+ * // Result: true
996
+ * ```
997
+ *
998
+ * @example With examples for consistency
999
+ * ```typescript
1000
+ * const result = await zai.check(text, 'Is this a technical question?', {
1001
+ * examples: [
1002
+ * {
1003
+ * input: 'How do I deploy to production?',
1004
+ * check: true,
1005
+ * reason: 'Question about deployment process'
1006
+ * },
1007
+ * {
1008
+ * input: 'What time is the meeting?',
1009
+ * check: false,
1010
+ * reason: 'Not a technical question'
1011
+ * }
1012
+ * ]
1013
+ * })
1014
+ * ```
1015
+ *
1016
+ * @example Quality assurance
1017
+ * ```typescript
1018
+ * const code = "function add(a, b) { return a + b }"
1019
+ * const hasDocumentation = await zai.check(code, 'Is this code properly documented?')
1020
+ * const hasTests = await zai.check(code, 'Does this include unit tests?')
1021
+ * const followsConventions = await zai.check(code, 'Does this follow naming conventions?')
1022
+ * ```
1023
+ */
1024
+ check(input: unknown, condition: string, options?: Options$8): Response<{
236
1025
  /** Whether the condition is true or not */
237
1026
  value: boolean;
238
1027
  /** The explanation of the decision */
@@ -246,7 +1035,7 @@ type Example$1 = {
246
1035
  filter: boolean;
247
1036
  reason?: string;
248
1037
  };
249
- type Options$6 = {
1038
+ type Options$7 = {
250
1039
  /** The maximum number of tokens per item */
251
1040
  tokensPerItem?: number;
252
1041
  /** Examples to filter the condition against */
@@ -254,12 +1043,97 @@ type Options$6 = {
254
1043
  };
255
1044
  declare module '@botpress/zai' {
256
1045
  interface Zai {
257
- /** Filters elements of an array against a condition */
258
- filter<T>(input: Array<T>, condition: string, options?: Options$6): Response<Array<T>>;
1046
+ /**
1047
+ * Filters array elements based on a natural language condition.
1048
+ *
1049
+ * This operation evaluates each element against a condition using LLMs,
1050
+ * returning only elements that match. Handles large arrays automatically
1051
+ * by processing in parallel chunks.
1052
+ *
1053
+ * @param input - Array of elements to filter
1054
+ * @param condition - Natural language description of what to keep
1055
+ * @param options - Configuration for token limits per item and examples
1056
+ * @returns Response promise resolving to filtered array
1057
+ *
1058
+ * @example Filter positive reviews
1059
+ * ```typescript
1060
+ * const reviews = [
1061
+ * "Great product, love it!",
1062
+ * "Terrible quality, broke immediately",
1063
+ * "Amazing! Exceeded expectations",
1064
+ * "Worst purchase ever"
1065
+ * ]
1066
+ *
1067
+ * const positive = await zai.filter(reviews, 'Keep only positive reviews')
1068
+ * // Result: ["Great product, love it!", "Amazing! Exceeded expectations"]
1069
+ * ```
1070
+ *
1071
+ * @example Filter technical questions
1072
+ * ```typescript
1073
+ * const questions = [
1074
+ * "How do I deploy to production?",
1075
+ * "What time is lunch?",
1076
+ * "Why is the API returning 500 errors?",
1077
+ * "Can you book the conference room?"
1078
+ * ]
1079
+ *
1080
+ * const technical = await zai.filter(
1081
+ * questions,
1082
+ * 'Keep only technical or engineering questions'
1083
+ * )
1084
+ * // Result: ["How do I deploy to production?", "Why is the API returning 500 errors?"]
1085
+ * ```
1086
+ *
1087
+ * @example Filter with object array
1088
+ * ```typescript
1089
+ * const products = [
1090
+ * { name: 'Laptop', category: 'Electronics', inStock: true },
1091
+ * { name: 'Desk', category: 'Furniture', inStock: false },
1092
+ * { name: 'Mouse', category: 'Electronics', inStock: true },
1093
+ * { name: 'Chair', category: 'Furniture', inStock: true }
1094
+ * ]
1095
+ *
1096
+ * const available = await zai.filter(
1097
+ * products,
1098
+ * 'Keep only products that are in stock'
1099
+ * )
1100
+ * // Returns products where inStock === true
1101
+ * ```
1102
+ *
1103
+ * @example Complex filtering logic
1104
+ * ```typescript
1105
+ * const emails = [...] // Array of email objects
1106
+ *
1107
+ * const urgent = await zai.filter(
1108
+ * emails,
1109
+ * 'Keep emails that are urgent, from the CEO, or mention "critical bug"'
1110
+ * )
1111
+ * ```
1112
+ *
1113
+ * @example With examples for consistency
1114
+ * ```typescript
1115
+ * const filtered = await zai.filter(items, 'Keep valid entries', {
1116
+ * examples: [
1117
+ * {
1118
+ * input: { status: 'active', verified: true },
1119
+ * filter: true,
1120
+ * reason: 'Active and verified'
1121
+ * },
1122
+ * {
1123
+ * input: { status: 'pending', verified: false },
1124
+ * filter: false,
1125
+ * reason: 'Not yet verified'
1126
+ * }
1127
+ * ],
1128
+ * tokensPerItem: 100 // Limit tokens per item for performance
1129
+ * })
1130
+ * ```
1131
+ */
1132
+ filter<T>(input: Array<T>, condition: string, options?: Options$7): Response<Array<T>>;
259
1133
  }
260
1134
  }
261
1135
 
262
- type Options$5 = {
1136
+ type Options$6 = {
263
1137
  /** Instructions to guide the user on how to extract the data */
264
1138
  instructions?: string;
265
1139
  /** The maximum number of tokens per chunk */
@@ -273,8 +1147,74 @@ type __Z<T extends any = any> = {
273
1147
  type OfType<O, T extends __Z = __Z<O>> = T extends __Z<O> ? T : never;
274
1148
  declare module '@botpress/zai' {
275
1149
  interface Zai {
276
- /** Extracts one or many elements from an arbitrary input */
277
- extract<S extends OfType<any>>(input: unknown, schema: S, options?: Options$5): Response<S['_output']>;
1150
+ /**
1151
+ * Extracts structured data from unstructured text using a Zod schema.
1152
+ *
1153
+ * This operation uses LLMs to intelligently parse text and extract information
1154
+ * according to your schema. It handles large inputs automatically by chunking
1155
+ * and supports both objects and arrays.
1156
+ *
1157
+ * @param input - The text or data to extract information from
1158
+ * @param schema - Zod schema defining the structure to extract
1159
+ * @param options - Optional configuration for extraction behavior
1160
+ * @returns A Response promise that resolves to data matching your schema
1161
+ *
1162
+ * @example Extract a single object
1163
+ * ```typescript
1164
+ * import { z } from '@bpinternal/zui'
1165
+ *
1166
+ * const personSchema = z.object({
1167
+ * name: z.string(),
1168
+ * age: z.number(),
1169
+ * email: z.string().email()
1170
+ * })
1171
+ *
1172
+ * const text = "Contact John Doe (35) at john@example.com"
1173
+ * const person = await zai.extract(text, personSchema)
1174
+ * // Result: { name: 'John Doe', age: 35, email: 'john@example.com' }
1175
+ * ```
1176
+ *
1177
+ * @example Extract an array of items
1178
+ * ```typescript
1179
+ * const productSchema = z.array(z.object({
1180
+ * name: z.string(),
1181
+ * price: z.number()
1182
+ * }))
1183
+ *
1184
+ * const text = "We have Apple ($1.50), Banana ($0.80), and Orange ($1.20)"
1185
+ * const products = await zai.extract(text, productSchema)
1186
+ * // Result: [
1187
+ * // { name: 'Apple', price: 1.50 },
1188
+ * // { name: 'Banana', price: 0.80 },
1189
+ * // { name: 'Orange', price: 1.20 }
1190
+ * // ]
1191
+ * ```
1192
+ *
1193
+ * @example With custom instructions
1194
+ * ```typescript
1195
+ * const result = await zai.extract(document, schema, {
1196
+ * instructions: 'Only extract confirmed information, skip uncertain data',
1197
+ * chunkLength: 10000, // Smaller chunks for better accuracy
1198
+ * strict: true // Enforce strict schema validation
1199
+ * })
1200
+ * ```
1201
+ *
1202
+ * @example Track usage and cost
1203
+ * ```typescript
1204
+ * const response = zai.extract(text, schema)
1205
+ *
1206
+ * // Monitor progress
1207
+ * response.on('progress', (usage) => {
1208
+ * console.log(`Tokens used: ${usage.tokens.total}`)
1209
+ * console.log(`Cost so far: $${usage.cost.total}`)
1210
+ * })
1211
+ *
1212
+ * // Get full results
1213
+ * const { output, usage, elapsed } = await response.result()
1214
+ * console.log(`Extraction took ${elapsed}ms and cost $${usage.cost.total}`)
1215
+ * ```
1216
+ */
1217
+ extract<S extends OfType<any>>(input: unknown, schema: S, options?: Options$6): Response<S['_output']>;
278
1218
  }
279
1219
  }
280
1220
 
@@ -293,7 +1233,7 @@ type Example<T extends string> = {
293
1233
  explanation?: string;
294
1234
  }>>;
295
1235
  };
296
- type Options$4<T extends string> = {
1236
+ type Options$5<T extends string> = {
297
1237
  /** Examples to help the user make a decision */
298
1238
  examples?: Array<Example<T>>;
299
1239
  /** Instructions to guide the user on how to extract the data */
@@ -304,8 +1244,126 @@ type Options$4<T extends string> = {
304
1244
  type Labels<T extends string> = Record<T, string>;
305
1245
  declare module '@botpress/zai' {
306
1246
  interface Zai {
307
- /** Tags the provided input with a list of predefined labels */
308
- label<T extends string>(input: unknown, labels: Labels<T>, options?: Options$4<T>): Response<{
1247
+ /**
1248
+ * Tags input with multiple labels, each with explanation, value, and confidence level.
1249
+ *
1250
+ * This operation evaluates input against multiple categories simultaneously, providing
1251
+ * nuanced confidence levels (ABSOLUTELY_YES, PROBABLY_YES, AMBIGUOUS, PROBABLY_NOT, ABSOLUTELY_NOT).
1252
+ * Perfect for content classification, intent detection, and multi-criteria evaluation.
1253
+ *
1254
+ * @param input - The data to label (text, object, or any value)
1255
+ * @param labels - Object mapping label keys to their descriptions
1256
+ * @param options - Configuration for examples, instructions, and chunking
1257
+ * @returns Response with detailed results per label (explanation, value, confidence), simplified to booleans
1258
+ *
1259
+ * @example Content moderation
1260
+ * ```typescript
1261
+ * const comment = "This is a great article! Click here for cheap products!"
1262
+ *
1263
+ * const result = await zai.label(comment, {
1264
+ * spam: 'Is this spam or promotional content?',
1265
+ * helpful: 'Is this comment helpful and constructive?',
1266
+ * profanity: 'Does this contain profanity or offensive language?'
1267
+ * }).result()
1268
+ *
1269
+ * // result.output:
1270
+ * // {
1271
+ * // spam: { value: true, confidence: 0.5, explanation: 'Contains promotional link...' },
1272
+ * // helpful: { value: true, confidence: 1, explanation: 'Positive feedback...' },
1273
+ * // profanity: { value: false, confidence: 1, explanation: 'No offensive language' }
1274
+ * // }
1275
+ *
1276
+ * // Simplified (when awaited directly):
1277
+ * const simple = await zai.label(comment, { spam: 'Is this spam?' })
1278
+ * // simple: { spam: true }
1279
+ * ```
1280
+ *
1281
+ * @example Intent classification
1282
+ * ```typescript
1283
+ * const message = "I can't log in to my account"
1284
+ *
1285
+ * const intents = await zai.label(message, {
1286
+ * technical_issue: 'Is this a technical problem?',
1287
+ * urgent: 'Does this require immediate attention?',
1288
+ * needs_human: 'Should this be escalated to a human agent?',
1289
+ * billing_related: 'Is this about billing or payments?'
1290
+ * })
1291
+ * // Returns boolean for each intent
1292
+ * ```
1293
+ *
1294
+ * @example Sentiment analysis with nuance
1295
+ * ```typescript
1296
+ * const review = "The product is okay, but shipping was slow"
1297
+ *
1298
+ * const { output } = await zai.label(review, {
1299
+ * positive: 'Is the overall sentiment positive?',
1300
+ * negative: 'Is the overall sentiment negative?',
1301
+ * mixed: 'Does this express mixed feelings?'
1302
+ * }).result()
1303
+ *
1304
+ * // Check confidence levels
1305
+ * if (output.mixed.confidence > 0.5) {
1306
+ * console.log('Mixed sentiment detected:', output.mixed.explanation)
1307
+ * }
1308
+ * ```
1309
+ *
1310
+ * @example Product categorization
1311
+ * ```typescript
1312
+ * const product = {
1313
+ * name: 'Wireless Headphones',
1314
+ * description: 'Noise-canceling Bluetooth headphones for music lovers',
1315
+ * price: 199
1316
+ * }
1317
+ *
1318
+ * const categories = await zai.label(product, {
1319
+ * electronics: 'Is this an electronic device?',
1320
+ * audio: 'Is this audio equipment?',
1321
+ * premium: 'Is this a premium/luxury product?',
1322
+ * portable: 'Is this portable/mobile?'
1323
+ * })
1324
+ * // Returns: { electronics: true, audio: true, premium: true, portable: true }
1325
+ * ```
1326
+ *
1327
+ * @example With examples for consistency
1328
+ * ```typescript
1329
+ * const result = await zai.label(email, {
1330
+ * urgent: 'Requires immediate response',
1331
+ * complaint: 'Customer is dissatisfied'
1332
+ * }, {
1333
+ * examples: [
1334
+ * {
1335
+ * input: 'ASAP! System is down!',
1336
+ * labels: {
1337
+ * urgent: { label: 'ABSOLUTELY_YES', explanation: 'Critical issue' },
1338
+ * complaint: { label: 'PROBABLY_YES', explanation: 'Implicit complaint' }
1339
+ * }
1340
+ * }
1341
+ * ],
1342
+ * instructions: 'Consider tone, urgency keywords, and context'
1343
+ * })
1344
+ * ```
1345
+ *
1346
+ * @example Understanding confidence levels
1347
+ * ```typescript
1348
+ * const { output } = await zai.label(text, labels).result()
1349
+ *
1350
+ * // Confidence values:
1351
+ * // ABSOLUTELY_YES: confidence = 1.0, value = true
1352
+ * // PROBABLY_YES: confidence = 0.5, value = true
1353
+ * // AMBIGUOUS: confidence = 0, value = false
1354
+ * // PROBABLY_NOT: confidence = 0.5, value = false
1355
+ * // ABSOLUTELY_NOT: confidence = 1.0, value = false
1356
+ *
1357
+ * Object.entries(output).forEach(([key, result]) => {
1358
+ * if (result.value && result.confidence === 1) {
1359
+ * console.log(`Definitely ${key}:`, result.explanation)
1360
+ * } else if (result.value && result.confidence === 0.5) {
1361
+ * console.log(`Probably ${key}:`, result.explanation)
1362
+ * }
1363
+ * })
1364
+ * ```
1365
+ */
1366
+ label<T extends string>(input: unknown, labels: Labels<T>, options?: Options$5<T>): Response<{
309
1367
  [K in T]: {
310
1368
  explanation: string;
311
1369
  value: boolean;
@@ -327,7 +1385,7 @@ type InitialGroup = {
327
1385
  label: string;
328
1386
  elements?: unknown[];
329
1387
  };
330
- type Options$3 = {
1388
+ type Options$4 = {
331
1389
  instructions?: string;
332
1390
  tokensPerElement?: number;
333
1391
  chunkLength?: number;
@@ -335,12 +1393,162 @@ type Options$3 = {
335
1393
  };
336
1394
  declare module '@botpress/zai' {
337
1395
  interface Zai {
338
- group<T>(input: Array<T>, options?: Options$3): Response<Array<Group<T>>, Record<string, T[]>>;
1396
+ /**
1397
+ * Groups array items into categories based on semantic similarity or criteria.
1398
+ *
1399
+ * This operation intelligently categorizes items by analyzing their content and
1400
+ * creating meaningful groups. It can discover natural groupings automatically or
1401
+ * use predefined categories. Perfect for clustering, classification, and organization.
1402
+ *
1403
+ * @param input - Array of items to group
1404
+ * @param options - Configuration for grouping behavior, instructions, and initial categories
1405
+ * @returns Response with groups array (simplified to Record<groupLabel, items[]>)
1406
+ *
1407
+ * @example Automatic grouping
1408
+ * ```typescript
1409
+ * const messages = [
1410
+ * "I can't log in to my account",
1411
+ * "How do I reset my password?",
1412
+ * "When will my order arrive?",
1413
+ * "The app keeps crashing",
1414
+ * "I haven't received my package",
1415
+ * "Error 500 on checkout"
1416
+ * ]
1417
+ *
1418
+ * const groups = await zai.group(messages, {
1419
+ * instructions: 'Group by type of customer issue'
1420
+ * })
1421
+ * // Result (simplified):
1422
+ * // {
1423
+ * // "Login Issues": ["I can't log in...", "How do I reset..."],
1424
+ * // "Shipping Questions": ["When will my order...", "I haven't received..."],
1425
+ * // "Technical Errors": ["The app keeps crashing", "Error 500..."]
1426
+ * // }
1427
+ *
1428
+ * // Full result:
1429
+ * const { output } = await zai.group(messages, { instructions: '...' }).result()
1430
+ * // output: [
1431
+ * // { id: 'login_issues', label: 'Login Issues', elements: [...] },
1432
+ * // { id: 'shipping', label: 'Shipping Questions', elements: [...] },
1433
+ * // { id: 'errors', label: 'Technical Errors', elements: [...] }
1434
+ * // ]
1435
+ * ```
1436
+ *
1437
+ * @example With predefined categories
1438
+ * ```typescript
1439
+ * const articles = [
1440
+ * "How to build a React app",
1441
+ * "Python machine learning tutorial",
1442
+ * "Understanding Docker containers",
1443
+ * "Vue.js best practices",
1444
+ * "Deep learning with TensorFlow"
1445
+ * ]
1446
+ *
1447
+ * const groups = await zai.group(articles, {
1448
+ * instructions: 'Categorize by technology',
1449
+ * initialGroups: [
1450
+ * { id: 'frontend', label: 'Frontend Development' },
1451
+ * { id: 'backend', label: 'Backend Development' },
1452
+ * { id: 'ml', label: 'Machine Learning' },
1453
+ * { id: 'devops', label: 'DevOps & Infrastructure' }
1454
+ * ]
1455
+ * })
1456
+ * // Groups articles into predefined categories
1457
+ * ```
1458
+ *
1459
+ * @example Grouping products
1460
+ * ```typescript
1461
+ * const products = [
1462
+ * { name: 'Laptop', price: 999, category: 'Electronics' },
1463
+ * { name: 'Desk', price: 299, category: 'Furniture' },
1464
+ * { name: 'Mouse', price: 29, category: 'Electronics' },
1465
+ * { name: 'Chair', price: 199, category: 'Furniture' }
1466
+ * ]
1467
+ *
1468
+ * const grouped = await zai.group(products, {
1469
+ * instructions: 'Group by price range: budget (< $100), mid-range ($100-$500), premium (> $500)'
1470
+ * })
1471
+ * // Result:
1472
+ * // {
1473
+ * // "Budget": [Mouse],
1474
+ * // "Mid-range": [Chair, Desk],
1475
+ * // "Premium": [Laptop]
1476
+ * // }
1477
+ * ```
1478
+ *
1479
+ * @example Content categorization
1480
+ * ```typescript
1481
+ * const emails = [
1482
+ * { subject: 'Meeting tomorrow', body: '...', from: 'boss@company.com' },
1483
+ * { subject: 'Invoice #1234', body: '...', from: 'billing@vendor.com' },
1484
+ * { subject: 'Weekly report', body: '...', from: 'team@company.com' },
1485
+ * { subject: 'Payment received', body: '...', from: 'accounting@company.com' }
1486
+ * ]
1487
+ *
1488
+ * const categorized = await zai.group(emails, {
1489
+ * instructions: 'Categorize by email type: work communication, financial, reports',
1490
+ * tokensPerElement: 300 // Allow more context per email
1491
+ * })
1492
+ * ```
1493
+ *
1494
+ * @example Customer feedback grouping
1495
+ * ```typescript
1496
+ * const feedback = [
1497
+ * "Love the new UI!",
1498
+ * "App is too slow",
1499
+ * "Great customer service",
1500
+ * "Confusing navigation",
1501
+ * "Fast shipping!",
1502
+ * "Hard to find features"
1503
+ * ]
1504
+ *
1505
+ * const grouped = await zai.group(feedback, {
1506
+ * instructions: 'Group by aspect: UI/UX, Performance, Customer Service, Shipping'
1507
+ * })
1508
+ * ```
1509
+ *
1510
+ * @example Topic clustering for research
1511
+ * ```typescript
1512
+ * const papers = [
1513
+ * { title: 'Transformer Networks for NLP', abstract: '...' },
1514
+ * { title: 'CNN Image Classification', abstract: '...' },
1515
+ * { title: 'BERT Language Understanding', abstract: '...' },
1516
+ * { title: 'Object Detection with YOLO', abstract: '...' }
1517
+ * ]
1518
+ *
1519
+ * const clusters = await zai.group(papers, {
1520
+ * instructions: 'Group by research area',
1521
+ * chunkLength: 10000 // Allow more tokens for detailed abstracts
1522
+ * })
1523
+ * // Result: Groups papers by topic (NLP, Computer Vision, etc.)
1524
+ * ```
1525
+ *
1526
+ * @example With initial seed groups
1527
+ * ```typescript
1528
+ * const tasks = [
1529
+ * "Fix login bug",
1530
+ * "Update documentation",
1531
+ * "Add dark mode",
1532
+ * "Optimize database queries"
1533
+ * ]
1534
+ *
1535
+ * const grouped = await zai.group(tasks, {
1536
+ * instructions: 'Categorize development tasks',
1537
+ * initialGroups: [
1538
+ * { id: 'bugs', label: 'Bug Fixes', elements: [] },
1539
+ * { id: 'features', label: 'New Features', elements: [] },
1540
+ * { id: 'docs', label: 'Documentation', elements: [] },
1541
+ * { id: 'performance', label: 'Performance', elements: [] }
1542
+ * ]
1543
+ * })
1544
+ * ```
1545
+ */
1546
+ group<T>(input: Array<T>, options?: Options$4): Response<Array<Group<T>>, Record<string, T[]>>;
339
1547
  }
340
1548
  }
341
1549
 
342
1550
  type RatingInstructions = string | Record<string, string>;
343
- type Options$2 = {
1551
+ type Options$3 = {
344
1552
  /** The maximum number of tokens per item */
345
1553
  tokensPerItem?: number;
346
1554
  /** The maximum number of items to rate per chunk */
@@ -358,33 +1566,245 @@ type SimplifiedRatingResult<T extends RatingInstructions> = T extends string ? n
358
1566
  declare module '@botpress/zai' {
359
1567
  interface Zai {
360
1568
  /**
361
- * Rates an array of items based on provided instructions.
362
- * Returns a number (1-5) if instructions is a string, or a Record<string, number> if instructions is a Record.
1569
+ * Rates array items on a 1-5 scale based on single or multiple criteria.
1570
+ *
1571
+ * This operation evaluates each item and assigns numeric ratings (1-5) where:
1572
+ * - 1 = Very Bad, 2 = Bad, 3 = Average, 4 = Good, 5 = Very Good
1573
+ *
1574
+ * Supports both simple single-criterion rating (string instructions) and
1575
+ * multi-criteria rating (object with criterion → description mapping).
1576
+ *
1577
+ * @param input - Array of items to rate
1578
+ * @param instructions - Single criterion (string) or multiple criteria (object)
1579
+ * @param options - Configuration for chunking and tokens per item
1580
+ * @returns Response with ratings array (simplified to numbers for single criterion)
1581
+ *
1582
+ * @example Single criterion rating
1583
+ * ```typescript
1584
+ * const reviews = [
1585
+ * "Amazing product! Best purchase ever!",
1586
+ * "It's okay, nothing special",
1587
+ * "Terrible quality, broke immediately"
1588
+ * ]
1589
+ *
1590
+ * const ratings = await zai.rate(reviews, 'Rate the sentiment')
1591
+ * // Result: [5, 3, 1] (simplified to numbers)
1592
+ *
1593
+ * // Get full details
1594
+ * const { output } = await zai.rate(reviews, 'Rate the sentiment').result()
1595
+ * // output[0]: { sentiment: 5, total: 5 }
1596
+ * ```
1597
+ *
1598
+ * @example Multi-criteria rating
1599
+ * ```typescript
1600
+ * const essays = [
1601
+ * "... student essay text ...",
1602
+ * "... another essay ...",
1603
+ * ]
1604
+ *
1605
+ * const ratings = await zai.rate(essays, {
1606
+ * grammar: 'Rate the grammar and spelling',
1607
+ * clarity: 'Rate how clear and well-organized the writing is',
1608
+ * argumentation: 'Rate the strength of arguments and evidence'
1609
+ * })
1610
+ *
1611
+ * // Result: [
1612
+ * // { grammar: 4, clarity: 5, argumentation: 3, total: 12 },
1613
+ * // { grammar: 3, clarity: 4, argumentation: 4, total: 11 }
1614
+ * // ]
1615
+ * ```
1616
+ *
1617
+ * @example Rating customer support conversations
1618
+ * ```typescript
1619
+ * const conversations = [
1620
+ * { agent: 'John', messages: [...], duration: 300 },
1621
+ * { agent: 'Jane', messages: [...], duration: 180 }
1622
+ * ]
1623
+ *
1624
+ * const ratings = await zai.rate(conversations, {
1625
+ * professionalism: 'How professional was the agent?',
1626
+ * helpfulness: 'How helpful was the agent in solving the issue?',
1627
+ * efficiency: 'How efficiently was the conversation handled?'
1628
+ * })
1629
+ *
1630
+ * // Calculate average scores
1631
+ * const avgProfessionalism = ratings.reduce((sum, r) => sum + r.professionalism, 0) / ratings.length
1632
+ * ```
1633
+ *
1634
+ * @example Rating code quality
1635
+ * ```typescript
1636
+ * const codeSamples = [
1637
+ * "function foo() { return x + y }",
1638
+ * "const calculateTotal = (items) => items.reduce((sum, item) => sum + item.price, 0)",
1639
+ * ]
1640
+ *
1641
+ * const quality = await zai.rate(codeSamples, {
1642
+ * readability: 'How readable and clear is the code?',
1643
+ * best_practices: 'Does it follow coding best practices?',
1644
+ * documentation: 'Is the code well-documented?'
1645
+ * })
1646
+ * ```
1647
+ *
1648
+ * @example Product review rating
1649
+ * ```typescript
1650
+ * const products = [
1651
+ * { name: 'Laptop', reviews: [...], avgStars: 4.2 },
1652
+ * { name: 'Mouse', reviews: [...], avgStars: 3.8 }
1653
+ * ]
1654
+ *
1655
+ * const ratings = await zai.rate(
1656
+ * products,
1657
+ * 'Rate overall product quality based on reviews and rating',
1658
+ * {
1659
+ * tokensPerItem: 500, // Allow more tokens for detailed reviews
1660
+ * maxItemsPerChunk: 20 // Process 20 products per chunk
1661
+ * }
1662
+ * )
1663
+ * ```
1664
+ *
1665
+ * @example Finding highest-rated items
1666
+ * ```typescript
1667
+ * const ratings = await zai.rate(items, {
1668
+ * quality: 'Product quality',
1669
+ * value: 'Value for money',
1670
+ * design: 'Design and aesthetics'
1671
+ * })
1672
+ *
1673
+ * // Sort by total score
1674
+ * const sorted = items
1675
+ * .map((item, idx) => ({ item, rating: ratings[idx] }))
1676
+ * .sort((a, b) => b.rating.total - a.rating.total)
1677
+ *
1678
+ * console.log('Top rated:', sorted[0].item)
1679
+ * console.log('Score breakdown:', sorted[0].rating)
1680
+ * ```
363
1681
  */
364
- rate<T, I extends RatingInstructions>(input: Array<T>, instructions: I, options?: Options$2): Response<Array<RatingResult<I>>, Array<SimplifiedRatingResult<I>>>;
1682
+ rate<T, I extends RatingInstructions>(input: Array<T>, instructions: I, options?: Options$3): Response<Array<RatingResult<I>>, Array<SimplifiedRatingResult<I>>>;
365
1683
  }
366
1684
  }
367
1685
 
368
- type Options$1 = {
1686
+ type Options$2 = {
369
1687
  /** The maximum number of tokens per item */
370
1688
  tokensPerItem?: number;
371
1689
  };
372
1690
  declare module '@botpress/zai' {
373
1691
  interface Zai {
374
1692
  /**
375
- * Sorts an array of items based on provided instructions.
376
- * Returns the sorted array directly when awaited.
377
- * Use .result() to get detailed scoring information including why each item got its position.
1693
+ * Sorts array items based on natural language sorting criteria.
378
1694
  *
379
- * @example
380
- * // Simple usage
381
- * const sorted = await zai.sort(items, 'from least expensive to most expensive')
1695
+ * This operation intelligently orders items according to your instructions, understanding
1696
+ * complex sorting logic like priority, quality, chronology, or any custom criteria.
1697
+ * Perfect for ranking, organizing, and prioritizing lists based on subjective or
1698
+ * multi-faceted criteria.
382
1699
  *
383
- * @example
384
- * // Get detailed results
385
- * const { output: sorted, usage } = await zai.sort(items, 'by priority').result()
1700
+ * @param input - Array of items to sort
1701
+ * @param instructions - Natural language description of how to sort (e.g., "by priority", "newest first")
1702
+ * @param options - Configuration for tokens per item
1703
+ * @returns Response resolving to the sorted array
1704
+ *
1705
+ * @example Sort by price
1706
+ * ```typescript
1707
+ * const products = [
1708
+ * { name: 'Laptop', price: 999 },
1709
+ * { name: 'Mouse', price: 29 },
1710
+ * { name: 'Keyboard', price: 79 }
1711
+ * ]
1712
+ *
1713
+ * const sorted = await zai.sort(products, 'from least expensive to most expensive')
1714
+ * // Result: [Mouse ($29), Keyboard ($79), Laptop ($999)]
1715
+ * ```
1716
+ *
1717
+ * @example Sort by priority/urgency
1718
+ * ```typescript
1719
+ * const tasks = [
1720
+ * "Update documentation",
1721
+ * "Fix critical security bug",
1722
+ * "Add new feature",
1723
+ * "System is down - all users affected"
1724
+ * ]
1725
+ *
1726
+ * const prioritized = await zai.sort(tasks, 'by urgency and impact, most urgent first')
1727
+ * // Result: ["System is down...", "Fix critical security bug", "Add new feature", "Update documentation"]
1728
+ * ```
1729
+ *
1730
+ * @example Sort by quality/rating
1731
+ * ```typescript
1732
+ * const reviews = [
1733
+ * "Product is okay",
1734
+ * "Absolutely amazing! Best purchase ever!",
1735
+ * "Terrible, broke immediately",
1736
+ * "Good quality for the price"
1737
+ * ]
1738
+ *
1739
+ * const sorted = await zai.sort(reviews, 'by sentiment, most positive first')
1740
+ * // Result: [amazing review, good review, okay review, terrible review]
1741
+ * ```
1742
+ *
1743
+ * @example Sort by complexity
1744
+ * ```typescript
1745
+ * const problems = [
1746
+ * "Fix typo in README",
1747
+ * "Redesign entire authentication system",
1748
+ * "Update a dependency version",
1749
+ * "Implement new microservice architecture"
1750
+ * ]
1751
+ *
1752
+ * const sorted = await zai.sort(problems, 'by complexity, simplest first')
1753
+ * ```
1754
+ *
1755
+ * @example Sort by relevance to query
1756
+ * ```typescript
1757
+ * const documents = [
1758
+ * "Article about cats",
1759
+ * "Article about dogs and their training",
1760
+ * "Article about dog breeds",
1761
+ * "Article about fish"
1762
+ * ]
1763
+ *
1764
+ * const sorted = await zai.sort(
1765
+ * documents,
1766
+ * 'by relevance to "dog training", most relevant first'
1767
+ * )
1768
+ * // Result: [dogs and training, dog breeds, cats, fish]
1769
+ * ```
1770
+ *
1771
+ * @example Sort candidates by fit
1772
+ * ```typescript
1773
+ * const candidates = [
1774
+ * { name: 'Alice', experience: 5, skills: ['React', 'Node'] },
1775
+ * { name: 'Bob', experience: 10, skills: ['Python', 'ML'] },
1776
+ * { name: 'Charlie', experience: 3, skills: ['React', 'TypeScript'] }
1777
+ * ]
1778
+ *
1779
+ * const sorted = await zai.sort(
1780
+ * candidates,
1781
+ * 'by fit for a senior React developer position, best fit first'
1782
+ * )
1783
+ * ```
1784
+ *
1785
+ * @example Sort chronologically
1786
+ * ```typescript
1787
+ * const events = [
1788
+ * "Started the project last month",
1789
+ * "Will launch next week",
1790
+ * "Met with client yesterday",
1791
+ * "Planning meeting tomorrow"
1792
+ * ]
1793
+ *
1794
+ * const chronological = await zai.sort(events, 'in chronological order')
1795
+ * // Understands relative time expressions
1796
+ * ```
1797
+ *
1798
+ * @example With token limit per item
1799
+ * ```typescript
1800
+ * const sorted = await zai.sort(
1801
+ * longDocuments,
1802
+ * 'by relevance to climate change research',
1803
+ * { tokensPerItem: 500 } // Allow 500 tokens per document
1804
+ * )
1805
+ * ```
386
1806
  */
387
- sort<T>(input: Array<T>, instructions: string, options?: Options$1): Response<Array<T>, Array<T>>;
1807
+ sort<T>(input: Array<T>, instructions: string, options?: Options$2): Response<Array<T>, Array<T>>;
388
1808
  }
389
1809
  }
390
1810
 
@@ -465,7 +1885,7 @@ type AnswerExample<T> = {
465
1885
  /** The expected answer result */
466
1886
  result: AnswerResult<T>;
467
1887
  };
468
- type Options<T> = {
1888
+ type Options$1<T> = {
469
1889
  /** Examples to help guide answer generation */
470
1890
  examples?: AnswerExample<T>[];
471
1891
  /** Additional instructions for answer generation */
@@ -481,13 +1901,229 @@ type Options<T> = {
481
1901
  declare module '@botpress/zai' {
482
1902
  interface Zai {
483
1903
  /**
484
- * Answer questions from a list of support documents with citations
485
- * @param documents - Array of support documents (can be strings, objects, etc.)
1904
+ * Answers questions from documents with citations and intelligent handling of edge cases.
1905
+ *
1906
+ * This operation provides a production-ready question-answering system that:
1907
+ * - Cites sources with precise line references
1908
+ * - Handles ambiguous questions with multiple interpretations
1909
+ * - Detects out-of-topic or invalid questions
1910
+ * - Identifies missing knowledge
1911
+ * - Automatically chunks and processes large document sets
1912
+ *
1913
+ * @param documents - Array of documents to search (strings, objects, or any type)
486
1914
  * @param question - The question to answer
487
- * @param options - Optional configuration
488
- * @returns Response with answer and citations, or other response types (ambiguous, out_of_topic, etc.)
1915
+ * @param options - Configuration for chunking, examples, and instructions
1916
+ * @returns Response with answer + citations, or error states (ambiguous, out_of_topic, invalid, missing_knowledge)
1917
+ *
1918
+ * @example Basic usage with string documents
1919
+ * ```typescript
1920
+ * const documents = [
1921
+ * 'Botpress was founded in 2016.',
1922
+ * 'The company is based in Quebec, Canada.',
1923
+ * 'Botpress provides an AI agent platform.'
1924
+ * ]
1925
+ *
1926
+ * const result = await zai.answer(documents, 'When was Botpress founded?')
1927
+ * if (result.type === 'answer') {
1928
+ * console.log(result.answer) // "Botpress was founded in 2016."
1929
+ * console.log(result.citations) // [{ offset: 30, item: documents[0], snippet: '...' }]
1930
+ * }
1931
+ * ```
1932
+ *
1933
+ * @example With object documents
1934
+ * ```typescript
1935
+ * const products = [
1936
+ * { id: 1, name: 'Pro Plan', price: 99, features: ['AI', 'Analytics'] },
1937
+ * { id: 2, name: 'Enterprise', price: 499, features: ['AI', 'Support', 'SLA'] }
1938
+ * ]
1939
+ *
1940
+ * const result = await zai.answer(products, 'What features does the Pro Plan include?')
1941
+ * // Returns answer with citations pointing to the product objects
1942
+ * ```
1943
+ *
1944
+ * @example Handling different response types
1945
+ * ```typescript
1946
+ * const result = await zai.answer(documents, question)
1947
+ *
1948
+ * switch (result.type) {
1949
+ * case 'answer':
1950
+ * console.log('Answer:', result.answer)
1951
+ * console.log('Sources:', result.citations)
1952
+ * break
1953
+ *
1954
+ * case 'ambiguous':
1955
+ * console.log('Question is ambiguous:', result.ambiguity)
1956
+ * console.log('Clarifying question:', result.follow_up)
1957
+ * console.log('Possible answers:', result.answers)
1958
+ * break
1959
+ *
1960
+ * case 'out_of_topic':
1961
+ * console.log('Question unrelated:', result.reason)
1962
+ * break
1963
+ *
1964
+ * case 'invalid_question':
1965
+ * console.log('Invalid question:', result.reason)
1966
+ * break
1967
+ *
1968
+ * case 'missing_knowledge':
1969
+ * console.log('Insufficient info:', result.reason)
1970
+ * break
1971
+ * }
1972
+ * ```
1973
+ *
1974
+ * @example With custom instructions
1975
+ * ```typescript
1976
+ * const result = await zai.answer(documents, 'What is the pricing?', {
1977
+ * instructions: 'Provide detailed pricing breakdown including all tiers',
1978
+ * chunkLength: 8000 // Process in smaller chunks for accuracy
1979
+ * })
1980
+ * ```
1981
+ *
1982
+ * @example Large document sets (auto-chunking)
1983
+ * ```typescript
1984
+ * // Handles thousands of documents automatically
1985
+ * const manyDocs = await loadDocuments() // 1000+ documents
1986
+ * const result = await zai.answer(manyDocs, 'What is the refund policy?')
1987
+ * // Automatically chunks, processes in parallel, and merges results
1988
+ * ```
1989
+ *
1990
+ * @example Tracking citations
1991
+ * ```typescript
1992
+ * const result = await zai.answer(documents, question)
1993
+ * if (result.type === 'answer') {
1994
+ * result.citations.forEach(citation => {
1995
+ * console.log(`At position ${citation.offset}:`)
1996
+ * console.log(` Cited: "${citation.snippet}"`)
1997
+ * console.log(` From document:`, citation.item)
1998
+ * })
1999
+ * }
2000
+ * ```
2001
+ */
2002
+ answer<T>(documents: T[], question: string, options?: Options$1<T>): Response<AnswerResult<T>, AnswerResult<T>>;
2003
+ }
2004
+ }
2005
+
2006
+ /**
2007
+ * Represents a file to be patched
2008
+ */
2009
+ type File = {
2010
+ /** The file path (e.g., 'src/components/Button.tsx') */
2011
+ path: string;
2012
+ /** The file name (e.g., 'Button.tsx') */
2013
+ name: string;
2014
+ /** The file content */
2015
+ content: string;
2016
+ /** The patch operations that were applied (only present in output) */
2017
+ patch?: string;
2018
+ };
2019
+ type Options = {
2020
+ /**
2021
+ * Maximum tokens per chunk when processing large files or many files.
2022
+ * If a single file exceeds this limit, it will be split into chunks.
2023
+ * If all files together exceed this limit, they will be processed in batches.
2024
+ * If not specified, all files must fit in a single prompt.
2025
+ */
2026
+ maxTokensPerChunk?: number;
2027
+ };
2028
+ declare module '@botpress/zai' {
2029
+ interface Zai {
2030
+ /**
2031
+ * Patches files based on natural language instructions using the micropatch protocol.
2032
+ *
2033
+ * This operation takes an array of files and instructions, then returns the modified files.
2034
+ * It uses a token-efficient line-based patching protocol (micropatch) that allows precise
2035
+ * modifications without regenerating entire files.
2036
+ *
2037
+ * @param files - Array of files to patch, each with path, name, and content
2038
+ * @param instructions - Natural language instructions describing what changes to make
2039
+ * @param options - Optional configuration for patch generation
2040
+ * @returns Response promise resolving to array of patched files
2041
+ *
2042
+ * @example Simple text replacement
2043
+ * ```typescript
2044
+ * const files = [{
2045
+ * path: 'src/hello.ts',
2046
+ * name: 'hello.ts',
2047
+ * content: 'console.log("Hello World")'
2048
+ * }]
2049
+ *
2050
+ * const patched = await zai.patch(
2051
+ * files,
2052
+ * 'change the message to say "Hi World"'
2053
+ * )
2054
+ * // patched[0].content contains: console.log("Hi World")
2055
+ * // patched[0].patch contains: ◼︎=1|console.log("Hi World")
2056
+ * ```
2057
+ *
2058
+ * @example Adding documentation
2059
+ * ```typescript
2060
+ * const files = [{
2061
+ * path: 'src/utils.ts',
2062
+ * name: 'utils.ts',
2063
+ * content: 'export function add(a: number, b: number) {\n return a + b\n}'
2064
+ * }]
2065
+ *
2066
+ * const patched = await zai.patch(
2067
+ * files,
2068
+ * 'add JSDoc comments to all exported functions'
2069
+ * )
2070
+ * ```
2071
+ *
2072
+ * @example Patching multiple files
2073
+ * ```typescript
2074
+ * const files = [
2075
+ * { path: 'package.json', name: 'package.json', content: '...' },
2076
+ * { path: 'config.json', name: 'config.json', content: '...' }
2077
+ * ]
2078
+ *
2079
+ * const patched = await zai.patch(
2080
+ * files,
2081
+ * 'update version to 2.0.0 in all config files'
2082
+ * )
2083
+ * ```
2084
+ *
2085
+ * @example Refactoring code
2086
+ * ```typescript
2087
+ * const files = [{
2088
+ * path: 'src/api.ts',
2089
+ * name: 'api.ts',
2090
+ * content: 'function fetchUser() { ... }'
2091
+ * }]
2092
+ *
2093
+ * const patched = await zai.patch(
2094
+ * files,
2095
+ * 'convert fetchUser to an async function and add error handling'
2096
+ * )
2097
+ * ```
2098
+ *
2099
+ * @example Removing code
2100
+ * ```typescript
2101
+ * const files = [{
2102
+ * path: 'src/legacy.ts',
2103
+ * name: 'legacy.ts',
2104
+ * content: 'const debug = true\nconsole.log("Debug mode")\nfunction main() {...}'
2105
+ * }]
2106
+ *
2107
+ * const patched = await zai.patch(
2108
+ * files,
2109
+ * 'remove all debug-related code'
2110
+ * )
2111
+ * ```
2112
+ *
2113
+ * @example Inspecting applied patches
2114
+ * ```typescript
2115
+ * const patched = await zai.patch(files, 'add error handling')
2116
+ *
2117
+ * // Check what patches were applied
2118
+ * for (const file of patched) {
2119
+ * if (file.patch) {
2120
+ * console.log(`Patches for ${file.path}:`)
2121
+ * console.log(file.patch)
2122
+ * }
2123
+ * }
2124
+ * ```
489
2125
  */
490
- answer<T>(documents: T[], question: string, options?: Options<T>): Response<AnswerResult<T>, AnswerResult<T>>;
2126
+ patch(files: Array<File>, instructions: string, options?: Options): Response<Array<File>>;
491
2127
  }
492
2128
  }
493
2129