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: CodeLens workflow #150

Merged
merged 12 commits into from
Oct 16, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"url": "https://github.com/smallcloudai/refact-vscode/issues",
"email": "[email protected]"
},
"version": "5.0.107",
"version": "5.0.109",
"dependencies": {
"@types/marked": "^4.0.8",
"@types/vscode": "^1.69.0",
Expand Down
3 changes: 3 additions & 0 deletions src/chatTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,8 @@ export async function chat_model_get(): Promise<[string, string]>
return ["", ""];
}

console.log(`[DEBUG]: context.globalState: `, context.globalState);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the debug statements?


try {
await global.rust_binary_blob?.read_caps();
} catch {
Expand Down Expand Up @@ -1011,6 +1013,7 @@ export async function chat_model_set(chat_model: string, model_function: string)
if (!chat_model) {
return;
}
console.log(`[DEBUG]: globalState: `, context.globalState);
await context.globalState.update("chat_model", chat_model);
await context.globalState.update("chat_model_function", model_function);
}
Expand Down
75 changes: 71 additions & 4 deletions src/codeLens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
/* eslint-disable @typescript-eslint/naming-convention */
import * as vscode from 'vscode';
import * as path from 'path';
import * as estate from "./estate";
import * as fetchH2 from 'fetch-h2';
import * as fetchAPI from "./fetchAPI";
import {
type ChatMessages,
type ChatMessage,
type ToolUse,
setInputValue,
} from "refact-chat-js/dist/events";
import { chat_model_get } from './chatTab';


class ExperimentalLens extends vscode.CodeLens {
Expand All @@ -15,12 +23,12 @@ class ExperimentalLens extends vscode.CodeLens {
super(range, {
title: msg,
command: 'refactaicmd.codeLensClicked',
arguments: [arg0, arg1]
arguments: [arg0, arg1, range]
});
}
}


export let custom_code_lens: { [key: string]: any } | null = null;
export class LensProvider implements vscode.CodeLensProvider
{
public notifyCodeLensesChanged: vscode.EventEmitter<void>;
Expand Down Expand Up @@ -57,16 +65,17 @@ export class LensProvider implements vscode.CodeLensProvider
if (response.status !== 200) {
console.log([`${url} http status`, response.status]);
} else if ("code_lens" in customization) {
const custom_code_lens = customization["code_lens"] as { [key: string]: any };
custom_code_lens = customization["code_lens"] as { [key: string]: any };
const this_file_lens = await response.json();
console.log(`[DEBUG]: this_file_lens: `, this_file_lens);
if ("detail" in this_file_lens) {
console.log(["/v1/code-lens error", this_file_lens["detail"]]);
}
if ("code_lens" in this_file_lens) {
for (let item of this_file_lens["code_lens"]) {
let range = new vscode.Range(item["line1"] - 1, 0, item["line2"] - 1, 0);
for (const [key, lensdict] of Object.entries(custom_code_lens)) {
lenses.push(new ExperimentalLens(range, lensdict["label"], `"CUSTOMLENS:${key}`, item["spath"]));
lenses.push(new ExperimentalLens(range, lensdict["label"], `CUSTOMLENS:${key}`, item["spath"]));
}
}
}
Expand All @@ -84,6 +93,63 @@ export class LensProvider implements vscode.CodeLensProvider
}
}

const sendCodeLensToChat = (messages: {content: string; role: string;}[], relative_path: string, text: string, auto_submit: boolean = false) => {
if (!global || !global.side_panel || !global.side_panel._view) {
return;
}

const messageBlock = messages.find((message: {content: string; role: string;}) => message.role === "user")?.content
.replace("%CURRENT_FILE%", relative_path)
.replace("%CURSOR_LINE%", "")
.replace("%CODE_SELECTION%", text);

// TODO: send auto_submit somehow?
const message = setInputValue({
value: messageBlock ? messageBlock : text,
send_immediately: auto_submit
});
global.side_panel._view.webview.postMessage(message);
};

export async function code_lens_execute(code_lens: string, range: any) {
if (custom_code_lens) {
const auto_submit = custom_code_lens[code_lens]["auto_submit"];
const new_tab = custom_code_lens[code_lens]["new_tab"];
let messages: {content: string; role: string;}[] = custom_code_lens[code_lens]["messages"];

const start_of_line = new vscode.Position(range.start.line, 0);
const end_of_line = new vscode.Position(range.end.line + 1, 0);
const block_range = new vscode.Range(start_of_line, end_of_line);

const file_path = vscode.window.activeTextEditor?.document.fileName || "";
const workspaceFolders = vscode.workspace.workspaceFolders;
let relative_path: string = "";

if (workspaceFolders) {
const workspacePath = workspaceFolders[0].uri.fsPath;
relative_path = path.relative(workspacePath, file_path);
}

let text = vscode.window.activeTextEditor!.document.getText(block_range);

if (messages.length === 0) {
vscode.commands.executeCommand('refactaicmd.callChat', '');
return;
}

if (global && global.side_panel && global.side_panel._view && global.side_panel._view.visible) {
const current_page = global.side_panel.context.globalState.get("chat_page");
if (typeof current_page === "string" && current_page !== '"chat"' || new_tab) {
vscode.commands.executeCommand('refactaicmd.callChat', '');
}
sendCodeLensToChat(messages, relative_path, text, auto_submit);
} else {
vscode.commands.executeCommand('refactaicmd.callChat', '');
sendCodeLensToChat(messages, relative_path, text, auto_submit);
}
}
}


