@ai-sdk/llamaindex 0.0.0-1c33ba03-20260114162300 → 0.0.0-4caafb2a-20260122145312
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 +83 -2
- package/package.json +3 -2
- package/src/index.ts +1 -0
- package/src/llamaindex-adapter.test.ts +39 -0
- package/src/llamaindex-adapter.ts +52 -0
- package/src/stream-callbacks.ts +65 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,91 @@
|
|
|
1
1
|
# @ai-sdk/llamaindex
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-4caafb2a-20260122145312
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- Updated dependencies [4caafb2]
|
|
8
|
+
- ai@0.0.0-4caafb2a-20260122145312
|
|
9
|
+
|
|
10
|
+
## 2.0.46
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 8dc54db: chore: add src folders to package bundle
|
|
15
|
+
- ai@6.0.46
|
|
16
|
+
|
|
17
|
+
## 2.0.45
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- ai@6.0.45
|
|
22
|
+
|
|
23
|
+
## 2.0.44
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- ai@6.0.44
|
|
28
|
+
|
|
29
|
+
## 2.0.43
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- Updated dependencies [2dc9bfa]
|
|
34
|
+
- ai@6.0.43
|
|
35
|
+
|
|
36
|
+
## 2.0.42
|
|
37
|
+
|
|
38
|
+
### Patch Changes
|
|
39
|
+
|
|
40
|
+
- ai@6.0.42
|
|
41
|
+
|
|
42
|
+
## 2.0.41
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- Updated dependencies [84b6e6d]
|
|
47
|
+
- ai@6.0.41
|
|
48
|
+
|
|
49
|
+
## 2.0.40
|
|
50
|
+
|
|
51
|
+
### Patch Changes
|
|
52
|
+
|
|
53
|
+
- Updated dependencies [ab57783]
|
|
54
|
+
- ai@6.0.40
|
|
55
|
+
|
|
56
|
+
## 2.0.39
|
|
57
|
+
|
|
58
|
+
### Patch Changes
|
|
59
|
+
|
|
60
|
+
- Updated dependencies [4e28ba0]
|
|
61
|
+
- ai@6.0.39
|
|
62
|
+
|
|
63
|
+
## 2.0.38
|
|
64
|
+
|
|
65
|
+
### Patch Changes
|
|
66
|
+
|
|
67
|
+
- ai@6.0.38
|
|
68
|
+
|
|
69
|
+
## 2.0.37
|
|
70
|
+
|
|
71
|
+
### Patch Changes
|
|
72
|
+
|
|
73
|
+
- Updated dependencies [b5dab9b]
|
|
74
|
+
- ai@6.0.37
|
|
75
|
+
|
|
76
|
+
## 2.0.36
|
|
77
|
+
|
|
78
|
+
### Patch Changes
|
|
79
|
+
|
|
80
|
+
- Updated dependencies [46f46e4]
|
|
81
|
+
- ai@6.0.36
|
|
82
|
+
|
|
83
|
+
## 2.0.35
|
|
84
|
+
|
|
85
|
+
### Patch Changes
|
|
86
|
+
|
|
87
|
+
- Updated dependencies [d7e7f1f]
|
|
88
|
+
- ai@6.0.35
|
|
8
89
|
|
|
9
90
|
## 2.0.34
|
|
10
91
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/llamaindex",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4caafb2a-20260122145312",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
11
|
+
"src",
|
|
11
12
|
"CHANGELOG.md",
|
|
12
13
|
"README.md"
|
|
13
14
|
],
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"ai": "0.0.0-
|
|
24
|
+
"ai": "0.0.0-4caafb2a-20260122145312"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"@types/node": "20.17.24",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './llamaindex-adapter';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
convertArrayToAsyncIterable,
|
|
3
|
+
convertReadableStreamToArray,
|
|
4
|
+
} from 'ai/test';
|
|
5
|
+
import { toUIMessageStream } from './llamaindex-adapter';
|
|
6
|
+
import { describe, it, expect } from 'vitest';
|
|
7
|
+
|
|
8
|
+
describe('toUIMessageStream', () => {
|
|
9
|
+
it('should convert AsyncIterable<EngineResponse>', async () => {
|
|
10
|
+
const inputStream = convertArrayToAsyncIterable([
|
|
11
|
+
{ delta: 'Hello' },
|
|
12
|
+
{ delta: 'World' },
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
expect(await convertReadableStreamToArray(toUIMessageStream(inputStream)))
|
|
16
|
+
.toMatchInlineSnapshot(`
|
|
17
|
+
[
|
|
18
|
+
{
|
|
19
|
+
"id": "1",
|
|
20
|
+
"type": "text-start",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"delta": "Hello",
|
|
24
|
+
"id": "1",
|
|
25
|
+
"type": "text-delta",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"delta": "World",
|
|
29
|
+
"id": "1",
|
|
30
|
+
"type": "text-delta",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"id": "1",
|
|
34
|
+
"type": "text-end",
|
|
35
|
+
},
|
|
36
|
+
]
|
|
37
|
+
`);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { UIMessageChunk } from 'ai';
|
|
2
|
+
import { convertAsyncIteratorToReadableStream } from 'ai/internal';
|
|
3
|
+
import {
|
|
4
|
+
createCallbacksTransformer,
|
|
5
|
+
StreamCallbacks,
|
|
6
|
+
} from './stream-callbacks';
|
|
7
|
+
|
|
8
|
+
type EngineResponse = {
|
|
9
|
+
delta: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function toUIMessageStream(
|
|
13
|
+
stream: AsyncIterable<EngineResponse>,
|
|
14
|
+
callbacks?: StreamCallbacks,
|
|
15
|
+
) {
|
|
16
|
+
const trimStart = trimStartOfStream();
|
|
17
|
+
|
|
18
|
+
return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]())
|
|
19
|
+
.pipeThrough(
|
|
20
|
+
new TransformStream({
|
|
21
|
+
async transform(message, controller): Promise<void> {
|
|
22
|
+
controller.enqueue(trimStart(message.delta));
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
.pipeThrough(createCallbacksTransformer(callbacks))
|
|
27
|
+
.pipeThrough(
|
|
28
|
+
new TransformStream<string, UIMessageChunk>({
|
|
29
|
+
start: async controller => {
|
|
30
|
+
controller.enqueue({ type: 'text-start', id: '1' });
|
|
31
|
+
},
|
|
32
|
+
transform: async (chunk, controller) => {
|
|
33
|
+
controller.enqueue({ type: 'text-delta', delta: chunk, id: '1' });
|
|
34
|
+
},
|
|
35
|
+
flush: async controller => {
|
|
36
|
+
controller.enqueue({ type: 'text-end', id: '1' });
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function trimStartOfStream(): (text: string) => string {
|
|
43
|
+
let isStreamStart = true;
|
|
44
|
+
|
|
45
|
+
return (text: string): string => {
|
|
46
|
+
if (isStreamStart) {
|
|
47
|
+
text = text.trimStart();
|
|
48
|
+
if (text) isStreamStart = false;
|
|
49
|
+
}
|
|
50
|
+
return text;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options and helper callback methods for stream lifecycle events.
|
|
3
|
+
*/
|
|
4
|
+
export interface StreamCallbacks {
|
|
5
|
+
/** `onStart`: Called once when the stream is initialized. */
|
|
6
|
+
onStart?: () => Promise<void> | void;
|
|
7
|
+
|
|
8
|
+
/** `onFinal`: Called once when the stream is closed with the final completion message. */
|
|
9
|
+
onFinal?: (completion: string) => Promise<void> | void;
|
|
10
|
+
|
|
11
|
+
/** `onToken`: Called for each tokenized message. */
|
|
12
|
+
onToken?: (token: string) => Promise<void> | void;
|
|
13
|
+
|
|
14
|
+
/** `onText`: Called for each text chunk. */
|
|
15
|
+
onText?: (text: string) => Promise<void> | void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a transform stream that encodes input messages and invokes optional callback functions.
|
|
20
|
+
* The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
|
|
21
|
+
* - `onStart`: Called once when the stream is initialized.
|
|
22
|
+
* - `onToken`: Called for each tokenized message.
|
|
23
|
+
* - `onFinal`: Called once when the stream is closed with the final completion message.
|
|
24
|
+
*
|
|
25
|
+
* This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.
|
|
26
|
+
*
|
|
27
|
+
* @param {StreamCallbacks} [callbacks] - An object containing the callback functions.
|
|
28
|
+
* @return {TransformStream<string, Uint8Array>} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const callbacks = {
|
|
32
|
+
* onStart: async () => console.log('Stream started'),
|
|
33
|
+
* onToken: async (token) => console.log(`Token: ${token}`),
|
|
34
|
+
* onFinal: async () => data.close()
|
|
35
|
+
* };
|
|
36
|
+
* const transformer = createCallbacksTransformer(callbacks);
|
|
37
|
+
*/
|
|
38
|
+
export function createCallbacksTransformer(
|
|
39
|
+
callbacks: StreamCallbacks | undefined = {},
|
|
40
|
+
): TransformStream<string, string> {
|
|
41
|
+
let aggregatedResponse = '';
|
|
42
|
+
|
|
43
|
+
return new TransformStream({
|
|
44
|
+
async start(): Promise<void> {
|
|
45
|
+
if (callbacks.onStart) await callbacks.onStart();
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async transform(message, controller): Promise<void> {
|
|
49
|
+
controller.enqueue(message);
|
|
50
|
+
|
|
51
|
+
aggregatedResponse += message;
|
|
52
|
+
|
|
53
|
+
if (callbacks.onToken) await callbacks.onToken(message);
|
|
54
|
+
if (callbacks.onText && typeof message === 'string') {
|
|
55
|
+
await callbacks.onText(message);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
async flush(): Promise<void> {
|
|
60
|
+
if (callbacks.onFinal) {
|
|
61
|
+
await callbacks.onFinal(aggregatedResponse);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|