Hello World
import {
AgentFactory,
AgentNetwork,
AgentNetworkEvent,
NextEndpoint,
S,
} from '@m4trix/core/matrix';
// 1. Define events
const requestEvent = AgentNetworkEvent.of(
'user-request',
S.Struct({ query: S.String }),
);
const responseEvent = AgentNetworkEvent.of(
'agent-response',
S.Struct({ answer: S.String, done: S.Boolean }),
);
// 2. Create an agent
const myAgent = AgentFactory.run()
.listensTo([requestEvent])
.emits([responseEvent])
.logic(async ({ triggerEvent, emit }) => {
emit({
name: 'agent-response',
payload: {
answer: `You asked: ${triggerEvent.payload.query}`,
done: true,
},
});
})
.produce({});
// 3. Wire the network
const network = AgentNetwork.setup(
({ mainChannel, createChannel, sink, registerAgent }) => {
const main = mainChannel('main');
const client = createChannel('client').sink(sink.httpStream());
registerAgent(myAgent).subscribe(main).publishTo(client);
},
);
// 4. Expose as an API
const api = network.expose({
protocol: 'sse',
select: { channels: 'client' },
startEventName: 'user-request',
});
export const GET = NextEndpoint.from(api).handler();
export const POST = NextEndpoint.from(api).handler();What This Does
Next
Last updated