@aigne/core 1.71.0 → 1.72.0-beta.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/lib/cjs/agents/agent.d.ts +5 -18
  3. package/lib/cjs/agents/agent.js +3 -3
  4. package/lib/cjs/loader/agent-yaml.d.ts +22 -1
  5. package/lib/cjs/loader/agent-yaml.js +18 -0
  6. package/lib/cjs/loader/index.js +26 -1
  7. package/lib/cjs/prompt/context/afs/history.d.ts +5 -0
  8. package/lib/cjs/prompt/context/afs/history.js +32 -0
  9. package/lib/cjs/prompt/context/afs/index.d.ts +20 -0
  10. package/lib/cjs/prompt/context/afs/index.js +48 -0
  11. package/lib/cjs/prompt/context/index.d.ts +31 -0
  12. package/lib/cjs/prompt/context/index.js +18 -0
  13. package/lib/cjs/prompt/prompt-builder.js +10 -39
  14. package/lib/cjs/prompt/skills/afs/edit.d.ts +1 -1
  15. package/lib/cjs/prompt/skills/afs/edit.js +4 -4
  16. package/lib/cjs/prompt/skills/afs/exec.d.ts +1 -1
  17. package/lib/cjs/prompt/skills/afs/exec.js +4 -2
  18. package/lib/cjs/prompt/skills/afs/list.d.ts +1 -2
  19. package/lib/cjs/prompt/skills/afs/list.js +19 -45
  20. package/lib/cjs/prompt/skills/afs/read.d.ts +1 -1
  21. package/lib/cjs/prompt/skills/afs/read.js +4 -4
  22. package/lib/cjs/prompt/skills/afs/search.d.ts +1 -1
  23. package/lib/cjs/prompt/skills/afs/search.js +1 -1
  24. package/lib/dts/agents/agent.d.ts +5 -18
  25. package/lib/dts/loader/agent-yaml.d.ts +22 -1
  26. package/lib/dts/prompt/context/afs/history.d.ts +5 -0
  27. package/lib/dts/prompt/context/afs/index.d.ts +20 -0
  28. package/lib/dts/prompt/context/index.d.ts +31 -0
  29. package/lib/dts/prompt/skills/afs/edit.d.ts +1 -1
  30. package/lib/dts/prompt/skills/afs/exec.d.ts +1 -1
  31. package/lib/dts/prompt/skills/afs/list.d.ts +1 -2
  32. package/lib/dts/prompt/skills/afs/read.d.ts +1 -1
  33. package/lib/dts/prompt/skills/afs/search.d.ts +1 -1
  34. package/lib/esm/agents/agent.d.ts +5 -18
  35. package/lib/esm/agents/agent.js +3 -3
  36. package/lib/esm/loader/agent-yaml.d.ts +22 -1
  37. package/lib/esm/loader/agent-yaml.js +18 -0
  38. package/lib/esm/loader/index.js +26 -1
  39. package/lib/esm/prompt/context/afs/history.d.ts +5 -0
  40. package/lib/esm/prompt/context/afs/history.js +29 -0
  41. package/lib/esm/prompt/context/afs/index.d.ts +20 -0
  42. package/lib/esm/prompt/context/afs/index.js +45 -0
  43. package/lib/esm/prompt/context/index.d.ts +31 -0
  44. package/lib/esm/prompt/context/index.js +15 -0
  45. package/lib/esm/prompt/prompt-builder.js +10 -39
  46. package/lib/esm/prompt/skills/afs/edit.d.ts +1 -1
  47. package/lib/esm/prompt/skills/afs/edit.js +4 -4
  48. package/lib/esm/prompt/skills/afs/exec.d.ts +1 -1
  49. package/lib/esm/prompt/skills/afs/exec.js +4 -2
  50. package/lib/esm/prompt/skills/afs/list.d.ts +1 -2
  51. package/lib/esm/prompt/skills/afs/list.js +19 -45
  52. package/lib/esm/prompt/skills/afs/read.d.ts +1 -1
  53. package/lib/esm/prompt/skills/afs/read.js +4 -4
  54. package/lib/esm/prompt/skills/afs/search.d.ts +1 -1
  55. package/lib/esm/prompt/skills/afs/search.js +1 -1
  56. package/package.json +5 -5
