|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +from flowllm.core.context import C |
| 6 | +from flowllm.core.op import BaseAsyncOp |
| 7 | +from loguru import logger |
| 8 | + |
| 9 | + |
| 10 | +@C.register_op() |
| 11 | +class ReadLocalThsOp(BaseAsyncOp): |
| 12 | + # Class-level cache: {tag: {code: tool_result}} |
| 13 | + _cache: Dict[str, Dict[str, str]] = None |
| 14 | + |
| 15 | + def __init__(self, tag: str = "", **kwargs): |
| 16 | + super().__init__(**kwargs) |
| 17 | + self.tag: str = tag |
| 18 | + # Initialize class-level cache if not exists |
| 19 | + if ReadLocalThsOp._cache is None: |
| 20 | + ReadLocalThsOp._cache = {} |
| 21 | + |
| 22 | + def _load_cache(self) -> Dict[str, str]: |
| 23 | + """Load all crawl_ths_{tag}*.json files and build code->tool_result mapping.""" |
| 24 | + tool_cache_dir = Path("tool_cache") |
| 25 | + pattern = f"crawl_ths_{self.tag}*.json" |
| 26 | + matching_files = list(tool_cache_dir.glob(pattern)) |
| 27 | + |
| 28 | + total_files = len(matching_files) |
| 29 | + logger.info(f"Found {total_files} files matching pattern '{pattern}'") |
| 30 | + |
| 31 | + result_dict = {} |
| 32 | + for idx, file_path in enumerate(matching_files, 1): |
| 33 | + logger.info(f"Loading file [{idx}/{total_files}]: {file_path.name}") |
| 34 | + |
| 35 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 36 | + data = json.load(f) |
| 37 | + |
| 38 | + file_records = 0 |
| 39 | + for item in data: |
| 40 | + code = item['tool_args']['code'] |
| 41 | + result_dict[code] = item['tool_result'] |
| 42 | + file_records += 1 |
| 43 | + |
| 44 | + logger.info(f" → Processed {file_records} records from {file_path.name}") |
| 45 | + |
| 46 | + logger.info(f"✓ Successfully loaded {len(result_dict)} total records for tag={self.tag}") |
| 47 | + return result_dict |
| 48 | + |
| 49 | + async def async_execute(self): |
| 50 | + """Read tool_result for self.context.code from cached data.""" |
| 51 | + # Load cache if not exists |
| 52 | + if self.tag not in ReadLocalThsOp._cache: |
| 53 | + ReadLocalThsOp._cache[self.tag] = self._load_cache() |
| 54 | + |
| 55 | + # Get code from context |
| 56 | + code = self.context.code |
| 57 | + if not code: |
| 58 | + self.context.response.answer = f"No code={code} found in context." |
| 59 | + logger.info(self.context.response.answer) |
| 60 | + return |
| 61 | + |
| 62 | + # Get tool_result from cache |
| 63 | + tool_result = ReadLocalThsOp._cache[self.tag].get(code) |
| 64 | + self.context.response.answer = tool_result |
0 commit comments