Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Protofy-xyz/Protofy
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0k4 committed Nov 18, 2024
2 parents f1fcb31 + c8766e7 commit 4adad80
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
75 changes: 54 additions & 21 deletions packages/protofy/src/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,44 @@ const AgentProtocolSchema = z.object({
}).catchall(z.any()).optional(), // Allow any other properties in config
});

const AgentInterfaceSchema = z.object({
const AgentIOSchema = z.object({
shape: z.custom<SchemaObject>((value) => {
// Use Ajv to validate the value
return validateSchemaObject(value);
}, "SchemaObject"),
protocol: AgentProtocolSchema.optional()
})

const AgentInterfaceSchema = z.object({
protocol: AgentProtocolSchema.optional(),
input: AgentIOSchema.optional(),
output: AgentIOSchema.optional()
})

const AgentSchema = z.object({
id: z.string(), // Required string
name: z.string().optional(), // Optional string
description: z.string().optional(), // Optional string
tags: z.array(z.string()).optional(), // Optional array of strings
protocol: AgentProtocolSchema.optional(),
input: AgentInterfaceSchema.optional(),
output: AgentInterfaceSchema.optional()
children: z.array(z.lazy(() => AgentSchema)).optional(), // Optional array of agents
interface: AgentInterfaceSchema.optional() // Optional interface
})

// Infer the TypeScript type
export type AgentData = z.infer<typeof AgentSchema>;
export type AgentProtocolData = z.infer<typeof AgentProtocolSchema>;
export type AgentIOData = z.infer<typeof AgentIOSchema>;
export type AgentInterfaceData = z.infer<typeof AgentInterfaceSchema>;

export class Agent {
data: AgentData;
children: Agent[];
input: AgentInterface | undefined;
output: AgentInterface | undefined;
interface: AgentInterface | undefined;
parent: Agent | undefined;
constructor(data: AgentData, children: Agent[] = [], parent: Agent = undefined, ) {
constructor(data: AgentData, parent: Agent = undefined, ) {
this.data = data;
this.children = children;
this.input = data.input && new AgentInputInterface(data.input, this);
this.output = data.output && new AgentOutputInterface(data.output, this);
this.children = data.children && data.children.map(agent => new Agent(agent, this)) || [];
this.interface = data.interface && new AgentInterface(data.interface, this);
this.parent = parent;
}

Expand All @@ -70,29 +74,30 @@ export class Agent {
}

getProtocol() {
const protocol = this.interface?.getProtocol()
if(this.parent) {
return {
...this.parent.getProtocol(),
...this.data.protocol
...protocol
}
}
return this.data.protocol
return protocol
}

getInputShape() {
return this.input?.getShape()
return this.interface?.getInput()?.getShape()
}

getInputProtocol() {
return this.input?.getProtocol()
return this.interface?.getInput()?.getProtocol()
}

getOutputShape() {
return this.output?.getShape()
return this.interface?.getOutput()?.getShape()
}

getOutputProtocol() {
return this.output?.getProtocol()
return this.interface?.getOutput()?.getProtocol()
}

addChildren(agents: Agent[]) {
Expand All @@ -104,6 +109,10 @@ export class Agent {
return this.children;
}

getInterface() {
return this.interface
}

addChild(agent: Agent) {
agent.setParent(this);
this.children.push(agent);
Expand All @@ -119,10 +128,34 @@ export class Agent {
}

export class AgentInterface {
input: AgentIOInterface;
output: AgentIOInterface;
protocol: AgentProtocolData;

constructor(data: AgentInterfaceData, agent: Agent) {
this.input = data.input && new AgentInputInterface(data.input, agent);
this.output = data.output && new AgentOutputInterface(data.output, agent);
this.protocol = data.protocol;
}

getProtocol() {
return this.protocol
}

getInput() {
return this.input
}

getOutput() {
return this.output
}
}

export class AgentIOInterface {
shape: SchemaObject;
protocol: AgentProtocolData;
agent: Agent;
constructor(data: AgentInterfaceData, agent: Agent) {
constructor(data: AgentIOData, agent: Agent) {
this.shape = data.shape;
this.protocol = data.protocol;
this.agent = agent;
Expand All @@ -143,14 +176,14 @@ export class AgentInterface {
}
}

export class AgentInputInterface extends AgentInterface {
constructor(data: AgentInterfaceData, agent: Agent) {
export class AgentInputInterface extends AgentIOInterface {
constructor(data: AgentIOData, agent: Agent) {
super(data, agent);
}
}

export class AgentOutputInterface extends AgentInterface {
constructor(data: AgentInterfaceData, agent: Agent) {
export class AgentOutputInterface extends AgentIOInterface {
constructor(data: AgentIOData, agent: Agent) {
super(data, agent);
}
}
5 changes: 2 additions & 3 deletions packages/protofy/src/protocols/function.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AgentInterface } from "../Agent";
import { AgentIOInterface } from "../Agent";

//function runner
export default (interf: AgentInterface) => {
export default (interf: AgentIOInterface) => {
//check if the interface is a function
if(interf.getProtocol().type !== 'function') {
throw new Error('Error: Invalid protocol type, expected function')
}




}
30 changes: 16 additions & 14 deletions packages/protofy/tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ describe('Agents basic behavior', () => {
name: 'getDisplayInfo',
description: 'Get display info of a user',
tags: ['user', 'display'],
protocol: {
type: 'function',
config: {
fn: (user) => {
return user.name + ', ' + user.age
}
}
},
input: {
shape: zodToJsonSchema(userSchema, "user"),
interface: {
protocol: {
config: {test: 'test'}
type: 'function',
config: {
fn: (user) => {
return user.name + ', ' + user.age
}
}
},
input: {
shape: zodToJsonSchema(userSchema, "user"),
protocol: {
config: {test: 'test'}
}
},
output: {
shape: zodToJsonSchema(returnSchema, "displayInfo")
}
},
output: {
shape: zodToJsonSchema(returnSchema, "displayInfo")
}
})
});
Expand Down

0 comments on commit 4adad80

Please sign in to comment.