Skip to content

Commit 2ed3701

Browse files
Extract shared snapshot loader logic
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 27c415a commit 2ed3701

4 files changed

Lines changed: 76 additions & 51 deletions

File tree

src/commands.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

3-
import json
43
from collections.abc import Mapping
54
from dataclasses import dataclass
65
from functools import lru_cache
76
from pathlib import Path
87
from types import MappingProxyType
98

109
from .models import PortingBacklog, PortingModule
10+
from .snapshot_loader import load_porting_modules
1111

1212
SNAPSHOT_PATH = (
1313
Path(__file__).resolve().parent / "reference_data" / "commands_snapshot.json"
@@ -23,21 +23,7 @@ class CommandExecution:
2323
message: str
2424

2525

26-
@lru_cache(maxsize=1)
27-
def load_command_snapshot() -> tuple[PortingModule, ...]:
28-
raw_entries = json.loads(SNAPSHOT_PATH.read_text())
29-
return tuple(
30-
PortingModule(
31-
name=entry["name"],
32-
responsibility=entry["responsibility"],
33-
source_hint=entry["source_hint"],
34-
status="mirrored",
35-
)
36-
for entry in raw_entries
37-
)
38-
39-
40-
PORTED_COMMANDS = load_command_snapshot()
26+
PORTED_COMMANDS = load_porting_modules(SNAPSHOT_PATH)
4127

4228

4329
@lru_cache(maxsize=1)

src/snapshot_loader.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from functools import lru_cache
5+
from pathlib import Path
6+
7+
from .models import PortingModule
8+
9+
10+
@lru_cache(maxsize=None)
11+
def load_porting_modules(snapshot_path: Path) -> tuple[PortingModule, ...]:
12+
raw_entries = json.loads(snapshot_path.read_text())
13+
return tuple(
14+
PortingModule(
15+
name=entry["name"],
16+
responsibility=entry["responsibility"],
17+
source_hint=entry["source_hint"],
18+
status="mirrored",
19+
)
20+
for entry in raw_entries
21+
)

src/tools.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from __future__ import annotations
22

3-
import json
43
from dataclasses import dataclass
5-
from functools import lru_cache
64
from pathlib import Path
75

86
from .models import PortingBacklog, PortingModule
97
from .permissions import ToolPermissionContext
8+
from .snapshot_loader import load_porting_modules
109

11-
SNAPSHOT_PATH = Path(__file__).resolve().parent / 'reference_data' / 'tools_snapshot.json'
10+
SNAPSHOT_PATH = (
11+
Path(__file__).resolve().parent / "reference_data" / "tools_snapshot.json"
12+
)
1213

1314

1415
@dataclass(frozen=True)
@@ -20,25 +21,11 @@ class ToolExecution:
2021
message: str
2122

2223

23-
@lru_cache(maxsize=1)
24-
def load_tool_snapshot() -> tuple[PortingModule, ...]:
25-
raw_entries = json.loads(SNAPSHOT_PATH.read_text())
26-
return tuple(
27-
PortingModule(
28-
name=entry['name'],
29-
responsibility=entry['responsibility'],
30-
source_hint=entry['source_hint'],
31-
status='mirrored',
32-
)
33-
for entry in raw_entries
34-
)
35-
36-
37-
PORTED_TOOLS = load_tool_snapshot()
24+
PORTED_TOOLS = load_porting_modules(SNAPSHOT_PATH)
3825

3926

4027
def build_tool_backlog() -> PortingBacklog:
41-
return PortingBacklog(title='Tool surface', modules=list(PORTED_TOOLS))
28+
return PortingBacklog(title="Tool surface", modules=list(PORTED_TOOLS))
4229

4330

4431
def tool_names() -> list[str]:
@@ -53,10 +40,15 @@ def get_tool(name: str) -> PortingModule | None:
5340
return None
5441

5542

56-
def filter_tools_by_permission_context(tools: tuple[PortingModule, ...], permission_context: ToolPermissionContext | None = None) -> tuple[PortingModule, ...]:
43+
def filter_tools_by_permission_context(
44+
tools: tuple[PortingModule, ...],
45+
permission_context: ToolPermissionContext | None = None,
46+
) -> tuple[PortingModule, ...]:
5747
if permission_context is None:
5848
return tools
59-
return tuple(module for module in tools if not permission_context.blocks(module.name))
49+
return tuple(
50+
module for module in tools if not permission_context.blocks(module.name)
51+
)
6052

6153

6254
def get_tools(
@@ -66,31 +58,56 @@ def get_tools(
6658
) -> tuple[PortingModule, ...]:
6759
tools = list(PORTED_TOOLS)
6860
if simple_mode:
69-
tools = [module for module in tools if module.name in {'BashTool', 'FileReadTool', 'FileEditTool'}]
61+
tools = [
62+
module
63+
for module in tools
64+
if module.name in {"BashTool", "FileReadTool", "FileEditTool"}
65+
]
7066
if not include_mcp:
71-
tools = [module for module in tools if 'mcp' not in module.name.lower() and 'mcp' not in module.source_hint.lower()]
67+
tools = [
68+
module
69+
for module in tools
70+
if "mcp" not in module.name.lower()
71+
and "mcp" not in module.source_hint.lower()
72+
]
7273
return filter_tools_by_permission_context(tuple(tools), permission_context)
7374

7475

7576
def find_tools(query: str, limit: int = 20) -> list[PortingModule]:
7677
needle = query.lower()
77-
matches = [module for module in PORTED_TOOLS if needle in module.name.lower() or needle in module.source_hint.lower()]
78+
matches = [
79+
module
80+
for module in PORTED_TOOLS
81+
if needle in module.name.lower() or needle in module.source_hint.lower()
82+
]
7883
return matches[:limit]
7984

8085

81-
def execute_tool(name: str, payload: str = '') -> ToolExecution:
86+
def execute_tool(name: str, payload: str = "") -> ToolExecution:
8287
module = get_tool(name)
8388
if module is None:
84-
return ToolExecution(name=name, source_hint='', payload=payload, handled=False, message=f'Unknown mirrored tool: {name}')
89+
return ToolExecution(
90+
name=name,
91+
source_hint="",
92+
payload=payload,
93+
handled=False,
94+
message=f"Unknown mirrored tool: {name}",
95+
)
8596
action = f"Mirrored tool '{module.name}' from {module.source_hint} would handle payload {payload!r}."
86-
return ToolExecution(name=module.name, source_hint=module.source_hint, payload=payload, handled=True, message=action)
97+
return ToolExecution(
98+
name=module.name,
99+
source_hint=module.source_hint,
100+
payload=payload,
101+
handled=True,
102+
message=action,
103+
)
87104

88105

89106
def render_tool_index(limit: int = 20, query: str | None = None) -> str:
90107
modules = find_tools(query, limit) if query else list(PORTED_TOOLS[:limit])
91-
lines = [f'Tool entries: {len(PORTED_TOOLS)}', '']
108+
lines = [f"Tool entries: {len(PORTED_TOOLS)}", ""]
92109
if query:
93-
lines.append(f'Filtered by: {query}')
94-
lines.append('')
95-
lines.extend(f'- {module.name}{module.source_hint}' for module in modules)
96-
return '\n'.join(lines)
110+
lines.append(f"Filtered by: {query}")
111+
lines.append("")
112+
lines.extend(f"- {module.name}{module.source_hint}" for module in modules)
113+
return "\n".join(lines)

tests/test_tools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

33
import unittest
4+
from src.snapshot_loader import load_porting_modules
5+
from src.tools import SNAPSHOT_PATH
46
from src.tools import (
5-
load_tool_snapshot,
67
build_tool_backlog,
78
tool_names,
89
get_tool,
@@ -18,8 +19,8 @@
1819

1920

2021
class TestTools(unittest.TestCase):
21-
def test_load_tool_snapshot(self) -> None:
22-
tools = load_tool_snapshot()
22+
def test_load_porting_modules(self) -> None:
23+
tools = load_porting_modules(SNAPSHOT_PATH)
2324
self.assertIsInstance(tools, tuple)
2425
self.assertTrue(len(tools) > 0)
2526
for tool in tools:

0 commit comments

Comments
 (0)