提交一波

This commit is contained in:
2024-08-03 02:38:38 +08:00
committed by GitHub
parent c65a24a7aa
commit 933788ffd0

View File

@@ -37,7 +37,9 @@ class dow_markdown(Plugin):
# 避免图片无法下载时,重复调用插件导致没有响应的问题 # 避免图片无法下载时,重复调用插件导致没有响应的问题
if content.startswith("[DOWNLOAD_ERROR]"): if content.startswith("[DOWNLOAD_ERROR]"):
return return
if bool(re.search(r'\!\[[^\]]+\]\(?',content)): has_md = re.search(r'\!\[[^\]]+\]\(?',content)
logger.info(f"{content}是否含有md语法==>{has_md}")
if has_md:
host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801') host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801')
image_path = re.search(r'!\[.*\]\((.*?)\)',reply.content).group(1) image_path = re.search(r'!\[.*\]\((.*?)\)',reply.content).group(1)
logger.info(f"提取到的数据==>host:{host},url:{image_path}") logger.info(f"提取到的数据==>host:{host},url:{image_path}")
@@ -55,37 +57,5 @@ class dow_markdown(Plugin):
logger.warn(f"[dow_markdown] on_decorate_reply failed, content={content}, error={e}") logger.warn(f"[dow_markdown] on_decorate_reply failed, content={content}, error={e}")
finally: finally:
e_context.action = EventAction.CONTINUE e_context.action = EventAction.CONTINUE
def decorate_markdown_image(self, content):
# 完全匹配Coze画图的Markdown图片coze.com对应ciciai.comcoze.cn对应coze.cn
markdown_image_official = r"([\S\s]*)\!?\[(?P<image_name>.*)\]\((?P<image_url>https\:\/\/\S+?\.(ciciai\.com|coze\.cn)\/[\S]*\.png(\?[\S]*)?)\)([\S\s]*)"
match_obj_official = re.fullmatch(markdown_image_official, content)
if match_obj_official and match_obj_official.group('image_url'):
image_name, image_url = match_obj_official.group('image_name'), match_obj_official.group('image_url')
logger.info(f"[dow_markdown] markdown_image_official found, image_name={image_name}, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url)
return [reply]
# 完全匹配一张Markdown图片格式`![name](url)`
markdown_image_single = r"\!\[(?P<image_name>.*)\]\((?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\)"
match_obj_single = re.fullmatch(markdown_image_single, content, re.DOTALL)
if match_obj_single and match_obj_single.group('image_url'):
image_name, image_url = match_obj_single.group('image_name'), match_obj_single.group('image_url')
logger.info(f"[dow_markdown] markdown_image_single found, image_name={image_name}, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url)
return [reply]
# 匹配多张Markdown图片(格式:`url\n![Image](url)`)
markdown_image_multi = r"(?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\n*\!\[Image\]\((?P=image_url)\)"
match_iter_multi = re.finditer(markdown_image_multi, content)
replies = []
for match in match_iter_multi:
image_url = match.group('image_url')
logger.info(f"[dow_markdown] markdown_image_multi found, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url)
replies.append(reply)
if replies:
return replies
if content.startswith('![') and 'http' in content and any(img in content for img in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
logger.info(f"[dow_markdown] it seems markdown image in the content but not matched, content={content}.")
def get_help_text(self, **kwargs): def get_help_text(self, **kwargs):
return "优化返回结果中的图片和网址链接。" return "优化返回结果中的图片和网址链接。"