Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/runtime/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import { runTemplates } from "./template.js";
import { buildExecuteShellCommand } from "./utils.js";
import { endTiming, startTiming } from "../utils/performance.js";

const getGeneratorContext = (cwd: string): Fig.GeneratorContext => {
const getGeneratorContext = (cwd: string, searchTerm: string): Fig.GeneratorContext => {
return {
environmentVariables: Object.fromEntries(Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] != null)),
currentWorkingDirectory: cwd,
currentProcess: "", // TODO: define current process
sshPrefix: "", // deprecated, should be empty
isDangerous: false,
searchTerm: "", // TODO: define search term
searchTerm,
};
};

export const executeGenerator = async (generator: Fig.Generator, tokens: string[], cwd: string, signal?: AbortSignal): Promise<Fig.Suggestion[]> => {
export const executeGenerator = async (
generator: Fig.Generator,
tokens: string[],
searchTerm: string,
cwd: string,
signal?: AbortSignal,
): Promise<Fig.Suggestion[]> => {
const generatorTiming = startTiming();
try {
signal?.throwIfAborted();
Expand All @@ -39,7 +45,7 @@ export const executeGenerator = async (generator: Fig.Generator, tokens: string[
}

if (custom) {
suggestions.push(...(await custom(tokens, executeShellCommand, getGeneratorContext(cwd))));
suggestions.push(...(await custom(tokens, executeShellCommand, getGeneratorContext(cwd, searchTerm))));
}

if (template != null) {
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/generatorCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ const waitForDebounce = async (signal?: AbortSignal): Promise<void> => {
});
};

const executeGeneratorSafely = async (generator: Fig.Generator, tokens: string[], cwd: string, signal?: AbortSignal): Promise<GeneratorResult> => {
const executeGeneratorSafely = async (
generator: Fig.Generator,
tokens: string[],
activeToken: string,
cwd: string,
signal?: AbortSignal,
): Promise<GeneratorResult> => {
try {
return { suggestions: await executeGenerator(generator, tokens, cwd, signal), cacheable: true };
return { suggestions: await executeGenerator(generator, tokens, activeToken, cwd, signal), cacheable: true };
} catch (error) {
if (signal?.aborted) signal.throwIfAborted();
const err = typeof error === "string" ? error : error instanceof Error ? error.message : error;
Expand All @@ -129,7 +135,7 @@ const generate = async (
const generationVersion = ++state.generationVersion;
const generation = (async (): Promise<CompletedGeneration> => {
if (debounce) await waitForDebounce(signal);
const result = await executeGeneratorSafely(generator, tokens, cwd, signal);
const result = await executeGeneratorSafely(generator, tokens, activeToken, cwd, signal);
return { ...result, current: state.generationVersion === generationVersion };
})();
state.inFlight = { key: generationKey, promise: generation, signal };
Expand Down
13 changes: 13 additions & 0 deletions src/tests/runtime/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,16 @@ test("reuses a generator when a path query moves past a delimiter", async () =>

expect(calls).toBe(1);
});

test("passes searchTerm correctly in generator context", async () => {
let passedSearchTerm = "";
const generator: Fig.Generator = {
custom: async (_tokens, _executeShellCommand, context) => {
passedSearchTerm = context.searchTerm;
return [];
},
};

await getGeneratorSuggestions(generator, ["tool", "mysearch"], "mysearch", process.cwd(), false);
expect(passedSearchTerm).toBe("mysearch");
});