@@ -3,7 +3,7 @@ import * as Dialog from "../components/ui/Dialog";
33import { AuthOrchestrationOperateScope } from "@t3tools/contracts" ;
44import { readEnvironmentScope } from "./session" ;
55import { useAtomValue } from "@effect/atom-react" ;
6- import { shouldCompactBeforeSend } from "@t3tools/client-runtime/state/context-window" ;
6+ import { resolveCompactBeforeSendTokens } from "@t3tools/client-runtime/state/context-window" ;
77import { threadRuntimeIsActive } from "@t3tools/client-runtime/state/shell" ;
88import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints" ;
99import { threadSteersDuringCompaction } from "@t3tools/client-runtime/state/thread-workflows" ;
@@ -42,6 +42,7 @@ import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLega
4242import { restoreAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences" ;
4343import { composerContextSendBlockReason , reidentifyComposerContext } from "../lib/composerContext" ;
4444import { uuidv4 } from "../lib/uuid" ;
45+ import { useNowMinuteMs } from "../lib/clock" ;
4546
4647import { makeQueuedMessageMetadata } from "../lib/commandMetadata" ;
4748import {
@@ -402,6 +403,57 @@ export function useThreadComposerState(input: {
402403 ? ( selectedThreadRuntime ?. activeRunId ?? null )
403404 : null ;
404405
406+ const resolveThreadCompactBeforeSendTokens = ( send : {
407+ readonly modelSelection : ModelSelection ;
408+ readonly text : string ;
409+ readonly now : string ;
410+ } ) =>
411+ selectedEnvironmentRuntime ?. connectionState === "connected" &&
412+ selectedThreadProjection !== null &&
413+ serverConfig != null
414+ ? resolveCompactBeforeSendTokens ( {
415+ projection : selectedThreadProjection . projection ,
416+ visibleTurnItems : selectedThreadVisibleTurnItems ,
417+ selection : send . modelSelection ,
418+ providers : serverConfig . providers ,
419+ settings : serverConfig . settings ,
420+ text : send . text ,
421+ threadIdle :
422+ ! activeThreadBusy &&
423+ ! runlessWorkActive &&
424+ ! isCompacting &&
425+ selectedThreadQueuedMessages . length === 0 ,
426+ now : send . now ,
427+ } )
428+ : null ;
429+ const nowMinuteMs = useNowMinuteMs ( ) ;
430+ const compactBeforeSendTokens =
431+ modelSelection === null || queuedRunEdit !== null || selectedDraft . freshContext === true
432+ ? null
433+ : resolveThreadCompactBeforeSendTokens ( {
434+ modelSelection,
435+ text : "" ,
436+ now : new Date ( nowMinuteMs ) . toISOString ( ) ,
437+ } ) ;
438+ // Per thread and for one send, as on web: the next send compacts again unless kept again.
439+ const [ fullHistoryThreadKeys , setFullHistoryThreadKeys ] = useState < ReadonlySet < string > > (
440+ ( ) => new Set ( ) ,
441+ ) ;
442+ const keepFullHistory =
443+ selectedThreadKey !== null && fullHistoryThreadKeys . has ( selectedThreadKey ) ;
444+ const setKeepFullHistory = ( threadKey : string , keep : boolean ) => {
445+ setFullHistoryThreadKeys ( ( current ) => {
446+ if ( current . has ( threadKey ) === keep ) return current ;
447+ const next = new Set ( current ) ;
448+ if ( keep ) next . add ( threadKey ) ;
449+ else next . delete ( threadKey ) ;
450+ return next ;
451+ } ) ;
452+ } ;
453+ const toggleKeepFullHistory = ( ) => {
454+ if ( selectedThreadKey !== null ) setKeepFullHistory ( selectedThreadKey , ! keepFullHistory ) ;
455+ } ;
456+
405457 const cancelQueuedRunEdit = ( ) => {
406458 if ( selectedThreadKey === null || savingQueuedEditRef . current ) return ;
407459 endQueuedRunEdit ( selectedThreadKey ) ;
@@ -734,24 +786,10 @@ export function useThreadComposerState(input: {
734786 // A fresh session drops the context a compaction would summarize.
735787 const compactBeforeSend =
736788 ! freshContext &&
789+ ! keepFullHistory &&
737790 followUpDispatchMode === null &&
738- selectedEnvironmentRuntime ?. connectionState === "connected" &&
739- selectedThreadProjection !== null &&
740- serverConfig != null &&
741- shouldCompactBeforeSend ( {
742- projection : selectedThreadProjection . projection ,
743- visibleTurnItems : selectedThreadVisibleTurnItems ,
744- selection : modelSelection ,
745- providers : serverConfig . providers ,
746- settings : serverConfig . settings ,
747- text,
748- threadIdle :
749- ! activeThreadBusy &&
750- ! runlessWorkActive &&
751- ! isCompacting &&
752- selectedThreadQueuedMessages . length === 0 ,
753- now : metadata . createdAt ,
754- } ) ;
791+ resolveThreadCompactBeforeSendTokens ( { modelSelection, text, now : metadata . createdAt } ) !==
792+ null ;
755793 const messageId = MessageId . make ( metadata . messageId ) ;
756794 onWillEnqueue ?.( ) ;
757795 clearThreadComposerError ( threadKey ) ;
@@ -776,6 +814,7 @@ export function useThreadComposerState(input: {
776814 } ) ;
777815 clearComposerDraftContent ( threadKey , { deferAttachmentCleanup : true } ) ;
778816 if ( freshContext ) setFreshSessionPending ( threadKey , false ) ;
817+ if ( keepFullHistory ) setKeepFullHistory ( threadKey , false ) ;
779818 enqueuePromise . then (
780819 ( ) => scheduleUnusedComposerAttachmentCleanup ( attachments ) ,
781820 ( error : unknown ) => {
@@ -861,6 +900,9 @@ export function useThreadComposerState(input: {
861900 ) ,
862901 providerSubagentStatus,
863902 isCompacting,
903+ compactBeforeSendTokens,
904+ keepFullHistory,
905+ toggleKeepFullHistory,
864906 composerDraftKey,
865907 followUpBehavior,
866908 canSteerActiveTurn,
0 commit comments