about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-04-08 20:20:16 +0100
committerArun Isaac2026-04-08 23:52:51 +0100
commit406396e69e2dbae27029982a42dcb749b30893b7 (patch)
treed97304f37ae7459c32e072f087b69ca58cbea05b
parent37fa27186f5793d36c3ee9e8f0a856007d15f959 (diff)
downloadkaagum-406396e69e2dbae27029982a42dcb749b30893b7.tar.gz
kaagum-406396e69e2dbae27029982a42dcb749b30893b7.tar.lz
kaagum-406396e69e2dbae27029982a42dcb749b30893b7.zip
Push check for tool calls and cancellation into state->llm-request.
In doing so, we also change state->llm-request to state->llm-requests.
The new function now returns a list of <llm-request> objects rather
than a single one.
-rw-r--r--kaakaa/tea.scm51
1 files changed, 27 insertions, 24 deletions
diff --git a/kaakaa/tea.scm b/kaakaa/tea.scm
index 0e50b45..5fc54fc 100644
--- a/kaakaa/tea.scm
+++ b/kaakaa/tea.scm
@@ -100,21 +100,31 @@
            (key-ref session-id)
            state-sessions))
 
-(define (state->llm-request session-id state)
-  "Return an @code{<llm-request>} for session with @var{session-id} in @var{state}."
-  (llm-request session-id
-               (map (lambda (message)
-                      ;; Strip out all fields (such as reasoning fields) other
-                      ;; than role, content and tool_calls.
-                      (filter (match-lambda
-                                ((key . _)
-                                 (member key (list "role" "content"
-                                                   "tool_calls"))))
-                              message))
-                    ;; Reverse because we have been prepending new messages onto
-                    ;; the list.
-                    (reverse (focus (state-messages session-id)
-                                    state)))))
+(define (state->llm-requests session-id state)
+  "Return a list of @code{<llm-request>} objects for session with @var{session-id}
+in @var{state}."
+  (if (and (null? (focus (state-tool-calls session-id)
+                         state))
+           (not (focus (state-session-cancelling? session-id)
+                       state)))
+      ;; There are no more tool calls in flight and a cancellation is not in
+      ;; progress; dispatch to LLM.
+      (list (llm-request session-id
+                         (map (lambda (message)
+                                ;; Strip out all fields (such as reasoning
+                                ;; fields) other than role, content and
+                                ;; tool_calls.
+                                (filter (match-lambda
+                                          ((key . _)
+                                           (member key (list "role" "content"
+                                                             "tool_calls"))))
+                                        message))
+                              ;; Reverse because we have been prepending new
+                              ;; messages onto the list.
+                              (reverse (focus (state-messages session-id)
+                                              state)))))
+      ;; There are tool calls or a cancellation in progress; do nothing.
+      (list)))
 
 (define (next-state-client-request state request)
   "Given current @var{state} and a new ACP @var{request}, return the next state and
@@ -170,7 +180,7 @@ a list of effects."
                                            request)))
                                 state)))
               (values state
-                      (list (state->llm-request session-id state)))))
+                      (state->llm-requests session-id state))))
            ("session/cancel"
             (let ((session-id (focus (in "params" "sessionId")
                                      request)))
@@ -364,14 +374,7 @@ state and a list of effects."
                                      ("text" . ,(focus (key-ref "content")
                                                        (tool-call-result-json result)))))
                                    ("rawInput" . ,(tool-call-result-arguments result))))))
-                  ;; If there are no more tool calls and a cancellation is not
-                  ;; in progress, dispatch to LLM.
-                  (if (and (null? (focus (state-tool-calls session-id)
-                                         state))
-                           (not (focus (state-session-cancelling? session-id)
-                                       state)))
-                      (list (state->llm-request session-id state))
-                      (list))))))
+                  (state->llm-requests session-id state)))))
 
 (define (next-state state message)
   "Given current @var{state} and a new @var{message}, return the next state and a