In ZeroGraph, the node tasks are created by inheriting the Node
class implementation. Here are the steps to create a simple greeting node:
- Defining Node Classes::
- rewrite
prep
method prepares the input data (e.g., getting a name from a shared object). - exist
exec
Write core logic (e.g., generating greetings) in the - pass (a bill or inspection etc)
post
Store the results to a shared object.
class GreetingNode extends Node { prep(shared: any): string { return shared.name || 'World'; } exec(name: string): string { return `Hello, ${name}!`; } post(shared: any, execRes: string): void { shared.greeting = execRes; } }
- rewrite
- workflow::
- establish
Flow
instance and pass in the node, run it and output the result:const flow = new Flow(new GreetingNode()); const shared = { name: 'TypeScript' }; flow.run(shared); console.log(shared.greeting); // 输出: Hello, TypeScript!
- establish
This answer comes from the articleZeroGraph: a lightweight AI agent programming frameworkThe