@ai-sdk/llamaindex 1.0.0-canary.6 → 1.0.0-canary.8
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/CHANGELOG.md +29 -0
- package/dist/index.d.mts +108 -13
- package/dist/index.d.ts +108 -13
- package/dist/index.js +31 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# @ai-sdk/llamaindex
|
|
2
2
|
|
|
3
|
+
## 1.0.0-canary.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- bc3109f: chore (ai): push stream-callbacks into langchain/llamaindex adapters
|
|
8
|
+
- Updated dependencies [13fef90]
|
|
9
|
+
- Updated dependencies [e90d45d]
|
|
10
|
+
- Updated dependencies [bc3109f]
|
|
11
|
+
- Updated dependencies [496bbc1]
|
|
12
|
+
- Updated dependencies [da70d79]
|
|
13
|
+
- Updated dependencies [bcea599]
|
|
14
|
+
- Updated dependencies [48d675a]
|
|
15
|
+
- Updated dependencies [c7710a9]
|
|
16
|
+
- Updated dependencies [35fc02c]
|
|
17
|
+
- Updated dependencies [b983b51]
|
|
18
|
+
- ai@5.0.0-canary.20
|
|
19
|
+
|
|
20
|
+
## 1.0.0-canary.7
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [2d03e19]
|
|
25
|
+
- Updated dependencies [319b989]
|
|
26
|
+
- Updated dependencies [441d042]
|
|
27
|
+
- Updated dependencies [dcc549b]
|
|
28
|
+
- Updated dependencies [cb2b53a]
|
|
29
|
+
- Updated dependencies [e244a78]
|
|
30
|
+
- ai@5.0.0-canary.19
|
|
31
|
+
|
|
3
32
|
## 1.0.0-canary.6
|
|
4
33
|
|
|
5
34
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options and helper callback methods for stream lifecycle events.
|
|
3
|
+
*/
|
|
4
|
+
interface StreamCallbacks {
|
|
5
|
+
/** `onStart`: Called once when the stream is initialized. */
|
|
6
|
+
onStart?: () => Promise<void> | void;
|
|
7
|
+
/** `onFinal`: Called once when the stream is closed with the final completion message. */
|
|
8
|
+
onFinal?: (completion: string) => Promise<void> | void;
|
|
9
|
+
/** `onToken`: Called for each tokenized message. */
|
|
10
|
+
onToken?: (token: string) => Promise<void> | void;
|
|
11
|
+
/** `onText`: Called for each text chunk. */
|
|
12
|
+
onText?: (text: string) => Promise<void> | void;
|
|
13
|
+
}
|
|
3
14
|
|
|
4
15
|
type EngineResponse = {
|
|
5
16
|
delta: string;
|
|
6
17
|
};
|
|
7
|
-
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
18
|
+
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<{
|
|
19
|
+
type: "text";
|
|
20
|
+
value: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: "data";
|
|
23
|
+
value: any[];
|
|
24
|
+
} | {
|
|
25
|
+
type: "error";
|
|
26
|
+
value: string;
|
|
27
|
+
} | {
|
|
28
|
+
type: "message-annotations";
|
|
29
|
+
value: any[];
|
|
30
|
+
} | {
|
|
31
|
+
type: "tool-call";
|
|
32
|
+
value: {
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
toolName: string;
|
|
35
|
+
args?: unknown;
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
type: "tool-result";
|
|
39
|
+
value: {
|
|
40
|
+
toolCallId: string;
|
|
41
|
+
result?: unknown;
|
|
42
|
+
providerMetadata?: any;
|
|
43
|
+
};
|
|
44
|
+
} | {
|
|
45
|
+
type: "tool-call-streaming-start";
|
|
46
|
+
value: {
|
|
47
|
+
toolCallId: string;
|
|
48
|
+
toolName: string;
|
|
49
|
+
};
|
|
50
|
+
} | {
|
|
51
|
+
type: "tool-call-delta";
|
|
52
|
+
value: {
|
|
53
|
+
toolCallId: string;
|
|
54
|
+
argsTextDelta: string;
|
|
55
|
+
};
|
|
56
|
+
} | {
|
|
57
|
+
type: "finish-message";
|
|
58
|
+
value: {
|
|
59
|
+
finishReason: "length" | "stop" | "tool-calls" | "content-filter" | "other" | "error" | "unknown";
|
|
60
|
+
usage?: {
|
|
61
|
+
inputTokens?: number | undefined;
|
|
62
|
+
outputTokens?: number | undefined;
|
|
63
|
+
totalTokens?: number | undefined;
|
|
64
|
+
reasoningTokens?: number | undefined;
|
|
65
|
+
cachedInputTokens?: number | undefined;
|
|
66
|
+
} | undefined;
|
|
67
|
+
};
|
|
68
|
+
} | {
|
|
69
|
+
type: "finish-step";
|
|
70
|
+
value: {
|
|
71
|
+
finishReason: "length" | "stop" | "tool-calls" | "content-filter" | "other" | "error" | "unknown";
|
|
72
|
+
isContinued: boolean;
|
|
73
|
+
usage?: {
|
|
74
|
+
inputTokens?: number | undefined;
|
|
75
|
+
outputTokens?: number | undefined;
|
|
76
|
+
totalTokens?: number | undefined;
|
|
77
|
+
reasoningTokens?: number | undefined;
|
|
78
|
+
cachedInputTokens?: number | undefined;
|
|
79
|
+
} | undefined;
|
|
80
|
+
};
|
|
81
|
+
} | {
|
|
82
|
+
type: "start-step";
|
|
83
|
+
value: {
|
|
84
|
+
messageId: string;
|
|
85
|
+
};
|
|
86
|
+
} | {
|
|
87
|
+
type: "reasoning";
|
|
88
|
+
value: {
|
|
89
|
+
text: string;
|
|
90
|
+
providerMetadata?: Record<string, any> | undefined;
|
|
91
|
+
};
|
|
92
|
+
} | {
|
|
93
|
+
type: "source";
|
|
94
|
+
value: {
|
|
95
|
+
type: "source";
|
|
96
|
+
sourceType: "url";
|
|
97
|
+
url: string;
|
|
98
|
+
id: string;
|
|
99
|
+
providerMetadata?: any;
|
|
100
|
+
title?: string | undefined;
|
|
101
|
+
};
|
|
102
|
+
} | {
|
|
103
|
+
type: "file";
|
|
104
|
+
value: {
|
|
105
|
+
url: string;
|
|
106
|
+
mediaType: string;
|
|
107
|
+
};
|
|
108
|
+
} | {
|
|
109
|
+
type: "reasoning-part-finish";
|
|
110
|
+
value: null;
|
|
111
|
+
}>;
|
|
17
112
|
|
|
18
|
-
export {
|
|
113
|
+
export { toDataStream };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options and helper callback methods for stream lifecycle events.
|
|
3
|
+
*/
|
|
4
|
+
interface StreamCallbacks {
|
|
5
|
+
/** `onStart`: Called once when the stream is initialized. */
|
|
6
|
+
onStart?: () => Promise<void> | void;
|
|
7
|
+
/** `onFinal`: Called once when the stream is closed with the final completion message. */
|
|
8
|
+
onFinal?: (completion: string) => Promise<void> | void;
|
|
9
|
+
/** `onToken`: Called for each tokenized message. */
|
|
10
|
+
onToken?: (token: string) => Promise<void> | void;
|
|
11
|
+
/** `onText`: Called for each text chunk. */
|
|
12
|
+
onText?: (text: string) => Promise<void> | void;
|
|
13
|
+
}
|
|
3
14
|
|
|
4
15
|
type EngineResponse = {
|
|
5
16
|
delta: string;
|
|
6
17
|
};
|
|
7
|
-
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
18
|
+
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<{
|
|
19
|
+
type: "text";
|
|
20
|
+
value: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: "data";
|
|
23
|
+
value: any[];
|
|
24
|
+
} | {
|
|
25
|
+
type: "error";
|
|
26
|
+
value: string;
|
|
27
|
+
} | {
|
|
28
|
+
type: "message-annotations";
|
|
29
|
+
value: any[];
|
|
30
|
+
} | {
|
|
31
|
+
type: "tool-call";
|
|
32
|
+
value: {
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
toolName: string;
|
|
35
|
+
args?: unknown;
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
type: "tool-result";
|
|
39
|
+
value: {
|
|
40
|
+
toolCallId: string;
|
|
41
|
+
result?: unknown;
|
|
42
|
+
providerMetadata?: any;
|
|
43
|
+
};
|
|
44
|
+
} | {
|
|
45
|
+
type: "tool-call-streaming-start";
|
|
46
|
+
value: {
|
|
47
|
+
toolCallId: string;
|
|
48
|
+
toolName: string;
|
|
49
|
+
};
|
|
50
|
+
} | {
|
|
51
|
+
type: "tool-call-delta";
|
|
52
|
+
value: {
|
|
53
|
+
toolCallId: string;
|
|
54
|
+
argsTextDelta: string;
|
|
55
|
+
};
|
|
56
|
+
} | {
|
|
57
|
+
type: "finish-message";
|
|
58
|
+
value: {
|
|
59
|
+
finishReason: "length" | "stop" | "tool-calls" | "content-filter" | "other" | "error" | "unknown";
|
|
60
|
+
usage?: {
|
|
61
|
+
inputTokens?: number | undefined;
|
|
62
|
+
outputTokens?: number | undefined;
|
|
63
|
+
totalTokens?: number | undefined;
|
|
64
|
+
reasoningTokens?: number | undefined;
|
|
65
|
+
cachedInputTokens?: number | undefined;
|
|
66
|
+
} | undefined;
|
|
67
|
+
};
|
|
68
|
+
} | {
|
|
69
|
+
type: "finish-step";
|
|
70
|
+
value: {
|
|
71
|
+
finishReason: "length" | "stop" | "tool-calls" | "content-filter" | "other" | "error" | "unknown";
|
|
72
|
+
isContinued: boolean;
|
|
73
|
+
usage?: {
|
|
74
|
+
inputTokens?: number | undefined;
|
|
75
|
+
outputTokens?: number | undefined;
|
|
76
|
+
totalTokens?: number | undefined;
|
|
77
|
+
reasoningTokens?: number | undefined;
|
|
78
|
+
cachedInputTokens?: number | undefined;
|
|
79
|
+
} | undefined;
|
|
80
|
+
};
|
|
81
|
+
} | {
|
|
82
|
+
type: "start-step";
|
|
83
|
+
value: {
|
|
84
|
+
messageId: string;
|
|
85
|
+
};
|
|
86
|
+
} | {
|
|
87
|
+
type: "reasoning";
|
|
88
|
+
value: {
|
|
89
|
+
text: string;
|
|
90
|
+
providerMetadata?: Record<string, any> | undefined;
|
|
91
|
+
};
|
|
92
|
+
} | {
|
|
93
|
+
type: "source";
|
|
94
|
+
value: {
|
|
95
|
+
type: "source";
|
|
96
|
+
sourceType: "url";
|
|
97
|
+
url: string;
|
|
98
|
+
id: string;
|
|
99
|
+
providerMetadata?: any;
|
|
100
|
+
title?: string | undefined;
|
|
101
|
+
};
|
|
102
|
+
} | {
|
|
103
|
+
type: "file";
|
|
104
|
+
value: {
|
|
105
|
+
url: string;
|
|
106
|
+
mediaType: string;
|
|
107
|
+
};
|
|
108
|
+
} | {
|
|
109
|
+
type: "reasoning-part-finish";
|
|
110
|
+
value: null;
|
|
111
|
+
}>;
|
|
17
112
|
|
|
18
|
-
export {
|
|
113
|
+
export { toDataStream };
|
package/dist/index.js
CHANGED
|
@@ -20,56 +20,53 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
|
|
24
|
-
toDataStream: () => toDataStream,
|
|
25
|
-
toDataStreamResponse: () => toDataStreamResponse
|
|
23
|
+
toDataStream: () => toDataStream
|
|
26
24
|
});
|
|
27
25
|
module.exports = __toCommonJS(src_exports);
|
|
28
26
|
|
|
29
27
|
// src/llamaindex-adapter.ts
|
|
30
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
31
|
-
var import_ai = require("ai");
|
|
32
28
|
var import_internal = require("ai/internal");
|
|
33
|
-
|
|
29
|
+
|
|
30
|
+
// src/stream-callbacks.ts
|
|
31
|
+
function createCallbacksTransformer(callbacks = {}) {
|
|
32
|
+
let aggregatedResponse = "";
|
|
33
|
+
return new TransformStream({
|
|
34
|
+
async start() {
|
|
35
|
+
if (callbacks.onStart) await callbacks.onStart();
|
|
36
|
+
},
|
|
37
|
+
async transform(message, controller) {
|
|
38
|
+
controller.enqueue(message);
|
|
39
|
+
aggregatedResponse += message;
|
|
40
|
+
if (callbacks.onToken) await callbacks.onToken(message);
|
|
41
|
+
if (callbacks.onText && typeof message === "string") {
|
|
42
|
+
await callbacks.onText(message);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
async flush() {
|
|
46
|
+
if (callbacks.onFinal) {
|
|
47
|
+
await callbacks.onFinal(aggregatedResponse);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/llamaindex-adapter.ts
|
|
54
|
+
function toDataStream(stream, callbacks) {
|
|
34
55
|
const trimStart = trimStartOfStream();
|
|
35
|
-
return (0,
|
|
56
|
+
return (0, import_internal.convertAsyncIteratorToReadableStream)(stream[Symbol.asyncIterator]()).pipeThrough(
|
|
36
57
|
new TransformStream({
|
|
37
58
|
async transform(message, controller) {
|
|
38
59
|
controller.enqueue(trimStart(message.delta));
|
|
39
60
|
}
|
|
40
61
|
})
|
|
41
|
-
).pipeThrough(
|
|
62
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
|
42
63
|
new TransformStream({
|
|
43
64
|
transform: async (chunk, controller) => {
|
|
44
|
-
controller.enqueue(
|
|
65
|
+
controller.enqueue({ type: "text", value: chunk });
|
|
45
66
|
}
|
|
46
67
|
})
|
|
47
68
|
);
|
|
48
69
|
}
|
|
49
|
-
function toDataStream(stream, callbacks) {
|
|
50
|
-
return toDataStreamInternal(stream, callbacks).pipeThrough(
|
|
51
|
-
new TextEncoderStream()
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
function toDataStreamResponse(stream, options = {}) {
|
|
55
|
-
var _a;
|
|
56
|
-
const { init, data, callbacks } = options;
|
|
57
|
-
const dataStream = toDataStreamInternal(stream, callbacks).pipeThrough(
|
|
58
|
-
new TextEncoderStream()
|
|
59
|
-
);
|
|
60
|
-
const responseStream = data ? (0, import_internal.mergeStreams)(data.stream, dataStream) : dataStream;
|
|
61
|
-
return new Response(responseStream, {
|
|
62
|
-
status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
|
|
63
|
-
statusText: init == null ? void 0 : init.statusText,
|
|
64
|
-
headers: (0, import_internal.prepareResponseHeaders)(init == null ? void 0 : init.headers, {
|
|
65
|
-
contentType: "text/plain; charset=utf-8",
|
|
66
|
-
dataStreamVersion: "v1"
|
|
67
|
-
})
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
function mergeIntoDataStream(stream, options) {
|
|
71
|
-
options.dataStream.merge(toDataStreamInternal(stream, options.callbacks));
|
|
72
|
-
}
|
|
73
70
|
function trimStartOfStream() {
|
|
74
71
|
let isStreamStart = true;
|
|
75
72
|
return (text) => {
|
|
@@ -82,8 +79,6 @@ function trimStartOfStream() {
|
|
|
82
79
|
}
|
|
83
80
|
// Annotate the CommonJS export names for ESM import in node:
|
|
84
81
|
0 && (module.exports = {
|
|
85
|
-
|
|
86
|
-
toDataStream,
|
|
87
|
-
toDataStreamResponse
|
|
82
|
+
toDataStream
|
|
88
83
|
});
|
|
89
84
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/llamaindex-adapter.ts"],"sourcesContent":["export * from './llamaindex-adapter';\n","import {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/llamaindex-adapter.ts","../src/stream-callbacks.ts"],"sourcesContent":["export * from './llamaindex-adapter';\n","import { DataStreamPart } from 'ai';\nimport { convertAsyncIteratorToReadableStream } from 'ai/internal';\nimport {\n createCallbacksTransformer,\n StreamCallbacks,\n} from './stream-callbacks';\n\ntype EngineResponse = {\n delta: string;\n};\n\nexport function toDataStream(\n stream: AsyncIterable<EngineResponse>,\n callbacks?: StreamCallbacks,\n) {\n const trimStart = trimStartOfStream();\n\n return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]())\n .pipeThrough(\n new TransformStream({\n async transform(message, controller): Promise<void> {\n controller.enqueue(trimStart(message.delta));\n },\n }),\n )\n .pipeThrough(createCallbacksTransformer(callbacks))\n .pipeThrough(\n new TransformStream<string, DataStreamPart>({\n transform: async (chunk, controller) => {\n controller.enqueue({ type: 'text', value: chunk });\n },\n }),\n );\n}\n\nfunction trimStartOfStream(): (text: string) => string {\n let isStreamStart = true;\n\n return (text: string): string => {\n if (isStreamStart) {\n text = text.trimStart();\n if (text) isStreamStart = false;\n }\n return text;\n };\n}\n","/**\n * Configuration options and helper callback methods for stream lifecycle events.\n */\nexport interface StreamCallbacks {\n /** `onStart`: Called once when the stream is initialized. */\n onStart?: () => Promise<void> | void;\n\n /** `onFinal`: Called once when the stream is closed with the final completion message. */\n onFinal?: (completion: string) => Promise<void> | void;\n\n /** `onToken`: Called for each tokenized message. */\n onToken?: (token: string) => Promise<void> | void;\n\n /** `onText`: Called for each text chunk. */\n onText?: (text: string) => Promise<void> | void;\n}\n\n/**\n * Creates a transform stream that encodes input messages and invokes optional callback functions.\n * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.\n * - `onStart`: Called once when the stream is initialized.\n * - `onToken`: Called for each tokenized message.\n * - `onFinal`: Called once when the stream is closed with the final completion message.\n *\n * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.\n *\n * @param {StreamCallbacks} [callbacks] - An object containing the callback functions.\n * @return {TransformStream<string, Uint8Array>} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.\n *\n * @example\n * const callbacks = {\n * onStart: async () => console.log('Stream started'),\n * onToken: async (token) => console.log(`Token: ${token}`),\n * onFinal: async () => data.close()\n * };\n * const transformer = createCallbacksTransformer(callbacks);\n */\nexport function createCallbacksTransformer(\n callbacks: StreamCallbacks | undefined = {},\n): TransformStream<string, string> {\n let aggregatedResponse = '';\n\n return new TransformStream({\n async start(): Promise<void> {\n if (callbacks.onStart) await callbacks.onStart();\n },\n\n async transform(message, controller): Promise<void> {\n controller.enqueue(message);\n\n aggregatedResponse += message;\n\n if (callbacks.onToken) await callbacks.onToken(message);\n if (callbacks.onText && typeof message === 'string') {\n await callbacks.onText(message);\n }\n },\n\n async flush(): Promise<void> {\n if (callbacks.onFinal) {\n await callbacks.onFinal(aggregatedResponse);\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,sBAAqD;;;ACoC9C,SAAS,2BACd,YAAyC,CAAC,GACT;AACjC,MAAI,qBAAqB;AAEzB,SAAO,IAAI,gBAAgB;AAAA,IACzB,MAAM,QAAuB;AAC3B,UAAI,UAAU,QAAS,OAAM,UAAU,QAAQ;AAAA,IACjD;AAAA,IAEA,MAAM,UAAU,SAAS,YAA2B;AAClD,iBAAW,QAAQ,OAAO;AAE1B,4BAAsB;AAEtB,UAAI,UAAU,QAAS,OAAM,UAAU,QAAQ,OAAO;AACtD,UAAI,UAAU,UAAU,OAAO,YAAY,UAAU;AACnD,cAAM,UAAU,OAAO,OAAO;AAAA,MAChC;AAAA,IACF;AAAA,IAEA,MAAM,QAAuB;AAC3B,UAAI,UAAU,SAAS;AACrB,cAAM,UAAU,QAAQ,kBAAkB;AAAA,MAC5C;AAAA,IACF;AAAA,EACF,CAAC;AACH;;;ADrDO,SAAS,aACd,QACA,WACA;AACA,QAAM,YAAY,kBAAkB;AAEpC,aAAO,sDAAqC,OAAO,OAAO,aAAa,EAAE,CAAC,EACvE;AAAA,IACC,IAAI,gBAAgB;AAAA,MAClB,MAAM,UAAU,SAAS,YAA2B;AAClD,mBAAW,QAAQ,UAAU,QAAQ,KAAK,CAAC;AAAA,MAC7C;AAAA,IACF,CAAC;AAAA,EACH,EACC,YAAY,2BAA2B,SAAS,CAAC,EACjD;AAAA,IACC,IAAI,gBAAwC;AAAA,MAC1C,WAAW,OAAO,OAAO,eAAe;AACtC,mBAAW,QAAQ,EAAE,MAAM,QAAQ,OAAO,MAAM,CAAC;AAAA,MACnD;AAAA,IACF,CAAC;AAAA,EACH;AACJ;AAEA,SAAS,oBAA8C;AACrD,MAAI,gBAAgB;AAEpB,SAAO,CAAC,SAAyB;AAC/B,QAAI,eAAe;AACjB,aAAO,KAAK,UAAU;AACtB,UAAI,KAAM,iBAAgB;AAAA,IAC5B;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
1
|
// src/llamaindex-adapter.ts
|
|
2
|
-
import { convertAsyncIteratorToReadableStream } from "
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
import { convertAsyncIteratorToReadableStream } from "ai/internal";
|
|
3
|
+
|
|
4
|
+
// src/stream-callbacks.ts
|
|
5
|
+
function createCallbacksTransformer(callbacks = {}) {
|
|
6
|
+
let aggregatedResponse = "";
|
|
7
|
+
return new TransformStream({
|
|
8
|
+
async start() {
|
|
9
|
+
if (callbacks.onStart) await callbacks.onStart();
|
|
10
|
+
},
|
|
11
|
+
async transform(message, controller) {
|
|
12
|
+
controller.enqueue(message);
|
|
13
|
+
aggregatedResponse += message;
|
|
14
|
+
if (callbacks.onToken) await callbacks.onToken(message);
|
|
15
|
+
if (callbacks.onText && typeof message === "string") {
|
|
16
|
+
await callbacks.onText(message);
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
async flush() {
|
|
20
|
+
if (callbacks.onFinal) {
|
|
21
|
+
await callbacks.onFinal(aggregatedResponse);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/llamaindex-adapter.ts
|
|
28
|
+
function toDataStream(stream, callbacks) {
|
|
10
29
|
const trimStart = trimStartOfStream();
|
|
11
30
|
return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]()).pipeThrough(
|
|
12
31
|
new TransformStream({
|
|
@@ -14,38 +33,14 @@ function toDataStreamInternal(stream, callbacks) {
|
|
|
14
33
|
controller.enqueue(trimStart(message.delta));
|
|
15
34
|
}
|
|
16
35
|
})
|
|
17
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
|
36
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
|
18
37
|
new TransformStream({
|
|
19
38
|
transform: async (chunk, controller) => {
|
|
20
|
-
controller.enqueue(
|
|
39
|
+
controller.enqueue({ type: "text", value: chunk });
|
|
21
40
|
}
|
|
22
41
|
})
|
|
23
42
|
);
|
|
24
43
|
}
|
|
25
|
-
function toDataStream(stream, callbacks) {
|
|
26
|
-
return toDataStreamInternal(stream, callbacks).pipeThrough(
|
|
27
|
-
new TextEncoderStream()
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
function toDataStreamResponse(stream, options = {}) {
|
|
31
|
-
var _a;
|
|
32
|
-
const { init, data, callbacks } = options;
|
|
33
|
-
const dataStream = toDataStreamInternal(stream, callbacks).pipeThrough(
|
|
34
|
-
new TextEncoderStream()
|
|
35
|
-
);
|
|
36
|
-
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
|
37
|
-
return new Response(responseStream, {
|
|
38
|
-
status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
|
|
39
|
-
statusText: init == null ? void 0 : init.statusText,
|
|
40
|
-
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
|
41
|
-
contentType: "text/plain; charset=utf-8",
|
|
42
|
-
dataStreamVersion: "v1"
|
|
43
|
-
})
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
function mergeIntoDataStream(stream, options) {
|
|
47
|
-
options.dataStream.merge(toDataStreamInternal(stream, options.callbacks));
|
|
48
|
-
}
|
|
49
44
|
function trimStartOfStream() {
|
|
50
45
|
let isStreamStart = true;
|
|
51
46
|
return (text) => {
|
|
@@ -57,8 +52,6 @@ function trimStartOfStream() {
|
|
|
57
52
|
};
|
|
58
53
|
}
|
|
59
54
|
export {
|
|
60
|
-
|
|
61
|
-
toDataStream,
|
|
62
|
-
toDataStreamResponse
|
|
55
|
+
toDataStream
|
|
63
56
|
};
|
|
64
57
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/llamaindex-adapter.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/llamaindex-adapter.ts","../src/stream-callbacks.ts"],"sourcesContent":["import { DataStreamPart } from 'ai';\nimport { convertAsyncIteratorToReadableStream } from 'ai/internal';\nimport {\n createCallbacksTransformer,\n StreamCallbacks,\n} from './stream-callbacks';\n\ntype EngineResponse = {\n delta: string;\n};\n\nexport function toDataStream(\n stream: AsyncIterable<EngineResponse>,\n callbacks?: StreamCallbacks,\n) {\n const trimStart = trimStartOfStream();\n\n return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]())\n .pipeThrough(\n new TransformStream({\n async transform(message, controller): Promise<void> {\n controller.enqueue(trimStart(message.delta));\n },\n }),\n )\n .pipeThrough(createCallbacksTransformer(callbacks))\n .pipeThrough(\n new TransformStream<string, DataStreamPart>({\n transform: async (chunk, controller) => {\n controller.enqueue({ type: 'text', value: chunk });\n },\n }),\n );\n}\n\nfunction trimStartOfStream(): (text: string) => string {\n let isStreamStart = true;\n\n return (text: string): string => {\n if (isStreamStart) {\n text = text.trimStart();\n if (text) isStreamStart = false;\n }\n return text;\n };\n}\n","/**\n * Configuration options and helper callback methods for stream lifecycle events.\n */\nexport interface StreamCallbacks {\n /** `onStart`: Called once when the stream is initialized. */\n onStart?: () => Promise<void> | void;\n\n /** `onFinal`: Called once when the stream is closed with the final completion message. */\n onFinal?: (completion: string) => Promise<void> | void;\n\n /** `onToken`: Called for each tokenized message. */\n onToken?: (token: string) => Promise<void> | void;\n\n /** `onText`: Called for each text chunk. */\n onText?: (text: string) => Promise<void> | void;\n}\n\n/**\n * Creates a transform stream that encodes input messages and invokes optional callback functions.\n * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.\n * - `onStart`: Called once when the stream is initialized.\n * - `onToken`: Called for each tokenized message.\n * - `onFinal`: Called once when the stream is closed with the final completion message.\n *\n * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.\n *\n * @param {StreamCallbacks} [callbacks] - An object containing the callback functions.\n * @return {TransformStream<string, Uint8Array>} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.\n *\n * @example\n * const callbacks = {\n * onStart: async () => console.log('Stream started'),\n * onToken: async (token) => console.log(`Token: ${token}`),\n * onFinal: async () => data.close()\n * };\n * const transformer = createCallbacksTransformer(callbacks);\n */\nexport function createCallbacksTransformer(\n callbacks: StreamCallbacks | undefined = {},\n): TransformStream<string, string> {\n let aggregatedResponse = '';\n\n return new TransformStream({\n async start(): Promise<void> {\n if (callbacks.onStart) await callbacks.onStart();\n },\n\n async transform(message, controller): Promise<void> {\n controller.enqueue(message);\n\n aggregatedResponse += message;\n\n if (callbacks.onToken) await callbacks.onToken(message);\n if (callbacks.onText && typeof message === 'string') {\n await callbacks.onText(message);\n }\n },\n\n async flush(): Promise<void> {\n if (callbacks.onFinal) {\n await callbacks.onFinal(aggregatedResponse);\n }\n },\n });\n}\n"],"mappings":";AACA,SAAS,4CAA4C;;;ACoC9C,SAAS,2BACd,YAAyC,CAAC,GACT;AACjC,MAAI,qBAAqB;AAEzB,SAAO,IAAI,gBAAgB;AAAA,IACzB,MAAM,QAAuB;AAC3B,UAAI,UAAU,QAAS,OAAM,UAAU,QAAQ;AAAA,IACjD;AAAA,IAEA,MAAM,UAAU,SAAS,YAA2B;AAClD,iBAAW,QAAQ,OAAO;AAE1B,4BAAsB;AAEtB,UAAI,UAAU,QAAS,OAAM,UAAU,QAAQ,OAAO;AACtD,UAAI,UAAU,UAAU,OAAO,YAAY,UAAU;AACnD,cAAM,UAAU,OAAO,OAAO;AAAA,MAChC;AAAA,IACF;AAAA,IAEA,MAAM,QAAuB;AAC3B,UAAI,UAAU,SAAS;AACrB,cAAM,UAAU,QAAQ,kBAAkB;AAAA,MAC5C;AAAA,IACF;AAAA,EACF,CAAC;AACH;;;ADrDO,SAAS,aACd,QACA,WACA;AACA,QAAM,YAAY,kBAAkB;AAEpC,SAAO,qCAAqC,OAAO,OAAO,aAAa,EAAE,CAAC,EACvE;AAAA,IACC,IAAI,gBAAgB;AAAA,MAClB,MAAM,UAAU,SAAS,YAA2B;AAClD,mBAAW,QAAQ,UAAU,QAAQ,KAAK,CAAC;AAAA,MAC7C;AAAA,IACF,CAAC;AAAA,EACH,EACC,YAAY,2BAA2B,SAAS,CAAC,EACjD;AAAA,IACC,IAAI,gBAAwC;AAAA,MAC1C,WAAW,OAAO,OAAO,eAAe;AACtC,mBAAW,QAAQ,EAAE,MAAM,QAAQ,OAAO,MAAM,CAAC;AAAA,MACnD;AAAA,IACF,CAAC;AAAA,EACH;AACJ;AAEA,SAAS,oBAA8C;AACrD,MAAI,gBAAgB;AAEpB,SAAO,CAAC,SAAyB;AAC/B,QAAI,eAAe;AACjB,aAAO,KAAK,UAAU;AACtB,UAAI,KAAM,iBAAgB;AAAA,IAC5B;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/llamaindex",
|
|
3
|
-
"version": "1.0.0-canary.
|
|
3
|
+
"version": "1.0.0-canary.8",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -19,8 +19,7 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"ai": "5.0.0-canary.
|
|
23
|
-
"@ai-sdk/provider-utils": "3.0.0-canary.15"
|
|
22
|
+
"ai": "5.0.0-canary.20"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@types/node": "20.17.24",
|