File size: 11,806 Bytes
505a9a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be2ff03
505a9a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4651f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""Live demo of the @mukundakatta agent reliability stack: fit, guard, snap, vet, cast.

Each tab runs the corresponding library against user input so you can see exactly
what it does without installing anything. All five libraries are pure Python,
zero runtime deps.
"""

import json
import gradio as gr

from agentfit import count, fit
from agentguard import policy, check
from agentsnap import diff
from agentvet import validate, adapters as vet_adapters
from agentcast import extract_json, adapters as cast_adapters


# ---------- agentfit ----------------------------------------------------------

DEFAULT_MESSAGES = json.dumps([
    {"role": "system", "content": "You are precise and concise."},
    {"role": "user", "content": "Tell me everything you know about the Roman Empire " * 30},
    {"role": "assistant", "content": "The Roman Empire was a vast civilization " * 30},
    {"role": "user", "content": "Now summarize that in 3 bullets " * 5},
    {"role": "assistant", "content": "Here is a summary " * 30},
    {"role": "user", "content": "What is 2+2?"},
], indent=2)


def fit_demo(messages_json: str, max_tokens: int, model: str, strategy: str, preserve_last_n: int):
    try:
        messages = json.loads(messages_json)
    except json.JSONDecodeError as e:
        return f"❌ Invalid JSON: {e}"
    before_tokens = count(messages, model=model)
    result = fit(
        messages,
        max_tokens=max_tokens,
        model=model,
        strategy=strategy,
        preserve_system=True,
        preserve_last_n=preserve_last_n,
        on_over_budget="return-partial",
    )
    return (
        f"**Before:** {before_tokens} tokens Β· **After:** {result.tokens.after} tokens "
        f"Β· **Budget:** {result.tokens.budget} Β· **Fit:** {result.fit}\n\n"
        f"**Dropped:** {len(result.dropped)} message(s)\n\n"
        f"**Surviving messages:**\n```json\n{json.dumps([dict(m) for m in result.messages], indent=2)}\n```"
    )


# ---------- agentguard --------------------------------------------------------

DEFAULT_POLICY = json.dumps({
    "network": {
        "allow": ["api.openai.com", "*.anthropic.com"],
        "deny": ["evil.example.com"],
    },
}, indent=2)

DEFAULT_URLS = "\n".join([
    "https://api.openai.com/v1/chat/completions",
    "https://api.anthropic.com/v1/messages",
    "https://evil.example.com/leak",
    "https://random.example.org/data",
])


def guard_demo(policy_json: str, urls_text: str):
    try:
        spec = json.loads(policy_json)
    except json.JSONDecodeError as e:
        return f"❌ Invalid JSON: {e}"
    try:
        p = policy(spec)
    except Exception as e:
        return f"❌ Invalid policy: {e}"
    rows = []
    for url in (u.strip() for u in urls_text.splitlines() if u.strip()):
        decision = check(p, url)
        if decision["action"] == "allow":
            rows.append(f"βœ… `{url}` β€” allowed")
        else:
            rows.append(f"❌ `{url}` β€” denied (`{decision['reason']}`)")
    return "\n".join(rows)


# ---------- agentsnap ---------------------------------------------------------

DEFAULT_BASELINE = json.dumps({
    "version": 1,
    "model": "claude-sonnet-4-6",
    "input": "search for python tutorials",
    "output": "Here are 3 results.",
    "tools": [
        {"name": "web_search", "args": {"q": "python tutorials"}, "result_hash": "abc123"},
        {"name": "fetch_page", "args": {"url": "https://example.com"}, "result_hash": "def456"},
    ],
    "error": None,
    "fingerprint": {"node": "20.0", "agentsnap": "0.1.0"},
}, indent=2)

DEFAULT_CURRENT = json.dumps({
    "version": 1,
    "model": "claude-sonnet-4-6",
    "input": "search for python tutorials",
    "output": "Here are 5 results.",
    "tools": [
        {"name": "web_search", "args": {"q": "python tutorials"}, "result_hash": "abc123"},
        {"name": "fetch_page", "args": {"url": "https://example.com"}, "result_hash": "DIFFERENT"},
        {"name": "summarize", "args": {"text": "..."}, "result_hash": "new789"},
    ],
    "error": None,
    "fingerprint": {"node": "20.0", "agentsnap": "0.1.0"},
}, indent=2)


def snap_demo(baseline_json: str, current_json: str):
    try:
        baseline = json.loads(baseline_json)
        current = json.loads(current_json)
    except json.JSONDecodeError as e:
        return f"❌ Invalid JSON: {e}"
    result = diff(baseline, current)
    out = [f"**Status:** `{result.status}`", "", "**Changes:**"]
    if not result.changes:
        out.append("(none β€” traces match)")
    else:
        for change in result.changes:
            out.append(f"- `{change.path}`")
            out.append(f"  - from: `{change.from_!r}`")
            out.append(f"  - to:   `{change.to!r}`")
    return "\n".join(out)


# ---------- agentvet ----------------------------------------------------------

DEFAULT_TOOL_NAME = "send_email"
DEFAULT_SHAPE = json.dumps({
    "to": "str",
    "subject": "str",
    "body": "str",
    "cc": "list?",
}, indent=2)
DEFAULT_ARGS = json.dumps({
    "to": "alice@example.com",
    "body": "hello",
}, indent=2)


def vet_demo(tool_name: str, shape_json: str, args_json: str):
    try:
        shape_spec = json.loads(shape_json)
        args = json.loads(args_json)
    except json.JSONDecodeError as e:
        return f"❌ Invalid JSON: {e}"
    validator = vet_adapters.shape(shape_spec)
    result = validate(tool_name, validator, args)
    if result["valid"]:
        return "βœ… **Valid** β€” args match the schema."
    err = result["error"]
    feedback = err.to_llm_feedback() if hasattr(err, "to_llm_feedback") else err.message
    return (
        f"❌ **Invalid** β€” {err.validation_error}\n\n"
        f"**LLM-friendly retry hint:**\n```\n{feedback}\n```"
    )


# ---------- agentcast ---------------------------------------------------------

DEFAULT_MESSY = """Sure! Here's the product info you asked for:

