fix: resolve TypeScript build errors#129
fix: resolve TypeScript build errors#129firstdraft-work wants to merge 1 commit intolinuxhsj:mainfrom
Conversation
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
📝 WalkthroughWalkthrough本次更改对流响应处理的类型安全进行了修复,并为模型配置同步逻辑增加了自动选择首个可用提供商的默认代理模型的功能。 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/agents/xiaomimo-web-stream.tssrc/commands/onboard-web-auth.ts
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
currentModevariable 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:
2. onboard-web-auth.ts: Missing import
Problem:
applyAgentDefaultModelPrimaryfunction was used but not imported, causingReferenceError: applyAgentDefaultModelPrimary is not definedat runtime.Fix: Added the missing import:
Test plan
pnpm build- should complete without TypeScript errorsdist/output is generated correctlySummary by CodeRabbit
发布说明
重构
杂务