@aigne/core 1.61.0-beta.2 → 1.61.0-beta.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 +16 -0
- package/lib/cjs/agents/chat-model.d.ts +1 -0
- package/lib/cjs/agents/chat-model.js +7 -1
- package/lib/cjs/loader/agent-yaml.d.ts +3 -1
- package/lib/cjs/loader/agent-yaml.js +32 -9
- package/lib/cjs/loader/index.js +6 -2
- package/lib/cjs/prompt/template.d.ts +110 -8
- package/lib/cjs/prompt/template.js +26 -19
- package/lib/dts/agents/chat-model.d.ts +1 -0
- package/lib/dts/loader/agent-yaml.d.ts +3 -1
- package/lib/dts/prompt/template.d.ts +110 -8
- package/lib/esm/agents/chat-model.d.ts +1 -0
- package/lib/esm/agents/chat-model.js +6 -0
- package/lib/esm/loader/agent-yaml.d.ts +3 -1
- package/lib/esm/loader/agent-yaml.js +32 -9
- package/lib/esm/loader/index.js +6 -2
- package/lib/esm/prompt/template.d.ts +110 -8
- package/lib/esm/prompt/template.js +26 -20
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.61.0-beta.4](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.61.0-beta.3...core-v1.61.0-beta.4) (2025-09-25)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Dependencies
|
|
7
|
+
|
|
8
|
+
* The following workspace dependencies were updated
|
|
9
|
+
* dependencies
|
|
10
|
+
* @aigne/observability-api bumped to 0.10.5-beta
|
|
11
|
+
|
|
12
|
+
## [1.61.0-beta.3](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.61.0-beta.2...core-v1.61.0-beta.3) (2025-09-24)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* **core:** add multi-roles instructions support for agent yaml ([#538](https://github.com/AIGNE-io/aigne-framework/issues/538)) ([97bf77f](https://github.com/AIGNE-io/aigne-framework/commit/97bf77f96b5f69321539311159010499eb3b1b25))
|
|
18
|
+
|
|
3
19
|
## [1.61.0-beta.2](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.61.0-beta.1...core-v1.61.0-beta.2) (2025-09-23)
|
|
4
20
|
|
|
5
21
|
|
|
@@ -158,6 +158,7 @@ export interface ChatModelInput extends Message {
|
|
|
158
158
|
* - tool: Tool call responses
|
|
159
159
|
*/
|
|
160
160
|
export type Role = "system" | "user" | "agent" | "tool";
|
|
161
|
+
export declare const roleSchema: z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"user">, z.ZodLiteral<"agent">, z.ZodLiteral<"tool">]>;
|
|
161
162
|
/**
|
|
162
163
|
* Structure of input messages
|
|
163
164
|
*
|
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.chatModelOutputUsageSchema = exports.unionContentSchema = exports.textContentSchema = exports.ChatModel = exports.StructuredOutputError = void 0;
|
|
36
|
+
exports.chatModelOutputUsageSchema = exports.unionContentSchema = exports.textContentSchema = exports.roleSchema = exports.ChatModel = exports.StructuredOutputError = void 0;
|
|
37
37
|
const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
|
|
38
38
|
const ajv_1 = require("ajv");
|
|
39
39
|
const zod_1 = require("zod");
|
|
@@ -257,6 +257,12 @@ class ChatModel extends model_js_1.Model {
|
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
exports.ChatModel = ChatModel;
|
|
260
|
+
exports.roleSchema = zod_1.z.union([
|
|
261
|
+
zod_1.z.literal("system"),
|
|
262
|
+
zod_1.z.literal("user"),
|
|
263
|
+
zod_1.z.literal("agent"),
|
|
264
|
+
zod_1.z.literal("tool"),
|
|
265
|
+
]);
|
|
260
266
|
exports.textContentSchema = zod_1.z.object({
|
|
261
267
|
type: zod_1.z.literal("text"),
|
|
262
268
|
text: zod_1.z.string(),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ZodType, z } from "zod";
|
|
2
2
|
import type { AgentHooks, FunctionAgentFn, TaskRenderMode } from "../agents/agent.js";
|
|
3
3
|
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
4
|
+
import { type Role } from "../agents/chat-model.js";
|
|
4
5
|
import { ProcessMode, type ReflectionMode } from "../agents/team-agent.js";
|
|
5
6
|
import { chatModelSchema, imageModelSchema } from "./schema.js";
|
|
6
7
|
export interface HooksSchema {
|
|
@@ -36,9 +37,10 @@ export interface BaseAgentSchema {
|
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
export type Instructions = {
|
|
40
|
+
role: Exclude<Role, "tool">;
|
|
39
41
|
content: string;
|
|
40
42
|
path: string;
|
|
41
|
-
};
|
|
43
|
+
}[];
|
|
42
44
|
export interface AIAgentSchema extends BaseAgentSchema {
|
|
43
45
|
type: "ai";
|
|
44
46
|
instructions?: Instructions;
|
|
@@ -7,6 +7,7 @@ const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
|
|
|
7
7
|
const yaml_1 = require("yaml");
|
|
8
8
|
const zod_1 = require("zod");
|
|
9
9
|
const ai_agent_js_1 = require("../agents/ai-agent.js");
|
|
10
|
+
const chat_model_js_1 = require("../agents/chat-model.js");
|
|
10
11
|
const team_agent_js_1 = require("../agents/team-agent.js");
|
|
11
12
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
12
13
|
const schema_js_1 = require("./schema.js");
|
|
@@ -52,18 +53,40 @@ async function parseAgentFile(path, data) {
|
|
|
52
53
|
})),
|
|
53
54
|
])),
|
|
54
55
|
});
|
|
55
|
-
const
|
|
56
|
-
.union([
|
|
57
|
-
zod_1.z.string(),
|
|
56
|
+
const instructionItemSchema = zod_1.z.union([
|
|
58
57
|
zod_1.z.object({
|
|
58
|
+
role: chat_model_js_1.roleSchema.default("system"),
|
|
59
59
|
url: zod_1.z.string(),
|
|
60
60
|
}),
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
zod_1.z.object({
|
|
62
|
+
role: chat_model_js_1.roleSchema.default("system"),
|
|
63
|
+
content: zod_1.z.string(),
|
|
64
|
+
}),
|
|
65
|
+
]);
|
|
66
|
+
const parseInstructionItem = async ({ role, ...v }) => {
|
|
67
|
+
if (role === "tool")
|
|
68
|
+
throw new Error(`'tool' role is not allowed in instruction item in agent file ${path}`);
|
|
69
|
+
if ("content" in v && typeof v.content === "string") {
|
|
70
|
+
return { role, content: v.content, path };
|
|
71
|
+
}
|
|
72
|
+
if ("url" in v && typeof v.url === "string") {
|
|
73
|
+
const url = index_js_1.nodejs.path.isAbsolute(v.url)
|
|
74
|
+
? v.url
|
|
75
|
+
: index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), v.url);
|
|
76
|
+
return index_js_1.nodejs.fs.readFile(url, "utf8").then((content) => ({ role, content, path: url }));
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Invalid instruction item in agent file ${path}. Expected 'content' or 'url' property`);
|
|
79
|
+
};
|
|
80
|
+
const instructionsSchema = zod_1.z
|
|
81
|
+
.union([zod_1.z.string(), instructionItemSchema, zod_1.z.array(instructionItemSchema)])
|
|
82
|
+
.transform(async (v) => {
|
|
83
|
+
if (typeof v === "string")
|
|
84
|
+
return [{ role: "system", content: v, path }];
|
|
85
|
+
if (Array.isArray(v)) {
|
|
86
|
+
return Promise.all(v.map((item) => parseInstructionItem(item)));
|
|
87
|
+
}
|
|
88
|
+
return [await parseInstructionItem(v)];
|
|
89
|
+
});
|
|
67
90
|
return (0, schema_js_1.camelizeSchema)(zod_1.z.discriminatedUnion("type", [
|
|
68
91
|
zod_1.z
|
|
69
92
|
.object({
|
package/lib/cjs/loader/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const mcp_agent_js_1 = require("../agents/mcp-agent.js");
|
|
|
13
13
|
const team_agent_js_1 = require("../agents/team-agent.js");
|
|
14
14
|
const transform_agent_js_1 = require("../agents/transform-agent.js");
|
|
15
15
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
16
|
+
const template_js_1 = require("../prompt/template.js");
|
|
16
17
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
17
18
|
const agent_js_js_1 = require("./agent-js.js");
|
|
18
19
|
const agent_yaml_js_1 = require("./agent-yaml.js");
|
|
@@ -111,8 +112,11 @@ async function parseAgent(path, agent, options, agentOptions) {
|
|
|
111
112
|
};
|
|
112
113
|
let instructions;
|
|
113
114
|
if ("instructions" in agent && agent.instructions) {
|
|
114
|
-
instructions = prompt_builder_js_1.PromptBuilder
|
|
115
|
-
|
|
115
|
+
instructions = new prompt_builder_js_1.PromptBuilder({
|
|
116
|
+
instructions: template_js_1.ChatMessagesTemplate.from((0, template_js_1.parseChatMessages)(agent.instructions.map((i) => ({
|
|
117
|
+
...i,
|
|
118
|
+
options: { workingDir: index_js_1.nodejs.path.dirname(i.path) },
|
|
119
|
+
})))),
|
|
116
120
|
});
|
|
117
121
|
}
|
|
118
122
|
switch (agent.type) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import nunjucks, { type Callback, type LoaderSource } from "nunjucks";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../agents/chat-model.js";
|
|
3
4
|
export interface FormatOptions {
|
|
4
5
|
workingDir?: string;
|
|
@@ -25,19 +26,20 @@ export declare class ChatMessageTemplate {
|
|
|
25
26
|
role: "system" | "user" | "agent" | "tool";
|
|
26
27
|
content?: ChatModelInputMessage["content"];
|
|
27
28
|
name?: string | undefined;
|
|
28
|
-
|
|
29
|
+
options?: FormatOptions | undefined;
|
|
30
|
+
constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined);
|
|
29
31
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage>;
|
|
30
32
|
}
|
|
31
33
|
export declare class SystemMessageTemplate extends ChatMessageTemplate {
|
|
32
|
-
static from(content: string, name?: string): SystemMessageTemplate;
|
|
34
|
+
static from(content: string, name?: string, options?: FormatOptions): SystemMessageTemplate;
|
|
33
35
|
}
|
|
34
36
|
export declare class UserMessageTemplate extends ChatMessageTemplate {
|
|
35
|
-
static from(template: ChatModelInputMessageContent, name?: string): UserMessageTemplate;
|
|
37
|
+
static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions): UserMessageTemplate;
|
|
36
38
|
}
|
|
37
39
|
export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
38
40
|
toolCalls?: ChatModelOutputToolCall[] | undefined;
|
|
39
|
-
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string): AgentMessageTemplate;
|
|
40
|
-
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
|
|
41
|
+
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions): AgentMessageTemplate;
|
|
42
|
+
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions);
|
|
41
43
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
42
44
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
43
45
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -48,8 +50,8 @@ export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
48
50
|
}
|
|
49
51
|
export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
50
52
|
toolCallId: string;
|
|
51
|
-
static from(content: object | string, toolCallId: string, name?: string): ToolMessageTemplate;
|
|
52
|
-
constructor(content: object | string, toolCallId: string, name?: string);
|
|
53
|
+
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions): ToolMessageTemplate;
|
|
54
|
+
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions);
|
|
53
55
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
54
56
|
toolCallId: string;
|
|
55
57
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -71,4 +73,104 @@ export declare class ChatMessagesTemplate {
|
|
|
71
73
|
constructor(messages: ChatMessageTemplate[]);
|
|
72
74
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage[]>;
|
|
73
75
|
}
|
|
74
|
-
|
|
76
|
+
declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
77
|
+
role: z.ZodLiteral<"system">;
|
|
78
|
+
content: z.ZodString;
|
|
79
|
+
name: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
|
81
|
+
role: "system";
|
|
82
|
+
content: string;
|
|
83
|
+
name?: string | undefined;
|
|
84
|
+
}, {
|
|
85
|
+
role: "system";
|
|
86
|
+
content: string;
|
|
87
|
+
name?: string | undefined;
|
|
88
|
+
}>, z.ZodObject<{
|
|
89
|
+
role: z.ZodLiteral<"user">;
|
|
90
|
+
content: z.ZodString;
|
|
91
|
+
name: z.ZodOptional<z.ZodString>;
|
|
92
|
+
}, "strip", z.ZodTypeAny, {
|
|
93
|
+
role: "user";
|
|
94
|
+
content: string;
|
|
95
|
+
name?: string | undefined;
|
|
96
|
+
}, {
|
|
97
|
+
role: "user";
|
|
98
|
+
content: string;
|
|
99
|
+
name?: string | undefined;
|
|
100
|
+
}>, z.ZodObject<{
|
|
101
|
+
role: z.ZodLiteral<"agent">;
|
|
102
|
+
content: z.ZodOptional<z.ZodString>;
|
|
103
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
104
|
+
id: z.ZodString;
|
|
105
|
+
type: z.ZodLiteral<"function">;
|
|
106
|
+
function: z.ZodObject<{
|
|
107
|
+
name: z.ZodString;
|
|
108
|
+
arguments: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
109
|
+
}, "strip", z.ZodTypeAny, {
|
|
110
|
+
name: string;
|
|
111
|
+
arguments: Record<string, unknown>;
|
|
112
|
+
}, {
|
|
113
|
+
name: string;
|
|
114
|
+
arguments: Record<string, unknown>;
|
|
115
|
+
}>;
|
|
116
|
+
}, "strip", z.ZodTypeAny, {
|
|
117
|
+
function: {
|
|
118
|
+
name: string;
|
|
119
|
+
arguments: Record<string, unknown>;
|
|
120
|
+
};
|
|
121
|
+
type: "function";
|
|
122
|
+
id: string;
|
|
123
|
+
}, {
|
|
124
|
+
function: {
|
|
125
|
+
name: string;
|
|
126
|
+
arguments: Record<string, unknown>;
|
|
127
|
+
};
|
|
128
|
+
type: "function";
|
|
129
|
+
id: string;
|
|
130
|
+
}>, "many">>;
|
|
131
|
+
name: z.ZodOptional<z.ZodString>;
|
|
132
|
+
}, "strip", z.ZodTypeAny, {
|
|
133
|
+
role: "agent";
|
|
134
|
+
name?: string | undefined;
|
|
135
|
+
content?: string | undefined;
|
|
136
|
+
toolCalls?: {
|
|
137
|
+
function: {
|
|
138
|
+
name: string;
|
|
139
|
+
arguments: Record<string, unknown>;
|
|
140
|
+
};
|
|
141
|
+
type: "function";
|
|
142
|
+
id: string;
|
|
143
|
+
}[] | undefined;
|
|
144
|
+
}, {
|
|
145
|
+
role: "agent";
|
|
146
|
+
name?: string | undefined;
|
|
147
|
+
content?: string | undefined;
|
|
148
|
+
toolCalls?: {
|
|
149
|
+
function: {
|
|
150
|
+
name: string;
|
|
151
|
+
arguments: Record<string, unknown>;
|
|
152
|
+
};
|
|
153
|
+
type: "function";
|
|
154
|
+
id: string;
|
|
155
|
+
}[] | undefined;
|
|
156
|
+
}>, z.ZodObject<{
|
|
157
|
+
role: z.ZodLiteral<"tool">;
|
|
158
|
+
content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
|
|
159
|
+
toolCallId: z.ZodString;
|
|
160
|
+
name: z.ZodOptional<z.ZodString>;
|
|
161
|
+
}, "strip", z.ZodTypeAny, {
|
|
162
|
+
role: "tool";
|
|
163
|
+
content: string;
|
|
164
|
+
toolCallId: string;
|
|
165
|
+
name?: string | undefined;
|
|
166
|
+
}, {
|
|
167
|
+
role: "tool";
|
|
168
|
+
content: string | Record<string, unknown>;
|
|
169
|
+
toolCallId: string;
|
|
170
|
+
name?: string | undefined;
|
|
171
|
+
}>]>;
|
|
172
|
+
export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
|
|
173
|
+
export declare function parseChatMessages(messages: (z.infer<typeof chatMessageSchema> & {
|
|
174
|
+
options?: FormatOptions;
|
|
175
|
+
})[]): ChatMessageTemplate[];
|
|
176
|
+
export {};
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ChatMessagesTemplate = exports.ToolMessageTemplate = exports.AgentMessageTemplate = exports.UserMessageTemplate = exports.SystemMessageTemplate = exports.ChatMessageTemplate = exports.CustomLoader = exports.PromptTemplate = void 0;
|
|
7
|
+
exports.safeParseChatMessages = safeParseChatMessages;
|
|
7
8
|
exports.parseChatMessages = parseChatMessages;
|
|
8
9
|
const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
|
|
9
10
|
const nunjucks_1 = __importDefault(require("nunjucks"));
|
|
@@ -75,12 +76,15 @@ class ChatMessageTemplate {
|
|
|
75
76
|
role;
|
|
76
77
|
content;
|
|
77
78
|
name;
|
|
78
|
-
|
|
79
|
+
options;
|
|
80
|
+
constructor(role, content, name, options) {
|
|
79
81
|
this.role = role;
|
|
80
82
|
this.content = content;
|
|
81
83
|
this.name = name;
|
|
84
|
+
this.options = options;
|
|
82
85
|
}
|
|
83
86
|
async format(variables, options) {
|
|
87
|
+
options = { ...this.options, ...(0, type_utils_js_1.omitBy)(options ?? {}, (v) => (0, type_utils_js_1.isNil)(v)) };
|
|
84
88
|
let { content } = this;
|
|
85
89
|
if (Array.isArray(content)) {
|
|
86
90
|
content = await Promise.all(content.map(async (i) => {
|
|
@@ -101,24 +105,24 @@ class ChatMessageTemplate {
|
|
|
101
105
|
}
|
|
102
106
|
exports.ChatMessageTemplate = ChatMessageTemplate;
|
|
103
107
|
class SystemMessageTemplate extends ChatMessageTemplate {
|
|
104
|
-
static from(content, name) {
|
|
105
|
-
return new SystemMessageTemplate("system", content, name);
|
|
108
|
+
static from(content, name, options) {
|
|
109
|
+
return new SystemMessageTemplate("system", content, name, options);
|
|
106
110
|
}
|
|
107
111
|
}
|
|
108
112
|
exports.SystemMessageTemplate = SystemMessageTemplate;
|
|
109
113
|
class UserMessageTemplate extends ChatMessageTemplate {
|
|
110
|
-
static from(template, name) {
|
|
111
|
-
return new UserMessageTemplate("user", template, name);
|
|
114
|
+
static from(template, name, options) {
|
|
115
|
+
return new UserMessageTemplate("user", template, name, options);
|
|
112
116
|
}
|
|
113
117
|
}
|
|
114
118
|
exports.UserMessageTemplate = UserMessageTemplate;
|
|
115
119
|
class AgentMessageTemplate extends ChatMessageTemplate {
|
|
116
120
|
toolCalls;
|
|
117
|
-
static from(template, toolCalls, name) {
|
|
118
|
-
return new AgentMessageTemplate(template, toolCalls, name);
|
|
121
|
+
static from(template, toolCalls, name, options) {
|
|
122
|
+
return new AgentMessageTemplate(template, toolCalls, name, options);
|
|
119
123
|
}
|
|
120
|
-
constructor(content, toolCalls, name) {
|
|
121
|
-
super("agent", content, name);
|
|
124
|
+
constructor(content, toolCalls, name, options) {
|
|
125
|
+
super("agent", content, name, options);
|
|
122
126
|
this.toolCalls = toolCalls;
|
|
123
127
|
}
|
|
124
128
|
async format(variables, options) {
|
|
@@ -131,13 +135,13 @@ class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
131
135
|
exports.AgentMessageTemplate = AgentMessageTemplate;
|
|
132
136
|
class ToolMessageTemplate extends ChatMessageTemplate {
|
|
133
137
|
toolCallId;
|
|
134
|
-
static from(content, toolCallId, name) {
|
|
135
|
-
return new ToolMessageTemplate(content, toolCallId, name);
|
|
138
|
+
static from(content, toolCallId, name, options) {
|
|
139
|
+
return new ToolMessageTemplate(content, toolCallId, name, options);
|
|
136
140
|
}
|
|
137
|
-
constructor(content, toolCallId, name) {
|
|
141
|
+
constructor(content, toolCallId, name, options) {
|
|
138
142
|
super("tool", typeof content === "string"
|
|
139
143
|
? content
|
|
140
|
-
: JSON.stringify(content, (_, value) => typeof value === "bigint" ? value.toString() : value), name);
|
|
144
|
+
: JSON.stringify(content, (_, value) => typeof value === "bigint" ? value.toString() : value), name, options);
|
|
141
145
|
this.toolCallId = toolCallId;
|
|
142
146
|
}
|
|
143
147
|
async format(variables, options) {
|
|
@@ -200,20 +204,23 @@ const chatMessageSchema = zod_1.z.union([
|
|
|
200
204
|
toolChatMessageSchema,
|
|
201
205
|
]);
|
|
202
206
|
const chatMessagesSchema = zod_1.z.array(chatMessageSchema);
|
|
203
|
-
function
|
|
207
|
+
function safeParseChatMessages(messages) {
|
|
204
208
|
const result = chatMessagesSchema.safeParse(messages);
|
|
205
209
|
if (!result.success)
|
|
206
210
|
return undefined;
|
|
207
|
-
return result.data
|
|
211
|
+
return parseChatMessages(result.data);
|
|
212
|
+
}
|
|
213
|
+
function parseChatMessages(messages) {
|
|
214
|
+
return messages.map((message) => {
|
|
208
215
|
switch (message.role) {
|
|
209
216
|
case "system":
|
|
210
|
-
return SystemMessageTemplate.from(message.content, message.name);
|
|
217
|
+
return SystemMessageTemplate.from(message.content, message.name, message.options);
|
|
211
218
|
case "user":
|
|
212
|
-
return UserMessageTemplate.from(message.content, message.name);
|
|
219
|
+
return UserMessageTemplate.from(message.content, message.name, message.options);
|
|
213
220
|
case "agent":
|
|
214
|
-
return AgentMessageTemplate.from(message.content, message.toolCalls, message.name);
|
|
221
|
+
return AgentMessageTemplate.from(message.content, message.toolCalls, message.name, message.options);
|
|
215
222
|
case "tool":
|
|
216
|
-
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name);
|
|
223
|
+
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name, message.options);
|
|
217
224
|
}
|
|
218
225
|
});
|
|
219
226
|
}
|
|
@@ -158,6 +158,7 @@ export interface ChatModelInput extends Message {
|
|
|
158
158
|
* - tool: Tool call responses
|
|
159
159
|
*/
|
|
160
160
|
export type Role = "system" | "user" | "agent" | "tool";
|
|
161
|
+
export declare const roleSchema: z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"user">, z.ZodLiteral<"agent">, z.ZodLiteral<"tool">]>;
|
|
161
162
|
/**
|
|
162
163
|
* Structure of input messages
|
|
163
164
|
*
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ZodType, z } from "zod";
|
|
2
2
|
import type { AgentHooks, FunctionAgentFn, TaskRenderMode } from "../agents/agent.js";
|
|
3
3
|
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
4
|
+
import { type Role } from "../agents/chat-model.js";
|
|
4
5
|
import { ProcessMode, type ReflectionMode } from "../agents/team-agent.js";
|
|
5
6
|
import { chatModelSchema, imageModelSchema } from "./schema.js";
|
|
6
7
|
export interface HooksSchema {
|
|
@@ -36,9 +37,10 @@ export interface BaseAgentSchema {
|
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
export type Instructions = {
|
|
40
|
+
role: Exclude<Role, "tool">;
|
|
39
41
|
content: string;
|
|
40
42
|
path: string;
|
|
41
|
-
};
|
|
43
|
+
}[];
|
|
42
44
|
export interface AIAgentSchema extends BaseAgentSchema {
|
|
43
45
|
type: "ai";
|
|
44
46
|
instructions?: Instructions;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import nunjucks, { type Callback, type LoaderSource } from "nunjucks";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../agents/chat-model.js";
|
|
3
4
|
export interface FormatOptions {
|
|
4
5
|
workingDir?: string;
|
|
@@ -25,19 +26,20 @@ export declare class ChatMessageTemplate {
|
|
|
25
26
|
role: "system" | "user" | "agent" | "tool";
|
|
26
27
|
content?: ChatModelInputMessage["content"];
|
|
27
28
|
name?: string | undefined;
|
|
28
|
-
|
|
29
|
+
options?: FormatOptions | undefined;
|
|
30
|
+
constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined);
|
|
29
31
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage>;
|
|
30
32
|
}
|
|
31
33
|
export declare class SystemMessageTemplate extends ChatMessageTemplate {
|
|
32
|
-
static from(content: string, name?: string): SystemMessageTemplate;
|
|
34
|
+
static from(content: string, name?: string, options?: FormatOptions): SystemMessageTemplate;
|
|
33
35
|
}
|
|
34
36
|
export declare class UserMessageTemplate extends ChatMessageTemplate {
|
|
35
|
-
static from(template: ChatModelInputMessageContent, name?: string): UserMessageTemplate;
|
|
37
|
+
static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions): UserMessageTemplate;
|
|
36
38
|
}
|
|
37
39
|
export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
38
40
|
toolCalls?: ChatModelOutputToolCall[] | undefined;
|
|
39
|
-
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string): AgentMessageTemplate;
|
|
40
|
-
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
|
|
41
|
+
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions): AgentMessageTemplate;
|
|
42
|
+
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions);
|
|
41
43
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
42
44
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
43
45
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -48,8 +50,8 @@ export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
48
50
|
}
|
|
49
51
|
export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
50
52
|
toolCallId: string;
|
|
51
|
-
static from(content: object | string, toolCallId: string, name?: string): ToolMessageTemplate;
|
|
52
|
-
constructor(content: object | string, toolCallId: string, name?: string);
|
|
53
|
+
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions): ToolMessageTemplate;
|
|
54
|
+
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions);
|
|
53
55
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
54
56
|
toolCallId: string;
|
|
55
57
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -71,4 +73,104 @@ export declare class ChatMessagesTemplate {
|
|
|
71
73
|
constructor(messages: ChatMessageTemplate[]);
|
|
72
74
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage[]>;
|
|
73
75
|
}
|
|
74
|
-
|
|
76
|
+
declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
77
|
+
role: z.ZodLiteral<"system">;
|
|
78
|
+
content: z.ZodString;
|
|
79
|
+
name: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
|
81
|
+
role: "system";
|
|
82
|
+
content: string;
|
|
83
|
+
name?: string | undefined;
|
|
84
|
+
}, {
|
|
85
|
+
role: "system";
|
|
86
|
+
content: string;
|
|
87
|
+
name?: string | undefined;
|
|
88
|
+
}>, z.ZodObject<{
|
|
89
|
+
role: z.ZodLiteral<"user">;
|
|
90
|
+
content: z.ZodString;
|
|
91
|
+
name: z.ZodOptional<z.ZodString>;
|
|
92
|
+
}, "strip", z.ZodTypeAny, {
|
|
93
|
+
role: "user";
|
|
94
|
+
content: string;
|
|
95
|
+
name?: string | undefined;
|
|
96
|
+
}, {
|
|
97
|
+
role: "user";
|
|
98
|
+
content: string;
|
|
99
|
+
name?: string | undefined;
|
|
100
|
+
}>, z.ZodObject<{
|
|
101
|
+
role: z.ZodLiteral<"agent">;
|
|
102
|
+
content: z.ZodOptional<z.ZodString>;
|
|
103
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
104
|
+
id: z.ZodString;
|
|
105
|
+
type: z.ZodLiteral<"function">;
|
|
106
|
+
function: z.ZodObject<{
|
|
107
|
+
name: z.ZodString;
|
|
108
|
+
arguments: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
109
|
+
}, "strip", z.ZodTypeAny, {
|
|
110
|
+
name: string;
|
|
111
|
+
arguments: Record<string, unknown>;
|
|
112
|
+
}, {
|
|
113
|
+
name: string;
|
|
114
|
+
arguments: Record<string, unknown>;
|
|
115
|
+
}>;
|
|
116
|
+
}, "strip", z.ZodTypeAny, {
|
|
117
|
+
function: {
|
|
118
|
+
name: string;
|
|
119
|
+
arguments: Record<string, unknown>;
|
|
120
|
+
};
|
|
121
|
+
type: "function";
|
|
122
|
+
id: string;
|
|
123
|
+
}, {
|
|
124
|
+
function: {
|
|
125
|
+
name: string;
|
|
126
|
+
arguments: Record<string, unknown>;
|
|
127
|
+
};
|
|
128
|
+
type: "function";
|
|
129
|
+
id: string;
|
|
130
|
+
}>, "many">>;
|
|
131
|
+
name: z.ZodOptional<z.ZodString>;
|
|
132
|
+
}, "strip", z.ZodTypeAny, {
|
|
133
|
+
role: "agent";
|
|
134
|
+
name?: string | undefined;
|
|
135
|
+
content?: string | undefined;
|
|
136
|
+
toolCalls?: {
|
|
137
|
+
function: {
|
|
138
|
+
name: string;
|
|
139
|
+
arguments: Record<string, unknown>;
|
|
140
|
+
};
|
|
141
|
+
type: "function";
|
|
142
|
+
id: string;
|
|
143
|
+
}[] | undefined;
|
|
144
|
+
}, {
|
|
145
|
+
role: "agent";
|
|
146
|
+
name?: string | undefined;
|
|
147
|
+
content?: string | undefined;
|
|
148
|
+
toolCalls?: {
|
|
149
|
+
function: {
|
|
150
|
+
name: string;
|
|
151
|
+
arguments: Record<string, unknown>;
|
|
152
|
+
};
|
|
153
|
+
type: "function";
|
|
154
|
+
id: string;
|
|
155
|
+
}[] | undefined;
|
|
156
|
+
}>, z.ZodObject<{
|
|
157
|
+
role: z.ZodLiteral<"tool">;
|
|
158
|
+
content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
|
|
159
|
+
toolCallId: z.ZodString;
|
|
160
|
+
name: z.ZodOptional<z.ZodString>;
|
|
161
|
+
}, "strip", z.ZodTypeAny, {
|
|
162
|
+
role: "tool";
|
|
163
|
+
content: string;
|
|
164
|
+
toolCallId: string;
|
|
165
|
+
name?: string | undefined;
|
|
166
|
+
}, {
|
|
167
|
+
role: "tool";
|
|
168
|
+
content: string | Record<string, unknown>;
|
|
169
|
+
toolCallId: string;
|
|
170
|
+
name?: string | undefined;
|
|
171
|
+
}>]>;
|
|
172
|
+
export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
|
|
173
|
+
export declare function parseChatMessages(messages: (z.infer<typeof chatMessageSchema> & {
|
|
174
|
+
options?: FormatOptions;
|
|
175
|
+
})[]): ChatMessageTemplate[];
|
|
176
|
+
export {};
|
|
@@ -158,6 +158,7 @@ export interface ChatModelInput extends Message {
|
|
|
158
158
|
* - tool: Tool call responses
|
|
159
159
|
*/
|
|
160
160
|
export type Role = "system" | "user" | "agent" | "tool";
|
|
161
|
+
export declare const roleSchema: z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"user">, z.ZodLiteral<"agent">, z.ZodLiteral<"tool">]>;
|
|
161
162
|
/**
|
|
162
163
|
* Structure of input messages
|
|
163
164
|
*
|
|
@@ -219,6 +219,12 @@ export class ChatModel extends Model {
|
|
|
219
219
|
return super.processAgentOutput(input, output, options);
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
|
+
export const roleSchema = z.union([
|
|
223
|
+
z.literal("system"),
|
|
224
|
+
z.literal("user"),
|
|
225
|
+
z.literal("agent"),
|
|
226
|
+
z.literal("tool"),
|
|
227
|
+
]);
|
|
222
228
|
export const textContentSchema = z.object({
|
|
223
229
|
type: z.literal("text"),
|
|
224
230
|
text: z.string(),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ZodType, z } from "zod";
|
|
2
2
|
import type { AgentHooks, FunctionAgentFn, TaskRenderMode } from "../agents/agent.js";
|
|
3
3
|
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
4
|
+
import { type Role } from "../agents/chat-model.js";
|
|
4
5
|
import { ProcessMode, type ReflectionMode } from "../agents/team-agent.js";
|
|
5
6
|
import { chatModelSchema, imageModelSchema } from "./schema.js";
|
|
6
7
|
export interface HooksSchema {
|
|
@@ -36,9 +37,10 @@ export interface BaseAgentSchema {
|
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
export type Instructions = {
|
|
40
|
+
role: Exclude<Role, "tool">;
|
|
39
41
|
content: string;
|
|
40
42
|
path: string;
|
|
41
|
-
};
|
|
43
|
+
}[];
|
|
42
44
|
export interface AIAgentSchema extends BaseAgentSchema {
|
|
43
45
|
type: "ai";
|
|
44
46
|
instructions?: Instructions;
|
|
@@ -3,6 +3,7 @@ import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
|
3
3
|
import { parse } from "yaml";
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
6
|
+
import { roleSchema } from "../agents/chat-model.js";
|
|
6
7
|
import { ProcessMode } from "../agents/team-agent.js";
|
|
7
8
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
8
9
|
import { camelizeSchema, chatModelSchema, defaultInputSchema, imageModelSchema, inputOutputSchema, optionalize, } from "./schema.js";
|
|
@@ -48,18 +49,40 @@ export async function parseAgentFile(path, data) {
|
|
|
48
49
|
})),
|
|
49
50
|
])),
|
|
50
51
|
});
|
|
51
|
-
const
|
|
52
|
-
.union([
|
|
53
|
-
z.string(),
|
|
52
|
+
const instructionItemSchema = z.union([
|
|
54
53
|
z.object({
|
|
54
|
+
role: roleSchema.default("system"),
|
|
55
55
|
url: z.string(),
|
|
56
56
|
}),
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
z.object({
|
|
58
|
+
role: roleSchema.default("system"),
|
|
59
|
+
content: z.string(),
|
|
60
|
+
}),
|
|
61
|
+
]);
|
|
62
|
+
const parseInstructionItem = async ({ role, ...v }) => {
|
|
63
|
+
if (role === "tool")
|
|
64
|
+
throw new Error(`'tool' role is not allowed in instruction item in agent file ${path}`);
|
|
65
|
+
if ("content" in v && typeof v.content === "string") {
|
|
66
|
+
return { role, content: v.content, path };
|
|
67
|
+
}
|
|
68
|
+
if ("url" in v && typeof v.url === "string") {
|
|
69
|
+
const url = nodejs.path.isAbsolute(v.url)
|
|
70
|
+
? v.url
|
|
71
|
+
: nodejs.path.join(nodejs.path.dirname(path), v.url);
|
|
72
|
+
return nodejs.fs.readFile(url, "utf8").then((content) => ({ role, content, path: url }));
|
|
73
|
+
}
|
|
74
|
+
throw new Error(`Invalid instruction item in agent file ${path}. Expected 'content' or 'url' property`);
|
|
75
|
+
};
|
|
76
|
+
const instructionsSchema = z
|
|
77
|
+
.union([z.string(), instructionItemSchema, z.array(instructionItemSchema)])
|
|
78
|
+
.transform(async (v) => {
|
|
79
|
+
if (typeof v === "string")
|
|
80
|
+
return [{ role: "system", content: v, path }];
|
|
81
|
+
if (Array.isArray(v)) {
|
|
82
|
+
return Promise.all(v.map((item) => parseInstructionItem(item)));
|
|
83
|
+
}
|
|
84
|
+
return [await parseInstructionItem(v)];
|
|
85
|
+
});
|
|
63
86
|
return camelizeSchema(z.discriminatedUnion("type", [
|
|
64
87
|
z
|
|
65
88
|
.object({
|
package/lib/esm/loader/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { MCPAgent } from "../agents/mcp-agent.js";
|
|
|
8
8
|
import { TeamAgent } from "../agents/team-agent.js";
|
|
9
9
|
import { TransformAgent } from "../agents/transform-agent.js";
|
|
10
10
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
11
|
+
import { ChatMessagesTemplate, parseChatMessages } from "../prompt/template.js";
|
|
11
12
|
import { flat, isNonNullable, tryOrThrow } from "../utils/type-utils.js";
|
|
12
13
|
import { loadAgentFromJsFile } from "./agent-js.js";
|
|
13
14
|
import { loadAgentFromYamlFile } from "./agent-yaml.js";
|
|
@@ -106,8 +107,11 @@ async function parseAgent(path, agent, options, agentOptions) {
|
|
|
106
107
|
};
|
|
107
108
|
let instructions;
|
|
108
109
|
if ("instructions" in agent && agent.instructions) {
|
|
109
|
-
instructions = PromptBuilder
|
|
110
|
-
|
|
110
|
+
instructions = new PromptBuilder({
|
|
111
|
+
instructions: ChatMessagesTemplate.from(parseChatMessages(agent.instructions.map((i) => ({
|
|
112
|
+
...i,
|
|
113
|
+
options: { workingDir: nodejs.path.dirname(i.path) },
|
|
114
|
+
})))),
|
|
111
115
|
});
|
|
112
116
|
}
|
|
113
117
|
switch (agent.type) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import nunjucks, { type Callback, type LoaderSource } from "nunjucks";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../agents/chat-model.js";
|
|
3
4
|
export interface FormatOptions {
|
|
4
5
|
workingDir?: string;
|
|
@@ -25,19 +26,20 @@ export declare class ChatMessageTemplate {
|
|
|
25
26
|
role: "system" | "user" | "agent" | "tool";
|
|
26
27
|
content?: ChatModelInputMessage["content"];
|
|
27
28
|
name?: string | undefined;
|
|
28
|
-
|
|
29
|
+
options?: FormatOptions | undefined;
|
|
30
|
+
constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined);
|
|
29
31
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage>;
|
|
30
32
|
}
|
|
31
33
|
export declare class SystemMessageTemplate extends ChatMessageTemplate {
|
|
32
|
-
static from(content: string, name?: string): SystemMessageTemplate;
|
|
34
|
+
static from(content: string, name?: string, options?: FormatOptions): SystemMessageTemplate;
|
|
33
35
|
}
|
|
34
36
|
export declare class UserMessageTemplate extends ChatMessageTemplate {
|
|
35
|
-
static from(template: ChatModelInputMessageContent, name?: string): UserMessageTemplate;
|
|
37
|
+
static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions): UserMessageTemplate;
|
|
36
38
|
}
|
|
37
39
|
export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
38
40
|
toolCalls?: ChatModelOutputToolCall[] | undefined;
|
|
39
|
-
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string): AgentMessageTemplate;
|
|
40
|
-
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
|
|
41
|
+
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions): AgentMessageTemplate;
|
|
42
|
+
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions);
|
|
41
43
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
42
44
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
43
45
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -48,8 +50,8 @@ export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
48
50
|
}
|
|
49
51
|
export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
50
52
|
toolCallId: string;
|
|
51
|
-
static from(content: object | string, toolCallId: string, name?: string): ToolMessageTemplate;
|
|
52
|
-
constructor(content: object | string, toolCallId: string, name?: string);
|
|
53
|
+
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions): ToolMessageTemplate;
|
|
54
|
+
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions);
|
|
53
55
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<{
|
|
54
56
|
toolCallId: string;
|
|
55
57
|
role: import("../agents/chat-model.js").Role;
|
|
@@ -71,4 +73,104 @@ export declare class ChatMessagesTemplate {
|
|
|
71
73
|
constructor(messages: ChatMessageTemplate[]);
|
|
72
74
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage[]>;
|
|
73
75
|
}
|
|
74
|
-
|
|
76
|
+
declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
77
|
+
role: z.ZodLiteral<"system">;
|
|
78
|
+
content: z.ZodString;
|
|
79
|
+
name: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
|
81
|
+
role: "system";
|
|
82
|
+
content: string;
|
|
83
|
+
name?: string | undefined;
|
|
84
|
+
}, {
|
|
85
|
+
role: "system";
|
|
86
|
+
content: string;
|
|
87
|
+
name?: string | undefined;
|
|
88
|
+
}>, z.ZodObject<{
|
|
89
|
+
role: z.ZodLiteral<"user">;
|
|
90
|
+
content: z.ZodString;
|
|
91
|
+
name: z.ZodOptional<z.ZodString>;
|
|
92
|
+
}, "strip", z.ZodTypeAny, {
|
|
93
|
+
role: "user";
|
|
94
|
+
content: string;
|
|
95
|
+
name?: string | undefined;
|
|
96
|
+
}, {
|
|
97
|
+
role: "user";
|
|
98
|
+
content: string;
|
|
99
|
+
name?: string | undefined;
|
|
100
|
+
}>, z.ZodObject<{
|
|
101
|
+
role: z.ZodLiteral<"agent">;
|
|
102
|
+
content: z.ZodOptional<z.ZodString>;
|
|
103
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
104
|
+
id: z.ZodString;
|
|
105
|
+
type: z.ZodLiteral<"function">;
|
|
106
|
+
function: z.ZodObject<{
|
|
107
|
+
name: z.ZodString;
|
|
108
|
+
arguments: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
109
|
+
}, "strip", z.ZodTypeAny, {
|
|
110
|
+
name: string;
|
|
111
|
+
arguments: Record<string, unknown>;
|
|
112
|
+
}, {
|
|
113
|
+
name: string;
|
|
114
|
+
arguments: Record<string, unknown>;
|
|
115
|
+
}>;
|
|
116
|
+
}, "strip", z.ZodTypeAny, {
|
|
117
|
+
function: {
|
|
118
|
+
name: string;
|
|
119
|
+
arguments: Record<string, unknown>;
|
|
120
|
+
};
|
|
121
|
+
type: "function";
|
|
122
|
+
id: string;
|
|
123
|
+
}, {
|
|
124
|
+
function: {
|
|
125
|
+
name: string;
|
|
126
|
+
arguments: Record<string, unknown>;
|
|
127
|
+
};
|
|
128
|
+
type: "function";
|
|
129
|
+
id: string;
|
|
130
|
+
}>, "many">>;
|
|
131
|
+
name: z.ZodOptional<z.ZodString>;
|
|
132
|
+
}, "strip", z.ZodTypeAny, {
|
|
133
|
+
role: "agent";
|
|
134
|
+
name?: string | undefined;
|
|
135
|
+
content?: string | undefined;
|
|
136
|
+
toolCalls?: {
|
|
137
|
+
function: {
|
|
138
|
+
name: string;
|
|
139
|
+
arguments: Record<string, unknown>;
|
|
140
|
+
};
|
|
141
|
+
type: "function";
|
|
142
|
+
id: string;
|
|
143
|
+
}[] | undefined;
|
|
144
|
+
}, {
|
|
145
|
+
role: "agent";
|
|
146
|
+
name?: string | undefined;
|
|
147
|
+
content?: string | undefined;
|
|
148
|
+
toolCalls?: {
|
|
149
|
+
function: {
|
|
150
|
+
name: string;
|
|
151
|
+
arguments: Record<string, unknown>;
|
|
152
|
+
};
|
|
153
|
+
type: "function";
|
|
154
|
+
id: string;
|
|
155
|
+
}[] | undefined;
|
|
156
|
+
}>, z.ZodObject<{
|
|
157
|
+
role: z.ZodLiteral<"tool">;
|
|
158
|
+
content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
|
|
159
|
+
toolCallId: z.ZodString;
|
|
160
|
+
name: z.ZodOptional<z.ZodString>;
|
|
161
|
+
}, "strip", z.ZodTypeAny, {
|
|
162
|
+
role: "tool";
|
|
163
|
+
content: string;
|
|
164
|
+
toolCallId: string;
|
|
165
|
+
name?: string | undefined;
|
|
166
|
+
}, {
|
|
167
|
+
role: "tool";
|
|
168
|
+
content: string | Record<string, unknown>;
|
|
169
|
+
toolCallId: string;
|
|
170
|
+
name?: string | undefined;
|
|
171
|
+
}>]>;
|
|
172
|
+
export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
|
|
173
|
+
export declare function parseChatMessages(messages: (z.infer<typeof chatMessageSchema> & {
|
|
174
|
+
options?: FormatOptions;
|
|
175
|
+
})[]): ChatMessageTemplate[];
|
|
176
|
+
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
2
2
|
import nunjucks from "nunjucks";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import { isNil } from "../utils/type-utils.js";
|
|
4
|
+
import { isNil, omitBy } from "../utils/type-utils.js";
|
|
5
5
|
nunjucks.runtime.suppressValue = (v) => {
|
|
6
6
|
if (isNil(v))
|
|
7
7
|
return "";
|
|
@@ -66,12 +66,15 @@ export class ChatMessageTemplate {
|
|
|
66
66
|
role;
|
|
67
67
|
content;
|
|
68
68
|
name;
|
|
69
|
-
|
|
69
|
+
options;
|
|
70
|
+
constructor(role, content, name, options) {
|
|
70
71
|
this.role = role;
|
|
71
72
|
this.content = content;
|
|
72
73
|
this.name = name;
|
|
74
|
+
this.options = options;
|
|
73
75
|
}
|
|
74
76
|
async format(variables, options) {
|
|
77
|
+
options = { ...this.options, ...omitBy(options ?? {}, (v) => isNil(v)) };
|
|
75
78
|
let { content } = this;
|
|
76
79
|
if (Array.isArray(content)) {
|
|
77
80
|
content = await Promise.all(content.map(async (i) => {
|
|
@@ -91,22 +94,22 @@ export class ChatMessageTemplate {
|
|
|
91
94
|
}
|
|
92
95
|
}
|
|
93
96
|
export class SystemMessageTemplate extends ChatMessageTemplate {
|
|
94
|
-
static from(content, name) {
|
|
95
|
-
return new SystemMessageTemplate("system", content, name);
|
|
97
|
+
static from(content, name, options) {
|
|
98
|
+
return new SystemMessageTemplate("system", content, name, options);
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
export class UserMessageTemplate extends ChatMessageTemplate {
|
|
99
|
-
static from(template, name) {
|
|
100
|
-
return new UserMessageTemplate("user", template, name);
|
|
102
|
+
static from(template, name, options) {
|
|
103
|
+
return new UserMessageTemplate("user", template, name, options);
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
export class AgentMessageTemplate extends ChatMessageTemplate {
|
|
104
107
|
toolCalls;
|
|
105
|
-
static from(template, toolCalls, name) {
|
|
106
|
-
return new AgentMessageTemplate(template, toolCalls, name);
|
|
108
|
+
static from(template, toolCalls, name, options) {
|
|
109
|
+
return new AgentMessageTemplate(template, toolCalls, name, options);
|
|
107
110
|
}
|
|
108
|
-
constructor(content, toolCalls, name) {
|
|
109
|
-
super("agent", content, name);
|
|
111
|
+
constructor(content, toolCalls, name, options) {
|
|
112
|
+
super("agent", content, name, options);
|
|
110
113
|
this.toolCalls = toolCalls;
|
|
111
114
|
}
|
|
112
115
|
async format(variables, options) {
|
|
@@ -118,13 +121,13 @@ export class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
118
121
|
}
|
|
119
122
|
export class ToolMessageTemplate extends ChatMessageTemplate {
|
|
120
123
|
toolCallId;
|
|
121
|
-
static from(content, toolCallId, name) {
|
|
122
|
-
return new ToolMessageTemplate(content, toolCallId, name);
|
|
124
|
+
static from(content, toolCallId, name, options) {
|
|
125
|
+
return new ToolMessageTemplate(content, toolCallId, name, options);
|
|
123
126
|
}
|
|
124
|
-
constructor(content, toolCallId, name) {
|
|
127
|
+
constructor(content, toolCallId, name, options) {
|
|
125
128
|
super("tool", typeof content === "string"
|
|
126
129
|
? content
|
|
127
|
-
: JSON.stringify(content, (_, value) => typeof value === "bigint" ? value.toString() : value), name);
|
|
130
|
+
: JSON.stringify(content, (_, value) => typeof value === "bigint" ? value.toString() : value), name, options);
|
|
128
131
|
this.toolCallId = toolCallId;
|
|
129
132
|
}
|
|
130
133
|
async format(variables, options) {
|
|
@@ -185,20 +188,23 @@ const chatMessageSchema = z.union([
|
|
|
185
188
|
toolChatMessageSchema,
|
|
186
189
|
]);
|
|
187
190
|
const chatMessagesSchema = z.array(chatMessageSchema);
|
|
188
|
-
export function
|
|
191
|
+
export function safeParseChatMessages(messages) {
|
|
189
192
|
const result = chatMessagesSchema.safeParse(messages);
|
|
190
193
|
if (!result.success)
|
|
191
194
|
return undefined;
|
|
192
|
-
return result.data
|
|
195
|
+
return parseChatMessages(result.data);
|
|
196
|
+
}
|
|
197
|
+
export function parseChatMessages(messages) {
|
|
198
|
+
return messages.map((message) => {
|
|
193
199
|
switch (message.role) {
|
|
194
200
|
case "system":
|
|
195
|
-
return SystemMessageTemplate.from(message.content, message.name);
|
|
201
|
+
return SystemMessageTemplate.from(message.content, message.name, message.options);
|
|
196
202
|
case "user":
|
|
197
|
-
return UserMessageTemplate.from(message.content, message.name);
|
|
203
|
+
return UserMessageTemplate.from(message.content, message.name, message.options);
|
|
198
204
|
case "agent":
|
|
199
|
-
return AgentMessageTemplate.from(message.content, message.toolCalls, message.name);
|
|
205
|
+
return AgentMessageTemplate.from(message.content, message.toolCalls, message.name, message.options);
|
|
200
206
|
case "tool":
|
|
201
|
-
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name);
|
|
207
|
+
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name, message.options);
|
|
202
208
|
}
|
|
203
209
|
});
|
|
204
210
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/core",
|
|
3
|
-
"version": "1.61.0-beta.
|
|
3
|
+
"version": "1.61.0-beta.4",
|
|
4
4
|
"description": "The functional core of agentic AI",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -92,7 +92,7 @@
|
|
|
92
92
|
"yaml": "^2.8.1",
|
|
93
93
|
"zod": "^3.25.67",
|
|
94
94
|
"zod-to-json-schema": "^3.24.6",
|
|
95
|
-
"@aigne/observability-api": "^0.10.
|
|
95
|
+
"@aigne/observability-api": "^0.10.5-beta",
|
|
96
96
|
"@aigne/platform-helpers": "^0.6.3-beta"
|
|
97
97
|
},
|
|
98
98
|
"devDependencies": {
|