about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-04-08 23:26:54 +0100
committerArun Isaac2026-04-08 23:53:03 +0100
commit90a4d6cb32fd363d1c4fa97cbdd15365b1540db4 (patch)
treeda67facd5faebb59a168beec1ed805fc3deab0d1
parent32c91e58ef4310be4aefcdcc7c6e76daf614d8c8 (diff)
downloadkaagum-90a4d6cb32fd363d1c4fa97cbdd15365b1540db4.tar.gz
kaagum-90a4d6cb32fd363d1c4fa97cbdd15365b1540db4.tar.lz
kaagum-90a4d6cb32fd363d1c4fa97cbdd15365b1540db4.zip
Parse tool call arguments early.
Parse tool call arguments in spec->tool-call and raise an exception on
failure.
-rw-r--r--kaakaa/tools.scm33
1 files changed, 15 insertions, 18 deletions
diff --git a/kaakaa/tools.scm b/kaakaa/tools.scm
index 8142d49..f383446 100644
--- a/kaakaa/tools.scm
+++ b/kaakaa/tools.scm
@@ -154,17 +154,20 @@ fails.
 @var{session-id} and @var{session-cwd} are the ID and current working directory
 of the session the tool call pertains to."
   ;; TODO: Assert that type is function, and do more sanitization.
-  (tool-call session-id
-             session-cwd
-             (focus (key-ref "id")
-                    spec)
-             (focus (in "function" "name")
-                    spec)
-             ;; The arguments may be invalid JSON. So, we don't deserialize
-             ;; right away.
-             (focus (in "function" "arguments")
-                    spec)
-             'pending-approval))
+  (let ((args (guard (c (else
+                         (raise-exception
+                          (tool-call-parse-failure
+                           "Error: Arguments are not valid JSON"))))
+                (json-string->scm (focus (in "function" "arguments")
+                                         spec)))))
+    (tool-call session-id
+               session-cwd
+               (focus (key-ref "id")
+                      spec)
+               (focus (in "function" "name")
+                      spec)
+               args
+               'pending-approval)))
 
 (define (eval-tool-call tool-call tools)
   "Evaluate @var{tool-call} and return a @code{<tool-call-result>}
@@ -187,13 +190,7 @@ available tools to their respective @code{<tool>} objects."
                                            ("tool_call_id" . ,(tool-call-id tool-call))
                                            ("content" . ,(tool-call-failure-message c)))
                                          #f)))
-              (let* ((args
-                      ;; Deserialize arguments checking for valid JSON.
-                      (guard (c (else
-                                 (raise-exception
-                                  (tool-call-failure
-                                   "Error: Arguments are not valid JSON"))))
-                        (json-string->scm (tool-call-arguments tool-call))))
+              (let* ((args (tool-call-arguments tool-call))
                      (filtered-args
                       ;; Only pick out valid arguments, and error out if any
                       ;; required arguments are missing.