diff --git a/apps/open-swe/src/subgraphs/planner/index.ts b/apps/open-swe/src/subgraphs/planner/index.ts index 1c564825..12e2f8ac 100644 --- a/apps/open-swe/src/subgraphs/planner/index.ts +++ b/apps/open-swe/src/subgraphs/planner/index.ts @@ -7,7 +7,7 @@ import { END, START, StateGraph } from "@langchain/langgraph"; import { PlannerGraphState, PlannerGraphStateObj } from "./types.js"; -import { GraphConfiguration } from "../../types.js"; +import { GraphConfig, GraphConfiguration } from "../../types.js"; import { generateAction, generatePlan, @@ -18,12 +18,15 @@ import { isAIMessage } from "@langchain/core/messages"; function takeActionOrGeneratePlan( state: PlannerGraphState, + config: GraphConfig, ): "take-plan-action" | "generate-plan" { const { plannerMessages } = state; const lastMessage = plannerMessages[plannerMessages.length - 1]; // If the last message is a tool call, and we have executed less than 6 actions, take action. - // Max actions is 13, because that's 6 actions (2 messages per action, ai & tool) plus the input message. - const maxActionsCount = 13; + // Max actions count is calculated as: maxContextActions * 2 + 1 + // This is because each action generates 2 messages (AI request + tool result) plus 1 initial human message + const maxContextActions = config.configurable?.maxContextActions ?? 6; + const maxActionsCount = maxContextActions * 2 + 1; if ( isAIMessage(lastMessage) && lastMessage.tool_calls?.length && diff --git a/apps/open-swe/src/types.ts b/apps/open-swe/src/types.ts index 49bc4806..9b80af41 100644 --- a/apps/open-swe/src/types.ts +++ b/apps/open-swe/src/types.ts @@ -336,6 +336,26 @@ export const GraphConfiguration = z.object({ description: "Controls randomness (0 = deterministic, 2 = creative)", }, }), + + /** + * The maximum number of context gathering actions to take during planning. + * Each action consists of 2 messages (request & result), plus 1 human message. + * Total messages = maxContextActions * 2 + 1 + * @default 6 + */ + maxContextActions: z + .number() + .optional() + .langgraph.metadata({ + x_oap_ui_config: { + type: "number", + default: 6, + min: 1, + max: 20, + description: + "Maximum number of context gathering actions during planning", + }, + }), }); export type GraphConfig = LangGraphRunnableConfig<