Skip to content

fix: add missing case Invalid in checkValid and errorString - #711

Open
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/375785
Open

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/375785

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

In src/common/iflytek_ai_assistant.cpp, the checkValid() method uses a switch(m_status) statement that only handles case NotInstalled: and case NoUserAgreement:. When m_status is Invalid(-1) — its initial value before checkAiExists() async detection completes — the switch falls through to default: break;, returning Invalid without performing any detection. This causes speechToText(), textToSpeech(), and textToTranslate() to abort early via if (Enable != checkValid()), so the right-click menu actions never reach the DBus call to invoke voice dictation.

Key evidence: iflytek_ai_assistant.cpp:169-198 (switch missing case Invalid:), iflytek_ai_assistant.h:73 (m_status initialized to Invalid), minimal demo reproduction confirms dbusCalled=0 before fix and dbusCalled=1 after fix.

Fix Approach

Added case Invalid: Q_FALLTHROUGH(); to both checkValid() and errorString() switch statements, so that when m_status == Invalid, it proceeds to the same detection logic as case NotInstalled: (calling copilotInstalled() and isCopilotEnabled()). This is a 4-line addition across 2 locations in a single file.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The case Invalid: fallthrough to case NotInstalled: does not change existing detection flow for other status values; it only extends the entry condition for the NotInstalled detection path to also cover Invalid
  • All three callers (speechToText, textToSpeech, textToTranslate) use the same checkValid() guard, behavior is consistent across all three right-click menu AI features
  • No change-safety.md was produced (shallow clone with single commit, no blame history available); risk assessment derived from code analysis of all call sites

Business Impact Scope

Affected functional modules: right-click context menu AI features in text editor — voice dictation (语音听写), text-to-speech (语音朗读), and translation (翻译). User scenario: immediately after launching the text editor, right-clicking and selecting any AI assistant feature would silently fail. After the fix, these features will properly trigger detection and invoke the DBus interface.

Verification Suggestion

  1. Launch text editor, immediately right-click and select voice dictation — should invoke uos-ai
  2. After uos-ai detection completes, verify all three AI features (dictation, TTS, translation) work normally
  3. Verify no regression when uos-ai is not installed (should show appropriate error message)

根因分析

在 src/common/iflytek_ai_assistant.cpp 中,checkValid() 方法的 switch(m_status) 语句仅有 case NotInstalled: 和 case NoUserAgreement: 分支。当 m_status 为 Invalid(-1)(异步检测 checkAiExists() 完成前的初始值)时,switch 走 default: break;,直接返回 Invalid 而不执行任何检测。导致 speechToText()、textToSpeech() 和 textToTranslate() 因 if (Enable != checkValid()) 提前返回,右键菜单动作无法到达 DBus 调用。

关键证据:iflytek_ai_assistant.cpp:169-198(switch 缺少 case Invalid:),iflytek_ai_assistant.h:73(m_status 初始化为 Invalid),最小 demo 复现确认改前 dbusCalled=0、改后 dbusCalled=1。

修复方案

在 checkValid() 和 errorString() 的 switch 语句中增加 case Invalid: Q_FALLTHROUGH();,使 m_status == Invalid 时进入与 case NotInstalled: 相同的检测逻辑(调用 copilotInstalled() 和 isCopilotEnabled())。单文件 2 处共 4 行新增。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • case Invalid: fallthrough 到 case NotInstalled: 不改变其他状态值的现有检测流程,仅扩展了 NotInstalled 检测路径的入口条件
  • 三个调用方(speechToText、textToSpeech、textToTranslate)均使用相同的 checkValid() 守卫,行为一致
  • 未产出 change-safety.md(浅克隆仅含单个 commit,无 blame 历史);风险评估基于全部调用点的代码分析

业务影响范围

受影响功能模块:文本编辑器右键菜单 AI 功能 — 语音听写、语音朗读、翻译。用户场景:启动文本编辑器后立即右键选择任意 AI 助手功能会静默失败。修复后这些功能将正确触发检测并调用 DBus 接口。

