@aigne/core 1.8.0 → 1.10.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/lib/cjs/agents/agent.d.ts +29 -3
  3. package/lib/cjs/agents/agent.js +48 -23
  4. package/lib/cjs/agents/ai-agent.d.ts +3 -2
  5. package/lib/cjs/agents/ai-agent.js +22 -9
  6. package/lib/cjs/agents/user-agent.d.ts +3 -3
  7. package/lib/cjs/agents/user-agent.js +14 -8
  8. package/lib/cjs/execution-engine/context.d.ts +18 -7
  9. package/lib/cjs/execution-engine/context.js +76 -28
  10. package/lib/cjs/loader/index.js +20 -4
  11. package/lib/cjs/models/chat-model.d.ts +4 -0
  12. package/lib/cjs/models/chat-model.js +6 -0
  13. package/lib/cjs/models/deepseek-chat-model.d.ts +7 -0
  14. package/lib/cjs/models/deepseek-chat-model.js +19 -0
  15. package/lib/cjs/models/gemini-chat-model.d.ts +8 -0
  16. package/lib/cjs/models/gemini-chat-model.js +20 -0
  17. package/lib/cjs/models/ollama-chat-model.d.ts +6 -0
  18. package/lib/cjs/models/ollama-chat-model.js +18 -0
  19. package/lib/cjs/models/open-router-chat-model.d.ts +6 -0
  20. package/lib/cjs/models/open-router-chat-model.js +18 -0
  21. package/lib/cjs/models/openai-chat-model.d.ts +33 -2
  22. package/lib/cjs/models/openai-chat-model.js +250 -80
  23. package/lib/cjs/models/xai-chat-model.d.ts +3 -11
  24. package/lib/cjs/models/xai-chat-model.js +1 -14
  25. package/lib/cjs/prompt/prompt-builder.js +3 -0
  26. package/lib/cjs/utils/prompts.d.ts +1 -0
  27. package/lib/cjs/utils/prompts.js +13 -0
  28. package/lib/cjs/utils/stream-utils.d.ts +15 -0
  29. package/lib/cjs/utils/stream-utils.js +159 -0
  30. package/lib/cjs/utils/type-utils.d.ts +2 -1
  31. package/lib/cjs/utils/type-utils.js +8 -1
  32. package/lib/dts/agents/agent.d.ts +29 -3
  33. package/lib/dts/agents/ai-agent.d.ts +3 -2
  34. package/lib/dts/agents/user-agent.d.ts +3 -3
  35. package/lib/dts/execution-engine/context.d.ts +18 -7
  36. package/lib/dts/models/chat-model.d.ts +4 -0
  37. package/lib/dts/models/deepseek-chat-model.d.ts +7 -0
  38. package/lib/dts/models/gemini-chat-model.d.ts +8 -0
  39. package/lib/dts/models/ollama-chat-model.d.ts +6 -0
  40. package/lib/dts/models/open-router-chat-model.d.ts +6 -0
  41. package/lib/dts/models/openai-chat-model.d.ts +33 -2
  42. package/lib/dts/models/xai-chat-model.d.ts +3 -11
  43. package/lib/dts/utils/prompts.d.ts +1 -0
  44. package/lib/dts/utils/stream-utils.d.ts +15 -0
  45. package/lib/dts/utils/type-utils.d.ts +2 -1
  46. package/lib/esm/agents/agent.d.ts +29 -3
  47. package/lib/esm/agents/agent.js +49 -24
  48. package/lib/esm/agents/ai-agent.d.ts +3 -2
  49. package/lib/esm/agents/ai-agent.js +23 -10
  50. package/lib/esm/agents/user-agent.d.ts +3 -3
  51. package/lib/esm/agents/user-agent.js +15 -9
  52. package/lib/esm/execution-engine/context.d.ts +18 -7
  53. package/lib/esm/execution-engine/context.js +78 -30
  54. package/lib/esm/loader/index.js +20 -4
  55. package/lib/esm/models/chat-model.d.ts +4 -0
  56. package/lib/esm/models/chat-model.js +6 -0
  57. package/lib/esm/models/deepseek-chat-model.d.ts +7 -0
  58. package/lib/esm/models/deepseek-chat-model.js +15 -0
  59. package/lib/esm/models/gemini-chat-model.d.ts +8 -0
  60. package/lib/esm/models/gemini-chat-model.js +16 -0
  61. package/lib/esm/models/ollama-chat-model.d.ts +6 -0
  62. package/lib/esm/models/ollama-chat-model.js +14 -0
  63. package/lib/esm/models/open-router-chat-model.d.ts +6 -0
  64. package/lib/esm/models/open-router-chat-model.js +14 -0
  65. package/lib/esm/models/openai-chat-model.d.ts +33 -2
  66. package/lib/esm/models/openai-chat-model.js +247 -80
  67. package/lib/esm/models/xai-chat-model.d.ts +3 -11
  68. package/lib/esm/models/xai-chat-model.js +1 -11
  69. package/lib/esm/prompt/prompt-builder.js +3 -0
  70. package/lib/esm/utils/prompts.d.ts +1 -0
  71. package/lib/esm/utils/prompts.js +10 -0
  72. package/lib/esm/utils/stream-utils.d.ts +15 -0
  73. package/lib/esm/utils/stream-utils.js +144 -0
  74. package/lib/esm/utils/type-utils.d.ts +2 -1
  75. package/lib/esm/utils/type-utils.js +7 -1
  76. package/package.json +2 -1
