@aigne/core 1.18.5 → 1.19.0
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 +15 -0
- package/lib/cjs/agents/agent.d.ts +20 -2
- package/lib/cjs/agents/agent.js +24 -13
- package/lib/cjs/agents/ai-agent.js +9 -8
- package/lib/cjs/agents/team-agent.js +1 -1
- package/lib/cjs/aigne/context.d.ts +3 -0
- package/lib/cjs/aigne/context.js +18 -13
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/prompt/prompt-builder.js +1 -2
- package/lib/cjs/utils/event-stream.d.ts +5 -1
- package/lib/cjs/utils/event-stream.js +88 -23
- package/lib/cjs/utils/stream-polyfill.d.ts +1 -0
- package/lib/cjs/utils/stream-polyfill.js +38 -0
- package/lib/cjs/utils/stream-utils.d.ts +9 -5
- package/lib/cjs/utils/stream-utils.js +49 -15
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +5 -0
- package/lib/dts/agents/agent.d.ts +20 -2
- package/lib/dts/aigne/context.d.ts +3 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/utils/event-stream.d.ts +5 -1
- package/lib/dts/utils/stream-polyfill.d.ts +1 -0
- package/lib/dts/utils/stream-utils.d.ts +9 -5
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +20 -2
- package/lib/esm/agents/agent.js +22 -13
- package/lib/esm/agents/ai-agent.js +10 -9
- package/lib/esm/agents/team-agent.js +2 -2
- package/lib/esm/aigne/context.d.ts +3 -0
- package/lib/esm/aigne/context.js +20 -15
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/prompt/prompt-builder.js +1 -2
- package/lib/esm/utils/event-stream.d.ts +5 -1
- package/lib/esm/utils/event-stream.js +86 -22
- package/lib/esm/utils/stream-polyfill.d.ts +1 -0
- package/lib/esm/utils/stream-polyfill.js +37 -0
- package/lib/esm/utils/stream-utils.d.ts +9 -5
- package/lib/esm/utils/stream-utils.js +49 -16
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +4 -0
- package/package.json +1 -1
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
2
|
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
3
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
5
6
|
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
6
7
|
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
7
8
|
export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
|
|
8
|
-
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, options?: {
|
|
10
|
+
onChunk?: (chunk: AgentResponseChunk<T>) => PromiseOrValue<AgentResponseChunk<T> | undefined | void>;
|
|
11
|
+
onResult?: (result: T) => PromiseOrValue<Partial<T> | undefined | void>;
|
|
12
|
+
onError?: (error: Error) => PromiseOrValue<Error>;
|
|
13
|
+
}): AgentResponseStream<T>;
|
|
12
14
|
export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
|
|
13
15
|
export declare function arrayToAgentProcessAsyncGenerator<T extends Message>(chunks: (AgentResponseChunk<T> | Error)[], result?: Partial<T>): AgentProcessAsyncGenerator<T>;
|
|
14
16
|
export declare function arrayToReadableStream<T>(chunks: (T | Error)[]): ReadableStream<T>;
|
|
@@ -16,8 +18,10 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
|
|
|
16
18
|
catchError: true;
|
|
17
19
|
}): Promise<(T | Error)[]>;
|
|
18
20
|
export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
|
|
19
|
-
catchError?:
|
|
21
|
+
catchError?: boolean;
|
|
20
22
|
}): Promise<T[]>;
|
|
21
23
|
export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
|
|
22
24
|
export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
23
25
|
export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
|
|
26
|
+
export declare function mergeReadableStreams<T1, T2>(s1: ReadableStream<T1>, s2: ReadableStream<T2>): ReadableStream<T1 | T2>;
|
|
27
|
+
export declare function mergeReadableStreams(...streams: ReadableStream<any>[]): ReadableStream<any>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import equal from "fast-deep-equal";
|
|
2
|
-
import { isEmptyChunk, } from "../agents/agent.js";
|
|
2
|
+
import { isAgentResponseDelta, isEmptyChunk, } from "../agents/agent.js";
|
|
3
3
|
import { omitBy } from "./type-utils.js";
|
|
4
|
+
import "./stream-polyfill.js";
|
|
4
5
|
export function objectToAgentResponseStream(json) {
|
|
5
6
|
return new ReadableStream({
|
|
6
7
|
pull(controller) {
|
|
@@ -10,16 +11,18 @@ export function objectToAgentResponseStream(json) {
|
|
|
10
11
|
});
|
|
11
12
|
}
|
|
12
13
|
export function mergeAgentResponseChunk(output, chunk) {
|
|
13
|
-
if (chunk
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
if (isAgentResponseDelta(chunk)) {
|
|
15
|
+
if (chunk.delta.text) {
|
|
16
|
+
for (const [key, text] of Object.entries(chunk.delta.text)) {
|
|
17
|
+
const original = output[key];
|
|
18
|
+
const t = (original || "") + (text || "");
|
|
19
|
+
if (t)
|
|
20
|
+
Object.assign(output, { [key]: t });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (chunk.delta.json) {
|
|
24
|
+
Object.assign(output, omitBy(chunk.delta.json, (v) => v === undefined));
|
|
19
25
|
}
|
|
20
|
-
}
|
|
21
|
-
if (chunk.delta.json) {
|
|
22
|
-
Object.assign(output, omitBy(chunk.delta.json, (v) => v === undefined));
|
|
23
26
|
}
|
|
24
27
|
return output;
|
|
25
28
|
}
|
|
@@ -70,7 +73,7 @@ export function asyncGeneratorToReadableStream(generator) {
|
|
|
70
73
|
},
|
|
71
74
|
});
|
|
72
75
|
}
|
|
73
|
-
export function onAgentResponseStreamEnd(stream,
|
|
76
|
+
export function onAgentResponseStreamEnd(stream, options) {
|
|
74
77
|
const json = {};
|
|
75
78
|
const reader = stream.getReader();
|
|
76
79
|
return new ReadableStream({
|
|
@@ -79,18 +82,17 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
|
|
|
79
82
|
while (true) {
|
|
80
83
|
const { value, done } = await reader.read();
|
|
81
84
|
if (done) {
|
|
82
|
-
const result = await
|
|
85
|
+
const result = (await options?.onResult?.(json)) ?? json;
|
|
83
86
|
if (result && !equal(result, json)) {
|
|
84
87
|
let chunk = { delta: { json: result } };
|
|
85
|
-
|
|
86
|
-
chunk = options.processChunk(chunk);
|
|
88
|
+
chunk = (await options?.onChunk?.(chunk)) ?? chunk;
|
|
87
89
|
controller.enqueue(chunk);
|
|
88
90
|
}
|
|
89
91
|
controller.close();
|
|
90
92
|
return;
|
|
91
93
|
}
|
|
92
94
|
mergeAgentResponseChunk(json, value);
|
|
93
|
-
const chunk = options?.
|
|
95
|
+
const chunk = (await options?.onChunk?.(value)) ?? value;
|
|
94
96
|
if (!isEmptyChunk(chunk)) {
|
|
95
97
|
controller.enqueue(chunk);
|
|
96
98
|
break;
|
|
@@ -98,7 +100,7 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
|
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
catch (error) {
|
|
101
|
-
controller.error((await options?.
|
|
103
|
+
controller.error((await options?.onError?.(error)) ?? error);
|
|
102
104
|
}
|
|
103
105
|
},
|
|
104
106
|
});
|
|
@@ -179,3 +181,34 @@ export function toReadableStream(stream) {
|
|
|
179
181
|
export async function readAllString(stream) {
|
|
180
182
|
return (await readableStreamToArray((stream instanceof ReadableStream ? stream : toReadableStream(stream)).pipeThrough(new TextDecoderStream()))).join("");
|
|
181
183
|
}
|
|
184
|
+
export function mergeReadableStreams(...streams) {
|
|
185
|
+
let readers;
|
|
186
|
+
return new ReadableStream({
|
|
187
|
+
async pull(controller) {
|
|
188
|
+
try {
|
|
189
|
+
readers ??= streams.map((s) => ({ reader: s.getReader(), data: [] }));
|
|
190
|
+
while (readers.length) {
|
|
191
|
+
const chunk = await Promise.race(readers.map((i) => {
|
|
192
|
+
i.reading ??= i.reader.read().then((result) => ({ result, item: i }));
|
|
193
|
+
return i.reading;
|
|
194
|
+
}));
|
|
195
|
+
if (chunk.result.value) {
|
|
196
|
+
controller.enqueue(chunk.result.value);
|
|
197
|
+
chunk.item.reading = undefined;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (chunk.result.done) {
|
|
201
|
+
readers = readers.filter((i) => i !== chunk.item);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
controller.close();
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
controller.error(error);
|
|
208
|
+
if (readers)
|
|
209
|
+
for (const item of readers)
|
|
210
|
+
item.reader.releaseLock();
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
}
|
|
@@ -15,6 +15,7 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
|
|
|
15
15
|
export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
16
16
|
export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
|
|
17
17
|
export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
18
|
+
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
|
|
18
19
|
export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
|
|
19
20
|
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
20
21
|
export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
|
|
@@ -58,6 +58,10 @@ export function unique(arr, key = (item) => item) {
|
|
|
58
58
|
return true;
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
|
+
export function omit(obj, ...keys) {
|
|
62
|
+
const flattenedKeys = new Set(keys.flat());
|
|
63
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !flattenedKeys.has(key)));
|
|
64
|
+
}
|
|
61
65
|
export function omitBy(obj, predicate) {
|
|
62
66
|
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
|
|
63
67
|
const k = key;
|