summaryrefslogtreecommitdiff
path: root/lua/99/prompt.lua
blob: fbb8265db62097478581bd6940b9e596fc2b0563 (plain)
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
local BaseProvider = require("99.providers")
local Logger = require("99.logger.logger")
local utils = require("99.utils")
local random_file = utils.random_file
local copy = utils.copy
local get_id = require("99.id")
local Range = require("99.geo").Range
local Time = require("99.time")

--- you can only set those marks after the visual selection is removed
local function set_selection_marks()
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
    "x",
    false
  )
end

local filetype_map = {
  typescriptreact = "typescript",
}

-- luacheck: ignore
--- @alias _99.Prompt.Data _99.Prompt.Data.Search | _99.Prompt.Data.Tutorial | _99.Prompt.Data.Visual | _99.Prompt.Data.Vibe
--- @alias _99.Prompt.Operation "visual" | "tutorial" | "search" | "vibe"
--- @alias _99.Prompt.QFixOperation "search" | "vibe"
--- @alias _99.Prompt.EndingState "failed" | "success" | "cancelled"
--- @alias _99.Prompt.State "ready" | "requesting" | _99.Prompt.EndingState
--- @alias _99.Prompt.Cleanup fun(): nil

--- @class _99.Prompt.Data.Search
--- @field type "search"
--- @field qfix_items _99.Search.Result[]
--- @field response string

--- @class _99.Prompt.Data.Vibe
--- @field type "vibe"
--- @field response string
--- @field qfix_items _99.Search.Result[]

--- @class _99.Prompt.Data.Visual
--- @field type "visual"
--- @field buffer number
--- @field file_type string
--- @field range _99.Range

--- @class _99.Prompt.Data.Tutorial
--- @field type "tutorial"
--- @field buffer number
--- @field window number
--- @field xid number TODO: we should probably get rid of this.  The request pattern is not quite correct
--- @field tutorial string[]

--- @class _99.Prompt
--- @field md_file_names string[]
--- @field model string
--- @field user_prompt string
--- @field operation _99.Prompt.Operation
--- @field state _99.Prompt.State
--- @field full_path string
--- @field started_at number
--- @field data _99.Prompt.Data
--- @field agent_context string[]
--- @field tmp_file string
--- @field marks table<string, _99.Mark>
--- @field logger _99.Logger
--- @field xid number
--- @field clean_ups (fun(): nil)[]
--- @field _99 _99.State
---@diagnostic disable-next-line: undefined-doc-name
--- @field _proc vim.SystemObj?
local Prompt = {}
Prompt.__index = Prompt

--- @type _99.Prompt[]
Prompt.__previous_contexts = {}

--- @type table<number, _99.Prompt>
Prompt.__context_by_id = {}

--- @param context  _99.Prompt
--- @param _99 _99.State
local function set_defaults(context, _99)
  local xid = get_id()
  local full_path = vim.api.nvim_buf_get_name(0)

  context.state = "ready"
  context._99 = _99
  context.user_prompt = ""
  context.clean_ups = {}
  context.md_file_names = copy(_99.md_files)
  context.model = _99.model
  context.agent_context = {}
  context.tmp_file = random_file(_99:tmp_dir())
  context.logger = Logger:set_id(xid)
  context.xid = xid
  context.full_path = full_path
  context.marks = {}
  context.started_at = Time.now()
end

--- TODO: Work item for "TODO implementation"
function Prompt.todo(_99)
  _ = _99
  assert(false, "not implemented")
end

--- @param _99 _99.State
--- @return _99.Prompt
function Prompt.vibe(_99)
  _99:refresh_rules()

  --- @type _99.Prompt
  local context = setmetatable({}, Prompt)
  set_defaults(context, _99)
  context.operation = "vibe"
  context.data = {
    type = "vibe",
    response = "",
    qfix_items = {},
  }
  context.logger:debug("99 Request", "method", "vibe")

  return context
end

