@@ -14,7 +14,7 @@ M.config = {
1414local request_json = {
1515 messages = {
1616 {
17- content = " You will be provided with a sentence in {{from}}, and your task is to translate it into {{to}}. " ,
17+ content = " " ,
1818 role = " system" ,
1919 },
2020 {
@@ -44,43 +44,46 @@ local request_json = {
4444local function trans_system_prompt (request )
4545 local from = request .from
4646 if request .from == " auto" then
47- from = " (detect language) "
47+ return " 你会得到一个需要你检测语言的文本, 将他翻译为 " .. request . to .. " 。 "
4848 end
49- return " You will be provided with a sentence in "
50- .. from
51- .. " , and your task is to translate it into "
52- .. request .to
53- .. " ."
49+ return " 你会得到一个" .. from .. " 文本, 将他翻译为" .. request .to .. " 。"
5450end
5551
5652local function token ()
5753 return " Bearer " .. M .config .API_KEY
5854end
5955
6056--- @param cmd string[]
61- --- @param callback fun ( data : string )
57+ --- @param callback fun ( data : string , done : boolean )
6258local function handle_sse_events (cmd , callback )
6359 local _ = vim .fn .jobstart (cmd , {
6460 on_stdout = function (_ , data , _ )
6561 for _ , value in ipairs (data ) do
6662 if vim .startswith (value , " data:" ) then
6763 local _ , _ , text = string.find (value , " data: (.+)" )
68- if text ~= " [DONE]" then
69- local resp_json = vim .fn .json_decode (text )
70- if resp_json .usage ~= nil and M .config .show_usage then
71- callback " \n "
72- callback (
73- " API[token usage]: "
74- .. vim .inspect (resp_json .usage .prompt_cache_hit_tokens )
75- .. " "
76- .. vim .inspect (resp_json .usage .prompt_tokens )
77- .. " + "
78- .. vim .inspect (resp_json .usage .completion_tokens )
79- .. " = "
80- .. vim .inspect (resp_json .usage .total_tokens )
81- )
64+ if text == " [DONE]" then
65+ callback (text , true )
66+ else
67+ local ok , resp_json = pcall (vim .fn .json_decode , text )
68+ if ok then
69+ if resp_json .usage ~= nil and M .config .show_usage then
70+ callback (" \n " , false )
71+ callback (
72+ " API[token usage]: "
73+ .. vim .inspect (resp_json .usage .prompt_cache_hit_tokens )
74+ .. " "
75+ .. vim .inspect (resp_json .usage .prompt_tokens )
76+ .. " + "
77+ .. vim .inspect (resp_json .usage .completion_tokens )
78+ .. " = "
79+ .. vim .inspect (resp_json .usage .total_tokens ),
80+ false
81+ )
82+ else
83+ callback (resp_json .choices [1 ].delta .content , false )
84+ end
8285 else
83- callback (resp_json . choices [ 1 ]. delta . content )
86+ callback (" Error: " .. text , false )
8487 end
8588 end
8689 end
@@ -160,12 +163,17 @@ M.translate_float = function(request)
160163 end , { noremap = true , silent = true , buffer = buf })
161164
162165 local curlines = 0
163- local callback = function (data )
166+ local callback = function (data , done )
164167 if closed then
165168 return
166169 end
170+ if done then
171+ vim .bo [buf ].readonly = true
172+ vim .bo [buf ].modifiable = false
173+ return
174+ end
167175 -- 判断是否换行符
168- if data == " \n " then
176+ if data : match " \n " then
169177 curlines = 0
170178 else
171179 curlines = curlines + vim .fn .strdisplaywidth (data )
@@ -181,8 +189,9 @@ M.translate_float = function(request)
181189 end
182190
183191 if vim .api .nvim_buf_is_valid (buf ) then
184- if data == " \n " then
185- vim .api .nvim_put ({ " " , " " }, " c" , true , true )
192+ if data :match " \n " then
193+ local ln = vim .split (data , " \n " )
194+ vim .api .nvim_put (ln , " c" , true , true )
186195 else
187196 vim .api .nvim_put ({ data }, " c" , true , true )
188197 end
0 commit comments