Repository navigation
feat(planners): port BasePlanner, BuiltInPlanner and PlanReActPlanner #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ScottMansfield
merged 1 commit into
main
from
foundry/c1351db8-b98b-4369-907a-0b6fee584dc8
Sep 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {Part} from '@google/genai'; | ||
|
|
||
| import {Context} from '../agents/context.js'; | ||
| import {ReadonlyContext} from '../agents/readonly_context.js'; | ||
| import {LlmRequest} from '../models/llm_request.js'; | ||
|
|
||
| /** | ||
| * Abstract base class for all planners. | ||
| * | ||
| * The planner allows the agent to generate plans for the queries to guide its | ||
| * action. | ||
| */ | ||
| export abstract class BasePlanner { | ||
| /** | ||
| * Builds the system instruction to be appended to the LLM request for | ||
| * planning. | ||
| * | ||
| * @param readonlyContext The readonly context of the invocation. | ||
| * @param llmRequest The LLM request. Readonly. | ||
| * @returns The planning system instruction, or undefined if no instruction is | ||
| * needed. | ||
| */ | ||
| abstract buildPlanningInstruction( | ||
| readonlyContext: ReadonlyContext, | ||
| llmRequest: LlmRequest, | ||
| ): string | undefined; | ||
|
|
||
| /** | ||
| * Processes the LLM response for planning. | ||
| * | ||
| * @param callbackContext The callback context of the invocation. | ||
| * @param responseParts The LLM response parts. Readonly. | ||
| * @returns The processed response parts, or undefined if no processing is | ||
| * needed. | ||
| */ | ||
| abstract processPlanningResponse( | ||
| callbackContext: Context, | ||
| responseParts: Part[], | ||
| ): Part[] | undefined; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {Part, ThinkingConfig} from '@google/genai'; | ||
|
|
||
| import {Context} from '../agents/context.js'; | ||
| import {ReadonlyContext} from '../agents/readonly_context.js'; | ||
| import {LlmRequest} from '../models/llm_request.js'; | ||
|
|
||
| import {BasePlanner} from './base_planner.js'; | ||
|
|
||
| /** | ||
| * The built-in planner that uses model's built-in thinking features. | ||
| */ | ||
| export class BuiltInPlanner extends BasePlanner { | ||
| /** | ||
| * Config for model built-in thinking features. An error will be returned if | ||
| * this field is set for models that don't support thinking. | ||
| */ | ||
| thinkingConfig: ThinkingConfig; | ||
|
|
||
| /** | ||
| * Initializes the built-in planner. | ||
| * | ||
| * @param options.thinkingConfig Config for model built-in thinking features. | ||
| * An error will be returned if this field is set for models that don't | ||
| * support thinking. | ||
| */ | ||
| constructor(options: {thinkingConfig: ThinkingConfig}) { | ||
| super(); | ||
| this.thinkingConfig = options.thinkingConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Applies the thinking config to the LLM request. | ||
| * | ||
| * @param llmRequest The LLM request to apply the thinking config to. | ||
| */ | ||
| applyThinkingConfig(llmRequest: LlmRequest): void { | ||
| if (this.thinkingConfig) { | ||
| llmRequest.config = llmRequest.config ?? {}; | ||
| llmRequest.config.thinkingConfig = this.thinkingConfig; | ||
| } | ||
| } | ||
|
|
||
| buildPlanningInstruction( | ||
| _readonlyContext: ReadonlyContext, | ||
| _llmRequest: LlmRequest, | ||
| ): string | undefined { | ||
| return undefined; | ||
| } | ||
|
|
||
| processPlanningResponse( | ||
| _callbackContext: Context, | ||
| _responseParts: Part[], | ||
| ): Part[] | undefined { | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {Part} from '@google/genai'; | ||
|
|
||
| import {Context} from '../agents/context.js'; | ||
| import {ReadonlyContext} from '../agents/readonly_context.js'; | ||
| import {LlmRequest} from '../models/llm_request.js'; | ||
|
|
||
| import {BasePlanner} from './base_planner.js'; | ||
|
|
||
| const PLANNING_TAG = '/*PLANNING*/'; | ||
| const REPLANNING_TAG = '/*REPLANNING*/'; | ||
| const REASONING_TAG = '/*REASONING*/'; | ||
| const ACTION_TAG = '/*ACTION*/'; | ||
| const FINAL_ANSWER_TAG = '/*FINAL_ANSWER*/'; | ||
|
|
||
| const THOUGHT_PREFIX_TAGS: readonly string[] = [ | ||
| PLANNING_TAG, | ||
| REASONING_TAG, | ||
| ACTION_TAG, | ||
| REPLANNING_TAG, | ||
| ]; | ||
|
|
||
| /** | ||
| * Plan-Re-Act planner that constrains the LLM response to generate a plan | ||
| * before any action/observation. | ||
| * | ||
| * Note: this planner does not require the model to support built-in thinking | ||
| * features or setting the thinking config. | ||
| */ | ||
| export class PlanReActPlanner extends BasePlanner { | ||
| buildPlanningInstruction( | ||
| _readonlyContext: ReadonlyContext, | ||
| _llmRequest: LlmRequest, | ||
| ): string { | ||
| return buildNlPlannerInstruction(); | ||
| } | ||
|
|
||
| processPlanningResponse( | ||
| _callbackContext: Context, | ||
| responseParts: Part[], | ||
| ): Part[] | undefined { | ||
| if (!responseParts || responseParts.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const preservedParts: Part[] = []; | ||
| let firstFcPartIndex = -1; | ||
| for (let i = 0; i < responseParts.length; i++) { | ||
| const part = responseParts[i]; | ||
| // Stop at the first (group of) function calls. | ||
| if (part.functionCall) { | ||
| // Ignore and filter out function calls with empty names. | ||
| if (!part.functionCall.name) { | ||
| continue; | ||
| } | ||
| preservedParts.push(part); | ||
| firstFcPartIndex = i; | ||
| break; | ||
| } | ||
|
|
||
| handleNonFunctionCallPart(part, preservedParts); | ||
| } | ||
|
|
||
| if (firstFcPartIndex > 0) { | ||
| for (let j = firstFcPartIndex + 1; j < responseParts.length; j++) { | ||
| if (!responseParts[j].functionCall) { | ||
| break; | ||
| } | ||
| preservedParts.push(responseParts[j]); | ||
| } | ||
| } | ||
|
|
||
| return preservedParts; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Splits the text by the last occurrence of the separator. | ||
| * | ||
| * @param text The text to split. | ||
| * @param separator The separator to split on. | ||
| * @returns The text up to and including the last separator, and the text after | ||
| * the last separator. | ||
| */ | ||
| function splitByLastPattern(text: string, separator: string): [string, string] { | ||
| const index = text.lastIndexOf(separator); | ||
| if (index === -1) { | ||
| return [text, '']; | ||
| } | ||
| const end = index + separator.length; | ||
| return [text.slice(0, end), text.slice(end)]; | ||
| } | ||
|
|
||
| /** | ||
| * Handles a non-function-call part of the response. | ||
| * | ||
| * @param responsePart The response part to handle. It is not modified. | ||
| * @param preservedParts The list to append the processed parts to. | ||
| */ | ||
| function handleNonFunctionCallPart( | ||
| responsePart: Part, | ||
| preservedParts: Part[], | ||
| ): void { | ||
| if (responsePart.text && responsePart.text.includes(FINAL_ANSWER_TAG)) { | ||
| const [reasoningText, finalAnswerText] = splitByLastPattern( | ||
| responsePart.text, | ||
| FINAL_ANSWER_TAG, | ||
| ); | ||
| if (reasoningText) { | ||
| preservedParts.push(markAsThought({text: reasoningText})); | ||
| } | ||
| if (finalAnswerText) { | ||
| preservedParts.push({text: finalAnswerText}); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const responseText = responsePart.text ?? ''; | ||
| // A text part that starts with a planning/reasoning/action tag is reasoning. | ||
| if ( | ||
| responseText && | ||
| THOUGHT_PREFIX_TAGS.some((tag) => responseText.startsWith(tag)) | ||
| ) { | ||
| preservedParts.push(markAsThought(responsePart)); | ||
| return; | ||
| } | ||
| preservedParts.push(responsePart); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a copy of the response part marked as thought, if it has text. | ||
| * | ||
| * The response parts are read-only, so the part is copied rather than changed | ||
| * in place. | ||
| * | ||
| * @param responsePart The response part to mark as thought. | ||
| * @returns The marked copy, or the original part if it has no text. | ||
| */ | ||
| function markAsThought(responsePart: Part): Part { | ||
| if (responsePart.text) { | ||
| return {...responsePart, thought: true}; | ||
| } | ||
| return responsePart; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the NL planner instruction for the Plan-Re-Act planner. | ||
| * | ||
| * The wording is part of the planner's behaviour and matches the Python ADK | ||
| * text exactly. | ||
| * | ||
| * @returns NL planner system instruction. | ||
| */ | ||
| function buildNlPlannerInstruction(): string { | ||
| const highLevelPreamble = ` | ||
| When answering the question, try to leverage the available tools to gather the information instead of your memorized knowledge. | ||
|
|
||
| Follow this process when answering the question: (1) first come up with a plan in natural language text format; (2) Then use tools to execute the plan and provide reasoning between tool code snippets to make a summary of current state and next step. Tool code snippets and reasoning should be interleaved with each other. (3) In the end, return one final answer. | ||
|
|
||
| Follow this format when answering the question: (1) The planning part should be under ${PLANNING_TAG}. (2) The tool code snippets should be under ${ACTION_TAG}, and the reasoning parts should be under ${REASONING_TAG}. (3) The final answer part should be under ${FINAL_ANSWER_TAG}. | ||
| `; | ||
|
|
||
| const planningPreamble = ` | ||
| Below are the requirements for the planning: | ||
| The plan is made to answer the user query if following the plan. The plan is coherent and covers all aspects of information from user query, and only involves the tools that are accessible by the agent. The plan contains the decomposed steps as a numbered list where each step should use one or multiple available tools. By reading the plan, you can intuitively know which tools to trigger or what actions to take. | ||
| If the initial plan cannot be successfully executed, you should learn from previous execution results and revise your plan. The revised plan should be be under ${REPLANNING_TAG}. Then use tools to follow the new plan. | ||
| `; | ||
|
|
||
| const reasoningPreamble = ` | ||
| Below are the requirements for the reasoning: | ||
| The reasoning makes a summary of the current trajectory based on the user query and tool outputs. Based on the tool outputs and plan, the reasoning also comes up with instructions to the next steps, making the trajectory closer to the final answer. | ||
| `; | ||
|
|
||
| const finalAnswerPreamble = ` | ||
| Below are the requirements for the final answer: | ||
| The final answer should be precise and follow query formatting requirements. Some queries may not be answerable with the available tools and information. In those cases, inform the user why you cannot process their query and ask for more information. | ||
| `; | ||
|
|
||
| // Only contains the requirements for custom tool/libraries. | ||
| const toolCodeWithoutPythonLibrariesPreamble = ` | ||
| Below are the requirements for the tool code: | ||
|
|
||
| **Custom Tools:** The available tools are described in the context and can be directly used. | ||
| - Code must be valid self-contained Python snippets with no imports and no references to tools or Python libraries that are not in the context. | ||
| - You cannot use any parameters or fields that are not explicitly defined in the APIs in the context. | ||
| - The code snippets should be readable, efficient, and directly relevant to the user query and reasoning steps. | ||
| - When using the tools, you should use the library name together with the function name, e.g., vertex_search.search(). | ||
| - If Python libraries are not provided in the context, NEVER write your own code other than the function calls using the provided tools. | ||
| `; | ||
|
|
||
| const userInputPreamble = ` | ||
| VERY IMPORTANT instruction that you MUST follow in addition to the above instructions: | ||
|
|
||
| You should ask for clarification if you need more information to answer the question. | ||
| You should prefer using the information available in the context instead of repeated tool use. | ||
| `; | ||
|
|
||
| return [ | ||
| highLevelPreamble, | ||
| planningPreamble, | ||
| reasoningPreamble, | ||
| finalAnswerPreamble, | ||
| toolCodeWithoutPythonLibrariesPreamble, | ||
| userInputPreamble, | ||
| ].join('\n\n'); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, optional. This guard matches adk-python v0.1.0, so no change is needed for this port.
Note for awareness: upstream later changed it to
>= 0. With> 0, a response that starts with a function call keeps only the first call and drops the following consecutive calls. Confirm you want the v0.1.0 behaviour.