Add openrouter attribution headers
This commit is contained in:
parent
17287bc8d0
commit
74039fc0f1
1 changed files with 27 additions and 0 deletions
|
|
@ -4,6 +4,11 @@ import { streamSimple } from "@mariozechner/pi-ai";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { log } from "./logger.js";
|
import { log } from "./logger.js";
|
||||||
|
|
||||||
|
const OPENROUTER_APP_HEADERS: Record<string, string> = {
|
||||||
|
"HTTP-Referer": "https://openclaw.ai",
|
||||||
|
"X-Title": "OpenClaw",
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve provider-specific extra params from model config.
|
* Resolve provider-specific extra params from model config.
|
||||||
* Used to pass through stream params like temperature/maxTokens.
|
* Used to pass through stream params like temperature/maxTokens.
|
||||||
|
|
@ -96,8 +101,25 @@ function createStreamFnWithExtraParams(
|
||||||
return wrappedStreamFn;
|
return wrappedStreamFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a streamFn wrapper that adds OpenRouter app attribution headers.
|
||||||
|
* These headers allow OpenClaw to appear on OpenRouter's leaderboard.
|
||||||
|
*/
|
||||||
|
function createOpenRouterHeadersWrapper(baseStreamFn: StreamFn | undefined): StreamFn {
|
||||||
|
const underlying = baseStreamFn ?? streamSimple;
|
||||||
|
return (model, context, options) =>
|
||||||
|
underlying(model as Model<Api>, context, {
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
...OPENROUTER_APP_HEADERS,
|
||||||
|
...options?.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply extra params (like temperature) to an agent's streamFn.
|
* Apply extra params (like temperature) to an agent's streamFn.
|
||||||
|
* Also adds OpenRouter app attribution headers when using the OpenRouter provider.
|
||||||
*
|
*
|
||||||
* @internal Exported for testing
|
* @internal Exported for testing
|
||||||
*/
|
*/
|
||||||
|
|
@ -126,4 +148,9 @@ export function applyExtraParamsToAgent(
|
||||||
log.debug(`applying extraParams to agent streamFn for ${provider}/${modelId}`);
|
log.debug(`applying extraParams to agent streamFn for ${provider}/${modelId}`);
|
||||||
agent.streamFn = wrappedStreamFn;
|
agent.streamFn = wrappedStreamFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (provider === "openrouter") {
|
||||||
|
log.debug(`applying OpenRouter app attribution headers for ${provider}/${modelId}`);
|
||||||
|
agent.streamFn = createOpenRouterHeadersWrapper(agent.streamFn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue