refactor(hermes_cli): collapse export text/frontmatter branches
This commit is contained in:
@@ -154,22 +154,18 @@ def _messages(session: Dict[str, Any]) -> List[Dict[str, Any]]:
|
||||
|
||||
|
||||
def _message_text(content: Any) -> str:
|
||||
if content is None:
|
||||
return ""
|
||||
if isinstance(content, list):
|
||||
return "\n".join(part for part in map(_content_part_text, content) if part)
|
||||
return _content_part_text(content)
|
||||
return "" if content is None else _content_part_text(content)
|
||||
|
||||
|
||||
def _content_part_text(part: Any) -> str:
|
||||
if isinstance(part, str):
|
||||
return part
|
||||
if isinstance(part, dict):
|
||||
for key in ("text", "content"):
|
||||
if isinstance(value := part.get(key), str):
|
||||
return value
|
||||
return json.dumps(part, ensure_ascii=False, sort_keys=True)
|
||||
return str(part)
|
||||
if not isinstance(part, dict):
|
||||
return part if isinstance(part, str) else str(part)
|
||||
for key in ("text", "content"):
|
||||
if isinstance(value := part.get(key), str):
|
||||
return value
|
||||
return json.dumps(part, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _format_timestamp(value: Any) -> Optional[str]:
|
||||
|
||||
@@ -31,14 +31,10 @@ def _iso_timestamp(value: Any) -> str:
|
||||
|
||||
|
||||
def _frontmatter_line(key: str, value: Any) -> str:
|
||||
if value is None:
|
||||
shown = "null"
|
||||
elif isinstance(value, bool):
|
||||
shown = "true" if value else "false"
|
||||
elif isinstance(value, (int, float, list)):
|
||||
shown = json.dumps(value, ensure_ascii=False)
|
||||
if value is None or isinstance(value, bool):
|
||||
shown = {None: "null", True: "true", False: "false"}[value]
|
||||
else:
|
||||
shown = json.dumps(str(value), ensure_ascii=False)
|
||||
shown = json.dumps(value if isinstance(value, (int, float, list)) else str(value), ensure_ascii=False)
|
||||
return f"{key}: {shown}"
|
||||
|
||||
|
||||
@@ -60,9 +56,7 @@ def _session_id(session: dict[str, Any]) -> str:
|
||||
|
||||
def _segments(session: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
segments = session.get("segments")
|
||||
if isinstance(segments, list) and segments:
|
||||
return [s for s in segments if isinstance(s, dict)]
|
||||
return [session]
|
||||
return [s for s in segments if isinstance(s, dict)] if isinstance(segments, list) and segments else [session]
|
||||
|
||||
|
||||
def _message_count(session: dict[str, Any]) -> int:
|
||||
@@ -165,10 +159,9 @@ def file_sha256(path: Path | str) -> str:
|
||||
|
||||
|
||||
def verify_export_file(path: Path | str, session: dict[str, Any]) -> tuple[bool, str]:
|
||||
p = Path(path)
|
||||
if not p.exists():
|
||||
if not Path(path).exists():
|
||||
return False, "file missing"
|
||||
text = p.read_text(encoding="utf-8")
|
||||
text = Path(path).read_text(encoding="utf-8")
|
||||
match = _SHA_LINE_RE.search(text)
|
||||
if not match:
|
||||
return False, "sha256 marker missing"
|
||||
|
||||
Reference in New Issue
Block a user