|
| 1 | +"""`to_model_output` and `next_turn_params` see the schema-parsed input |
| 2 | +`execute` gets, and results stored before an approval pause go through |
| 3 | +`to_model_output` before they reach the model. Mirrors upstream |
| 4 | +`tests/unit/to-model-output-parity.test.ts`. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +from typing import Any, Dict, List |
| 11 | + |
| 12 | +from pydantic import BaseModel |
| 13 | + |
| 14 | +from openrouter_agent import call_model, tool |
| 15 | +from tests._fixtures import ( |
| 16 | + MemoryStateAccessor, |
| 17 | + QueuedClient, |
| 18 | + function_call_item, |
| 19 | + make_response, |
| 20 | + text_response, |
| 21 | + tool_call_response, |
| 22 | +) |
| 23 | + |
| 24 | +PRIVATE = "private-value-must-not-reach-model" |
| 25 | +RAW_ARGS = json.dumps({"count": "3"}) |
| 26 | +PARSED_ARGS = {"count": 3, "mode": "safe"} |
| 27 | + |
| 28 | + |
| 29 | +class LookupArgs(BaseModel): |
| 30 | + count: int |
| 31 | + mode: str = "safe" |
| 32 | + |
| 33 | + |
| 34 | +def _as_dict(value: Any) -> Any: |
| 35 | + return value.model_dump() if isinstance(value, BaseModel) else value |
| 36 | + |
| 37 | + |
| 38 | +def _outputs(request: Dict[str, Any]) -> Dict[str, Any]: |
| 39 | + return { |
| 40 | + i.get("callId", i.get("call_id")): i["output"] |
| 41 | + for i in request["input"] |
| 42 | + if i.get("type") == "function_call_output" |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +def _lookup_tool(seen: List[Dict[str, Any]], **extra: Any) -> Any: |
| 47 | + def execute(params: Any, ctx: Any) -> Dict[str, Any]: |
| 48 | + seen.append({"execute": _as_dict(params)}) |
| 49 | + return {"public_status": "ok", "private_token": PRIVATE} |
| 50 | + |
| 51 | + def to_model_output(args: Dict[str, Any]) -> Dict[str, Any]: |
| 52 | + seen.append({"to_model_output": _as_dict(args["input"])}) |
| 53 | + return {"type": "json", "value": {"public_status": args["output"]["public_status"]}} |
| 54 | + |
| 55 | + return tool( |
| 56 | + name="lookup", |
| 57 | + input_schema=LookupArgs, |
| 58 | + output_schema=dict, |
| 59 | + execute=execute, |
| 60 | + to_model_output=to_model_output, |
| 61 | + **extra, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +async def test_to_model_output_receives_schema_parsed_input() -> None: |
| 66 | + seen: List[Dict[str, Any]] = [] |
| 67 | + client = QueuedClient([tool_call_response("r1", "lookup", arguments=RAW_ARGS), text_response("r2", "done")]) |
| 68 | + |
| 69 | + await call_model(client, {"model": "m", "input": "hi", "tools": [_lookup_tool(seen)]}).get_text() |
| 70 | + |
| 71 | + assert seen == [{"execute": PARSED_ARGS}, {"to_model_output": PARSED_ARGS}] |
| 72 | + |
| 73 | + |
| 74 | +async def test_next_turn_params_receive_schema_parsed_input() -> None: |
| 75 | + seen: List[Dict[str, Any]] = [] |
| 76 | + |
| 77 | + def temperature(args: Any, ctx: Any) -> float: |
| 78 | + seen.append({"next_turn_params": _as_dict(args)}) |
| 79 | + return 0.2 |
| 80 | + |
| 81 | + lookup = _lookup_tool(seen, next_turn_params={"temperature": temperature}) |
| 82 | + client = QueuedClient([tool_call_response("r1", "lookup", arguments=RAW_ARGS), text_response("r2", "done")]) |
| 83 | + |
| 84 | + await call_model(client, {"model": "m", "input": "hi", "tools": [lookup]}).get_text() |
| 85 | + |
| 86 | + assert {"next_turn_params": PARSED_ARGS} in seen |
| 87 | + assert client.requests[1]["temperature"] == 0.2 |
| 88 | + |
| 89 | + |
| 90 | +async def test_unsent_result_from_approval_pause_applies_to_model_output_on_resume() -> None: |
| 91 | + seen: List[Dict[str, Any]] = [] |
| 92 | + state = MemoryStateAccessor() |
| 93 | + deploy = tool( |
| 94 | + name="deploy", |
| 95 | + input_schema=dict, |
| 96 | + output_schema=dict, |
| 97 | + execute=lambda params, ctx: {"ok": True}, |
| 98 | + require_approval=True, |
| 99 | + ) |
| 100 | + tools = [_lookup_tool(seen), deploy] |
| 101 | + first = make_response( |
| 102 | + "r1", |
| 103 | + [function_call_item("call_lookup", "lookup", RAW_ARGS), function_call_item("call_deploy", "deploy")], |
| 104 | + ) |
| 105 | + |
| 106 | + await call_model( |
| 107 | + QueuedClient([first]), {"model": "m", "input": "lookup then deploy", "tools": tools, "state": state} |
| 108 | + ).get_response() |
| 109 | + assert state.stored is not None and state.stored.status == "awaiting_approval" |
| 110 | + |
| 111 | + client = QueuedClient([text_response("r2", "done")]) |
| 112 | + await call_model( |
| 113 | + client, |
| 114 | + {"model": "m", "input": [], "tools": tools, "state": state, "approve_tool_calls": ["call_deploy"]}, |
| 115 | + ).get_text() |
| 116 | + |
| 117 | + outputs = _outputs(client.requests[0]) |
| 118 | + assert PRIVATE not in outputs["call_lookup"] |
| 119 | + assert json.loads(outputs["call_lookup"]) == {"public_status": "ok"} |
| 120 | + assert {"to_model_output": PARSED_ARGS} in seen |
0 commit comments