about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-04-13 19:24:14 +0100
committerArun Isaac2026-04-13 19:25:59 +0100
commit40798ff23f64434136a3984e8d0f33500da9d284 (patch)
tree88f00714a29272497185db241cf9495e7bf6c4ed
parente1304cb5f943aebf796aa52fcb256b2efe0f47b1 (diff)
downloadkaagum-40798ff23f64434136a3984e8d0f33500da9d284.tar.gz
kaagum-40798ff23f64434136a3984e8d0f33500da9d284.tar.lz
kaagum-40798ff23f64434136a3984e8d0f33500da9d284.zip
Expose run-tea-loop interface from (kaagum tea).
This encapsulates (kaagum tea) better. tea-loop and initial-state are
really internal implementation details that should not be exposed.
-rwxr-xr-xbin/kaagum9
-rw-r--r--kaagum/tea.scm39
2 files changed, 26 insertions, 22 deletions
diff --git a/bin/kaagum b/bin/kaagum
index b8ee084..42475c5 100755
--- a/bin/kaagum
+++ b/bin/kaagum
@@ -104,8 +104,7 @@ Run kaagum AI agent.
          (exit #t))
        (unless (assq-ref args 'api-key-command)
          (die "--api-key-command not specified"))
-       (tea-loop (initial-state)
-                 (assq-ref args 'api-base-uri)
-                 (get-api-key (assq-ref args 'api-key-command))
-                 (assq-ref args 'model)
-                 %base-tools)))))
+       (run-tea-loop (assq-ref args 'api-base-uri)
+                     (get-api-key (assq-ref args 'api-key-command))
+                     (assq-ref args 'model)
+                     %base-tools)))))
diff --git a/kaagum/tea.scm b/kaagum/tea.scm
index cf01f81..bdfb55e 100644
--- a/kaagum/tea.scm
+++ b/kaagum/tea.scm
@@ -34,8 +34,7 @@
   #:use-module (kaagum records)
   #:use-module (kaagum tools)
   #:use-module (kaagum utils)
-  #:export (initial-state
-            tea-loop))
+  #:export (run-tea-loop))
 
 (define %tool-allow-once
   '(("optionId" . "allow-once")
@@ -84,9 +83,6 @@
           (requests-alist state-requests-alist lensed)
           (sessions state-sessions lensed)))
 
-(define (initial-state)
-  (state #f 0 0 '() '()))
-
 (define (state-cwd session-id)
   "Return a lens to focus on current working directory of session with
 @var{session-id} in state."
@@ -264,7 +260,7 @@ the table."
 @var{argument} for @var{session-id}, return the next state and a list of
 effects.
 
-@var{tools} is the same as in @code{tea-loop}."
+@var{tools} is the same as in @code{run-tea-loop}."
   (cond
    ;; command exists
    ((focus (key-ref command-name)
@@ -281,7 +277,7 @@ effects.
   "Given current @var{state} and a new ACP @var{request}, return the next state and
 a list of effects.
 
-@var{model} and @var{tools} are the same as in @code{tea-loop}."
+@var{model} and @var{tools} are the same as in @code{run-tea-loop}."
   (let ((request-id (focus (key-ref "id")
                            request)))
     (cond
@@ -534,7 +530,7 @@ the agent to the client. Stash @var{context} against request ID in
   "Given current @var{state} and a new tool @var{call-json}, return the next state
 and a list of effects.
 
-@var{tools} is the same as in @code{tea-loop}."
+@var{tools} is the same as in @code{run-tea-loop}."
   (guard (c ((tool-call-parse-failure? c)
              (let ((call-id (focus (key-ref "id") call-json)))
                (values (-> state
@@ -613,7 +609,7 @@ and a list of effects.
   "Given current @var{state} and a new LLM @var{response}, return the next state
 and a list of effects.
 
-@var{tools} is the same as in @code{tea-loop}."
+@var{tools} is the same as in @code{run-tea-loop}."
   (let* ((session-id (llm-response-session-id response))
          (llm-reply (focus (in-json "choices" 0 "message")
                            (llm-response-json response)))
@@ -700,7 +696,7 @@ state and a list of effects."
   "Given current @var{state} and a new @var{message}, return the next state and a
 list of effects.
 
-@var{model} and @var{tools} are the same as in @code{tea-loop}."
+@var{model} and @var{tools} are the same as in @code{run-tea-loop}."
   (cond
    ((acp-message? message)
     (let ((json-message (focus acp-message-json message)))
@@ -728,11 +724,8 @@ list of effects.
 (define (tea-loop state llm-base-uri llm-api-key model tools)
   "Run a @acronym{TEA, The Elm Architecture} loop starting with @var{state}.
 
-@var{llm-base-uri} is the base URI of the LLM provider. @var{llm-api-key} is the
-API key to authenticate with the LLM provider. @var{model} is the name of the
-model to initialize sessions with. @var{tools} is the list of tools made
-available to the LLM. It is an association list matching tool names to
-@code{<tool>} objects."
+@var{llm-base-uri}, @var{llm-api-key}, @var{model} and @var{tools} are the same
+as in @code{run-tea-loop}."
   ;; Read a JSON-RPC message, handle it, and loop.
   (let ((line (get-line (current-input-port))))
     (unless (eof-object? line)
@@ -751,7 +744,7 @@ available to the LLM. It is an association list matching tool names to
   "Handle @var{event} with @var{state} and return a new state.
 
 @var{llm-base-uri}, @var{llm-api-key}, @var{model} and @var{tools} are the same
-as in @code{tea-loop}."
+as in @code{run-tea-loop}."
   (let-values (((state effects)
                 ;; Compute the next state and collect the effects.
                 (next-state state event model tools)))
@@ -764,7 +757,7 @@ as in @code{tea-loop}."
   "Do @var{effect} with @var{state} and return a new state.
 
 @var{llm-base-uri}, @var{llm-api-key}, @var{model} and @var{tools} are the same
-as in @code{tea-loop}."
+as in @code{run-tea-loop}."
   (cond
    ;; Send message to client, and return the state unchanged.
    ((acp-message? effect)
@@ -797,3 +790,15 @@ as in @code{tea-loop}."
                   llm-api-key
                   model
                   tools))))
+
+(define (run-tea-loop llm-base-uri llm-api-key model tools)
+  "Run a @acronym{TEA, The Elm Architecture} loop. @var{llm-base-uri} is the base
+URI of the LLM provider. @var{llm-api-key} is the API key to authenticate with
+the LLM provider. @var{model} is the name of the model to initialize sessions
+with. @var{tools} is the list of tools made available to the LLM. It is an
+association list matching tool names to @code{<tool>} objects."
+  (tea-loop (state #f 0 0 '() '())
+            llm-base-uri
+            llm-api-key
+            model
+            tools))