Skip to content

Commit

Permalink
Object 22
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Dec 28, 2024
1 parent e35da31 commit a96de97
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 59 deletions.
21 changes: 13 additions & 8 deletions lua/fittencode/chat/conversation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Conversation:new(opts)
id = opts.id,
template = opts.template,
init_variables = opts.init_variables,
messages = {},
update_view = Fn.schedule_call_wrap_fn(opts.update_view),
update_status = Fn.schedule_call_wrap_fn(opts.update_status)
}
Expand All @@ -36,7 +37,7 @@ end
function Conversation:get_title()
local header = self.template.header
local message
if self.messages and self.messages[1] then
if self.messages[1] then
message = self.messages[1].content
end
if header.useFirstMessageAsTitle == true and message ~= nil then
Expand All @@ -52,7 +53,7 @@ end

---@return boolean
function Conversation:is_title_message()
return self.template.header.useFirstMessageAsTitle == true and self.messages and self.messages[1] ~= nil
return self.template.header.useFirstMessageAsTitle == true and self.messages[1] ~= nil
end

---@return string
Expand Down Expand Up @@ -105,6 +106,9 @@ function Conversation:evaluate_template(template, variables)
variables.temporaryEditorContent = self.temporary_editor_content
end
local env = vim.tbl_deep_extend('force', {}, self.init_variables or {}, self.variables or {})
env.messages = self.messages
Log.debug('Evaluating template: {}', template)
Log.debug('Evaluating env: {}', env)
return VM.run(env, template)
end

Expand Down Expand Up @@ -138,7 +142,7 @@ end

---@param opts table
function Conversation:execute_chat(opts)
if Config.fitten.version == 'default' then
if Config.server.fitten_version == 'default' then
opts.workspace = false
end
if opts._workspace then
Expand All @@ -161,6 +165,7 @@ function Conversation:execute_chat(opts)
local variables = self:resolve_variables_at_message_time()
local retrieval_augmentation = ir.retrievalAugmentation
local evaluated = self:evaluate_template(ir.template, variables)
Log.debug('Evaluated message: {}', evaluated)

