From 1b2372a2d47b60b8d0eb0adaf1930d43c74c6de1 Mon Sep 17 00:00:00 2001 From: isamu Date: Sat, 11 May 2024 04:01:21 +0900 Subject: [PATCH 1/3] add filterParams --- src/node.ts | 8 +- src/type.ts | 6 +- src/validators/common.ts | 2 +- tests/agentFilters/test_filter_params.ts | 95 ++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 tests/agentFilters/test_filter_params.ts diff --git a/src/node.ts b/src/node.ts index 8fdcb2b99..5af3e705c 100644 --- a/src/node.ts +++ b/src/node.ts @@ -11,6 +11,7 @@ import { AgentFunctionContext, AgentFunction, AgentFilterInfo, + AgentFilterParams, DefaultParamsType, DefaultInputData, } from "@/type"; @@ -53,6 +54,7 @@ export class ComputedNode extends Node { public readonly graphId: string; public readonly isResult: boolean; public readonly params: NodeDataParams; // Agent-specific parameters + private readonly filterParams: AgentFilterParams; private readonly dynamicParams: Record; public readonly nestedGraph?: GraphData; public readonly retryLimit: number; @@ -76,6 +78,7 @@ export class ComputedNode extends Node { super(nodeId, graph); this.graphId = graphId; this.params = data.params ?? {}; + this.filterParams = data.filterParams ?? {}; this.nestedGraph = data.graph; if (typeof data.agent === "string") { this.agentId = data.agent; @@ -220,6 +223,9 @@ export class ComputedNode extends Node { const agentFilter = this.graph.agentFilters[index++]; if (agentFilter) { if (this.shouldApplyAgentFilter(agentFilter)) { + if (agentFilter.filterParams) { + context.filterParams = { ...agentFilter.filterParams, ...context.filterParams }; + } return agentFilter.agent(context, next); } return next(context); @@ -267,7 +273,7 @@ export class ComputedNode extends Node { retry: this.retryCount, verbose: this.graph.verbose, }, - filterParams: {}, + filterParams: this.filterParams, log: localLog, }; diff --git a/src/type.ts b/src/type.ts index 718037c3c..49573412b 100644 --- a/src/type.ts +++ b/src/type.ts @@ -33,11 +33,14 @@ export type StaticNodeData = { }; export type AgentNamelessFunction = (...param: any[]) => unknown; +export type AgentFilterParams = Record; + export type ComputedNodeData = { agent: string | AgentNamelessFunction; inputs?: Array; anyInput?: boolean; // any input makes this node ready params?: NodeDataParams; + filterParams?: AgentFilterParams; // agent filter retry?: number; timeout?: number; // msec if?: string; // conditional execution @@ -73,7 +76,7 @@ export type AgentFunctionContext; // agent filter + filterParams: AgentFilterParams; // agent filter log?: TransactionLog[]; }; @@ -91,6 +94,7 @@ export type AgentFilterInfo = { agent: AgentFilterFunction; agentIds?: string[]; nodeIds?: string[]; + filterParams?: AgentFilterParams; }; export type AgentFunctionDictonary = Record>; diff --git a/src/validators/common.ts b/src/validators/common.ts index eaa16a6d4..7a094f1c6 100644 --- a/src/validators/common.ts +++ b/src/validators/common.ts @@ -1,4 +1,4 @@ export const graphDataAttributeKeys = ["nodes", "concurrency", "agentId", "loop", "verbose", "version"]; -export const computedNodeAttributeKeys = ["inputs", "anyInput", "params", "retry", "timeout", "agent", "graph", "isResult", "priority", "if"]; +export const computedNodeAttributeKeys = ["inputs", "anyInput", "params", "retry", "timeout", "agent", "graph", "isResult", "priority", "if", "filterParams"]; export const staticNodeAttributeKeys = ["value", "update", "isResult"]; diff --git a/tests/agentFilters/test_filter_params.ts b/tests/agentFilters/test_filter_params.ts new file mode 100644 index 000000000..eceb5d312 --- /dev/null +++ b/tests/agentFilters/test_filter_params.ts @@ -0,0 +1,95 @@ +import { GraphAI } from "@/graphai"; +import { AgentFilterFunction } from "@/type"; + +import { defaultTestAgents } from "@/utils/test_agents"; + +import test from "node:test"; +import assert from "node:assert"; + +const httpAgentFilter: AgentFilterFunction = async (context, next) => { + console.log(context.filterParams); + return next(context); +}; + +const callbackDictonary = {}; + +test("test filterParams on agent filter", async () => { + const graph_data = { + version: 0.3, + nodes: { + echo: { + agent: "echoAgent", + params: { + filterParams: true, + }, + }, + bypassAgent: { + agent: "bypassAgent", + inputs: [":echo"], + isResult: true, + }, + }, + }; + const agentFilters = [ + { + name: "httpAgentFilter", + agent: httpAgentFilter, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8085/agents/", + stream: true, + }, + }, + agentIds: ["echoAgent"], + }, + ]; + + const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); + + const result = await graph.run(); + console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8085/agents/", stream: true } }] }); +}); + +test("test filterParams on agent filter2", async () => { + const graph_data = { + version: 0.3, + nodes: { + echo: { + agent: "echoAgent", + params: { + filterParams: true, + }, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8081/agents/", + }, + }, + }, + bypassAgent: { + agent: "bypassAgent", + inputs: [":echo"], + isResult: true, + }, + }, + }; + const agentFilters = [ + { + name: "httpAgentFilter", + agent: httpAgentFilter, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8085/agents/", + stream: true, + }, + }, + agentIds: ["echoAgent"], + }, + ]; + + const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); + + const result = await graph.run(); + console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/agents/", stream: true } }] }); +}); From 5532acff3ed22e1435d16380b35d006ffa75266f Mon Sep 17 00:00:00 2001 From: isamu Date: Sat, 11 May 2024 04:21:52 +0900 Subject: [PATCH 2/3] add filterParams tests --- tests/agentFilters/test_filter_params.ts | 102 +++++++++++++++++++++-- 1 file changed, 94 insertions(+), 8 deletions(-) diff --git a/tests/agentFilters/test_filter_params.ts b/tests/agentFilters/test_filter_params.ts index eceb5d312..01a93c4e3 100644 --- a/tests/agentFilters/test_filter_params.ts +++ b/tests/agentFilters/test_filter_params.ts @@ -7,7 +7,6 @@ import test from "node:test"; import assert from "node:assert"; const httpAgentFilter: AgentFilterFunction = async (context, next) => { - console.log(context.filterParams); return next(context); }; @@ -36,7 +35,7 @@ test("test filterParams on agent filter", async () => { agent: httpAgentFilter, filterParams: { agentServer: { - baseUrl: "http://localhost:8085/agents/", + baseUrl: "http://localhost:8085/agentFilters/", stream: true, }, }, @@ -47,11 +46,48 @@ test("test filterParams on agent filter", async () => { const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); const result = await graph.run(); - console.log(JSON.stringify(result)); - assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8085/agents/", stream: true } }] }); + // console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8085/agentFilters/", stream: true } }] }); +}); + +test("test filterParams on node", async () => { + const graph_data = { + version: 0.3, + nodes: { + echo: { + agent: "echoAgent", + params: { + filterParams: true, + }, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8081/nodeParameter/", + }, + }, + }, + bypassAgent: { + agent: "bypassAgent", + inputs: [":echo"], + isResult: true, + }, + }, + }; + const agentFilters = [ + { + name: "httpAgentFilter", + agent: httpAgentFilter, + agentIds: ["echoAgent"], + }, + ]; + + const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); + + const result = await graph.run(); + // console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/nodeParameter/" } }] }); }); -test("test filterParams on agent filter2", async () => { +test("test filterParams on agent filter and node. Then node.ts use filterParams on node", async () => { const graph_data = { version: 0.3, nodes: { @@ -62,7 +98,7 @@ test("test filterParams on agent filter2", async () => { }, filterParams: { agentServer: { - baseUrl: "http://localhost:8081/agents/", + baseUrl: "http://localhost:8081/nodeParameter/", }, }, }, @@ -79,7 +115,7 @@ test("test filterParams on agent filter2", async () => { agent: httpAgentFilter, filterParams: { agentServer: { - baseUrl: "http://localhost:8085/agents/", + baseUrl: "http://localhost:8085/agentFilters/", stream: true, }, }, @@ -91,5 +127,55 @@ test("test filterParams on agent filter2", async () => { const result = await graph.run(); console.log(JSON.stringify(result)); - assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/agents/", stream: true } }] }); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/nodeParameter/" } }] }); +}); + +test("test filterParams on each agent", async () => { + const graph_data = { + version: 0.3, + nodes: { + echo: { + agent: "echoAgent", + params: { + filterParams: true, + }, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8081/nodeParameter/", + }, + }, + }, + echo2: { + agent: "echoAgent", + params: { + filterParams: true, + }, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8081/nodeParameter2/", + }, + }, + }, + bypassAgent: { + agent: "bypassAgent", + inputs: [":echo", ":echo2"], + isResult: true, + }, + }, + }; + const agentFilters = [ + { + name: "httpAgentFilter", + agent: httpAgentFilter, + agentIds: ["echoAgent"], + }, + ]; + + const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); + + const result = await graph.run(); + // console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { + bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/nodeParameter/" } }, { agentServer: { baseUrl: "http://localhost:8081/nodeParameter2/" } }], + }); }); From 97e98f19a9da805649c783c665be89489bbe6c42 Mon Sep 17 00:00:00 2001 From: isamu Date: Sat, 11 May 2024 04:25:45 +0900 Subject: [PATCH 3/3] more test --- tests/agentFilters/test_filter_params.ts | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/agentFilters/test_filter_params.ts b/tests/agentFilters/test_filter_params.ts index 01a93c4e3..de2e81d01 100644 --- a/tests/agentFilters/test_filter_params.ts +++ b/tests/agentFilters/test_filter_params.ts @@ -179,3 +179,53 @@ test("test filterParams on each agent", async () => { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8081/nodeParameter/" } }, { agentServer: { baseUrl: "http://localhost:8081/nodeParameter2/" } }], }); }); + + +test("test filterParams on agent filter", async () => { + const graph_data = { + version: 0.3, + nodes: { + echo: { + agent: "echoAgent", + params: { + filterParams: true, + }, + }, + bypassAgent: { + agent: "bypassAgent", + inputs: [":echo"], + isResult: true, + }, + }, + }; + const agentFilters = [ + { + name: "httpAgentFilter", + agent: httpAgentFilter, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8085/agentFilters/", + stream: true, + }, + }, + agentIds: ["echoAgent"], + }, + { + name: "httpAgentFilter", + agent: httpAgentFilter, + filterParams: { + agentServer: { + baseUrl: "http://localhost:8085/agentFilters2/", + stream: true, + }, + }, + agentIds: ["echoAgent"], + }, + ]; + + const graph = new GraphAI({ ...graph_data }, { ...defaultTestAgents, ...callbackDictonary }, { agentFilters }); + + const result = await graph.run(); + // console.log(JSON.stringify(result)); + assert.deepStrictEqual(result, { bypassAgent: [{ agentServer: { baseUrl: "http://localhost:8085/agentFilters/", stream: true } }] }); +});