Files
OmniRoute/tests/unit/vision-bridge-maxchars.test.ts
Diego Rodrigues de Sa e Souza a7ddcf16ba feat(bridge): native-vision skip guard + configurable describe output cap (#10289)
* test(bridge): explicit native-vision skip guard + skip log

* feat(bridge): configurable describe output cap (modalityBridgeVisionMaxChars)

* feat(dashboard): maxChars field on Modality Bridge vision tab

Add the "Max description characters" field to the Vision tab's Advanced
panel (modalityBridgeVisionMaxChars, clamped to the 100-50000 schema
range with 0 treated as the explicit "unlimited" sentinel), wire the
en.json copy and sync it across all 42 locales, and document the new
setting in GUARDRAILS.md.

* fix(bridge): allow explicit 0 to disable the describe cap

updateSettingsSchema previously rejected modalityBridgeVisionMaxChars: 0
because the field's range was min(100).max(50000), so a dashboard PATCH
sending the explicit "unlimited" sentinel would 400. Widen the schema to
z.union([z.literal(0), z.number().int().min(100).max(50000)]) so 0
validates as its own valid value, not just an implicit default.

* chore(i18n): resync locale keys after release merge

* chore(quality): rebaseline deadExports for the OCR/image-to-text series

---------

Co-authored-by: Xiangzhe <bakryun0718@proton.me>
2026-08-14 13:06:29 -03:00

143 lines
5.6 KiB
TypeScript

/**
* `modalityBridgeVisionMaxChars` — configurable teto for the vision-bridge
* describe-path output. 0 (default) means "no cap" (existing behavior
* unchanged); a positive value truncates the description with a `…` suffix
* before it is spliced back as `[Image N]: <description>`.
*
* Follows the DI harness pattern from `vision-bridge-cc-no-reroute.test.ts` /
* `vision-bridge-mode.test.ts` (settings + vision call injected, no real DB
* dependency for the describe path itself).
*/
import test from "node:test";
import assert from "node:assert/strict";
const { VisionBridgeGuardrail } = await import("../../src/lib/guardrails/visionBridge.ts");
const { resolveVisionBridgeRuntimeSettings } =
await import("../../src/shared/constants/modalityBridgeDefaults.ts");
const { updateSettingsSchema } = await import("../../src/shared/validation/settingsSchemas.ts");
import type { GuardrailContext } from "../../src/lib/guardrails/base.ts";
import type { VisionModelConfig } from "../../src/lib/guardrails/visionBridgeHelpers.ts";
const TEXT_ONLY_MODEL = "some/text-only-model";
const LONG_DESCRIPTION = "x".repeat(500);
function imageBody(uniqueRef: string): Record<string, unknown> {
return {
model: TEXT_ONLY_MODEL,
messages: [
{
role: "user",
content: [
{ type: "text", text: "what is in the image?" },
{
type: "image_url",
image_url: {
url: `data:image/png;base64,${Buffer.from(uniqueRef).toString("base64")}`,
},
},
],
},
],
};
}
function findImageDescriptionText(modifiedPayload: unknown): string | undefined {
const body = modifiedPayload as { messages?: Array<{ content?: unknown }> } | undefined;
const messages = body?.messages ?? [];
for (const message of messages) {
const parts = message.content;
if (!Array.isArray(parts)) continue;
for (const part of parts) {
const p = part as { type?: string; text?: string };
if (p?.type === "text" && typeof p.text === "string" && p.text.startsWith("[Image 1]:")) {
return p.text;
}
}
}
return undefined;
}
test("modalityBridgeVisionMaxChars=120 caps the description with a … suffix", async () => {
const guardrail = new VisionBridgeGuardrail({
deps: {
getSettings: async () => ({
visionBridgeEnabled: true,
modalityBridgeVisionMaxChars: 120,
}),
callVisionModel: async (_imageDataUri: string, _config: VisionModelConfig) =>
LONG_DESCRIPTION,
},
});
const body = imageBody("maxchars-cap-test");
const context: GuardrailContext = { model: TEXT_ONLY_MODEL, log: console };
const result = await guardrail.preCall(body, context);
const text = findImageDescriptionText(result.modifiedPayload);
assert.ok(text, "expected a [Image 1]: text part in the modified payload");
const description = text!.slice("[Image 1]: ".length);
assert.ok(
description.length <= 121,
`capped description should be <= 121 chars (120 + ellipsis), got ${description.length}`
);
assert.ok(description.endsWith("…"), "capped description must end with an ellipsis suffix");
});
test("no modalityBridgeVisionMaxChars key: description is passed through in full", async () => {
const guardrail = new VisionBridgeGuardrail({
deps: {
getSettings: async () => ({
visionBridgeEnabled: true,
}),
callVisionModel: async (_imageDataUri: string, _config: VisionModelConfig) =>
LONG_DESCRIPTION,
},
});
const body = imageBody("maxchars-nokey-test");
const context: GuardrailContext = { model: TEXT_ONLY_MODEL, log: console };
const result = await guardrail.preCall(body, context);
const text = findImageDescriptionText(result.modifiedPayload);
assert.ok(text, "expected a [Image 1]: text part in the modified payload");
const description = text!.slice("[Image 1]: ".length);
assert.equal(description, LONG_DESCRIPTION, "without a cap the description must be untouched");
assert.ok(!description.endsWith("…"), "unconfigured cap must never add an ellipsis");
});
test("resolveVisionBridgeRuntimeSettings({}) defaults maxChars to 0 (no cap)", () => {
const runtime = resolveVisionBridgeRuntimeSettings({});
assert.equal(runtime.maxChars, 0);
});
test("updateSettingsSchema accepts an explicit modalityBridgeVisionMaxChars: 0 to disable the cap", async () => {
const parsed = updateSettingsSchema.safeParse({ modalityBridgeVisionMaxChars: 0 });
assert.equal(parsed.success, true, "explicit 0 must validate (disables the cap)");
const guardrail = new VisionBridgeGuardrail({
deps: {
getSettings: async () => ({
visionBridgeEnabled: true,
modalityBridgeVisionMaxChars: 0,
}),
callVisionModel: async (_imageDataUri: string, _config: VisionModelConfig) =>
LONG_DESCRIPTION,
},
});
const body = imageBody("maxchars-explicit-zero-test");
const context: GuardrailContext = { model: TEXT_ONLY_MODEL, log: console };
const result = await guardrail.preCall(body, context);
const text = findImageDescriptionText(result.modifiedPayload);
assert.ok(text, "expected a [Image 1]: text part in the modified payload");
const description = text!.slice("[Image 1]: ".length);
assert.equal(description, LONG_DESCRIPTION, "explicit 0 must disable truncation entirely");
assert.ok(!description.endsWith("…"), "explicit 0 must never add an ellipsis");
});
test("updateSettingsSchema rejects a value below the 100 floor that is not the explicit-0 sentinel", () => {
const parsed = updateSettingsSchema.safeParse({ modalityBridgeVisionMaxChars: 50 });
assert.equal(parsed.success, false, "50 is neither the 0 sentinel nor >= 100");
});