Skip to content

chore: tighten TypeScript guards, silence stray rejections - #550

Open
haotianliangye wants to merge 1 commit into
agegr:mainfrom
haotianliangye:chore/ts-guards
Open

chore: tighten TypeScript guards, silence stray rejections#550
haotianliangye wants to merge 1 commit into
agegr:mainfrom
haotianliangye:chore/ts-guards

Conversation

@haotianliangye

Copy link
Copy Markdown

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.

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.
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.

1 participant