Skip to content

Commit a6f56a1

Browse files
authored
chore(langgraph): allow generic node name and arguments for Command / Send (#1234)
2 parents 95d43aa + 2763e52 commit a6f56a1

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

libs/langgraph/src/constants.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ export const RESERVED = [
8181
export const CHECKPOINT_NAMESPACE_SEPARATOR = "|";
8282
export const CHECKPOINT_NAMESPACE_END = ":";
8383

84-
export interface SendInterface {
85-
node: string;
86-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
args: any;
84+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
85+
export interface SendInterface<Node extends string = string, Args = any> {
86+
node: Node;
87+
args: Args;
8888
}
8989

9090
export function _isSendInterface(x: unknown): x is SendInterface {
@@ -143,26 +143,23 @@ export function _isSendInterface(x: unknown): x is SendInterface {
143143
* // { subjects: ["cats", "dogs"], jokes: [`Joke about cats`, `Joke about dogs`] }
144144
* ```
145145
*/
146-
export class Send implements SendInterface {
146+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
147+
export class Send<Node extends string = string, Args = any>
148+
implements SendInterface<Node, Args>
149+
{
147150
lg_name = "Send";
148151

149-
public node: string;
152+
public node: Node;
150153

151-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152-
public args: any;
154+
public args: Args;
153155

154-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
155-
constructor(node: string, args: any) {
156+
constructor(node: Node, args: Args) {
156157
this.node = node;
157-
this.args = _deserializeCommandSendObjectGraph(args);
158+
this.args = _deserializeCommandSendObjectGraph(args) as Args;
158159
}
159160

160161
toJSON() {
161-
return {
162-
lg_name: this.lg_name,
163-
node: this.node,
164-
args: this.args,
165-
};
162+
return { lg_name: this.lg_name, node: this.node, args: this.args };
166163
}
167164
}
168165

@@ -180,7 +177,11 @@ export type Interrupt = {
180177
ns?: string[];
181178
};
182179

183-
export type CommandParams<R> = {
180+
export type CommandParams<
181+
Resume = unknown,
182+
Update extends Record<string, unknown> = Record<string, unknown>,
183+
Nodes extends string = string
184+
> = {
184185
/**
185186
* A discriminator field used to identify the type of object. Must be populated when serializing.
186187
*
@@ -192,7 +193,7 @@ export type CommandParams<R> = {
192193
/**
193194
* Value to resume execution with. To be used together with {@link interrupt}.
194195
*/
195-
resume?: R;
196+
resume?: Resume;
196197
/**
197198
* Graph to send the command to. Supported values are:
198199
* - None: the current graph (default)
@@ -204,7 +205,7 @@ export type CommandParams<R> = {
204205
/**
205206
* Update to apply to the graph's state.
206207
*/
207-
update?: Record<string, unknown> | [string, unknown][];
208+
update?: Update | [string, unknown][];
208209

209210
/**
210211
* Can be one of the following:
@@ -213,7 +214,10 @@ export type CommandParams<R> = {
213214
* - `Send` object (to execute a node with the input provided)
214215
* - sequence of `Send` objects
215216
*/
216-
goto?: string | SendInterface | (string | SendInterface)[];
217+
goto?:
218+
| Nodes
219+
| SendInterface<Nodes> // eslint-disable-line @typescript-eslint/no-explicit-any
220+
| (Nodes | SendInterface<Nodes>)[]; // eslint-disable-line @typescript-eslint/no-explicit-any
217221
};
218222

219223
/**
@@ -278,7 +282,11 @@ export type CommandParams<R> = {
278282
* // { foo: 'a|c' } and { foo: 'a|b' }
279283
* ```
280284
*/
281-
export class Command<R = unknown> {
285+
export class Command<
286+
Resume = unknown,
287+
Update extends Record<string, unknown> = Record<string, unknown>,
288+
Nodes extends string = string
289+
> {
282290
readonly lg_name = "Command";
283291

284292
lc_direct_tool_output = true;
@@ -295,12 +303,12 @@ export class Command<R = unknown> {
295303
* Update to apply to the graph's state as a result of executing the node that is returning the command.
296304
* Written to the state as if the node had simply returned this value instead of the Command object.
297305
*/
298-
update?: Record<string, unknown> | [string, unknown][];
306+
update?: Update | [string, unknown][];
299307

300308
/**
301309
* Value to resume execution with. To be used together with {@link interrupt}.
302310
*/
303-
resume?: R;
311+
resume?: Resume;
304312

305313
/**
306314
* Can be one of the following:
@@ -309,18 +317,20 @@ export class Command<R = unknown> {
309317
* - {@link Send} object (to execute a node with the exact input provided in the {@link Send} object)
310318
* - sequence of {@link Send} objects
311319
*/
312-
goto?: string | Send | (string | Send)[] = [];
320+
goto?: Nodes | Send<Nodes> | (Nodes | Send<Nodes>)[] = [];
313321

314322
static PARENT = "__parent__";
315323

316-
constructor(args: CommandParams<R>) {
324+
constructor(args: CommandParams<Resume, Update, Nodes>) {
317325
this.resume = args.resume;
318326
this.graph = args.graph;
319327
this.update = args.update;
320328
if (args.goto) {
329+
type ValidArg = Nodes | Send<Nodes, Update>;
330+
321331
this.goto = Array.isArray(args.goto)
322-
? (_deserializeCommandSendObjectGraph(args.goto) as (string | Send)[])
323-
: [_deserializeCommandSendObjectGraph(args.goto) as string | Send];
332+
? (_deserializeCommandSendObjectGraph(args.goto) as ValidArg[])
333+
: [_deserializeCommandSendObjectGraph(args.goto) as ValidArg];
324334
}
325335
}
326336

0 commit comments

Comments
 (0)