iobroker.javascript
Version:
Rules Engine for ioBroker
5 lines • 2.4 kB
JavaScript
var e=null,t=null,n=null;function r(r,i,a){let o=[];for(let s of[`javascript`,`typescript`]){let c=r.languages.registerInlineCompletionsProvider(s,{disposeInlineCompletions(){},provideInlineCompletions(o,s,c,l){return new Promise(c=>{if(e&&=(e.cancel(),null),t&&clearTimeout(t),l.isCancellationRequested){c(void 0);return}t=setTimeout(async()=>{if(l.isCancellationRequested){c(void 0);return}let t=Object.keys(a)[0];if(!t){c(void 0);return}if(!n||Date.now()-n.ts>6e4)try{let e=await i.sendTo(t,`getAvailableAiProviders`,{});n={providers:(e==null?void 0:e.providers)||[],ts:Date.now()}}catch{c(void 0);return}let u=[`custom`,`openai`,`deepseek`,`anthropic`,`gemini`],d=``,f=``;for(let e of u){let t=n.providers.find(t=>t.provider===e);if(t){d=e===`custom`?`openai`:e,f=t.baseUrl||``;break}}if(!d){c(void 0);return}let p=window.localStorage.getItem(`openai-model`)||``,m=o.getLineCount(),h=Math.max(1,s.lineNumber-50),g=Math.min(m,s.lineNumber+10),_=``;for(let e=h;e<s.lineNumber;e++)_+=`${o.getLineContent(e)}\n`;_+=o.getLineContent(s.lineNumber).substring(0,s.column-1);let v=o.getLineContent(s.lineNumber).substring(s.column-1);for(let e=s.lineNumber+1;e<=g;e++)v+=`\n${o.getLineContent(e)}`;let y=!1;e={cancel:()=>{y=!0}},l.onCancellationRequested(()=>{y=!0});try{let e=await i.sendTo(t,`chatCompletion`,{timeout:15e3,...f?{baseUrl:f}:{},model:p,provider:d,messages:[{role:`system`,content:`You are a code completion engine for ioBroker JavaScript adapter scripts.
Complete the code at the cursor position marked with <CURSOR>.
Return ONLY the completion text, no explanation, no markdown fences, no comments about what you did.
If you cannot complete, return an empty string.
Available functions: on(), setState(), getState(), schedule(), sendTo(), log(), createState(), setStateDelayed(), existsState(), httpGet(), httpPost(), exec(), formatDate(), $(), wait(), toInt(), toFloat(), readFile(), writeFile().`},{role:`user`,content:`${_}<CURSOR>${v}`}]});if(y||l.isCancellationRequested){c(void 0);return}if(e.error||!e.content){c(void 0);return}let n=e.content.trim(),a=n.match(/```\w*\n?([\s\S]*?)```/);if(a&&(n=a[1].trim()),!n){c(void 0);return}c({items:[{insertText:n,range:new r.Range(s.lineNumber,s.column,s.lineNumber,s.column)}]})}catch{c(void 0)}e=null},800)})}});o.push(c)}return{dispose:()=>{t&&clearTimeout(t),e&&e.cancel(),o.forEach(e=>e.dispose())}}}export{r as registerAiInlineProvider};