The inspector hands its browser parser only graphs the inspection projection parsed (strict JSON, depth 64, node ceiling); a deeper record is "limited" there, so the legacy fill never runs and no seed shows. The card parsed the raw records instead and showed a seed the inspector did not, e.g. for a 900-deep class_type beside a normal sampler. project_graph() is now the one projection step, used by the inspection response and by the card. The card runs the shared parser on the projected graphs, the same values the browser receives: big integers arrive as their exact digits, so the separate exact-integer re-parse and the parse_js_json flag it needed are gone.
100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
"""Workspace card facts from the metadata records a header open already loaded.
|
|
|
|
The card shows what the inspector shows, so it reads exactly what the
|
|
inspector's browser parser reads: each ``prompt``/``workflow`` record through
|
|
the inspection projection (``project_graph``: strict JSON, the depth and node
|
|
bounds, big integers and decimals as text), and only graphs it parsed, through
|
|
the shared parser (``extract_generation_params``, the Python twin of the
|
|
browser parser the inspector fills from since #251). No separate seed
|
|
heuristic, no graph resolution and no extra file reads. One parse feeds both
|
|
the seed and the positive that confirms a first-line card title.
|
|
"""
|
|
|
|
import re
|
|
from collections.abc import Mapping
|
|
|
|
from .generation_params import (
|
|
PLACEHOLDERS,
|
|
GenerationParamsError,
|
|
extract_generation_params,
|
|
)
|
|
from .inspection_response import project_graph
|
|
|
|
__all__ = ["MAX_RECORD_BYTES", "card_seed", "header_params", "parsed_positive"]
|
|
|
|
# Each record is parsed only up to this many UTF-8 bytes; past it, nothing is read.
|
|
MAX_RECORD_BYTES = 2 * 1024 * 1024
|
|
_RECORD_KEYS = ("prompt", "workflow")
|
|
_INTEGER = re.compile(r"-?[0-9]+\Z")
|
|
|
|
|
|
def _utf8_size_over(text: str, limit: int) -> bool:
|
|
"""True when ``text`` is over ``limit`` UTF-8 bytes, encoding only if unclear.
|
|
|
|
A code point takes one to four bytes, so most records are decided from
|
|
their length alone without copying them.
|
|
"""
|
|
if len(text) > limit:
|
|
return True
|
|
if len(text) * 4 <= limit:
|
|
return False
|
|
return len(text.encode("utf-8", "surrogatepass")) > limit
|
|
|
|
|
|
def header_params(info) -> dict | None:
|
|
"""The shared parser's result for the inspector's view of ``info``'s records.
|
|
|
|
``info`` is a Pillow ``Image.info`` (or the inspection read's metadata).
|
|
A record the projection does not parse is left out, as the inspector
|
|
leaves it out. None when no record parses, a record is not text or is
|
|
over ``MAX_RECORD_BYTES``, or the parser cannot use the graphs.
|
|
"""
|
|
if not isinstance(info, Mapping):
|
|
return None
|
|
graphs = {}
|
|
for key in _RECORD_KEYS:
|
|
text = info.get(key)
|
|
if text is None:
|
|
continue
|
|
if not isinstance(text, str) or _utf8_size_over(text, MAX_RECORD_BYTES):
|
|
return None
|
|
graph = project_graph(text, key)
|
|
if graph["status"] == "parsed":
|
|
graphs[key] = graph["value"]
|
|
if not graphs:
|
|
return None
|
|
try:
|
|
return extract_generation_params(graphs)
|
|
except (GenerationParamsError, RecursionError):
|
|
# Defence in depth: a parser failure gives no card facts, never a 500.
|
|
return None
|
|
|
|
|
|
def card_seed(params: dict | None) -> int | None:
|
|
"""The parsed seed as an integer, else None.
|
|
|
|
The projection carries integers beyond 2**53 as their exact digits, so a
|
|
digit string is the recorded seed (the inspector shows it too). A
|
|
placeholder, other text, a boolean or a decimal gives None.
|
|
"""
|
|
if not isinstance(params, Mapping):
|
|
return None
|
|
value = params.get("seed")
|
|
if isinstance(value, bool):
|
|
return None
|
|
if isinstance(value, int):
|
|
return value
|
|
if isinstance(value, str) and _INTEGER.match(value):
|
|
return int(value)
|
|
return None
|
|
|
|
|
|
def parsed_positive(params: dict | None) -> str | None:
|
|
"""The parsed positive prompt text, or None for the placeholder or blank."""
|
|
if not isinstance(params, Mapping):
|
|
return None
|
|
text = params.get("positivePrompt")
|
|
if not isinstance(text, str) or text == PLACEHOLDERS["positivePrompt"]:
|
|
return None
|
|
return text if text.strip() else None
|