monacopilot 0.11.7 → 0.11.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 CHANGED
@@ -11,29 +11,28 @@
11
11
  ### Table of Contents
12
12
 
13
13
  - [Examples](#examples)
14
- - [Inline Completions](#inline-completions)
15
- - [Installation](#installation)
16
- - [Usage](#usage)
17
- - [API Handler](#api-handler)
18
- - [Register Completion with the Monaco Editor](#register-completion-with-the-monaco-editor)
19
- - [Register Completion Options](#register-completion-options)
20
- - [Get Completions in Real-Time](#get-completions-in-real-time)
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)
24
- - [Multi-File Context](#multi-file-context)
25
- - [Filename](#filename)
26
- - [Completions for Specific Technologies](#completions-for-specific-technologies)
27
- - [Max Context Lines](#max-context-lines)
28
- - [Handling Errors](#handling-errors)
29
- - [Custom Request Handler](#custom-request-handler)
14
+ - [Demo](#demo)
15
+ - [Installation](#installation)
16
+ - [Usage](#usage)
17
+ - [API Handler](#api-handler)
18
+ - [Register Completion with the Monaco Editor](#register-completion-with-the-monaco-editor)
19
+ - [Register Completion Options](#register-completion-options)
20
+ - [Get Completions in Real-Time](#get-completions-in-real-time)
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)
24
+ - [Multi-File Context](#multi-file-context)
25
+ - [Filename](#filename)
26
+ - [Completions for Specific Technologies](#completions-for-specific-technologies)
27
+ - [Max Context Lines](#max-context-lines)
28
+ - [Handling Errors](#handling-errors)
29
+ - [Custom Request Handler](#custom-request-handler)
30
30
  - [Copilot Options](#copilot-options)
31
31
  - [Changing the Provider and Model](#changing-the-provider-and-model)
32
32
  - [Custom Model](#custom-model)
33
33
  - [Completion Request Options](#completion-request-options)
34
34
  - [Custom Headers for AI Model Requests](#custom-headers-for-ai-model-requests)
35
35
  - [Using a Different Language for the API Handler](#using-a-different-language-for-the-api-handler)
36
- - [Select and Modify](#select-and-modify)
37
36
  - [Contributing](#contributing)
38
37
 
39
38
  ### Examples
@@ -46,9 +45,7 @@ Here are some examples of how to integrate Monacopilot into your project:
46
45
  - [Remix](https://github.com/arshad-yaseen/monacopilot/tree/main/examples/remix)
47
46
  - [Vue](https://github.com/arshad-yaseen/monacopilot/tree/main/examples/vue)
48
47
 
49
- ## Inline Completions
50
-
51
- Monacopilot provides inline completions, offering real-time, AI-powered, context-aware code suggestions as you type.
48
+ ### Demo
52
49
 
53
50
  [Inline Completions Demo Video](https://github.com/user-attachments/assets/f2ec4ae1-f658-4002-af9c-c6b1bbad70d9)
54
51
 
@@ -510,7 +507,7 @@ copilot.complete({
510
507
 
511
508
  #### Parameters
512
509
 
513
- The `customPrompt` function receives a `metadata` object, which contains information about the current editor state and can be used to tailor the prompt.
510
+ The `customPrompt` function receives a `completionMetadata` object, which contains information about the current editor state and can be used to tailor the prompt.
514
511
 
515
512
  ##### Completion Metadata
516
513
 
@@ -533,7 +530,7 @@ The `editorState.completionMode` can be one of the following:
533
530
  | `complete` | Indicates that there is a character after the cursor but not immediately. In this mode, the AI will generate content to complete the text from the cursor position. |
534
531
  | `continue` | Indicates that there is no character after the cursor. In this mode, the AI will generate content to continue the text from the cursor position. |
535
532
 
536
- For additional `metadata` needs, please [open an issue](https://github.com/arshad-yaseen/monacopilot/issues/new).
533
+ For additional `completionMetadata` needs, please [open an issue](https://github.com/arshad-yaseen/monacopilot/issues/new).
537
534
 
538
535
  The `customPrompt` function should return an object with two properties:
539
536
 
@@ -600,7 +597,7 @@ Check out the [prompt.ts](https://github.com/arshad-yaseen/monacopilot/blob/main
600
597
 
601
598
  ### Metadata Overview
602
599
 
603
- The request body's `metadata` object contains essential information for crafting a prompt for the AI model to generate accurate completions. See the [Completion Metadata](#completion-metadata) section for more details.
600
+ The request body's `completionMetadata` object contains essential information for crafting a prompt for the AI model to generate accurate completions. See the [Completion Metadata](#completion-metadata) section for more details.
604
601
 
605
602
  ### Example Implementation (Python with FastAPI)
606
603
 
@@ -615,7 +612,7 @@ app = FastAPI()
615
612
  async def handle_completion(request: Request):
616
613
  try:
617
614
  body = await request.json()
618
- metadata = body['metadata']
615
+ metadata = body['completionMetadata']
619
616
 
620
617
  prompt = f"""Please complete the following {metadata['language']} code:
621
618
 
@@ -649,12 +646,6 @@ registerCompletion(monaco, editor, {
649
646
  });
650
647
  ```
651
648
 
652
- ## Select and Modify
653
-
654
- This feature allows you to edit the code by selecting the part you want to modify and ask AI to modify it.
655
-
656
- This feature is coming soon™️.
657
-
658
649
  ## Contributing
659
650
 
660
651
  For guidelines on contributing, please read the [contributing guide](https://github.com/arshad-yaseen/monacopilot/blob/main/CONTRIBUTING.md).
package/build/index.d.mts CHANGED
@@ -1,63 +1,6 @@
1
1
  import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
2
2
  import * as monaco_editor from 'monaco-editor';
3
3
 
4
- type CurrentFileName = string;
5
- type Technologies = string[];
6
- type RelatedFile = {
7
- /**
8
- * The relative path from the current editing code in the editor to an external file.
9
- *
10
- * Examples:
11
- * - To include a file `utils.js` in the same directory, set as `./utils.js`.
12
- * - To include a file `utils.js` in the parent directory, set as `../utils.js`.
13
- * - To include a file `utils.js` in the child directory, set as `./child/utils.js`.
14
- */
15
- path: string;
16
- /**
17
- * The content of the external file as a string.
18
- */
19
- content: string;
20
- };
21
- type Context = {
22
- /**
23
- * The programming language of the current file being edited.
24
- */
25
- currentLanguage: string;
26
- /**
27
- * The resolved programming language of the current file being edited.
28
- * This is used to provide more specific language-based completions and suggestions,
29
- * especially when the initial language might be ambiguous (e.g., 'javascript' could be resolved to 'JavaScript (ES2024)').
30
- */
31
- resolvedCurrentLanguage: string;
32
- /**
33
- * The name of the file you are currently editing.
34
- */
35
- currentFileName?: CurrentFileName;
36
- /**
37
- * The related files with the adjusted maximum lines.
38
- */
39
- relatedFiles?: RelatedFile[];
40
- /**
41
- * The technologies (libraries, frameworks, etc.) you want to use for the completion.
42
- * This can provide technology-specific completions.
43
- * If you don't specify a technology, the completion will be specific to the language (provided as the `language`).
44
- *
45
- * @example
46
- * ['react', 'nextjs', 'tailwindcss', 'tanstack/react-query']
47
- * ['tensorflow', 'keras', 'numpy', 'pandas']
48
- * etc.
49
- */
50
- technologies?: Technologies;
51
- /**
52
- * The maximum number of lines of code to include in the context sent to the Language Model (LLM).
53
- * This helps to reduce the cost of the completion request.
54
- *
55
- * @default undefined - No limit is applied if not specified
56
- */
57
- maxContextLines?: number;
58
- };
59
- type BuildContextOptions = Omit<Context, 'resolvedCurrentLanguage'>;
60
-
61
4
  type OpenAIModel = 'gpt-4o' | 'gpt-4o-mini' | 'o1-preview' | 'o1-mini';
62
5
  type GroqModel = 'llama-3-70b';
63
6
  type AnthropicModel = 'claude-3.5-sonnet' | 'claude-3-opus' | 'claude-3-haiku' | 'claude-3-sonnet';
@@ -140,26 +83,38 @@ type CustomCopilotModelTransformResponse = (response: unknown) => {
140
83
  */
141
84
  completion: string | null;
142
85
  };
143
- type CustomPrompt<T> = (metadata: T) => Partial<PromptData>;
144
86
 
145
87
  type Monaco = typeof monaco;
146
88
  type StandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
147
89
  type CursorPosition = monaco.IPosition;
148
- type EditorSelection = monaco.Selection;
149
90
 
150
91
  type Endpoint = string;
92
+ type Filename = string;
93
+ type Technologies = string[];
94
+ type RelatedFile = {
95
+ /**
96
+ * The relative path from the current editing code in the editor to an external file.
97
+ *
98
+ * Examples:
99
+ * - To include a file `utils.js` in the same directory, set as `./utils.js`.
100
+ * - To include a file `utils.js` in the parent directory, set as `../utils.js`.
101
+ * - To include a file `utils.js` in the child directory, set as `./child/utils.js`.
102
+ */
103
+ path: string;
104
+ /**
105
+ * The content of the external file as a string.
106
+ */
107
+ content: string;
108
+ };
151
109
  interface RegisterCompletionOptions {
152
110
  /**
153
- * The API endpoint to fetch the completion item from.
111
+ * Language of the current model
154
112
  */
155
- endpoint: Endpoint;
113
+ language: string;
156
114
  /**
157
- * The context object containing contextual information for generating relevant completions.
158
-
159
- * This object is created by the `buildContext` function from monacopilot.
160
- * It helps tailor completions to the specific project environment and coding context.
115
+ * The API endpoint where you started the completion service.
161
116
  */
162
- context: Context;
117
+ endpoint: Endpoint;
163
118
  /**
164
119
  * Specifies when the completion service should provide code completions.
165
120
  *
@@ -173,6 +128,36 @@ interface RegisterCompletionOptions {
173
128
  * @default 'onIdle'
174
129
  */
175
130
  trigger?: 'onTyping' | 'onIdle' | 'onDemand';
131
+ /**
132
+ * The name of the file you are editing. This is used to provide more relevant completions based on the file's purpose.
133
+ * For example, if you are editing a file named `utils.js`, the completions will be more relevant to utility functions.
134
+ */
135
+ filename?: Filename;
136
+ /**
137
+ * The technologies (libraries, frameworks, etc.) you want to use for the completion.
138
+ * This can provide technology-specific completions.
139
+ * If you don't specify a technology, the completion will be specific to the language (provided as the `language`).
140
+ *
141
+ * @example
142
+ * ['react', 'nextjs', 'tailwindcss', 'tanstack/react-query']
143
+ * ['tensorflow', 'keras', 'numpy', 'pandas']
144
+ * etc.
145
+ */
146
+ technologies?: Technologies;
147
+ /**
148
+ * Helps to give more relevant completions based on the full context.
149
+ * You can include things like the contents/codes of other files in the same workspace.
150
+ */
151
+ relatedFiles?: RelatedFile[];
152
+ /**
153
+ * The maximum number of lines of code to include in the completion request.
154
+ * This limits the request size to the model to prevent `429 Too Many Requests` errors
155
+ * and reduce costs for long code.
156
+ *
157
+ * It is recommended to set `maxContextLines` to `60` or less if you are using `Groq` as your provider,
158
+ * since `Groq` does not implement pay-as-you-go pricing and has only low rate limits.
159
+ */
160
+ maxContextLines?: number;
176
161
  /**
177
162
  * Callback function that is called when an error occurs during the completion request.
178
163
  * This function allows you to handle errors gracefully and provide appropriate feedback to the user.
@@ -203,23 +188,23 @@ interface CompletionRegistration {
203
188
  */
204
189
  deregister: () => void;
205
190
  }
206
- interface CompletionApiRequest {
191
+ interface CompletionRequest {
207
192
  /**
208
193
  * The body of the completion request.
209
194
  */
210
- body: CompletionApiRequestBody;
195
+ body: CompletionRequestBody;
211
196
  /**
212
197
  * Additional options to include in the completion request.
213
198
  */
214
- options?: CompletionApiRequestOptions;
199
+ options?: CompletionRequestOptions;
215
200
  }
216
- interface CompletionApiRequestBody {
201
+ interface CompletionRequestBody {
217
202
  /**
218
203
  * The metadata required to generate the completion.
219
204
  */
220
- metadata: CompletionMetadata;
205
+ completionMetadata: CompletionMetadata;
221
206
  }
222
- interface CompletionApiRequestOptions {
207
+ interface CompletionRequestOptions {
223
208
  /**
224
209
  * Custom headers to include in the request to the AI provider.
225
210
  */
@@ -229,21 +214,34 @@ interface CompletionApiRequestOptions {
229
214
  * This function allows you to override the default system and user prompts
230
215
  * used in the completion request, providing more control over the AI's context and behavior.
231
216
  *
232
- * @param metadata - Metadata about the current completion context
217
+ * @param completionMetadata - Metadata about the current completion context
233
218
  * @returns An object containing custom 'system' and 'user' prompts
234
219
  */
235
- customPrompt?: CustomPrompt<CompletionMetadata>;
220
+ customPrompt?: CustomPrompt;
236
221
  }
237
- interface CompletionApiResponse {
222
+ type CustomPrompt = (completionMetadata: CompletionMetadata) => Partial<PromptData>;
223
+ interface CompletionResponse {
238
224
  completion: string | null;
239
225
  error?: string;
240
226
  }
241
227
  type CompletionMode = 'insert' | 'complete' | 'continue';
242
228
  interface CompletionMetadata {
243
229
  /**
244
- * The context object containing contextual information for generating relevant completions.
230
+ * The programming language of the code.
231
+ */
232
+ language: string | undefined;
233
+ /**
234
+ * The name of the file being edited.
235
+ */
236
+ filename: Filename | undefined;
237
+ /**
238
+ * The technologies used in the completion.
239
+ */
240
+ technologies: Technologies | undefined;
241
+ /**
242
+ * Additional context from related files.
245
243
  */
246
- context?: Context;
244
+ relatedFiles: RelatedFile[] | undefined;
247
245
  /**
248
246
  * The text that appears after the cursor.
249
247
  */
@@ -260,6 +258,11 @@ interface CompletionMetadata {
260
258
  * The current state of the editor.
261
259
  */
262
260
  editorState: {
261
+ /**
262
+ * The mode of the completion.
263
+ * - `fill-in-the-middle`: Indicates that the cursor is positioned within the existing text. In this mode, the AI will generate content to be inserted at the cursor position.
264
+ * - `completion`: Indicates that the cursor is at the end of the existing text. In this mode, the AI will generate content to continue or complete the text from the cursor position.
265
+ */
263
266
  completionMode: CompletionMode;
264
267
  };
265
268
  }
@@ -269,114 +272,7 @@ type FetchCompletionItemReturn = {
269
272
  };
270
273
  interface FetchCompletionItemParams {
271
274
  endpoint: string;
272
- body: CompletionApiRequestBody;
273
- }
274
-
275
- interface SelectionActionsRegistration {
276
- deregister: () => void;
277
- }
278
- type SelectionAction = 'modify';
279
- interface RegisterSelectionActionsOptions {
280
- actions: SelectionAction[];
281
- modify: ModifyOptions;
282
- onError?: (error: Error) => void;
283
- }
284
- interface ModifyOptions {
285
- /**
286
- * The API endpoint to fetch the modified text from.
287
- */
288
- endpoint: string;
289
- /**
290
- * The context object containing contextual information for generating relevant modifications.
291
- */
292
- context: Context;
293
- /**
294
- * The text to display as a placeholder in the text area where users input their modification instructions.
295
- */
296
- placeholder?: string;
297
- /**
298
- * Custom fetch modified text handler. This function overrides the default modified text fetch handler.
299
- * It allows you to customize how modified text requests are made and responses are processed.
300
- * You can implement your own logic for fetching and processing modified text.
301
- * The function should return either a string (the modified text to be replaced with the selected text) or null.
302
- * @param params - The parameters for the modified text request.
303
- * @param {string} params.endpoint - The endpoint to fetch the modified text from.
304
- * @param {ModifyApiRequestBody} params.body - The body of the modified text request.
305
- * @returns {Promise<{modifiedText: string | null}>} An object containing the modified text or null if no modified text is available.
306
- */
307
- requestHandler?: FetchModifiedTextHandler;
308
- }
309
- interface ModifyMetadata {
310
- /**
311
- * The context object containing contextual information for generating relevant modifications.
312
- */
313
- context?: Context;
314
- /**
315
- * The full text of the current file.
316
- */
317
- fullText: string;
318
- /**
319
- * The selected text range in the file.
320
- */
321
- selection: EditorSelection | null;
322
- /**
323
- * The selected text.
324
- * This will be null if the user has a cursor position and no text is selected.
325
- */
326
- selectedText: string | null;
327
- /**
328
- * The current cursor position.
329
- */
330
- cursorPosition: CursorPosition | null;
331
- /**
332
- * The text before the cursor position.
333
- * This will be null if the user has selected text instead of having a cursor position.
334
- */
335
- textBeforeCursor: string | null;
336
- /**
337
- * The text after the cursor position.
338
- * This will be null if the user has selected text instead of having a cursor position.
339
- */
340
- textAfterCursor: string | null;
341
- /**
342
- * The user-provided instructions for modifying the selected text.
343
- */
344
- prompt: string;
345
- }
346
- interface FetchModifiedTextParams {
347
- endpoint: string;
348
- body: ModifyApiRequestBody;
349
- }
350
- interface FetchModifiedTextReturn {
351
- modifiedText: string | null;
352
- }
353
- type FetchModifiedTextHandler = (params: FetchModifiedTextParams) => Promise<FetchModifiedTextReturn>;
354
- interface ModifyApiRequest {
355
- body: ModifyApiRequestBody;
356
- options?: ModifyApiRequestOptions;
357
- }
358
- interface ModifyApiRequestOptions {
359
- /**
360
- * Custom headers to include in the request to the AI provider.
361
- */
362
- headers?: Record<string, string>;
363
- /**
364
- * Custom prompt generator function for the modify request.
365
- * This function allows you to override the default system and user prompts
366
- * used in the modify request, providing more control over the AI's context and behavior.
367
- *
368
- * @param metadata - Metadata about the current modify context
369
- * @returns An object containing custom 'system' and 'user' prompts
370
- */
371
- customPrompt?: ModifyCustomPrompt;
372
- }
373
- type ModifyCustomPrompt = (metadata: ModifyMetadata) => Partial<PromptData>;
374
- interface ModifyApiRequestBody {
375
- metadata: ModifyMetadata;
376
- }
377
- interface ModifyApiResponse {
378
- modifiedText: string | null;
379
- error?: string;
275
+ body: CompletionRequestBody;
380
276
  }
381
277
 
382
278
  declare class Copilot {
@@ -385,16 +281,12 @@ declare class Copilot {
385
281
  private model;
386
282
  constructor(apiKey: string, options?: CopilotOptions);
387
283
  private validateInputs;
388
- complete(request: CompletionApiRequest): Promise<CompletionApiResponse>;
389
- modify(request: ModifyApiRequest): Promise<ModifyApiResponse>;
390
- private makeApiCall;
284
+ complete(request: CompletionRequest): Promise<CompletionResponse>;
391
285
  private generatePrompt;
392
286
  private prepareRequestDetails;
393
287
  private sendCompletionRequest;
394
288
  private processCompletionResponse;
395
- private processModifyResponse;
396
289
  private handleCompletionError;
397
- private handleModifyError;
398
290
  }
399
291
 
400
292
  /**
@@ -410,21 +302,4 @@ declare const registerCompletion: (monaco: Monaco, editor: StandaloneCodeEditor,
410
302
  */
411
303
  declare const registerCopilot: (monaco: typeof monaco_editor, editor: monaco_editor.editor.IStandaloneCodeEditor, options: RegisterCompletionOptions) => CompletionRegistration;
412
304
 
413
- /**
414
- * Registers the selection action functionality with the Monaco editor.
415
- *
416
- * @param monaco - The Monaco instance.
417
- * @param editor - The editor instance.
418
- * @param options - Options for the action functionality.
419
- * @returns A SelectionActionsRegistration object with a deregister method.
420
- */
421
- declare const registerSelectionActions: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterSelectionActionsOptions) => SelectionActionsRegistration;
422
-
423
- /**
424
- * Builds a context object containing contextual information for generating relevant completions/suggestions.
425
- * @param options - The options for building the context.
426
- * @returns The context object.
427
- */
428
- declare const buildContext: (options: BuildContextOptions) => Context;
429
-
430
- export { type CompletionApiRequest, type CompletionApiRequestBody, type CompletionApiRequestOptions, type CompletionMetadata, type CompletionRegistration, type Context, Copilot, type CopilotModel, type CopilotOptions, type CopilotProvider, type CustomCopilotModel, type CustomCopilotModelConfig, type CustomCopilotModelTransformResponse, type Monaco, type RegisterCompletionOptions, type StandaloneCodeEditor, buildContext, registerCompletion, registerCopilot, registerSelectionActions };
305
+ export { type CompletionMetadata, type CompletionRegistration, type CompletionRequest, type CompletionRequestBody, type CompletionRequestOptions, Copilot, type CopilotModel, type CopilotOptions, type CopilotProvider, type CustomCopilotModel, type CustomCopilotModelConfig, type CustomCopilotModelTransformResponse, type Monaco, type RegisterCompletionOptions, type StandaloneCodeEditor, registerCompletion, registerCopilot };