Skip to content

perf(copilot): narrow getAccessibleCopilotChat projection#4720

Merged
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/copilot-chat-projection-narrow
May 22, 2026
Merged

perf(copilot): narrow getAccessibleCopilotChat projection#4720
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/copilot-chat-projection-narrow

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Narrows getAccessibleCopilotChat in lib/copilot/chat/lifecycle.ts from db.select() (all columns) to an explicit projection that drops previewYaml (JSONB), pinned, and lastSeenAt — none of which the legacy chat detail endpoint returns
  • Adds copilotChatLegacyDetailColumns (extends copilotChatDetailColumns with model, planArtifact, config) and a CopilotChatLegacyDetailRow type
  • Sole production caller (app/api/copilot/chat/queries.ts:83) still gets every field its transformChat response consumes (id, title, model, messages, planArtifact, config, conversationId, resources, createdAt, updatedAt + auth columns)
  • Targets the 8.4%-of-DB-time hotspot identified by PlanetScale insights — ~2.9 GB/day egress on the second hottest copilot_chats query, p99 3.5s

Type of Change

  • Improvement

Testing

Tested manually; existing test mocks ({ id, userId, workflowId } shape) remain structurally compatible with the new CopilotChatLegacyDetailRow type. Lint passes.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 22, 2026 3:13pm

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 22, 2026

PR Summary

Low Risk
Low risk performance change that limits selected DB columns, with small risk of missing a field if other callers relied on the previous select() behavior.

Overview
Reduces DB load for the legacy copilot chat detail fetch by changing getAccessibleCopilotChat to select an explicit column projection instead of db.select().

Introduces copilotChatLegacyDetailColumns and CopilotChatLegacyDetailRow to keep returning only the fields consumed by the legacy transformChat shape (including model, planArtifact, config) while omitting heavy/unused columns like previewYaml, pinned, and lastSeenAt.

Reviewed by Cursor Bugbot for commit fe941f8. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 22, 2026

Greptile Summary

This PR tightens the DB projection in getAccessibleCopilotChat (the legacy copilot chat detail path) from a full SELECT * to an explicit column list that drops previewYaml (JSONB), pinned, and lastSeenAt. A new copilotChatLegacyDetailColumns constant and CopilotChatLegacyDetailRow type are added to capture the narrowed shape.

  • The only production caller (app/api/copilot/chat/queries.ts) passes the result to transformChat, which consumes id, title, model, messages, planArtifact, config, conversationId, resources, createdAt, and updatedAt — all of which remain in the new projection.
  • All other callers in the codebase already use the narrower getAccessibleCopilotChatAuth or getAccessibleCopilotChatWithMessages variants and are unaffected.
  • CopilotChatLegacyDetailRow is a structural superset of CopilotChatDetailRow, keeping the ChatLoadResult interface and resolveOrCreateChat paths fully compatible.

Confidence Score: 5/5

Safe to merge — the narrowed projection covers every field the single production caller consumes, and unrelated callers use different function variants.

The column set in copilotChatLegacyDetailColumns was cross-checked against every field read by transformChat in queries.ts and all are present. No other call site uses getAccessibleCopilotChat directly. The new CopilotChatLegacyDetailRow type is a strict superset of CopilotChatDetailRow, so existing type boundaries remain sound.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/chat/lifecycle.ts Adds copilotChatLegacyDetailColumns projection and CopilotChatLegacyDetailRow type; narrows getAccessibleCopilotChat from db.select() to the explicit column set, dropping previewYaml, pinned, and lastSeenAt. The sole production caller in queries.ts consumes exactly the retained fields.

Sequence Diagram

sequenceDiagram
    participant Client
    participant GET as GET /api/copilot/chat
    participant LC as lifecycle.ts
    participant DB as copilot_chats

    Client->>GET: "GET ?chatId=..."
    GET->>LC: getAccessibleCopilotChat(chatId, userId)
    LC->>DB: "SELECT id, userId, workflowId, workspaceId, type, title, messages, conversationId, resources, createdAt, updatedAt, model, planArtifact, config WHERE id=? AND userId=? LIMIT 1"
    Note over DB: Drops previewYaml, pinned, lastSeenAt
    DB-->>LC: CopilotChatLegacyDetailRow
    LC->>LC: authorizeCopilotChatRow(chat, ...)
    LC-->>GET: CopilotChatLegacyDetailRow or null
    GET->>GET: transformChat(chat)
    GET-->>Client: "{ success, chat }"
Loading

Reviews (1): Last reviewed commit: "perf(copilot): narrow getAccessibleCopil..." | Re-trigger Greptile

@waleedlatif1 waleedlatif1 merged commit 1631b36 into staging May 22, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/copilot-chat-projection-narrow branch May 22, 2026 15:35
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