monacopilot 0.9.2 → 0.9.4
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/build/index.d.mts +10 -4
- package/build/index.d.ts +10 -4
- package/build/index.js +7 -7
- package/build/index.mjs +7 -7
- package/package.json +1 -1
package/build/index.d.mts
CHANGED
|
@@ -56,6 +56,13 @@ interface RegisterCopilotOptions {
|
|
|
56
56
|
*/
|
|
57
57
|
externalContext?: ExternalContext;
|
|
58
58
|
}
|
|
59
|
+
interface CopilotRegistration {
|
|
60
|
+
/**
|
|
61
|
+
* Unregisters the Copilot from the Monaco editor.
|
|
62
|
+
* This should be called when the Copilot is no longer needed.
|
|
63
|
+
*/
|
|
64
|
+
unregister: () => void;
|
|
65
|
+
}
|
|
59
66
|
|
|
60
67
|
type CompletionModel = 'llama';
|
|
61
68
|
interface CompletionRequest {
|
|
@@ -105,9 +112,8 @@ declare class Copilot {
|
|
|
105
112
|
* @param monaco The Monaco instance.
|
|
106
113
|
* @param editor The editor instance.
|
|
107
114
|
* @param options The options for the Copilot.
|
|
108
|
-
*
|
|
109
|
-
* @returns A function to unregister the Copilot. Use this to clean up the Copilot.
|
|
115
|
+
* @returns CopilotRegistration object with an unregister method.
|
|
110
116
|
*/
|
|
111
|
-
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) =>
|
|
117
|
+
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
112
118
|
|
|
113
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
|
119
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type CopilotOptions, type CopilotRegistration, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
package/build/index.d.ts
CHANGED
|
@@ -56,6 +56,13 @@ interface RegisterCopilotOptions {
|
|
|
56
56
|
*/
|
|
57
57
|
externalContext?: ExternalContext;
|
|
58
58
|
}
|
|
59
|
+
interface CopilotRegistration {
|
|
60
|
+
/**
|
|
61
|
+
* Unregisters the Copilot from the Monaco editor.
|
|
62
|
+
* This should be called when the Copilot is no longer needed.
|
|
63
|
+
*/
|
|
64
|
+
unregister: () => void;
|
|
65
|
+
}
|
|
59
66
|
|
|
60
67
|
type CompletionModel = 'llama';
|
|
61
68
|
interface CompletionRequest {
|
|
@@ -105,9 +112,8 @@ declare class Copilot {
|
|
|
105
112
|
* @param monaco The Monaco instance.
|
|
106
113
|
* @param editor The editor instance.
|
|
107
114
|
* @param options The options for the Copilot.
|
|
108
|
-
*
|
|
109
|
-
* @returns A function to unregister the Copilot. Use this to clean up the Copilot.
|
|
115
|
+
* @returns CopilotRegistration object with an unregister method.
|
|
110
116
|
*/
|
|
111
|
-
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) =>
|
|
117
|
+
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
112
118
|
|
|
113
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
|
119
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type CopilotOptions, type CopilotRegistration, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
package/build/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`);return e[e.length-1].length};var
|
|
3
|
-
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=
|
|
1
|
+
"use strict";var I=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames;var re=Object.prototype.hasOwnProperty;var ne=(t,e)=>{for(var o in e)I(t,o,{get:e[o],enumerable:!0})},ie=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of oe(e))!re.call(t,i)&&i!==o&&I(t,i,{get:()=>e[i],enumerable:!(r=te(e,i))||r.enumerable});return t};var se=t=>ie(I({},"__esModule",{value:!0}),t);var fe={};ne(fe,{Copilot:()=>O,registerCopilot:()=>ee});module.exports=se(fe);var b=()=>{},S=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Promise((n,s)=>{o=setTimeout(()=>{t(...i).then(n).catch(s)},e)}))},B=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var x=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],$=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),u=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),_=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var h=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),L=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var D=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},i=e==="POST"&&o.body?JSON.stringify(o.body):void 0,n=await fetch(t,{method:e,headers:r,body:i,signal:o.signal});if(!n.ok)throw new Error(`${o.error||"Network error"}: ${n.statusText}`);return n.json()},le=(t,e)=>D(t,"GET",e),me=(t,e,o)=>D(t,"POST",{...o,body:e}),T={GET:le,POST:me};var P=class{constructor(e,o){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=o,this.lineCount=e.getLineCount()}ignoreBlankLines(){return this.formattedCompletion.trimStart()===""&&this.originalCompletion!==`
|
|
3
|
+
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=h(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,i=Math.min(o.length,e.length);for(let n=1;n<=i;n++){let s=e.slice(-n),m=o.slice(0,n);if(s===m)r=n;else break}return r>0&&(this.formattedCompletion=this.formattedCompletion.slice(r)),this}preventDuplicateLines(){for(let e=this.cursorPosition.lineNumber+1;e<this.cursorPosition.lineNumber+3&&e<this.lineCount;e++){let o=this.model.getLineContent(e);if(this.normalise(o)===this.normalise(this.originalCompletion))return this.formattedCompletion="",this}return this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}trimStart(){let e=this.formattedCompletion.search(/\S/);return e>this.cursorPosition.column-1&&(this.formattedCompletion=this.formattedCompletion.slice(e)),this}format(e){return this.originalCompletion=e,this.formattedCompletion=e,this.ignoreBlankLines().removeDuplicatesFromStartOfCompletion().preventDuplicateLines().removeInvalidLineBreaks().trimStart(),this.formattedCompletion}};var ae=10,g=[],k=(t,e)=>g.filter(o=>{let r=e.getValueInRange(o.range);return u(t,e).startsWith(o.textBeforeCursorInLine)&&(o.range.startLineNumber===t.lineNumber&&t.column===o.range.startColumn||o.completion.startsWith(r)&&o.range.startLineNumber===t.lineNumber&&t.column>=o.range.startColumn-r.length&&t.column<=o.range.endColumn)}),q=t=>{g.length>=ae&&g.shift(),g.push(t)},F=()=>{g.length=0};var U={llama:"llama3-70b-8192"},y="llama",G="https://api.groq.com/openai/v1/chat/completions";var j=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var W=(t,e)=>{let o=x(t,e);return!!o&&!j.has(o)},H=(t,e)=>{let o=$(t,e).trim(),r=u(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var K=(t,e,o,r)=>{let i=(t.match(/\n/g)||[]).length,n=_(t),s=x(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+i,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&i===0?o.column+n:n:o.column}},V=(t,e,o)=>new P(t,e).format(o),c=t=>({items:t,enableForwardStability:!0});var R=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!W(this.cursorPosition,this.model)&&!H(this.cursorPosition,this.model)}};var M=class{static getModel(){return this.model}static setModel(e){this.model=e}};M.model=y;var N=M;var v=class{constructor(e){this.error=e}logError(e,o,r){console.error(`${e}: ${o}`,r)}monacopilotError(e){this.logError("MONACO_PILOT_ERROR",e,this.error)}apiError(e){this.logError("API_ERROR",e,this.error)}completionError(e){this.logError("COMPLETION_ERROR",e,this.error)}predictionError(e){this.logError("PREDICTION_ERROR",e,this.error)}editorError(e){this.logError("EDITOR_ERROR",e,this.error)}unexpectedError(){this.error instanceof Error?this.logError("UNEXPECTED_ERROR",this.error.message,this.error.stack):this.logError("UNKNOWN_ERROR",String(this.error))}},a=t=>new v(t);var f="<<CURSOR>>",Y=t=>t==="javascript"?"latest JavaScript":t,J=t=>{switch(t){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},z=t=>{let e=Y(t.language),o=J(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},pe=(t,e)=>{if(!t?.length&&!e)return"";let o=B(t),r=Y(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},Q=t=>{let{filename:e,language:o,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:s,externalContext:m}=t,p=J(i.completionMode),E=e?`the file named ${e}`:"a larger project",l=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${f}'. Your task is to assist with ${p}. This code is part of ${E}. Please `;switch(i.completionMode){case"fill-in-the-middle":l+=`generate a completion to fill the middle of the code around '${f}'. Ensure the completion replaces '${f}' precisely, maintaining consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break;case"completion":l+=`provide the necessary completion for '${f}' while ensuring consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break}l+=` Output only the necessary completion code, without additional explanations or content.${pe(r,o)}`;let d=`${n}${f}${s}
|
|
4
4
|
|
|
5
|
-
`;return
|
|
5
|
+
`;return m&&m.length>0&&(d+=m.map(C=>`// Path: ${C.path}
|
|
6
6
|
${C.content}
|
|
7
7
|
`).join(`
|
|
8
|
-
`)),
|
|
8
|
+
`)),l+=`
|
|
9
9
|
|
|
10
10
|
<code>
|
|
11
|
-
${
|
|
12
|
-
</code>`,
|
|
11
|
+
${d}
|
|
12
|
+
</code>`,l.endsWith(".")?l:`${l}.`};var ce="application/json",X=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:i,model:n,position:s})=>{try{let{completion:m}=await T.POST(e,{completionMetadata:de({filename:t,position:s,model:n,language:o,technologies:r,externalContext:i})},{headers:{"Content-Type":ce},error:"Error while fetching completion item"});return m||null}catch(m){return a(m).completionError("Error while fetching completion item"),null}},de=({filename:t,position:e,model:o,language:r,technologies:i,externalContext:n})=>{let s=ue(e,o),m=h(e,o),p=L(e,o);return{filename:t,language:r,technologies:i,externalContext:n,textBeforeCursor:m,textAfterCursor:p,editorState:{completionMode:s}}},ue=(t,e)=>{let o=h(t,e),r=L(t,e);return o&&r?"fill-in-the-middle":"completion"};var O=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,N.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=N.getModel(),r=this.createRequestBody(e,o),i=this.createHeaders(),n=await T.POST(G,r,{headers:i});if(!n.choices||n.choices.length===0)throw new Error("No completion choices received from API");return{completion:n.choices[0].message.content}}catch(o){return o instanceof Error?a(o).apiError("Failed to fetch completion"):a(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{model:U[o],messages:[{role:"system",content:z(e)},{role:"user",content:Q(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var Ce=250,he=S(X,Ce),ge=async({monaco:t,model:e,position:o,token:r,isCompletionAccepted:i,onShowCompletion:n,options:s})=>{let m=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new R(o,e).shouldProvideCompletions())return c([]);let E=k(o,e).map(l=>({insertText:l.completion,range:l.range}));if(E.length)return n(),c(E);if(r.isCancellationRequested)return c([]);if(i)return c([]);try{let l=await he({...s,text:m,model:e,position:o});if(l){let d=V(e,o,l),C=K(d,p,o,e);return q({completion:d,range:C,textBeforeCursorInLine:u(o,e)}),n(),c([{insertText:d,range:C}])}}catch(l){a(l).completionError("Failed to fetch completion item")}return c([])},Z=ge;var w=!1,A=!1,ee=(t,e,o)=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,s,m,p)=>Z({monaco:t,model:n,position:s,token:p,isCompletionAccepted:w,onShowCompletion:()=>A=!0,options:o}),freeInlineCompletions:b});return e.onKeyDown(n=>{A&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?(w=!0,A=!1):w=!1}),{unregister:()=>{r.dispose(),F()}}}catch(r){return a(r).editorError("Error while registering Copilot"),{unregister:b}}};0&&(module.exports={Copilot,registerCopilot});
|
package/build/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
`);return e[e.length-1].length};var
|
|
3
|
-
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=
|
|
1
|
+
var O=()=>{},A=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Promise((n,s)=>{o=setTimeout(()=>{t(...i).then(n).catch(s)},e)}))},S=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var x=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],B=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),u=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),$=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var h=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),I=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var _=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},i=e==="POST"&&o.body?JSON.stringify(o.body):void 0,n=await fetch(t,{method:e,headers:r,body:i,signal:o.signal});if(!n.ok)throw new Error(`${o.error||"Network error"}: ${n.statusText}`);return n.json()},Z=(t,e)=>_(t,"GET",e),ee=(t,e,o)=>_(t,"POST",{...o,body:e}),T={GET:Z,POST:ee};var P=class{constructor(e,o){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=o,this.lineCount=e.getLineCount()}ignoreBlankLines(){return this.formattedCompletion.trimStart()===""&&this.originalCompletion!==`
|
|
3
|
+
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=h(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,i=Math.min(o.length,e.length);for(let n=1;n<=i;n++){let s=e.slice(-n),m=o.slice(0,n);if(s===m)r=n;else break}return r>0&&(this.formattedCompletion=this.formattedCompletion.slice(r)),this}preventDuplicateLines(){for(let e=this.cursorPosition.lineNumber+1;e<this.cursorPosition.lineNumber+3&&e<this.lineCount;e++){let o=this.model.getLineContent(e);if(this.normalise(o)===this.normalise(this.originalCompletion))return this.formattedCompletion="",this}return this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}trimStart(){let e=this.formattedCompletion.search(/\S/);return e>this.cursorPosition.column-1&&(this.formattedCompletion=this.formattedCompletion.slice(e)),this}format(e){return this.originalCompletion=e,this.formattedCompletion=e,this.ignoreBlankLines().removeDuplicatesFromStartOfCompletion().preventDuplicateLines().removeInvalidLineBreaks().trimStart(),this.formattedCompletion}};var te=10,g=[],D=(t,e)=>g.filter(o=>{let r=e.getValueInRange(o.range);return u(t,e).startsWith(o.textBeforeCursorInLine)&&(o.range.startLineNumber===t.lineNumber&&t.column===o.range.startColumn||o.completion.startsWith(r)&&o.range.startLineNumber===t.lineNumber&&t.column>=o.range.startColumn-r.length&&t.column<=o.range.endColumn)}),k=t=>{g.length>=te&&g.shift(),g.push(t)},q=()=>{g.length=0};var F={llama:"llama3-70b-8192"},y="llama",U="https://api.groq.com/openai/v1/chat/completions";var G=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var j=(t,e)=>{let o=x(t,e);return!!o&&!G.has(o)},W=(t,e)=>{let o=B(t,e).trim(),r=u(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var H=(t,e,o,r)=>{let i=(t.match(/\n/g)||[]).length,n=$(t),s=x(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+i,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&i===0?o.column+n:n:o.column}},K=(t,e,o)=>new P(t,e).format(o),c=t=>({items:t,enableForwardStability:!0});var R=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!j(this.cursorPosition,this.model)&&!W(this.cursorPosition,this.model)}};var M=class{static getModel(){return this.model}static setModel(e){this.model=e}};M.model=y;var b=M;var L=class{constructor(e){this.error=e}logError(e,o,r){console.error(`${e}: ${o}`,r)}monacopilotError(e){this.logError("MONACO_PILOT_ERROR",e,this.error)}apiError(e){this.logError("API_ERROR",e,this.error)}completionError(e){this.logError("COMPLETION_ERROR",e,this.error)}predictionError(e){this.logError("PREDICTION_ERROR",e,this.error)}editorError(e){this.logError("EDITOR_ERROR",e,this.error)}unexpectedError(){this.error instanceof Error?this.logError("UNEXPECTED_ERROR",this.error.message,this.error.stack):this.logError("UNKNOWN_ERROR",String(this.error))}},a=t=>new L(t);var f="<<CURSOR>>",V=t=>t==="javascript"?"latest JavaScript":t,Y=t=>{switch(t){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},J=t=>{let e=V(t.language),o=Y(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},oe=(t,e)=>{if(!t?.length&&!e)return"";let o=S(t),r=V(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},z=t=>{let{filename:e,language:o,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:s,externalContext:m}=t,p=Y(i.completionMode),E=e?`the file named ${e}`:"a larger project",l=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${f}'. Your task is to assist with ${p}. This code is part of ${E}. Please `;switch(i.completionMode){case"fill-in-the-middle":l+=`generate a completion to fill the middle of the code around '${f}'. Ensure the completion replaces '${f}' precisely, maintaining consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break;case"completion":l+=`provide the necessary completion for '${f}' while ensuring consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break}l+=` Output only the necessary completion code, without additional explanations or content.${oe(r,o)}`;let d=`${n}${f}${s}
|
|
4
4
|
|
|
5
|
-
`;return
|
|
5
|
+
`;return m&&m.length>0&&(d+=m.map(C=>`// Path: ${C.path}
|
|
6
6
|
${C.content}
|
|
7
7
|
`).join(`
|
|
8
|
-
`)),
|
|
8
|
+
`)),l+=`
|
|
9
9
|
|
|
10
10
|
<code>
|
|
11
|
-
${
|
|
12
|
-
</code>`,
|
|
11
|
+
${d}
|
|
12
|
+
</code>`,l.endsWith(".")?l:`${l}.`};var re="application/json",Q=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:i,model:n,position:s})=>{try{let{completion:m}=await T.POST(e,{completionMetadata:ne({filename:t,position:s,model:n,language:o,technologies:r,externalContext:i})},{headers:{"Content-Type":re},error:"Error while fetching completion item"});return m||null}catch(m){return a(m).completionError("Error while fetching completion item"),null}},ne=({filename:t,position:e,model:o,language:r,technologies:i,externalContext:n})=>{let s=ie(e,o),m=h(e,o),p=I(e,o);return{filename:t,language:r,technologies:i,externalContext:n,textBeforeCursor:m,textAfterCursor:p,editorState:{completionMode:s}}},ie=(t,e)=>{let o=h(t,e),r=I(t,e);return o&&r?"fill-in-the-middle":"completion"};var N=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,b.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=b.getModel(),r=this.createRequestBody(e,o),i=this.createHeaders(),n=await T.POST(U,r,{headers:i});if(!n.choices||n.choices.length===0)throw new Error("No completion choices received from API");return{completion:n.choices[0].message.content}}catch(o){return o instanceof Error?a(o).apiError("Failed to fetch completion"):a(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{model:F[o],messages:[{role:"system",content:J(e)},{role:"user",content:z(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var se=250,le=A(Q,se),me=async({monaco:t,model:e,position:o,token:r,isCompletionAccepted:i,onShowCompletion:n,options:s})=>{let m=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new R(o,e).shouldProvideCompletions())return c([]);let E=D(o,e).map(l=>({insertText:l.completion,range:l.range}));if(E.length)return n(),c(E);if(r.isCancellationRequested)return c([]);if(i)return c([]);try{let l=await le({...s,text:m,model:e,position:o});if(l){let d=K(e,o,l),C=H(d,p,o,e);return k({completion:d,range:C,textBeforeCursorInLine:u(o,e)}),n(),c([{insertText:d,range:C}])}}catch(l){a(l).completionError("Failed to fetch completion item")}return c([])},X=me;var v=!1,w=!1,ae=(t,e,o)=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,s,m,p)=>X({monaco:t,model:n,position:s,token:p,isCompletionAccepted:v,onShowCompletion:()=>w=!0,options:o}),freeInlineCompletions:O});return e.onKeyDown(n=>{w&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?(v=!0,w=!1):v=!1}),{unregister:()=>{r.dispose(),q()}}}catch(r){return a(r).editorError("Error while registering Copilot"),{unregister:O}}};export{N as Copilot,ae as registerCopilot};
|