mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-14 11:12:17 +03:00
* feat(sse): unified media-part detection helper (image+audio, input_image) * refactor(guardrails): extractImageParts/comboStructure delegate to unified media detector * fix(sse): media detector — audio parts no longer shadow sibling/nested image indicators * fix(guardrails): close extract↔replace contract for input_image (allowlist + splice) * perf(guardrails): skip media traversal when bridge disabled; short-circuit combo image check * feat(guardrails): in-memory LRU bridge cache (sha256 keyed) * feat(settings): modalityBridge* schema with legacy visionBridge* fallback * feat(db): migrate visionBridge* settings to modalityBridge* (idempotent) * refactor(guardrails): harden bridge cache key/config + settings resolution (review minors) * feat(guardrails): vision bridge mode selector (auto/describe/reroute) short-circuit * feat(guardrails): task-aware vision description prompt (default on) * feat(guardrails): describe-path cache integration * docs(guardrails): review polish — cache-key coupling notes + helper header * feat(guardrails): in-memory bridge stats + modality-bridge response header * feat(api): modality bridge stats endpoint + header wiring in chat handler * docs(guardrails): document modality bridge mode/task-aware/cache/header + stats endpoint * chore: untrack _tasks symlink (inherited from base tip; blocks pre-commit tracked-artifacts gate) * fix(db): renumber modality bridge migration 139->140 (base renumbered ccr_blocks to 139) * docs(guardrails): migration filename touch-up 139->140 * docs(db): stale comment touch-ups after 139->140 renumber and #9688 landing * fix(db): renumber modality bridge migration 140->141 (base renumbered connection_runtime_state to 140) * test(db): migration test titles 139->141 --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
103 lines
3.8 KiB
TypeScript
103 lines
3.8 KiB
TypeScript
/**
|
|
* Describe-path cache integration (Modality Bridge PR-1): the describe loop
|
|
* consults the shared BridgeCache (sha256 of contentRef+prompt+model) so the
|
|
* same image with the same prompt/model is described once per TTL. Failures
|
|
* are never cached. Opt-out via `modalityBridgeCacheEnabled: false`.
|
|
*
|
|
* The shared cache is PROCESS-WIDE — every test uses a unique image payload so
|
|
* tests cannot cross-contaminate each other's keys. Guardrail cases use
|
|
* `model: "auto/..."` + `mode: "describe"` so the flow is DB-free.
|
|
*/
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { VisionBridgeGuardrail } from "../../src/lib/guardrails/visionBridge.ts";
|
|
|
|
function cacheGuardrail(
|
|
settings: Record<string, unknown>,
|
|
counter: { calls: number },
|
|
behavior?: { failFirstCall?: boolean }
|
|
): InstanceType<typeof VisionBridgeGuardrail> {
|
|
return new VisionBridgeGuardrail({
|
|
deps: {
|
|
getSettings: async () => ({ modalityBridgeVisionMode: "describe", ...settings }),
|
|
callVisionModel: async () => {
|
|
counter.calls++;
|
|
if (behavior?.failFirstCall && counter.calls === 1) {
|
|
throw new Error("primeiro describe falhou");
|
|
}
|
|
return "uma descrição da imagem";
|
|
},
|
|
hasUsableCredentials: async () => null,
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Unique per-test payload — the test name lands inside the base64 content. */
|
|
function bodyWithImage(uniqueRef: string): Record<string, unknown> {
|
|
return {
|
|
model: "auto/describe-cache",
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{ type: "text", text: "o que há na imagem?" },
|
|
{
|
|
type: "image_url",
|
|
image_url: {
|
|
url: `data:image/png;base64,${Buffer.from(uniqueRef).toString("base64")}`,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
const context = { model: "auto/describe-cache", log: console };
|
|
|
|
test("same image+prompt+model described twice → single upstream call (cache hit)", async () => {
|
|
const counter = { calls: 0 };
|
|
const guardrail = cacheGuardrail({}, counter);
|
|
|
|
const first = await guardrail.preCall(bodyWithImage("cache-hit-test"), context);
|
|
assert.equal((first.meta ?? {}).imagesProcessed, 1);
|
|
assert.equal(counter.calls, 1);
|
|
|
|
const second = await guardrail.preCall(bodyWithImage("cache-hit-test"), context);
|
|
assert.equal((second.meta ?? {}).imagesProcessed, 1, "cached describe still replaces the image");
|
|
assert.equal(counter.calls, 1, "second identical request must be served from the cache");
|
|
|
|
const descriptions = (second.meta ?? {}).descriptions as string[];
|
|
assert.ok(
|
|
descriptions?.[0]?.includes("uma descrição da imagem"),
|
|
"cached description must be spliced into the payload"
|
|
);
|
|
});
|
|
|
|
test("modalityBridgeCacheEnabled=false → every request hits the vision model", async () => {
|
|
const counter = { calls: 0 };
|
|
const guardrail = cacheGuardrail({ modalityBridgeCacheEnabled: false }, counter);
|
|
|
|
await guardrail.preCall(bodyWithImage("cache-disabled-test"), context);
|
|
await guardrail.preCall(bodyWithImage("cache-disabled-test"), context);
|
|
assert.equal(counter.calls, 2, "disabled cache must not dedupe describe calls");
|
|
});
|
|
|
|
test("failed describe is NOT cached — the next request retries upstream", async () => {
|
|
const counter = { calls: 0 };
|
|
const guardrail = cacheGuardrail({}, counter, { failFirstCall: true });
|
|
|
|
await guardrail.preCall(bodyWithImage("failure-not-cached-test"), context);
|
|
assert.equal(counter.calls, 1);
|
|
|
|
const second = await guardrail.preCall(bodyWithImage("failure-not-cached-test"), context);
|
|
assert.equal(counter.calls, 2, "failure must not be cached; retry must reach upstream");
|
|
|
|
const descriptions = (second.meta ?? {}).descriptions as string[];
|
|
assert.ok(
|
|
descriptions?.[0]?.includes("uma descrição da imagem"),
|
|
"successful retry description must be used"
|
|
);
|
|
});
|