feat: init project (#2395)

This commit is contained in:
yetone
2025-07-02 18:44:30 +08:00
committed by GitHub
parent 1a6d1d6af4
commit 203861bb71
6 changed files with 116 additions and 58 deletions

View File

@@ -200,6 +200,92 @@ function M.generate_todos(user_input, cb)
})
end
---@class avante.AgentLoopOptions
---@field system_prompt string
---@field user_input string
---@field tools AvanteLLMTool[]
---@field on_complete fun(error: string | nil): nil
---@field session_ctx? table
---@field on_tool_log? fun(tool_id: string, tool_name: string, log: string, state: AvanteLLMToolUseState): nil
---@field on_start? fun(): nil
---@field on_chunk? fun(chunk: string): nil
---@field on_messages_add? fun(messages: avante.HistoryMessage[]): nil
---@param opts avante.AgentLoopOptions
function M.agent_loop(opts)
local messages = {}
table.insert(messages, { role = "user", content = "<task>" .. opts.user_input .. "</task>" })
local memory_content = nil
local history_messages = {}
local function no_op() end
local session_ctx = opts.session_ctx or {}
local stream_options = {
ask = true,
memory = memory_content,
code_lang = "unknown",
provider = Providers[Config.provider],
get_history_messages = function() return history_messages end,
on_tool_log = opts.on_tool_log or no_op,
on_messages_add = function(msgs)
msgs = vim.islist(msgs) and msgs or { msgs }
for _, msg in ipairs(msgs) do
local idx = nil
for i, m in ipairs(history_messages) do
if m.uuid == msg.uuid then
idx = i
break
end
end
if idx ~= nil then
history_messages[idx] = msg
else
table.insert(history_messages, msg)
end
end
end,
session_ctx = session_ctx,
prompt_opts = {
system_prompt = opts.system_prompt,
tools = opts.tools,
messages = messages,
},
on_start = opts.on_start or no_op,
on_chunk = opts.on_chunk or no_op,
on_stop = function(stop_opts)
if stop_opts.error ~= nil then
local err = string.format("dispatch_agent failed: %s", vim.inspect(stop_opts.error))
opts.on_complete(err)
return
end
opts.on_complete(nil)
end,
}
local function on_memory_summarize(pending_compaction_history_messages)
local compaction_history_message_uuids = {}
for _, msg in ipairs(pending_compaction_history_messages or {}) do
compaction_history_message_uuids[msg.uuid] = true
end
M.summarize_memory(memory_content, pending_compaction_history_messages or {}, function(memory)
if memory then stream_options.memory = memory.content end
local new_history_messages = {}
for _, msg in ipairs(history_messages) do
if compaction_history_message_uuids[msg.uuid] then goto continue end
table.insert(new_history_messages, msg)
::continue::
end
history_messages = new_history_messages
M._stream(stream_options)
end)
end
stream_options.on_memory_summarize = on_memory_summarize
M._stream(stream_options)
end
---@param opts AvanteGeneratePromptsOptions
---@return AvantePromptOptions
function M.generate_prompts(opts)

View File

