Skip to content

fix(app-core): return session media URLs synchronously for unknown sessions - #5336

Merged
liruifengv merged 1 commit into
mainfrom
chengluyu-fix-session-media-url-promise
Nov 24, 2026
Merged

liruifengv merged 1 commit into
mainfrom
chengluyu-fix-session-media-url-promise

Conversation

@chengluyu

@chengluyu chengluyu commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Screenshots / Recordings

在 kimi web 开发环境里(和桌面端是同一套渲染层,无 SSH 主机),先打开页面,再通过 REST 从外部新建一个带图片的会话,然后在侧栏点开它。看对话区域:修复前整段崩溃,修复后图片正常显示。

修复前 修复后
修复前:对话区显示「出错了 / 重试」 修复后:对话和图片正常显示

修复后,在同一个会话里从输入框再上传一张图发送(用户日志里实际的触发方式),也正常显示:

修复后:从输入框发送第二张图

Requirement or Bug

用户在桌面端打开带图片或视频的会话(包括刚发出一条带图的消息时),整个对话区域变成「出错了 / 重试」,DevTools 里持续报 n.url?.startsWith is not a function 和 i.value?.url.startsWith is not a function。点重试无效,只有重启 App 才可能恢复。kimi web 用的是同一套渲染层,同样受影响。这个 PR 之后,会话里的图片和视频在任何会话上都能正常渲染。

Bug Reproduction Steps

  1. 桌面端不配置 SSH 主机,正常启动。
  2. 打开一个渲染进程启动后没有经过会话列表接口加载的本机会话,例如在 CLI、kimi web 或 Remote Control 里新建的会话,或者不在侧栏某项目前 5 个、后来因为有活动才被顶上来的会话。
  3. 在这个会话里发一条带图片的消息,或打开一个有工具截图结果的会话。
  4. 对话区域显示「出错了」,控制台出现上面两种 startsWith 报错,重试后仍然出错。

Root Cause

HostRouter 把 getSessionMediaUrl 和 getFileDownloadUrl 标成 'session' 路由,但这两个方法在 KimiWebApi 里声明为同步返回 string,所有调用方也都同步使用返回值。

sequenceDiagram
    participant View as MediaThumb / MediaTool
    participant Router as HostRouter
    participant Local as 本机 API
    View->>Router: getSessionMediaUrl(sessionId, fileId)
    alt sessionHost 已记录该会话
        Router->>Local: getSessionMediaUrl(...)
        Local-->>View: "…/media/f_xxx"(string)
    else sessionHost 未记录
        Router->>Router: dispatchUnknownSession(async)
        Router-->>View: Promise
        View->>View: url.startsWith(...) → TypeError,整段对话渲染失败
    end
Loading

本机会话的归属只在会话列表接口(首屏每个项目只取前 5 个)和本窗口的 createSession / forkSession 里记录。本机的 sessionCreated 等事件不会记录归属。而且没有远程主机时,dispatchUnknownSession 本身也不会补记,所以这类会话每次调用都会拿到 Promise。另外,算出来的 tool media 按会话缓存,点「重试」也恢复不了。

这次是根本修复:让同步方法在任何情况下都同步返回字符串,不再依赖归属是否已知。问题来自 #5079 的桌面迁移。

Code Changes

新增一种路由 sessionSync,用于按会话路由、但必须同步返回的 URL 方法:

 dispatch(method, route, args)
   if route is 'session' or 'localSession'
     owner known   → call owner host
     owner unknown → async lookup, then call   (returns Promise)
+  if route is 'sessionSync'
+    if owner unknown → start background lookup (errors ignored)
+    call owner host if known, otherwise this machine   (always returns string)
-  getFileDownloadUrl: 'session',
+  getFileDownloadUrl: 'sessionSync',
-  getSessionMediaUrl: 'session',
+  getSessionMediaUrl: 'sessionSync',

归属未知时回落到本机 URL:

  • 没有远程主机时,所有会话都在本机,这就是正确的 URL。
  • 有远程主机时,后台查询记下归属,之后的调用会改用远程主机。实际的图片和视频在有 fileId 时由 AuthMedia 通过异步的 getSessionMediaBlob 加载,灯箱下载也优先走这条路径。这条路径会先查到会话归属,所以远程会话的显示不受影响。

hostRouting.test.ts 原有一条测试断言 getSessionMediaUrl 对未知会话 resolves 出 URL。它断言的是返回 Promise,和 types.ts 里声明的 string 返回类型矛盾,正是这个 bug。这里改成断言同步返回字符串,并在拿到归属后改用远程主机。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
归属未知会话的 getSessionMediaUrl 返回值 Promise(违反接口类型,渲染时崩溃) 立即返回本机 URL,并在后台查询归属 无:所有调用方都按字符串同步使用 不需要
归属未知会话的 getFileDownloadUrl 返回值 Promise 立即返回本机 URL,并在后台查询归属 无:files store 按 string | null 使用 不需要
归属已知会话的上述两个方法 同步调用所属主机 不变 — —
配了 SSH 主机、归属未知的远程会话里,工具结果中的音频 渲染崩溃 第一次渲染时 <audio src> 可能指向本机地址,播放失败 无 无;影响很小,留待后续处理

受影响人群:桌面端和 kimi web 用户,只要打开的会话还没经过会话列表接口加载,且会话里有图片或视频(用户消息里的附件、工具截图、浏览器工具结果),就会受影响。其他按会话路由的 API('session' / 'localSession')完全没动。

测试覆盖与验证
  • packages/app-core/test/hostRouting.test.ts › returns session URLs synchronously and uses the owning host once it is known:覆盖第 1、2 行(同步返回本机 URL)和远程归属记下后改用远程主机。修复前这条测试失败(expected Promise{…} to be 'local-media-url'),修复后通过。
  • pnpm exec vitest run --reporter=agent packages/app-core/test/hostRouting.test.ts packages/app-core/test/conversation.test.ts packages/app-core/test/browserToolMedia.test.ts:73 个测试全部通过。
  • pnpm --filter kimi-code-app run typecheck:通过。
  • pnpm lint:通过。
  • 平台:只在 macOS 上运行过单元测试。改动不涉及路径、子进程或终端输入。
  • 手动复现(pnpm dev:server + pnpm dev:web,隔离的 KIMI_CODE_HOME,本地假模型,无 SSH 主机):先打开 Web 页面,再通过 REST 从外部新建会话,并发一条带图片的提示,然后在页面侧栏点开这个会话。
    • 修复前:对话区显示「出错了 / 重试」,控制台报 props.url?.startsWith is not a function,点重试仍然报错。刷新页面后可以恢复。
    • 修复后:新的外部会话打开后正常显示,图片加载成功(64×64)。在这个会话里从输入框再上传一张图发送,也正常显示,控制台没有报错。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7126e55

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
kimi-code-app Patch
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T06:20:34.971420Z 7126e55 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@7126e55
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7126e55

commit: 7126e55

@liruifengv
liruifengv merged commit dee503c into main Nov 24, 2026
27 checks passed
@liruifengv
liruifengv deleted the chengluyu-fix-session-media-url-promise branch November 24, 2026 06:51
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Route file download and session media URL calls through a sessionSync
route that answers from the current owner without a session load (MoonshotAI#5336).
Add thoughtsTokenCount to the Google GenAI output usage with thinking and
zero-thinking fixtures (MoonshotAI#5334), update the desktop composer e2e specs for
the label-only slash menu (MoonshotAI#5274), and document the Bash session/workspace
env vars (MoonshotAI#5053).
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