|
| 1 | +"""Prompt-sensitive fake behavior generator; never reads eval IDs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +from trpc_agent_sdk.evaluation import EvalCase, Invocation |
| 8 | + |
| 9 | + |
| 10 | +def user_text(case: EvalCase) -> str: |
| 11 | + return " ".join(part.text or "" for part in case.conversation[0].user_content.parts).lower() |
| 12 | + |
| 13 | + |
| 14 | +def generate_trace(case: EvalCase, prompts: dict[str, str]) -> EvalCase: |
| 15 | + """Generate behavior from user semantics and prompt rules.""" |
| 16 | + changed = case.model_copy(deep=True) |
| 17 | + text = user_text(case) |
| 18 | + router = prompts.get("router_prompt", "") |
| 19 | + skill = prompts.get("skill_prompt", "") |
| 20 | + system = prompts.get("system_prompt", "") |
| 21 | + tool_name = None |
| 22 | + tool_args = None |
| 23 | + if "shipping" in text or "shipment" in text: |
| 24 | + tracking = re.search(r"t-\d+", text).group(0).upper() |
| 25 | + tool_name, tool_args = "track_shipment", {"tracking_id": tracking} |
| 26 | + response = f'{{"tracking_id":"{tracking}","status":"in_transit"}}' |
| 27 | + elif "billing" in text or "invoice" in text: |
| 28 | + invoice = re.search(r"i-\d+", text).group(0).upper() |
| 29 | + tool_name = "create_refund" if "BILLING_TO_REFUND=ON" in router else "get_invoice" |
| 30 | + tool_args = {"invoice_id": invoice} |
| 31 | + response = f'{{"invoice_id":"{invoice}","status":"open"}}' |
| 32 | + else: |
| 33 | + order_match = re.search(r"o-\d+", text) |
| 34 | + if order_match is None: |
| 35 | + response = '{"status":"unsupported"}' |
| 36 | + raw = { |
| 37 | + "user_content": case.conversation[0].user_content.model_dump(mode="json"), |
| 38 | + "final_response": {"role": "model", "parts": [{"text": response}]}, |
| 39 | + } |
| 40 | + changed.actual_conversation = [Invocation.model_validate(raw)] |
| 41 | + return changed |
| 42 | + order = order_match.group(0).upper() |
| 43 | + if "summarize" in text: |
| 44 | + response = ( |
| 45 | + f'{{"status":"pending","order_id":"{order}"}}' |
| 46 | + if "JSON_STATUS=ALWAYS_REQUIRED" in system |
| 47 | + else f'{{"order_id":"{order}"}}' |
| 48 | + ) |
| 49 | + else: |
| 50 | + reason = "duplicate" if "duplicate" in text else "damaged" |
| 51 | + strict = "REFUND_ROUTE=STRICT" in router |
| 52 | + tool_name = "create_refund" if strict or reason == "duplicate" else "get_invoice" |
| 53 | + tool_args = {"order_id": order} |
| 54 | + if "REFUND_REASON=REQUIRED" in skill or reason == "damaged": |
| 55 | + tool_args["reason"] = reason |
| 56 | + response = f'{{"status":"submitted","order_id":"{order}"}}' |
| 57 | + raw = { |
| 58 | + "user_content": case.conversation[0].user_content.model_dump(mode="json"), |
| 59 | + "final_response": {"role": "model", "parts": [{"text": response}]}, |
| 60 | + } |
| 61 | + if tool_name: |
| 62 | + raw["intermediate_data"] = {"tool_uses": [{"name": tool_name, "args": tool_args}]} |
| 63 | + changed.actual_conversation = [Invocation.model_validate(raw)] |
| 64 | + return changed |
0 commit comments