8888
8989--- Parse the chat buffer for the last message
9090--- @param chat CodeCompanion.Chat
91- --- @param role string
9291--- @param start_range number
9392--- @return { content : string }
94- local function ts_parse_messages (chat , role , start_range )
93+ local function ts_parse_messages (chat , start_range )
9594 local query = get_query (" markdown" , " chat" )
9695
9796 local tree = chat .parser :parse ({ start_range - 1 , - 1 })[1 ]
@@ -589,7 +588,7 @@ function Chat:submit(opts)
589588
590589 local bufnr = self .bufnr
591590
592- local message = ts_parse_messages (self , user_role , self .header_line )
591+ local message = ts_parse_messages (self , self .header_line )
593592
594593 -- Check if any watched buffers have any changes
595594 self .watchers :check_for_changes (self )
@@ -860,6 +859,8 @@ function Chat:close()
860859 self = nil
861860end
862861
862+ local has_been_reasoning = false
863+
863864--- Add a message directly to the chat buffer. This will be visible to the user
864865--- @param data table
865866--- @param opts ? table
@@ -868,21 +869,42 @@ function Chat:add_buf_message(data, opts)
868869 local bufnr = self .bufnr
869870 local new_response = false
870871
871- if (data .role and data .role ~= self .last_role ) or (opts and opts .force_role ) then
872+ local function write (text )
873+ for _ , t in ipairs (vim .split (text , " \n " , { plain = true , trimempty = false })) do
874+ table.insert (lines , t )
875+ end
876+ end
877+
878+ local function new_role ()
872879 new_response = true
873880 self .last_role = data .role
874881 table.insert (lines , " " )
875882 table.insert (lines , " " )
876883 self .ui :set_header (lines , config .strategies .chat .roles [data .role ])
877884 end
878885
879- if data .content then
880- for _ , text in ipairs (vim .split (data .content , " \n " , { plain = true , trimempty = false })) do
881- table.insert (lines , text )
886+ local function append_data ()
887+ if data .reasoning then
888+ has_been_reasoning = true
889+ if new_response then
890+ table.insert (lines , " ### Reasoning" )
891+ table.insert (lines , " " )
892+ end
893+ write (data .reasoning )
894+ else
895+ if has_been_reasoning then
896+ has_been_reasoning = false
897+ table.insert (lines , " " )
898+ table.insert (lines , " " )
899+ table.insert (lines , " ### Response" )
900+ table.insert (lines , " " )
901+ end
902+ write (data .content )
882903 end
904+ end
883905
906+ local function update_buffer ()
884907 self .ui :unlock_buf ()
885-
886908 local last_line , last_column , line_count = self .ui :last ()
887909 if opts and opts .insert_at then
888910 last_line = opts .insert_at
@@ -906,6 +928,17 @@ function Chat:add_buf_message(data, opts)
906928 self .ui :follow ()
907929 end
908930 end
931+
932+ -- Handle a new role
933+ if (data .role and data .role ~= self .last_role ) or (opts and opts .force_role ) then
934+ new_role ()
935+ end
936+
937+ -- Append the output from the LLM
938+ if data .content or data .reasoning then
939+ append_data ()
940+ update_buffer ()
941+ end
909942end
910943
911944--- When a request has finished, reset the chat buffer
0 commit comments