@aigne/core 1.18.6 → 1.20.1

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/lib/cjs/agents/agent.d.ts +31 -8
  3. package/lib/cjs/agents/agent.js +31 -22
  4. package/lib/cjs/agents/ai-agent.d.ts +18 -6
  5. package/lib/cjs/agents/ai-agent.js +23 -12
  6. package/lib/cjs/agents/guide-rail-agent.d.ts +1 -1
  7. package/lib/cjs/agents/mcp-agent.d.ts +1 -1
  8. package/lib/cjs/agents/team-agent.js +1 -1
  9. package/lib/cjs/aigne/aigne.d.ts +10 -10
  10. package/lib/cjs/aigne/context.d.ts +11 -6
  11. package/lib/cjs/aigne/context.js +73 -48
  12. package/lib/cjs/aigne/message-queue.d.ts +1 -1
  13. package/lib/cjs/aigne/message-queue.js +2 -3
  14. package/lib/cjs/aigne/usage.d.ts +1 -0
  15. package/lib/cjs/aigne/usage.js +1 -0
  16. package/lib/cjs/loader/agent-yaml.d.ts +2 -1
  17. package/lib/cjs/loader/agent-yaml.js +4 -0
  18. package/lib/cjs/prompt/prompt-builder.d.ts +2 -16
  19. package/lib/cjs/prompt/prompt-builder.js +12 -25
  20. package/lib/cjs/utils/event-stream.d.ts +5 -1
  21. package/lib/cjs/utils/event-stream.js +88 -23
  22. package/lib/cjs/utils/stream-utils.d.ts +9 -7
  23. package/lib/cjs/utils/stream-utils.js +48 -15
  24. package/lib/cjs/utils/type-utils.d.ts +2 -0
  25. package/lib/cjs/utils/type-utils.js +18 -0
  26. package/lib/dts/agents/agent.d.ts +31 -8
  27. package/lib/dts/agents/ai-agent.d.ts +18 -6
  28. package/lib/dts/agents/guide-rail-agent.d.ts +1 -1
  29. package/lib/dts/agents/mcp-agent.d.ts +1 -1
  30. package/lib/dts/aigne/aigne.d.ts +10 -10
  31. package/lib/dts/aigne/context.d.ts +11 -6
  32. package/lib/dts/aigne/message-queue.d.ts +1 -1
  33. package/lib/dts/aigne/usage.d.ts +1 -0
  34. package/lib/dts/loader/agent-yaml.d.ts +2 -1
  35. package/lib/dts/prompt/prompt-builder.d.ts +2 -16
  36. package/lib/dts/utils/event-stream.d.ts +5 -1
  37. package/lib/dts/utils/stream-utils.d.ts +9 -7
  38. package/lib/dts/utils/type-utils.d.ts +2 -0
  39. package/lib/esm/agents/agent.d.ts +31 -8
  40. package/lib/esm/agents/agent.js +29 -22
  41. package/lib/esm/agents/ai-agent.d.ts +18 -6
  42. package/lib/esm/agents/ai-agent.js +25 -14
  43. package/lib/esm/agents/guide-rail-agent.d.ts +1 -1
  44. package/lib/esm/agents/mcp-agent.d.ts +1 -1
  45. package/lib/esm/agents/team-agent.js +2 -2
  46. package/lib/esm/aigne/aigne.d.ts +10 -10
  47. package/lib/esm/aigne/context.d.ts +11 -6
  48. package/lib/esm/aigne/context.js +76 -51
  49. package/lib/esm/aigne/message-queue.d.ts +1 -1
  50. package/lib/esm/aigne/message-queue.js +2 -3
  51. package/lib/esm/aigne/usage.d.ts +1 -0
  52. package/lib/esm/aigne/usage.js +1 -0
  53. package/lib/esm/loader/agent-yaml.d.ts +2 -1
  54. package/lib/esm/loader/agent-yaml.js +4 -0
  55. package/lib/esm/prompt/prompt-builder.d.ts +2 -16
  56. package/lib/esm/prompt/prompt-builder.js +12 -23
  57. package/lib/esm/utils/event-stream.d.ts +5 -1
  58. package/lib/esm/utils/event-stream.js +86 -22
  59. package/lib/esm/utils/stream-utils.d.ts +9 -7
  60. package/lib/esm/utils/stream-utils.js +48 -16
  61. package/lib/esm/utils/type-utils.d.ts +2 -0
  62. package/lib/esm/utils/type-utils.js +16 -0
  63. package/package.json +2 -2
