(references?: Reference[])
| 14 | } |
| 15 | |
| 16 | export function useReferenceProcessing(references?: Reference[]) { |
| 17 | const [processingStatus, setProcessingStatus] = useState<ProcessingStatus>( |
| 18 | {}, |
| 19 | ); |
| 20 | |
| 21 | // Create an array of reference IDs to monitor |
| 22 | const referenceIds = references?.map((ref) => ref.id) || []; |
| 23 | |
| 24 | // For each reference, create a tag to monitor |
| 25 | const tags = referenceIds.map((id) => `reference:${id}`); |
| 26 | |
| 27 | // Use the useRealtimeRunsWithTag hook to listen for processing status updates |
| 28 | const { runs, error } = useRealtimeRunsWithTag(tags.join(",")); |
| 29 | |
| 30 | // Update processing status whenever runs change |
| 31 | useEffect(() => { |
| 32 | if (!runs || !Array.isArray(runs)) return; |
| 33 | |
| 34 | const newProcessingStatus: ProcessingStatus = {}; |
| 35 | |
| 36 | for (const run of runs) { |
| 37 | // Extract the reference ID from the tag |
| 38 | const refTag = run.tags?.find((tagValue: string) => |
| 39 | tagValue.startsWith("reference:"), |
| 40 | ); |
| 41 | if (!refTag) continue; |
| 42 | |
| 43 | const refId = refTag.replace("reference:", ""); |
| 44 | |
| 45 | // Calculate progress if available in metadata |
| 46 | let progress: number | undefined; |
| 47 | let statusMessage: string | undefined; |
| 48 | |
| 49 | if (run.metadata) { |
| 50 | if (typeof run.metadata.progress === "number") { |
| 51 | progress = run.metadata.progress; |
| 52 | } |
| 53 | if (typeof run.metadata.status === "string") { |
| 54 | statusMessage = run.metadata.status; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | newProcessingStatus[refId] = { |
| 59 | isProcessing: |
| 60 | run.status === "RUNNING" || |
| 61 | run.status === "PENDING" || |
| 62 | run.status === "QUEUED", |
| 63 | progress, |
| 64 | status: statusMessage || run.status, |
| 65 | error: run.status === "FAILED" ? run.error?.message : undefined, |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | setProcessingStatus(newProcessingStatus); |
| 70 | }, [runs]); |
| 71 | |
| 72 | return { |
| 73 | processingStatus, |
no test coverage detected