@@ -49,27 +49,15 @@ export interface QueuedRunsControlHandle {
4949
5050type ThreadQueueProjection = Parameters < typeof deriveThreadQueueWorkflowState > [ 0 ] ;
5151
52- // Separate hooks so these stay cached across the useAssetUrls call, which keys on
53- // the resources array.
54- function useQueueWorkflow ( projection : ThreadQueueProjection | undefined ) {
55- "use memo" ;
56- const workflow = projection ? deriveThreadQueueWorkflowState ( projection ) : null ;
57- const queuedImageAttachmentIds : string [ ] = [ ] ;
58- for ( const { attachments } of workflow ?. queuedRuns ?? [ ] ) {
59- for ( const attachment of attachments ) {
60- if ( attachment . type === "image" ) queuedImageAttachmentIds . push ( attachment . id ) ;
61- }
62- }
63- const queuedImageAttachmentResources = queuedImageAttachmentIds . map ( ( attachmentId ) => ( {
64- _tag : "attachment" as const ,
65- attachmentId,
66- } ) ) ;
67- return { workflow, queuedImageAttachmentIds, queuedImageAttachmentResources } ;
68- }
69-
70- function useAcknowledgedMessageIds ( projection : ThreadQueueProjection | undefined ) {
71- "use memo" ;
72- return new Set ( ( projection ?. messages ?? [ ] ) . map ( ( message ) => message . id ) ) ;
52+ function queuedImageResources ( projection : ThreadQueueProjection | undefined ) {
53+ if ( ! projection ) return [ ] ;
54+ return deriveThreadQueueWorkflowState ( projection ) . queuedRuns . flatMap ( ( { attachments } ) =>
55+ attachments . flatMap ( ( attachment ) =>
56+ attachment . type === "image"
57+ ? [ { _tag : "attachment" as const , attachmentId : attachment . id } ]
58+ : [ ] ,
59+ ) ,
60+ ) ;
7361}
7462
7563export function QueuedRunsControl ( {
@@ -104,25 +92,26 @@ export function QueuedRunsControl({
10492 // Drags must start from the grip, not from an accidental text-drag on the
10593 // row. The grip arms its run id on pointerdown; dragstart verifies it.
10694 const dragArmedRunIdRef = useRef < RunId | null > ( null ) ;
107- const { workflow, queuedImageAttachmentIds, queuedImageAttachmentResources } =
108- useQueueWorkflow ( projection ) ;
109- const queued = workflow ?. queuedRuns ?? [ ] ;
110- const activeRun = workflow ?. activeRun ?? null ;
111- const canReorder = workflow ?. canReorder === true ;
95+ const steerInFlightRef = useRef ( false ) ;
96+ // Once the projection holds the message the optimistic copy is stale no
97+ // matter what happened to its run — keying on still-queued runs alone would
98+ // resurrect a phantom "pending" row after the run is cancelled or started.
99+ const acknowledgedMessageIds = new Set ( ( projection ?. messages ?? [ ] ) . map ( ( message ) => message . id ) ) ;
100+ const queuedImageAttachmentResources = queuedImageResources ( projection ) ;
112101 const queuedImageAttachmentUrls = useAssetUrls (
113102 props . environmentId ,
114103 queuedImageAttachmentResources ,
115104 ) ;
105+ const workflow = projection ? deriveThreadQueueWorkflowState ( projection ) : null ;
106+ const queued = workflow ?. queuedRuns ?? [ ] ;
107+ const activeRun = workflow ?. activeRun ?? null ;
108+ const canReorder = workflow ?. canReorder === true ;
116109 const queuedImageUrlById = new Map (
117- queuedImageAttachmentIds . flatMap ( ( attachmentId , index ) => {
118- const url = queuedImageAttachmentUrls [ index ] ;
119- return url ? [ [ attachmentId , url ] as const ] : [ ] ;
110+ queuedImageAttachmentUrls . flatMap ( ( url , index ) => {
111+ const resource = queuedImageAttachmentResources [ index ] ;
112+ return url && resource ? [ [ resource . attachmentId , url ] as const ] : [ ] ;
120113 } ) ,
121114 ) ;
122- // Once the projection holds the message the optimistic copy is stale no
123- // matter what happened to its run — keying on still-queued runs alone would
124- // resurrect a phantom "pending" row after the run is cancelled or started.
125- const acknowledgedMessageIds = useAcknowledgedMessageIds ( projection ) ;
126115 const optimisticQueued = props . optimisticMessages . filter (
127116 ( message ) => message . inputIntent === "queued_turn" && ! acknowledgedMessageIds . has ( message . id ) ,
128117 ) ;
@@ -180,7 +169,6 @@ export function QueuedRunsControl({
180169 void move ( runId , queued [ insertIndex ] ?. run . id ?? null ) ;
181170 } ;
182171
183- const steerInFlightRef = useRef ( false ) ;
184172 const steer = async ( queuedRunId : RunId ) => {
185173 if ( activeRun === null || ! workflow ?. canPromoteToSteer || steerInFlightRef . current ) return ;
186174 steerInFlightRef . current = true ;
0 commit comments