1- import type { ScopedThreadRef } from "@t3tools/contracts" ;
1+ import type { ModelSelection , RuntimeMode , ScopedThreadRef } from "@t3tools/contracts" ;
22import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
33import { deriveThreadTitleSeed } from "@t3tools/client-runtime/operations" ;
44import {
99import { useRef } from "react" ;
1010
1111import { newMessageId , newThreadId } from "~/lib/utils" ;
12+ import { useComposerDraftStore } from "../../composerDraftStore" ;
1213import { useStableCallback } from "../../hooks/useStableCallback" ;
1314import { useRightPanelStore } from "../../rightPanelStore" ;
1415import { readThreadShell , waitForThreadShell } from "../../state/entities" ;
@@ -18,16 +19,24 @@ import { useOrchestrationCommand } from "../../state/use-orchestration-command";
1819
1920/**
2021 * Starts side chats from `threadRef`, or offers none when it is null. `open` forks the
21- * thread's latest stable point, sends `prompt` there, and shows the side chat's tab. It
22- * resolves false when the side chat did not start, or another one is still starting.
22+ * thread's latest stable point, sends `prompt` there with `readSettings()`, the parent
23+ * composer's settings, and shows the tab. It resolves false when the side chat did not
24+ * start, or another one is still starting.
2325 */
2426export function useSideChat ( input : {
2527 readonly threadRef : ScopedThreadRef | null ;
28+ readonly readSettings : ( ) => {
29+ readonly runtimeMode : RuntimeMode ;
30+ readonly modelSelection : ModelSelection | undefined ;
31+ } ;
2632 readonly onError : ( message : string ) => void ;
2733} ) {
2834 const pendingRef = useRef ( false ) ;
2935 const forkSideChat = useAtomCommand ( threadEnvironment . forkSideChat , { reportFailure : false } ) ;
3036 const deleteThread = useAtomCommand ( threadEnvironment . delete , { reportFailure : false } ) ;
37+ const setThreadRuntimeMode = useOrchestrationCommand ( threadEnvironment . setRuntimeMode , {
38+ reportFailure : false ,
39+ } ) ;
3140 const startThreadTurn = useOrchestrationCommand ( threadEnvironment . startTurn , {
3241 reportFailure : false ,
3342 } ) ;
@@ -40,6 +49,7 @@ export function useSideChat(input: {
4049 input . onError ( error instanceof Error ? error . message : fallback ) ;
4150 } ;
4251 const start = async ( threadRef : ScopedThreadRef , prompt : string | undefined ) => {
52+ const { runtimeMode, modelSelection } = input . readSettings ( ) ;
4353 const targetThreadId = newThreadId ( ) ;
4454 const fork = await forkSideChat ( {
4555 environmentId : threadRef . environmentId ,
@@ -67,6 +77,17 @@ export function useSideChat(input: {
6777 discard ( ) ;
6878 return false ;
6979 }
80+ if ( runtimeMode !== sideChat . runtimeMode ) {
81+ const mode = await setThreadRuntimeMode ( {
82+ environmentId : threadRef . environmentId ,
83+ input : { threadId : targetThreadId , runtimeMode, createdAt : new Date ( ) . toISOString ( ) } ,
84+ } ) ;
85+ if ( mode . _tag === "Failure" ) {
86+ reportFailure ( mode , "Could not set the side chat's permission mode." ) ;
87+ discard ( ) ;
88+ return false ;
89+ }
90+ }
7091 const text = prompt ?. trim ( ) ;
7192 // Send before showing the tab: closing a tab whose side chat has no run deletes it.
7293 if ( text ) {
@@ -75,8 +96,9 @@ export function useSideChat(input: {
7596 input : {
7697 threadId : targetThreadId ,
7798 message : { messageId : newMessageId ( ) , role : "user" , text, attachments : [ ] } ,
99+ ...( modelSelection === undefined ? { } : { modelSelection } ) ,
78100 titleSeed : deriveThreadTitleSeed ( { text, attachments : [ ] } ) ,
79- runtimeMode : sideChat . runtimeMode ,
101+ runtimeMode,
80102 interactionMode : "default" ,
81103 createdAt : new Date ( ) . toISOString ( ) ,
82104 } ,
@@ -86,6 +108,8 @@ export function useSideChat(input: {
86108 discard ( ) ;
87109 return false ;
88110 }
111+ } else if ( modelSelection !== undefined ) {
112+ useComposerDraftStore . getState ( ) . setModelSelection ( targetRef , modelSelection ) ;
89113 }
90114 useRightPanelStore . getState ( ) . openSideChat ( threadRef , targetThreadId ) ;
91115 return true ;
0 commit comments