From: Robin Krens Date: Wed, 1 Jan 2025 20:53:08 +0000 (+0100) Subject: init claude plugin X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=b82a9d86d54e5e8b1df52ade72d70a435aaff6bc;p=vim-claudy init claude plugin --- b82a9d86d54e5e8b1df52ade72d70a435aaff6bc diff --git a/claudy.vim b/claudy.vim new file mode 100644 index 0000000..2b8fe1e --- /dev/null +++ b/claudy.vim @@ -0,0 +1,78 @@ +if exists('g:loaded_claudy') + finish +endif +let g:loaded_claudy = 1 + +function! ClaudyRequest(json_data) + + let api_key = g:claude_api_key + + let json_string = json_encode(a:json_data) + let curl_cmd = 'curl -s -X POST "https://api.anthropic.com/v1/messages" ' . + \ '-H "x-api-key: ' . api_key . '" ' . + \ '-H "anthropic-version: 2023-06-01" ' . + \ '-H "content-type: application/json" ' . + \ '--data ' . shellescape(json_string) + + let response = system(curl_cmd) + let parsed_response = json_decode(response) + + if has_key(parsed_response, 'content') && len(parsed_response.content) > 0 + let message = parsed_response.content[0].text + "let stripped_msg = substitute(message, '^```\|```$', '', 'g') + let stripped_msg = message[4:-4] + return stripped_msg + + else + echo "Error: " . response + endif +endfunction + +function! ClaudyImplement(user_prompt) + + let system_prompt = "reply with an implementation of one function in C should be able to copied right into an editor, no explaining text, start always with ```c and end with ```" + + let json_data = { + \ 'model': 'claude-3-5-sonnet-20241022', + \ 'max_tokens': 1024, + \ 'system': system_prompt, + \ 'messages': [ + \ { + \ 'role': 'user', + \ 'content': a:user_prompt + \ } + \ ] + \ } + + let new_code = ClaudyRequest(json_data) + put =new_code +endfunction + +function! ClaudyRefactor(user_prompt) + + let l:start_line = line("'<") + let l:end_line = line("'>") + let selected_text = a:user_prompt . shellescape(join(getline(l:start_line, l:end_line))) + + let system_prompt = "reply with one C function (do not implement other functions) code should be able to copied right into an editor, no explaining text" + + " Prepare the JSON payload + let json_data = { + \ 'model': 'claude-3-5-sonnet-20241022', + \ 'max_tokens': 1024, + \ 'system': system_prompt, + \ 'messages': [ + \ { + \ 'role': 'user', + \ 'content': selected_text + \ } + \ ] + \ } + + let refactored_code = ClaudyRequest(json_data) + vnew + put =refactored_code +endfunction + +vnoremap c :call ClaudyRefactor() +vnoremap i :call ClaudyImplement()