refactor conversation logging with structured format and additional fields
This commit is contained in:
parent
a42f065332
commit
3fc563e72a
@ -181,11 +181,12 @@ class DepartmentAgent:
|
|||||||
usage_records: List[Dict[str, Any]] = []
|
usage_records: List[Dict[str, Any]] = []
|
||||||
tool_call_records: List[Dict[str, Any]] = []
|
tool_call_records: List[Dict[str, Any]] = []
|
||||||
rounds_executed = 0
|
rounds_executed = 0
|
||||||
CONV_LOGGER.info(
|
self._log_conversation(
|
||||||
"dept=%s ts_code=%s trade_date=%s start",
|
"info",
|
||||||
self.settings.code,
|
"start",
|
||||||
context.ts_code,
|
ts_code=context.ts_code,
|
||||||
context.trade_date,
|
trade_date=context.trade_date,
|
||||||
|
template_meta=template_meta,
|
||||||
)
|
)
|
||||||
|
|
||||||
for round_idx in range(self._max_rounds):
|
for round_idx in range(self._max_rounds):
|
||||||
@ -231,11 +232,11 @@ class DepartmentAgent:
|
|||||||
if tool_calls:
|
if tool_calls:
|
||||||
assistant_record["tool_calls"] = tool_calls
|
assistant_record["tool_calls"] = tool_calls
|
||||||
messages.append(assistant_record)
|
messages.append(assistant_record)
|
||||||
CONV_LOGGER.info(
|
self._log_conversation(
|
||||||
"dept=%s round=%s assistant=%s",
|
"info",
|
||||||
self.settings.code,
|
"assistant_reply",
|
||||||
round_idx + 1,
|
round=round_idx + 1,
|
||||||
assistant_record,
|
assistant=assistant_record,
|
||||||
)
|
)
|
||||||
|
|
||||||
if tool_calls:
|
if tool_calls:
|
||||||
@ -281,12 +282,12 @@ class DepartmentAgent:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
delivered_requests.update(delivered)
|
delivered_requests.update(delivered)
|
||||||
CONV_LOGGER.info(
|
self._log_conversation(
|
||||||
"dept=%s round=%s tool_call=%s response=%s",
|
"info",
|
||||||
self.settings.code,
|
"tool_call",
|
||||||
round_idx + 1,
|
round=round_idx + 1,
|
||||||
call,
|
call=call,
|
||||||
tool_response,
|
tool_response=tool_response,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -300,10 +301,10 @@ class DepartmentAgent:
|
|||||||
extra=LOG_EXTRA,
|
extra=LOG_EXTRA,
|
||||||
)
|
)
|
||||||
final_message = message
|
final_message = message
|
||||||
CONV_LOGGER.warning(
|
self._log_conversation(
|
||||||
"dept=%s rounds_exhausted last_message=%s",
|
"warning",
|
||||||
self.settings.code,
|
"rounds_exhausted",
|
||||||
final_message,
|
last_message=final_message,
|
||||||
)
|
)
|
||||||
|
|
||||||
mutable_context.raw["supplement_transcript"] = list(transcript)
|
mutable_context.raw["supplement_transcript"] = list(transcript)
|
||||||
@ -399,17 +400,19 @@ class DepartmentAgent:
|
|||||||
decision.confidence,
|
decision.confidence,
|
||||||
extra=LOG_EXTRA,
|
extra=LOG_EXTRA,
|
||||||
)
|
)
|
||||||
CONV_LOGGER.info(
|
self._log_conversation(
|
||||||
"dept=%s decision action=%s confidence=%.2f summary=%s",
|
"info",
|
||||||
self.settings.code,
|
"decision",
|
||||||
decision.action.value,
|
action=decision.action.value,
|
||||||
decision.confidence,
|
confidence=decision.confidence,
|
||||||
summary or "",
|
summary=summary or "",
|
||||||
|
signals=decision.signals,
|
||||||
|
risks=decision.risks,
|
||||||
)
|
)
|
||||||
CONV_LOGGER.info(
|
self._log_conversation(
|
||||||
"dept=%s telemetry=%s",
|
"info",
|
||||||
self.settings.code,
|
"telemetry",
|
||||||
json.dumps(telemetry, ensure_ascii=False),
|
telemetry=telemetry,
|
||||||
)
|
)
|
||||||
return decision
|
return decision
|
||||||
|
|
||||||
@ -574,6 +577,36 @@ class DepartmentAgent:
|
|||||||
summary["row_count"] = len(rows)
|
summary["row_count"] = len(rows)
|
||||||
return summary
|
return summary
|
||||||
|
|
||||||
|
def _log_conversation(
|
||||||
|
self,
|
||||||
|
level: str,
|
||||||
|
event: str,
|
||||||
|
**fields: Any,
|
||||||
|
) -> None:
|
||||||
|
lines = [f"[{self.settings.code}] {event}"]
|
||||||
|
for key, value in fields.items():
|
||||||
|
if value is None:
|
||||||
|
continue
|
||||||
|
if isinstance(value, (dict, list)):
|
||||||
|
serialized = json.dumps(value, ensure_ascii=False, indent=2)
|
||||||
|
lines.append(f" {key}:")
|
||||||
|
for line in serialized.splitlines():
|
||||||
|
lines.append(f" {line}")
|
||||||
|
else:
|
||||||
|
text = str(value)
|
||||||
|
if "\n" in text:
|
||||||
|
lines.append(f" {key}:")
|
||||||
|
for segment in text.splitlines():
|
||||||
|
if segment.strip():
|
||||||
|
lines.append(f" {segment}")
|
||||||
|
else:
|
||||||
|
lines.append("")
|
||||||
|
else:
|
||||||
|
lines.append(f" {key}: {text}")
|
||||||
|
message = "\n".join(lines)
|
||||||
|
log_method = getattr(CONV_LOGGER, level, CONV_LOGGER.info)
|
||||||
|
log_method(message)
|
||||||
|
|
||||||
|
|
||||||
def _handle_tool_call(
|
def _handle_tool_call(
|
||||||
self,
|
self,
|
||||||
@ -739,10 +772,10 @@ class DepartmentAgent:
|
|||||||
exc,
|
exc,
|
||||||
extra=LOG_EXTRA,
|
extra=LOG_EXTRA,
|
||||||
)
|
)
|
||||||
CONV_LOGGER.error(
|
self._log_conversation(
|
||||||
"dept=%s legacy_call_failed err=%s",
|
"error",
|
||||||
self.settings.code,
|
"legacy_call_failed",
|
||||||
exc,
|
error=str(exc),
|
||||||
)
|
)
|
||||||
return DepartmentDecision(
|
return DepartmentDecision(
|
||||||
department=self.settings.code,
|
department=self.settings.code,
|
||||||
@ -753,7 +786,11 @@ class DepartmentAgent:
|
|||||||
)
|
)
|
||||||
|
|
||||||
context.raw["supplement_transcript"] = [response]
|
context.raw["supplement_transcript"] = [response]
|
||||||
CONV_LOGGER.info("dept=%s legacy_response=%s", self.settings.code, response)
|
self._log_conversation(
|
||||||
|
"info",
|
||||||
|
"legacy_response",
|
||||||
|
response=response,
|
||||||
|
)
|
||||||
decision_data = _parse_department_response(response)
|
decision_data = _parse_department_response(response)
|
||||||
action = _normalize_action(decision_data.get("action"))
|
action = _normalize_action(decision_data.get("action"))
|
||||||
confidence = _clamp_float(decision_data.get("confidence"), default=0.5)
|
confidence = _clamp_float(decision_data.get("confidence"), default=0.5)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user