feat: add stateful responses API
This commit is contained in:
@@ -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 ""
|
||||
),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user