@@ -0,0 +1,144 @@
1
+ import equal from "fast-deep-equal";
2
+ import { omitBy } from "./type-utils.js";
3
+ export function objectToAgentResponseStream(json) {
4
+ return new ReadableStream({
5
+ start(controller) {
6
+ controller.enqueue({ delta: { json } });
7
+ controller.close();
8
+ },
9
+ });
10
+ }
11
+ export function mergeAgentResponseChunk(output, chunk) {
12
+ if (chunk.delta.text) {
13
+ for (const [key, text] of Object.entries(chunk.delta.text)) {
14
+ const original = output[key];
15
+ const t = (original || "") + (text || "");
16
+ if (t)
17
+ Object.assign(output, { [key]: t });
18
+ }
19
+ }
20
+ if (chunk.delta.json) {
21
+ Object.assign(output, omitBy(chunk.delta.json, (v) => v === undefined));
22
+ }
23
+ return output;
24
+ }
25
+ export async function agentResponseStreamToObject(stream) {
26
+ const json = {};
27
+ if (stream instanceof ReadableStream) {
28
+ for await (const value of readableStreamToAsyncIterator(stream)) {
29
+ mergeAgentResponseChunk(json, value);
30
+ }
31
+ }
32
+ else {
33
+ for (;;) {
34
+ const chunk = await stream.next();
35
+ if (chunk.value) {
36
+ if (chunk.done) {
37
+ Object.assign(json, chunk.value);
38
+ }
39
+ else {
40
+ mergeAgentResponseChunk(json, chunk.value);
41
+ }
42
+ }
43
+ if (chunk.done)
44
+ break;
45
+ }
46
+ }
47
+ return json;
48
+ }
49
+ export function asyncGeneratorToReadableStream(generator) {
50
+ return new ReadableStream({
51
+ async start(controller) {
52
+ try {
53
+ for (;;) {
54
+ const chunk = await generator.next();
55
+ if (chunk.value) {
56
+ if (chunk.done) {
57
+ controller.enqueue({ delta: { json: chunk.value } });
58
+ }
59
+ else {
60
+ controller.enqueue(chunk.value);
61
+ }
62
+ }
63
+ if (chunk.done)
64
+ break;
65
+ }
66
+ }
67
+ catch (error) {
68
+ controller.error(error);
69
+ }
70
+ finally {
71
+ controller.close();
72
+ }
73
+ },
74
+ });
75
+ }
76
+ export function onAgentResponseStreamEnd(stream, callback, options) {
77
+ return new ReadableStream({
78
+ async start(controller) {
79
+ try {
80
+ const json = {};
81
+ for await (const value of readableStreamToAsyncIterator(stream)) {
82
+ const chunk = options?.processChunk ? options.processChunk(value) : value;
83
+ controller.enqueue(chunk);
84
+ mergeAgentResponseChunk(json, value);
85
+ }
86
+ const result = await callback(json);
87
+ if (result && !equal(result, json)) {
88
+ let chunk = { delta: { json: result } };
89
+ if (options?.processChunk)
90
+ chunk = options.processChunk(chunk);
91
+ controller.enqueue(chunk);
92
+ }
93
+ }
94
+ catch (error) {
95
+ controller.error(options?.errorCallback?.(error) ?? error);
96
+ }
97
+ finally {
98
+ controller.close();
99
+ }
100
+ },
101
+ });
102
+ }
103
+ export function isAsyncGenerator(value) {
104
+ return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
105
+ }
106
+ export async function* arrayToAgentProcessAsyncGenerator(chunks, result) {
107
+ for (const chunk of chunks) {
108
+ if (chunk instanceof Error)
109
+ throw chunk;
110
+ yield chunk;
111
+ }
112
+ if (result !== undefined)
113
+ return result;
114
+ }
115
+ export function arrayToAgentResponseStream(chunks) {
116
+ return new ReadableStream({
117
+ start(controller) {
118
+ for (const chunk of chunks) {
119
+ if (chunk instanceof Error) {
120
+ controller.error(chunk);
121
+ return;
122
+ }
123
+ controller.enqueue(chunk);
124
+ }
125
+ controller.close();
126
+ },
127
+ });
128
+ }
129
+ export async function readableStreamToArray(stream) {
130
+ const result = [];
131
+ for await (const value of readableStreamToAsyncIterator(stream)) {
132
+ result.push(value);
133
+ }
134
+ return result;
135
+ }
136
+ export async function* readableStreamToAsyncIterator(stream) {
137
+ const reader = stream.getReader();
138
+ while (true) {
139
+ const { value, done } = await reader.read();
140
+ if (done)
141
+ break;
142
+ yield value;
143
+ }
144
+ }
@@ -7,9 +7,10 @@ export declare function isEmpty(obj: unknown): boolean;
7
7
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
8
8
  export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
