@ai-sdk/llamaindex 1.0.0-canary.7 → 1.0.0-canary.9
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 +30 -0
- package/dist/index.d.mts +51 -49
- package/dist/index.d.ts +51 -49
- package/dist/index.js +27 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @ai-sdk/llamaindex
|
|
2
2
|
|
|
3
|
+
## 1.0.0-canary.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [d964901]
|
|
8
|
+
- Updated dependencies [0560977]
|
|
9
|
+
- Updated dependencies [66af894]
|
|
10
|
+
- Updated dependencies [516be5b]
|
|
11
|
+
- Updated dependencies [bfbfc4c]
|
|
12
|
+
- Updated dependencies [ea7a7c9]
|
|
13
|
+
- Updated dependencies [1409e13]
|
|
14
|
+
- ai@5.0.0-canary.21
|
|
15
|
+
|
|
16
|
+
## 1.0.0-canary.8
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- bc3109f: chore (ai): push stream-callbacks into langchain/llamaindex adapters
|
|
21
|
+
- Updated dependencies [13fef90]
|
|
22
|
+
- Updated dependencies [e90d45d]
|
|
23
|
+
- Updated dependencies [bc3109f]
|
|
24
|
+
- Updated dependencies [496bbc1]
|
|
25
|
+
- Updated dependencies [da70d79]
|
|
26
|
+
- Updated dependencies [bcea599]
|
|
27
|
+
- Updated dependencies [48d675a]
|
|
28
|
+
- Updated dependencies [c7710a9]
|
|
29
|
+
- Updated dependencies [35fc02c]
|
|
30
|
+
- Updated dependencies [b983b51]
|
|
31
|
+
- ai@5.0.0-canary.20
|
|
32
|
+
|
|
3
33
|
## 1.0.0-canary.7
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,101 +1,103 @@
|
|
|
1
|
-
|
|
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
|
+
}
|
|
2
14
|
|
|
3
15
|
type EngineResponse = {
|
|
4
16
|
delta: string;
|
|
5
17
|
};
|
|
6
18
|
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<{
|
|
7
|
-
value: string;
|
|
8
19
|
type: "text";
|
|
9
|
-
} | {
|
|
10
|
-
value: any[];
|
|
11
|
-
type: "data";
|
|
12
|
-
} | {
|
|
13
20
|
value: string;
|
|
14
|
-
type: "error";
|
|
15
21
|
} | {
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
type: "error";
|
|
23
|
+
value: string;
|
|
18
24
|
} | {
|
|
25
|
+
type: "tool-call";
|
|
19
26
|
value: {
|
|
20
|
-
toolName: string;
|
|
21
27
|
toolCallId: string;
|
|
28
|
+
toolName: string;
|
|
22
29
|
args?: unknown;
|
|
23
30
|
};
|
|
24
|
-
type: "tool-call";
|
|
25
31
|
} | {
|
|
32
|
+
type: "tool-result";
|
|
26
33
|
value: {
|
|
27
34
|
toolCallId: string;
|
|
28
35
|
result?: unknown;
|
|
29
36
|
providerMetadata?: any;
|
|
30
37
|
};
|
|
31
|
-
type: "tool-result";
|
|
32
38
|
} | {
|
|
39
|
+
type: "tool-call-streaming-start";
|
|
33
40
|
value: {
|
|
34
|
-
toolName: string;
|
|
35
41
|
toolCallId: string;
|
|
42
|
+
toolName: string;
|
|
36
43
|
};
|
|
37
|
-
type: "tool-call-streaming-start";
|
|
38
44
|
} | {
|
|
45
|
+
type: "tool-call-delta";
|
|
39
46
|
value: {
|
|
40
47
|
toolCallId: string;
|
|
41
48
|
argsTextDelta: string;
|
|
42
49
|
};
|
|
43
|
-
type: "tool-call-delta";
|
|
44
50
|
} | {
|
|
51
|
+
type: "reasoning";
|
|
45
52
|
value: {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
text: string;
|
|
54
|
+
providerMetadata?: Record<string, any> | undefined;
|
|
55
|
+
};
|
|
56
|
+
} | {
|
|
57
|
+
type: "source";
|
|
58
|
+
value: {
|
|
59
|
+
type: "source";
|
|
60
|
+
sourceType: "url";
|
|
61
|
+
url: string;
|
|
62
|
+
id: string;
|
|
63
|
+
providerMetadata?: any;
|
|
64
|
+
title?: string | undefined;
|
|
54
65
|
};
|
|
55
|
-
type: "finish-message";
|
|
56
66
|
} | {
|
|
67
|
+
type: "file";
|
|
57
68
|
value: {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
usage?: {
|
|
61
|
-
inputTokens?: number | undefined;
|
|
62
|
-
outputTokens?: number | undefined;
|
|
63
|
-
totalTokens?: number | undefined;
|
|
64
|
-
reasoningTokens?: number | undefined;
|
|
65
|
-
cachedInputTokens?: number | undefined;
|
|
66
|
-
} | undefined;
|
|
69
|
+
url: string;
|
|
70
|
+
mediaType: string;
|
|
67
71
|
};
|
|
68
|
-
type: "finish-step";
|
|
69
72
|
} | {
|
|
73
|
+
type: "metadata";
|
|
70
74
|
value: {
|
|
71
|
-
|
|
75
|
+
metadata?: unknown;
|
|
72
76
|
};
|
|
77
|
+
} | {
|
|
73
78
|
type: "start-step";
|
|
79
|
+
value: {
|
|
80
|
+
metadata?: unknown;
|
|
81
|
+
};
|
|
74
82
|
} | {
|
|
83
|
+
type: "finish-step";
|
|
75
84
|
value: {
|
|
76
|
-
|
|
77
|
-
providerMetadata?: Record<string, any> | undefined;
|
|
85
|
+
metadata?: unknown;
|
|
78
86
|
};
|
|
79
|
-
type: "reasoning";
|
|
80
87
|
} | {
|
|
88
|
+
type: "start";
|
|
81
89
|
value: {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
sourceType: "url";
|
|
85
|
-
url: string;
|
|
86
|
-
providerMetadata?: any;
|
|
87
|
-
title?: string | undefined;
|
|
90
|
+
metadata?: unknown;
|
|
91
|
+
messageId?: string | undefined;
|
|
88
92
|
};
|
|
89
|
-
type: "source";
|
|
90
93
|
} | {
|
|
94
|
+
type: "finish";
|
|
91
95
|
value: {
|
|
92
|
-
|
|
93
|
-
url: string;
|
|
96
|
+
metadata?: unknown;
|
|
94
97
|
};
|
|
95
|
-
type: "file";
|
|
96
98
|
} | {
|
|
97
|
-
value: null;
|
|
98
99
|
type: "reasoning-part-finish";
|
|
100
|
+
value: null;
|
|
99
101
|
}>;
|
|
100
102
|
|
|
101
103
|
export { toDataStream };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,101 +1,103 @@
|
|
|
1
|
-
|
|
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
|
+
}
|
|
2
14
|
|
|
3
15
|
type EngineResponse = {
|
|
4
16
|
delta: string;
|
|
5
17
|
};
|
|
6
18
|
declare function toDataStream(stream: AsyncIterable<EngineResponse>, callbacks?: StreamCallbacks): ReadableStream<{
|
|
7
|
-
value: string;
|
|
8
19
|
type: "text";
|
|
9
|
-
} | {
|
|
10
|
-
value: any[];
|
|
11
|
-
type: "data";
|
|
12
|
-
} | {
|
|
13
20
|
value: string;
|
|
14
|
-
type: "error";
|
|
15
21
|
} | {
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
type: "error";
|
|
23
|
+
value: string;
|
|
18
24
|
} | {
|
|
25
|
+
type: "tool-call";
|
|
19
26
|
value: {
|
|
20
|
-
toolName: string;
|
|
21
27
|
toolCallId: string;
|
|
28
|
+
toolName: string;
|
|
22
29
|
args?: unknown;
|
|
23
30
|
};
|
|
24
|
-
type: "tool-call";
|
|
25
31
|
} | {
|
|
32
|
+
type: "tool-result";
|
|
26
33
|
value: {
|
|
27
34
|
toolCallId: string;
|
|
28
35
|
result?: unknown;
|
|
29
36
|
providerMetadata?: any;
|
|
30
37
|
};
|
|
31
|
-
type: "tool-result";
|
|
32
38
|
} | {
|
|
39
|
+
type: "tool-call-streaming-start";
|
|
33
40
|
value: {
|
|
34
|
-
toolName: string;
|
|
35
41
|
toolCallId: string;
|
|
42
|
+
toolName: string;
|
|
36
43
|
};
|
|
37
|
-
type: "tool-call-streaming-start";
|
|
38
44
|
} | {
|
|
45
|
+
type: "tool-call-delta";
|
|
39
46
|
value: {
|
|
40
47
|
toolCallId: string;
|
|
41
48
|
argsTextDelta: string;
|
|
42
49
|
};
|
|
43
|
-
type: "tool-call-delta";
|
|
44
50
|
} | {
|
|
51
|
+
type: "reasoning";
|
|
45
52
|
value: {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
text: string;
|
|
54
|
+
providerMetadata?: Record<string, any> | undefined;
|
|
55
|
+
};
|
|
56
|
+
} | {
|
|
57
|
+
type: "source";
|
|
58
|
+
value: {
|
|
59
|
+
type: "source";
|
|
60
|
+
sourceType: "url";
|
|
61
|
+
url: string;
|
|
62
|
+
id: string;
|
|
63
|
+
providerMetadata?: any;
|
|
64
|
+
title?: string | undefined;
|
|
54
65
|
};
|
|
55
|
-
type: "finish-message";
|
|
56
66
|
} | {
|
|
67
|
+
type: "file";
|
|
57
68
|
value: {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
usage?: {
|
|
61
|
-
inputTokens?: number | undefined;
|
|
62
|
-
outputTokens?: number | undefined;
|
|
63
|
-
totalTokens?: number | undefined;
|
|
64
|
-
reasoningTokens?: number | undefined;
|
|
65
|
-
cachedInputTokens?: number | undefined;
|
|
66
|
-
} | undefined;
|
|
69
|
+
url: string;
|
|
70
|
+
mediaType: string;
|
|
67
71
|
};
|
|
68
|
-
type: "finish-step";
|
|
69
72
|
} | {
|
|
73
|
+
type: "metadata";
|
|
70
74
|
value: {
|
|
71
|
-
|
|
75
|
+
metadata?: unknown;
|
|
72
76
|
};
|
|
77
|
+
} | {
|
|
73
78
|
type: "start-step";
|
|
79
|
+
value: {
|
|
80
|
+
metadata?: unknown;
|
|
81
|
+
};
|
|
74
82
|
} | {
|
|
83
|
+
type: "finish-step";
|
|
75
84
|
value: {
|
|
76
|
-
|
|
77
|
-
providerMetadata?: Record<string, any> | undefined;
|
|
85
|
+
metadata?: unknown;
|
|
78
86
|
};
|
|
79
|
-
type: "reasoning";
|
|
80
87
|
} | {
|
|
88
|
+
type: "start";
|
|
81
89
|
value: {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
sourceType: "url";
|
|
85
|
-
url: string;
|
|
86
|
-
providerMetadata?: any;
|
|
87
|
-
title?: string | undefined;
|
|
90
|
+
metadata?: unknown;
|
|
91
|
+
messageId?: string | undefined;
|
|
88
92
|
};
|
|
89
|
-
type: "source";
|
|
90
93
|
} | {
|
|
94
|
+
type: "finish";
|
|
91
95
|
value: {
|
|
92
|
-
|
|
93
|
-
url: string;
|
|
96
|
+
metadata?: unknown;
|
|
94
97
|
};
|
|
95
|
-
type: "file";
|
|
96
98
|
} | {
|
|
97
|
-
value: null;
|
|
98
99
|
type: "reasoning-part-finish";
|
|
100
|
+
value: null;
|
|
99
101
|
}>;
|
|
100
102
|
|
|
101
103
|
export { toDataStream };
|
package/dist/index.js
CHANGED
|
@@ -25,17 +25,41 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/llamaindex-adapter.ts
|
|
28
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
29
28
|
var import_internal = require("ai/internal");
|
|
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
|
|
30
54
|
function toDataStream(stream, callbacks) {
|
|
31
55
|
const trimStart = trimStartOfStream();
|
|
32
|
-
return (0,
|
|
56
|
+
return (0, import_internal.convertAsyncIteratorToReadableStream)(stream[Symbol.asyncIterator]()).pipeThrough(
|
|
33
57
|
new TransformStream({
|
|
34
58
|
async transform(message, controller) {
|
|
35
59
|
controller.enqueue(trimStart(message.delta));
|
|
36
60
|
}
|
|
37
61
|
})
|
|
38
|
-
).pipeThrough(
|
|
62
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
|
39
63
|
new TransformStream({
|
|
40
64
|
transform: async (chunk, controller) => {
|
|
41
65
|
controller.enqueue({ type: "text", value: chunk });
|
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,6 +1,30 @@
|
|
|
1
1
|
// src/llamaindex-adapter.ts
|
|
2
|
-
import { convertAsyncIteratorToReadableStream } from "
|
|
3
|
-
|
|
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
|
|
4
28
|
function toDataStream(stream, callbacks) {
|
|
5
29
|
const trimStart = trimStartOfStream();
|
|
6
30
|
return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]()).pipeThrough(
|
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.9",
|
|
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.21"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@types/node": "20.17.24",
|