@@ -7,6 +7,12 @@ import {
77 resolveMothershipModelSettings ,
88} from '@/lib/mothership/model-options'
99
10+ /** A chat's effort pick and the token that tells it apart from other picks of the same value. */
11+ interface ChatEffortPick {
12+ effort : MothershipEffort
13+ pick : number
14+ }
15+
1016interface MothershipEffortState {
1117 modelSelection : ModelSelection
1218 setModel : ( model : ModelSelection [ 'model' ] ) => void
@@ -21,10 +27,11 @@ interface MothershipEffortState {
2127 * Picks made in existing chats this session, by chat id. They win over the chat's loaded
2228 * value, so a detail refetch or a save still in flight never shows or sends an older one.
2329 */
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
30+ chatEfforts : Record < string , ChatEffortPick >
31+ /** Records a pick and returns its token for {@link MothershipEffortState.dropChatEffort}. */
32+ setChatEffort : ( chatId : string , effort : MothershipEffort ) => number
33+ /** Drops a pick whose save failed, unless a newer pick replaced it, even one of the same value. */
34+ dropChatEffort : ( chatId : string , pick : number ) => void
2835 /** Moves the new-chat pick onto the chat its first send created. */
2936 adoptNewChatEffort : ( chatId : string , effort : MothershipEffort ) => void
3037 reset : ( ) => void
@@ -39,6 +46,9 @@ const initialState: Pick<
3946 chatEfforts : { } ,
4047}
4148
49+ /** Counts picks across chats so a token never repeats within a session. */
50+ let lastChatEffortPick = 0
51+
4252function withModelSelection (
4353 modelSelection : ModelSelection
4454) : Pick < MothershipEffortState , 'modelSelection' > {
@@ -55,18 +65,23 @@ export const useMothershipEffortStore = create<MothershipEffortState>()(
5565 setModel : ( model ) =>
5666 set ( ( state ) => withModelSelection ( { model, fastMode : state . modelSelection . fastMode } ) ) ,
5767 setNewChatEffort : ( newChatEffort ) => set ( { newChatEffort } ) ,
58- setChatEffort : ( chatId , effort ) =>
59- set ( ( state ) => ( { chatEfforts : { ...state . chatEfforts , [ chatId ] : effort } } ) ) ,
60- dropChatEffort : ( chatId , effort ) =>
68+ setChatEffort : ( chatId , effort ) => {
69+ const pick = ++ lastChatEffortPick
70+ set ( ( state ) => ( { chatEfforts : { ...state . chatEfforts , [ chatId ] : { effort, pick } } } ) )
71+ return pick
72+ } ,
73+ dropChatEffort : ( chatId , pick ) =>
6174 set ( ( state ) => {
62- if ( state . chatEfforts [ chatId ] !== effort ) return state
75+ if ( state . chatEfforts [ chatId ] ?. pick !== pick ) return state
6376 return { chatEfforts : omit ( state . chatEfforts , [ chatId ] ) }
6477 } ) ,
65- adoptNewChatEffort : ( chatId , effort ) =>
78+ adoptNewChatEffort : ( chatId , effort ) => {
79+ const pick = ++ lastChatEffortPick
6680 set ( ( state ) => ( {
6781 newChatEffort : null ,
68- chatEfforts : { ...state . chatEfforts , [ chatId ] : effort } ,
69- } ) ) ,
82+ chatEfforts : { ...state . chatEfforts , [ chatId ] : { effort, pick } } ,
83+ } ) )
84+ } ,
7085 reset : ( ) => set ( initialState ) ,
7186 } ) ,
7287 {
0 commit comments