From f3cd4472935242a5d0a69b45ec56a4835c8f6b36 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Thu, 14 Nov 2024 10:43:41 +0800 Subject: [PATCH] revert fileContents and add meta filter --- src/extension/chat-service.ts | 24 ++++++++++++++++++++++++ src/extension/providers/base.ts | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/extension/chat-service.ts b/src/extension/chat-service.ts index 106dab56..793c1824 100644 --- a/src/extension/chat-service.ts +++ b/src/extension/chat-service.ts @@ -27,6 +27,7 @@ import { import { CodeLanguageDetails } from "../common/languages" import { logger } from "../common/logger" import { + FileItem, Message, RequestBodyBase, ServerMessage, @@ -529,8 +530,23 @@ export class ChatService extends Base { return combinedContext.trim() || null } + private async loadFileContents(files: FileItem[]): Promise { + if (!files?.length) return "" + let fileContents = "" + for (const file of files) { + try { + const content = await fs.readFile(file.path, "utf-8") + fileContents += `File: ${file.name}\n\n${content}\n\n` + } catch (error) { + console.error(`Error reading file ${file.path}:`, error) + } + } + return fileContents.trim() + } + public async streamChatCompletion( messages: Message[], + filePaths: FileItem[] ) { this._completion = "" this.sendEditorLanguage() @@ -561,6 +577,14 @@ export class ChatService extends Base { additionalContext += `Additional Context:\n${ragContext}\n\n` } + filePaths = filePaths.filter((filepath) => + filepath.name !== "workspace" && filepath.name !== "problems" + ) + const fileContents = await this.loadFileContents(filePaths) + if (fileContents) { + additionalContext += `File Contents:\n${fileContents}\n\n` + } + const provider = this.getProvider() if (!provider) return diff --git a/src/extension/providers/base.ts b/src/extension/providers/base.ts index 7684555f..c0070d71 100644 --- a/src/extension/providers/base.ts +++ b/src/extension/providers/base.ts @@ -13,6 +13,7 @@ import { logger } from "../../common/logger" import { ApiModel, ClientMessage, + FileItem, InferenceRequest, Message, ServerMessage @@ -354,6 +355,7 @@ export class BaseProvider { this._chatService?.streamChatCompletion( data.data || [], + data.meta as FileItem[] ) }