From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=systemreboot.net; s=default; t=1794101792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F//ptdjMNLt94GSzr2BGqRQEgUO5pNgV5LtTbMssi1Q=; b=R2l8xX+54gfz3EElu1g1ZnJVeQ/hkIY4sPjUIgYQRwJ9zqtS5a6wfyV7e3LZQhoVEEZDze 6pac+K2vl81C2Qm2HTVQoL0ngfafylhEh+fBXhOkj2aYD8j3Fe/N34H9TedCduFqbe27jf GDJ9iabKCRocRtvp+8dcqTTk76YCNETQS5VQ3aDkqjNjK5jKS3+yLBJGhYDNw1zOCmgmcI 7BGJybRtE1DXVm4SvsOR431uZFbQpJPEwtSe4CJciwih7WcjU5CjzSiFGrfMVVh7Kv83my +5edlj62ITuTwnl5vNEDESsy5sTkQ7AgSoXZ+ajnQI5NjnzwAWdcd3kP9GrTJg== Received: from steel ( [192.168.1.1]) by hrrol.systemreboot.net (OpenSMTPD) with ESMTPSA id 37675e9d (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 8 Nov 2026 01:36:32 +0000 (UTC) From: Arun Isaac To: Arun Isaac , Ricardo Wurmus Cc: kaagum@systemreboot.net Subject: [PATCH] Support context compaction. Date: Sun, 8 Nov 2026 01:36:26 +0000 Message-ID: <20261108013629.2076-1-arunisaac@systemreboot.net> X-Mailer: git-send-email 2.54.0 In-Reply-To: <87zevkqf4d.fsf@systemreboot.net> References: <87zevkqf4d.fsf@systemreboot.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: --- bin/kaagum | 9 ++++++++- kaagum/tea.scm | 34 +++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/bin/kaagum b/bin/kaagum index 2c8c9ee..14a65b1 100755 --- a/bin/kaagum +++ b/bin/kaagum @@ -53,6 +53,9 @@ exec guile --no-auto-compile -e main -s "$0" "$@" (option (list #\t "more-tools") #t #f (lambda (opt name arg result) (acons 'more-tools arg result))) + (option (list #c "compacter") #t #f + (lambda (opt name arg result) + (acons 'compacter arg result))) (option (list "kagi-api-key-command") #t #f (lambda (opt name arg result) (acons 'kagi-api-key-command arg result))) @@ -79,6 +82,7 @@ Run kaagum AI agent. -m, --model=MODEL LLM model name to start new sessions with (default: \"anthropic/claude-opus-4.8\") -t, --more-tools=FILE scheme file providing more tools to the agent + -c, --compacter=FILE scheme file providing context compacter --kagi-api-key-command=COMMAND command to run to get Kagi API key @@ -135,7 +139,10 @@ Run kaagum AI agent. '()) (if (assq-ref args 'more-tools) (load (assq-ref args 'more-tools)) - '()))))) + '())) + (if (assq-ref args 'compacter) + (load (assq-ref args 'compacter)) + identity)))) (parameterize ((%kagi-api-key (and kagi-api-key-command (get-api-key kagi-api-key-command)))) ;; (trace 'kagi "Key ~a" (%kagi-api-key)) diff --git a/kaagum/tea.scm b/kaagum/tea.scm index f3b2397..8d28213 100644 --- a/kaagum/tea.scm +++ b/kaagum/tea.scm @@ -962,7 +962,7 @@ in @code{tea-loop}." ((tool-call-result? message) (tool-call-result-effects message)))) -(define* (tea-loop state llm-base-uri llm-api-key models tools +(define* (tea-loop state llm-base-uri llm-api-key models tools compacter #:optional (events (let ((line (get-line (current-input-port)))) (and (not (eof-object? line)) (begin @@ -970,10 +970,10 @@ in @code{tea-loop}." (list (acp-message (json-string->scm line)))))))) "Run a @acronym{TEA, The Elm Architecture} loop starting with @var{state}. -@var{llm-base-uri}, @var{llm-api-key} and @var{tools} are the same as in -@code{run-tea-loop}. @var{models} is an association list mapping model IDs to -@code{} objects, the first element of which is the default model. -@var{events} is an internal recursion variable." +@var{llm-base-uri}, @var{llm-api-key}, @var{tools} and @var{compacter} are the +same as in @code{run-tea-loop}. @var{models} is an association list mapping +model IDs to @code{} objects, the first element of which is the default +model. @var{events} is an internal recursion variable." ;; Read a JSON-RPC message, handle it, and loop. (when events (let-values (((effects next-state) @@ -981,18 +981,18 @@ in @code{tea-loop}." (state-append-map (cut acp-effects <> models tools) events) state))) - (match (append-map (cut do-effect <> llm-base-uri llm-api-key tools) + (match (append-map (cut do-effect <> llm-base-uri llm-api-key tools compacter) effects) (() - (tea-loop next-state llm-base-uri llm-api-key models tools)) + (tea-loop next-state llm-base-uri llm-api-key models tools compacter)) ((events ...) - (tea-loop next-state llm-base-uri llm-api-key models tools events)))))) + (tea-loop next-state llm-base-uri llm-api-key models tools compacter events)))))) -(define (do-effect effect llm-base-uri llm-api-key tools) +(define (do-effect effect llm-base-uri llm-api-key tools compacter) "Do @var{effect}. Return list of events produced by the effect. -@var{llm-base-uri}, @var{llm-api-key} and @var{tools} are the same as in -@code{run-tea-loop}." +@var{llm-base-uri}, @var{llm-api-key}, @var{tools} and @var{compacter} are the +same as in @code{run-tea-loop}." (cond ;; Send message to client, and return the state unchanged. ((acp-message? effect) @@ -1009,7 +1009,7 @@ in @code{tea-loop}." (openai-query llm-base-uri llm-api-key (llm-request-model effect) - (llm-request-messages effect) + (compacter (llm-request-messages effect)) (map (match-lambda ((name . tool) (tool->spec name tool))) @@ -1018,16 +1018,20 @@ in @code{tea-loop}." ((tool-call? effect) (list (eval-tool-call effect tools))))) -(define (run-tea-loop llm-base-uri llm-api-key model tools) +(define (run-tea-loop llm-base-uri llm-api-key model tools compacter) "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{} objects." +association list matching tool names to @code{} objects. @var{compacter} +is a function that takes a list of messages and returns a compacted list of +messages to query the LLM with. The compacted messages hopefully use fewer +tokens than the original messages." (let ((models (openai-models llm-base-uri llm-api-key))) (tea-loop (state #f 0 0 '() '()) llm-base-uri llm-api-key (cons (assoc model models) (alist-delete model models)) - tools))) + tools + compacter))) -- 2.54.0