Comparing version 2.1.9 to 2.1.10
@@ -68,3 +68,5 @@ "use strict"; | ||
createCallbacksTransformer: () => createCallbacksTransformer, | ||
createChunkDecoder: () => createChunkDecoder, | ||
createEventStreamTransformer: () => createEventStreamTransformer, | ||
nanoid: () => nanoid, | ||
streamToResponse: () => streamToResponse, | ||
@@ -163,6 +165,15 @@ trimStartOfStreamHelper: () => trimStartOfStreamHelper | ||
return (data) => { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n; | ||
const json = JSON.parse(data); | ||
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) { | ||
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`; | ||
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) { | ||
const argumentChunk = json.choices[0].delta.function_call.arguments; | ||
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"); | ||
return `${escapedPartialJson}`; | ||
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") { | ||
return '"}}'; | ||
} | ||
const text = trimStartOfStream( | ||
(_e = (_d = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content) != null ? _d : (_c = json.choices[0]) == null ? void 0 : _c.text) != null ? _e : "" | ||
(_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : "" | ||
); | ||
@@ -298,2 +309,17 @@ return text; | ||
} | ||
// shared/utils.ts | ||
var import_nanoid = require("nanoid"); | ||
var nanoid = (0, import_nanoid.customAlphabet)( | ||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | ||
7 | ||
); | ||
function createChunkDecoder() { | ||
const decoder = new TextDecoder(); | ||
return function(chunk) { | ||
if (!chunk) | ||
return ""; | ||
return decoder.decode(chunk, { stream: true }); | ||
}; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -309,5 +335,7 @@ 0 && (module.exports = { | ||
createCallbacksTransformer, | ||
createChunkDecoder, | ||
createEventStreamTransformer, | ||
nanoid, | ||
streamToResponse, | ||
trimStartOfStreamHelper | ||
}); |
import { ServerResponse } from 'node:http'; | ||
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge'; | ||
import { ChatCompletionFunctions } from 'openai-edge/types/api'; | ||
@@ -112,10 +114,25 @@ /** | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
role: 'system' | 'user' | 'assistant' | 'function'; | ||
/** | ||
* If the message has a role of `function`, the `name` field is the name of the function. | ||
* Otherwise, the name field should not be set. | ||
*/ | ||
name?: string; | ||
/** | ||
* If the assistant role makes a function call, the `function_call` field | ||
* contains the function call name and arguments. Otherwise, the field should | ||
* not be set. | ||
*/ | ||
function_call?: string | ChatCompletionRequestMessageFunctionCall; | ||
}; | ||
type CreateMessage = { | ||
id?: string; | ||
createdAt?: Date; | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
type CreateMessage = Omit<Message, 'id'> & { | ||
id?: Message['id']; | ||
}; | ||
type ChatRequest = { | ||
messages: Message[]; | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>; | ||
type RequestOptions = { | ||
@@ -125,2 +142,7 @@ headers?: Record<string, string> | Headers; | ||
}; | ||
type ChatRequestOptions = { | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type UseChatOptions = { | ||
@@ -133,3 +155,3 @@ /** | ||
/** | ||
* An unique identifier for the chat. If not provided, a random one will be | ||
* A unique identifier for the chat. If not provided, a random one will be | ||
* generated. When provided, the `useChat` hook with the same `id` will | ||
@@ -148,2 +170,8 @@ * have shared states across components. | ||
/** | ||
* Callback function to be called when a function call is received. | ||
* If the function returns a `ChatRequest` object, the request will be sent | ||
* automatically to the API and will be used to update the chat. | ||
*/ | ||
experimental_onFunctionCall?: FunctionCallHandler; | ||
/** | ||
* Callback function to be called when the API response is received. | ||
@@ -161,2 +189,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -218,2 +252,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -237,2 +277,5 @@ */ | ||
export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, CohereStream, CreateMessage, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createEventStreamTransformer, streamToResponse, trimStartOfStreamHelper }; | ||
declare const nanoid: (size?: number | undefined) => string; | ||
declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string; | ||
export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, ChatRequest, ChatRequestOptions, CohereStream, CreateMessage, FunctionCallHandler, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, nanoid, streamToResponse, trimStartOfStreamHelper }; |
@@ -68,3 +68,5 @@ "use strict"; | ||
createCallbacksTransformer: () => createCallbacksTransformer, | ||
createChunkDecoder: () => createChunkDecoder, | ||
createEventStreamTransformer: () => createEventStreamTransformer, | ||
nanoid: () => nanoid, | ||
streamToResponse: () => streamToResponse, | ||
@@ -163,6 +165,15 @@ trimStartOfStreamHelper: () => trimStartOfStreamHelper | ||
return (data) => { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n; | ||
const json = JSON.parse(data); | ||
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) { | ||
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`; | ||
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) { | ||
const argumentChunk = json.choices[0].delta.function_call.arguments; | ||
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"); | ||
return `${escapedPartialJson}`; | ||
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") { | ||
return '"}}'; | ||
} | ||
const text = trimStartOfStream( | ||
(_e = (_d = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content) != null ? _d : (_c = json.choices[0]) == null ? void 0 : _c.text) != null ? _e : "" | ||
(_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : "" | ||
); | ||
@@ -298,2 +309,17 @@ return text; | ||
} | ||
// shared/utils.ts | ||
var import_nanoid = require("nanoid"); | ||
var nanoid = (0, import_nanoid.customAlphabet)( | ||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | ||
7 | ||
); | ||
function createChunkDecoder() { | ||
const decoder = new TextDecoder(); | ||
return function(chunk) { | ||
if (!chunk) | ||
return ""; | ||
return decoder.decode(chunk, { stream: true }); | ||
}; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -309,5 +335,7 @@ 0 && (module.exports = { | ||
createCallbacksTransformer, | ||
createChunkDecoder, | ||
createEventStreamTransformer, | ||
nanoid, | ||
streamToResponse, | ||
trimStartOfStreamHelper | ||
}); |
{ | ||
"name": "ai", | ||
"version": "2.1.9", | ||
"version": "2.1.10", | ||
"license": "Apache-2.0", | ||
@@ -48,5 +48,5 @@ "sideEffects": false, | ||
"eventsource-parser": "1.0.0", | ||
"swr": "2.1.5", | ||
"nanoid": "^3.3.6", | ||
"sswr": "^1.10.0", | ||
"swr": "2.1.5", | ||
"swrv": "1.0.3" | ||
@@ -62,11 +62,12 @@ }, | ||
"jest": "29.2.1", | ||
"openai-edge": "^1.1.0", | ||
"ts-jest": "29.0.3", | ||
"tsup": "^6.7.0", | ||
"typescript": "5.1.3", | ||
"@vercel/ai-tsconfig": "0.0.0", | ||
"eslint-config-vercel-ai": "0.0.0" | ||
"eslint-config-vercel-ai": "0.0.0", | ||
"@vercel/ai-tsconfig": "0.0.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.2.0", | ||
"svelte": "^3.29.0", | ||
"svelte": "^4.0.0", | ||
"vue": "^3.3.4" | ||
@@ -73,0 +74,0 @@ }, |
@@ -0,1 +1,3 @@ | ||
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge'; | ||
import { ChatCompletionFunctions } from 'openai-edge/types/api'; | ||
import * as react_jsx_runtime from 'react/jsx-runtime'; | ||
@@ -10,10 +12,25 @@ | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
role: 'system' | 'user' | 'assistant' | 'function'; | ||
/** | ||
* If the message has a role of `function`, the `name` field is the name of the function. | ||
* Otherwise, the name field should not be set. | ||
*/ | ||
name?: string; | ||
/** | ||
* If the assistant role makes a function call, the `function_call` field | ||
* contains the function call name and arguments. Otherwise, the field should | ||
* not be set. | ||
*/ | ||
function_call?: string | ChatCompletionRequestMessageFunctionCall; | ||
}; | ||
type CreateMessage = { | ||
id?: string; | ||
createdAt?: Date; | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
type CreateMessage = Omit<Message, 'id'> & { | ||
id?: Message['id']; | ||
}; | ||
type ChatRequest = { | ||
messages: Message[]; | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>; | ||
type RequestOptions = { | ||
@@ -23,2 +40,7 @@ headers?: Record<string, string> | Headers; | ||
}; | ||
type ChatRequestOptions = { | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type UseChatOptions = { | ||
@@ -31,3 +53,3 @@ /** | ||
/** | ||
* An unique identifier for the chat. If not provided, a random one will be | ||
* A unique identifier for the chat. If not provided, a random one will be | ||
* generated. When provided, the `useChat` hook with the same `id` will | ||
@@ -46,2 +68,8 @@ * have shared states across components. | ||
/** | ||
* Callback function to be called when a function call is received. | ||
* If the function returns a `ChatRequest` object, the request will be sent | ||
* automatically to the API and will be used to update the chat. | ||
*/ | ||
experimental_onFunctionCall?: FunctionCallHandler; | ||
/** | ||
* Callback function to be called when the API response is received. | ||
@@ -59,2 +87,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -116,2 +150,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -146,3 +186,3 @@ */ | ||
*/ | ||
append: (message: Message | CreateMessage, options?: RequestOptions) => Promise<string | null | undefined>; | ||
append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>; | ||
/** | ||
@@ -153,3 +193,3 @@ * Reload the last AI chat response for the given chat history. If the last | ||
*/ | ||
reload: (options?: RequestOptions) => Promise<string | null | undefined>; | ||
reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>; | ||
/** | ||
@@ -172,7 +212,8 @@ * Abort the current request immediately, keep the generated tokens if any. | ||
/** Form submission handler to automattically reset input and append a user message */ | ||
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void; | ||
handleSubmit: (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void; | ||
metadata?: Object; | ||
/** Whether the API request is in progress */ | ||
isLoading: boolean; | ||
}; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers; | ||
@@ -221,3 +262,3 @@ type UseCompletionHelpers = { | ||
}; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
@@ -224,0 +265,0 @@ type Props = { |
@@ -95,2 +95,82 @@ 'use client' | ||
// react/use-chat.ts | ||
var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => __async(void 0, null, function* () { | ||
var _a, _b; | ||
const previousMessages = messagesRef.current; | ||
mutate(chatRequest.messages, false); | ||
const res = yield fetch(api, __spreadValues({ | ||
method: "POST", | ||
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues({ | ||
messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map( | ||
({ role, content, name, function_call }) => __spreadValues(__spreadValues({ | ||
role, | ||
content | ||
}, name !== void 0 && { name }), function_call !== void 0 && { | ||
function_call | ||
}) | ||
) | ||
}, extraMetadataRef.current.body), (_a = chatRequest.options) == null ? void 0 : _a.body), chatRequest.functions !== void 0 && { | ||
functions: chatRequest.functions | ||
}), chatRequest.function_call !== void 0 && { | ||
function_call: chatRequest.function_call | ||
})), | ||
credentials: extraMetadataRef.current.credentials, | ||
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), (_b = chatRequest.options) == null ? void 0 : _b.headers) | ||
}, abortControllerRef.current !== null && { | ||
signal: abortControllerRef.current.signal | ||
})).catch((err) => { | ||
mutate(previousMessages, false); | ||
throw err; | ||
}); | ||
if (onResponse) { | ||
try { | ||
yield onResponse(res); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
if (!res.ok) { | ||
mutate(previousMessages, false); | ||
throw new Error((yield res.text()) || "Failed to fetch the chat response."); | ||
} | ||
if (!res.body) { | ||
throw new Error("The response body is empty."); | ||
} | ||
let streamedResponse = ""; | ||
const createdAt = /* @__PURE__ */ new Date(); | ||
const replyId = nanoid(); | ||
const reader = res.body.getReader(); | ||
const decode = createChunkDecoder(); | ||
let responseMessage = { | ||
id: replyId, | ||
createdAt, | ||
content: "", | ||
role: "assistant" | ||
}; | ||
while (true) { | ||
const { done, value } = yield reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
streamedResponse += decode(value); | ||
if (streamedResponse.startsWith('{"function_call":')) { | ||
responseMessage["function_call"] = streamedResponse; | ||
} else { | ||
responseMessage["content"] = streamedResponse; | ||
} | ||
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)], false); | ||
if (abortControllerRef.current === null) { | ||
reader.cancel(); | ||
break; | ||
} | ||
} | ||
if (streamedResponse.startsWith('{"function_call":')) { | ||
const parsedFunctionCall = JSON.parse(streamedResponse).function_call; | ||
responseMessage["function_call"] = parsedFunctionCall; | ||
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)]); | ||
} | ||
if (onFinish) { | ||
onFinish(responseMessage); | ||
} | ||
return responseMessage; | ||
}); | ||
function useChat({ | ||
@@ -102,5 +182,7 @@ api = "/api/chat", | ||
sendExtraMessageFields, | ||
experimental_onFunctionCall, | ||
onResponse, | ||
onFinish, | ||
onError, | ||
credentials, | ||
headers, | ||
@@ -121,2 +203,3 @@ body | ||
const extraMetadataRef = (0, import_react.useRef)({ | ||
credentials, | ||
headers, | ||
@@ -127,83 +210,42 @@ body | ||
extraMetadataRef.current = { | ||
credentials, | ||
headers, | ||
body | ||
}; | ||
}, [headers, body]); | ||
}, [credentials, headers, body]); | ||
const { error, trigger, isMutating } = (0, import_mutation.default)( | ||
[api, chatId], | ||
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) { | ||
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) { | ||
try { | ||
const { messages: messagesSnapshot, options } = arg; | ||
const abortController = new AbortController(); | ||
abortControllerRef.current = abortController; | ||
const previousMessages = messagesRef.current; | ||
mutate(messagesSnapshot, false); | ||
const res = yield fetch(api, { | ||
method: "POST", | ||
body: JSON.stringify(__spreadValues(__spreadValues({ | ||
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({ | ||
role, | ||
content | ||
})) | ||
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)), | ||
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers), | ||
signal: abortController.signal | ||
}).catch((err) => { | ||
mutate(previousMessages, false); | ||
throw err; | ||
}); | ||
if (onResponse) { | ||
try { | ||
yield onResponse(res); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
if (!res.ok) { | ||
mutate(previousMessages, false); | ||
throw new Error( | ||
(yield res.text()) || "Failed to fetch the chat response." | ||
); | ||
} | ||
if (!res.body) { | ||
throw new Error("The response body is empty."); | ||
} | ||
let result = ""; | ||
const createdAt = /* @__PURE__ */ new Date(); | ||
const replyId = nanoid(); | ||
const reader = res.body.getReader(); | ||
const decode = createChunkDecoder(); | ||
let chatRequest = initialChatRequest; | ||
while (true) { | ||
const { done, value } = yield reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
result += decode(value); | ||
mutate( | ||
[ | ||
...messagesSnapshot, | ||
{ | ||
id: replyId, | ||
createdAt, | ||
content: result, | ||
role: "assistant" | ||
} | ||
], | ||
false | ||
const streamedResponseMessage = yield getStreamedResponse( | ||
api, | ||
chatRequest, | ||
mutate, | ||
extraMetadataRef, | ||
messagesRef, | ||
abortControllerRef, | ||
onFinish, | ||
onResponse, | ||
sendExtraMessageFields | ||
); | ||
if (abortControllerRef.current === null) { | ||
reader.cancel(); | ||
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") { | ||
break; | ||
} | ||
if (experimental_onFunctionCall) { | ||
const functionCall = streamedResponseMessage.function_call; | ||
const functionCallResponse = yield experimental_onFunctionCall( | ||
messagesRef.current, | ||
functionCall | ||
); | ||
if (functionCallResponse === void 0) | ||
break; | ||
chatRequest = functionCallResponse; | ||
} | ||
} | ||
if (onFinish) { | ||
onFinish({ | ||
id: replyId, | ||
createdAt, | ||
content: result, | ||
role: "assistant" | ||
}); | ||
} | ||
abortControllerRef.current = null; | ||
return result; | ||
return null; | ||
} catch (err) { | ||
@@ -226,10 +268,11 @@ if (err.name === "AbortError") { | ||
const append = (0, import_react.useCallback)( | ||
(message, options) => __async(this, null, function* () { | ||
(_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) { | ||
if (!message.id) { | ||
message.id = nanoid(); | ||
} | ||
return trigger({ | ||
const chatRequest = __spreadValues(__spreadValues({ | ||
messages: messagesRef.current.concat(message), | ||
options | ||
}); | ||
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call }); | ||
return trigger(chatRequest); | ||
}), | ||
@@ -239,3 +282,3 @@ [trigger] | ||
const reload = (0, import_react.useCallback)( | ||
(options) => __async(this, null, function* () { | ||
(..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) { | ||
if (messagesRef.current.length === 0) | ||
@@ -245,11 +288,13 @@ return null; | ||
if (lastMessage.role === "assistant") { | ||
return trigger({ | ||
const chatRequest2 = __spreadValues(__spreadValues({ | ||
messages: messagesRef.current.slice(0, -1), | ||
options | ||
}); | ||
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call }); | ||
return trigger(chatRequest2); | ||
} | ||
return trigger({ | ||
const chatRequest = __spreadValues(__spreadValues({ | ||
messages: messagesRef.current, | ||
options | ||
}); | ||
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call }); | ||
return trigger(chatRequest); | ||
}), | ||
@@ -273,3 +318,3 @@ [trigger] | ||
const handleSubmit = (0, import_react.useCallback)( | ||
(e, metadata) => { | ||
(e, { options, functions, function_call } = {}, metadata) => { | ||
if (metadata) { | ||
@@ -281,7 +326,10 @@ extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata); | ||
return; | ||
append({ | ||
content: input, | ||
role: "user", | ||
createdAt: /* @__PURE__ */ new Date() | ||
}); | ||
append( | ||
{ | ||
content: input, | ||
role: "user", | ||
createdAt: /* @__PURE__ */ new Date() | ||
}, | ||
{ options, functions, function_call } | ||
); | ||
setInput(""); | ||
@@ -318,2 +366,3 @@ }, | ||
initialInput = "", | ||
credentials, | ||
headers, | ||
@@ -333,2 +382,3 @@ body, | ||
const extraMetadataRef = (0, import_react2.useRef)({ | ||
credentials, | ||
headers, | ||
@@ -339,6 +389,7 @@ body | ||
extraMetadataRef.current = { | ||
credentials, | ||
headers, | ||
body | ||
}; | ||
}, [headers, body]); | ||
}, [credentials, headers, body]); | ||
const { error, trigger, isMutating } = (0, import_mutation2.default)( | ||
@@ -357,2 +408,3 @@ [api, completionId], | ||
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)), | ||
credentials: extraMetadataRef.current.credentials, | ||
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers), | ||
@@ -359,0 +411,0 @@ signal: abortController2.signal |
import { Readable, Writable } from 'svelte/store'; | ||
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge'; | ||
import { ChatCompletionFunctions } from 'openai-edge/types/api'; | ||
@@ -10,10 +12,25 @@ /** | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
role: 'system' | 'user' | 'assistant' | 'function'; | ||
/** | ||
* If the message has a role of `function`, the `name` field is the name of the function. | ||
* Otherwise, the name field should not be set. | ||
*/ | ||
name?: string; | ||
/** | ||
* If the assistant role makes a function call, the `function_call` field | ||
* contains the function call name and arguments. Otherwise, the field should | ||
* not be set. | ||
*/ | ||
function_call?: string | ChatCompletionRequestMessageFunctionCall; | ||
}; | ||
type CreateMessage = { | ||
id?: string; | ||
createdAt?: Date; | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
type CreateMessage = Omit<Message, 'id'> & { | ||
id?: Message['id']; | ||
}; | ||
type ChatRequest = { | ||
messages: Message[]; | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>; | ||
type RequestOptions = { | ||
@@ -30,3 +47,3 @@ headers?: Record<string, string> | Headers; | ||
/** | ||
* An unique identifier for the chat. If not provided, a random one will be | ||
* A unique identifier for the chat. If not provided, a random one will be | ||
* generated. When provided, the `useChat` hook with the same `id` will | ||
@@ -45,2 +62,8 @@ * have shared states across components. | ||
/** | ||
* Callback function to be called when a function call is received. | ||
* If the function returns a `ChatRequest` object, the request will be sent | ||
* automatically to the API and will be used to update the chat. | ||
*/ | ||
experimental_onFunctionCall?: FunctionCallHandler; | ||
/** | ||
* Callback function to be called when the API response is received. | ||
@@ -58,2 +81,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -115,2 +144,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -167,3 +202,3 @@ */ | ||
}; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers; | ||
@@ -202,4 +237,4 @@ type UseCompletionHelpers = { | ||
}; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion }; |
@@ -398,3 +398,3 @@ "use strict"; | ||
// ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.54.0/node_modules/sswr/dist/sswr.mjs | ||
// ../../node_modules/.pnpm/sswr@1.10.0_svelte@4.0.0/node_modules/sswr/dist/sswr.mjs | ||
var import_svelte = require("svelte"); | ||
@@ -531,2 +531,3 @@ function h() { | ||
onError, | ||
credentials, | ||
headers, | ||
@@ -566,3 +567,4 @@ body | ||
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers), | ||
signal: abortController.signal | ||
signal: abortController.signal, | ||
credentials | ||
}).catch((err) => { | ||
@@ -697,2 +699,3 @@ mutate(previousMessages); | ||
initialInput = "", | ||
credentials, | ||
headers, | ||
@@ -731,3 +734,4 @@ body, | ||
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers), | ||
signal: abortController.signal | ||
signal: abortController.signal, | ||
credentials | ||
}).catch((err) => { | ||
@@ -734,0 +738,0 @@ throw err; |
import { Ref } from 'vue'; | ||
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge'; | ||
import { ChatCompletionFunctions } from 'openai-edge/types/api'; | ||
@@ -10,10 +12,25 @@ /** | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
role: 'system' | 'user' | 'assistant' | 'function'; | ||
/** | ||
* If the message has a role of `function`, the `name` field is the name of the function. | ||
* Otherwise, the name field should not be set. | ||
*/ | ||
name?: string; | ||
/** | ||
* If the assistant role makes a function call, the `function_call` field | ||
* contains the function call name and arguments. Otherwise, the field should | ||
* not be set. | ||
*/ | ||
function_call?: string | ChatCompletionRequestMessageFunctionCall; | ||
}; | ||
type CreateMessage = { | ||
id?: string; | ||
createdAt?: Date; | ||
content: string; | ||
role: 'system' | 'user' | 'assistant'; | ||
type CreateMessage = Omit<Message, 'id'> & { | ||
id?: Message['id']; | ||
}; | ||
type ChatRequest = { | ||
messages: Message[]; | ||
options?: RequestOptions; | ||
functions?: Array<ChatCompletionFunctions>; | ||
function_call?: CreateChatCompletionRequestFunctionCall; | ||
}; | ||
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>; | ||
type RequestOptions = { | ||
@@ -30,3 +47,3 @@ headers?: Record<string, string> | Headers; | ||
/** | ||
* An unique identifier for the chat. If not provided, a random one will be | ||
* A unique identifier for the chat. If not provided, a random one will be | ||
* generated. When provided, the `useChat` hook with the same `id` will | ||
@@ -45,2 +62,8 @@ * have shared states across components. | ||
/** | ||
* Callback function to be called when a function call is received. | ||
* If the function returns a `ChatRequest` object, the request will be sent | ||
* automatically to the API and will be used to update the chat. | ||
*/ | ||
experimental_onFunctionCall?: FunctionCallHandler; | ||
/** | ||
* Callback function to be called when the API response is received. | ||
@@ -58,2 +81,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -115,2 +144,8 @@ */ | ||
/** | ||
* The credentials mode to be used for the fetch request. | ||
* Possible values are: 'omit', 'same-origin', 'include'. | ||
* Defaults to 'same-origin'. | ||
*/ | ||
credentials?: RequestCredentials; | ||
/** | ||
* HTTP headers to be sent with the API request. | ||
@@ -167,3 +202,3 @@ */ | ||
}; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers; | ||
@@ -202,4 +237,4 @@ type UseCompletionHelpers = { | ||
}; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers; | ||
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion }; |
@@ -104,2 +104,3 @@ "use strict"; | ||
onError, | ||
credentials, | ||
headers, | ||
@@ -139,3 +140,4 @@ body | ||
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers), | ||
signal: abortController.signal | ||
signal: abortController.signal, | ||
credentials | ||
}).catch((err) => { | ||
@@ -271,2 +273,3 @@ mutate(previousMessages); | ||
initialInput = "", | ||
credentials, | ||
headers, | ||
@@ -305,3 +308,4 @@ body, | ||
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers), | ||
signal: abortController.signal | ||
signal: abortController.signal, | ||
credentials | ||
}).catch((err) => { | ||
@@ -308,0 +312,0 @@ throw err; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172118
5290
13