@aigne/core 1.72.0-beta.2 → 1.72.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 +34 -0
- package/lib/cjs/agents/agent.d.ts +31 -1
- package/lib/cjs/agents/agent.js +13 -0
- package/lib/cjs/agents/ai-agent.d.ts +7 -0
- package/lib/cjs/agents/ai-agent.js +85 -3
- package/lib/cjs/agents/chat-model.d.ts +153 -0
- package/lib/cjs/agents/chat-model.js +35 -1
- package/lib/cjs/agents/image-agent.d.ts +17 -1
- package/lib/cjs/agents/image-agent.js +16 -0
- package/lib/cjs/agents/image-model.d.ts +14 -4
- package/lib/cjs/agents/mcp-agent.d.ts +17 -0
- package/lib/cjs/agents/mcp-agent.js +18 -0
- package/lib/cjs/agents/team-agent.d.ts +55 -0
- package/lib/cjs/agents/team-agent.js +31 -0
- package/lib/cjs/agents/transform-agent.d.ts +12 -0
- package/lib/cjs/agents/transform-agent.js +13 -0
- package/lib/cjs/agents/video-model.d.ts +14 -4
- package/lib/cjs/aigne/usage.d.ts +4 -0
- package/lib/cjs/aigne/usage.js +6 -0
- package/lib/cjs/loader/agent-yaml.d.ts +5 -63
- package/lib/cjs/loader/agent-yaml.js +4 -129
- package/lib/cjs/loader/agents.d.ts +4 -0
- package/lib/cjs/loader/agents.js +17 -0
- package/lib/cjs/loader/index.d.ts +18 -14
- package/lib/cjs/loader/index.js +20 -81
- package/lib/cjs/loader/schema.d.ts +21 -6
- package/lib/cjs/loader/schema.js +60 -1
- package/lib/cjs/prompt/prompt-builder.js +0 -1
- package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.js +63 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.js +61 -0
- package/lib/cjs/prompt/skills/afs/delete.js +15 -3
- package/lib/cjs/prompt/skills/afs/edit.js +30 -7
- package/lib/cjs/prompt/skills/afs/exec.js +17 -6
- package/lib/cjs/prompt/skills/afs/index.js +4 -1
- package/lib/cjs/prompt/skills/afs/list.js +26 -10
- package/lib/cjs/prompt/skills/afs/read.js +14 -3
- package/lib/cjs/prompt/skills/afs/rename.js +18 -4
- package/lib/cjs/prompt/skills/afs/search.js +21 -5
- package/lib/cjs/prompt/skills/afs/write.js +18 -4
- package/lib/cjs/prompt/template.d.ts +92 -17
- package/lib/cjs/prompt/template.js +46 -17
- package/lib/dts/agents/agent.d.ts +31 -1
- package/lib/dts/agents/ai-agent.d.ts +7 -0
- package/lib/dts/agents/chat-model.d.ts +153 -0
- package/lib/dts/agents/image-agent.d.ts +17 -1
- package/lib/dts/agents/image-model.d.ts +14 -4
- package/lib/dts/agents/mcp-agent.d.ts +17 -0
- package/lib/dts/agents/team-agent.d.ts +55 -0
- package/lib/dts/agents/transform-agent.d.ts +12 -0
- package/lib/dts/agents/video-model.d.ts +14 -4
- package/lib/dts/aigne/context.d.ts +2 -2
- package/lib/dts/aigne/usage.d.ts +4 -0
- package/lib/dts/loader/agent-yaml.d.ts +5 -63
- package/lib/dts/loader/agents.d.ts +4 -0
- package/lib/dts/loader/index.d.ts +18 -14
- package/lib/dts/loader/schema.d.ts +21 -6
- package/lib/dts/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
- package/lib/dts/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
- package/lib/dts/prompt/template.d.ts +92 -17
- package/lib/esm/agents/agent.d.ts +31 -1
- package/lib/esm/agents/agent.js +13 -0
- package/lib/esm/agents/ai-agent.d.ts +7 -0
- package/lib/esm/agents/ai-agent.js +85 -3
- package/lib/esm/agents/chat-model.d.ts +153 -0
- package/lib/esm/agents/chat-model.js +34 -0
- package/lib/esm/agents/image-agent.d.ts +17 -1
- package/lib/esm/agents/image-agent.js +16 -0
- package/lib/esm/agents/image-model.d.ts +14 -4
- package/lib/esm/agents/mcp-agent.d.ts +17 -0
- package/lib/esm/agents/mcp-agent.js +18 -0
- package/lib/esm/agents/team-agent.d.ts +55 -0
- package/lib/esm/agents/team-agent.js +31 -0
- package/lib/esm/agents/transform-agent.d.ts +12 -0
- package/lib/esm/agents/transform-agent.js +13 -0
- package/lib/esm/agents/video-model.d.ts +14 -4
- package/lib/esm/aigne/context.d.ts +2 -2
- package/lib/esm/aigne/usage.d.ts +4 -0
- package/lib/esm/aigne/usage.js +6 -0
- package/lib/esm/loader/agent-yaml.d.ts +5 -63
- package/lib/esm/loader/agent-yaml.js +4 -128
- package/lib/esm/loader/agents.d.ts +4 -0
- package/lib/esm/loader/agents.js +14 -0
- package/lib/esm/loader/index.d.ts +18 -14
- package/lib/esm/loader/index.js +21 -81
- package/lib/esm/loader/schema.d.ts +21 -6
- package/lib/esm/loader/schema.js +57 -0
- package/lib/esm/prompt/prompt-builder.js +1 -2
- package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
- package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.js +59 -0
- package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
- package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.js +53 -0
- package/lib/esm/prompt/skills/afs/delete.js +15 -3
- package/lib/esm/prompt/skills/afs/edit.js +30 -7
- package/lib/esm/prompt/skills/afs/exec.js +17 -6
- package/lib/esm/prompt/skills/afs/index.js +4 -1
- package/lib/esm/prompt/skills/afs/list.js +26 -10
- package/lib/esm/prompt/skills/afs/read.js +14 -3
- package/lib/esm/prompt/skills/afs/rename.js +18 -4
- package/lib/esm/prompt/skills/afs/search.js +21 -5
- package/lib/esm/prompt/skills/afs/write.js +18 -4
- package/lib/esm/prompt/template.d.ts +92 -17
- package/lib/esm/prompt/template.js +46 -17
- package/package.json +5 -4
|
@@ -5,9 +5,8 @@ import type { ChatModel } from "../agents/chat-model.js";
|
|
|
5
5
|
import type { ImageModel } from "../agents/image-model.js";
|
|
6
6
|
import type { AIGNEOptions } from "../aigne/aigne.js";
|
|
7
7
|
import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
|
|
8
|
-
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
9
8
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
10
|
-
import {
|
|
9
|
+
import { loadAgentFromYamlFile, type NestAgentSchema } from "./agent-yaml.js";
|
|
11
10
|
export interface LoadOptions {
|
|
12
11
|
memories?: {
|
|
13
12
|
new (parameters?: MemoryAgentOptions): MemoryAgent;
|
|
@@ -19,7 +18,10 @@ export interface LoadOptions {
|
|
|
19
18
|
availableModules?: {
|
|
20
19
|
module: string;
|
|
21
20
|
alias?: string[];
|
|
22
|
-
|
|
21
|
+
load: (options: {
|
|
22
|
+
filepath: string;
|
|
23
|
+
parsed?: object;
|
|
24
|
+
}) => PromiseOrValue<AFSModule>;
|
|
23
25
|
}[];
|
|
24
26
|
};
|
|
25
27
|
aigne?: z.infer<typeof aigneFileSchema>;
|
|
@@ -27,6 +29,9 @@ export interface LoadOptions {
|
|
|
27
29
|
parent?: string;
|
|
28
30
|
}) => Promise<any>;
|
|
29
31
|
}
|
|
32
|
+
export interface AgentLoadOptions extends LoadOptions {
|
|
33
|
+
loadNestAgent: (path: string, agent: NestAgentSchema, options: LoadOptions, agentOptions?: AgentOptions<any, any> & Record<string, unknown>) => Promise<Agent>;
|
|
34
|
+
}
|
|
30
35
|
export declare function load(path: string, options?: LoadOptions): Promise<AIGNEOptions>;
|
|
31
36
|
export declare function loadAgent(path: string, options: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
|
|
32
37
|
export declare function loadNestAgent(path: string, agent: NestAgentSchema, options: LoadOptions, agentOptions?: AgentOptions<any, any> & Record<string, unknown>): Promise<Agent>;
|
|
@@ -67,9 +72,9 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
67
72
|
} | undefined, z.ZodTypeDef, number | {
|
|
68
73
|
$get: string;
|
|
69
74
|
} | undefined>;
|
|
70
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
75
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
71
76
|
$get: string;
|
|
72
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
77
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
73
78
|
$get: string;
|
|
74
79
|
} | undefined>;
|
|
75
80
|
}, z.ZodTypeAny, "passthrough"> | undefined, z.ZodTypeDef, z.objectInputType<{
|
|
@@ -98,9 +103,9 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
98
103
|
} | undefined, z.ZodTypeDef, number | {
|
|
99
104
|
$get: string;
|
|
100
105
|
} | undefined>;
|
|
101
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
106
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
102
107
|
$get: string;
|
|
103
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
108
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
104
109
|
$get: string;
|
|
105
110
|
} | undefined>;
|
|
106
111
|
}, z.ZodTypeAny, "passthrough"> | undefined>;
|
|
@@ -132,6 +137,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
132
137
|
chat?: string | undefined;
|
|
133
138
|
} | undefined>;
|
|
134
139
|
}, "strip", z.ZodTypeAny, {
|
|
140
|
+
name?: string | undefined;
|
|
135
141
|
model?: z.objectInputType<{
|
|
136
142
|
model: ZodType<string | {
|
|
137
143
|
$get: string;
|
|
@@ -158,13 +164,12 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
158
164
|
} | undefined, z.ZodTypeDef, number | {
|
|
159
165
|
$get: string;
|
|
160
166
|
} | undefined>;
|
|
161
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
167
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
162
168
|
$get: string;
|
|
163
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
169
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
164
170
|
$get: string;
|
|
165
171
|
} | undefined>;
|
|
166
172
|
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
167
|
-
name?: string | undefined;
|
|
168
173
|
description?: string | undefined;
|
|
169
174
|
imageModel?: z.objectInputType<{
|
|
170
175
|
model: ZodType<string | {
|
|
@@ -183,6 +188,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
183
188
|
chat?: string | undefined;
|
|
184
189
|
} | undefined;
|
|
185
190
|
}, {
|
|
191
|
+
name?: string | undefined;
|
|
186
192
|
model?: z.objectInputType<{
|
|
187
193
|
model: ZodType<string | {
|
|
188
194
|
$get: string;
|
|
@@ -209,13 +215,12 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
209
215
|
} | undefined, z.ZodTypeDef, number | {
|
|
210
216
|
$get: string;
|
|
211
217
|
} | undefined>;
|
|
212
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
218
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
213
219
|
$get: string;
|
|
214
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
220
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
215
221
|
$get: string;
|
|
216
222
|
} | undefined>;
|
|
217
223
|
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
218
|
-
name?: string | undefined;
|
|
219
224
|
description?: string | undefined;
|
|
220
225
|
imageModel?: z.objectInputType<{
|
|
221
226
|
model: ZodType<string | {
|
|
@@ -239,5 +244,4 @@ export declare function loadAIGNEFile(path: string): Promise<{
|
|
|
239
244
|
rootDir: string;
|
|
240
245
|
}>;
|
|
241
246
|
export declare function findAIGNEFile(path: string): Promise<string>;
|
|
242
|
-
export declare function instructionsToPromptBuilder(instructions: Instructions): PromptBuilder;
|
|
243
247
|
export {};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type ZodType, z } from "zod";
|
|
2
|
+
import { type Role } from "../agents/chat-model.js";
|
|
3
|
+
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
2
4
|
export declare const inputOutputSchema: ({ path }: {
|
|
3
5
|
path: string;
|
|
4
6
|
}) => z.ZodUnion<[ZodType<any, z.ZodTypeDef, any>, z.ZodEffects<z.ZodObject<{
|
|
@@ -55,9 +57,9 @@ declare const chatModelObjectSchema: z.ZodObject<{
|
|
|
55
57
|
} | undefined, z.ZodTypeDef, number | {
|
|
56
58
|
$get: string;
|
|
57
59
|
} | undefined>;
|
|
58
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
60
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
59
61
|
$get: string;
|
|
60
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
62
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
61
63
|
$get: string;
|
|
62
64
|
} | undefined>;
|
|
63
65
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
@@ -86,9 +88,9 @@ declare const chatModelObjectSchema: z.ZodObject<{
|
|
|
86
88
|
} | undefined, z.ZodTypeDef, number | {
|
|
87
89
|
$get: string;
|
|
88
90
|
} | undefined>;
|
|
89
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
91
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
90
92
|
$get: string;
|
|
91
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
93
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
92
94
|
$get: string;
|
|
93
95
|
} | undefined>;
|
|
94
96
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
@@ -117,9 +119,9 @@ declare const chatModelObjectSchema: z.ZodObject<{
|
|
|
117
119
|
} | undefined, z.ZodTypeDef, number | {
|
|
118
120
|
$get: string;
|
|
119
121
|
} | undefined>;
|
|
120
|
-
thinkingEffort: ZodType<number | "high" | "
|
|
122
|
+
thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
|
|
121
123
|
$get: string;
|
|
122
|
-
} | undefined, z.ZodTypeDef, number | "high" | "
|
|
124
|
+
} | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
|
|
123
125
|
$get: string;
|
|
124
126
|
} | undefined>;
|
|
125
127
|
}, z.ZodTypeAny, "passthrough">>;
|
|
@@ -149,4 +151,17 @@ export declare function camelizeSchema<T extends ZodType>(schema: T, { shallow }
|
|
|
149
151
|
shallow?: boolean;
|
|
150
152
|
}): T;
|
|
151
153
|
export declare function preprocessSchema<T extends ZodType>(fn: (data: unknown) => unknown, schema: T): T;
|
|
154
|
+
export type Instructions = {
|
|
155
|
+
role: Exclude<Role, "tool">;
|
|
156
|
+
content: string;
|
|
157
|
+
path: string;
|
|
158
|
+
cacheControl?: {
|
|
159
|
+
type: "ephemeral";
|
|
160
|
+
ttl?: "5m" | "1h";
|
|
161
|
+
};
|
|
162
|
+
}[];
|
|
163
|
+
export declare const getInstructionsSchema: ({ filepath }: {
|
|
164
|
+
filepath: string;
|
|
165
|
+
}) => ZodType<Instructions>;
|
|
166
|
+
export declare function instructionsToPromptBuilder(instructions: Instructions | string): PromptBuilder;
|
|
152
167
|
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Agent, type AgentOptions, type Message } from "../../../../agents/agent.js";
|
|
2
|
+
import type { Skill } from "./skill-loader.js";
|
|
3
|
+
export interface SkillToolInput extends Message {
|
|
4
|
+
skill: string;
|
|
5
|
+
args?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SkillToolOutput extends Message {
|
|
8
|
+
result: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SkillToolOptions extends AgentOptions<SkillToolInput, SkillToolOutput> {
|
|
11
|
+
agentSkills: Skill[];
|
|
12
|
+
}
|
|
13
|
+
export declare class AgentSkill extends Agent<SkillToolInput, SkillToolOutput> {
|
|
14
|
+
constructor(options: SkillToolOptions);
|
|
15
|
+
private agentSkills;
|
|
16
|
+
process(input: SkillToolInput): Promise<SkillToolOutput>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AFS } from "@aigne/afs";
|
|
2
|
+
import { AgentSkill } from "./agent-skill.js";
|
|
3
|
+
export interface Skill {
|
|
4
|
+
path: string;
|
|
5
|
+
name: string;
|
|
6
|
+
description: string;
|
|
7
|
+
content: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function loadSkill(path: string): Promise<Skill>;
|
|
10
|
+
export declare function loadSkills(paths: string[]): Promise<Skill[]>;
|
|
11
|
+
export declare function loadAgentSkillFromAFS({ afs, }: {
|
|
12
|
+
afs: AFS;
|
|
13
|
+
}): Promise<AgentSkill | undefined>;
|
|
@@ -27,35 +27,38 @@ export declare class ChatMessageTemplate {
|
|
|
27
27
|
content?: ChatModelInputMessage["content"];
|
|
28
28
|
name?: string | undefined;
|
|
29
29
|
options?: FormatOptions | undefined;
|
|
30
|
-
|
|
30
|
+
cacheControl?: ChatModelInputMessage["cacheControl"];
|
|
31
|
+
constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined, cacheControl?: ChatModelInputMessage["cacheControl"]);
|
|
31
32
|
format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage>;
|
|
32
33
|
}
|
|
33
34
|
export declare class SystemMessageTemplate extends ChatMessageTemplate {
|
|
34
|
-
static from(content: string, name?: string, options?: FormatOptions): SystemMessageTemplate;
|
|
35
|
+
static from(content: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): SystemMessageTemplate;
|
|
35
36
|
}
|
|
36
37
|
export declare class UserMessageTemplate extends ChatMessageTemplate {
|
|
37
|
-
static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions): UserMessageTemplate;
|
|
38
|
+
static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): UserMessageTemplate;
|
|
38
39
|
}
|
|
39
40
|
export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
40
41
|
toolCalls?: ChatModelOutputToolCall[] | undefined;
|
|
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);
|
|
42
|
+
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): AgentMessageTemplate;
|
|
43
|
+
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
|
|
43
44
|
format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
|
|
44
|
-
role: "
|
|
45
|
+
role: "system" | "user" | "agent" | "tool";
|
|
45
46
|
name: string | undefined;
|
|
46
47
|
content: ChatModelInputMessageContent | undefined;
|
|
47
48
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
49
|
+
cacheControl: import("../index.js").CacheControl | undefined;
|
|
48
50
|
}>;
|
|
49
51
|
}
|
|
50
52
|
export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
51
53
|
toolCallId: string;
|
|
52
|
-
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions): ToolMessageTemplate;
|
|
53
|
-
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions);
|
|
54
|
+
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): ToolMessageTemplate;
|
|
55
|
+
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
|
|
54
56
|
format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
|
|
55
|
-
role: "
|
|
57
|
+
role: "system" | "user" | "agent" | "tool";
|
|
56
58
|
name: string | undefined;
|
|
57
59
|
content: ChatModelInputMessageContent | undefined;
|
|
58
60
|
toolCallId: string;
|
|
61
|
+
cacheControl: import("../index.js").CacheControl | undefined;
|
|
59
62
|
}>;
|
|
60
63
|
}
|
|
61
64
|
export declare class ChatMessagesTemplate {
|
|
@@ -69,25 +72,61 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
69
72
|
role: z.ZodLiteral<"system">;
|
|
70
73
|
content: z.ZodString;
|
|
71
74
|
name: z.ZodOptional<z.ZodString>;
|
|
75
|
+
cacheControl: z.ZodOptional<z.ZodObject<{
|
|
76
|
+
type: z.ZodLiteral<"ephemeral">;
|
|
77
|
+
ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
|
|
78
|
+
}, "strip", z.ZodTypeAny, {
|
|
79
|
+
type: "ephemeral";
|
|
80
|
+
ttl?: "5m" | "1h" | undefined;
|
|
81
|
+
}, {
|
|
82
|
+
type: "ephemeral";
|
|
83
|
+
ttl?: "5m" | "1h" | undefined;
|
|
84
|
+
}>>;
|
|
72
85
|
}, "strip", z.ZodTypeAny, {
|
|
73
|
-
role: "system";
|
|
74
86
|
content: string;
|
|
87
|
+
role: "system";
|
|
88
|
+
cacheControl?: {
|
|
89
|
+
type: "ephemeral";
|
|
90
|
+
ttl?: "5m" | "1h" | undefined;
|
|
91
|
+
} | undefined;
|
|
75
92
|
name?: string | undefined;
|
|
76
93
|
}, {
|
|
77
|
-
role: "system";
|
|
78
94
|
content: string;
|
|
95
|
+
role: "system";
|
|
96
|
+
cacheControl?: {
|
|
97
|
+
type: "ephemeral";
|
|
98
|
+
ttl?: "5m" | "1h" | undefined;
|
|
99
|
+
} | undefined;
|
|
79
100
|
name?: string | undefined;
|
|
80
101
|
}>, z.ZodObject<{
|
|
81
102
|
role: z.ZodLiteral<"user">;
|
|
82
103
|
content: z.ZodString;
|
|
83
104
|
name: z.ZodOptional<z.ZodString>;
|
|
105
|
+
cacheControl: z.ZodOptional<z.ZodObject<{
|
|
106
|
+
type: z.ZodLiteral<"ephemeral">;
|
|
107
|
+
ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
|
|
108
|
+
}, "strip", z.ZodTypeAny, {
|
|
109
|
+
type: "ephemeral";
|
|
110
|
+
ttl?: "5m" | "1h" | undefined;
|
|
111
|
+
}, {
|
|
112
|
+
type: "ephemeral";
|
|
113
|
+
ttl?: "5m" | "1h" | undefined;
|
|
114
|
+
}>>;
|
|
84
115
|
}, "strip", z.ZodTypeAny, {
|
|
85
|
-
role: "user";
|
|
86
116
|
content: string;
|
|
117
|
+
role: "user";
|
|
118
|
+
cacheControl?: {
|
|
119
|
+
type: "ephemeral";
|
|
120
|
+
ttl?: "5m" | "1h" | undefined;
|
|
121
|
+
} | undefined;
|
|
87
122
|
name?: string | undefined;
|
|
88
123
|
}, {
|
|
89
|
-
role: "user";
|
|
90
124
|
content: string;
|
|
125
|
+
role: "user";
|
|
126
|
+
cacheControl?: {
|
|
127
|
+
type: "ephemeral";
|
|
128
|
+
ttl?: "5m" | "1h" | undefined;
|
|
129
|
+
} | undefined;
|
|
91
130
|
name?: string | undefined;
|
|
92
131
|
}>, z.ZodObject<{
|
|
93
132
|
role: z.ZodLiteral<"agent">;
|
|
@@ -121,10 +160,23 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
121
160
|
id: string;
|
|
122
161
|
}>, "many">>;
|
|
123
162
|
name: z.ZodOptional<z.ZodString>;
|
|
163
|
+
cacheControl: z.ZodOptional<z.ZodObject<{
|
|
164
|
+
type: z.ZodLiteral<"ephemeral">;
|
|
165
|
+
ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
|
|
166
|
+
}, "strip", z.ZodTypeAny, {
|
|
167
|
+
type: "ephemeral";
|
|
168
|
+
ttl?: "5m" | "1h" | undefined;
|
|
169
|
+
}, {
|
|
170
|
+
type: "ephemeral";
|
|
171
|
+
ttl?: "5m" | "1h" | undefined;
|
|
172
|
+
}>>;
|
|
124
173
|
}, "strip", z.ZodTypeAny, {
|
|
125
174
|
role: "agent";
|
|
126
|
-
name?: string | undefined;
|
|
127
175
|
content?: string | undefined;
|
|
176
|
+
cacheControl?: {
|
|
177
|
+
type: "ephemeral";
|
|
178
|
+
ttl?: "5m" | "1h" | undefined;
|
|
179
|
+
} | undefined;
|
|
128
180
|
toolCalls?: {
|
|
129
181
|
function: {
|
|
130
182
|
name: string;
|
|
@@ -133,10 +185,14 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
133
185
|
type: "function";
|
|
134
186
|
id: string;
|
|
135
187
|
}[] | undefined;
|
|
188
|
+
name?: string | undefined;
|
|
136
189
|
}, {
|
|
137
190
|
role: "agent";
|
|
138
|
-
name?: string | undefined;
|
|
139
191
|
content?: string | undefined;
|
|
192
|
+
cacheControl?: {
|
|
193
|
+
type: "ephemeral";
|
|
194
|
+
ttl?: "5m" | "1h" | undefined;
|
|
195
|
+
} | undefined;
|
|
140
196
|
toolCalls?: {
|
|
141
197
|
function: {
|
|
142
198
|
name: string;
|
|
@@ -145,20 +201,39 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
145
201
|
type: "function";
|
|
146
202
|
id: string;
|
|
147
203
|
}[] | undefined;
|
|
204
|
+
name?: string | undefined;
|
|
148
205
|
}>, z.ZodObject<{
|
|
149
206
|
role: z.ZodLiteral<"tool">;
|
|
150
207
|
content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
|
|
151
208
|
toolCallId: z.ZodString;
|
|
152
209
|
name: z.ZodOptional<z.ZodString>;
|
|
210
|
+
cacheControl: z.ZodOptional<z.ZodObject<{
|
|
211
|
+
type: z.ZodLiteral<"ephemeral">;
|
|
212
|
+
ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
|
|
213
|
+
}, "strip", z.ZodTypeAny, {
|
|
214
|
+
type: "ephemeral";
|
|
215
|
+
ttl?: "5m" | "1h" | undefined;
|
|
216
|
+
}, {
|
|
217
|
+
type: "ephemeral";
|
|
218
|
+
ttl?: "5m" | "1h" | undefined;
|
|
219
|
+
}>>;
|
|
153
220
|
}, "strip", z.ZodTypeAny, {
|
|
154
|
-
role: "tool";
|
|
155
221
|
content: string;
|
|
222
|
+
role: "tool";
|
|
156
223
|
toolCallId: string;
|
|
224
|
+
cacheControl?: {
|
|
225
|
+
type: "ephemeral";
|
|
226
|
+
ttl?: "5m" | "1h" | undefined;
|
|
227
|
+
} | undefined;
|
|
157
228
|
name?: string | undefined;
|
|
158
229
|
}, {
|
|
159
|
-
role: "tool";
|
|
160
230
|
content: string | Record<string, unknown>;
|
|
231
|
+
role: "tool";
|
|
161
232
|
toolCallId: string;
|
|
233
|
+
cacheControl?: {
|
|
234
|
+
type: "ephemeral";
|
|
235
|
+
ttl?: "5m" | "1h" | undefined;
|
|
236
|
+
} | undefined;
|
|
162
237
|
name?: string | undefined;
|
|
163
238
|
}>]>;
|
|
164
239
|
export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AFS, type AFSExecOptions, type AFSExecResult, type AFSListOptions, type AFSListResult, type AFSModule, type AFSOptions, type AFSReadResult, type AFSSearchOptions } from "@aigne/afs";
|
|
2
2
|
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
3
3
|
import type * as prompts from "@inquirer/prompts";
|
|
4
|
-
import { type ZodObject, type ZodType } from "zod";
|
|
4
|
+
import { type ZodObject, type ZodType, z } from "zod";
|
|
5
5
|
import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
|
|
6
6
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
7
7
|
import type { ContextUsage } from "../aigne/usage.js";
|
|
@@ -783,6 +783,25 @@ export interface AgentResponseProgress {
|
|
|
783
783
|
} | {
|
|
784
784
|
event: "agentFailed";
|
|
785
785
|
error: Error;
|
|
786
|
+
} | {
|
|
787
|
+
event: "message";
|
|
788
|
+
role: "user" | "agent";
|
|
789
|
+
message: ({
|
|
790
|
+
type: "text";
|
|
791
|
+
content: string;
|
|
792
|
+
} | {
|
|
793
|
+
type: "thinking";
|
|
794
|
+
thoughts: string;
|
|
795
|
+
} | {
|
|
796
|
+
type: "tool_use";
|
|
797
|
+
toolUseId: string;
|
|
798
|
+
name: string;
|
|
799
|
+
input: unknown;
|
|
800
|
+
} | {
|
|
801
|
+
type: "tool_result";
|
|
802
|
+
toolUseId: string;
|
|
803
|
+
content: unknown;
|
|
804
|
+
})[];
|
|
786
805
|
}) & Omit<AgentEvent, "agent"> & {
|
|
787
806
|
agent: {
|
|
788
807
|
name: string;
|
|
@@ -870,6 +889,17 @@ export interface FunctionAgentOptions<I extends Message = Message, O extends Mes
|
|
|
870
889
|
*/
|
|
871
890
|
export declare class FunctionAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
872
891
|
tag: string;
|
|
892
|
+
static schema(): ZodObject<{
|
|
893
|
+
process: ZodType<FunctionAgentFn>;
|
|
894
|
+
}, "strip", z.ZodTypeAny, {
|
|
895
|
+
process: FunctionAgentFn<any, any, FunctionAgent<any, any>>;
|
|
896
|
+
}, {
|
|
897
|
+
process: FunctionAgentFn<any, any, FunctionAgent<any, any>>;
|
|
898
|
+
}>;
|
|
899
|
+
static load<I extends Message = any, O extends Message = any>(options: {
|
|
900
|
+
filepath: string;
|
|
901
|
+
parsed: object;
|
|
902
|
+
}): Promise<Agent<I, O>>;
|
|
873
903
|
/**
|
|
874
904
|
* Create a function agent from a function or options
|
|
875
905
|
*
|
package/lib/esm/agents/agent.js
CHANGED
|
@@ -5,6 +5,7 @@ import nunjucks from "nunjucks";
|
|
|
5
5
|
import { joinURL } from "ufo";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
8
|
+
import { codeToFunctionAgentFn } from "../loader/function-agent.js";
|
|
8
9
|
import { sortHooks } from "../utils/agent-utils.js";
|
|
9
10
|
import { getZodObjectKeys, isZodSchema } from "../utils/json-schema.js";
|
|
10
11
|
import { logger } from "../utils/logger.js";
|
|
@@ -824,6 +825,18 @@ function checkAgentInputOutputSchema(schema) {
|
|
|
824
825
|
*/
|
|
825
826
|
export class FunctionAgent extends Agent {
|
|
826
827
|
tag = "FunctionAgent";
|
|
828
|
+
static schema() {
|
|
829
|
+
return z.object({
|
|
830
|
+
process: z.preprocess((v) => (typeof v === "string" ? codeToFunctionAgentFn(v) : v), z.custom()),
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
static async load(options) {
|
|
834
|
+
const valid = await FunctionAgent.schema().parseAsync(options.parsed);
|
|
835
|
+
return new FunctionAgent({
|
|
836
|
+
...options.parsed,
|
|
837
|
+
process: valid.process,
|
|
838
|
+
});
|
|
839
|
+
}
|
|
827
840
|
/**
|
|
828
841
|
* Create a function agent from a function or options
|
|
829
842
|
*
|
|
@@ -188,6 +188,13 @@ export declare const aiAgentOptionsSchema: ZodObject<{
|
|
|
188
188
|
*/
|
|
189
189
|
export declare class AIAgent<I extends Message = any, O extends Message = any> extends Agent<I, O> {
|
|
190
190
|
tag: string;
|
|
191
|
+
static schema<T>({ filepath }: {
|
|
192
|
+
filepath: string;
|
|
193
|
+
}): ZodType<T>;
|
|
194
|
+
static load<I extends Message = any, O extends Message = any>(options: {
|
|
195
|
+
filepath: string;
|
|
196
|
+
parsed: object;
|
|
197
|
+
}): Promise<Agent<I, O>>;
|
|
191
198
|
/**
|
|
192
199
|
* Create an AIAgent with the specified options
|
|
193
200
|
*
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { camelizeSchema, getInstructionsSchema, instructionsToPromptBuilder, optionalize, } from "../loader/schema.js";
|
|
2
3
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
3
4
|
import { STRUCTURED_STREAM_INSTRUCTIONS } from "../prompt/prompts/structured-stream-instructions.js";
|
|
4
5
|
import { AgentMessageTemplate, ToolMessageTemplate } from "../prompt/template.js";
|
|
5
6
|
import * as fastq from "../utils/queue.js";
|
|
7
|
+
import { mergeAgentResponseChunk } from "../utils/stream-utils.js";
|
|
6
8
|
import { ExtractMetadataTransform } from "../utils/structured-stream-extractor.js";
|
|
7
9
|
import { checkArguments, isEmpty } from "../utils/type-utils.js";
|
|
8
10
|
import { Agent, agentOptionsSchema, isAgentResponseDelta, } from "./agent.js";
|
|
@@ -84,6 +86,32 @@ export const aiAgentOptionsSchema = agentOptionsSchema.extend({
|
|
|
84
86
|
*/
|
|
85
87
|
export class AIAgent extends Agent {
|
|
86
88
|
tag = "AIAgent";
|
|
89
|
+
static schema({ filepath }) {
|
|
90
|
+
const instructionsSchema = getInstructionsSchema({ filepath });
|
|
91
|
+
return camelizeSchema(z.object({
|
|
92
|
+
instructions: optionalize(instructionsSchema),
|
|
93
|
+
autoReorderSystemMessages: optionalize(z.boolean()),
|
|
94
|
+
autoMergeSystemMessages: optionalize(z.boolean()),
|
|
95
|
+
inputKey: optionalize(z.string()),
|
|
96
|
+
outputKey: optionalize(z.string()),
|
|
97
|
+
inputFileKey: optionalize(z.string()),
|
|
98
|
+
outputFileKey: optionalize(z.string()),
|
|
99
|
+
toolChoice: optionalize(z.nativeEnum(AIAgentToolChoice)),
|
|
100
|
+
toolCallsConcurrency: optionalize(z.number().int().min(0)),
|
|
101
|
+
keepTextInToolUses: optionalize(z.boolean()),
|
|
102
|
+
catchToolsError: optionalize(z.boolean()),
|
|
103
|
+
structuredStreamMode: optionalize(z.boolean()),
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
static async load(options) {
|
|
107
|
+
const schema = AIAgent.schema(options);
|
|
108
|
+
const valid = await schema.parseAsync(options.parsed);
|
|
109
|
+
return new AIAgent({
|
|
110
|
+
...options.parsed,
|
|
111
|
+
...valid,
|
|
112
|
+
instructions: valid.instructions && instructionsToPromptBuilder(valid.instructions),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
87
115
|
/**
|
|
88
116
|
* Create an AIAgent with the specified options
|
|
89
117
|
*
|
|
@@ -280,6 +308,16 @@ export class AIAgent extends Agent {
|
|
|
280
308
|
}
|
|
281
309
|
const toolCallMessages = [];
|
|
282
310
|
const outputKey = this.outputKey;
|
|
311
|
+
const inputMessage = this.inputKey ? input[this.inputKey] : undefined;
|
|
312
|
+
if (inputMessage) {
|
|
313
|
+
yield {
|
|
314
|
+
progress: {
|
|
315
|
+
event: "message",
|
|
316
|
+
role: "user",
|
|
317
|
+
message: [{ type: "text", content: inputMessage }],
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
|
283
321
|
for (;;) {
|
|
284
322
|
const modelOutput = {};
|
|
285
323
|
let stream = await this.invokeChildAgent(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { ...options, streaming: true });
|
|
@@ -289,12 +327,12 @@ export class AIAgent extends Agent {
|
|
|
289
327
|
}
|
|
290
328
|
let isTextIgnored = false;
|
|
291
329
|
for await (const value of stream) {
|
|
330
|
+
mergeAgentResponseChunk(modelOutput, value);
|
|
292
331
|
if (isAgentResponseDelta(value)) {
|
|
293
332
|
if (!isTextIgnored && value.delta.text?.text) {
|
|
294
333
|
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
295
334
|
}
|
|
296
335
|
if (value.delta.json) {
|
|
297
|
-
Object.assign(modelOutput, value.delta.json);
|
|
298
336
|
if (this.structuredStreamMode) {
|
|
299
337
|
yield { delta: { json: value.delta.json.json } };
|
|
300
338
|
if (!isTextIgnored && modelOutput.json && this.ignoreTextOfStructuredStreamMode) {
|
|
@@ -304,13 +342,23 @@ export class AIAgent extends Agent {
|
|
|
304
342
|
}
|
|
305
343
|
}
|
|
306
344
|
}
|
|
307
|
-
const { toolCalls, json, text, files } = modelOutput;
|
|
345
|
+
const { toolCalls, json, text, thoughts, files } = modelOutput;
|
|
346
|
+
if (text) {
|
|
347
|
+
yield {
|
|
348
|
+
progress: { event: "message", role: "agent", message: [{ type: "text", content: text }] },
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
if (thoughts) {
|
|
352
|
+
yield {
|
|
353
|
+
progress: { event: "message", role: "agent", message: [{ type: "thinking", thoughts }] },
|
|
354
|
+
};
|
|
355
|
+
}
|
|
308
356
|
if (toolCalls?.length) {
|
|
309
357
|
if (this.keepTextInToolUses !== true) {
|
|
310
358
|
yield { delta: { json: { [outputKey]: "" } } };
|
|
311
359
|
}
|
|
312
360
|
else {
|
|
313
|
-
yield { delta: { text: { [outputKey]: "\n" } } };
|
|
361
|
+
yield { delta: { text: { [outputKey]: "\n\n" } } };
|
|
314
362
|
}
|
|
315
363
|
const executedToolCalls = [];
|
|
316
364
|
let error;
|
|
@@ -340,6 +388,20 @@ export class AIAgent extends Agent {
|
|
|
340
388
|
const tool = toolsMap.get(call.function.name);
|
|
341
389
|
if (!tool)
|
|
342
390
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
391
|
+
yield {
|
|
392
|
+
progress: {
|
|
393
|
+
event: "message",
|
|
394
|
+
role: "agent",
|
|
395
|
+
message: [
|
|
396
|
+
{
|
|
397
|
+
type: "tool_use",
|
|
398
|
+
name: call.function.name,
|
|
399
|
+
input: call.function.arguments,
|
|
400
|
+
toolUseId: call.id,
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
},
|
|
404
|
+
};
|
|
343
405
|
queue.push({ tool, call });
|
|
344
406
|
}
|
|
345
407
|
await queue.drained();
|
|
@@ -347,6 +409,21 @@ export class AIAgent extends Agent {
|
|
|
347
409
|
throw error;
|
|
348
410
|
// Continue LLM function calling loop if any tools were executed
|
|
349
411
|
if (executedToolCalls.length) {
|
|
412
|
+
for (const { call, output } of executedToolCalls) {
|
|
413
|
+
yield {
|
|
414
|
+
progress: {
|
|
415
|
+
event: "message",
|
|
416
|
+
role: "agent",
|
|
417
|
+
message: [
|
|
418
|
+
{
|
|
419
|
+
type: "tool_result",
|
|
420
|
+
toolUseId: call.id,
|
|
421
|
+
content: output,
|
|
422
|
+
},
|
|
423
|
+
],
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
}
|
|
350
427
|
const transferOutput = executedToolCalls.find((i) => isTransferAgentOutput(i.output))?.output;
|
|
351
428
|
if (transferOutput)
|
|
352
429
|
return transferOutput;
|
|
@@ -367,6 +444,11 @@ export class AIAgent extends Agent {
|
|
|
367
444
|
if (!isEmpty(result)) {
|
|
368
445
|
yield { delta: { json: result } };
|
|
369
446
|
}
|
|
447
|
+
if (text) {
|
|
448
|
+
yield {
|
|
449
|
+
progress: { event: "message", role: "agent", message: [{ type: "text", content: text }] },
|
|
450
|
+
};
|
|
451
|
+
}
|
|
370
452
|
return;
|
|
371
453
|
}
|
|
372
454
|
}
|