Skip to content

Conversation

anka-afk
Copy link
Member

@anka-afk anka-afk commented Sep 21, 2025

#2810 相同


Motivation / 动机

问题是由keys初始化格式不当引起的, 但是修改keys的初始值将导致dashboard默认出现一个空key的问题, 不符合逻辑
将keys的初值修补转移到获取时, 并统一供应商使用基类方法获取keys

Modifications / 改动点

Provider基类的方法
几个子类获取keys的逻辑

Verification Steps / 验证步骤

创建空key Provider即可

Screenshots or Test Results / 运行截图或测试结果

image 这是一个无key的供应商, 被正确初始化了

Compatibility & Breaking Changes / 兼容性与破坏性变更

  • 这是一个破坏性变更 (Breaking Change)。/ This is a breaking change.
  • 这不是一个破坏性变更。/ This is NOT a breaking change.

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Sourcery 总结

修复 Provider 类中空键的初始化问题,通过集中默认键逻辑并更新子类使用基础的 get_keys 方法,确保可以在没有显式键的情况下创建提供者。

错误修复:

  • 确保 get_keys 至少返回一个空字符串键,以允许实例化没有配置键的提供者

改进:

  • 将子类中对 provider_config 键的直接访问替换为 super().get_keys()
  • 在提供者源方法中应用一致的格式调整和括号分组,以提高可读性
Original summary in English

Summary by Sourcery

Fix empty key initialization in Provider classes by centralizing default key logic and updating subclasses to use the base get_keys method, ensuring providers can be created without explicit keys.

Bug Fixes:

  • Ensure get_keys returns at least one empty string key to allow instantiation of providers with no configured keys

Enhancements:

  • Replace direct provider_config key access in subclasses with super().get_keys()
  • Apply consistent formatting adjustments and parenthesis grouping in provider source methods for readability

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

你好 - 我已经审查了你的更改,它们看起来很棒!

AI 代理提示
请解决此代码审查中的评论:

## 个别评论

### 评论 1
<location> `astrbot/core/provider/provider.py:72` </location>
<code_context>
        return keys if keys else [""]

</code_context>

<issue_to_address>
**suggestion (code-quality):** 使用 `or` 替换 if 表达式 ([`or-if-exp-identity`](https://docs.sourcery.ai/Reference/Rules-and-In-Line-Suggestions/Python/Default-Rules/or-if-exp-identity))

```suggestion
        return keys or [""]
```

<br/><details><summary>解释</summary>在这里,我们发现如果一个值评估为 `True`,就设置它,否则使用默认值。

“之后”的情况更容易阅读,并且避免了 `input_currency` 的重复。

它的工作原理是先评估左侧。如果它评估为 true,则 `currency` 将设置为此值,并且不会评估右侧。如果它评估为 false,则将评估右侧,并且 `currency` 将设置为 `DEFAULT_CURRENCY`。
</details>
</issue_to_address>

Sourcery 对开源免费 - 如果您喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用这些反馈来改进你的评论。
Original comment in English

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

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `astrbot/core/provider/provider.py:72` </location>
<code_context>
        return keys if keys else [""]

</code_context>

<issue_to_address>
**suggestion (code-quality):** Replace if-expression with `or` ([`or-if-exp-identity`](https://docs.sourcery.ai/Reference/Rules-and-In-Line-Suggestions/Python/Default-Rules/or-if-exp-identity))

```suggestion
        return keys or [""]
```

<br/><details><summary>Explanation</summary>Here we find ourselves setting a value if it evaluates to `True`, and otherwise
using a default.

The 'After' case is a bit easier to read and avoids the duplication of
`input_currency`.

It works because the left-hand side is evaluated first. If it evaluates to
true then `currency` will be set to this and the right-hand side will not be
evaluated. If it evaluates to false the right-hand side will be evaluated and
`currency` will be set to `DEFAULT_CURRENCY`.
</details>
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

anka-afk and others added 2 commits September 21, 2025 18:27
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@Soulter Soulter changed the title fix: 修复空key导致的无法创建Provider对象的问题 fix: 当没有填写 api key 时,设置为空字符串 Oct 12, 2025
@Soulter Soulter merged commit 5e9c7cd into AstrBotDevs:master Oct 12, 2025
5 checks passed
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.

2 participants