197197
198198--- Show an error in the chat window.
199199--- @param err string | table | nil
200- --- @param append_newline boolean ?
201- local function show_error (err , append_newline )
200+ local function show_error (err )
202201 err = err or ' Unknown error'
203202
204203 if type (err ) == ' string' then
@@ -213,11 +212,7 @@ local function show_error(err, append_newline)
213212 err = utils .make_string (err )
214213 end
215214
216- if append_newline then
217- M .chat :append (' \n ' )
218- end
219-
220- M .chat :append (M .config .error_header .. ' \n ```error\n ' .. err .. ' \n ```' )
215+ M .chat :append (' \n ' .. M .config .error_header .. ' \n ```error\n ' .. err .. ' \n ```' )
221216 finish ()
222217end
223218
@@ -876,22 +871,21 @@ function M.ask(prompt, config)
876871 local selected_model , prompt = M .resolve_model (prompt , config )
877872 local embeddings , prompt = M .resolve_context (prompt , config )
878873
879- local has_output = false
880874 local query_ok , filtered_embeddings =
881875 pcall (context .filter_embeddings , prompt , selected_model , config .headless , embeddings )
882876
883877 if not query_ok then
884878 utils .schedule_main ()
885879 log .error (filtered_embeddings )
886880 if not config .headless then
887- show_error (filtered_embeddings , has_output )
881+ show_error (filtered_embeddings )
888882 end
889883 return
890884 end
891885
892886 local ask_ok , response , references , token_count , token_max_count = pcall (client .ask , client , prompt , {
893887 load_history = not config .headless ,
894- store_history = not config .headless ,
888+ summarize_history = not config .headless ,
895889 contexts = contexts ,
896890 selection = selection ,
897891 embeddings = filtered_embeddings ,
@@ -900,10 +894,16 @@ function M.ask(prompt, config)
900894 agent = selected_agent ,
901895 temperature = config .temperature ,
902896 on_progress = vim .schedule_wrap (function (token )
903- if not config .headless then
897+ local to_print = not config .headless and token
898+ if to_print and config .stream then
899+ local out = config .stream (token , state .source )
900+ if out ~= nil then
901+ to_print = out
902+ end
903+ end
904+ if to_print and to_print ~= ' ' then
904905 M .chat :append (token )
905906 end
906- has_output = true
907907 end ),
908908 })
909909
@@ -912,11 +912,30 @@ function M.ask(prompt, config)
912912 if not ask_ok then
913913 log .error (response )
914914 if not config .headless then
915- show_error (response , has_output )
915+ show_error (response )
916916 end
917917 return
918918 end
919919
920+ -- Call the callback function and store to history
921+ local to_store = not config .headless and response
922+ if to_store and config .callback then
923+ local out = config .callback (response , state .source )
924+ if out ~= nil then
925+ to_store = out
926+ end
927+ end
928+ if to_store and to_store ~= ' ' then
929+ table.insert (client .history , {
930+ content = prompt ,
931+ role = ' user' ,
932+ })
933+ table.insert (client .history , {
934+ content = to_store ,
935+ role = ' assistant' ,
936+ })
937+ end
938+
920939 if not config .headless then
921940 state .last_response = response
922941 M .chat .references = references
@@ -932,10 +951,6 @@ function M.ask(prompt, config)
932951
933952 finish ()
934953 end
935-
936- if config .callback then
937- config .callback (response , state .source )
938- end
939954 end )
940955
941956 if not ok then
0 commit comments