monacopilot 0.11.6 → 0.11.7

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
@@ -33,7 +33,7 @@
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 Edit](#select-and-edit)
36
+ - [Select and Modify](#select-and-modify)
37
37
  - [Contributing](#contributing)
38
38
 
39
39
  ### Examples
@@ -510,7 +510,7 @@ copilot.complete({
510
510
 
511
511
  #### Parameters
512
512
 
513
- The `customPrompt` function receives a `completionMetadata` object, which contains information about the current editor state and can be used to tailor the prompt.
513
+ The `customPrompt` function receives a `metadata` object, which contains information about the current editor state and can be used to tailor the prompt.
514
514
 
515
515
  ##### Completion Metadata
516
516
 
@@ -533,7 +533,7 @@ The `editorState.completionMode` can be one of the following:
533
533
  | `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
534
  | `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
535
 
536
- For additional `completionMetadata` needs, please [open an issue](https://github.com/arshad-yaseen/monacopilot/issues/new).
536
+ For additional `metadata` needs, please [open an issue](https://github.com/arshad-yaseen/monacopilot/issues/new).
537
537
 
538
538
  The `customPrompt` function should return an object with two properties:
539
539
 
@@ -600,7 +600,7 @@ Check out the [prompt.ts](https://github.com/arshad-yaseen/monacopilot/blob/main
600
600
 
601
601
  ### Metadata Overview
602
602
 
603
- 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.
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.
604
604
 
605
605
  ### Example Implementation (Python with FastAPI)
606
606
 
@@ -615,7 +615,7 @@ app = FastAPI()
615
615
  async def handle_completion(request: Request):
616
616
  try:
617
617
  body = await request.json()
618
- metadata = body['completionMetadata']
618
+ metadata = body['metadata']
619
619
 
620
620
  prompt = f"""Please complete the following {metadata['language']} code:
621
621
 
@@ -649,9 +649,9 @@ registerCompletion(monaco, editor, {
649
649
  });
650
650
  ```
651
651
 
652
- ## Select and Edit
652
+ ## Select and Modify
653
653
 
654
- Select and Edit is a feature that allows you to select code from the editor and edit it inline with AI assistance.
654
+ This feature allows you to edit the code by selecting the part you want to modify and ask AI to modify it.
655
655
 
656
656
  This feature is coming soon™️.
657
657
 
package/build/index.d.mts CHANGED
@@ -1,6 +1,63 @@
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
+
4
61
  type OpenAIModel = 'gpt-4o' | 'gpt-4o-mini' | 'o1-preview' | 'o1-mini';
5
62
  type GroqModel = 'llama-3-70b';
6
63
  type AnthropicModel = 'claude-3.5-sonnet' | 'claude-3-opus' | 'claude-3-haiku' | 'claude-3-sonnet';
@@ -83,38 +140,26 @@ type CustomCopilotModelTransformResponse = (response: unknown) => {
83
140
  */
84
141
  completion: string | null;
85
142
  };
143
+ type CustomPrompt<T> = (metadata: T) => Partial<PromptData>;
86
144
 
87
145
  type Monaco = typeof monaco;
88
146
  type StandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
89
147
  type CursorPosition = monaco.IPosition;
148
+ type EditorSelection = monaco.Selection;
90
149
 
91
150
  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
- };
109
151
  interface RegisterCompletionOptions {
110
152
  /**
111
- * Language of the current model
153
+ * The API endpoint to fetch the completion item from.
112
154
  */
113
- language: string;
155
+ endpoint: Endpoint;
114
156
  /**
115
- * The API endpoint where you started the completion service.
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.
116
161
  */
117
- endpoint: Endpoint;
162
+ context: Context;
118
163
  /**
119
164
  * Specifies when the completion service should provide code completions.
120
165
  *
@@ -128,36 +173,6 @@ interface RegisterCompletionOptions {
128
173
  * @default 'onIdle'
129
174
  */
130
175
  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;
161
176
  /**
162
177
  * Callback function that is called when an error occurs during the completion request.
163
178
  * This function allows you to handle errors gracefully and provide appropriate feedback to the user.
@@ -188,23 +203,23 @@ interface CompletionRegistration {
188
203
  */
189
204
  deregister: () => void;
190
205
  }
191
- interface CompletionRequest {
206
+ interface CompletionApiRequest {
192
207
  /**
193
208
  * The body of the completion request.
194
209
  */
195
- body: CompletionRequestBody;
210
+ body: CompletionApiRequestBody;
196
211
  /**
197
212
  * Additional options to include in the completion request.
198
213
  */
199
- options?: CompletionRequestOptions;
214
+ options?: CompletionApiRequestOptions;
200
215
  }
201
- interface CompletionRequestBody {
216
+ interface CompletionApiRequestBody {
202
217
  /**
203
218
  * The metadata required to generate the completion.
204
219
  */
205
- completionMetadata: CompletionMetadata;
220
+ metadata: CompletionMetadata;
206
221
  }
207
- interface CompletionRequestOptions {
222
+ interface CompletionApiRequestOptions {
208
223
  /**
209
224
  * Custom headers to include in the request to the AI provider.
210
225
  */
@@ -214,34 +229,21 @@ interface CompletionRequestOptions {
214
229
  * This function allows you to override the default system and user prompts
215
230
  * used in the completion request, providing more control over the AI's context and behavior.
216
231
  *
217
- * @param completionMetadata - Metadata about the current completion context
232
+ * @param metadata - Metadata about the current completion context
218
233
  * @returns An object containing custom 'system' and 'user' prompts
219
234
  */
220
- customPrompt?: CustomPrompt;
235
+ customPrompt?: CustomPrompt<CompletionMetadata>;
221
236
  }
222
- type CustomPrompt = (completionMetadata: CompletionMetadata) => Partial<PromptData>;
223
- interface CompletionResponse {
237
+ interface CompletionApiResponse {
224
238
  completion: string | null;
225
239
  error?: string;
226
240
  }
227
241
  type CompletionMode = 'insert' | 'complete' | 'continue';
228
242
  interface CompletionMetadata {
229
243
  /**
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.
244
+ * The context object containing contextual information for generating relevant completions.
243
245
  */
244
- relatedFiles: RelatedFile[] | undefined;
246
+ context?: Context;
245
247
  /**
246
248
  * The text that appears after the cursor.
247
249
  */
@@ -258,11 +260,6 @@ interface CompletionMetadata {
258
260
  * The current state of the editor.
259
261
  */
260
262
  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
- */
266
263
  completionMode: CompletionMode;
267
264
  };
268
265
  }
@@ -272,7 +269,114 @@ type FetchCompletionItemReturn = {
272
269
  };
273
270
  interface FetchCompletionItemParams {
274
271
  endpoint: string;
275
- body: CompletionRequestBody;
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;
276
380
  }
277
381
 
278
382
  declare class Copilot {
@@ -281,12 +385,16 @@ declare class Copilot {
281
385
  private model;
282
386
  constructor(apiKey: string, options?: CopilotOptions);
283
387
  private validateInputs;
284
- complete(request: CompletionRequest): Promise<CompletionResponse>;
388
+ complete(request: CompletionApiRequest): Promise<CompletionApiResponse>;
389
+ modify(request: ModifyApiRequest): Promise<ModifyApiResponse>;
390
+ private makeApiCall;
285
391
  private generatePrompt;
286
392
  private prepareRequestDetails;
287
393
  private sendCompletionRequest;
288
394
  private processCompletionResponse;
395
+ private processModifyResponse;
289
396
  private handleCompletionError;
397
+ private handleModifyError;
290
398
  }
291
399
 
292
400
  /**
@@ -302,4 +410,21 @@ declare const registerCompletion: (monaco: Monaco, editor: StandaloneCodeEditor,
302
410
  */
303
411
  declare const registerCopilot: (monaco: typeof monaco_editor, editor: monaco_editor.editor.IStandaloneCodeEditor, options: RegisterCompletionOptions) => CompletionRegistration;
304
412
 
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 };
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 };