Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

6.2 KB
Raw Blame
"""Closed public projection for context status; no session text crosses here."""
import re
from image_message import matches_archived_message


def _text(value, maximum, pattern=r"^[^\x00-\x1f\x7f]+$"):
    return isinstance(value, str) and 0 < len(value) <= maximum and re.fullmatch(pattern, value) is not None


def _number(value, maximum=100_000_000):
    return type(value) is int and 0 <= value <= maximum


def project_context(value):
    if not isinstance(value, dict) or type(value.get("schemaVersion")) is not int or value["schemaVersion"] != 1:
        raise ValueError("invalid context")
    if value.get("status") not in {"ready", "missing", "busy", "unavailable"}:
        raise ValueError("invalid status")
    revision = value.get("sessionRevision")
    if revision is not None and not _text(revision, 160, r"^[A-Za-z0-9:_-]+$"):
        raise ValueError("invalid revision")
    result = {"schemaVersion": 1, "status": value["status"], "sessionRevision": revision, "context": None, "model": None}
    context, model = value.get("context"), value.get("model")
    if context is not None:
        if (not isinstance(context, dict) or not _number(context.get("used"))
                or not _number(context.get("window"), 10_000_000) or context["window"] < 1
                or not _text(context.get("measuredAt"), 64, r"^[0-9TZ: .+\-]+$")):
            raise ValueError("invalid context measurement")
        result["context"] = {key: context[key] for key in ("used", "window", "measuredAt")}
    if model is not None:
        if (not isinstance(model, dict) or not _text(model.get("id"), 512) or not _text(model.get("provider"), 128)
                or not _number(model.get("contextWindow"), 10_000_000) or model["contextWindow"] < 1
                or context is not None and context["window"] != model["contextWindow"]):
            raise ValueError("invalid model")
        result["model"] = {key: model[key] for key in ("id", "provider", "contextWindow")}
        if "routeFingerprint" in model:
            if not _text(model["routeFingerprint"], 64, r"^[a-f0-9]{64}$"):
                raise ValueError("invalid model route")
            result["model"]["routeFingerprint"] = model["routeFingerprint"]
        if "imageInput" in model:
            if model["imageInput"] not in ("supported", "unsupported", "unknown"):
                raise ValueError("invalid image capability")
            result["model"]["imageInput"] = model["imageInput"]
        if "imageRouteFingerprint" in model:
            if not _text(model["imageRouteFingerprint"], 64, r"^[a-f0-9]{64}$"):
                raise ValueError("invalid image route")
            result["model"]["imageRouteFingerprint"] = model["imageRouteFingerprint"]
    compact = value.get("compaction")
    if (not isinstance(compact, dict) or compact.get("status") not in {"idle", "running", "completed", "skipped", "failed", "unknown"}
            or not _number(compact.get("count"))):
        raise ValueError("invalid compaction")
    result["compaction"] = {key: compact[key] for key in ("status", "count")}
    for key in ("requestId", "reason", "tokensBefore", "tokensAfter"):
        item = compact.get(key)
        if item is None:
            continue
        valid = (_text(item, 128, r"^[A-Za-z0-9_-]+$") if key == "requestId" else
                 _text(item, 96, r"^[a-z][a-z0-9-]*$") if key == "reason" else _number(item))
        if not valid:
            raise ValueError("invalid compaction detail")
        result["compaction"][key] = item
    history = value.get("history")
    if (not isinstance(history, dict) or history.get("status") not in {"ready", "pending", "unknown"}
            or not _number(history.get("acknowledgedMessages"), 2000)
            or history.get("revision") is not None and not _text(history["revision"], 64, r"^[a-f0-9]{64}$")):
        raise ValueError("invalid history")
    result["history"] = {key: history[key] for key in ("status", "revision", "acknowledgedMessages")}
    if history.get("reason") is not None:
        if not _text(history["reason"], 96, r"^[a-z][a-z0-9-]*$"):
            raise ValueError("invalid history reason")
        result["history"]["reason"] = history["reason"]
    return result


def valid_history_snapshot(data):
    snapshot = data.get("history_snapshot")
    if snapshot is None:
        return True  # Existing OpenAI-compatible clients remain supported.
    if (not _text(data.get("request_id"), 128, r"^[A-Za-z0-9_-]+$")
            or not isinstance(snapshot, dict) or set(snapshot) != {"schemaVersion", "messages"}
            or type(snapshot["schemaVersion"]) is not int or snapshot["schemaVersion"] not in (1, 2)
            or not isinstance(snapshot["messages"], list) or not 1 <= len(snapshot["messages"]) <= 2000):
        return False
    size = 0
    identities = {}
    for message in snapshot["messages"]:
        if (not isinstance(message, dict) or set(message) not in ({"role", "content"}, {"role", "content", "images"})
                or message["role"] not in {"user", "assistant"} or not isinstance(message["content"], str)):
            return False
        if "images" in message:
            images = message["images"]
            if (snapshot["schemaVersion"] != 2 or message["role"] != "user"
                    or not isinstance(images, list) or not 1 <= len(images) <= 4):
                return False
            seen = set()
            for image in images:
                if (not isinstance(image, dict) or set(image) != {"id", "sha256"}
                        or not _text(image["id"], 36, r"^img-[a-f0-9]{32}$")
                        or not _text(image["sha256"], 64, r"^[a-f0-9]{64}$")
                        or image["id"] in seen
                        or image["id"] in identities and identities[image["id"]] != image["sha256"]):
                    return False
                seen.add(image["id"])
                identities[image["id"]] = image["sha256"]
                size += len(image["id"]) + len(image["sha256"])
        size += len(message["content"].encode("utf-8"))
        if size > 4 * 1024 * 1024:
            return False
    return bool(data.get("messages")) and matches_archived_message(data["messages"][-1], snapshot["messages"][-1]) and snapshot["messages"][-1]["role"] == "user"