Skip to content

Commit

Permalink
Adds function runner to agent network
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarlosn committed Nov 20, 2024
1 parent 696438b commit 82d8890
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/protofy/src/protocols/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Agent } from "../Agent";

//function runner
export default (agent: Agent) => {
export default (agent: Agent, params) => {
//check if the interface is a function
if(agent.getProtocol().type !== 'function') {
throw new Error('Error: Invalid protocol type, expected function')
Expand All @@ -16,5 +16,8 @@ export default (agent: Agent) => {
throw new Error('Error: Invalid function, expected function')
}


//call the function
return agent.getProtocol().config.fn(
...(Array.isArray(params) ? params : [params])
);
}
58 changes: 58 additions & 0 deletions packages/protofy/tests/function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Agent } from '../src/Agent';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import functionRunner from '../src/protocols/function';

let paramsSchema;
let returnSchema: z.ZodString;
let agent: Agent;
let childAgent: Agent;

describe('Function Agents', () => {
beforeEach(() => {
paramsSchema = z.tuple([z.object({
id: z.string().uuid(),
name: z.string().min(1),
age: z.number().min(18),
email: z.string().email(),
})]);

returnSchema = z.string()

agent = new Agent({
id: 'getDisplayInfo',
name: 'getDisplayInfo',
description: 'Get display info of a user',
tags: ['user', 'display'],
interface: {
protocol: {
type: 'function',
config: {
fn: (user) => {
return user.name + ', ' + user.age
}
}
},
input: {
shape: zodToJsonSchema(paramsSchema, "params"),
protocol: {
config: {test: 'test'}
}
},
output: {
shape: zodToJsonSchema(returnSchema, "displayInfo")
}
}
})
});

it('Should be able to call the function and get the result back', () => {
expect(functionRunner(agent, {
id: 1,
name: 'John Doe',
age: 30,
email: 'a@a.com'
})).toBe('John Doe, 30');
});

});

0 comments on commit 82d8890

Please sign in to comment.