Skip to content

Commit

Permalink
clean up EntrypointFinal type
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Jan 28, 2025
1 parent eaf1c85 commit bff76bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
5 changes: 1 addition & 4 deletions libs/langgraph/src/func/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
EntrypointFinal,
EntrypointReturnT,
EntrypointFuncSaveT,
finalSymbol,
EntrypointFunc,
} from "./types.js";
import { LangGraphRunnableConfig } from "../pregel/runnable_types.js";
Expand Down Expand Up @@ -163,9 +162,7 @@ entrypoint.final = function final<ValueT, SaveT>({
value?: ValueT;
save?: SaveT;
}): EntrypointFinal<ValueT, SaveT> {
return {
[finalSymbol]: { value, save },
};
return { value, save, __lg_type: "__pregel_final" };
};

/**
Expand Down
45 changes: 29 additions & 16 deletions libs/langgraph/src/func/types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { LangGraphRunnableConfig } from "../pregel/runnable_types.js";

export const finalSymbol = Symbol.for("__pregel_final");

/**
* Allows the entrypoint function to return a value to the caller, as well as a separate state value to persist to the checkpoint
*/
export type EntrypointFinal<ValueT, SaveT> = {
[finalSymbol]: {
/**
* The value to return to the caller
*/
value?: ValueT;

/**
* The value to save to the checkpoint
*/
save?: SaveT;
};
};
export class EntrypointFinal<ValueT, SaveT> {
/**
* The value to return to the caller
*/
value?: ValueT;

/**
* The value to save to the checkpoint
*/
save?: SaveT;

__lg_type: "__pregel_final";

constructor(value?: ValueT, save?: SaveT) {
this.value = value;
this.save = save;
}
}

/**
* Checks if a value is an EntrypointFinal - use this instead of `instanceof`, as value may have been deserialized
* @param value The value to check
* @returns Whether the value is an EntrypointFinal
*/
export function isEntrypointFinal<ValueT, SaveT>(
value: unknown
): value is EntrypointFinal<ValueT, SaveT> {
return typeof value === "object" && value !== null && finalSymbol in value;
return (
typeof value === "object" &&
value !== null &&
"__lg_type" in value &&
value.__lg_type === "__pregel_final"
);
}

export type EntrypointReturnT<OutputT> = OutputT extends
Expand Down
30 changes: 10 additions & 20 deletions libs/langgraph/src/pregel/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ import { ChannelWrite, PASSTHROUGH } from "./write.js";
import { RetryPolicy } from "./utils/index.js";
import { RunnableCallable, type RunnableCallableArgs } from "../utils.js";
import {
EntrypointFunc,
EntrypointReturnT,
finalSymbol,
isEntrypointFinal,
} from "../func/types.js";
import { LangGraphRunnableConfig } from "./runnable_types.js";
/**
* Wraps a user function in a Runnable.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getRunnableForFunc<FuncT extends (...args: any[]) => any>(
name: string,
func: FuncT
): Runnable<Parameters<FuncT>, ReturnType<FuncT>> {
export function getRunnableForFunc<
FuncT extends (...args: unknown[]) => unknown
>(name: string, func: FuncT): Runnable<Parameters<FuncT>, ReturnType<FuncT>> {
const run = new RunnableCallable<Parameters<FuncT>, ReturnType<FuncT>>({
func: (input: Parameters<FuncT>) => func(...input),
name,
Expand All @@ -47,16 +45,10 @@ export function getRunnableForFunc<FuncT extends (...args: any[]) => any>(

export function getRunnableForEntrypoint<InputT, OutputT>(
name: string,
func:
| ((
input: InputT,
config: LangGraphRunnableConfig
) => Promise<EntrypointReturnT<OutputT>>)
| ((input: InputT, config: LangGraphRunnableConfig) => OutputT)
func: EntrypointFunc<InputT, OutputT>
): Runnable<InputT, EntrypointReturnT<OutputT>, LangGraphRunnableConfig> {
const run = new RunnableCallable<InputT, EntrypointReturnT<OutputT>>({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
func: (input: any, config: LangGraphRunnableConfig) => {
func: (input: InputT, config: LangGraphRunnableConfig) => {
return func(input, config);
},
name,
Expand All @@ -74,8 +66,7 @@ export function getRunnableForEntrypoint<InputT, OutputT>(
channel: END,
value: PASSTHROUGH,
mapper: new RunnableCallable({
func: (value) =>
isEntrypointFinal(value) ? value[finalSymbol].value : value,
func: (value) => (isEntrypointFinal(value) ? value.value : value),
}),
},
],
Expand All @@ -87,15 +78,15 @@ export function getRunnableForEntrypoint<InputT, OutputT>(
value: PASSTHROUGH,
mapper: new RunnableCallable({
func: (value) => {
return isEntrypointFinal(value) ? value[finalSymbol].save : value;
return isEntrypointFinal(value) ? value.save : value;
},
}),
},
]),
],
last: new RunnableCallable({
func: (final: EntrypointReturnT<typeof func>) =>
isEntrypointFinal(final) ? final[finalSymbol].value : final,
isEntrypointFinal(final) ? final.value : final,
}),
});
}
Expand All @@ -107,8 +98,7 @@ export type CallWrapperOptions<FuncT extends (...args: unknown[]) => unknown> =
retry?: RetryPolicy;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function call<FuncT extends (...args: any[]) => any>(
export function call<FuncT extends (...args: unknown[]) => unknown>(
{ func, name, retry }: CallWrapperOptions<FuncT>,
...args: Parameters<FuncT>
): Promise<ReturnType<FuncT>> {
Expand Down

0 comments on commit bff76bd

Please sign in to comment.