```json
{
  "name": "Widget Pro",
  "price": 29.99,
  "in_stock": true,
  "tags": ["best-seller", "new"]
}
```

Let me know if you need anything else!"""

DEFAULT_VALIDATE_SHAPE = json.dumps({
    "name": "str",
    "price": "float",
    "in_stock": "bool",
    "tags": "list",
}, indent=2)


def cast_demo(messy_text: str, shape_json: str):
    extracted = extract_json(messy_text)
    if extracted is None:
        return "❌ Could not find any JSON in the text."
    try:
        shape_spec = json.loads(shape_json)
    except json.JSONDecodeError as e:
        return f"❌ Invalid shape JSON: {e}"
    validator = cast_adapters.shape(shape_spec)
    val_result = validator(extracted)
    if val_result["valid"]:
        return (
            f"βœ… **Extracted + validated:**\n```json\n{json.dumps(extracted, indent=2)}\n```"
        )
    return (
        f"⚠️  **Extracted but failed validation:**\n```json\n{json.dumps(extracted, indent=2)}\n```\n\n"
        f"**Validation error:** `{val_result['error']}`"
    )


# ---------- UI ----------------------------------------------------------------

with gr.Blocks(title="The Agent Reliability Stack β€” Live Demo", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # The Agent Reliability Stack β€” Live Demo

        Five small libraries that fix the boring problems every long-running AI agent eventually hits.
        Pick a tab below to see what each one does. Pure Python, zero runtime deps.

        🌐 **Landing page:** https://mukundakatta.github.io/agent-stack/
        πŸ“¦ **PyPI:** [`agentfit-py`](https://pypi.org/project/agentfit-py/) Β· [`agentguard-firewall`](https://pypi.org/project/agentguard-firewall/) Β· [`agentsnap-py`](https://pypi.org/project/agentsnap-py/) Β· [`agentvet-py`](https://pypi.org/project/agentvet-py/) Β· [`agentcast-py`](https://pypi.org/project/agentcast-py/)
        πŸ“¦ **npm:** [`@mukundakatta/agentkit`](https://www.npmjs.com/package/@mukundakatta/agentkit) (one install for the whole stack)
        """
    )

    with gr.Tab("πŸͺŸ fit β€” message truncation"):
        gr.Markdown("**`agentfit`** β€” fit a chat history into a token budget.")
        with gr.Row():
            with gr.Column():
                fit_messages = gr.Code(value=DEFAULT_MESSAGES, language="json", label="Messages (JSON array)", lines=14)
                fit_max = gr.Number(value=200, label="max_tokens")
                fit_model = gr.Dropdown(["claude-sonnet-4-6", "gpt-5", "claude-haiku-4-5", "default"], value="claude-sonnet-4-6", label="Model")
                fit_strategy = gr.Radio(["drop-oldest", "drop-middle", "priority"], value="drop-oldest", label="Strategy")
                fit_preserve = gr.Number(value=2, label="preserve_last_n")
                fit_btn = gr.Button("Fit", variant="primary")
            fit_output = gr.Markdown()
        fit_btn.click(fit_demo, inputs=[fit_messages, fit_max, fit_model, fit_strategy, fit_preserve], outputs=fit_output)

    with gr.Tab("πŸ›‘οΈ guard β€” egress firewall"):
        gr.Markdown("**`agentguard`** β€” check URLs against a declarative network policy before any fetch.")
        with gr.Row():
            with gr.Column():
                guard_policy = gr.Code(value=DEFAULT_POLICY, language="json", label="Policy", lines=10)
                guard_urls = gr.Textbox(value=DEFAULT_URLS, label="URLs to check (one per line)", lines=6)
                guard_btn = gr.Button("Check", variant="primary")
            guard_output = gr.Markdown()
        guard_btn.click(guard_demo, inputs=[guard_policy, guard_urls], outputs=guard_output)

    with gr.Tab("πŸ“Έ snap β€” trace diffing"):
        gr.Markdown("**`agentsnap`** β€” diff two tool-call traces, catch silent regressions.")
        with gr.Row():
            with gr.Column():
                snap_baseline = gr.Code(value=DEFAULT_BASELINE, language="json", label="Baseline trace", lines=14)
            with gr.Column():
                snap_current = gr.Code(value=DEFAULT_CURRENT, language="json", label="Current trace", lines=14)
        snap_btn = gr.Button("Diff", variant="primary")
        snap_output = gr.Markdown()
        snap_btn.click(snap_demo, inputs=[snap_baseline, snap_current], outputs=snap_output)

    with gr.Tab("βœ… vet β€” tool-arg validation"):
        gr.Markdown("**`agentvet`** β€” validate tool-call args before execution; produce LLM-friendly retry hints when wrong.")
        with gr.Row():
            with gr.Column():
                vet_tool = gr.Textbox(value=DEFAULT_TOOL_NAME, label="Tool name")
                vet_shape = gr.Code(value=DEFAULT_SHAPE, language="json", label="Shape (suffix '?' for optional)", lines=8)
                vet_args = gr.Code(value=DEFAULT_ARGS, language="json", label="Args from LLM", lines=8)
                vet_btn = gr.Button("Validate", variant="primary")
            vet_output = gr.Markdown()
        vet_btn.click(vet_demo, inputs=[vet_tool, vet_shape, vet_args], outputs=vet_output)

    with gr.Tab("🎯 cast β€” structured output"):
        gr.Markdown("**`agentcast`** β€” extract JSON from messy LLM text, validate against a shape.")
        with gr.Row():
            with gr.Column():
                cast_text = gr.Textbox(value=DEFAULT_MESSY, label="Messy LLM output", lines=12)
                cast_shape = gr.Code(value=DEFAULT_VALIDATE_SHAPE, language="json", label="Expected shape", lines=8)
                cast_btn = gr.Button("Extract + validate", variant="primary")
            cast_output = gr.Markdown()
        cast_btn.click(cast_demo, inputs=[cast_text, cast_shape], outputs=cast_output)

    gr.Markdown(
        """
        ---

        Built by [Mukunda Katta](https://github.com/MukundaKatta) Β· MIT licensed across the board Β· [GitHub](https://github.com/MukundaKatta)
        """
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)