@@ -38,6 +38,7 @@ import {
3838} from "./types" ;
3939import { resolveEnvironmentHttpUrl } from "./environments/runtime" ;
4040import { sanitizeThreadErrorMessage } from "./rpc/transportError" ;
41+ import { getThreadFromEnvironmentState } from "./threadDerivation" ;
4142
4243export interface EnvironmentState {
4344 projectIds : ProjectId [ ] ;
@@ -94,19 +95,6 @@ const MAX_THREAD_CHECKPOINTS = 500;
9495const MAX_THREAD_PROPOSED_PLANS = 200 ;
9596const MAX_THREAD_ACTIVITIES = 500 ;
9697const EMPTY_THREAD_IDS : ThreadId [ ] = [ ] ;
97- const EMPTY_MESSAGE_IDS : MessageId [ ] = [ ] ;
98- const EMPTY_ACTIVITY_IDS : string [ ] = [ ] ;
99- const EMPTY_PROPOSED_PLAN_IDS : string [ ] = [ ] ;
100- const EMPTY_TURN_IDS : TurnId [ ] = [ ] ;
101- const EMPTY_MESSAGES : ChatMessage [ ] = [ ] ;
102- const EMPTY_ACTIVITIES : OrchestrationThreadActivity [ ] = [ ] ;
103- const EMPTY_PROPOSED_PLANS : ProposedPlan [ ] = [ ] ;
104- const EMPTY_TURN_DIFF_SUMMARIES : TurnDiffSummary [ ] = [ ] ;
105- const EMPTY_MESSAGE_MAP : Record < MessageId , ChatMessage > = { } ;
106- const EMPTY_ACTIVITY_MAP : Record < string , OrchestrationThreadActivity > = { } ;
107- const EMPTY_PROPOSED_PLAN_MAP : Record < string , ProposedPlan > = { } ;
108- const EMPTY_TURN_DIFF_MAP : Record < TurnId , TurnDiffSummary > = { } ;
109- const EMPTY_THREAD_TURN_STATE : ThreadTurnState = Object . freeze ( { latestTurn : null } ) ;
11098
11199function arraysEqual < T > ( left : readonly T [ ] , right : readonly T [ ] ) : boolean {
112100 return left . length === right . length && left . every ( ( value , index ) => value === right [ index ] ) ;
@@ -403,78 +391,6 @@ function buildTurnDiffSlice(thread: Thread): {
403391 } ;
404392}
405393
406- function selectThreadMessages ( state : EnvironmentState , threadId : ThreadId ) : ChatMessage [ ] {
407- const ids = state . messageIdsByThreadId [ threadId ] ?? EMPTY_MESSAGE_IDS ;
408- const byId = state . messageByThreadId [ threadId ] ?? EMPTY_MESSAGE_MAP ;
409- if ( ids . length === 0 ) {
410- return EMPTY_MESSAGES ;
411- }
412- return ids . flatMap ( ( id ) => {
413- const message = byId [ id ] ;
414- return message ? [ message ] : [ ] ;
415- } ) ;
416- }
417-
418- function selectThreadActivities (
419- state : EnvironmentState ,
420- threadId : ThreadId ,
421- ) : OrchestrationThreadActivity [ ] {
422- const ids = state . activityIdsByThreadId [ threadId ] ?? EMPTY_ACTIVITY_IDS ;
423- const byId = state . activityByThreadId [ threadId ] ?? EMPTY_ACTIVITY_MAP ;
424- if ( ids . length === 0 ) {
425- return EMPTY_ACTIVITIES ;
426- }
427- return ids . flatMap ( ( id ) => {
428- const activity = byId [ id ] ;
429- return activity ? [ activity ] : [ ] ;
430- } ) ;
431- }
432-
433- function selectThreadProposedPlans ( state : EnvironmentState , threadId : ThreadId ) : ProposedPlan [ ] {
434- const ids = state . proposedPlanIdsByThreadId [ threadId ] ?? EMPTY_PROPOSED_PLAN_IDS ;
435- const byId = state . proposedPlanByThreadId [ threadId ] ?? EMPTY_PROPOSED_PLAN_MAP ;
436- if ( ids . length === 0 ) {
437- return EMPTY_PROPOSED_PLANS ;
438- }
439- return ids . flatMap ( ( id ) => {
440- const plan = byId [ id ] ;
441- return plan ? [ plan ] : [ ] ;
442- } ) ;
443- }
444-
445- function selectThreadTurnDiffSummaries (
446- state : EnvironmentState ,
447- threadId : ThreadId ,
448- ) : TurnDiffSummary [ ] {
449- const ids = state . turnDiffIdsByThreadId [ threadId ] ?? EMPTY_TURN_IDS ;
450- const byId = state . turnDiffSummaryByThreadId [ threadId ] ?? EMPTY_TURN_DIFF_MAP ;
451- if ( ids . length === 0 ) {
452- return EMPTY_TURN_DIFF_SUMMARIES ;
453- }
454- return ids . flatMap ( ( id ) => {
455- const summary = byId [ id ] ;
456- return summary ? [ summary ] : [ ] ;
457- } ) ;
458- }
459-
460- function getThread ( state : EnvironmentState , threadId : ThreadId ) : Thread | undefined {
461- const shell = state . threadShellById [ threadId ] ;
462- if ( ! shell ) {
463- return undefined ;
464- }
465- const turnState = state . threadTurnStateById [ threadId ] ?? EMPTY_THREAD_TURN_STATE ;
466- return {
467- ...shell ,
468- session : state . threadSessionById [ threadId ] ?? null ,
469- latestTurn : turnState . latestTurn ,
470- pendingSourceProposedPlan : turnState . pendingSourceProposedPlan ,
471- messages : selectThreadMessages ( state , threadId ) ,
472- activities : selectThreadActivities ( state , threadId ) ,
473- proposedPlans : selectThreadProposedPlans ( state , threadId ) ,
474- turnDiffSummaries : selectThreadTurnDiffSummaries ( state , threadId ) ,
475- } ;
476- }
477-
478394function getProjects ( state : EnvironmentState ) : Project [ ] {
479395 return state . projectIds . flatMap ( ( projectId ) => {
480396 const project = state . projectById [ projectId ] ;
@@ -484,7 +400,7 @@ function getProjects(state: EnvironmentState): Project[] {
484400
485401function getThreads ( state : EnvironmentState ) : Thread [ ] {
486402 return state . threadIds . flatMap ( ( threadId ) => {
487- const thread = getThread ( state , threadId ) ;
403+ const thread = getThreadFromEnvironmentState ( state , threadId ) ;
488404 return thread ? [ thread ] : [ ] ;
489405 } ) ;
490406}
@@ -896,7 +812,7 @@ function updateThreadState(
896812 threadId : ThreadId ,
897813 updater : ( thread : Thread ) => Thread ,
898814) : EnvironmentState {
899- const currentThread = getThread ( state , threadId ) ;
815+ const currentThread = getThreadFromEnvironmentState ( state , threadId ) ;
900816 if ( ! currentThread ) {
901817 return state ;
902818 }
@@ -1163,7 +1079,7 @@ function applyEnvironmentOrchestrationEvent(
11631079 }
11641080
11651081 case "thread.created" : {
1166- const previousThread = getThread ( state , event . payload . threadId ) ;
1082+ const previousThread = getThreadFromEnvironmentState ( state , event . payload . threadId ) ;
11671083 const nextThread = mapThread (
11681084 {
11691085 id : event . payload . threadId ,
@@ -1669,10 +1585,19 @@ export function selectThreadByRef(
16691585 ref : ScopedThreadRef | null | undefined ,
16701586) : Thread | undefined {
16711587 return ref
1672- ? getThread ( selectEnvironmentState ( state , ref . environmentId ) , ref . threadId )
1588+ ? getThreadFromEnvironmentState ( selectEnvironmentState ( state , ref . environmentId ) , ref . threadId )
16731589 : undefined ;
16741590}
16751591
1592+ export function selectThreadExistsByRef (
1593+ state : AppState ,
1594+ ref : ScopedThreadRef | null | undefined ,
1595+ ) : boolean {
1596+ return ref
1597+ ? selectEnvironmentState ( state , ref . environmentId ) . threadShellById [ ref . threadId ] !== undefined
1598+ : false ;
1599+ }
1600+
16761601export function selectSidebarThreadSummaryByRef (
16771602 state : AppState ,
16781603 ref : ScopedThreadRef | null | undefined ,
0 commit comments