ai 3.0.0 → 3.0.2
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/README.md +1 -60
- package/dist/index.d.ts +14 -3
- package/dist/index.js +35 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +35 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/rsc/dist/rsc-server.d.ts +5 -5
- package/rsc/dist/rsc-server.mjs +64 -27
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.d.ts +2 -2
package/rsc/dist/rsc-server.d.ts
CHANGED
@@ -6,12 +6,12 @@ import './rsc-shared';
|
|
6
6
|
|
7
7
|
type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
|
8
8
|
type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
|
9
|
-
type AIProviderProps<AIState = any, UIState = any> = {
|
9
|
+
type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
|
10
10
|
children: React.ReactNode;
|
11
11
|
initialAIState?: AIState;
|
12
12
|
initialUIState?: UIState;
|
13
13
|
};
|
14
|
-
type AIProvider<AIState = any, UIState = any, Actions = any> = (props: AIProviderProps<AIState, UIState>) => Promise<React.ReactElement>;
|
14
|
+
type AIProvider<AIState = any, UIState = any, Actions = any> = (props: AIProviderProps<AIState, UIState, Actions>) => Promise<React.ReactElement>;
|
15
15
|
type InferAIState<T, Fallback> = T extends AIProvider<infer AIState, any, any> ? AIState : Fallback;
|
16
16
|
type OnSetAIState<S> = ({ key, state, done, }: {
|
17
17
|
key: string | number | symbol | undefined;
|
@@ -98,12 +98,12 @@ declare function render<TS extends {
|
|
98
98
|
[name: string]: z.Schema;
|
99
99
|
} = {}>(options: {
|
100
100
|
/**
|
101
|
-
* The model name to use.
|
102
|
-
* GPT models (3.5/4)
|
101
|
+
* The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
|
102
|
+
* GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
|
103
103
|
*
|
104
104
|
* @example "gpt-3.5-turbo"
|
105
105
|
*/
|
106
|
-
model:
|
106
|
+
model: string;
|
107
107
|
/**
|
108
108
|
* The provider instance to use. Currently the only provider available is OpenAI.
|
109
109
|
* This needs to match the model name.
|
package/rsc/dist/rsc-server.mjs
CHANGED
@@ -418,10 +418,14 @@ function createCallbacksTransformer(cb) {
|
|
418
418
|
await callbacks.onStart();
|
419
419
|
},
|
420
420
|
async transform(message, controller) {
|
421
|
-
|
422
|
-
|
421
|
+
const content = typeof message === "string" ? message : message.content;
|
422
|
+
controller.enqueue(textEncoder.encode(content));
|
423
|
+
aggregatedResponse += content;
|
423
424
|
if (callbacks.onToken)
|
424
|
-
await callbacks.onToken(
|
425
|
+
await callbacks.onToken(content);
|
426
|
+
if (callbacks.onText && typeof message === "string") {
|
427
|
+
await callbacks.onText(message);
|
428
|
+
}
|
425
429
|
},
|
426
430
|
async flush() {
|
427
431
|
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
@@ -565,25 +569,46 @@ function chunkToText() {
|
|
565
569
|
const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
|
566
570
|
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
567
571
|
isFunctionStreamingIn = true;
|
568
|
-
return
|
572
|
+
return {
|
573
|
+
isText: false,
|
574
|
+
content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
|
575
|
+
};
|
569
576
|
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
570
577
|
isFunctionStreamingIn = true;
|
571
578
|
const toolCall = delta.tool_calls[0];
|
572
579
|
if (toolCall.index === 0) {
|
573
|
-
return
|
580
|
+
return {
|
581
|
+
isText: false,
|
582
|
+
content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
|
583
|
+
};
|
574
584
|
} else {
|
575
|
-
return
|
585
|
+
return {
|
586
|
+
isText: false,
|
587
|
+
content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
|
588
|
+
};
|
576
589
|
}
|
577
590
|
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
578
|
-
return
|
591
|
+
return {
|
592
|
+
isText: false,
|
593
|
+
content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
|
594
|
+
};
|
579
595
|
} 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
|
596
|
+
return {
|
597
|
+
isText: false,
|
598
|
+
content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
|
599
|
+
};
|
581
600
|
} 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
601
|
isFunctionStreamingIn = false;
|
583
|
-
return
|
602
|
+
return {
|
603
|
+
isText: false,
|
604
|
+
content: '"}}'
|
605
|
+
};
|
584
606
|
} else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
|
585
607
|
isFunctionStreamingIn = false;
|
586
|
-
return
|
608
|
+
return {
|
609
|
+
isText: false,
|
610
|
+
content: '"}}]}'
|
611
|
+
};
|
587
612
|
}
|
588
613
|
}
|
589
614
|
const text = trimStartOfStream(
|
@@ -941,6 +966,11 @@ function render(options) {
|
|
941
966
|
};
|
942
967
|
}
|
943
968
|
) : void 0;
|
969
|
+
if (functions && tools) {
|
970
|
+
throw new Error(
|
971
|
+
"You can't have both functions and tools defined. Please choose one or the other."
|
972
|
+
);
|
973
|
+
}
|
944
974
|
let finished;
|
945
975
|
async function handleRender(args, renderer, res) {
|
946
976
|
if (!renderer)
|
@@ -954,17 +984,21 @@ function render(options) {
|
|
954
984
|
res.update(node);
|
955
985
|
finished == null ? void 0 : finished.resolve(void 0);
|
956
986
|
} else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
|
957
|
-
|
958
|
-
|
987
|
+
const it = value;
|
988
|
+
while (true) {
|
989
|
+
const { done, value: value2 } = await it.next();
|
990
|
+
res.update(value2);
|
991
|
+
if (done)
|
992
|
+
break;
|
959
993
|
}
|
960
994
|
finished == null ? void 0 : finished.resolve(void 0);
|
961
995
|
} else if (value && typeof value === "object" && Symbol.iterator in value) {
|
962
996
|
const it = value;
|
963
997
|
while (true) {
|
964
998
|
const { done, value: value2 } = it.next();
|
999
|
+
res.update(value2);
|
965
1000
|
if (done)
|
966
1001
|
break;
|
967
|
-
res.update(value2);
|
968
1002
|
}
|
969
1003
|
finished == null ? void 0 : finished.resolve(void 0);
|
970
1004
|
} else {
|
@@ -990,15 +1024,17 @@ function render(options) {
|
|
990
1024
|
} : {}
|
991
1025
|
}),
|
992
1026
|
{
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1027
|
+
...functions ? {
|
1028
|
+
async experimental_onFunctionCall(functionCallPayload) {
|
1029
|
+
var _a, _b;
|
1030
|
+
hasFunction = true;
|
1031
|
+
handleRender(
|
1032
|
+
functionCallPayload.arguments,
|
1033
|
+
(_b = (_a = options.functions) == null ? void 0 : _a[functionCallPayload.name]) == null ? void 0 : _b.render,
|
1034
|
+
ui
|
1035
|
+
);
|
1036
|
+
}
|
1037
|
+
} : {},
|
1002
1038
|
...tools ? {
|
1003
1039
|
async experimental_onToolCall(toolCallPayload) {
|
1004
1040
|
var _a, _b;
|
@@ -1012,15 +1048,16 @@ function render(options) {
|
|
1012
1048
|
}
|
1013
1049
|
}
|
1014
1050
|
} : {},
|
1015
|
-
|
1016
|
-
text +=
|
1017
|
-
if (hasFunction)
|
1018
|
-
return;
|
1051
|
+
onText(chunk) {
|
1052
|
+
text += chunk;
|
1019
1053
|
handleRender({ content: text, done: false }, options.text, ui);
|
1020
1054
|
},
|
1021
1055
|
async onFinal() {
|
1022
|
-
if (hasFunction)
|
1056
|
+
if (hasFunction) {
|
1057
|
+
await (finished == null ? void 0 : finished.promise);
|
1058
|
+
ui.done();
|
1023
1059
|
return;
|
1060
|
+
}
|
1024
1061
|
handleRender({ content: text, done: true }, options.text, ui);
|
1025
1062
|
await (finished == null ? void 0 : finished.promise);
|
1026
1063
|
ui.done();
|