ai 2.2.37 → 3.0.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/package.json +20 -7
- package/rsc/dist/rsc-client.d.ts +1 -0
- package/rsc/dist/rsc-client.mjs +16 -0
- package/rsc/dist/rsc-client.mjs.map +1 -0
- package/rsc/dist/rsc-server.d.ts +143 -0
- package/rsc/dist/rsc-server.mjs +1112 -0
- package/rsc/dist/rsc-server.mjs.map +1 -0
- package/rsc/dist/rsc-shared.d.ts +46 -0
- package/rsc/dist/rsc-shared.mjs +223 -0
- package/rsc/dist/rsc-shared.mjs.map +1 -0
- package/rsc/dist/rsc-types.d.ts +6 -0
- package/rsc/dist/rsc-types.mjs +1 -0
- package/rsc/dist/rsc-types.mjs.map +1 -0
@@ -0,0 +1,1112 @@
|
|
1
|
+
// rsc/ai-state.tsx
|
2
|
+
import { AsyncLocalStorage } from "async_hooks";
|
3
|
+
import * as jsondiffpatch from "jsondiffpatch";
|
4
|
+
|
5
|
+
// rsc/utils.tsx
|
6
|
+
import { Suspense } from "react";
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
8
|
+
function createResolvablePromise() {
|
9
|
+
let resolve, reject;
|
10
|
+
const promise = new Promise((res, rej) => {
|
11
|
+
resolve = res;
|
12
|
+
reject = rej;
|
13
|
+
});
|
14
|
+
return {
|
15
|
+
promise,
|
16
|
+
resolve,
|
17
|
+
reject
|
18
|
+
};
|
19
|
+
}
|
20
|
+
function createSuspensedChunk(initialValue) {
|
21
|
+
const Row = async ({
|
22
|
+
current,
|
23
|
+
next
|
24
|
+
}) => {
|
25
|
+
const chunk = await next;
|
26
|
+
if (chunk.done) {
|
27
|
+
return chunk.value;
|
28
|
+
}
|
29
|
+
if (chunk.append) {
|
30
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
31
|
+
current,
|
32
|
+
/* @__PURE__ */ jsx(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx(Row, { current: chunk.value, next: chunk.next }) })
|
33
|
+
] });
|
34
|
+
}
|
35
|
+
return /* @__PURE__ */ jsx(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx(Row, { current: chunk.value, next: chunk.next }) });
|
36
|
+
};
|
37
|
+
const { promise, resolve, reject } = createResolvablePromise();
|
38
|
+
return {
|
39
|
+
row: /* @__PURE__ */ jsx(Suspense, { fallback: initialValue, children: /* @__PURE__ */ jsx(Row, { current: initialValue, next: promise }) }),
|
40
|
+
resolve,
|
41
|
+
reject
|
42
|
+
};
|
43
|
+
}
|
44
|
+
var isFunction = (x) => typeof x === "function";
|
45
|
+
var consumeStream = async (stream) => {
|
46
|
+
const reader = stream.getReader();
|
47
|
+
while (true) {
|
48
|
+
const { done } = await reader.read();
|
49
|
+
if (done)
|
50
|
+
break;
|
51
|
+
}
|
52
|
+
};
|
53
|
+
|
54
|
+
// rsc/ai-state.tsx
|
55
|
+
var asyncAIStateStorage = new AsyncLocalStorage();
|
56
|
+
function getAIStateStoreOrThrow(message) {
|
57
|
+
const store = asyncAIStateStorage.getStore();
|
58
|
+
if (!store) {
|
59
|
+
throw new Error(message);
|
60
|
+
}
|
61
|
+
return store;
|
62
|
+
}
|
63
|
+
function withAIState({ state, options }, fn) {
|
64
|
+
return asyncAIStateStorage.run(
|
65
|
+
{
|
66
|
+
currentState: state,
|
67
|
+
originalState: state,
|
68
|
+
sealed: false,
|
69
|
+
options
|
70
|
+
},
|
71
|
+
fn
|
72
|
+
);
|
73
|
+
}
|
74
|
+
function getAIStateDeltaPromise() {
|
75
|
+
const store = getAIStateStoreOrThrow("Internal error occurred.");
|
76
|
+
return store.mutationDeltaPromise;
|
77
|
+
}
|
78
|
+
function sealMutableAIState() {
|
79
|
+
const store = getAIStateStoreOrThrow("Internal error occurred.");
|
80
|
+
store.sealed = true;
|
81
|
+
}
|
82
|
+
function getAIState(...args) {
|
83
|
+
const store = getAIStateStoreOrThrow(
|
84
|
+
"`getAIState` must be called within an AI Action."
|
85
|
+
);
|
86
|
+
if (args.length > 0) {
|
87
|
+
const key = args[0];
|
88
|
+
if (typeof store.currentState !== "object") {
|
89
|
+
throw new Error(
|
90
|
+
`You can't get the "${String(
|
91
|
+
key
|
92
|
+
)}" field from the AI state because it's not an object.`
|
93
|
+
);
|
94
|
+
}
|
95
|
+
return store.currentState[key];
|
96
|
+
}
|
97
|
+
return store.currentState;
|
98
|
+
}
|
99
|
+
function getMutableAIState(...args) {
|
100
|
+
const store = getAIStateStoreOrThrow(
|
101
|
+
"`getMutableAIState` must be called within an AI Action."
|
102
|
+
);
|
103
|
+
if (store.sealed) {
|
104
|
+
throw new Error(
|
105
|
+
"`getMutableAIState` must be called before returning from an AI Action. Please move it to the top level of the Action's function body."
|
106
|
+
);
|
107
|
+
}
|
108
|
+
if (!store.mutationDeltaPromise) {
|
109
|
+
const { promise, resolve } = createResolvablePromise();
|
110
|
+
store.mutationDeltaPromise = promise;
|
111
|
+
store.mutationDeltaResolve = resolve;
|
112
|
+
}
|
113
|
+
function doUpdate(newState, done) {
|
114
|
+
var _a, _b;
|
115
|
+
if (args.length > 0) {
|
116
|
+
if (typeof store.currentState !== "object") {
|
117
|
+
const key = args[0];
|
118
|
+
throw new Error(
|
119
|
+
`You can't modify the "${String(
|
120
|
+
key
|
121
|
+
)}" field of the AI state because it's not an object.`
|
122
|
+
);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
if (isFunction(newState)) {
|
126
|
+
if (args.length > 0) {
|
127
|
+
store.currentState[args[0]] = newState(store.currentState[args[0]]);
|
128
|
+
} else {
|
129
|
+
store.currentState = newState(store.currentState);
|
130
|
+
}
|
131
|
+
} else {
|
132
|
+
if (args.length > 0) {
|
133
|
+
store.currentState[args[0]] = newState;
|
134
|
+
} else {
|
135
|
+
store.currentState = newState;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
(_b = (_a = store.options).onSetAIState) == null ? void 0 : _b.call(_a, {
|
139
|
+
key: args.length > 0 ? args[0] : void 0,
|
140
|
+
state: store.currentState,
|
141
|
+
done
|
142
|
+
});
|
143
|
+
}
|
144
|
+
const mutableState = {
|
145
|
+
get: () => {
|
146
|
+
if (args.length > 0) {
|
147
|
+
const key = args[0];
|
148
|
+
if (typeof store.currentState !== "object") {
|
149
|
+
throw new Error(
|
150
|
+
`You can't get the "${String(
|
151
|
+
key
|
152
|
+
)}" field from the AI state because it's not an object.`
|
153
|
+
);
|
154
|
+
}
|
155
|
+
return store.currentState[key];
|
156
|
+
}
|
157
|
+
return store.currentState;
|
158
|
+
},
|
159
|
+
update: function update(newAIState) {
|
160
|
+
doUpdate(newAIState, false);
|
161
|
+
},
|
162
|
+
done: function done(...doneArgs) {
|
163
|
+
if (doneArgs.length > 0) {
|
164
|
+
doUpdate(doneArgs[0], true);
|
165
|
+
}
|
166
|
+
const delta = jsondiffpatch.diff(store.originalState, store.currentState);
|
167
|
+
store.mutationDeltaResolve(delta);
|
168
|
+
}
|
169
|
+
};
|
170
|
+
return mutableState;
|
171
|
+
}
|
172
|
+
|
173
|
+
// rsc/streamable.tsx
|
174
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
175
|
+
|
176
|
+
// shared/utils.ts
|
177
|
+
import { customAlphabet } from "nanoid/non-secure";
|
178
|
+
|
179
|
+
// shared/stream-parts.ts
|
180
|
+
var textStreamPart = {
|
181
|
+
code: "0",
|
182
|
+
name: "text",
|
183
|
+
parse: (value) => {
|
184
|
+
if (typeof value !== "string") {
|
185
|
+
throw new Error('"text" parts expect a string value.');
|
186
|
+
}
|
187
|
+
return { type: "text", value };
|
188
|
+
}
|
189
|
+
};
|
190
|
+
var functionCallStreamPart = {
|
191
|
+
code: "1",
|
192
|
+
name: "function_call",
|
193
|
+
parse: (value) => {
|
194
|
+
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
195
|
+
throw new Error(
|
196
|
+
'"function_call" parts expect an object with a "function_call" property.'
|
197
|
+
);
|
198
|
+
}
|
199
|
+
return {
|
200
|
+
type: "function_call",
|
201
|
+
value
|
202
|
+
};
|
203
|
+
}
|
204
|
+
};
|
205
|
+
var dataStreamPart = {
|
206
|
+
code: "2",
|
207
|
+
name: "data",
|
208
|
+
parse: (value) => {
|
209
|
+
if (!Array.isArray(value)) {
|
210
|
+
throw new Error('"data" parts expect an array value.');
|
211
|
+
}
|
212
|
+
return { type: "data", value };
|
213
|
+
}
|
214
|
+
};
|
215
|
+
var errorStreamPart = {
|
216
|
+
code: "3",
|
217
|
+
name: "error",
|
218
|
+
parse: (value) => {
|
219
|
+
if (typeof value !== "string") {
|
220
|
+
throw new Error('"error" parts expect a string value.');
|
221
|
+
}
|
222
|
+
return { type: "error", value };
|
223
|
+
}
|
224
|
+
};
|
225
|
+
var assistantMessageStreamPart = {
|
226
|
+
code: "4",
|
227
|
+
name: "assistant_message",
|
228
|
+
parse: (value) => {
|
229
|
+
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
230
|
+
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
231
|
+
)) {
|
232
|
+
throw new Error(
|
233
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
234
|
+
);
|
235
|
+
}
|
236
|
+
return {
|
237
|
+
type: "assistant_message",
|
238
|
+
value
|
239
|
+
};
|
240
|
+
}
|
241
|
+
};
|
242
|
+
var assistantControlDataStreamPart = {
|
243
|
+
code: "5",
|
244
|
+
name: "assistant_control_data",
|
245
|
+
parse: (value) => {
|
246
|
+
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
247
|
+
throw new Error(
|
248
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
249
|
+
);
|
250
|
+
}
|
251
|
+
return {
|
252
|
+
type: "assistant_control_data",
|
253
|
+
value: {
|
254
|
+
threadId: value.threadId,
|
255
|
+
messageId: value.messageId
|
256
|
+
}
|
257
|
+
};
|
258
|
+
}
|
259
|
+
};
|
260
|
+
var dataMessageStreamPart = {
|
261
|
+
code: "6",
|
262
|
+
name: "data_message",
|
263
|
+
parse: (value) => {
|
264
|
+
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
265
|
+
throw new Error(
|
266
|
+
'"data_message" parts expect an object with a "role" and "data" property.'
|
267
|
+
);
|
268
|
+
}
|
269
|
+
return {
|
270
|
+
type: "data_message",
|
271
|
+
value
|
272
|
+
};
|
273
|
+
}
|
274
|
+
};
|
275
|
+
var toolCallStreamPart = {
|
276
|
+
code: "7",
|
277
|
+
name: "tool_calls",
|
278
|
+
parse: (value) => {
|
279
|
+
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
|
280
|
+
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
|
281
|
+
})) {
|
282
|
+
throw new Error(
|
283
|
+
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
284
|
+
);
|
285
|
+
}
|
286
|
+
return {
|
287
|
+
type: "tool_calls",
|
288
|
+
value
|
289
|
+
};
|
290
|
+
}
|
291
|
+
};
|
292
|
+
var messageAnnotationsStreamPart = {
|
293
|
+
code: "8",
|
294
|
+
name: "message_annotations",
|
295
|
+
parse: (value) => {
|
296
|
+
if (!Array.isArray(value)) {
|
297
|
+
throw new Error('"message_annotations" parts expect an array value.');
|
298
|
+
}
|
299
|
+
return { type: "message_annotations", value };
|
300
|
+
}
|
301
|
+
};
|
302
|
+
var streamParts = [
|
303
|
+
textStreamPart,
|
304
|
+
functionCallStreamPart,
|
305
|
+
dataStreamPart,
|
306
|
+
errorStreamPart,
|
307
|
+
assistantMessageStreamPart,
|
308
|
+
assistantControlDataStreamPart,
|
309
|
+
dataMessageStreamPart,
|
310
|
+
toolCallStreamPart,
|
311
|
+
messageAnnotationsStreamPart
|
312
|
+
];
|
313
|
+
var streamPartsByCode = {
|
314
|
+
[textStreamPart.code]: textStreamPart,
|
315
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
316
|
+
[dataStreamPart.code]: dataStreamPart,
|
317
|
+
[errorStreamPart.code]: errorStreamPart,
|
318
|
+
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
319
|
+
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
320
|
+
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
321
|
+
[toolCallStreamPart.code]: toolCallStreamPart,
|
322
|
+
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
|
323
|
+
};
|
324
|
+
var StreamStringPrefixes = {
|
325
|
+
[textStreamPart.name]: textStreamPart.code,
|
326
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
327
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
328
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
329
|
+
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
330
|
+
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
331
|
+
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
332
|
+
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
333
|
+
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
|
334
|
+
};
|
335
|
+
var validCodes = streamParts.map((part) => part.code);
|
336
|
+
var parseStreamPart = (line) => {
|
337
|
+
const firstSeparatorIndex = line.indexOf(":");
|
338
|
+
if (firstSeparatorIndex === -1) {
|
339
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
340
|
+
}
|
341
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
342
|
+
if (!validCodes.includes(prefix)) {
|
343
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
344
|
+
}
|
345
|
+
const code = prefix;
|
346
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
347
|
+
const jsonValue = JSON.parse(textValue);
|
348
|
+
return streamPartsByCode[code].parse(jsonValue);
|
349
|
+
};
|
350
|
+
function formatStreamPart(type, value) {
|
351
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
352
|
+
if (!streamPart) {
|
353
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
354
|
+
}
|
355
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
356
|
+
`;
|
357
|
+
}
|
358
|
+
|
359
|
+
// shared/utils.ts
|
360
|
+
var nanoid = customAlphabet(
|
361
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
362
|
+
7
|
363
|
+
);
|
364
|
+
function createChunkDecoder(complex) {
|
365
|
+
const decoder = new TextDecoder();
|
366
|
+
if (!complex) {
|
367
|
+
return function(chunk) {
|
368
|
+
if (!chunk)
|
369
|
+
return "";
|
370
|
+
return decoder.decode(chunk, { stream: true });
|
371
|
+
};
|
372
|
+
}
|
373
|
+
return function(chunk) {
|
374
|
+
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
375
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
376
|
+
};
|
377
|
+
}
|
378
|
+
|
379
|
+
// streams/ai-stream.ts
|
380
|
+
import {
|
381
|
+
createParser
|
382
|
+
} from "eventsource-parser";
|
383
|
+
function createEventStreamTransformer(customParser) {
|
384
|
+
const textDecoder = new TextDecoder();
|
385
|
+
let eventSourceParser;
|
386
|
+
return new TransformStream({
|
387
|
+
async start(controller) {
|
388
|
+
eventSourceParser = createParser(
|
389
|
+
(event) => {
|
390
|
+
if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
|
391
|
+
// @see https://replicate.com/docs/streaming
|
392
|
+
event.event === "done") {
|
393
|
+
controller.terminate();
|
394
|
+
return;
|
395
|
+
}
|
396
|
+
if ("data" in event) {
|
397
|
+
const parsedMessage = customParser ? customParser(event.data, {
|
398
|
+
event: event.event
|
399
|
+
}) : event.data;
|
400
|
+
if (parsedMessage)
|
401
|
+
controller.enqueue(parsedMessage);
|
402
|
+
}
|
403
|
+
}
|
404
|
+
);
|
405
|
+
},
|
406
|
+
transform(chunk) {
|
407
|
+
eventSourceParser.feed(textDecoder.decode(chunk));
|
408
|
+
}
|
409
|
+
});
|
410
|
+
}
|
411
|
+
function createCallbacksTransformer(cb) {
|
412
|
+
const textEncoder = new TextEncoder();
|
413
|
+
let aggregatedResponse = "";
|
414
|
+
const callbacks = cb || {};
|
415
|
+
return new TransformStream({
|
416
|
+
async start() {
|
417
|
+
if (callbacks.onStart)
|
418
|
+
await callbacks.onStart();
|
419
|
+
},
|
420
|
+
async transform(message, controller) {
|
421
|
+
controller.enqueue(textEncoder.encode(message));
|
422
|
+
aggregatedResponse += message;
|
423
|
+
if (callbacks.onToken)
|
424
|
+
await callbacks.onToken(message);
|
425
|
+
},
|
426
|
+
async flush() {
|
427
|
+
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
428
|
+
if (callbacks.onCompletion) {
|
429
|
+
await callbacks.onCompletion(aggregatedResponse);
|
430
|
+
}
|
431
|
+
if (callbacks.onFinal && !isOpenAICallbacks) {
|
432
|
+
await callbacks.onFinal(aggregatedResponse);
|
433
|
+
}
|
434
|
+
}
|
435
|
+
});
|
436
|
+
}
|
437
|
+
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
438
|
+
return "experimental_onFunctionCall" in callbacks;
|
439
|
+
}
|
440
|
+
function trimStartOfStreamHelper() {
|
441
|
+
let isStreamStart = true;
|
442
|
+
return (text) => {
|
443
|
+
if (isStreamStart) {
|
444
|
+
text = text.trimStart();
|
445
|
+
if (text)
|
446
|
+
isStreamStart = false;
|
447
|
+
}
|
448
|
+
return text;
|
449
|
+
};
|
450
|
+
}
|
451
|
+
function AIStream(response, customParser, callbacks) {
|
452
|
+
if (!response.ok) {
|
453
|
+
if (response.body) {
|
454
|
+
const reader = response.body.getReader();
|
455
|
+
return new ReadableStream({
|
456
|
+
async start(controller) {
|
457
|
+
const { done, value } = await reader.read();
|
458
|
+
if (!done) {
|
459
|
+
const errorText = new TextDecoder().decode(value);
|
460
|
+
controller.error(new Error(`Response error: ${errorText}`));
|
461
|
+
}
|
462
|
+
}
|
463
|
+
});
|
464
|
+
} else {
|
465
|
+
return new ReadableStream({
|
466
|
+
start(controller) {
|
467
|
+
controller.error(new Error("Response error: No response body"));
|
468
|
+
}
|
469
|
+
});
|
470
|
+
}
|
471
|
+
}
|
472
|
+
const responseBodyStream = response.body || createEmptyReadableStream();
|
473
|
+
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
474
|
+
}
|
475
|
+
function createEmptyReadableStream() {
|
476
|
+
return new ReadableStream({
|
477
|
+
start(controller) {
|
478
|
+
controller.close();
|
479
|
+
}
|
480
|
+
});
|
481
|
+
}
|
482
|
+
function readableFromAsyncIterable(iterable) {
|
483
|
+
let it = iterable[Symbol.asyncIterator]();
|
484
|
+
return new ReadableStream({
|
485
|
+
async pull(controller) {
|
486
|
+
const { done, value } = await it.next();
|
487
|
+
if (done)
|
488
|
+
controller.close();
|
489
|
+
else
|
490
|
+
controller.enqueue(value);
|
491
|
+
},
|
492
|
+
async cancel(reason) {
|
493
|
+
var _a;
|
494
|
+
await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
|
495
|
+
}
|
496
|
+
});
|
497
|
+
}
|
498
|
+
|
499
|
+
// streams/stream-data.ts
|
500
|
+
function createStreamDataTransformer(experimental_streamData) {
|
501
|
+
if (!experimental_streamData) {
|
502
|
+
return new TransformStream({
|
503
|
+
transform: async (chunk, controller) => {
|
504
|
+
controller.enqueue(chunk);
|
505
|
+
}
|
506
|
+
});
|
507
|
+
}
|
508
|
+
const encoder = new TextEncoder();
|
509
|
+
const decoder = new TextDecoder();
|
510
|
+
return new TransformStream({
|
511
|
+
transform: async (chunk, controller) => {
|
512
|
+
const message = decoder.decode(chunk);
|
513
|
+
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
514
|
+
}
|
515
|
+
});
|
516
|
+
}
|
517
|
+
|
518
|
+
// streams/openai-stream.ts
|
519
|
+
function parseOpenAIStream() {
|
520
|
+
const extract = chunkToText();
|
521
|
+
return (data) => extract(JSON.parse(data));
|
522
|
+
}
|
523
|
+
async function* streamable(stream) {
|
524
|
+
const extract = chunkToText();
|
525
|
+
for await (let chunk of stream) {
|
526
|
+
if ("promptFilterResults" in chunk) {
|
527
|
+
chunk = {
|
528
|
+
id: chunk.id,
|
529
|
+
created: chunk.created.getDate(),
|
530
|
+
object: chunk.object,
|
531
|
+
// not exposed by Azure API
|
532
|
+
model: chunk.model,
|
533
|
+
// not exposed by Azure API
|
534
|
+
choices: chunk.choices.map((choice) => {
|
535
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
536
|
+
return {
|
537
|
+
delta: {
|
538
|
+
content: (_a = choice.delta) == null ? void 0 : _a.content,
|
539
|
+
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
540
|
+
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
541
|
+
tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
|
542
|
+
index,
|
543
|
+
id: toolCall.id,
|
544
|
+
function: toolCall.function,
|
545
|
+
type: toolCall.type
|
546
|
+
})) : void 0
|
547
|
+
},
|
548
|
+
finish_reason: choice.finishReason,
|
549
|
+
index: choice.index
|
550
|
+
};
|
551
|
+
})
|
552
|
+
};
|
553
|
+
}
|
554
|
+
const text = extract(chunk);
|
555
|
+
if (text)
|
556
|
+
yield text;
|
557
|
+
}
|
558
|
+
}
|
559
|
+
function chunkToText() {
|
560
|
+
const trimStartOfStream = trimStartOfStreamHelper();
|
561
|
+
let isFunctionStreamingIn;
|
562
|
+
return (json) => {
|
563
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
564
|
+
if (isChatCompletionChunk(json)) {
|
565
|
+
const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
|
566
|
+
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
567
|
+
isFunctionStreamingIn = true;
|
568
|
+
return `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`;
|
569
|
+
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
570
|
+
isFunctionStreamingIn = true;
|
571
|
+
const toolCall = delta.tool_calls[0];
|
572
|
+
if (toolCall.index === 0) {
|
573
|
+
return `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`;
|
574
|
+
} else {
|
575
|
+
return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`;
|
576
|
+
}
|
577
|
+
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
578
|
+
return cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments);
|
579
|
+
} else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
|
580
|
+
return cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments);
|
581
|
+
} else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
|
582
|
+
isFunctionStreamingIn = false;
|
583
|
+
return '"}}';
|
584
|
+
} else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
|
585
|
+
isFunctionStreamingIn = false;
|
586
|
+
return '"}}]}';
|
587
|
+
}
|
588
|
+
}
|
589
|
+
const text = trimStartOfStream(
|
590
|
+
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
591
|
+
);
|
592
|
+
return text;
|
593
|
+
};
|
594
|
+
function cleanupArguments(argumentChunk) {
|
595
|
+
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
596
|
+
return `${escapedPartialJson}`;
|
597
|
+
}
|
598
|
+
}
|
599
|
+
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
600
|
+
"internal_openai_fn_messages"
|
601
|
+
);
|
602
|
+
function isChatCompletionChunk(data) {
|
603
|
+
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
604
|
+
}
|
605
|
+
function isCompletion(data) {
|
606
|
+
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
607
|
+
}
|
608
|
+
function OpenAIStream(res, callbacks) {
|
609
|
+
const cb = callbacks;
|
610
|
+
let stream;
|
611
|
+
if (Symbol.asyncIterator in res) {
|
612
|
+
stream = readableFromAsyncIterable(streamable(res)).pipeThrough(
|
613
|
+
createCallbacksTransformer(
|
614
|
+
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
615
|
+
...cb,
|
616
|
+
onFinal: void 0
|
617
|
+
} : {
|
618
|
+
...cb
|
619
|
+
}
|
620
|
+
)
|
621
|
+
);
|
622
|
+
} else {
|
623
|
+
stream = AIStream(
|
624
|
+
res,
|
625
|
+
parseOpenAIStream(),
|
626
|
+
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
627
|
+
...cb,
|
628
|
+
onFinal: void 0
|
629
|
+
} : {
|
630
|
+
...cb
|
631
|
+
}
|
632
|
+
);
|
633
|
+
}
|
634
|
+
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
635
|
+
const functionCallTransformer = createFunctionCallTransformer(cb);
|
636
|
+
return stream.pipeThrough(functionCallTransformer);
|
637
|
+
} else {
|
638
|
+
return stream.pipeThrough(
|
639
|
+
createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData)
|
640
|
+
);
|
641
|
+
}
|
642
|
+
}
|
643
|
+
function createFunctionCallTransformer(callbacks) {
|
644
|
+
const textEncoder = new TextEncoder();
|
645
|
+
let isFirstChunk = true;
|
646
|
+
let aggregatedResponse = "";
|
647
|
+
let aggregatedFinalCompletionResponse = "";
|
648
|
+
let isFunctionStreamingIn = false;
|
649
|
+
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
650
|
+
const isComplexMode = callbacks == null ? void 0 : callbacks.experimental_streamData;
|
651
|
+
const decode = createChunkDecoder();
|
652
|
+
return new TransformStream({
|
653
|
+
async transform(chunk, controller) {
|
654
|
+
const message = decode(chunk);
|
655
|
+
aggregatedFinalCompletionResponse += message;
|
656
|
+
const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
|
657
|
+
if (shouldHandleAsFunction) {
|
658
|
+
isFunctionStreamingIn = true;
|
659
|
+
aggregatedResponse += message;
|
660
|
+
isFirstChunk = false;
|
661
|
+
return;
|
662
|
+
}
|
663
|
+
if (!isFunctionStreamingIn) {
|
664
|
+
controller.enqueue(
|
665
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", message)) : chunk
|
666
|
+
);
|
667
|
+
return;
|
668
|
+
} else {
|
669
|
+
aggregatedResponse += message;
|
670
|
+
}
|
671
|
+
},
|
672
|
+
async flush(controller) {
|
673
|
+
try {
|
674
|
+
if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
|
675
|
+
isFunctionStreamingIn = false;
|
676
|
+
const payload = JSON.parse(aggregatedResponse);
|
677
|
+
let newFunctionCallMessages = [
|
678
|
+
...functionCallMessages
|
679
|
+
];
|
680
|
+
let functionResponse = void 0;
|
681
|
+
if (callbacks.experimental_onFunctionCall) {
|
682
|
+
if (payload.function_call === void 0) {
|
683
|
+
console.warn(
|
684
|
+
"experimental_onFunctionCall should not be defined when using tools"
|
685
|
+
);
|
686
|
+
}
|
687
|
+
const argumentsPayload = JSON.parse(
|
688
|
+
payload.function_call.arguments
|
689
|
+
);
|
690
|
+
functionResponse = await callbacks.experimental_onFunctionCall(
|
691
|
+
{
|
692
|
+
name: payload.function_call.name,
|
693
|
+
arguments: argumentsPayload
|
694
|
+
},
|
695
|
+
(result) => {
|
696
|
+
newFunctionCallMessages = [
|
697
|
+
...functionCallMessages,
|
698
|
+
{
|
699
|
+
role: "assistant",
|
700
|
+
content: "",
|
701
|
+
function_call: payload.function_call
|
702
|
+
},
|
703
|
+
{
|
704
|
+
role: "function",
|
705
|
+
name: payload.function_call.name,
|
706
|
+
content: JSON.stringify(result)
|
707
|
+
}
|
708
|
+
];
|
709
|
+
return newFunctionCallMessages;
|
710
|
+
}
|
711
|
+
);
|
712
|
+
}
|
713
|
+
if (callbacks.experimental_onToolCall) {
|
714
|
+
const toolCalls = {
|
715
|
+
tools: []
|
716
|
+
};
|
717
|
+
for (const tool of payload.tool_calls) {
|
718
|
+
toolCalls.tools.push({
|
719
|
+
id: tool.id,
|
720
|
+
type: "function",
|
721
|
+
func: {
|
722
|
+
name: tool.function.name,
|
723
|
+
arguments: tool.function.arguments
|
724
|
+
}
|
725
|
+
});
|
726
|
+
}
|
727
|
+
let responseIndex = 0;
|
728
|
+
try {
|
729
|
+
functionResponse = await callbacks.experimental_onToolCall(
|
730
|
+
toolCalls,
|
731
|
+
(result) => {
|
732
|
+
if (result) {
|
733
|
+
const { tool_call_id, function_name, tool_call_result } = result;
|
734
|
+
newFunctionCallMessages = [
|
735
|
+
...newFunctionCallMessages,
|
736
|
+
// Only append the assistant message if it's the first response
|
737
|
+
...responseIndex === 0 ? [
|
738
|
+
{
|
739
|
+
role: "assistant",
|
740
|
+
content: "",
|
741
|
+
tool_calls: payload.tool_calls.map(
|
742
|
+
(tc) => ({
|
743
|
+
id: tc.id,
|
744
|
+
type: "function",
|
745
|
+
function: {
|
746
|
+
name: tc.function.name,
|
747
|
+
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
748
|
+
arguments: JSON.stringify(
|
749
|
+
tc.function.arguments
|
750
|
+
)
|
751
|
+
}
|
752
|
+
})
|
753
|
+
)
|
754
|
+
}
|
755
|
+
] : [],
|
756
|
+
// Append the function call result message
|
757
|
+
{
|
758
|
+
role: "tool",
|
759
|
+
tool_call_id,
|
760
|
+
name: function_name,
|
761
|
+
content: JSON.stringify(tool_call_result)
|
762
|
+
}
|
763
|
+
];
|
764
|
+
responseIndex++;
|
765
|
+
}
|
766
|
+
return newFunctionCallMessages;
|
767
|
+
}
|
768
|
+
);
|
769
|
+
} catch (e) {
|
770
|
+
console.error("Error calling experimental_onToolCall:", e);
|
771
|
+
}
|
772
|
+
}
|
773
|
+
if (!functionResponse) {
|
774
|
+
controller.enqueue(
|
775
|
+
textEncoder.encode(
|
776
|
+
isComplexMode ? formatStreamPart(
|
777
|
+
payload.function_call ? "function_call" : "tool_calls",
|
778
|
+
// parse to prevent double-encoding:
|
779
|
+
JSON.parse(aggregatedResponse)
|
780
|
+
) : aggregatedResponse
|
781
|
+
)
|
782
|
+
);
|
783
|
+
return;
|
784
|
+
} else if (typeof functionResponse === "string") {
|
785
|
+
controller.enqueue(
|
786
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", functionResponse)) : textEncoder.encode(functionResponse)
|
787
|
+
);
|
788
|
+
aggregatedFinalCompletionResponse = functionResponse;
|
789
|
+
return;
|
790
|
+
}
|
791
|
+
const filteredCallbacks = {
|
792
|
+
...callbacks,
|
793
|
+
onStart: void 0
|
794
|
+
};
|
795
|
+
callbacks.onFinal = void 0;
|
796
|
+
const openAIStream = OpenAIStream(functionResponse, {
|
797
|
+
...filteredCallbacks,
|
798
|
+
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
799
|
+
});
|
800
|
+
const reader = openAIStream.getReader();
|
801
|
+
while (true) {
|
802
|
+
const { done, value } = await reader.read();
|
803
|
+
if (done) {
|
804
|
+
break;
|
805
|
+
}
|
806
|
+
controller.enqueue(value);
|
807
|
+
}
|
808
|
+
}
|
809
|
+
} finally {
|
810
|
+
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
|
811
|
+
await callbacks.onFinal(aggregatedFinalCompletionResponse);
|
812
|
+
}
|
813
|
+
}
|
814
|
+
}
|
815
|
+
});
|
816
|
+
}
|
817
|
+
|
818
|
+
// rsc/constants.ts
|
819
|
+
var STREAMABLE_VALUE_TYPE = Symbol.for("ui.streamable.value");
|
820
|
+
|
821
|
+
// rsc/streamable.tsx
|
822
|
+
import { Fragment as Fragment2, jsxs as jsxs2 } from "react/jsx-runtime";
|
823
|
+
function createStreamableUI(initialValue) {
|
824
|
+
let currentValue = initialValue;
|
825
|
+
let closed = false;
|
826
|
+
let { row, resolve, reject } = createSuspensedChunk(initialValue);
|
827
|
+
function assertStream() {
|
828
|
+
if (closed) {
|
829
|
+
throw new Error("UI stream is already closed.");
|
830
|
+
}
|
831
|
+
}
|
832
|
+
return {
|
833
|
+
value: row,
|
834
|
+
update(value) {
|
835
|
+
assertStream();
|
836
|
+
const resolvable = createResolvablePromise();
|
837
|
+
resolve({ value, done: false, next: resolvable.promise });
|
838
|
+
resolve = resolvable.resolve;
|
839
|
+
reject = resolvable.reject;
|
840
|
+
currentValue = value;
|
841
|
+
},
|
842
|
+
append(value) {
|
843
|
+
assertStream();
|
844
|
+
const resolvable = createResolvablePromise();
|
845
|
+
resolve({ value, done: false, next: resolvable.promise });
|
846
|
+
resolve = resolvable.resolve;
|
847
|
+
reject = resolvable.reject;
|
848
|
+
if (typeof currentValue === "string" && typeof value === "string") {
|
849
|
+
currentValue += value;
|
850
|
+
} else {
|
851
|
+
currentValue = /* @__PURE__ */ jsxs2(Fragment2, { children: [
|
852
|
+
currentValue,
|
853
|
+
value
|
854
|
+
] });
|
855
|
+
}
|
856
|
+
},
|
857
|
+
error(error) {
|
858
|
+
assertStream();
|
859
|
+
closed = true;
|
860
|
+
reject(error);
|
861
|
+
},
|
862
|
+
done(...args) {
|
863
|
+
assertStream();
|
864
|
+
closed = true;
|
865
|
+
if (args.length) {
|
866
|
+
resolve({ value: args[0], done: true });
|
867
|
+
return;
|
868
|
+
}
|
869
|
+
resolve({ value: currentValue, done: true });
|
870
|
+
}
|
871
|
+
};
|
872
|
+
}
|
873
|
+
function createStreamableValue(initialValue) {
|
874
|
+
let closed = false;
|
875
|
+
let { promise, resolve, reject } = createResolvablePromise();
|
876
|
+
function assertStream() {
|
877
|
+
if (closed) {
|
878
|
+
throw new Error("Value stream is already closed.");
|
879
|
+
}
|
880
|
+
}
|
881
|
+
function createWrapped(val, initial) {
|
882
|
+
if (initial) {
|
883
|
+
return {
|
884
|
+
type: STREAMABLE_VALUE_TYPE,
|
885
|
+
curr: val,
|
886
|
+
next: promise
|
887
|
+
};
|
888
|
+
}
|
889
|
+
return {
|
890
|
+
curr: val,
|
891
|
+
next: promise
|
892
|
+
};
|
893
|
+
}
|
894
|
+
return {
|
895
|
+
value: createWrapped(initialValue, true),
|
896
|
+
update(value) {
|
897
|
+
assertStream();
|
898
|
+
const resolvePrevious = resolve;
|
899
|
+
const resolvable = createResolvablePromise();
|
900
|
+
promise = resolvable.promise;
|
901
|
+
resolve = resolvable.resolve;
|
902
|
+
reject = resolvable.reject;
|
903
|
+
resolvePrevious(createWrapped(value));
|
904
|
+
},
|
905
|
+
error(error) {
|
906
|
+
assertStream();
|
907
|
+
closed = true;
|
908
|
+
reject(error);
|
909
|
+
},
|
910
|
+
done(...args) {
|
911
|
+
assertStream();
|
912
|
+
closed = true;
|
913
|
+
if (args.length) {
|
914
|
+
resolve({ curr: args[0] });
|
915
|
+
return;
|
916
|
+
}
|
917
|
+
resolve({});
|
918
|
+
}
|
919
|
+
};
|
920
|
+
}
|
921
|
+
function render(options) {
|
922
|
+
const ui = createStreamableUI(options.initial);
|
923
|
+
const functions = options.functions ? Object.entries(options.functions).map(
|
924
|
+
([name, { description, parameters }]) => {
|
925
|
+
return {
|
926
|
+
name,
|
927
|
+
description,
|
928
|
+
parameters: zodToJsonSchema(parameters)
|
929
|
+
};
|
930
|
+
}
|
931
|
+
) : void 0;
|
932
|
+
const tools = options.tools ? Object.entries(options.tools).map(
|
933
|
+
([name, { description, parameters }]) => {
|
934
|
+
return {
|
935
|
+
type: "function",
|
936
|
+
function: {
|
937
|
+
name,
|
938
|
+
description,
|
939
|
+
parameters: zodToJsonSchema(parameters)
|
940
|
+
}
|
941
|
+
};
|
942
|
+
}
|
943
|
+
) : void 0;
|
944
|
+
let finished;
|
945
|
+
async function handleRender(args, renderer, res) {
|
946
|
+
if (!renderer)
|
947
|
+
return;
|
948
|
+
if (finished)
|
949
|
+
await finished.promise;
|
950
|
+
finished = createResolvablePromise();
|
951
|
+
const value = renderer(args);
|
952
|
+
if (value instanceof Promise || value && typeof value === "object" && "then" in value && typeof value.then === "function") {
|
953
|
+
const node = await value;
|
954
|
+
res.update(node);
|
955
|
+
finished == null ? void 0 : finished.resolve(void 0);
|
956
|
+
} else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
|
957
|
+
for await (const node of value) {
|
958
|
+
res.update(node);
|
959
|
+
}
|
960
|
+
finished == null ? void 0 : finished.resolve(void 0);
|
961
|
+
} else if (value && typeof value === "object" && Symbol.iterator in value) {
|
962
|
+
const it = value;
|
963
|
+
while (true) {
|
964
|
+
const { done, value: value2 } = it.next();
|
965
|
+
if (done)
|
966
|
+
break;
|
967
|
+
res.update(value2);
|
968
|
+
}
|
969
|
+
finished == null ? void 0 : finished.resolve(void 0);
|
970
|
+
} else {
|
971
|
+
res.update(value);
|
972
|
+
finished == null ? void 0 : finished.resolve(void 0);
|
973
|
+
}
|
974
|
+
}
|
975
|
+
(async () => {
|
976
|
+
let hasFunction = false;
|
977
|
+
let text = "";
|
978
|
+
consumeStream(
|
979
|
+
OpenAIStream(
|
980
|
+
await options.provider.chat.completions.create({
|
981
|
+
model: options.model,
|
982
|
+
messages: options.messages,
|
983
|
+
temperature: options.temperature,
|
984
|
+
stream: true,
|
985
|
+
...functions ? {
|
986
|
+
functions
|
987
|
+
} : {},
|
988
|
+
...tools ? {
|
989
|
+
tools
|
990
|
+
} : {}
|
991
|
+
}),
|
992
|
+
{
|
993
|
+
async experimental_onFunctionCall(functionCallPayload) {
|
994
|
+
var _a, _b;
|
995
|
+
hasFunction = true;
|
996
|
+
handleRender(
|
997
|
+
functionCallPayload.arguments,
|
998
|
+
(_b = (_a = options.functions) == null ? void 0 : _a[functionCallPayload.name]) == null ? void 0 : _b.render,
|
999
|
+
ui
|
1000
|
+
);
|
1001
|
+
},
|
1002
|
+
...tools ? {
|
1003
|
+
async experimental_onToolCall(toolCallPayload) {
|
1004
|
+
var _a, _b;
|
1005
|
+
hasFunction = true;
|
1006
|
+
for (const tool of toolCallPayload.tools) {
|
1007
|
+
handleRender(
|
1008
|
+
tool.func.arguments,
|
1009
|
+
(_b = (_a = options.tools) == null ? void 0 : _a[tool.func.name]) == null ? void 0 : _b.render,
|
1010
|
+
ui
|
1011
|
+
);
|
1012
|
+
}
|
1013
|
+
}
|
1014
|
+
} : {},
|
1015
|
+
onToken(token) {
|
1016
|
+
text += token;
|
1017
|
+
if (hasFunction)
|
1018
|
+
return;
|
1019
|
+
handleRender({ content: text, done: false }, options.text, ui);
|
1020
|
+
},
|
1021
|
+
async onFinal() {
|
1022
|
+
if (hasFunction)
|
1023
|
+
return;
|
1024
|
+
handleRender({ content: text, done: true }, options.text, ui);
|
1025
|
+
await (finished == null ? void 0 : finished.promise);
|
1026
|
+
ui.done();
|
1027
|
+
}
|
1028
|
+
}
|
1029
|
+
)
|
1030
|
+
);
|
1031
|
+
})();
|
1032
|
+
return ui.value;
|
1033
|
+
}
|
1034
|
+
|
1035
|
+
// rsc/provider.tsx
|
1036
|
+
import * as React2 from "react";
|
1037
|
+
import { InternalAIProvider } from "./rsc-shared";
|
1038
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
1039
|
+
async function innerAction({
|
1040
|
+
action,
|
1041
|
+
options
|
1042
|
+
}, state, ...args) {
|
1043
|
+
"use server";
|
1044
|
+
return await withAIState(
|
1045
|
+
{
|
1046
|
+
state,
|
1047
|
+
options
|
1048
|
+
},
|
1049
|
+
async () => {
|
1050
|
+
const result = await action(...args);
|
1051
|
+
sealMutableAIState();
|
1052
|
+
return [getAIStateDeltaPromise(), result];
|
1053
|
+
}
|
1054
|
+
);
|
1055
|
+
}
|
1056
|
+
function wrapAction(action, options) {
|
1057
|
+
return innerAction.bind(null, { action, options });
|
1058
|
+
}
|
1059
|
+
function createAI({
|
1060
|
+
actions,
|
1061
|
+
initialAIState,
|
1062
|
+
initialUIState,
|
1063
|
+
unstable_onSetAIState: onSetAIState,
|
1064
|
+
unstable_onGetUIState: onGetUIState
|
1065
|
+
}) {
|
1066
|
+
const wrappedActions = {};
|
1067
|
+
for (const name in actions) {
|
1068
|
+
wrappedActions[name] = wrapAction(actions[name], {
|
1069
|
+
onSetAIState
|
1070
|
+
});
|
1071
|
+
}
|
1072
|
+
const wrappedSyncUIState = onGetUIState ? wrapAction(onGetUIState, {}) : void 0;
|
1073
|
+
async function AI(props) {
|
1074
|
+
var _a, _b;
|
1075
|
+
if ("useState" in React2) {
|
1076
|
+
throw new Error(
|
1077
|
+
"This component can only be used inside Server Components."
|
1078
|
+
);
|
1079
|
+
}
|
1080
|
+
let uiState = (_a = props.initialUIState) != null ? _a : initialUIState;
|
1081
|
+
let aiState = (_b = props.initialAIState) != null ? _b : initialAIState;
|
1082
|
+
let aiStateDelta = void 0;
|
1083
|
+
if (wrappedSyncUIState) {
|
1084
|
+
const [newAIStateDelta, newUIState] = await wrappedSyncUIState(aiState);
|
1085
|
+
if (newUIState !== void 0) {
|
1086
|
+
aiStateDelta = newAIStateDelta;
|
1087
|
+
uiState = newUIState;
|
1088
|
+
}
|
1089
|
+
}
|
1090
|
+
return /* @__PURE__ */ jsx2(
|
1091
|
+
InternalAIProvider,
|
1092
|
+
{
|
1093
|
+
wrappedActions,
|
1094
|
+
wrappedSyncUIState,
|
1095
|
+
initialUIState: uiState,
|
1096
|
+
initialAIState: aiState,
|
1097
|
+
initialAIStatePatch: aiStateDelta,
|
1098
|
+
children: props.children
|
1099
|
+
}
|
1100
|
+
);
|
1101
|
+
}
|
1102
|
+
return AI;
|
1103
|
+
}
|
1104
|
+
export {
|
1105
|
+
createAI,
|
1106
|
+
createStreamableUI,
|
1107
|
+
createStreamableValue,
|
1108
|
+
getAIState,
|
1109
|
+
getMutableAIState,
|
1110
|
+
render
|
1111
|
+
};
|
1112
|
+
//# sourceMappingURL=rsc-server.mjs.map
|