fix(platform): use full_name instead of username for Telegram user nickname (#6657)#6693
fix(platform): use full_name instead of username for Telegram user nickname (#6657)#6693Yaohua-Leo wants to merge 4 commits intoAstrBotDevs:masterfrom
Conversation
- Fix AstrBotDevs#6661: Streaming tool_call arguments lost when OpenAI-compatible proxy omits index field - Gemini and some proxies (e.g. Continue) don't include index field in tool_call deltas - Add default index=0 when missing to prevent ChatCompletionStreamState.handle_chunk() from rejecting chunks Fixes AstrBotDevs#6661
- Use enumerate() to assign correct index based on list position - Iterate over all choices (not just the first) for completeness - Addresses review feedback from sourcery-ai and gemini-code-assist
…ckname Fix AstrBotDevs#6657 - Telegram 暱稱與用戶 ID 問題 Previously, plugins used the Telegram username as the user's nickname, which was inconsistent with the actual display name. Now uses the priority order: full_name > first_name > username > Unknown This ensures plugins can properly match users by their numeric ID instead of relying on username, and the agent can address users by their actual display name.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses two distinct issues: incorrect user nickname resolution in the Telegram adapter and a missing Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- This PR mixes two unrelated fixes (Telegram nickname change and tool_call index patch for #6661); consider splitting them into separate PRs or at least updating the title/description to reflect both changes clearly.
- In the stream tool_call index patch, you only add an index when it is missing or None; if the upstream returns incorrect indices, they will silently pass through—if that's a realistic failure mode for those proxies, you may want to normalize indices based on enumeration regardless of existing values.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- This PR mixes two unrelated fixes (Telegram nickname change and tool_call index patch for #6661); consider splitting them into separate PRs or at least updating the title/description to reflect both changes clearly.
- In the stream tool_call index patch, you only add an index when it is missing or None; if the upstream returns incorrect indices, they will silently pass through—if that's a realistic failure mode for those proxies, you may want to normalize indices based on enumeration regardless of existing values.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/openai_source.py" line_range="310-317" />
<code_context>
state = ChatCompletionStreamState()
async for chunk in stream:
+ # Fix for #6661: Add missing 'index' field to tool_call deltas
+ # Gemini and some OpenAI-compatible proxies omit this field
+ if chunk.choices:
+ for choice in chunk.choices:
+ if choice.delta and choice.delta.tool_calls:
+ for idx, tc in enumerate(choice.delta.tool_calls):
+ if not hasattr(tc, "index") or tc.index is None:
+ tc.index = idx
try:
state.handle_chunk(chunk)
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider moving the tool_call index-normalization inside the existing try/except block
The new normalization logic can raise AttributeError/TypeError (e.g., unexpected `choice` shape or non-iterable `tool_calls`) before we reach `state.handle_chunk(chunk)`, so those errors won’t be caught by the existing `try/except` and will now abort `_query_stream`. To keep behavior closer to before and be more robust to provider-specific schema quirks, consider either moving this entire `if chunk.choices:` block inside the current `try` or wrapping the normalization in its own narrow `try/except` that logs and skips malformed chunks rather than failing the whole stream.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| # Fix for #6661: Add missing 'index' field to tool_call deltas | ||
| # Gemini and some OpenAI-compatible proxies omit this field | ||
| if chunk.choices: | ||
| for choice in chunk.choices: | ||
| if choice.delta and choice.delta.tool_calls: | ||
| for idx, tc in enumerate(choice.delta.tool_calls): | ||
| if not hasattr(tc, "index") or tc.index is None: | ||
| tc.index = idx |
There was a problem hiding this comment.
issue (bug_risk): Consider moving the tool_call index-normalization inside the existing try/except block
The new normalization logic can raise AttributeError/TypeError (e.g., unexpected choice shape or non-iterable tool_calls) before we reach state.handle_chunk(chunk), so those errors won’t be caught by the existing try/except and will now abort _query_stream. To keep behavior closer to before and be more robust to provider-specific schema quirks, consider either moving this entire if chunk.choices: block inside the current try or wrapping the normalization in its own narrow try/except that logs and skips malformed chunks rather than failing the whole stream.
There was a problem hiding this comment.
Code Review
This pull request introduces two fixes. First, it correctly prioritizes the Telegram user's full_name and first_name for the sender's nickname, falling back to username. This is a good improvement for user experience. Second, it adds a compatibility layer for OpenAI-compatible streaming providers by ensuring the index field is present in tool call deltas, which is a necessary fix for handling responses from providers like Gemini. The changes are logical and well-implemented. I've added one suggestion to refactor the new code in openai_source.py for better readability.
| if chunk.choices: | ||
| for choice in chunk.choices: | ||
| if choice.delta and choice.delta.tool_calls: | ||
| for idx, tc in enumerate(choice.delta.tool_calls): | ||
| if not hasattr(tc, "index") or tc.index is None: | ||
| tc.index = idx |
There was a problem hiding this comment.
The logic to patch the index field is correct. However, the nested structure can be simplified to improve readability and reduce nesting. Using or [] for potentially missing chunk.choices and a guard clause for tool_calls can make the code flatter and easier to follow.
| if chunk.choices: | |
| for choice in chunk.choices: | |
| if choice.delta and choice.delta.tool_calls: | |
| for idx, tc in enumerate(choice.delta.tool_calls): | |
| if not hasattr(tc, "index") or tc.index is None: | |
| tc.index = idx | |
| for choice in chunk.choices or []: | |
| if not (choice.delta and choice.delta.tool_calls): | |
| continue | |
| for idx, tc in enumerate(choice.delta.tool_calls): | |
| if getattr(tc, "index", None) is None: | |
| tc.index = idx |
Addresses review feedback from sourcery-ai to ensure robustness against provider-specific schema quirks during streaming.
|
Addressed review feedback: wrapped tool_call index normalization in the try/except block to ensure robustness against provider-specific schema quirks during streaming. |
faf411f to
0068960
Compare
Summary
Fix #6657 - Telegram 暱稱與用戶 ID 問題
Problem
Previously, Telegram adapter used the
usernameas the user's nickname for plugins. This caused:Solution
Changed the nickname priority order to use the actual display name:
Changes
Modified
tg_adapter.py:Testing
The fix maintains backward compatibility by falling back to username if full_name is not available.
Summary by Sourcery
Align Telegram user nickname handling and strengthen tool call streaming compatibility in the OpenAI provider.
Bug Fixes: