-- lua/chatgpt_nvim/init.lua -- Modified to: -- 1) Use YAML for config and response parsing. -- 2) Assume ChatGPT response is YAML of form: -- files: -- - path: "somefile.lua" -- content: | -- multi line -- content -- local M = {} local context = require('chatgpt_nvim.context') local handler = require('chatgpt_nvim.handler') local config = require('chatgpt_nvim.config') local ok_yaml, lyaml = pcall(require, "lyaml") local function copy_to_clipboard(text) vim.fn.setreg('+', text) end -- Parse the response from ChatGPT in YAML. local function parse_response(raw) if not ok_yaml then vim.api.nvim_err_writeln("lyaml is not available. Please install lyaml for YAML parsing.") return nil end local ok, data = pcall(lyaml.load, raw) if not ok or not data then vim.api.nvim_err_writeln("Failed to parse YAML response.") return nil end -- lyaml.load returns a list of documents. We assume only one document is given. data = data[1] return data end function M.run_chatgpt_command() local conf = config.load() local user_input = vim.fn.input("Message for O1 Model: ") if user_input == "" then print("No input provided.") return end local dirs = conf.directories or {"."} local project_structure = context.get_project_structure(dirs) local all_files = context.get_project_files(dirs) local file_sections = context.get_file_contents(all_files) local current_file_content, current_file_path = context.get_current_file() local readme_content = context.get_readme_content() local sections = { conf.initial_prompt .. "\n", user_input, "\n\nproject structure:\n", project_structure, "\n\nproject files:\n" } -- Add all other files in configured directories table.insert(sections, file_sections) local prompt = table.concat(sections, "\n") -- Copy prompt to clipboard copy_to_clipboard(prompt) print("Prompt copied to clipboard! Please paste it into the ChatGPT O1 model and get the YAML response.") end function M.run_chatgpt_paste_command() print("Reading ChatGPT YAML response from clipboard...") local raw = handler.get_clipboard_content() if raw == "" then vim.api.nvim_err_writeln("Clipboard is empty. Please copy the YAML response from ChatGPT first.") return end local data = parse_response(raw) if not data or not data.files then vim.api.nvim_err_writeln("No 'files' field found in the YAML response.") return end for _, fileinfo in ipairs(data.files) do if fileinfo.path and fileinfo.content then handler.write_file(fileinfo.path, fileinfo.content) print("Wrote file: " .. fileinfo.path) else vim.api.nvim_err_writeln("Invalid file entry. Must have 'path' and 'content'.") end end end return M