Skip to content

Commit 8b33e48

Browse files
validate jump targets
1 parent 344a07b commit 8b33e48

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

libs/langchain/src/agents/middlewareAgent/ReactAgent.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export class ReactAgent<
357357
/**
358358
* Get possible edge destinations from model node.
359359
* @param toolClasses names of tools to call
360-
* @param includeModelRequest whether to include model_request as a valid path (for jumpTo routing)
360+
* @param includeModelRequest whether to include "model_request" as a valid path (for jumpTo routing)
361361
* @returns list of possible edge destinations
362362
*/
363363
#getModelPaths(
@@ -490,13 +490,29 @@ export class ReactAgent<
490490
if (state.jumpTo) {
491491
const jumpTarget = state.jumpTo;
492492

493-
// Validate that the jump target is available
494-
if (jumpTarget === "tools" && toolClasses.length === 0) {
493+
// If jumpTo is "model", go to model_request node
494+
if (jumpTarget === "model") {
495+
return "model_request";
496+
}
497+
498+
// If jumpTo is "tools", go to tools node
499+
if (jumpTarget === "tools") {
495500
// If trying to jump to tools but no tools are available, go to END
501+
if (toolClasses.length === 0) {
502+
return END;
503+
}
504+
505+
return "tools";
506+
}
507+
508+
// If jumpTo is END, go to END
509+
if (jumpTarget === END) {
496510
return END;
497511
}
498512

499-
return jumpTarget;
513+
throw new Error(
514+
`Invalid jump target: ${jumpTarget}, must be "model" or "tools".`
515+
);
500516
}
501517

502518
// check if there are pending tool calls

libs/langchain/src/agents/middlewareAgent/middleware/hitl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ export function humanInTheLoopMiddleware(
477477
}
478478

479479
return {
480-
jumpTo: "model_request",
480+
jumpTo: "model",
481481
messages: [...state.messages, ...artificialToolMessages],
482482
};
483483
},

libs/langchain/src/agents/middlewareAgent/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface BuiltInState {
5656
* - "model_request": Jump back to the model for another LLM call
5757
* - "tools": Jump to tool execution (requires tools to be available)
5858
*/
59-
jumpTo?: "model_request" | "tools";
59+
jumpTo?: "model" | "tools";
6060
}
6161

6262
/**

0 commit comments

Comments
 (0)