--- @param _99 _99.State
--- @return _99.Prompt
function Prompt.visual(_99)
  _99:refresh_rules()

  set_selection_marks()
  local range = Range.from_visual_selection()

  local file_type = vim.bo[0].ft
  local buffer = vim.api.nvim_get_current_buf()
  file_type = filetype_map[file_type] or file_type

  local mds = {}
  for _, md in ipairs(_99.md_files) do
    table.insert(mds, md)
  end

  --- @type _99.Prompt
  local context = setmetatable({}, Prompt)
  set_defaults(context, _99)
  context.operation = "visual"
  context.data = {
    type = "visual",
    buffer = buffer,
    file_type = file_type,
    range = range,
  }
  context.logger:debug("99 Request", "method", "visual")

  return context
end

--- @return string
function Prompt:summary()
  return string.format("%s: %s", self.operation, self.user_prompt)
end

--- @param _99 _99.State
--- @return _99.Prompt
function Prompt.tutorial(_99)
  _99:refresh_rules()

  --- @type _99.Prompt
  local context = setmetatable({}, Prompt)
  set_defaults(context, _99)
  context.operation = "tutorial"
  context.data = {
    type = "tutorial",
    xid = context.xid, -- TODO: i want to get rid of this when i implement rehydration of the data.
    buffer = 0,
    window = 0,
    tutorial = {},
  }
  context.logger:debug("99 Request", "method", "tutorial")

  return context
end

--- @param _99 _99.State
--- @return _99.Prompt
function Prompt.search(_99)
  _99:refresh_rules()

  --- @type _99.Prompt
  local context = setmetatable({}, Prompt)
  set_defaults(context, _99)
  context.operation = "search"
  context.data = {
    type = "search",
    qfix_items = {},
    response = "",
  }
  context.logger:debug("99 Request", "method", "search")

  return context
end

--- @param obs _99.Providers.Observer | nil
function Prompt:_observer(obs)
  return {
    on_start = function()
      self.state = "requesting"
      self._99:track_prompt_request(self)

      if obs then
        obs.on_start()
      end
    end,
    on_complete = function(status, res)
      self.state = status
      if obs then
        obs.on_complete(status, res)
      end
    end,
    on_stderr = function(line)
      if obs then
        obs.on_stderr(line)
      end
    end,
    on_stdout = function(line)
      if obs then
        obs.on_stdout(line)
      end
    end,
  }
end

local allowed_context_types = {
  "visual",
  "search",
  "tutorial",
  "vibe",
}
--- @return boolean
function Prompt:valid()
  local t = self.data.type
  for _, allowed in ipairs(allowed_context_types) do
    if t == allowed then
      return true
    end
  end
  return false
end

--- @param observer _99.Providers.Observer?
function Prompt:start_request(observer)
  local l = self.logger
  l:assert(
    self.state == "ready",
    'state is not "ready" when attempting to start a request'
  )

  local ok = self:finalize()
  l:assert(ok, "context failed to finalize")

  --- TODO: create a prompt context class that can actually organize.
  --- do not do this during the request context refactoring, but next
  local prompt = table.concat(self.agent_context, "\n")
  local obs = self:_observer(observer)
  local provider = self._99.provider_override or BaseProvider.OpenCodeProvider

  self:save_prompt(prompt)
  l:debug("start", "prompt", prompt)

  provider:make_request(prompt, self, obs)
end

function Prompt:is_cancelled()
  return self.state == "cancelled"
end

---@diagnostic disable-next-line: undefined-doc-name
--- @param proc vim.SystemObj?
function Prompt:_set_process(proc)
  self._proc = proc
end

function Prompt:cancel()
  if self:is_cancelled() then
    return
  end

  self.logger:debug("cancel")
  self.state = "cancelled"
  local proc = self._proc
  ---@diagnostic disable-next-line: undefined-field
  if proc and proc.pid then
    self._proc = nil
    pcall(function()
      local sigterm = (vim.uv and vim.uv.constants and vim.uv.constants.SIGTERM)
        or 15
      ---@diagnostic disable-next-line: undefined-field
      proc:kill(sigterm)
    end)
  end
end

--- @return _99.Prompt.Data.Visual
function Prompt:visual_data()
  assert(
    self.data.type == "visual",
    "you cannot get visual data if its not type visual"
  )
  return self.data --[[@as _99.Prompt.Data.Visual]]
end

