From c752cc72fdab2cbac0a18972d411bf3cfad766b1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 15 Jul 2026 16:00:22 -0400 Subject: [PATCH] refac --- cptr/utils/ai.py | 69 ++++++++++++++++++----------------------- cptr/utils/chat_task.py | 24 ++++++-------- 2 files changed, 40 insertions(+), 53 deletions(-) diff --git a/cptr/utils/ai.py b/cptr/utils/ai.py index 2d7cbe1..f3e0a30 100644 --- a/cptr/utils/ai.py +++ b/cptr/utils/ai.py @@ -389,26 +389,6 @@ async def stream_anthropic( # ── OpenAI Chat Completions ────────────────────────────────── -def _reasoning_text_from_blocks(blocks) -> list[str]: - texts: list[str] = [] - if isinstance(blocks, str): - return [blocks] if blocks else [] - if not isinstance(blocks, list): - return texts - for block in blocks: - if isinstance(block, str): - if block: - texts.append(block) - continue - if not isinstance(block, dict): - continue - btype = block.get("type") - text = block.get("text") - if btype in ("text", "output_text", "summary_text") and text: - texts.append(text) - return texts - - def _reasoning_items_to_content(items: list[dict]) -> str: """Convert replayable reasoning items to a reasoning_content string. @@ -417,21 +397,24 @@ def _reasoning_items_to_content(items: list[dict]) -> str: """ texts: list[str] = [] for item in items: - texts.extend(_reasoning_text_from_blocks(item.get("content"))) - texts.extend(_reasoning_text_from_blocks(item.get("summary"))) + for blocks in (item.get("content"), item.get("summary")): + if isinstance(blocks, str): + if blocks: + texts.append(blocks) + elif isinstance(blocks, list): + for block in blocks: + if isinstance(block, str): + if block: + texts.append(block) + elif ( + isinstance(block, dict) + and block.get("type") in ("text", "output_text", "summary_text") + and block.get("text") + ): + texts.append(block["text"]) return "\n".join(texts) -def _clean_chat_message(m: dict) -> dict: - out = dict(m) - out.pop("reasoning_items", None) - if out.get("tool_calls"): - out["tool_calls"] = [ - {k: v for k, v in tc.items() if k != "fc_id"} for tc in out["tool_calls"] - ] - return out - - def _to_openai_messages( messages: list[dict], instructions: str, *, provider_type: str = "default" ) -> list[dict]: @@ -461,7 +444,13 @@ def _to_openai_messages( elif block.get("type") == "image": data_uri = f"data:{block.get('media_type', 'image/jpeg')};base64,{block.get('base64', '')}" formatted_content.append({"type": "image_url", "image_url": {"url": data_uri}}) - new_m = _clean_chat_message(m) + new_m = dict(m) + new_m.pop("reasoning_items", None) + if new_m.get("tool_calls"): + new_m["tool_calls"] = [ + {k: v for k, v in tc.items() if k != "fc_id"} + for tc in new_m["tool_calls"] + ] new_m["content"] = formatted_content ri = m.get("reasoning_items") if provider_type == "llama.cpp" and ri and new_m.get("role") == "assistant": @@ -470,7 +459,12 @@ def _to_openai_messages( new_m["reasoning_content"] = rc result.append(new_m) else: - out = _clean_chat_message(m) + out = dict(m) + out.pop("reasoning_items", None) + if out.get("tool_calls"): + out["tool_calls"] = [ + {k: v for k, v in tc.items() if k != "fc_id"} for tc in out["tool_calls"] + ] ri = m.get("reasoning_items") if provider_type == "llama.cpp" and ri and out.get("role") == "assistant": rc = _reasoning_items_to_content(ri) @@ -686,10 +680,9 @@ def _replayable_reasoning_items(items: list[dict] | None, *, provider_type: str) if str(item.get("id", "")).startswith("reasoning-"): continue out = copy.deepcopy(item) - if provider_type == "llama.cpp" and not _reasoning_text_from_blocks(out.get("content")): - text = _reasoning_items_to_content([out]) - if text: - out["content"] = [{"type": "text", "text": text}] + text = _reasoning_items_to_content([out]) + if provider_type == "llama.cpp" and text: + out["content"] = [{"type": "text", "text": text}] replayable.append(out) return replayable diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 1dd0d34..598d8c0 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -909,7 +909,15 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic for item in m.output: itype = item.get("type") if itype == "reasoning": - if not _is_provider_replayable_reasoning_item(item): + if ( + item.get("status") not in (None, "completed") + or str(item.get("id", "")).startswith("reasoning-") + or ( + not item.get("encrypted_content") + and not item.get("reasoning_details") + and _reasoning_text_len(item) <= 0 + ) + ): continue # A reasoning item after we already have outputs means # a new API iteration started — flush the current turn. @@ -1138,20 +1146,6 @@ def _tool_result_for_model(tool_name: str, result: str) -> str: } ) - -def _is_provider_replayable_reasoning_item(item: dict) -> bool: - """Return True for provider reasoning that can be sent back to model APIs.""" - if item.get("type") != "reasoning": - return False - if item.get("status") not in (None, "completed"): - return False - if str(item.get("id", "")).startswith("reasoning-"): - return False - if item.get("encrypted_content") or item.get("reasoning_details"): - return True - return _reasoning_text_len(item) > 0 - - def _append_tool_to_messages( messages: list[dict], event: dict,