Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
docs(bots): keep the comments of text-safe in English
Jean-Christophe Tourniere83f78465658
diff --git a/src/bots/bot-turn.ts b/src/bots/bot-turn.tsindex 13b7622f..26cbacd7 100644--- a/src/bots/bot-turn.ts+++ b/src/bots/bot-turn.ts@@ -587,7 +587,7 @@ export function resolveTurnLimits(manifest: BotManifest, overrides?: Partial<Bot/*** The text a failure is classified on: the message, and what the error wraps.*- * The AI SDK reports a cut stream as « Failed to process successful response » and keeps the real+ * The AI SDK reports a cut stream as "Failed to process successful response" and keeps the real* reason in `cause` — an ECONNRESET, a body cut mid-read, a JSON parse error. Reading only the* message sent every one of those to unknown_error: no retry, DLQ, needs-you. A network blip then* looked like a permanent fault, and the owner was told a bot had failed when it had merely lostdiff --git a/src/utils/config-vision.test.ts b/src/utils/config-vision.test.tsindex 52928b65..11e9451e 100644--- a/src/utils/config-vision.test.ts+++ b/src/utils/config-vision.test.ts@@ -3,7 +3,7 @@ import { getDefaultConfig } from './config.js';import { modelSeesImages } from '../core/attachments.js';/**- * « Not declared » is a third state, not `false`.+ * "Not declared" is a third state, not `false`.** The three self-hosted endpoints (`openaiCompat`, `lmStudio`, `litellm`) used to default their* capability flag to `false` through `getEnvBool(…, false)`. A `false` is a declaration: it turneddiff --git a/src/utils/text-safe.test.ts b/src/utils/text-safe.test.tsindex ba780994..c19802d6 100644--- a/src/utils/text-safe.test.ts+++ b/src/utils/text-safe.test.ts@@ -1,51 +1,51 @@import { describe, expect, it } from 'vitest';import { stripLoneSurrogates, truncateText } from './text-safe.js';-const HEADPHONES = '\u{1F3A7}'; // 🎧 — une paire de surrogates+const HEADPHONES = '\u{1F3A7}'; // 🎧 — a surrogate pairdescribe('stripLoneSurrogates', () => {- it('laisse une paire valide intacte', () => {+ it('leaves a valid pair intact', () => {expect(stripLoneSurrogates(`casque ${HEADPHONES}`)).toBe(`casque ${HEADPHONES}`);});- it('jette une moitié haute orpheline, celle qui fait échouer le fournisseur', () => {+ it('drops an orphaned high half — the one the provider rejects', () => {const half = HEADPHONES.slice(0, 1);expect(stripLoneSurrogates(`abc${half}`)).toBe('abc');});- it('jette une moitié basse orpheline', () => {+ it('drops an orphaned low half', () => {const half = HEADPHONES.slice(1);expect(stripLoneSurrogates(`abc${half}`)).toBe('abc');});- it('conserve le texte autour et recolle les paires suivantes', () => {+ it('keeps the text around it and rejoins the pairs that follow', () => {const half = HEADPHONES.slice(0, 1);expect(stripLoneSurrogates(`a${half}b${HEADPHONES}c`)).toBe(`ab${HEADPHONES}c`);});});describe('truncateText', () => {- it('ne coupe pas une paire de surrogates au milieu', () => {- // La coupe tombe exactement entre le high et le low : c'est le cas qui partait en 400.+ it('never cuts through a surrogate pair', () => {+ // The cut lands exactly between the high and the low half: the case that used to go out as a 400.const text = `${'x'.repeat(10)}${HEADPHONES}fin`;const cut = truncateText(text, 11);expect(cut).toBe('x'.repeat(10));expect(cut.charCodeAt(cut.length - 1)).toBeLessThan(0xd800);});- it('produit un corps JSON que plus aucun parseur strict ne refuse', () => {+ it('produces a JSON body no strict parser refuses', () => {const text = `${'x'.repeat(10)}${HEADPHONES}fin`;const body = JSON.stringify({ content: truncateText(text, 11) });- expect(body).not.toMatch(/\\u[dD][89abAB]/); // aucun \udXXX isolé+ expect(body).not.toMatch(/\\u[dD][89abAB]/); // no lone \udXXX});- it('garde le texte entier quand il tient, sans surrogates isolés', () => {+ it('keeps the whole text when it fits, minus any lone surrogate', () => {const half = HEADPHONES.slice(0, 1);expect(truncateText(`court${half}`, 100)).toBe('court');expect(truncateText('court', 100)).toBe('court');});- it('rend une chaîne vide pour une limite nulle ou négative', () => {+ it('returns an empty string for a zero or negative limit', () => {expect(truncateText('abc', 0)).toBe('');expect(truncateText('abc', -5)).toBe('');});diff --git a/src/utils/text-safe.ts b/src/utils/text-safe.tsindex 5893dacd..35cb4d54 100644--- a/src/utils/text-safe.ts+++ b/src/utils/text-safe.ts@@ -1,13 +1,12 @@/**- * Texte sûr pour un corps JSON destiné à un fournisseur.+ * Text that is safe to put in a JSON body sent to a provider.*- * Un surrogate UTF-16 isolé — ce que produit n'importe quelle coupe par index sur une chaîne qui- * contient un emoji — traverse `JSON.stringify` sous la forme `\ud83c`, et certains serveurs- * refusent le corps entier : DeepSeek répond « 400 Failed to parse the request body as JSON:- * messages[6].content: unexpected end of hex escape ». Mesuré sur api.deepseek.com : la même- * chaîne avec le surrogate *brut* en UTF-8 passe (200), la même en séquence d'échappement est- * rejetée (400). La coupe doit donc tomber entre deux points de code, et le texte envoyé ne doit- * plus contenir de moitié de paire.+ * A lone UTF-16 surrogate — what any index-based slice of a string holding an emoji produces —+ * travels through `JSON.stringify` as `\ud83c`, and some servers reject the whole body for it:+ * DeepSeek answers "400 Failed to parse the request body as JSON: messages[6].content: unexpected+ * end of hex escape". Measured against api.deepseek.com: the same string with the *raw* surrogate+ * in UTF-8 passes (200), the same one as an escape sequence is rejected (400). So a cut has to land+ * between two code points, and the text we send must no longer hold half a pair.*/const HIGH_SURROGATE_START = 0xd800;@@ -24,8 +23,8 @@ function isLow(code: number): boolean {}/**- * Retire les surrogates isolés. Une paire valide (emoji) est conservée telle quelle : seules les- * moitiés orphelines — celles qui font échouer le serveur — disparaissent.+ * Drops lone surrogates. A valid pair (an emoji) is kept as it is: only the orphaned halves — the+ * ones that make the server fail — disappear.*/export function stripLoneSurrogates(text: string): string {let out = '';@@ -38,22 +37,22 @@ export function stripLoneSurrogates(text: string): string {i += 1;continue;}- continue; // high sans low : on jette la moitié+ continue; // high without low: drop the half}- if (isLow(code)) continue; // low sans high : on jette la moitié+ if (isLow(code)) continue; // low without high: drop the halfout += text[i];}return out;}/**- * Coupe à `max` caractères sans jamais séparer une paire de surrogates, puis retire ce qui- * resterait d'isolé. `max` compte des unités UTF-16, comme `String.prototype.length`.+ * Cuts at `max` characters without ever splitting a surrogate pair, then drops whatever would still+ * be lone. `max` counts UTF-16 units, like `String.prototype.length`.*/export function truncateText(text: string, max: number): string {if (max <= 0) return '';if (text.length <= max) return stripLoneSurrogates(text);let end = max;- if (end > 0 && isHigh(text.charCodeAt(end - 1))) end -= 1; // la paire commençait juste avant la coupe+ if (end > 0 && isHigh(text.charCodeAt(end - 1))) end -= 1; // the pair started just before the cutreturn stripLoneSurrogates(text.slice(0, end));}