Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ let g:llm_agent_log_level=0 " 0=off, 1=basic, 2=verbose
- **Allow Once**: Approve this single tool execution
- **Always Allow**: Remember approval for this tool in the current session
- **Deny**: Block this tool and remember the denial
- **g:llm_agent_summary_compaction_size**: Trigger summary regeneration after this many bytes of new conversation since last summary. Default: 51200 (50KB). This implements automatic conversation compaction.
- **g:llm_agent_summary_compaction_size**: Trigger summary regeneration after this many bytes of new conversation since last summary. Default: 76800 (75KB). This implements automatic conversation compaction.
- **g:llm_agent_recent_history_size**: Keep this many bytes of recent conversation uncompressed. Older content gets compressed into summary. Default: 20480 (20KB). Controls the sliding window size.

**Advanced Options:**
Expand Down Expand Up @@ -513,7 +513,7 @@ The `cutoff_byte` metadata tracks which portion of history has been compressed,
**Configure compaction behavior:**
```vim
" Trigger summary update after this many bytes of new conversation
let g:llm_agent_summary_compaction_size = 51200 " Default: 50KB
let g:llm_agent_summary_compaction_size = 76800 " Default: 75KB

" Keep this much recent history uncompressed
let g:llm_agent_recent_history_size = 20480 " Default: 20KB
Expand Down
36 changes: 15 additions & 21 deletions autoload/chatgpt.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
" ChatGPT Autoload Core Functions
" This file contains the main API functions for the ChatGPT plugin

" Add the plugin's python3/ directory to Python's sys.path once per session.
" Call this before any heredoc that imports from chatgpt.*
function! chatgpt#ensure_python_path() abort
python3 << PYEOF
import sys, os, vim
_plugin_dir = vim.eval('expand("<sfile>:p:h:h")')
_python_path = os.path.join(_plugin_dir, 'python3')
if _python_path not in sys.path:
sys.path.insert(0, _python_path)
PYEOF
endfunction

" Main ChatGPT function - delegates to Python
function! chatgpt#chat(prompt) abort
" Ensure suppress_display is off for normal chat operations
Expand All @@ -21,17 +33,7 @@ function! chatgpt#chat(prompt) abort
let g:chatgpt_history_size_before = filereadable(history_file) ? getfsize(history_file) : 0

python3 << EOF
import sys
import vim
import os

# Add python3/chatgpt to Python path
plugin_dir = vim.eval('expand("<sfile>:p:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

# Import and call main chat function
from chatgpt.core import chat_gpt
chat_gpt(vim.eval('a:prompt'))
EOF
Expand Down Expand Up @@ -99,7 +101,7 @@ function! chatgpt#display_response(response, finish_reason, chat_gpt_session_id)

call setbufline(chat_gpt_session_id, '$', clean_lines)

" Switch to chat window and scroll to bottom
" Switch to chat window and scroll to bottom, then restore the original window
let chat_winnr = bufwinnr(chat_gpt_session_id)
if chat_winnr != -1
let current_win = winnr()
Expand All @@ -108,23 +110,15 @@ function! chatgpt#display_response(response, finish_reason, chat_gpt_session_id)
call cursor('$', 1)
execute "normal! \<C-E>\<C-Y>"
redraw
execute current_win . 'wincmd w'
endif

" Save to history file if this is a persistent session
if chat_gpt_session_id ==# 'gpt-persistent-session' && response != ''
python3 << EOF
import vim
import sys
import os

plugin_dir = vim.eval('expand("<sfile>:p:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

from chatgpt.utils import save_to_history
response = vim.eval('a:response')
save_to_history(response)
save_to_history(vim.eval('a:response'))
EOF
endif
endfunction
Expand Down
9 changes: 7 additions & 2 deletions autoload/chatgpt/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ function! chatgpt#config#setup() abort
let g:split_ratio = 3
endif

if !exists("g:chat_persona")
let g:chat_persona = 'default'
if !exists("g:llm_agent_persona") && !exists("g:chat_gpt_persona")
let g:llm_agent_persona = 'default'
elseif !exists("g:llm_agent_persona")
let g:llm_agent_persona = g:chat_gpt_persona
endif

" Enable tools/function calling (default: enabled for supported providers)
Expand Down Expand Up @@ -182,4 +184,7 @@ function! chatgpt#config#setup() abort
elseif exists('g:chat_gpt_custom_persona')
call extend(g:gpt_personas, g:chat_gpt_custom_persona)
endif

" Set up Python path once at plugin load so all heredocs can import chatgpt.*
call chatgpt#ensure_python_path()
endfunction
53 changes: 19 additions & 34 deletions autoload/chatgpt/context.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ function! chatgpt#context#check_and_generate() abort
let project_dir = getcwd()
let home = expand('~')

" Skip if we're in home directory, parent of home, or root
if project_dir ==# home || project_dir ==# '/' || len(project_dir) <= len(home)
" Skip if we're in home directory, a parent of home, or root
if project_dir ==# home || project_dir ==# '/' ||
\ stridx(project_dir . '/', home . '/') == 0
return
endif

Expand All @@ -39,13 +40,10 @@ function! chatgpt#context#check_and_generate() abort
echo "No project context found. Generating automatically..."
let should_generate = 1
else
" Check if file is older than 24 hours
let file_time = getftime(context_file)
let current_time = localtime()
let age_in_hours = (current_time - file_time) / 3600

if age_in_hours > 24
echo "Project context is " . float2nr(age_in_hours) . " hours old. Regenerating..."
" Check if new files have been added since last generation
let has_new_files = s:check_for_new_files(vim_dir)
if has_new_files
echo "New files detected in project. Regenerating context..."
let should_generate = 1
endif
endif
Expand All @@ -67,19 +65,7 @@ endfunction
function! chatgpt#context#generate_silent() abort
" Call Python context generation directly
python3 << EOF
import vim
import sys
import os

# Add python3/chatgpt to Python path
plugin_dir = vim.eval('expand("<sfile>:p:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

from chatgpt.context import generate_project_context

# Generate context (will save to .vim-llm-agent/context.md automatically)
generate_project_context()
EOF
endfunction
Expand All @@ -99,24 +85,23 @@ function! chatgpt#context#generate() abort

echo "Generating project context... (this will use AI tools to explore your project)"

" Call Python context generation directly
python3 << EOF
import vim
import sys
import os

# Add python3/chatgpt to Python path
plugin_dir = vim.eval('expand("<sfile>:p:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

from chatgpt.context import generate_project_context

# Generate context (will save to .vim-llm-agent/context.md automatically)
generate_project_context()
EOF

echo "\nProject context generated at " . dir_name . "/context.md"
echo "You can edit this file to customize the project context."
endfunction

" Check if new files have been added since last context generation
function! s:check_for_new_files(vim_dir) abort
let result = 0
python3 << EOF
import vim
from chatgpt.context import has_new_files
has_new = has_new_files(vim.eval('a:vim_dir'))
vim.command(f'let result = {1 if has_new else 0}')
EOF
return result
endfunction
4 changes: 2 additions & 2 deletions autoload/chatgpt/persona.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ function! chatgpt#persona#set(persona) abort
let personas = keys(g:gpt_personas)
if index(personas, a:persona) != -1
echo 'Persona set to: ' . a:persona
let g:chat_persona = a:persona
let g:llm_agent_persona = a:persona
else
let g:chat_persona = 'default'
let g:llm_agent_persona = 'default'
echo 'Persona set to default, not found ' . a:persona
end
endfunction
30 changes: 1 addition & 29 deletions autoload/chatgpt/summary.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
function! s:get_summary_cutoff(project_dir) abort
python3 << EOF
import vim
import sys
import os

plugin_dir = vim.eval('expand("<sfile>:p:h:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

from chatgpt.summary import get_summary_cutoff
project_dir = vim.eval('a:project_dir')
cutoff = get_summary_cutoff(project_dir)
cutoff = get_summary_cutoff(vim.eval('a:project_dir'))
vim.command(f'let l:cutoff_result = {cutoff}')
EOF
return l:cutoff_result
Expand Down Expand Up @@ -108,16 +99,6 @@ function! chatgpt#summary#generate(...) abort
" This function is complex and calls Python code
" For now, delegate to the main chat function with appropriate prompt
python3 << EOF
import vim
import sys
import os

plugin_dir = vim.eval('expand("<sfile>:p:h:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

# Import the summary generation logic
from chatgpt.summary import generate_conversation_summary
generate_conversation_summary()
EOF
Expand Down Expand Up @@ -186,15 +167,6 @@ function! s:check_and_resume_plan() abort
elseif choice == 3
" Clear the plan using Python function
python3 << EOF
import vim
import sys
import os

plugin_dir = vim.eval('expand("<sfile>:p:h:h:h")')
python_path = os.path.join(plugin_dir, 'python3')
if python_path not in sys.path:
sys.path.insert(0, python_path)

from chatgpt.utils import clear_plan
clear_plan()
EOF
Expand Down
Loading