ForcePilot/backend/package/yuxi/channels/adapters/nostr/session.py
Kris b44aa607e0 feat(nostr adapter): 新增Nostr会话处理和线程模拟工具
新增两个文件实现Nostr适配器的基础工具能力:
1. session.py:实现聊天ID、线程键、代理路由的解析与目标标准化
2. thread_simulator.py:实现Nostr事件线程链构建、标签解析和事件缓存获取功能
2026-05-13 16:13:17 +08:00

32 lines
920 B
Python

from __future__ import annotations
from yuxi.channels.models import ChannelMessage
def resolve_thread_key(message: ChannelMessage) -> str:
identity = message.identity
chat_id = identity.channel_chat_id
if chat_id.startswith("dm:"):
pubkey = chat_id.removeprefix("dm:")
return f"nostr:dm:{pubkey}"
if chat_id.startswith("channel:"):
channel = chat_id.removeprefix("channel:")
return f"nostr:channel:{channel}"
return f"nostr:unknown:{chat_id}"
def resolve_chat_id(message: ChannelMessage) -> str:
return message.identity.channel_chat_id
def resolve_agent_route(message: ChannelMessage, default_agent_id: str = "default") -> str:
thread_key = resolve_thread_key(message)
return f"agent:{default_agent_id}:{thread_key}"
def normalize_target(target: str) -> str:
if target.startswith("nostr:"):
return target
return f"nostr:{target}"