@@ -21,7 +21,7 @@ class AFSReadAgent extends agent_js_1.Agent {
21
21
  tool: zod_1.z.string(),
22
22
  path: zod_1.z.string(),
23
23
  withLineNumbers: zod_1.z.boolean().optional(),
24
- result: zod_1.z.custom().optional(),
24
+ data: zod_1.z.custom().optional(),
25
25
  message: zod_1.z.string().optional(),
26
26
  }),
27
27
  });
@@ -30,7 +30,7 @@ class AFSReadAgent extends agent_js_1.Agent {
30
30
  if (!this.afs)
31
31
  throw new Error("AFS is not configured for this agent.");
32
32
  const result = await this.afs.read(input.path);
33
- let content = result.result?.content;
33
+ let content = result.data?.content;
34
34
  if (input.withLineNumbers && typeof content === "string") {
35
35
  content = content
36
36
  .split("\n")
@@ -43,8 +43,8 @@ class AFSReadAgent extends agent_js_1.Agent {
43
43
  path: input.path,
44
44
  withLineNumbers: input.withLineNumbers,
45
45
  ...result,
46
- result: result.result && {
47
- ...result.result,
46
+ data: result.data && {
47
+ ...result.data,
48
48
  content,
49
49
  },
50
50
  };
@@ -11,7 +11,7 @@ export interface AFSSearchOutput extends Message {
11
11
  path: string;
12
12
  query: string;
13
13
  options?: AFSSearchOptions;
14
- list: AFSEntry[];
14
+ data: AFSEntry[];
15
15
  message?: string;
16
16
  }
17
17
  export interface AFSSearchAgentOptions extends AgentOptions<AFSSearchInput, AFSSearchOutput> {
@@ -33,7 +33,7 @@ class AFSSearchAgent extends agent_js_1.Agent {
33
33
  caseSensitive: zod_1.z.boolean().optional(),
34
34
  })
35
35
  .optional(),
36
- list: zod_1.z.array(zod_1.z.custom()),
36
+ data: zod_1.z.array(zod_1.z.custom()),
37
37
  message: zod_1.z.string().optional(),
38
38
  }),
39
39
  });
@@ -1,4 +1,4 @@
1
- import { AFS, type AFSEntry, type AFSListOptions, type AFSModule, type AFSOptions, type AFSSearchOptions } from "@aigne/afs";
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
4
  import { type ZodObject, type ZodType } from "zod";
@@ -573,23 +573,10 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
573
573
  /** For AFSModule interface **/
574
574
  private agentToAFSEntry;
575
575
  private findAgentByAFSPath;
576
- list(_path: string, _options?: AFSListOptions): Promise<{
577
- list: AFSEntry[];
578
- message?: string;
579
- }>;
580
- read(path: string): Promise<{
581
- result?: AFSEntry;
582
- message?: string;
583
- }>;
584
- search(path: string, _query: string, options?: AFSSearchOptions): Promise<{
585
- list: AFSEntry[];
586
- message?: string;
587
- }>;
588
- exec(path: string, args: Record<string, any>, options: {
589
- context: Context;
590
- }): Promise<{
591
- result: Record<string, any>;
592
- }>;
576
+ list(_path: string, _options?: AFSListOptions): Promise<AFSListResult>;
577
+ read(path: string): Promise<AFSReadResult>;
578
+ search(path: string, _query: string, options?: AFSSearchOptions): Promise<AFSListResult>;
579
+ exec(path: string, args: Record<string, any>, options: AFSExecOptions): Promise<AFSExecResult>;
593
580
  }
594
581
  export type AgentInput<T extends Agent> = T extends Agent<infer I, any> ? I : never;
595
582
  export type AgentOutput<T extends Agent> = T extends Agent<any, infer O> ? O : never;
@@ -25,6 +25,26 @@ export type AFSModuleSchema = string | {
25
25
  module: string;
26
26
  options?: Record<string, any>;
27
27
  };
