|
6 | 6 | from collections.abc import Awaitable, Callable |
7 | 7 | from datetime import datetime |
8 | 8 |
|
9 | | -from fastapi import APIRouter, Depends, HTTPException, Request, Response, status |
| 9 | +from fastapi import APIRouter, Depends, HTTPException, Request, status |
| 10 | +from fastapi.responses import HTMLResponse |
10 | 11 | from loguru import logger |
11 | | -from sqlalchemy import select, or_ |
| 12 | +from sqlalchemy import select |
12 | 13 | from sqlalchemy.ext.asyncio import AsyncSession |
13 | 14 |
|
14 | 15 | from app.core.permissions import check_agent_access, is_agent_creator, is_agent_expired |
15 | 16 | from app.core.security import get_current_user |
16 | 17 | from app.database import async_session as _async_session, get_db |
17 | 18 | from app.models.channel_config import ChannelConfig |
18 | 19 | from app.models.user import User |
19 | | -from app.models.identity import IdentityProvider |
20 | 20 | from app.schemas.schemas import ChannelConfigCreate, ChannelConfigOut, TokenResponse, UserOut |
21 | 21 | from app.services.feishu_service import feishu_service |
| 22 | +from app.services.llm.utils import convert_chat_messages_to_llm_format, truncate_messages_with_pair_integrity |
22 | 23 | from app.services.storage import agent_upload_key, get_storage_backend, store_agent_upload |
23 | 24 |
|
24 | 25 | router = APIRouter(tags=["feishu"]) |
@@ -265,8 +266,6 @@ async def _save_feishu_tool_call( |
265 | 266 |
|
266 | 267 | # ─── OAuth ────────────────────────────────────────────── |
267 | 268 |
|
268 | | -from fastapi.responses import HTMLResponse, Response |
269 | | - |
270 | 269 | @router.get("/auth/feishu/callback") |
271 | 270 | @router.post("/auth/feishu/callback", response_model=TokenResponse) |
272 | 271 | async def feishu_oauth_callback( |
@@ -673,10 +672,9 @@ async def process_feishu_event(agent_id: uuid.UUID, body: dict): |
673 | 672 | .limit(ctx_size) |
674 | 673 | ) |
675 | 674 | history_msgs = history_result.scalars().all() |
676 | | - history = _build_llm_history_from_chat_messages(list(reversed(history_msgs))) |
| 675 | + history = convert_chat_messages_to_llm_format(reversed(history_msgs)) |
677 | 676 |
|
678 | 677 | # --- Resolve Feishu sender identity & find/create platform user --- |
679 | | - import uuid as _uuid |
680 | 678 | import httpx as _httpx |
681 | 679 |
|
682 | 680 | sender_name = "" |
@@ -732,7 +730,9 @@ async def process_feishu_event(agent_id: uuid.UUID, body: dict): |
732 | 730 | # Cache sender info so feishu_user_search can find them by name |
733 | 731 | if sender_name and sender_open_id: |
734 | 732 | try: |
735 | | - import pathlib as _pl, json as _cj, time as _ct |
| 733 | + import pathlib as _pl |
| 734 | + import json as _cj |
| 735 | + import time as _ct |
736 | 736 | _safe_id = str(agent_id).replace("..", "").replace("/", "") |
737 | 737 | _cache = _pl.Path(f"/data/workspaces/{_safe_id}/feishu_contacts_cache.json") |
738 | 738 | _cache.parent.mkdir(parents=True, exist_ok=True) |
@@ -1221,15 +1221,14 @@ async def _handle_feishu_file( |
1221 | 1221 | chat_id, |
1222 | 1222 | ): |
1223 | 1223 | """Handle incoming file or image messages from Feishu (runs as a background task).""" |
1224 | | - import asyncio, random, json |
| 1224 | + import asyncio |
| 1225 | + import random |
| 1226 | + import json |
1225 | 1227 | from app.models.audit import ChatMessage |
1226 | 1228 | from app.models.agent import Agent as AgentModel |
1227 | | - from app.models.user import User as UserModel |
1228 | 1229 | from app.services.channel_session import find_or_create_channel_session |
1229 | | - from app.core.security import hash_password |
1230 | 1230 | from app.database import async_session as _async_session |
1231 | 1231 | from datetime import datetime as _dt, timezone as _tz |
1232 | | - import uuid as _uuid |
1233 | 1232 | from sqlalchemy import select as _select |
1234 | 1233 |
|
1235 | 1234 | msg_type = message.get("message_type", "file") |
@@ -1404,7 +1403,7 @@ async def _handle_feishu_file( |
1404 | 1403 | .order_by(ChatMessage.created_at.desc()) |
1405 | 1404 | .limit(ctx_size) |
1406 | 1405 | ) |
1407 | | - _history = _build_llm_history_from_chat_messages(list(reversed(_hist_r.scalars().all()))) |
| 1406 | + _history = convert_chat_messages_to_llm_format(reversed(_hist_r.scalars().all())) |
1408 | 1407 |
|
1409 | 1408 | await db.commit() |
1410 | 1409 |
|
@@ -1666,7 +1665,7 @@ async def _call_llm_with_config( |
1666 | 1665 | from app.models.agent import DEFAULT_CONTEXT_WINDOW_SIZE |
1667 | 1666 | ctx_size = agent.context_window_size or DEFAULT_CONTEXT_WINDOW_SIZE |
1668 | 1667 | if history: |
1669 | | - messages.extend(_normalize_history_messages(history)[-ctx_size:]) |
| 1668 | + messages.extend(truncate_messages_with_pair_integrity(history, ctx_size)) |
1670 | 1669 | messages.append({"role": "user", "content": user_text}) |
1671 | 1670 |
|
1672 | 1671 | effective_user_id = user_id or agent_id |
|
0 commit comments