monacopilot 0.10.6 → 0.10.8
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 +48 -12
- package/build/index.d.mts +21 -2
- package/build/index.d.ts +21 -2
- package/build/index.js +1 -1
- package/build/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,12 +19,15 @@
|
|
|
19
19
|
- [Register Completion Options](#register-completion-options)
|
|
20
20
|
- [Get Completions in Real-Time](#get-completions-in-real-time)
|
|
21
21
|
- [Manually Trigger Completions](#manually-trigger-completions)
|
|
22
|
+
- [Trigger Completions with a Keyboard Shortcut](#trigger-completions-with-a-keyboard-shortcut)
|
|
23
|
+
- [Trigger Completions with an Editor Action](#trigger-completions-with-an-editor-action)
|
|
22
24
|
- [External Context](#external-context)
|
|
23
25
|
- [Filename](#filename)
|
|
24
26
|
- [Completions for Specific Technologies](#completions-for-specific-technologies)
|
|
25
27
|
- [Max Context Lines](#max-context-lines)
|
|
26
28
|
- [Copilot Options](#copilot-options)
|
|
27
29
|
- [Changing the Provider and Model](#changing-the-provider-and-model)
|
|
30
|
+
- [Dynamically Updating Provider and Model](#dynamically-updating-provider-and-model)
|
|
28
31
|
- [Custom Model](#custom-model)
|
|
29
32
|
- [Completion Request Options](#completion-request-options)
|
|
30
33
|
- [Custom Headers](#custom-headers)
|
|
@@ -111,6 +114,8 @@ registerCompletion(monaco, editor, {
|
|
|
111
114
|
});
|
|
112
115
|
```
|
|
113
116
|
|
|
117
|
+
The `registerCompletion` function returns a `completion` object with useful methods such as `trigger` and `dispose`. The `trigger` method is explained later in this document. The `dispose` method should be used to clean up resources when the completion object is no longer needed. For instance, in a React component, you can call `completion.dispose()` within the `useEffect` cleanup function to ensure proper disposal when the component unmounts.
|
|
118
|
+
|
|
114
119
|
🎉 Congratulations! The AI auto-completion is now connected to the Monaco Editor. Start typing and see completions in the editor.
|
|
115
120
|
|
|
116
121
|
## Register Completion Options
|
|
@@ -121,15 +126,15 @@ The `trigger` option determines when the completion service provides code comple
|
|
|
121
126
|
|
|
122
127
|
```javascript
|
|
123
128
|
registerCompletion(monaco, editor, {
|
|
124
|
-
// ...other options
|
|
125
129
|
trigger: 'onTyping',
|
|
126
130
|
});
|
|
127
131
|
```
|
|
128
132
|
|
|
129
|
-
| Trigger | Description
|
|
130
|
-
| -------------------- |
|
|
131
|
-
| `'onIdle'` (default) |
|
|
132
|
-
| `'onTyping'` |
|
|
133
|
+
| Trigger | Description | Notes |
|
|
134
|
+
| -------------------- | --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| `'onIdle'` (default) | Provides completions after a brief pause in typing. | This approach is less resource-intensive, as it only initiates a request when the editor is idle. |
|
|
136
|
+
| `'onTyping'` | Provides completions in real-time as you type. | Best suited for models with low response latency, such as Groq models. This trigger mode initiates additional background requests to deliver real-time suggestions, a method known as predictive caching. |
|
|
137
|
+
| `'onDemand'` | Does not provide completions automatically. | Completions are triggered manually using the `trigger` function from the `registerCompletion` return. This allows for precise control over when completions are provided. |
|
|
133
138
|
|
|
134
139
|
[OnTyping Demo](https://github.com/user-attachments/assets/22c2ce44-334c-4963-b853-01b890b8e39f)
|
|
135
140
|
|
|
@@ -151,11 +156,12 @@ completion.trigger();
|
|
|
151
156
|
|
|
152
157
|
To set up manual triggering, configure the `trigger` option to `'onDemand'`. This disables automatic completions, allowing you to call the `completion.trigger()` method explicitly when needed.
|
|
153
158
|
|
|
154
|
-
|
|
159
|
+
#### Trigger Completions with a Keyboard Shortcut
|
|
160
|
+
|
|
161
|
+
You can set up completions to trigger when the `Ctrl+Shift+Space` keyboard shortcut is pressed.
|
|
155
162
|
|
|
156
163
|
```javascript
|
|
157
164
|
const completion = registerCompletion(monaco, editor, {
|
|
158
|
-
// ...other options
|
|
159
165
|
trigger: 'onDemand',
|
|
160
166
|
});
|
|
161
167
|
|
|
@@ -167,13 +173,36 @@ monaco.editor.addCommand(
|
|
|
167
173
|
);
|
|
168
174
|
```
|
|
169
175
|
|
|
176
|
+
#### Trigger Completions with an Editor Action
|
|
177
|
+
|
|
178
|
+
You can add a custom editor action to trigger completions manually.
|
|
179
|
+
|
|
180
|
+

|
|
181
|
+
|
|
182
|
+
```javascript
|
|
183
|
+
const completion = registerCompletion(monaco, editor, {
|
|
184
|
+
trigger: 'onDemand',
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
monaco.editor.addEditorAction({
|
|
188
|
+
id: 'monacopilot.triggerCompletion',
|
|
189
|
+
label: 'Complete Code',
|
|
190
|
+
contextMenuGroupId: 'navigation',
|
|
191
|
+
keybindings: [
|
|
192
|
+
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Space,
|
|
193
|
+
],
|
|
194
|
+
run: () => {
|
|
195
|
+
completion.trigger();
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
170
200
|
### External Context
|
|
171
201
|
|
|
172
202
|
Enhance the accuracy and relevance of Copilot's completions by providing additional code context from your workspace.
|
|
173
203
|
|
|
174
204
|
```javascript
|
|
175
205
|
registerCompletion(monaco, editor, {
|
|
176
|
-
// ...other options
|
|
177
206
|
externalContext: [
|
|
178
207
|
{
|
|
179
208
|
path: './utils.js',
|
|
@@ -192,7 +221,6 @@ Specify the name of the file being edited to receive more contextually relevant
|
|
|
192
221
|
|
|
193
222
|
```javascript
|
|
194
223
|
registerCompletion(monaco, editor, {
|
|
195
|
-
// ...other options
|
|
196
224
|
filename: 'utils.js', // e.g., "index.js", "utils/objects.js"
|
|
197
225
|
});
|
|
198
226
|
```
|
|
@@ -205,7 +233,6 @@ Enable completions tailored to specific technologies by using the `technologies`
|
|
|
205
233
|
|
|
206
234
|
```javascript
|
|
207
235
|
registerCompletion(monaco, editor, {
|
|
208
|
-
// ...other options
|
|
209
236
|
technologies: ['react', 'next.js', 'tailwindcss'],
|
|
210
237
|
});
|
|
211
238
|
```
|
|
@@ -220,7 +247,6 @@ For example, if there's a chance that the code in your editor may exceed `500+ l
|
|
|
220
247
|
|
|
221
248
|
```javascript
|
|
222
249
|
registerCompletion(monaco, editor, {
|
|
223
|
-
// ...other options
|
|
224
250
|
maxContextLines: 80,
|
|
225
251
|
});
|
|
226
252
|
```
|
|
@@ -250,6 +276,17 @@ There are other providers and models available. Here is a list:
|
|
|
250
276
|
| OpenAI | `gpt-4o`, `gpt-4o-mini`, `o1-preview`, `o1-mini` |
|
|
251
277
|
| Anthropic | `claude-3.5-Sonnet`, `claude-3-opus`, `claude-3-sonnet`, `claude-3-haiku` |
|
|
252
278
|
|
|
279
|
+
#### Dynamically Updating Provider and Model
|
|
280
|
+
|
|
281
|
+
You can change the provider and model at runtime using the `setModel` method.
|
|
282
|
+
|
|
283
|
+
```javascript
|
|
284
|
+
copilot.setModel({
|
|
285
|
+
provider: 'anthropic',
|
|
286
|
+
model: 'claude-3-haiku',
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
253
290
|
### Custom Model
|
|
254
291
|
|
|
255
292
|
You can use a custom AI model that isn't built into Monacopilot by setting up a `model` when you create a new Copilot. This feature lets you connect to AI models from other services or your own custom-built models.
|
|
@@ -312,7 +349,6 @@ You can add custom headers to the provider's completion requests. For example, i
|
|
|
312
349
|
```javascript
|
|
313
350
|
copilot.complete({
|
|
314
351
|
options: {
|
|
315
|
-
// ...other options
|
|
316
352
|
headers: {
|
|
317
353
|
'X-Custom-Header': 'custom-value',
|
|
318
354
|
},
|
package/build/index.d.mts
CHANGED
|
@@ -54,6 +54,20 @@ type CustomCopilotModel = {
|
|
|
54
54
|
*/
|
|
55
55
|
transformResponse: CustomCopilotModelTransformResponse;
|
|
56
56
|
};
|
|
57
|
+
type SetModelOptions = {
|
|
58
|
+
/**
|
|
59
|
+
* The provider to set (e.g., 'openai', 'anthropic', 'groq').
|
|
60
|
+
*/
|
|
61
|
+
provider: CopilotProvider;
|
|
62
|
+
/**
|
|
63
|
+
* The model to use for copilot AI requests.
|
|
64
|
+
* This can be either:
|
|
65
|
+
* 1. A predefined model name (e.g. 'claude-3-opus'): Use this option if you want to use a model that is built into Monacopilot.
|
|
66
|
+
* If you choose this option, also set the `provider` property to the corresponding provider of the model.
|
|
67
|
+
* 2. A custom model configuration object: Use this option if you want to use a AI model from a third-party service or your own custom model.
|
|
68
|
+
*/
|
|
69
|
+
model: CopilotModel | CustomCopilotModel;
|
|
70
|
+
};
|
|
57
71
|
type CustomCopilotModelConfig = (apiKey: string, prompt: {
|
|
58
72
|
system: string;
|
|
59
73
|
user: string;
|
|
@@ -251,8 +265,8 @@ interface CompletionMetadata {
|
|
|
251
265
|
|
|
252
266
|
declare class Copilot {
|
|
253
267
|
private readonly apiKey;
|
|
254
|
-
private
|
|
255
|
-
private
|
|
268
|
+
private provider;
|
|
269
|
+
private model;
|
|
256
270
|
/**
|
|
257
271
|
* Initializes the Copilot instance with an API key and optional configuration.
|
|
258
272
|
* @param apiKey - The API key for the chosen provider.
|
|
@@ -270,6 +284,11 @@ declare class Copilot {
|
|
|
270
284
|
* @returns A promise resolving to the completion response or an error.
|
|
271
285
|
*/
|
|
272
286
|
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
287
|
+
/**
|
|
288
|
+
* Sets the model and provider for the Copilot.
|
|
289
|
+
* @param options - The options for setting the model and provider.
|
|
290
|
+
*/
|
|
291
|
+
setModel({ model, provider }: SetModelOptions): void;
|
|
273
292
|
}
|
|
274
293
|
|
|
275
294
|
/**
|
package/build/index.d.ts
CHANGED
|
@@ -54,6 +54,20 @@ type CustomCopilotModel = {
|
|
|
54
54
|
*/
|
|
55
55
|
transformResponse: CustomCopilotModelTransformResponse;
|
|
56
56
|
};
|
|
57
|
+
type SetModelOptions = {
|
|
58
|
+
/**
|
|
59
|
+
* The provider to set (e.g., 'openai', 'anthropic', 'groq').
|
|
60
|
+
*/
|
|
61
|
+
provider: CopilotProvider;
|
|
62
|
+
/**
|
|
63
|
+
* The model to use for copilot AI requests.
|
|
64
|
+
* This can be either:
|
|
65
|
+
* 1. A predefined model name (e.g. 'claude-3-opus'): Use this option if you want to use a model that is built into Monacopilot.
|
|
66
|
+
* If you choose this option, also set the `provider` property to the corresponding provider of the model.
|
|
67
|
+
* 2. A custom model configuration object: Use this option if you want to use a AI model from a third-party service or your own custom model.
|
|
68
|
+
*/
|
|
69
|
+
model: CopilotModel | CustomCopilotModel;
|
|
70
|
+
};
|
|
57
71
|
type CustomCopilotModelConfig = (apiKey: string, prompt: {
|
|
58
72
|
system: string;
|
|
59
73
|
user: string;
|
|
@@ -251,8 +265,8 @@ interface CompletionMetadata {
|
|
|
251
265
|
|
|
252
266
|
declare class Copilot {
|
|
253
267
|
private readonly apiKey;
|
|
254
|
-
private
|
|
255
|
-
private
|
|
268
|
+
private provider;
|
|
269
|
+
private model;
|
|
256
270
|
/**
|
|
257
271
|
* Initializes the Copilot instance with an API key and optional configuration.
|
|
258
272
|
* @param apiKey - The API key for the chosen provider.
|
|
@@ -270,6 +284,11 @@ declare class Copilot {
|
|
|
270
284
|
* @returns A promise resolving to the completion response or an error.
|
|
271
285
|
*/
|
|
272
286
|
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
287
|
+
/**
|
|
288
|
+
* Sets the model and provider for the Copilot.
|
|
289
|
+
* @param options - The options for setting the model and provider.
|
|
290
|
+
*/
|
|
291
|
+
setModel({ model, provider }: SetModelOptions): void;
|
|
273
292
|
}
|
|
274
293
|
|
|
275
294
|
/**
|
package/build/index.js
CHANGED
|
@@ -38,7 +38,7 @@ ${p}
|
|
|
38
38
|
${s}
|
|
39
39
|
${m}
|
|
40
40
|
</task>
|
|
41
|
-
`};function j(e){return{system:ye(e),user:Oe(e)}}var ne={"claude-3.5-sonnet":8192,"claude-3-opus":4096,"claude-3-haiku":4096,"claude-3-sonnet":4096};var Re={createRequestBody:(e,t)=>{let n=e==="o1-preview"||e==="o1-mini"?[{role:"user",content:t.user}]:[{role:"system",content:t.system},{role:"user",content:t.user}];return{model:K(e),temperature:M,messages:n}},createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Me={createRequestBody:(e,t)=>({model:K(e),temperature:M,messages:[{role:"system",content:t.system},{role:"user",content:t.user}]}),createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Ie={createRequestBody:(e,t)=>({model:K(e),temperature:M,system:t.system,messages:[{role:"user",content:t.user}],max_tokens:be(e)}),createHeaders:e=>({"Content-Type":"application/json","x-api-key":e,"anthropic-version":"2023-06-01"}),parseCompletion:e=>!e.content||typeof e.content!="string"?null:e.content},G={openai:Re,groq:Me,anthropic:Ie},re=(e,t,o)=>G[t].createRequestBody(e,o),ie=(e,t)=>G[t].createHeaders(e),se=(e,t)=>G[t].parseCompletion(e),K=e=>X[e],le=e=>Z[e],be=e=>ne[e]||4096;var L=class{constructor(t,o={}){if(!t)throw new Error("Please provide an API key.");this.apiKey=t,this.provider=o.provider??z,this.model=o.model??J,this.validateInputs()}validateInputs(){if(!q.includes(this.provider))throw new Error(`The provider "${this.provider}" is not supported. Please choose a supported provider: ${y(q)}. If you're using a custom model, you don't need to specify a provider.`);if(typeof this.model=="string"&&!F[this.provider].includes(this.model)){let t=y(F[this.provider]);throw new Error(`Model "${this.model}" is not supported by the "${this.provider}" provider. Supported models: ${t}`)}}async complete(t){let{body:o,options:n}=t,{completionMetadata:r}=o,{headers:i={},customPrompt:l}=n??{},c=j(r),a=l?{...c,...l(r)}:c,s=le(this.provider),m,u=ie(this.apiKey,this.provider);if(typeof this.model=="object"&&"config"in this.model){let p=this.model.config(this.apiKey,a);s=p.endpoint??s,m=p.body??{},u={...u,...p.headers}}else m=re(this.model,this.provider,a);u={...u,...i};try{let p=await v.POST(s,m,{headers:u}),d;if(typeof this.model=="object"&&"transformResponse"in this.model){let C=this.model.transformResponse(p);d={completion:C.text??C.completion}}else d={completion:se(p,this.provider)};return d}catch(p){return{error:g(p,"COPILOT_COMPLETION_FETCH_ERROR").message,completion:null}}}};var _=class e{constructor(t){this.formattedCompletion="";this.formattedCompletion=t}static create(t){return new e(t)}setCompletion(t){return this.formattedCompletion=t,this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}removeMarkdownCodeSyntax(){return this.formattedCompletion=this.removeMarkdownCodeBlocks(this.formattedCompletion),this}removeMarkdownCodeBlocks(t){let o=/```[\s\S]*?```/g,n=t,r;for(;(r=o.exec(t))!==null;){let i=r[0],l=i.split(`
|
|
41
|
+
`};function j(e){return{system:ye(e),user:Oe(e)}}var ne={"claude-3.5-sonnet":8192,"claude-3-opus":4096,"claude-3-haiku":4096,"claude-3-sonnet":4096};var Re={createRequestBody:(e,t)=>{let n=e==="o1-preview"||e==="o1-mini"?[{role:"user",content:t.user}]:[{role:"system",content:t.system},{role:"user",content:t.user}];return{model:K(e),temperature:M,messages:n}},createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Me={createRequestBody:(e,t)=>({model:K(e),temperature:M,messages:[{role:"system",content:t.system},{role:"user",content:t.user}]}),createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Ie={createRequestBody:(e,t)=>({model:K(e),temperature:M,system:t.system,messages:[{role:"user",content:t.user}],max_tokens:be(e)}),createHeaders:e=>({"Content-Type":"application/json","x-api-key":e,"anthropic-version":"2023-06-01"}),parseCompletion:e=>!e.content||typeof e.content!="string"?null:e.content},G={openai:Re,groq:Me,anthropic:Ie},re=(e,t,o)=>G[t].createRequestBody(e,o),ie=(e,t)=>G[t].createHeaders(e),se=(e,t)=>G[t].parseCompletion(e),K=e=>X[e],le=e=>Z[e],be=e=>ne[e]||4096;var L=class{constructor(t,o={}){if(!t)throw new Error("Please provide an API key.");this.apiKey=t,this.provider=o.provider??z,this.model=o.model??J,this.validateInputs()}validateInputs(){if(!q.includes(this.provider))throw new Error(`The provider "${this.provider}" is not supported. Please choose a supported provider: ${y(q)}. If you're using a custom model, you don't need to specify a provider.`);if(typeof this.model=="string"&&!F[this.provider].includes(this.model)){let t=y(F[this.provider]);throw new Error(`Model "${this.model}" is not supported by the "${this.provider}" provider. Supported models: ${t}`)}}async complete(t){let{body:o,options:n}=t,{completionMetadata:r}=o,{headers:i={},customPrompt:l}=n??{},c=j(r),a=l?{...c,...l(r)}:c,s=le(this.provider),m,u=ie(this.apiKey,this.provider);if(typeof this.model=="object"&&"config"in this.model){let p=this.model.config(this.apiKey,a);s=p.endpoint??s,m=p.body??{},u={...u,...p.headers}}else m=re(this.model,this.provider,a);u={...u,...i};try{let p=await v.POST(s,m,{headers:u}),d;if(typeof this.model=="object"&&"transformResponse"in this.model){let C=this.model.transformResponse(p);d={completion:C.text??C.completion}}else d={completion:se(p,this.provider)};return d}catch(p){return{error:g(p,"COPILOT_COMPLETION_FETCH_ERROR").message,completion:null}}}setModel({model:t,provider:o}){this.model=t,this.provider=o,this.validateInputs()}};var _=class e{constructor(t){this.formattedCompletion="";this.formattedCompletion=t}static create(t){return new e(t)}setCompletion(t){return this.formattedCompletion=t,this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}removeMarkdownCodeSyntax(){return this.formattedCompletion=this.removeMarkdownCodeBlocks(this.formattedCompletion),this}removeMarkdownCodeBlocks(t){let o=/```[\s\S]*?```/g,n=t,r;for(;(r=o.exec(t))!==null;){let i=r[0],l=i.split(`
|
|
42
42
|
`).slice(1,-1).join(`
|
|
43
43
|
`);n=n.replace(i,l)}return n.trim()}removeExcessiveNewlines(){return this.formattedCompletion=this.formattedCompletion.replace(/\n{3,}/g,`
|
|
44
44
|
|
package/build/index.mjs
CHANGED
|
@@ -38,7 +38,7 @@ ${p}
|
|
|
38
38
|
${s}
|
|
39
39
|
${m}
|
|
40
40
|
</task>
|
|
41
|
-
`};function U(e){return{system:Ce(e),user:ge(e)}}var te={"claude-3.5-sonnet":8192,"claude-3-opus":4096,"claude-3-haiku":4096,"claude-3-sonnet":4096};var he={createRequestBody:(e,t)=>{let n=e==="o1-preview"||e==="o1-mini"?[{role:"user",content:t.user}]:[{role:"system",content:t.system},{role:"user",content:t.user}];return{model:j(e),temperature:M,messages:n}},createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},fe={createRequestBody:(e,t)=>({model:j(e),temperature:M,messages:[{role:"system",content:t.system},{role:"user",content:t.user}]}),createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Ee={createRequestBody:(e,t)=>({model:j(e),temperature:M,system:t.system,messages:[{role:"user",content:t.user}],max_tokens:xe(e)}),createHeaders:e=>({"Content-Type":"application/json","x-api-key":e,"anthropic-version":"2023-06-01"}),parseCompletion:e=>!e.content||typeof e.content!="string"?null:e.content},V={openai:he,groq:fe,anthropic:Ee},oe=(e,t,o)=>V[t].createRequestBody(e,o),ne=(e,t)=>V[t].createHeaders(e),re=(e,t)=>V[t].parseCompletion(e),j=e=>W[e],ie=e=>J[e],xe=e=>te[e]||4096;var G=class{constructor(t,o={}){if(!t)throw new Error("Please provide an API key.");this.apiKey=t,this.provider=o.provider??X,this.model=o.model??Y,this.validateInputs()}validateInputs(){if(!k.includes(this.provider))throw new Error(`The provider "${this.provider}" is not supported. Please choose a supported provider: ${y(k)}. If you're using a custom model, you don't need to specify a provider.`);if(typeof this.model=="string"&&!H[this.provider].includes(this.model)){let t=y(H[this.provider]);throw new Error(`Model "${this.model}" is not supported by the "${this.provider}" provider. Supported models: ${t}`)}}async complete(t){let{body:o,options:n}=t,{completionMetadata:r}=o,{headers:i={},customPrompt:l}=n??{},c=U(r),a=l?{...c,...l(r)}:c,s=ie(this.provider),m,u=ne(this.apiKey,this.provider);if(typeof this.model=="object"&&"config"in this.model){let p=this.model.config(this.apiKey,a);s=p.endpoint??s,m=p.body??{},u={...u,...p.headers}}else m=oe(this.model,this.provider,a);u={...u,...i};try{let p=await v.POST(s,m,{headers:u}),d;if(typeof this.model=="object"&&"transformResponse"in this.model){let C=this.model.transformResponse(p);d={completion:C.text??C.completion}}else d={completion:re(p,this.provider)};return d}catch(p){return{error:g(p,"COPILOT_COMPLETION_FETCH_ERROR").message,completion:null}}}};var L=class e{constructor(t){this.formattedCompletion="";this.formattedCompletion=t}static create(t){return new e(t)}setCompletion(t){return this.formattedCompletion=t,this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}removeMarkdownCodeSyntax(){return this.formattedCompletion=this.removeMarkdownCodeBlocks(this.formattedCompletion),this}removeMarkdownCodeBlocks(t){let o=/```[\s\S]*?```/g,n=t,r;for(;(r=o.exec(t))!==null;){let i=r[0],l=i.split(`
|
|
41
|
+
`};function U(e){return{system:Ce(e),user:ge(e)}}var te={"claude-3.5-sonnet":8192,"claude-3-opus":4096,"claude-3-haiku":4096,"claude-3-sonnet":4096};var he={createRequestBody:(e,t)=>{let n=e==="o1-preview"||e==="o1-mini"?[{role:"user",content:t.user}]:[{role:"system",content:t.system},{role:"user",content:t.user}];return{model:j(e),temperature:M,messages:n}},createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},fe={createRequestBody:(e,t)=>({model:j(e),temperature:M,messages:[{role:"system",content:t.system},{role:"user",content:t.user}]}),createHeaders:e=>({"Content-Type":"application/json",Authorization:`Bearer ${e}`}),parseCompletion:e=>e.choices?.length?e.choices[0].message.content:null},Ee={createRequestBody:(e,t)=>({model:j(e),temperature:M,system:t.system,messages:[{role:"user",content:t.user}],max_tokens:xe(e)}),createHeaders:e=>({"Content-Type":"application/json","x-api-key":e,"anthropic-version":"2023-06-01"}),parseCompletion:e=>!e.content||typeof e.content!="string"?null:e.content},V={openai:he,groq:fe,anthropic:Ee},oe=(e,t,o)=>V[t].createRequestBody(e,o),ne=(e,t)=>V[t].createHeaders(e),re=(e,t)=>V[t].parseCompletion(e),j=e=>W[e],ie=e=>J[e],xe=e=>te[e]||4096;var G=class{constructor(t,o={}){if(!t)throw new Error("Please provide an API key.");this.apiKey=t,this.provider=o.provider??X,this.model=o.model??Y,this.validateInputs()}validateInputs(){if(!k.includes(this.provider))throw new Error(`The provider "${this.provider}" is not supported. Please choose a supported provider: ${y(k)}. If you're using a custom model, you don't need to specify a provider.`);if(typeof this.model=="string"&&!H[this.provider].includes(this.model)){let t=y(H[this.provider]);throw new Error(`Model "${this.model}" is not supported by the "${this.provider}" provider. Supported models: ${t}`)}}async complete(t){let{body:o,options:n}=t,{completionMetadata:r}=o,{headers:i={},customPrompt:l}=n??{},c=U(r),a=l?{...c,...l(r)}:c,s=ie(this.provider),m,u=ne(this.apiKey,this.provider);if(typeof this.model=="object"&&"config"in this.model){let p=this.model.config(this.apiKey,a);s=p.endpoint??s,m=p.body??{},u={...u,...p.headers}}else m=oe(this.model,this.provider,a);u={...u,...i};try{let p=await v.POST(s,m,{headers:u}),d;if(typeof this.model=="object"&&"transformResponse"in this.model){let C=this.model.transformResponse(p);d={completion:C.text??C.completion}}else d={completion:re(p,this.provider)};return d}catch(p){return{error:g(p,"COPILOT_COMPLETION_FETCH_ERROR").message,completion:null}}}setModel({model:t,provider:o}){this.model=t,this.provider=o,this.validateInputs()}};var L=class e{constructor(t){this.formattedCompletion="";this.formattedCompletion=t}static create(t){return new e(t)}setCompletion(t){return this.formattedCompletion=t,this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}removeMarkdownCodeSyntax(){return this.formattedCompletion=this.removeMarkdownCodeBlocks(this.formattedCompletion),this}removeMarkdownCodeBlocks(t){let o=/```[\s\S]*?```/g,n=t,r;for(;(r=o.exec(t))!==null;){let i=r[0],l=i.split(`
|
|
42
42
|
`).slice(1,-1).join(`
|
|
43
43
|
`);n=n.replace(i,l)}return n.trim()}removeExcessiveNewlines(){return this.formattedCompletion=this.formattedCompletion.replace(/\n{3,}/g,`
|
|
44
44
|
|