File size: 8,328 Bytes
6bedc74
 
 
 
c47cb60
6bedc74
c47cb60
 
 
 
 
 
 
 
b6b28d2
c47cb60
 
 
 
 
6bedc74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47cb60
 
6bedc74
 
 
 
 
 
 
 
 
 
c47cb60
6bedc74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47cb60
6bedc74
 
 
 
 
 
 
 
 
 
 
 
 
 
c47cb60
 
6bedc74
c47cb60
 
 
6bedc74
c47cb60
6bedc74
 
c47cb60
 
6bedc74
c47cb60
 
 
6bedc74
c47cb60
6bedc74
 
c47cb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bedc74
c47cb60
 
 
 
6bedc74
 
c47cb60
 
 
 
 
 
 
6bedc74
 
 
c47cb60
 
 
 
 
b6b28d2
c47cb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bedc74
 
c47cb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bedc74
c47cb60
6bedc74
c47cb60
 
 
 
 
 
 
 
 
6bedc74
 
 
c47cb60
 
 
 
 
6bedc74
c47cb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6b28d2
c47cb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bedc74
 
 
c47cb60
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
295
296
297
298
299
from typing import Any, Dict, List

import gradio as gr

from src.agent import run_agent, run_agent_iter
from src.audit import read_audit_records
from src.demo_presentation import (
    APP_CSS,
    ARCHITECTURE_MARKDOWN,
    AUTHORITY_HTML,
    BUSINESS_CASE_HTML,
    HERO_HTML,
    SECURITY_MARKDOWN,
    TOOL_BELT_HTML,
    idle_activity_html,
    render_activity,
    render_summary,
    starting_activity_html,
)
from src.schemas import AgentRunEvent, AgentRunResult


def get_run_audit_records(
    call_ids: List[str],
) -> List[Dict[str, Any]]:
    """Return audit records belonging only to the current agent run."""

    if not call_ids:
        return []

    records = read_audit_records()

    matching_records = [
        record
        for record in records
        if record.call.call_id in call_ids
    ]

    return [
        record.model_dump(mode="json")
        for record in matching_records
    ]


def handle_request(user_request: str):
    """Preserve the original programmatic callback contract."""

    if not user_request or not user_request.strip():
        return (
            "Please enter a request.",
            [],
            [],
            [],
        )

    try:
        result = run_agent(user_request.strip())

        tool_calls = [
            call.model_dump(mode="json")
            for call in result.tool_calls
        ]
        tool_results = [
            tool_result.model_dump(mode="json")
            for tool_result in result.tool_results
        ]
        call_ids = [
            call.call_id
            for call in result.tool_calls
        ]

        return (
            result.final_answer,
            tool_calls,
            tool_results,
            get_run_audit_records(call_ids),
        )

    except Exception:
        return (
            (
                "Agent execution failed. "
                "Please review the server logs for details."
            ),
            [],
            [],
            [],
        )


def _current_calls(events: list[AgentRunEvent]) -> list[Dict[str, Any]]:
    calls: dict[str, Dict[str, Any]] = {}

    for event in events:
        if event.call is not None:
            calls[event.call.call_id] = event.call.model_dump(mode="json")

    return list(calls.values())


def _current_results(events: list[AgentRunEvent]) -> list[Dict[str, Any]]:
    results: dict[str, Dict[str, Any]] = {}

    for event in events:
        if event.result is not None:
            results[event.result.call_id] = event.result.model_dump(mode="json")

    return list(results.values())


def _stream_outputs(
    events: list[AgentRunEvent],
    final_result: AgentRunResult | None = None,
    complete: bool = False,
):
    calls = (
        [call.model_dump(mode="json") for call in final_result.tool_calls]
        if final_result is not None
        else _current_calls(events)
    )
    results = (
        [result.model_dump(mode="json") for result in final_result.tool_results]
        if final_result is not None
        else _current_results(events)
    )
    call_ids = [call["call_id"] for call in calls]
    audits = get_run_audit_records(call_ids) if complete else []

    answer = (
        final_result.final_answer
        if final_result is not None
        else "*The final business answer will appear after controlled execution completes.*"
    )

    return (
        render_activity(events, complete=complete),
        answer,
        render_summary(final_result),
        calls,
        results,
        audits,
    )