export var global_provider: LensProvider | null = null;

Expand All @@ -97,6 +163,7 @@ export function save_provider(provider: LensProvider)
export function quick_refresh()
{
if (global_provider) {
console.log(`[DEBUG]: refreshing code lens!`);
global_provider.notifyCodeLensesChanged.fire();
}
}
57 changes: 36 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Mode } from "./estate";
import { fileURLToPath } from 'url';
import { ChatTab } from './chatTab';
import { FimDebugData } from 'refact-chat-js/dist/events/index.js';
import { code_lens_execute } from './codeLens';

declare global {
var rust_binary_blob: launchRust.RustBinaryBlob|undefined;
Expand Down Expand Up @@ -141,7 +142,7 @@ async function pressed_tab()
}


async function code_lens_clicked(arg0: any)
async function code_lens_clicked(arg0: any, arg1: any, range: vscode.Range)
{
let editor = vscode.window.activeTextEditor;
if (editor) {
Expand Down Expand Up @@ -170,7 +171,10 @@ async function code_lens_clicked(arg0: any)
// await vscode.commands.executeCommand('editor.action.inlineSuggest.hide');
// await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger');
} else {
console.log(["code_lens_clicked: can't do", arg0]);
if (arg0.startsWith('CUSTOMLENS:')) {
let custom_lens_name = arg0.substring("CUSTOMLENS:".length);
code_lens_execute(custom_lens_name, range);
}
}
}
}
Expand Down Expand Up @@ -233,25 +237,25 @@ export function activate(context: vscode.ExtensionContext)
const comp = new completionProvider.MyInlineCompletionProvider();
vscode.languages.registerInlineCompletionItemProvider({pattern: "**"}, comp);

const quickProvider = new QuickActionProvider();
vscode.languages.registerCodeActionsProvider({pattern: "**"},quickProvider,
{
providedCodeActionKinds: [
// vscode.CodeActionKind.RefactorRewrite,
vscode.CodeActionKind.QuickFix,
],
}
);
context.subscriptions.push(quickProvider);

for (const action of QuickActionProvider.actions_static_list) {
context.subscriptions.push(
vscode.commands.registerCommand(
`refactcmd.${action.id}`,
(actionId: string, diagnosticMessage: string) => QuickActionProvider.handleAction(actionId, diagnosticMessage)
)
);
}
// const quickProvider = new QuickActionProvider();
// vscode.languages.registerCodeActionsProvider({pattern: "**"},quickProvider,
// {
// providedCodeActionKinds: [
// // vscode.CodeActionKind.RefactorRewrite,
// vscode.CodeActionKind.QuickFix,
// ],
// }
// );
// context.subscriptions.push(quickProvider);

// for (const action of QuickActionProvider.actions_static_list) {
// context.subscriptions.push(
// vscode.commands.registerCommand(
// `refactcmd.${action.id}`,
// (actionId: string, diagnosticMessage: string) => QuickActionProvider.handleAction(actionId, diagnosticMessage)
// )
// );
// }

let disposable4 = vscode.commands.registerCommand('refactaicmd.esc', pressed_escape);
let disposable5 = vscode.commands.registerCommand('refactaicmd.tab', pressed_tab);
Expand Down Expand Up @@ -383,6 +387,17 @@ export function activate(context: vscode.ExtensionContext)
}
}
});

const quickProvider = new QuickActionProvider();
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider(
{ pattern: "**" },
quickProvider,
{
providedCodeActionKinds: QuickActionProvider.providedCodeActionKinds
}
)
);
}

export async function rollback_and_regen(editor: vscode.TextEditor)
Expand Down
1 change: 1 addition & 0 deletions src/fetchAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ type AtParamDict = {

type AtToolFunction = {
name: string;
agentic: boolean;
description: string;
parameters: AtParamDict[];
parameters_required: string[];
Expand Down
8 changes: 8 additions & 0 deletions src/launchRust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { join } from 'path';
import * as lspClient from 'vscode-languageclient/node';
import * as net from 'net';
import { register_commands } from './rconsoleCommands';
import { QuickActionProvider } from './quickProvider';


const DEBUG_HTTP_PORT = 8001;
Expand Down Expand Up @@ -231,6 +232,13 @@ export class RustBinaryBlob
global.chat_default_model = json["code_chat_default_model"] || "";
global.have_caps = true;
global.status_bar.set_socket_error(false, "");

const promptCustomization = await fetchAPI.get_prompt_customization();
if (promptCustomization && promptCustomization.toolbox_commands) {
await QuickActionProvider.updateActions(promptCustomization.toolbox_commands as Record<string, ToolboxCommand>);
}


} catch (e) {
global.chat_models = [];
global.have_caps = false;
Expand Down
Loading
Loading