diff --git a/extensions/cli/src/args.prompt.test.ts b/extensions/cli/src/args.prompt.test.ts index 121e11b1c..46b70b4db 100644 --- a/extensions/cli/src/args.prompt.test.ts +++ b/extensions/cli/src/args.prompt.test.ts @@ -22,13 +22,28 @@ describe("args --prompt flag", () => { }); it("should parse multiple prompts from multiple --prompt flags", () => { - process.argv = ["node", "script.js", "--prompt", "prompt1", "--prompt", "prompt2"]; + process.argv = [ + "node", + "script.js", + "--prompt", + "prompt1", + "--prompt", + "prompt2", + ]; const result = parseArgs(); expect(result.prompts).toEqual(["prompt1", "prompt2"]); }); it("should handle mixed flags including --prompt", () => { - process.argv = ["node", "script.js", "--config", "myconfig", "--prompt", "myprompt", "--resume"]; + process.argv = [ + "node", + "script.js", + "--config", + "myconfig", + "--prompt", + "myprompt", + "--resume", + ]; const result = parseArgs(); expect(result.configPath).toBe("myconfig"); expect(result.prompts).toEqual(["myprompt"]); @@ -36,14 +51,27 @@ describe("args --prompt flag", () => { }); it("should not include --prompt value in non-flag arguments", () => { - process.argv = ["node", "script.js", "--prompt", "prompt-value", "actual-prompt"]; + process.argv = [ + "node", + "script.js", + "--prompt", + "prompt-value", + "actual-prompt", + ]; const result = parseArgs(); expect(result.prompts).toEqual(["prompt-value"]); expect(result.prompt).toBe("actual-prompt"); }); it("should handle both --rule and --prompt flags", () => { - process.argv = ["node", "script.js", "--rule", "my-rule", "--prompt", "my-prompt"]; + process.argv = [ + "node", + "script.js", + "--rule", + "my-rule", + "--prompt", + "my-prompt", + ]; const result = parseArgs(); expect(result.rules).toEqual(["my-rule"]); expect(result.prompts).toEqual(["my-prompt"]); @@ -53,4 +81,4 @@ describe("args --prompt flag", () => { const result = await processPromptOrRule("This is a direct prompt"); expect(result).toBe("This is a direct prompt"); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/args.test.ts b/extensions/cli/src/args.test.ts index d20622113..d22333c05 100644 --- a/extensions/cli/src/args.test.ts +++ b/extensions/cli/src/args.test.ts @@ -269,7 +269,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { statusText: "Not Found", } as Response); - await expect(processPromptOrRule("continuedev/nonexistent-rule")).rejects.toThrow( + await expect( + processPromptOrRule("continuedev/nonexistent-rule"), + ).rejects.toThrow( 'Failed to load rule from hub "continuedev/nonexistent-rule": HTTP 404: Not Found', ); }); @@ -277,7 +279,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { it("should handle network errors when loading from hub", async () => { mockFetch.mockRejectedValue(new Error("Network error")); - await expect(processPromptOrRule("continuedev/sentry-nextjs")).rejects.toThrow( + await expect( + processPromptOrRule("continuedev/sentry-nextjs"), + ).rejects.toThrow( 'Failed to load rule from hub "continuedev/sentry-nextjs": Network error', ); }); @@ -285,7 +289,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { it("should handle invalid slug format", async () => { // "invalid-slug" doesn't contain "/" so it's treated as direct content, not a hub slug // Let's test with a slug that has wrong format but contains "/" - await expect(processPromptOrRule("invalid/slug/too/many/parts")).rejects.toThrow( + await expect( + processPromptOrRule("invalid/slug/too/many/parts"), + ).rejects.toThrow( 'Invalid hub slug format. Expected "owner/package", got: invalid/slug/too/many/parts', ); }); @@ -300,7 +306,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { arrayBuffer: () => Promise.resolve(zipBuffer), } as Response); - await expect(processPromptOrRule("continuedev/empty-rule")).rejects.toThrow( + await expect( + processPromptOrRule("continuedev/empty-rule"), + ).rejects.toThrow( 'Failed to load rule from hub "continuedev/empty-rule": No rule content found in downloaded zip file', ); }); @@ -316,7 +324,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { arrayBuffer: () => Promise.resolve(zipBuffer), } as Response); - await expect(processPromptOrRule("continuedev/directory-only")).rejects.toThrow( + await expect( + processPromptOrRule("continuedev/directory-only"), + ).rejects.toThrow( 'Failed to load rule from hub "continuedev/directory-only": No rule content found in downloaded zip file', ); }); @@ -373,7 +383,9 @@ describe("processPromptOrRule (loadRuleFromHub integration)", () => { arrayBuffer: () => Promise.resolve(invalidZipBuffer), } as Response); - await expect(processPromptOrRule("continuedev/corrupted-zip")).rejects.toThrow( + await expect( + processPromptOrRule("continuedev/corrupted-zip"), + ).rejects.toThrow( 'Failed to load rule from hub "continuedev/corrupted-zip"', ); }); diff --git a/extensions/cli/src/args.ts b/extensions/cli/src/args.ts index 272a7a465..2a2c79819 100644 --- a/extensions/cli/src/args.ts +++ b/extensions/cli/src/args.ts @@ -90,20 +90,12 @@ async function loadRuleFromHub(slug: string): Promise { */ export async function processPromptOrRule(spec: string): Promise { // If it looks like a hub slug (contains / but doesn't start with . or /) - if ( - spec.includes("/") && - !spec.startsWith(".") && - !spec.startsWith("/") - ) { + if (spec.includes("/") && !spec.startsWith(".") && !spec.startsWith("/")) { return await loadRuleFromHub(spec); } // If it looks like a file path (contains . or / or ends with common file extensions) - if ( - spec.includes(".") || - spec.includes("/") || - spec.includes("\\") - ) { + if (spec.includes(".") || spec.includes("/") || spec.includes("\\")) { return loadRuleFromFile(spec); } @@ -186,20 +178,26 @@ export function parseArgs(): CommandLineArgs { if (promptIndex + 1 >= args.length) { throw new Error("--prompt flag requires a value"); } - + const promptValue = args[promptIndex + 1]; - + // Check if the next argument is actually another flag if (promptValue.startsWith("--")) { throw new Error("--prompt flag requires a value, found flag instead"); } - + result.prompts.push(promptValue); } } // Find the last argument that's not a flag or a flag value - const flagsWithValues = ["--config", "--org", "--rule", "--prompt", "--format"]; + const flagsWithValues = [ + "--config", + "--org", + "--rule", + "--prompt", + "--format", + ]; const nonFlagArgs = args.filter((arg, index) => { // Skip flags (starting with --) if (arg.startsWith("--") || arg === "-p") return false; diff --git a/extensions/cli/src/commands/chat.ts b/extensions/cli/src/commands/chat.ts index adcda2b15..951160110 100644 --- a/extensions/cli/src/commands/chat.ts +++ b/extensions/cli/src/commands/chat.ts @@ -397,8 +397,13 @@ async function runHeadlessMode( } // Handle additional prompts from --prompt flags - const { processAndCombinePrompts } = await import("../util/promptProcessor.js"); - const initialUserInput = await processAndCombinePrompts(options.prompt, prompt); + const { processAndCombinePrompts } = await import( + "../util/promptProcessor.js" + ); + const initialUserInput = await processAndCombinePrompts( + options.prompt, + prompt, + ); let isFirstMessage = true; while (true) { diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index 59289b832..a8b522260 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -164,16 +164,24 @@ export function useChat({ useEffect(() => { // Only handle initial prompt after chat history is initialized // This prevents race conditions where the prompt runs before system message is loaded - if ((initialPrompt || (additionalPrompts && additionalPrompts.length > 0)) && isChatHistoryInitialized) { + if ( + (initialPrompt || (additionalPrompts && additionalPrompts.length > 0)) && + isChatHistoryInitialized + ) { const processPrompts = async () => { - const { processAndCombinePrompts } = await import("../../util/promptProcessor.js"); - const finalMessage = await processAndCombinePrompts(additionalPrompts, initialPrompt); - + const { processAndCombinePrompts } = await import( + "../../util/promptProcessor.js" + ); + const finalMessage = await processAndCombinePrompts( + additionalPrompts, + initialPrompt, + ); + if (finalMessage) { await handleUserMessage(finalMessage); } }; - + processPrompts().catch(console.error); } }, [initialPrompt, additionalPrompts, isChatHistoryInitialized]); diff --git a/extensions/cli/src/util/promptProcessor.test.ts b/extensions/cli/src/util/promptProcessor.test.ts index 2bc28004d..4644e17c8 100644 --- a/extensions/cli/src/util/promptProcessor.test.ts +++ b/extensions/cli/src/util/promptProcessor.test.ts @@ -82,11 +82,7 @@ describe("promptProcessor", () => { .mockRejectedValueOnce(new Error("processing failed")) .mockResolvedValueOnce("success2"); - const result = await processAndCombinePrompts([ - "good1", - "bad", - "good2", - ]); + const result = await processAndCombinePrompts(["good1", "bad", "good2"]); expect(mockProcessPromptOrRule).toHaveBeenCalledTimes(3); expect(result).toBe("success1\n\nsuccess2"); @@ -98,7 +94,10 @@ describe("promptProcessor", () => { .mockRejectedValueOnce(new Error("fail1")) .mockRejectedValueOnce(new Error("fail2")); - const result = await processAndCombinePrompts(["bad1", "bad2"], "initial"); + const result = await processAndCombinePrompts( + ["bad1", "bad2"], + "initial", + ); expect(result).toBe("initial"); }); @@ -125,4 +124,4 @@ describe("promptProcessor", () => { ); }); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/util/promptProcessor.ts b/extensions/cli/src/util/promptProcessor.ts index ef36bf6ea..60d926cd4 100644 --- a/extensions/cli/src/util/promptProcessor.ts +++ b/extensions/cli/src/util/promptProcessor.ts @@ -22,9 +22,7 @@ export async function processAndCombinePrompts( const processed = await processPromptOrRule(promptSpec); processedPrompts.push(processed); } catch (error: any) { - logger.warn( - `Failed to process prompt "${promptSpec}": ${error.message}`, - ); + logger.warn(`Failed to process prompt "${promptSpec}": ${error.message}`); } } @@ -36,4 +34,4 @@ export async function processAndCombinePrompts( return initialPrompt ? `${combinedPrompts}\n\n${initialPrompt}` : combinedPrompts; -} \ No newline at end of file +}