karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.04k stars 113 forks source link

Restoring chat state is broken with multi-line directives #180

Closed karthink closed 3 months ago

karthink commented 5 months ago

If the system message string used in a chat has newlines, it is not stored correctly either as a file-local variable or an Org property. Trying to resume this chat causes an error.

One possible fix is to replace newlines with an unused ASCII char when storing it in the buffer. Example: one of the ASCII chars TAB, C-l, C-g or the null character.

karthink commented 5 months ago

If anyone has ideas for how to store multi-line strings as org properties or file-local variables I'd be happy to get your input.

Please note that we don't want to use an auxiliary file to store this data -- the Org or markdown file should be fully portable and self-contained. gptel will try its best to stay backwards compatible.

martenlienen commented 5 months ago

What about replacing the newline character with "\n" and then revert that change on load?

karthink commented 5 months ago

@martenlienen That's the idea for now, but there are a few concerns to be addressed.

  1. We would prefer to break it up into multiple lines to avoid long lines issues with Emacs. This is not theoretical, I have system prompts that are 2000+ characters, sufficient to start causing display engine slowdowns on Emacs 27 and 28.
  2. Placing them on multiple lines is possible, but requires using the variable org-property-separators, which was introduced in Org 9.6-something. This fix will not work for Emacs 27 and some Emacs 28 users.
  3. Local variables cannot be more than 3000 chars away from the end of the file. This will cause storing the system message as is in non-Org files to fail silently. (This problem does not affect Org)

EDIT: See also this bug report I filed with Emacs (and subsequently retracted) about newline handling for local variable storage.

karthink commented 5 months ago

@martenlienen Should be "fixed" in that your Org files won't break any more, but the "\n" -> "\n" replacement solution is a hack with the above problems.

I'll keep this issue open until I can find better solutions for both saving chats in both Org and other modes.

karthink commented 3 months ago

Closing since the makeshift solution is working well, but with a comment that a better solution would be welcome.