Keep SDK client type declarations in the server image

Merged
CH
4h ago
+1-1

Why

In Docker images, the AI component preview fails for any generated component that imports the client SDK:

component.tsx:2:35 TS2307: Cannot find module 'twenty-client-sdk/metadata' or its corresponding type declarations.

The preview typecheck (#29412) reads the SDK types from the generated client archive, which is built from the template copied to dist/assets/twenty-client-sdk. The image cleanup step deletes every .d.ts under the server dist, template included, so every archive built from an image has no declarations. On twenty-main the template holds 13 files: package.json and the JS bundles, nothing under dist/core or dist/metadata.

Local dev is not affected, since the template is resolved from the workspace package there.

How the declarations reach the preview build

The preview runs in Lambda, but both Lambdas get the SDK types from the server image.

flowchart TD
  sdk["Image build:<br/>client SDK<br/>JS + .d.ts"]
  copy["Image build:<br/>copied to<br/>dist/assets"]
  clean["Image build:<br/>cleanup deletes<br/>every .d.ts"]
  template["SDK template<br/>in the image"]
  pack["Server:<br/>packs template<br/>into generator"]
  generator["Lambda:<br/>template +<br/>core client"]
  archive["SDK archive<br/>in file<br/>storage"]
  read["Server:<br/>reads .ts and<br/>package.json"]
  builder["Lambda:<br/>typechecks<br/>component"]
  schema["Workspace<br/>GraphQL<br/>schema"]
  source["AI component<br/>source"]
  entry{"Entry .d.ts<br/>in archive?"}
  failure["TS2307<br/>module<br/>not found"]
  success["Preview<br/>built"]

  sdk --> copy --> clean --> template --> pack --> generator --> archive --> read --> builder --> entry
  schema --> generator
  source --> builder
  entry -- no --> failure
  entry -- yes --> success

  classDef changed fill:#fde68a,stroke:#b45309,color:#111827
  class clean changed

The highlighted step is the one this PR changes: the cleanup now skips the template.

package.json in the archive maps ./core and ./metadata to dist/<name>/index.d.ts. The core client generated from the workspace schema is reached through dist/core/index.d.ts, so without that file the per-workspace types are in the archive but cannot be resolved either.

Template in the imageArchivePreview of a component importing the SDK
Before13 files, no .d.tsgenerated core sources, no entry declarationsTS2307 on twenty-client-sdk/core and /metadata
After82 files, 69 .d.tsentry declarations and metadata types includedtypechecked against the workspace schema

Fix

The cleanup skips dist/assets/twenty-client-sdk. The server's own declarations are still removed.

Follow-up

Archives already stored were built without declarations. If they are not regenerated on their own, bumping SDK_CLIENT_GENERATION_KEY forces it, at the cost of rebuilding every SDK layer on first use. Left out of this PR on purpose.

Screenshots

No UI change.

Test

The find expression was checked on a scratch tree: declarations under the template are kept, the others are deleted. No full image build.