@@ -83,6 +83,7 @@ end
function M.func(opts, on_log, on_complete, session_ctx)
local Llm = require("avante.llm")
if not on_complete then return false, "on_complete not provided" end
local prompt = opts.prompt
local tools = get_available_tools()
local start_time = Utils.get_timestamp()
@@ -94,23 +95,16 @@ Your task is to help the user with their request: "${prompt}"
Be thorough and use the tools available to you to find the most relevant information.
When you're done, provide a clear and concise summary of what you found.]]):gsub("${prompt}", prompt)
local messages = {}
table.insert(messages, { role = "user", content = "go!" })
local tool_use_messages = {}
local total_tokens = 0
local final_response = ""
local memory_content = nil
local history_messages = {}
local stream_options = {
ask = true,
memory = memory_content,
code_lang = "unknown",
provider = Providers[Config.provider],
get_history_messages = function() return history_messages end,
---@type avante.AgentLoopOptions
local agent_loop_options = {
system_prompt = system_prompt,
user_input = "start",
tools = tools,
on_tool_log = session_ctx.on_tool_log,
on_messages_add = function(msgs)
msgs = vim.islist(msgs) and msgs or { msgs }
@@ -120,37 +114,18 @@ When you're done, provide a clear and concise summary of what you found.]]):gsub
tool_use_messages[msg.uuid] = true
end
end
for _, msg in ipairs(msgs) do
local idx = nil
for i, m in ipairs(history_messages) do
if m.uuid == msg.uuid then
idx = i
break
end
end
if idx ~= nil then
history_messages[idx] = msg
else
table.insert(history_messages, msg)
end
end
if session_ctx.on_messages_add then session_ctx.on_messages_add(msgs) end
end,
session_ctx = session_ctx,
prompt_opts = {
system_prompt = system_prompt,
tools = tools,
messages = messages,
},
on_start = session_ctx.on_start,
on_chunk = function(chunk)
if not chunk then return end
final_response = final_response .. chunk
total_tokens = total_tokens + (#vim.split(chunk, " ") * 1.3)
end,
on_stop = function(stop_opts)
if stop_opts.error ~= nil then
local err = string.format("dispatch_agent failed: %s", vim.inspect(stop_opts.error))
on_complete = function(err)
if err ~= nil then
err = string.format("dispatch_agent failed: %s", vim.inspect(err))
on_complete(err, nil)
return
end
@@ -178,29 +153,7 @@ When you're done, provide a clear and concise summary of what you found.]]):gsub
end,
}
local function on_memory_summarize(pending_compaction_history_messages)
Llm.summarize_memory(memory_content, pending_compaction_history_messages or {}, function(memory)
if memory then stream_options.memory = memory.content end
local new_history_messages = {}
for _, msg in ipairs(history_messages) do
if
vim
.iter(pending_compaction_history_messages)
:find(function(pending_compaction_msg) return pending_compaction_msg.uuid == msg.uuid end)
then
goto continue
end
table.insert(new_history_messages, msg)
::continue::
end
history_messages = new_history_messages
Llm._stream(stream_options)
end)
end
stream_options.on_memory_summarize = on_memory_summarize
Llm._stream(stream_options)
Llm.agent_loop(agent_loop_options)
end
return M

View File

@@ -1962,6 +1962,19 @@ function Sidebar:render_state()
self.state_timer = vim.defer_fn(function() self:render_state() end, 160)
end
function Sidebar:init_current_project(args, cb)
local user_input = [[
Please analyze the code and documentation of this project, then:
- If there is an AGENTS.md file in the project root directory, combine it with the existing AGENTS.md content to generate a new AGENTS.md.
- If the existing AGENTS.md content conflicts with the newly generated content, replace the conflicting old parts with the new content.
- If there is no AGENTS.md file in the project root directory, create a new AGENTS.md file and write the new content in it.]]
self:new_chat(args, cb)
self.code.selection = nil
self.file_selector:reset()
if self.selected_files_container then self.selected_files_container:unmount() end
vim.api.nvim_exec_autocmds("User", { pattern = "AvanteInputSubmitted", data = { request = user_input } })
end
function Sidebar:compact_history_messages(args, cb)
local history_memory = self.chat_history.memory
local messages = Utils.get_history_messages(self.chat_history)

View File

@@ -36,6 +36,10 @@ You accomplish a given task iteratively, breaking it down into clear steps and w
2. Work through these goals sequentially, utilizing available tools one at a time as necessary. Each goal should correspond to a distinct step in your problem-solving process. You will be informed on the work completed and what's remaining as you go.
3. Once you've completed the user's task, you must use the attempt_completion tool to present the result of the task to the user. You may also provide a CLI command to showcase the result of your task; this can be particularly useful for web development tasks, where you can run e.g. \`open index.html\` to show the website you've built.
4. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.`
5. After each task completion, use the attempt_completion tool to inform the user of the task completion results.
6. At the end of the task, check if AGENTS.md already exists in the root directory:
- If the AGENTS.md file already exists, combine the existing content with the operations performed in this task to update its content, and sync the updated content to the AGENTS.md file.
- If the AGENTS.md file does not exist, do not perform any operations.
{% endblock %}

View File

@@ -399,7 +399,7 @@ vim.g.avante_login = vim.g.avante_login
---@alias AvanteLLMMemorySummarizeCallback fun(pending_compaction_history_messages: avante.HistoryMessage[]): nil
---
---@alias AvanteLLMToolUseState "generating" | "generated" | "running" | "succeeded" | "failed"
---@alias avante.GenerateState "generating" | "tool calling" | "failed" | "succeeded" | "cancelled" | "searching" | "thinking" | "compacting" | "compacted"
---@alias avante.GenerateState "generating" | "tool calling" | "failed" | "succeeded" | "cancelled" | "searching" | "thinking" | "compacting" | "compacted" | "initializing" | "initialized"
---
---@class AvanteLLMStreamOptions: AvanteGeneratePromptsOptions
---@field on_start AvanteLLMStartCallback

View File

@@ -1376,6 +1376,7 @@ function M.get_commands()
local builtin_items = {
{ description = "Show help message", name = "help" },
{ description = "Init AGENTS.md based on the current project", name = "init" },
{ description = "Clear chat history", name = "clear" },
{ description = "New chat", name = "new" },
{ description = "Compact history messages to save tokens", name = "compact" },
@@ -1397,6 +1398,7 @@ function M.get_commands()
clear = function(sidebar, args, cb) sidebar:clear_history(args, cb) end,
new = function(sidebar, args, cb) sidebar:new_chat(args, cb) end,
compact = function(sidebar, args, cb) sidebar:compact_history_messages(args, cb) end,
init = function(sidebar, args, cb) sidebar:init_current_project(args, cb) end,
lines = function(_, args, cb)
if cb then cb(args) end
end,