From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: pareto optimal via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help Subject: Re: Need help interoperating between comint and CLI app's multi-line mode Date: Sun, 10 Dec 2023 13:13:09 +0100 (CET) Message-ID: <959676810.818772.1702210389083@ichabod.co-bxl> References: <2084887661.816349.1702207290891@ichabod.co-bxl> Reply-To: pareto optimal Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40062"; mail-complaints-to="usenet@ciao.gmane.io" To: Help Gnu Emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 11 06:30:04 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rCYrj-000ACe-N1 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 11 Dec 2023 06:30:03 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rCYql-0006Vg-2O; Mon, 11 Dec 2023 00:29:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rCIgR-0007os-0X for help-gnu-emacs@gnu.org; Sun, 10 Dec 2023 07:13:19 -0500 Original-Received: from wilbur.contactoffice.com ([212.3.242.68]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rCIgN-0004h0-Te for help-gnu-emacs@gnu.org; Sun, 10 Dec 2023 07:13:18 -0500 Original-Received: from ichabod.co-bxl (ichabod.co-bxl [10.2.0.36]) by wilbur.contactoffice.com (Postfix) with ESMTP id 0014C1E7B for ; Sun, 10 Dec 2023 13:13:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1702210391; s=20210208-e7xh; d=mailfence.com; i=pareto.optimal@mailfence.com; h=Date:From:To:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type; l=10755; bh=PDhuhDQTDlqt997fU/q4TCYIxRexBgzv3TgTBD5kJgM=; b=q7QksLksWH2pqY+Q6S3gZH/LYBI39mycVNUBpk94lYg3Yhtw3JBsRs3lDk692qkC SMn3NAVgVEIBjcE7yDrWhAtYBD4vn+7QE5UXGJT0L1Jk09uiRTeJ1KB2ehhpE3ArVlr /yHRB+VvYqQrsYjwbo1rn6mX9eSBwhA1MC7WRMPtbe0EAu6l2NFvyzp/miRGqr/xpos YMaLBkfe2Z4xsTq/74FSFWeD2DzHQNbtFsYG4AjcRC0oofkj6gv9aYEMflD5T+pSTJg TCoxe3C7VXTgdGguNGbBBoBu9ap90u7QsJ1TQttwR3GanqmXXURmhU/Ei/kHT4boYNg ueGpwC4GBA== In-Reply-To: <2084887661.816349.1702207290891@ichabod.co-bxl> X-Mailer: ContactOffice Mail X-ContactOffice-Account: com:311504211 Received-SPF: pass client-ip=212.3.242.68; envelope-from=pareto.optimal@mailfence.com; helo=wilbur.contactoffice.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 11 Dec 2023 00:29:01 -0500 Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:145635 Archived-At: I feel like this is definitely not the best way, but it works and may help = others understand my problem: ``` (defvar multi-line-mode nil "Flag to track if we're in multi-line mode.") (defvar multi-line-input-list nil "List to store multi-line inputs.") (defun send-multi-line-command () =C2=A0 (interactive) =C2=A0 (if (not multi-line-mode) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (progn =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq multi-line-input-list '()) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq multi-line-mode t) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (message "Multi-line mode: ON")) =C2=A0=C2=A0=C2=A0 (let ((input (string-join multi-line-input-list "\n"))) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (process-send-string "*memgpt*" (concat inpu= t "\n")) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; Here we send the equivalent of 'Alt+Enter= ' =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (process-send-string "*memgpt*" "\e\r") =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq multi-line-mode nil) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq multi-line-input-list nil) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (message "Multi-line mode: OFF")))) (defun capture-multi-line-input () =C2=A0 (interactive) =C2=A0 (when multi-line-mode =C2=A0=C2=A0=C2=A0 (end-of-buffer) =C2=A0=C2=A0=C2=A0 (let ((input (read-string "Multi-line input: "))) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (add-to-list 'multi-line-input-list input t)= ))) (global-set-key (kbd "C-c C-m") 'capture-multi-line-input) (global-set-key (kbd "M-RET") 'send-multi-line-command) ``` On Dec 10, 2023 at 5:21 AM, pareto optimal w= rote: Hello all, been stuck on this one for quite some hours :) I'm trying to make an inferior comint based mode for memgpt. In a terminal outside of emacs, when I use `memgpt run`, it has a special m= ulti-line mode you can enter with `//`. It looks like: ``` > Enter your message: // > Enter your message:=C2=A0 (Finish with 'Alt+Enter' or 'Esc then Enter') > =C2=A0 - say hi =C2=A0 - count to 10 =C2=A0 - say sayonara (I press Alt+enter) =F0=9F=92=AD User wants to say hi, count to 10, and say sayonara. Handling = the commands one by one. =F0=9F=A4=96 Hello! How are you doing today? I'm all ears. Oh, and one, two= , three... ``` The problem is in emacs if you just do something like this to run it with c= omint: ``` (progn =C2=A0 (make-comint "memgpt" "memgpt" nil "run" "--strip-ui") =C2=A0 (pop-to-buffer "*memgpt*")) ``` Then you go into multi-line mode, you'll get an error about `M-RET` not bei= ng bound. That's surmountable since I can bind `M-RET` to something. ``` (process-send-string "*memgpt*" "\e\r") ;; send escape codes for ESC enter = to underlying process ``` The problem is that process-send-string sends at the beginning of the line. I can prove that. Given comint buffer below and `|` denoting the cursor: ``` > Enter your message:=C2=A0 (Finish with 'Alt+Enter' or 'Esc then Enter') =C2=A0 - say hi =C2=A0 - count to 10 =C2=A0 - say sayonara| ``` Send: ``` (process-send-string "*memgpt*" "****") ``` and you get: ``` > Enter your message:=C2=A0 (Finish with 'Alt+Enter' or 'Esc then Enter') ****=C2=A0 - say hi =C2=A0 - count to 10 =C2=A0 - say sayonara ``` So when I send `(process-send-string "*memgpt*" "\e\r")` I predictably get = this result: ``` Empty input received. Try again! > Enter your message:=C2=A0 (Finish with 'Alt+Enter' or 'Esc then Enter') =C2=A0 - say hi =C2=A0 - count to 10 =C2=A0 - say sayonara ``` Well a little confusing since it puts the response above your input string. Then I went to the docs and found `comint-accumulate`. This is a not too ho= rrible workaround and does work, but the output is a little noisy and stran= ge if you don't know exactly whats happening: ``` > Enter your message:=C2=A0 (Finish with 'Alt+Enter' or 'Esc then Enter') =C2=A0 - say hi =C2=A0 - count to 10 =C2=A0 - say sayonara > =C2=A0=C2=A0=C2=A0 - say hi =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 - count to 10 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 - say sayonara =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 > Enter your message:=C2=A0 (Fin= ish with 'Alt+Enter' or 'Esc then Enter') > =C2=A0=C2=A0=C2=A0 - say hi =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 - count to 10 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 - say sayonara =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =F0=9F=92=AD INSERT silly wrong LLM output ``` Any guidance or advice would really help so I can have a comint memgpt that= isn't much more terrible to use than the normal version. Thanks!