From 3fe2c8e9dbb73774c5541b7f94d78def9502cd3e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 26 Aug 2025 23:22:43 -0500 Subject: [PATCH] Add system prompt --- src/components/Shakespeare/ChatPane.tsx | 61 ++++++-------- src/lib/system.ts | 101 ++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 39 deletions(-) create mode 100644 src/lib/system.ts diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index cbabe37f..a53a279d 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -1,4 +1,4 @@ -import { useState, useRef, useEffect, forwardRef, useImperativeHandle } from 'react'; +import { useState, useRef, useEffect, forwardRef, useImperativeHandle, useMemo } from 'react'; import { useSearchParams } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; @@ -24,6 +24,7 @@ import { BuildProjectTool } from '@/lib/tools/BuildProjectTool'; import { toolToOpenAI } from '@/lib/tools/openai-adapter'; import { Tool } from '@/lib/tools/Tool'; import OpenAI from 'openai'; +import { makeSystemPrompt } from '@/lib/system'; interface ChatPaneProps { @@ -56,7 +57,7 @@ export const ChatPane = forwardRef(({ isDeployLoading: externalIsDeployLoading }, ref) => { const [input, setInput] = useState(''); - + const [systemPrompt, setSystemPrompt] = useState(''); // Use external state if provided, otherwise default to false const isBuildLoading = externalIsBuildLoading || false; @@ -73,7 +74,7 @@ export const ChatPane = forwardRef(({ // Initialize AI chat with tools const cwd = `/projects/${projectId}`; - const customTools = { + const customTools = useMemo(() => ({ git_commit: new GitCommitTool(browserFS, cwd), text_editor_view: new TextEditorViewTool(browserFS, cwd), text_editor_write: new TextEditorWriteTool(browserFS, cwd), @@ -81,45 +82,27 @@ export const ChatPane = forwardRef(({ npm_add_package: new NpmAddPackageTool(browserFS, cwd), npm_remove_package: new NpmRemovePackageTool(browserFS, cwd), build_project: new BuildProjectTool(browserFS, cwd), - }; + }), [browserFS, cwd]); // Convert tools to OpenAI format - const tools: Record = {}; - for (const [name, tool] of Object.entries(customTools)) { - tools[name] = toolToOpenAI(name, tool as Tool); - } - - const systemPrompt = `You are an AI assistant helping users build custom Nostr websites. You have access to tools that allow you to read, write, and manage files in the project, as well as build the project and manage npm packages. - -Key capabilities: -- Read and write files in the project -- List directory contents -- Check if files exist -- Build the project using esbuild (use the build_project tool) -- Get project structure overview -- Search through files -- Add and remove npm packages -- Install dependencies and dev dependencies -- Commit changes to git with automatic staging - -Guidelines: -- Always check if files exist before writing to avoid overwriting -- Provide helpful explanations of what you're doing -- Suggest improvements and best practices -- Use modern React patterns and TypeScript -- Follow Nostr protocol standards when relevant -- Ensure the project builds successfully - -The project uses: -- React 18 with TypeScript -- esbuild for building -- TailwindCSS for styling -- Nostrify for Nostr integration -- shadcn/ui components - -When creating new components or pages, follow the existing patterns in the codebase.`; - + const tools = useMemo(() => { + const result: Record = {}; + for (const [name, tool] of Object.entries(customTools)) { + result[name] = toolToOpenAI(name, tool as Tool); + } + return result; + }, [customTools]); + useEffect(() => { + makeSystemPrompt({ + cwd, + fs: browserFS, + mode: "agent", + name: "Shakespeare", + profession: "software extraordinaire", + tools: Object.values(tools), + }).then(setSystemPrompt) + }, [browserFS, cwd, tools]); const { messages, diff --git a/src/lib/system.ts b/src/lib/system.ts new file mode 100644 index 00000000..e28a56fe --- /dev/null +++ b/src/lib/system.ts @@ -0,0 +1,101 @@ +import { join } from "@std/path"; + +import OpenAI from "openai"; +import { JSRuntimeFS } from "./JSRuntime"; + +export interface MakeSystemPromptOpts { + name: string; + profession: string; + tools: OpenAI.Chat.Completions.ChatCompletionTool[]; + mode: "init" | "agent"; + fs: JSRuntimeFS; + cwd: string; +} + +export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise { + const { name, profession, tools, mode, fs, cwd } = opts; + + let system = mode === "init" + ? `You are ${name}, an expert ${profession}. The files in the current directory are a template. Your goal is to transform this template into a working project according to the user's request.` + : `You are ${name}, an expert ${profession}. Your goal is to work on the project in the current directory according to the user's request. First, explore and understand the project structure, examine the existing files, and understand the context before making any assumptions about what the user is asking for.`; + + // Add current date + const currentDate = new Date().toLocaleDateString("en-US", { + weekday: "long", + year: "numeric", + month: "long", + day: "numeric", + }); + + system += `\n\nThe current date is ${currentDate}.`; + + // Add available tools to the system prompt + const toolNames = tools + .filter((tool) => tool.type === "function") + .map((tool) => tool.function.name); + + if (toolNames.length > 0) { + system += + "\n\n# Available Tools\n\nYou have access to the following tools:\n"; + for (const tool of tools) { + if (tool.type === "function") { + system += `\n- **${tool.function.name}**: ${ + tool.function.description || "No description available" + }`; + } + } + } + + // Add README.md if it exists + try { + const readmePath = join(cwd, "README.md"); + const readmeText = await fs.readFile(readmePath, "utf8"); + if (readmeText) { + system += "\n\n" + readmeText; + } + } catch { + // README.md not found, continue + } + + // Add context from CONTEXT.md if it exists + try { + const { text } = await getContext(fs, cwd); + system += "\n\n" + text; + } catch { + // CONTEXT.md not found, continue + } + + return system; +} + +async function getContext( + fs: JSRuntimeFS, + cwd: string, +): Promise<{ filename: string; text: string }> { + const contextFiles = [ + "CONTEXT.md", + "CLAUDE.md", + "codex.md", + ".goosehints", + ".cursorrules", + ".github/copilot-instructions.md", + ]; + + for (const filename of contextFiles) { + const filePath = join(cwd, filename); + try { + const text = await fs.readFile(filePath, "utf8"); + if (text) { + return { filename, text }; + } + } catch { + // continue + } + } + + throw new Error( + `No context file found in ${cwd}. Please create one of the following files: ${ + contextFiles.join(", ") + }`, + ); +}