Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
can now see context files
  • Loading branch information
tjdevries committed Feb 27, 2024
commit 66c13c756ea25a879f9adb313e2f93a59c0c936e
8,824 changes: 5,165 additions & 3,659 deletions dist/cody-agent.js

Large diffs are not rendered by default.

32 changes: 9 additions & 23 deletions lua/sg/cody/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@
---@config { module = "sg.cody.commands" }

local auth = require "sg.auth"
local chat = require "sg.cody.rpc.chat"
local util = require "sg.utils"

local CodyBase = require "sg.components.layout.base"
local CodyFloat = require "sg.components.layout.float"
local CodySplit = require "sg.components.layout.split"
local CodyHover = require "sg.components.layout.hover"
local Message = require "sg.cody.message"
local State = require "sg.cody.state"
local protocol = require "sg.cody.protocol"
local CodySpeaker = require("sg.types").CodySpeaker

local commands = {}

--- Ask Cody a question, without any selection
---@param message string[]
commands.ask = function(message)
local layout = CodySplit.init {}

---@param opts? cody.ChatOpts
commands.ask = function(message, opts)
local contents = vim.tbl_flatten(message)
layout:request_user_message(contents)

chat.new(opts, function(_, id)
chat.submit_message(id, Message.init(CodySpeaker.human, contents):to_submit_message())
end)
end

