monacopilot 0.8.24 → 0.9.1
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/README.md +1 -1
- package/{dist → build}/index.d.mts +17 -32
- package/{dist → build}/index.d.ts +17 -32
- package/build/index.js +28 -0
- package/build/index.mjs +28 -0
- package/package.json +10 -12
- package/dist/index.js +0 -29
- package/dist/index.mjs +0 -29
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Monacopilot
|
|
2
2
|
|
|
3
|
-
Extended Monaco Editor with AI auto-completion
|
|
3
|
+
Extended [Monaco Editor](https://microsoft.github.io/monaco-editor/) with AI auto-completion, inspired by [GitHub Copilot](https://github.com/features/copilot/).
|
|
4
4
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, loader, useMonaco } from '@monaco-editor/react';
|
|
1
|
+
import { EditorProps } from '@monaco-editor/react';
|
|
2
|
+
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, Theme, loader, useMonaco } from '@monaco-editor/react';
|
|
3
3
|
import React from 'react';
|
|
4
4
|
|
|
5
|
-
type EditorBuiltInTheme = Theme$1;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Themes available for the Monacopilot
|
|
9
|
-
*/
|
|
10
|
-
type CustomTheme = 'codesandbox-dark' | 'dracula-soft' | 'dracula' | 'github-dark-dimmed' | 'github-dark' | 'github-light' | 'monokai' | 'nord' | 'one-dark-pro-darker' | 'one-monokai';
|
|
11
|
-
type Theme = EditorBuiltInTheme | CustomTheme;
|
|
12
5
|
type Endpoint = string;
|
|
13
6
|
type Filename = string;
|
|
14
7
|
type Technologies = string[];
|
|
@@ -49,10 +42,6 @@ interface MonaCopilotProps extends EditorProps {
|
|
|
49
42
|
* etc.
|
|
50
43
|
*/
|
|
51
44
|
technologies?: Technologies;
|
|
52
|
-
/**
|
|
53
|
-
* The theme you want to use for the editor.
|
|
54
|
-
*/
|
|
55
|
-
theme?: Theme;
|
|
56
45
|
/**
|
|
57
46
|
* Helps to give more relevant completions based on the full context.
|
|
58
47
|
* You can include things like the contents/codes of other files in the same workspace.
|
|
@@ -74,8 +63,8 @@ interface CompletionMetadata {
|
|
|
74
63
|
filename: Filename | undefined;
|
|
75
64
|
technologies: Technologies | undefined;
|
|
76
65
|
externalContext: ExternalContext | undefined;
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
textAfterCursor: string;
|
|
67
|
+
textBeforeCursor: string;
|
|
79
68
|
editorState: {
|
|
80
69
|
completionMode: CompletionMode;
|
|
81
70
|
};
|
|
@@ -86,31 +75,27 @@ interface CopilotOptions {
|
|
|
86
75
|
}
|
|
87
76
|
|
|
88
77
|
/**
|
|
89
|
-
*
|
|
90
|
-
* and an API key, and provides a method to send a completion request to Groq API and return the completion.
|
|
91
|
-
*
|
|
92
|
-
* @param {string} apiKey - The Groq API key.
|
|
93
|
-
* @param {CopilotOptions} [options] - Optional parameters to configure the completion model,
|
|
94
|
-
* such as the model ID. Defaults to `llama` if not specified.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* const copilot = new Copilot(process.env.GROQ_API_KEY, {
|
|
99
|
-
* model: 'llama',
|
|
100
|
-
* });
|
|
101
|
-
* ```
|
|
78
|
+
* Copilot class for handling completions using the Groq API.
|
|
102
79
|
*/
|
|
103
80
|
declare class Copilot {
|
|
104
|
-
private apiKey;
|
|
81
|
+
private readonly apiKey;
|
|
82
|
+
/**
|
|
83
|
+
* Initializes the Copilot with an API key and optional configuration.
|
|
84
|
+
* @param {string} apiKey - The Groq API key.
|
|
85
|
+
* @param {CopilotOptions} [options] - Optional parameters to configure the completion model.
|
|
86
|
+
* @throws {Error} If the API key is not provided.
|
|
87
|
+
*/
|
|
105
88
|
constructor(apiKey: string, options?: CopilotOptions);
|
|
106
89
|
/**
|
|
107
90
|
* Sends a completion request to Groq API and returns the completion.
|
|
108
91
|
* @param {CompletionRequest} params - The metadata required to generate the completion.
|
|
109
|
-
* @returns {Promise<CompletionResponse>} The completed
|
|
92
|
+
* @returns {Promise<CompletionResponse>} The completed text snippet or an error.
|
|
110
93
|
*/
|
|
111
94
|
complete({ completionMetadata, }: CompletionRequest): Promise<CompletionResponse>;
|
|
95
|
+
private createRequestBody;
|
|
96
|
+
private createHeaders;
|
|
112
97
|
}
|
|
113
98
|
|
|
114
|
-
declare const MonaCopilot: ({ filename, endpoint, technologies,
|
|
99
|
+
declare const MonaCopilot: ({ filename, endpoint, technologies, externalContext, onMount, ...props }: MonaCopilotProps) => React.JSX.Element;
|
|
115
100
|
|
|
116
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type Endpoint, type MonaCopilotProps, type Technologies,
|
|
101
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type Endpoint, type MonaCopilotProps, type Technologies, MonaCopilot as default };
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, loader, useMonaco } from '@monaco-editor/react';
|
|
1
|
+
import { EditorProps } from '@monaco-editor/react';
|
|
2
|
+
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, Theme, loader, useMonaco } from '@monaco-editor/react';
|
|
3
3
|
import React from 'react';
|
|
4
4
|
|
|
5
|
-
type EditorBuiltInTheme = Theme$1;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Themes available for the Monacopilot
|
|
9
|
-
*/
|
|
10
|
-
type CustomTheme = 'codesandbox-dark' | 'dracula-soft' | 'dracula' | 'github-dark-dimmed' | 'github-dark' | 'github-light' | 'monokai' | 'nord' | 'one-dark-pro-darker' | 'one-monokai';
|
|
11
|
-
type Theme = EditorBuiltInTheme | CustomTheme;
|
|
12
5
|
type Endpoint = string;
|
|
13
6
|
type Filename = string;
|
|
14
7
|
type Technologies = string[];
|
|
@@ -49,10 +42,6 @@ interface MonaCopilotProps extends EditorProps {
|
|
|
49
42
|
* etc.
|
|
50
43
|
*/
|
|
51
44
|
technologies?: Technologies;
|
|
52
|
-
/**
|
|
53
|
-
* The theme you want to use for the editor.
|
|
54
|
-
*/
|
|
55
|
-
theme?: Theme;
|
|
56
45
|
/**
|
|
57
46
|
* Helps to give more relevant completions based on the full context.
|
|
58
47
|
* You can include things like the contents/codes of other files in the same workspace.
|
|
@@ -74,8 +63,8 @@ interface CompletionMetadata {
|
|
|
74
63
|
filename: Filename | undefined;
|
|
75
64
|
technologies: Technologies | undefined;
|
|
76
65
|
externalContext: ExternalContext | undefined;
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
textAfterCursor: string;
|
|
67
|
+
textBeforeCursor: string;
|
|
79
68
|
editorState: {
|
|
80
69
|
completionMode: CompletionMode;
|
|
81
70
|
};
|
|
@@ -86,31 +75,27 @@ interface CopilotOptions {
|
|
|
86
75
|
}
|
|
87
76
|
|
|
88
77
|
/**
|
|
89
|
-
*
|
|
90
|
-
* and an API key, and provides a method to send a completion request to Groq API and return the completion.
|
|
91
|
-
*
|
|
92
|
-
* @param {string} apiKey - The Groq API key.
|
|
93
|
-
* @param {CopilotOptions} [options] - Optional parameters to configure the completion model,
|
|
94
|
-
* such as the model ID. Defaults to `llama` if not specified.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* const copilot = new Copilot(process.env.GROQ_API_KEY, {
|
|
99
|
-
* model: 'llama',
|
|
100
|
-
* });
|
|
101
|
-
* ```
|
|
78
|
+
* Copilot class for handling completions using the Groq API.
|
|
102
79
|
*/
|
|
103
80
|
declare class Copilot {
|
|
104
|
-
private apiKey;
|
|
81
|
+
private readonly apiKey;
|
|
82
|
+
/**
|
|
83
|
+
* Initializes the Copilot with an API key and optional configuration.
|
|
84
|
+
* @param {string} apiKey - The Groq API key.
|
|
85
|
+
* @param {CopilotOptions} [options] - Optional parameters to configure the completion model.
|
|
86
|
+
* @throws {Error} If the API key is not provided.
|
|
87
|
+
*/
|
|
105
88
|
constructor(apiKey: string, options?: CopilotOptions);
|
|
106
89
|
/**
|
|
107
90
|
* Sends a completion request to Groq API and returns the completion.
|
|
108
91
|
* @param {CompletionRequest} params - The metadata required to generate the completion.
|
|
109
|
-
* @returns {Promise<CompletionResponse>} The completed
|
|
92
|
+
* @returns {Promise<CompletionResponse>} The completed text snippet or an error.
|
|
110
93
|
*/
|
|
111
94
|
complete({ completionMetadata, }: CompletionRequest): Promise<CompletionResponse>;
|
|
95
|
+
private createRequestBody;
|
|
96
|
+
private createHeaders;
|
|
112
97
|
}
|
|
113
98
|
|
|
114
|
-
declare const MonaCopilot: ({ filename, endpoint, technologies,
|
|
99
|
+
declare const MonaCopilot: ({ filename, endpoint, technologies, externalContext, onMount, ...props }: MonaCopilotProps) => React.JSX.Element;
|
|
115
100
|
|
|
116
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type Endpoint, type MonaCopilotProps, type Technologies,
|
|
101
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type Endpoint, type MonaCopilotProps, type Technologies, MonaCopilot as default };
|
package/build/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";var ue=Object.create;var x=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var Ce=Object.getPrototypeOf,he=Object.prototype.hasOwnProperty;var Ee=(t,e)=>{for(var o in e)x(t,o,{get:e[o],enumerable:!0})},q=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of ge(e))!he.call(t,n)&&n!==o&&x(t,n,{get:()=>e[n],enumerable:!(r=fe(e,n))||r.enumerable});return t};var Te=(t,e,o)=>(o=t!=null?ue(Ce(t)):{},q(e||!t||!t.__esModule?x(o,"default",{value:t,enumerable:!0}):o,t)),Pe=t=>q(x({},"__esModule",{value:!0}),t);var Se={};Ee(Se,{Copilot:()=>b,DiffEditor:()=>g.DiffEditor,default:()=>de,loader:()=>g.loader,useMonaco:()=>g.useMonaco});module.exports=Pe(Se);var F=(t,e=1e3)=>{let o=null;return(...n)=>(o&&clearTimeout(o),new Promise((i,s)=>{o=setTimeout(()=>{t(...n).then(i).catch(s)},e)}))},w=(t,e)=>{let o={...e};for(let r in t)typeof t[r]=="object"&&!Array.isArray(t[r])?e[r]&&typeof e[r]=="object"&&!Array.isArray(e[r])?o[r]=w(t[r],e[r]):o[r]={...t[r]}:o[r]=t[r];return o},U=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`,A=t=>t.split("").reverse().join("");var y=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],j=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),f=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),G=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}),S=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var W=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},n=e==="POST"&&o.body?JSON.stringify(o.body):void 0,i=await fetch(t,{method:e,headers:r,body:n,signal:o.signal});if(!i.ok)throw new Error(`${o.error||"Network error"}: ${i.statusText}`);return i.json()},xe=(t,e)=>W(t,"GET",e),ye=(t,e,o)=>W(t,"POST",{...o,body:e}),M={GET:xe,POST:ye};var R=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,n=Math.min(o.length,e.length);for(let i=1;i<=n;i++){let s=e.slice(-i),l=o.slice(0,i);if(s===l)r=i;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 Me=25,E=[],H=(t,e)=>E.filter(o=>{let r=e.getValueInRange(o.range);return f(t,e)===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=>{E.length>=Me&&E.shift(),E.push(t)},V=()=>{E.length=0};var Y={scrollBeyondLastColumn:0,codeLens:!1,minimap:{enabled:!1},quickSuggestions:!1,folding:!1,foldingHighlight:!1,foldingImportsByDefault:!1,links:!1,fontSize:14,wordWrap:"on",automaticLayout:!0,formatOnPaste:!0,inlineSuggest:{enabled:!0,mode:void 0}};var z={llama:"llama3-70b-8192"},O="llama",J="https://api.groq.com/openai/v1/chat/completions";var Q=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var X=(t,e)=>{let o=y(t,e);return!!o&&!Q.has(o)},Z=(t,e)=>{let o=j(t,e).trim(),r=f(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var ee=(t,e,o,r)=>{let n=(t.match(/\n/g)||[]).length,i=G(t),s=y(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+n,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&n===0?o.column+i:i:o.column}},te=(t,e,o)=>new R(t,e).format(o),u=t=>({items:t,enableForwardStability:!0});var L=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!X(this.cursorPosition,this.model)&&!Z(this.cursorPosition,this.model)}};var I=class{static getModel(){return this.model}static setModel(e){this.model=e}};I.model=O;var $=I;var B=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 B(t);var T="<<CURSOR>>",oe=t=>t==="javascript"?"latest JavaScript":t,re=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"}},ne=t=>{let e=oe(t.language),o=re(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},Re=(t,e)=>{if(!t?.length&&!e)return"";let o=U(t),r=oe(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},ie=t=>{let{filename:e,language:o,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:s,externalContext:l}=t,c=re(n.completionMode),p=e?`the file named ${e}`:"a larger project",m=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${T}'. Your task is to assist with ${c}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":m+=`generate a completion to fill the middle of the code around '${T}'. Ensure the completion replaces '${T}' 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":m+=`provide the necessary completion for '${T}' 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}m+=` Output only the necessary completion code, without additional explanations or content.${Re(r,o)}`;let C=`${i}${T}${s}
|
|
4
|
+
|
|
5
|
+
`;return l&&l.length>0&&(C+=l.map(d=>`// Path: ${d.path}
|
|
6
|
+
${d.content}
|
|
7
|
+
`).join(`
|
|
8
|
+
`)),m+=`
|
|
9
|
+
|
|
10
|
+
<code>
|
|
11
|
+
${C}
|
|
12
|
+
</code>`,m.endsWith(".")?m:`${m}.`};var Oe="application/json",se=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:n,model:i,position:s,token:l})=>{let c=new AbortController;if(l.isCancellationRequested)return c.abort(),null;try{let{completion:p}=await M.POST(e,{completionMetadata:Le({filename:t,position:s,model:i,language:o,technologies:r,externalContext:n})},{headers:{"Content-Type":Oe},error:"Error while fetching completion item",signal:c.signal});return p||null}catch(p){return a(p).completionError("Error while fetching completion item"),null}},Le=({filename:t,position:e,model:o,language:r,technologies:n,externalContext:i})=>{let s=Ie(e,o),l=h(e,o),c=S(e,o);return{filename:t,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},Ie=(t,e)=>{let o=h(t,e),r=S(t,e);return o&&r?"fill-in-the-middle":"completion"};var b=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,$.setModel(o?.model||O)}async complete({completionMetadata:e}){try{let o=$.getModel(),r=this.createRequestBody(e,o),n=this.createHeaders(),i=await M.POST(J,r,{headers:n});if(!i.choices||i.choices.length===0)throw new Error("No completion choices received from API");return{completion:i.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:z[o],messages:[{role:"system",content:ne(e)},{role:"user",content:ie(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var le={conso:"le.log($1)","new P":`romise((resolve, reject) => {
|
|
13
|
+
$1
|
|
14
|
+
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
15
|
+
$1
|
|
16
|
+
}, $2)`,setInterval:`(() => {
|
|
17
|
+
$1
|
|
18
|
+
}, $2)`,"async f":`unction $1() {
|
|
19
|
+
$2
|
|
20
|
+
}`,"async (":`) => {
|
|
21
|
+
$1
|
|
22
|
+
}`,"async =>":` {
|
|
23
|
+
$1
|
|
24
|
+
}`,") =>":` {
|
|
25
|
+
$1
|
|
26
|
+
}`,"=>":` {
|
|
27
|
+
$1
|
|
28
|
+
}`,"new M":"ap","new W":"eakMap"};var ae=[{language:"javascript",snippets:le}];var v=class{constructor(){this.predictions=new Map,this.loadPredictions()}predict(e,o){try{let r=this.predictions.get(e);if(!r)return"";let n=A(o);return this.findMatchingPrediction(r,n)}catch(r){return a(r).predictionError("Error while predicting next text snippet"),""}}loadPredictions(){ae.forEach(e=>{this.predictions.set(e.language,e.snippets)})}findMatchingPrediction(e,o){for(let[r,n]of Object.entries(e))if(o.startsWith(A(r)))return n;return""}};var P=Te(require("react")),pe=require("@monaco-editor/react");var be=new v,ve=200,Ne=F(se,ve),we=async({monaco:t,model:e,position:o,context:r,token:n,hasCompletionBeenAccepted:i,onShowCompletion:s,options:l})=>{if(i)return u([]);let c=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new L(o,e).shouldProvideCompletions())return u([]);let m=H(o,e).map(d=>({insertText:d.completion,range:d.range}));if(m.length)return s(),u(m);if(n.isCancellationRequested)return u([]);let C=be.predict(l.language,e.getLineContent(o.lineNumber));if(C)return s(),u([{insertText:{snippet:C},range:p}]);try{let d=await Ne({...l,text:c,model:e,position:o,token:n});if(d){let N=te(e,o,d),k=ee(N,p,o,e);return K({completion:N,range:k,textBeforeCursorInLine:f(o,e)}),s(),u([{insertText:N,range:k}])}}catch(d){a(d).completionError("Failed to fetch completion item")}return u([])},me=we;var D=!1,_=!1,ce=({monaco:t,editor:e,...o})=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,i,s,l)=>me({monaco:t,model:n,position:i,context:s,token:l,hasCompletionBeenAccepted:D,onShowCompletion:()=>_=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(n=>{_&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?(D=!0,_=!1):D=!1}),()=>{r.dispose(),V()}}catch(r){a(r).editorError("Error while registering Copilot")}};var Ae=({filename:t,endpoint:e,technologies:o,externalContext:r,onMount:n,...i})=>{let s=P.default.useRef(),l=P.default.useCallback((c,p)=>{try{e&&i.language&&(s.current=ce({monaco:p,editor:c,filename:t,technologies:o,externalContext:r,endpoint:e,language:i.language}))}catch(m){a(m).monacopilotError("Error while registering copilot")}n?.(c,p)},[e,t,n,i.language,o,r]);return P.default.useEffect(()=>()=>{s.current?.()},[]),P.default.createElement(pe.Editor,{...i,onMount:l,options:w(i.options,Y)})},de=Ae;var g=require("@monaco-editor/react");0&&(module.exports={Copilot,DiffEditor,loader,useMonaco});
|
package/build/index.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var _=(t,e=1e3)=>{let o=null;return(...n)=>(o&&clearTimeout(o),new Promise((i,s)=>{o=setTimeout(()=>{t(...n).then(i).catch(s)},e)}))},b=(t,e)=>{let o={...e};for(let r in t)typeof t[r]=="object"&&!Array.isArray(t[r])?e[r]&&typeof e[r]=="object"&&!Array.isArray(e[r])?o[r]=b(t[r],e[r]):o[r]={...t[r]}:o[r]=t[r];return o},k=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`,v=t=>t.split("").reverse().join("");var T=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],q=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),f=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),F=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var C=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),N=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var U=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},n=e==="POST"&&o.body?JSON.stringify(o.body):void 0,i=await fetch(t,{method:e,headers:r,body:n,signal:o.signal});if(!i.ok)throw new Error(`${o.error||"Network error"}: ${i.statusText}`);return i.json()},ae=(t,e)=>U(t,"GET",e),me=(t,e,o)=>U(t,"POST",{...o,body:e}),P={GET:ae,POST:me};var x=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=C(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,n=Math.min(o.length,e.length);for(let i=1;i<=n;i++){let s=e.slice(-i),l=o.slice(0,i);if(s===l)r=i;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 ce=25,h=[],j=(t,e)=>h.filter(o=>{let r=e.getValueInRange(o.range);return f(t,e)===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}),G=t=>{h.length>=ce&&h.shift(),h.push(t)},W=()=>{h.length=0};var H={scrollBeyondLastColumn:0,codeLens:!1,minimap:{enabled:!1},quickSuggestions:!1,folding:!1,foldingHighlight:!1,foldingImportsByDefault:!1,links:!1,fontSize:14,wordWrap:"on",automaticLayout:!0,formatOnPaste:!0,inlineSuggest:{enabled:!0,mode:void 0}};var K={llama:"llama3-70b-8192"},y="llama",V="https://api.groq.com/openai/v1/chat/completions";var Y=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var z=(t,e)=>{let o=T(t,e);return!!o&&!Y.has(o)},J=(t,e)=>{let o=q(t,e).trim(),r=f(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var Q=(t,e,o,r)=>{let n=(t.match(/\n/g)||[]).length,i=F(t),s=T(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+n,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&n===0?o.column+i:i:o.column}},X=(t,e,o)=>new x(t,e).format(o),u=t=>({items:t,enableForwardStability:!0});var M=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!z(this.cursorPosition,this.model)&&!J(this.cursorPosition,this.model)}};var R=class{static getModel(){return this.model}static setModel(e){this.model=e}};R.model=y;var w=R;var A=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 A(t);var E="<<CURSOR>>",Z=t=>t==="javascript"?"latest JavaScript":t,ee=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"}},te=t=>{let e=Z(t.language),o=ee(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=k(t),r=Z(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},oe=t=>{let{filename:e,language:o,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:s,externalContext:l}=t,c=ee(n.completionMode),p=e?`the file named ${e}`:"a larger project",m=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${E}'. Your task is to assist with ${c}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":m+=`generate a completion to fill the middle of the code around '${E}'. Ensure the completion replaces '${E}' 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":m+=`provide the necessary completion for '${E}' 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}m+=` Output only the necessary completion code, without additional explanations or content.${pe(r,o)}`;let g=`${i}${E}${s}
|
|
4
|
+
|
|
5
|
+
`;return l&&l.length>0&&(g+=l.map(d=>`// Path: ${d.path}
|
|
6
|
+
${d.content}
|
|
7
|
+
`).join(`
|
|
8
|
+
`)),m+=`
|
|
9
|
+
|
|
10
|
+
<code>
|
|
11
|
+
${g}
|
|
12
|
+
</code>`,m.endsWith(".")?m:`${m}.`};var de="application/json",re=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:n,model:i,position:s,token:l})=>{let c=new AbortController;if(l.isCancellationRequested)return c.abort(),null;try{let{completion:p}=await P.POST(e,{completionMetadata:ue({filename:t,position:s,model:i,language:o,technologies:r,externalContext:n})},{headers:{"Content-Type":de},error:"Error while fetching completion item",signal:c.signal});return p||null}catch(p){return a(p).completionError("Error while fetching completion item"),null}},ue=({filename:t,position:e,model:o,language:r,technologies:n,externalContext:i})=>{let s=fe(e,o),l=C(e,o),c=N(e,o);return{filename:t,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},fe=(t,e)=>{let o=C(t,e),r=N(t,e);return o&&r?"fill-in-the-middle":"completion"};var S=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,w.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=w.getModel(),r=this.createRequestBody(e,o),n=this.createHeaders(),i=await P.POST(V,r,{headers:n});if(!i.choices||i.choices.length===0)throw new Error("No completion choices received from API");return{completion:i.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:K[o],messages:[{role:"system",content:te(e)},{role:"user",content:oe(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var ne={conso:"le.log($1)","new P":`romise((resolve, reject) => {
|
|
13
|
+
$1
|
|
14
|
+
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
15
|
+
$1
|
|
16
|
+
}, $2)`,setInterval:`(() => {
|
|
17
|
+
$1
|
|
18
|
+
}, $2)`,"async f":`unction $1() {
|
|
19
|
+
$2
|
|
20
|
+
}`,"async (":`) => {
|
|
21
|
+
$1
|
|
22
|
+
}`,"async =>":` {
|
|
23
|
+
$1
|
|
24
|
+
}`,") =>":` {
|
|
25
|
+
$1
|
|
26
|
+
}`,"=>":` {
|
|
27
|
+
$1
|
|
28
|
+
}`,"new M":"ap","new W":"eakMap"};var ie=[{language:"javascript",snippets:ne}];var O=class{constructor(){this.predictions=new Map,this.loadPredictions()}predict(e,o){try{let r=this.predictions.get(e);if(!r)return"";let n=v(o);return this.findMatchingPrediction(r,n)}catch(r){return a(r).predictionError("Error while predicting next text snippet"),""}}loadPredictions(){ie.forEach(e=>{this.predictions.set(e.language,e.snippets)})}findMatchingPrediction(e,o){for(let[r,n]of Object.entries(e))if(o.startsWith(v(r)))return n;return""}};import L from"react";import{Editor as Te}from"@monaco-editor/react";var ge=new O,Ce=200,he=_(re,Ce),Ee=async({monaco:t,model:e,position:o,context:r,token:n,hasCompletionBeenAccepted:i,onShowCompletion:s,options:l})=>{if(i)return u([]);let c=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new M(o,e).shouldProvideCompletions())return u([]);let m=j(o,e).map(d=>({insertText:d.completion,range:d.range}));if(m.length)return s(),u(m);if(n.isCancellationRequested)return u([]);let g=ge.predict(l.language,e.getLineContent(o.lineNumber));if(g)return s(),u([{insertText:{snippet:g},range:p}]);try{let d=await he({...l,text:c,model:e,position:o,token:n});if(d){let I=X(e,o,d),D=Q(I,p,o,e);return G({completion:I,range:D,textBeforeCursorInLine:f(o,e)}),s(),u([{insertText:I,range:D}])}}catch(d){a(d).completionError("Failed to fetch completion item")}return u([])},se=Ee;var $=!1,B=!1,le=({monaco:t,editor:e,...o})=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,i,s,l)=>se({monaco:t,model:n,position:i,context:s,token:l,hasCompletionBeenAccepted:$,onShowCompletion:()=>B=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(n=>{B&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?($=!0,B=!1):$=!1}),()=>{r.dispose(),W()}}catch(r){a(r).editorError("Error while registering Copilot")}};var Pe=({filename:t,endpoint:e,technologies:o,externalContext:r,onMount:n,...i})=>{let s=L.useRef(),l=L.useCallback((c,p)=>{try{e&&i.language&&(s.current=le({monaco:p,editor:c,filename:t,technologies:o,externalContext:r,endpoint:e,language:i.language}))}catch(m){a(m).monacopilotError("Error while registering copilot")}n?.(c,p)},[e,t,n,i.language,o,r]);return L.useEffect(()=>()=>{s.current?.()},[]),L.createElement(Te,{...i,onMount:l,options:b(i.options,H)})},xe=Pe;import{DiffEditor as Qt,useMonaco as Xt,loader as Zt}from"@monaco-editor/react";export{S as Copilot,Qt as DiffEditor,xe as default,Zt as loader,Xt as useMonaco};
|
package/package.json
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monacopilot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Extended Monaco Editor with AI auto-completion and new themes for React.",
|
|
5
|
-
"main": "./
|
|
6
|
-
"module": "./
|
|
7
|
-
"types": "./
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"module": "./build/index.mjs",
|
|
7
|
+
"types": "./build/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
|
-
"
|
|
9
|
+
"build"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"type-check": "tsc --noEmit",
|
|
13
12
|
"build": "tsup src/index.ts",
|
|
14
13
|
"dev": "tsup src/index.ts --watch",
|
|
15
14
|
"dev:test": "pnpm -C test dev",
|
|
16
15
|
"dev:docs": "pnpm -C docs dev",
|
|
16
|
+
"type-check": "tsc --noEmit",
|
|
17
17
|
"lint": "eslint . --ext .ts,.tsx --fix",
|
|
18
18
|
"lint:test": "pnpm -C test lint",
|
|
19
19
|
"format": "prettier --write .",
|
|
20
20
|
"pre-commit": "pnpm type-check && pnpm lint && pnpm lint:test",
|
|
21
|
-
"generate:themes": "node themes/_scripts/generate.mjs",
|
|
22
|
-
"build:themes": "npx lerna run build",
|
|
23
|
-
"publish:themes": "npx lerna publish --force-publish --no-private",
|
|
24
21
|
"release": "release-it"
|
|
25
22
|
},
|
|
26
23
|
"devDependencies": {
|
|
@@ -32,7 +29,6 @@
|
|
|
32
29
|
"eslint": "^8.57.0",
|
|
33
30
|
"eslint-plugin-react": "^7.34.1",
|
|
34
31
|
"groq-sdk": "^0.3.2",
|
|
35
|
-
"lerna": "^8.1.5",
|
|
36
32
|
"monaco-editor": "^0.50.0",
|
|
37
33
|
"prettier": "^3.2.5",
|
|
38
34
|
"react": "^18.2.0",
|
|
@@ -42,15 +38,17 @@
|
|
|
42
38
|
"typescript": "^5.4.3"
|
|
43
39
|
},
|
|
44
40
|
"keywords": [
|
|
45
|
-
"monaco",
|
|
46
41
|
"monaco-editor",
|
|
47
42
|
"react-monaco-editor",
|
|
48
43
|
"ai-monaco-editor",
|
|
49
44
|
"monacopilot",
|
|
50
|
-
"monaco-editor-react",
|
|
51
45
|
"ai-editor"
|
|
52
46
|
],
|
|
53
47
|
"homepage": "https://monacopilot.vercel.app",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "https://github.com/arshad-yaseen/monacopilot"
|
|
51
|
+
},
|
|
54
52
|
"license": "MIT",
|
|
55
53
|
"author": "Arshad Yaseen <m@arshadyaseen.com>",
|
|
56
54
|
"peerDependencies": {
|