Skip to content

Commit 4d31efa

Browse files
committed
Add debugging for agent conversation in /tmp/conversation.log
Signed-off-by: Swarup Ghosh <swghosh@redhat.com>
1 parent 0973770 commit 4d31efa

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

server/server.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
GET /api-implement?ep_url=<enhancement-pr-url>&cwd=<operator-repo-path>
1010
"""
1111

12+
import json
13+
import logging
1214
import os
1315
import re
14-
import json
16+
import traceback
1517
from pathlib import Path
16-
17-
import anyio
1818
from fastapi import FastAPI, HTTPException, Query
1919
from claude_agent_sdk import (
2020
query,
@@ -46,6 +46,14 @@
4646
PLUGIN_DIR = str(Path(__file__).resolve().parent.parent / "plugins" / "oape")
4747
print(PLUGIN_DIR)
4848

49+
CONVERSATION_LOG = Path("/tmp/conversation.log")
50+
51+
conv_logger = logging.getLogger("conversation")
52+
conv_logger.setLevel(logging.INFO)
53+
_handler = logging.FileHandler(CONVERSATION_LOG)
54+
_handler.setFormatter(logging.Formatter("%(message)s"))
55+
conv_logger.addHandler(_handler)
56+
4957

5058
@app.get("/api-implement")
5159
async def api_implement(
@@ -94,27 +102,42 @@ async def api_implement(
94102

95103
# --- Run the agent ---
96104
output_parts: list[str] = []
105+
conversation: list[dict] = []
97106
cost_usd = 0.0
98107

108+
def _log(role: str, content, **extra):
109+
entry = {"role": role, "content": content, **extra}
110+
conversation.append(entry)
111+
conv_logger.info(f"[{role}] {content}")
112+
113+
conv_logger.info(f"\n{'=' * 60}\n[request] ep_url={ep_url} cwd={working_dir}\n{'=' * 60}")
114+
99115
try:
100116
async for message in query(
101117
prompt=f"/oape:api-implement {ep_url}",
102-
# prompt="explain the enhancement proposal to me like I'm 5 in 10 sentences, {ep_url}",
103118
options=options,
104119
):
105120
if isinstance(message, AssistantMessage):
106121
for block in message.content:
107122
if isinstance(block, TextBlock):
108123
output_parts.append(block.text)
124+
_log("assistant", block.text)
125+
else:
126+
_log(f"assistant:{type(block).__name__}",
127+
json.dumps(getattr(block, "__dict__", str(block)), default=str))
109128
elif isinstance(message, ResultMessage):
110129
cost_usd = message.total_cost_usd
111130
if message.result:
112131
output_parts.append(message.result)
132+
_log("result", message.result, cost_usd=cost_usd)
133+
else:
134+
_log(type(message).__name__,
135+
json.dumps(getattr(message, "__dict__", str(message)), default=str))
113136
except Exception as exc:
114-
raise HTTPException(
115-
status_code=500,
116-
detail=f"Agent execution failed: {exc}",
117-
)
137+
conv_logger.info(f"[error] {traceback.format_exc()}")
138+
raise HTTPException(status_code=500, detail=f"Agent execution failed: {exc}")
139+
140+
conv_logger.info(f"[done] cost=${cost_usd:.4f} parts={len(output_parts)}\n")
118141

119142
return {
120143
"status": "success",

0 commit comments

Comments
 (0)