-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.lua
More file actions
169 lines (153 loc) · 4.34 KB
/
api.lua
File metadata and controls
169 lines (153 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
-- lua/neopencode/api.lua
local server = require("neopencode.server")
local config = require("neopencode.config")
local M = {}
function M.list_sessions(callback)
local pids = server.get_all_pids()
if #pids == 0 then
callback({})
return
end
local all_sessions = {}
local completed_requests = 0
for _, pid in ipairs(pids) do
local port = server.get_port(pid)
if port then
M._list_sessions_with_port(port, function(sessions)
-- Add port info to each session
for _, session in ipairs(sessions or {}) do
session._port = port
session._pid = pid
table.insert(all_sessions, session)
end
completed_requests = completed_requests + 1
if completed_requests == #pids then
callback(all_sessions)
end
end)
else
completed_requests = completed_requests + 1
if completed_requests == #pids then
callback(all_sessions)
end
end
end
end
function M._list_sessions_with_port(port, callback)
local url = "http://localhost:" .. port .. "/session"
local command = {
"curl",
"-s",
"-X",
"GET",
"-H",
"Content-Type: application/json",
url,
}
local stderr_lines = {}
vim.fn.jobstart(command, {
on_stdout = function(_, data)
if data and #data > 0 then
local response_str = table.concat(data, "")
if response_str == "" then return end
local ok, sessions = pcall(vim.fn.json_decode, response_str)
if ok then
callback(sessions)
else
vim.notify("JSON decode error: " .. sessions, vim.log.levels.ERROR)
end
end
end,
on_stderr = function(_, data)
if data then
for _, line in ipairs(data) do
table.insert(stderr_lines, line)
end
end
end,
on_exit = function(_, code)
if code ~= 0 then
local error_message = "curl command failed with exit code: " ..
code .. "\n\nStderr:\n" .. table.concat(stderr_lines, "\n")
vim.notify(error_message, vim.log.levels.ERROR)
end
end,
})
end
function M.send_chat(session_id, prompt)
local session = require("neopencode.session").current_session
if session and session._port then
M._send_chat_with_port(session._port, session_id, prompt)
else
vim.notify("No session selected or session port unknown", vim.log.levels.ERROR)
end
end
function M._send_chat_with_port(port, session_id, prompt)
local url = "http://localhost:" .. port .. "/session/" .. session_id .. "/message"
local message_id = vim.fn.system("uuidgen"):gsub("\n", "")
local body = {
messageID = message_id,
providerID = config.get("provider_id"),
modelID = config.get("model_id"),
mode = "build",
parts = {},
}
if prompt then
body.parts[#body.parts + 1] = {
type = "text",
id = vim.fn.system("uuidgen"):gsub("\n", ""),
sessionID = session_id,
messageID = message_id,
text = prompt
}
end
local command = {
"curl",
"-s",
"-X",
"POST",
"-H",
"Content-Type: application/json",
"-d",
vim.fn.json_encode(body),
url,
}
local stderr_lines = {}
vim.fn.jobstart(command, {
on_stdout = function(_, data)
if data and #data > 0 then
local response_str = table.concat(data, "")
if response_str == "" then return end
local ok, response = pcall(vim.fn.json_decode, response_str)
if ok then
local message_content = ""
if response and response.parts then
for _, part in ipairs(response.parts) do
if part.type == "text" then
message_content = message_content .. part.text
end
end
end
require("neopencode.actions").display_response(message_content)
else
vim.notify("JSON decode error: " .. response, vim.log.levels.ERROR)
end
end
end,
on_stderr = function(_, data)
if data then
for _, line in ipairs(data) do
table.insert(stderr_lines, line)
end
end
end,
on_exit = function(_, code)
if code ~= 0 then
local error_message = "curl command failed with exit code: " ..
code .. "\n\nStderr:\n" .. table.concat(stderr_lines, "\n")
vim.notify(error_message, vim.log.levels.ERROR)
end
end,
})
end
return M