@aigne/core 1.18.2 → 1.18.4

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 CHANGED
@@ -5,6 +5,20 @@
5
5
 
6
6
  * add user context support ([#131](https://github.com/AIGNE-io/aigne-framework/issues/131)) ([4dd9d20](https://github.com/AIGNE-io/aigne-framework/commit/4dd9d20953f6ac33933723db56efd9b44bafeb02))
7
7
 
8
+ ## [1.18.4](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.3...core-v1.18.4) (2025-06-05)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **core:** prioritize returning json chunks ([#151](https://github.com/AIGNE-io/aigne-framework/issues/151)) ([8bf49a1](https://github.com/AIGNE-io/aigne-framework/commit/8bf49a18c083b33d2e0b35e8d0f22f68d9d6effa))
14
+
15
+ ## [1.18.3](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.2...core-v1.18.3) (2025-06-05)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * compatible nodejs version >=20 ([#149](https://github.com/AIGNE-io/aigne-framework/issues/149)) ([d5ae9f2](https://github.com/AIGNE-io/aigne-framework/commit/d5ae9f245972e87e70fd87cdd960ade9940f288c))
21
+
8
22
  ## [1.18.2](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.18.1...core-v1.18.2) (2025-05-30)
9
23
 
10
24
 
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.AIGNEContext = void 0;
7
+ const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
4
8
  const strict_event_emitter_1 = require("strict-event-emitter");
5
9
  const uuid_1 = require("uuid");
6
10
  const zod_1 = require("zod");
@@ -8,6 +12,7 @@ const agent_js_1 = require("../agents/agent.js");
8
12
  const types_js_1 = require("../agents/types.js");
9
13
  const user_agent_js_1 = require("../agents/user-agent.js");
10
14
  const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
15
+ const promise_js_1 = require("../utils/promise.js");
11
16
  const stream_utils_js_1 = require("../utils/stream-utils.js");
12
17
  const type_utils_js_1 = require("../utils/type-utils.js");
13
18
  const message_queue_js_1 = require("./message-queue.js");
@@ -78,7 +83,7 @@ class AIGNEContext {
78
83
  }
79
84
  return output;
80
85
  }
81
- const activeAgentPromise = Promise.withResolvers();
86
+ const activeAgentPromise = (0, promise_js_1.promiseWithResolvers)();
82
87
  const stream = (0, stream_utils_js_1.onAgentResponseStreamEnd)((0, stream_utils_js_1.asyncGeneratorToReadableStream)(response), async ({ __activeAgent__: activeAgent }) => {
83
88
  activeAgentPromise.resolve(activeAgent);
84
89
  }, {
@@ -181,7 +186,6 @@ class AIGNEContextShared {
181
186
  }
182
187
  async *invokeAgent(agent, input, context, options) {
183
188
  let activeAgent = agent;
184
- let output;
185
189
  for (;;) {
186
190
  const result = {};
187
191
  if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
@@ -194,12 +198,14 @@ class AIGNEContextShared {
194
198
  }
195
199
  const stream = await activeAgent.invoke(input, { ...options, context, streaming: true });
196
200
  for await (const value of stream) {
197
- if (value.delta.text) {
198
- yield { delta: { text: value.delta.text } };
199
- }
200
201
  if (value.delta.json) {
202
+ value.delta.json = omitExistsProperties(result, value.delta.json);
201
203
  Object.assign(result, value.delta.json);
202
204
  }
205
+ delete value.delta.json?.[types_js_1.transferAgentOutputKey];
206
+ if ((0, agent_js_1.isEmptyChunk)(value))
207
+ continue;
208
+ yield value;
203
209
  }
204
210
  if (!options?.disableTransfer) {
205
211
  const transferToAgent = (0, types_js_1.isTransferAgentOutput)(result)
@@ -210,24 +216,25 @@ class AIGNEContextShared {
210
216
  continue;
211
217
  }
212
218
  }
213
- output = result;
214
219
  break;
215
220
  }
216
- if (!output)
217
- throw new Error("Unexpected empty output");
218
221
  yield {
219
222
  delta: {
220
- json: {
221
- ...output,
222
- __activeAgent__: activeAgent,
223
- },
223
+ json: { __activeAgent__: activeAgent },
224
224
  },
225
225
  };
226
226
  }
227
227
  }
228
+ function omitExistsProperties(result, { ...delta }) {
229
+ for (const [key, val] of Object.entries(delta)) {
230
+ if ((0, fast_deep_equal_1.default)(result[key], val))
231
+ delete delta[key];
232
+ }
233
+ return (0, type_utils_js_1.isEmpty)(delta) ? undefined : delta;
234
+ }
228
235
  async function* withAbortSignal(signal, error, fn) {
229
236
  const iterator = fn();
230
- const timeoutPromise = Promise.withResolvers();
237
+ const timeoutPromise = (0, promise_js_1.promiseWithResolvers)();
231
238
  const listener = () => {
232
239
  timeoutPromise.reject(error);
233
240
  };
@@ -0,0 +1,6 @@
1
+ export interface PromiseWithResolvers<T> {
2
+ promise: Promise<T>;
3
+ resolve: (value: T | PromiseLike<T>) => void;
4
+ reject: (reason?: unknown) => void;
5
+ }
6
+ export declare function promiseWithResolvers<T = void>(): PromiseWithResolvers<T>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.promiseWithResolvers = promiseWithResolvers;
4
+ function promiseWithResolvers() {
5
+ let resolve;
6
+ let reject;
7
+ const promise = new Promise((res, rej) => {
8
+ resolve = res;
9
+ reject = rej;
10
+ });
11
+ return { promise, resolve, reject };
12
+ }
@@ -0,0 +1,6 @@
1
+ export interface PromiseWithResolvers<T> {
2
+ promise: Promise<T>;
3
+ resolve: (value: T | PromiseLike<T>) => void;
4
+ reject: (reason?: unknown) => void;
5
+ }
6
+ export declare function promiseWithResolvers<T = void>(): PromiseWithResolvers<T>;
@@ -1,12 +1,14 @@
1
+ import equal from "fast-deep-equal";
1
2
  import { Emitter } from "strict-event-emitter";
2
3
  import { v7 } from "uuid";
3
4
  import { z } from "zod";
4
- import { Agent, } from "../agents/agent.js";
5
+ import { Agent, isEmptyChunk, } from "../agents/agent.js";
5
6
  import { isTransferAgentOutput, transferAgentOutputKey, } from "../agents/types.js";
6
7
  import { UserAgent } from "../agents/user-agent.js";
7
8
  import { createMessage } from "../prompt/prompt-builder.js";
9
+ import { promiseWithResolvers } from "../utils/promise.js";
8
10
  import { agentResponseStreamToObject, asyncGeneratorToReadableStream, onAgentResponseStreamEnd, } from "../utils/stream-utils.js";
9
- import { checkArguments, isNil, omitBy, } from "../utils/type-utils.js";
11
+ import { checkArguments, isEmpty, isNil, omitBy, } from "../utils/type-utils.js";
10
12
  import { MessageQueue, toMessagePayload, } from "./message-queue.js";
11
13
  import { newEmptyContextUsage } from "./usage.js";
12
14
  /**
@@ -75,7 +77,7 @@ export class AIGNEContext {
75
77
  }
76
78
  return output;
77
79
  }
78
- const activeAgentPromise = Promise.withResolvers();
80
+ const activeAgentPromise = promiseWithResolvers();
79
81
  const stream = onAgentResponseStreamEnd(asyncGeneratorToReadableStream(response), async ({ __activeAgent__: activeAgent }) => {
80
82
  activeAgentPromise.resolve(activeAgent);
81
83
  }, {
@@ -177,7 +179,6 @@ class AIGNEContextShared {
177
179
  }
178
180
  async *invokeAgent(agent, input, context, options) {
179
181
  let activeAgent = agent;
180
- let output;
181
182
  for (;;) {
182
183
  const result = {};
183
184
  if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
@@ -190,12 +191,14 @@ class AIGNEContextShared {
190
191
  }
191
192
  const stream = await activeAgent.invoke(input, { ...options, context, streaming: true });
192
193
  for await (const value of stream) {
193
- if (value.delta.text) {
194
- yield { delta: { text: value.delta.text } };
195
- }
196
194
  if (value.delta.json) {
195
+ value.delta.json = omitExistsProperties(result, value.delta.json);
197
196
  Object.assign(result, value.delta.json);
198
197
  }
198
+ delete value.delta.json?.[transferAgentOutputKey];
199
+ if (isEmptyChunk(value))
200
+ continue;
201
+ yield value;
199
202
  }
200
203
  if (!options?.disableTransfer) {
201
204
  const transferToAgent = isTransferAgentOutput(result)
@@ -206,24 +209,25 @@ class AIGNEContextShared {
206
209
  continue;
207
210
  }
208
211
  }
209
- output = result;
210
212
  break;
211
213
  }
212
- if (!output)
213
- throw new Error("Unexpected empty output");
214
214
  yield {
215
215
  delta: {
216
- json: {
217
- ...output,
218
- __activeAgent__: activeAgent,
219
- },
216
+ json: { __activeAgent__: activeAgent },
220
217
  },
221
218
  };
222
219
  }
223
220
  }
221
+ function omitExistsProperties(result, { ...delta }) {
222
+ for (const [key, val] of Object.entries(delta)) {
223
+ if (equal(result[key], val))
224
+ delete delta[key];
225
+ }
226
+ return isEmpty(delta) ? undefined : delta;
227
+ }
224
228
  async function* withAbortSignal(signal, error, fn) {
225
229
  const iterator = fn();
226
- const timeoutPromise = Promise.withResolvers();
230
+ const timeoutPromise = promiseWithResolvers();
227
231
  const listener = () => {
228
232
  timeoutPromise.reject(error);
229
233
  };
@@ -0,0 +1,6 @@
1
+ export interface PromiseWithResolvers<T> {
2
+ promise: Promise<T>;
3
+ resolve: (value: T | PromiseLike<T>) => void;
4
+ reject: (reason?: unknown) => void;
5
+ }
6
+ export declare function promiseWithResolvers<T = void>(): PromiseWithResolvers<T>;
@@ -0,0 +1,9 @@
1
+ export function promiseWithResolvers() {
2
+ let resolve;
3
+ let reject;
4
+ const promise = new Promise((res, rej) => {
5
+ resolve = res;
6
+ reject = rej;
7
+ });
8
+ return { promise, resolve, reject };
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.18.2",
3
+ "version": "1.18.4",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"