chore: tighten TypeScript guards, silence stray rejections - #550
Open
haotianliangye wants to merge 1 commit into
Open
chore: tighten TypeScript guards, silence stray rejections#550haotianliangye wants to merge 1 commit into
haotianliangye wants to merge 1 commit into
Conversation
A grab bag of small changes that keep the dev experience clean under
the strict TypeScript / Next 16 dev overlay:
TypeScript type guards on `AssistantMessage.content`:
- app/api/models-config/test/route.ts
- components/ChatWindow.tsx
- lib/session-title.ts
`@earendil-works/pi-ai`'s `AssistantMessage.content` is typed as
`AssistantContentBlock[]`, which is a discriminated union. The naive
`.filter((b) => b.type === "text")` produced a `(AssistantContentBlock
| { type: 'text'; text: string })[]` that the compiler still treated
as the wide type. Switching to `.filter((b): b is TextContent => ...)`
narrows the result so the downstream `.map((b) => b.text)` typechecks
without an `as TextContent` cast.
hooks/useTheme.ts:
- `document.startViewTransition` is not yet in the default lib types on
every platform we ship to. Replace the unguarded access with a type
guard, and bail to the synchronous `apply()` path when the API is
missing so the theme still flips on browsers without View Transitions.
- No runtime behavior change for browsers that have the API.
hooks/useAgentSession.ts:
- `loadModels` already swallows its own errors (network failures, JSON
parse failures, AbortError on cleanup). Add a defensive `.catch(() =>
{})` so anything that ever bubbles up is silenced — Next 16's dev
overlay still reports the AbortError source line even when caught,
which previously surfaced as an unhandledRejection noise.
components/ChatWindow.tsx:
- Same AbortError-catching comment near the new-session update check.
- Same TextContent guard on the user-message text extraction path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A grab bag of small changes that keep the dev experience clean under the strict TypeScript / Next 16 dev overlay:
TypeScript type guards on
AssistantMessage.content:@earendil-works/pi-ai'sAssistantMessage.contentis typed asAssistantContentBlock[], which is a discriminated union. The naive.filter((b) => b.type === "text")produced a(AssistantContentBlock | { type: 'text'; text: string })[]that the compiler still treated as the wide type. Switching to.filter((b): b is TextContent => ...)narrows the result so the downstream.map((b) => b.text)typechecks without anas TextContentcast.hooks/useTheme.ts:
document.startViewTransitionis not yet in the default lib types on every platform we ship to. Replace the unguarded access with a type guard, and bail to the synchronousapply()path when the API is missing so the theme still flips on browsers without View Transitions.hooks/useAgentSession.ts:
loadModelsalready swallows its own errors (network failures, JSON parse failures, AbortError on cleanup). Add a defensive.catch(() => {})so anything that ever bubbles up is silenced — Next 16's dev overlay still reports the AbortError source line even when caught, which previously surfaced as an unhandledRejection noise.components/ChatWindow.tsx: