|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage |
| 7 | + |
| 8 | +from eval_protocol.models import Message |
| 9 | + |
| 10 | + |
| 11 | +def _dbg_enabled() -> bool: |
| 12 | + return os.getenv("EP_DEBUG_SERIALIZATION", "0").strip() == "1" |
| 13 | + |
| 14 | + |
| 15 | +def _dbg_print(*args): |
| 16 | + if _dbg_enabled(): |
| 17 | + try: |
| 18 | + print(*args) |
| 19 | + except Exception: |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +def serialize_lc_message_to_ep(msg: BaseMessage) -> Message: |
| 24 | + _dbg_print("[EP-Ser] Input LC msg:", type(msg).__name__, { |
| 25 | + "has_additional_kwargs": isinstance(getattr(msg, "additional_kwargs", None), dict), |
| 26 | + "content_type": type(getattr(msg, "content", None)).__name__, |
| 27 | + }) |
| 28 | + |
| 29 | + if isinstance(msg, HumanMessage): |
| 30 | + ep_msg = Message(role="user", content=str(msg.content)) |
| 31 | + _dbg_print("[EP-Ser] -> EP Message:", {"role": ep_msg.role, "len": len(ep_msg.content or "")}) |
| 32 | + return ep_msg |
| 33 | + |
| 34 | + if isinstance(msg, AIMessage): |
| 35 | + content = "" |
| 36 | + if isinstance(msg.content, str): |
| 37 | + content = msg.content |
| 38 | + elif isinstance(msg.content, list): |
| 39 | + parts: List[str] = [] |
| 40 | + for item in msg.content: |
| 41 | + if isinstance(item, dict): |
| 42 | + if item.get("type") == "text": |
| 43 | + parts.append(str(item.get("text", ""))) |
| 44 | + elif isinstance(item, str): |
| 45 | + parts.append(item) |
| 46 | + content = "\n".join(parts) |
| 47 | + |
| 48 | + tool_calls_payload = None |
| 49 | + |
| 50 | + def _normalize_tool_calls(tc_list: list) -> list[dict]: |
| 51 | + mapped: List[dict] = [] |
| 52 | + for call in tc_list: |
| 53 | + if not isinstance(call, dict): |
| 54 | + continue |
| 55 | + try: |
| 56 | + call_id = call.get("id") or "toolcall_0" |
| 57 | + if isinstance(call.get("function"), dict): |
| 58 | + fn = call["function"] |
| 59 | + fn_name = fn.get("name") or call.get("name") or "tool" |
| 60 | + fn_args = fn.get("arguments") |
| 61 | + else: |
| 62 | + fn_name = call.get("name") or "tool" |
| 63 | + fn_args = call.get("arguments") if call.get("arguments") is not None else call.get("args") |
| 64 | + if not isinstance(fn_args, str): |
| 65 | + import json as _json |
| 66 | + |
| 67 | + fn_args = _json.dumps(fn_args or {}, ensure_ascii=False) |
| 68 | + mapped.append({ |
| 69 | + "id": call_id, |
| 70 | + "type": "function", |
| 71 | + "function": {"name": fn_name, "arguments": fn_args}, |
| 72 | + }) |
| 73 | + except Exception: |
| 74 | + continue |
| 75 | + return mapped |
| 76 | + |
| 77 | + ak = getattr(msg, "additional_kwargs", None) |
| 78 | + if isinstance(ak, dict): |
| 79 | + tc = ak.get("tool_calls") |
| 80 | + if isinstance(tc, list) and tc: |
| 81 | + mapped = _normalize_tool_calls(tc) |
| 82 | + if mapped: |
| 83 | + tool_calls_payload = mapped |
| 84 | + |
| 85 | + if tool_calls_payload is None: |
| 86 | + raw_attr_tc = getattr(msg, "tool_calls", None) |
| 87 | + if isinstance(raw_attr_tc, list) and raw_attr_tc: |
| 88 | + mapped = _normalize_tool_calls(raw_attr_tc) |
| 89 | + if mapped: |
| 90 | + tool_calls_payload = mapped |
| 91 | + |
| 92 | + # Extract reasoning/thinking parts into reasoning_content |
| 93 | + reasoning_content = None |
| 94 | + if isinstance(msg.content, list): |
| 95 | + collected = [it.get("thinking", "") for it in msg.content if isinstance(it, dict) and it.get("type") == "thinking"] |
| 96 | + if collected: |
| 97 | + reasoning_content = "\n\n".join([s for s in collected if s]) or None |
| 98 | + |
| 99 | + ep_msg = Message(role="assistant", content=content, tool_calls=tool_calls_payload, reasoning_content=reasoning_content) |
| 100 | + _dbg_print("[EP-Ser] -> EP Message:", { |
| 101 | + "role": ep_msg.role, |
| 102 | + "content_len": len(ep_msg.content or ""), |
| 103 | + "tool_calls": len(ep_msg.tool_calls or []) if isinstance(ep_msg.tool_calls, list) else 0, |
| 104 | + }) |
| 105 | + return ep_msg |
| 106 | + |
| 107 | + if isinstance(msg, ToolMessage): |
| 108 | + tool_name = msg.name or "tool" |
| 109 | + status = msg.status or "success" |
| 110 | + content = str(msg.content) |
| 111 | + tool_call_id = getattr(msg, "tool_call_id", None) |
| 112 | + ep_msg = Message( |
| 113 | + role="tool", |
| 114 | + name=tool_name, |
| 115 | + tool_call_id=tool_call_id, |
| 116 | + content=f"<{tool_name} status=\"{status}\">\n{content}\n</{tool_name}>", |
| 117 | + ) |
| 118 | + _dbg_print("[EP-Ser] -> EP Message:", {"role": ep_msg.role, "name": ep_msg.name, "has_id": bool(ep_msg.tool_call_id)}) |
| 119 | + return ep_msg |
| 120 | + |
| 121 | + ep_msg = Message(role=getattr(msg, "type", "assistant"), content=str(getattr(msg, "content", ""))) |
| 122 | + _dbg_print("[EP-Ser] -> EP Message (fallback):", {"role": ep_msg.role, "len": len(ep_msg.content or "")}) |
| 123 | + return ep_msg |
| 124 | + |
| 125 | + |
0 commit comments