From be835ac788813e995e9996cbcf08561fde836d11 Mon Sep 17 00:00:00 2001 From: Samyak Jain <56000318+samyakkkk@users.noreply.github.com> Date: Fri, 14 Jun 2024 22:43:00 +0530 Subject: [PATCH 1/2] (feat): add surrounding code --- commanddash/lib/steps/chat/chat_step.dart | 147 +++++++++++----------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/commanddash/lib/steps/chat/chat_step.dart b/commanddash/lib/steps/chat/chat_step.dart index 194bfd2..ea044ac 100644 --- a/commanddash/lib/steps/chat/chat_step.dart +++ b/commanddash/lib/steps/chat/chat_step.dart @@ -85,86 +85,83 @@ class ChatStep extends Step { final Map nestedCodes = {}; // This function is commented out because it is not used as contextual code is not added for now. - // void appendNestedCodeCount(String filePath, {int priority = 1}) { - // nestedCodes[filePath] = (nestedCodes[filePath] ?? 0) + priority; - // } + void appendNestedCodeCount(String filePath, {int priority = 1}) { + nestedCodes[filePath] = (nestedCodes[filePath] ?? 0) + priority; + } + + void markIncludedInPrompt( + {required String path, required List ranges}) { + final existingFileIndex = + includedInPrompt.indexWhere((file) => file.path == path); + if (existingFileIndex != -1) { + includedInPrompt[existingFileIndex].selectedRanges.addAll(ranges); + } else { + includedInPrompt + .add(WorkspaceFile.fromPath(path, selectedRanges: ranges)); + } + } - // void markIncludedInPrompt( - // {required String path, required List ranges}) { - // final existingFileIndex = - // includedInPrompt.indexWhere((file) => file.path == path); - // if (existingFileIndex != -1) { - // includedInPrompt[existingFileIndex].selectedRanges.addAll(ranges); - // } else { - // includedInPrompt - // .add(WorkspaceFile.fromPath(path, selectedRanges: ranges)); - // } - // } + for (CodeInput code in codeInputs) { + /// add the code file itself as context + appendNestedCodeCount(code.filePath, priority: 10); + markIncludedInPrompt(path: code.filePath, ranges: [code.range]); - // Skipping adding contextual code for now due to the lack of a consistent way to retrieve document definitions for all languages. - // This feature may be added in the future when a reliable method becomes available. - // Commented out the code below that adds contextual code. - // for (CodeInput code in codeInputs) { - // /// add the code file itself as context - // appendNestedCodeCount(code.filePath, priority: 10); + // Skipping adding contextual code for now due to the lack of a consistent way to retrieve document definitions for all languages. + // This feature may be added in the future when a reliable method becomes available. - // final data = await taskAssist.processStep( - // kind: "context", - // args: { - // "filePath": code.filePath, - // "range": code.range.toJson(), - // }, - // timeoutKind: TimeoutKind.stretched, - // ); - // final context = data['context']; - // if (context != null) { - // final listOfContext = List>.from(context); - // for (final nestedCode in listOfContext) { - // final filePath = nestedCode['filePath']; - // appendNestedCodeCount(filePath); - // } - // } - // } + // final data = await taskAssist.processStep( + // kind: "context", + // args: { + // "filePath": code.filePath, + // "range": code.range.toJson(), + // }, + // timeoutKind: TimeoutKind.stretched, + // ); + // final context = data['context']; + // if (context != null) { + // final listOfContext = List>.from(context); + // for (final nestedCode in listOfContext) { + // final filePath = nestedCode['filePath']; + // appendNestedCodeCount(filePath); + // } + // } + } // nestedCode sorted by frequency - // final sortedNestedCode = nestedCodes.entries.toList() - // ..sort(((a, b) { - // return b.value.compareTo(a.value); - // })); - // String contextualCodePrefix = - // "[CONTEXTUAL CODE FOR YOUR INFORMATION FROM USER PROJECT]\n\n"; - // String contextualCode = ""; + final sortedNestedCode = nestedCodes.entries.toList() + ..sort(((a, b) { + return b.value.compareTo(a.value); + })); + String contextualCodePrefix = + "[CONTEXTUAL CODE FOR YOUR INFORMATION FROM USER PROJECT]\n\n"; + String contextualCode = ""; + + for (final nestedFilePath in sortedNestedCode.map((e) => e.key)) { + final includedInPromptIndex = includedInPrompt + .indexWhere((element) => element.path == nestedFilePath); + if (includedInPromptIndex != -1) { + final content = + includedInPrompt[includedInPromptIndex].surroundingContent; + if (content != null) { + if (availableToken - content.length > 0) { + contextualCode = + '$contextualCode$nestedFilePath\n```$content```\n\n'; + availableToken -= content.length; + } + } + continue; + } + final content = (await File(nestedFilePath).readAsString()) + .replaceAll(RegExp(r"[\n\s]+"), ""); + if (availableToken - content.length < 0) continue; + contextualCode = '$contextualCode$nestedFilePath\n```$content```\n\n'; + availableToken -= content.length; + } + if (contextualCode.isNotEmpty) { + contextualCode = + '$contextualCodePrefix$contextualCode\n\n[END OF CONTEXTUAL CODE.]\n\n'; + prompt = '$contextualCode$prompt'; + } - // ///TODO: Figure out a way to attach the most relevant part of the file if the full file is extremely long - // for (final nestedFilePath in sortedNestedCode.map((e) => e.key)) { - // final includedInPromptIndex = includedInPrompt - // .indexWhere((element) => element.path == nestedFilePath); - // if (includedInPromptIndex != -1) { - // final content = - // includedInPrompt[includedInPromptIndex].surroundingContent; - // if (content != null) { - // if (availableToken - content.length > 0) { - // contextualCode = - // '$contextualCode$nestedFilePath\n```$content```\n\n'; - // availableToken -= content.length; - // } - // } - // continue; - // } - // final content = (await File(nestedFilePath).readAsString()) - // .replaceAll(RegExp(r"[\n\s]+"), ""); - // if (content.length > 9500) { - // continue; // Don't include extremely large nested code files. - // } - // if (availableToken - content.length < 0) continue; - // contextualCode = '$contextualCode$nestedFilePath\n```$content```\n\n'; - // availableToken -= content.length; - // } - // if (contextualCode.isNotEmpty) { - // contextualCode = - // '$contextualCodePrefix$contextualCode\n\n[END OF CONTEXTUAL CODE.]\n\n'; - // prompt = '$contextualCode$prompt'; - // } - print(prompt); var filesInvolved = Set.from( includedInPrompt.map((e) => e.path).toList() + nestedCodes.keys.toList()) From c32ff22d5371902c2c1fc7ee669c1e292488e6f1 Mon Sep 17 00:00:00 2001 From: Samyak Jain <56000318+samyakkkk@users.noreply.github.com> Date: Fri, 14 Jun 2024 22:45:43 +0530 Subject: [PATCH 2/2] Release 0.1.3 --- commanddash/lib/runner.dart | 4 ++-- commanddash/pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commanddash/lib/runner.dart b/commanddash/lib/runner.dart index 8aee8a8..86eea0b 100644 --- a/commanddash/lib/runner.dart +++ b/commanddash/lib/runner.dart @@ -33,7 +33,7 @@ class VersionCommand extends Command { @override void run() { /// It is not possible to fetch version from pubspec.yaml hence assigning manually - print('0.1.2'); + print('0.1.3'); } } @@ -49,6 +49,6 @@ class MinimumClientVersionCommand extends Command { @override void run() { - print('0.4.4'); + print('0.4.0'); } } diff --git a/commanddash/pubspec.yaml b/commanddash/pubspec.yaml index ceaeb1e..f9d5462 100644 --- a/commanddash/pubspec.yaml +++ b/commanddash/pubspec.yaml @@ -1,6 +1,6 @@ name: commanddash description: Processing engine for CommandDash clients like the IDEs -version: 0.1.1 +version: 0.1.3 repository: https://github.com/Welltested-AI/commanddash environment: