Skip to content

Commit

Permalink
docs: improve requestHandler guide
Browse files Browse the repository at this point in the history
  • Loading branch information
arshad-yaseen committed Sep 29, 2024
1 parent 69bc530 commit 3689720
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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:
Expand All @@ -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',
}),
});
Expand All @@ -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
Expand All @@ -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`.
Expand Down

0 comments on commit 3689720

Please sign in to comment.