Skip to content

Commit 07fccff

Browse files
committed
Use StateKeys constants
1 parent b5b65f4 commit 07fccff

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/llms_gen_agent/constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class StateKeys:
2+
ALL_SUMMARIES = "all_summaries"
3+
BATCHES = "batches"
4+
BATCH_SUMMARIES = "batch_summaries"
5+
CURRENT_BATCH = "current_batch"
6+
DOC_SUMMARIES = "doc_summaries"
7+
FILES = "files"
8+
FILES_CONTENT = "files_content"
9+
LOOP_ITERATION = "loop_iteration"
10+
PROJECT_SUMMARY_RAW = "project_summary_raw"

src/llms_gen_agent/sub_agents/doc_summariser/tools.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from google.adk.tools import ToolContext
1313

1414
from llms_gen_agent.config import logger
15+
from llms_gen_agent.constants import StateKeys
1516

1617

1718
def read_files(tool_context: ToolContext) -> dict:
@@ -20,7 +21,6 @@ def read_files(tool_context: ToolContext) -> dict:
2021
This tool retrieves a list of file paths from the `current_batch` key in the
2122
`tool_context.state`. It then iterates through this list, reads the
2223
content of each file, and stores it in a dictionary under the
23-
2424
`files_content` key in the `tool_context.state`. The file path serves as
2525
the key for its content.
2626
@@ -34,29 +34,30 @@ def read_files(tool_context: ToolContext) -> dict:
3434

3535
# The files to read are either in 'current_batch' (for batched processing)
3636
# or in 'files' (for direct processing or initial setup).
37-
file_paths = tool_context.state.get("current_batch", tool_context.state.get("files", []))
37+
file_paths = tool_context.state.get(StateKeys.CURRENT_BATCH,
38+
tool_context.state.get(StateKeys.FILES, []))
3839
logger.debug(f"Got {len(file_paths)} files")
3940

4041
# Initialise our session state key
41-
tool_context.state["files_content"] = {}
42+
tool_context.state[StateKeys.FILES_CONTENT] = {}
4243

4344
response = {"status": "success"}
4445
for file_path in file_paths:
45-
if file_path not in tool_context.state["files_content"]:
46+
if file_path not in tool_context.state[StateKeys.FILES_CONTENT]:
4647
try:
4748
logger.debug(f"Reading file: {file_path}")
4849
with open(file_path) as f:
4950
content = f.read()
5051
logger.debug(f"Read content: {content[:80]}...")
51-
tool_context.state["files_content"][file_path] = content
52+
tool_context.state[StateKeys.FILES_CONTENT][file_path] = content
5253
except (FileNotFoundError, PermissionError, UnicodeDecodeError) as e:
5354
logger.warning("Could not read file %s: %s", file_path, e)
5455
# Store an error message so the summarizer knows it failed
55-
tool_context.state["files_content"][file_path] = f"Error: Could not read file. Reason: {e}"
56+
tool_context.state[StateKeys.FILES_CONTENT][file_path] = f"Error: Could not read file. Reason: {e}"
5657
response = {"status": "warnings"}
5758
except Exception as e:
5859
logger.error("An unexpected error occurred while reading %s: %s", file_path, e)
59-
tool_context.state["files_content"][file_path] = f"Error: An unexpected error occurred. Reason: {e}"
60+
tool_context.state[StateKeys.FILES_CONTENT][file_path] = f"Error: An unexpected error occurred. Reason: {e}"
6061
response = {"status": "warnings"}
6162