验证建议

  1. 启动文本编辑器后立即右键使用语音听写 — 应能唤醒 uos-ai
  2. uos-ai 检测完成后验证三个 AI 功能(听写、朗读、翻译)正常工作
  3. 验证 uos-ai 未安装时无回归(应显示适当错误提示)

PMS: BUG-375785

Summary by Sourcery

Bug Fixes:

  • Ensure AI assistant actions perform availability detection when their status is initially invalid, allowing dictation, text-to-speech, and translation to proceed correctly immediately after startup.

1. Root cause: m_status initialized to Invalid(-1), checkAiExists()
   uses async detection so m_status stays Invalid before detection
   completes; checkValid() switch had no case Invalid, falling to
   default:break and returning Invalid, causing speechToText() to
   abort early
2. Fix: add case Invalid with Q_FALLTHROUGH() to checkValid() so it
   proceeds to copilotInstalled check like NotInstalled, and to
   errorString() for explicit handling
3. Impact: right-click menu can now invoke speech-to-text when
   async detection has not yet completed; no behavior change for
   other status values

Log: fix speech-to-text unavailable via right-click menu in text editor

Influence:
1. Test speech-to-text via right-click menu immediately after launch
2. Test speech-to-text after uos-ai detection completes
3. Verify no regression on TTS and translation features

fix: 修复文本编辑器右键菜单无法唤醒语音听写的问题

1. 根因:m_status 初始值为 Invalid(-1),checkAiExists() 异步检测
   完成前 m_status 保持 Invalid,checkValid() 的 switch 无 case
   Invalid 分支,走 default:break 返回 Invalid,导致 speechToText()
   提前返回
2. 方案:在 checkValid() 和 errorString() 的 switch 中增加 case
   Invalid: Q_FALLTHROUGH(),使 Invalid 状态如 NotInstalled 一样
   进入检测流程
3. 影响:右键菜单在异步检测完成前可正常唤醒语音听写,对其余
   状态无行为变化

Log: 修复文本编辑器右键菜单无法唤醒语音听写的问题

Influence:
1. 测试启动后立即通过右键菜单使用语音听写
2. 测试 uos-ai 检测完成后使用语音听写
3. 验证语音朗读和翻译功能无回归

PMS: BUG-375785
@pengfeixx
pengfeixx marked this pull request as draft November 2, 2026 18:16
@sourcery-ai

sourcery-ai Bot commented Nov 2, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The fix handles the assistant’s initial Invalid status by reusing the existing detection flow, allowing dictation, text-to-speech, and translation actions to reach DBus immediately after editor startup while retaining existing behavior for other statuses.

Sequence diagram for initial AI assistant action validation

sequenceDiagram
    actor User
    participant Editor
    participant Assistant as IflytekAiAssistant
    participant Copilot as uos_ai
    participant DBus

    User->>Editor: Select AI feature
    Editor->>Assistant: speechToText() / textToSpeech() / textToTranslate()
    Assistant->>Assistant: checkValid()
    alt m_status == Invalid
        Assistant->>Copilot: copilotInstalled()
        Assistant->>Copilot: isCopilotEnabled()
    end
    Assistant->>DBus: Invoke selected AI action
    DBus->>Copilot: Execute request
Loading

File-Level Changes

Change Details Files
Route the initial Invalid status through the existing AI availability detection path.
  • Add an Invalid case that falls through to NotInstalled in checkValid(), triggering copilot installation and enablement checks before feature guards return.
  • Preserve the same fallback behavior for Invalid in errorString() while allowing the existing unknown-error handling to run.
src/common/iflytek_ai_assistant.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@pengfeixx
pengfeixx marked this pull request as ready for review November 3, 2026 05:48
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复了 iflytek_ai_assistant.cpp 中 checkValid 和 errorString 两个 switch 语句缺少 case Invalid 的问题。使用 Q_FALLTHROUGH() 宏标记有意的 fallthrough,语法正确、逻辑合理、无安全漏洞。四维度均通过,代码质量优秀。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例


本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants