mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-22 04:19:40 +00:00
fix(plugins): treat an in-place onRequest body rewrite as a rewrite
A hook that replaces a top-level body property in place and returns the same object was remembered as a pass-through, so a failover attempt that got its own top-level copy of the body skipped the hook and lost the rewrite. The gate now compares the body's top-level property references before and after the hook and only remembers an unchanged body. The changelog fragment states the cases that still call the hook again.
This commit is contained in:
@@ -1 +1 @@
|
||||
- **fix(plugins):** a plugin `onRequest` hook now runs once per client request, not once per account failover attempt, so request-counting plugins no longer count a failover twice. ([#20372](https://github.com/cheaper-inference/OmniRoute/pull/20372))
|
||||
- **fix(plugins):** a plugin `onRequest` hook that lets a request through unchanged is no longer called again when that request fails over to another account of the same provider and model, so request-counting plugins no longer count a failover twice. A block, a body rewrite (in place or a new body), a new provider or model, or a caller without a headers snapshot still calls the hook again. ([#20372](https://github.com/cheaper-inference/OmniRoute/pull/20372))
|
||||
|
||||
@@ -27,15 +27,32 @@ export type PluginRequestGateResult<TBody> =
|
||||
// client requests, not attempts. A plain pass-through is therefore remembered per client request
|
||||
// (the headers snapshot chat.ts builds once) and target. A block or a rewrite is never remembered:
|
||||
// the next attempt gets the plugin's answer again.
|
||||
// A hook that rewrites the body in place and returns the same object is a rewrite too: a
|
||||
// failover attempt can get its own top-level copy of the body (chat.ts copies it when it rewrites
|
||||
// the model), which would miss the edit. It is detected by comparing the body's top-level property
|
||||
// references before and after the hook (cheap, no serialization). Nested in-place edits are shared
|
||||
// with such a copy, so they still reach the next attempt.
|
||||
// shortcut: a caller with no headers snapshot is never remembered; upgrade if one needs it.
|
||||
const passedThrough = new WeakMap<object, Set<string>>();
|
||||
|
||||
function topLevelSnapshot(body: unknown): unknown[] | null {
|
||||
if (!body || typeof body !== "object") return null;
|
||||
return Object.entries(body as Record<string, unknown>).flat();
|
||||
}
|
||||
|
||||
function keepsTopLevel(before: unknown[] | null, body: unknown): boolean {
|
||||
const after = topLevelSnapshot(body);
|
||||
if (before === null || after === null) return before === after;
|
||||
return before.length === after.length && before.every((value, i) => Object.is(value, after[i]));
|
||||
}
|
||||
|
||||
export async function runPluginRequestGate<TBody>(
|
||||
args: Omit<Parameters<typeof runPluginOnRequestHook>[0], "body"> & { body: TBody }
|
||||
): Promise<PluginRequestGateResult<TBody>> {
|
||||
const owner = args.headers ?? null;
|
||||
const key = `${args.provider}\u0000${args.model}`;
|
||||
if (owner && passedThrough.get(owner)?.has(key)) return { blocked: false, body: args.body };
|
||||
const before = owner ? topLevelSnapshot(args.body) : null;
|
||||
const pluginGate = await runPluginOnRequestHook(args);
|
||||
if (pluginGate.blocked === true) {
|
||||
return {
|
||||
@@ -57,6 +74,8 @@ export async function runPluginRequestGate<TBody>(
|
||||
if (pluginGate.body && pluginGate.body !== args.body) {
|
||||
return { blocked: false, body: pluginGate.body };
|
||||
}
|
||||
if (owner) passedThrough.set(owner, (passedThrough.get(owner) ?? new Set()).add(key));
|
||||
if (owner && keepsTopLevel(before, args.body)) {
|
||||
passedThrough.set(owner, (passedThrough.get(owner) ?? new Set()).add(key));
|
||||
}
|
||||
return { blocked: false, body: args.body };
|
||||
}
|
||||
|
||||
@@ -180,3 +180,24 @@ test("a body-rewriting hook is asked again on every call", async () => {
|
||||
assert.deepEqual(second.body, first.body);
|
||||
}
|
||||
});
|
||||
|
||||
test("a hook that rewrites the body in place and returns the same object is asked again", async () => {
|
||||
const hook = { calls: 0 };
|
||||
registerHook("onRequest", PLUGIN, (ctx: { body: { messages?: unknown[] } }) => {
|
||||
hook.calls += 1;
|
||||
ctx.body.messages = [{ role: "user", content: "rewritten-in-place" }];
|
||||
return { body: ctx.body };
|
||||
});
|
||||
const headers = { "x-test": "1" };
|
||||
await runPluginRequestGate(gateArgs(headers));
|
||||
// The failover attempt gets its own top-level body object: the in-place rewrite of the first
|
||||
// attempt is not on it, so the hook must run again.
|
||||
const second = await runPluginRequestGate(gateArgs(headers));
|
||||
assert.equal(hook.calls, 2);
|
||||
assert.equal(second.blocked, false);
|
||||
if (!second.blocked) {
|
||||
assert.deepEqual((second.body as { messages: unknown[] }).messages, [
|
||||
{ role: "user", content: "rewritten-in-place" },
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user