@aigne/core 1.43.0 → 1.43.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.
package/CHANGELOG.md CHANGED
@@ -12,6 +12,13 @@
12
12
  * dependencies
13
13
  * @aigne/observability bumped to 0.1.0
14
14
 
15
+ ## [1.43.1](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.43.0...core-v1.43.1) (2025-08-05)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **core:** filter empty memory content ([#312](https://github.com/AIGNE-io/aigne-framework/issues/312)) ([39dd77a](https://github.com/AIGNE-io/aigne-framework/commit/39dd77a68154d51c7a132adccd9f21b8bc461be0))
21
+
15
22
  ## [1.43.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.42.0...core-v1.43.0) (2025-08-04)
16
23
 
17
24
 
package/README.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @aigne/core
2
2
 
3
+ <p align="center">
4
+ <picture>
5
+ <source srcset="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo-dark.svg" media="(prefers-color-scheme: dark)">
6
+ <source srcset="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo.svg" media="(prefers-color-scheme: light)">
7
+ <img src="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo.svg" alt="AIGNE Logo" width="400" />
8
+ </picture>
9
+ </p>
10
+
3
11
  [![GitHub star chart](https://img.shields.io/github/stars/AIGNE-io/aigne-framework?style=flat-square)](https://star-history.com/#AIGNE-io/aigne-framework)
4
12
  [![Open Issues](https://img.shields.io/github/issues-raw/AIGNE-io/aigne-framework?style=flat-square)](https://github.com/AIGNE-io/aigne-framework/issues)
5
13
  [![codecov](https://codecov.io/gh/AIGNE-io/aigne-framework/graph/badge.svg?token=DO07834RQL)](https://codecov.io/gh/AIGNE-io/aigne-framework)
@@ -11,4 +11,6 @@ export * from "./aigne/index.js";
11
11
  export * from "./memory/index.js";
12
12
  export * from "./prompt/prompt-builder.js";
13
13
  export * from "./prompt/template.js";
14
+ export * from "./utils/json-utils.js";
15
+ export * from "./utils/role-utils.js";
14
16
  export * from "./utils/stream-utils.js";
package/lib/cjs/index.js CHANGED
@@ -27,4 +27,6 @@ __exportStar(require("./aigne/index.js"), exports);
27
27
  __exportStar(require("./memory/index.js"), exports);
28
28
  __exportStar(require("./prompt/prompt-builder.js"), exports);
29
29
  __exportStar(require("./prompt/template.js"), exports);
30
+ __exportStar(require("./utils/json-utils.js"), exports);
31
+ __exportStar(require("./utils/role-utils.js"), exports);
30
32
  __exportStar(require("./utils/stream-utils.js"), exports);
@@ -110,7 +110,12 @@ class PromptBuilder {
110
110
  const stringOrStringify = (value) => typeof value === "string" ? value : (0, yaml_1.stringify)(value);
111
111
  for (const { content } of memories) {
112
112
  if ((0, type_utils_js_1.isRecord)(content) && "input" in content && "output" in content) {
113
- messages.push({ role: "user", content: stringOrStringify(content.input) }, { role: "agent", content: stringOrStringify(content.output) });
113
+ if (!(0, type_utils_js_1.isNil)(content.input) && content.input !== "") {
114
+ messages.push({ role: "user", content: stringOrStringify(content.input) });
115
+ }
116
+ if (!(0, type_utils_js_1.isNil)(content.output) && content.output !== "") {
117
+ messages.push({ role: "agent", content: stringOrStringify(content.output) });
118
+ }
114
119
  }
115
120
  else {
116
121
  other.push(content);
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Safely parses JSON text using jaison library which handles malformed JSON better than JSON.parse
3
+ *
4
+ * @param text - The text to parse as JSON
5
+ * @returns Parsed JSON object or null if parsing fails
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const result = safeParseJSON('{"key": "value"}');
10
+ * console.log(result); // { key: "value" }
11
+ *
12
+ * const malformed = safeParseJSON('{"key": "value"'); // Missing closing brace
13
+ * console.log(malformed); // null
14
+ * ```
15
+ */
16
+ export declare function safeParseJSON(text: string): any;
17
+ /**
18
+ * Safely stringifies a value to JSON, handling errors gracefully
19
+ *
20
+ * @param value - The value to stringify
21
+ * @param space - Optional spacing for pretty printing
22
+ * @returns JSON string or null if stringification fails
23
+ */
24
+ export declare function safeStringifyJSON(value: any, space?: number): string | null;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.safeParseJSON = safeParseJSON;
7
+ exports.safeStringifyJSON = safeStringifyJSON;
8
+ const jaison_1 = __importDefault(require("jaison"));
9
+ /**
10
+ * Safely parses JSON text using jaison library which handles malformed JSON better than JSON.parse
11
+ *
12
+ * @param text - The text to parse as JSON
13
+ * @returns Parsed JSON object or null if parsing fails
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const result = safeParseJSON('{"key": "value"}');
18
+ * console.log(result); // { key: "value" }
19
+ *
20
+ * const malformed = safeParseJSON('{"key": "value"'); // Missing closing brace
21
+ * console.log(malformed); // null
22
+ * ```
23
+ */
24
+ function safeParseJSON(text) {
25
+ if (!text)
26
+ return null;
27
+ try {
28
+ return (0, jaison_1.default)(text);
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ }
34
+ /**
35
+ * Safely stringifies a value to JSON, handling errors gracefully
36
+ *
37
+ * @param value - The value to stringify
38
+ * @param space - Optional spacing for pretty printing
39
+ * @returns JSON string or null if stringification fails
40
+ */
41
+ function safeStringifyJSON(value, space) {
42
+ try {
43
+ return JSON.stringify(value, null, space);
44
+ }
45
+ catch {
46
+ return null;
47
+ }
48
+ }
@@ -0,0 +1,31 @@
1
+ import type { Role } from "../agents/chat-model.js";
2
+ /**
3
+ * Standard role mapping for most chat model providers
4
+ * Maps AIGNE framework roles to common provider role names
5
+ */
6
+ export declare const STANDARD_ROLE_MAP: {
7
+ [key in Role]: string;
8
+ };
9
+ /**
10
+ * Creates a role mapper function for a specific provider
11
+ *
12
+ * @param roleMap - Custom role mapping for the provider
13
+ * @returns Function that maps AIGNE roles to provider roles
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // For standard providers (OpenAI, Anthropic, etc.)
18
+ * const mapRole = createRoleMapper(STANDARD_ROLE_MAP);
19
+ *
20
+ * // For providers with different role names
21
+ * const customMap = { ...STANDARD_ROLE_MAP, agent: "bot" };
22
+ * const customMapper = createRoleMapper(customMap);
23
+ * ```
24
+ */
25
+ export declare function createRoleMapper<T extends string>(roleMap: {
26
+ [key in Role]: T;
27
+ }): (role: Role) => T;
28
+ /**
29
+ * Standard role mapper using the default role mapping
30
+ */
31
+ export declare const mapStandardRole: (role: Role) => string;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapStandardRole = exports.STANDARD_ROLE_MAP = void 0;
4
+ exports.createRoleMapper = createRoleMapper;
5
+ /**
6
+ * Standard role mapping for most chat model providers
7
+ * Maps AIGNE framework roles to common provider role names
8
+ */
9
+ exports.STANDARD_ROLE_MAP = {
10
+ system: "system",
11
+ user: "user",
12
+ agent: "assistant",
13
+ tool: "tool",
14
+ };
15
+ /**
16
+ * Creates a role mapper function for a specific provider
17
+ *
18
+ * @param roleMap - Custom role mapping for the provider
19
+ * @returns Function that maps AIGNE roles to provider roles
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * // For standard providers (OpenAI, Anthropic, etc.)
24
+ * const mapRole = createRoleMapper(STANDARD_ROLE_MAP);
25
+ *
26
+ * // For providers with different role names
27
+ * const customMap = { ...STANDARD_ROLE_MAP, agent: "bot" };
28
+ * const customMapper = createRoleMapper(customMap);
29
+ * ```
30
+ */
31
+ function createRoleMapper(roleMap) {
32
+ return (role) => roleMap[role];
33
+ }
34
+ /**
35
+ * Standard role mapper using the default role mapping
36
+ */
37
+ exports.mapStandardRole = createRoleMapper(exports.STANDARD_ROLE_MAP);
@@ -11,4 +11,6 @@ export * from "./aigne/index.js";
11
11
  export * from "./memory/index.js";
12
12
  export * from "./prompt/prompt-builder.js";
13
13
  export * from "./prompt/template.js";
14
+ export * from "./utils/json-utils.js";
15
+ export * from "./utils/role-utils.js";
14
16
  export * from "./utils/stream-utils.js";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Safely parses JSON text using jaison library which handles malformed JSON better than JSON.parse
3
+ *
4
+ * @param text - The text to parse as JSON
5
+ * @returns Parsed JSON object or null if parsing fails
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const result = safeParseJSON('{"key": "value"}');
10
+ * console.log(result); // { key: "value" }
11
+ *
12
+ * const malformed = safeParseJSON('{"key": "value"'); // Missing closing brace
13
+ * console.log(malformed); // null
14
+ * ```
15
+ */
16
+ export declare function safeParseJSON(text: string): any;
17
+ /**
18
+ * Safely stringifies a value to JSON, handling errors gracefully
19
+ *
20
+ * @param value - The value to stringify
21
+ * @param space - Optional spacing for pretty printing
22
+ * @returns JSON string or null if stringification fails
23
+ */
24
+ export declare function safeStringifyJSON(value: any, space?: number): string | null;
@@ -0,0 +1,31 @@
1
+ import type { Role } from "../agents/chat-model.js";
2
+ /**
3
+ * Standard role mapping for most chat model providers
4
+ * Maps AIGNE framework roles to common provider role names
5
+ */
6
+ export declare const STANDARD_ROLE_MAP: {
7
+ [key in Role]: string;
8
+ };
9
+ /**
10
+ * Creates a role mapper function for a specific provider
11
+ *
12
+ * @param roleMap - Custom role mapping for the provider
13
+ * @returns Function that maps AIGNE roles to provider roles
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // For standard providers (OpenAI, Anthropic, etc.)
18
+ * const mapRole = createRoleMapper(STANDARD_ROLE_MAP);
19
+ *
20
+ * // For providers with different role names
21
+ * const customMap = { ...STANDARD_ROLE_MAP, agent: "bot" };
22
+ * const customMapper = createRoleMapper(customMap);
23
+ * ```
24
+ */
25
+ export declare function createRoleMapper<T extends string>(roleMap: {
26
+ [key in Role]: T;
27
+ }): (role: Role) => T;
28
+ /**
29
+ * Standard role mapper using the default role mapping
30
+ */
31
+ export declare const mapStandardRole: (role: Role) => string;
@@ -11,4 +11,6 @@ export * from "./aigne/index.js";
11
11
  export * from "./memory/index.js";
12
12
  export * from "./prompt/prompt-builder.js";
13
13
  export * from "./prompt/template.js";
14
+ export * from "./utils/json-utils.js";
15
+ export * from "./utils/role-utils.js";
14
16
  export * from "./utils/stream-utils.js";
package/lib/esm/index.js CHANGED
@@ -11,4 +11,6 @@ export * from "./aigne/index.js";
11
11
  export * from "./memory/index.js";
12
12
  export * from "./prompt/prompt-builder.js";
13
13
  export * from "./prompt/template.js";
14
+ export * from "./utils/json-utils.js";
15
+ export * from "./utils/role-utils.js";
14
16
  export * from "./utils/stream-utils.js";
@@ -4,7 +4,7 @@ import { ZodObject } from "zod";
4
4
  import { zodToJsonSchema } from "zod-to-json-schema";
5
5
  import { Agent } from "../agents/agent.js";
6
6
  import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
7
- import { isRecord, unique } from "../utils/type-utils.js";
7
+ import { isNil, isRecord, unique } from "../utils/type-utils.js";
8
8
  import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
9
9
  import { STRUCTURED_STREAM_INSTRUCTIONS } from "./prompts/structured-stream-instructions.js";
10
10
  import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
@@ -107,7 +107,12 @@ export class PromptBuilder {
107
107
  const stringOrStringify = (value) => typeof value === "string" ? value : stringify(value);
108
108
  for (const { content } of memories) {
109
109
  if (isRecord(content) && "input" in content && "output" in content) {
110
- messages.push({ role: "user", content: stringOrStringify(content.input) }, { role: "agent", content: stringOrStringify(content.output) });
110
+ if (!isNil(content.input) && content.input !== "") {
111
+ messages.push({ role: "user", content: stringOrStringify(content.input) });
112
+ }
113
+ if (!isNil(content.output) && content.output !== "") {
114
+ messages.push({ role: "agent", content: stringOrStringify(content.output) });
115
+ }
111
116
  }
112
117
  else {
113
118
  other.push(content);
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Safely parses JSON text using jaison library which handles malformed JSON better than JSON.parse
3
+ *
4
+ * @param text - The text to parse as JSON
5
+ * @returns Parsed JSON object or null if parsing fails
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const result = safeParseJSON('{"key": "value"}');
10
+ * console.log(result); // { key: "value" }
11
+ *
12
+ * const malformed = safeParseJSON('{"key": "value"'); // Missing closing brace
13
+ * console.log(malformed); // null
14
+ * ```
15
+ */
16
+ export declare function safeParseJSON(text: string): any;
17
+ /**
18
+ * Safely stringifies a value to JSON, handling errors gracefully
19
+ *
20
+ * @param value - The value to stringify
21
+ * @param space - Optional spacing for pretty printing
22
+ * @returns JSON string or null if stringification fails
23
+ */
24
+ export declare function safeStringifyJSON(value: any, space?: number): string | null;
@@ -0,0 +1,41 @@
1
+ import jaison from "jaison";
2
+ /**
3
+ * Safely parses JSON text using jaison library which handles malformed JSON better than JSON.parse
4
+ *
5
+ * @param text - The text to parse as JSON
6
+ * @returns Parsed JSON object or null if parsing fails
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const result = safeParseJSON('{"key": "value"}');
11
+ * console.log(result); // { key: "value" }
12
+ *
13
+ * const malformed = safeParseJSON('{"key": "value"'); // Missing closing brace
14
+ * console.log(malformed); // null
15
+ * ```
16
+ */
17
+ export function safeParseJSON(text) {
18
+ if (!text)
19
+ return null;
20
+ try {
21
+ return jaison(text);
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ }
27
+ /**
28
+ * Safely stringifies a value to JSON, handling errors gracefully
29
+ *
30
+ * @param value - The value to stringify
31
+ * @param space - Optional spacing for pretty printing
32
+ * @returns JSON string or null if stringification fails
33
+ */
34
+ export function safeStringifyJSON(value, space) {
35
+ try {
36
+ return JSON.stringify(value, null, space);
37
+ }
38
+ catch {
39
+ return null;
40
+ }
41
+ }
@@ -0,0 +1,31 @@
1
+ import type { Role } from "../agents/chat-model.js";
2
+ /**
3
+ * Standard role mapping for most chat model providers
4
+ * Maps AIGNE framework roles to common provider role names
5
+ */
6
+ export declare const STANDARD_ROLE_MAP: {
7
+ [key in Role]: string;
8
+ };
9
+ /**
10
+ * Creates a role mapper function for a specific provider
11
+ *
12
+ * @param roleMap - Custom role mapping for the provider
13
+ * @returns Function that maps AIGNE roles to provider roles
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // For standard providers (OpenAI, Anthropic, etc.)
18
+ * const mapRole = createRoleMapper(STANDARD_ROLE_MAP);
19
+ *
20
+ * // For providers with different role names
21
+ * const customMap = { ...STANDARD_ROLE_MAP, agent: "bot" };
22
+ * const customMapper = createRoleMapper(customMap);
23
+ * ```
24
+ */
25
+ export declare function createRoleMapper<T extends string>(roleMap: {
26
+ [key in Role]: T;
27
+ }): (role: Role) => T;
28
+ /**
29
+ * Standard role mapper using the default role mapping
30
+ */
31
+ export declare const mapStandardRole: (role: Role) => string;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Standard role mapping for most chat model providers
3
+ * Maps AIGNE framework roles to common provider role names
4
+ */
5
+ export const STANDARD_ROLE_MAP = {
6
+ system: "system",
7
+ user: "user",
8
+ agent: "assistant",
9
+ tool: "tool",
10
+ };
11
+ /**
12
+ * Creates a role mapper function for a specific provider
13
+ *
14
+ * @param roleMap - Custom role mapping for the provider
15
+ * @returns Function that maps AIGNE roles to provider roles
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // For standard providers (OpenAI, Anthropic, etc.)
20
+ * const mapRole = createRoleMapper(STANDARD_ROLE_MAP);
21
+ *
22
+ * // For providers with different role names
23
+ * const customMap = { ...STANDARD_ROLE_MAP, agent: "bot" };
24
+ * const customMapper = createRoleMapper(customMap);
25
+ * ```
26
+ */
27
+ export function createRoleMapper(roleMap) {
28
+ return (role) => roleMap[role];
29
+ }
30
+ /**
31
+ * Standard role mapper using the default role mapping
32
+ */
33
+ export const mapStandardRole = createRoleMapper(STANDARD_ROLE_MAP);
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.43.0",
3
+ "version": "1.43.1",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
9
- "homepage": "https://github.com/AIGNE-io/aigne-framework",
9
+ "homepage": "https://www.aigne.io/framework",
10
10
  "license": "Elastic-2.0",
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "git+https://github.com/AIGNE-io/aigne-framework"
14
14
  },
15
+ "bugs": {
16
+ "url": "https://github.com/AIGNE-io/aigne-framework/issues"
17
+ },
15
18
  "files": [
16
19
  "lib/cjs",
17
20
  "lib/dts",
@@ -74,6 +77,7 @@
74
77
  "eventsource-parser": "^3.0.3",
75
78
  "fast-deep-equal": "^3.1.3",
76
79
  "immer": "^10.1.1",
80
+ "jaison": "^2.0.2",
77
81
  "jsonata": "^2.0.6",
78
82
  "mustache": "^4.2.0",
79
83
  "nanoid": "^5.1.5",