import { join } from "node:path"; import { BUILTIN_WORKFLOW_COMMAND_NAME, expandBuiltinWorkflowCommandPrompt, } from "./builtin-workflow-command.js"; const BUILTIN_PROMPT_COMMAND_PATTERN = /^\/([^\s]+)(?:\s+([\s\S]*))?$/; const INIT_COMMAND_NAME = "init"; interface ResolveZCodeBuiltinPromptCommandOptions { /** * 动态工作流开关:只有显式 false 才禁止展开 `/workflow`。 * TUI 使用默认开启策略;headless 按本次 `--enable-workflow` 显式传入 true/false,默认 false。 * 关闭时返回 undefined;`workflow` 是保留名,自定义命令解析也不会展开它,原文作为普通 prompt * 交给模型。这与命令目录隐藏该入口的规则一致。 */ dynamicWorkflowEnabled?: boolean; workingDirectory?: string; } export function resolveZCodeBuiltinPromptCommand( input: string, options: ResolveZCodeBuiltinPromptCommandOptions = {}, ): string | undefined { const invocation = parseBuiltinPromptCommandInvocation(input); if (!invocation) { return undefined; } if (invocation.name === INIT_COMMAND_NAME) { const workingDirectory = options.workingDirectory ?? process.cwd(); return buildInitAgentsPrompt({ args: invocation.args, targetPath: join(workingDirectory, "AGENTS.md"), workingDirectory, }); } if (invocation.name === BUILTIN_WORKFLOW_COMMAND_NAME) { if (options.dynamicWorkflowEnabled === false) { return undefined; } return expandBuiltinWorkflowCommandPrompt(invocation.args); } return undefined; } function parseBuiltinPromptCommandInvocation(input: string): { args: string; name: string } | null { const match = BUILTIN_PROMPT_COMMAND_PATTERN.exec(input.trim()); if (!match?.[1]) { return null; } return { args: match[2]?.trim() ?? "", name: match[1].toLowerCase(), }; } function buildInitAgentsPrompt(params: { args: string; targetPath: string; workingDirectory: string; }): string { const additionalInstructions = params.args ? ["", "Additional user instructions supplied with /init:", "```text", params.args, "```"].join( "\n", ) : ""; return [ "You are running ZCode's built-in /init command.", "", "Your task is to create or update a concise workspace instruction file for future ZCode agents.", "", "Target:", `- Workspace directory: ${params.workingDirectory}`, `- Instruction file: ${params.targetPath}`, `- Existing hidden instruction candidates: ${join(params.workingDirectory, ".zcode", "AGENTS.md")} and ${join(params.workingDirectory, ".agents", "AGENTS.md")}`, "- File name must be exactly AGENTS.md.", "- This command targets the current workspace only. Do not write ~/.zcode/AGENTS.md.", additionalInstructions, "", "Process:", "1. First check whether .zcode/AGENTS.md or .agents/AGENTS.md exists in the workspace. If either exists, tell the user they already have an instructions file, mention the path found, and stop without creating a new AGENTS.md.", "2. Inspect the repository before writing. Prefer Read, Glob, Grep, and safe Bash commands such as ls, find, git status, and package-manager script inspection.", "3. If AGENTS.md already exists, read it first and update it with Edit instead of replacing it wholesale.", "4. If AGENTS.md does not exist, create it at the workspace root.", "5. Keep the file practical and short enough for future agents to read quickly.", "6. Include only project-specific facts future ZCode agents would otherwise miss.", "7. Ask the user only if a repository-specific decision cannot be inferred and would materially change the file.", "", "Recommended AGENTS.md content:", "- Repository purpose and major directories.", "- Build, typecheck, lint, and focused test commands discovered from the repo.", "- Architecture boundaries and layer rules that matter for edits.", "- Coding conventions, import/path rules, logging rules, UI/design rules, and platform compatibility constraints if present.", "- Known gotchas for desktop app, web, remote, stdio, protocols, or agent runtime if this repo has them.", "- Any documentation files that agents should read before changing sensitive areas.", "", "After creating or editing AGENTS.md, summarize the main sections you wrote and mention the file path.", ].join("\n"); }