Skip to content

fix: Improve file URI generation for POSIX paths - #138

Closed
Stlara-F wants to merge 3 commits into
Zhalslar:mainfrom
Stlara-F:revert-127-new
Closed

fix: Improve file URI generation for POSIX paths#138
Stlara-F wants to merge 3 commits into
Zhalslar:mainfrom
Stlara-F:revert-127-new

Conversation

@Stlara-F

@Stlara-F Stlara-F commented Jun 9, 2026

Copy link
Copy Markdown

Reverts #127

ncm.py 因 路径问题 导致语音无法发出

Fix handling of POSIX absolute paths in _to_file_uri method.

The _to_file_uri method in core/sender.py now correctly handles both Windows and POSIX absolute paths by ensuring that the leading slash is preserved after path URI conversion.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@sourcery-ai

sourcery-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor
审阅者指南(在小型 PR 上默认折叠)

审阅者指南

sender._to_file_uri 中恢复此前的文件 URI 格式化逻辑,再次生成 file://// 风格的 URI,以便 AstrBot 现有的 [:8] 前缀截取逻辑能得到正确的绝对路径,从而修复音频路径解析问题。

文件级变更

变更 详情 文件
调整文件 URI 生成逻辑,有意生成与 AstrBot 现有路径截取逻辑兼容的 file://// URI。
  • 确保非绝对路径首先被解析为绝对路径。
  • 通过 as_posix() 将路径转换为 POSIX 风格的字符串。
  • 当 POSIX 路径是绝对路径时,构造并返回形如 file:////{posix_path_without_leading_slash} 的文件 URI。
  • 对于非绝对路径或没有前导斜杠的路径,则回退使用 Path.as_uri()
core/sender.py

可能关联的问题

  • up #1.4.0版本 “No such file or directory”报错请看此贴:该 PR 恢复了自定义的 _to_file_uri,生成 file://// URI,与该问题中针对路径错误提出的修复方案完全一致。

提示与命令

