@imgly/plugin-ai-text-generation-web 0.1.0-rc.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -4
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,10 +88,68 @@ Key features:
|
|
|
88
88
|
|
|
89
89
|
The plugin accepts the following configuration options:
|
|
90
90
|
|
|
91
|
-
| Option
|
|
92
|
-
|
|
|
93
|
-
| `provider`
|
|
94
|
-
| `debug`
|
|
91
|
+
| Option | Type | Description | Default |
|
|
92
|
+
| ------------ | ---------- | ----------------------------------------------- | -------- |
|
|
93
|
+
| `provider` | Provider | Provider for text generation and transformation | required |
|
|
94
|
+
| `debug` | boolean | Enable debug logging | false |
|
|
95
|
+
| `middleware` | Function[] | Array of middleware functions for the generation| undefined|
|
|
96
|
+
|
|
97
|
+
### Middleware Configuration
|
|
98
|
+
|
|
99
|
+
The `middleware` option allows you to add pre-processing and post-processing capabilities to the generation process:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import TextGeneration from '@imgly/plugin-ai-text-generation-web';
|
|
103
|
+
import Anthropic from '@imgly/plugin-ai-text-generation-web/anthropic';
|
|
104
|
+
import { loggingMiddleware, rateLimitMiddleware } from '@imgly/plugin-ai-generation-web';
|
|
105
|
+
|
|
106
|
+
// Create middleware functions
|
|
107
|
+
const logging = loggingMiddleware();
|
|
108
|
+
const rateLimit = rateLimitMiddleware({
|
|
109
|
+
maxRequests: 20,
|
|
110
|
+
timeWindowMs: 60000, // 1 minute
|
|
111
|
+
onRateLimitExceeded: (input, options, info) => {
|
|
112
|
+
console.log(`Text generation rate limit exceeded: ${info.currentCount}/${info.maxRequests}`);
|
|
113
|
+
return false; // Reject request
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Create custom middleware
|
|
118
|
+
const customMiddleware = async (input, options, next) => {
|
|
119
|
+
console.log('Before generation:', input);
|
|
120
|
+
|
|
121
|
+
// Add custom fields or modify the input
|
|
122
|
+
const modifiedInput = {
|
|
123
|
+
...input,
|
|
124
|
+
customField: 'custom value'
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// Call the next middleware or generation function
|
|
128
|
+
const result = await next(modifiedInput, options);
|
|
129
|
+
|
|
130
|
+
console.log('After generation:', result);
|
|
131
|
+
|
|
132
|
+
// You can also modify the result before returning it
|
|
133
|
+
return result;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Apply middleware to plugin
|
|
137
|
+
cesdk.addPlugin(
|
|
138
|
+
TextGeneration({
|
|
139
|
+
provider: Anthropic.AnthropicProvider({
|
|
140
|
+
proxyUrl: 'https://your-anthropic-proxy.example.com'
|
|
141
|
+
}),
|
|
142
|
+
middleware: [logging, rateLimit, customMiddleware] // Apply middleware in order
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Built-in middleware options:
|
|
148
|
+
|
|
149
|
+
- **loggingMiddleware**: Logs generation requests and responses
|
|
150
|
+
- **rateLimitMiddleware**: Limits the number of generation requests in a time window
|
|
151
|
+
|
|
152
|
+
You can also create custom middleware functions to meet your specific needs.
|
|
95
153
|
|
|
96
154
|
### Using a Proxy
|
|
97
155
|
|
package/dist/index.mjs
CHANGED
|
@@ -136,7 +136,7 @@ var ce="@imgly/plugin-ai-text-generation",tt=`
|
|
|
136
136
|
|
|
137
137
|
</symbol>
|
|
138
138
|
</svg>
|
|
139
|
-
`,Gn=Bn,Ie="ly.img.ai.quickAction.order",_="ly.img.ai.quickAction.actions";function Fn(e,t){return{id:t,setQuickActionMenuOrder:i=>{e.ui.experimental.setGlobalStateValue(`${Ie}.${t}`,i)},getQuickActionMenuOrder:()=>e.ui.experimental.getGlobalStateValue(`${Ie}.${t}`,[]),registerQuickAction:i=>{if(!e.ui.experimental.hasGlobalStateValue(`${_}.${t}`))e.ui.experimental.setGlobalStateValue(`${_}.${t}`,{[i.id]:i});else{let r=e.ui.experimental.getGlobalStateValue(`${_}.${t}`,{});e.ui.experimental.setGlobalStateValue(`${_}.${t}`,{...r,[i.id]:i})}},getQuickAction:i=>e.ui.experimental.getGlobalStateValue(`${_}.${t}`,{})[i]}}var le=Fn,X="ly.img.ai.inference.editMode",We="ly.img.ai.inference.metadata";function Je(e){return`ly.img.ai.quickAction.${e.quickActionMenuId}.${e.quickActionId}`}function Zn(e){if(e.length===0)return[];let t=[...e];for(;t.length>0&&t[0]==="ly.img.separator";)t.shift();for(;t.length>0&&t[t.length-1]==="ly.img.separator";)t.pop();return t.reduce((i,r)=>(r==="ly.img.separator"&&i.length>0&&i[i.length-1]==="ly.img.separator"||i.push(r),i),[])}function qn({alwaysOnTop:e=!0,disableClipping:t=!0}){return async(i,r,a)=>{let o={},n={},l=e||t?r.blockIds??r.engine.block.findAllSelected():[];return l.forEach(c=>{if(r.engine.block.isValid(c)&&e&&(o[c]=r.engine.block.isAlwaysOnTop(c),r.engine.block.setAlwaysOnTop(c,!0)),t){let s=r.engine.block.getParent(c);s!=null&&r.engine.block.getType(s)!=="//ly.img.ubq/scene"&&(n[s]=r.engine.block.isClipped(s),r.engine.block.setClipped(s,!1))}}),r.addDisposer(async()=>{l.forEach(c=>{if(r.engine.block.isValid(c)&&e&&r.engine.block.setAlwaysOnTop(c,o[c]),t){let s=r.engine.block.getParent(c);s!=null&&r.engine.block.getType(s)!=="//ly.img.ubq/scene"&&n[s]!=null&&r.engine.block.setClipped(s,n[s])}})}),await a(i,r)}}var Hn=qn;function Qn({pending:e=!0}){return async(t,i,r)=>{let a=e?i.blockIds??i.engine.block.findAllSelected():[];try{return a.forEach(o=>{i.engine.block.isValid(o)&&i.engine.block.setState(o,{type:"Pending",progress:0})}),await r(t,i)}finally{a.forEach(o=>{i.engine.block.isValid(o)&&i.engine.block.setState(o,{type:"Ready"})})}}}var Rn=Qn;function Yn({editMode:e,automaticallyUnlock:t=!1}){return async(i,r,a)=>{let o=r.blockIds??r.engine.block.findAllSelected(),n=()=>{};t||r.addDisposer(async()=>{n?.()});try{return n=Wn(r.engine,o,e),await a(i,r)}catch(l){throw A(l)&&n(),l}finally{t&&n()}}}function Wn(e,t,i){let r=e.editor.getGlobalScope("editor/select"),a=e.editor.getEditMode();l(),e.editor.setGlobalScope("editor/select","Deny"),e.editor.setEditMode(i);let o=e.editor.createHistory(),n=e.editor.getActiveHistory();e.editor.setActiveHistory(o);function l(){e.block.findAllSelected().forEach(u=>{t.includes(u)||e.block.setSelected(u,!1)}),t.forEach(u=>{e.block.setSelected(u,!0)})}let c=e.editor.onStateChanged(()=>{e.editor.getEditMode()!==i&&e.editor.setEditMode(i)}),s=e.block.onSelectionChanged(l);return()=>{r!=null&&e.editor.setGlobalScope("editor/select",r),e.editor.setEditMode(a),e.editor.setActiveHistory(n),e.editor.destroyHistory(o),s(),c()}}var Jn=Yn;function Kn(e,t){switch(t.kind){case"text":return Xn(e,t);case"image":return eo(e,t);case"video":return to(e,t);case"audio":return ro(e,t);default:throw new Error(`Unsupported output kind for quick actions: ${t.kind}`)}}async function Xn(e,t){let{cesdk:i,blockIds:r,abortSignal:a}=t,o=r.map(s=>i.engine.block.getString(s,"text/text")),n;if(oe(e)){let s="";for await(let u of e){if(a.aborted)break;typeof u=="string"?s=u:u.kind==="text"&&(s=u.text),r.forEach(p=>{i.engine.block.setString(p,"text/text",s)}),n={kind:"text",text:s}}}else n=e;if(n==null||n.kind!=="text")throw new Error("Output kind from generation is not text");let l=()=>{t.blockIds.forEach(s=>{t.cesdk.engine.block.setString(s,"text/text",n.text)})},c=()=>{t.blockIds.forEach((s,u)=>{t.cesdk.engine.block.setString(s,"text/text",o[u])})};return{consumedGenerationResult:n,applyCallbacks:{onBefore:c,onAfter:l,onCancel:c,onApply:l}}}async function eo(e,t){let{cesdk:i,blockIds:r,abortSignal:a}=t;if(r.length!==1)throw new Error("Only one block is supported for image generation");let[o]=r,n=i.engine.block.getFill(o),l=i.engine.block.getSourceSet(n,"fill/image/sourceSet"),[c]=l,s=await i.engine.editor.getMimeType(c.uri);if(a.throwIfAborted(),s==="image/svg+xml")throw new Error("SVG images are not supported");let u=i.engine.block.getCropScaleX(o),p=i.engine.block.getCropScaleY(o),g=i.engine.block.getCropTranslationX(o),m=i.engine.block.getCropTranslationY(o),C=i.engine.block.getCropRotation(o),v=()=>{i.engine.block.setCropScaleX(o,u),i.engine.block.setCropScaleY(o,p),i.engine.block.setCropTranslationX(o,g),i.engine.block.setCropTranslationY(o,m),i.engine.block.setCropRotation(o,C)};if(oe(e))throw new Error("Streaming generation is not supported yet from a panel");if(e.kind!=="image"||typeof e.url!="string")throw new Error("Output kind from generation is not an image");let b=e.url,d=await i.engine.editor.getMimeType(b),y=[{uri:await io(i,b,d),width:c.width,height:c.height}];i.engine.block.setSourceSet(n,"fill/image/sourceSet",y),v();let h=()=>{i.engine.block.setSourceSet(n,"fill/image/sourceSet",l),v()},f=()=>{i.engine.block.setSourceSet(n,"fill/image/sourceSet",y),v()};return{consumedGenerationResult:e,applyCallbacks:{onBefore:h,onAfter:f,onCancel:()=>{h()},onApply:()=>{f()}}}}async function to(e,t){throw new Error("Function not implemented.")}async function ro(e,t){throw new Error("Function not implemented.")}async function io(e,t,i){let r=await(await fetch(t)).blob(),a=new File([r],`image.${no(i)}`,{type:i}),o=await e.unstable_upload(a,()=>{});return o?.meta?.uri??(console.warn("Failed to upload image:",o),t)}function no(e){return{"image/png":"png","image/jpeg":"jpg","image/webp":"webp","image/gif":"gif","image/svg+xml":"svg"}[e]??"png"}var oo=Kn;function ao({editMode:e,automaticallyUnlock:t=!1,showNotification:i=!0}){return async(r,a,o)=>{let n=a.blockIds??a.engine.block.findAllSelected(),l=()=>{};t||a.addDisposer(async()=>{l?.()});try{l=lo(a.engine,n,e);let c=await o(r,a);if(i){let s=a.engine.block.findAllSelected();n.some(u=>s.includes(u))||a.cesdk?.ui.showNotification({type:"success",message:"AI generation complete",action:{label:"Select",onClick:()=>{n.forEach(u=>{a.engine.block.select(u)})}}})}return c}catch(c){throw A(c)&&l(),c}finally{t&&l()}}}function lo(e,t,i){function r(){let n=e.block.findAllSelected();return t.some(l=>n.includes(l))}let a=e.editor.onStateChanged(()=>{e.editor.getEditMode()!==i&&r()&&e.editor.setEditMode(i)}),o=e.block.onSelectionChanged(()=>{r()?e.editor.setEditMode(i):e.editor.setEditMode("Transform")});return r()&&e.editor.setEditMode(i),()=>{o(),a(),e.editor.setEditMode("Transform")}}var so=ao;function co(e,t){let{cesdk:i,quickActionMenu:r,provider:a}=e,o=`ly.img.ai.${r.id}`,n=`${o}.confirmation`;i.setTranslations({en:{[`${n}.apply`]:"Apply",[`${n}.before`]:"Before",[`${n}.after`]:"After",[`${o}.cancel`]:"Cancel",[`${o}.processing`]:"Generating..."}});let l=`${n}.canvasnMenu`,c={unlock:()=>{},abort:()=>{},applyCallbacks:void 0},s=()=>{let p=new AbortController,g=p.signal;return c.abort=()=>{p.abort()},g};i.ui.registerComponent(l,({builder:p,engine:g,state:m})=>{let C=g.block.findAllSelected();if(C.length===0)return null;let v=new Ge(i.engine,We),b=v.get(C[0]);if(b==null)return null;let d=()=>{C.forEach(y=>{v.clear(y)})};switch(b.status){case"processing":{p.Button(`${o}.spinner`,{label:[`ly.img.ai.inference.${b.quickActionId}.processing`,`${o}.cancel`],isLoading:!0}),p.Separator(`${o}.separator`),p.Button(`${o}.cancel`,{icon:"@imgly/Cross",tooltip:`${o}.cancel`,onClick:()=>{c.abort(),d()}});break}case"confirmation":{let y=m(`${n}.comparing`,"after"),h=c.applyCallbacks?.onCancel;h!=null&&p.Button(`${n}.cancel`,{icon:"@imgly/Cross",tooltip:`${o}.cancel`,onClick:()=>{c.unlock(),h(),d()}});let f=c.applyCallbacks?.onBefore,M=c.applyCallbacks?.onAfter;f!=null&&M!=null&&p.ButtonGroup(`${n}.compare`,{children:()=>{p.Button(`${n}.compare.before`,{label:`${n}.before`,variant:"regular",isActive:y.value==="before",onClick:()=>{f(),y.setValue("before")}}),p.Button(`${n}.compare.after`,{label:`${n}.after`,variant:"regular",isActive:y.value==="after",onClick:()=>{M(),y.setValue("after")}})}});let k=c.applyCallbacks?.onApply;k!=null&&p.Button(`${n}.apply`,{icon:"@imgly/Checkmark",tooltip:`${n}.apply`,color:"accent",isDisabled:y.value!=="after",onClick:()=>{c.unlock(),d(),i.engine.editor._update(),k(),g.editor.addUndoStep()}});break}default:}});let u=`${o}.canvasMenu`;return i.ui.registerComponent(u,p=>{let g=p.engine.block.findAllSelected(),m=r.getQuickActionMenuOrder().map(y=>{if(y==="ly.img.separator")return y;let h=r.getQuickAction(y);if(h==null||!i.feature.isEnabled(Je({quickActionId:y,quickActionMenuId:r.id}),{engine:p.engine}))return null;let f=h.scopes;return f!=null&&f.length>0&&!g.every(M=>f.every(k=>p.engine.block.isAllowedByScope(M,k)))?null:h}).filter(y=>y!=null);if(m=Zn(m),m.length===0||m.every(y=>y==="ly.img.separator"))return null;let{builder:C,experimental:v,state:b}=p,d=b(`${o}.toggleExpandedState`,void 0);v.builder.Popover(`${o}.popover`,{icon:"@imgly/Sparkle",variant:"plain",trailingIcon:null,children:({close:y})=>{C.Section(`${o}.popover.section`,{children:()=>{if(d.value!=null){let h=d.value,f=r.getQuickAction(h);if(f!=null&&f.renderExpanded!=null){f.renderExpanded(p,{blockIds:g,closeMenu:y,toggleExpand:()=>{d.setValue(void 0)},handleGenerationError:M=>{P(M,{cesdk:i,provider:a},t)},generate:async(M,k)=>{try{let{returnValue:x,applyCallbacks:R,dispose:et}=await Ee({input:M,quickAction:f,quickActionMenu:r,provider:a,cesdk:i,abortSignal:s(),blockIds:k?.blockIds??g,confirmationComponentId:l},t);return c.unlock=et,c.applyCallbacks=R,x}catch(x){throw A(x)||P(x,{cesdk:i,provider:a},t),x}}});return}}v.builder.Menu(`${o}.menu`,{children:()=>{m.forEach(h=>{h==="ly.img.separator"?C.Separator(`${o}.separator.${Math.random().toString()}`):h.render(p,{blockIds:g,closeMenu:y,handleGenerationError:f=>{P(f,{cesdk:i,provider:a},t)},toggleExpand:()=>{d.setValue(h.id)},generate:async(f,M)=>{try{let{returnValue:k,applyCallbacks:x,dispose:R}=await Ee({input:f,quickAction:h,quickActionMenu:r,provider:a,cesdk:i,abortSignal:s(),blockIds:M?.blockIds??g,confirmationComponentId:l},t);return c.unlock=R,c.applyCallbacks=x,k}catch(k){throw A(k)||P(k,{cesdk:i,provider:a},t),k}}})})}})}})}})}),{canvasMenuComponentId:u}}async function Ee(e,t){let{cesdk:i,input:r,blockIds:a,provider:o,quickAction:n,confirmationComponentId:l,abortSignal:c}=e;n.confirmation&&i.ui.setCanvasMenuOrder([l],{editMode:X});let s=new Ge(i.engine,We);a.forEach(d=>{s.set(d,{status:"processing",quickActionId:n.id})});let u={cesdk:i,engine:i.engine,abortSignal:c},p=Pe([...o.output.middleware??[],t.debug?Ue():void 0,Rn({}),...n.confirmation?[n.lockDuringConfirmation?Jn({editMode:X}):so({editMode:X}),n.confirmation&&Hn({})]:[]]),{result:g,dispose:m}=await p(o.output.generate)(r,u),{consumedGenerationResult:C,applyCallbacks:v}=await oo(g,{abortSignal:c,kind:o.kind,blockIds:a,cesdk:i});n.confirmation?a.forEach(d=>{s.set(d,{status:"confirmation",quickActionId:n.id})}):a.forEach(d=>{s.clear(d)});let b=()=>{m(),a.forEach(d=>{s.clear(d)})};return c.addEventListener("abort",b),{dispose:b,returnValue:C,applyCallbacks:v}}var uo=co;async function po(e,t,i){await e.initialize(t);let r=await mo(t.engine,e.id,e.output.history??"@imgly/local");if(t.cesdk==null)return{};let a=r?`${e.id}.history.entry`:void 0,o={...t,cesdk:t.cesdk,historyAssetSourceId:r,historyAssetLibraryEntryId:a,i18n:{prompt:"common.ai-generation.prompt.placeholder"}};a!=null&&r!=null&&t.cesdk.ui.addAssetLibraryEntry({id:a,sourceIds:[r],sortBy:{sortKey:"insertedAt",sortingOrder:"Descending"},canRemove:!0,gridItemHeight:"square",gridBackgroundType:"cover"}),t.cesdk.i18n.setTranslations({en:{"common.ai-generation.success":"Generation Successful","common.ai-generation.failed":"Generation Failed","common.ai-generation.generate":"Generate","common.ai-generation.prompt.placeholder":"Describe what you want to create...",[`panel.${e.id}`]:go(e)}});let n="@imgly/plugin-ai-generation.iconSetAdded";return t.cesdk.ui.experimental.hasGlobalStateValue(n)||(t.cesdk.ui.addIconSet("@imgly/plugin-ai-generation",Gn),t.cesdk.ui.experimental.setGlobalStateValue(n,!0)),{renderBuilderFunctions:await yo(e,o,i)}}function go(e){if(e.name!=null)return e.name;switch(e.kind){case"image":return"Generate Image";case"video":return"Generate Video";case"audio":return"Generate Audio";default:return"Generate Asset"}}async function mo(e,t,i){if(!(i==null||i===!1)){if(i==="@imgly/local"){let r=`${t}.history`;return e.asset.addLocalSource(r),r}if(i==="@imgly/indexedDB"){let r=`${t}.history`;return e.asset.addSource(new Gt(r,e)),r}return i}}async function yo(e,t,i){let r={panel:void 0};return e.input?.panel!=null&&(r.panel=await fo(e,e.input.panel,t,i)),e.input?.quickActions!=null&&(i.debug&&console.log(`Initializing quick actions for provider '${e.kind}' (${e.id})`),ho(e,e.input.quickActions,t,i)),r}async function fo(e,t,i,r){switch(t.type){case"custom":return Bt(e,t,i,r);case"schema":return Ut(e,t,i,r);default:r.debug&&console.warn(`Invalid panel input type '${t.type}' - skipping`)}}async function ho(e,t,i,r){let{cesdk:a}=i,o=e.kind,n=le(a,o);t.actions.forEach(c=>{let s=c.enable??!0;a.feature.enable(Je({quickActionId:c.id,quickActionMenuId:o}),s),n.registerQuickAction(c),r.debug&&console.log(`Registered quick action: '${c.id}' (v${c.version}) to menu '${o}'`)});let{canvasMenuComponentId:l}=uo({cesdk:a,quickActionMenu:n,provider:e},r);r.debug&&console.log(`Registered quick action menu component: ${l}`)}var Ke=po;var se="@imgly/plugin-ai-text-generation-web";var Xe=e=>({async initialize({cesdk:t}){if(t==null)return;let i=await e.provider?.({cesdk:t});Ke(i,{cesdk:t,engine:t.engine},{debug:!0,dryRun:!1});let r=le(t,"text");r.setQuickActionMenuOrder(["improve","fix","shorter","longer","ly.img.separator","changeTone","translate","ly.img.separator","changeTextTo",...r.getQuickActionMenuOrder()]),t.ui.addIconSet(ce,ue),t.setTranslations({en:{"ly.img.ai.inference.apply":"Apply","ly.img.ai.inference.cancel":"Cancel","ly.img.ai.inference.improve":"Improve Writing","ly.img.ai.inference.improve.processing":"Improving Writing...","ly.img.ai.inference.fix":"Fix Spelling & Grammar","ly.img.ai.inference.fix.processing":"Fixing spelling & grammar...","ly.img.ai.inference.longer":"Make Longer","ly.img.ai.inference.longer.processing":"Making longer...","ly.img.ai.inference.shorter":"Make Shorter","ly.img.ai.inference.shorter.processing":"Making shorter...","ly.img.ai.inference.changeTone":"Change Tone","ly.img.ai.inference.changeTone.processing":"Changing tone...","ly.img.ai.inference.changeTone.type.professional":"Professional","ly.img.ai.inference.changeTone.type.casual":"Casual","ly.img.ai.inference.changeTone.type.friendly":"Friendly","ly.img.ai.inference.changeTone.type.serious":"Serious","ly.img.ai.inference.changeTone.type.humorous":"Humorous","ly.img.ai.inference.changeTone.type.optimistic":"Optimistic","ly.img.ai.inference.translate":"Translate","ly.img.ai.inference.translate.processing":"Translating...","ly.img.ai.inference.changeTextTo":"Change Text to...","ly.img.ai.inference.changeTextTo.processing":"Changing text..."}})}});var bo=e=>({name:se,version:"0.1.0-rc.2",...Xe(e)}),qo=bo;export{qo as default};
|
|
139
|
+
`,Gn=Bn,Ie="ly.img.ai.quickAction.order",_="ly.img.ai.quickAction.actions";function Fn(e,t){return{id:t,setQuickActionMenuOrder:i=>{e.ui.experimental.setGlobalStateValue(`${Ie}.${t}`,i)},getQuickActionMenuOrder:()=>e.ui.experimental.getGlobalStateValue(`${Ie}.${t}`,[]),registerQuickAction:i=>{if(!e.ui.experimental.hasGlobalStateValue(`${_}.${t}`))e.ui.experimental.setGlobalStateValue(`${_}.${t}`,{[i.id]:i});else{let r=e.ui.experimental.getGlobalStateValue(`${_}.${t}`,{});e.ui.experimental.setGlobalStateValue(`${_}.${t}`,{...r,[i.id]:i})}},getQuickAction:i=>e.ui.experimental.getGlobalStateValue(`${_}.${t}`,{})[i]}}var le=Fn,X="ly.img.ai.inference.editMode",We="ly.img.ai.inference.metadata";function Je(e){return`ly.img.ai.quickAction.${e.quickActionMenuId}.${e.quickActionId}`}function Zn(e){if(e.length===0)return[];let t=[...e];for(;t.length>0&&t[0]==="ly.img.separator";)t.shift();for(;t.length>0&&t[t.length-1]==="ly.img.separator";)t.pop();return t.reduce((i,r)=>(r==="ly.img.separator"&&i.length>0&&i[i.length-1]==="ly.img.separator"||i.push(r),i),[])}function qn({alwaysOnTop:e=!0,disableClipping:t=!0}){return async(i,r,a)=>{let o={},n={},l=e||t?r.blockIds??r.engine.block.findAllSelected():[];return l.forEach(c=>{if(r.engine.block.isValid(c)&&e&&(o[c]=r.engine.block.isAlwaysOnTop(c),r.engine.block.setAlwaysOnTop(c,!0)),t){let s=r.engine.block.getParent(c);s!=null&&r.engine.block.getType(s)!=="//ly.img.ubq/scene"&&(n[s]=r.engine.block.isClipped(s),r.engine.block.setClipped(s,!1))}}),r.addDisposer(async()=>{l.forEach(c=>{if(r.engine.block.isValid(c)&&e&&r.engine.block.setAlwaysOnTop(c,o[c]),t){let s=r.engine.block.getParent(c);s!=null&&r.engine.block.getType(s)!=="//ly.img.ubq/scene"&&n[s]!=null&&r.engine.block.setClipped(s,n[s])}})}),await a(i,r)}}var Hn=qn;function Qn({pending:e=!0}){return async(t,i,r)=>{let a=e?i.blockIds??i.engine.block.findAllSelected():[];try{return a.forEach(o=>{i.engine.block.isValid(o)&&i.engine.block.setState(o,{type:"Pending",progress:0})}),await r(t,i)}finally{a.forEach(o=>{i.engine.block.isValid(o)&&i.engine.block.setState(o,{type:"Ready"})})}}}var Rn=Qn;function Yn({editMode:e,automaticallyUnlock:t=!1}){return async(i,r,a)=>{let o=r.blockIds??r.engine.block.findAllSelected(),n=()=>{};t||r.addDisposer(async()=>{n?.()});try{return n=Wn(r.engine,o,e),await a(i,r)}catch(l){throw A(l)&&n(),l}finally{t&&n()}}}function Wn(e,t,i){let r=e.editor.getGlobalScope("editor/select"),a=e.editor.getEditMode();l(),e.editor.setGlobalScope("editor/select","Deny"),e.editor.setEditMode(i);let o=e.editor.createHistory(),n=e.editor.getActiveHistory();e.editor.setActiveHistory(o);function l(){e.block.findAllSelected().forEach(u=>{t.includes(u)||e.block.setSelected(u,!1)}),t.forEach(u=>{e.block.setSelected(u,!0)})}let c=e.editor.onStateChanged(()=>{e.editor.getEditMode()!==i&&e.editor.setEditMode(i)}),s=e.block.onSelectionChanged(l);return()=>{r!=null&&e.editor.setGlobalScope("editor/select",r),e.editor.setEditMode(a),e.editor.setActiveHistory(n),e.editor.destroyHistory(o),s(),c()}}var Jn=Yn;function Kn(e,t){switch(t.kind){case"text":return Xn(e,t);case"image":return eo(e,t);case"video":return to(e,t);case"audio":return ro(e,t);default:throw new Error(`Unsupported output kind for quick actions: ${t.kind}`)}}async function Xn(e,t){let{cesdk:i,blockIds:r,abortSignal:a}=t,o=r.map(s=>i.engine.block.getString(s,"text/text")),n;if(oe(e)){let s="";for await(let u of e){if(a.aborted)break;typeof u=="string"?s=u:u.kind==="text"&&(s=u.text),r.forEach(p=>{i.engine.block.setString(p,"text/text",s)}),n={kind:"text",text:s}}}else n=e;if(n==null||n.kind!=="text")throw new Error("Output kind from generation is not text");let l=()=>{t.blockIds.forEach(s=>{t.cesdk.engine.block.setString(s,"text/text",n.text)})},c=()=>{t.blockIds.forEach((s,u)=>{t.cesdk.engine.block.setString(s,"text/text",o[u])})};return{consumedGenerationResult:n,applyCallbacks:{onBefore:c,onAfter:l,onCancel:c,onApply:l}}}async function eo(e,t){let{cesdk:i,blockIds:r,abortSignal:a}=t;if(r.length!==1)throw new Error("Only one block is supported for image generation");let[o]=r,n=i.engine.block.getFill(o),l=i.engine.block.getSourceSet(n,"fill/image/sourceSet"),[c]=l,s=await i.engine.editor.getMimeType(c.uri);if(a.throwIfAborted(),s==="image/svg+xml")throw new Error("SVG images are not supported");let u=i.engine.block.getCropScaleX(o),p=i.engine.block.getCropScaleY(o),g=i.engine.block.getCropTranslationX(o),m=i.engine.block.getCropTranslationY(o),C=i.engine.block.getCropRotation(o),v=()=>{i.engine.block.setCropScaleX(o,u),i.engine.block.setCropScaleY(o,p),i.engine.block.setCropTranslationX(o,g),i.engine.block.setCropTranslationY(o,m),i.engine.block.setCropRotation(o,C)};if(oe(e))throw new Error("Streaming generation is not supported yet from a panel");if(e.kind!=="image"||typeof e.url!="string")throw new Error("Output kind from generation is not an image");let b=e.url,d=await i.engine.editor.getMimeType(b),y=[{uri:await io(i,b,d),width:c.width,height:c.height}];i.engine.block.setSourceSet(n,"fill/image/sourceSet",y),v();let h=()=>{i.engine.block.setSourceSet(n,"fill/image/sourceSet",l),v()},f=()=>{i.engine.block.setSourceSet(n,"fill/image/sourceSet",y),v()};return{consumedGenerationResult:e,applyCallbacks:{onBefore:h,onAfter:f,onCancel:()=>{h()},onApply:()=>{f()}}}}async function to(e,t){throw new Error("Function not implemented.")}async function ro(e,t){throw new Error("Function not implemented.")}async function io(e,t,i){let r=await(await fetch(t)).blob(),a=new File([r],`image.${no(i)}`,{type:i}),o=await e.unstable_upload(a,()=>{});return o?.meta?.uri??(console.warn("Failed to upload image:",o),t)}function no(e){return{"image/png":"png","image/jpeg":"jpg","image/webp":"webp","image/gif":"gif","image/svg+xml":"svg"}[e]??"png"}var oo=Kn;function ao({editMode:e,automaticallyUnlock:t=!1,showNotification:i=!0}){return async(r,a,o)=>{let n=a.blockIds??a.engine.block.findAllSelected(),l=()=>{};t||a.addDisposer(async()=>{l?.()});try{l=lo(a.engine,n,e);let c=await o(r,a);if(i){let s=a.engine.block.findAllSelected();n.some(u=>s.includes(u))||a.cesdk?.ui.showNotification({type:"success",message:"AI generation complete",action:{label:"Select",onClick:()=>{n.forEach(u=>{a.engine.block.select(u)})}}})}return c}catch(c){throw A(c)&&l(),c}finally{t&&l()}}}function lo(e,t,i){function r(){let n=e.block.findAllSelected();return t.some(l=>n.includes(l))}let a=e.editor.onStateChanged(()=>{e.editor.getEditMode()!==i&&r()&&e.editor.setEditMode(i)}),o=e.block.onSelectionChanged(()=>{r()?e.editor.setEditMode(i):e.editor.setEditMode("Transform")});return r()&&e.editor.setEditMode(i),()=>{o(),a(),e.editor.setEditMode("Transform")}}var so=ao;function co(e,t){let{cesdk:i,quickActionMenu:r,provider:a}=e,o=`ly.img.ai.${r.id}`,n=`${o}.confirmation`;i.setTranslations({en:{[`${n}.apply`]:"Apply",[`${n}.before`]:"Before",[`${n}.after`]:"After",[`${o}.cancel`]:"Cancel",[`${o}.processing`]:"Generating..."}});let l=`${n}.canvasnMenu`,c={unlock:()=>{},abort:()=>{},applyCallbacks:void 0},s=()=>{let p=new AbortController,g=p.signal;return c.abort=()=>{p.abort()},g};i.ui.registerComponent(l,({builder:p,engine:g,state:m})=>{let C=g.block.findAllSelected();if(C.length===0)return null;let v=new Ge(i.engine,We),b=v.get(C[0]);if(b==null)return null;let d=()=>{C.forEach(y=>{v.clear(y)})};switch(b.status){case"processing":{p.Button(`${o}.spinner`,{label:[`ly.img.ai.inference.${b.quickActionId}.processing`,`${o}.cancel`],isLoading:!0}),p.Separator(`${o}.separator`),p.Button(`${o}.cancel`,{icon:"@imgly/Cross",tooltip:`${o}.cancel`,onClick:()=>{c.abort(),d()}});break}case"confirmation":{let y=m(`${n}.comparing`,"after"),h=c.applyCallbacks?.onCancel;h!=null&&p.Button(`${n}.cancel`,{icon:"@imgly/Cross",tooltip:`${o}.cancel`,onClick:()=>{c.unlock(),h(),d()}});let f=c.applyCallbacks?.onBefore,M=c.applyCallbacks?.onAfter;f!=null&&M!=null&&p.ButtonGroup(`${n}.compare`,{children:()=>{p.Button(`${n}.compare.before`,{label:`${n}.before`,variant:"regular",isActive:y.value==="before",onClick:()=>{f(),y.setValue("before")}}),p.Button(`${n}.compare.after`,{label:`${n}.after`,variant:"regular",isActive:y.value==="after",onClick:()=>{M(),y.setValue("after")}})}});let k=c.applyCallbacks?.onApply;k!=null&&p.Button(`${n}.apply`,{icon:"@imgly/Checkmark",tooltip:`${n}.apply`,color:"accent",isDisabled:y.value!=="after",onClick:()=>{c.unlock(),d(),i.engine.editor._update(),k(),g.editor.addUndoStep()}});break}default:}});let u=`${o}.canvasMenu`;return i.ui.registerComponent(u,p=>{let g=p.engine.block.findAllSelected(),m=r.getQuickActionMenuOrder().map(y=>{if(y==="ly.img.separator")return y;let h=r.getQuickAction(y);if(h==null||!i.feature.isEnabled(Je({quickActionId:y,quickActionMenuId:r.id}),{engine:p.engine}))return null;let f=h.scopes;return f!=null&&f.length>0&&!g.every(M=>f.every(k=>p.engine.block.isAllowedByScope(M,k)))?null:h}).filter(y=>y!=null);if(m=Zn(m),m.length===0||m.every(y=>y==="ly.img.separator"))return null;let{builder:C,experimental:v,state:b}=p,d=b(`${o}.toggleExpandedState`,void 0);v.builder.Popover(`${o}.popover`,{icon:"@imgly/Sparkle",variant:"plain",trailingIcon:null,children:({close:y})=>{C.Section(`${o}.popover.section`,{children:()=>{if(d.value!=null){let h=d.value,f=r.getQuickAction(h);if(f!=null&&f.renderExpanded!=null){f.renderExpanded(p,{blockIds:g,closeMenu:y,toggleExpand:()=>{d.setValue(void 0)},handleGenerationError:M=>{P(M,{cesdk:i,provider:a},t)},generate:async(M,k)=>{try{let{returnValue:x,applyCallbacks:R,dispose:et}=await Ee({input:M,quickAction:f,quickActionMenu:r,provider:a,cesdk:i,abortSignal:s(),blockIds:k?.blockIds??g,confirmationComponentId:l},t);return c.unlock=et,c.applyCallbacks=R,x}catch(x){throw A(x)||P(x,{cesdk:i,provider:a},t),x}}});return}}v.builder.Menu(`${o}.menu`,{children:()=>{m.forEach(h=>{h==="ly.img.separator"?C.Separator(`${o}.separator.${Math.random().toString()}`):h.render(p,{blockIds:g,closeMenu:y,handleGenerationError:f=>{P(f,{cesdk:i,provider:a},t)},toggleExpand:()=>{d.setValue(h.id)},generate:async(f,M)=>{try{let{returnValue:k,applyCallbacks:x,dispose:R}=await Ee({input:f,quickAction:h,quickActionMenu:r,provider:a,cesdk:i,abortSignal:s(),blockIds:M?.blockIds??g,confirmationComponentId:l},t);return c.unlock=R,c.applyCallbacks=x,k}catch(k){throw A(k)||P(k,{cesdk:i,provider:a},t),k}}})})}})}})}})}),{canvasMenuComponentId:u}}async function Ee(e,t){let{cesdk:i,input:r,blockIds:a,provider:o,quickAction:n,confirmationComponentId:l,abortSignal:c}=e;n.confirmation&&i.ui.setCanvasMenuOrder([l],{editMode:X});let s=new Ge(i.engine,We);a.forEach(d=>{s.set(d,{status:"processing",quickActionId:n.id})});let u={cesdk:i,engine:i.engine,abortSignal:c},p=Pe([...o.output.middleware??[],t.debug?Ue():void 0,Rn({}),...n.confirmation?[n.lockDuringConfirmation?Jn({editMode:X}):so({editMode:X}),n.confirmation&&Hn({})]:[]]),{result:g,dispose:m}=await p(o.output.generate)(r,u),{consumedGenerationResult:C,applyCallbacks:v}=await oo(g,{abortSignal:c,kind:o.kind,blockIds:a,cesdk:i});n.confirmation?a.forEach(d=>{s.set(d,{status:"confirmation",quickActionId:n.id})}):a.forEach(d=>{s.clear(d)});let b=()=>{m(),a.forEach(d=>{s.clear(d)})};return c.addEventListener("abort",b),{dispose:b,returnValue:C,applyCallbacks:v}}var uo=co;async function po(e,t,i){await e.initialize(t);let r=await mo(t.engine,e.id,e.output.history??"@imgly/local");if(t.cesdk==null)return{};let a=r?`${e.id}.history.entry`:void 0,o={...t,cesdk:t.cesdk,historyAssetSourceId:r,historyAssetLibraryEntryId:a,i18n:{prompt:"common.ai-generation.prompt.placeholder"}};a!=null&&r!=null&&t.cesdk.ui.addAssetLibraryEntry({id:a,sourceIds:[r],sortBy:{sortKey:"insertedAt",sortingOrder:"Descending"},canRemove:!0,gridItemHeight:"square",gridBackgroundType:"cover"}),t.cesdk.i18n.setTranslations({en:{"common.ai-generation.success":"Generation Successful","common.ai-generation.failed":"Generation Failed","common.ai-generation.generate":"Generate","common.ai-generation.prompt.placeholder":"Describe what you want to create...",[`panel.${e.id}`]:go(e)}});let n="@imgly/plugin-ai-generation.iconSetAdded";return t.cesdk.ui.experimental.hasGlobalStateValue(n)||(t.cesdk.ui.addIconSet("@imgly/plugin-ai-generation",Gn),t.cesdk.ui.experimental.setGlobalStateValue(n,!0)),{renderBuilderFunctions:await yo(e,o,i)}}function go(e){if(e.name!=null)return e.name;switch(e.kind){case"image":return"Generate Image";case"video":return"Generate Video";case"audio":return"Generate Audio";default:return"Generate Asset"}}async function mo(e,t,i){if(!(i==null||i===!1)){if(i==="@imgly/local"){let r=`${t}.history`;return e.asset.addLocalSource(r),r}if(i==="@imgly/indexedDB"){let r=`${t}.history`;return e.asset.addSource(new Gt(r,e)),r}return i}}async function yo(e,t,i){let r={panel:void 0};return e.input?.panel!=null&&(r.panel=await fo(e,e.input.panel,t,i)),e.input?.quickActions!=null&&(i.debug&&console.log(`Initializing quick actions for provider '${e.kind}' (${e.id})`),ho(e,e.input.quickActions,t,i)),r}async function fo(e,t,i,r){switch(t.type){case"custom":return Bt(e,t,i,r);case"schema":return Ut(e,t,i,r);default:r.debug&&console.warn(`Invalid panel input type '${t.type}' - skipping`)}}async function ho(e,t,i,r){let{cesdk:a}=i,o=e.kind,n=le(a,o);t.actions.forEach(c=>{let s=c.enable??!0;a.feature.enable(Je({quickActionId:c.id,quickActionMenuId:o}),s),n.registerQuickAction(c),r.debug&&console.log(`Registered quick action: '${c.id}' (v${c.version}) to menu '${o}'`)});let{canvasMenuComponentId:l}=uo({cesdk:a,quickActionMenu:n,provider:e},r);r.debug&&console.log(`Registered quick action menu component: ${l}`)}var Ke=po;var se="@imgly/plugin-ai-text-generation-web";var Xe=e=>({async initialize({cesdk:t}){if(t==null)return;let i=await e.provider?.({cesdk:t});Ke(i,{cesdk:t,engine:t.engine},{debug:!0,dryRun:!1});let r=le(t,"text");r.setQuickActionMenuOrder(["improve","fix","shorter","longer","ly.img.separator","changeTone","translate","ly.img.separator","changeTextTo",...r.getQuickActionMenuOrder()]),t.ui.addIconSet(ce,ue),t.setTranslations({en:{"ly.img.ai.inference.apply":"Apply","ly.img.ai.inference.cancel":"Cancel","ly.img.ai.inference.improve":"Improve Writing","ly.img.ai.inference.improve.processing":"Improving Writing...","ly.img.ai.inference.fix":"Fix Spelling & Grammar","ly.img.ai.inference.fix.processing":"Fixing spelling & grammar...","ly.img.ai.inference.longer":"Make Longer","ly.img.ai.inference.longer.processing":"Making longer...","ly.img.ai.inference.shorter":"Make Shorter","ly.img.ai.inference.shorter.processing":"Making shorter...","ly.img.ai.inference.changeTone":"Change Tone","ly.img.ai.inference.changeTone.processing":"Changing tone...","ly.img.ai.inference.changeTone.type.professional":"Professional","ly.img.ai.inference.changeTone.type.casual":"Casual","ly.img.ai.inference.changeTone.type.friendly":"Friendly","ly.img.ai.inference.changeTone.type.serious":"Serious","ly.img.ai.inference.changeTone.type.humorous":"Humorous","ly.img.ai.inference.changeTone.type.optimistic":"Optimistic","ly.img.ai.inference.translate":"Translate","ly.img.ai.inference.translate.processing":"Translating...","ly.img.ai.inference.changeTextTo":"Change Text to...","ly.img.ai.inference.changeTextTo.processing":"Changing text..."}})}});var bo=e=>({name:se,version:"0.1.0",...Xe(e)}),qo=bo;export{qo as default};
|
|
140
140
|
/*! Bundled license information:
|
|
141
141
|
|
|
142
142
|
@imgly/plugin-ai-generation-web/dist/index.mjs:
|