Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

AI chat: keep only the newest views in the history, and explain HTTP 413 (#2298) (#2299)

Christopher Kochb01f361edf98
diff --git a/assets/locales/cs.json b/assets/locales/cs.json
index 243e9a2c0f..aa0884302f 100644
--- a/assets/locales/cs.json
+++ b/assets/locales/cs.json
@@ -1557,6 +1557,7 @@
"ui.ai_http_busy": "endpoint je zaneprázdněný nebo překročil limit; zkuste to později",
"ui.ai_http_key": "endpoint odmítl klíč: zkontrolujte jej v Předvolby > AI",
"ui.ai_http_not_found": "takový endpoint nebo model neexistuje: zkontrolujte základní URL a název modelu",
+ "ui.ai_http_too_large": "konverzace je pro tento endpoint příliš velká; začněte nový chat",
"ui.ai_key": "API klíč",
"ui.ai_key_hint": "volitelné; uloženo v systémové klíčence",
"ui.ai_key_needs_https": "Klíč se odesílá pouze přes HTTPS nebo na tento počítač.",
diff --git a/assets/locales/da.json b/assets/locales/da.json
index c6abc80ee5..332b952d24 100644
--- a/assets/locales/da.json
+++ b/assets/locales/da.json
@@ -1738,6 +1738,7 @@
"ui.ai_http_busy": "endepunktet er optaget eller har nået sin grænse; prøv igen senere",
"ui.ai_http_key": "endepunktet afviste nøglen: Kontrollér den i Indstillinger > AI",
"ui.ai_http_not_found": "endepunktet eller modellen findes ikke: Kontrollér basis-URL og modelnavn",
+ "ui.ai_http_too_large": "samtalen er for stor til dette endpoint; start en ny chat",
"ui.ai_key": "API-nøgle",
"ui.ai_key_hint": "valgfri; gemmes i systemets nøglering",
"ui.ai_key_needs_https": "En nøgle sendes kun via HTTPS eller til denne computer.",
diff --git a/assets/locales/de.json b/assets/locales/de.json
index aac2de1af4..b74e4c2203 100644
--- a/assets/locales/de.json
+++ b/assets/locales/de.json
@@ -1642,6 +1642,7 @@
"ui.ai_http_busy": "der Endpunkt ist ausgelastet oder am Limit; versuchen Sie es später",
"ui.ai_http_key": "der Endpunkt hat den Schlüssel abgelehnt: prüfen Sie ihn unter Einstellungen > KI",
"ui.ai_http_not_found": "Endpunkt oder Modell nicht gefunden: prüfen Sie Basis-URL und Modellnamen",
+ "ui.ai_http_too_large": "die Unterhaltung ist für diesen Endpunkt zu groß; beginnen Sie einen neuen Chat",
"ui.ai_key": "API-Schlüssel",
"ui.ai_key_hint": "optional; im Schlüsselbund des Systems gespeichert",
"ui.ai_key_needs_https": "Ein Schlüssel wird nur über HTTPS oder an diesen Computer gesendet.",
diff --git a/assets/locales/en.json b/assets/locales/en.json
index d37a063e91..41e40ff529 100644
--- a/assets/locales/en.json
+++ b/assets/locales/en.json
@@ -2495,6 +2495,7 @@
"ui.ai_http_busy": "the endpoint is busy or over its limit; try again later",
"ui.ai_http_key": "the endpoint refused the key: check it in Preferences > AI",
"ui.ai_http_not_found": "no such endpoint or model: check the base URL and the model name",
+ "ui.ai_http_too_large": "the conversation is too large for this endpoint; start a new chat",
"ui.ai_key": "API key",
"ui.ai_key_hint": "optional; kept in the system keychain",
"ui.ai_key_needs_https": "A key is only sent over HTTPS, or to this computer.",
diff --git a/assets/locales/es.json b/assets/locales/es.json
index 485ee30296..2b413af789 100644
--- a/assets/locales/es.json
+++ b/assets/locales/es.json
@@ -2041,6 +2041,7 @@
"ui.ai_http_busy": "el servicio está ocupado o ha superado su límite; inténtalo más tarde",
"ui.ai_http_key": "el servicio ha rechazado la clave: compruébala en Preferencias > IA",
"ui.ai_http_not_found": "no existe ese servicio o modelo: comprueba la URL base y el nombre del modelo",
+ "ui.ai_http_too_large": "la conversación es demasiado grande para este endpoint; empieza un chat nuevo",
"ui.ai_key": "Clave de API",
"ui.ai_key_hint": "opcional; se guarda en el llavero del sistema",
"ui.ai_key_needs_https": "La clave solo se envía mediante HTTPS o a este ordenador.",
diff --git a/assets/locales/fr.json b/assets/locales/fr.json
index 2aaaeb7d45..26aa075cf5 100644
--- a/assets/locales/fr.json
+++ b/assets/locales/fr.json
@@ -1404,6 +1404,7 @@
"ui.ai_http_busy": "le point d'accès est occupé ou a dépassé sa limite ; réessayez plus tard",
"ui.ai_http_key": "le point d'accès a refusé la clé : vérifiez-la dans Préférences › IA",
"ui.ai_http_not_found": "point d'accès ou modèle introuvable : vérifiez l'URL de base et le nom du modèle",
+ "ui.ai_http_too_large": "la conversation est trop volumineuse pour ce point d'accès ; commencez une nouvelle discussion",
"ui.ai_key": "Clé d'API",
"ui.ai_key_hint": "facultative ; conservée dans le trousseau de clés du système",
"ui.ai_key_needs_https": "Une clé n'est envoyée qu'en HTTPS, ou vers cet ordinateur.",
diff --git a/assets/locales/gl.json b/assets/locales/gl.json
index 8b49f6ece6..1983aa9b48 100644
--- a/assets/locales/gl.json
+++ b/assets/locales/gl.json
@@ -1390,6 +1390,7 @@
"ui.ai_http_busy": "o punto de acceso está ocupado ou superou o seu límite; téntao de novo máis tarde",
"ui.ai_http_key": "o punto de acceso rexeitou a clave: compróbaa en Preferencias > IA",
"ui.ai_http_not_found": "non existe ese punto de acceso ou modelo: comproba o URL base e o nome do modelo",
+ "ui.ai_http_too_large": "a conversa é demasiado grande para este punto de acceso; comeza un chat novo",
"ui.ai_key": "Clave da API",
"ui.ai_key_hint": "opcional; gárdase no chaveiro do sistema",
"ui.ai_key_needs_https": "A clave só se envía por HTTPS ou a este ordenador.",
diff --git a/assets/locales/hu.json b/assets/locales/hu.json
index cfcf12d665..6aac08f01d 100644
--- a/assets/locales/hu.json
+++ b/assets/locales/hu.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "a végpont foglalt vagy túllépte a korlátját; próbálja később",
"ui.ai_http_key": "a végpont elutasította a kulcsot: ellenőrizze a Beállítások > AI menüben",
"ui.ai_http_not_found": "nincs ilyen végpont vagy modell: ellenőrizze az alap-URL-t és a modell nevét",
+ "ui.ai_http_too_large": "a beszélgetés túl nagy ehhez a végponthoz; kezdjen új csevegést",
"ui.ai_key": "API-kulcs",
"ui.ai_key_hint": "opcionális; a rendszer kulcstartójában tárolódik",
"ui.ai_key_needs_https": "A kulcs csak HTTPS-en vagy erre a számítógépre kerül elküldésre.",
diff --git a/assets/locales/id.json b/assets/locales/id.json
index 3f87c7cc05..11eae48e27 100644
--- a/assets/locales/id.json
+++ b/assets/locales/id.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "endpoint sibuk atau melebihi batas; coba lagi nanti",
"ui.ai_http_key": "endpoint menolak key: periksa di Preferensi > AI",
"ui.ai_http_not_found": "endpoint atau model tidak ada: periksa URL dasar dan nama model",
+ "ui.ai_http_too_large": "percakapan terlalu besar untuk endpoint ini; mulai obrolan baru",
"ui.ai_key": "Key API",
"ui.ai_key_hint": "opsional; disimpan di keychain sistem",
"ui.ai_key_needs_https": "Key hanya dikirim lewat HTTPS, atau ke komputer ini.",
diff --git a/assets/locales/it.json b/assets/locales/it.json
index 773d489fbd..1c61ab57a9 100644
--- a/assets/locales/it.json
+++ b/assets/locales/it.json
@@ -1406,6 +1406,7 @@
"ui.ai_http_busy": "l'endpoint è occupato o ha superato il limite; riprova più tardi",
"ui.ai_http_key": "l'endpoint ha rifiutato la chiave: controllala in Preferenze > IA",
"ui.ai_http_not_found": "endpoint o modello inesistente: controlla l'URL di base e il nome del modello",
+ "ui.ai_http_too_large": "la conversazione è troppo grande per questo endpoint; inizia una nuova chat",
"ui.ai_key": "Chiave API",
"ui.ai_key_hint": "facoltativa; conservata nel portachiavi di sistema",
"ui.ai_key_needs_https": "La chiave viene inviata solo tramite HTTPS o a questo computer.",
diff --git a/assets/locales/ja.json b/assets/locales/ja.json
index 3c484a75e4..92043f78df 100644
--- a/assets/locales/ja.json
+++ b/assets/locales/ja.json
@@ -2495,6 +2495,7 @@
"ui.ai_http_busy": "エンドポイントが混雑しているか上限に達しています。後で再試行してください",
"ui.ai_http_key": "エンドポイントがキーを拒否しました。「環境設定 > AI」で確認してください",
"ui.ai_http_not_found": "エンドポイントまたはモデルが見つかりません。ベース URL とモデル名を確認してください",
+ "ui.ai_http_too_large": "会話がこのエンドポイントには大きすぎます。新しいチャットを始めてください",
"ui.ai_key": "API キー",
"ui.ai_key_hint": "任意。システムのキーチェーンに保存",
"ui.ai_key_needs_https": "キーは HTTPS 経由、またはこのコンピューター宛てにのみ送信されます。",
diff --git a/assets/locales/ko.json b/assets/locales/ko.json
index 467bfb98f5..7f587c0fa1 100644
--- a/assets/locales/ko.json
+++ b/assets/locales/ko.json
@@ -1813,6 +1813,7 @@
"ui.ai_http_busy": "엔드포인트가 바쁘거나 한도에 도달했습니다. 나중에 다시 시도하세요",
"ui.ai_http_key": "엔드포인트가 키를 거부했습니다. 환경 설정 > AI에서 확인하세요",
"ui.ai_http_not_found": "엔드포인트나 모델을 찾을 수 없습니다. 기본 URL과 모델 이름을 확인하세요",
+ "ui.ai_http_too_large": "대화가 이 엔드포인트에 비해 너무 큽니다. 새 채팅을 시작하세요",
"ui.ai_key": "API 키",
"ui.ai_key_hint": "선택 사항, 시스템 키체인에 저장",
"ui.ai_key_needs_https": "키는 HTTPS를 통해서만, 또는 이 컴퓨터로만 전송됩니다.",
diff --git a/assets/locales/ms.json b/assets/locales/ms.json
index 587b8a80a8..e25eb7d9db 100644
--- a/assets/locales/ms.json
+++ b/assets/locales/ms.json
@@ -1390,6 +1390,7 @@
"ui.ai_http_busy": "titik akhir sibuk atau telah melebihi had; cuba lagi kemudian",
"ui.ai_http_key": "kunci ditolak oleh titik akhir: semak dalam Tetapan > AI",
"ui.ai_http_not_found": "titik akhir atau model tidak ditemui: semak URL asas dan nama model",
+ "ui.ai_http_too_large": "perbualan terlalu besar untuk titik akhir ini; mulakan sembang baharu",
"ui.ai_key": "Kunci API",
"ui.ai_key_hint": "tidak wajib; disimpan dalam rantai kunci sistem",
"ui.ai_key_needs_https": "Kunci hanya dihantar melalui HTTPS, atau ke komputer ini.",
diff --git a/assets/locales/nl.json b/assets/locales/nl.json
index 751155eef7..6791dc2919 100644
--- a/assets/locales/nl.json
+++ b/assets/locales/nl.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "het eindpunt is bezet of boven zijn limiet; probeer het later opnieuw",
"ui.ai_http_key": "het eindpunt weigerde de sleutel: controleer deze in Voorkeuren > AI",
"ui.ai_http_not_found": "geen eindpunt of model gevonden: controleer de basis-URL en de modelnaam",
+ "ui.ai_http_too_large": "het gesprek is te groot voor dit endpoint; begin een nieuwe chat",
"ui.ai_key": "API-sleutel",
"ui.ai_key_hint": "optioneel; bewaard in de sleutelhanger van het systeem",
"ui.ai_key_needs_https": "Een sleutel wordt alleen via HTTPS of naar deze computer verzonden.",
diff --git a/assets/locales/pl.json b/assets/locales/pl.json
index e4dd336668..21198cab19 100644
--- a/assets/locales/pl.json
+++ b/assets/locales/pl.json
@@ -1576,6 +1576,7 @@
"ui.ai_http_busy": "punkt końcowy jest zajęty lub przekroczył limit; spróbuj ponownie później",
"ui.ai_http_key": "punkt końcowy odrzucił klucz: sprawdź go w Preferencje > AI",
"ui.ai_http_not_found": "nie ma takiego punktu końcowego lub modelu: sprawdź bazowy URL i nazwę modelu",
+ "ui.ai_http_too_large": "rozmowa jest za duża dla tego punktu końcowego; rozpocznij nowy czat",
"ui.ai_key": "Klucz API",
"ui.ai_key_hint": "opcjonalny; przechowywany w systemowym pęku kluczy",
"ui.ai_key_needs_https": "Klucz jest wysyłany tylko przez HTTPS albo do tego komputera.",
diff --git a/assets/locales/pt-BR.json b/assets/locales/pt-BR.json
index 8c2222d9ec..42604bb9ef 100644
--- a/assets/locales/pt-BR.json
+++ b/assets/locales/pt-BR.json
@@ -1705,6 +1705,7 @@
"ui.ai_http_busy": "o endpoint está ocupado ou acima do limite; tente de novo mais tarde",
"ui.ai_http_key": "o endpoint recusou a chave: verifique-a em Preferências > IA",
"ui.ai_http_not_found": "endpoint ou modelo inexistente: verifique a URL base e o nome do modelo",
+ "ui.ai_http_too_large": "a conversa é grande demais para este endpoint; comece um novo chat",
"ui.ai_key": "Chave de API",
"ui.ai_key_hint": "opcional; guardada no chaveiro do sistema",
"ui.ai_key_needs_https": "Uma chave só é enviada por HTTPS ou para este computador.",
diff --git a/assets/locales/ro.json b/assets/locales/ro.json
index ca916a218c..01cec7dbd9 100644
--- a/assets/locales/ro.json
+++ b/assets/locales/ro.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "endpointul este ocupat sau și-a depășit limita; încercați mai târziu",
"ui.ai_http_key": "endpointul a refuzat cheia: verificați-o în Preferințe > AI",
"ui.ai_http_not_found": "nu există un astfel de endpoint sau model: verificați URL-ul de bază și numele modelului",
+ "ui.ai_http_too_large": "conversația este prea mare pentru acest endpoint; începeți un chat nou",
"ui.ai_key": "Cheie API",
"ui.ai_key_hint": "opțional; păstrată în depozitul de chei al sistemului",
"ui.ai_key_needs_https": "O cheie se trimite doar prin HTTPS sau către acest calculator.",
diff --git a/assets/locales/ru.json b/assets/locales/ru.json
index 3066bd3695..e13f608b63 100644
--- a/assets/locales/ru.json
+++ b/assets/locales/ru.json
@@ -1389,6 +1389,7 @@
"ui.ai_http_busy": "конечная точка занята или превысила лимит; повторите позже",
"ui.ai_http_key": "конечная точка отклонила ключ: проверьте его в Настройки > ИИ",
"ui.ai_http_not_found": "нет такой конечной точки или модели: проверьте базовый URL и имя модели",
+ "ui.ai_http_too_large": "разговор слишком велик для этой конечной точки; начните новый чат",
"ui.ai_key": "Ключ API",
"ui.ai_key_hint": "необязательно; хранится в системной связке ключей",
"ui.ai_key_needs_https": "Ключ отправляется только по HTTPS или на этот компьютер.",
diff --git a/assets/locales/sv.json b/assets/locales/sv.json
index f71e5e46cf..b4c31c7263 100644
--- a/assets/locales/sv.json
+++ b/assets/locales/sv.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "slutpunkten är upptagen eller över sin gräns; försök igen senare",
"ui.ai_http_key": "slutpunkten nekade nyckeln: kontrollera den under Inställningar > AI",
"ui.ai_http_not_found": "ingen sådan slutpunkt eller modell: kontrollera bas-URL:en och modellnamnet",
+ "ui.ai_http_too_large": "konversationen är för stor för den här slutpunkten; starta en ny chatt",
"ui.ai_key": "API-nyckel",
"ui.ai_key_hint": "valfritt; lagras i systemets nyckelring",
"ui.ai_key_needs_https": "En nyckel skickas endast via HTTPS, eller till den här datorn.",
diff --git a/assets/locales/tr.json b/assets/locales/tr.json
index 686556011a..413a877132 100644
--- a/assets/locales/tr.json
+++ b/assets/locales/tr.json
@@ -1389,6 +1389,7 @@
"ui.ai_http_busy": "uç nokta meşgul veya sınırda; daha sonra tekrar deneyin",
"ui.ai_http_key": "uç nokta anahtarı reddetti: Ayarlar > AI altında kontrol edin",
"ui.ai_http_not_found": "Uç nokta veya model bulunamadı: temel URL'yi ve model adını kontrol edin",
+ "ui.ai_http_too_large": "konuşma bu uç nokta için çok büyük; yeni bir sohbet başlatın",
"ui.ai_key": "API anahtarı",
"ui.ai_key_hint": "isteğe bağlı; sistem anahtar zincirinde saklanır",
"ui.ai_key_needs_https": "Anahtar yalnızca HTTPS üzerinden veya bu bilgisayara gönderilir.",
diff --git a/assets/locales/uk.json b/assets/locales/uk.json
index bdd095a484..53fdcd7053 100644
--- a/assets/locales/uk.json
+++ b/assets/locales/uk.json
@@ -1685,6 +1685,7 @@
"ui.ai_http_busy": "кінцева точка зайнята або перевищила ліміт; спробуйте пізніше",
"ui.ai_http_key": "кінцева точка відхилила ключ: перевірте його в Налаштування > ШІ",
"ui.ai_http_not_found": "немає такої кінцевої точки чи моделі: перевірте базову URL-адресу й назву моделі",
+ "ui.ai_http_too_large": "розмова завелика для цієї кінцевої точки; почніть новий чат",
"ui.ai_key": "Ключ API",
"ui.ai_key_hint": "необов'язково; зберігається в системному сховищі ключів",
"ui.ai_key_needs_https": "Ключ надсилається лише через HTTPS або на цей комп'ютер.",
diff --git a/assets/locales/vi.json b/assets/locales/vi.json
index 6714a9452d..220b93c08c 100644
--- a/assets/locales/vi.json
+++ b/assets/locales/vi.json
@@ -1390,6 +1390,7 @@
"ui.ai_http_busy": "điểm cuối đang bận hoặc vượt quá giới hạn; vui lòng thử lại sau",
"ui.ai_http_key": "điểm cuối từ chối khóa API: hãy kiểm tra trong Tùy chọn cài đặt > AI",
"ui.ai_http_not_found": "không tìm thấy điểm cuối hoặc mô hình: hãy kiểm tra URL cơ sở và tên mô hình",
+ "ui.ai_http_too_large": "cuộc trò chuyện quá lớn đối với điểm cuối này; hãy bắt đầu cuộc trò chuyện mới",
"ui.ai_key": "Khóa API",
"ui.ai_key_hint": "tùy chọn; lưu trữ bảo mật trong chuỗi khóa hệ thống",
"ui.ai_key_needs_https": "Khóa chỉ được gửi qua HTTPS, hoặc trực tiếp trên máy tính này.",
diff --git a/assets/locales/zh-TW.json b/assets/locales/zh-TW.json
index 02ec3618d0..b98c39ebc0 100644
--- a/assets/locales/zh-TW.json
+++ b/assets/locales/zh-TW.json
@@ -1333,6 +1333,7 @@
"ui.ai_http_busy": "端點繁忙或已達上限,請稍後再試",
"ui.ai_http_key": "端點拒絕了金鑰:請在「偏好設定 > AI」中檢查",
"ui.ai_http_not_found": "找不到該端點或模型:請檢查基礎 URL 和模型名稱",
+ "ui.ai_http_too_large": "對話對此端點來說過大;請開始新的聊天",
"ui.ai_key": "API 金鑰",
"ui.ai_key_hint": "可選;儲存在系統鑰匙圈中",
"ui.ai_key_needs_https": "金鑰只會透過 HTTPS 傳送,或傳送到本機。",
diff --git a/assets/locales/zh.json b/assets/locales/zh.json
index 180f481e5d..c63ac44370 100644
--- a/assets/locales/zh.json
+++ b/assets/locales/zh.json
@@ -2495,6 +2495,7 @@
"ui.ai_http_busy": "端点繁忙或已达上限,请稍后再试",
"ui.ai_http_key": "端点拒绝了密钥:请在“首选项 > AI”中检查",
"ui.ai_http_not_found": "找不到该端点或模型:请检查基础 URL 和模型名称",
+ "ui.ai_http_too_large": "对话对此端点来说过大,请开始新的聊天",
"ui.ai_key": "API 密钥",
"ui.ai_key_hint": "可选;保存在系统钥匙串中",
"ui.ai_key_needs_https": "密钥只会通过 HTTPS 发送,或发送到本机。",
diff --git a/crates/ui-egui/src/agent_chat/api_client.rs b/crates/ui-egui/src/agent_chat/api_client.rs
index e34217782f..6eef97597b 100644
--- a/crates/ui-egui/src/agent_chat/api_client.rs
+++ b/crates/ui-egui/src/agent_chat/api_client.rs
@@ -24,6 +24,12 @@ use super::process::{AgentProcess, FromAgent, ProcessHandle};
const MAX_TOOL_TEXT: usize = 20_000;
/// Messages kept in the conversation (the system note stays).
const MAX_HISTORY: usize = 240;
+/// Pictures of the view kept in the conversation. The whole history goes with every request
+/// (up to `MAX_REQUESTS_PER_TURN` per message), so older views are swapped for a short note:
+/// otherwise each attached view makes every later request bigger, until the endpoint refuses it.
+const MAX_HISTORY_IMAGES: usize = 2;
+/// What an older view becomes in the history.
+const DROPPED_VIEW_NOTE: &str = "[an earlier picture of the view, removed to keep the request small]";
/// How long one tool call may take (it may wait for the user's Allow, declined after four
/// minutes). It does not depend on the endpoint's request timeout: a tool call is SolidCraft's own
/// work and the user's answer, never a wait for the model, so a longer request timeout does not
@@ -274,6 +280,7 @@ impl Worker {
fn turn(&mut self, parts: Vec<PromptPart>) {
self.history.push(user_message(self.cfg.protocol, &parts));
+ drop_old_views(&mut self.history, MAX_HISTORY_IMAGES);
self.trim_history();
// No cap on requests: Stop sets `cancel`, checked at the top of every round.
loop {
@@ -492,6 +499,7 @@ pub fn http_error(status: u16, body: &Value) -> String {
401 | 403 => crate::i18n::tr("ui.ai_http_key"),
404 => crate::i18n::tr("ui.ai_http_not_found"),
429 => crate::i18n::tr("ui.ai_http_busy"),
+ 413 => crate::i18n::tr("ui.ai_http_too_large"),
_ => "",
};
format!(
@@ -652,6 +660,25 @@ fn anthropic_reply(body: &Value) -> Reply {
Reply { text, calls, assistant }
}
+/// Keeps the newest `keep` pictures of the view in the history and swaps every older one for a
+/// short note, so the model still knows a view was shown. Text is never touched. Pictures come
+/// as OpenAI `image_url` parts or Anthropic `image` blocks, depending on the endpoint.
+fn drop_old_views(history: &mut [Value], keep: usize) {
+ let mut seen = 0;
+ for msg in history.iter_mut().rev() {
+ let Some(content) = msg.get_mut("content").and_then(Value::as_array_mut) else { continue };
+ for part in content.iter_mut().rev() {
+ if !matches!(part.get("type").and_then(Value::as_str), Some("image_url" | "image")) {
+ continue;
+ }
+ seen += 1;
+ if seen > keep {
+ *part = json!({"type": "text", "text": DROPPED_VIEW_NOTE});
+ }
+ }
+ }
+}
+
/// MCP `tools/list` as OpenAI function tools (the hidden ones left out).
pub fn openai_tools(list: &Value) -> Vec<Value> {
list.get("tools")
@@ -904,6 +931,80 @@ mod tests {
assert_eq!(a["content"][1]["source"]["data"], "AA==");
}
+ fn view(n: u8) -> Value {
+ user_message(
+ Protocol::OpenAi,
+ &[PromptPart::Text(format!("look {n}")), PromptPart::Image { mime: "image/png".into(), base64: format!("V{n}") }],
+ )
+ }
+
+ fn pictures(history: &[Value]) -> Vec<String> {
+ history
+ .iter()
+ .filter_map(|m| m["content"].as_array())
+ .flatten()
+ .filter_map(|p| p.pointer("/image_url/url").and_then(Value::as_str).map(str::to_string))
+ .collect()
+ }
+
+ #[test]
+ fn only_the_newest_views_stay_in_the_history_and_older_ones_become_a_note() {
+ let mut h = vec![json!({"role": "system", "content": "note"}), view(1), json!({"role": "assistant", "content": "ok"}), view(2)];
+ drop_old_views(&mut h, 2);
+ assert_eq!(pictures(&h).len(), 2, "two views fit, nothing is dropped");
+
+ h.push(json!({"role": "user", "content": "plain text"}));
+ h.push(view(3));
+ drop_old_views(&mut h, 2);
+ assert_eq!(pictures(&h), vec!["data:image/png;base64,V2", "data:image/png;base64,V3"]);
+ // The oldest view is a note now; the words sent with it, and every other message, stay.
+ assert_eq!(h[1]["content"][0]["text"], "look 1");
+ assert_eq!(h[1]["content"][1], json!({"type": "text", "text": DROPPED_VIEW_NOTE}));
+ assert_eq!(h[0]["content"], "note");
+ assert_eq!(h[4]["content"], "plain text");
+ }
+
+ #[test]
+ fn a_long_chat_with_a_view_per_message_does_not_grow_without_end() {
+ let mut h = vec![json!({"role": "system", "content": "note"})];
+ let big = "A".repeat(200_000);
+ for _ in 0..40 {
+ h.push(user_message(
+ Protocol::OpenAi,
+ &[PromptPart::Text("again".into()), PromptPart::Image { mime: "image/png".into(), base64: big.clone() }],
+ ));
+ drop_old_views(&mut h, MAX_HISTORY_IMAGES);
+ h.push(json!({"role": "assistant", "content": "done"}));
+ }
+ let size = json!({"messages": h}).to_string().len();
+ assert_eq!(pictures(&h).len(), MAX_HISTORY_IMAGES);
+ assert!(size < (MAX_HISTORY_IMAGES + 1) * big.len(), "request body is {size} bytes");
+ }
+
+ #[test]
+ fn anthropic_views_are_dropped_the_same_way() {
+ let view = |n: u8| {
+ user_message(
+ Protocol::Anthropic,
+ &[PromptPart::Text(format!("look {n}")), PromptPart::Image { mime: "image/png".into(), base64: format!("V{n}") }],
+ )
+ };
+ let mut h = vec![view(1), json!({"role": "assistant", "content": "ok"}), view(2), view(3)];
+ drop_old_views(&mut h, 2);
+ let kept: Vec<&str> =
+ h.iter().filter_map(|m| m["content"].as_array()).flatten().filter_map(|p| p.pointer("/source/data").and_then(Value::as_str)).collect();
+ assert_eq!(kept, vec!["V2", "V3"]);
+ assert_eq!(h[0]["content"][0]["text"], "look 1");
+ assert_eq!(h[0]["content"][1], json!({"type": "text", "text": DROPPED_VIEW_NOTE}));
+ }
+
+ #[test]
+ fn a_too_large_request_says_so() {
+ let e = http_error(413, &json!({"error": {"message": "Request body is too large."}}));
+ assert!(e.starts_with("HTTP 413: "), "{e}");
+ assert!(e.ends_with("(Request body is too large.)"), "{e}");
+ }
+
/// A scripted endpoint: one answer with a tool call, then a plain answer. The loop runs the
/// tool (here without SolidCraft tools: an unknown tool fails) and reports the turn's end.
#[test]