| 1 |
(()=>{"use strict";const e={n:t=>{const n=t&&t.__esModule?()=>t.default:()=>t;return e.d(n,{a:n}),n},d:(t,n)=>{if(Array.isArray(n))for(var i=0;i<n.length;){var a=n[i++],r=n[i++];e.o(t,a)?0===r&&i++:0===r?Object.defineProperty(t,a,{enumerable:!0,value:n[i++]}):Object.defineProperty(t,a,{enumerable:!0,get:r})}else for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.hasOwn(e,t)},t=window.wp.blocks,n=window.wp.hooks,i=window.wp.i18n,a=window.wp.plugins,r=window.wp.components,o=window.wp.editor,s=window.wp.primitives,c=window.ReactJSXRuntime;var l=(0,c.jsx)(s.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",children:(0,c.jsx)(s.Path,{d:"m11.3 17.2-5-5c-.1-.1-.1-.3 0-.4l2.3-2.3-1.1-1-2.3 2.3c-.7.7-.7 1.8 0 2.5l5 5H7.5v1.5h5.3v-5.2h-1.5v2.6zm7.5-6.4-5-5h2.7V4.2h-5.2v5.2h1.5V6.8l5 5c.1.1.1.3 0 .4l-2.3 2.3 1.1 1.1 2.3-2.3c.6-.7.6-1.9-.1-2.5z"})});const u=window.wp.compose,m=window.wp.blockEditor,d=window.wp.data,w=window.wp.element,p=window.wp.notices,h=window.wp.apiFetch;var g=e.n(h);const b=window.wp.url;let y=!1;const f=()=>{y||(console.warn("[AI] Client ability execution is unavailable. Falling back to REST."),y=!0)},_=(e,t,n)=>{if("GET"===n||"DELETE"===n)return{path:null==t?`/wp-abilities/v1/abilities/${e}/run`:(0,b.addQueryArgs)(`/wp-abilities/v1/abilities/${e}/run`,{input:t}),method:n};const i={path:`/wp-abilities/v1/abilities/${e}/run`,method:"POST"};return void 0!==t&&(i.data={input:t}),i};const S=window.wp.wordcount;function x(e,t){return function(e){return(0,S.count)(e,"characters_excluding_spaces")}(e)>=t}function k(e){return e.reduce((e,t)=>(e.push(t),t.innerBlocks?.length&&e.push(...k(t.innerBlocks)),e),[])}function v(e){return e.split(/\n\n+/).map(e=>e.trim()).filter(Boolean).map(e=>(0,t.createBlock)("core/paragraph",{content:e}))}function z(e){return k(e).find(e=>"core/group"===e.name&&!0===e.attributes.aiGeneratedSummary)}const B="ai_summarization_error";let j=!1;const C=new Set;function P(e){return C.add(e),()=>{C.delete(e)}}function E(){return j}function I(e){j=e,C.forEach(e=>e())}const T=()=>{const e=window.aiSummarizationData??{};return{enabled:e.enabled??!1,minContentLength:e.minContentLength??250}};function G(){const{allBlocks:e,postId:n,content:a,meta:r}=(0,d.useSelect)(e=>({allBlocks:e(m.store).getBlocks(),postId:e(o.store).getCurrentPostId(),content:e(o.store).getEditedPostContent(),meta:e(o.store).getEditedPostAttribute("meta")}),[]),{editPost:s}=(0,d.useDispatch)(o.store);return{isSummarizing:(0,w.useSyncExternalStore)(P,E),hasSummary:(0,w.useMemo)(()=>Boolean(z(e)),[e]),handleSummarize:async()=>{if(!j&&function(e){const t=window.aiProviderData??{hasProvider:!1,connectorsUrl:""};if(t.hasProvider)return!0;const{connectorsUrl:n}=t;return(0,d.dispatch)(p.store).createErrorNotice((0,i.__)("This feature requires an AI Connector to function properly.","ai"),{id:e,isDismissible:!0,actions:n?[{label:(0,i.__)("Manage Connectors","ai"),url:n}]:[]}),!1}(B)){I(!0),(0,d.dispatch)(p.store).removeNotice(B);try{const o=await async function(e,t){return async function(e,t,n){try{const n=await(async()=>{try{const{ready:e}=await import("@wordpress/core-abilities");return await e,await import("@wordpress/abilities")}catch{return null}})();if("function"==typeof n?.executeAbility)return await n.executeAbility(e,t);f()}catch(e){if(console.error(e),!(e=>{if(!e||"object"!=typeof e)return!1;const t="message"in e&&"string"==typeof e.message?e.message:"",n="code"in e&&"string"==typeof e.code?e.code:"";return"ability_not_found"===n||t.includes("Ability not found")||t.includes("missing callback")||"ability_invalid_input"===n})(e))throw e;f()}const i=n?.method??"POST";return await g()({..._(e,t,i),...n?.signal?{signal:n.signal}:{}})}("ai/summarization",{context:e.toString(),content:t}).then(e=>{if(e&&"string"==typeof e)return e;throw new Error((0,i.__)("Invalid response from API.","ai"))}).catch(e=>{throw new Error(e.message)})}(n,a);s({meta:{...r,wpai_generated_summary:o}});const c=z(e);if(c){const e=v(o);(0,d.dispatch)(m.store).replaceInnerBlocks(c.clientId,e,!1)}else{const e=function(e){const n=v(e);return(0,t.createBlock)("core/group",{className:"ai-summarization-summary",aiGeneratedSummary:!0},n)}(o);(0,d.dispatch)(m.store).insertBlock(e,0)}}catch(e){const t="string"==typeof e?e:e?.message??(0,i.__)("Failed to generate summary.","ai");(0,d.dispatch)(p.store).createErrorNotice(t,{id:B,isDismissible:!0})}finally{I(!1)}}},isContentTooShort:!x(a||"",T().minContentLength),minContentLength:T().minContentLength}}const{aiSummarizationData:A}=window;window.React;const{aiSummarizationData:D}=window,F=()=>{const{isSummarizing:e,hasSummary:t,handleSummarize:n,isContentTooShort:a}=G();let o=(0,i.__)("Generate Summary","ai");return e?o=(0,i.__)("Generating…","ai"):t&&(o=(0,i.__)("Regenerate Summary","ai")),D?.enabled?(0,c.jsx)(m.BlockControls,{children:(0,c.jsx)(r.ToolbarGroup,{children:(0,c.jsx)(r.ToolbarButton,{label:o,icon:l,className:"ai-summarization-block-controls-button",onClick:n,disabled:e||a,isBusy:e})})}):null},N=(0,u.createHigherOrderComponent)(e=>t=>{const{name:n,isSelected:i,attributes:{aiGeneratedSummary:a=!1}}=t;return"core/group"===n&&a?(0,c.jsxs)(c.Fragment,{children:[i&&(0,c.jsx)(F,{...t}),(0,c.jsx)(e,{...t})]}):(0,c.jsx)(e,{...t})},"addBlockControls");(0,a.registerPlugin)("classifai-plugin-summarization",{render:function e(){const{isSummarizing:t,hasSummary:n,handleSummarize:a,isContentTooShort:s,minContentLength:m}=G(),d=(0,u.useInstanceId)(e,"ai-summarization-plugin-description");let w=(0,i.__)("Generate Summary","ai");t?w=(0,i.__)("Generating…","ai"):n&&(w=(0,i.__)("Regenerate Summary","ai"));const p=t||s;let h;return h=s?(0,i.sprintf)(/* translators: %d: minimum number of characters required. */ /* translators: %d: minimum number of characters required. */ |
| 2 |
(0,i.__)("Summarization will be available when the post content has at least %d characters.","ai"),m):n?(0,i.__)("This will update the generated summary block with a new summary of the content of this post.","ai"):(0,i.__)("This will create a block that is a summary of the content of this post.","ai"),A?.enabled?(0,c.jsx)(o.PluginPostStatusInfo,{children:(0,c.jsxs)(r.Flex,{direction:"column",className:"ai-summarization-plugin-container",gap:2,children:[(0,c.jsx)(r.FlexItem,{children:(0,c.jsx)(r.Button,{accessibleWhenDisabled:!0,className:"ai-summarization-plugin-button",variant:"secondary",label:w,icon:l,onClick:a,disabled:p,isBusy:t,"aria-describedby":d,__next40pxDefaultSize:!0,children:w})}),(0,c.jsx)(r.FlexItem,{children:(0,c.jsx)("span",{id:d,className:"description",children:h})})]})}):null}}),(0,n.addFilter)("blocks.registerBlockType","ai/summarization-attribute",(e,t)=>"core/group"!==t?e:{...e,attributes:{...e.attributes,aiGeneratedSummary:{type:"boolean",default:!1}}}),(0,t.registerBlockVariation)("core/group",{name:"ai-summarization-summary",title:(0,i.__)("Content Summary","ai"),description:(0,i.__)("A generated summary of the post content.","ai"),attributes:{className:"ai-summarization-summary",aiGeneratedSummary:!0},scope:["block"],isActive:["aiGeneratedSummary"]}),(0,n.addFilter)("editor.BlockEdit","ai/summarization-block-controls",N)})(); |