Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions api-panel/api-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,16 +630,33 @@ export function initApiPanel({ rootWindow = window, rootDocument = document } =
break;
}
case 'INJECT_TEXT_WITH_IMAGES': {
if (typeof data.text === 'string') {
const fallbackImages = Array.isArray(data.fallbackImages) ? data.fallbackImages : [];
const fallbackIds = fallbackImages
.map((img) => img?.id)
.filter((id) => typeof id === 'string');
const isClearedOnRetry = Boolean(
data.retry &&
fallbackImages.length > 0 &&
composerInputEl.value.trim().length === 0 &&
stagedImages.length === 0
);
if (typeof data.text === 'string' && (data.text.length > 0 || !data.retry || isClearedOnRetry)) {
composerInputEl.value = data.text;
}
const incomingImages = Array.isArray(data.images) ? data.images : [];
const succeededIds = mergeIncomingImages(incomingImages);
const incomingImages = isClearedOnRetry
? fallbackImages
: (Array.isArray(data.images) ? data.images : []);
const succeededIds = incomingImages.length === 0 && data.retry && fallbackIds.length > 0
? fallbackIds
: mergeIncomingImages(incomingImages);
const expectedCount = incomingImages.length === 0 && data.retry && fallbackIds.length > 0
? fallbackIds.length
: incomingImages.length;
syncComposerControls();
if (data.action === 'fill') {
postFillActionResult(data.requestId, incomingImages.length, succeededIds);
postFillActionResult(data.requestId, expectedCount, succeededIds);
}
if (data.autoSubmit && succeededIds.length === incomingImages.length) {
if (data.autoSubmit && succeededIds.length === expectedCount) {
submitCurrentPayload();
}
break;
Expand Down
42 changes: 34 additions & 8 deletions content-scripts/custom-provider-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@
if (Array.isArray(result.succeededImageIds)) {
message.succeededImageIds = result.succeededImageIds;
}
if (!result.ok && Array.isArray(result.attemptedImageIds)) {
message.attemptedImageIds = result.attemptedImageIds;
}
if (!result.ok) {
message.reason = result.reason || ACTION_FAILURE_REASONS.INJECTION_ERROR;
}
Expand Down Expand Up @@ -1865,9 +1868,14 @@
// ===== Message Dispatch =====

async function handleFillWithResult(eventData, providerId) {
const { text, images, autoSubmit, requestId, retry = false } = eventData;
const { text, images, fallbackImages, autoSubmit, requestId, retry = false } = eventData;
const hasImages = Array.isArray(images) && images.length > 0;
const normalizedFallbackImages = Array.isArray(fallbackImages) ? fallbackImages : [];
const fallbackSucceededIds = normalizedFallbackImages
.map((img) => img?.id)
.filter((id) => typeof id === 'string');
const hasText = typeof text === 'string' && text.length > 0;
const hasFallbackRetry = Boolean(retry && normalizedFallbackImages.length > 0);

if (requestId && providerId) {
startUserInteractionTracking(requestId, providerId);
Expand All @@ -1884,7 +1892,7 @@
return;
}

if (!hasImages && !hasText) {
if (!hasImages && !hasText && !hasFallbackRetry) {
postActionResult(requestId, providerId, {
ok: false,
reason: ACTION_FAILURE_REASONS.INJECTION_ERROR,
Expand All @@ -1893,7 +1901,7 @@
return;
}

const composer = await findComposerWithRetry(hasImages ? 600 : 1000);
const composer = await findComposerWithRetry(hasImages || hasFallbackRetry ? 600 : 1000);
if (!composer) {
postActionResult(requestId, providerId, {
ok: false,
Expand All @@ -1903,23 +1911,39 @@
return;
}

let succeededImageIds = [];
if (hasImages) {
const uploadResult = await uploadImagesToComposer(composer, images);
const composerClearedOnRetry = Boolean(
hasFallbackRetry &&
isComposerEmpty(composer) &&
inspectComposerAttachments(composer).count === 0
);
const effectiveImages = composerClearedOnRetry
? normalizedFallbackImages
: (Array.isArray(images) ? images : []);
const effectiveRetry = composerClearedOnRetry ? false : Boolean(retry);
const failureAttemptedIds = composerClearedOnRetry
? effectiveImages.map((img) => img?.id).filter((id) => typeof id === 'string')
: undefined;

let succeededImageIds = effectiveImages.length === 0 && hasFallbackRetry
? fallbackSucceededIds
: [];
if (effectiveImages.length > 0) {
const uploadResult = await uploadImagesToComposer(composer, effectiveImages);
succeededImageIds = uploadResult.succeededImageIds || [];
if (!uploadResult.ok) {
if (hasText) {
const refreshedComposer = composer.isConnected ? composer : findComposerElement();
if (refreshedComposer) {
await injectTextIntoComposer(refreshedComposer, text, {
skipIfAlreadyPresent: Boolean(retry),
skipIfAlreadyPresent: effectiveRetry,
});
}
}
postActionResult(requestId, providerId, {
ok: false,
reason: uploadResult.reason || ACTION_FAILURE_REASONS.INJECTION_ERROR,
succeededImageIds,
attemptedImageIds: failureAttemptedIds,
});
return;
}
Expand All @@ -1932,17 +1956,19 @@
ok: false,
reason: ACTION_FAILURE_REASONS.CONTROL_NOT_FOUND,
succeededImageIds,
attemptedImageIds: failureAttemptedIds,
});
return;
}
const injected = await injectTextIntoComposer(activeComposer, text, {
skipIfAlreadyPresent: Boolean(retry),
skipIfAlreadyPresent: effectiveRetry,
});
if (!injected) {
postActionResult(requestId, providerId, {
ok: false,
reason: ACTION_FAILURE_REASONS.INJECTION_ERROR,
succeededImageIds,
attemptedImageIds: failureAttemptedIds,
});
return;
}
Expand Down
Loading
Loading