openclaw-abacusai-auth 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -570,6 +570,17 @@ async function handleProxyRequest(req: IncomingMessage, res: ServerResponse) {
570
570
 
571
571
  async function handleProxyRequestInner(req: IncomingMessage, res: ServerResponse) {
572
572
  const path = req.url ?? "/";
573
+
574
+ if (path === "/__kill") {
575
+ console.log("[abacusai] Received /__kill command, stopping zombie proxy...");
576
+ sendJsonResponse(res, 200, { success: true });
577
+ // Execute stop proxy asynchronously after sending response
578
+ setTimeout(() => {
579
+ stopProxy().catch(() => process.exit(0));
580
+ }, 100);
581
+ return;
582
+ }
583
+
573
584
  const target = `${ROUTELLM_BASE}${path}`;
574
585
  const headers: Record<string, string> = {
575
586
  Authorization: `Bearer ${proxyApiKey}`,
@@ -703,33 +714,50 @@ function startProxy(apiKey: string): Promise<void> {
703
714
  });
704
715
  });
705
716
 
706
- // Try fixed port first, then retry with port+1, +2, etc.
707
- const tryListen = (port: number, attempt: number) => {
717
+ let killAttempts = 0;
718
+ const tryListen = (port: number) => {
708
719
  proxyServer!.listen(port, PROXY_HOST, () => {
709
720
  proxyPort = port;
710
721
  console.log(`[abacusai] proxy listening on http://${PROXY_HOST}:${proxyPort}`);
711
722
  resolve();
712
723
  });
713
724
  proxyServer!.once("error", (err: NodeJS.ErrnoException) => {
714
- if (err.code === "EADDRINUSE" && attempt < 10) {
715
- console.log(`[abacusai] port ${port} in use, trying ${port + 1}...`);
725
+ if (err.code === "EADDRINUSE") {
726
+ console.log(`[abacusai] port ${port} in use. Attempting to kill zombie proxy...`);
727
+ killAttempts++;
728
+ if (killAttempts > 5) {
729
+ console.error("[abacusai] Could not kill zombie proxy after multiple attempts.");
730
+ reject(new Error("EADDRINUSE on port 18862 and cannot kill zombie proxy."));
731
+ return;
732
+ }
716
733
  proxyServer!.removeAllListeners("error");
717
- proxyServer!.close(() => {
734
+
735
+ // Try to kill the zombie proxy by sending it the /__kill command
736
+ const { request } = require("node:http");
737
+ const req = request(`http://${PROXY_HOST}:${port}/__kill`, { method: 'GET' }, (res: IncomingMessage) => {
738
+ res.resume();
739
+ });
740
+ req.on('error', () => { }); // Ignore network errors
741
+ req.end();
742
+
743
+ console.log(`[abacusai] Waiting 1s for port ${port} to free up...`);
744
+ setTimeout(() => {
745
+ // Create fresh proxyServer to avoid closed state issues
718
746
  proxyServer = createServer((req, res) => {
719
747
  handleProxyRequest(req, res).catch((e) => {
720
748
  console.error("[abacusai] proxy error:", e);
721
749
  sendJsonResponse(res, 500, { error: { message: String(e) } });
722
750
  });
723
751
  });
724
- tryListen(port + 1, attempt + 1);
725
- });
752
+ tryListen(port);
753
+ }, 1000);
726
754
  } else {
727
755
  reject(err);
728
756
  }
729
757
  });
730
758
  };
731
759
 
732
- tryListen(PROXY_PORT_DEFAULT, 0);
760
+ tryListen(PROXY_PORT_DEFAULT);
733
761
  });
734
762
  }
735
763
 
@@ -814,7 +842,7 @@ function buildModelDefinition(modelId: string) {
814
842
  * but the config still stores the port from when `openclaw models auth login`
815
843
  * was first run.
816
844
  */
817
- function updateBaseUrlInConfig(): void {
845
+ function updateBaseUrlInConfig(pluginApi: any): void {
818
846
  if (!proxyPort) return;
819
847
  const newBaseUrl = `http://${PROXY_HOST}:${proxyPort}`;
820
848
  try {
@@ -823,24 +851,30 @@ function updateBaseUrlInConfig(): void {
823
851
  process.env.CLAWDBOT_STATE_DIR ||
824
852
  join(homedir(), ".openclaw");
825
853
  const configPath = join(stateDir, "openclaw.json");
826
- if (!existsSync(configPath)) return;
827
-
828
- let raw = readFileSync(configPath, "utf-8");
829
- // Strip UTF-8 BOM if present
830
- if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1);
831
- const config = JSON.parse(raw);
832
- const currentUrl = config?.models?.providers?.abacusai?.baseUrl;
833
854
 
834
- if (currentUrl === newBaseUrl) {
835
- // Already up to date
836
- return;
855
+ // 1. Update in-memory OpenClaw config if available (so it works immediately and saves correctly)
856
+ let inMemoryUpdated = false;
857
+ if (pluginApi?.config?.models?.providers?.abacusai) {
858
+ if (pluginApi.config.models.providers.abacusai.baseUrl !== newBaseUrl) {
859
+ pluginApi.config.models.providers.abacusai.baseUrl = newBaseUrl;
860
+ inMemoryUpdated = true;
861
+ }
837
862
  }
838
863
 
839
- // Update the baseUrl
840
- if (config.models?.providers?.abacusai) {
841
- config.models.providers.abacusai.baseUrl = newBaseUrl;
842
- writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n", "utf-8");
843
- console.log(`[abacusai] Updated config baseUrl: ${currentUrl} → ${newBaseUrl}`);
864
+ // 2. Fallback to writing the disk file directly if needed
865
+ if (existsSync(configPath)) {
866
+ let raw = readFileSync(configPath, "utf-8");
867
+ if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1);
868
+ const config = JSON.parse(raw);
869
+ const currentUrl = config?.models?.providers?.abacusai?.baseUrl;
870
+
871
+ if (currentUrl !== newBaseUrl && config.models?.providers?.abacusai) {
872
+ config.models.providers.abacusai.baseUrl = newBaseUrl;
873
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n", "utf-8");
874
+ console.log(`[abacusai] Updated config baseUrl on disk: ${currentUrl} → ${newBaseUrl}`);
875
+ } else if (inMemoryUpdated) {
876
+ console.log(`[abacusai] Updated in-memory baseUrl to ${newBaseUrl}`);
877
+ }
844
878
  }
845
879
  } catch (err) {
846
880
  console.error("[abacusai] Failed to update baseUrl in config:", err);
@@ -913,7 +947,7 @@ const abacusaiPlugin = {
913
947
  // Update baseUrl in config to match the new proxy port
914
948
  // (The proxy gets a new random port each time the gateway starts,
915
949
  // but the config still has the port from when auth was first run)
916
- updateBaseUrlInConfig();
950
+ updateBaseUrlInConfig(pluginApi);
917
951
  })
918
952
  .catch((err) => {
919
953
  console.error("[abacusai] Failed to auto-start proxy:", err);
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openclaw-abacusai-auth",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "OpenClaw AbacusAI provider plugin - Third-party plugin for AbacusAI RouteLLM integration",
5
5
  "type": "module",
6
6
  "main": "index.ts",