feat: add stateful responses API

This commit is contained in:
Xin Wang
2026-09-07 00:15:32 +08:00
parent ab409ba9c0
commit 43f2cc445e
22 changed files with 2461 additions and 5 deletions
+20 -1
View File
@@ -24,6 +24,8 @@ class DifyBrain(BaseBrain):
def __init__(self):
self._user_id = f"ai-video-{uuid4().hex}"
self._client: AsyncClient | None = None
self._llm: DifyLLMService | None = None
self._restored_conversation_id = ""
def _get_client(self, cfg: AssistantConfig) -> AsyncClient:
if self._client is None:
@@ -54,8 +56,25 @@ class DifyBrain(BaseBrain):
return cfg.greeting
def build_llm(self, cfg: AssistantConfig, context: LLMContext) -> FrameProcessor:
return DifyLLMService(
self._llm = DifyLLMService(
cfg,
client=self._get_client(cfg),
user_id=self._user_id,
conversation_id=self._restored_conversation_id,
)
return self._llm
def restore_text_state(self, snapshot: dict) -> None:
if snapshot.get("type") != "dify":
raise ValueError("Responses runtime state does not belong to Dify")
self._user_id = str(snapshot.get("user_id") or self._user_id)
self._restored_conversation_id = str(snapshot.get("conversation_id") or "")
def text_state_snapshot(self) -> dict:
return {
"type": "dify",
"user_id": self._user_id,
"conversation_id": (
self._llm.conversation_id if self._llm is not None else ""
),
}