UNPKG

jodit-pro

Version:

PRO Version of Jodit Editor

4 lines (2 loc) 15.3 kB
var j=Object.defineProperty;var f=(m,t)=>j(m,"name",{value:t,configurable:!0});var u,h=function(m,t,e,a){var s=arguments.length,i=s<3?t:a===null?a=Object.getOwnPropertyDescriptor(t,e):a,n;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(m,t,e,a);else for(var o=m.length-1;o>=0;o--)(n=m[o])&&(i=(s<3?n(i):s>3?n(t,e,i):n(t,e))||i);return s>3&&i&&Object.defineProperty(t,e,i),i};import"./config.js";import{APIClient as x}from"./helpers/api-client.js";import{PermissionManager as A}from"./helpers/permission-manager.js";import{StateManager as T}from"./helpers/state-manager.js";import{createStorageProvider as D,WebStorageConversationStorage as I}from"./helpers/storage/index.js";import{ToolRegistry as S}from"./helpers/tool-registry.js";import{UIMainPanel as b}from"./ui/main-panel/index.js";import{applyTheme as R,areAllToolCallsExecuted as E,createNewConversation as M,createUserMessage as _,deleteConversationWithCleanup as O,executeToolCall as k,findParentMessageId as F,getCurrentSelectionContext as v,handleBackToList as W,handleConversationSearch as z,handleConversationSelect as H,handleToolApproval as q,handleToolDenial as L,invertMode as Q,loadInitialData as V,processArtifacts as $,processFinalResponse as N,processStreamingResponse as U,resizeInPanelView as y,updateToolCallInMessage as B}from"./core/index.js";import*as G from"./langs/index.js";import{IS_PROD as J}from"jodit/esm/core/constants.js";import{cache as c,cached as g,component as X,debounce as Y,watch as d}from"jodit/esm/core/decorators/index.js";import{extendLang as Z}from"jodit/esm/core/global.js";import{isAbortError as C,isString as K}from"jodit/esm/core/helpers/index.js";import{Plugin as tt}from"jodit/esm/core/plugin/index.js";import{JoditPro as et}from"../../jodit-pro.js";import{UIFlightPosition as w}from"../../traits/flight-position/flight-position.js";import{UIResize as P}from"../../traits/resize/resize.js";let r=(u=class extends tt{constructor(){super(...arguments),this.buttons=[{name:"aiAssistantPro",group:"insert"},{name:"aiCommandsPro",group:"insert"}],this.__initedPromise=null,this.__openedMode=null}get state(){return this.stateManager.state}get toolRegistry(){return new S(this.j)}get permissionManager(){return new A(this.j,this.toolRegistry)}get apiClient(){return new x(this.j,this.toolRegistry)}get storage(){const{storage:t,storageKey:e,maxConversations:a}=this.j.o.aiAssistantPro;if(typeof t=="string"){const s=D(t,e);return new I(s,a)}return t}get stateManager(){return new T(this.j)}get mainPanel(){return new b(this.j,this.stateManager.state,this.j.o.aiAssistantPro,t=>{const e=this.stateManager.getCurrentConversation();e&&this.stateManager.setCurrentConversation({...e,...t})})}get dialog(){const t=this.j.dlg({resizable:this.j.o.aiAssistantPro.resizable,eventEmmiter:this.j.e});return t.e.on(t,"afterClose",()=>{this.j.e.fire("closeAIAssistantPro.ai-assistant-pro")}),t}afterInit(){Z(G),this.setupEventListeners(),this.j.async.promise(t=>{const{initiallyOpened:e,persistOpenState:a}=this.j.o.aiAssistantPro;let s=e;if(a){this.storage.getGlobalSettings().then(i=>{if(i?.isOpen!=null&&(s=i.isOpen),s)return this.openAssistant()}).catch(()=>null).finally(()=>t(void 0));return}t(void 0)}).catch(()=>null)}__loadData(){return this.__initedPromise?this.__initedPromise:this.isDestructed?Promise.reject():(this.__initedPromise=this.j.async.promise(t=>{this.loadInitialData().then(t)}).catch(()=>null),this.__initedPromise)}async loadInitialData(){this.mainPanel.setMod("ready",!0),await V(this.j,this.storage,this.stateManager)}setupEventListeners(){this.j.e.on("selectConversation.ai-assistant-pro",async t=>{try{await this.__loadData(),await H(t,this.storage,this.stateManager)}catch(e){J||console.log(e)}}),this.j.e.on("newConversation.ai-assistant-pro",async()=>{await this.__loadData();const t=M(this.j,this.state.defaultModel,this.state.defaultTemperature);this.stateManager.setCurrentConversation(t)}),this.j.e.on("deleteConversation.ai-assistant-pro",async t=>{await this.__loadData(),await this.deleteConversation(t)}),this.j.e.on("backToList.ai-assistant-pro",async()=>{await this.__loadData(),await W(this.storage,this.stateManager)}),this.j.e.on("removeContext.ai-assistant-pro",async t=>{await this.__loadData(),this.stateManager.removeSelectionContext(t)}),this.j.e.on("approveToolCall.ai-assistant-pro",async(t,e)=>{try{await this.__loadData();const a=await q(t,e,this.stateManager,this.permissionManager,this.storage);if(a){const s=this.stateManager.getCurrentConversation();if(!s)return;const i=s.messages.find(n=>{var o;return(o=n.toolCalls)===null||o===void 0?void 0:o.some(l=>l.id===a.id)});if(!i)return;await this.executeToolCall(i.id,a)}}catch(a){if(C(a))return;this.stateManager.setError(a instanceof Error?a.message:String(a))}}),this.j.e.on("denyToolCall.ai-assistant-pro",async t=>{await this.__loadData(),await L(this.j,t,this.stateManager,this.storage)}),this.j.e.on("abortRequest.ai-assistant-pro",async()=>{await this.__loadData(),this.apiClient.abort(),this.stateManager.setLoading(!1)})}__applyTheme(){R(this.j,this.stateManager.state.theme,this.stateManager.state.displayMode,this.mainPanel.container,this.dialog)}async openAssistant(){if(this.stateManager.isOpen()){this.stateManager.close(),await this.__closeInContainer();return}await this.__loadData(),!this.isDestructed&&(this.stateManager.open(),await this.__openInContainer(),this.j.o.aiAssistantPro.includeSelectionByDefault&&!this.j.s.isCollapsed()&&this.addCurrentSelectionToContext())}async __openInContainer(){const{resizable:t,allowClose:e,flightPanelHeight:a}=this.j.o.aiAssistantPro,{displayMode:s="right",panelWidth:i,panelHeight:n}=this.stateManager.state;switch(this.__applyTheme(),this.mainPanel.setMod("displayMode",s),this.mainPanel.setMod("allowClose",e),this.mainPanel.setParentView(s==="dialog"||s==="flight"?this.dialog:this.j),this.__openedMode=s,s){case"dialog":{this.dialog.setSize(i,n).open().setContent(this.mainPanel.container);break}case"flight":{w.install(this.mainPanel,this.j.container,{panelHeight:a,containerClassName:this.getFullElName("flight-container")});break}default:{t&&(P.remove(this.mainPanel),P.install(this.mainPanel,Q(s),{handleClassName:this.getFullElName("ai-assistent-resize-handle"),onResize:f(({newSize:o})=>{const l=s==="left"||s==="right"?o:i,p=s==="top"||s==="bottom"?o:n;return this.stateManager.state.panelWidth=l,this.stateManager.state.panelHeight=p,!0},"onResize")})),this.j.currentPlace.slots[s].appendChild(this.mainPanel.container),y(this.j,s,i,n);break}}}__onResizeInPanelView(){const{displayMode:t,panelWidth:e,panelHeight:a}=this.stateManager.state;y(this.j,t,e,a)}async __closeInContainer(){const{displayMode:t}=await this.stateManager.state;switch(this.__openedMode!==t&&this.__openedMode==="dialog"&&this.dialog.close(),this.__openedMode!==t&&this.__openedMode==="flight"&&this.__closeFlightMode(),t){case"dialog":{this.dialog.close();break}case"flight":{this.__closeFlightMode();break}default:{this.mainPanel.container.remove();break}}}__closeFlightMode(){w.remove(this.mainPanel)}__onSavedStateChange(){var t;const e=this.stateManager.state;return this.storage.saveGlobalSettings({displayMode:e.displayMode,panelWidth:e.panelWidth,panelHeight:e.panelHeight,theme:e.theme,defaultModel:e.defaultModel,defaultTemperature:e.defaultTemperature,currentConversationId:(t=e.currentConversation)===null||t===void 0?void 0:t.id,...this.j.o.aiAssistantPro.persistOpenState?{isOpen:e.isOpen}:{}})}async closeAssistant(){this.stateManager.isOpen()&&(await this.__loadData(),this.stateManager.close(),this.__closeInContainer())}async addCurrentSelectionToContext(){await this.__loadData();const t=v(this.j);t&&this.stateManager.addSelectionContext(t)}async executeQuickCommand(t){const{quickCommandsSilentMode:e,quickCommandsDisplayMode:a}=this.j.o.aiAssistantPro,s=this.j.o.aiAssistant;let i;if(s&&s[t]&&K(s[t])&&(i=s[t]),!i){this.j.e.fire("error",`Command not found: ${t}`);return}const n=this.j.s.isCollapsed()?this.j.value:this.j.s.html;if(!n)return;const o=s?.aiCommonPrefixPrompt||"",l=s?.aiCommonSuffixPrompt||"",p=`${o}${i}${l} ${n}`.trim();e?await this.__executeQuickCommandSilent(p,n):await this.__executeQuickCommandWithPanel(p,a)}async __executeQuickCommandSilent(t,e){await this.__loadData(),this.j.progressbar.show().progress(100);try{const a=_(t),s=M(this.j,this.state.defaultModel,this.state.defaultTemperature,[a]),i=await this.apiClient.sendRequest(s.id,s.messages,null,[],s.options);if(i.mode==="final"){const n=i.response.content;n&&this.j.s.insertHTML(n)}}catch(a){C(a)||this.j.e.fire("error",a)}finally{this.j.progressbar.hide()}}async __executeQuickCommandWithPanel(t,e){await this.__loadData();const a=this.stateManager.state.displayMode;this.stateManager.state.displayMode=e;const s=v(this.j);s&&this.stateManager.addSelectionContext(s),this.stateManager.isOpen()||await this.openAssistant(),await this.sendMessage(t),this.stateManager.state.displayMode=a}async sendMessage(t){await this.__loadData();let e=this.stateManager.getCurrentConversation();const a=this.stateManager.getEditingMessageId();if(a&&e){const i=e.messages.findIndex(n=>n.id===a);if(i!==-1){const n=e.messages.slice(0,i),o={...e,messages:n,updatedAt:Date.now()};this.stateManager.setCurrentConversation(o),await this.storage.save(o),this.stateManager.clearEditingMessage(),e=o}}e||(e=M(this.j,this.state.defaultModel,this.state.defaultTemperature),this.stateManager.setCurrentConversation(e));let s=null;t.trim()&&(s=_(t),e.title||(e={...e,title:t}),e={...e,updated:Date.now(),messages:[...e.messages,s]}),this.stateManager.setCurrentConversation({...e}),s&&this.j.e.fire("messageSent.ai-assistant-pro",s),this.stateManager.setLoading(!0),this.stateManager.clearError();try{await this.sendRequestToAI(0),this.stateManager.clearSelectionContexts()}catch(i){if(C(i))return;this.stateManager.setError(i instanceof Error?i.message:String(i))}finally{this.stateManager.setLoading(!1)}}async sendRequestToAI(t=0){const e=this.j.o.aiAssistantPro.maxRecursionToolCallDepth;if(t>=e)throw Error(`Max recursion depth ${e} reached for tool calls`);let a=this.stateManager.getCurrentConversation();if(!a)return;const s=F(a),[i]=await Promise.all([this.apiClient.sendRequest(a.id,a.messages,s,this.stateManager.getSelectionContexts(),a.options),this.storage.save(a)]);let n,o;if(i.mode==="stream"){const l=await U(this.stateManager,i,a);n=l.aiMessage,o=l.index,a=l.conversation}else{const l=N(i,a);n=l.aiMessage,o=l.index,a=l.conversation}a=await $(this.j,n,a,o),this.j.e.fire("messageReceived.ai-assistant-pro",n),this.stateManager.setCurrentConversation({...a}),await this.storage.save(a),await this.handleToolCalls(n.id,t)}async deleteConversation(t){await this.__loadData(),await O(this.j,t,this.storage,this.stateManager,this.permissionManager)}async searchConversations(t){await this.__loadData(),await z(t,this.storage,this.stateManager)}async handleToolCalls(t,e=0){const a=this.stateManager.getCurrentConversation();if(!a)return;const s=a.messages.find(l=>l.id===t);if(!s)return;const i=s.toolCalls;if(!i||i.length===0)return;await this.__loadData();const{approved:n,needsPermission:o}=this.permissionManager.filterToolCalls(i,a.permissions);this.stateManager.setPendingToolCalls(o);for(const l of n)await this.executeToolCall(t,l,e)}async executeToolCall(t,e,a=0){await this.__loadData();let s=this.stateManager.getCurrentConversation();if(!s)return;const i=await k(this.j,e,this.toolRegistry),n=s.messages.findIndex(p=>p.id===t);if(n===-1)return;const o=s.messages[n],l=s.messages.slice();l[n]=B(o,i),s={...s,messages:l,updated:Date.now()},this.stateManager.setCurrentConversation(s),E(s)&&await this.sendRequestToAI(a+1)}async copyMessage(t){const e=this.stateManager.getCurrentConversation();if(!e)return;const a=e.messages.find(s=>s.id===t);if(a)try{await navigator.clipboard.writeText(a.content),this.j.message.success("Message copied to clipboard")}catch{this.j.e.fire("error",new Error("Failed to copy message"))}}async restartFromMessage(t){const e=this.stateManager.getCurrentConversation();if(!e)return;const a=e.messages.findIndex(o=>o.id===t);if(a===-1)return;const s=e.messages[a];if(s.role!=="user")return;await this.__loadData();const i=e.messages.slice(0,a),n={...e,messages:i,updatedAt:Date.now()};this.stateManager.setCurrentConversation(n),await this.storage.save(n),await this.sendMessage(s.content)}async editMessage(t){const e=this.stateManager.getCurrentConversation();if(!e)return;const a=e.messages.find(s=>s.id===t);!a||a.role!=="user"||(this.stateManager.setEditingMessageId(t),this.mainPanel.inputArea.setValue(a.content),this.mainPanel.inputArea.focus())}cancelEditMessage(){this.stateManager.isEditingMessage()&&(this.stateManager.clearEditingMessage(),this.mainPanel.inputArea.setValue(""))}async deleteMessage(t){await this.__loadData();const e=this.stateManager.getCurrentConversation();if(!e||e.messages.findIndex(n=>n.id===t)===-1)return;const s=e.messages.filter(n=>n.id!==t),i={...e,messages:s,updatedAt:Date.now()};this.stateManager.setCurrentConversation(i),await this.storage.save(i)}beforeDestruct(){var t,e,a,s,i,n,o,l;this.__closeFlightMode(),(t=g(this,"toolRegistry"))===null||t===void 0||t.destruct(),(e=g(this,"permissionManager"))===null||e===void 0||e.destruct(),(a=g(this,"apiClient"))===null||a===void 0||a.destruct(),(s=g(this,"stateManager"))===null||s===void 0||s.destruct(),(i=g(this,"mainPanel"))===null||i===void 0||i.destruct(),(n=g(this,"dialog"))===null||n===void 0||n.destruct(),(l=(o=g(this,"storage"))===null||o===void 0?void 0:o.close)===null||l===void 0||l.call(o)}},f(u,"aiAssistantPro"),u);r.requires=["license"],h([c],r.prototype,"toolRegistry",null),h([c],r.prototype,"permissionManager",null),h([c],r.prototype,"apiClient",null),h([c],r.prototype,"storage",null),h([c],r.prototype,"stateManager",null),h([c],r.prototype,"mainPanel",null),h([c],r.prototype,"dialog",null),h([d("state.theme")],r.prototype,"__applyTheme",null),h([d(":openAIAssistantPro.ai-assistant-pro")],r.prototype,"openAssistant",null),h([d([":resize","state.panelWidth","state.panelHeight"])],r.prototype,"__onResizeInPanelView",null),h([d(["state.isOpen","state.displayMode","state.panelWidth","state.panelHeight","state.theme","state.defaultModel","state.defaultTemperature","state.currentConversation"]),Y()],r.prototype,"__onSavedStateChange",null),h([d(":closeAIAssistantPro.ai-assistant-pro")],r.prototype,"closeAssistant",null),h([d(":addContextToAIAssistant.ai-assistant-pro")],r.prototype,"addCurrentSelectionToContext",null),h([d(":executeQuickCommand.ai-assistant-pro")],r.prototype,"executeQuickCommand",null),h([d(":sendMessage.ai-assistant-pro")],r.prototype,"sendMessage",null),h([d(":searchConversations.ai-assistant-pro")],r.prototype,"searchConversations",null),h([d(":copyMessage.ai-assistant-pro")],r.prototype,"copyMessage",null),h([d(":restartFromMessage.ai-assistant-pro")],r.prototype,"restartFromMessage",null),h([d(":editMessage.ai-assistant-pro")],r.prototype,"editMessage",null),h([d(":cancelEditMessage.ai-assistant-pro")],r.prototype,"cancelEditMessage",null),h([d(":deleteMessage.ai-assistant-pro")],r.prototype,"deleteMessage",null),r=h([X],r);et.plugins.add("aiAssistantPro",r);export{r as aiAssistantPro};