def stream_request(user_request: str):
    """Stream real model/application boundary events into the Gradio demo."""

    if not user_request or not user_request.strip():
        yield (
            idle_activity_html(),
            "Please enter a request.",
            render_summary(None),
            [],
            [],
            [],
        )
        return

    events: list[AgentRunEvent] = []

    yield (
        starting_activity_html(),
        "*The final business answer will appear after controlled execution completes.*",
        render_summary(None),
        [],
        [],
        [],
    )

    try:
        for event in run_agent_iter(user_request.strip()):
            events.append(event)
            final_result = event.final_result
            complete = final_result is not None

            yield _stream_outputs(
                events=events,
                final_result=final_result,
                complete=complete,
            )

    except Exception:
        error_event = AgentRunEvent(
            event="max_rounds_reached",
            message=(
                "The request could not complete. The application stopped the run "
                "without exposing internal server details."
            ),
        )
        events.append(error_event)

        yield (
            render_activity(events, complete=True),
            (
                "Agent execution failed. Please review the server logs for details."
            ),
            render_summary(None),
            _current_calls(events),
            _current_results(events),
            [],
        )


FLAGSHIP_REQUEST = (
    "We may ship equipment to Japan. Find the Electronics items currently in "
    "inventory, calculate an accessory budget of $347 per matching item, and "
    "give me Japan's capital, region, and income classification."
)


with gr.Blocks(
    title="Governed Tool-Using Agent",
    css=APP_CSS,
) as demo:
    with gr.Column(elem_classes=["agent-shell"]):
        gr.HTML(HERO_HTML)
        gr.HTML(BUSINESS_CASE_HTML)

        gr.Markdown("## The approved tool belt")
        gr.HTML(TOOL_BELT_HTML)

        gr.Markdown("## Who controls what?")
        gr.HTML(AUTHORITY_HTML)

        gr.Markdown("## Try a controlled business request")
        request_box = gr.Textbox(
            label="Operations request",
            value=FLAGSHIP_REQUEST,
            placeholder="Ask for work that may require one or more approved tools.",
            lines=4,
        )

        gr.Examples(
            examples=[
                [FLAGSHIP_REQUEST],
                ["What is 347 multiplied by 29?"],
                ["What electronics are currently in inventory?"],
                ["What is the capital, region, and income level of Japan?"],
                [
                    (
                        "How many Electronics items are in inventory, "
                        "and what is 347 multiplied by that number?"
                    )
                ],
            ],
            inputs=request_box,
        )

        run_button = gr.Button(
            "Run controlled request",
            variant="primary",
        )

        activity_output = gr.HTML(idle_activity_html())

        with gr.Tabs():
            with gr.Tab("Business Result"):
                gr.Markdown(
                    "The answer below is produced only after requested capabilities "
                    "pass through the application-controlled boundary."
                )
                final_answer = gr.Markdown(
                    "*Run a request to produce a business answer.*"
                )
                summary_output = gr.HTML(render_summary(None))

            with gr.Tab("Engineering Audit"):
                gr.Markdown(
                    "These are the structured requests, normalized results, and "
                    "current-run audit records behind the business-facing view."
                )
                tool_calls_output = gr.JSON(label="Model-proposed Tool Calls")
                tool_results_output = gr.JSON(label="Controlled Tool Results")
                audit_output = gr.JSON(label="Current Run Audit Records")

            with gr.Tab("Security & Failure Semantics"):
                gr.Markdown(SECURITY_MARKDOWN)

            with gr.Tab("Architecture"):
                gr.Markdown(ARCHITECTURE_MARKDOWN)

        run_button.click(
            fn=stream_request,
            inputs=request_box,
            outputs=[
                activity_output,
                final_answer,
                summary_output,
                tool_calls_output,
                tool_results_output,
                audit_output,
            ],
            show_progress="hidden",
        )


if __name__ == "__main__":
    demo.launch()