@@ -29,7 +29,7 @@ import {
2929 scopeThreadRef ,
3030 scopedThreadKey ,
3131} from "@t3tools/client-runtime/environment" ;
32- import type { ScopedThreadRef , ThreadId } from "@t3tools/contracts" ;
32+ import { ThreadId , type ScopedThreadRef } from "@t3tools/contracts" ;
3333import type { TimestampFormat } from "@t3tools/contracts/settings" ;
3434import {
3535 AlarmClockIcon ,
@@ -99,6 +99,7 @@ import {
9999import { legacyProjectCwdPreferenceKey , useUiStateStore } from "../uiStateStore" ;
100100import { useThreadSelectionStore } from "../threadSelectionStore" ;
101101import { useThreadActions } from "../hooks/useThreadActions" ;
102+ import { useThreadForkActions } from "../hooks/useThreadFork" ;
102103import { useHandleNewThread } from "../hooks/useHandleNewThread" ;
103104import { openCommandPalette } from "../commandPaletteBus" ;
104105import { startNewThreadFromContext } from "../lib/chatThreadActions" ;
@@ -107,7 +108,7 @@ import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
107108import { useLocalStorage } from "../hooks/useLocalStorage" ;
108109import { useNowMinute } from "../hooks/useNowMinute" ;
109110import { useEnvironments , usePrimaryEnvironmentId } from "../state/environments" ;
110- import { useProjects , useThreadShells } from "../state/entities" ;
111+ import { readSideChatsByParent , useProjects , useThreadShells } from "../state/entities" ;
111112import { environmentServerConfigsAtom , primaryServerKeybindingsAtom } from "../state/server" ;
112113import { vcsEnvironment } from "../state/vcs" ;
113114import { threadEnvironment } from "../state/threads" ;
@@ -119,8 +120,10 @@ import {
119120 resolveThreadRouteTarget ,
120121} from "../threadRoutes" ;
121122import { formatRelativeTimeLabel , parseTimestampDate } from "../timestampFormat" ;
123+ import { resolveForkEntryAvailability } from "../threadForking.logic" ;
122124import type { SidebarThreadSummary } from "../types" ;
123125import { cn } from "~/lib/utils" ;
126+ import { useRightPanelStore } from "../rightPanelStore" ;
124127import { buildThreadActionMenuItems } from "./threadActionMenu.logic" ;
125128import {
126129 animatePinnedLayoutChanges ,
@@ -1767,6 +1770,7 @@ export default function Sidebar() {
17671770 archiveThread,
17681771 deleteThread,
17691772 } = useThreadActions ( ) ;
1773+ const threadFork = useThreadForkActions ( null ) ;
17701774 const updateThreadMetadata = useAtomCommand ( threadEnvironment . updateMetadata , {
17711775 reportFailure : false ,
17721776 } ) ;
@@ -3114,6 +3118,16 @@ export default function Sidebar() {
31143118 const isSettled = settledThreadKeysRef . current . has ( threadKey ) ;
31153119 const isSnoozed = snoozedThreadKeysRef . current . has ( threadKey ) ;
31163120 const isPinned = thread . pinnedAt != null ;
3121+ const providerInstanceId =
3122+ thread . session ?. providerInstanceId ?? thread . modelSelection . instanceId ;
3123+ const forkCapability = serverConfigs
3124+ . get ( thread . environmentId )
3125+ ?. providers . find ( ( provider ) => provider . instanceId === providerInstanceId ) ?. sessionFork ;
3126+ const forkEntry = resolveForkEntryAvailability ( {
3127+ capability : forkCapability ,
3128+ latestTurn : thread . latestTurn ,
3129+ } ) ;
3130+ const sideChats = readSideChatsByParent ( threadRef ) ;
31173131 // Presets resolve at menu-open time (same as the popover).
31183132 const snoozePresets = resolveSnoozePresets ( new Date ( ) , timestampFormat ) ;
31193133 const clicked = await settlePromise ( ( ) =>
@@ -3133,6 +3147,11 @@ export default function Sidebar() {
31333147 pinning : supportsPinning ,
31343148 titleRegeneration : supportsTitleRegeneration ,
31353149 } ,
3150+ forking : {
3151+ enabled : forkEntry . enabled ,
3152+ disabledReason : forkEntry . disabledReason ,
3153+ sideChats,
3154+ } ,
31363155 snoozePresets,
31373156 } ) ,
31383157 position ,
@@ -3146,6 +3165,15 @@ export default function Sidebar() {
31463165 if ( preset ) attemptSnooze ( threadRef , preset ) ;
31473166 return ;
31483167 }
3168+ if ( clicked . value ?. startsWith ( "open-existing-side-chat:" ) ) {
3169+ useRightPanelStore
3170+ . getState ( )
3171+ . openSideChat (
3172+ threadRef ,
3173+ ThreadId . make ( clicked . value . slice ( "open-existing-side-chat:" . length ) ) ,
3174+ ) ;
3175+ return ;
3176+ }
31493177 switch ( clicked . value ) {
31503178 case "project-settings" : {
31513179 const projectGroup = projectGroupsRef . current . find ( ( group ) =>
@@ -3181,6 +3209,16 @@ export default function Sidebar() {
31813209 }
31823210 return ;
31833211 }
3212+ case "open-side-chat" :
3213+ if ( forkEntry . target ) {
3214+ void threadFork . forkTarget ( thread , forkEntry . target , true ) ;
3215+ }
3216+ return ;
3217+ case "fork-thread" :
3218+ if ( forkEntry . target ) {
3219+ void threadFork . forkTarget ( thread , forkEntry . target , false ) ;
3220+ }
3221+ return ;
31843222 case "settle" :
31853223 attemptSettle ( threadRef ) ;
31863224 return ;
@@ -3321,6 +3359,7 @@ export default function Sidebar() {
33213359 projectCwdByKey ,
33223360 serverConfigs ,
33233361 startThreadRename ,
3362+ threadFork . forkTarget ,
33243363 updateThreadMetadata ,
33253364 timestampFormat ,
33263365 ] ,
0 commit comments