import asyncio

from gvn.harness.messages import Message, ToolCall
from gvn.harness.ports import Reply
from gvn.kernel.schema import Record, Text
from gvn.shortcuts import FunctionTool, run_agent
from gvn.testing.fakes import ScriptedModel


def order_status(order_id: str) -> dict[str, str]:
    return {"order_id": order_id, "status": "shipped"}


tool = FunctionTool(
    name="order_status",
    description="Look up an order's shipping status.",
    arguments=Record({"order_id": Text()}),
    function=order_status,
)
model = ScriptedModel(script=[
    Reply(message=Message.assistant(calls=[
        ToolCall(call_id="lookup-1", name="order_status", arguments={"order_id": "A-17"}),
    ])),
    Reply(message=Message.assistant(text="Order A-17 has shipped.")),
])

run = asyncio.run(run_agent(
    instructions="Look up the order before answering.",
    prompt="Where is order A-17?",
    model=model,
    tools=[tool],
))
assert run.outcome().payload()["tools"][0]["content"]["status"] == "shipped"
print(run.outcome().payload()["text"])
