Elffuss BraidHub

kikocis/continue public

Merge pull request #7500 from continuedev/tips

feat: tips for cli
Changesetc61dd95048cfa5068f4cde2417c00a55b0953395fb09709f8b24e9e829e125ce
AuthorNate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-01 22:36 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-01 22:36 -07:00
Parents937b30c7b45d, b9bda6b740dc

Browse the files at this changeset

4 files changed

modified extensions/cli/src/ui/IntroMessage.test.tsx +60 −171
@@ -1,195 +1,84 @@
1-import { AssistantUnrolled } from "@continuedev/config-yaml";
1+import { Text } from "ink";
22 import { render } from "ink-testing-library";
33 import React from "react";
4-import { describe, expect, it, vi } from "vitest";
4+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55
66 import { IntroMessage } from "./IntroMessage.js";
77
8-// Mock the MCPService
9-vi.mock("../services/MCPService.js", () => ({
10- MCPService: vi.fn().mockImplementation(() => ({
11- getState: () => ({ prompts: [] }),
12- })),
8+// Mock the TipsDisplay module
9+vi.mock("./TipsDisplay.js", () => ({
10+ TipsDisplay: () => React.createElement(Text, null, "Mocked TipsDisplay"),
11+ shouldShowTip: vi.fn(),
12+}));
13+
14+// Mock other dependencies
15+vi.mock("../asciiArt.js", () => ({
16+ getDisplayableAsciiArt: () => "MOCK ASCII ART",
1317 }));
1418
15-// Mock the model capability check
1619 vi.mock("../utils/modelCapability.js", () => ({
17- isModelCapable: vi.fn().mockReturnValue(true),
20+ isModelCapable: () => true,
1821 }));
1922
2023 describe("IntroMessage", () => {
21- const mockMcpService = {
22- getState: () => ({ prompts: [] }),
23- } as any;
24-
25- const mockModel = {
26- provider: "anthropic",
27- name: "claude-3-sonnet",
28- model: "claude-3-sonnet",
29- } as any;
30-
31- it("should not duplicate rules from command line", () => {
32- // Config with rules that were already injected from command line
33- // Hub rules are stored as RuleObject with name and rule fields
34- const config: AssistantUnrolled = {
35- name: "Test Assistant",
36- rules: [
37- { name: "nate/spanish", rule: "Always respond in Spanish." },
38- "Always be helpful",
39- ],
40- } as any;
41-
42- const { lastFrame } = render(
43- <IntroMessage
44- config={config}
45- model={mockModel}
46- mcpService={mockMcpService}
47- />,
48- );
49-
50- const output = lastFrame();
51-
52- // Count occurrences of "nate/spanish" in the output
53- const matches = output?.match(/nate\/spanish/g) || [];
54-
55- // Should appear exactly once, not twice
56- expect(matches.length).toBe(1);
57-
58- // Verify the slug is shown, not the content
59- expect(output).toContain("nate/spanish");
60- expect(output).not.toContain("Always respond in Spanish");
61-
62- // Verify plain string rule is shown as-is
63- expect(output).toContain("Always be helpful");
24+ beforeEach(() => {
25+ vi.clearAllMocks();
26+ });
27+
28+ afterEach(() => {
29+ vi.restoreAllMocks();
30+ });
31+
32+ it("renders ASCII art and basic structure", () => {
33+ const { lastFrame } = render(<IntroMessage />);
34+
35+ expect(lastFrame()).toContain("MOCK ASCII ART");
6436 });
6537
66- it("should display rules from config correctly", () => {
67- const config: AssistantUnrolled = {
68- name: "Test Assistant",
69- rules: ["Rule 1", { name: "Rule 2" }, "Rule 3"],
70- } as any;
71-
72- const { lastFrame } = render(
73- <IntroMessage
74- config={config}
75- model={mockModel}
76- mcpService={mockMcpService}
77- />,
78- );
79-
80- const output = lastFrame();
81-
82- expect(output).toContain("Rules:");
83- expect(output).toContain("Rule 1");
84- expect(output).toContain("Rule 2");
85- expect(output).toContain("Rule 3");
38+ it("shows tips when shouldShowTip returns true", async () => {
39+ const { shouldShowTip } = await import("./TipsDisplay.js");
40+ vi.mocked(shouldShowTip).mockReturnValue(true);
41+
42+ const { lastFrame } = render(<IntroMessage />);
43+
44+ expect(lastFrame()).toContain("Mocked TipsDisplay");
8645 });
8746
88- it("should not show Rules section when no rules exist", () => {
89- const config: AssistantUnrolled = {
90- name: "Test Assistant",
91- } as any;
92-
93- const { lastFrame } = render(
94- <IntroMessage
95- config={config}
96- model={mockModel}
97- mcpService={mockMcpService}
98- />,
99- );
100-
101- const output = lastFrame();
47+ it("does not show tips when shouldShowTip returns false", async () => {
48+ const { shouldShowTip } = await import("./TipsDisplay.js");
49+ vi.mocked(shouldShowTip).mockReturnValue(false);
10250
103- expect(output).not.toContain("Rules:");
51+ const { lastFrame } = render(<IntroMessage />);
52+
53+ expect(lastFrame()).not.toContain("Mocked TipsDisplay");
10454 });
10555
106- it("should handle rule objects with missing name property", () => {
107- const config: AssistantUnrolled = {
108- name: "Test Assistant",
109- rules: [
110- "String rule",
111- { someOtherProp: "value" }, // Missing 'name' property
112- ],
113- } as any;
114-
115- const { lastFrame } = render(
116- <IntroMessage
117- config={config}
118- model={mockModel}
119- mcpService={mockMcpService}
120- />,
121- );
122-
123- const output = lastFrame();
124-
125- expect(output).toContain("String rule");
126- expect(output).toContain("Unknown"); // Should show "Unknown" for objects without name
56+ it("renders agent name when config is provided", () => {
57+ const config = { name: "Test Agent", version: "1.0.0", rules: [] };
58+
59+ const { lastFrame } = render(<IntroMessage config={config} />);
60+
61+ expect(lastFrame()).toContain("Agent:");
62+ expect(lastFrame()).toContain("Test Agent");
12763 });
12864
129- it("should display hub rule slugs, not their content", () => {
130- const config: AssistantUnrolled = {
131- name: "Test Assistant",
132- rules: [
133- {
134- name: "org/complex-rule",
135- rule: `---
136-alwaysApply: true
137----
138-
139-This is a very long rule content that should not be displayed.
140-It contains multiple lines and complex formatting.
141-The UI should only show the slug 'org/complex-rule'.`,
142- },
143- { name: "another/rule", rule: "Short content" },
144- ],
145- } as any;
146-
147- const { lastFrame } = render(
148- <IntroMessage
149- config={config}
150- model={mockModel}
151- mcpService={mockMcpService}
152- />,
153- );
154-
155- const output = lastFrame();
156-
157- // Should show slugs only
158- expect(output).toContain("org/complex-rule");
159- expect(output).toContain("another/rule");
160-
161- // Should NOT show the rule content
162- expect(output).not.toContain("This is a very long rule");
163- expect(output).not.toContain("alwaysApply");
164- expect(output).not.toContain("Short content");
65+ it("renders model information when model is provided", () => {
66+ const model = {
67+ name: "provider/model-name",
68+ provider: "test-provider",
69+ model: "test-model",
70+ };
71+
72+ const { lastFrame } = render(<IntroMessage model={model} />);
73+
74+ expect(lastFrame()).toContain("Model:");
75+ expect(lastFrame()).toContain("model-name");
16576 });
16677
167- it("should handle partial loading gracefully", () => {
168- // Test with no config, no model, no mcpService
169- const { lastFrame: frame1 } = render(<IntroMessage />);
170- let output = frame1();
171- expect(output).toContain("Loading...");
172-
173- // Test with only model missing
174- const config: AssistantUnrolled = {
175- name: "Test Assistant",
176- rules: ["Rule 1"],
177- } as any;
178-
179- const { lastFrame: frame2 } = render(
180- <IntroMessage config={config} mcpService={mockMcpService} />,
181- );
182- output = frame2();
183- expect(output).toContain("Test Assistant");
184- expect(output).toContain("Loading...");
185- expect(output).toContain("Rule 1");
186-
187- // Test with only config missing
188- const { lastFrame: frame3 } = render(
189- <IntroMessage model={mockModel} mcpService={mockMcpService} />,
190- );
191- output = frame3();
192- expect(output).toContain("claude-3-sonnet");
193- expect(output).not.toContain("Rules:");
78+ it("shows loading state when model is not provided", () => {
79+ const { lastFrame } = render(<IntroMessage />);
80+
81+ expect(lastFrame()).toContain("Model:");
82+ expect(lastFrame()).toContain("Loading...");
19483 });
19584 });
modified extensions/cli/src/ui/IntroMessage.tsx +62 −49
@@ -7,6 +7,7 @@
77 import { isModelCapable } from "../utils/modelCapability.js";
88
99 import { ModelCapabilityWarning } from "./ModelCapabilityWarning.js";
10+import { TipsDisplay, shouldShowTip } from "./TipsDisplay.js";
1011
1112 interface IntroMessageProps {
1213 config?: AssistantUnrolled;
@@ -14,6 +15,13 @@
1415 mcpService?: MCPService;
1516 }
1617
18+// Helper function to extract rule names
19+const extractRuleNames = (rules: any[] = []): string[] => {
20+ return rules.map((rule: any) =>
21+ typeof rule === "string" ? rule : rule?.name || "Unknown",
22+ );
23+};
24+
1725 const IntroMessage: React.FC<IntroMessageProps> = ({
1826 config,
1927 model,
@@ -22,15 +30,12 @@
2230 // Get MCP prompts directly (not memoized since they can change after first render)
2331 const mcpPrompts = mcpService?.getState().prompts ?? [];
2432
33+ // Determine if we should show a tip (1 in 5 chance) - computed once on mount
34+ const showTip = useMemo(() => shouldShowTip(), []);
35+
2536 // Memoize expensive operations to avoid running on every resize
2637 const { allRules, modelCapable } = useMemo(() => {
27- // Show all rules from config (command-line rules are already merged into config)
28- const configRules =
29- config?.rules?.map((rule: any) =>
30- typeof rule === "string" ? rule : rule?.name || "Unknown",
31- ) || [];
32-
33- const allRules = configRules;
38+ const allRules = extractRuleNames(config?.rules);
3439
3540 // Check if model is capable - now checking both name and model properties
3641 const modelCapable = model
@@ -40,12 +45,59 @@
4045 return { allRules, modelCapable };
4146 }, [config?.rules, model?.provider, model?.name, model?.model]);
4247
48+ // Render helper components
49+ const renderMcpPrompts = () =>
50+ mcpPrompts.length > 0 ? (
51+ <>
52+ {mcpPrompts.map((prompt, index) => (
53+ <Text key={`mcp-${index}`}>
54+ - <Text color="white">/{prompt.name}</Text>:{" "}
55+ <Text color="gray">{prompt.description}</Text>
56+ </Text>
57+ ))}
58+ <Text> </Text>
59+ </>
60+ ) : null;
61+
62+ const renderRules = () =>
63+ allRules.length > 0 ? (
64+ <>
65+ <Text bold color="blue">
66+ Rules:
67+ </Text>
68+ {allRules.map((rule, index) => (
69+ <Text key={index}>
70+ - <Text color="white">{rule}</Text>
71+ </Text>
72+ ))}
73+ <Text> </Text>
74+ </>
75+ ) : null;
76+
77+ const renderMcpServers = () =>
78+ (config?.mcpServers?.length ?? 0) > 0 ? (
79+ <>
80+ <Text bold color="blue">
81+ MCP Servers:
82+ </Text>
83+ {config?.mcpServers?.map((server: any, index: number) => (
84+ <Text key={index}>
85+ - <Text color="white">{server?.name}</Text>
86+ </Text>
87+ ))}
88+ <Text> </Text>
89+ </>
90+ ) : null;
91+
4392 return (
4493 <Box flexDirection="column" paddingX={1} paddingY={1}>
4594 {/* ASCII Art */}
4695 <Text>{getDisplayableAsciiArt()}</Text>
4796 <Text> </Text>
4897
98+ {/* Tips Display - shown randomly 1 in 5 times */}
99+ {showTip && <TipsDisplay />}
100+
49101 {/* Agent name */}
50102 {config && (
51103 <Text color="blue">
@@ -77,48 +129,9 @@
77129 </>
78130 )}
79131
80- {/* MCP prompts */}
81- {mcpPrompts.length > 0 && (
82- <>
83- {mcpPrompts.map((prompt, index) => (
84- <Text key={`mcp-${index}`}>
85- - <Text color="white">/{prompt.name}</Text>:{" "}
86- <Text color="gray">{prompt.description}</Text>
87- </Text>
88- ))}
89- <Text> </Text>
90- </>
91- )}
92-
93- {/* Rules */}
94- {allRules.length > 0 && (
95- <>
96- <Text bold color="blue">
97- Rules:
98- </Text>
99- {allRules.map((rule, index) => (
100- <Text key={index}>
101- - <Text color="white">{rule}</Text>
102- </Text>
103- ))}
104- <Text> </Text>
105- </>
106- )}
107-
108- {/* MCP Servers */}
109- {(config?.mcpServers?.length ?? 0) > 0 && (
110- <>
111- <Text bold color="blue">
112- MCP Servers:
113- </Text>
114- {config?.mcpServers?.map((server: any, index: number) => (
115- <Text key={index}>
116- - <Text color="white">{server?.name}</Text>
117- </Text>
118- ))}
119- <Text> </Text>
120- </>
121- )}
132+ {renderMcpPrompts()}
133+ {renderRules()}
134+ {renderMcpServers()}
122135 </Box>
123136 );
124137 };
added extensions/cli/src/ui/TipsDisplay.test.tsx +155 −0
@@ -0,0 +1,155 @@
1+import { render } from "ink-testing-library";
2+import React from "react";
3+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
4+
5+import {
6+ TipsDisplay,
7+ shouldShowTip,
8+ CONTINUE_CLI_TIPS,
9+} from "./TipsDisplay.js";
10+
11+describe("TipsDisplay", () => {
12+ beforeEach(() => {
13+ // Mock Math.random to make tests deterministic
14+ vi.spyOn(Math, "random");
15+ });
16+
17+ afterEach(() => {
18+ vi.restoreAllMocks();
19+ });
20+
21+ it("renders a random tip from the tips array", () => {
22+ // Mock Math.random to return 0.5, which should select the middle tip
23+ vi.mocked(Math.random).mockReturnValue(0.5);
24+
25+ const expectedTipIndex = Math.floor(0.5 * CONTINUE_CLI_TIPS.length);
26+ const expectedTip = CONTINUE_CLI_TIPS[expectedTipIndex];
27+
28+ const { lastFrame } = render(<TipsDisplay />);
29+
30+ expect(lastFrame()).toContain("* Tip:");
31+ expect(lastFrame()).toContain(expectedTip);
32+ });
33+
34+ it("renders different tips based on random selection", () => {
35+ // Test with first tip
36+ vi.mocked(Math.random).mockReturnValue(0.0);
37+ const { lastFrame: firstFrame } = render(<TipsDisplay />);
38+ expect(firstFrame()).toContain(CONTINUE_CLI_TIPS[0]);
39+
40+ // Test with last tip - use partial match due to line wrapping
41+ vi.mocked(Math.random).mockReturnValue(0.99);
42+ const { lastFrame: lastFrame } = render(<TipsDisplay />);
43+ const lastTip = CONTINUE_CLI_TIPS[CONTINUE_CLI_TIPS.length - 1];
44+ const firstPartOfLastTip = lastTip.substring(0, 30); // Check first 30 chars
45+ expect(lastFrame()).toContain(firstPartOfLastTip);
46+ });
47+
48+ it("always renders the tip structure correctly", () => {
49+ vi.mocked(Math.random).mockReturnValue(0.5);
50+
51+ const { lastFrame } = render(<TipsDisplay />);
52+ const output = lastFrame();
53+
54+ // Should contain the tip icon and label
55+ expect(output).toContain("* Tip:");
56+
57+ // Should contain one of the tips
58+ const containsAtLeastOneTip = CONTINUE_CLI_TIPS.some(
59+ (tip) => output?.includes(tip) ?? false,
60+ );
61+ expect(containsAtLeastOneTip).toBe(true);
62+ });
63+});
64+
65+describe("shouldShowTip", () => {
66+ beforeEach(() => {
67+ vi.spyOn(Math, "random");
68+ });
69+
70+ afterEach(() => {
71+ vi.restoreAllMocks();
72+ });
73+
74+ it("returns true when random value is less than 0.2 (20%)", () => {
75+ vi.mocked(Math.random).mockReturnValue(0.1);
76+ expect(shouldShowTip()).toBe(true);
77+
78+ vi.mocked(Math.random).mockReturnValue(0.19);
79+ expect(shouldShowTip()).toBe(true);
80+ });
81+
82+ it("returns false when random value is 0.2 or greater", () => {
83+ vi.mocked(Math.random).mockReturnValue(0.2);
84+ expect(shouldShowTip()).toBe(false);
85+
86+ vi.mocked(Math.random).mockReturnValue(0.5);
87+ expect(shouldShowTip()).toBe(false);
88+
89+ vi.mocked(Math.random).mockReturnValue(0.99);
90+ expect(shouldShowTip()).toBe(false);
91+ });
92+
93+ it("has approximately 20% probability over many calls", () => {
94+ // Restore original Math.random for this statistical test
95+ vi.restoreAllMocks();
96+
97+ // Test the probability distribution over many calls
98+ let trueCount = 0;
99+ const iterations = 1000;
100+
101+ for (let i = 0; i < iterations; i++) {
102+ if (shouldShowTip()) {
103+ trueCount++;
104+ }
105+ }
106+
107+ const probability = trueCount / iterations;
108+ // Allow for some variance (15% to 25% range)
109+ expect(probability).toBeGreaterThan(0.15);
110+ expect(probability).toBeLessThan(0.25);
111+
112+ // Re-mock for proper cleanup
113+ vi.spyOn(Math, "random");
114+ });
115+});
116+
117+describe("CONTINUE_CLI_TIPS", () => {
118+ it("contains at least 6 tips", () => {
119+ expect(CONTINUE_CLI_TIPS.length).toBeGreaterThanOrEqual(6);
120+ });
121+
122+ it("all tips are non-empty strings", () => {
123+ CONTINUE_CLI_TIPS.forEach((tip, index) => {
124+ expect(typeof tip).toBe("string");
125+ expect(tip.trim().length).toBeGreaterThan(0);
126+ });
127+ });
128+
129+ it("tips are helpful and informative", () => {
130+ // Check that tips contain useful keywords
131+ const usefulKeywords = [
132+ "/help",
133+ "escape",
134+ "pause",
135+ "arrow keys",
136+ "multi-line",
137+ "history",
138+ "/resume",
139+ "headless",
140+ "flag",
141+ "keyboard shortcuts",
142+ ];
143+
144+ const allTipsText = CONTINUE_CLI_TIPS.join(" ").toLowerCase();
145+
146+ // At least half of the keywords should appear in the tips
147+ const foundKeywords = usefulKeywords.filter((keyword) =>
148+ allTipsText.includes(keyword.toLowerCase()),
149+ );
150+
151+ expect(foundKeywords.length).toBeGreaterThanOrEqual(
152+ usefulKeywords.length / 2,
153+ );
154+ });
155+});
added extensions/cli/src/ui/TipsDisplay.tsx +51 −0
@@ -0,0 +1,51 @@
1+import { Box, Text } from "ink";
2+import React, { useMemo } from "react";
3+
4+// Array of helpful tips for Continue CLI users
5+const CONTINUE_CLI_TIPS = [
6+ "Use `/help` to learn keyboard shortcuts",
7+ "Press escape to pause cn, and press enter to resume",
8+ "Use arrow keys (↑/↓) to navigate through your input history",
9+ 'Multi-line input is supported by typing "\\" and pressing enter',
10+ "Use `cn ls` or `/resume` to resume a previous conversation",
11+ 'Run `cn` with the `-p` flag for headless mode. For example: `cn -p "Generate a commit message for the current changes. Output _only_ the commit message and nothing else."`',
12+];
13+
14+interface TipsDisplayProps {
15+ // No props needed - component handles its own randomization
16+}
17+
18+/**
19+ * Randomly selects and displays a tip from the CONTINUE_CLI_TIPS array.
20+ * Should only be shown 1 in 5 times (20% chance).
21+ */
22+const TipsDisplay: React.FC<TipsDisplayProps> = () => {
23+ // Randomly select a tip, memoized to prevent changing on re-renders
24+ const randomTip = useMemo(
25+ () =>
26+ CONTINUE_CLI_TIPS[Math.floor(Math.random() * CONTINUE_CLI_TIPS.length)],
27+ [],
28+ );
29+
30+ return (
31+ <Box flexDirection="row" paddingX={1} paddingBottom={1}>
32+ <Text color="green" bold>
33+ * Tip:
34+ </Text>
35+ <Text color="gray" italic>
36+ {" "}
37+ {randomTip}
38+ </Text>
39+ <Text> </Text>
40+ </Box>
41+ );
42+};
43+
44+/**
45+ * Determines whether to show a tip (1 in 5 chance, 20% probability)
46+ */
47+export function shouldShowTip(): boolean {
48+ return Math.random() < 0.2; // 20% chance (1 in 5)
49+}
50+
51+export { CONTINUE_CLI_TIPS, TipsDisplay };