@@ -15,7 +15,7 @@ import {
1515 ListOrderedIcon ,
1616 PencilIcon ,
1717} from "lucide-react" ;
18- import { useId , useImperativeHandle , useMemo , useRef , useState , type Ref } from "react" ;
18+ import { useId , useImperativeHandle , useRef , useState , type Ref } from "react" ;
1919
2020import { useAssetUrls } from "../../assets/assetUrls" ;
2121import { threadEnvironment } from "../../state/threads" ;
@@ -47,6 +47,31 @@ export interface QueuedRunsControlHandle {
4747 editLatest : ( repeat : boolean ) => boolean ;
4848}
4949
50+ type ThreadQueueProjection = Parameters < typeof deriveThreadQueueWorkflowState > [ 0 ] ;
51+
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 ) ) ;
73+ }
74+
5075export function QueuedRunsControl ( {
5176 ref,
5277 ...props
@@ -79,51 +104,25 @@ export function QueuedRunsControl({
79104 // Drags must start from the grip, not from an accidental text-drag on the
80105 // row. The grip arms its run id on pointerdown; dragstart verifies it.
81106 const dragArmedRunIdRef = useRef < RunId | null > ( null ) ;
82- const workflow = useMemo (
83- ( ) => ( projection ? deriveThreadQueueWorkflowState ( projection ) : null ) ,
84- [ projection ] ,
85- ) ;
107+ const { workflow, queuedImageAttachmentIds, queuedImageAttachmentResources } =
108+ useQueueWorkflow ( projection ) ;
86109 const queued = workflow ?. queuedRuns ?? [ ] ;
87110 const activeRun = workflow ?. activeRun ?? null ;
88111 const canReorder = workflow ?. canReorder === true ;
89- const queuedImageAttachmentIds = useMemo ( ( ) => {
90- const ids : string [ ] = [ ] ;
91- for ( const { attachments } of workflow ?. queuedRuns ?? [ ] ) {
92- for ( const attachment of attachments ) {
93- if ( attachment . type === "image" ) ids . push ( attachment . id ) ;
94- }
95- }
96- return ids ;
97- } , [ workflow ] ) ;
98- const queuedImageAttachmentResources = useMemo (
99- ( ) =>
100- queuedImageAttachmentIds . map ( ( attachmentId ) => ( {
101- _tag : "attachment" as const ,
102- attachmentId,
103- } ) ) ,
104- [ queuedImageAttachmentIds ] ,
105- ) ;
106112 const queuedImageAttachmentUrls = useAssetUrls (
107113 props . environmentId ,
108114 queuedImageAttachmentResources ,
109115 ) ;
110- const queuedImageUrlById = useMemo (
111- ( ) =>
112- new Map (
113- queuedImageAttachmentIds . flatMap ( ( attachmentId , index ) => {
114- const url = queuedImageAttachmentUrls [ index ] ;
115- return url ? [ [ attachmentId , url ] as const ] : [ ] ;
116- } ) ,
117- ) ,
118- [ queuedImageAttachmentIds , queuedImageAttachmentUrls ] ,
116+ const queuedImageUrlById = new Map (
117+ queuedImageAttachmentIds . flatMap ( ( attachmentId , index ) => {
118+ const url = queuedImageAttachmentUrls [ index ] ;
119+ return url ? [ [ attachmentId , url ] as const ] : [ ] ;
120+ } ) ,
119121 ) ;
120122 // Once the projection holds the message the optimistic copy is stale no
121123 // matter what happened to its run — keying on still-queued runs alone would
122124 // resurrect a phantom "pending" row after the run is cancelled or started.
123- const acknowledgedMessageIds = useMemo (
124- ( ) => new Set ( ( projection ?. messages ?? [ ] ) . map ( ( message ) => message . id ) ) ,
125- [ projection ] ,
126- ) ;
125+ const acknowledgedMessageIds = useAcknowledgedMessageIds ( projection ) ;
127126 const optimisticQueued = props . optimisticMessages . filter (
128127 ( message ) => message . inputIntent === "queued_turn" && ! acknowledgedMessageIds . has ( message . id ) ,
129128 ) ;
@@ -164,14 +163,11 @@ export function QueuedRunsControl({
164163
165164 const move = async ( runId : RunId , beforeRunId : RunId | null ) => {
166165 setBusyRunId ( runId ) ;
167- try {
168- await reorder ( {
169- environmentId : props . environmentId ,
170- input : { threadId : props . threadId , runId, beforeRunId } ,
171- } ) ;
172- } finally {
173- setBusyRunId ( null ) ;
174- }
166+ await reorder ( {
167+ environmentId : props . environmentId ,
168+ input : { threadId : props . threadId , runId, beforeRunId } ,
169+ } ) ;
170+ setBusyRunId ( null ) ;
175171 } ;
176172
177173 const completeDrag = ( runId : RunId , insertIndex : number | null ) => {
@@ -189,15 +185,12 @@ export function QueuedRunsControl({
189185 if ( activeRun === null || ! workflow ?. canPromoteToSteer || steerInFlightRef . current ) return ;
190186 steerInFlightRef . current = true ;
191187 setBusyRunId ( queuedRunId ) ;
192- try {
193- await promote ( {
194- environmentId : props . environmentId ,
195- input : { threadId : props . threadId , queuedRunId, targetRunId : activeRun . id } ,
196- } ) ;
197- } finally {
198- steerInFlightRef . current = false ;
199- setBusyRunId ( null ) ;
200- }
188+ await promote ( {
189+ environmentId : props . environmentId ,
190+ input : { threadId : props . threadId , queuedRunId, targetRunId : activeRun . id } ,
191+ } ) ;
192+ steerInFlightRef . current = false ;
193+ setBusyRunId ( null ) ;
201194 } ;
202195
203196 useImperativeHandle ( ref , ( ) => ( {
@@ -229,14 +222,11 @@ export function QueuedRunsControl({
229222
230223 const remove = async ( runId : RunId ) => {
231224 setBusyRunId ( runId ) ;
232- try {
233- await cancel ( {
234- environmentId : props . environmentId ,
235- input : { threadId : props . threadId , runId } ,
236- } ) ;
237- } finally {
238- setBusyRunId ( null ) ;
239- }
225+ await cancel ( {
226+ environmentId : props . environmentId ,
227+ input : { threadId : props . threadId , runId } ,
228+ } ) ;
229+ setBusyRunId ( null ) ;
240230 } ;
241231
242232 return (
0 commit comments