Skip to content

Commit

Permalink
fix task/entrypoint func type (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns authored Jan 29, 2025
1 parent 6c80e18 commit 488f33f
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions libs/langgraph/src/func/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,39 @@ export type EntrypointFinalValueT<OutputT> = OutputT extends
? EntrypointFinal<ValueT, SaveT>
: OutputT;

/**
* Checks if an AsyncGenerator exists in the ES target/lib that we're compiling to.
*
* This is necessary because `tsc --init` targets ES2016 by default, which doesn't include AsyncGenerators.
*
* This works because when `skipLibCheck` is true (and it is in the default `tsconfig.json` created by `tsc --init`),
* TypeScript will replace any unresolved libary types with `any`. So, when `AsyncGenerator` doesn't exist, this checks
* if `any` extends `object`, which it doesn't. When that happens, this type resolves to the `false` literal, and we can
* use it in the type predicates below to skip over the AsyncGenerator-specific logic.
*
* If we didn't have this, then the types below would be checking if the user's function extends `any` in place of the
* `AsyncGenerator` type, and the type predicate would branch to `never`, disallowing any valid function from being passed
* to `task` or `entrypoint`.
*/
type AsyncGeneratorExists = AsyncGenerator<
unknown,
unknown,
unknown
> extends object
? true
: false;

/**
* Matches valid function signatures for entrypoints. Disallows generator functions.
*/
export type EntrypointFunc<InputT, OutputT> = [OutputT] extends never
? (input: InputT, config: LangGraphRunnableConfig) => never
: OutputT extends AsyncGenerator<unknown, unknown, unknown>
? never
: AsyncGeneratorExists extends true // only check if it may be an AsyncGenerator when those actually exist
? OutputT extends AsyncGenerator<unknown, unknown, unknown>
? never
: OutputT extends Generator<unknown, unknown, unknown>
? never
: (input: InputT, config: LangGraphRunnableConfig) => OutputT
: OutputT extends Generator<unknown, unknown, unknown>
? never
: (input: InputT, config: LangGraphRunnableConfig) => OutputT;
Expand All @@ -78,8 +104,12 @@ export type TaskFunc<ArgsT extends unknown[], OutputT> = [OutputT] extends [
never
]
? (...args: ArgsT) => never
: OutputT extends AsyncGenerator<unknown, unknown, unknown>
? never
: AsyncGeneratorExists extends true // only check if it may be an AsyncGenerator when those actually exist
? OutputT extends AsyncGenerator<unknown, unknown, unknown>
? never
: OutputT extends Generator<unknown, unknown, unknown>
? never
: (...args: ArgsT) => OutputT
: OutputT extends Generator<unknown, unknown, unknown>
? never
: (...args: ArgsT) => OutputT;

0 comments on commit 488f33f

Please sign in to comment.