Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update code style for pr 3113. #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions clients/tabby-agent/src/chat/inlineEdit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Range, Location, Connection, CancellationToken } from "vscode-languageserver";
import type { Connection, CancellationToken } from "vscode-languageserver";
import type { TextDocument } from "vscode-languageserver-textdocument";
import type { TextDocuments } from "../lsp/textDocuments";
import type { Feature } from "../feature";
Expand All @@ -20,19 +20,7 @@ import {
} from "../protocol";
import cryptoRandomString from "crypto-random-string";
import { isEmptyRange } from "../utils/range";
import { applyWorkspaceEdit, readResponseStream } from "./utils";

export type Edit = {
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
id: ChatEditToken;
location: Location;
languageId: string;
originalText: string;
editedRange: Range;
editedText: string;
comments: string;
buffer: string;
state: "editing" | "stopped" | "completed";
};
import { applyWorkspaceEdit, readResponseStream, Edit } from "./utils";

export class ChatEditProvider implements Feature {
private lspConnection: Connection | undefined = undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TextDocument } from "vscode-languageserver-textdocument";
import {
CancellationToken,
Connection,
Expand All @@ -12,18 +13,17 @@ import {
ChatEditMutexError,
ChatFeatureNotAvailableError,
ServerCapabilities,
SmartApplyCodeRequest,
SmartApplyCodeParams,
SmartApplyRequest,
SmartApplyParams,
} from "../protocol";
import { Configurations } from "../config";
import { TabbyApiClient } from "../http/tabbyApiClient";
import cryptoRandomString from "crypto-random-string";
import { getLogger } from "../logger";
import { readResponseStream, showDocument } from "./utils";
import { TextDocument } from "vscode-languageserver-textdocument";
import { getSmartApplyRange } from "./SmartRange";
import { Edit } from "./inlineEdit";
const logger = getLogger("ChatEditProvider");
import { readResponseStream, showDocument, Edit } from "./utils";
import { getSmartApplyRange } from "./smartRange";

const logger = getLogger("SmartApplyFeature");
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved

export class SmartApplyFeature implements Feature {
private lspConnection: Connection | undefined = undefined;
Expand All @@ -36,7 +36,7 @@ export class SmartApplyFeature implements Feature {

initialize(connection: Connection): ServerCapabilities | Promise<ServerCapabilities> {
this.lspConnection = connection;
connection.onRequest(SmartApplyCodeRequest.type, async (params, token) => {
connection.onRequest(SmartApplyRequest.type, async (params, token) => {
return this.provideSmartApplyEdit(params, token);
});

Expand All @@ -49,15 +49,15 @@ export class SmartApplyFeature implements Feature {
//nothing
}

async provideSmartApplyEdit(params: SmartApplyCodeParams, token: CancellationToken): Promise<boolean> {
logger.info("Getting document");
async provideSmartApplyEdit(params: SmartApplyParams, token: CancellationToken): Promise<boolean> {
logger.debug("Getting document");
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
const document = this.documents.get(params.location.uri);
if (!document) {
logger.info("Document not found, returning false");
logger.debug("Document not found, returning false");
return false;
}
if (!this.lspConnection) {
logger.info("LSP connection lost.");
logger.debug("LSP connection lost.");
return false;
}

Expand All @@ -69,21 +69,16 @@ export class SmartApplyFeature implements Feature {
} as ChatEditMutexError;
}
this.mutexAbortController = new AbortController();
logger.info("mutex abort status: " + (this.mutexAbortController === undefined));
logger.debug("mutex abort status: " + (this.mutexAbortController === undefined));
token.onCancellationRequested(() => this.mutexAbortController?.abort());

let applyRange = getSmartApplyRange(document, params.applyCode);
let applyRange = getSmartApplyRange(document, params.text);
//if cannot find range, lets use backend LLMs
if (!applyRange) {
if (!this.tabbyApiClient.isChatApiAvailable) {
return false;
}
applyRange = await provideSmartApplyLineRange(
document,
params.applyCode,
this.tabbyApiClient,
this.configurations,
);
applyRange = await provideSmartApplyLineRange(document, params.text, this.tabbyApiClient, this.configurations);
}

if (!applyRange) {
Expand Down Expand Up @@ -114,7 +109,7 @@ export class SmartApplyFeature implements Feature {
end: { line: applyRange.range.end.line + 1, character: 0 },
},
},
params.applyCode,
params.text,
applyRange.action === "insert" ? true : false,
document,
this.lspConnection,
Expand All @@ -130,13 +125,13 @@ export class SmartApplyFeature implements Feature {
logger.error("Error applying smart edit:", error);
return false;
} finally {
logger.info("Resetting mutex abort controller");
logger.debug("Resetting mutex abort controller");
this.mutexAbortController = undefined;
}
}
}

export async function provideSmartApplyLineRange(
async function provideSmartApplyLineRange(
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
document: TextDocument,
applyCodeBlock: string,
tabbyApiClient: TabbyApiClient,
Expand All @@ -159,7 +154,7 @@ export async function provideSmartApplyLineRange(
.join("\n");

const config = configurations.getMergedConfig();
const promptTemplate = config.chat.provideSmartApplyLineRange.promptTemplate;
const promptTemplate = config.chat.smartApplyLineRange.promptTemplate;

const messages: { role: "user"; content: string }[] = [
{
Expand Down Expand Up @@ -219,7 +214,7 @@ export async function provideSmartApplyLineRange(
}
}

export async function provideSmartApplyEditLLM(
async function provideSmartApplyEditLLM(
location: Location,
applyCode: string,
insertMode: boolean,
Expand Down Expand Up @@ -254,13 +249,13 @@ export async function provideSmartApplyEditLLM(
};
const selectedDocumentText = documentText.substring(selection.start, selection.end);

logger.info("current selectedDoc: " + selectedDocumentText);
logger.debug("current selectedDoc: " + selectedDocumentText);

if (selection.end - selection.start > config.chat.edit.documentMaxChars) {
throw { name: "ChatEditDocumentTooLongError", message: "Document too long" } as ChatEditDocumentTooLongError;
}

const promptTemplate = config.chat.provideSmartApply.promptTemplate;
const promptTemplate = config.chat.smartApply.promptTemplate;

// Extract the selected text and the surrounding context
let documentPrefix = documentText.substring(0, selection.start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getSmartApplyRange(
return { range: applyRange.range, action: "replace" };
}

export function fuzzyApplyRange(document: TextDocument, snippet: string): { range: Range; score: number } | null {
function fuzzyApplyRange(document: TextDocument, snippet: string): { range: Range; score: number } | null {
const lines = document.getText().split("\n");
const snippetLines = snippet.split("\n");

Expand Down
25 changes: 21 additions & 4 deletions clients/tabby-agent/src/chat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
//chat related utils functions

import { Readable } from "stream";
import { Edit } from "./inlineEdit";
import { ShowDocumentParams, ShowDocumentRequest, WorkspaceEdit } from "vscode-languageserver-protocol";
import * as Diff from "diff";
import { isBlank } from "../utils/string";
import {
Range,
Location,
ShowDocumentParams,
ShowDocumentRequest,
WorkspaceEdit,
} from "vscode-languageserver-protocol";
import { Connection } from "vscode-languageserver";
import * as Diff from "diff";
import { ApplyWorkspaceEditParams, ApplyWorkspaceEditRequest } from "../protocol";
import { isBlank } from "../utils/string";

export type Edit = {
id: string;
location: Location;
languageId: string;
originalText: string;
editedRange: Range;
editedText: string;
comments: string;
buffer: string;
state: "editing" | "stopped" | "completed";
};

export async function readResponseStream(
stream: Readable,
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export const defaultConfigData: ConfigData = {
responseMatcher:
/(?<=(["'`]+)?\s*)(feat|fix|docs|refactor|style|test|build|ci|chore)(\(\S+\))?:.+(?=\s*\1)/gis.toString(),
},
provideSmartApplyLineRange: {
smartApplyLineRange: {
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
promptTemplate: provideSmartApplyLineRangePrompt,
},
provideSmartApply: {
smartApply: {
promptTemplate: generateSmartApplyPrompt,
},
},
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/config/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ export type ConfigData = {
promptTemplate: string;
responseMatcher: string;
};
provideSmartApplyLineRange: {
smartApplyLineRange: {
promptTemplate: string;
};
provideSmartApply: {
smartApply: {
promptTemplate: string;
};
};
Expand Down
21 changes: 10 additions & 11 deletions clients/tabby-agent/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,34 +527,33 @@ export type ChatEditResolveCommand = LspCommand & {
};

/**
* [Tabby] Chat Edit Request(↩️)
* [Tabby] Smart Apply Request(↩️)
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
*
* This method is sent from the client to the server to edit the document content by user's command.
* This method is sent from the client to the server to smart apply the text to the target location.
* The server will edit the document content using ApplyEdit(`workspace/applyEdit`) request,
* which requires the client to have this capability.
* - method: `tabby/chat/edit`
* - params: {@link SmartApplyCodeParams}
* - method: `tabby/chat/smartApply`
* - params: {@link SmartApplyParams}
* - result: boolean
* - error: {@link ChatFeatureNotAvailableError}
* | {@link ChatEditDocumentTooLongError}
* | {@link ChatEditCommandTooLongError}
* | {@link ChatEditMutexError}
*/
export namespace SmartApplyCodeRequest {
export const method = "tabby/chat/smartApply/apply";
export namespace SmartApplyRequest {
export const method = "tabby/chat/smartApply";
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
export const messageDirection = MessageDirection.clientToServer;
export const type = new ProtocolRequestType<
SmartApplyCodeParams,
SmartApplyParams,
boolean,
void,
ChatFeatureNotAvailableError | ChatEditDocumentTooLongError | ChatEditCommandTooLongError | ChatEditMutexError,
ChatFeatureNotAvailableError | ChatEditDocumentTooLongError | ChatEditMutexError,
void
>(method);
}

export type SmartApplyCodeParams = {
export type SmartApplyParams = {
location: Location;
applyCode: string;
text: string;
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { StatusProvider } from "./status";
import { CommandProvider } from "./command";
import { name as serverName, version as serverVersion } from "../package.json";
import "./utils/array";
import { SmartApplyFeature } from "./chat/SmartApply";
import { SmartApplyFeature } from "./chat/smartApply";
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
import { FileTracker } from "./codeSearch/fileTracker";

export class Server {
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-chat-panel/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tabby-chat-panel",
"type": "module",
"version": "0.2.1",
"version": "0.2.2",
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
"keywords": [],
"sideEffects": false,
"exports": {
Expand Down
12 changes: 5 additions & 7 deletions clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { GitProvider } from "../git/GitProvider";
import { createClient } from "./chatPanel";
import { ChatFeature } from "../lsp/ChatFeature";
import { isBrowser } from "../env";
import { getLogger } from "../logger";

export class WebviewHelper {
webview?: Webview;
Expand Down Expand Up @@ -576,13 +575,14 @@ export class WebviewHelper {
};
const smartApplyInEditor = async (editor: TextEditor, opts: { languageId: string; smart: boolean }) => {
if (editor.document.languageId !== opts.languageId) {
getLogger().info("editor:", editor.document.languageId, "opts:", opts.languageId);
this.logger.debug("Editor's languageId:", editor.document.languageId, "opts.languageId:", opts.languageId);
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
window.showInformationMessage("The active editor is not in the correct language. Did normal apply.");
applyInEditor(editor);
return;
}

getLogger("Tabby").info("Smart apply in editor started.", content);
this.logger.info("Smart apply in editor started.");
this.logger.trace("Smart apply in editor with content:", { content });
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved

await window.withProgress(
{
Expand All @@ -591,13 +591,11 @@ export class WebviewHelper {
cancellable: true,
},
async (progress, token) => {
progress.report({ increment: 0, message: "Analyzing code..." });
progress.report({ increment: 0, message: "Applying smart edit..." });
try {
getLogger().info("getting provide edit command", content);
progress.report({ increment: 30, message: "Applying smart edit..." });
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
await this.chat?.provideSmartApplyEdit(
{
applyCode: content,
text: content,
location: {
uri: editor.document.uri.toString(),
range: {
Expand Down
3 changes: 0 additions & 3 deletions clients/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Commands } from "./Commands";
import { Status } from "tabby-agent";
import { CodeActions } from "./CodeActions";
import { isBrowser } from "./env";
import { ChatPanelViewProvider } from "./chat/ChatPanelViewProvider";

const logger = getLogger();
let client: Client | undefined = undefined;
Expand Down Expand Up @@ -85,8 +84,6 @@ export async function activate(context: ExtensionContext) {
webviewOptions: { retainContextWhenHidden: true },
}),
);
// Create chat panel view
new ChatPanelViewProvider(context, client.agent, gitProvider, client.chat);
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
await gitProvider.init();
await client.start();

Expand Down
9 changes: 4 additions & 5 deletions clients/vscode/src/lsp/ChatFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
ChatEditToken,
ChatEditResolveRequest,
ChatEditResolveParams,
SmartApplyCodeParams,
SmartApplyCodeRequest,
SmartApplyParams,
SmartApplyRequest,
} from "tabby-agent";

export class ChatFeature extends EventEmitter implements DynamicFeature<unknown> {
Expand Down Expand Up @@ -111,12 +111,11 @@ export class ChatFeature extends EventEmitter implements DynamicFeature<unknown>
return this.client.sendRequest(ChatEditRequest.method, params, token);
}

async provideSmartApplyEdit(params: SmartApplyCodeParams, token?: CancellationToken): Promise<boolean | null> {
async provideSmartApplyEdit(params: SmartApplyParams, token?: CancellationToken): Promise<boolean | null> {
if (!this.isAvailable) {
return null;
}
const res = await this.client.sendRequest(SmartApplyCodeRequest.type, params, token);
return res;
return this.client.sendRequest(SmartApplyRequest.method, params, token);
}

async resolveEdit(params: ChatEditResolveParams): Promise<boolean> {
Expand Down
Loading