Skip to content

Commit

Permalink
Graph hotfix (#125)
Browse files Browse the repository at this point in the history
* export state return schema

* properly build FlowOutptSchema
  • Loading branch information
debkanchan authored Aug 9, 2024
1 parent 12a0f1b commit 392ef3f
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions plugins/graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,37 @@ import { defineFlow, streamFlow, Flow, FlowAuthPolicy } from '@genkit-ai/flow';
import * as express from 'express';
import * as z from 'zod';

const StateReturnSchema = <T extends z.ZodTypeAny>(stateSchema: T) => {
export const StateReturnSchema = <S extends z.ZodTypeAny>(stateSchema: S) => {
return z.object({
state: stateSchema,
nextNode: z.string(),
});
};
type StateReturnSchema<T extends z.ZodTypeAny> = ReturnType<
typeof StateReturnSchema<T>

type StateReturnSchema<S extends z.ZodTypeAny> = ReturnType<
typeof StateReturnSchema<S>
>;

export const FlowOutputSchema = <
S extends z.ZodTypeAny,
O extends z.ZodTypeAny,
>(
stateSchema: S,
outputSchema: O
) => {
return z
.object({
state: stateSchema,
nextNode: z.string(),
})
.or(outputSchema);
};

type FlowOutputSchema<
S extends z.ZodTypeAny,
O extends z.ZodTypeAny,
> = ReturnType<typeof FlowOutputSchema<S, O>>;

export function defineGraph<
StateSchema extends z.ZodTypeAny = z.ZodTypeAny,
InputSchema extends z.ZodTypeAny = z.ZodTypeAny,
Expand Down Expand Up @@ -58,25 +79,21 @@ export function defineGraph<
addNode: (
flow: Flow<
StateSchema,
StateReturnSchema<StateSchema> | OutputSchema,
FlowOutputSchema<StateSchema, OutputSchema>,
StreamSchema
>
) => void;
removeNode: (name: string) => void;
} {
const nodes: Record<
string,
Flow<
StateSchema,
StateReturnSchema<StateSchema> | OutputSchema,
StreamSchema
>
Flow<StateSchema, FlowOutputSchema<StateSchema, OutputSchema>, StreamSchema>
> = {};

const addNode = (
flow: Flow<
StateSchema,
StateReturnSchema<StateSchema> | OutputSchema,
FlowOutputSchema<StateSchema, OutputSchema>,
StreamSchema
>
) => {
Expand Down

0 comments on commit 392ef3f

Please sign in to comment.