Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(fork): fork chat from any assistant message#4343

Merged
waleedlatif1 merged 4 commits intostagingsimstudioai/sim:stagingfrom
feat/forksimstudioai/sim:feat/forkCopy head branch name to clipboard
Apr 29, 2026
Merged

feat(fork): fork chat from any assistant message#4343
waleedlatif1 merged 4 commits intostagingsimstudioai/sim:stagingfrom
feat/forksimstudioai/sim:feat/forkCopy head branch name to clipboard

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Adds a Fork button next to thumbs-down on every assistant message in Mothership chat
  • Clicking Fork creates a new chat with messages 1..N (up to and including the clicked response), copies all attached resources, and navigates immediately to the new chat
  • New chat appears in the sidebar as `${parentTitle} | Fork`
  • Copilot-side conversation state (messages, active_messages, memory files) is cloned via a new `POST /api/chats/fork` endpoint in the copilot service (see simstudioai/copilot#242)
  • Auth header (`x-api-key`) and OTel trace propagation (`fetchGo`) match the pattern used by all other Sim → copilot service calls

Type of Change

  • New feature

Testing

Tested manually

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 Apr 29, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 29, 2026 7:19pm

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 29, 2026

PR Summary

Medium Risk
Introduces new server-side write path that duplicates chat data and triggers a downstream service call; failures or incorrect validation could create orphaned/incorrect forked tasks or inconsistent copilot state.

Overview
Adds POST /api/mothership/chats/[chatId]/fork, which validates ownership/workspace access, slices the parent chat’s persisted messages up to a specified message, copies resources/config artifacts, creates a new chat titled … | Fork, and best-effort calls the copilot service to clone server-side conversation state.

Updates the Mothership chat UI to pass messageId into MessageActions and show a new fork button on assistant messages; clicking it calls a new useForkTask mutation, navigates to the new task, invalidates task lists, and emits a new PostHog task_forked event.

Reviewed by Cursor Bugbot for commit 4468099. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 29, 2026

Greptile Summary

This PR adds a Fork button to every assistant message in the Mothership chat, creating a new chat branched at that message with full state copied (messages, resources, config, previewYaml, planArtifact) and copilot-service conversation state cloned best-effort. The implementation is clean and consistent with existing patterns: session-only auth, ownership + workspace-access guards, standard useMutation + onSettled invalidation, and PostHog analytics.

Confidence Score: 5/5

Safe to merge — no P0/P1 issues found; all previously flagged concerns have been resolved.

All five files follow established project conventions (auth, query patterns, component structure). Previously raised concerns about config copying, fork-of-fork title accumulation, and signal forwarding have been addressed or confirmed as intentional design decisions. No new logic errors or security issues were found.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts New fork endpoint — correctly verifies session auth, ownership, workspace access, slices messages, copies all DB fields (config, previewYaml, planArtifact), clones copilot-service state best-effort, and fires analytics/pubsub.
apps/sim/app/workspace/[workspaceId]/components/message-actions/message-actions.tsx Adds GitBranch fork button guarded by canFork check; pending state disables the button; catches errors with a toast. Follows existing button patterns cleanly.
apps/sim/hooks/queries/tasks.ts Adds forkChat fetch helper and useForkTask mutation hook with onSettled invalidation of taskKeys.lists(); follows project mutation patterns.
apps/sim/app/workspace/[workspaceId]/home/components/mothership-chat/mothership-chat.tsx One-line change passing msg.id as messageId prop to MessageActions; rendered only on non-streaming messages with content.
apps/sim/lib/posthog/events.ts Adds task_forked analytics event type with workspace_id and source_chat_id properties.

Sequence Diagram

sequenceDiagram
    actor User
    participant UI as MessageActions (UI)
    participant TQ as TanStack Query (useForkTask)
    participant API as POST /api/mothership/chats/[chatId]/fork
    participant DB as copilotChats DB
    participant CS as Copilot Service
    participant PubSub as taskPubSub

    User->>UI: Clicks GitBranch (Fork) button
    UI->>TQ: mutateAsync({ chatId, upToMessageId })
    TQ->>API: POST /api/mothership/chats/[chatId]/fork
    API->>API: authenticateCopilotRequestSessionOnly()
    API->>DB: SELECT parent chat (verify ownership + type)
    DB-->>API: parent row
    API->>API: slice messages up to forkIdx
    API->>DB: INSERT forked chat row (messages, resources, config, previewYaml...)
    DB-->>API: { id, workspaceId }
    API->>CS: POST /api/chats/fork (best-effort)
    CS-->>API: 200 OK (or error, silently logged)
    API->>PubSub: publishStatusChanged({ type: 'created' })
    API->>API: captureServerEvent('task_forked')
    API-->>TQ: { success: true, id: newId }
    TQ->>TQ: onSettled → invalidateQueries(taskKeys.lists())
    TQ-->>UI: result.id
    UI->>UI: router.push(/workspace/.../task/newId)
Loading

Reviews (2): Last reviewed commit: "fix(fork): register task_forked PostHog ..." | Re-trigger Greptile

Comment thread apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts
Comment thread apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts Outdated
Comment thread apps/sim/hooks/queries/tasks.ts
Comment thread apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 4468099. Configure here.

@waleedlatif1 waleedlatif1 merged commit 1a321c5 into staging Apr 29, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the feat/fork branch April 29, 2026 19:51
waleedlatif1 added a commit that referenced this pull request Apr 30, 2026
* feat(fork): fork chat from any assistant message

* fix(fork): toast on failure, disabled opacity, copy previewYaml/planArtifact/config

* fix(fork): type guard for mothership-only, prevent title accumulation on re-fork

* fix(fork): register task_forked PostHog event type
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.