"""The HF2 interface. The original repair and live API handlers are shared."""
import base64
import json
from html import escape
import gradio as gr
from ..generators import PRESETS
from ..presets import MODELS, WEB_EXAMPLE_NAMES, WEB_EXAMPLES
from .app import STATIC, _trace_of, make_functions
from .benchmark import benchmark_panel
from .pipeline import STAGES, char_diff
LABELS = dict(box for _, boxes in STAGES for box in boxes)
OUTCOMES = {"pass": "Passed", "fail": "Issue found", "repaired": "Repaired",
"filled": "Filled", "hole": "Needs review", "no_call": "No call",
"skipped": "Skipped"}
CSS = (STATIC / "studio.css").read_text(encoding="utf-8")
# The page is light only. Gradio adds the class "dark" for a dark system theme or ?__theme=dark.
LIGHT_ONLY = """
"""
# Phones only: nothing is wider than the screen. This goes in the page head, because Gradio rewrites the
# selectors inside a media query of its css option.
PHONE = """"""
# The colours of preview.html: teal instead of Gradio's orange, on a light blue-grey page.
TEAL = gr.themes.Color(c50="#eefaf6", c100="#d7f4eb", c200="#b0e8d8", c300="#75bfb1", c400="#2f9d8a", c500="#007f6f",
c600="#007666", c700="#006356", c800="#075347", c900="#06443a", c950="#032a24", name="studio")
THEME = gr.themes.Default(primary_hue=TEAL).set(
body_background_fill="#f2f5fa", body_text_color="#16243a", body_text_color_subdued="#47596f",
border_color_primary="#d9e1eb", color_accent="#007666", link_text_color="#007666", loader_color="#007666",
button_primary_background_fill="#007666", button_primary_background_fill_hover="#006356",
button_primary_border_color="#007666", button_primary_text_color="#ffffff",
button_secondary_background_fill="#ffffff", button_secondary_background_fill_hover="#e7edf4",
button_secondary_border_color="#cdd8e4", input_border_color_focus="#007666")
CAPSULES = ""
# No "Settings" link in the footer: its panel holds the theme switch.
LAUNCH = {"css": CSS, "head": LIGHT_ONLY + PHONE + CAPSULES, "theme": THEME, "footer_links": ["api", "gradio"], "favicon_path": str(STATIC / "icon.png")}
def timeline(result):
trace = _trace_of(result)
if not trace or not trace["steps"]:
return '
Run a repair to see its steps.
'
cards = []
previous_payload = None
for index, step in enumerate(trace["steps"]):
outcome = step["outcome"]
label = LABELS.get(step["box"], step["box"])
before, after = step.get("text_before"), step.get("text_after")
if step.get("payload_after") is not None:
payload = json.dumps(step["payload_after"], ensure_ascii=False, indent=2)
if after is None:
before, after = previous_payload, payload
previous_payload = payload
detail = ""
if after is not None and after != before:
content = char_diff(before, after) if before and len(before) + len(after) < 8000 else escape(after)
detail = f'Inspect JSON
{content}
'
model = ' · Model' if step["model_used"] else ''
cards.append(
f'
A small formatting error can stop the whole request.
Smart Lock repair from the paper
See what changed.
Follow the checks and edits from the original call to the result.
CheckJSON repair pipeline
Try a repair.
Start with Smart Lock in the Demo tab.
46.50% → 99.36%Schema-valid calls on 157 RestGPT queries
"""
METHOD = """
How it works
CheckJSON sits between the model response and the tool. It reads the user request, the tool definition, and the candidate call. It returns a repaired call and a trace your application can inspect.
Start with a real call
Figure 1. A repair in context. The model identifies the Smart Lock, but single quotes break JSON parsing and the tool expects domain to be a list. CheckJSON fixes both issues while keeping the tool name and the other arguments.
The request gives context about the device. The tool definition tells CheckJSON what a valid argument looks like. In this example, the source already says “Entry” and “lock,” so neither value needs to be invented. The repair changes the quote style and wraps the existing domain value in brackets.
Follow the pipeline
Figure 2. The repair pipeline. Follow the call from validation through the relevant repair steps to the final output.
Parse first, then validate. Parsing checks whether the response is legal JSON. Schema validation checks whether the named tool exists and whether its arguments meet the declared types and requirements. A call can parse successfully and still fail validation.
Repair syntax before argument structure. When parsing fails, lexical rules address supported problems such as quotes, punctuation, and brackets. Once the call can be read, schema-aware rules can fix supported mismatches in names and argument types. In the Smart Lock case, fixing the quotes exposes the remaining string-versus-list error.
Use the optional corrector for gaps. If rules leave a hole and a corrector is configured, it can propose a value from the request or help resolve whether a call is needed. Rules-only mode leaves a hole unresolved when it cannot be repaired. Proposed repairs are checked before returning.
Inspect what came back. A valid call can pass through unchanged. A repaired call comes back with a trace of the checks and edits. The output can also indicate an unresolved hole or that a tool call is unnecessary. Schema validation establishes the shape of the call. Your application still checks intent and permission before execution.
In Demo, replay Smart Lock to compare the two failed checks with the two edits. Expand Inspect JSON on a repair step to see the change.
"""
INSTALL = """
Install CheckJSON
Install the prerelease and run a built-in example locally.
python -m pip install --pre checkjson-repair
checkjson example
ckjn tui