Skip to content

Commit

Permalink
refactor(search-strategy): rename StrategyOutput to StrategyFinalPrompt
Browse files Browse the repository at this point in the history
The StrategyOutput class and its instances have been renamed to StrategyFinalPrompt to better reflect their purpose. This change affects the HydeKeywordsStrategy, HydeCodeStrategy, and HydeStrategy classes, as well as the Catalyser class. The output property of the renamed class has also been renamed to prompt.
  • Loading branch information
phodal committed May 24, 2024
1 parent ddc4216 commit 1032bf9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
21 changes: 12 additions & 9 deletions src/agent/catalyser/Catalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { channel } from "../../channel";
import { HydeKeywordsStrategy } from "../../code-search/search-strategy/HydeKeywordsStrategy";
import { SystemActionType } from "../../action/setting/SystemActionType";
import { HydeCodeStrategy } from "../../code-search/search-strategy/HydeCodeStrategy";
import { StrategyOutput } from "../../code-search/search-strategy/_base/StrategyOutput";
import { StrategyFinalPrompt } from "../../code-search/search-strategy/_base/StrategyFinalPrompt";
import { NamedChunk } from "../../code-search/embedding/_base/NamedChunk";
import { TeamTermService } from "../../domain/TeamTermService";
import { QueryExpansion } from "../../domain/QueryExpansion";
Expand Down Expand Up @@ -31,30 +31,33 @@ export class Catalyser {
let expandedQuery = QueryExpansion.instance().expand(query);
channel.append("Expanded query: " + expandedQuery + "\n");

let evaluateOutput: StrategyOutput | undefined = undefined;
let finalPrompt: StrategyFinalPrompt | undefined = undefined;
switch (type) {
case SystemActionType.SemanticSearchKeyword:
let keywordsStrategy = new HydeKeywordsStrategy(expandedQuery, this.extension);
evaluateOutput = await keywordsStrategy.execute();
finalPrompt = await keywordsStrategy.execute();
break;
case SystemActionType.SemanticSearchCode:
let strategy = new HydeCodeStrategy(expandedQuery, this.extension);
evaluateOutput = await strategy.execute();
finalPrompt = await strategy.execute();
break;
default:
channel.append("Unknown action type: " + type + "\n");
break;
}

if (!evaluateOutput) {
if (!finalPrompt) {
channel.append("No output from the strategy\n");
return;
}

if (evaluateOutput.chunks.length > 0) {
channel.append("\n");
channel.appendLine("Found " + evaluateOutput.chunks.length + " code snippets\n");
for (let chunk of evaluateOutput.chunks) {
this.extension.sidebar.webviewProtocol?.request("newSessionWithPrompt", {
prompt: finalPrompt.prompt,
});

if (finalPrompt.chunks.length > 0) {
channel.appendLine("Found " + finalPrompt.chunks.length + " code snippets\n");
for (let chunk of finalPrompt.chunks) {
// a file path will be like: `build.gradle.kts (0-5)`, we need to parse range from name
let rangeResult = chunk.name.match(/\((\d+)-(\d+)\)/);

Expand Down
8 changes: 4 additions & 4 deletions src/code-search/search-strategy/HydeCodeStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DefaultRetrieval } from "../retrieval/DefaultRetrieval";
import { ContextItem, RetrieveOption } from "../retrieval/Retrieval";
import { KeywordEvaluateContext, KeywordsProposeContext } from "./HydeKeywordsStrategy";
import { StreamingMarkdownCodeBlock } from "../../markdown/StreamingMarkdownCodeBlock";
import { StrategyOutput } from "./_base/StrategyOutput";
import { StrategyFinalPrompt } from "./_base/StrategyFinalPrompt";

/**
* Generate hypothetical document base on user input, and then used to retrieve similar code by symbols.
Expand Down Expand Up @@ -105,7 +105,7 @@ export class HydeCodeStrategy implements HydeStrategy<string> {
return docs;
}

async execute(): Promise<StrategyOutput> {
async execute(): Promise<StrategyFinalPrompt> {
channel.appendLine("=".repeat(80));
channel.appendLine(`= Hyde Keywords Strategy: ${this.constructor.name} =`);
channel.appendLine("=".repeat(80));
Expand All @@ -127,12 +127,12 @@ export class HydeCodeStrategy implements HydeStrategy<string> {

if (chunks.length === 0) {
channel.appendLine("No code snippets found.");
return new StrategyOutput("", []);
return new StrategyFinalPrompt("", []);
}

channel.appendLine("\n");
channel.appendLine(" --- Summary --- ");
let evaluateIns = await PromptManager.getInstance().renderHydeTemplate(this.step, this.documentType, evaluateContext);
return new StrategyOutput(await executeIns(evaluateIns), chunks);
return new StrategyFinalPrompt(evaluateIns, chunks);
}
}
8 changes: 4 additions & 4 deletions src/code-search/search-strategy/HydeKeywordsStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DefaultRetrieval } from "../retrieval/DefaultRetrieval";
import { AutoDevExtension } from "../../AutoDevExtension";
import { channel } from "../../channel";
import { ContextItem, RetrieveOption } from "../retrieval/Retrieval";
import { StrategyOutput } from "./_base/StrategyOutput";
import { StrategyFinalPrompt } from "./_base/StrategyFinalPrompt";

/**
* The `HydeKeywordsStrategy` class is a part of the Hyde Strategy pattern and is used to generate keywords from a query.
Expand Down Expand Up @@ -88,7 +88,7 @@ export class HydeKeywordsStrategy implements HydeStrategy<HydeKeywords> {
return docs;
}

async execute() : Promise<StrategyOutput> {
async execute() : Promise<StrategyFinalPrompt> {
channel.appendLine("=".repeat(80));
channel.appendLine(`= Hyde Keywords Strategy: ${this.constructor.name} =`);
channel.appendLine("=".repeat(80));
Expand All @@ -111,13 +111,13 @@ export class HydeKeywordsStrategy implements HydeStrategy<HydeKeywords> {

if (chunkItems.length === 0) {
channel.appendLine("No code snippets found.");
return new StrategyOutput("", []);
return new StrategyFinalPrompt("", []);
}

channel.appendLine("\n");
channel.appendLine(" --- Summary --- ");
let evaluateIns = await PromptManager.getInstance().renderHydeTemplate(this.step, this.documentType, evaluateContext);
return new StrategyOutput(await executeIns(evaluateIns), chunkItems);
return new StrategyFinalPrompt(evaluateIns, chunkItems);
}

private createQueryTerm(keywords: HydeKeywords) {
Expand Down
6 changes: 3 additions & 3 deletions src/code-search/search-strategy/_base/HydeStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChunkItem, Embedding } from "../../embedding/_base/Embedding";
import { HydeDocument, HydeDocumentType } from "./HydeDocument";
import { StrategyOutput } from "./StrategyOutput";
import { StrategyFinalPrompt } from "./StrategyFinalPrompt";
import { CustomActionPrompt } from "../../../prompt-manage/custom-action/CustomActionPrompt";
import { AutoDevStatus, AutoDevStatusManager } from "../../../editor/editor-api/AutoDevStatusManager";
import { LlmProvider } from "../../../llm-provider/LlmProvider";
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface HydeStrategy<T> {
/**
* Executes the method and returns a Promise that resolves with a StrategyOutput object.
*/
execute(): Promise<StrategyOutput>;
execute(): Promise<StrategyFinalPrompt>;
}

export async function executeIns(instruction: string) {
Expand All @@ -76,10 +76,10 @@ export async function executeIns(instruction: string) {
AutoDevStatusManager.instance.setStatus(AutoDevStatus.InProgress);
let response = await LlmProvider.codeCompletion()._streamChat(chatMessages);
for await (let chatMessage of response) {
channel.append(chatMessage.content);
result += chatMessage.content;
}

channel.append(result);
AutoDevStatusManager.instance.setStatus(AutoDevStatus.Done);
return result;
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions src/code-search/search-strategy/_base/StrategyFinalPrompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ChunkItem } from "../../embedding/_base/Embedding";

export class StrategyFinalPrompt {
constructor(public readonly prompt: string, public readonly chunks: ChunkItem[]) {
}
}
6 changes: 0 additions & 6 deletions src/code-search/search-strategy/_base/StrategyOutput.ts

This file was deleted.

0 comments on commit 1032bf9

Please sign in to comment.