madox2 / vim-ai

AI-powered code assistant for Vim. OpenAI and ChatGPT plugin for Vim and Neovim.
MIT License
727 stars 66 forks source link

ValueError: Invalid header value on Windows (PowerShell) #65

Closed CRTejaswi closed 1 day ago

CRTejaswi commented 11 months ago

I'm on a Windows x64 machine (Version 22H2, OS build 19045.3693). I'm able to make it work on WSL, but not on PowerShell. API key stored both into ~/.config/openai.token & env variable OPENAI_API_KEY for both these environments. (The API key is correct, and starts with sk as can be seen below. I believe the issue lies in the string that's sent as a request) Please tell me how to resolve this.

ERROR LOG

Error detected while processing function vim_ai#AIRun:
line   19:
Traceback (most recent call last):
  File "", line 1, in <module>
  File "C:\Users\me\vimfiles\plugged\vim-ai/py/complete.py", line 57, in <module>
    handle_completion_error(error)
  File "C:\Users\me\vimfiles\plugged\vim-ai/py/utils.py", line 195, in handle_completion_error
    raise error
  File "C:\Users\me\vimfiles\plugged\vim-ai/py/complete.py", line 54, in <module>
    render_text_chunks(text_chunks)
  File "C:\Users\me\vimfiles\plugged\vim-ai/py/utils.py", line 68, in render_text_chunks
    for text in chunks:
  File "C:\Users\me\vimfiles\plugged\vim-ai/py/utils.py", line 157, in openai_request
    with urllib.request.urlopen(req, timeout=request_timeout) as response:
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 1391, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\urllib\request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\http\client.py", line 1323, in _send_request
    self.putheader(hdr, value)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\Lib\http\client.py", line 1260, in putheader
    raise ValueError('Invalid header value %r' % (values[i],))
ValueError: Invalid header value b'Bearer \xff\xfes\x00k\x00-\x00m\x006 ...'

SPECIFICATIONS

VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Nov  5 2023 23:51:53)
MS-Windows 64-bit console version
Included patches: 1-2092
Compiled by appveyor@APPVEYOR-VM
Huge version without GUI.  Features included (+) or not (-):
+acl                +ex_extra           +multi_lang         -tag_any_white
+arabic             +extra_search       +mzscheme/dyn       -tcl
+autocmd            -farsi              -netbeans_intg      +termguicolors
+autochdir          +file_in_path       +num64              +terminal
+autoservername     +find_in_path       +packages           -termresponse
-balloon_eval       +float              +path_extra         +textobjects
+balloon_eval_term  +folding            +perl/dyn           +textprop
-browse             -footer             +persistent_undo    -tgetent
++builtin_terms     +gettext/dyn        +popupwin           +timers
+byte_offset        -hangul_input       -postscript         +title
+channel            +iconv/dyn          +printer            -toolbar
+cindent            +insert_expand      +profile            +user_commands
+clientserver       +ipv6               +python/dyn         +vartabs
+clipboard          +job                +python3/dyn-stable +vertsplit
+cmdline_compl      +jumplist           +quickfix           +vim9script
+cmdline_hist       +keymap             +reltime            +viminfo
+cmdline_info       +lambda             +rightleft          +virtualedit
+comments           +langmap            +ruby/dyn           +visual
+conceal            +libcall            +scrollbind         +visualextra
+cryptv             +linebreak          +signs              +vreplace
+cscope             +lispindent         +smartindent        +vtp
+cursorbind         +listcmds           +sodium/dyn         +wildignore
+cursorshape        +localmap           +sound              +wildmenu
+dialog_con         +lua/dyn            +spell              +windows
+diff               +menu               +startuptime        +writebackup
+digraphs           +mksession          +statusline         -xattr
-dnd                +modify_fname       -sun_workshop       -xfontset
-ebcdic             +mouse              +syntax             -xim
+emacs_tags         -mouseshape         +tag_binary         -xpm_w32
+eval               +multi_byte_ime/dyn -tag_old_static     -xterm_save
   system vimrc file: "$VIM\vimrc"
     user vimrc file: "$HOME\_vimrc"
 2nd user vimrc file: "$HOME\vimfiles\vimrc"
 3rd user vimrc file: "$VIM\_vimrc"
      user exrc file: "$HOME\_exrc"
  2nd user exrc file: "$VIM\_exrc"
       defaults file: "$VIMRUNTIME\defaults.vim"
Compilation: cl -c /W3 /GF /nologo -I. -Iproto -DHAVE_PATHDEF -DWIN32 -DHAVE_STDINT_H  -DFEAT_CSCOPE -DFEAT_TERMINAL -DFEAT_SOUND  -DFEAT_JOB_CHANNEL -DFEAT_IPV6 -DHAVE_INET_NTOP    -DHAVE_SODIUM -DDYNAMIC_SODIUM -DDYNAMIC_SODIUM_DLL=\"libsodium.dll\" /I "C:\projects\vim-win32-installer\dependencies\libsodium\include" -DWINVER=0x0601 -D_WIN32_WINNT=0x0601 /source-charset:utf-8 /MP /Ox /GL -DNDEBUG /Zl /MT /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE -DFEAT_MBYTE_IME -DDYNAMIC_IME -DDYNAMIC_ICONV -DDYNAMIC_GETTEXT -DFEAT_LUA -DDYNAMIC_LUA -DDYNAMIC_LUA_DLL=\"lua54.dll\" -DFEAT_PYTHON -DDYNAMIC_PYTHON -DDYNAMIC_PYTHON_DLL=\"python27.dll\" -DFEAT_PYTHON3 -DDYNAMIC_PYTHON3 -DDYNAMIC_PYTHON3_DLL=\"python3.dll\" -DDYNAMIC_PYTHON3_STABLE_ABI -DFEAT_MZSCHEME -I "C:\projects\vim-win32-installer\dependencies\racket\include" -DMZ_PRECISE_GC -DDYNAMIC_MZSCHEME -DDYNAMIC_MZSCH_DLL=\"libracket3m_dcgt6o.dll\" -DDYNAMIC_MZGC_DLL=\"libracket3m_dcgt6o.dll\" -DFEAT_PERL -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DDYNAMIC_PERL -DDYNAMIC_PERL_DLL=\"perl532.dll\" -DFEAT_RUBY -DDYNAMIC_RUBY -DDYNAMIC_RUBY_DLL=\"x64-ucrt-ruby320.dll\" -DRUBY_VERSION=32 -DFEAT_HUGE /Fd.\ObjCULYHRZAMD64/ /Zi
Linking: link /nologo /opt:ref /LTCG oldnames.lib kernel32.lib advapi32.lib shell32.lib gdi32.lib  comdlg32.lib ole32.lib netapi32.lib uuid.lib user32.lib  /machine:AMD64  libcmt.lib   /nodefaultlib:lua54.lib  /STACK:8388608  /nodefaultlib:python27.lib /nodefaultlib:python3.lib    winmm.lib Ws2_32.lib   /PDB:vim.pdb -debug
Konfekt commented 11 months ago

Speaking on behalf of ChatGPT, could

#  py/utils.py (line 159)
line = line_bytes.decode("utf-8-sig", errors="replace")

handle the BOM header better?

madox2 commented 1 day ago

Closing due to inactivity