3434from .tools .explore_project import explore_project
3535from .tools .read_config_files import read_config_files
3636from .tools .read_files import read_files
37- from .tools .resolve_root_directory import resolve_root_directory
3837from .tools .search_adk_source import search_adk_source
3938from .tools .write_config_files import write_config_files
4039from .tools .write_files import write_files
@@ -60,9 +59,7 @@ def create_agent(
6059 Configured LlmAgent with embedded ADK AgentConfig schema
6160 """
6261 # Load full ADK AgentConfig schema directly into instruction context
63- instruction = AgentBuilderAssistant ._load_instruction_with_schema (
64- model , working_directory
65- )
62+ instruction = AgentBuilderAssistant ._load_instruction_with_schema (model )
6663
6764 # TOOL ARCHITECTURE: Hybrid approach using both AgentTools and FunctionTools
6865 #
@@ -95,8 +92,6 @@ def create_agent(
9592 write_config_files
9693 ), # Write/validate multiple YAML configs
9794 FunctionTool (explore_project ), # Analyze project structure
98- # Working directory context tools
99- FunctionTool (resolve_root_directory ),
10095 # File management tools (multi-file support)
10196 FunctionTool (read_files ), # Read multiple files
10297 FunctionTool (write_files ), # Write multiple files
@@ -135,7 +130,6 @@ def _load_schema() -> str:
135130 # ADK AgentConfig schema loading with caching and error handling.
136131 schema_content = load_agent_config_schema (
137132 raw_format = True , # Get as JSON string
138- escape_braces = True , # Escape braces for template embedding
139133 )
140134
141135 # Format as indented code block for instruction embedding
@@ -157,7 +151,6 @@ def _load_schema() -> str:
157151 @staticmethod
158152 def _load_instruction_with_schema (
159153 model : Union [str , BaseLlm ],
160- working_directory : Optional [str ] = None ,
161154 ) -> Callable [[ReadonlyContext ], str ]:
162155 """Load instruction template and embed ADK AgentConfig schema content."""
163156 instruction_template = (
@@ -172,19 +165,43 @@ def _load_instruction_with_schema(
172165 else getattr (model , "model_name" , str (model ))
173166 )
174167
175- # Fill the instruction template with ADK AgentConfig schema content and default model
176- instruction_text = instruction_template .format (
177- schema_content = schema_content , default_model = model_str
178- )
179-
180168 # Return a function that accepts ReadonlyContext and returns the instruction
181169 def instruction_provider (context : ReadonlyContext ) -> str :
182- return AgentBuilderAssistant ._compile_instruction_with_context (
183- instruction_text , context , working_directory
170+ # Extract project folder name from session state
171+ project_folder_name = AgentBuilderAssistant ._extract_project_folder_name (
172+ context
184173 )
185174
175+ # Fill the instruction template with all variables
176+ instruction_text = instruction_template .format (
177+ schema_content = schema_content ,
178+ default_model = model_str ,
179+ project_folder_name = project_folder_name ,
180+ )
181+ return instruction_text
182+
186183 return instruction_provider
187184
185+ @staticmethod
186+ def _extract_project_folder_name (context : ReadonlyContext ) -> str :
187+ """Extract project folder name from session state using resolve_file_path."""
188+ from .utils .resolve_root_directory import resolve_file_path
189+
190+ session_state = context ._invocation_context .session .state
191+
192+ # Use resolve_file_path to get the full resolved path for "."
193+ # This handles all the root_directory resolution logic consistently
194+ resolved_path = resolve_file_path ("." , session_state )
195+
196+ # Extract the project folder name from the resolved path
197+ project_folder_name = resolved_path .name
198+
199+ # Fallback to "project" if we somehow get an empty name
200+ if not project_folder_name :
201+ project_folder_name = "project"
202+
203+ return project_folder_name
204+
188205 @staticmethod
189206 def _load_embedded_schema_instruction_template () -> str :
190207 """Load instruction template for embedded ADK AgentConfig schema mode."""
@@ -197,70 +214,3 @@ def _load_embedded_schema_instruction_template() -> str:
197214
198215 with open (template_path , "r" , encoding = "utf-8" ) as f :
199216 return f .read ()
200-
201- @staticmethod
202- def _compile_instruction_with_context (
203- instruction_text : str ,
204- context : ReadonlyContext ,
205- working_directory : Optional [str ] = None ,
206- ) -> str :
207- """Compile instruction with session context and working directory information.
208-
209- This method enhances instructions with:
210- 1. Working directory information for path resolution
211- 2. Session-based root directory binding if available
212-
213- Args:
214- instruction_text: Base instruction text
215- context: ReadonlyContext from the agent session
216- working_directory: Optional working directory for path resolution
217-
218- Returns:
219- Enhanced instruction text with context information
220- """
221- import os
222-
223- # Get working directory (use provided or current working directory)
224- actual_working_dir = working_directory or os .getcwd ()
225-
226- # Check for existing root directory in session state
227- session_root_directory = context ._invocation_context .session .state .get (
228- "root_directory"
229- )
230-
231- # Compile additional context information
232- context_info = f"""
233-
234- ## SESSION CONTEXT
235-
236- **Working Directory**: `{ actual_working_dir } `
237- - Use this as the base directory for path resolution when calling resolve_root_directory
238- - Pass this as the working_directory parameter to resolve_root_directory tool
239-
240- """
241-
242- if session_root_directory :
243- context_info += f"""**Established Root Directory**: `{ session_root_directory } `
244- - This session is bound to root directory: { session_root_directory }
245- - DO NOT ask the user for root directory - use this established path
246- - All agent building should happen within this root directory
247- - If user wants to work in a different directory, ask them to start a new chat session
248-
249- """
250- else :
251- context_info += f"""**Root Directory**: Not yet established
252- - You MUST ask the user for their desired root directory first
253- - Use resolve_root_directory tool to validate the path
254- - Once confirmed, this session will be bound to that root directory
255-
256- """
257-
258- context_info += """**Session Binding Rules**:
259- - Each chat session is bound to ONE root directory
260- - Once established, work only within that root directory
261- - To switch directories, user must start a new chat session
262- - Always verify paths using resolve_root_directory tool before creating files
263-
264- """
265-
266- return instruction_text + context_info
0 commit comments