与 Sourcery 交互

  • 触发新的审阅: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub Issue: 在某条审阅评论下回复,请求 Sourcery 从该评论创建一个 issue。你也可以在审阅评论中回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题的任意位置写入 @sourcery-ai,即可随时生成标题。你也可以在 Pull Request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文任意位置写入 @sourcery-ai summary,即可在该位置生成 PR 摘要。你也可以在 Pull Request 中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在 Pull Request 中评论 @sourcery-ai guide,即可在任意时间(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 中评论 @sourcery-ai resolve,将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不希望再看到它们,这会非常有用。
  • 忽略所有 Sourcery 审阅: 在 Pull Request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。当你希望从一次全新的审阅开始时尤其有用——别忘了随后评论 @sourcery-ai review 以触发新的审阅!

自定义你的体验

访问你的控制面板 来:

  • 启用或禁用某些审阅功能,例如 Sourcery 生成的 Pull Request 摘要、审阅者指南等。
  • 更改审阅使用的语言。
  • 添加、移除或编辑自定义审阅说明。
  • 调整其他审阅相关设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Restores the previous file URI formatting logic in sender._to_file_uri to again emit file:////-style URIs so AstrBot’s fixed [8:] prefix stripping yields correct absolute paths, fixing audio path resolution issues.

File-Level Changes

Change Details Files
Adjust file URI generation to intentionally produce file://// URIs compatible with AstrBot’s existing path-stripping logic.
  • Ensure non-absolute paths are first resolved to absolute paths.
  • Convert the path to a POSIX-style string via as_posix().
  • When the POSIX path is absolute, build and return a file URI of the form file:////{posix_path_without_leading_slash}.
  • Fall back to Path.as_uri() for non-absolute paths or paths without a leading slash.
core/sender.py

Possibly linked issues

  • up #1.4.0版本 “No such file or directory”报错请看此贴: PR restores custom _to_file_uri generating file://// URIs, exactly matching the issue’s proposed fix for path errors.

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
Contributor

Choose a reason for hiding this comment

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

Hey - 我发现了 1 个问题,并给出了一些整体反馈:

  • 自定义的 file://// 构造方式是对 Path.as_uri() 一种不太直观的偏离;建议加一条简短的行内注释,说明 AstrBot 需要进行 [8:] 截取的原因,以避免未来的回归问题。
  • posix_path.startswith('/') 这个分支同样会匹配以 // 开头的路径(例如类似 UNC 的路径),会变成 file:////server/share;如果这些是可能出现的输入,可能值得显式地处理或排除它们。
给 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- 自定义的 `file:////` 构造方式是对 `Path.as_uri()` 一种不太直观的偏离;建议加一条简短的行内注释,说明 AstrBot 需要进行 `[8:]` 截取的原因,以避免未来的回归问题。
- `posix_path.startswith('/')` 这个分支同样会匹配以 `//` 开头的路径(例如类似 UNC 的路径),会变成 `file:////server/share`;如果这些是可能出现的输入,可能值得显式地处理或排除它们。

## Individual Comments

### Comment 1
<location path="core/sender.py" line_range="57-63" />
<code_context>
         if not path.is_absolute():
             path = path.resolve()
+        posix_path = path.as_posix()
+        if posix_path.startswith("/"):
+            return f"file:////{posix_path.lstrip('/')}"
         return path.as_uri()

</code_context>
<issue_to_address>
**suggestion (bug_risk):** 条件判断 `startswith('/')` 较为宽泛,可能会捕获一些你并不打算以这种方式归一化的路径(例如类似 UNC 的路径)。

在 Windows 上,像 `//server/share/...` 这样的 UNC 路径会以 POSIX 形式表示,同样会满足 `startswith('/')`,从而得到 `file:////server/share/...`,而不是预期的 `file://server/share/...`。如果你只打算调整本地的绝对 POSIX 路径,建议收紧这个条件(例如加上平台检查,或显式排除 `//host/...`),以避免意外修改 UNC/网络路径。

```suggestion
    def _to_file_uri(self, path: Path) -> str:
        if not path.is_absolute():
            path = path.resolve()
        posix_path = path.as_posix()
        # Only normalize local absolute POSIX paths; avoid touching UNC-like //host/share paths
        if posix_path.startswith("/") and not posix_path.startswith("//"):
            return f"file:////{posix_path.lstrip('/')}"
        return path.as_uri()
```
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进今后的评审。
Original comment in English

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

  • The custom file://// construction is a non-obvious deviation from Path.as_uri(); consider adding a brief inline comment explaining the AstrBot [8:] trimming requirement to avoid future regressions.
  • The posix_path.startswith('/') branch will also trigger for paths starting with // (e.g., UNC-like paths), which become file:////server/share; if those are possible inputs, it may be worth explicitly handling or excluding them.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The custom `file:////` construction is a non-obvious deviation from `Path.as_uri()`; consider adding a brief inline comment explaining the AstrBot `[8:]` trimming requirement to avoid future regressions.
- The `posix_path.startswith('/')` branch will also trigger for paths starting with `//` (e.g., UNC-like paths), which become `file:////server/share`; if those are possible inputs, it may be worth explicitly handling or excluding them.

## Individual Comments

### Comment 1
<location path="core/sender.py" line_range="57-63" />
<code_context>
         if not path.is_absolute():
             path = path.resolve()
+        posix_path = path.as_posix()
+        if posix_path.startswith("/"):
+            return f"file:////{posix_path.lstrip('/')}"
         return path.as_uri()

</code_context>
<issue_to_address>
**suggestion (bug_risk):** The conditional `startswith('/')` is fairly broad and may catch paths (e.g., UNC-like) that you might not intend to normalize this way.

On Windows, UNC paths like `//server/share/...` are represented in POSIX form and will also satisfy `startswith('/')`, giving `file:////server/share/...` instead of the expected `file://server/share/...`. If you only intend to adjust local absolute POSIX paths, consider tightening the condition (e.g., platform check or explicitly excluding `//host/...`) to avoid altering UNC/network paths unexpectedly.

```suggestion
    def _to_file_uri(self, path: Path) -> str:
        if not path.is_absolute():
            path = path.resolve()
        posix_path = path.as_posix()
        # Only normalize local absolute POSIX paths; avoid touching UNC-like //host/share paths
        if posix_path.startswith("/") and not posix_path.startswith("//"):
            return f"file:////{posix_path.lstrip('/')}"
        return path.as_uri()
```
</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 core/sender.py
Fix handling of POSIX absolute paths in _to_file_uri method.
@Stlara-F Stlara-F changed the title Revert "Update sender.py" Improve file URI generation for POSIX paths Jun 10, 2026
@Stlara-F Stlara-F changed the title Improve file URI generation for POSIX paths fix: Improve file URI generation for POSIX paths Jun 10, 2026
@Zhalslar

Copy link
Copy Markdown
Owner

其他各环境下的解析器是否有充足测试,你的改动是否会导致其他部署环境用户报错

@Stlara-F

Copy link
Copy Markdown
Author

仅已测试linux docker环境,因在astrbot拉取最新插件时,发现拉取的版本commit仍处于三个月之前且未有路径错误,直接使用仓库最新源码发现路径错误,最终找到错误来源是此处删除的三行代码,因此在不改变原有逻辑下已针对linux路径错误进行修复

@Zhalslar Zhalslar closed this Jun 22, 2026
@Stlara-F
Stlara-F deleted the revert-127-new branch June 22, 2026 01:29
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