|
| 1 | +""" |
| 2 | +Interactive multi-turn testbench of the agent's performance. |
| 3 | +
|
| 4 | +Keeps the conversation history across turns and feeds the returned metadata |
| 5 | +(summary, conversationalStyle) back into the next request, the way the platform |
| 6 | +does — so summarisation and style analysis behave as they do in production. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python tests/manual_agent_chat.py # defaults to example_input_3.json |
| 10 | + python tests/manual_agent_chat.py 1 # use example_input_1.json |
| 11 | + python tests/manual_agent_chat.py 1 --keep-seed # keep the file's canned messages as history |
| 12 | +
|
| 13 | +Commands (at the prompt): |
| 14 | + exit / quit end the session |
| 15 | + /state show the current summary and conversational style |
| 16 | + /system show the system prompt that would be sent for the next turn |
| 17 | + /history show the conversation history |
| 18 | + /reset clear history, summary and style |
| 19 | +""" |
| 20 | + |
| 21 | +import json |
| 22 | +import sys |
| 23 | +import time |
| 24 | + |
| 25 | +try: # line editing and history at the input() prompt |
| 26 | + import readline # noqa: F401 |
| 27 | +except ImportError: |
| 28 | + pass |
| 29 | + |
| 30 | +from lf_toolkit.chat import ChatRequest |
| 31 | +from src.module import chat_module |
| 32 | + |
| 33 | +PATH = "tests/example_inputs/" |
| 34 | +SUMMARISE_AFTER = 11 # mirrors BaseAgent.max_messages_to_summarize |
| 35 | + |
| 36 | + |
| 37 | +def build_request(payload: dict, messages: list, summary: str, style: str) -> ChatRequest: |
| 38 | + """Assemble the next ChatRequest from the running conversation state.""" |
| 39 | + payload = json.loads(json.dumps(payload)) # deep copy, leave the file's data untouched |
| 40 | + payload["messages"] = messages |
| 41 | + payload.setdefault("context", {})["summary"] = summary |
| 42 | + payload.setdefault("user", {}).setdefault("preference", {})["conversationalStyle"] = style |
| 43 | + return ChatRequest.model_validate(payload) |
| 44 | + |
| 45 | + |
| 46 | +def show_system_prompt(payload: dict, messages: list, summary: str, style: str) -> None: |
| 47 | + """Render the system prompt for the next turn without calling the LLM.""" |
| 48 | + from unittest.mock import patch |
| 49 | + |
| 50 | + captured = {} |
| 51 | + |
| 52 | + class CaptureLLM: |
| 53 | + def invoke(self, msgs): |
| 54 | + captured["prompt"] = msgs[0].content |
| 55 | + raise SystemExit # stop before the network call |
| 56 | + |
| 57 | + with patch("src.agent.llm_factory.OpenAILLMs.get_llm", return_value=CaptureLLM()): |
| 58 | + from src.agent.agent import BaseAgent |
| 59 | + |
| 60 | + try: |
| 61 | + BaseAgent().call_model( |
| 62 | + {"messages": [], "summary": summary, "conversationalStyle": style}, |
| 63 | + {"configurable": {"context_prompt": _context_prompt(payload)}}, |
| 64 | + ) |
| 65 | + except SystemExit: |
| 66 | + pass |
| 67 | + print(captured.get("prompt", "(no prompt captured)")) |
| 68 | + |
| 69 | + |
| 70 | +def _context_prompt(payload: dict) -> str: |
| 71 | + from src.agent.context import parse_json_to_prompt |
| 72 | + |
| 73 | + return parse_json_to_prompt( |
| 74 | + payload.get("context") or {}, |
| 75 | + (payload.get("user") or {}).get("taskProgress") or {}, |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + args = [a for a in sys.argv[1:] if not a.startswith("--")] |
| 81 | + keep_seed = "--keep-seed" in sys.argv |
| 82 | + index = args[0] if args else "1" |
| 83 | + input_file = f"{PATH}example_input_{index}.json" |
| 84 | + |
| 85 | + with open(input_file) as f: |
| 86 | + payload = json.load(f) |
| 87 | + |
| 88 | + seed = payload.get("messages", []) if keep_seed else [] |
| 89 | + messages = list(seed) |
| 90 | + summary = (payload.get("context") or {}).get("summary", "") or "" |
| 91 | + style = ((payload.get("user") or {}).get("preference") or {}).get("conversationalStyle", "") or "" |
| 92 | + |
| 93 | + print(f"Loaded {input_file}" |
| 94 | + f"{f' with {len(seed)} seeded messages' if seed else ' (fresh history)'}") |
| 95 | + print("Type your message, or 'exit' to quit. '/system' shows the assembled system prompt.\n") |
| 96 | + |
| 97 | + while True: |
| 98 | + try: |
| 99 | + user_input = input("you > ").strip() |
| 100 | + except (EOFError, KeyboardInterrupt): |
| 101 | + print("\nbye") |
| 102 | + return |
| 103 | + |
| 104 | + if not user_input: |
| 105 | + continue |
| 106 | + if user_input.lower() in ("exit", "quit"): |
| 107 | + print("bye") |
| 108 | + return |
| 109 | + if user_input == "/state": |
| 110 | + print(f"\n[summary]\n{summary or '(empty)'}\n\n[style]\n{style or '(empty)'}\n") |
| 111 | + continue |
| 112 | + if user_input == "/system": |
| 113 | + show_system_prompt(payload, messages, summary, style) |
| 114 | + continue |
| 115 | + if user_input == "/history": |
| 116 | + for m in messages: |
| 117 | + print(f" {m['role']:<9} {m['content'][:100]}") |
| 118 | + print() |
| 119 | + continue |
| 120 | + if user_input == "/reset": |
| 121 | + messages, summary, style = list(seed), "", "" |
| 122 | + print("history, summary and style cleared\n") |
| 123 | + continue |
| 124 | + |
| 125 | + messages.append({"role": "USER", "content": user_input}) |
| 126 | + |
| 127 | + # The agent summarises once the history passes the threshold; flag it so the |
| 128 | + # effect on the next turn's system prompt is visible. |
| 129 | + if len(messages) > SUMMARISE_AFTER: |
| 130 | + print(f"[{len(messages)} messages — summarisation will trigger this turn]") |
| 131 | + |
| 132 | + try: |
| 133 | + request = build_request(payload, messages, summary, style) |
| 134 | + start = time.time() |
| 135 | + response = chat_module(request) |
| 136 | + except Exception as e: |
| 137 | + messages.pop() # don't leave a turn half-applied |
| 138 | + print(f"[error] {type(e).__name__}: {e}\n") |
| 139 | + continue |
| 140 | + |
| 141 | + reply = response.output.content |
| 142 | + print(f"\nbot > {reply}\n") |
| 143 | + |
| 144 | + messages.append({"role": "ASSISTANT", "content": reply}) |
| 145 | + |
| 146 | + metadata = response.metadata or {} |
| 147 | + new_summary = metadata.get("summary", "") or "" |
| 148 | + new_style = metadata.get("conversationalStyle", "") or "" |
| 149 | + if new_summary != summary: |
| 150 | + print("[summary updated — '/state' to view]") |
| 151 | + # history was trimmed server-side; keep only what the summary doesn't cover |
| 152 | + messages = messages[-3:] |
| 153 | + if new_style != style: |
| 154 | + print("[conversational style updated — '/state' to view]") |
| 155 | + summary, style = new_summary, new_style |
| 156 | + |
| 157 | + print(f"[{round((time.time() - start) * 1000)} ms, {len(messages)} messages in history]\n") |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main() |
0 commit comments