@@ -1,5 +1,5 @@
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
4
  import "./stream-polyfill.js";
5
5
  export function objectToAgentResponseStream(json) {
@@ -11,16 +11,18 @@ export function objectToAgentResponseStream(json) {
11
11
  });
12
12
  }
13
13
  export function mergeAgentResponseChunk(output, chunk) {
14
- if (chunk.delta.text) {
15
- for (const [key, text] of Object.entries(chunk.delta.text)) {
16
- const original = output[key];
17
- const t = (original || "") + (text || "");
18
- if (t)
19
- Object.assign(output, { [key]: t });
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));
20
25
  }
21
- }
22
- if (chunk.delta.json) {
23
- Object.assign(output, omitBy(chunk.delta.json, (v) => v === undefined));
24
26
  }
25
27
  return output;
26
28
  }
@@ -71,7 +73,7 @@ export function asyncGeneratorToReadableStream(generator) {
71
73
  },
72
74
  });
73
75
  }
74
- export function onAgentResponseStreamEnd(stream, callback, options) {
76
+ export function onAgentResponseStreamEnd(stream, options) {
75
77
  const json = {};
76
78
  const reader = stream.getReader();
77
79
  return new ReadableStream({
@@ -80,18 +82,17 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
80
82
  while (true) {
81
83
  const { value, done } = await reader.read();
82
84
  if (done) {
83
- const result = await callback(json);
85
+ const result = (await options?.onResult?.(json)) ?? json;
84
86
  if (result && !equal(result, json)) {
85
87
  let chunk = { delta: { json: result } };
86
- if (options?.processChunk)
87
- chunk = options.processChunk(chunk);
88
+ chunk = (await options?.onChunk?.(chunk)) ?? chunk;
88
89
  controller.enqueue(chunk);
89
90
  }
90
91
  controller.close();
91
92
  return;
92
93
  }
93
94
  mergeAgentResponseChunk(json, value);
94
- const chunk = options?.processChunk ? options.processChunk(value) : value;
95
+ const chunk = (await options?.onChunk?.(value)) ?? value;
95
96
  if (!isEmptyChunk(chunk)) {
96
97
  controller.enqueue(chunk);
97
98
  break;
@@ -99,7 +100,7 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
99
100
  }
100
101
  }
101
102
  catch (error) {
102
- controller.error((await options?.errorCallback?.(error)) ?? error);
103
+ controller.error((await options?.onError?.(error)) ?? error);
103
104
  }
104
105
  },
105
106
  });
@@ -180,3 +181,34 @@ export function toReadableStream(stream) {
180
181
  export async function readAllString(stream) {
181
182
  return (await readableStreamToArray((stream instanceof ReadableStream ? stream : toReadableStream(stream)).pipeThrough(new TextDecoderStream()))).join("");
182
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,8 @@ 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>;
19
+ export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
18
20
  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
21
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
20
22
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
@@ -58,6 +58,22 @@ 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
+ }
65
+ export function omitDeep(obj, ...keys) {
66
+ if (Array.isArray(obj)) {
67
+ return obj.map((item) => omitDeep(item, ...keys));
68
+ }
69
+ if (isRecord(obj)) {
70
+ const flattenedKeys = new Set(keys.flat());
71
+ return Object.fromEntries(Object.entries(obj)
72
+ .filter(([key]) => !flattenedKeys.has(key))
73
+ .map(([key, value]) => [key, omitDeep(value, ...keys)]));
74
+ }
75
+ return obj;
76
+ }
61
77
  export function omitBy(obj, predicate) {
62
78
  return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
63
79
  const k = key;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.18.6",
3
+ "version": "1.20.1",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -81,7 +81,7 @@
81
81
  "yaml": "^2.7.1",
82
82
  "zod": "^3.24.4",
83
83
  "zod-to-json-schema": "^3.24.5",
84
- "@aigne/platform-helpers": "^0.1.1"
84
+ "@aigne/platform-helpers": "^0.1.2"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@types/bun": "^1.2.12",