ai 2.1.23 → 2.1.24
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/package.json +19 -6
- package/solid/dist/index.d.ts +239 -0
- package/solid/dist/index.js +411 -0
- package/solid/dist/index.mjs +386 -0
- package/svelte/dist/index.js +364 -310
- package/svelte/dist/index.mjs +367 -313
@@ -0,0 +1,386 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6
|
+
var __spreadValues = (a, b) => {
|
7
|
+
for (var prop in b || (b = {}))
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
10
|
+
if (__getOwnPropSymbols)
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
12
|
+
if (__propIsEnum.call(b, prop))
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
14
|
+
}
|
15
|
+
return a;
|
16
|
+
};
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
18
|
+
return new Promise((resolve, reject) => {
|
19
|
+
var fulfilled = (value) => {
|
20
|
+
try {
|
21
|
+
step(generator.next(value));
|
22
|
+
} catch (e) {
|
23
|
+
reject(e);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
var rejected = (value) => {
|
27
|
+
try {
|
28
|
+
step(generator.throw(value));
|
29
|
+
} catch (e) {
|
30
|
+
reject(e);
|
31
|
+
}
|
32
|
+
};
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
35
|
+
});
|
36
|
+
};
|
37
|
+
|
38
|
+
// solid/use-chat.ts
|
39
|
+
import { createSignal } from "solid-js";
|
40
|
+
import { useSWRStore } from "solid-swr-store";
|
41
|
+
import { createSWRStore } from "swr-store";
|
42
|
+
|
43
|
+
// shared/utils.ts
|
44
|
+
import { customAlphabet } from "nanoid/non-secure";
|
45
|
+
var nanoid = customAlphabet(
|
46
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
47
|
+
7
|
48
|
+
);
|
49
|
+
function createChunkDecoder() {
|
50
|
+
const decoder = new TextDecoder();
|
51
|
+
return function(chunk) {
|
52
|
+
if (!chunk)
|
53
|
+
return "";
|
54
|
+
return decoder.decode(chunk, { stream: true });
|
55
|
+
};
|
56
|
+
}
|
57
|
+
|
58
|
+
// solid/use-chat.ts
|
59
|
+
var uniqueId = 0;
|
60
|
+
var store = {};
|
61
|
+
var chatApiStore = createSWRStore({
|
62
|
+
get: (key) => __async(void 0, null, function* () {
|
63
|
+
var _a;
|
64
|
+
return (_a = store[key]) != null ? _a : [];
|
65
|
+
})
|
66
|
+
});
|
67
|
+
function useChat({
|
68
|
+
api = "/api/chat",
|
69
|
+
id,
|
70
|
+
initialMessages = [],
|
71
|
+
initialInput = "",
|
72
|
+
sendExtraMessageFields,
|
73
|
+
onResponse,
|
74
|
+
onFinish,
|
75
|
+
onError,
|
76
|
+
credentials,
|
77
|
+
headers,
|
78
|
+
body
|
79
|
+
} = {}) {
|
80
|
+
const chatId = id || `chat-${uniqueId++}`;
|
81
|
+
const key = `${api}|${chatId}`;
|
82
|
+
const data = useSWRStore(chatApiStore, () => [key], {
|
83
|
+
initialData: initialMessages
|
84
|
+
});
|
85
|
+
const mutate = (data2) => {
|
86
|
+
store[key] = data2;
|
87
|
+
return chatApiStore.mutate([key], {
|
88
|
+
status: "success",
|
89
|
+
data: data2
|
90
|
+
});
|
91
|
+
};
|
92
|
+
const messages = data;
|
93
|
+
const [error, setError] = createSignal(void 0);
|
94
|
+
const [isLoading, setIsLoading] = createSignal(false);
|
95
|
+
let abortController = null;
|
96
|
+
function triggerRequest(messagesSnapshot, options) {
|
97
|
+
return __async(this, null, function* () {
|
98
|
+
try {
|
99
|
+
setIsLoading(true);
|
100
|
+
abortController = new AbortController();
|
101
|
+
const previousMessages = chatApiStore.get([key], {
|
102
|
+
shouldRevalidate: false
|
103
|
+
});
|
104
|
+
mutate(messagesSnapshot);
|
105
|
+
const res = yield fetch(api, {
|
106
|
+
method: "POST",
|
107
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
108
|
+
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
109
|
+
role,
|
110
|
+
content
|
111
|
+
}))
|
112
|
+
}, body), options == null ? void 0 : options.body)),
|
113
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
114
|
+
signal: abortController.signal,
|
115
|
+
credentials
|
116
|
+
}).catch((err) => {
|
117
|
+
if (previousMessages.status === "success") {
|
118
|
+
mutate(previousMessages.data);
|
119
|
+
}
|
120
|
+
throw err;
|
121
|
+
});
|
122
|
+
if (onResponse) {
|
123
|
+
try {
|
124
|
+
yield onResponse(res);
|
125
|
+
} catch (err) {
|
126
|
+
throw err;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
if (!res.ok) {
|
130
|
+
if (previousMessages.status === "success") {
|
131
|
+
mutate(previousMessages.data);
|
132
|
+
}
|
133
|
+
throw new Error(
|
134
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
135
|
+
);
|
136
|
+
}
|
137
|
+
if (!res.body) {
|
138
|
+
throw new Error("The response body is empty.");
|
139
|
+
}
|
140
|
+
let result = "";
|
141
|
+
const createdAt = /* @__PURE__ */ new Date();
|
142
|
+
const replyId = nanoid();
|
143
|
+
const reader = res.body.getReader();
|
144
|
+
const decoder = createChunkDecoder();
|
145
|
+
while (true) {
|
146
|
+
const { done, value } = yield reader.read();
|
147
|
+
if (done) {
|
148
|
+
break;
|
149
|
+
}
|
150
|
+
result += decoder(value);
|
151
|
+
mutate([
|
152
|
+
...messagesSnapshot,
|
153
|
+
{
|
154
|
+
id: replyId,
|
155
|
+
createdAt,
|
156
|
+
content: result,
|
157
|
+
role: "assistant"
|
158
|
+
}
|
159
|
+
]);
|
160
|
+
if (abortController === null) {
|
161
|
+
reader.cancel();
|
162
|
+
break;
|
163
|
+
}
|
164
|
+
}
|
165
|
+
if (onFinish) {
|
166
|
+
onFinish({
|
167
|
+
id: replyId,
|
168
|
+
createdAt,
|
169
|
+
content: result,
|
170
|
+
role: "assistant"
|
171
|
+
});
|
172
|
+
}
|
173
|
+
abortController = null;
|
174
|
+
return result;
|
175
|
+
} catch (err) {
|
176
|
+
if (err.name === "AbortError") {
|
177
|
+
abortController = null;
|
178
|
+
return null;
|
179
|
+
}
|
180
|
+
if (onError && err instanceof Error) {
|
181
|
+
onError(err);
|
182
|
+
}
|
183
|
+
setError(err);
|
184
|
+
} finally {
|
185
|
+
setIsLoading(false);
|
186
|
+
}
|
187
|
+
});
|
188
|
+
}
|
189
|
+
const append = (message, options) => __async(this, null, function* () {
|
190
|
+
var _a;
|
191
|
+
if (!message.id) {
|
192
|
+
message.id = nanoid();
|
193
|
+
}
|
194
|
+
return triggerRequest(
|
195
|
+
((_a = messages()) != null ? _a : []).concat(message),
|
196
|
+
options
|
197
|
+
);
|
198
|
+
});
|
199
|
+
const reload = (options) => __async(this, null, function* () {
|
200
|
+
const messagesSnapshot = messages();
|
201
|
+
if (!messagesSnapshot || messagesSnapshot.length === 0)
|
202
|
+
return null;
|
203
|
+
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
204
|
+
if (lastMessage.role === "assistant") {
|
205
|
+
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
206
|
+
}
|
207
|
+
return triggerRequest(messagesSnapshot, options);
|
208
|
+
});
|
209
|
+
const stop = () => {
|
210
|
+
if (abortController) {
|
211
|
+
abortController.abort();
|
212
|
+
abortController = null;
|
213
|
+
}
|
214
|
+
};
|
215
|
+
const setMessages = (messages2) => {
|
216
|
+
mutate(messages2);
|
217
|
+
};
|
218
|
+
const [input, setInput] = createSignal(initialInput);
|
219
|
+
const handleSubmit = (e) => {
|
220
|
+
e.preventDefault();
|
221
|
+
const inputValue = input();
|
222
|
+
if (!inputValue)
|
223
|
+
return;
|
224
|
+
append({
|
225
|
+
content: inputValue,
|
226
|
+
role: "user",
|
227
|
+
createdAt: /* @__PURE__ */ new Date()
|
228
|
+
});
|
229
|
+
setInput("");
|
230
|
+
};
|
231
|
+
return {
|
232
|
+
messages,
|
233
|
+
append,
|
234
|
+
error,
|
235
|
+
reload,
|
236
|
+
stop,
|
237
|
+
setMessages,
|
238
|
+
input,
|
239
|
+
setInput,
|
240
|
+
handleSubmit,
|
241
|
+
isLoading
|
242
|
+
};
|
243
|
+
}
|
244
|
+
|
245
|
+
// solid/use-completion.ts
|
246
|
+
import { createSignal as createSignal2 } from "solid-js";
|
247
|
+
import { createSWRStore as createSWRStore2 } from "swr-store";
|
248
|
+
import { useSWRStore as useSWRStore2 } from "solid-swr-store";
|
249
|
+
var uniqueId2 = 0;
|
250
|
+
var store2 = {};
|
251
|
+
var completionApiStore = createSWRStore2({
|
252
|
+
get: (key) => __async(void 0, null, function* () {
|
253
|
+
var _a;
|
254
|
+
return (_a = store2[key]) != null ? _a : [];
|
255
|
+
})
|
256
|
+
});
|
257
|
+
function useCompletion({
|
258
|
+
api = "/api/completion",
|
259
|
+
id,
|
260
|
+
initialCompletion = "",
|
261
|
+
initialInput = "",
|
262
|
+
credentials,
|
263
|
+
headers,
|
264
|
+
body,
|
265
|
+
onResponse,
|
266
|
+
onFinish,
|
267
|
+
onError
|
268
|
+
} = {}) {
|
269
|
+
const completionId = id || `completion-${uniqueId2++}`;
|
270
|
+
const key = `${api}|${completionId}`;
|
271
|
+
const data = useSWRStore2(completionApiStore, () => [key], {
|
272
|
+
initialData: initialCompletion
|
273
|
+
});
|
274
|
+
const mutate = (data2) => {
|
275
|
+
store2[key] = data2;
|
276
|
+
return completionApiStore.mutate([key], {
|
277
|
+
data: data2,
|
278
|
+
status: "success"
|
279
|
+
});
|
280
|
+
};
|
281
|
+
const completion = data;
|
282
|
+
const [error, setError] = createSignal2(void 0);
|
283
|
+
const [isLoading, setIsLoading] = createSignal2(false);
|
284
|
+
let abortController = null;
|
285
|
+
function triggerRequest(prompt, options) {
|
286
|
+
return __async(this, null, function* () {
|
287
|
+
try {
|
288
|
+
setIsLoading(true);
|
289
|
+
abortController = new AbortController();
|
290
|
+
mutate("");
|
291
|
+
const res = yield fetch(api, {
|
292
|
+
method: "POST",
|
293
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
294
|
+
prompt
|
295
|
+
}, body), options == null ? void 0 : options.body)),
|
296
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
297
|
+
signal: abortController.signal,
|
298
|
+
credentials
|
299
|
+
}).catch((err) => {
|
300
|
+
throw err;
|
301
|
+
});
|
302
|
+
if (onResponse) {
|
303
|
+
try {
|
304
|
+
yield onResponse(res);
|
305
|
+
} catch (err) {
|
306
|
+
throw err;
|
307
|
+
}
|
308
|
+
}
|
309
|
+
if (!res.ok) {
|
310
|
+
throw new Error(
|
311
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
312
|
+
);
|
313
|
+
}
|
314
|
+
if (!res.body) {
|
315
|
+
throw new Error("The response body is empty.");
|
316
|
+
}
|
317
|
+
let result = "";
|
318
|
+
const reader = res.body.getReader();
|
319
|
+
const decoder = createChunkDecoder();
|
320
|
+
while (true) {
|
321
|
+
const { done, value } = yield reader.read();
|
322
|
+
if (done) {
|
323
|
+
break;
|
324
|
+
}
|
325
|
+
result += decoder(value);
|
326
|
+
mutate(result);
|
327
|
+
if (abortController === null) {
|
328
|
+
reader.cancel();
|
329
|
+
break;
|
330
|
+
}
|
331
|
+
}
|
332
|
+
if (onFinish) {
|
333
|
+
onFinish(prompt, result);
|
334
|
+
}
|
335
|
+
abortController = null;
|
336
|
+
return result;
|
337
|
+
} catch (err) {
|
338
|
+
if (err.name === "AbortError") {
|
339
|
+
abortController = null;
|
340
|
+
return null;
|
341
|
+
}
|
342
|
+
if (onError && error instanceof Error) {
|
343
|
+
onError(error);
|
344
|
+
}
|
345
|
+
setError(err);
|
346
|
+
} finally {
|
347
|
+
setIsLoading(false);
|
348
|
+
}
|
349
|
+
});
|
350
|
+
}
|
351
|
+
const complete = (prompt, options) => __async(this, null, function* () {
|
352
|
+
return triggerRequest(prompt, options);
|
353
|
+
});
|
354
|
+
const stop = () => {
|
355
|
+
if (abortController) {
|
356
|
+
abortController.abort();
|
357
|
+
abortController = null;
|
358
|
+
}
|
359
|
+
};
|
360
|
+
const setCompletion = (completion2) => {
|
361
|
+
mutate(completion2);
|
362
|
+
};
|
363
|
+
const [input, setInput] = createSignal2(initialInput);
|
364
|
+
const handleSubmit = (e) => {
|
365
|
+
e.preventDefault();
|
366
|
+
const inputValue = input();
|
367
|
+
if (!inputValue)
|
368
|
+
return;
|
369
|
+
return complete(inputValue);
|
370
|
+
};
|
371
|
+
return {
|
372
|
+
completion,
|
373
|
+
complete,
|
374
|
+
error,
|
375
|
+
stop,
|
376
|
+
setCompletion,
|
377
|
+
input,
|
378
|
+
setInput,
|
379
|
+
handleSubmit,
|
380
|
+
isLoading
|
381
|
+
};
|
382
|
+
}
|
383
|
+
export {
|
384
|
+
useChat,
|
385
|
+
useCompletion
|
386
|
+
};
|