-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.ts
More file actions
82 lines (66 loc) · 3.39 KB
/
api.ts
File metadata and controls
82 lines (66 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {FastifyInstance, FastifyReply} from "fastify";
import { FastifySSEPlugin} from "fastify-sse-v2";
import {sendHtml} from "./htmx";
import {delayAsync, filterEventAsync, mapAsync} from "./stream";
import {getOrCreateWorkflow} from "./agents/agent-store";
export function routes(fastify: FastifyInstance) {
fastify.register(FastifySSEPlugin);
fastify.register(import('@fastify/formbody'))
fastify.get('/', async function handler(_, reply) {
reply.redirect('/agents/agent-catalog/index');
})
fastify.get('/agents', async function handler(_, reply) {
reply.redirect('/agents/agent-catalog/index');
})
fastify.get('/agents/:agent', async function handler(request, reply: FastifyReply) {
const {agent} = request.params as { agent: string };
reply.redirect(`/agents/${agent}/${generateActorId()}`);
function generateActorId() {
return Math.random().toString(36).substring(2, 8);
}
})
fastify.get('/agents/:agent/:workflow', async function handler(request, reply: FastifyReply) {
const {agent, workflow} = request.params as { agent: string, workflow: string };
const actor = await getOrCreateWorkflow(agent, workflow);
const {service, hub} = actor.getSnapshot().context;
if (request.headers.accept === 'text/event-stream') {
service.start();
return reply.sse(delayAsync(mapAsync(hub.emitted, ({data,event, type}) => ({
data: data,
event: event || type
}))));
}
sendHtml(reply, agent,workflow);
})
fastify.get('/agents/:agent/:workflow/events', async function handler(request, reply: FastifyReply) {
const {agent, workflow} = request.params as { agent: string, workflow: string };
const service = await getOrCreateWorkflow(agent, workflow);
const { hub} = service.getSnapshot().context;
return reply.sse((hub.emitted))
})
fastify.get('/agents/:agent/:workflow/events/:event', async function handler(request, reply: FastifyReply) {
const {agent, workflow, event} = request.params as { agent: string, workflow: string, event: string };
const service = await getOrCreateWorkflow(agent, workflow);
const { hub} = service.getSnapshot().context;
return reply.sse(filterEventAsync(hub.emitted, event))
})
fastify.post('/agents/:agent/:workflow/events/:event', async function handler(request, reply: FastifyReply) {
const {agent, workflow, event} = request.params as { agent: string, workflow: string, event:string };
const data = request.body as object;
const actorRef = await getOrCreateWorkflow(agent, workflow);
const {service} = actorRef.getSnapshot().context;
service.send({type: event, ...data});
return reply.send('sent at '+ new Date().toISOString());
})
fastify.get('/agents/:agent/:workflow/:service/events/:event', async function handler(request, reply: FastifyReply) {
const {agent, workflow, service, event} = request.params as {
agent: string,
workflow: string,
event: string,
service: string
};
const actor = await getOrCreateWorkflow(agent, workflow);
const hub=actor.getSnapshot().context.hub;
return reply.sse(filterEventAsync((hub.emitted), `@${service}.${event}`))
})
}