ai 2.0.1 → 2.1.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/dist/index.d.ts +48 -8
- package/dist/index.mjs +234 -22
- package/package.json +20 -8
- package/react/dist/index.d.ts +206 -3
- package/react/dist/index.mjs +384 -7
- package/svelte/dist/index.d.ts +194 -4
- package/svelte/dist/index.mjs +779 -7
- package/{react/dist/types-f862f74a.d.ts → vue/dist/index.d.ts} +72 -1
- package/vue/dist/index.js +384 -0
- package/vue/dist/index.mjs +349 -0
- package/dist/ai-stream.d.ts +0 -18
- package/dist/ai-stream.js +0 -132
- package/dist/ai-stream.mjs +0 -13
- package/dist/anthropic-stream.d.ts +0 -5
- package/dist/anthropic-stream.js +0 -133
- package/dist/anthropic-stream.mjs +0 -8
- package/dist/chunk-265FSSO4.mjs +0 -91
- package/dist/chunk-2L3ZO4UM.mjs +0 -45
- package/dist/chunk-GT4HKF2X.mjs +0 -33
- package/dist/chunk-JGDC3BXD.mjs +0 -22
- package/dist/chunk-NK2CVBLI.mjs +0 -38
- package/dist/chunk-PEYAHBDF.mjs +0 -43
- package/dist/chunk-TJMME6CL.mjs +0 -24
- package/dist/huggingface-stream.d.ts +0 -5
- package/dist/huggingface-stream.js +0 -121
- package/dist/huggingface-stream.mjs +0 -8
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.js +0 -12
- package/dist/index.test.mjs +0 -10
- package/dist/langchain-stream.d.ts +0 -12
- package/dist/langchain-stream.js +0 -102
- package/dist/langchain-stream.mjs +0 -8
- package/dist/openai-stream.d.ts +0 -5
- package/dist/openai-stream.js +0 -144
- package/dist/openai-stream.mjs +0 -8
- package/dist/streaming-text-response.d.ts +0 -17
- package/dist/streaming-text-response.js +0 -75
- package/dist/streaming-text-response.mjs +0 -9
- package/react/dist/chunk-5PP6W52J.mjs +0 -202
- package/react/dist/chunk-6EH3SWMP.mjs +0 -55
- package/react/dist/chunk-PW6HSU2N.mjs +0 -154
- package/react/dist/use-chat.d.ts +0 -42
- package/react/dist/use-chat.js +0 -276
- package/react/dist/use-chat.mjs +0 -8
- package/react/dist/use-completion.d.ts +0 -47
- package/react/dist/use-completion.js +0 -229
- package/react/dist/use-completion.mjs +0 -8
- package/svelte/dist/chunk-6USBQIV6.mjs +0 -177
- package/svelte/dist/chunk-BQ64GHZ3.mjs +0 -136
- package/svelte/dist/chunk-CENOSGDG.mjs +0 -493
- package/svelte/dist/types-f862f74a.d.ts +0 -123
- package/svelte/dist/use-chat.d.ts +0 -39
- package/svelte/dist/use-chat.js +0 -680
- package/svelte/dist/use-chat.mjs +0 -7
- package/svelte/dist/use-completion.d.ts +0 -38
- package/svelte/dist/use-completion.js +0 -640
- package/svelte/dist/use-completion.mjs +0 -7
package/react/dist/use-chat.d.ts
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
import { M as Message, C as CreateMessage, U as UseChatOptions } from './types-f862f74a.js';
|
2
|
-
|
3
|
-
type UseChatHelpers = {
|
4
|
-
/** Current messages in the chat */
|
5
|
-
messages: Message[];
|
6
|
-
/** The error object of the API request */
|
7
|
-
error: undefined | Error;
|
8
|
-
/**
|
9
|
-
* Append a user message to the chat list. This triggers the API call to fetch
|
10
|
-
* the assistant's response.
|
11
|
-
*/
|
12
|
-
append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
|
13
|
-
/**
|
14
|
-
* Reload the last AI chat response for the given chat history. If the last
|
15
|
-
* message isn't from the assistant, it will request the API to generate a
|
16
|
-
* new response.
|
17
|
-
*/
|
18
|
-
reload: () => Promise<string | null | undefined>;
|
19
|
-
/**
|
20
|
-
* Abort the current request immediately, keep the generated tokens if any.
|
21
|
-
*/
|
22
|
-
stop: () => void;
|
23
|
-
/**
|
24
|
-
* Update the `messages` state locally. This is useful when you want to
|
25
|
-
* edit the messages on the client, and then trigger the `reload` method
|
26
|
-
* manually to regenerate the AI response.
|
27
|
-
*/
|
28
|
-
setMessages: (messages: Message[]) => void;
|
29
|
-
/** The current value of the input */
|
30
|
-
input: string;
|
31
|
-
/** setState-powered method to update the input value */
|
32
|
-
setInput: React.Dispatch<React.SetStateAction<string>>;
|
33
|
-
/** An input/textarea-ready onChange handler to control the value of the input */
|
34
|
-
handleInputChange: (e: any) => void;
|
35
|
-
/** Form submission handler to automattically reset input and append a user message */
|
36
|
-
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
|
37
|
-
/** Whether the API request is in progress */
|
38
|
-
isLoading: boolean;
|
39
|
-
};
|
40
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
|
41
|
-
|
42
|
-
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, useChat };
|
package/react/dist/use-chat.js
DELETED
@@ -1,276 +0,0 @@
|
|
1
|
-
'use client'
|
2
|
-
"use strict";
|
3
|
-
var __create = Object.create;
|
4
|
-
var __defProp = Object.defineProperty;
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
12
|
-
var __spreadValues = (a, b) => {
|
13
|
-
for (var prop in b || (b = {}))
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
if (__getOwnPropSymbols)
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
18
|
-
if (__propIsEnum.call(b, prop))
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
20
|
-
}
|
21
|
-
return a;
|
22
|
-
};
|
23
|
-
var __export = (target, all) => {
|
24
|
-
for (var name in all)
|
25
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
26
|
-
};
|
27
|
-
var __copyProps = (to, from, except, desc) => {
|
28
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
29
|
-
for (let key of __getOwnPropNames(from))
|
30
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
31
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
32
|
-
}
|
33
|
-
return to;
|
34
|
-
};
|
35
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
36
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
37
|
-
// file that has been converted to a CommonJS file using a Babel-
|
38
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
39
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
40
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
41
|
-
mod
|
42
|
-
));
|
43
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
44
|
-
var __async = (__this, __arguments, generator) => {
|
45
|
-
return new Promise((resolve, reject) => {
|
46
|
-
var fulfilled = (value) => {
|
47
|
-
try {
|
48
|
-
step(generator.next(value));
|
49
|
-
} catch (e) {
|
50
|
-
reject(e);
|
51
|
-
}
|
52
|
-
};
|
53
|
-
var rejected = (value) => {
|
54
|
-
try {
|
55
|
-
step(generator.throw(value));
|
56
|
-
} catch (e) {
|
57
|
-
reject(e);
|
58
|
-
}
|
59
|
-
};
|
60
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
61
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
62
|
-
});
|
63
|
-
};
|
64
|
-
|
65
|
-
// react/use-chat.ts
|
66
|
-
var use_chat_exports = {};
|
67
|
-
__export(use_chat_exports, {
|
68
|
-
useChat: () => useChat
|
69
|
-
});
|
70
|
-
module.exports = __toCommonJS(use_chat_exports);
|
71
|
-
var import_react = require("react");
|
72
|
-
var import_mutation = __toESM(require("swr/mutation"));
|
73
|
-
var import_swr = __toESM(require("swr"));
|
74
|
-
|
75
|
-
// shared/utils.ts
|
76
|
-
var import_nanoid = require("nanoid");
|
77
|
-
var nanoid = (0, import_nanoid.customAlphabet)(
|
78
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
79
|
-
7
|
80
|
-
);
|
81
|
-
var decoder = new TextDecoder();
|
82
|
-
function decodeAIStreamChunk(chunk) {
|
83
|
-
return decoder.decode(chunk);
|
84
|
-
}
|
85
|
-
|
86
|
-
// react/use-chat.ts
|
87
|
-
function useChat({
|
88
|
-
api = "/api/chat",
|
89
|
-
id,
|
90
|
-
initialMessages = [],
|
91
|
-
initialInput = "",
|
92
|
-
sendExtraMessageFields,
|
93
|
-
onResponse,
|
94
|
-
onFinish,
|
95
|
-
onError,
|
96
|
-
headers,
|
97
|
-
body
|
98
|
-
} = {}) {
|
99
|
-
const hookId = (0, import_react.useId)();
|
100
|
-
const chatId = id || hookId;
|
101
|
-
const { data, mutate } = (0, import_swr.default)([api, chatId], null, {
|
102
|
-
fallbackData: initialMessages
|
103
|
-
});
|
104
|
-
const messages = data;
|
105
|
-
const messagesRef = (0, import_react.useRef)(messages);
|
106
|
-
(0, import_react.useEffect)(() => {
|
107
|
-
messagesRef.current = messages;
|
108
|
-
}, [messages]);
|
109
|
-
const abortControllerRef = (0, import_react.useRef)(null);
|
110
|
-
const extraMetadataRef = (0, import_react.useRef)({
|
111
|
-
headers,
|
112
|
-
body
|
113
|
-
});
|
114
|
-
(0, import_react.useEffect)(() => {
|
115
|
-
extraMetadataRef.current = {
|
116
|
-
headers,
|
117
|
-
body
|
118
|
-
};
|
119
|
-
}, [headers, body]);
|
120
|
-
const { error, trigger, isMutating } = (0, import_mutation.default)(
|
121
|
-
[api, chatId],
|
122
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: messagesSnapshot }) {
|
123
|
-
try {
|
124
|
-
const abortController = new AbortController();
|
125
|
-
abortControllerRef.current = abortController;
|
126
|
-
const previousMessages = messagesRef.current;
|
127
|
-
mutate(messagesSnapshot, false);
|
128
|
-
const res = yield fetch(api, {
|
129
|
-
method: "POST",
|
130
|
-
body: JSON.stringify(__spreadValues({
|
131
|
-
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
132
|
-
role,
|
133
|
-
content
|
134
|
-
}))
|
135
|
-
}, extraMetadataRef.current.body)),
|
136
|
-
headers: extraMetadataRef.current.headers || {},
|
137
|
-
signal: abortController.signal
|
138
|
-
}).catch((err) => {
|
139
|
-
mutate(previousMessages, false);
|
140
|
-
throw err;
|
141
|
-
});
|
142
|
-
if (onResponse) {
|
143
|
-
try {
|
144
|
-
yield onResponse(res);
|
145
|
-
} catch (err) {
|
146
|
-
throw err;
|
147
|
-
}
|
148
|
-
}
|
149
|
-
if (!res.ok) {
|
150
|
-
mutate(previousMessages, false);
|
151
|
-
throw new Error(
|
152
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
153
|
-
);
|
154
|
-
}
|
155
|
-
if (!res.body) {
|
156
|
-
throw new Error("The response body is empty.");
|
157
|
-
}
|
158
|
-
let result = "";
|
159
|
-
const createdAt = /* @__PURE__ */ new Date();
|
160
|
-
const replyId = nanoid();
|
161
|
-
const reader = res.body.getReader();
|
162
|
-
while (true) {
|
163
|
-
const { done, value } = yield reader.read();
|
164
|
-
if (done) {
|
165
|
-
break;
|
166
|
-
}
|
167
|
-
result += decodeAIStreamChunk(value);
|
168
|
-
mutate(
|
169
|
-
[
|
170
|
-
...messagesSnapshot,
|
171
|
-
{
|
172
|
-
id: replyId,
|
173
|
-
createdAt,
|
174
|
-
content: result,
|
175
|
-
role: "assistant"
|
176
|
-
}
|
177
|
-
],
|
178
|
-
false
|
179
|
-
);
|
180
|
-
if (abortControllerRef.current === null) {
|
181
|
-
reader.cancel();
|
182
|
-
break;
|
183
|
-
}
|
184
|
-
}
|
185
|
-
if (onFinish) {
|
186
|
-
onFinish({
|
187
|
-
id: replyId,
|
188
|
-
createdAt,
|
189
|
-
content: result,
|
190
|
-
role: "assistant"
|
191
|
-
});
|
192
|
-
}
|
193
|
-
abortControllerRef.current = null;
|
194
|
-
return result;
|
195
|
-
} catch (err) {
|
196
|
-
if (err.name === "AbortError") {
|
197
|
-
abortControllerRef.current = null;
|
198
|
-
return null;
|
199
|
-
}
|
200
|
-
if (onError && err instanceof Error) {
|
201
|
-
onError(err);
|
202
|
-
}
|
203
|
-
throw err;
|
204
|
-
}
|
205
|
-
}),
|
206
|
-
{
|
207
|
-
populateCache: false,
|
208
|
-
revalidate: false
|
209
|
-
}
|
210
|
-
);
|
211
|
-
const append = (0, import_react.useCallback)(
|
212
|
-
(message) => __async(this, null, function* () {
|
213
|
-
if (!message.id) {
|
214
|
-
message.id = nanoid();
|
215
|
-
}
|
216
|
-
return trigger(messagesRef.current.concat(message));
|
217
|
-
}),
|
218
|
-
[trigger]
|
219
|
-
);
|
220
|
-
const reload = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
221
|
-
if (messagesRef.current.length === 0)
|
222
|
-
return null;
|
223
|
-
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
224
|
-
if (lastMessage.role === "assistant") {
|
225
|
-
return trigger(messagesRef.current.slice(0, -1));
|
226
|
-
}
|
227
|
-
return trigger(messagesRef.current);
|
228
|
-
}), [trigger]);
|
229
|
-
const stop = (0, import_react.useCallback)(() => {
|
230
|
-
if (abortControllerRef.current) {
|
231
|
-
abortControllerRef.current.abort();
|
232
|
-
abortControllerRef.current = null;
|
233
|
-
}
|
234
|
-
}, []);
|
235
|
-
const setMessages = (0, import_react.useCallback)(
|
236
|
-
(messages2) => {
|
237
|
-
mutate(messages2, false);
|
238
|
-
messagesRef.current = messages2;
|
239
|
-
},
|
240
|
-
[mutate]
|
241
|
-
);
|
242
|
-
const [input, setInput] = (0, import_react.useState)(initialInput);
|
243
|
-
const handleSubmit = (0, import_react.useCallback)(
|
244
|
-
(e) => {
|
245
|
-
e.preventDefault();
|
246
|
-
if (!input)
|
247
|
-
return;
|
248
|
-
append({
|
249
|
-
content: input,
|
250
|
-
role: "user"
|
251
|
-
});
|
252
|
-
setInput("");
|
253
|
-
},
|
254
|
-
[input, append]
|
255
|
-
);
|
256
|
-
const handleInputChange = (e) => {
|
257
|
-
setInput(e.target.value);
|
258
|
-
};
|
259
|
-
return {
|
260
|
-
messages,
|
261
|
-
error,
|
262
|
-
append,
|
263
|
-
reload,
|
264
|
-
stop,
|
265
|
-
setMessages,
|
266
|
-
input,
|
267
|
-
setInput,
|
268
|
-
handleInputChange,
|
269
|
-
handleSubmit,
|
270
|
-
isLoading: isMutating
|
271
|
-
};
|
272
|
-
}
|
273
|
-
// Annotate the CommonJS export names for ESM import in node:
|
274
|
-
0 && (module.exports = {
|
275
|
-
useChat
|
276
|
-
});
|
package/react/dist/use-chat.mjs
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
import { a as UseCompletionOptions } from './types-f862f74a.js';
|
2
|
-
|
3
|
-
type UseCompletionHelpers = {
|
4
|
-
/** The current completion result */
|
5
|
-
completion: string;
|
6
|
-
/**
|
7
|
-
* Send a new prompt to the API endpoint and update the completion state.
|
8
|
-
*/
|
9
|
-
complete: (prompt: string) => Promise<string | null | undefined>;
|
10
|
-
/** The error object of the API request */
|
11
|
-
error: undefined | Error;
|
12
|
-
/**
|
13
|
-
* Abort the current API request but keep the generated tokens.
|
14
|
-
*/
|
15
|
-
stop: () => void;
|
16
|
-
/**
|
17
|
-
* Update the `completion` state locally.
|
18
|
-
*/
|
19
|
-
setCompletion: (completion: string) => void;
|
20
|
-
/** The current value of the input */
|
21
|
-
input: string;
|
22
|
-
/** setState-powered method to update the input value */
|
23
|
-
setInput: React.Dispatch<React.SetStateAction<string>>;
|
24
|
-
/**
|
25
|
-
* An input/textarea-ready onChange handler to control the value of the input
|
26
|
-
* @example
|
27
|
-
* ```jsx
|
28
|
-
* <input onChange={handleInputChange} value={input} />
|
29
|
-
* ```
|
30
|
-
*/
|
31
|
-
handleInputChange: (e: any) => void;
|
32
|
-
/**
|
33
|
-
* Form submission handler to automattically reset input and append a user message
|
34
|
-
* @example
|
35
|
-
* ```jsx
|
36
|
-
* <form onSubmit={handleSubmit}>
|
37
|
-
* <input onChange={handleInputChange} value={input} />
|
38
|
-
* </form>
|
39
|
-
* ```
|
40
|
-
*/
|
41
|
-
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
|
42
|
-
/** Whether the API request is in progress */
|
43
|
-
isLoading: boolean;
|
44
|
-
};
|
45
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
46
|
-
|
47
|
-
export { UseCompletionHelpers, useCompletion };
|
@@ -1,229 +0,0 @@
|
|
1
|
-
'use client'
|
2
|
-
"use strict";
|
3
|
-
var __create = Object.create;
|
4
|
-
var __defProp = Object.defineProperty;
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
12
|
-
var __spreadValues = (a, b) => {
|
13
|
-
for (var prop in b || (b = {}))
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
if (__getOwnPropSymbols)
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
18
|
-
if (__propIsEnum.call(b, prop))
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
20
|
-
}
|
21
|
-
return a;
|
22
|
-
};
|
23
|
-
var __export = (target, all) => {
|
24
|
-
for (var name in all)
|
25
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
26
|
-
};
|
27
|
-
var __copyProps = (to, from, except, desc) => {
|
28
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
29
|
-
for (let key of __getOwnPropNames(from))
|
30
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
31
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
32
|
-
}
|
33
|
-
return to;
|
34
|
-
};
|
35
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
36
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
37
|
-
// file that has been converted to a CommonJS file using a Babel-
|
38
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
39
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
40
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
41
|
-
mod
|
42
|
-
));
|
43
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
44
|
-
var __async = (__this, __arguments, generator) => {
|
45
|
-
return new Promise((resolve, reject) => {
|
46
|
-
var fulfilled = (value) => {
|
47
|
-
try {
|
48
|
-
step(generator.next(value));
|
49
|
-
} catch (e) {
|
50
|
-
reject(e);
|
51
|
-
}
|
52
|
-
};
|
53
|
-
var rejected = (value) => {
|
54
|
-
try {
|
55
|
-
step(generator.throw(value));
|
56
|
-
} catch (e) {
|
57
|
-
reject(e);
|
58
|
-
}
|
59
|
-
};
|
60
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
61
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
62
|
-
});
|
63
|
-
};
|
64
|
-
|
65
|
-
// react/use-completion.ts
|
66
|
-
var use_completion_exports = {};
|
67
|
-
__export(use_completion_exports, {
|
68
|
-
useCompletion: () => useCompletion
|
69
|
-
});
|
70
|
-
module.exports = __toCommonJS(use_completion_exports);
|
71
|
-
var import_react = require("react");
|
72
|
-
var import_mutation = __toESM(require("swr/mutation"));
|
73
|
-
var import_swr = __toESM(require("swr"));
|
74
|
-
|
75
|
-
// shared/utils.ts
|
76
|
-
var import_nanoid = require("nanoid");
|
77
|
-
var nanoid = (0, import_nanoid.customAlphabet)(
|
78
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
79
|
-
7
|
80
|
-
);
|
81
|
-
var decoder = new TextDecoder();
|
82
|
-
function decodeAIStreamChunk(chunk) {
|
83
|
-
return decoder.decode(chunk);
|
84
|
-
}
|
85
|
-
|
86
|
-
// react/use-completion.ts
|
87
|
-
function useCompletion({
|
88
|
-
api = "/api/completion",
|
89
|
-
id,
|
90
|
-
initialCompletion = "",
|
91
|
-
initialInput = "",
|
92
|
-
headers,
|
93
|
-
body,
|
94
|
-
onResponse,
|
95
|
-
onFinish,
|
96
|
-
onError
|
97
|
-
} = {}) {
|
98
|
-
const hookId = (0, import_react.useId)();
|
99
|
-
const completionId = id || hookId;
|
100
|
-
const { data, mutate } = (0, import_swr.default)([api, completionId], null, {
|
101
|
-
fallbackData: initialCompletion
|
102
|
-
});
|
103
|
-
const completion = data;
|
104
|
-
const [abortController, setAbortController] = (0, import_react.useState)(null);
|
105
|
-
const extraMetadataRef = (0, import_react.useRef)({
|
106
|
-
headers,
|
107
|
-
body
|
108
|
-
});
|
109
|
-
(0, import_react.useEffect)(() => {
|
110
|
-
extraMetadataRef.current = {
|
111
|
-
headers,
|
112
|
-
body
|
113
|
-
};
|
114
|
-
}, [headers, body]);
|
115
|
-
const { error, trigger, isMutating } = (0, import_mutation.default)(
|
116
|
-
[api, completionId],
|
117
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: prompt }) {
|
118
|
-
try {
|
119
|
-
const abortController2 = new AbortController();
|
120
|
-
setAbortController(abortController2);
|
121
|
-
mutate("", false);
|
122
|
-
const res = yield fetch(api, {
|
123
|
-
method: "POST",
|
124
|
-
body: JSON.stringify(__spreadValues({
|
125
|
-
prompt
|
126
|
-
}, extraMetadataRef.current.body)),
|
127
|
-
headers: extraMetadataRef.current.headers || {},
|
128
|
-
signal: abortController2.signal
|
129
|
-
}).catch((err) => {
|
130
|
-
throw err;
|
131
|
-
});
|
132
|
-
if (onResponse) {
|
133
|
-
try {
|
134
|
-
yield onResponse(res);
|
135
|
-
} catch (err) {
|
136
|
-
throw err;
|
137
|
-
}
|
138
|
-
}
|
139
|
-
if (!res.ok) {
|
140
|
-
throw new Error(
|
141
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
142
|
-
);
|
143
|
-
}
|
144
|
-
if (!res.body) {
|
145
|
-
throw new Error("The response body is empty.");
|
146
|
-
}
|
147
|
-
let result = "";
|
148
|
-
const reader = res.body.getReader();
|
149
|
-
while (true) {
|
150
|
-
const { done, value } = yield reader.read();
|
151
|
-
if (done) {
|
152
|
-
break;
|
153
|
-
}
|
154
|
-
result += decodeAIStreamChunk(value);
|
155
|
-
mutate(result, false);
|
156
|
-
if (abortController2 === null) {
|
157
|
-
reader.cancel();
|
158
|
-
break;
|
159
|
-
}
|
160
|
-
}
|
161
|
-
if (onFinish) {
|
162
|
-
onFinish(prompt, result);
|
163
|
-
}
|
164
|
-
setAbortController(null);
|
165
|
-
return result;
|
166
|
-
} catch (err) {
|
167
|
-
if (err.name === "AbortError") {
|
168
|
-
setAbortController(null);
|
169
|
-
return null;
|
170
|
-
}
|
171
|
-
if (onError && err instanceof Error) {
|
172
|
-
onError(err);
|
173
|
-
}
|
174
|
-
throw err;
|
175
|
-
}
|
176
|
-
}),
|
177
|
-
{
|
178
|
-
populateCache: false,
|
179
|
-
revalidate: false
|
180
|
-
}
|
181
|
-
);
|
182
|
-
const stop = (0, import_react.useCallback)(() => {
|
183
|
-
if (abortController) {
|
184
|
-
abortController.abort();
|
185
|
-
setAbortController(null);
|
186
|
-
}
|
187
|
-
}, [abortController]);
|
188
|
-
const setCompletion = (0, import_react.useCallback)(
|
189
|
-
(completion2) => {
|
190
|
-
mutate(completion2, false);
|
191
|
-
},
|
192
|
-
[mutate]
|
193
|
-
);
|
194
|
-
const [input, setInput] = (0, import_react.useState)(initialInput);
|
195
|
-
const handleSubmit = (0, import_react.useCallback)(
|
196
|
-
(e) => {
|
197
|
-
e.preventDefault();
|
198
|
-
if (!input)
|
199
|
-
return;
|
200
|
-
return trigger(input);
|
201
|
-
},
|
202
|
-
[input, trigger]
|
203
|
-
);
|
204
|
-
const handleInputChange = (e) => {
|
205
|
-
setInput(e.target.value);
|
206
|
-
};
|
207
|
-
const complete = (0, import_react.useCallback)(
|
208
|
-
(prompt) => __async(this, null, function* () {
|
209
|
-
return trigger(prompt);
|
210
|
-
}),
|
211
|
-
[trigger]
|
212
|
-
);
|
213
|
-
return {
|
214
|
-
completion,
|
215
|
-
complete,
|
216
|
-
error,
|
217
|
-
setCompletion,
|
218
|
-
stop,
|
219
|
-
input,
|
220
|
-
setInput,
|
221
|
-
handleInputChange,
|
222
|
-
handleSubmit,
|
223
|
-
isLoading: isMutating
|
224
|
-
};
|
225
|
-
}
|
226
|
-
// Annotate the CommonJS export names for ESM import in node:
|
227
|
-
0 && (module.exports = {
|
228
|
-
useCompletion
|
229
|
-
});
|