Typecheck AI component previews against app dependencies
AI-generated components currently pass through esbuild without semantic TypeScript checks. A mistake such as rendering <Card> instead of <Card.Root>, passing an unsupported prop, or selecting a nonexistent SDK field can compile and fail only when the preview runs. This PR checks the authored component before esbuild and returns source locations and TypeScript errors through the existing preview failure response, so chat can correct the code.
The check uses the app's installed dependency declarations and its generated SDK. The existing dependency build, in either the CLI or the server, saves a compressed .types.json.gz file beside the JavaScript bundle and records its checksum in the bundle manifest. Previews reuse that file without another install. Application installation retains this file from CLI uploads. Existing bundles without it rebuild once; a mismatched checksum also triggers a rebuild. Compiler standard-library and React declarations provide the platform defaults, with app-installed declarations taking precedence.
When a bundled dependency resolves to JavaScript without declarations, the type-reference collector records a small untyped declaration for that exact import. It keeps JS-only dependencies usable without executing or storing their JavaScript in the type archive. Bundled declarations and installed @types packages still take precedence; missing packages or subpaths remain errors.
Generated SDK declarations are read from the app's stored archive and cached per workspace/application while that archive's file-storage version identity stays unchanged (S3 ETag or local file metadata). This avoids downloading and unzipping the same SDK on each preview while still invalidating type-only schema changes, which can leave runtime JavaScript unchanged. The in-process LRU holds at most eight compressed entries of 1 MiB each. Missing version identities bypass the cache; an archive replaced during a read is not cached. Type-reference encoding and decoding share a 32 MiB uncompressed limit. Lambda requests are checked against the 6 MiB payload budget before any AWS call; dependency responses explicitly budget the type file alongside the JavaScript bundle. Oversized inputs return a clear error and still require reducing dependencies.
Both LOCAL and Lambda use the same checker. This adds no Redis cache, database migration, service, or separate build target. UI documentation lookup is handled independently in #29411.
Validation:
-
Earlier validation, before the cache and JS-only dependency follow-ups: 63 focused tests passed: 9 shared, 52 server builder/driver/installation, and 2 CLI. Coverage includes invalid compound components, version-specific props, generated SDK fields, authored locations for missing props, explicit unknown locations for missing exports, Windows-style paths, real host-file isolation, and payload limits.
-
Fresh shared build, direct TypeScript checks for shared/server/CLI, Nest build, scoped type-aware lint, formatting, and diff checks pass.
-
Real Yarn 4.9.2 immutable install of the shipped Workspace manifest/lockfile, using UI 2.53.0 and React 19.2.0:
Card.Rootpasses,<Card>fails; package and lock bytes remain unchanged. The compressed type file is about 1 MB; the full dependency-build response remains within the existing limit. -
Generated a real SDK from a fixture GraphQL schema and invoked the packaged builder: valid fields pass, an unknown field returns a TypeScript diagnostic. A real-generator regression changes a field from
String!toStringwhile producing identical runtime JavaScript. The same compiled service instance now reads the updated types and rejects unsafe field access without a restart. Recovery from a partially written dependency type artifact was also checked. -
SDK cache follow-up: 9 temporary regression scenarios pass, including a real generated SDK and the local file-storage driver, repeated-read reuse, type-only schema changes, replacement during download, workspace/app isolation and cache limits. The 9 existing shared type-reference tests and 14 LRU tests also pass, along with fresh shared/SDK builds, direct server typecheck, Nest build, scoped lint, Knip and formatting.
-
Latest JS-only dependency follow-up: 23 shared compiler tests pass, including 16 type-reference tests and seven new cases for JS/MJS/CJS/JSX, aliases sharing one file, installed
@typesprecedence and missing imports. The 30 affected server builder/type-validation tests also pass after rebuilding the local UI artifacts. Fresh shared builds, shared/server direct typechecks, scoped lint, formatting and diff checks pass.
No new live-model chat or AWS run was performed for these follow-ups. Typechecking catches API/type mistakes; it does not prove browser behavior or visual correctness.