From 7f034b4ffad553882af954bc4822cd062566cd70 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Mon, 6 Jan 2025 21:28:36 +0800 Subject: [PATCH] fix(dify): display agent image --- pkg/plugin/context.py | 2 +- pkg/provider/runners/difysvapi.py | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/plugin/context.py b/pkg/plugin/context.py index d00506ef..2131a59a 100644 --- a/pkg/plugin/context.py +++ b/pkg/plugin/context.py @@ -117,7 +117,7 @@ async def initialize(self): # ========== 插件可调用的 API(主程序API) ========== def get_platform_adapters(self) -> list[platform_adapter.MessageSourceAdapter]: - """获取所有消息平台适配器 + """获取已启用的消息平台适配器列表 Returns: list[platform.adapter.MessageSourceAdapter]: 已启用的消息平台适配器列表 diff --git a/pkg/provider/runners/difysvapi.py b/pkg/provider/runners/difysvapi.py index 733d6344..5c3f19bd 100644 --- a/pkg/provider/runners/difysvapi.py +++ b/pkg/provider/runners/difysvapi.py @@ -5,6 +5,8 @@ import uuid import base64 +import aiohttp + from .. import runner from ...core import entities as core_entities from .. import entities as llm_entities @@ -97,7 +99,7 @@ async def _chat_messages( files=files, timeout=self.ap.provider_cfg.data["dify-service-api"]["chat"]["timeout"], ): - self.ap.logger.debug("dify-chat-chunk: ", chunk) + self.ap.logger.debug("dify-chat-chunk: " + str(chunk)) if chunk['event'] == 'workflow_started': mode = "workflow" @@ -149,7 +151,8 @@ async def _agent_chat_messages( files=files, timeout=self.ap.provider_cfg.data["dify-service-api"]["chat"]["timeout"], ): - self.ap.logger.debug("dify-agent-chunk: ", chunk) + self.ap.logger.debug("dify-agent-chunk: " + str(chunk)) + if chunk["event"] in ignored_events: continue if chunk["event"] == "agent_thought": @@ -179,6 +182,21 @@ async def _agent_chat_messages( ], ) yield msg + if chunk['event'] == 'message_file': + + if chunk['type'] == 'image' and chunk['belongs_to'] == 'assistant': + + base_url = self.dify_client.base_url + + if base_url.endswith('/v1'): + base_url = base_url[:-3] + + image_url = base_url + chunk['url'] + + yield llm_entities.Message( + role="assistant", + content=[llm_entities.ContentElement.from_image_url(image_url)], + ) query.session.using_conversation.uuid = chunk["conversation_id"] @@ -215,7 +233,7 @@ async def _workflow_messages( files=files, timeout=self.ap.provider_cfg.data["dify-service-api"]["workflow"]["timeout"], ): - self.ap.logger.debug("dify-workflow-chunk: ", chunk) + self.ap.logger.debug("dify-workflow-chunk: " + str(chunk)) if chunk["event"] in ignored_events: continue