Open History to see the last commit
1import type { MessageId, ThreadId, TurnId } from "@workbench/contracts";
2import type { EnvironmentState } from "./store";
3import type {
4 ChatMessage,
5 ProposedPlan,
6 Thread,
7 ThreadSession,
8 ThreadShell,
9 ThreadTurnState,
10 TurnDiffSummary,
11} from "./types";
12
13const EMPTY_MESSAGES: ChatMessage[] = [];
14const EMPTY_ACTIVITIES: Thread["activities"] = [];
15const EMPTY_PROPOSED_PLANS: ProposedPlan[] = [];
16const EMPTY_TURN_DIFF_SUMMARIES: TurnDiffSummary[] = [];
17const EMPTY_MESSAGE_MAP: Record<MessageId, ChatMessage> = {};
18const EMPTY_ACTIVITY_MAP: Record<string, Thread["activities"][number]> = {};
19const EMPTY_PROPOSED_PLAN_MAP: Record<string, ProposedPlan> = {};
20const EMPTY_TURN_DIFF_MAP: Record<TurnId, TurnDiffSummary> = {};
21
22const collectedByIdsCache = new WeakMap<readonly string[], WeakMap<object, readonly unknown[]>>();
23const threadCache = new WeakMap<
24 ThreadShell,
25 {
26 session: ThreadSession | null;
27 turnState: ThreadTurnState | undefined;
28 messages: Thread["messages"];
29 activities: Thread["activities"];
30 proposedPlans: Thread["proposedPlans"];
31 turnDiffSummaries: Thread["turnDiffSummaries"];
32 thread: Thread;
33 }
34>();
35
36function collectByIds<TKey extends string, TValue>(
37 ids: readonly TKey[] | undefined,
38 byId: Record<TKey, TValue> | undefined,
39 emptyValue: TValue[],
40): TValue[] {
41 if (!ids || ids.length === 0 || !byId) {
42 return emptyValue;
43 }
44
45 const cachedByRecord = collectedByIdsCache.get(ids);
46 const cached = cachedByRecord?.get(byId);
47 if (cached) {
48 return cached as TValue[];
49 }
50
51 const nextValues = ids.flatMap((id) => {
52 const value = byId[id];
53 return value ? [value] : [];
54 });
55 const nextCachedByRecord = cachedByRecord ?? new WeakMap<object, readonly unknown[]>();
56 nextCachedByRecord.set(byId, nextValues);
57 if (!cachedByRecord) {
58 collectedByIdsCache.set(ids, nextCachedByRecord);
59 }
60 return nextValues;
61}
62
63function selectThreadMessages(state: EnvironmentState, threadId: ThreadId): Thread["messages"] {
64 return collectByIds(
65 state.messageIdsByThreadId[threadId],
66 state.messageByThreadId[threadId] ?? EMPTY_MESSAGE_MAP,
67 EMPTY_MESSAGES,
68 );
69}
70
71function selectThreadActivities(state: EnvironmentState, threadId: ThreadId): Thread["activities"] {
72 return collectByIds(
73 state.activityIdsByThreadId[threadId],
74 state.activityByThreadId[threadId] ?? EMPTY_ACTIVITY_MAP,
75 EMPTY_ACTIVITIES,
76 );
77}
78
79function selectThreadProposedPlans(
80 state: EnvironmentState,
81 threadId: ThreadId,
82): Thread["proposedPlans"] {
83 return collectByIds(
84 state.proposedPlanIdsByThreadId[threadId],
85 state.proposedPlanByThreadId[threadId] ?? EMPTY_PROPOSED_PLAN_MAP,
86 EMPTY_PROPOSED_PLANS,
87 );
88}
89
90function selectThreadTurnDiffSummaries(
91 state: EnvironmentState,
92 threadId: ThreadId,
93): Thread["turnDiffSummaries"] {
94 return collectByIds(
95 state.turnDiffIdsByThreadId[threadId],
96 state.turnDiffSummaryByThreadId[threadId] ?? EMPTY_TURN_DIFF_MAP,
97 EMPTY_TURN_DIFF_SUMMARIES,
98 );
99}
100
101export function getThreadFromEnvironmentState(
102 state: EnvironmentState,
103 threadId: ThreadId,
104): Thread | undefined {
105 const shell = state.threadShellById[threadId];
106 if (!shell) {
107 return undefined;
108 }
109
110 const session = state.threadSessionById[threadId] ?? null;
111 const turnState = state.threadTurnStateById[threadId];
112 const messages = selectThreadMessages(state, threadId);
113 const activities = selectThreadActivities(state, threadId);
114 const proposedPlans = selectThreadProposedPlans(state, threadId);
115 const turnDiffSummaries = selectThreadTurnDiffSummaries(state, threadId);
116 const cached = threadCache.get(shell);
117
118 if (
119 cached &&
120 cached.session === session &&
121 cached.turnState === turnState &&
122 cached.messages === messages &&
123 cached.activities === activities &&
124 cached.proposedPlans === proposedPlans &&
125 cached.turnDiffSummaries === turnDiffSummaries
126 ) {
127 return cached.thread;
128 }
129
130 const thread: Thread = {
131 ...shell,
132 session,
133 latestTurn: turnState?.latestTurn ?? null,
134 pendingSourceProposedPlan: turnState?.pendingSourceProposedPlan,
135 messages,
136 activities,
137 proposedPlans,
138 turnDiffSummaries,
139 };
140
141 threadCache.set(shell, {
142 session,
143 turnState,
144 messages,
145 activities,
146 proposedPlans,
147 turnDiffSummaries,
148 thread,
149 });
150
151 return thread;
152}