| 1 |
import { isAbilityTool } from '@agent/lib/abilities'; |
| 2 |
import { __, sprintf } 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 |
content: ({ mediaId }) => ({ |
| 25 |
// translators: Title of a card in the AI Agent chat, shown after the user added an image to their website. |
| 26 |
title: __('Optimize your new image', 'extendify-local'), |
| 27 |
// translators: Body of a card in the AI Agent chat. Imagify is the name of an image optimization plugin. |
| 28 |
body: __( |
| 29 |
'Imagify can make the file smaller so your page loads faster.', |
| 30 |
'extendify-local', |
| 31 |
), |
| 32 |
action: { |
| 33 |
// translators: Button on a card in the AI Agent chat that starts optimizing an image with the Imagify plugin. |
| 34 |
label: __('Optimize image', 'extendify-local'), |
| 35 |
// The next run doesn't see this run's tool calls. |
| 36 |
message: sprintf( |
| 37 |
// translators: %d is the ID of an image in the WordPress media library. Shown in the AI Agent chat as the user's own request. Imagify is the name of an image optimization plugin. |
| 38 |
__('Optimize the image with ID %d using Imagify', 'extendify-local'), |
| 39 |
mediaId, |
| 40 |
), |
| 41 |
}, |
| 42 |
}), |
| 43 |
}; |
| 44 |
|