|
| 1 | +// src/agent/system-prompt.ts |
| 2 | +import { type Config } from "@/config.js"; |
| 3 | +import { toolModules } from "./tools/definitions/index.js"; |
| 4 | +import { zodToTs, printNode } from "zod-to-ts"; |
| 5 | +import fs from "fs/promises"; |
| 6 | +import path from "path"; |
| 7 | +import os from "os"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Recursively searches for instruction files (TOBI.md, AGENTS.md) upwards from the current directory. |
| 11 | + * @param currentDir The directory to start searching from. |
| 12 | + * @returns The content of the found file, or null if no file is found. |
| 13 | + */ |
| 14 | +async function findInstructionFile(currentDir: string): Promise<string | null> { |
| 15 | + const instructionFiles = ["TOBI.md", "AGENTS.md"]; |
| 16 | + const homeDir = os.homedir(); |
| 17 | + |
| 18 | + let dir = currentDir; |
| 19 | + // Stop if we reach the root directory or the user's home directory |
| 20 | + while (dir !== path.dirname(dir) && dir.startsWith(homeDir)) { |
| 21 | + for (const fileName of instructionFiles) { |
| 22 | + const filePath = path.join(dir, fileName); |
| 23 | + try { |
| 24 | + // Check if the file exists and we can read it |
| 25 | + await fs.access(filePath, fs.constants.R_OK); |
| 26 | + return await fs.readFile(filePath, "utf-8"); |
| 27 | + } catch { |
| 28 | + // File does not exist or is not readable, continue searching |
| 29 | + } |
| 30 | + } |
| 31 | + // Move to the parent directory |
| 32 | + dir = path.dirname(dir); |
| 33 | + } |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Generates the dynamic system prompt for the agent. |
| 39 | + * @param config The application configuration. |
| 40 | + * @returns A promise that resolves to the generated system prompt string. |
| 41 | + */ |
| 42 | +export async function generateSystemPrompt(_config: Config): Promise<string> { |
| 43 | + const cwd = process.cwd(); |
| 44 | + |
| 45 | + // 1. Get workspace files |
| 46 | + const dirents = await fs.readdir(cwd, { withFileTypes: true }); |
| 47 | + const filesAndDirs = dirents.map((d) => (d.isDirectory() ? `${d.name}/` : d.name)).join("\n"); |
| 48 | + |
| 49 | + // 2. Find instruction file |
| 50 | + const instructionContent = await findInstructionFile(cwd); |
| 51 | + |
| 52 | + // 3. Generate tool definitions as TypeScript types |
| 53 | + const toolDefinitions = Object.values(toolModules) |
| 54 | + .map((module) => { |
| 55 | + // We pass the zod schema for the arguments to zodToTs |
| 56 | + const { node } = zodToTs(module.schema.shape.arguments); |
| 57 | + // Then we print the resulting TypeScript AST node to a string. |
| 58 | + const argumentsString = printNode(node); |
| 59 | + |
| 60 | + return ( |
| 61 | + `// ${module.description}\n` + |
| 62 | + `type ${module.schema.shape.name.value} = {\n` + |
| 63 | + ` name: "${module.schema.shape.name.value}";\n` + |
| 64 | + ` arguments: ${argumentsString};\n` + |
| 65 | + `};` |
| 66 | + ); |
| 67 | + }) |
| 68 | + .join("\n\n"); |
| 69 | + |
| 70 | + // 4. Assemble the prompt |
| 71 | + const promptParts: string[] = [ |
| 72 | + `You are a helpful AI assistant named Tobi. You can use tools to help the user with coding and file system tasks.`, |
| 73 | + ]; |
| 74 | + |
| 75 | + promptParts.push("### Current Workspace"); |
| 76 | + promptParts.push("Here is a list of files and directories in the current working directory:"); |
| 77 | + promptParts.push("```"); |
| 78 | + promptParts.push(filesAndDirs); |
| 79 | + promptParts.push("```"); |
| 80 | + |
| 81 | + if (instructionContent) { |
| 82 | + promptParts.push("### User-Provided Instructions"); |
| 83 | + promptParts.push( |
| 84 | + "The user has provided the following instructions in a `TOBI.md` or `AGENTS.md` file. Follow them carefully.", |
| 85 | + ); |
| 86 | + promptParts.push("```markdown"); |
| 87 | + promptParts.push(instructionContent); |
| 88 | + promptParts.push("```"); |
| 89 | + } |
| 90 | + |
| 91 | + promptParts.push("### Available Tools"); |
| 92 | + promptParts.push( |
| 93 | + "You have the following tools available. To use a tool, respond with a JSON object that strictly adheres to the TypeScript type definition of the tool.", |
| 94 | + ); |
| 95 | + promptParts.push( |
| 96 | + "The following are TypeScript type definitions for the tools. The `name` property is the tool to call, and you must provide the corresponding `arguments` object.", |
| 97 | + ); |
| 98 | + promptParts.push("```typescript"); |
| 99 | + promptParts.push(toolDefinitions); |
| 100 | + promptParts.push("```"); |
| 101 | + |
| 102 | + return promptParts.join("\n\n"); |
| 103 | +} |
0 commit comments