GenkitG
Genkit14mo ago
1 reply
urgent-maroon

Typed call flow utility

Bringing a side thread here.. I'm trying to create a utility for mounting and calling flows in a Next.js API route. I have a flow chat

export const chat = ai.defineFlow({
    name: "chat",
    inputSchema: ChatRequestSchema,
    outputSchema: z.string(),
}, async (input: ChatRequest): Promise<string> => {
    const chat = ai.chat({
        system,
        messages: input.history.map(h => { return { role: h.sender, content: [ { text: h.message }]}; }),
    });
    const response = await chat.send(input.query);
    console.log("Chat response is ", JSON.stringify(response, null, 2));
    return response.text;
});


I have a utility method callFlow with the following signature:

type FlowInput<F> = F extends CallableFlow<infer Input, any> ? z.infer<Input> : never;
type FlowOutput<F> = F extends CallableFlow<any, infer Output> ? z.infer<Output> : never;

export async function callFlow<Flow extends CallableFlow>(path: string, input: FlowInput<Flow>): Promise<FlowOutput<Flow>>;


But when I try to call it with

const text = await callFlow<typeof chat>("/api/chat", { history: original, query: message });


I get an error that typeof chat cannot be coerced because "The types of 'flow.authPolicy' are incompatible between these types"
Was this page helpful?