Compact plain chat loops
ober
4b882249e765e703895c54062378c08416e13046
--- a/src/jcode/core/agent.ss +++ b/src/jcode/core/agent.ss @@ -1663,7 +1663,7 @@ Be concise. Prefer edit over write for modifying existing files. (agent-chat-loop provider messages tools 0)))))))) (def (agent-chat-loop provider messages tools round) - (let* ((msgs messages) + (let* ((msgs (refresh-system-prompt messages)) (response (chat-with-expert provider msgs tools))) (cond ((not (message-tool-calls response)) (message-content response)) @@ -1681,7 +1681,7 @@ Be concise. Prefer edit over write for modifying existing files. (agent-chat-loop provider new-messages tools (+ round 1))))))) (def (agent-chat-loop-stream provider messages tools round) - (let* ((msgs messages) + (let* ((msgs (refresh-system-prompt messages)) (raw-cb (current-stream-cb)) (cb (and raw-cb (make-tool-call-stream-filter raw-cb)))) (let-values (((content tool-calls usage) @@ -1732,9 +1732,10 @@ Be concise. Prefer edit over write for modifying existing files. ;; Task/sub-agent conversations are not rendered token-by-token, but they ;; still need streaming transport. A non-streaming local MLX read can block ;; in the kernel long enough to pin the TUI and make ESC ineffective. - (let-values (((content tool-calls usage) - (stream-chat-with-expert provider messages tools - (lambda (_tok) (void))))) + (let ((msgs (refresh-system-prompt messages))) + (let-values (((content tool-calls usage) + (stream-chat-with-expert provider msgs tools + (lambda (_tok) (void))))) (let* ((hermes-tcs (if (and (null? tool-calls) (not (string=? content ""))) (try-parse-hermes-tool-calls content) '())) @@ -1749,11 +1750,11 @@ Be concise. Prefer edit over write for modifying existing files. ((null? effective-tcs) (let ((final (make-assistant-message effective-content #f))) (values (or effective-content "") - (append messages (list final))))) + (append msgs (list final))))) ((>= round *max-tool-rounds*) (let* ((response (make-assistant-message effective-content effective-tcs)) (results (execute-tool-calls effective-tcs)) - (new-messages (append messages (list response) results))) + (new-messages (append msgs (list response) results))) (let-values (((fc _tc _u) (stream-chat-with-expert provider new-messages '() (lambda (_tok) (void))))) @@ -1768,18 +1769,19 @@ Be concise. Prefer edit over write for modifying existing files. (else (let* ((response (make-assistant-message effective-content effective-tcs)) (results (execute-tool-calls effective-tcs)) - (new-messages (append messages (list response) results))) - (agent-chat-loop-track-stream provider new-messages tools (+ round 1)))))))) + (new-messages (append msgs (list response) results))) + (agent-chat-loop-track-stream provider new-messages tools (+ round 1))))))))) (def (agent-chat-loop-track provider messages tools round) - (let ((response (chat-with-expert provider messages tools))) + (let* ((msgs (refresh-system-prompt messages)) + (response (chat-with-expert provider msgs tools))) (cond ((not (message-tool-calls response)) (values (or (message-content response) "") - (append messages (list response)))) + (append msgs (list response)))) ((>= round *max-tool-rounds*) (let* ((results (execute-tool-calls (message-tool-calls response))) - (new-messages (append messages (list response) results)) + (new-messages (append msgs (list response) results)) (final (chat-with-expert provider new-messages '()))) (values (or (message-content final) "") (append new-messages (list final))))) @@ -1790,7 +1792,7 @@ Be concise. Prefer edit over write for modifying existing files. (append messages (list final))))) (else (let* ((results (execute-tool-calls (message-tool-calls response))) - (new-messages (append messages (list response) results))) + (new-messages (append msgs (list response) results))) (agent-chat-loop-track provider new-messages tools (+ round 1))))))) (def (agent-step messages)