Elffuss BraidHub

kikocis/continue public

73672f931e26 · 4.7 KiBRawPermalink
1import * as os from "node:os";
2import * as path from "node:path";
3
4import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
5import { Box, Text } from "ink";
6import React, { useMemo } from "react";
7
8import { getDisplayableAsciiArt } from "../asciiArt.js";
9import { MCPService } from "../services/MCPService.js";
10import { isModelCapable } from "../utils/modelCapability.js";
11
12import { ModelCapabilityWarning } from "./ModelCapabilityWarning.js";
13import { TipsDisplay, shouldShowTip } from "./TipsDisplay.js";
14
15// Export the warning message for testing
16export const HOME_DIRECTORY_WARNING =
17 "Run cn in a project directory for the best experience (currently in home directory)";
18
19interface IntroMessageProps {
20 config?: AssistantUnrolled;
21 model?: ModelConfig;
22 mcpService?: MCPService;
23}
24
25// Helper function to extract rule names
26const extractRuleNames = (rules: any[] = []): string[] => {
27 return rules.map((rule: any) =>
28 typeof rule === "string" ? rule : rule?.name || "Unknown",
29 );
30};
31
32const IntroMessage: React.FC<IntroMessageProps> = ({
33 config,
34 model,
35 mcpService,
36}) => {
37 // Get MCP prompts directly (not memoized since they can change after first render)
38 const mcpPrompts = mcpService?.getState().prompts ?? [];
39
40 // Determine if we should show a tip (1 in 5 chance) - computed once on mount
41 const showTip = useMemo(() => shouldShowTip(), []);
42
43 // Check if current working directory is the home directory
44 const isInHomeDirectory = useMemo(() => {
45 const cwd = process.cwd();
46 const homedir = os.homedir();
47 const resolvedCwd = path.resolve(cwd);
48 const resolvedHome = path.resolve(homedir);
49 if (process.platform === "win32") {
50 return resolvedCwd.toLowerCase() === resolvedHome.toLowerCase();
51 }
52 return resolvedCwd === resolvedHome;
53 }, []);
54
55 // Memoize expensive operations to avoid running on every resize
56 const { allRules, modelCapable } = useMemo(() => {
57 const allRules = extractRuleNames(config?.rules);
58
59 // Check if model is capable - now checking both name and model properties
60 const modelCapable = model
61 ? isModelCapable(model.provider, model.name, model.model)
62 : true; // Default to true if model not loaded yet
63
64 return { allRules, modelCapable };
65 }, [config?.rules, model?.provider, model?.name, model?.model]);
66
67 // Render helper components
68 const renderMcpPrompts = () =>
69 mcpPrompts.length > 0 ? (
70 <>
71 {mcpPrompts.map((prompt, index) => (
72 <Text key={`mcp-${index}`}>
73 - <Text color="white">/{prompt.name}</Text>:{" "}
74 <Text color="gray">{prompt.description}</Text>
75 </Text>
76 ))}
77 <Text> </Text>
78 </>
79 ) : null;
80
81 const renderRules = () =>
82 allRules.length > 0 ? (
83 <>
84 <Text bold color="blue">
85 Rules:
86 </Text>
87 {allRules.map((rule, index) => (
88 <Text key={index}>
89 - <Text color="white">{rule}</Text>
90 </Text>
91 ))}
92 <Text> </Text>
93 </>
94 ) : null;
95
96 const renderMcpServers = () =>
97 (config?.mcpServers?.length ?? 0) > 0 ? (
98 <>
99 <Text bold color="blue">
100 MCP Servers:
101 </Text>
102 {config?.mcpServers?.map((server: any, index: number) => (
103 <Text key={index}>
104 - <Text color="white">{server?.name}</Text>
105 </Text>
106 ))}
107 <Text> </Text>
108 </>
109 ) : null;
110
111 return (
112 <Box flexDirection="column" paddingX={1} paddingY={1}>
113 {/* ASCII Art */}
114 <Text>{getDisplayableAsciiArt()}</Text>
115 <Text> </Text>
116
117 {/* Tips Display - shown randomly 1 in 5 times */}
118 {showTip && <TipsDisplay />}
119
120 {/* Agent name */}
121 {config && (
122 <Text color="blue">
123 <Text bold>Agent:</Text> <Text color="white">{config.name}</Text>
124 </Text>
125 )}
126
127 {/* Model */}
128 {model ? (
129 <Text color="blue">
130 <Text bold>Model:</Text>{" "}
131 <Text color="white">{model.name.split("/").pop()}</Text>
132 </Text>
133 ) : (
134 <Text color="blue">
135 <Text bold>Model:</Text> <Text color="gray">Loading...</Text>
136 </Text>
137 )}
138
139 <Text> </Text>
140
141 {/* Model capability warning */}
142 {model && !modelCapable && (
143 <>
144 <ModelCapabilityWarning
145 modelName={model.name.split("/").pop() || model.name}
146 />
147 <Text> </Text>
148 </>
149 )}
150
151 {renderMcpPrompts()}
152 {renderRules()}
153 {renderMcpServers()}
154
155 {/* Home directory warning */}
156 {isInHomeDirectory && (
157 <>
158 <Text color="yellow">{HOME_DIRECTORY_WARNING}</Text>
159 <Text> </Text>
160 </>
161 )}
162 </Box>
163 );
164};
165
166export { IntroMessage };