Promise:new(function(resolve, reject)
self.request_handle = Client.chat({
Expand All @@ -170,19 +175,19 @@ function Conversation:execute_chat(opts)
project_id = '',
}
}, function()
self.update_state({ id = self.id, stream = true })
self.update_status({ id = self.id, stream = true })
end, nil, function(response)
self.update_state({ id = self.id, stream = true })
self:handle_partial_completion(response)
self.update_status({ id = self.id, stream = true })
-- self:handle_partial_completion(response)
end, function(error)
reject(error)
end, function()
resolve()
end)
end):forward(function()
self.update_state({ id = self.id, stream = false })
self.update_status({ id = self.id, stream = false })
end, function(error)
self.update_state({ id = self.id, stream = false })
self.update_status({ id = self.id, stream = false })
Log.error('Error while executing chat, conversation id = {}, error = {}', self.id, error)
end)
end
Expand Down
8 changes: 5 additions & 3 deletions lua/fittencode/chat/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,19 @@ function View:update_char_input(enable, id)
if vim.api.nvim_get_mode().mode == 'i' and vim.api.nvim_get_current_buf() == self.char_input.buf and key == enter_key then
vim.api.nvim_buf_call(self.char_input.buf, function()
Log.debug('View char input enter key')
vim.api.nvim_command('doautocmd User fittencode.ChatInputReady')
vim.api.nvim_exec_autocmds('User', { pattern = 'fittencode.ChatInputReady', modeline = false })
end)
end
end)
end)
self.char_input_autocmd = vim.api.nvim_create_autocmd('fittencode.ChatInputReady', {
buffer = self.char_input.buf,
self.char_input_autocmd = vim.api.nvim_create_autocmd('User', {
pattern = 'fittencode.ChatInputReady',
once = true,
callback = function()
vim.api.nvim_buf_call(self.char_input.buf, function()
local message = vim.api.nvim_buf_get_lines(self.char_input.buf, 0, -1, false)[1]
message = self:with_fcps(message)
Log.debug('View char input send message: {}', message)
self:send_message({
type = 'send_message',
data = {
Expand Down
64 changes: 25 additions & 39 deletions lua/fittencode/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local function get_platform_info_as_url_params()
if not platform_info then
platform_info = table.concat({
'ide=' .. ide,
'ide_v=' .. vim.version(),
'ide_v=' .. tostring(vim.version()),
'os=' .. vim.uv.os_uname().sysname,
'os_v=' .. vim.uv.os_uname().release,
'v=' .. version,
Expand Down Expand Up @@ -154,8 +154,8 @@ local function login(username, password, on_success, on_error)
on_error = vim.schedule_wrap(function()
reject()
end),
on_once = vim.schedule_wrap(function(res)
local _, login_data = pcall(vim.fn.json_decode, res)
on_once = vim.schedule_wrap(function(data)
local _, login_data = pcall(vim.fn.json_decode, data.output)
if not _ or login_data.code ~= 200 then
reject()
else
Expand All @@ -172,8 +172,8 @@ local function login(username, password, on_success, on_error)
on_error = vim.schedule_wrap(function()
reject()
end),
on_once = vim.schedule_wrap(function(res)
local _, fico_data = pcall(vim.fn.json_decode, res)
on_once = vim.schedule_wrap(function(data)
local _, fico_data = pcall(vim.fn.json_decode, data.output)
if not _ or fico_data.data == nil or fico_data.data.fico_token == nil then
reject()
else
Expand Down Expand Up @@ -362,8 +362,8 @@ local function login3rd(source, on_success, on_error)
Promise:new(function(resolve, reject)
HTTP.get(check_url, {
on_error = vim.schedule_wrap(function() reject() end),
on_once = vim.schedule_wrap(function(fico_res)
local _, fico_data = pcall(vim.fn.json_decode, fico_res)
on_once = vim.schedule_wrap(function(data)
local _, fico_data = pcall(vim.fn.json_decode, data.output)
if not _ or fico_data.token == nil or fico_data.token == '' then
reject()
else
Expand Down Expand Up @@ -415,37 +415,40 @@ end
---@field is_active function

---@return RequestHandle?
local function request(method, url, headers, body, on_create, on_once, on_stream, on_error, on_exit)
local function request(method, url, headers, body, no_buffer, on_create, on_once, on_stream, on_error, on_exit)
local function wrap()
local canceled = false
---@type uv_process_t?
local process = nil
local opts = {
headers = headers,
body = body,
no_buffer = no_buffer,
on_create = vim.schedule_wrap(function(data)
Log.debug('process created {}', data)
if canceled then return end
process = data.process
Fn.schedule_call(on_create)
end),
on_once = vim.schedule_wrap(function(res)
on_once = vim.schedule_wrap(function(data)
Log.debug('process once {}', data)
if canceled then return end
Fn.schedule_call(on_once, res)
Fn.schedule_call(on_once, data)
end),
on_stream = vim.schedule_wrap(function(error, chunk)
on_stream = vim.schedule_wrap(function(data)
if canceled then return end
if error then
Fn.schedule_call(on_error)
if data.error then
Fn.schedule_call(on_error, { error = data.error })
else
Fn.schedule_call(on_stream, chunk)
Fn.schedule_call(on_stream, { chunk = data.chunk })
end
end),
on_error = vim.schedule_wrap(function()
on_error = vim.schedule_wrap(function(data)
if canceled then return end
Fn.schedule_call(on_error)
Fn.schedule_call(on_error, data)
end),
on_exit = vim.schedule_wrap(function()
Fn.schedule_call(on_exit)
on_exit = vim.schedule_wrap(function(data)
Fn.schedule_call(on_exit, data)
end),
}
HTTP[method](url, opts)
Expand Down Expand Up @@ -496,7 +499,7 @@ local function generate_one_stage(prompt, on_once, on_error, on_exit)
['Content-Type'] = 'application/json',
}
local url = server_url() .. preset_urls.generate_one_stage .. '/' .. key .. '' .. get_platform_info_as_url_params()
return request('post', url, headers, prompt, nil, on_once, nil, on_error, on_exit)
return request('post', url, headers, prompt, false, nil, on_once, nil, on_error, on_exit)
end

local function chat(prompt, on_create, on_once, on_stream, on_error, on_exit)
Expand All @@ -508,26 +511,9 @@ local function chat(prompt, on_create, on_once, on_stream, on_error, on_exit)
local headers = {
['Content-Type'] = 'application/json',
}
local url = server_url() .. preset_urls.chat .. '/?ft_token=' .. key .. '&' .. get_platform_info_as_url_params()
return request('post', url, headers, prompt, on_create, on_once, on_stream, on_error, on_exit)
end

local function chat_heartbeat(prompt, on_once, on_stream, on_error, on_exit)
local on_once_hb = function(output)
local data = {}
for _, line in ipairs(output) do
local _, delta = pcall(vim.fn.json_decode, line)
if not _ then
-- ignore invalid json
else
if not Fn.startwith(delta, 'heartbeat') then
data[#data + 1] = delta
end
end
end
on_once(table.concat(data))
end
return chat(prompt, on_once_hb, on_stream, on_error, on_exit)
local url = server_url() .. preset_urls.chat .. '?ft_token=' .. key .. '&' .. get_platform_info_as_url_params()
Log.debug('chat url: {}', url)
return request('post', url, headers, prompt, true, on_create, on_once, on_stream, on_error, on_exit)
end

return {
Expand Down
5 changes: 4 additions & 1 deletion lua/fittencode/fn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ local function schedule_call_wrap_fn(fx, ...)
end

local function schedule_call_foreach(v, ...)
if not v then
return
end
if vim.islist(v) then
for _, fx in ipairs(v) do
schedule_call(fx, ...)
Expand Down Expand Up @@ -92,7 +95,7 @@ local function timezone_language()
end

local function pack(...)
return { n = select("#", ...); ... }
return { n = select('#', ...), ... }
end

local function format(msg, ...)
Expand Down
22 changes: 16 additions & 6 deletions lua/fittencode/http/curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ local Log = require('fittencode.log')
local Promise = require('fittencode.promise')

local function spawn(params, on_create, on_once, on_stream, on_error, on_exit)
Log.debug('spawn params = {}', params)

local cmd = params.cmd
local args = params.args

Log.debug('spawn args = {}', table.concat(args, ' '))

local output = {}
local error = {}
local process = nil
Expand Down Expand Up @@ -35,20 +39,22 @@ local function spawn(params, on_create, on_once, on_stream, on_error, on_exit)
else
Fn.schedule_call(on_once, { exit_code = exit_code, output = output, error = error, })
end
Fn.schedule_call(on_exit)
Fn.schedule_call(on_exit, { exit_code = exit_code })
end)
end)

Fn.schedule_call(on_create, { process = process, pid = pid, })

local function on_stdout(err, chunk)
Log.debug('on_stdout err = {}, chunk = {}', err, chunk)
Fn.schedule_call(on_stream, { error = err, chunk = chunk })
if not err and chunk then
output[#output + 1] = chunk
end
end

local function on_stderr(err, chunk)
Log.debug('on_stderr err = {}, chunk = {}', err, chunk)
if not err and chunk then
error[#error + 1] = chunk
end
Expand Down Expand Up @@ -79,15 +85,19 @@ local function spawn_curl(args, opts)
if exit_code ~= curl.exit_code_success then
Fn.schedule_call(opts.on_error, { exit_code = exit_code, error = error, })
else
Fn.schedule_call(opts.on_once, output)
Fn.schedule_call(opts.on_once, { output = output })
end
end
spawn(params, opts.on_create, on_once, opts.on_stream, opts.on_error, opts.on_exit)
end

local function build_args(args, headers)
local function build_args(args, opts)
if opts.no_buffer then
args[#args + 1] = '--no-buffer'
end
local headers = opts.headers or {}
vim.list_extend(args, curl.default_args)
for k, v in pairs(headers or {}) do
for k, v in pairs(headers) do
args[#args + 1] = '-H'
if Fn.is_windows() then
args[#args + 1] = '"' .. k .. ': ' .. v .. '"'
Expand All @@ -107,7 +117,7 @@ local function get(url, opts)
local args = {
url,
}
build_args(args, opts.headers)
build_args(args, opts)
spawn_curl(args, opts)
end

Expand Down Expand Up @@ -135,7 +145,7 @@ local function post(url, opts)
'-X',
'POST',
}
build_args(args, opts.headers)
build_args(args, opts)
if type(opts.body) == 'string' and vim.fn.filereadable(opts.body) == 1 then
add_data_argument(args, opts.body, true)
spawn_curl(args, opts)
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function StateConversation:is_empty()
end

function StateConversation:user_can_reply()
return self.content.state and self.content.state.type == 'user_can_reply'
return self.content.state == nil or (self.content.state ~= nil and self.content.state.type == 'user_can_reply')
end

---@param conversation fittencode.Chat.Conversation
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
---@field evaluate_template function
---@field request_handle RequestHandle?
---@field update_view function?
---@field update_state function?
---@field update_status function?

---@class fittencode.Chat.StateConversation.Header
---@field title string
Expand Down

0 comments on commit a96de97

Please sign in to comment.