1- import { toRecord } from '@sim/utils/object'
1+ import { omit , toRecord } from '@sim/utils/object'
22import { create } from 'zustand'
33import { devtools , persist } from 'zustand/middleware'
44import { type ModelSelection , ModelSelectionSchema } from '@/lib/mothership/generated/protocol'
@@ -13,17 +13,31 @@ interface MothershipEffortState {
1313 setFastMode : ( fastMode : boolean ) => void
1414 /**
1515 * The effort picked in a composer whose chat does not exist yet. Its first send records
16- * it on the new chat; existing chats keep their own choice on the chat itself .
16+ * it on the new chat; leaving that composer unsent drops it .
1717 */
1818 newChatEffort : MothershipEffort | null
1919 setNewChatEffort : ( effort : MothershipEffort | null ) => void
20+ /**
21+ * Picks made in existing chats this session, by chat id. They win over the chat's loaded
22+ * value, so a detail refetch or a save still in flight never shows or sends an older one.
23+ */
24+ chatEfforts : Record < string , MothershipEffort >
25+ setChatEffort : ( chatId : string , effort : MothershipEffort ) => void
26+ /** Drops a pick whose save failed, unless a newer pick replaced it. */
27+ dropChatEffort : ( chatId : string , effort : MothershipEffort ) => void
28+ /** Moves the new-chat pick onto the chat its first send created. */
29+ adoptNewChatEffort : ( chatId : string , effort : MothershipEffort ) => void
2030 reset : ( ) => void
2131}
2232
23- const initialState = {
33+ const initialState : Pick <
34+ MothershipEffortState ,
35+ 'modelSelection' | 'newChatEffort' | 'chatEfforts'
36+ > = {
2437 modelSelection : { model : 'gpt-6-astra' , fastMode : false } ,
2538 newChatEffort : null ,
26- } satisfies Pick < MothershipEffortState , 'modelSelection' | 'newChatEffort' >
39+ chatEfforts : { } ,
40+ }
2741
2842function withModelSelection (
2943 modelSelection : ModelSelection
@@ -41,6 +55,18 @@ export const useMothershipEffortStore = create<MothershipEffortState>()(
4155 setModel : ( model ) =>
4256 set ( ( state ) => withModelSelection ( { model, fastMode : state . modelSelection . fastMode } ) ) ,
4357 setNewChatEffort : ( newChatEffort ) => set ( { newChatEffort } ) ,
58+ setChatEffort : ( chatId , effort ) =>
59+ set ( ( state ) => ( { chatEfforts : { ...state . chatEfforts , [ chatId ] : effort } } ) ) ,
60+ dropChatEffort : ( chatId , effort ) =>
61+ set ( ( state ) => {
62+ if ( state . chatEfforts [ chatId ] !== effort ) return state
63+ return { chatEfforts : omit ( state . chatEfforts , [ chatId ] ) }
64+ } ) ,
65+ adoptNewChatEffort : ( chatId , effort ) =>
66+ set ( ( state ) => ( {
67+ newChatEffort : null ,
68+ chatEfforts : { ...state . chatEfforts , [ chatId ] : effort } ,
69+ } ) ) ,
4470 reset : ( ) => set ( initialState ) ,
4571 } ) ,
4672 {
0 commit comments