feat(openrouter):add claw-x header (#213)

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
This commit is contained in:
Haze
2026-02-28 15:46:26 +08:00
committed by GitHub
Unverified
parent 6859656847
commit b4ef2bd51d
3 changed files with 163 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
/**
* Gateway fetch preload — loaded via NODE_OPTIONS --require before
* the OpenClaw Gateway starts.
*
* Patches globalThis.fetch so that every request whose URL contains
* "openrouter.ai" carries the ClawX app-attribution headers.
*
* The OpenAI SDK (used by OpenClaw) captures globalThis.fetch in its
* constructor, so patching here guarantees all SDK requests go through
* the interceptor.
*/
'use strict';
(function () {
var _f = globalThis.fetch;
if (typeof _f !== 'function') return;
if (globalThis.__clawxFetchPatched) return;
globalThis.__clawxFetchPatched = true;
globalThis.fetch = function clawxFetch(input, init) {
var url =
typeof input === 'string' ? input
: input && typeof input === 'object' && typeof input.url === 'string'
? input.url : '';
if (url.indexOf('openrouter.ai') !== -1) {
init = init ? Object.assign({}, init) : {};
var prev = init.headers;
var flat = {};
if (prev && typeof prev.forEach === 'function') {
prev.forEach(function (v, k) { flat[k] = v; });
} else if (prev && typeof prev === 'object') {
Object.assign(flat, prev);
}
delete flat['http-referer'];
delete flat['HTTP-Referer'];
delete flat['x-title'];
delete flat['X-Title'];
flat['HTTP-Referer'] = 'https://claw-x.com';
flat['X-Title'] = 'ClawX';
init.headers = flat;
}
return _f.call(globalThis, input, init);
};
})();