-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix:Fixed the issue where the rich text processing in the DingTalk AP… #1759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…I did not account for multiple texts and images, as well as the presence of default line breaks. Also resolved the error in Dify caused by sending only images, which resulted in an empty query.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds file upload support to the DingTalk integration and extends the Dify Service API runner to handle file types beyond images. The changes enable processing of rich text content with multiple elements from DingTalk and uploading various file types to Dify.
Key changes:
- Extended Dify Service API runner to support generic file uploads alongside image uploads
- Added rich text content parsing for DingTalk messages to maintain proper order of text and images
- Updated file upload client to handle multiple input types (Path objects, file paths, file objects, bytes)
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| pkg/provider/runners/difysvapi.py | Renamed image_ids to file_ids, added file type handling, added fallback query text for file-only messages |
| pkg/platform/sources/dingtalk.py | Added rich content parsing with element-based processing, fallback to legacy fields, debug print statement |
| libs/dingtalk_api/dingtalkevent.py | Added rich_content property to access Rich_Content field |
| libs/dify_service_api/v1/client.py | Added file input preprocessing to handle Path, string paths, and file objects before upload |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/provider/runners/difysvapi.py
Outdated
| image_b64, image_format = await image.extract_b64_and_format(ce.image_base64) | ||
| file_bytes = base64.b64decode(image_b64) | ||
| file_upload_resp = await self.dify_client.upload_file( | ||
| file_bytes, |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file type handler is incorrectly accessing ce.image_base64 attribute. Based on the pattern from cozeapi.py line 88, file types should access ce.file attribute instead. This will cause an AttributeError when processing file content elements.
| image_b64, image_format = await image.extract_b64_and_format(ce.image_base64) | |
| file_bytes = base64.b64decode(image_b64) | |
| file_upload_resp = await self.dify_client.upload_file( | |
| file_bytes, | |
| # Assume ce.file is a tuple: (filename, file_bytes, mimetype) | |
| file = ce.file | |
| file_upload_resp = await self.dify_client.upload_file( | |
| file, |
pkg/provider/runners/difysvapi.py
Outdated
| async for chunk in self.dify_client.chat_messages( | ||
| inputs=inputs, | ||
| query=plain_text, | ||
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comma in Chinese text. Should be '如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容' (comma after '文件' before '请读取').
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", | |
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
pkg/provider/runners/difysvapi.py
Outdated
| async for chunk in self.dify_client.chat_messages( | ||
| inputs=inputs, | ||
| query=plain_text, | ||
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comma in Chinese text. Should be '如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容' (comma after '文件' before '请读取').
pkg/provider/runners/difysvapi.py
Outdated
| async for chunk in self.dify_client.chat_messages( | ||
| inputs=inputs, | ||
| query=plain_text, | ||
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comma in Chinese text. Should be '如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容' (comma after '文件' before '请读取').
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", | |
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
pkg/provider/runners/difysvapi.py
Outdated
| async for chunk in self.dify_client.chat_messages( | ||
| inputs=inputs, | ||
| query=plain_text, | ||
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comma in Chinese text. Should be '如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容' (comma after '文件' before '请读取').
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", | |
| query=plain_text if plain_text else "如果是可读取文件,请读取此文件内容,如果是图片就描述此图片中内容", |
| if event.rich_content: | ||
| elements = event.rich_content.get("Elements") | ||
| for element in elements: | ||
| print(element) |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug print statement left in production code. This should be removed or replaced with proper logging.
| print(element) | |
| EventLogger.debug(element) |
| if event.picture: | ||
| yiri_msg_list.append(platform_message.Image(base64=event.picture)) | ||
|
|
||
| # 处理其他类型消息(文件、音频等) |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment '处理其他类型消息(文件、音频等)' is incorrectly positioned. It's inside the else block but the file/audio handling code applies to both branches (rich_content and fallback). The comment should be dedented to align with the if statement at line 58.
| # 处理其他类型消息(文件、音频等) | |
| # 处理其他类型消息(文件、音频等) |
| # 处理 Path 对象 | ||
| if isinstance(file, Path): | ||
| if not file.exists(): | ||
| raise ValueError(f"File not found: {file}") | ||
| with open(file, "rb") as f: | ||
| file = f.read() | ||
|
|
||
| # 处理文件路径字符串 | ||
| elif isinstance(file, str): | ||
| if not os.path.isfile(file): | ||
| raise ValueError(f"File not found: {file}") | ||
| with open(file, "rb") as f: | ||
| file = f.read() | ||
|
|
||
| # 处理文件对象 | ||
| elif hasattr(file, 'read'): | ||
| file = file.read() | ||
| """上传文件""" |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring '上传文件' is misplaced after the preprocessing logic. It should be placed immediately after the function signature at line 114, before any implementation code.
| image_placeholders = [] | ||
|
|
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable image_placeholders is not used.
| image_placeholders = [] |
… plan_text is empty when there is file content, and there is no file (the message could not be parsed) and the content is empty.
…I did not account for multiple texts and images, as well as the presence of default line breaks. Also resolved the error in Dify caused by sending only images, which resulted in an empty query.
概述 / Overview
修复了因为dingtalk的api中富文本处理没有考虑多文多图的情况,以及会有默认换行符。修复了dify只发图片导致的query为空报错的问题
检查清单 / Checklist
PR 作者完成 / For PR author
请在方括号间写
x以打勾 / Please tick the box withx项目维护者完成 / For project maintainer