Skip to content

Commit

Permalink
Merge pull request #858 from isamu/addConfigTest
Browse files Browse the repository at this point in the history
Add config test
  • Loading branch information
snakajima authored Dec 29, 2024
2 parents a2d4b91 + bee513d commit 9a820bf
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/graphai/lib/bundle.cjs.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/graphai/lib/bundle.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/graphai/lib/bundle.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/graphai/lib/bundle.umd.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/graphai/lib/graphai.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AgentFunctionInfoDictionary, AgentFilterInfo, GraphData, DataSource, ResultDataDictionary, ResultData, DefaultResultData, GraphOptions, PropFunction, GraphDataLoader } from "./type";
import { AgentFunctionInfoDictionary, AgentFilterInfo, GraphData, DataSource, ResultDataDictionary, ResultData, DefaultResultData, GraphOptions, PropFunction, GraphDataLoader, ConfigDataDictionary } from "./type";
import { TransactionLog } from "./transaction_log";
import { ComputedNode, GraphNodes } from "./node";
import { TaskManager } from "./task_manager";
Expand All @@ -11,7 +11,7 @@ export declare class GraphAI {
private readonly loop?;
private readonly logs;
readonly bypassAgentIds: string[];
readonly config?: Record<string, unknown>;
readonly config?: ConfigDataDictionary;
readonly agentFunctionInfoDictionary: AgentFunctionInfoDictionary;
readonly taskManager: TaskManager;
readonly agentFilters: AgentFilterInfo[];
Expand Down
3 changes: 1 addition & 2 deletions packages/graphai/lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ class ComputedNode extends Node {
const agent = data.agent;
this.agentFunction = async ({ namedInputs, params }) => agent(namedInputs, params);
}
this.config = this.agentId ? (this.graph.config ?? {})[this.agentId] ?? {} : {},
this.anyInput = data.anyInput ?? false;
(this.config = this.agentId ? ((this.graph.config ?? {})[this.agentId] ?? {}) : {}), (this.anyInput = data.anyInput ?? false);
this.inputs = data.inputs;
this.output = data.output;
this.dataSources = [
Expand Down
15 changes: 9 additions & 6 deletions packages/graphai/lib/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export declare enum NodeState {
}
export type DefaultResultData = Record<string, any> | string | number | boolean | Array<DefaultResultData>;
export type DefaultInputData = Record<string, any>;
export type DefaultConfigData = Record<string, any>;
export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
export type ResultDataDictionary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;
export type ConfigData<ConfigType = DefaultConfigData> = ConfigType;
export type ConfigDataDictionary<ConfigType = DefaultConfigData> = Record<string, ConfigType>;
export type DefaultParamsType = Record<string, any>;
export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType;
export type PassThrough = Record<string, any>;
Expand Down Expand Up @@ -78,11 +81,11 @@ export type GraphOptions = {
agentFilters?: AgentFilterInfo[] | undefined;
taskManager?: TaskManager | undefined;
bypassAgentIds?: string[] | undefined;
config?: Record<string, unknown>;
config?: ConfigDataDictionary;
graphLoader?: GraphDataLoader;
};
export type CacheTypes = "pureAgent" | "impureAgent";
export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataType = DefaultInputData> = {
export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataType = DefaultInputData, ConfigType = DefaultConfigData> = {
params: NodeDataParams<ParamsType>;
inputSchema?: any;
namedInputs: NamedInputDataType;
Expand All @@ -104,9 +107,9 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataT
filterParams: AgentFilterParams;
agentFilters?: AgentFilterInfo[];
log?: TransactionLog[];
config?: Record<string, unknown>;
config?: ConfigType;
};
export type AgentFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, NamedInputDataType = DefaultInputData> = (context: AgentFunctionContext<ParamsType, NamedInputDataType>) => Promise<ResultData<ResultType>>;
export type AgentFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, NamedInputDataType = DefaultInputData, ConfigType = DefaultConfigData> = (context: AgentFunctionContext<ParamsType, NamedInputDataType, ConfigType>) => Promise<ResultData<ResultType>>;
export type AgentFilterFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, NamedInputDataType = DefaultInputData> = (context: AgentFunctionContext<ParamsType, NamedInputDataType>, agent: AgentFunction) => Promise<ResultData<ResultType>>;
export type AgentFilterInfo = {
name: string;
Expand All @@ -123,8 +126,8 @@ export type AgentFunctionInfoSample = {
};
export type AgentFunctionInfo = {
name: string;
agent: AgentFunction<any, any, any>;
mock: AgentFunction<any, any, any>;
agent: AgentFunction<any, any, any, any>;
mock: AgentFunction<any, any, any, any>;
inputs?: any;
output?: any;
outputFormat?: any;
Expand Down
3 changes: 2 additions & 1 deletion packages/graphai/src/graphai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GraphOptions,
PropFunction,
GraphDataLoader,
ConfigDataDictionary,
} from "@/type";
import { TransactionLog } from "@/transaction_log";

Expand All @@ -33,7 +34,7 @@ export class GraphAI {
private readonly loop?: LoopData;
private readonly logs: Array<TransactionLog> = [];
public readonly bypassAgentIds: string[];
public readonly config?: Record<string, unknown> = {};
public readonly config?: ConfigDataDictionary = {};
public readonly agentFunctionInfoDictionary: AgentFunctionInfoDictionary;
public readonly taskManager: TaskManager;
public readonly agentFilters: AgentFilterInfo[];
Expand Down
11 changes: 7 additions & 4 deletions packages/graphai/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
DefaultInputData,
PassThrough,
ConsoleElement,
ConfigData,
} from "@/type";
import { parseNodeName, assert, isLogicallyTrue, isObject } from "@/utils/utils";
import { TransactionLog } from "@/transaction_log";
Expand Down Expand Up @@ -78,7 +79,7 @@ export class ComputedNode extends Node {
public readonly params: NodeDataParams; // Agent-specific parameters
private readonly filterParams: AgentFilterParams;
public readonly nestedGraph?: GraphData | DataSource;
private readonly config?: any;
private readonly config?: ConfigData;
public readonly retryLimit: number;
public retryCount: number = 0;
private readonly agentId?: string;
Expand Down Expand Up @@ -121,7 +122,9 @@ export class ComputedNode extends Node {
const agent = data.agent;
this.agentFunction = async ({ namedInputs, params }) => agent(namedInputs, params);
}
(this.config = this.agentId ? ((this.graph.config ?? {})[this.agentId] ?? {}) : {}), (this.anyInput = data.anyInput ?? false);
this.config = this.agentId ? (data.graph ? this.graph.config : ((this.graph.config ?? {})[this.agentId] ?? {})) : {};

this.anyInput = data.anyInput ?? false;
this.inputs = data.inputs;
this.output = data.output;
this.dataSources = [
Expand Down Expand Up @@ -308,7 +311,7 @@ export class ComputedNode extends Node {
agentFilters: this.graph.agentFilters,
taskManager: this.graph.taskManager,
bypassAgentIds: this.graph.bypassAgentIds,
config: this.graph.config,
config: this.config,
graphLoader: this.graph.graphLoader,
},
onLogCallback: this.graph.onLogCallback,
Expand Down Expand Up @@ -390,7 +393,7 @@ export class ComputedNode extends Node {
cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.cacheType,
filterParams: this.filterParams,
agentFilters: this.graph.agentFilters,
config: this.graph.config,
config: this.config,
log: localLog,
};
return context;
Expand Down
22 changes: 14 additions & 8 deletions packages/graphai/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export enum NodeState {

export type DefaultResultData = Record<string, any> | string | number | boolean | Array<DefaultResultData>;
export type DefaultInputData = Record<string, any>;
export type DefaultConfigData = Record<string, any>;
export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
export type ResultDataDictionary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;

export type ConfigData<ConfigType = DefaultConfigData> = ConfigType;
export type ConfigDataDictionary<ConfigType = DefaultConfigData> = Record<string, ConfigType>;
export type DefaultParamsType = Record<string, any>;
export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType; // Agent-specific parameters

Expand Down Expand Up @@ -87,13 +90,13 @@ export type GraphOptions = {
agentFilters?: AgentFilterInfo[] | undefined;
taskManager?: TaskManager | undefined;
bypassAgentIds?: string[] | undefined;
config?: Record<string, unknown>;
config?: ConfigDataDictionary;
graphLoader?: GraphDataLoader;
};

export type CacheTypes = "pureAgent" | "impureAgent";

export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataType = DefaultInputData> = {
export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataType = DefaultInputData, ConfigType = DefaultConfigData> = {
params: NodeDataParams<ParamsType>;
inputSchema?: any;
namedInputs: NamedInputDataType;
Expand All @@ -115,12 +118,15 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataT
filterParams: AgentFilterParams; // agent filter
agentFilters?: AgentFilterInfo[]; // TODO remove next version
log?: TransactionLog[];
config?: Record<string, unknown>;
config?: ConfigType;
};

export type AgentFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, NamedInputDataType = DefaultInputData> = (
context: AgentFunctionContext<ParamsType, NamedInputDataType>,
) => Promise<ResultData<ResultType>>;
export type AgentFunction<
ParamsType = DefaultParamsType,
ResultType = DefaultResultData,
NamedInputDataType = DefaultInputData,
ConfigType = DefaultConfigData,
> = (context: AgentFunctionContext<ParamsType, NamedInputDataType, ConfigType>) => Promise<ResultData<ResultType>>;

export type AgentFilterFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, NamedInputDataType = DefaultInputData> = (
context: AgentFunctionContext<ParamsType, NamedInputDataType>,
Expand All @@ -144,8 +150,8 @@ export type AgentFunctionInfoSample = {

export type AgentFunctionInfo = {
name: string;
agent: AgentFunction<any, any, any>;
mock: AgentFunction<any, any, any>;
agent: AgentFunction<any, any, any, any>;
mock: AgentFunction<any, any, any, any>;
inputs?: any;
output?: any;
outputFormat?: any;
Expand Down
24 changes: 24 additions & 0 deletions packages/graphai/tests/test_agents/copy_config_agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AgentFunction, AgentFunctionInfo } from "@/index";

export const copyConfigAgent: AgentFunction<{ namedKey?: string }> = async ({ params, config }) => {
const { namedKey } = params;
if (namedKey && config) {
return config[namedKey];
}
return config;
};

// for test and document
const copyConfigAgentInfo: AgentFunctionInfo = {
name: "copyConfigAgent",
agent: copyConfigAgent,
mock: copyConfigAgent,
samples: [],
description: "copy config agent",
category: ["test"],
author: "Receptron team",
repository: "https://github.com/receptron/graphai",
license: "MIT",
};

export default copyConfigAgentInfo;
3 changes: 2 additions & 1 deletion packages/graphai/tests/test_agents/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import echoAgent from "./echo_agent";
import copyAgent from "./copy_agent";
import copyConfigAgent from "./copy_config_agent";
import streamMockAgent from "./stream_mock_agent";

import nestedAgent from "./nested_agent";
import mapAgent from "./map_agent";
import pushAgent from "./push_agent";

export { echoAgent, copyAgent, streamMockAgent, nestedAgent, mapAgent, pushAgent };
export { echoAgent, copyAgent, copyConfigAgent, streamMockAgent, nestedAgent, mapAgent, pushAgent };
4 changes: 2 additions & 2 deletions packages/graphai/tests/units/test_option_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test("test graphai config option", async () => {
const testAgent: AgentFunction = async ({ config }) => {
return config;
};
const config = { message: "hello" };
const config = { testAgent: { message: "hello" } };
const graph = new GraphAI(graph_data, { testAgent: agentInfoWrapper(testAgent) }, { config });
const result = await graph.run();
assert.deepStrictEqual(result, { result: { message: "hello" } });
Expand Down Expand Up @@ -67,7 +67,7 @@ test("test graphai nested config option", async () => {
const testAgent: AgentFunction = async ({ config }) => {
return config;
};
const config = { message: "hello config" };
const config = { testAgent: { message: "hello config" } };
const graph = new GraphAI(nested_graph_data, { testAgent: agentInfoWrapper(testAgent), ...testAgents }, { config });

const result = await graph.run();
Expand Down

0 comments on commit 9a820bf

Please sign in to comment.