ai 3.3.6 → 3.3.7

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.
@@ -1,7 +1,7 @@
1
- import OpenAI from 'openai';
1
+ import { LanguageModelV1FinishReason, LanguageModelV1CallWarning, LanguageModelV1 } from '@ai-sdk/provider';
2
2
  import { ReactNode } from 'react';
3
3
  import { z } from 'zod';
4
- import { LanguageModelV1FinishReason, LanguageModelV1CallWarning, LanguageModelV1 } from '@ai-sdk/provider';
4
+ import OpenAI from 'openai';
5
5
 
6
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
7
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
@@ -26,16 +26,6 @@ type MutableAIState<AIState> = {
26
26
  update: (newState: ValueOrUpdater<AIState>) => void;
27
27
  done: ((newState: AIState) => void) | (() => void);
28
28
  };
29
- declare const __internal_curr: unique symbol;
30
- declare const __internal_error: unique symbol;
31
- /**
32
- * StreamableValue is a value that can be streamed over the network via AI Actions.
33
- * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
34
- */
35
- type StreamableValue<T = any, E = any> = {
36
- [__internal_curr]?: T;
37
- [__internal_error]?: E;
38
- };
39
29
 
40
30
  /**
41
31
  * Get the current AI state.
@@ -68,156 +58,45 @@ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState
68
58
  declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
69
59
  declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
70
60
 
71
- type StreamableUIWrapper = {
72
- /**
73
- * The value of the streamable UI. This can be returned from a Server Action and received by the client.
74
- */
75
- readonly value: React.ReactNode;
61
+ declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
62
+ actions: Actions;
63
+ initialAIState?: AIState;
64
+ initialUIState?: UIState;
76
65
  /**
77
- * This method updates the current UI node. It takes a new UI node and replaces the old one.
66
+ * This function is called whenever the AI state is updated by an Action.
67
+ * You can use this to persist the AI state to a database, or to send it to a
68
+ * logging service.
78
69
  */
