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/dist/openai-stream.js
DELETED
@@ -1,144 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __export = (target, all) => {
|
7
|
-
for (var name in all)
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
-
};
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
-
for (let key of __getOwnPropNames(from))
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
-
}
|
16
|
-
return to;
|
17
|
-
};
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
-
var __async = (__this, __arguments, generator) => {
|
20
|
-
return new Promise((resolve, reject) => {
|
21
|
-
var fulfilled = (value) => {
|
22
|
-
try {
|
23
|
-
step(generator.next(value));
|
24
|
-
} catch (e) {
|
25
|
-
reject(e);
|
26
|
-
}
|
27
|
-
};
|
28
|
-
var rejected = (value) => {
|
29
|
-
try {
|
30
|
-
step(generator.throw(value));
|
31
|
-
} catch (e) {
|
32
|
-
reject(e);
|
33
|
-
}
|
34
|
-
};
|
35
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
36
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
37
|
-
});
|
38
|
-
};
|
39
|
-
|
40
|
-
// streams/openai-stream.ts
|
41
|
-
var openai_stream_exports = {};
|
42
|
-
__export(openai_stream_exports, {
|
43
|
-
OpenAIStream: () => OpenAIStream
|
44
|
-
});
|
45
|
-
module.exports = __toCommonJS(openai_stream_exports);
|
46
|
-
|
47
|
-
// streams/ai-stream.ts
|
48
|
-
var import_eventsource_parser = require("eventsource-parser");
|
49
|
-
function createEventStreamTransformer(customParser) {
|
50
|
-
const decoder = new TextDecoder();
|
51
|
-
let parser;
|
52
|
-
return new TransformStream({
|
53
|
-
start(controller) {
|
54
|
-
return __async(this, null, function* () {
|
55
|
-
function onParse(event) {
|
56
|
-
if (event.type === "event") {
|
57
|
-
const data = event.data;
|
58
|
-
if (data === "[DONE]") {
|
59
|
-
controller.terminate();
|
60
|
-
return;
|
61
|
-
}
|
62
|
-
const message = customParser(data);
|
63
|
-
if (message)
|
64
|
-
controller.enqueue(message);
|
65
|
-
}
|
66
|
-
}
|
67
|
-
parser = (0, import_eventsource_parser.createParser)(onParse);
|
68
|
-
});
|
69
|
-
},
|
70
|
-
transform(chunk) {
|
71
|
-
parser.feed(decoder.decode(chunk));
|
72
|
-
}
|
73
|
-
});
|
74
|
-
}
|
75
|
-
function createCallbacksTransformer(callbacks) {
|
76
|
-
const encoder = new TextEncoder();
|
77
|
-
let fullResponse = "";
|
78
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
79
|
-
return new TransformStream({
|
80
|
-
start() {
|
81
|
-
return __async(this, null, function* () {
|
82
|
-
if (onStart)
|
83
|
-
yield onStart();
|
84
|
-
});
|
85
|
-
},
|
86
|
-
transform(message, controller) {
|
87
|
-
return __async(this, null, function* () {
|
88
|
-
controller.enqueue(encoder.encode(message));
|
89
|
-
if (onToken)
|
90
|
-
yield onToken(message);
|
91
|
-
if (onCompletion)
|
92
|
-
fullResponse += message;
|
93
|
-
});
|
94
|
-
},
|
95
|
-
flush() {
|
96
|
-
return __async(this, null, function* () {
|
97
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
98
|
-
});
|
99
|
-
}
|
100
|
-
});
|
101
|
-
}
|
102
|
-
function trimStartOfStreamHelper() {
|
103
|
-
let start = true;
|
104
|
-
return (text) => {
|
105
|
-
if (start)
|
106
|
-
text = text.trimStart();
|
107
|
-
if (text)
|
108
|
-
start = false;
|
109
|
-
return text;
|
110
|
-
};
|
111
|
-
}
|
112
|
-
function AIStream(res, customParser, callbacks) {
|
113
|
-
if (!res.ok) {
|
114
|
-
throw new Error(
|
115
|
-
`Failed to convert the response to stream. Received status code: ${res.status}.`
|
116
|
-
);
|
117
|
-
}
|
118
|
-
const stream = res.body || new ReadableStream({
|
119
|
-
start(controller) {
|
120
|
-
controller.close();
|
121
|
-
}
|
122
|
-
});
|
123
|
-
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
124
|
-
}
|
125
|
-
|
126
|
-
// streams/openai-stream.ts
|
127
|
-
function parseOpenAIStream() {
|
128
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
129
|
-
return (data) => {
|
130
|
-
var _a, _b, _c, _d, _e;
|
131
|
-
const json = JSON.parse(data);
|
132
|
-
const text = trimStartOfStream(
|
133
|
-
(_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 : ""
|
134
|
-
);
|
135
|
-
return text;
|
136
|
-
};
|
137
|
-
}
|
138
|
-
function OpenAIStream(res, cb) {
|
139
|
-
return AIStream(res, parseOpenAIStream(), cb);
|
140
|
-
}
|
141
|
-
// Annotate the CommonJS export names for ESM import in node:
|
142
|
-
0 && (module.exports = {
|
143
|
-
OpenAIStream
|
144
|
-
});
|
package/dist/openai-stream.mjs
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
import { ServerResponse } from 'node:http';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* A utility class for streaming text responses.
|
5
|
-
*/
|
6
|
-
declare class StreamingTextResponse extends Response {
|
7
|
-
constructor(res: ReadableStream, init?: ResponseInit);
|
8
|
-
}
|
9
|
-
/**
|
10
|
-
* A utility function to stream a ReadableStream to a Node.js response-like object.
|
11
|
-
*/
|
12
|
-
declare function streamToResponse(res: ReadableStream, response: ServerResponse, init?: {
|
13
|
-
headers?: Record<string, string>;
|
14
|
-
status?: number;
|
15
|
-
}): void;
|
16
|
-
|
17
|
-
export { StreamingTextResponse, streamToResponse };
|
@@ -1,75 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __defProps = Object.defineProperties;
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
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 __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
36
|
-
|
37
|
-
// streams/streaming-text-response.ts
|
38
|
-
var streaming_text_response_exports = {};
|
39
|
-
__export(streaming_text_response_exports, {
|
40
|
-
StreamingTextResponse: () => StreamingTextResponse,
|
41
|
-
streamToResponse: () => streamToResponse
|
42
|
-
});
|
43
|
-
module.exports = __toCommonJS(streaming_text_response_exports);
|
44
|
-
var StreamingTextResponse = class extends Response {
|
45
|
-
constructor(res, init) {
|
46
|
-
super(res, __spreadProps(__spreadValues({}, init), {
|
47
|
-
status: 200,
|
48
|
-
headers: __spreadValues({
|
49
|
-
"Content-Type": "text/plain; charset=utf-8"
|
50
|
-
}, init == null ? void 0 : init.headers)
|
51
|
-
}));
|
52
|
-
}
|
53
|
-
};
|
54
|
-
function streamToResponse(res, response, init) {
|
55
|
-
response.writeHead((init == null ? void 0 : init.status) || 200, __spreadValues({
|
56
|
-
"Content-Type": "text/plain; charset=utf-8"
|
57
|
-
}, init == null ? void 0 : init.headers));
|
58
|
-
const reader = res.getReader();
|
59
|
-
function read() {
|
60
|
-
reader.read().then(({ done, value }) => {
|
61
|
-
if (done) {
|
62
|
-
response.end();
|
63
|
-
return;
|
64
|
-
}
|
65
|
-
response.write(value);
|
66
|
-
read();
|
67
|
-
});
|
68
|
-
}
|
69
|
-
read();
|
70
|
-
}
|
71
|
-
// Annotate the CommonJS export names for ESM import in node:
|
72
|
-
0 && (module.exports = {
|
73
|
-
StreamingTextResponse,
|
74
|
-
streamToResponse
|
75
|
-
});
|
@@ -1,202 +0,0 @@
|
|
1
|
-
'use client'
|
2
|
-
import {
|
3
|
-
__async,
|
4
|
-
__spreadValues,
|
5
|
-
decodeAIStreamChunk,
|
6
|
-
nanoid
|
7
|
-
} from "./chunk-6EH3SWMP.mjs";
|
8
|
-
|
9
|
-
// react/use-chat.ts
|
10
|
-
import { useCallback, useId, useRef, useEffect, useState } from "react";
|
11
|
-
import useSWRMutation from "swr/mutation";
|
12
|
-
import useSWR from "swr";
|
13
|
-
function useChat({
|
14
|
-
api = "/api/chat",
|
15
|
-
id,
|
16
|
-
initialMessages = [],
|
17
|
-
initialInput = "",
|
18
|
-
sendExtraMessageFields,
|
19
|
-
onResponse,
|
20
|
-
onFinish,
|
21
|
-
onError,
|
22
|
-
headers,
|
23
|
-
body
|
24
|
-
} = {}) {
|
25
|
-
const hookId = useId();
|
26
|
-
const chatId = id || hookId;
|
27
|
-
const { data, mutate } = useSWR([api, chatId], null, {
|
28
|
-
fallbackData: initialMessages
|
29
|
-
});
|
30
|
-
const messages = data;
|
31
|
-
const messagesRef = useRef(messages);
|
32
|
-
useEffect(() => {
|
33
|
-
messagesRef.current = messages;
|
34
|
-
}, [messages]);
|
35
|
-
const abortControllerRef = useRef(null);
|
36
|
-
const extraMetadataRef = useRef({
|
37
|
-
headers,
|
38
|
-
body
|
39
|
-
});
|
40
|
-
useEffect(() => {
|
41
|
-
extraMetadataRef.current = {
|
42
|
-
headers,
|
43
|
-
body
|
44
|
-
};
|
45
|
-
}, [headers, body]);
|
46
|
-
const { error, trigger, isMutating } = useSWRMutation(
|
47
|
-
[api, chatId],
|
48
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: messagesSnapshot }) {
|
49
|
-
try {
|
50
|
-
const abortController = new AbortController();
|
51
|
-
abortControllerRef.current = abortController;
|
52
|
-
const previousMessages = messagesRef.current;
|
53
|
-
mutate(messagesSnapshot, false);
|
54
|
-
const res = yield fetch(api, {
|
55
|
-
method: "POST",
|
56
|
-
body: JSON.stringify(__spreadValues({
|
57
|
-
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
58
|
-
role,
|
59
|
-
content
|
60
|
-
}))
|
61
|
-
}, extraMetadataRef.current.body)),
|
62
|
-
headers: extraMetadataRef.current.headers || {},
|
63
|
-
signal: abortController.signal
|
64
|
-
}).catch((err) => {
|
65
|
-
mutate(previousMessages, false);
|
66
|
-
throw err;
|
67
|
-
});
|
68
|
-
if (onResponse) {
|
69
|
-
try {
|
70
|
-
yield onResponse(res);
|
71
|
-
} catch (err) {
|
72
|
-
throw err;
|
73
|
-
}
|
74
|
-
}
|
75
|
-
if (!res.ok) {
|
76
|
-
mutate(previousMessages, false);
|
77
|
-
throw new Error(
|
78
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
79
|
-
);
|
80
|
-
}
|
81
|
-
if (!res.body) {
|
82
|
-
throw new Error("The response body is empty.");
|
83
|
-
}
|
84
|
-
let result = "";
|
85
|
-
const createdAt = /* @__PURE__ */ new Date();
|
86
|
-
const replyId = nanoid();
|
87
|
-
const reader = res.body.getReader();
|
88
|
-
while (true) {
|
89
|
-
const { done, value } = yield reader.read();
|
90
|
-
if (done) {
|
91
|
-
break;
|
92
|
-
}
|
93
|
-
result += decodeAIStreamChunk(value);
|
94
|
-
mutate(
|
95
|
-
[
|
96
|
-
...messagesSnapshot,
|
97
|
-
{
|
98
|
-
id: replyId,
|
99
|
-
createdAt,
|
100
|
-
content: result,
|
101
|
-
role: "assistant"
|
102
|
-
}
|
103
|
-
],
|
104
|
-
false
|
105
|
-
);
|
106
|
-
if (abortControllerRef.current === null) {
|
107
|
-
reader.cancel();
|
108
|
-
break;
|
109
|
-
}
|
110
|
-
}
|
111
|
-
if (onFinish) {
|
112
|
-
onFinish({
|
113
|
-
id: replyId,
|
114
|
-
createdAt,
|
115
|
-
content: result,
|
116
|
-
role: "assistant"
|
117
|
-
});
|
118
|
-
}
|
119
|
-
abortControllerRef.current = null;
|
120
|
-
return result;
|
121
|
-
} catch (err) {
|
122
|
-
if (err.name === "AbortError") {
|
123
|
-
abortControllerRef.current = null;
|
124
|
-
return null;
|
125
|
-
}
|
126
|
-
if (onError && err instanceof Error) {
|
127
|
-
onError(err);
|
128
|
-
}
|
129
|
-
throw err;
|
130
|
-
}
|
131
|
-
}),
|
132
|
-
{
|
133
|
-
populateCache: false,
|
134
|
-
revalidate: false
|
135
|
-
}
|
136
|
-
);
|
137
|
-
const append = useCallback(
|
138
|
-
(message) => __async(this, null, function* () {
|
139
|
-
if (!message.id) {
|
140
|
-
message.id = nanoid();
|
141
|
-
}
|
142
|
-
return trigger(messagesRef.current.concat(message));
|
143
|
-
}),
|
144
|
-
[trigger]
|
145
|
-
);
|
146
|
-
const reload = useCallback(() => __async(this, null, function* () {
|
147
|
-
if (messagesRef.current.length === 0)
|
148
|
-
return null;
|
149
|
-
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
150
|
-
if (lastMessage.role === "assistant") {
|
151
|
-
return trigger(messagesRef.current.slice(0, -1));
|
152
|
-
}
|
153
|
-
return trigger(messagesRef.current);
|
154
|
-
}), [trigger]);
|
155
|
-
const stop = useCallback(() => {
|
156
|
-
if (abortControllerRef.current) {
|
157
|
-
abortControllerRef.current.abort();
|
158
|
-
abortControllerRef.current = null;
|
159
|
-
}
|
160
|
-
}, []);
|
161
|
-
const setMessages = useCallback(
|
162
|
-
(messages2) => {
|
163
|
-
mutate(messages2, false);
|
164
|
-
messagesRef.current = messages2;
|
165
|
-
},
|
166
|
-
[mutate]
|
167
|
-
);
|
168
|
-
const [input, setInput] = useState(initialInput);
|
169
|
-
const handleSubmit = useCallback(
|
170
|
-
(e) => {
|
171
|
-
e.preventDefault();
|
172
|
-
if (!input)
|
173
|
-
return;
|
174
|
-
append({
|
175
|
-
content: input,
|
176
|
-
role: "user"
|
177
|
-
});
|
178
|
-
setInput("");
|
179
|
-
},
|
180
|
-
[input, append]
|
181
|
-
);
|
182
|
-
const handleInputChange = (e) => {
|
183
|
-
setInput(e.target.value);
|
184
|
-
};
|
185
|
-
return {
|
186
|
-
messages,
|
187
|
-
error,
|
188
|
-
append,
|
189
|
-
reload,
|
190
|
-
stop,
|
191
|
-
setMessages,
|
192
|
-
input,
|
193
|
-
setInput,
|
194
|
-
handleInputChange,
|
195
|
-
handleSubmit,
|
196
|
-
isLoading: isMutating
|
197
|
-
};
|
198
|
-
}
|
199
|
-
|
200
|
-
export {
|
201
|
-
useChat
|
202
|
-
};
|
@@ -1,55 +0,0 @@
|
|
1
|
-
'use client'
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
5
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
7
|
-
var __spreadValues = (a, b) => {
|
8
|
-
for (var prop in b || (b = {}))
|
9
|
-
if (__hasOwnProp.call(b, prop))
|
10
|
-
__defNormalProp(a, prop, b[prop]);
|
11
|
-
if (__getOwnPropSymbols)
|
12
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
13
|
-
if (__propIsEnum.call(b, prop))
|
14
|
-
__defNormalProp(a, prop, b[prop]);
|
15
|
-
}
|
16
|
-
return a;
|
17
|
-
};
|
18
|
-
var __async = (__this, __arguments, generator) => {
|
19
|
-
return new Promise((resolve, reject) => {
|
20
|
-
var fulfilled = (value) => {
|
21
|
-
try {
|
22
|
-
step(generator.next(value));
|
23
|
-
} catch (e) {
|
24
|
-
reject(e);
|
25
|
-
}
|
26
|
-
};
|
27
|
-
var rejected = (value) => {
|
28
|
-
try {
|
29
|
-
step(generator.throw(value));
|
30
|
-
} catch (e) {
|
31
|
-
reject(e);
|
32
|
-
}
|
33
|
-
};
|
34
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
35
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
36
|
-
});
|
37
|
-
};
|
38
|
-
|
39
|
-
// shared/utils.ts
|
40
|
-
import { customAlphabet } from "nanoid";
|
41
|
-
var nanoid = customAlphabet(
|
42
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
43
|
-
7
|
44
|
-
);
|
45
|
-
var decoder = new TextDecoder();
|
46
|
-
function decodeAIStreamChunk(chunk) {
|
47
|
-
return decoder.decode(chunk);
|
48
|
-
}
|
49
|
-
|
50
|
-
export {
|
51
|
-
__spreadValues,
|
52
|
-
__async,
|
53
|
-
nanoid,
|
54
|
-
decodeAIStreamChunk
|
55
|
-
};
|
@@ -1,154 +0,0 @@
|
|
1
|
-
'use client'
|
2
|
-
import {
|
3
|
-
__async,
|
4
|
-
__spreadValues,
|
5
|
-
decodeAIStreamChunk
|
6
|
-
} from "./chunk-6EH3SWMP.mjs";
|
7
|
-
|
8
|
-
// react/use-completion.ts
|
9
|
-
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
10
|
-
import useSWRMutation from "swr/mutation";
|
11
|
-
import useSWR from "swr";
|
12
|
-
function useCompletion({
|
13
|
-
api = "/api/completion",
|
14
|
-
id,
|
15
|
-
initialCompletion = "",
|
16
|
-
initialInput = "",
|
17
|
-
headers,
|
18
|
-
body,
|
19
|
-
onResponse,
|
20
|
-
onFinish,
|
21
|
-
onError
|
22
|
-
} = {}) {
|
23
|
-
const hookId = useId();
|
24
|
-
const completionId = id || hookId;
|
25
|
-
const { data, mutate } = useSWR([api, completionId], null, {
|
26
|
-
fallbackData: initialCompletion
|
27
|
-
});
|
28
|
-
const completion = data;
|
29
|
-
const [abortController, setAbortController] = useState(null);
|
30
|
-
const extraMetadataRef = useRef({
|
31
|
-
headers,
|
32
|
-
body
|
33
|
-
});
|
34
|
-
useEffect(() => {
|
35
|
-
extraMetadataRef.current = {
|
36
|
-
headers,
|
37
|
-
body
|
38
|
-
};
|
39
|
-
}, [headers, body]);
|
40
|
-
const { error, trigger, isMutating } = useSWRMutation(
|
41
|
-
[api, completionId],
|
42
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: prompt }) {
|
43
|
-
try {
|
44
|
-
const abortController2 = new AbortController();
|
45
|
-
setAbortController(abortController2);
|
46
|
-
mutate("", false);
|
47
|
-
const res = yield fetch(api, {
|
48
|
-
method: "POST",
|
49
|
-
body: JSON.stringify(__spreadValues({
|
50
|
-
prompt
|
51
|
-
}, extraMetadataRef.current.body)),
|
52
|
-
headers: extraMetadataRef.current.headers || {},
|
53
|
-
signal: abortController2.signal
|
54
|
-
}).catch((err) => {
|
55
|
-
throw err;
|
56
|
-
});
|
57
|
-
if (onResponse) {
|
58
|
-
try {
|
59
|
-
yield onResponse(res);
|
60
|
-
} catch (err) {
|
61
|
-
throw err;
|
62
|
-
}
|
63
|
-
}
|
64
|
-
if (!res.ok) {
|
65
|
-
throw new Error(
|
66
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
67
|
-
);
|
68
|
-
}
|
69
|
-
if (!res.body) {
|
70
|
-
throw new Error("The response body is empty.");
|
71
|
-
}
|
72
|
-
let result = "";
|
73
|
-
const reader = res.body.getReader();
|
74
|
-
while (true) {
|
75
|
-
const { done, value } = yield reader.read();
|
76
|
-
if (done) {
|
77
|
-
break;
|
78
|
-
}
|
79
|
-
result += decodeAIStreamChunk(value);
|
80
|
-
mutate(result, false);
|
81
|
-
if (abortController2 === null) {
|
82
|
-
reader.cancel();
|
83
|
-
break;
|
84
|
-
}
|
85
|
-
}
|
86
|
-
if (onFinish) {
|
87
|
-
onFinish(prompt, result);
|
88
|
-
}
|
89
|
-
setAbortController(null);
|
90
|
-
return result;
|
91
|
-
} catch (err) {
|
92
|
-
if (err.name === "AbortError") {
|
93
|
-
setAbortController(null);
|
94
|
-
return null;
|
95
|
-
}
|
96
|
-
if (onError && err instanceof Error) {
|
97
|
-
onError(err);
|
98
|
-
}
|
99
|
-
throw err;
|
100
|
-
}
|
101
|
-
}),
|
102
|
-
{
|
103
|
-
populateCache: false,
|
104
|
-
revalidate: false
|
105
|
-
}
|
106
|
-
);
|
107
|
-
const stop = useCallback(() => {
|
108
|
-
if (abortController) {
|
109
|
-
abortController.abort();
|
110
|
-
setAbortController(null);
|
111
|
-
}
|
112
|
-
}, [abortController]);
|
113
|
-
const setCompletion = useCallback(
|
114
|
-
(completion2) => {
|
115
|
-
mutate(completion2, false);
|
116
|
-
},
|
117
|
-
[mutate]
|
118
|
-
);
|
119
|
-
const [input, setInput] = useState(initialInput);
|
120
|
-
const handleSubmit = useCallback(
|
121
|
-
(e) => {
|
122
|
-
e.preventDefault();
|
123
|
-
if (!input)
|
124
|
-
return;
|
125
|
-
return trigger(input);
|
126
|
-
},
|
127
|
-
[input, trigger]
|
128
|
-
);
|
129
|
-
const handleInputChange = (e) => {
|
130
|
-
setInput(e.target.value);
|
131
|
-
};
|
132
|
-
const complete = useCallback(
|
133
|
-
(prompt) => __async(this, null, function* () {
|
134
|
-
return trigger(prompt);
|
135
|
-
}),
|
136
|
-
[trigger]
|
137
|
-
);
|
138
|
-
return {
|
139
|
-
completion,
|
140
|
-
complete,
|
141
|
-
error,
|
142
|
-
setCompletion,
|
143
|
-
stop,
|
144
|
-
input,
|
145
|
-
setInput,
|
146
|
-
handleInputChange,
|
147
|
-
handleSubmit,
|
148
|
-
isLoading: isMutating
|
149
|
-
};
|
150
|
-
}
|
151
|
-
|
152
|
-
export {
|
153
|
-
useCompletion
|
154
|
-
};
|