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
@@ -1,3 +1,5 @@
|
|
1
|
+
import { Ref } from 'vue';
|
2
|
+
|
1
3
|
/**
|
2
4
|
* Shared types between the API and UI packages.
|
3
5
|
*/
|
@@ -120,4 +122,73 @@ type UseCompletionOptions = {
|
|
120
122
|
body?: object;
|
121
123
|
};
|
122
124
|
|
123
|
-
|
125
|
+
type UseChatHelpers = {
|
126
|
+
/** Current messages in the chat */
|
127
|
+
messages: Ref<Message[]>;
|
128
|
+
/** The error object of the API request */
|
129
|
+
error: Ref<undefined | Error>;
|
130
|
+
/**
|
131
|
+
* Append a user message to the chat list. This triggers the API call to fetch
|
132
|
+
* the assistant's response.
|
133
|
+
*/
|
134
|
+
append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
|
135
|
+
/**
|
136
|
+
* Reload the last AI chat response for the given chat history. If the last
|
137
|
+
* message isn't from the assistant, it will request the API to generate a
|
138
|
+
* new response.
|
139
|
+
*/
|
140
|
+
reload: () => Promise<string | null | undefined>;
|
141
|
+
/**
|
142
|
+
* Abort the current request immediately, keep the generated tokens if any.
|
143
|
+
*/
|
144
|
+
stop: () => void;
|
145
|
+
/**
|
146
|
+
* Update the `messages` state locally. This is useful when you want to
|
147
|
+
* edit the messages on the client, and then trigger the `reload` method
|
148
|
+
* manually to regenerate the AI response.
|
149
|
+
*/
|
150
|
+
setMessages: (messages: Message[]) => void;
|
151
|
+
/** The current value of the input */
|
152
|
+
input: Ref<string>;
|
153
|
+
/** Form submission handler to automattically reset input and append a user message */
|
154
|
+
handleSubmit: (e: any) => void;
|
155
|
+
/** Whether the API request is in progress */
|
156
|
+
isLoading: Ref<boolean>;
|
157
|
+
};
|
158
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
|
159
|
+
|
160
|
+
type UseCompletionHelpers = {
|
161
|
+
/** The current completion result */
|
162
|
+
completion: Ref<string>;
|
163
|
+
/** The error object of the API request */
|
164
|
+
error: Ref<undefined | Error>;
|
165
|
+
/**
|
166
|
+
* Send a new prompt to the API endpoint and update the completion state.
|
167
|
+
*/
|
168
|
+
complete: (prompt: string) => Promise<string | null | undefined>;
|
169
|
+
/**
|
170
|
+
* Abort the current API request but keep the generated tokens.
|
171
|
+
*/
|
172
|
+
stop: () => void;
|
173
|
+
/**
|
174
|
+
* Update the `completion` state locally.
|
175
|
+
*/
|
176
|
+
setCompletion: (completion: string) => void;
|
177
|
+
/** The current value of the input */
|
178
|
+
input: Ref<string>;
|
179
|
+
/**
|
180
|
+
* Form submission handler to automatically reset input and append a user message
|
181
|
+
* @example
|
182
|
+
* ```jsx
|
183
|
+
* <form @submit="handleSubmit">
|
184
|
+
* <input @change="handleInputChange" v-model="input" />
|
185
|
+
* </form>
|
186
|
+
* ```
|
187
|
+
*/
|
188
|
+
handleSubmit: (e: any) => void;
|
189
|
+
/** Whether the API request is in progress */
|
190
|
+
isLoading: Ref<boolean>;
|
191
|
+
};
|
192
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
193
|
+
|
194
|
+
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
|
@@ -0,0 +1,384 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
11
|
+
var __spreadValues = (a, b) => {
|
12
|
+
for (var prop in b || (b = {}))
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
15
|
+
if (__getOwnPropSymbols)
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
17
|
+
if (__propIsEnum.call(b, prop))
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
19
|
+
}
|
20
|
+
return a;
|
21
|
+
};
|
22
|
+
var __export = (target, all) => {
|
23
|
+
for (var name in all)
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
25
|
+
};
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
28
|
+
for (let key of __getOwnPropNames(from))
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
31
|
+
}
|
32
|
+
return to;
|
33
|
+
};
|
34
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
35
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
36
|
+
// file that has been converted to a CommonJS file using a Babel-
|
37
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
38
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
39
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
40
|
+
mod
|
41
|
+
));
|
42
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
43
|
+
var __async = (__this, __arguments, generator) => {
|
44
|
+
return new Promise((resolve, reject) => {
|
45
|
+
var fulfilled = (value) => {
|
46
|
+
try {
|
47
|
+
step(generator.next(value));
|
48
|
+
} catch (e) {
|
49
|
+
reject(e);
|
50
|
+
}
|
51
|
+
};
|
52
|
+
var rejected = (value) => {
|
53
|
+
try {
|
54
|
+
step(generator.throw(value));
|
55
|
+
} catch (e) {
|
56
|
+
reject(e);
|
57
|
+
}
|
58
|
+
};
|
59
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
60
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
61
|
+
});
|
62
|
+
};
|
63
|
+
|
64
|
+
// vue/index.ts
|
65
|
+
var vue_exports = {};
|
66
|
+
__export(vue_exports, {
|
67
|
+
useChat: () => useChat,
|
68
|
+
useCompletion: () => useCompletion
|
69
|
+
});
|
70
|
+
module.exports = __toCommonJS(vue_exports);
|
71
|
+
|
72
|
+
// vue/use-chat.ts
|
73
|
+
var import_swrv = __toESM(require("swrv"));
|
74
|
+
var import_vue = require("vue");
|
75
|
+
|
76
|
+
// shared/utils.ts
|
77
|
+
var import_nanoid = require("nanoid");
|
78
|
+
var nanoid = (0, import_nanoid.customAlphabet)(
|
79
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
80
|
+
7
|
81
|
+
);
|
82
|
+
var decoder = new TextDecoder();
|
83
|
+
function decodeAIStreamChunk(chunk) {
|
84
|
+
return decoder.decode(chunk);
|
85
|
+
}
|
86
|
+
|
87
|
+
// vue/use-chat.ts
|
88
|
+
var uniqueId = 0;
|
89
|
+
var useSWRV = import_swrv.default.default || import_swrv.default;
|
90
|
+
var store = {};
|
91
|
+
function useChat({
|
92
|
+
api = "/api/chat",
|
93
|
+
id,
|
94
|
+
initialMessages = [],
|
95
|
+
initialInput = "",
|
96
|
+
sendExtraMessageFields,
|
97
|
+
onResponse,
|
98
|
+
onFinish,
|
99
|
+
onError,
|
100
|
+
headers,
|
101
|
+
body
|
102
|
+
} = {}) {
|
103
|
+
const chatId = id || `chat-${uniqueId++}`;
|
104
|
+
const key = `${api}|${chatId}`;
|
105
|
+
const { data, mutate: originalMutate } = useSWRV(
|
106
|
+
key,
|
107
|
+
() => store[key] || initialMessages
|
108
|
+
);
|
109
|
+
data.value || (data.value = initialMessages);
|
110
|
+
const mutate = (data2) => {
|
111
|
+
store[key] = data2;
|
112
|
+
return originalMutate();
|
113
|
+
};
|
114
|
+
const messages = data;
|
115
|
+
const error = (0, import_vue.ref)(void 0);
|
116
|
+
const isLoading = (0, import_vue.ref)(false);
|
117
|
+
let abortController = null;
|
118
|
+
function triggerRequest(messagesSnapshot) {
|
119
|
+
return __async(this, null, function* () {
|
120
|
+
try {
|
121
|
+
isLoading.value = true;
|
122
|
+
abortController = new AbortController();
|
123
|
+
const previousMessages = messages.value;
|
124
|
+
mutate(messagesSnapshot);
|
125
|
+
const res = yield fetch(api, {
|
126
|
+
method: "POST",
|
127
|
+
body: JSON.stringify(__spreadValues({
|
128
|
+
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
129
|
+
role,
|
130
|
+
content
|
131
|
+
}))
|
132
|
+
}, body)),
|
133
|
+
headers: headers || {},
|
134
|
+
signal: abortController.signal
|
135
|
+
}).catch((err) => {
|
136
|
+
mutate(previousMessages);
|
137
|
+
throw err;
|
138
|
+
});
|
139
|
+
if (onResponse) {
|
140
|
+
try {
|
141
|
+
yield onResponse(res);
|
142
|
+
} catch (err) {
|
143
|
+
throw err;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
if (!res.ok) {
|
147
|
+
mutate(previousMessages);
|
148
|
+
throw new Error(
|
149
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
150
|
+
);
|
151
|
+
}
|
152
|
+
if (!res.body) {
|
153
|
+
throw new Error("The response body is empty.");
|
154
|
+
}
|
155
|
+
let result = "";
|
156
|
+
const createdAt = /* @__PURE__ */ new Date();
|
157
|
+
const replyId = nanoid();
|
158
|
+
const reader = res.body.getReader();
|
159
|
+
while (true) {
|
160
|
+
const { done, value } = yield reader.read();
|
161
|
+
if (done) {
|
162
|
+
break;
|
163
|
+
}
|
164
|
+
result += decodeAIStreamChunk(value);
|
165
|
+
mutate([
|
166
|
+
...messagesSnapshot,
|
167
|
+
{
|
168
|
+
id: replyId,
|
169
|
+
createdAt,
|
170
|
+
content: result,
|
171
|
+
role: "assistant"
|
172
|
+
}
|
173
|
+
]);
|
174
|
+
if (abortController === null) {
|
175
|
+
reader.cancel();
|
176
|
+
break;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
if (onFinish) {
|
180
|
+
onFinish({
|
181
|
+
id: replyId,
|
182
|
+
createdAt,
|
183
|
+
content: result,
|
184
|
+
role: "assistant"
|
185
|
+
});
|
186
|
+
}
|
187
|
+
abortController = null;
|
188
|
+
return result;
|
189
|
+
} catch (err) {
|
190
|
+
if (err.name === "AbortError") {
|
191
|
+
abortController = null;
|
192
|
+
return null;
|
193
|
+
}
|
194
|
+
if (onError && err instanceof Error) {
|
195
|
+
onError(err);
|
196
|
+
}
|
197
|
+
error.value = err;
|
198
|
+
} finally {
|
199
|
+
isLoading.value = false;
|
200
|
+
}
|
201
|
+
});
|
202
|
+
}
|
203
|
+
const append = (message) => __async(this, null, function* () {
|
204
|
+
if (!message.id) {
|
205
|
+
message.id = nanoid();
|
206
|
+
}
|
207
|
+
return triggerRequest(messages.value.concat(message));
|
208
|
+
});
|
209
|
+
const reload = () => __async(this, null, function* () {
|
210
|
+
const messagesSnapshot = messages.value;
|
211
|
+
if (messagesSnapshot.length === 0)
|
212
|
+
return null;
|
213
|
+
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
214
|
+
if (lastMessage.role === "assistant") {
|
215
|
+
return triggerRequest(messagesSnapshot.slice(0, -1));
|
216
|
+
}
|
217
|
+
return triggerRequest(messagesSnapshot);
|
218
|
+
});
|
219
|
+
const stop = () => {
|
220
|
+
if (abortController) {
|
221
|
+
abortController.abort();
|
222
|
+
abortController = null;
|
223
|
+
}
|
224
|
+
};
|
225
|
+
const setMessages = (messages2) => {
|
226
|
+
mutate(messages2);
|
227
|
+
};
|
228
|
+
const input = (0, import_vue.ref)(initialInput);
|
229
|
+
const handleSubmit = (e) => {
|
230
|
+
e.preventDefault();
|
231
|
+
const inputValue = input.value;
|
232
|
+
if (!inputValue)
|
233
|
+
return;
|
234
|
+
append({
|
235
|
+
content: inputValue,
|
236
|
+
role: "user"
|
237
|
+
});
|
238
|
+
input.value = "";
|
239
|
+
};
|
240
|
+
return {
|
241
|
+
messages,
|
242
|
+
append,
|
243
|
+
error,
|
244
|
+
reload,
|
245
|
+
stop,
|
246
|
+
setMessages,
|
247
|
+
input,
|
248
|
+
handleSubmit,
|
249
|
+
isLoading
|
250
|
+
};
|
251
|
+
}
|
252
|
+
|
253
|
+
// vue/use-completion.ts
|
254
|
+
var import_swrv2 = __toESM(require("swrv"));
|
255
|
+
var import_vue2 = require("vue");
|
256
|
+
var uniqueId2 = 0;
|
257
|
+
var useSWRV2 = import_swrv2.default.default || import_swrv2.default;
|
258
|
+
var store2 = {};
|
259
|
+
function useCompletion({
|
260
|
+
api = "/api/completion",
|
261
|
+
id,
|
262
|
+
initialCompletion = "",
|
263
|
+
initialInput = "",
|
264
|
+
headers,
|
265
|
+
body,
|
266
|
+
onResponse,
|
267
|
+
onFinish,
|
268
|
+
onError
|
269
|
+
} = {}) {
|
270
|
+
const completionId = id || `completion-${uniqueId2++}`;
|
271
|
+
const key = `${api}|${completionId}`;
|
272
|
+
const { data, mutate: originalMutate } = useSWRV2(
|
273
|
+
key,
|
274
|
+
() => store2[key] || initialCompletion
|
275
|
+
);
|
276
|
+
data.value || (data.value = initialCompletion);
|
277
|
+
const mutate = (data2) => {
|
278
|
+
store2[key] = data2;
|
279
|
+
return originalMutate();
|
280
|
+
};
|
281
|
+
const completion = data;
|
282
|
+
const error = (0, import_vue2.ref)(void 0);
|
283
|
+
const isLoading = (0, import_vue2.ref)(false);
|
284
|
+
let abortController = null;
|
285
|
+
function triggerRequest(prompt) {
|
286
|
+
return __async(this, null, function* () {
|
287
|
+
try {
|
288
|
+
isLoading.value = true;
|
289
|
+
abortController = new AbortController();
|
290
|
+
mutate("");
|
291
|
+
const res = yield fetch(api, {
|
292
|
+
method: "POST",
|
293
|
+
body: JSON.stringify(__spreadValues({
|
294
|
+
prompt
|
295
|
+
}, body)),
|
296
|
+
headers: headers || {},
|
297
|
+
signal: abortController.signal
|
298
|
+
}).catch((err) => {
|
299
|
+
throw err;
|
300
|
+
});
|
301
|
+
if (onResponse) {
|
302
|
+
try {
|
303
|
+
yield onResponse(res);
|
304
|
+
} catch (err) {
|
305
|
+
throw err;
|
306
|
+
}
|
307
|
+
}
|
308
|
+
if (!res.ok) {
|
309
|
+
throw new Error(
|
310
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
311
|
+
);
|
312
|
+
}
|
313
|
+
if (!res.body) {
|
314
|
+
throw new Error("The response body is empty.");
|
315
|
+
}
|
316
|
+
let result = "";
|
317
|
+
const reader = res.body.getReader();
|
318
|
+
while (true) {
|
319
|
+
const { done, value } = yield reader.read();
|
320
|
+
if (done) {
|
321
|
+
break;
|
322
|
+
}
|
323
|
+
result += decodeAIStreamChunk(value);
|
324
|
+
mutate(result);
|
325
|
+
if (abortController === null) {
|
326
|
+
reader.cancel();
|
327
|
+
break;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
if (onFinish) {
|
331
|
+
onFinish(prompt, result);
|
332
|
+
}
|
333
|
+
abortController = null;
|
334
|
+
return result;
|
335
|
+
} catch (err) {
|
336
|
+
if (err.name === "AbortError") {
|
337
|
+
abortController = null;
|
338
|
+
return null;
|
339
|
+
}
|
340
|
+
if (onError && error instanceof Error) {
|
341
|
+
onError(error);
|
342
|
+
}
|
343
|
+
error.value = err;
|
344
|
+
} finally {
|
345
|
+
isLoading.value = false;
|
346
|
+
}
|
347
|
+
});
|
348
|
+
}
|
349
|
+
const complete = (prompt) => __async(this, null, function* () {
|
350
|
+
return triggerRequest(prompt);
|
351
|
+
});
|
352
|
+
const stop = () => {
|
353
|
+
if (abortController) {
|
354
|
+
abortController.abort();
|
355
|
+
abortController = null;
|
356
|
+
}
|
357
|
+
};
|
358
|
+
const setCompletion = (completion2) => {
|
359
|
+
mutate(completion2);
|
360
|
+
};
|
361
|
+
const input = (0, import_vue2.ref)(initialInput);
|
362
|
+
const handleSubmit = (e) => {
|
363
|
+
e.preventDefault();
|
364
|
+
const inputValue = input.value;
|
365
|
+
if (!inputValue)
|
366
|
+
return;
|
367
|
+
return complete(inputValue);
|
368
|
+
};
|
369
|
+
return {
|
370
|
+
completion,
|
371
|
+
complete,
|
372
|
+
error,
|
373
|
+
stop,
|
374
|
+
setCompletion,
|
375
|
+
input,
|
376
|
+
handleSubmit,
|
377
|
+
isLoading
|
378
|
+
};
|
379
|
+
}
|
380
|
+
// Annotate the CommonJS export names for ESM import in node:
|
381
|
+
0 && (module.exports = {
|
382
|
+
useChat,
|
383
|
+
useCompletion
|
384
|
+
});
|