9
9
  export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
10
+ export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
10
11
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
11
12
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
12
13
  [key: string]: T;
13
14
  };
14
- export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
15
+ export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): T;
15
16
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
@@ -31,6 +31,12 @@ export function duplicates(arr, key = (item) => item) {
31
31
  }
32
32
  return Array.from(duplicates);
33
33
  }
34
+ export function omitBy(obj, predicate) {
35
+ return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
36
+ const k = key;
37
+ return !predicate(value, k);
38
+ }));
39
+ }
34
40
  export function orArrayToArray(value) {
35
41
  if (isNil(value))
36
42
  return [];
@@ -43,7 +49,7 @@ export function createAccessorArray(array, accessor) {
43
49
  }
44
50
  export function checkArguments(prefix, schema, args) {
45
51
  try {
46
- schema.parse(args, {
52
+ return schema.parse(args, {
47
53
  errorMap: (issue, ctx) => {
48
54
  if (issue.code === "invalid_union") {
49
55
  // handle all issues that are not invalid_type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -66,6 +66,7 @@
66
66
  "@modelcontextprotocol/sdk": "^1.9.0",
67
67
  "@types/debug": "^4.1.12",
68
68
  "debug": "^4.4.0",
69
+ "fast-deep-equal": "^3.1.3",
69
70
  "inquirer": "^12.5.2",
70
71
  "mustache": "^4.2.0",
71
72
  "nanoid": "^5.1.5",