Skip to content

Commit

Permalink
chore: stop sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
arshad-yaseen committed Feb 26, 2025
1 parent 6fdec22 commit c906d50
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 242 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/constants.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core/src/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Provider,
} from './types/llm';
import {BaseCopilotMetadata} from './types/metadata';
import {fetchWithTimeout} from './utils/fetch-with-timeout';
import validate from './validator';

export abstract class Copilot<Metadata> {
Expand Down Expand Up @@ -139,7 +140,7 @@ export abstract class Copilot<Metadata> {
requestBody: CompletionCreateParams,
headers: Record<string, string>,
) {
const response = await fetch(endpoint, {
const response = await fetchWithTimeout(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_COPILOT_MAX_TOKENS = 256 as const;
export const DEFAULT_COPILOT_STOP_SEQUENCE = '\n\n' as const;
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {Copilot} from './copilot';

export * from './logger';
export * from './constants';
export * from './defaults';
export * from './llm/base';

export type * from './types/llm';
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/llm/providers/mistral.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {DEFAULT_COPILOT_TEMPERATURE} from '../../constants';
import {
DEFAULT_COPILOT_MAX_TOKENS,
DEFAULT_COPILOT_STOP_SEQUENCE,
} from '../../defaults';
import type {PromptData} from '../../types/copilot';
import type {
PickCompletion,
Expand All @@ -21,10 +24,12 @@ export class MistralHandler extends BaseProviderHandler<'mistral'> {
): PickCompletionCreateParams<'mistral'> {
return {
model: MODEL_IDS[model],
temperature: DEFAULT_COPILOT_TEMPERATURE,
prompt: `${prompt.context}\n${prompt.instruction}\n\n${metadata.textBeforeCursor}`,
prompt: `${prompt.context}\n${prompt.instruction}\n${metadata.textBeforeCursor}`,
max_tokens: DEFAULT_COPILOT_MAX_TOKENS,
stop: DEFAULT_COPILOT_STOP_SEQUENCE,
suffix: metadata.textAfterCursor,
stream: false,
top_p: 0.9,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/llm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
FIMCompletionResponse as MistralFIMCompletion,
FIMCompletionRequest as MistralFIMCompletionCreateParams,
FIMCompletionRequest$Outbound as MistralFIMCompletionCreateParams,
} from '@mistralai/mistralai/models/components';

import type {PromptData} from './copilot';
Expand Down
28 changes: 28 additions & 0 deletions packages/core/src/utils/fetch-with-timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const fetchWithTimeout = async (
url: string,
options: RequestInit = {},
timeoutMs: number = 5000,
): Promise<Response> => {
const controller = new AbortController();
const {signal} = controller;

const timeoutId = setTimeout(() => {
controller.abort();
}, timeoutMs);

try {
const response = await fetch(url, {
...options,
signal,
});

return response;
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
throw new Error(`Request timed out after ${timeoutMs}ms`);
}
throw error;
} finally {
clearTimeout(timeoutId);
}
};
45 changes: 1 addition & 44 deletions packages/monacopilot/src/classes/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
export class CompletionFormatter {
private formattedCompletion = '';
private currentColumn = 0;
private textBeforeCursorInLine = '';

constructor(
completion: string,
currentColumn: number,
textBeforeCursorInLine: string,
) {
constructor(completion: string) {
this.formattedCompletion = completion;
this.currentColumn = currentColumn;
this.textBeforeCursorInLine = textBeforeCursorInLine;
}

public setCompletion(completion: string): CompletionFormatter {
Expand All @@ -30,41 +22,6 @@ export class CompletionFormatter {
return this;
}

public indentByColumn(): CompletionFormatter {
const lines = this.formattedCompletion.split('\n');

if (this.textBeforeCursorInLine.trim() !== '') {
return this;
}

const firstLine = lines[0].trimStart();
const firstLineIndent = lines[0].length - firstLine.length;

if (lines.length === 1) {
this.formattedCompletion = firstLine;
return this;
}

this.formattedCompletion =
firstLine +
'\n' +
lines
.slice(1)
.map(line => {
const currentLineIndent =
line.length - line.trimStart().length;
const trimAmount = Math.min(
firstLineIndent,
currentLineIndent,
);
const trimmedStart = line.slice(trimAmount);
return ' '.repeat(this.currentColumn - 1) + trimmedStart;
})
.join('\n');

return this;
}

private removeMarkdownCodeBlocks(text: string): string {
const lines = text.split('\n');
const result: string[] = [];
Expand Down
9 changes: 2 additions & 7 deletions packages/monacopilot/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from './types';
import type {EditorInlineCompletionsResult} from './types/monaco';
import {asyncDebounce} from './utils/async-debounce';
import {getTextBeforeCursor, getTextBeforeCursorInLine} from './utils/editor';
import {getTextBeforeCursor} from './utils/editor';
import {isCancellationError} from './utils/error';
import {createInlineCompletionResult} from './utils/result';

Expand Down Expand Up @@ -86,15 +86,10 @@ export const processInlineCompletions = async ({
});

if (completion) {
const formattedCompletion = new CompletionFormatter(
completion,
pos.column,
getTextBeforeCursorInLine(pos, mdl),
)
const formattedCompletion = new CompletionFormatter(completion)
.removeMarkdownCodeSyntax()
.removeExcessiveNewlines()
.removeInvalidLineBreaks()
.indentByColumn()
.build();

const completionRange = new CompletionRange(monaco);
Expand Down
Loading

0 comments on commit c906d50

Please sign in to comment.