Skip to content

Commit

Permalink
[Spelunker] Add .focus command to set focus directory (#655)
Browse files Browse the repository at this point in the history
In a Spelunker session (after `@config request spelunker`) you can now
ask to see the focus by typing `.focus` or change the focus using `focus
<folder>`. Folders can start with `~` indicating `$HOME`, or be absolute
paths or relative paths (relative to whatever the CLI or Shell uses as
its current directory).
  • Loading branch information
gvanrossum-ms authored Feb 3, 2025
1 parent 54273c4 commit ad5e688
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ts/packages/agents/spelunker/src/searchCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export async function searchCode(
}

// 4. Construct a prompt from those chunks.
console_log(`[Step 4: Construct a prompt for the smart LLM]`);
console_log(`[Step 4: Construct a prompt for the oracle]`);
const preppedChunks: Chunk[] = chunkDescs
.map((chunkDesc) => prepChunk(chunkDesc, allChunks))
.filter(Boolean) as Chunk[];
Expand Down
61 changes: 50 additions & 11 deletions ts/packages/agents/spelunker/src/spelunkerActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ class RequestCommandHandler implements CommandHandler {
params: ParsedCommandParams<ParameterDefinitions>,
): Promise<void> {
if (typeof params.args?.question === "string") {
const result: ActionResult = await searchCode(
actionContext.sessionContext.agentContext,
params.args.question,
[],
[],
);
let result: ActionResult;
const question = params.args.question.trim();
if (question.startsWith(".")) {
result = handleFocus(actionContext.sessionContext, question);
} else {
result = await searchCode(
actionContext.sessionContext.agentContext,
question,
[],
[],
);
}
if (typeof result.error == "string") {
actionContext.actionIO.appendDisplay({
type: "text",
Expand Down Expand Up @@ -154,13 +160,11 @@ async function handleSpelunkerAction(
): Promise<ActionResult> {
switch (action.actionName) {
case "searchCode": {
if (
typeof action.parameters.question == "string" &&
action.parameters.question.trim()
) {
const question = action.parameters.question.trim();
if (typeof question == "string" && question) {
return await searchCode(
context.agentContext,
action.parameters.question,
question,
action.parameters.entityUniqueIds,
entities,
);
Expand Down Expand Up @@ -223,3 +227,38 @@ function focusReport(
];
return createActionResult(literalText, undefined, entities);
}

function handleFocus(
sessionContext: SessionContext<SpelunkerContext>,
question: string,
): ActionResult {
question = question.trim();
if (!question.startsWith(".")) {
throw new Error("handleFocus requires a question starting with '.'");
}
const spelunkerContext: SpelunkerContext = sessionContext.agentContext;
const words = question.split(/\s+/);
if (words[0] != ".focus") {
const text = `Unknown '.' command (${words[0]}) -- try .focus`;
return createActionResult(text);
}
if (words.length < 2) {
return focusReport(
sessionContext.agentContext,
"Focus is empty",
"Focus is",
);
}
spelunkerContext.focusFolders = [
...words
.slice(1)
.map((folder) => path.resolve(expandHome(folder)))
.filter((f) => fs.existsSync(f) && fs.statSync(f).isDirectory()),
];
saveContext(sessionContext);
return focusReport(
sessionContext.agentContext,
"Focus cleared",
"Focus set to",
);
}

0 comments on commit ad5e688

Please sign in to comment.