| 1 |
import { isAbilityTool } from '@agent/lib/abilities'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
const placedImage = ({ id, inputs, result }) => { |
| 5 |
if (id === 'acquire-image') return result?.id; |
| 6 |
if (id !== 'block-patching') return null; |
| 7 |
return inputs?.operations?.find( |
| 8 |
(operation) => operation?.op === 'replace-image', |
| 9 |
)?.image?.id; |
| 10 |
}; |
| 11 |
|
| 12 |
export default { |
| 13 |
id: 'optimize-image', |
| 14 |
follows: '*', |
| 15 |
data: ({ toolCalls }) => { |
| 16 |
const mediaId = toolCalls.map(placedImage).findLast(Boolean); |
| 17 |
return mediaId ? { mediaId } : null; |
| 18 |
}, |
| 19 |
// Imagify registers its abilities even with no account connected. |
| 20 |
score: ({ data, context }) => |
| 21 |
data && context?.hasImagifyApiKey && isAbilityTool('imagify/optimize-media') |
| 22 |
? 3 |
| 23 |
: 0, |
| 24 |
// translators: Title of a card in the AI Agent chat, shown after the user added an image to their website. |
| 25 |
title: __('Optimize your new image', 'extendify-local'), |
| 26 |
// translators: Body of a card in the AI Agent chat. Imagify is the name of an image optimization plugin. |
| 27 |
body: __( |
| 28 |
'Imagify can make the file smaller so your page loads faster.', |
| 29 |
'extendify-local', |
| 30 |
), |
| 31 |
action: { |
| 32 |
// translators: Button on a card in the AI Agent chat that starts optimizing an image with the Imagify plugin. |
| 33 |
label: __('Optimize image', 'extendify-local'), |
| 34 |
// translators: Shown in the AI Agent chat as the user's own request, sent when they click the "Optimize image" button after adding an image. Imagify is the name of an image optimization plugin. |
| 35 |
message: __('Optimize the new image using Imagify', 'extendify-local'), |
| 36 |
}, |
| 37 |
toolContext: ({ data }) => ({ |
| 38 |
refersTo: { mediaId: data.mediaId }, |
| 39 |
nextStep: 'Optimize this image using Imagify', |
| 40 |
}), |
| 41 |
}; |
| 42 |
|