* Lookup an attachment by id across staged attachments, pending run attachments, and disk index.
* Used by all three host runtime classes.
*
* @param {string} id
* @param {Map} attachments - staged attachment Map (this.attachments)
* @param {Iterable} runs - runs Map values (this.runs.values())
* @param {() => Promise<{entries: Array<{id: string}>}>} loadIndex - function that returns the disk attachment index
*/
export async function lookupAttachment(id, attachments, runs, loadIndex) {
const staged = attachments.get(id);
if (staged) {
return staged;
}
for (const run of runs) {
const pending = Array.isArray(run.pendingAttachments)
? run.pendingAttachments.find((item) => item.id === id)
: null;
if (pending) {
return pending;
}
}
const index = await loadIndex();
return index.entries.find((item) => item.id === id) || null;
}