ai 3.4.33 → 4.0.0-canary.1
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/CHANGELOG.md +28 -0
- package/dist/index.d.mts +7 -475
- package/dist/index.d.ts +7 -475
- package/dist/index.js +94 -771
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -746
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -55
- package/rsc/dist/index.d.ts +5 -67
- package/rsc/dist/rsc-server.d.mts +5 -67
- package/rsc/dist/rsc-server.mjs +19 -623
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/prompts/dist/index.d.mts +0 -220
- package/prompts/dist/index.d.ts +0 -220
- package/prompts/dist/index.js +0 -178
- package/prompts/dist/index.js.map +0 -1
- package/prompts/dist/index.mjs +0 -146
- package/prompts/dist/index.mjs.map +0 -1
- package/solid/dist/index.d.mts +0 -13
- package/solid/dist/index.d.ts +0 -13
- package/solid/dist/index.js +0 -35
- package/solid/dist/index.js.map +0 -1
- package/solid/dist/index.mjs +0 -12
- package/solid/dist/index.mjs.map +0 -1
- package/svelte/dist/index.d.mts +0 -175
- package/svelte/dist/index.d.ts +0 -175
- package/svelte/dist/index.js +0 -1161
- package/svelte/dist/index.js.map +0 -1
- package/svelte/dist/index.mjs +0 -1136
- package/svelte/dist/index.mjs.map +0 -1
- package/vue/dist/index.d.mts +0 -17
- package/vue/dist/index.d.ts +0 -17
- package/vue/dist/index.js +0 -38
- package/vue/dist/index.js.map +0 -1
- package/vue/dist/index.mjs +0 -15
- package/vue/dist/index.mjs.map +0 -1
package/rsc/dist/rsc-server.mjs
CHANGED
@@ -1516,13 +1516,13 @@ function createStreamableUI(initialValue) {
|
|
1516
1516
|
}
|
1517
1517
|
}
|
1518
1518
|
warnUnclosedStream();
|
1519
|
-
const
|
1519
|
+
const streamable = {
|
1520
1520
|
value: row,
|
1521
1521
|
update(value) {
|
1522
1522
|
assertStream(".update()");
|
1523
1523
|
if (value === currentValue) {
|
1524
1524
|
warnUnclosedStream();
|
1525
|
-
return
|
1525
|
+
return streamable;
|
1526
1526
|
}
|
1527
1527
|
const resolvable = createResolvablePromise();
|
1528
1528
|
currentValue = value;
|
@@ -1530,7 +1530,7 @@ function createStreamableUI(initialValue) {
|
|
1530
1530
|
resolve = resolvable.resolve;
|
1531
1531
|
reject = resolvable.reject;
|
1532
1532
|
warnUnclosedStream();
|
1533
|
-
return
|
1533
|
+
return streamable;
|
1534
1534
|
},
|
1535
1535
|
append(value) {
|
1536
1536
|
assertStream(".append()");
|
@@ -1540,7 +1540,7 @@ function createStreamableUI(initialValue) {
|
|
1540
1540
|
resolve = resolvable.resolve;
|
1541
1541
|
reject = resolvable.reject;
|
1542
1542
|
warnUnclosedStream();
|
1543
|
-
return
|
1543
|
+
return streamable;
|
1544
1544
|
},
|
1545
1545
|
error(error) {
|
1546
1546
|
assertStream(".error()");
|
@@ -1549,7 +1549,7 @@ function createStreamableUI(initialValue) {
|
|
1549
1549
|
}
|
1550
1550
|
closed = true;
|
1551
1551
|
reject(error);
|
1552
|
-
return
|
1552
|
+
return streamable;
|
1553
1553
|
},
|
1554
1554
|
done(...args) {
|
1555
1555
|
assertStream(".done()");
|
@@ -1559,13 +1559,13 @@ function createStreamableUI(initialValue) {
|
|
1559
1559
|
closed = true;
|
1560
1560
|
if (args.length) {
|
1561
1561
|
resolve({ value: args[0], done: true });
|
1562
|
-
return
|
1562
|
+
return streamable;
|
1563
1563
|
}
|
1564
1564
|
resolve({ value: currentValue, done: true });
|
1565
|
-
return
|
1565
|
+
return streamable;
|
1566
1566
|
}
|
1567
1567
|
};
|
1568
|
-
return
|
1568
|
+
return streamable;
|
1569
1569
|
}
|
1570
1570
|
|
1571
1571
|
// rsc/stream-ui/stream-ui.tsx
|
@@ -1614,7 +1614,7 @@ async function streamUI({
|
|
1614
1614
|
const textRender = text || defaultTextRenderer;
|
1615
1615
|
let finished;
|
1616
1616
|
let finishEvent = null;
|
1617
|
-
async function
|
1617
|
+
async function render({
|
1618
1618
|
args,
|
1619
1619
|
renderer,
|
1620
1620
|
streamableUI,
|
@@ -1688,7 +1688,7 @@ async function streamUI({
|
|
1688
1688
|
switch (value.type) {
|
1689
1689
|
case "text-delta": {
|
1690
1690
|
content += value.textDelta;
|
1691
|
-
|
1691
|
+
render({
|
1692
1692
|
renderer: textRender,
|
1693
1693
|
args: [{ content, done: false, delta: value.textDelta }],
|
1694
1694
|
streamableUI: ui
|
@@ -1723,7 +1723,7 @@ async function streamUI({
|
|
1723
1723
|
cause: parseResult.error
|
1724
1724
|
});
|
1725
1725
|
}
|
1726
|
-
|
1726
|
+
render({
|
1727
1727
|
renderer: tool.generate,
|
1728
1728
|
args: [
|
1729
1729
|
parseResult.value,
|
@@ -1752,7 +1752,7 @@ async function streamUI({
|
|
1752
1752
|
}
|
1753
1753
|
}
|
1754
1754
|
if (!hasToolCall) {
|
1755
|
-
|
1755
|
+
render({
|
1756
1756
|
renderer: textRender,
|
1757
1757
|
args: [{ content, done: true }],
|
1758
1758
|
streamableUI: ui,
|
@@ -1777,609 +1777,6 @@ async function streamUI({
|
|
1777
1777
|
};
|
1778
1778
|
}
|
1779
1779
|
|
1780
|
-
// rsc/stream-ui/render.ts
|
1781
|
-
import zodToJsonSchema from "zod-to-json-schema";
|
1782
|
-
|
1783
|
-
// streams/ai-stream.ts
|
1784
|
-
import {
|
1785
|
-
createParser
|
1786
|
-
} from "eventsource-parser";
|
1787
|
-
function createEventStreamTransformer(customParser) {
|
1788
|
-
const textDecoder = new TextDecoder();
|
1789
|
-
let eventSourceParser;
|
1790
|
-
return new TransformStream({
|
1791
|
-
async start(controller) {
|
1792
|
-
eventSourceParser = createParser(
|
1793
|
-
(event) => {
|
1794
|
-
if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
|
1795
|
-
// @see https://replicate.com/docs/streaming
|
1796
|
-
event.event === "done") {
|
1797
|
-
controller.terminate();
|
1798
|
-
return;
|
1799
|
-
}
|
1800
|
-
if ("data" in event) {
|
1801
|
-
const parsedMessage = customParser ? customParser(event.data, {
|
1802
|
-
event: event.event
|
1803
|
-
}) : event.data;
|
1804
|
-
if (parsedMessage)
|
1805
|
-
controller.enqueue(parsedMessage);
|
1806
|
-
}
|
1807
|
-
}
|
1808
|
-
);
|
1809
|
-
},
|
1810
|
-
transform(chunk) {
|
1811
|
-
eventSourceParser.feed(textDecoder.decode(chunk));
|
1812
|
-
}
|
1813
|
-
});
|
1814
|
-
}
|
1815
|
-
function createCallbacksTransformer(cb) {
|
1816
|
-
const textEncoder = new TextEncoder();
|
1817
|
-
let aggregatedResponse = "";
|
1818
|
-
const callbacks = cb || {};
|
1819
|
-
return new TransformStream({
|
1820
|
-
async start() {
|
1821
|
-
if (callbacks.onStart)
|
1822
|
-
await callbacks.onStart();
|
1823
|
-
},
|
1824
|
-
async transform(message, controller) {
|
1825
|
-
const content = typeof message === "string" ? message : message.content;
|
1826
|
-
controller.enqueue(textEncoder.encode(content));
|
1827
|
-
aggregatedResponse += content;
|
1828
|
-
if (callbacks.onToken)
|
1829
|
-
await callbacks.onToken(content);
|
1830
|
-
if (callbacks.onText && typeof message === "string") {
|
1831
|
-
await callbacks.onText(message);
|
1832
|
-
}
|
1833
|
-
},
|
1834
|
-
async flush() {
|
1835
|
-
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
1836
|
-
if (callbacks.onCompletion) {
|
1837
|
-
await callbacks.onCompletion(aggregatedResponse);
|
1838
|
-
}
|
1839
|
-
if (callbacks.onFinal && !isOpenAICallbacks) {
|
1840
|
-
await callbacks.onFinal(aggregatedResponse);
|
1841
|
-
}
|
1842
|
-
}
|
1843
|
-
});
|
1844
|
-
}
|
1845
|
-
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
1846
|
-
return "experimental_onFunctionCall" in callbacks;
|
1847
|
-
}
|
1848
|
-
function trimStartOfStreamHelper() {
|
1849
|
-
let isStreamStart = true;
|
1850
|
-
return (text) => {
|
1851
|
-
if (isStreamStart) {
|
1852
|
-
text = text.trimStart();
|
1853
|
-
if (text)
|
1854
|
-
isStreamStart = false;
|
1855
|
-
}
|
1856
|
-
return text;
|
1857
|
-
};
|
1858
|
-
}
|
1859
|
-
function AIStream(response, customParser, callbacks) {
|
1860
|
-
if (!response.ok) {
|
1861
|
-
if (response.body) {
|
1862
|
-
const reader = response.body.getReader();
|
1863
|
-
return new ReadableStream({
|
1864
|
-
async start(controller) {
|
1865
|
-
const { done, value } = await reader.read();
|
1866
|
-
if (!done) {
|
1867
|
-
const errorText = new TextDecoder().decode(value);
|
1868
|
-
controller.error(new Error(`Response error: ${errorText}`));
|
1869
|
-
}
|
1870
|
-
}
|
1871
|
-
});
|
1872
|
-
} else {
|
1873
|
-
return new ReadableStream({
|
1874
|
-
start(controller) {
|
1875
|
-
controller.error(new Error("Response error: No response body"));
|
1876
|
-
}
|
1877
|
-
});
|
1878
|
-
}
|
1879
|
-
}
|
1880
|
-
const responseBodyStream = response.body || createEmptyReadableStream();
|
1881
|
-
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
1882
|
-
}
|
1883
|
-
function createEmptyReadableStream() {
|
1884
|
-
return new ReadableStream({
|
1885
|
-
start(controller) {
|
1886
|
-
controller.close();
|
1887
|
-
}
|
1888
|
-
});
|
1889
|
-
}
|
1890
|
-
function readableFromAsyncIterable(iterable) {
|
1891
|
-
let it = iterable[Symbol.asyncIterator]();
|
1892
|
-
return new ReadableStream({
|
1893
|
-
async pull(controller) {
|
1894
|
-
const { done, value } = await it.next();
|
1895
|
-
if (done)
|
1896
|
-
controller.close();
|
1897
|
-
else
|
1898
|
-
controller.enqueue(value);
|
1899
|
-
},
|
1900
|
-
async cancel(reason) {
|
1901
|
-
var _a9;
|
1902
|
-
await ((_a9 = it.return) == null ? void 0 : _a9.call(it, reason));
|
1903
|
-
}
|
1904
|
-
});
|
1905
|
-
}
|
1906
|
-
|
1907
|
-
// streams/stream-data.ts
|
1908
|
-
import { formatStreamPart } from "@ai-sdk/ui-utils";
|
1909
|
-
function createStreamDataTransformer() {
|
1910
|
-
const encoder = new TextEncoder();
|
1911
|
-
const decoder = new TextDecoder();
|
1912
|
-
return new TransformStream({
|
1913
|
-
transform: async (chunk, controller) => {
|
1914
|
-
const message = decoder.decode(chunk);
|
1915
|
-
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
1916
|
-
}
|
1917
|
-
});
|
1918
|
-
}
|
1919
|
-
|
1920
|
-
// streams/openai-stream.ts
|
1921
|
-
import {
|
1922
|
-
createChunkDecoder,
|
1923
|
-
formatStreamPart as formatStreamPart2
|
1924
|
-
} from "@ai-sdk/ui-utils";
|
1925
|
-
function parseOpenAIStream() {
|
1926
|
-
const extract = chunkToText();
|
1927
|
-
return (data) => extract(JSON.parse(data));
|
1928
|
-
}
|
1929
|
-
async function* streamable(stream) {
|
1930
|
-
const extract = chunkToText();
|
1931
|
-
for await (let chunk of stream) {
|
1932
|
-
if ("promptFilterResults" in chunk) {
|
1933
|
-
chunk = {
|
1934
|
-
id: chunk.id,
|
1935
|
-
created: chunk.created.getDate(),
|
1936
|
-
object: chunk.object,
|
1937
|
-
// not exposed by Azure API
|
1938
|
-
model: chunk.model,
|
1939
|
-
// not exposed by Azure API
|
1940
|
-
choices: chunk.choices.map((choice) => {
|
1941
|
-
var _a9, _b, _c, _d, _e, _f, _g;
|
1942
|
-
return {
|
1943
|
-
delta: {
|
1944
|
-
content: (_a9 = choice.delta) == null ? void 0 : _a9.content,
|
1945
|
-
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
1946
|
-
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
1947
|
-
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) => ({
|
1948
|
-
index,
|
1949
|
-
id: toolCall.id,
|
1950
|
-
function: toolCall.function,
|
1951
|
-
type: toolCall.type
|
1952
|
-
})) : void 0
|
1953
|
-
},
|
1954
|
-
finish_reason: choice.finishReason,
|
1955
|
-
index: choice.index
|
1956
|
-
};
|
1957
|
-
})
|
1958
|
-
};
|
1959
|
-
}
|
1960
|
-
const text = extract(chunk);
|
1961
|
-
if (text)
|
1962
|
-
yield text;
|
1963
|
-
}
|
1964
|
-
}
|
1965
|
-
function chunkToText() {
|
1966
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
1967
|
-
let isFunctionStreamingIn;
|
1968
|
-
return (json) => {
|
1969
|
-
var _a9, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
1970
|
-
if (isChatCompletionChunk(json)) {
|
1971
|
-
const delta = (_a9 = json.choices[0]) == null ? void 0 : _a9.delta;
|
1972
|
-
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
1973
|
-
isFunctionStreamingIn = true;
|
1974
|
-
return {
|
1975
|
-
isText: false,
|
1976
|
-
content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
|
1977
|
-
};
|
1978
|
-
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
1979
|
-
isFunctionStreamingIn = true;
|
1980
|
-
const toolCall = delta.tool_calls[0];
|
1981
|
-
if (toolCall.index === 0) {
|
1982
|
-
return {
|
1983
|
-
isText: false,
|
1984
|
-
content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
|
1985
|
-
};
|
1986
|
-
} else {
|
1987
|
-
return {
|
1988
|
-
isText: false,
|
1989
|
-
content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
|
1990
|
-
};
|
1991
|
-
}
|
1992
|
-
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
1993
|
-
return {
|
1994
|
-
isText: false,
|
1995
|
-
content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
|
1996
|
-
};
|
1997
|
-
} else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
|
1998
|
-
return {
|
1999
|
-
isText: false,
|
2000
|
-
content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
|
2001
|
-
};
|
2002
|
-
} 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")) {
|
2003
|
-
isFunctionStreamingIn = false;
|
2004
|
-
return {
|
2005
|
-
isText: false,
|
2006
|
-
content: '"}}'
|
2007
|
-
};
|
2008
|
-
} else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
|
2009
|
-
isFunctionStreamingIn = false;
|
2010
|
-
return {
|
2011
|
-
isText: false,
|
2012
|
-
content: '"}}]}'
|
2013
|
-
};
|
2014
|
-
}
|
2015
|
-
}
|
2016
|
-
const text = trimStartOfStream(
|
2017
|
-
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
2018
|
-
);
|
2019
|
-
return text;
|
2020
|
-
};
|
2021
|
-
function cleanupArguments(argumentChunk) {
|
2022
|
-
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");
|
2023
|
-
return `${escapedPartialJson}`;
|
2024
|
-
}
|
2025
|
-
}
|
2026
|
-
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
2027
|
-
"internal_openai_fn_messages"
|
2028
|
-
);
|
2029
|
-
function isChatCompletionChunk(data) {
|
2030
|
-
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
2031
|
-
}
|
2032
|
-
function isCompletion(data) {
|
2033
|
-
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
2034
|
-
}
|
2035
|
-
function OpenAIStream(res, callbacks) {
|
2036
|
-
const cb = callbacks;
|
2037
|
-
let stream;
|
2038
|
-
if (Symbol.asyncIterator in res) {
|
2039
|
-
stream = readableFromAsyncIterable(streamable(res)).pipeThrough(
|
2040
|
-
createCallbacksTransformer(
|
2041
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
2042
|
-
...cb,
|
2043
|
-
onFinal: void 0
|
2044
|
-
} : {
|
2045
|
-
...cb
|
2046
|
-
}
|
2047
|
-
)
|
2048
|
-
);
|
2049
|
-
} else {
|
2050
|
-
stream = AIStream(
|
2051
|
-
res,
|
2052
|
-
parseOpenAIStream(),
|
2053
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
2054
|
-
...cb,
|
2055
|
-
onFinal: void 0
|
2056
|
-
} : {
|
2057
|
-
...cb
|
2058
|
-
}
|
2059
|
-
);
|
2060
|
-
}
|
2061
|
-
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
2062
|
-
const functionCallTransformer = createFunctionCallTransformer(cb);
|
2063
|
-
return stream.pipeThrough(functionCallTransformer);
|
2064
|
-
} else {
|
2065
|
-
return stream.pipeThrough(createStreamDataTransformer());
|
2066
|
-
}
|
2067
|
-
}
|
2068
|
-
function createFunctionCallTransformer(callbacks) {
|
2069
|
-
const textEncoder = new TextEncoder();
|
2070
|
-
let isFirstChunk = true;
|
2071
|
-
let aggregatedResponse = "";
|
2072
|
-
let aggregatedFinalCompletionResponse = "";
|
2073
|
-
let isFunctionStreamingIn = false;
|
2074
|
-
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
2075
|
-
const decode = createChunkDecoder();
|
2076
|
-
return new TransformStream({
|
2077
|
-
async transform(chunk, controller) {
|
2078
|
-
const message = decode(chunk);
|
2079
|
-
aggregatedFinalCompletionResponse += message;
|
2080
|
-
const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
|
2081
|
-
if (shouldHandleAsFunction) {
|
2082
|
-
isFunctionStreamingIn = true;
|
2083
|
-
aggregatedResponse += message;
|
2084
|
-
isFirstChunk = false;
|
2085
|
-
return;
|
2086
|
-
}
|
2087
|
-
if (!isFunctionStreamingIn) {
|
2088
|
-
controller.enqueue(
|
2089
|
-
textEncoder.encode(formatStreamPart2("text", message))
|
2090
|
-
);
|
2091
|
-
return;
|
2092
|
-
} else {
|
2093
|
-
aggregatedResponse += message;
|
2094
|
-
}
|
2095
|
-
},
|
2096
|
-
async flush(controller) {
|
2097
|
-
try {
|
2098
|
-
if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
|
2099
|
-
isFunctionStreamingIn = false;
|
2100
|
-
const payload = JSON.parse(aggregatedResponse);
|
2101
|
-
let newFunctionCallMessages = [
|
2102
|
-
...functionCallMessages
|
2103
|
-
];
|
2104
|
-
let functionResponse = void 0;
|
2105
|
-
if (callbacks.experimental_onFunctionCall) {
|
2106
|
-
if (payload.function_call === void 0) {
|
2107
|
-
console.warn(
|
2108
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
2109
|
-
);
|
2110
|
-
}
|
2111
|
-
const argumentsPayload = JSON.parse(
|
2112
|
-
payload.function_call.arguments
|
2113
|
-
);
|
2114
|
-
functionResponse = await callbacks.experimental_onFunctionCall(
|
2115
|
-
{
|
2116
|
-
name: payload.function_call.name,
|
2117
|
-
arguments: argumentsPayload
|
2118
|
-
},
|
2119
|
-
(result) => {
|
2120
|
-
newFunctionCallMessages = [
|
2121
|
-
...functionCallMessages,
|
2122
|
-
{
|
2123
|
-
role: "assistant",
|
2124
|
-
content: "",
|
2125
|
-
function_call: payload.function_call
|
2126
|
-
},
|
2127
|
-
{
|
2128
|
-
role: "function",
|
2129
|
-
name: payload.function_call.name,
|
2130
|
-
content: JSON.stringify(result)
|
2131
|
-
}
|
2132
|
-
];
|
2133
|
-
return newFunctionCallMessages;
|
2134
|
-
}
|
2135
|
-
);
|
2136
|
-
}
|
2137
|
-
if (callbacks.experimental_onToolCall) {
|
2138
|
-
const toolCalls = {
|
2139
|
-
tools: []
|
2140
|
-
};
|
2141
|
-
for (const tool of payload.tool_calls) {
|
2142
|
-
toolCalls.tools.push({
|
2143
|
-
id: tool.id,
|
2144
|
-
type: "function",
|
2145
|
-
func: {
|
2146
|
-
name: tool.function.name,
|
2147
|
-
arguments: JSON.parse(tool.function.arguments)
|
2148
|
-
}
|
2149
|
-
});
|
2150
|
-
}
|
2151
|
-
let responseIndex = 0;
|
2152
|
-
try {
|
2153
|
-
functionResponse = await callbacks.experimental_onToolCall(
|
2154
|
-
toolCalls,
|
2155
|
-
(result) => {
|
2156
|
-
if (result) {
|
2157
|
-
const { tool_call_id, function_name, tool_call_result } = result;
|
2158
|
-
newFunctionCallMessages = [
|
2159
|
-
...newFunctionCallMessages,
|
2160
|
-
// Only append the assistant message if it's the first response
|
2161
|
-
...responseIndex === 0 ? [
|
2162
|
-
{
|
2163
|
-
role: "assistant",
|
2164
|
-
content: "",
|
2165
|
-
tool_calls: payload.tool_calls.map(
|
2166
|
-
(tc) => ({
|
2167
|
-
id: tc.id,
|
2168
|
-
type: "function",
|
2169
|
-
function: {
|
2170
|
-
name: tc.function.name,
|
2171
|
-
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
2172
|
-
arguments: JSON.stringify(
|
2173
|
-
tc.function.arguments
|
2174
|
-
)
|
2175
|
-
}
|
2176
|
-
})
|
2177
|
-
)
|
2178
|
-
}
|
2179
|
-
] : [],
|
2180
|
-
// Append the function call result message
|
2181
|
-
{
|
2182
|
-
role: "tool",
|
2183
|
-
tool_call_id,
|
2184
|
-
name: function_name,
|
2185
|
-
content: JSON.stringify(tool_call_result)
|
2186
|
-
}
|
2187
|
-
];
|
2188
|
-
responseIndex++;
|
2189
|
-
}
|
2190
|
-
return newFunctionCallMessages;
|
2191
|
-
}
|
2192
|
-
);
|
2193
|
-
} catch (e) {
|
2194
|
-
console.error("Error calling experimental_onToolCall:", e);
|
2195
|
-
}
|
2196
|
-
}
|
2197
|
-
if (!functionResponse) {
|
2198
|
-
controller.enqueue(
|
2199
|
-
textEncoder.encode(
|
2200
|
-
formatStreamPart2(
|
2201
|
-
payload.function_call ? "function_call" : "tool_calls",
|
2202
|
-
// parse to prevent double-encoding:
|
2203
|
-
JSON.parse(aggregatedResponse)
|
2204
|
-
)
|
2205
|
-
)
|
2206
|
-
);
|
2207
|
-
return;
|
2208
|
-
} else if (typeof functionResponse === "string") {
|
2209
|
-
controller.enqueue(
|
2210
|
-
textEncoder.encode(formatStreamPart2("text", functionResponse))
|
2211
|
-
);
|
2212
|
-
aggregatedFinalCompletionResponse = functionResponse;
|
2213
|
-
return;
|
2214
|
-
}
|
2215
|
-
const filteredCallbacks = {
|
2216
|
-
...callbacks,
|
2217
|
-
onStart: void 0
|
2218
|
-
};
|
2219
|
-
callbacks.onFinal = void 0;
|
2220
|
-
const openAIStream = OpenAIStream(functionResponse, {
|
2221
|
-
...filteredCallbacks,
|
2222
|
-
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
2223
|
-
});
|
2224
|
-
const reader = openAIStream.getReader();
|
2225
|
-
while (true) {
|
2226
|
-
const { done, value } = await reader.read();
|
2227
|
-
if (done) {
|
2228
|
-
break;
|
2229
|
-
}
|
2230
|
-
controller.enqueue(value);
|
2231
|
-
}
|
2232
|
-
}
|
2233
|
-
} finally {
|
2234
|
-
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
|
2235
|
-
await callbacks.onFinal(aggregatedFinalCompletionResponse);
|
2236
|
-
}
|
2237
|
-
}
|
2238
|
-
}
|
2239
|
-
});
|
2240
|
-
}
|
2241
|
-
|
2242
|
-
// util/consume-stream.ts
|
2243
|
-
async function consumeStream(stream) {
|
2244
|
-
const reader = stream.getReader();
|
2245
|
-
while (true) {
|
2246
|
-
const { done } = await reader.read();
|
2247
|
-
if (done)
|
2248
|
-
break;
|
2249
|
-
}
|
2250
|
-
}
|
2251
|
-
|
2252
|
-
// rsc/stream-ui/render.ts
|
2253
|
-
function render(options) {
|
2254
|
-
const ui = createStreamableUI(options.initial);
|
2255
|
-
const text = options.text ? options.text : ({ content }) => content;
|
2256
|
-
const functions = options.functions ? Object.entries(options.functions).map(
|
2257
|
-
([name9, { description, parameters }]) => {
|
2258
|
-
return {
|
2259
|
-
name: name9,
|
2260
|
-
description,
|
2261
|
-
parameters: zodToJsonSchema(parameters)
|
2262
|
-
};
|
2263
|
-
}
|
2264
|
-
) : void 0;
|
2265
|
-
const tools = options.tools ? Object.entries(options.tools).map(
|
2266
|
-
([name9, { description, parameters }]) => {
|
2267
|
-
return {
|
2268
|
-
type: "function",
|
2269
|
-
function: {
|
2270
|
-
name: name9,
|
2271
|
-
description,
|
2272
|
-
parameters: zodToJsonSchema(parameters)
|
2273
|
-
}
|
2274
|
-
};
|
2275
|
-
}
|
2276
|
-
) : void 0;
|
2277
|
-
if (functions && tools) {
|
2278
|
-
throw new Error(
|
2279
|
-
"You can't have both functions and tools defined. Please choose one or the other."
|
2280
|
-
);
|
2281
|
-
}
|
2282
|
-
let finished;
|
2283
|
-
async function handleRender(args, renderer, res) {
|
2284
|
-
if (!renderer)
|
2285
|
-
return;
|
2286
|
-
const resolvable = createResolvablePromise();
|
2287
|
-
if (finished) {
|
2288
|
-
finished = finished.then(() => resolvable.promise);
|
2289
|
-
} else {
|
2290
|
-
finished = resolvable.promise;
|
2291
|
-
}
|
2292
|
-
const value = renderer(args);
|
2293
|
-
if (value instanceof Promise || value && typeof value === "object" && "then" in value && typeof value.then === "function") {
|
2294
|
-
const node = await value;
|
2295
|
-
res.update(node);
|
2296
|
-
resolvable.resolve(void 0);
|
2297
|
-
} else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
|
2298
|
-
const it = value;
|
2299
|
-
while (true) {
|
2300
|
-
const { done, value: value2 } = await it.next();
|
2301
|
-
res.update(value2);
|
2302
|
-
if (done)
|
2303
|
-
break;
|
2304
|
-
}
|
2305
|
-
resolvable.resolve(void 0);
|
2306
|
-
} else if (value && typeof value === "object" && Symbol.iterator in value) {
|
2307
|
-
const it = value;
|
2308
|
-
while (true) {
|
2309
|
-
const { done, value: value2 } = it.next();
|
2310
|
-
res.update(value2);
|
2311
|
-
if (done)
|
2312
|
-
break;
|
2313
|
-
}
|
2314
|
-
resolvable.resolve(void 0);
|
2315
|
-
} else {
|
2316
|
-
res.update(value);
|
2317
|
-
resolvable.resolve(void 0);
|
2318
|
-
}
|
2319
|
-
}
|
2320
|
-
(async () => {
|
2321
|
-
let hasFunction = false;
|
2322
|
-
let content = "";
|
2323
|
-
consumeStream(
|
2324
|
-
OpenAIStream(
|
2325
|
-
await options.provider.chat.completions.create({
|
2326
|
-
model: options.model,
|
2327
|
-
messages: options.messages,
|
2328
|
-
temperature: options.temperature,
|
2329
|
-
stream: true,
|
2330
|
-
...functions ? {
|
2331
|
-
functions
|
2332
|
-
} : {},
|
2333
|
-
...tools ? {
|
2334
|
-
tools
|
2335
|
-
} : {}
|
2336
|
-
}),
|
2337
|
-
{
|
2338
|
-
...functions ? {
|
2339
|
-
async experimental_onFunctionCall(functionCallPayload) {
|
2340
|
-
var _a9, _b;
|
2341
|
-
hasFunction = true;
|
2342
|
-
handleRender(
|
2343
|
-
functionCallPayload.arguments,
|
2344
|
-
(_b = (_a9 = options.functions) == null ? void 0 : _a9[functionCallPayload.name]) == null ? void 0 : _b.render,
|
2345
|
-
ui
|
2346
|
-
);
|
2347
|
-
}
|
2348
|
-
} : {},
|
2349
|
-
...tools ? {
|
2350
|
-
async experimental_onToolCall(toolCallPayload) {
|
2351
|
-
var _a9, _b;
|
2352
|
-
hasFunction = true;
|
2353
|
-
for (const tool of toolCallPayload.tools) {
|
2354
|
-
handleRender(
|
2355
|
-
tool.func.arguments,
|
2356
|
-
(_b = (_a9 = options.tools) == null ? void 0 : _a9[tool.func.name]) == null ? void 0 : _b.render,
|
2357
|
-
ui
|
2358
|
-
);
|
2359
|
-
}
|
2360
|
-
}
|
2361
|
-
} : {},
|
2362
|
-
onText(chunk) {
|
2363
|
-
content += chunk;
|
2364
|
-
handleRender({ content, done: false, delta: chunk }, text, ui);
|
2365
|
-
},
|
2366
|
-
async onFinal() {
|
2367
|
-
if (hasFunction) {
|
2368
|
-
await finished;
|
2369
|
-
ui.done();
|
2370
|
-
return;
|
2371
|
-
}
|
2372
|
-
handleRender({ content, done: true }, text, ui);
|
2373
|
-
await finished;
|
2374
|
-
ui.done();
|
2375
|
-
}
|
2376
|
-
}
|
2377
|
-
)
|
2378
|
-
);
|
2379
|
-
})();
|
2380
|
-
return ui.value;
|
2381
|
-
}
|
2382
|
-
|
2383
1780
|
// rsc/streamable-value/streamable-value.ts
|
2384
1781
|
var STREAMABLE_VALUE_TYPE = Symbol.for("ui.streamable.value");
|
2385
1782
|
|
@@ -2479,7 +1876,7 @@ function createStreamableValueImpl(initialValue) {
|
|
2479
1876
|
}
|
2480
1877
|
currentValue = value;
|
2481
1878
|
}
|
2482
|
-
const
|
1879
|
+
const streamable = {
|
2483
1880
|
set [STREAMABLE_VALUE_INTERNAL_LOCK](state) {
|
2484
1881
|
locked = state;
|
2485
1882
|
},
|
@@ -2494,7 +1891,7 @@ function createStreamableValueImpl(initialValue) {
|
|
2494
1891
|
currentPromise = resolvable.promise;
|
2495
1892
|
resolvePrevious(createWrapped());
|
2496
1893
|
warnUnclosedStream();
|
2497
|
-
return
|
1894
|
+
return streamable;
|
2498
1895
|
},
|
2499
1896
|
append(value) {
|
2500
1897
|
assertStream(".append()");
|
@@ -2520,7 +1917,7 @@ function createStreamableValueImpl(initialValue) {
|
|
2520
1917
|
currentPromise = resolvable.promise;
|
2521
1918
|
resolvePrevious(createWrapped());
|
2522
1919
|
warnUnclosedStream();
|
2523
|
-
return
|
1920
|
+
return streamable;
|
2524
1921
|
},
|
2525
1922
|
error(error) {
|
2526
1923
|
assertStream(".error()");
|
@@ -2531,7 +1928,7 @@ function createStreamableValueImpl(initialValue) {
|
|
2531
1928
|
currentError = error;
|
2532
1929
|
currentPromise = void 0;
|
2533
1930
|
resolvable.resolve({ error });
|
2534
|
-
return
|
1931
|
+
return streamable;
|
2535
1932
|
},
|
2536
1933
|
done(...args) {
|
2537
1934
|
assertStream(".done()");
|
@@ -2543,13 +1940,13 @@ function createStreamableValueImpl(initialValue) {
|
|
2543
1940
|
if (args.length) {
|
2544
1941
|
updateValueStates(args[0]);
|
2545
1942
|
resolvable.resolve(createWrapped());
|
2546
|
-
return
|
1943
|
+
return streamable;
|
2547
1944
|
}
|
2548
1945
|
resolvable.resolve({});
|
2549
|
-
return
|
1946
|
+
return streamable;
|
2550
1947
|
}
|
2551
1948
|
};
|
2552
|
-
return
|
1949
|
+
return streamable;
|
2553
1950
|
}
|
2554
1951
|
export {
|
2555
1952
|
createAI,
|
@@ -2557,7 +1954,6 @@ export {
|
|
2557
1954
|
createStreamableValue,
|
2558
1955
|
getAIState,
|
2559
1956
|
getMutableAIState,
|
2560
|
-
render,
|
2561
1957
|
streamUI
|
2562
1958
|
};
|
2563
1959
|
//# sourceMappingURL=rsc-server.mjs.map
|