( currentInput: string, currentCursorOffset: number, )
| 426 | * Returns undefined if no editable commands in queue. |
| 427 | */ |
| 428 | export function popAllEditable( |
| 429 | currentInput: string, |
| 430 | currentCursorOffset: number, |
| 431 | ): PopAllEditableResult | undefined { |
| 432 | if (commandQueue.length === 0) { |
| 433 | return undefined |
| 434 | } |
| 435 | |
| 436 | const { editable = [], nonEditable = [] } = objectGroupBy( |
| 437 | [...commandQueue], |
| 438 | cmd => (isQueuedCommandEditable(cmd) ? 'editable' : 'nonEditable'), |
| 439 | ) |
| 440 | |
| 441 | if (editable.length === 0) { |
| 442 | return undefined |
| 443 | } |
| 444 | |
| 445 | // Extract text from queued commands (handles both strings and ContentBlockParam[]) |
| 446 | const queuedTexts = editable.map(cmd => extractTextFromValue(cmd.value)) |
| 447 | const newInput = [...queuedTexts, currentInput].filter(Boolean).join('\n') |
| 448 | |
| 449 | // Calculate cursor offset: length of joined queued commands + 1 + current cursor offset |
| 450 | const cursorOffset = queuedTexts.join('\n').length + 1 + currentCursorOffset |
| 451 | |
| 452 | // Extract images from queued commands |
| 453 | const images: PastedContent[] = [] |
| 454 | let nextImageId = Date.now() // Use timestamp as base for unique IDs |
| 455 | for (const cmd of editable) { |
| 456 | // handlePromptSubmit queues images in pastedContents (value is a string). |
| 457 | // Preserve the original PastedContent id so imageStore lookups still work. |
| 458 | if (cmd.pastedContents) { |
| 459 | for (const content of Object.values(cmd.pastedContents)) { |
| 460 | if (content.type === 'image') { |
| 461 | images.push(content) |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | // Bridge/remote commands may embed images directly in ContentBlockParam[]. |
| 466 | const cmdImages = extractImagesFromValue(cmd.value, nextImageId) |
| 467 | images.push(...cmdImages) |
| 468 | nextImageId += cmdImages.length |
| 469 | } |
| 470 | |
| 471 | for (const command of editable) { |
| 472 | logOperation( |
| 473 | 'popAll', |
| 474 | typeof command.value === 'string' ? command.value : undefined, |
| 475 | ) |
| 476 | } |
| 477 | |
| 478 | // Replace queue contents with only the non-editable commands |
| 479 | commandQueue.length = 0 |
| 480 | commandQueue.push(...nonEditable) |
| 481 | notifySubscribers() |
| 482 | |
| 483 | return { text: newInput, cursorOffset, images } |
| 484 | } |
| 485 |
no test coverage detected