Skip to content

Commit dfa2533

Browse files
committed
feat(gpt): track and manage job IDs for SSE requests
1 parent fb3cb90 commit dfa2533

6 files changed

Lines changed: 27 additions & 7 deletions

File tree

lua/kide/gpt/chat.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local state = {
77
cursormoved = false,
88
chatruning = false,
99
winleave = false,
10+
job = nil,
1011
}
1112
local chat_last = {}
1213

@@ -64,6 +65,10 @@ local close_gpt_win = function()
6465
state.cursormoved = false
6566
state.chatruning = false
6667
state.winleave = false
68+
if state.job then
69+
pcall(vim.fn.jobstop, state.job)
70+
state.job = nil
71+
end
6772
end
6873
end
6974

@@ -258,7 +263,7 @@ M.gpt_chat = function()
258263
end
259264
end
260265

261-
require("kide.gpt.sse").request(json, callback)
266+
state.job = require("kide.gpt.sse").request(json, callback)
262267
end
263268

264269
M.gpt_last = function(buf)

lua/kide/gpt/commit.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local M = {}
2+
local job = nil
23
local request_json = {
34
messages = {
45
{
@@ -33,7 +34,7 @@ function M.commit_message(diff, callback)
3334
json.messages[1].content =
3435
"I want you to act as a commit message generator. I will provide you with information about the task and the prefix for the task code, and I would like you to generate an appropriate commit message using the conventional commit format. Do not write any explanations or other words, just reply with the commit message."
3536
json.messages[2].content = diff
36-
require("kide.gpt.sse").request(json, callback)
37+
job = require("kide.gpt.sse").request(json, callback)
3738
end
3839

3940
M.commit_diff_msg = function()
@@ -52,6 +53,10 @@ M.commit_diff_msg = function()
5253
buffer = codebuf,
5354
callback = function()
5455
closed = true
56+
if job then
57+
pcall(vim.fn.jobstop, job)
58+
job = nil
59+
end
5560
end,
5661
})
5762

lua/kide/gpt/reasoner.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local state = {
77
cursormoved = false,
88
chatruning = false,
99
winleave = false,
10+
job = nil,
1011
}
1112
local chat_last = {}
1213

@@ -58,6 +59,10 @@ local close_gpt_win = function()
5859
state.cursormoved = false
5960
state.chatruning = false
6061
state.winleave = false
62+
if state.job then
63+
pcall(vim.fn.jobstop, state.job)
64+
state.job = nil
65+
end
6166
end
6267
end
6368

@@ -268,7 +273,7 @@ M.gpt_chat = function()
268273
end
269274
end
270275

271-
require("kide.gpt.sse").request(json, callback)
276+
state.job = require("kide.gpt.sse").request(json, callback)
272277
end
273278

274279
M.gpt_last = function(buf)

lua/kide/gpt/sse.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ local function handle_sse_events(cmd, callback)
7777
})
7878
end,
7979
})
80+
return job
8081
end
8182

8283
function M.request(json, callback)
@@ -95,7 +96,7 @@ function M.request(json, callback)
9596
body,
9697
M.config.API_URL,
9798
}
98-
handle_sse_events(cmd, callback)
99+
return handle_sse_events(cmd, callback)
99100
end
100101

101102
return M

lua/kide/gpt/translate.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ function M.translate(request, callback)
5454
local json = request_json
5555
json.messages[1].content = trans_system_prompt(request)
5656
json.messages[2].content = request.text
57-
require("kide.gpt.sse").request(json, callback)
57+
return require("kide.gpt.sse").request(json, callback)
5858
end
5959

6060
local max_width = 120
6161
local max_height = 40
62+
local job = nil
6263

6364
M.translate_float = function(request)
6465
local codebuf = vim.api.nvim_get_current_buf()
@@ -113,6 +114,10 @@ M.translate_float = function(request)
113114
buffer = buf,
114115
callback = function()
115116
closed = true
117+
if job then
118+
pcall(vim.fn.jobstop, job)
119+
job = nil
120+
end
116121
end,
117122
})
118123
vim.api.nvim_create_autocmd("WinLeave", {
@@ -172,7 +177,7 @@ M.translate_float = function(request)
172177
vim.api.nvim_put(put_data, "c", true, true)
173178
end
174179
end
175-
M.translate(request, callback)
180+
job = M.translate(request, callback)
176181
end
177182

178183
return M

lua/mappings.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ map("v", "<A-i>", function()
2121
end, 500)
2222
end, { desc = "toggle term" })
2323

24-
2524
map("n", "<leader>gb", require("gitsigns").blame_line, { desc = "gitsigns blame line" })
2625
map("n", "<ESC>", "<CMD>noh<CR>", { desc = "Clear Highlight" })
2726

0 commit comments

Comments
 (0)