mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
- Defaults and config aligned with English-first product surface - Adds/removes ASR/TTS paths; drops China-only provider implementations and bundled SenseVoice assets - Westernizes prompts, plugin/tool copy, connection and handler behavior on upstream fixes - Powermem max_tokens and OpenAI no-thinking guard preserved on downstream path
117 lines
3 KiB
Python
117 lines
3 KiB
Python
import json
|
||
from typing import TYPE_CHECKING
|
||
|
||
if TYPE_CHECKING:
|
||
from core.connection import ConnectionHandler
|
||
|
||
TAG = __name__
|
||
EMOJI_MAP = {
|
||
"😂": "funny",
|
||
"😭": "crying",
|
||
"😠": "angry",
|
||
"😔": "sad",
|
||
"😍": "loving",
|
||
"😲": "surprised",
|
||
"😱": "shocked",
|
||
"🤔": "thinking",
|
||
"😌": "relaxed",
|
||
"😴": "sleepy",
|
||
"😜": "silly",
|
||
"🙄": "confused",
|
||
"😶": "neutral",
|
||
"🙂": "happy",
|
||
"😆": "laughing",
|
||
"😳": "embarrassed",
|
||
"😉": "winking",
|
||
"😎": "cool",
|
||
"🤤": "delicious",
|
||
"😘": "kissy",
|
||
"😏": "confident",
|
||
}
|
||
EMOJI_RANGES = [
|
||
(0x1F600, 0x1F64F),
|
||
(0x1F300, 0x1F5FF),
|
||
(0x1F680, 0x1F6FF),
|
||
(0x1F900, 0x1F9FF),
|
||
(0x1FA70, 0x1FAFF),
|
||
(0x2600, 0x26FF),
|
||
(0x2700, 0x27BF),
|
||
]
|
||
|
||
|
||
def get_string_no_punctuation_or_emoji(s):
|
||
"""Trim surrounding whitespace, punctuation, and emoji from a string."""
|
||
chars = list(s)
|
||
# Trim characters from the beginning.
|
||
start = 0
|
||
while start < len(chars) and is_punctuation_or_emoji(chars[start]):
|
||
start += 1
|
||
# Trim characters from the end.
|
||
end = len(chars) - 1
|
||
while end >= start and is_punctuation_or_emoji(chars[end]):
|
||
end -= 1
|
||
return "".join(chars[start : end + 1])
|
||
|
||
|
||
def is_punctuation_or_emoji(char):
|
||
"""Check whether a character is whitespace, punctuation, or emoji."""
|
||
# Punctuation to strip, including ASCII and full-width forms.
|
||
punctuation_set = {
|
||
",",
|
||
",", # Comma
|
||
"。",
|
||
".", # Period
|
||
"!",
|
||
"!", # Exclamation mark
|
||
"“",
|
||
"”",
|
||
'"', # Quotation mark
|
||
":",
|
||
":", # Colon
|
||
"-",
|
||
"-", # Full-width dash
|
||
"、", # Ideographic comma
|
||
"[",
|
||
"]", # Square brackets
|
||
"【",
|
||
"】", # Full-width square brackets
|
||
}
|
||
if char.isspace() or char in punctuation_set:
|
||
return True
|
||
return is_emoji(char)
|
||
|
||
|
||
async def get_emotion(conn: "ConnectionHandler", text):
|
||
"""Extract and send the first matching emotion marker from the text."""
|
||
emoji = "🙂"
|
||
emotion = "happy"
|
||
for char in text:
|
||
if char in EMOJI_MAP:
|
||
emoji = char
|
||
emotion = EMOJI_MAP[char]
|
||
break
|
||
try:
|
||
await conn.websocket.send(
|
||
json.dumps(
|
||
{
|
||
"type": "llm",
|
||
"text": emoji,
|
||
"emotion": emotion,
|
||
"session_id": conn.session_id,
|
||
}
|
||
)
|
||
)
|
||
except Exception as e:
|
||
conn.logger.bind(tag=TAG).warning(f"Failed to send emotion emoji: {e}")
|
||
return
|
||
|
||
|
||
def is_emoji(char):
|
||
"""Check whether a character is an emoji."""
|
||
code_point = ord(char)
|
||
return any(start <= code_point <= end for start, end in EMOJI_RANGES)
|
||
|
||
|
||
def check_emoji(text):
|
||
"""Remove all emoji from the text."""
|
||
return "".join(char for char in text if not is_emoji(char) and char != "\n")
|