6263
return response
@@ -68,38 +69,38 @@ def create_file_batches(tool_context: ToolContext, batch_size: int) -> list[list
6869
divides them into smaller batches, and stores these batches back into the
6970
session state for iterative processing by the LoopAgent.
7071
"""
71-
file_paths = tool_context.state.get("files", [])
72+
file_paths = tool_context.state.get(StateKeys.FILES, [])
7273
logger.debug(f"create_file_batches: Received {len(file_paths)} files from session state.")
7374
logger.debug(f"Creating batches for {len(file_paths)} files with batch size {batch_size}")
7475
if not file_paths:
7576
logger.debug("No files to batch.")
76-
tool_context.state["batches"] = [] # Ensure batches is set even if empty
77+
tool_context.state[StateKeys.BATCHES] = [] # Ensure batches is set even if empty
7778
return []
7879
num_batches = math.ceil(len(file_paths) / batch_size)
7980
batches = [file_paths[i * batch_size:(i + 1) * batch_size] for i in range(num_batches)]
8081
logger.debug(f"Created {len(batches)} batches.")
81-
tool_context.state["batches"] = batches # Store batches in session state
82+
tool_context.state[StateKeys.BATCHES] = batches # Store batches in session state
8283
return batches
8384

8485

8586
def process_batch_selection(tool_context: ToolContext) -> dict:
8687
"""Manages the batch selection for the loop, increments iteration counter, and logs batch info."""
8788
logger.debug("Executing process_batch_selection")
8889

89-
batches = tool_context.state.get("batches", [])
90-
loop_iteration = tool_context.state.get("loop_iteration", 0)
90+
batches = tool_context.state.get(StateKeys.BATCHES, [])
91+
loop_iteration = tool_context.state.get(StateKeys.LOOP_ITERATION, 0)
9192

9293
if not batches:
9394
logger.debug("No more batches to process. Exiting loop.")
9495
tool_context.actions.escalate = True # Signal to exit the loop
9596
return {"status": "no_more_batches"}
9697

9798
current_batch = batches.pop(0) # Get the next batch
98-
tool_context.state["batches"] = batches # Update batches in state
99-
tool_context.state["current_batch"] = current_batch # Set current batch
99+
tool_context.state[StateKeys.BATCHES] = batches # Update batches in state
100+
tool_context.state[StateKeys.CURRENT_BATCH] = current_batch # Set current batch
100101

101102
loop_iteration += 1
102-
tool_context.state["loop_iteration"] = loop_iteration
103+
tool_context.state[StateKeys.LOOP_ITERATION] = loop_iteration
103104

104105
logger.debug(f"Processing batch {loop_iteration}. Files in batch: {len(current_batch)}. Remaining batches: {len(batches)}")
105106

@@ -114,16 +115,16 @@ def update_summaries(tool_context: ToolContext) -> dict:
114115
"""
115116
logger.debug("Executing update_summaries")
116117

117-
batch_summaries_output = tool_context.state.get("batch_summaries", {})
118-
batch_summaries = batch_summaries_output.get("batch_summaries", {}) # Get the actual dict from the output_key
118+
batch_summaries_output = tool_context.state.get(StateKeys.BATCH_SUMMARIES, {})
119+
batch_summaries = batch_summaries_output.get(StateKeys.BATCH_SUMMARIES, {}) # Get the actual dict from the output_key
119120

120-
if "all_summaries" not in tool_context.state:
121-
tool_context.state["all_summaries"] = {}
121+
if StateKeys.ALL_SUMMARIES not in tool_context.state:
122+
tool_context.state[StateKeys.ALL_SUMMARIES] = {}
122123

123-
tool_context.state["all_summaries"].update(batch_summaries)
124+
tool_context.state[StateKeys.ALL_SUMMARIES].update(batch_summaries)
124125

125126
logger.debug(f"Merged {len(batch_summaries)} summaries from current batch. "
126-
f"Total summaries collected: {len(tool_context.state['all_summaries'])}")
127+
f"Total summaries collected: {len(tool_context.state[StateKeys.ALL_SUMMARIES])}")
127128

128129
return {"status": "success"}
129130

@@ -136,15 +137,16 @@ def finalize_summaries(tool_context: ToolContext) -> dict:
136137
in `tool_context.state["doc_summaries"]`.
137138
"""
138139
logger.debug("Executing finalize_summaries")
139-
all_summaries = tool_context.state.get("all_summaries", {})
140-
project_summary_raw = tool_context.state.get("project_summary_raw", {}).get("project_summary", "No project summary found.")
140+
all_summaries = tool_context.state.get(StateKeys.ALL_SUMMARIES, {})
141+
project_summary_raw = tool_context.state.get(StateKeys.PROJECT_SUMMARY_RAW,
142+
{}).get("project_summary", "No project summary found.")
141143

142144
final_doc_summaries = {
143145
"summaries": {
144146
**all_summaries,
145147
"project": project_summary_raw
146148
}
147149
}
148-
tool_context.state["doc_summaries"] = final_doc_summaries
150+
tool_context.state[StateKeys.DOC_SUMMARIES] = final_doc_summaries
149151

150152
return {"status": "success"}

0 commit comments

Comments
 (0)