Download testing/nano_simulation_agents/src/nano_python_agents/events.py from yuhanqin/ut-evaluation: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/nano_simulation_agents/src/nano_python_agents/events.py
- Command line
-
hf download hf://datasets/yuhanqin/ut-evaluation/testing/nano_simulation_agents/src/nano_python_agents/events.py
-
curl -L -o events.py https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/nano_simulation_agents/src/nano_python_agents/events.py
1.78 kB
| """Append-only JSONL events with bounded, JSON-safe fields.""" | |
| from __future__ import annotations | |
| import json | |
| import time | |
| import uuid | |
| from pathlib import Path | |
| from typing import Any | |
| _MAX_FIELD_CHARS = 2000 | |
| _MAX_COLLECTION_ITEMS = 100 | |
| def _sanitize(value: Any) -> Any: | |
| if value is None or isinstance(value, (bool, int, float)): | |
| return value | |
| if isinstance(value, str): | |
| if len(value) <= _MAX_FIELD_CHARS: | |
| return value | |
| omitted = len(value) - _MAX_FIELD_CHARS | |
| return value[:_MAX_FIELD_CHARS] + f"...[clipped {omitted} chars]" | |
| if isinstance(value, dict): | |
| return { | |
| str(key): _sanitize(item) | |
| for key, item in list(value.items())[:_MAX_COLLECTION_ITEMS] | |
| } | |
| if isinstance(value, (list, tuple)): | |
| return [_sanitize(item) for item in value[:_MAX_COLLECTION_ITEMS]] | |
| return _sanitize(repr(value)) | |
| class EventLogger: | |
| def __init__(self, workspace_root: Path, session_id: str | None = None) -> None: | |
| self.session_id = session_id or time.strftime("%Y%m%d-%H%M%S-") + uuid.uuid4().hex[:8] | |
| sessions_dir = workspace_root / ".nano_agent" / "sessions" | |
| sessions_dir.mkdir(parents=True, exist_ok=True) | |
| self.path = sessions_dir / f"{self.session_id}.jsonl" | |
| def log(self, event_type: str, **fields: Any) -> None: | |
| record = {"ts": time.time(), "type": event_type} | |
| record.update({key: _sanitize(value) for key, value in fields.items()}) | |
| with self.path.open("a", encoding="utf-8") as stream: | |
| stream.write(json.dumps(record, ensure_ascii=False, sort_keys=True) + "\n") | |
| class NullEventLogger: | |
| session_id = "null" | |
| path = None | |
| def log(self, event_type: str, **fields: Any) -> None: | |
| return None | |