Skip to content

Commit

Permalink
iron out some kinks (TheFireCo#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
debkanchan authored Aug 10, 2024
1 parent d90543a commit 481c24c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions plugins/graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function defineGraph<
async (input, streamingCallback) => {
let { state, nextNode } = await entrypoint(input);

let currentNode = nextNode;
let currentNode: string = nextNode;

while (true) {
if (!nodes[currentNode]) {
Expand All @@ -140,24 +140,27 @@ export function defineGraph<
}

const result = await output();
let parseResult = config.outputSchema!.safeParse(result);

if (parseResult.success) {
await beforeFinish?.(state, result);

return result;
}

parseResult = config.stateSchema!.safeParse(result);
let parseResult = StateReturnSchema(
config.stateSchema ?? z.any()
).safeParse(result);

if (parseResult.success) {
state = (result as z.infer<StateReturnSchema<StateSchema>>).state!;
state = (result as z.infer<StateReturnSchema<StateSchema>>).state;
currentNode = (result as z.infer<StateReturnSchema<StateSchema>>)
.nextNode;
continue;
}

parseResult = (config.outputSchema ?? z.any()).safeParse(result);

if (parseResult.success) {
await beforeFinish?.(state, result);

return result;
} else {
throw new Error(
'Invalid output: Output does not satisfy stateSchema or outputSchema'
`Invalid output: Output of node ${currentNode} does not satisfy StateSchema or OutputSchema`
);
}
}
Expand Down

0 comments on commit 481c24c

Please sign in to comment.