Skip to content

Commit

Permalink
dynamic routes and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnrushefsky committed Aug 15, 2024
1 parent 03d08d8 commit fe3867f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
85 changes: 41 additions & 44 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import {
PromptResponseSchema,
PromptRequest,
Workflow,
WorkflowRequestSchema,
WorkflowRequest,
} from "./types";
import { workflows } from "./workflows";
import { z } from "zod";
import { randomUUID } from "crypto";

const outputWatcher = new DirectoryWatcher(config.outputDir);

Expand Down Expand Up @@ -223,55 +222,53 @@ server.after(() => {
}
);

app.post<{
Params: { base_model: string; workflow_id: string };
Body: WorkflowRequest;
}>(
"/workflow/:base_model/:workflow_id",
{
schema: {
body: WorkflowRequestSchema,
params: z.object({
base_model: z.string(),
workflow_id: z.string(),
}),
},
},
async (request, reply) => {
const { base_model, workflow_id } = request.params;
if (!(workflows as any)[base_model]) {
return reply.code(404).send({ error: "Base model not found" });
}

if (!(workflows as any)[base_model][workflow_id]) {
return reply.code(404).send({ error: "Workflow not found" });
}

const workflow = (workflows as any)[base_model][workflow_id] as Workflow;
for (const baseModel in workflows) {
for (const workflowId in workflows[baseModel]) {
const workflow = workflows[baseModel][workflowId] as Workflow;
server.log.info(`Registering workflow ${baseModel}/${workflowId}`);
const BodySchema = z.object({
id: z
.string()
.optional()
.default(() => randomUUID()),
input: workflow.RequestSchema,
webhook: z.string().optional(),
});

const { id, workflow: input, webhook } = request.body;
type BodyType = z.infer<typeof BodySchema>;

const { success, data, error } = workflow.RequestSchema.safeParse(input);
if (!success) {
app.log.error(`Failed to validate input: ${JSON.stringify(data)}`);
return reply.code(400).send({ error: error.errors });
}

const prompt = workflow.generateWorkflow(data);

const resp = await fetch(
`http://localhost:${config.wrapperPort}/prompt`,
app.post<{
Body: BodyType;
}>(
`/workflow/${baseModel}/${workflowId}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
schema: {
body: BodySchema,
response: {
200: PromptResponseSchema,
202: PromptResponseSchema,
},
},
body: JSON.stringify({ prompt, id, webhook }),
},
async (request, reply) => {
const { id, input, webhook } = request.body;
const prompt = workflow.generateWorkflow(input);

const resp = await fetch(
`http://localhost:${config.wrapperPort}/prompt`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ prompt, id, webhook }),
}
);
return reply.code(resp.status).send(await resp.json());
}
);
return reply.code(resp.status).send(await resp.json());
}
);
}
});

let warm = false;
Expand Down
4 changes: 2 additions & 2 deletions src/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fluxTxt2img from "../workflows/flux/txt2img";

export const workflows = {
export const workflows: any = {
flux: {
txt2img: fluxTxt2img,
},
} as const;
};

0 comments on commit fe3867f

Please sign in to comment.