-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathllm.lua
More file actions
499 lines (440 loc) · 13.9 KB
/
Copy pathllm.lua
File metadata and controls
499 lines (440 loc) · 13.9 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
local M = {}
local function get_config()
return require("jumpy").config
end
local function build_file_block(path, contents)
return string.format("--- FILE: %s ---\n%s\n--- END FILE ---", path, contents)
end
local function build_messages(context)
local config = get_config()
local tagged = context.tagged_files
if tagged and #tagged > 0 then
local parts = {}
for _, file in ipairs(tagged) do
table.insert(parts, build_file_block(file.path, table.concat(file.lines, "\n")))
end
if context.symbols and context.symbols ~= "" then
table.insert(parts, context.symbols)
end
table.insert(parts, "")
-- Name the active buffer so relative references ("this file", "here")
-- resolve. The FILE headers are left verbatim since SEARCH paths must
-- match them exactly.
if context.primary_path and context.primary_path ~= "" then
table.insert(parts, string.format('The current file (what "this file" refers to) is: %s', context.primary_path))
end
table.insert(parts, "Instruction: " .. context.prompt)
local user_content = table.concat(parts, "\n")
local system = config.system_prompt .. "\n\n" .. config.system_prompt_multi_file
return {
{ role = "system", content = system },
{ role = "user", content = user_content },
}
end
-- A visual selection marks the region to focus on. The full file is still
-- sent for context; the model is asked to prefer changing only the selection.
local selection_note = ""
local sel = context.selection
if sel and sel.lines and #sel.lines > 0 then
selection_note = string.format(
"\n\nFocus on the SELECTED region below (lines %d-%d). Use the rest of the "
.. "file as context and prefer to change only the selection:\n"
.. "--- SELECTION (lines %d-%d) ---\n%s\n--- END SELECTION ---",
sel.start_line or 0,
sel.end_line or 0,
sel.start_line or 0,
sel.end_line or 0,
table.concat(sel.lines, "\n")
)
end
local user_content = string.format(
"File type: %s\n\n--- FILE CONTENTS ---\n%s\n--- END FILE ---%s%s\n\nInstruction: %s",
context.filetype or "text",
context.file_contents,
context.symbols,
selection_note,
context.prompt
)
return {
{ role = "system", content = config.system_prompt },
{ role = "user", content = user_content },
}
end
local function build_reprompt_messages(context)
local config = get_config()
local template = "File type: %s\n\n"
.. "--- PROPOSED BLOCK ---\n%s\n--- END PROPOSED ---%s\n\n"
.. "The user rejected the PROPOSED BLOCK above. "
.. "Return SEARCH/REPLACE blocks (per the system prompt) whose SEARCH "
.. "content matches lines from the PROPOSED BLOCK, revising it "
.. "according to:\n\n"
.. "New instruction: %s"
local user_content = string.format(
template,
context.filetype or "text",
table.concat(context.proposed_lines, "\n"),
context.symbols or "",
context.prompt
)
return {
{ role = "system", content = config.system_prompt },
{ role = "user", content = user_content },
}
end
M._build_messages = build_messages
local function is_anthropic()
local config = get_config()
return config.provider == "anthropic"
end
local function is_claude_code()
local config = get_config()
return config.provider == "claude_code"
end
local function build_curl_cmd(body_json, config, extra_headers)
local cmd = { "curl", "-s", "-H", "Content-Type: application/json" }
for _, h in ipairs(extra_headers or {}) do
table.insert(cmd, "-H")
table.insert(cmd, h)
end
table.insert(cmd, "-d")
table.insert(cmd, body_json)
table.insert(cmd, config.endpoint)
return cmd
end
local function build_curl_cmd_openai(body_json, config)
return build_curl_cmd(body_json, config, {
string.format("Authorization: Bearer %s", config.api_key),
})
end
local function build_curl_cmd_anthropic(body_json, config)
return build_curl_cmd(body_json, config, {
string.format("x-api-key: %s", config.api_key),
"anthropic-version: 2023-06-01",
})
end
local function extract_content_openai(parsed)
return parsed.choices and parsed.choices[1] and parsed.choices[1].message and parsed.choices[1].message.content
end
local function extract_content_anthropic(parsed)
if not parsed.content or #parsed.content == 0 then
return nil
end
for _, block in ipairs(parsed.content) do
if block.type == "text" then
return block.text
end
end
return nil
end
local function extract_content_claude_code(parsed)
return parsed.result
end
local function claude_code_env()
local env = vim.fn.environ()
-- Claude Code prefers an API key over its subscription login if both exist.
env.ANTHROPIC_API_KEY = nil
return env
end
function M._build_claude_code_cmd(messages, config)
local system_text = ""
local user_text = ""
for _, message in ipairs(messages) do
if message.role == "system" then
system_text = message.content
elseif message.role == "user" then
user_text = message.content
end
end
-- Note: we deliberately do NOT pass --bare. In current Claude Code, --bare
-- forces Anthropic auth to ANTHROPIC_API_KEY/apiKeyHelper only and never
-- reads the OAuth/keychain login, which breaks Pro/Max subscription users
-- (the CLI reports "Not logged in"). We still keep the request tool-free,
-- MCP-restricted, and session-less via the explicit flags below.
local cmd = {
config.claude_code_command or "claude",
"-p",
"--output-format",
"json",
"--tools",
"",
"--strict-mcp-config",
"--no-session-persistence",
"--system-prompt",
system_text,
}
if config.model and config.model ~= "" then
table.insert(cmd, "--model")
table.insert(cmd, config.model)
end
-- End-of-options separator: multi-file prompts start with "--- FILE: ...",
-- which the CLI would otherwise parse as an unknown option.
table.insert(cmd, "--")
table.insert(cmd, user_text)
return cmd
end
local function make_claude_code_request(messages, callback, opts)
opts = opts or {}
local config = get_config()
local command = config.claude_code_command or "claude"
local loading = require("jumpy.loading")
local requests = require("jumpy.requests")
if vim.fn.executable(command) ~= 1 then
loading.error("Claude Code CLI not found; install it or set claude_code_command")
return
end
local env = claude_code_env()
local request_id = requests.begin({ label = opts.label, targets = opts.targets })
loading.start()
-- Every terminal path must release exactly one spinner refcount and drop
-- the request from the registry.
local finished = false
local function finish()
if finished then
return
end
finished = true
requests.finish(request_id)
loading.stop()
end
local function fail(msg)
vim.schedule(function()
finish()
loading.error(msg)
end)
end
local auth_jid
auth_jid = vim.fn.jobstart({ command, "auth", "status" }, {
env = env,
clear_env = true,
stdout_buffered = true,
stderr_buffered = true,
on_exit = function(_, exit_code)
if requests.is_cancelled(request_id) then
finish()
return
end
if exit_code ~= 0 then
fail("Claude Code is not authenticated; run 'claude login' and select your Claude plan")
return
end
local response_chunks = {}
local stderr_chunks = {}
local request_jid
request_jid = vim.fn.jobstart(M._build_claude_code_cmd(messages, config), {
env = env,
clear_env = true,
stdout_buffered = true,
stderr_buffered = true,
on_stdout = function(_, data)
if data then
for _, line in ipairs(data) do
table.insert(response_chunks, line)
end
end
end,
on_stderr = function(_, data)
if data then
for _, line in ipairs(data) do
if line ~= "" then
table.insert(stderr_chunks, line)
end
end
end
end,
on_exit = function(_, request_exit_code)
if requests.is_cancelled(request_id) then
finish()
return
end
local stderr_text = table.concat(stderr_chunks, "\n")
if request_exit_code ~= 0 then
local msg = "Claude Code request failed (exit " .. request_exit_code .. ")"
if stderr_text ~= "" then
msg = msg .. " — " .. stderr_text
end
fail(msg)
return
end
local raw = table.concat(response_chunks, "\n")
local ok, parsed = pcall(vim.fn.json_decode, raw)
local content = ok and extract_content_claude_code(parsed) or nil
if type(content) ~= "string" or content == "" then
fail("Claude Code returned an invalid response (update the CLI and try again)")
return
end
finish()
content = content:gsub("^```[%w]*\n", ""):gsub("\n```%s*$", "")
callback(content)
end,
})
if request_jid <= 0 then
finish()
loading.error("failed to start Claude Code CLI")
else
-- Jumpy never writes to Claude Code's stdin; close it so the CLI does
-- not wait ~3s for piped input ("no stdin data received in 3s").
pcall(vim.fn.chanclose, request_jid, "stdin")
requests.set_job(request_id, request_jid)
end
end,
})
if auth_jid <= 0 then
finish()
loading.error("failed to start Claude Code CLI")
else
requests.set_job(request_id, auth_jid)
end
end
local function make_request(messages, callback, opts)
opts = opts or {}
local config = get_config()
if is_claude_code() then
make_claude_code_request(messages, callback, opts)
return
end
if not config.api_key or config.api_key == "" then
local loading = require("jumpy.loading")
loading.error("no API key — set " .. (config.provider or "JUMPY") .. " env var or pass api_key in setup()")
return
end
local cmd, body_json
if is_anthropic() then
local system_text = nil
local api_messages = {}
for _, msg in ipairs(messages) do
if msg.role == "system" then
system_text = msg.content
else
table.insert(api_messages, msg)
end
end
local body = {
model = config.model,
max_tokens = 8192,
messages = api_messages,
}
if system_text then
body.system = system_text
end
body_json = vim.fn.json_encode(body)
cmd = build_curl_cmd_anthropic(body_json, config)
else
body_json = vim.fn.json_encode({
model = config.model,
messages = messages,
temperature = 0,
})
cmd = build_curl_cmd_openai(body_json, config)
end
local response_chunks = {}
local stderr_chunks = {}
local loading = require("jumpy.loading")
local requests = require("jumpy.requests")
local request_id = requests.begin({ label = opts.label, targets = opts.targets })
loading.start()
-- Every terminal path must release exactly one spinner refcount and drop
-- the request from the registry.
local finished = false
local function finish()
if finished then
return
end
finished = true
requests.finish(request_id)
loading.stop()
end
local function fail(msg)
vim.schedule(function()
finish()
loading.error(msg)
end)
end
local jid
jid = vim.fn.jobstart(cmd, {
stdout_buffered = true,
stderr_buffered = true,
on_stdout = function(_, data)
if data then
for _, line in ipairs(data) do
table.insert(response_chunks, line)
end
end
end,
on_stderr = function(_, data)
if data then
for _, line in ipairs(data) do
if line ~= "" then
table.insert(stderr_chunks, line)
end
end
end
end,
on_exit = function(_, exit_code)
if requests.is_cancelled(request_id) then
finish()
return
end
local stderr_text = table.concat(stderr_chunks, "\n")
if exit_code ~= 0 then
local msg = "request failed (curl exit " .. exit_code .. ")"
if stderr_text ~= "" then
msg = msg .. " — " .. stderr_text
end
fail(msg)
return
end
local raw = table.concat(response_chunks, "\n")
local ok, parsed = pcall(vim.fn.json_decode, raw)
if not ok then
vim.schedule(function()
local preview = vim.fn.strcharpart(vim.fn.substitute(raw, "\n", " ", "g"), 0, 120)
fail("response was not JSON: " .. preview)
end)
return
end
if parsed.error then
local err = parsed.error
local msg = type(err) == "table" and (err.message or vim.inspect(err)) or tostring(err)
fail("API error: " .. msg)
return
end
local content
if is_anthropic() then
content = extract_content_anthropic(parsed)
else
content = extract_content_openai(parsed)
end
if not content then
fail("empty response from LLM (check model / response shape)")
return
end
finish()
content = content:gsub("^```[%w]*\n", ""):gsub("\n```%s*$", "")
callback(content)
end,
})
if jid <= 0 then
finish()
loading.error("failed to start curl — is it installed?")
else
requests.set_job(request_id, jid)
end
end
function M.request(context, callback, opts)
local messages = build_messages(context)
make_request(messages, callback, opts)
end
function M.reprompt(context, callback, opts)
local messages = build_reprompt_messages(context)
make_request(messages, function(content)
local patch = require("jumpy.patch")
local new_lines, unmatched = patch.apply(context.proposed_lines or {}, content)
if unmatched > 0 then
vim.schedule(function()
vim.notify(string.format("jumpy: %d reprompt block(s) could not be matched", unmatched), vim.log.levels.WARN)
end)
end
callback(new_lines)
end, opts)
end
return M