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/chunk-265FSSO4.mjs
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
__async
|
3
|
-
} from "./chunk-2L3ZO4UM.mjs";
|
4
|
-
|
5
|
-
// streams/ai-stream.ts
|
6
|
-
import {
|
7
|
-
createParser
|
8
|
-
} from "eventsource-parser";
|
9
|
-
function createEventStreamTransformer(customParser) {
|
10
|
-
const decoder = new TextDecoder();
|
11
|
-
let parser;
|
12
|
-
return new TransformStream({
|
13
|
-
start(controller) {
|
14
|
-
return __async(this, null, function* () {
|
15
|
-
function onParse(event) {
|
16
|
-
if (event.type === "event") {
|
17
|
-
const data = event.data;
|
18
|
-
if (data === "[DONE]") {
|
19
|
-
controller.terminate();
|
20
|
-
return;
|
21
|
-
}
|
22
|
-
const message = customParser(data);
|
23
|
-
if (message)
|
24
|
-
controller.enqueue(message);
|
25
|
-
}
|
26
|
-
}
|
27
|
-
parser = createParser(onParse);
|
28
|
-
});
|
29
|
-
},
|
30
|
-
transform(chunk) {
|
31
|
-
parser.feed(decoder.decode(chunk));
|
32
|
-
}
|
33
|
-
});
|
34
|
-
}
|
35
|
-
function createCallbacksTransformer(callbacks) {
|
36
|
-
const encoder = new TextEncoder();
|
37
|
-
let fullResponse = "";
|
38
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
39
|
-
return new TransformStream({
|
40
|
-
start() {
|
41
|
-
return __async(this, null, function* () {
|
42
|
-
if (onStart)
|
43
|
-
yield onStart();
|
44
|
-
});
|
45
|
-
},
|
46
|
-
transform(message, controller) {
|
47
|
-
return __async(this, null, function* () {
|
48
|
-
controller.enqueue(encoder.encode(message));
|
49
|
-
if (onToken)
|
50
|
-
yield onToken(message);
|
51
|
-
if (onCompletion)
|
52
|
-
fullResponse += message;
|
53
|
-
});
|
54
|
-
},
|
55
|
-
flush() {
|
56
|
-
return __async(this, null, function* () {
|
57
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
58
|
-
});
|
59
|
-
}
|
60
|
-
});
|
61
|
-
}
|
62
|
-
function trimStartOfStreamHelper() {
|
63
|
-
let start = true;
|
64
|
-
return (text) => {
|
65
|
-
if (start)
|
66
|
-
text = text.trimStart();
|
67
|
-
if (text)
|
68
|
-
start = false;
|
69
|
-
return text;
|
70
|
-
};
|
71
|
-
}
|
72
|
-
function AIStream(res, customParser, callbacks) {
|
73
|
-
if (!res.ok) {
|
74
|
-
throw new Error(
|
75
|
-
`Failed to convert the response to stream. Received status code: ${res.status}.`
|
76
|
-
);
|
77
|
-
}
|
78
|
-
const stream = res.body || new ReadableStream({
|
79
|
-
start(controller) {
|
80
|
-
controller.close();
|
81
|
-
}
|
82
|
-
});
|
83
|
-
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
84
|
-
}
|
85
|
-
|
86
|
-
export {
|
87
|
-
createEventStreamTransformer,
|
88
|
-
createCallbacksTransformer,
|
89
|
-
trimStartOfStreamHelper,
|
90
|
-
AIStream
|
91
|
-
};
|
package/dist/chunk-2L3ZO4UM.mjs
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __defProps = Object.defineProperties;
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
-
var __spreadValues = (a, b) => {
|
9
|
-
for (var prop in b || (b = {}))
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
12
|
-
if (__getOwnPropSymbols)
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
-
if (__propIsEnum.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
}
|
17
|
-
return a;
|
18
|
-
};
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20
|
-
var __async = (__this, __arguments, generator) => {
|
21
|
-
return new Promise((resolve, reject) => {
|
22
|
-
var fulfilled = (value) => {
|
23
|
-
try {
|
24
|
-
step(generator.next(value));
|
25
|
-
} catch (e) {
|
26
|
-
reject(e);
|
27
|
-
}
|
28
|
-
};
|
29
|
-
var rejected = (value) => {
|
30
|
-
try {
|
31
|
-
step(generator.throw(value));
|
32
|
-
} catch (e) {
|
33
|
-
reject(e);
|
34
|
-
}
|
35
|
-
};
|
36
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
37
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
38
|
-
});
|
39
|
-
};
|
40
|
-
|
41
|
-
export {
|
42
|
-
__spreadValues,
|
43
|
-
__spreadProps,
|
44
|
-
__async
|
45
|
-
};
|
package/dist/chunk-GT4HKF2X.mjs
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
createCallbacksTransformer
|
3
|
-
} from "./chunk-265FSSO4.mjs";
|
4
|
-
import {
|
5
|
-
__async
|
6
|
-
} from "./chunk-2L3ZO4UM.mjs";
|
7
|
-
|
8
|
-
// streams/langchain-stream.ts
|
9
|
-
function LangChainStream(callbacks) {
|
10
|
-
const stream = new TransformStream();
|
11
|
-
const writer = stream.writable.getWriter();
|
12
|
-
return {
|
13
|
-
stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)),
|
14
|
-
handlers: {
|
15
|
-
handleLLMNewToken: (token) => __async(this, null, function* () {
|
16
|
-
yield writer.ready;
|
17
|
-
yield writer.write(token);
|
18
|
-
}),
|
19
|
-
handleChainEnd: () => __async(this, null, function* () {
|
20
|
-
yield writer.ready;
|
21
|
-
yield writer.close();
|
22
|
-
}),
|
23
|
-
handleLLMError: (e) => __async(this, null, function* () {
|
24
|
-
yield writer.ready;
|
25
|
-
yield writer.abort(e);
|
26
|
-
})
|
27
|
-
}
|
28
|
-
};
|
29
|
-
}
|
30
|
-
|
31
|
-
export {
|
32
|
-
LangChainStream
|
33
|
-
};
|
package/dist/chunk-JGDC3BXD.mjs
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
AIStream
|
3
|
-
} from "./chunk-265FSSO4.mjs";
|
4
|
-
|
5
|
-
// streams/anthropic-stream.ts
|
6
|
-
function parseAnthropicStream() {
|
7
|
-
let previous = "";
|
8
|
-
return (data) => {
|
9
|
-
const json = JSON.parse(data);
|
10
|
-
const text = json.completion;
|
11
|
-
const delta = text.slice(previous.length);
|
12
|
-
previous = text;
|
13
|
-
return delta;
|
14
|
-
};
|
15
|
-
}
|
16
|
-
function AnthropicStream(res, cb) {
|
17
|
-
return AIStream(res, parseAnthropicStream(), cb);
|
18
|
-
}
|
19
|
-
|
20
|
-
export {
|
21
|
-
AnthropicStream
|
22
|
-
};
|
package/dist/chunk-NK2CVBLI.mjs
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
__spreadProps,
|
3
|
-
__spreadValues
|
4
|
-
} from "./chunk-2L3ZO4UM.mjs";
|
5
|
-
|
6
|
-
// streams/streaming-text-response.ts
|
7
|
-
var StreamingTextResponse = class extends Response {
|
8
|
-
constructor(res, init) {
|
9
|
-
super(res, __spreadProps(__spreadValues({}, init), {
|
10
|
-
status: 200,
|
11
|
-
headers: __spreadValues({
|
12
|
-
"Content-Type": "text/plain; charset=utf-8"
|
13
|
-
}, init == null ? void 0 : init.headers)
|
14
|
-
}));
|
15
|
-
}
|
16
|
-
};
|
17
|
-
function streamToResponse(res, response, init) {
|
18
|
-
response.writeHead((init == null ? void 0 : init.status) || 200, __spreadValues({
|
19
|
-
"Content-Type": "text/plain; charset=utf-8"
|
20
|
-
}, init == null ? void 0 : init.headers));
|
21
|
-
const reader = res.getReader();
|
22
|
-
function read() {
|
23
|
-
reader.read().then(({ done, value }) => {
|
24
|
-
if (done) {
|
25
|
-
response.end();
|
26
|
-
return;
|
27
|
-
}
|
28
|
-
response.write(value);
|
29
|
-
read();
|
30
|
-
});
|
31
|
-
}
|
32
|
-
read();
|
33
|
-
}
|
34
|
-
|
35
|
-
export {
|
36
|
-
StreamingTextResponse,
|
37
|
-
streamToResponse
|
38
|
-
};
|
package/dist/chunk-PEYAHBDF.mjs
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
createCallbacksTransformer,
|
3
|
-
trimStartOfStreamHelper
|
4
|
-
} from "./chunk-265FSSO4.mjs";
|
5
|
-
import {
|
6
|
-
__async
|
7
|
-
} from "./chunk-2L3ZO4UM.mjs";
|
8
|
-
|
9
|
-
// streams/huggingface-stream.ts
|
10
|
-
function createParser(res) {
|
11
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
12
|
-
return new ReadableStream({
|
13
|
-
pull(controller) {
|
14
|
-
return __async(this, null, function* () {
|
15
|
-
var _a2, _b;
|
16
|
-
const { value, done } = yield res.next();
|
17
|
-
if (done) {
|
18
|
-
controller.close();
|
19
|
-
return;
|
20
|
-
}
|
21
|
-
const text = trimStartOfStream((_b = (_a2 = value.token) == null ? void 0 : _a2.text) != null ? _b : "");
|
22
|
-
if (!text)
|
23
|
-
return;
|
24
|
-
if (value.generated_text != null && value.generated_text.length > 0) {
|
25
|
-
controller.close();
|
26
|
-
return;
|
27
|
-
}
|
28
|
-
if (text === "</s>" || text === "<|endoftext|>") {
|
29
|
-
controller.close();
|
30
|
-
} else {
|
31
|
-
controller.enqueue(text);
|
32
|
-
}
|
33
|
-
});
|
34
|
-
}
|
35
|
-
});
|
36
|
-
}
|
37
|
-
function HuggingFaceStream(res, callbacks) {
|
38
|
-
return createParser(res).pipeThrough(createCallbacksTransformer(callbacks));
|
39
|
-
}
|
40
|
-
|
41
|
-
export {
|
42
|
-
HuggingFaceStream
|
43
|
-
};
|
package/dist/chunk-TJMME6CL.mjs
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
AIStream,
|
3
|
-
trimStartOfStreamHelper
|
4
|
-
} from "./chunk-265FSSO4.mjs";
|
5
|
-
|
6
|
-
// streams/openai-stream.ts
|
7
|
-
function parseOpenAIStream() {
|
8
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
9
|
-
return (data) => {
|
10
|
-
var _a, _b, _c, _d, _e;
|
11
|
-
const json = JSON.parse(data);
|
12
|
-
const text = trimStartOfStream(
|
13
|
-
(_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 : ""
|
14
|
-
);
|
15
|
-
return text;
|
16
|
-
};
|
17
|
-
}
|
18
|
-
function OpenAIStream(res, cb) {
|
19
|
-
return AIStream(res, parseOpenAIStream(), cb);
|
20
|
-
}
|
21
|
-
|
22
|
-
export {
|
23
|
-
OpenAIStream
|
24
|
-
};
|
@@ -1,121 +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/huggingface-stream.ts
|
41
|
-
var huggingface_stream_exports = {};
|
42
|
-
__export(huggingface_stream_exports, {
|
43
|
-
HuggingFaceStream: () => HuggingFaceStream
|
44
|
-
});
|
45
|
-
module.exports = __toCommonJS(huggingface_stream_exports);
|
46
|
-
|
47
|
-
// streams/ai-stream.ts
|
48
|
-
var import_eventsource_parser = require("eventsource-parser");
|
49
|
-
function createCallbacksTransformer(callbacks) {
|
50
|
-
const encoder = new TextEncoder();
|
51
|
-
let fullResponse = "";
|
52
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
53
|
-
return new TransformStream({
|
54
|
-
start() {
|
55
|
-
return __async(this, null, function* () {
|
56
|
-
if (onStart)
|
57
|
-
yield onStart();
|
58
|
-
});
|
59
|
-
},
|
60
|
-
transform(message, controller) {
|
61
|
-
return __async(this, null, function* () {
|
62
|
-
controller.enqueue(encoder.encode(message));
|
63
|
-
if (onToken)
|
64
|
-
yield onToken(message);
|
65
|
-
if (onCompletion)
|
66
|
-
fullResponse += message;
|
67
|
-
});
|
68
|
-
},
|
69
|
-
flush() {
|
70
|
-
return __async(this, null, function* () {
|
71
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
72
|
-
});
|
73
|
-
}
|
74
|
-
});
|
75
|
-
}
|
76
|
-
function trimStartOfStreamHelper() {
|
77
|
-
let start = true;
|
78
|
-
return (text) => {
|
79
|
-
if (start)
|
80
|
-
text = text.trimStart();
|
81
|
-
if (text)
|
82
|
-
start = false;
|
83
|
-
return text;
|
84
|
-
};
|
85
|
-
}
|
86
|
-
|
87
|
-
// streams/huggingface-stream.ts
|
88
|
-
function createParser2(res) {
|
89
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
90
|
-
return new ReadableStream({
|
91
|
-
pull(controller) {
|
92
|
-
return __async(this, null, function* () {
|
93
|
-
var _a2, _b;
|
94
|
-
const { value, done } = yield res.next();
|
95
|
-
if (done) {
|
96
|
-
controller.close();
|
97
|
-
return;
|
98
|
-
}
|
99
|
-
const text = trimStartOfStream((_b = (_a2 = value.token) == null ? void 0 : _a2.text) != null ? _b : "");
|
100
|
-
if (!text)
|
101
|
-
return;
|
102
|
-
if (value.generated_text != null && value.generated_text.length > 0) {
|
103
|
-
controller.close();
|
104
|
-
return;
|
105
|
-
}
|
106
|
-
if (text === "</s>" || text === "<|endoftext|>") {
|
107
|
-
controller.close();
|
108
|
-
} else {
|
109
|
-
controller.enqueue(text);
|
110
|
-
}
|
111
|
-
});
|
112
|
-
}
|
113
|
-
});
|
114
|
-
}
|
115
|
-
function HuggingFaceStream(res, callbacks) {
|
116
|
-
return createParser2(res).pipeThrough(createCallbacksTransformer(callbacks));
|
117
|
-
}
|
118
|
-
// Annotate the CommonJS export names for ESM import in node:
|
119
|
-
0 && (module.exports = {
|
120
|
-
HuggingFaceStream
|
121
|
-
});
|
package/dist/index.test.d.ts
DELETED
package/dist/index.test.js
DELETED
package/dist/index.test.mjs
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import { AIStreamCallbacks } from './ai-stream.js';
|
2
|
-
|
3
|
-
declare function LangChainStream(callbacks?: AIStreamCallbacks): {
|
4
|
-
stream: ReadableStream<Uint8Array>;
|
5
|
-
handlers: {
|
6
|
-
handleLLMNewToken: (token: string) => Promise<void>;
|
7
|
-
handleChainEnd: () => Promise<void>;
|
8
|
-
handleLLMError: (e: any) => Promise<void>;
|
9
|
-
};
|
10
|
-
};
|
11
|
-
|
12
|
-
export { LangChainStream };
|
package/dist/langchain-stream.js
DELETED
@@ -1,102 +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/langchain-stream.ts
|
41
|
-
var langchain_stream_exports = {};
|
42
|
-
__export(langchain_stream_exports, {
|
43
|
-
LangChainStream: () => LangChainStream
|
44
|
-
});
|
45
|
-
module.exports = __toCommonJS(langchain_stream_exports);
|
46
|
-
|
47
|
-
// streams/ai-stream.ts
|
48
|
-
var import_eventsource_parser = require("eventsource-parser");
|
49
|
-
function createCallbacksTransformer(callbacks) {
|
50
|
-
const encoder = new TextEncoder();
|
51
|
-
let fullResponse = "";
|
52
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
53
|
-
return new TransformStream({
|
54
|
-
start() {
|
55
|
-
return __async(this, null, function* () {
|
56
|
-
if (onStart)
|
57
|
-
yield onStart();
|
58
|
-
});
|
59
|
-
},
|
60
|
-
transform(message, controller) {
|
61
|
-
return __async(this, null, function* () {
|
62
|
-
controller.enqueue(encoder.encode(message));
|
63
|
-
if (onToken)
|
64
|
-
yield onToken(message);
|
65
|
-
if (onCompletion)
|
66
|
-
fullResponse += message;
|
67
|
-
});
|
68
|
-
},
|
69
|
-
flush() {
|
70
|
-
return __async(this, null, function* () {
|
71
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
72
|
-
});
|
73
|
-
}
|
74
|
-
});
|
75
|
-
}
|
76
|
-
|
77
|
-
// streams/langchain-stream.ts
|
78
|
-
function LangChainStream(callbacks) {
|
79
|
-
const stream = new TransformStream();
|
80
|
-
const writer = stream.writable.getWriter();
|
81
|
-
return {
|
82
|
-
stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)),
|
83
|
-
handlers: {
|
84
|
-
handleLLMNewToken: (token) => __async(this, null, function* () {
|
85
|
-
yield writer.ready;
|
86
|
-
yield writer.write(token);
|
87
|
-
}),
|
88
|
-
handleChainEnd: () => __async(this, null, function* () {
|
89
|
-
yield writer.ready;
|
90
|
-
yield writer.close();
|
91
|
-
}),
|
92
|
-
handleLLMError: (e) => __async(this, null, function* () {
|
93
|
-
yield writer.ready;
|
94
|
-
yield writer.abort(e);
|
95
|
-
})
|
96
|
-
}
|
97
|
-
};
|
98
|
-
}
|
99
|
-
// Annotate the CommonJS export names for ESM import in node:
|
100
|
-
0 && (module.exports = {
|
101
|
-
LangChainStream
|
102
|
-
});
|