|
3 | 3 |
|
4 | 4 | " Utilities for chat |
5 | 5 |
|
| 6 | +function! s:ResetChatContents() abort |
| 7 | + let chat_buf = bufnr('AugmentChatHistory') |
| 8 | + if chat_buf == -1 |
| 9 | + call augment#log#Error('Chat reset failed: Could not find chat history buffer') |
| 10 | + return |
| 11 | + endif |
| 12 | + |
| 13 | + call setbufvar(chat_buf, '&modifiable', v:true) |
| 14 | + call deletebufline(chat_buf, 1, '$') |
| 15 | + call augment#chat#AppendText('# Augment Chat History' |
| 16 | + \ . "\n\n" |
| 17 | + \ . '`:Augment chat` Send a chat message in the current conversation' |
| 18 | + \ . "\n" |
| 19 | + \ . '`:Augment chat-new` Start a new conversation' |
| 20 | + \ . "\n" |
| 21 | + \ . '`:Augment chat-toggle` Toggle the chat panel visibility' |
| 22 | + \ . "\n\n") |
| 23 | +endfunction |
| 24 | + |
| 25 | +function! augment#chat#Toggle() abort |
| 26 | + let chat_id = bufwinid('AugmentChatHistory') |
| 27 | + if chat_id == -1 |
| 28 | + call augment#chat#OpenChatPanel() |
| 29 | + else |
| 30 | + " Don't close if it's the last window |
| 31 | + if winnr('$') > 1 |
| 32 | + call win_execute(chat_id, 'close') |
| 33 | + endif |
| 34 | + endif |
| 35 | +endfunction |
| 36 | + |
| 37 | +function! augment#chat#OpenChatPanel() abort |
| 38 | + let current_win = win_getid() |
| 39 | + |
| 40 | + " Check if the panel already exists and has been setup |
| 41 | + if bufexists('AugmentChatHistory') && !getbufvar('AugmentChatHistory', '&modifiable') |
| 42 | + if bufwinid('AugmentChatHistory') == -1 |
| 43 | + botright 80vnew AugmentChatHistory |
| 44 | + endif |
| 45 | + call win_gotoid(current_win) |
| 46 | + return |
| 47 | + endif |
| 48 | + |
| 49 | + " Open a buffer for the chat history with a width of 80 characters |
| 50 | + botright 80vnew AugmentChatHistory |
| 51 | + setlocal buftype=nofile " Buffer will never be written to a file |
| 52 | + setlocal nomodifiable " Prevent any modifications |
| 53 | + setlocal noswapfile " Don't create a swapfile |
| 54 | + setlocal winfixbuf " Keep buffer in window when splitting |
| 55 | + setlocal bufhidden=hide " When buffer is abandoned, hide it |
| 56 | + setlocal nobuflisted " Hide from :ls |
| 57 | + setlocal wrap " Wrap long lines |
| 58 | + setlocal linebreak " Wrap at word boundaries |
| 59 | + setlocal filetype=markdown " Use markdown syntax highlighting |
| 60 | + setlocal nonumber " Hide line numbers |
| 61 | + setlocal norelativenumber " Hide relative line numbers |
| 62 | + setlocal signcolumn=no " Hide sign column |
| 63 | + setlocal nocursorline " Disable cursor line highlighting |
| 64 | + setlocal nospell " Disable spell checking |
| 65 | + setlocal nofoldenable " Disable folding |
| 66 | + setlocal textwidth=0 " Disable text width limit |
| 67 | + setlocal scrolloff=0 " Disable scrolloff |
| 68 | + |
| 69 | + " Add the chat header to the buffer |
| 70 | + call s:ResetChatContents() |
| 71 | + |
| 72 | + " TODO(AU-6480): create another buffer for the chat input |
| 73 | + " new AugmentChatInput |
| 74 | + |
| 75 | + call win_gotoid(current_win) |
| 76 | +endfunction |
| 77 | + |
| 78 | +function! augment#chat#Reset() abort |
| 79 | + call s:ResetChatContents() |
| 80 | + call s:ResetHistory() |
| 81 | +endfunction |
| 82 | + |
| 83 | +function! s:ResetHistory() abort |
| 84 | + let g:_augment_chat_history = [] |
| 85 | +endfunction |
| 86 | + |
| 87 | +function! augment#chat#AppendText(text) abort |
| 88 | + let chat_buf = bufnr('AugmentChatHistory') |
| 89 | + if chat_buf == -1 |
| 90 | + call augment#log#Error('Chat append failed: Could not find chat history buffer') |
| 91 | + return |
| 92 | + endif |
| 93 | + |
| 94 | + let lines = split(a:text, "\n", v:true) |
| 95 | + let last_line = getbufline(chat_buf, '$')[0] |
| 96 | + |
| 97 | + call setbufvar(chat_buf, '&modifiable', v:true) |
| 98 | + call setbufline(chat_buf, '$', last_line . lines[0]) |
| 99 | + call appendbufline(chat_buf, '$', lines[1:]) |
| 100 | + call setbufvar(chat_buf, '&modifiable', v:false) |
| 101 | +endfunction |
| 102 | + |
| 103 | +function! augment#chat#AppendMessage(message) abort |
| 104 | + " If not the first message, scroll to the bottom |
| 105 | + let chat_id = bufwinid('AugmentChatHistory') |
| 106 | + if !empty(augment#chat#GetHistory()) && chat_id != -1 |
| 107 | + let command = "call winrestview({'lnum': line('$'), 'topline': line('$')})" |
| 108 | + call win_execute(chat_id, command) |
| 109 | + endif |
| 110 | + |
| 111 | + let message_text = '================================================================================' |
| 112 | + \ . "\n\n" |
| 113 | + \ . "\t*You*" |
| 114 | + \ . "\n\n" |
| 115 | + \ . a:message |
| 116 | + \ . "\n\n" |
| 117 | + \ . '--------------------------------------------------------------------------------' |
| 118 | + \ . "\n\n" |
| 119 | + \ . "\t*Augment*" |
| 120 | + \ . "\n\n" |
| 121 | + call augment#chat#AppendText(message_text) |
| 122 | +endfunction |
| 123 | + |
| 124 | +function! augment#chat#AppendHistory(request_message, response_text, request_id) abort |
| 125 | + if !exists('g:_augment_chat_history') |
| 126 | + let g:_augment_chat_history = [] |
| 127 | + endif |
| 128 | + call add(g:_augment_chat_history, { |
| 129 | + \ 'request_message': a:request_message, |
| 130 | + \ 'response_text': a:response_text, |
| 131 | + \ 'request_id': a:request_id, |
| 132 | + \ }) |
| 133 | +endfunction |
| 134 | + |
| 135 | +function! augment#chat#GetHistory() abort |
| 136 | + if exists('g:_augment_chat_history') |
| 137 | + return g:_augment_chat_history |
| 138 | + endif |
| 139 | + return [] |
| 140 | +endfunction |
| 141 | + |
| 142 | +function! augment#chat#SaveUri() abort |
| 143 | + if bufname('%') !=# 'AugmentChatHistory' |
| 144 | + let g:_augment_current_uri = 'file://' . expand('%:p') |
| 145 | + endif |
| 146 | +endfunction |
| 147 | + |
| 148 | +function! augment#chat#GetUri() abort |
| 149 | + if exists('g:_augment_current_uri') |
| 150 | + return g:_augment_current_uri |
| 151 | + endif |
| 152 | + return 'file://' . expand('%:p') |
| 153 | +endfunction |
| 154 | + |
6 | 155 | function! s:GetBufSelection(line_start, col_start, line_end, col_end) abort |
7 | 156 | if a:line_start == a:line_end |
8 | 157 | return getline(a:line_start)[a:col_start - 1:a:col_end - 1] |
@@ -63,15 +212,3 @@ function! augment#chat#GetSelectedText() abort |
63 | 212 | let [line_end, col_end] = getpos("'>")[1:2] |
64 | 213 | return s:GetBufSelection(line_start, col_start, line_end, col_end) |
65 | 214 | endfunction |
66 | | - |
67 | | -function! augment#chat#CreateBuffer(bufname) abort |
68 | | - botright vnew |
69 | | - setlocal buftype=nofile |
70 | | - setlocal bufhidden=hide |
71 | | - setlocal noswapfile |
72 | | - setlocal wrap |
73 | | - setlocal linebreak |
74 | | - execute 'file ' . a:bufname |
75 | | - setlocal readonly |
76 | | - setlocal filetype=markdown |
77 | | -endfunction |
|
0 commit comments