@@ -22,6 +22,7 @@ export type CitationPhase =
2222export interface CitedThreadSnapshot {
2323 readonly message : "assistant" | "other" | null ;
2424 readonly hasMoreHistory : boolean ;
25+ readonly loadFailed ?: true ;
2526}
2627
2728export type CitedHistoryOutcome =
@@ -62,6 +63,7 @@ export async function loadCitedHistory(input: {
6263 for ( let attempts = 0 ; ; attempts += 1 ) {
6364 const thread = await input . readThread ( ) ;
6465 if ( thread === null || input . signal . aborted ) return { _tag : "aborted" } ;
66+ if ( thread . loadFailed ) return COULD_NOT_LOAD ;
6567 if ( thread . message === "assistant" ) return { _tag : "found" } ;
6668 if ( thread . message === "other" ) {
6769 return {
@@ -102,6 +104,39 @@ export function resolveCitationPhase(input: {
102104 return { _tag : "expanding" } ;
103105}
104106
107+ type ThreadDetail < K extends keyof typeof environmentThreadDetails > = Atom . Type <
108+ ReturnType < ( typeof environmentThreadDetails ) [ K ] >
109+ > ;
110+
111+ /**
112+ * The cited thread as `loadCitedHistory` reads it, or null while it is still
113+ * settling. A thread that failed to load before any projection arrived counts
114+ * as settled, so the citation fails instead of waiting on the stream's retries.
115+ */
116+ export function citedThreadSnapshot ( input : {
117+ readonly thread : ThreadDetail < "quietThreadAtom" > ;
118+ readonly history : ThreadDetail < "historyAtom" > ;
119+ readonly status : ThreadDetail < "statusAtom" > ;
120+ readonly error : ThreadDetail < "errorAtom" > ;
121+ readonly messageId : string ;
122+ } ) : CitedThreadSnapshot | null {
123+ if ( input . history . loading ) return null ;
124+ if ( input . thread === null ) {
125+ if ( input . status === "deleted" ) return { message : null , hasMoreHistory : false } ;
126+ return input . error === null ? null : { message : null , hasMoreHistory : false , loadFailed : true } ;
127+ }
128+ // Older pages merge into the visible turn items, not the message list.
129+ const item = input . thread . projection . visibleTurnItems . find (
130+ ( row ) =>
131+ ( row . item . type === "assistant_message" || row . item . type === "user_message" ) &&
132+ row . item . messageId === input . messageId ,
133+ ) ?. item ;
134+ return {
135+ message : item === undefined ? null : item . type === "assistant_message" ? "assistant" : "other" ,
136+ hasMoreHistory : input . history . hasMoreHistory ,
137+ } ;
138+ }
139+
105140interface CitedHistoryKey {
106141 readonly key : string ;
107142 readonly environmentId : AssistantCitationRequest [ "citation" ] [ "environmentId" ] ;
@@ -146,26 +181,16 @@ const citedHistoryAtom = Atom.family(
146181 const threadAtom = environmentThreadDetails . quietThreadAtom ( ref ) ;
147182 const historyAtom = environmentThreadDetails . historyAtom ( ref ) ;
148183 const statusAtom = environmentThreadDetails . statusAtom ( ref ) ;
149- const settledThreadAtom = Atom . make ( ( get ) : CitedThreadSnapshot | null => {
150- const thread = get ( threadAtom ) ;
151- const history = get ( historyAtom ) ;
152- const status = get ( statusAtom ) ;
153- if ( history . loading ) return null ;
154- if ( thread === null ) {
155- return status === "deleted" ? { message : null , hasMoreHistory : false } : null ;
156- }
157- // Older pages merge into the visible turn items, not the message list.
158- const item = thread . projection . visibleTurnItems . find (
159- ( row ) =>
160- ( row . item . type === "assistant_message" || row . item . type === "user_message" ) &&
161- row . item . messageId === messageId ,
162- ) ?. item ;
163- return {
164- message :
165- item === undefined ? null : item . type === "assistant_message" ? "assistant" : "other" ,
166- hasMoreHistory : history . hasMoreHistory ,
167- } ;
168- } ) ;
184+ const errorAtom = environmentThreadDetails . errorAtom ( ref ) ;
185+ const settledThreadAtom = Atom . make ( ( get ) =>
186+ citedThreadSnapshot ( {
187+ thread : get ( threadAtom ) ,
188+ history : get ( historyAtom ) ,
189+ status : get ( statusAtom ) ,
190+ error : get ( errorAtom ) ,
191+ messageId,
192+ } ) ,
193+ ) ;
169194 return Atom . make ( ( get ) : CitedHistory => {
170195 const controller = new AbortController ( ) ;
171196 get . addFinalizer ( ( ) => controller . abort ( ) ) ;
0 commit comments