Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.0 KB
Raw Blame
"""The metadata values supported by the upload parser's JSON protocol."""

from time import struct_time
from typing import Any


def metadata_for_output(metadata: dict[str, Any], extension: str) -> dict:
    """Encode the registered loaders' metadata without arbitrary objects."""
    result = {}
    for key, value in metadata.items():
        if not isinstance(key, str):
            raise ValueError("invalid parser metadata key")
        if isinstance(value, str):
            result[key] = value
        elif extension == ".enex" and value is None:
            result[key] = None
        elif (
            extension == ".enex"
            and key in {"created", "updated"}
            and isinstance(value, struct_time)
        ):
            result[key] = list(value)
        elif (
            extension == ".enex"
            and key == "tag"
            and isinstance(value, list)
            and all(isinstance(tag, str) for tag in value)
        ):
            result[key] = value
        else:
            raise ValueError("invalid parser metadata value")
    return result


def metadata_from_output(metadata: dict, extension: str | None) -> dict:
    """Restore Evernote dates and retain the existing string-only contract."""
    result = {}
    for key, value in metadata.items():
        if isinstance(value, str):
            result[key] = value
        elif extension == ".enex" and value is None:
            result[key] = None
        elif (
            extension == ".enex"
            and key in {"created", "updated"}
            and isinstance(value, list)
            and len(value) == 9
            and all(type(part) is int for part in value)
        ):
            result[key] = struct_time(value)
        elif (
            extension == ".enex"
            and key == "tag"
            and isinstance(value, list)
            and all(isinstance(tag, str) for tag in value)
        ):
            result[key] = value
        else:
            raise ValueError("invalid parser metadata value")
    return result