79
- update(value: React.ReactNode): StreamableUIWrapper;
70
+ onSetAIState?: OnSetAIState<AIState>;
80
71
  /**
81
- * This method is used to append a new UI node to the end of the old one.
82
- * Once appended a new UI node, the previous UI node cannot be updated anymore.
83
- *
84
- * @example
85
- * ```jsx
86
- * const ui = createStreamableUI(<div>hello</div>)
87
- * ui.append(<div>world</div>)
72
+ * This function is used to retrieve the UI state based on the AI state.
73
+ * For example, to render the initial UI state based on a given AI state, or
74
+ * to sync the UI state when the application is already loaded.
88
75
  *
89
- * // The UI node will be:
90
- * // <>
91
- * // <div>hello</div>
92
- * // <div>world</div>
93
- * // </>
94
- * ```
95
- */
96
- append(value: React.ReactNode): StreamableUIWrapper;
97
- /**
98
- * This method is used to signal that there is an error in the UI stream.
99
- * It will be thrown on the client side and caught by the nearest error boundary component.
100
- */
101
- error(error: any): StreamableUIWrapper;
102
- /**
103
- * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
104
- * Once called, the UI node cannot be updated or appended anymore.
76
+ * If returning `undefined`, the client side UI state will not be updated.
105
77
  *
106
- * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
107
- */
108
- done(...args: [React.ReactNode] | []): StreamableUIWrapper;
109
- };
110
- /**
111
- * Create a piece of changeable UI that can be streamed to the client.
112
- * On the client side, it can be rendered as a normal React node.
113
- */
114
- declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
115
- /**
116
- * Create a wrapped, changeable value that can be streamed to the client.
117
- * On the client side, the value can be accessed via the readStreamableValue() API.
118
- */
119
- declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
120
- type StreamableValueWrapper<T, E> = {
121
- /**
122
- * The value of the streamable. This can be returned from a Server Action and
123
- * received by the client. To read the streamed values, use the
124
- * `readStreamableValue` or `useStreamableValue` APIs.
125
- */
126
- readonly value: StreamableValue<T, E>;
127
- /**
128
- * This method updates the current value with a new one.
129
- */
130
- update(value: T): StreamableValueWrapper<T, E>;
131
- /**
132
- * This method is used to append a delta string to the current value. It
133
- * requires the current value of the streamable to be a string.
78
+ * This function must be annotated with the `"use server"` directive.
134
79
  *
135
80
  * @example
136
- * ```jsx
137
- * const streamable = createStreamableValue('hello');
138
- * streamable.append(' world');
81
+ * ```tsx
82
+ * onGetUIState: async () => {
83
+ * 'use server';
139
84
  *
140
- * // The value will be 'hello world'
141
- * ```
142
- */
143
- append(value: T): StreamableValueWrapper<T, E>;
144
- /**
145
- * This method is used to signal that there is an error in the value stream.
146
- * It will be thrown on the client side when consumed via
147
- * `readStreamableValue` or `useStreamableValue`.
148
- */
149
- error(error: any): StreamableValueWrapper<T, E>;
150
- /**
151
- * This method marks the value as finalized. You can either call it without
152
- * any parameters or with a new value as the final state.
153
- * Once called, the value cannot be updated or appended anymore.
85
+ * const currentAIState = getAIState();
86
+ * const externalAIState = await loadAIStateFromDatabase();
154
87
  *
155
- * This method is always **required** to be called, otherwise the response
156
- * will be stuck in a loading state.
157
- */
158
- done(...args: [T] | []): StreamableValueWrapper<T, E>;
159
- };
160
-
161
- type Streamable$1 = ReactNode | Promise<ReactNode>;
162
- type Renderer$1<T> = (props: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
163
- /**
164
- * `render` is a helper function to create a streamable UI from some LLMs.
165
- * This API only supports OpenAI's GPT models with Function Calling and Assistants Tools,
166
- * please use `streamUI` for compatibility with other providers.
167
- *
168
- * @deprecated It's recommended to use the `streamUI` API for compatibility with AI SDK Core APIs
169
- * and future features. This API will be removed in a future release.
170
- */
171
- declare function render<TS extends {
172
- [name: string]: z.Schema;
173
- } = {}, FS extends {
174
- [name: string]: z.Schema;
175
- } = {}>(options: {
176
- /**
177
- * The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
178
- * GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
88
+ * if (currentAIState === externalAIState) return undefined;
179
89
  *
180
- * @example "gpt-3.5-turbo"
181
- */
182
- model: string;
183
- /**
184
- * The provider instance to use. Currently the only provider available is OpenAI.
185
- * This needs to match the model name.
90
+ * // Update current AI state and return the new UI state
91
+ * const state = getMutableAIState()
92
+ * state.done(externalAIState)
93
+ *
94
+ * return <div>...</div>;
95
+ * }
96
+ * ```
186
97
  */
187
- provider: OpenAI;
188
- messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
189
- text?: Renderer$1<{
190
- /**
191
- * The full text content from the model so far.
192
- */
193
- content: string;
194
- /**
195
- * The new appended text content from the model since the last `text` call.
196
- */
197
- delta: string;
198
- /**
199
- * Whether the model is done generating text.
200
- * If `true`, the `content` will be the final output and this call will be the last.
201
- */
202
- done: boolean;
203
- }>;
204
- tools?: {
205
- [name in keyof TS]: {
206
- description?: string;
207
- parameters: TS[name];
208
- render: Renderer$1<z.infer<TS[name]>>;
209
- };
210
- };
211
- functions?: {
212
- [name in keyof FS]: {
213
- description?: string;
214
- parameters: FS[name];
215
- render: Renderer$1<z.infer<FS[name]>>;
216
- };
217
- };
218
- initial?: ReactNode;
219
- temperature?: number;
220
- }): ReactNode;
98
+ onGetUIState?: OnGetUIState<UIState>;
99
+ }): AIProvider<AIState, UIState, Actions>;
221
100
 