--- Ask Cody about the selected code
---@param bufnr number
---@param start_row number
---@param end_row number
---@param message string
---@param opts cody.ChatOpts
commands.ask_range = function(bufnr, start_row, end_row, message, opts)
local chat = require "sg.cody.rpc.chat"
local selection = vim.api.nvim_buf_get_lines(bufnr, start_row, end_row, false)
local contents = vim.tbl_flatten {
message,
Expand Down Expand Up @@ -100,19 +98,7 @@ end

--- Open a selection to get an existing Cody conversation
commands.history = function()
local states = State.history()

vim.ui.select(states, {
prompt = "Cody History: ",
format_item = function(state)
return string.format("%s (%d)", state.name, #state.messages)
end,
}, function(state)
vim.schedule(function()
local layout = CodyFloat.init { state = state }
layout:show()
end)
end)
error "NOT YET IMPLEMENTED. PLEASE REPORT IF YOU WERE USING THIS"
end

--- Focus the currently active history window.
Expand Down
35 changes: 29 additions & 6 deletions lua/sg/cody/protocol.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local config = require "sg.config"
local debounce = require "sg.vendored.debounce"
local document = require "sg.document"

local proto = {}

Expand Down Expand Up @@ -57,7 +55,7 @@ proto.did_open = function(bufnr)
return
end

if not document.is_useful(bufnr) then
if not proto.document.is_useful(bufnr) then
return
end

Expand Down Expand Up @@ -90,7 +88,7 @@ proto.did_close = function(bufnr)
debounce_handles[bufnr] = nil
end

if not document.is_useful(bufnr) then
if not proto.document.is_useful(bufnr) then
return
end

Expand All @@ -103,11 +101,14 @@ proto.did_close = function(bufnr)
end

proto.did_focus = function(bufnr)
if not document.is_useful(bufnr) then
if not proto.document.is_useful(bufnr) then
return
end

require("sg.cody.rpc").notify("textDocument/didFocus", proto.get_text_document(bufnr, { content = false }))
require("sg.cody.rpc").notify(
"textDocument/didFocus",
proto.get_text_document(bufnr, { content = false })
)
end

proto.exit = function()
Expand All @@ -121,4 +122,26 @@ proto.exit = function()
rpc.exit()
end

proto.document = {
--- Determines if buffer is useful
---@param bufnr any
is_useful = function(bufnr)
if not vim.api.nvim_buf_is_valid(bufnr) then
return false
end

local bo = vim.bo[bufnr]
if not bo.buflisted then
return false
end

local name = vim.api.nvim_buf_get_name(bufnr)
if not name or name == "" then
return false
end

return true
end,
}

return proto
3 changes: 3 additions & 0 deletions lua/sg/cody/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ local get_server_config = function(creds, remote_url)
-- ["cody.debug.enable"] = true,
-- ["cody.debug.verbose"] = true,
},
-- TODO: Which should I put here? I cannot get multi-line completions anymore
autocompleteAdvancedProvider = "fireworks",
autocompleteAdvancedModel = "starcoder-7b",
},
capabilities = {
chat = "streaming",
Expand Down
132 changes: 113 additions & 19 deletions lua/sg/cody/rpc/chat.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local keymaps = require "sg.keymaps"
local log = require "sg.log"
local rpc = require "sg.cody.rpc"
local shared = require "sg.components.shared"
local util = require "sg.utils"

local CodySpeaker = require("sg.types").CodySpeaker
local Mark = require "sg.mark"
Expand Down Expand Up @@ -77,27 +79,88 @@ function Chat:reopen()
end

function Chat:_add_prompt_keymaps()
local set = function(mode, key, cb)
vim.keymap.set(mode, key, cb, { buffer = self.windows.prompt_bufnr })
local bufnr = self.windows.prompt_bufnr

keymaps.map(bufnr, "n", "<CR>", "Submit Message", function()
self:complete()
end)

keymaps.map(bufnr, "i", "<C-CR>", "Submit Message", function()
self:complete()
end)

keymaps.map(bufnr, { "i", "n" }, "<c-c>", "Quit Chat", function()
self:close()
end)

local with_history = function(key, mapped)
if not mapped then
mapped = key
end

local desc = "Execute '" .. key .. "' in history"
keymaps.map(bufnr, { "n", "i" }, key, desc, function()
if vim.api.nvim_win_is_valid(self.windows.history_win) then
vim.api.nvim_win_call(self.windows.history_win, function()
util.execute_keystrokes(mapped)
end)
end
end)
end

-- stylua: ignore start
set("i", "<CR>", function() self:complete() end)
set({"i", "n"}, "<C-C>", function() self:close() end)
-- stylua: ignore end

set("n", "<space>m", function()
rpc.request("webview/receiveMessage", {
id = self.id,
message = {
command = "chatModel",
model = "openai/gpt-4-1106-preview",
},
}, function(err, data)
self:set_current_model "openai/gpt-4-1106-preview"
self:render()
with_history "<c-f>"
with_history "<c-b>"
with_history "<c-e>"
with_history "<c-y>"

keymaps.map(bufnr, "n", "M", "Select Model", function()
require("sg.cody.rpc.chat").models(self.id, function(err, data)
if err then
return
end

---@type cody.ChatModelProvider[]
local models = data.models or {}
vim.ui.select(models, {
prompt = "Select a model for conversation",

--- Format an item
---@param item cody.ChatModelProvider
format_item = function(item)
return item.model
end,
}, function(choice)
rpc.request("webview/receiveMessage", {
id = self.id,
message = {
command = "chatModel",
model = choice.model,
},
}, function()
self:set_current_model(choice.model)
self:render()
end)
end)
end)
end)

keymaps.map(bufnr, "n", "?", "Show Keymaps", function()
keymaps.help(bufnr)
end)

-- TODO: Need to write a bit more stuff to manage this
-- keymaps.map(bufnr, "n", "<space>m", function()
-- rpc.request("webview/receiveMessage", {
-- id = self.id,
-- message = {
-- command = "chatModel",
-- model = "openai/gpt-4-1106-preview",
-- },
-- }, function(err, data)
-- self:set_current_model "openai/gpt-4-1106-preview"
-- self:render()
-- end)
-- end)
end

function Chat:set_current_model(model)
Expand All @@ -110,13 +173,14 @@ function Chat:close()
if self.windows.settings_win then
pcall(vim.api.nvim_win_close, self.windows.settings_win, true)
end

pcall(vim.api.nvim_buf_delete, self.windows.prompt_bufnr)
end

--- Add a new message and return its id
---@param message sg.cody.Message
function Chat:submit(message, callback)
callback = callback or function() end
-- require("sg.cody.rpc.chat").submit_message(self.id, message:to_submit_message(), callback)

rpc.request(
"chat/submitMessage",
Expand Down Expand Up @@ -198,6 +262,34 @@ function Chat:render()
)
end

if self.transcript then
-- Had some weird errors here
pcall(function()
table.insert(lines, "")
table.insert(lines, "Context Files:")
table.insert(lines, "")
for _, context_file in ipairs(self.transcript:context_files()) do
local range = context_file.range
local start = range.start

table.insert(
lines,
string.format(
"%s:%s:%s",
vim.fn.fnamemodify(context_file.uri.path, ":."),
start.line,
start.character
)
)
end
end)
end

-- Add keymaps
table.insert(lines, "")
table.insert(lines, "Cody Keymaps:")
vim.list_extend(lines, keymaps.help_lines(self.windows.prompt_bufnr))

vim.api.nvim_buf_set_lines(self.windows.settings_bufnr, 0, -1, false, lines)
end

Expand Down Expand Up @@ -338,14 +430,16 @@ function Chat._make_windows(opts)

shared.make_buf_minimal(settings_bufnr)
shared.make_win_minimal(settings_win)

vim.wo[settings_win].wrap = false
end

local prompt_bufnr = vim.api.nvim_create_buf(false, true)
local prompt_win = vim.api.nvim_open_win(prompt_bufnr, true, prompt_opts)
shared.make_buf_minimal(prompt_bufnr)
shared.make_win_minimal(prompt_win)

vim.api.nvim_create_autocmd("BufLeave", {
vim.api.nvim_create_autocmd("BufDelete", {
buffer = prompt_bufnr,
once = true,
callback = function()
Expand Down
14 changes: 14 additions & 0 deletions lua/sg/cody/transcript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ function Transcript:is_message_in_progress() return self._transcript.isMessageIn
function Transcript:last_message() return self.messages[#self.messages] end
-- stylua: ignore stop

--- Get context files
---@return cody.ContextFile[]
function Transcript:context_files()
local context = {}
for _, message in ipairs(self._transcript.messages) do
local files = message.contextFiles or {}
for _, file in ipairs(files) do
table.insert(context, file)
end
end

return context
end

return Transcript
23 changes: 0 additions & 23 deletions lua/sg/document.lua

This file was deleted.

Loading