mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 08:36:01 +00:00
public commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { type Static, transformObject } from "core/utils";
|
||||
import { Flow, HttpTrigger } from "flows";
|
||||
import { Hono } from "hono";
|
||||
import { Module } from "modules/Module";
|
||||
import { TASKS, flowsConfigSchema } from "./flows-schema";
|
||||
|
||||
export type AppFlowsSchema = Static<typeof flowsConfigSchema>;
|
||||
export type TAppFlowSchema = AppFlowsSchema["flows"][number];
|
||||
export type TAppFlowTriggerSchema = TAppFlowSchema["trigger"];
|
||||
export type { TAppFlowTaskSchema } from "./flows-schema";
|
||||
|
||||
export class AppFlows extends Module<typeof flowsConfigSchema> {
|
||||
private flows: Record<string, Flow> = {};
|
||||
|
||||
override async build() {
|
||||
//console.log("building flows", this.config);
|
||||
const flows = transformObject(this.config.flows, (flowConfig, name) => {
|
||||
return Flow.fromObject(name, flowConfig as any, TASKS);
|
||||
});
|
||||
|
||||
this.flows = flows;
|
||||
|
||||
const hono = new Hono();
|
||||
|
||||
hono.get("/", async (c) => {
|
||||
const flowsInfo = transformObject(this.flows, (flow) => this.getFlowInfo(flow));
|
||||
return c.json(flowsInfo);
|
||||
});
|
||||
|
||||
hono.get("/flow/:name", async (c) => {
|
||||
const name = c.req.param("name");
|
||||
return c.json(this.flows[name]?.toJSON());
|
||||
});
|
||||
|
||||
hono.get("/flow/:name/run", async (c) => {
|
||||
const name = c.req.param("name");
|
||||
const flow = this.flows[name]!;
|
||||
const execution = flow.createExecution();
|
||||
|
||||
const start = performance.now();
|
||||
await execution.start();
|
||||
const time = performance.now() - start;
|
||||
const errors = execution.getErrors();
|
||||
return c.json({
|
||||
success: errors.length === 0,
|
||||
time,
|
||||
errors,
|
||||
response: execution.getResponse(),
|
||||
flow: this.getFlowInfo(flow),
|
||||
logs: execution.logs
|
||||
});
|
||||
});
|
||||
|
||||
this.ctx.server.route(this.config.basepath, hono);
|
||||
|
||||
// register flows
|
||||
for (const [name, flow] of Object.entries(this.flows)) {
|
||||
const trigger = flow.trigger;
|
||||
|
||||
switch (true) {
|
||||
case trigger instanceof HttpTrigger:
|
||||
await trigger.register(flow, this.ctx.server);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.setBuilt();
|
||||
}
|
||||
|
||||
getSchema() {
|
||||
return flowsConfigSchema;
|
||||
}
|
||||
|
||||
private getFlowInfo(flow: Flow) {
|
||||
return {
|
||||
...flow.toJSON(),
|
||||
tasks: flow.tasks.length,
|
||||
connections: flow.connections
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user