From 68ead5d2f0e889a006ace6acbef03e07bfcd83a7 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 30 Jun 2025 18:44:06 +0800 Subject: [PATCH] feat: respect agents.md (#2376) --- lua/avante/llm.lua | 4 ++++ lua/avante/utils/prompts.lua | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index 500a454..4173eaa 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -5,6 +5,7 @@ local uv = vim.uv local curl = require("plenary.curl") local Utils = require("avante.utils") +local Prompts = require("avante.utils.prompts") local Config = require("avante.config") local Path = require("avante.path") local Providers = require("avante.providers") @@ -385,6 +386,9 @@ function M.generate_prompts(opts) if opts.tools then tools = vim.list_extend(tools, opts.tools) end if opts.prompt_opts and opts.prompt_opts.tools then tools = vim.list_extend(tools, opts.prompt_opts.tools) end + local agents_rules = Prompts.get_agents_rules_prompt() + if agents_rules then system_prompt = system_prompt .. "\n\n" .. agents_rules end + ---@type AvantePromptOptions return { system_prompt = system_prompt, diff --git a/lua/avante/utils/prompts.lua b/lua/avante/utils/prompts.lua index ba7ad7f..54c21be 100644 --- a/lua/avante/utils/prompts.lua +++ b/lua/avante/utils/prompts.lua @@ -210,4 +210,27 @@ I've successfully created the requested React component with the following featu return system_prompt end +--- Get the content of AGENTS.md or CLAUDE.md or OPENCODE.md +---@return string | nil +function M.get_agents_rules_prompt() + local Utils = require("avante.utils") + local project_root = Utils.get_project_root() + local file_names = { + "AGENTS.md", + "CLAUDE.md", + "OPENCODE.md", + ".cursorrules", + ".windsurfrules", + Utils.join_paths(".github", "copilot-instructions.md"), + } + for _, file_name in ipairs(file_names) do + local file_path = Utils.join_paths(project_root, file_name) + if vim.fn.filereadable(file_path) == 1 then + local content = vim.fn.readfile(file_path) + if content then return table.concat(content, "\n") end + end + end + return nil +end + return M