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