jodit-pro
Version:
PRO Version of Jodit Editor
2 lines (1 loc) • 3.08 kB
JavaScript
var b=Object.defineProperty;var w=(y,o)=>b(y,"name",{value:o,configurable:!0});import{IS_PROD as C}from"jodit/esm/core/constants.js";import{abort as R}from"jodit/esm/core/helpers/index.js";const p=class p{constructor(o,t){this.jodit=o,this.toolRegistry=t,this.abortControllers=new Set}async sendRequest(o,t,e,r,l){const{apiRequest:n,requestTimeout:c,maxRetries:i}=this.jodit.o.aiAssistantPro;if(!n)throw new Error("AI Assistant Pro callback is not configured");const s=new AbortController;this.abortControllers.add(s);try{const a=this.buildRequestContext(o,t,e,r,l);let h=null;for(let f=0;f<=i;f++)try{if(s.signal.aborted)throw R();const d=setTimeout(()=>{s.abort()},c);try{const u=await n(a,s.signal);return clearTimeout(d),this.validateResponse(u),u}catch(u){throw clearTimeout(d),u}}catch(d){if(h=d,C||console.warn(`API request attempt ${f+1} failed:`,d),f<i){const u=this.calculateRetryDelay(f);await this.sleep(u)}else throw d}throw h||new Error("API request failed after all retries")}finally{this.abortControllers.delete(s)}}abort(){this.abortControllers.forEach(o=>{o.abort()}),this.abortControllers.clear()}isInProgress(){return this.abortControllers.size>0}buildRequestContext(o,t,e,r,l){const{apiMode:n,instructions:c}=this.jodit.o.aiAssistantPro;let i={mode:n,conversationId:o,tools:this.toolRegistry.getAllTools(),selectionContexts:r,conversationOptions:l,instructions:c},s=[];if(n==="full")s=t;else if(e){const a=t.findIndex(h=>h.id===e);a!==-1?s=t.slice(a+1):s=t,i={...i,parentMessageId:e}}else s=t;return i={...i,messages:this.expandToolMessages(s,t.find(a=>a.id===e))},i}expandToolMessages(o,t){const e=[];for(const r of o)e.push(r),this.expandToolResultsMessage(r,e);return t&&this.expandToolResultsMessage(t,e),e}expandToolResultsMessage(o,t){var e;if(o.toolCalls&&o.toolCalls.length>0)for(const r of o.toolCalls){const l=(e=r.result)!==null&&e!==void 0?e:{error:"result not found"};if(t.find(c=>c.id===`${r.id}-result`))continue;const n={id:`${r.id}-result`,role:"tool",content:l.error?`Error: ${l.error}`:JSON.stringify(l.result),timestamp:o.timestamp,toolResults:[{toolCallId:r.id,result:l}]};t.push(n)}}validateResponse(o){if(o.mode!=="final")return;const t=o.response;if(!t)throw new Error("API returned empty response");if(!t.responseId)throw new Error("API response missing responseId");if(t.content===void 0)throw new Error("API response missing content");if(t.finished===void 0)throw new Error("API response missing finished flag");if(t.toolCalls){if(!Array.isArray(t.toolCalls))throw new Error("API response toolCalls must be an array");t.toolCalls.forEach((e,r)=>{if(!e.id)throw new Error(`Tool call at index ${r} is missing id`);if(!e.name)throw new Error(`Tool call at index ${r} is missing name`);if(!e.arguments||typeof e.arguments!="object")throw new Error(`Tool call at index ${r} has invalid arguments`)})}}calculateRetryDelay(o){const{retryDelay:t}=this.jodit.o.aiAssistantPro,e=t*Math.pow(2,o),r=Math.random()*t;return e+r}sleep(o){return new Promise(t=>setTimeout(t,o))}destruct(){this.abort()}};w(p,"APIClient");let m=p;export{m as APIClient};