From e9ce38472f26a268bc59263ff09637e050369f1b Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Thu, 1 May 2025 18:18:39 -0700 Subject: [PATCH] [PROTOBUS] Move commitSearch to protobus (#3229) * commitSearch protobus migration * rename * one small change to comments * moved GitCommmit proto mapping to proto-conversions --- .changeset/funny-numbers-trade.md | 5 + proto/file.proto | 19 +- src/core/controller/file/methods.ts | 2 + src/core/controller/file/searchCommits.ts | 31 +++ src/core/controller/index.ts | 15 -- .../file/git-commit-conversion.ts | 28 +++ src/shared/proto/file.ts | 207 ++++++++++++++++++ .../src/components/chat/ChatTextArea.tsx | 33 +-- 8 files changed, 309 insertions(+), 31 deletions(-) create mode 100644 .changeset/funny-numbers-trade.md create mode 100644 src/core/controller/file/searchCommits.ts create mode 100644 src/shared/proto-conversions/file/git-commit-conversion.ts diff --git a/.changeset/funny-numbers-trade.md b/.changeset/funny-numbers-trade.md new file mode 100644 index 00000000..014eba53 --- /dev/null +++ b/.changeset/funny-numbers-trade.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +searchCommits protobus migration diff --git a/proto/file.proto b/proto/file.proto index 5f7dc3c2..dd1815a6 100644 --- a/proto/file.proto +++ b/proto/file.proto @@ -10,7 +10,7 @@ import "common.proto"; service FileService { // Opens a file in the editor rpc openFile(StringRequest) returns (Empty); - + // Opens an image in the system viewer rpc openImage(StringRequest) returns (Empty); @@ -19,6 +19,23 @@ service FileService { // Creates a rule file from either global or workspace rules directory rpc createRuleFile(RuleFileRequest) returns (RuleFile); + + // Search git commits in the workspace + rpc searchCommits(StringRequest) returns (GitCommits); +} + +// Response for searchCommits +message GitCommits { + repeated GitCommit commits = 1; +} + +// Represents a Git commit +message GitCommit { + string hash = 1; + string short_hash = 2; + string subject = 3; + string author = 4; + string date = 5; } // Unified request for all rule file operations diff --git a/src/core/controller/file/methods.ts b/src/core/controller/file/methods.ts index 8c22621d..16d93f34 100644 --- a/src/core/controller/file/methods.ts +++ b/src/core/controller/file/methods.ts @@ -7,6 +7,7 @@ import { createRuleFile } from "./createRuleFile" import { deleteRuleFile } from "./deleteRuleFile" import { openFile } from "./openFile" import { openImage } from "./openImage" +import { searchCommits } from "./searchCommits" // Register all file service methods export function registerAllMethods(): void { @@ -15,4 +16,5 @@ export function registerAllMethods(): void { registerMethod("deleteRuleFile", deleteRuleFile) registerMethod("openFile", openFile) registerMethod("openImage", openImage) + registerMethod("searchCommits", searchCommits) } diff --git a/src/core/controller/file/searchCommits.ts b/src/core/controller/file/searchCommits.ts new file mode 100644 index 00000000..772be381 --- /dev/null +++ b/src/core/controller/file/searchCommits.ts @@ -0,0 +1,31 @@ +import { Controller } from ".." +import { GitCommits } from "@shared/proto/file" +import { StringRequest } from "@shared/proto/common" +import { searchCommits as searchCommitsUtil } from "@utils/git" +import { getWorkspacePath } from "@utils/path" +import { FileMethodHandler } from "./index" +import { convertGitCommitsToProtoGitCommits } from "@shared/proto-conversions/file/git-commit-conversion" + +/** + * Searches for git commits in the workspace repository + * @param controller The controller instance + * @param request The request message containing the search query in the 'value' field + * @returns GitCommits containing the matching commits + */ +export const searchCommits: FileMethodHandler = async (controller: Controller, request: StringRequest): Promise => { + const cwd = getWorkspacePath() + if (!cwd) { + return GitCommits.create({ commits: [] }) + } + + try { + const commits = await searchCommitsUtil(request.value || "", cwd) + + const protoCommits = convertGitCommitsToProtoGitCommits(commits) + + return GitCommits.create({ commits: protoCommits }) + } catch (error) { + console.error(`Error searching commits: ${JSON.stringify(error)}`) + return GitCommits.create({ commits: [] }) + } +} diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index d8b4dbfd..fd06abcb 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -538,21 +538,6 @@ export class Controller { this.mcpHub?.sendLatestMcpServers() break } - case "searchCommits": { - const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) - if (cwd) { - try { - const commits = await searchCommits(message.text || "", cwd) - await this.postMessageToWebview({ - type: "commitSearchResults", - commits, - }) - } catch (error) { - console.error(`Error searching commits: ${JSON.stringify(error)}`) - } - } - break - } case "openExtensionSettings": { const settingsFilter = message.text || "" await vscode.commands.executeCommand( diff --git a/src/shared/proto-conversions/file/git-commit-conversion.ts b/src/shared/proto-conversions/file/git-commit-conversion.ts new file mode 100644 index 00000000..16d31cd3 --- /dev/null +++ b/src/shared/proto-conversions/file/git-commit-conversion.ts @@ -0,0 +1,28 @@ +import { GitCommit as ProtoGitCommit } from "@shared/proto/file" +import { GitCommit } from "@utils/git" + +/** + * Converts domain GitCommit objects to proto GitCommit objects + */ +export function convertGitCommitsToProtoGitCommits(commits: GitCommit[]): ProtoGitCommit[] { + return commits.map((commit) => ({ + hash: commit.hash, + shortHash: commit.shortHash, + subject: commit.subject, + author: commit.author, + date: commit.date, + })) +} + +/** + * Converts proto GitCommit objects to domain GitCommit objects + */ +export function convertProtoGitCommitsToGitCommits(protoCommits: ProtoGitCommit[]): GitCommit[] { + return protoCommits.map((protoCommit) => ({ + hash: protoCommit.hash, + shortHash: protoCommit.shortHash, + subject: protoCommit.subject, + author: protoCommit.author, + date: protoCommit.date, + })) +} diff --git a/src/shared/proto/file.ts b/src/shared/proto/file.ts index 1960aa7e..a366db06 100644 --- a/src/shared/proto/file.ts +++ b/src/shared/proto/file.ts @@ -10,6 +10,20 @@ import { Empty, Metadata, StringRequest } from "./common" export const protobufPackage = "cline" +/** Response for searchCommits */ +export interface GitCommits { + commits: GitCommit[] +} + +/** Represents a Git commit */ +export interface GitCommit { + hash: string + shortHash: string + subject: string + author: string + date: string +} + /** Unified request for all rule file operations */ export interface RuleFileRequest { metadata?: Metadata | undefined @@ -31,6 +45,190 @@ export interface RuleFile { alreadyExists: boolean } +function createBaseGitCommits(): GitCommits { + return { commits: [] } +} + +export const GitCommits: MessageFns = { + encode(message: GitCommits, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + for (const v of message.commits) { + GitCommit.encode(v!, writer.uint32(10).fork()).join() + } + return writer + }, + + decode(input: BinaryReader | Uint8Array, length?: number): GitCommits { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input) + let end = length === undefined ? reader.len : reader.pos + length + const message = createBaseGitCommits() + while (reader.pos < end) { + const tag = reader.uint32() + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break + } + + message.commits.push(GitCommit.decode(reader, reader.uint32())) + continue + } + } + if ((tag & 7) === 4 || tag === 0) { + break + } + reader.skip(tag & 7) + } + return message + }, + + fromJSON(object: any): GitCommits { + return { + commits: globalThis.Array.isArray(object?.commits) ? object.commits.map((e: any) => GitCommit.fromJSON(e)) : [], + } + }, + + toJSON(message: GitCommits): unknown { + const obj: any = {} + if (message.commits?.length) { + obj.commits = message.commits.map((e) => GitCommit.toJSON(e)) + } + return obj + }, + + create, I>>(base?: I): GitCommits { + return GitCommits.fromPartial(base ?? ({} as any)) + }, + fromPartial, I>>(object: I): GitCommits { + const message = createBaseGitCommits() + message.commits = object.commits?.map((e) => GitCommit.fromPartial(e)) || [] + return message + }, +} + +function createBaseGitCommit(): GitCommit { + return { hash: "", shortHash: "", subject: "", author: "", date: "" } +} + +export const GitCommit: MessageFns = { + encode(message: GitCommit, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.hash !== "") { + writer.uint32(10).string(message.hash) + } + if (message.shortHash !== "") { + writer.uint32(18).string(message.shortHash) + } + if (message.subject !== "") { + writer.uint32(26).string(message.subject) + } + if (message.author !== "") { + writer.uint32(34).string(message.author) + } + if (message.date !== "") { + writer.uint32(42).string(message.date) + } + return writer + }, + + decode(input: BinaryReader | Uint8Array, length?: number): GitCommit { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input) + let end = length === undefined ? reader.len : reader.pos + length + const message = createBaseGitCommit() + while (reader.pos < end) { + const tag = reader.uint32() + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break + } + + message.hash = reader.string() + continue + } + case 2: { + if (tag !== 18) { + break + } + + message.shortHash = reader.string() + continue + } + case 3: { + if (tag !== 26) { + break + } + + message.subject = reader.string() + continue + } + case 4: { + if (tag !== 34) { + break + } + + message.author = reader.string() + continue + } + case 5: { + if (tag !== 42) { + break + } + + message.date = reader.string() + continue + } + } + if ((tag & 7) === 4 || tag === 0) { + break + } + reader.skip(tag & 7) + } + return message + }, + + fromJSON(object: any): GitCommit { + return { + hash: isSet(object.hash) ? globalThis.String(object.hash) : "", + shortHash: isSet(object.shortHash) ? globalThis.String(object.shortHash) : "", + subject: isSet(object.subject) ? globalThis.String(object.subject) : "", + author: isSet(object.author) ? globalThis.String(object.author) : "", + date: isSet(object.date) ? globalThis.String(object.date) : "", + } + }, + + toJSON(message: GitCommit): unknown { + const obj: any = {} + if (message.hash !== "") { + obj.hash = message.hash + } + if (message.shortHash !== "") { + obj.shortHash = message.shortHash + } + if (message.subject !== "") { + obj.subject = message.subject + } + if (message.author !== "") { + obj.author = message.author + } + if (message.date !== "") { + obj.date = message.date + } + return obj + }, + + create, I>>(base?: I): GitCommit { + return GitCommit.fromPartial(base ?? ({} as any)) + }, + fromPartial, I>>(object: I): GitCommit { + const message = createBaseGitCommit() + message.hash = object.hash ?? "" + message.shortHash = object.shortHash ?? "" + message.subject = object.subject ?? "" + message.author = object.author ?? "" + message.date = object.date ?? "" + return message + }, +} + function createBaseRuleFileRequest(): RuleFileRequest { return { metadata: undefined, isGlobal: false, rulePath: undefined, filename: undefined } } @@ -274,6 +472,15 @@ export const FileServiceDefinition = { responseStream: false, options: {}, }, + /** Search git commits in the workspace */ + searchCommits: { + name: "searchCommits", + requestType: StringRequest, + requestStream: false, + responseType: GitCommits, + responseStream: false, + options: {}, + }, }, } as const diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index 96b87529..bca0e812 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -27,6 +27,7 @@ import { import { useMetaKeyDetection, useShortcut } from "@/utils/hooks" import { validateApiConfiguration, validateModelId } from "@/utils/validate" import { vscode } from "@/utils/vscode" +import { FileServiceClient } from "@/services/grpc-client" import { CODE_BLOCK_BG_COLOR } from "@/components/common/CodeBlock" import Thumbnails from "@/components/common/Thumbnails" import Tooltip from "@/components/common/Tooltip" @@ -282,27 +283,29 @@ const ChatTextArea = forwardRef( // Fetch git commits when Git is selected or when typing a hash useEffect(() => { if (selectedType === ContextMenuOptionType.Git || /^[a-f0-9]+$/i.test(searchQuery)) { - vscode.postMessage({ - type: "searchCommits", - text: searchQuery || "", - }) + FileServiceClient.searchCommits({ value: searchQuery || "" }) + .then((response) => { + if (response.commits) { + const commits: GitCommit[] = response.commits.map( + (commit: { hash: string; shortHash: string; subject: string; author: string; date: string }) => ({ + type: ContextMenuOptionType.Git, + value: commit.hash, + label: commit.subject, + description: `${commit.shortHash} by ${commit.author} on ${commit.date}`, + }), + ) + setGitCommits(commits) + } + }) + .catch((error) => { + console.error("Error searching commits:", error) + }) } }, [selectedType, searchQuery]) const handleMessage = useCallback((event: MessageEvent) => { const message: ExtensionMessage = event.data switch (message.type) { - case "commitSearchResults": { - const commits: GitCommit[] = - message.commits?.map((commit) => ({ - type: ContextMenuOptionType.Git, - value: commit.hash, - label: commit.subject, - description: `${commit.shortHash} by ${commit.author} on ${commit.date}`, - })) || [] - setGitCommits(commits) - break - } case "relativePathsResponse": { // New case for batch response const validPaths = message.paths?.filter((path): path is string => !!path) || []