Skip to content

fix: resolve TypeScript build errors#129

Closed
firstdraft-work wants to merge 1 commit intolinuxhsj:mainfrom
firstdraft-work:fix/typescript-build-errors
Closed

fix: resolve TypeScript build errors#129
firstdraft-work wants to merge 1 commit intolinuxhsj:mainfrom
firstdraft-work:fix/typescript-build-errors

Conversation

@firstdraft-work
Copy link

@firstdraft-work firstdraft-work commented Mar 25, 2026

Summary

This PR fixes two TypeScript build errors:

1. xiaomimo-web-stream.ts: Type assertion for closure variable

Problem: TypeScript control flow analysis cannot track variable mutations inside closures. The currentMode variable is modified by nested functions (checkTags, processLine), but TypeScript narrows its type to "text" at the usage site.

Fix: Added explicit type assertion to help the compiler understand the union type:

const currentModeValue = currentMode as "text" | "thinking" | "tool_call";

2. onboard-web-auth.ts: Missing import

Problem: applyAgentDefaultModelPrimary function was used but not imported, causing ReferenceError: applyAgentDefaultModelPrimary is not defined at runtime.

Fix: Added the missing import:

import { applyAgentDefaultModelPrimary } from "./onboard-auth.config-shared.js";

Test plan

  • Run pnpm build - should complete without TypeScript errors
  • Verify dist/ output is generated correctly

Summary by CodeRabbit

发布说明

  • 重构

    • 改进了流处理逻辑的类型安全性和比较操作。
  • 杂务

    • 优化了配置初始化流程,在启动时自动检测并应用首个可用的默认AI代理模型。

1. xiaomimo-web-stream.ts: Add explicit type assertion for currentMode
   - TypeScript control flow analysis cannot track closure variable mutations
   - Added type assertion to help compiler understand the union type

2. onboard-web-auth.ts: Add missing import for applyAgentDefaultModelPrimary
   - Function was used but not imported, causing ReferenceError at runtime
@coderabbitai
Copy link

coderabbitai bot commented Mar 25, 2026

📝 Walkthrough

Walkthrough

本次更改对流响应处理的类型安全进行了修复,并为模型配置同步逻辑增加了自动选择首个可用提供商的默认代理模型的功能。

Changes

Cohort / File(s) Summary
流响应类型安全
src/agents/xiaomimo-web-stream.ts
将最终刷新逻辑中的 currentMode 直接比较替换为 currentModeValue(已强制转换为 "text" | "thinking" | "tool_call"),确保发送模式的类型安全性。
默认模型配置
src/commands/onboard-web-auth.ts
导入 applyAgentDefaultModelPrimary 函数,并扩展 syncModelsProvidersToConfig() 以在未设置默认代理模型时,从首个可用的 Web 提供商自动选择并应用默认模型。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 小小兔在键盘舞动,
修复类型把代码护正,
自动选模型如同做梦,
提供商列表中找到最佳伴侣,
流水般的数据又显端庄!✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main purpose of the PR - fixing TypeScript build errors affecting two files.
Description check ✅ Passed The description provides clear problem statements, fixes, and test verification, though it omits many template sections like Change Type, Scope, Security Impact, and Human Verification.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/commands/onboard-web-auth.ts (1)

145-153: ⚠️ Potential issue | 🟡 Minor

默认模型筛选未限定为 web provider,可能选到非 Web 模型。

这里的 find 只检查了 provider 是否有 models,未限制 provider 类型;当 models.json 首项不是 Web provider 时,会偏离“首个 web provider”的意图。建议按 providerId 增加 Web 过滤条件。