28
+ export interface AFSContextPresetSchema {
29
+ view?: string;
30
+ select?: {
31
+ agent: NestAgentSchema;
32
+ };
33
+ per?: {
34
+ agent: NestAgentSchema;
35
+ };
36
+ dedupe?: {
37
+ agent: NestAgentSchema;
38
+ };
39
+ }
40
+ export interface AFSContextSchema {
41
+ search?: {
42
+ presets?: Record<string, AFSContextPresetSchema>;
43
+ };
44
+ list?: {
45
+ presets?: Record<string, AFSContextPresetSchema>;
46
+ };
47
+ }
28
48
  export interface BaseAgentSchema {
29
49
  name?: string;
30
50
  description?: string;
@@ -42,8 +62,9 @@ export interface BaseAgentSchema {
42
62
  provider: string;
43
63
  subscribeTopic?: string[];
44
64
  };
45
- afs?: boolean | (Omit<AFSOptions, "modules"> & {
65
+ afs?: boolean | (Omit<AFSOptions, "modules" | "context"> & {
46
66
  modules?: AFSModuleSchema[];
67
+ context?: AFSContextSchema;
47
68
  });
48
69
  shareAFS?: boolean;
49
70
  }
@@ -0,0 +1,5 @@
1
+ import type { Agent } from "../../../agents/agent.js";
2
+ export declare function getHistories(agent: Agent): Promise<{
3
+ role: "user" | "agent";
4
+ content: unknown;
5
+ }[]>;
@@ -0,0 +1,20 @@
1
+ import type { AFSRootListOptions, AFSRootSearchOptions } from "@aigne/afs";
2
+ import type { Agent, AgentInvokeOptions } from "../../../agents/agent.js";
3
+ export declare function createAFSContext(agent?: Agent<any, any>, context?: AgentInvokeOptions["context"]): {
4
+ readonly enabled: boolean;
5
+ description: string;
6
+ readonly modules: Promise<Pick<{
7
+ name: string;
8
+ path: string;
9
+ description?: string;
10
+ module: import("@aigne/afs").AFSModule;
11
+ }, "path" | "name" | "description">[]> | undefined;
12
+ readonly histories: Promise<{
13
+ role: "user" | "agent";
14
+ content: unknown;
15
+ }[]>;
16
+ readonly skills: never[] | Promise<Pick<Agent<any, any>, "name" | "description">[]>;
17
+ list(path: string, options?: AFSRootListOptions): Promise<any>;
18
+ read(path: string): Promise<import("@aigne/afs").AFSEntry<any> | undefined>;
19
+ search(path: string, query: string, options?: AFSRootSearchOptions): Promise<any>;
20
+ };
@@ -0,0 +1,31 @@
1
+ import type { PromptBuildOptions } from "../prompt-builder.js";
2
+ export declare function createPromptBuilderContext(options: PromptBuildOptions): {
3
+ $afs: {
4
+ readonly enabled: boolean;
5
+ description: string;
6
+ readonly modules: Promise<Pick<{
7
+ name: string;
8
+ path: string;
9
+ description?: string;
10
+ module: import("@aigne/afs").AFSModule;
11
+ }, "path" | "name" | "description">[]> | undefined;
12
+ readonly histories: Promise<{
13
+ role: "user" | "agent";
14
+ content: unknown;
15
+ }[]>;
16
+ readonly skills: never[] | Promise<Pick<import("../../index.js").Agent<any, any>, "name" | "description">[]>;
17
+ list(path: string, options?: import("@aigne/afs").AFSRootListOptions): Promise<any>;
18
+ read(path: string): Promise<import("@aigne/afs").AFSEntry<any> | undefined>;
19
+ search(path: string, query: string, options?: import("@aigne/afs").AFSRootSearchOptions): Promise<any>;
20
+ };
21
+ $agent: {
22
+ readonly skills: Pick<import("../../index.js").Agent<any, any>, "name" | "description">[] | undefined;
23
+ };
24
+ $meta?: {
25
+ usage?: import("../../index.js").ContextUsage;
26
+ [key: string]: unknown;
27
+ };
28
+ userId?: string;
29
+ sessionId?: string;
30
+ userContext: import("../../index.js").UserContext | undefined;
31
+ };
@@ -14,7 +14,7 @@ export interface AFSEditOutput extends Message {
14
14
  tool: string;
15
15
  path: string;
16
16
  message: string;
17
- content: string;
17
+ data: string;
18
18
  }
19
19
  export interface AFSEditAgentOptions extends AgentOptions<AFSEditInput, AFSEditOutput> {
20
20
  afs: NonNullable<AgentOptions<AFSEditInput, AFSEditOutput>["afs"]>;
@@ -4,7 +4,7 @@ export interface AFSExecInput extends Message {
4
4
  args: string;
5
5
  }
6
6
  export interface AFSExecOutput extends Message {
7
- result: Record<string, any>;
7
+ data: Record<string, any>;
8
8
  }
9
9
  export interface AFSExecAgentOptions extends AgentOptions<AFSExecInput, AFSExecOutput> {
10
10
  afs: NonNullable<AgentOptions<AFSExecInput, AFSExecOutput>["afs"]>;
@@ -10,7 +10,7 @@ export interface AFSListOutput extends Message {
10
10
  path: string;
11
11
  options?: AFSListOptions;
12
12
  message?: string;
13
- result: string;
13
+ data?: unknown;
14
14
  }
15
15
  export interface AFSListAgentOptions extends AgentOptions<AFSListInput, AFSListOutput> {
16
16
  afs: NonNullable<AgentOptions<AFSListInput, AFSListOutput>["afs"]>;
@@ -18,5 +18,4 @@ export interface AFSListAgentOptions extends AgentOptions<AFSListInput, AFSListO
18
18
  export declare class AFSListAgent extends Agent<AFSListInput, AFSListOutput> {
19
19
  constructor(options: AFSListAgentOptions);
20
20
  process(input: AFSListInput, _options: AgentInvokeOptions): Promise<AFSListOutput>;
21
- private buildTreeView;
22
21
  }
@@ -9,7 +9,7 @@ export interface AFSReadOutput extends Message {
9
9
  tool: string;
10
10
  path: string;
11
11
  withLineNumbers?: boolean;
12
- result?: AFSEntry;
12
+ data?: AFSEntry;
13
13
  message?: string;
14
14
  }
15
15
  export interface AFSReadAgentOptions extends AgentOptions<AFSReadInput, AFSReadOutput> {
@@ -11,7 +11,7 @@ export interface AFSSearchOutput extends Message {
11
11
  path: string;
12
12
  query: string;
13
13
  options?: AFSSearchOptions;
14
- list: AFSEntry[];
14
+ data: AFSEntry[];
15
15
  message?: string;
16
16
  }
17
17
  export interface AFSSearchAgentOptions extends AgentOptions<AFSSearchInput, AFSSearchOutput> {
@@ -1,4 +1,4 @@
1
- import { AFS, type AFSEntry, type AFSListOptions, type AFSModule, type AFSOptions, type AFSSearchOptions } from "@aigne/afs";
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
4
  import { type ZodObject, type ZodType } from "zod";
@@ -573,23 +573,10 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
573
573
  /** For AFSModule interface **/
574
574
  private agentToAFSEntry;
575
575
  private findAgentByAFSPath;
576
- list(_path: string, _options?: AFSListOptions): Promise<{
577
- list: AFSEntry[];
578
- message?: string;
579
- }>;
580
- read(path: string): Promise<{
581
- result?: AFSEntry;
582
- message?: string;
583
- }>;
584
- search(path: string, _query: string, options?: AFSSearchOptions): Promise<{
585
- list: AFSEntry[];
586
- message?: string;
587
- }>;
588
- exec(path: string, args: Record<string, any>, options: {
589
- context: Context;
590
- }): Promise<{
591
- result: Record<string, any>;
592
- }>;
576
+ list(_path: string, _options?: AFSListOptions): Promise<AFSListResult>;
577
+ read(path: string): Promise<AFSReadResult>;
578
+ search(path: string, _query: string, options?: AFSSearchOptions): Promise<AFSListResult>;
579
+ exec(path: string, args: Record<string, any>, options: AFSExecOptions): Promise<AFSExecResult>;
593
580
  }
594
581
  export type AgentInput<T extends Agent> = T extends Agent<infer I, any> ? I : never;
595
582
  export type AgentOutput<T extends Agent> = T extends Agent<any, infer O> ? O : never;
@@ -733,7 +733,7 @@ export class Agent {
733
733
  // TODO: support list skills inside agent path, and use options to filter skills
734
734
  async list(_path, _options) {
735
735
  const agents = [this, ...this.skills];
736
- return { list: agents.map((agent) => this.agentToAFSEntry(agent)) };
736
+ return { data: agents.map((agent) => this.agentToAFSEntry(agent)) };
737
737
  }
738
738
  async read(path) {
739
739
  const agent = this.findAgentByAFSPath(path);
@@ -741,7 +741,7 @@ export class Agent {
741
741
  return { message: `Agent not found at path: ${path}` };
742
742
  }
743
743
  return {
744
- result: this.agentToAFSEntry(agent),
744
+ data: this.agentToAFSEntry(agent),
745
745
  };
746
746
  }
747
747
  // TODO: implement search inside agent skills
@@ -752,7 +752,7 @@ export class Agent {
752
752
  const agent = this.findAgentByAFSPath(path);
753
753
  if (!agent)
754
754
  throw new Error(`Agent not found at path: ${path}`);
755
- return { result: await options.context.invoke(agent, args) };
755
+ return { data: await options.context.invoke(agent, args) };
756
756
  }
757
757
  }
758
758
  /**
@@ -25,6 +25,26 @@ export type AFSModuleSchema = string | {
25
25
  module: string;
26
26
  options?: Record<string, any>;
27
27
  };
28
+ export interface AFSContextPresetSchema {
29
+ view?: string;
30
+ select?: {
31
+ agent: NestAgentSchema;
32
+ };
33
+ per?: {
34
+ agent: NestAgentSchema;
35
+ };
36
+ dedupe?: {
37
+ agent: NestAgentSchema;
38
+ };
39
+ }
40
+ export interface AFSContextSchema {
41
+ search?: {
42
+ presets?: Record<string, AFSContextPresetSchema>;
43
+ };
44
+ list?: {
45
+ presets?: Record<string, AFSContextPresetSchema>;
46
+ };
47
+ }
28
48
  export interface BaseAgentSchema {
29
49
  name?: string;
30
50
  description?: string;
@@ -42,8 +62,9 @@ export interface BaseAgentSchema {
42
62
  provider: string;
43
63
  subscribeTopic?: string[];
44
64
  };
45
- afs?: boolean | (Omit<AFSOptions, "modules"> & {
65
+ afs?: boolean | (Omit<AFSOptions, "modules" | "context"> & {
46
66
  modules?: AFSModuleSchema[];
67
+ context?: AFSContextSchema;
47
68
  });
48
69
  shareAFS?: boolean;
49
70
  }
@@ -80,6 +80,20 @@ export const getAgentSchema = ({ filepath, options, }) => {
80
80
  onSkillEnd: optionalize(nestAgentSchema),
81
81
  onHandoff: optionalize(nestAgentSchema),
82
82
  }));
83
+ const afsContextPresetsSchema = z.object({
84
+ presets: optionalize(z.record(z.string(), z.object({
85
+ view: optionalize(z.string()),
86
+ select: optionalize(z.object({
87
+ agent: nestAgentSchema,
88
+ })),
89
+ per: optionalize(z.object({
90
+ agent: nestAgentSchema,
91
+ })),
92
+ dedupe: optionalize(z.object({
93
+ agent: nestAgentSchema,
94
+ })),
95
+ }))),
96
+ });
83
97
  const baseAgentSchema = z.object({
84
98
  name: optionalize(z.string()),
85
99
  alias: optionalize(z.array(z.string())),
@@ -111,6 +125,10 @@ export const getAgentSchema = ({ filepath, options, }) => {
111
125
  options: optionalize(z.record(z.any())),
112
126
  })),
113
127
  ]))),
128
+ context: optionalize(z.object({
129
+ search: optionalize(afsContextPresetsSchema),
130
+ list: optionalize(afsContextPresetsSchema),
131
+ })),
114
132
  })),
115
133
  ])),
116
134
  shareAFS: optionalize(z.boolean()),
@@ -123,7 +123,32 @@ export async function parseAgent(path, agent, options, agentOptions) {
123
123
  afs = new AFS();
124
124
  }
125
125
  else if (agent.afs) {
126
- afs = new AFS();
126
+ afs = new AFS({});
127
+ const loadAFSContextPresets = async (presets) => {
128
+ return Object.fromEntries(await Promise.all(Object.entries(presets).map(async ([key, value]) => {
129
+ const [select, per, dedupe] = await Promise.all([value.select, value.per, value.dedupe].map(async (item) => {
130
+ if (!item?.agent)
131
+ return undefined;
132
+ const agent = await loadNestAgent(path, item.agent, options, { afs });
133
+ return {
134
+ invoke: (input, options) => options.context.invoke(agent, input, {
135
+ ...options,
136
+ streaming: false,
137
+ }),
138
+ };
139
+ }));
140
+ return [key, { ...value, select, per, dedupe }];
141
+ })));
142
+ };
143
+ const context = {
144
+ search: {
145
+ presets: await loadAFSContextPresets(agent.afs.context?.search?.presets || {}),
146
+ },
147
+ list: {
148
+ presets: await loadAFSContextPresets(agent.afs.context?.list?.presets || {}),
149
+ },
150
+ };
151
+ afs.options.context = context;
127
152
  for (const m of agent.afs.modules || []) {
128
153
  const moduleName = typeof m === "string" ? m : m.module;
129
154
  const mod = options?.afs?.availableModules?.find((mod) => mod.module === moduleName || mod.alias?.includes(moduleName));
@@ -0,0 +1,5 @@
1
+ import type { Agent } from "../../../agents/agent.js";
2
+ export declare function getHistories(agent: Agent): Promise<{
3
+ role: "user" | "agent";
4
+ content: unknown;
5
+ }[]>;
@@ -0,0 +1,29 @@
1
+ import { AFSHistory } from "@aigne/afs-history";
2
+ import { isNonNullable } from "../../../utils/type-utils.js";
3
+ export async function getHistories(agent) {
4
+ const afs = agent?.afs;
5
+ if (!afs)
6
+ return [];
7
+ const historyModule = (await afs.listModules()).find((m) => m.module instanceof AFSHistory);
8
+ if (!historyModule)
9
+ return [];
10
+ const history = (await afs.list(historyModule.path, {
11
+ limit: agent.historyConfig?.maxItems || 10,
12
+ orderBy: [["createdAt", "desc"]],
13
+ })).data;
14
+ return history
15
+ .reverse()
16
+ .map((i) => {
17
+ if (!i.content)
18
+ return;
19
+ const { input, output } = i.content;
20
+ if (!input || !output)
21
+ return;
22
+ return [
23
+ { role: "user", content: input },
24
+ { role: "agent", content: output },
25
+ ];
26
+ })
27
+ .filter(isNonNullable)
28
+ .flat();
29
+ }
@@ -0,0 +1,20 @@
1
+ import type { AFSRootListOptions, AFSRootSearchOptions } from "@aigne/afs";
2
+ import type { Agent, AgentInvokeOptions } from "../../../agents/agent.js";
3
+ export declare function createAFSContext(agent?: Agent<any, any>, context?: AgentInvokeOptions["context"]): {
4
+ readonly enabled: boolean;
5
+ description: string;
6
+ readonly modules: Promise<Pick<{
7
+ name: string;
8
+ path: string;
9
+ description?: string;
10
+ module: import("@aigne/afs").AFSModule;
11
+ }, "path" | "name" | "description">[]> | undefined;
12
+ readonly histories: Promise<{
13
+ role: "user" | "agent";
14
+ content: unknown;
15
+ }[]>;
16
+ readonly skills: never[] | Promise<Pick<Agent<any, any>, "name" | "description">[]>;
17
+ list(path: string, options?: AFSRootListOptions): Promise<any>;
18
+ read(path: string): Promise<import("@aigne/afs").AFSEntry<any> | undefined>;
19
+ search(path: string, query: string, options?: AFSRootSearchOptions): Promise<any>;
20
+ };
@@ -0,0 +1,45 @@
1
+ import { pick } from "../../../utils/type-utils.js";
2
+ import { AFS_DESCRIPTION_PROMPT_TEMPLATE } from "../../prompts/afs-builtin-prompt.js";
3
+ import { getAFSSkills } from "../../skills/afs/index.js";
4
+ import { getHistories } from "./history.js";
5
+ export function createAFSContext(agent, context) {
6
+ const afs = agent?.afs;
7
+ return {
8
+ get enabled() {
9
+ return !!afs;
10
+ },
11
+ description: AFS_DESCRIPTION_PROMPT_TEMPLATE,
12
+ get modules() {
13
+ return afs
14
+ ?.listModules()
15
+ .then((list) => list.map((i) => pick(i, ["name", "path", "description"])));
16
+ },
17
+ get histories() {
18
+ if (!agent)
19
+ return Promise.resolve([]);
20
+ return getHistories(agent);
21
+ },
22
+ get skills() {
23
+ const afs = agent?.afs;
24
+ if (!afs)
25
+ return [];
26
+ return getAFSSkills(afs).then((skills) => skills.map((s) => pick(s, ["name", "description"])));
27
+ },
28
+ async list(path, options) {
29
+ if (!afs)
30
+ throw new Error("AFS is not configured for this agent.");
31
+ return (await afs.list(path, { ...options, context, format: options?.format || "tree" }))
32
+ .data;
33
+ },
34
+ async read(path) {
35
+ if (!afs)
36
+ throw new Error("AFS is not configured for this agent.");
37
+ return (await afs.read(path)).data;
38
+ },
39
+ async search(path, query, options = {}) {
40
+ if (!afs)
41
+ throw new Error("AFS is not configured for this agent.");
42
+ return (await afs.search(path, query, { ...options, context })).data;
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,31 @@
1
+ import type { PromptBuildOptions } from "../prompt-builder.js";
2
+ export declare function createPromptBuilderContext(options: PromptBuildOptions): {
3
+ $afs: {
4
+ readonly enabled: boolean;
5
+ description: string;
6
+ readonly modules: Promise<Pick<{
7
+ name: string;
8
+ path: string;
9
+ description?: string;
10
+ module: import("@aigne/afs").AFSModule;
11
+ }, "path" | "name" | "description">[]> | undefined;
12
+ readonly histories: Promise<{
13
+ role: "user" | "agent";
14
+ content: unknown;
15
+ }[]>;
16
+ readonly skills: never[] | Promise<Pick<import("../../index.js").Agent<any, any>, "name" | "description">[]>;
17
+ list(path: string, options?: import("@aigne/afs").AFSRootListOptions): Promise<any>;
18
+ read(path: string): Promise<import("@aigne/afs").AFSEntry<any> | undefined>;
19
+ search(path: string, query: string, options?: import("@aigne/afs").AFSRootSearchOptions): Promise<any>;
20
+ };
21
+ $agent: {
22
+ readonly skills: Pick<import("../../index.js").Agent<any, any>, "name" | "description">[] | undefined;
23
+ };
24
+ $meta?: {
25
+ usage?: import("../../index.js").ContextUsage;
26
+ [key: string]: unknown;
27
+ };
28
+ userId?: string;
29
+ sessionId?: string;
30
+ userContext: import("../../index.js").UserContext | undefined;
31
+ };
@@ -0,0 +1,15 @@
1
+ import { pick } from "../../utils/type-utils.js";
2
+ import { createAFSContext } from "./afs/index.js";
3
+ export function createPromptBuilderContext(options) {
4
+ return {
5
+ userContext: options.context?.userContext,
6
+ ...options.context?.userContext,
7
+ ...options.input,
8
+ $afs: createAFSContext(options.agent, options.context),
9
+ $agent: {
10
+ get skills() {
11
+ return options.agent?.skills.map((s) => pick(s, ["name", "description"]));
12
+ },
13
+ },
14
+ };
15
+ }
@@ -8,8 +8,9 @@ import { DEFAULT_OUTPUT_FILE_KEY, DEFAULT_OUTPUT_KEY } from "../agents/ai-agent.
8
8
  import { fileUnionContentsSchema } from "../agents/model.js";
9
9
  import { optionalize } from "../loader/schema.js";
10
10
  import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
11
- import { checkArguments, flat, isNonNullable, isRecord, partition, pick, unique, } from "../utils/type-utils.js";
12
- import { AFS_DESCRIPTION_PROMPT_TEMPLATE, AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE, getAFSSystemPrompt, } from "./prompts/afs-builtin-prompt.js";
11
+ import { checkArguments, flat, isNonNullable, isRecord, partition, unique, } from "../utils/type-utils.js";
12
+ import { createPromptBuilderContext } from "./context/index.js";
13
+ import { AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE, getAFSSystemPrompt, } from "./prompts/afs-builtin-prompt.js";
13
14
  import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
14
15
  import { STRUCTURED_STREAM_INSTRUCTIONS } from "./prompts/structured-stream-instructions.js";
15
16
  import { getAFSSkills } from "./skills/afs/index.js";
@@ -92,37 +93,7 @@ export class PromptBuilder {
92
93
  };
93
94
  }
94
95
  getTemplateVariables(options) {
95
- const self = this;
96
- return {
97
- userContext: options.context?.userContext,
98
- ...options.context?.userContext,
99
- ...options.input,
100
- $afs: {
101
- get enabled() {
102
- return !!options.agent?.afs;
103
- },
104
- description: AFS_DESCRIPTION_PROMPT_TEMPLATE,
105
- get modules() {
106
- return options.agent?.afs
107
- ?.listModules()
108
- .then((list) => list.map((i) => pick(i, ["name", "path", "description"])));
109
- },
110
- get histories() {
111
- return self.getHistories(options);
112
- },
113
- get skills() {
114
- const afs = options.agent?.afs;
115
- if (!afs)
116
- return [];
117
- return getAFSSkills(afs).then((skills) => skills.map((s) => pick(s, ["name", "description"])));
118
- },
119
- },
120
- $agent: {
121
- get skills() {
122
- return options.agent?.skills.map((s) => pick(s, ["name", "description"]));
123
- },
124
- },
125
- };
96
+ return createPromptBuilderContext(options);
126
97
  }
127
98
  async buildMessages(options) {
128
99
  const { input } = options;
@@ -151,12 +122,12 @@ export class PromptBuilder {
151
122
  limit: options.agent?.maxRetrieveMemoryCount || 10,
152
123
  orderBy: [["createdAt", "desc"]],
153
124
  });
154
- memories.push(...history.list
125
+ memories.push(...history.data
155
126
  .reverse()
156
127
  .filter((i) => isNonNullable(i.content)));
157
128
  if (message) {
158
- const result = await afs.search("/", message);
159
- const ms = result.list
129
+ const result = (await afs.search("/", message)).data;
130
+ const ms = result
160
131
  .map((entry) => {
161
132
  if (entry.metadata?.execute)
162
133
  return null;
@@ -170,7 +141,7 @@ export class PromptBuilder {
170
141
  })
171
142
  .filter(isNonNullable);
172
143
  memories.push(...ms);
173
- const executable = result.list.filter((i) => !!i.metadata?.execute);
144
+ const executable = result.filter((i) => !!i.metadata?.execute);
174
145
  if (executable.length) {
175
146
  messages.push({
176
147
  role: "system",
@@ -229,10 +200,10 @@ export class PromptBuilder {
229
200
  const historyModule = (await afs.listModules()).find((m) => m.module instanceof AFSHistory);
230
201
  if (!historyModule)
231
202
  return [];
232
- const { list: history } = await afs.list(historyModule.path, {
203
+ const history = (await afs.list(historyModule.path, {
233
204
  limit: agent.historyConfig?.maxItems || 10,
234
205
  orderBy: [["createdAt", "desc"]],
235
- });
206
+ })).data;
236
207
  return history
237
208
  .reverse()
238
209
  .map((i) => {
@@ -14,7 +14,7 @@ export interface AFSEditOutput extends Message {
14
14
  tool: string;
15
15
  path: string;
16
16
  message: string;
17
- content: string;
17
+ data: string;
18
18
  }
19
19
  export interface AFSEditAgentOptions extends AgentOptions<AFSEditInput, AFSEditOutput> {
20
20
  afs: NonNullable<AgentOptions<AFSEditInput, AFSEditOutput>["afs"]>;