Skip to content

Commit

Permalink
Add code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Wzixiao committed Nov 2, 2023
1 parent a17f2ed commit c32637e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
14 changes: 12 additions & 2 deletions packages/jupyter-ai/src/bigcode-Inline-completion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class BigcodeInlineCompletionProvider
{
readonly identifier = '@jupyterlab/inline-completer:bigcode';
private _trans: TranslationBundle;
// Used to record information from the last user request
private _lastRequestInfo: {
insertText: string;
cellCode: string;
Expand All @@ -29,10 +30,15 @@ export class BigcodeInlineCompletionProvider
cellCode: ''
};
private _requesting = false;
// Change it in "fetch" and then use this field in "stream" to call a different function
private _requestMode: InlineCompletionTriggerKind = 0;
// Whether to stop the stream immediately, When the stream is in progress, the user requests to change this field to true, and then interrupts the stream request.
private _streamStop = false;
// Whether to finish for request
private _finish = false;
// Debounce use
private _timeoutId: number | null = null;
// Debounce use
private _callCounter = 0;

get finish(): boolean {
Expand Down Expand Up @@ -61,6 +67,9 @@ export class BigcodeInlineCompletionProvider
this._finish = !error;
}

/**
* When the user executes the "accept" function, this function will be called to clear the status.
*/
clearState(): void {
this._streamStop = true;
this._finish = false;
Expand Down Expand Up @@ -226,6 +235,7 @@ export class BigcodeInlineCompletionProvider
);

const items: IInlineCompletionItem[] = [];
// Determine whether the keyboard pressed by the user is the beginning of ghost text
if (
this._lastRequestInfo.insertText.startsWith(newAddedCodeText) &&
newAddedCodeText !== ''
Expand Down Expand Up @@ -382,7 +392,7 @@ export class BigcodeInlineCompletionProvider
let reponseData: ReadableStream<Uint8Array> | null = null;

try {
reponseData = await bigcodeRequestInstance.fetchStream(true);
reponseData = await bigcodeRequestInstance.send(true);
} catch {
yield {
response: {
Expand Down Expand Up @@ -434,7 +444,7 @@ export class BigcodeInlineCompletionProvider
token: string
): AsyncGenerator<{ response: IInlineCompletionItem }, undefined, unknown> {
try {
const reponseData = await bigcodeRequestInstance.fetchStream(false);
const reponseData = await bigcodeRequestInstance.send(false);

yield {
response: {
Expand Down
9 changes: 5 additions & 4 deletions packages/jupyter-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ const plugin: JupyterFrontEndPlugin<void> = {
}
};

const bigcodeCodeCompletion: JupyterFrontEndPlugin<void> = {
id: 'jupyter_ai:plugin:inline-bigcode',
description: 'Adds inline completion provider suggesting code from bigcode.',
const inlineCompletionPlugin: JupyterFrontEndPlugin<void> = {
id: 'jupyter_ai:plugin:inline-completion',
description:
'Adding an inline completion provider suggestion comes from jupyter-ai.',
requires: [ICompletionProviderManager],
optional: [ILayoutRestorer],
autoStart: true,
Expand Down Expand Up @@ -116,6 +117,6 @@ const bigcodeCodeCompletion: JupyterFrontEndPlugin<void> = {
}
};

const plugins: JupyterFrontEndPlugin<void>[] = [plugin, bigcodeCodeCompletion];
const plugins: JupyterFrontEndPlugin<void>[] = [plugin, inlineCompletionPlugin];

export default plugins;
13 changes: 3 additions & 10 deletions packages/jupyter-ai/src/utils/bigcode-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ export type BigCodeServiceNotStreamResponse = {
generated_text: string;
}[];

/**
* Generates the appropriate prompt string based on the cell type.
* The cell type can be either 'code' or 'markdown'.
*
* @param {ICell} cell - The cell object which includes the type and content.
* @returns {string} The generated prompt string for the cell.
*/
class Bigcode {
spaceCount: number;
private _prompt: string;
Expand All @@ -52,9 +45,9 @@ class Bigcode {
return this._prompt;
}

async fetchStream(stream: true): Promise<ReadableStream<Uint8Array>>;
async fetchStream(stream: false): Promise<BigCodeServiceNotStreamResponse>;
async fetchStream(
async send(stream: true): Promise<ReadableStream<Uint8Array>>;
async send(stream: false): Promise<BigCodeServiceNotStreamResponse>;
async send(
stream = false
): Promise<ReadableStream<Uint8Array> | BigCodeServiceNotStreamResponse> {
if (!this.bigcodeUrl || !this.accessToken) {
Expand Down

0 comments on commit c32637e

Please sign in to comment.