This commit is contained in:
Timothy Jaeryang Baek
2026-06-12 17:30:06 +01:00
Unverified
parent ce934d34e0
commit 7f2d092c95
2 changed files with 19 additions and 7 deletions
+8 -2
View File
@@ -12,6 +12,7 @@ from __future__ import annotations
import asyncio
import json
import logging
import uuid
from collections.abc import AsyncIterator
from typing import Dict, List
@@ -501,11 +502,16 @@ def _to_responses_input(messages: list[dict], instructions: str) -> list[dict]:
elif role == "assistant" and m.get("tool_calls"):
for tc in m["tool_calls"]:
args = tc["function"].get("arguments", "{}")
call_id = tc.get("id", "")
# Responses API requires id to start with "fc_"
fc_id = tc.get("fc_id", "")
if not fc_id or not fc_id.startswith("fc_"):
fc_id = f"fc_{call_id.replace('call_', '', 1) or uuid.uuid4().hex}"
items.append(
{
"type": "function_call",
"id": tc.get("fc_id", tc.get("id", "")),
"call_id": tc.get("id", ""),
"id": fc_id,
"call_id": call_id,
"name": tc["function"]["name"],
"arguments": args if isinstance(args, str) else json.dumps(args),
"status": "completed",
+11 -5
View File
@@ -174,15 +174,18 @@ async def delete_bot_config(bot_id: str) -> bool:
async def find_chat_for_thread(bot_id: str, external_thread_id: str) -> str | None:
"""Find an existing cptr chat_id for a platform thread.
"""Find the most recent cptr chat_id for a platform thread.
Scans chats with matching bridge metadata. This is fine for the
small number of active bridge threads (typically < 100).
Scans chats with matching bridge metadata and returns the newest one,
so /new (which creates a new chat with the same thread ID) takes effect.
"""
from cptr.models import Chat
from cptr.utils.db import get_db
from sqlalchemy import select
best_id = None
best_ts = -1
async with await get_db() as db:
result = await db.execute(
select(Chat).where(Chat.user_id.isnot(None))
@@ -193,8 +196,11 @@ async def find_chat_for_thread(bot_id: str, external_thread_id: str) -> str | No
meta.get("bridge_bot_id") == bot_id
and meta.get("bridge_external_thread_id") == external_thread_id
):
return chat.id
return None
ts = getattr(chat, "created_at", 0) or 0
if ts > best_ts:
best_ts = ts
best_id = chat.id
return best_id
# ── BotManager ───────────────────────────────────────────────