222
101
  type CallSettings = {
223
102
  /**
@@ -486,12 +365,12 @@ type CoreToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | '
486
365
  toolName: keyof TOOLS;
487
366
  };
488
367
 
489
- type Streamable = ReactNode | Promise<ReactNode>;
490
- type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
368
+ type Streamable$1 = ReactNode | Promise<ReactNode>;
369
+ type Renderer$1<T extends Array<any>> = (...args: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
491
370
  type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
492
371
  description?: string;
493
372
  parameters: PARAMETERS;
494
- generate?: Renderer<[
373
+ generate?: Renderer$1<[
495
374
  z.infer<PARAMETERS>,
496
375
  {
497
376
  toolName: string;
@@ -499,7 +378,7 @@ type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
499
378
  }
500
379
  ]>;
501
380
  };
502
- type RenderText = Renderer<[
381
+ type RenderText = Renderer$1<[
503
382
  {
504
383
  /**
505
384
  * The full text content from the model so far.
@@ -573,44 +452,167 @@ declare function streamUI<TOOLS extends {
573
452
  }) => Promise<void> | void;
574
453
  }): Promise<RenderResult>;
575
454
 
576
- declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
577
- actions: Actions;
578
- initialAIState?: AIState;
579
- initialUIState?: UIState;
455
+ type Streamable = ReactNode | Promise<ReactNode>;
456
+ type Renderer<T> = (props: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
457
+ /**
458
+ * `render` is a helper function to create a streamable UI from some LLMs.
459
+ * This API only supports OpenAI's GPT models with Function Calling and Assistants Tools,
460
+ * please use `streamUI` for compatibility with other providers.
461
+ *
462
+ * @deprecated It's recommended to use the `streamUI` API for compatibility with AI SDK Core APIs
463
+ * and future features. This API will be removed in a future release.
464
+ */
465
+ declare function render<TS extends {
466
+ [name: string]: z.Schema;
467
+ } = {}, FS extends {
468
+ [name: string]: z.Schema;
469
+ } = {}>(options: {
580
470
  /**
581
- * This function is called whenever the AI state is updated by an Action.
582
- * You can use this to persist the AI state to a database, or to send it to a
583
- * logging service.
471
+ * The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
472
+ * GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
473
+ *
474
+ * @example "gpt-3.5-turbo"
584
475
  */
585
- onSetAIState?: OnSetAIState<AIState>;
476
+ model: string;
586
477
  /**
587
- * This function is used to retrieve the UI state based on the AI state.
588
- * For example, to render the initial UI state based on a given AI state, or
589
- * to sync the UI state when the application is already loaded.
590
- *
591
- * If returning `undefined`, the client side UI state will not be updated.
592
- *
593
- * This function must be annotated with the `"use server"` directive.
478
+ * The provider instance to use. Currently the only provider available is OpenAI.
479
+ * This needs to match the model name.
480
+ */
481
+ provider: OpenAI;
482
+ messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
483
+ text?: Renderer<{
484
+ /**
485
+ * The full text content from the model so far.
486
+ */
487
+ content: string;
488
+ /**
489
+ * The new appended text content from the model since the last `text` call.
490
+ */
491
+ delta: string;
492
+ /**
493
+ * Whether the model is done generating text.
494
+ * If `true`, the `content` will be the final output and this call will be the last.
495
+ */
496
+ done: boolean;
497
+ }>;
498
+ tools?: {
499
+ [name in keyof TS]: {
500
+ description?: string;
501
+ parameters: TS[name];
502
+ render: Renderer<z.infer<TS[name]>>;
503
+ };
504
+ };
505
+ functions?: {
506
+ [name in keyof FS]: {
507
+ description?: string;
508
+ parameters: FS[name];
509
+ render: Renderer<z.infer<FS[name]>>;
510
+ };
511
+ };
512
+ initial?: ReactNode;
513
+ temperature?: number;
514
+ }): ReactNode;
515
+
516
+ type StreamableUIWrapper = {
517
+ /**
518
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
519
+ */
520
+ readonly value: React.ReactNode;
521
+ /**
522
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
523
+ */
524
+ update(value: React.ReactNode): StreamableUIWrapper;
525
+ /**
526
+ * This method is used to append a new UI node to the end of the old one.
527
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
594
528
  *
595
529
  * @example
596
- * ```tsx
597
- * onGetUIState: async () => {
598
- * 'use server';
530
+ * ```jsx
531
+ * const ui = createStreamableUI(<div>hello</div>)
532
+ * ui.append(<div>world</div>)
599
533
  *
600
- * const currentAIState = getAIState();
601
- * const externalAIState = await loadAIStateFromDatabase();
534
+ * // The UI node will be:
535
+ * // <>
536
+ * // <div>hello</div>
537
+ * // <div>world</div>
538
+ * // </>
539
+ * ```
540
+ */
541
+ append(value: React.ReactNode): StreamableUIWrapper;
542
+ /**
543
+ * This method is used to signal that there is an error in the UI stream.
544
+ * It will be thrown on the client side and caught by the nearest error boundary component.
545
+ */
546
+ error(error: any): StreamableUIWrapper;
547
+ /**
548
+ * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
549
+ * Once called, the UI node cannot be updated or appended anymore.
602
550
  *
603
- * if (currentAIState === externalAIState) return undefined;
551
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
552
+ */
553
+ done(...args: [React.ReactNode] | []): StreamableUIWrapper;
554
+ };
555
+ /**
556
+ * Create a piece of changeable UI that can be streamed to the client.
557
+ * On the client side, it can be rendered as a normal React node.
558
+ */
559
+ declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
560
+
561
+ declare const __internal_curr: unique symbol;
562
+ declare const __internal_error: unique symbol;
563
+ /**
564
+ * StreamableValue is a value that can be streamed over the network via AI Actions.
565
+ * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
566
+ */
567
+ type StreamableValue<T = any, E = any> = {
568
+ [__internal_curr]?: T;
569
+ [__internal_error]?: E;
570
+ };
571
+
572
+ /**
573
+ * Create a wrapped, changeable value that can be streamed to the client.
574
+ * On the client side, the value can be accessed via the readStreamableValue() API.
575
+ */
576
+ declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
577
+ type StreamableValueWrapper<T, E> = {
578
+ /**
579
+ * The value of the streamable. This can be returned from a Server Action and
580
+ * received by the client. To read the streamed values, use the
581
+ * `readStreamableValue` or `useStreamableValue` APIs.
582
+ */
583
+ readonly value: StreamableValue<T, E>;
584
+ /**
585
+ * This method updates the current value with a new one.
586
+ */
587
+ update(value: T): StreamableValueWrapper<T, E>;
588
+ /**
589
+ * This method is used to append a delta string to the current value. It
590
+ * requires the current value of the streamable to be a string.
604
591
  *
605
- * // Update current AI state and return the new UI state
606
- * const state = getMutableAIState()
607
- * state.done(externalAIState)
592
+ * @example
593
+ * ```jsx
594
+ * const streamable = createStreamableValue('hello');
595
+ * streamable.append(' world');
608
596
  *
609
- * return <div>...</div>;
610
- * }
597
+ * // The value will be 'hello world'
611
598
  * ```
612
599
  */
613
- onGetUIState?: OnGetUIState<UIState>;
614
- }): AIProvider<AIState, UIState, Actions>;
600
+ append(value: T): StreamableValueWrapper<T, E>;
601
+ /**
602
+ * This method is used to signal that there is an error in the value stream.
603
+ * It will be thrown on the client side when consumed via
604
+ * `readStreamableValue` or `useStreamableValue`.
605
+ */
606
+ error(error: any): StreamableValueWrapper<T, E>;
607
+ /**
608
+ * This method marks the value as finalized. You can either call it without
609
+ * any parameters or with a new value as the final state.
610
+ * Once called, the value cannot be updated or appended anymore.
611
+ *
612
+ * This method is always **required** to be called, otherwise the response
613
+ * will be stuck in a loading state.
614
+ */
615
+ done(...args: [T] | []): StreamableValueWrapper<T, E>;
616
+ };
615
617
 
616
618
  export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, render, streamUI };