--- @return _99.Prompt.Data.Tutorial
function Prompt:tutorial_data()
  assert(
    self.data.type == "tutorial",
    "you cannot get tutorial data if its not type tutorial"
  )
  return self.data --[[@as _99.Prompt.Data.Tutorial]]
end

--- @return _99.Prompt.Data.Search
function Prompt:search_data()
  assert(
    self.data.type == "search",
    "you cannot get search data if its not type search"
  )
  return self.data --[[@as _99.Prompt.Data.Search]]
end

--- @return _99.Search.Result[]
function Prompt:qfix_data()
  assert(
    self.data.type == "search" or self.data.type == "vibe",
    "data type is not search or vibe: " .. self.data.type
  )
  return self.data.qfix_items
end

function Prompt:stop()
  self:cancel()
  for _, cb in ipairs(self.clean_ups) do
    cb()
  end
end

--- @param clean_up fun(): nil
function Prompt:add_clean_up(clean_up)
  table.insert(self.clean_ups, clean_up)
end

--- @param md_file_name string
--- @return self
function Prompt:add_md_file_name(md_file_name)
  table.insert(self.md_file_names, md_file_name)
  return self
end

--- @param content string
--- @return self
function Prompt:add_prompt_content(content)
  table.insert(self.agent_context, content)
  return self
end

--- @param refs _99.Reference[]
function Prompt:add_references(refs)
  for _, ref in ipairs(refs) do
    self.logger:debug("adding reference to context")
    table.insert(self.agent_context, ref.content)
  end
end

function Prompt:_read_md_files()
  local cwd = vim.uv.cwd()
  local dir = vim.fn.fnamemodify(self.full_path, ":h")

  while dir:find(cwd, 1, true) == 1 do
    for _, md_file_name in ipairs(self.md_file_names) do
      local md_path = dir .. "/" .. md_file_name
      local file = io.open(md_path, "r")
      if file then
        local content = file:read("*a")
        file:close()
        self.logger:info(
          "Context#adding md file to the context",
          "md_path",
          md_path
        )
        table.insert(self.agent_context, content)
      end
    end

    if dir == cwd then
      break
    end

    dir = vim.fn.fnamemodify(dir, ":h")
  end
end

--- @return string[]
function Prompt:content()
  return self.agent_context
end

--- @return boolean
function Prompt:_ready_request_files()
  local response_file = self.tmp_file
  local prompt_file = self.tmp_file .. "-prompt"

  local dir = vim.fs.dirname(prompt_file)

  if dir and not vim.uv.fs_stat(dir) then
    vim.fn.mkdir(dir, "p")
  end

  local files = { prompt_file, response_file }
  for _, f in ipairs(files) do
    local file = io.open(f, "w")
    if file then
      file:write("")
      file:close()
    else
      self.logger:error("unable to create prompt file")
      return false
    end
  end
  return true
end

--- @param prompt string
function Prompt:save_prompt(prompt)
  local prompt_file = self.tmp_file .. "-prompt"
  local file = io.open(prompt_file, "w")
  if file then
    file:write(prompt)
    file:close()
    self.logger:debug("saved prompt to file", "path", prompt_file)
  else
    self.logger:error("failed to save prompt", "path", prompt_file)
  end
end

--- @return boolean, self
function Prompt:finalize()
  if self:_ready_request_files() == false then
    return false, self
  end
  self:_read_md_files()

  local ok, visual_data = pcall(self.visual_data, self)
  if ok then
    local f_loc =
      self._99.prompts.get_file_location(self.full_path, visual_data.range)
    table.insert(self.agent_context, f_loc)
    table.insert(
      self.agent_context,
      self._99.prompts.get_range_text(visual_data.range)
    )
  end
  table.insert(
    self.agent_context,
    self._99.prompts.tmp_file_location(self.tmp_file)
  )

  if
    self.operation == "visual"
    or self.operation == "tutorial"
    or self.operation == "search"
  then
    table.insert(self.agent_context, self._99.prompts.only_tmp_file_change())
  end

  return true, self
end

function Prompt:clear_marks()
  for _, mark in pairs(self.marks) do
    mark:delete()
  end
end

return Prompt