Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(openclaw): wait for proxy before gateway routing

JerrettDavis621c3b6f43b1
diff --git a/plugins/openclaw/src/engine.ts b/plugins/openclaw/src/engine.ts
index c79e0b139..0d6439bbc 100644
--- a/plugins/openclaw/src/engine.ts
+++ b/plugins/openclaw/src/engine.ts
@@ -27,6 +27,7 @@ export class HeadroomContextEngine {
private proxyUrl: string | null = null;
private config: HeadroomEngineConfig;
private logger: ProxyManagerLogger;
+ private proxyReadyListeners = new Set<(proxyUrl: string) => void | Promise<void>>();
private stats = {
totalCompressions: 0,
totalTokensSaved: 0,
@@ -53,6 +54,7 @@ export class HeadroomContextEngine {
try {
this.proxyUrl = await this.proxyManager.start();
+ await this.notifyProxyReady(this.proxyUrl);
this.logger.info(`Engine bootstrapped (proxy: ${this.proxyUrl})`);
return { bootstrapped: true };
} catch (error) {
@@ -238,12 +240,26 @@ export class HeadroomContextEngine {
return this.proxyUrl;
}
+ onProxyReady(listener: (proxyUrl: string) => void | Promise<void>): () => void {
+ this.proxyReadyListeners.add(listener);
+ return () => {
+ this.proxyReadyListeners.delete(listener);
+ };
+ }
+
async ensureProxyUrl(): Promise<string> {
if (this.proxyUrl) {
return this.proxyUrl;
}
this.proxyUrl = await this.proxyManager.start();
+ await this.notifyProxyReady(this.proxyUrl);
return this.proxyUrl;
}
+
+ private async notifyProxyReady(proxyUrl: string): Promise<void> {
+ for (const listener of this.proxyReadyListeners) {
+ await listener(proxyUrl);
+ }
+ }
}
diff --git a/plugins/openclaw/src/plugin/index.ts b/plugins/openclaw/src/plugin/index.ts
index a8558a3d0..61c1639a1 100644
--- a/plugins/openclaw/src/plugin/index.ts
+++ b/plugins/openclaw/src/plugin/index.ts
@@ -40,14 +40,12 @@ export default function headroomPlugin(api: any) {
});
const gatewayProviderIds = resolveGatewayProviderIds(config);
- const ensureGatewayRouting = async () => {
+ const applyGatewayRouting = async (activeProxyUrl: string) => {
if (gatewayProviderIds.length === 0) {
return;
}
try {
- const activeProxyUrl = await engine.ensureProxyUrl();
-
const changed = applyGatewayProviderBaseUrlsInPlace(api.config, activeProxyUrl, gatewayProviderIds);
if (changed) {
@@ -64,6 +62,19 @@ export default function headroomPlugin(api: any) {
}
};
+ const ensureGatewayRouting = async () => {
+ const activeProxyUrl = engine.getProxyUrl();
+ if (!activeProxyUrl) {
+ logger.debug?.("[headroom] Deferring upstream gateway routing until proxy is available");
+ return;
+ }
+ await applyGatewayRouting(activeProxyUrl);
+ };
+
+ engine.onProxyReady(async (activeProxyUrl) => {
+ await applyGatewayRouting(activeProxyUrl);
+ });
+
// Register as context engine
api.registerContextEngine("headroom", () => engine);
diff --git a/plugins/openclaw/test/plugin-runtime-routing.test.ts b/plugins/openclaw/test/plugin-runtime-routing.test.ts
index 2e25e9fc0..6df99bd9e 100644
--- a/plugins/openclaw/test/plugin-runtime-routing.test.ts
+++ b/plugins/openclaw/test/plugin-runtime-routing.test.ts
@@ -2,14 +2,20 @@ import { afterEach, describe, expect, it, vi } from "vitest";
const mocked = vi.hoisted(() => ({
ensureProxyUrl: vi.fn(async () => "http://127.0.0.1:8787"),
- getProxyUrl: vi.fn(() => "http://127.0.0.1:8787"),
+ getProxyUrl: vi.fn(() => null as string | null),
createHeadroomRetrieveTool: vi.fn(({ proxyUrl }: { proxyUrl: string }) => ({ proxyUrl })),
}));
+const proxyReadyListeners: Array<(proxyUrl: string) => void | Promise<void>> = [];
+
vi.mock("../src/engine.js", () => ({
HeadroomContextEngine: class {
ensureProxyUrl = mocked.ensureProxyUrl;
getProxyUrl = mocked.getProxyUrl;
+ onProxyReady(listener: (proxyUrl: string) => void | Promise<void>) {
+ proxyReadyListeners.push(listener);
+ return () => {};
+ }
},
}));
@@ -23,10 +29,11 @@ afterEach(() => {
mocked.ensureProxyUrl.mockClear();
mocked.getProxyUrl.mockClear();
mocked.createHeadroomRetrieveTool.mockClear();
+ proxyReadyListeners.length = 0;
});
describe("headroomPlugin runtime routing", () => {
- it("routes configured providers in memory without writing config files", async () => {
+ it("routes configured providers in memory once the proxy becomes available", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
const writeConfigFile = vi.fn();
const loadConfig = vi.fn(() => ({
@@ -80,9 +87,13 @@ describe("headroomPlugin runtime routing", () => {
headroomPlugin(api);
await Promise.resolve();
- expect(mocked.ensureProxyUrl).toHaveBeenCalledTimes(1);
+ expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(writeConfigFile).not.toHaveBeenCalled();
expect(loadConfig).not.toHaveBeenCalled();
+ expect(api.config.models.providers["openai-codex"]).toBeUndefined();
+
+ await proxyReadyListeners[0]?.("http://127.0.0.1:8787");
+
expect(api.config.models.providers["openai-codex"]).toEqual({
baseUrl: "http://127.0.0.1:8787",
models: [],
@@ -102,5 +113,6 @@ describe("headroomPlugin runtime routing", () => {
await gatewayStart?.();
expect(writeConfigFile).not.toHaveBeenCalled();
expect(loadConfig).not.toHaveBeenCalled();
+ expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
});
});