File size: 1,289 Bytes
77169b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""会话指纹:基于 system prompt + 首条 user 消息计算 SHA-256 指纹。

同一逻辑对话(相同 system + 相同首条 user)的指纹恒定,
不同对话指纹不同,杜绝上下文污染。
"""

import hashlib

from core.api.schemas import OpenAIMessage


def _norm_content(content: str | list | None) -> str:
    if content is None:
        return ""
    if isinstance(content, str):
        return content.strip()
    # list[OpenAIContentPart]
    parts: list[str] = []
    for p in content:
        if hasattr(p, "type") and p.type == "text" and p.text:
            parts.append(p.text.strip())
    return " ".join(parts)


def compute_conversation_fingerprint(messages: list[OpenAIMessage]) -> str:
    """sha256(system_prompt + first_user_message)[:16]

    Returns empty string if no user message found.
    """
    system_text = ""
    first_user_text = ""
    for m in messages:
        if m.role == "system" and not system_text:
            system_text = _norm_content(m.content)
        elif m.role == "user" and not first_user_text:
            first_user_text = _norm_content(m.content)
            break
    if not first_user_text:
        return ""
    raw = f"{system_text}\n{first_user_text}"
    return hashlib.sha256(raw.encode()).hexdigest()[:16]