From 368972084e499963865443f815e8a2320b42ed27 Mon Sep 17 00:00:00 2001 From: Arshad Yaseen Date: Sun, 29 Sep 2024 18:43:40 +0530 Subject: [PATCH] docs: improve requestHandler guide --- README.md | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 9c63792..b37814b 100644 --- a/README.md +++ b/README.md @@ -314,14 +314,13 @@ registerCompletion(monaco, editor, { ### Custom Request Handler -The `requestHandler` option in the `registerCompletion` function allows you to handle requests sent to the specified endpoint, offering high customization for both requests and responses. By leveraging this functionality, you can manipulate and customize the request or response to meet your specific requirements. However, ensure that the completion text is returned in the response. +The `requestHandler` option in the `registerCompletion` function allows you to handle requests sent to the specified endpoint, offering high customization for both requests and responses. By leveraging this functionality, you can manipulate and customize the request or response to meet your specific requirements. ```javascript registerCompletion(monaco, editor, { endpoint: 'https://api.example.com/complete', // ... other options requestHandler: async ({endpoint, body}) => { - // Perform custom actions with the request const response = await fetch(endpoint, { method: 'POST', headers: { @@ -339,14 +338,21 @@ registerCompletion(monaco, editor, { }); ``` -The `requestHandler` function takes `endpoint` and `body` as parameters. +The `requestHandler` function takes an object with `endpoint` and `body` as parameters. | Property | Type | Description | | ---------- | -------- | ------------------------------------------------------------------------------------------------------ | | `endpoint` | `string` | The endpoint to which the request is sent. This is the same as the `endpoint` in `registerCompletion`. | -| `body` | `object` | The body of the request processed by Monacopilot. You can add additional properties to this body. | +| `body` | `object` | The body of the request processed by Monacopilot. | -> **Note:** The `body` object is processed by Monacopilot, so you cannot modify the original `body` object. If you need to add additional properties to the body, you can do so by creating a new object. +> **Note:** The `body` object contains properties generated by Monacopilot. If you need to include additional properties in the request body, you can create a new object that combines the existing `body` with your custom properties. For example: +> +> ```javascript +> const customBody = { +> ...body, +> myCustomProperty: 'value', +> }; +> ``` The `requestHandler` should return an object with the following property: @@ -358,23 +364,21 @@ The `requestHandler` should return an object with the following property: The example below demonstrates how to use the `requestHandler` function for more customized handling: -```javascript +````javascript registerCompletion(monaco, editor, { endpoint: 'https://api.example.com/complete', // ... other options requestHandler: async ({endpoint, body}) => { try { - const headers = { - 'Content-Type': 'application/json', - 'X-Request-ID': generateUniqueId(), - }; - const response = await fetch(endpoint, { method: 'POST', - headers, + headers: { + 'Content-Type': 'application/json', + 'X-Request-ID': generateUniqueId(), + }, body: JSON.stringify({ ...body, - any: 'thing', + additionalProperty: 'value', }), }); @@ -384,29 +388,18 @@ registerCompletion(monaco, editor, { const data = await response.json(); - // Custom processing of the response if (data.error) { console.error('API Error:', data.error); - return { - completion: null, - }; + return { completion: null }; } - // Additional processing on the completion - const processedCompletion = data.completion.trim(); - - return { - completion: processedCompletion, - }; + return { completion: data.completion.trim() }; } catch (error) { console.error('Fetch error:', error); - return { - completion: null, - }; + return { completion: null }; } }, }); -``` ## Copilot Options @@ -419,7 +412,7 @@ const copilot = new Copilot(process.env.OPENAI_API_KEY, { provider: 'openai', model: 'gpt-4o', }); -``` +```` The default provider is `groq`, and the default model is `llama-3-70b`.