Skip to content

fix(gsvi_tts): Use the correct calling method#7083

Merged
Soulter merged 5 commits intoAstrBotDevs:masterfrom
Rain-0x01-39:master
Mar 28, 2026
Merged

fix(gsvi_tts): Use the correct calling method#7083
Soulter merged 5 commits intoAstrBotDevs:masterfrom
Rain-0x01-39:master

Conversation

@Rain-0x01-39
Copy link
Copy Markdown
Contributor

@Rain-0x01-39 Rain-0x01-39 commented Mar 28, 2026

修复 GSVI TTS API,使其能正常调用

Closes #5638

Modifications / 改动点

  • astrbot/core/provider/sources/gsvi_tts_source.py 修正 API 调用方法

  • astrbot/core/config/default.py 默认配置

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

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

[2026-03-28 17:05:05.842] [Core] [INFO] [core.event_bus:61]: [default] [ylz_ob(aiocqhttp)] Rain-0x01/***: [At:***] 喵—— 
[2026-03-28 17:05:11.925] [Core] [INFO] [result_decorate.stage:299]: TTS 请求: ...
[2026-03-28 17:07:10.920] [Core] [INFO] [result_decorate.stage:301]: TTS 结果: /AstrBot/data/temp/gsvi_tts_f4011b6d-5b48-43fe-9b11-a6cf129f1a85.wav
[2026-03-28 17:07:10.966] [Core] [INFO] [respond.stage:184]: Prepare to send - Rain-0x01/***: [ComponentType.Record]

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 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.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Update the GSVI TTS provider to use the new authenticated v4 inference API and align the default configuration with the new parameters.

New Features:

  • Support selecting TTS version, prompt text language, and text language for the GSVI TTS provider.

Enhancements:

  • Switch the GSVI TTS integration to use a POST-based infer_single endpoint with API key authentication and structured JSON response handling.
  • Improve error handling for GSVI TTS synthesis and audio download failures.
  • Return audio file paths as strings and use pathlib for temporary file handling.
  • Update default GSVI TTS provider configuration to match the new API schema and defaults.

@auto-assign auto-assign bot requested review from LIghtJUNction and Soulter March 28, 2026 08:47
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Mar 28, 2026
Copy link
Copy Markdown
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.

Hey - I've found 1 issue, and left some high level feedback:

  • In gsvi_tts_source.py you switched to Path(temp_dir) / ... but the module doesn’t appear to import Path from pathlib, which will raise a NameError at runtime.
  • You introduced api_key usage for the GSVI TTS provider but didn’t add a corresponding api_key field to the default provider config in default.py, which may make configuration unclear or lead to missing-key issues for users.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `gsvi_tts_source.py` you switched to `Path(temp_dir) / ...` but the module doesn’t appear to import `Path` from `pathlib`, which will raise a `NameError` at runtime.
- You introduced `api_key` usage for the GSVI TTS provider but didn’t add a corresponding `api_key` field to the default provider config in `default.py`, which may make configuration unclear or lead to missing-key issues for users.

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/gsvi_tts_source.py" line_range="61-62" />
<code_context>
     ) -> None:
         super().__init__(provider_config, provider_settings)
-        self.api_base = provider_config.get("api_base", "http://127.0.0.1:5000")
+        self.api_key = provider_config.get(
+            "api_key"
+        )
+        self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000")
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Validate that api_key is present before using it in the Authorization header.

If `api_key` is missing, the Authorization header will end up as `Bearer None`, which can lead to confusing auth failures or incorrect handling by the backend. Either fail fast when `api_key` is required, or skip setting the Authorization header when `self.api_key` is falsy so misconfiguration is easier to detect.

Suggested implementation:

```python
        self.api_key = provider_config.get("api_key")
        if not self.api_key:
            raise ValueError(
                "Missing required 'api_key' in provider_config for 'gsvi_tts_api' provider"
            )
        self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000")

```

If there are call sites or configuration loaders that might intentionally omit `api_key` (e.g., for unauthenticated/local development use), you may instead want to:
1. Make `api_key` optional in config and
2. Adjust wherever the Authorization header is built to only include it when `self.api_key` is truthy, e.g.:

   ```python
   headers = {"Content-Type": "application/json"}
   if self.api_key:
       headers["Authorization"] = f"Bearer {self.api_key}"
   ```

Those header-building call sites are not visible in the provided snippet and would need to be updated accordingly in the rest of the file.
</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.

Comment thread astrbot/core/provider/sources/gsvi_tts_source.py Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the GSVI TTS provider to support a new API version (v4) and authentication via API keys. The implementation switches from a GET request to a POST request for audio generation and refactors file path handling using the pathlib library. Review feedback suggests adding validation for the API key to prevent invalid requests, including the key in the default configuration, replacing magic strings with constants, and utilizing asynchronous file I/O to avoid blocking the event loop during audio downloads.

Comment thread astrbot/core/provider/sources/gsvi_tts_source.py
Comment thread astrbot/core/config/default.py
resp_json = await response.json()
msg = resp_json.get("msg")
audio_url = resp_json.get("audio_url")
if not msg or msg != "合成成功":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

魔法字符串 "合成成功" 用于判断 API 调用是否成功。建议将其定义为模块级别的常量,例如在文件顶部定义 _SUCCESS_MSG = "合成成功",然后在代码中引用该常量。这会提高代码的可读性和可维护性。

Comment on lines +99 to +100
with open(path, "wb") as f:
f.write(await audio_response.read())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

在异步函数中使用了同步的文件写入操作 open(...)f.write(...),这可能会阻塞事件循环。虽然在单线程 asyncio 事件循环中,同步代码块是原子执行的,不存在竞态条件,但为了保证事件循环的响应性,建议使用 aiofiles 库来进行异步文件操作。

请在文件顶部添加 import aiofiles,然后将此处的文件操作修改为异步方式。

Suggested change
with open(path, "wb") as f:
f.write(await audio_response.read())
async with aiofiles.open(path, "wb") as f:
await f.write(await audio_response.read())
References
  1. In a single-threaded asyncio event loop, synchronous functions (code blocks without 'await') are executed atomically and will not be interrupted by other coroutines. Therefore, they are safe from race conditions when modifying shared state within that block.

@Rain-0x01-39 Rain-0x01-39 changed the title Merge branch 'master' of https://github.com/AstrBotDevs/AstrBot fix(gsvi_tts): Use the correct calling method Mar 28, 2026
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Mar 28, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 28, 2026
@Soulter Soulter merged commit 995a318 into AstrBotDevs:master Mar 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] GSVI TTS(API) 调用不存在的 /tts 接口

2 participants