💡 建议修复
-    const firstEntry = Object.entries(providers).find(
-      ([, p]) =>
+    const firstEntry = Object.entries(providers).find(
+      ([providerId, p]) =>
+        providerId.endsWith("-web") &&
         p &&
         typeof p === "object" &&
         Array.isArray((p as { models?: unknown[] }).models) &&
         (p as { models: { id?: string }[] }).models.length > 0,
     );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/commands/onboard-web-auth.ts` around lines 145 - 153, The current
fallback logic that sets a default model when
resolveAgentModelPrimaryValue(config.agents?.defaults?.model) is falsy uses
Object.entries(providers).find(...) but only checks for a models array and its
length, so it can pick a non-web provider; update that find predicate to also
restrict to web providers by checking the provider's identity (e.g., ensure the
provider entry has providerId or id indicating a web provider such as
p.providerId === "web" or p.providerId?.startsWith("web") or p.type === "web"),
so the selected firstEntry definitively comes from a web provider before reading
its models array and using its first model. Ensure you reference the same
variables: providers, firstEntry, and keep resolveAgentModelPrimaryValue usage
unchanged.
🧹 Nitpick comments (1)
src/agents/xiaomimo-web-stream.ts (1)

468-476: 考虑显式处理 "error" 模式以提高代码清晰度。

类型断言 currentMode as "text" | "thinking" | "tool_call" 解决了 TypeScript 编译错误,但它隐藏了 currentMode 可能为 "error" 的事实。当 processLine() 接收到错误事件时(第 408 行),currentMode 会被设置为 "error"。如果此时到达最终刷新逻辑,类型断言会将其强制转换,导致错误模式被隐式地当作 "text" 处理。

建议显式检查 "error" 状态,使行为意图更加清晰:

♻️ 更明确的处理方式
-        if (tagBuffer) {
-          const currentModeValue = currentMode as "text" | "thinking" | "tool_call";
+        if (tagBuffer && currentMode !== "error") {
           const mode =
-            currentModeValue === "thinking"
+            currentMode === "thinking"
               ? "thinking"
-              : currentModeValue === "tool_call"
+              : currentMode === "tool_call"
                 ? "toolcall"
                 : "text";
           emitDelta(mode, tagBuffer);
         }

如果错误模式下也需要刷新缓冲区,可以显式地将其映射为 "text":

         if (tagBuffer) {
-          const currentModeValue = currentMode as "text" | "thinking" | "tool_call";
+          const effectiveMode = currentMode === "error" ? "text" : currentMode;
           const mode =
-            currentModeValue === "thinking"
+            effectiveMode === "thinking"
               ? "thinking"
-              : currentModeValue === "tool_call"
+              : effectiveMode === "tool_call"
                 ? "toolcall"
                 : "text";
           emitDelta(mode, tagBuffer);
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/agents/xiaomimo-web-stream.ts` around lines 468 - 476, The code currently
force-casts currentMode with `currentMode as "text" | "thinking" | "tool_call"`,
which hides the possible "error" value set by processLine(); update the final
flush logic to explicitly handle the "error" case instead of asserting it
away—inspect currentMode (or currentModeValue) and add a branch for "error" that
either maps to "text" if you want the buffer flushed the same way or handles it
specially (e.g., emitDelta("error", tagBuffer) or discard/transform tagBuffer),
then call emitDelta(mode, tagBuffer) using the explicitly-determined mode so
error is not implicitly treated as text.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/commands/onboard-web-auth.ts`:
- Around line 145-153: The current fallback logic that sets a default model when
resolveAgentModelPrimaryValue(config.agents?.defaults?.model) is falsy uses
Object.entries(providers).find(...) but only checks for a models array and its
length, so it can pick a non-web provider; update that find predicate to also
restrict to web providers by checking the provider's identity (e.g., ensure the
provider entry has providerId or id indicating a web provider such as
p.providerId === "web" or p.providerId?.startsWith("web") or p.type === "web"),
so the selected firstEntry definitively comes from a web provider before reading
its models array and using its first model. Ensure you reference the same
variables: providers, firstEntry, and keep resolveAgentModelPrimaryValue usage
unchanged.

---

Nitpick comments:
In `@src/agents/xiaomimo-web-stream.ts`:
- Around line 468-476: The code currently force-casts currentMode with
`currentMode as "text" | "thinking" | "tool_call"`, which hides the possible
"error" value set by processLine(); update the final flush logic to explicitly
handle the "error" case instead of asserting it away—inspect currentMode (or
currentModeValue) and add a branch for "error" that either maps to "text" if you
want the buffer flushed the same way or handles it specially (e.g.,
emitDelta("error", tagBuffer) or discard/transform tagBuffer), then call
emitDelta(mode, tagBuffer) using the explicitly-determined mode so error is not
implicitly treated as text.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c6177c89-92dc-4bc0-a4e4-f8ba9541d3cb

📥 Commits

Reviewing files that changed from the base of the PR and between 4364525 and 280ce9f.

📒 Files selected for processing (2)
  • src/agents/xiaomimo-web-stream.ts
  • src/commands/onboard-web-auth.ts

@linuxhsj linuxhsj closed this Mar 25, 2026
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