UNPKG

on-codemerge

Version:

A WYSIWYG editor for on-codemerge is a user-friendly interface that allows users to edit and view their code in real time, exactly as it will appear in the final product

14 lines (12 loc) 5.22 kB
"use strict";var h=Object.defineProperty;var d=(o,e,t)=>e in o?h(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var r=(o,e,t)=>d(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});;/* empty css */;/* empty css */const c=require("../../core/ui/PopupManager.cjs"),m=require("../ToolbarPlugin/utils.cjs"),v=require("../../icons/ai-assistant.svg.cjs"),g=require("./drivers/DeepSeekDriver.cjs"),y=require("./drivers/HuggingFaceDriver.cjs"),b=require("./drivers/OpenAIDriver.cjs"),P=require("./drivers/GitHubAzureDriver.cjs"),f=require("./drivers/LlamaDriver.cjs"),D=require("./drivers/MistralDriver.cjs"),O=require("./drivers/OllamaDriver.cjs"),n="aiAssistantSettings",p="Write an article about the benefits of using artificial intelligence in web development. Include examples of JavaScript code and explain how AI can simplify the development process.",u=`The response should be formatted as HTML code that can be inserted into a text editor. Follow these rules: 1. Headings should be wrapped in <h1>, <h2>, <h3>, etc. 2. Paragraphs should be wrapped in <p>. 3. Lists should use <ul>, <ol>, and <li>. 4. Code should be wrapped in <pre><code>. 5. Use <strong> and <em> for emphasis. 6. For images, use the <img> tag with the src attribute. 7. Links should use the <a> tag. 8. Tables should use <table>, <tr>, <th>, and <td>. 9. Do not include unnecessary tags like <html>, <head>, or <body>. 10. Ensure code examples are properly formatted.`;class S{constructor(){r(this,"name","ai-assistant");r(this,"editor",null);r(this,"popup",null);r(this,"apiKey","");r(this,"driver",null);r(this,"driverName","openai");r(this,"structurePrompt",u);r(this,"prompt",p);r(this,"driverOptions",{});r(this,"drivers",{});this.loadSettings(),this.initializeDrivers()}initializeDrivers(){this.drivers={openai:new b.OpenAIDriver(this.apiKey),deepseek:new g.DeepSeekDriver(this.apiKey),huggingface:new y.HuggingFaceDriver(this.apiKey),github:new P.GitHubAzureDriver(this.apiKey),llama:new f.LlamaDriver(this.apiKey),mistral:new D.MistralDriver(this.apiKey),ollama:new O.OllamaDriver(this.apiKey)}}initialize(e){this.editor=e,this.setupPopup(),this.addToolbarButton()}addToolbarButton(){const e=document.querySelector(".editor-toolbar");if(e){const t=m.createToolbarButton({icon:v.default,title:"AI Assistant",onClick:()=>{var i;(i=this.popup)==null||i.show()}});e.appendChild(t)}}createPopupItems(){const e=[{type:"list",id:"driver-select",label:"AI Driver:",options:["openai","deepseek","huggingface","github","llama","mistral","ollama"],value:this.driverName,onChange:i=>{this.driverName=i,this.saveSettings(),this.updatePopupContent()}},{type:"input",id:"api-key-input",label:"API Key:",placeholder:"Enter your API key",value:this.apiKey,onChange:i=>{this.apiKey=i,this.saveSettings()}},{type:"textarea",id:"structure-prompt-textarea",label:"Structure Prompt:",placeholder:"Enter your structure prompt",value:this.structurePrompt,onChange:i=>{this.structurePrompt=i,this.saveSettings()}},{type:"textarea",id:"prompt-textarea",label:"Prompt:",placeholder:"Enter your prompt",value:this.prompt,onChange:i=>{this.prompt=i,this.saveSettings()}}],t=this.getDriverOptionsDescription();for(const[i,s]of Object.entries(t)){const a={type:s.type,id:`driver-option-${i}`,label:s.label,options:s.type==="list"?s.options:void 0,value:this.driverOptions[i]||s.default,onChange:l=>{this.driverOptions[i]=l,this.saveSettings()}};s.min&&(a.min=s.min),s.max&&(a.max=s.max),e.push(a)}return e}setupPopup(){if(!this.editor)return;const e=this.createPopupItems();this.popup=new c.PopupManager(this.editor,{title:"AI Assistant",className:"ai-assistant",closeOnClickOutside:!0,buttons:[{label:"Generate",variant:"primary",onClick:()=>this.handleGenerate()},{label:"Cancel",variant:"secondary",onClick:()=>{var t;return(t=this.popup)==null?void 0:t.hide()}}],items:e})}updatePopupContent(){if(!this.popup)return;const e=this.createPopupItems();this.popup.rerender(e)}getDriverOptionsDescription(){const e=this.drivers[this.driverName];return e?e.getOptionsDescription():{}}async handleGenerate(){if(!(!this.editor||!this.popup)){if(this.popup.rerender([{type:"loader",id:"loader"}]),this.driver=this.drivers[this.driverName],!this.driver)throw new Error("Unsupported driver");try{const e=`${this.structurePrompt} ${this.prompt}`,t=await this.driver.generateText(e,this.driverOptions);this.editor.insertContent(t),this.popup.hide()}catch(e){console.error("Error generating text:",e),alert("Failed to generate text. Please check your API key and try again.")}finally{const e=this.createPopupItems();this.popup.rerender(e)}}}saveSettings(){const e={apiKey:this.apiKey,driverName:this.driverName,prompt:this.prompt,structurePrompt:this.structurePrompt,driverOptions:this.driverOptions};localStorage.setItem(n,JSON.stringify(e))}loadSettings(){const e=localStorage.getItem(n);if(e){const t=JSON.parse(e);this.apiKey=t.apiKey||"",this.driverName=t.driverName||"openai",this.prompt=t.prompt||p,this.structurePrompt=t.structurePrompt||u,this.driverOptions=t.driverOptions||{}}}destroy(){this.popup&&(this.popup.destroy(),this.popup=null),this.editor=null}}exports.AIAssistantPlugin=S;