mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host. Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean. Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
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"
|
|
);
|
|
});
|