From 25a67cf947f786d4cdc36c60e36e303a4e466c87 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 13 Jul 2025 22:16:21 -0700 Subject: [PATCH] feat: base system message --- src/index.ts | 4 +- src/systemMessage.ts | 114 ++++++++++++++++++++++++++++++++++++++----- src/ui/TUIChat.tsx | 4 +- 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index f46fbd18f..61e4a40b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,8 +93,8 @@ async function chat() { // Rules const chatHistory: ChatCompletionMessageParam[] = []; - const baseSystemMessage = assistant.systemMessage; - const systemMessage = constructSystemMessage(baseSystemMessage); + const rulesSystemMessage = assistant.systemMessage; + const systemMessage = constructSystemMessage(rulesSystemMessage); if (systemMessage) { chatHistory.push({ role: "system", content: systemMessage }); } diff --git a/src/systemMessage.ts b/src/systemMessage.ts index 6fe1556f5..db3e66ea4 100644 --- a/src/systemMessage.ts +++ b/src/systemMessage.ts @@ -1,30 +1,122 @@ +import { execSync } from "child_process"; import * as fs from "fs"; import * as path from "path"; /** - * Load and append agent configuration content to the system message if it exists - * @param baseSystemMessage - The base system message from the assistant - * @returns The enhanced system message with agent configuration content if available + * Check if current directory is a git repository */ -export function constructSystemMessage(baseSystemMessage: string): string { +function isGitRepo(): boolean { + try { + execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" }); + return true; + } catch { + return false; + } +} + +/** + * Get basic directory structure + */ +function getDirectoryStructure(): string { + try { + const result = execSync( + 'find . -type f -name "*.ts" -o -name "*.js" -o -name "*.json" -o -name "*.md" | head -20', + { + encoding: "utf-8", + cwd: process.cwd(), + } + ); + return result.trim() || "No structure available"; + } catch { + return "Directory structure not available"; + } +} + +/** + * Get git status + */ +function getGitStatus(): string { + try { + if (!isGitRepo()) { + return "Not a git repository"; + } + const result = execSync("git status --porcelain", { + encoding: "utf-8", + cwd: process.cwd(), + }); + return result.trim() || "Working tree clean"; + } catch { + return "Git status not available"; + } +} + +const baseSystemMessage = `You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question. + +Notes: +1. IMPORTANT: You should be concise, direct, and to the point, since your responses will be displayed on a command line interface. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is .", "Here is the content of the file..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". +2. When relevant, share file names and code snippets relevant to the query +3. Any file paths you return in your final response MUST be absolute. DO NOT use relative paths. +Here is useful information about the environment you are running in: + +Working directory: ${process.cwd()} +Is directory a git repo: ${isGitRepo()} +Platform: ${process.platform} +Today's date: ${new Date().toISOString().split("T")[0]} + + +As you answer the user's questions, you can use the following context: + +Below is a snapshot of this project's file structure at the start of the conversation. This snapshot will NOT update during the conversation. It skips over .gitignore patterns. + +${getDirectoryStructure()} + +This is the git status at the start of the conversation. Note that this status is a snapshot in time, and will not update during the conversation. +${getGitStatus()} +`; + +/** + * Load and construct a comprehensive system message with base message and rules section + * @param rulesSystemMessage - The rules system message from the assistant + * @returns The comprehensive system message with base message and rules section + */ +export function constructSystemMessage(rulesSystemMessage: string): string { const agentFiles = ["AGENTS.md", "AGENT.md", "CLAUDE.md", "CODEX.md"]; + let agentContent = ""; + try { for (const fileName of agentFiles) { const filePath = path.join(process.cwd(), fileName); if (fs.existsSync(filePath)) { - const fileContent = fs.readFileSync(filePath, "utf-8"); - - // Append file content to the base system message - const separator = baseSystemMessage ? "\n\n" : ""; - return `${baseSystemMessage}${separator}# Repository Context from ${fileName}\n\n${fileContent}`; + agentContent = fs.readFileSync(filePath, "utf-8"); + break; // Use the first found agent file } } } catch (error) { - // If there's any error reading the file, just return the base system message + // If there's any error reading the file, continue without agent content console.warn("Warning: Could not read agent configuration file:", error); } - return baseSystemMessage; + // Construct the comprehensive system message + let systemMessage = baseSystemMessage; + + // Add rules section if we have either rules or agent content + if (rulesSystemMessage || agentContent) { + systemMessage += '\n\n'; + + if (rulesSystemMessage) { + systemMessage += `\n${rulesSystemMessage}`; + } + + if (agentContent) { + const separator = rulesSystemMessage ? "\n\n" : "\n"; + systemMessage += `${separator}${agentContent}`; + } + + systemMessage += "\n"; + } + + console.log("SYS: ", systemMessage); + return systemMessage; } diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index fcc7fc04f..f1ec8d6f0 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -33,8 +33,8 @@ const TUIChat: React.FC = ({ const [chatHistory, setChatHistory] = useState( () => { const history: ChatCompletionMessageParam[] = []; - const baseSystemMessage = assistant.systemMessage; - const systemMessage = constructSystemMessage(baseSystemMessage); + const rulesSystemMessage = assistant.systemMessage; + const systemMessage = constructSystemMessage(rulesSystemMessage); if (systemMessage) { history.push({ role: "system", content: systemMessage }); }