From b8aa5ad7e686348c9bdb5fbefaeea73e7d4544b9 Mon Sep 17 00:00:00 2001 From: yushen Date: Sat, 14 Feb 2026 07:30:36 +0800 Subject: [PATCH] refactor(core): convert MULTICA_API_URL constant to getApiBaseUrl() function Lazy-read process.env at call time instead of module import time. This ensures the env bridge in the Electron main process has time to set process.env.MULTICA_API_URL before the first API request. Co-Authored-By: Claude Opus 4.6 --- packages/core/src/agent/tools/data/finance/api.ts | 4 ++-- packages/core/src/agent/tools/web/web-search.ts | 4 ++-- packages/core/src/hub/api-client.ts | 4 +++- packages/core/src/hub/index.ts | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/src/agent/tools/data/finance/api.ts b/packages/core/src/agent/tools/data/finance/api.ts index ab0a3a22..15004438 100644 --- a/packages/core/src/agent/tools/data/finance/api.ts +++ b/packages/core/src/agent/tools/data/finance/api.ts @@ -5,7 +5,7 @@ * All endpoints use GET with query parameters. */ -import { API_BASE_URL, getAuthHeaders } from "../../../../hub/api-client.js"; +import { getApiBaseUrl, getAuthHeaders } from "../../../../hub/api-client.js"; const PATH_PREFIX = "/api/v1/financial"; const TIMEOUT_MS = 30_000; @@ -24,7 +24,7 @@ export async function financeFetch>( ): Promise<{ data: T; url: string }> { const authHeaders = getAuthHeaders("to use financial data tools"); - const url = new URL(PATH_PREFIX + path, API_BASE_URL); + const url = new URL(PATH_PREFIX + path, getApiBaseUrl()); for (const [key, value] of Object.entries(params)) { if (value === undefined || value === null) continue; if (Array.isArray(value)) { diff --git a/packages/core/src/agent/tools/web/web-search.ts b/packages/core/src/agent/tools/web/web-search.ts index 67558acc..5d2bece1 100644 --- a/packages/core/src/agent/tools/web/web-search.ts +++ b/packages/core/src/agent/tools/web/web-search.ts @@ -1,7 +1,7 @@ import { Type } from "@sinclair/typebox"; import type { AgentTool } from "@mariozechner/pi-agent-core"; -import { API_BASE_URL, getAuthHeaders } from "../../../hub/api-client.js"; +import { getApiBaseUrl, getAuthHeaders } from "../../../hub/api-client.js"; import { DEFAULT_CACHE_TTL_MINUTES, DEFAULT_TIMEOUT_SECONDS, @@ -61,7 +61,7 @@ async function runDevvSearch(params: { }> { const authHeaders = getAuthHeaders("to use web search"); - const res = await fetch(`${API_BASE_URL}${WEB_SEARCH_PATH}`, { + const res = await fetch(`${getApiBaseUrl()}${WEB_SEARCH_PATH}`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/packages/core/src/hub/api-client.ts b/packages/core/src/hub/api-client.ts index 7b54cf07..c5888b5b 100644 --- a/packages/core/src/hub/api-client.ts +++ b/packages/core/src/hub/api-client.ts @@ -1,6 +1,8 @@ import { getLocalAuth } from "./auth-store.js"; -export const API_BASE_URL = "https://api.multica.ai"; +export function getApiBaseUrl(): string { + return process.env.MULTICA_API_URL || "https://api.multica.ai"; +} /** * Return auth headers for the proxy API. diff --git a/packages/core/src/hub/index.ts b/packages/core/src/hub/index.ts index 93c15b7a..a15a27f6 100644 --- a/packages/core/src/hub/index.ts +++ b/packages/core/src/hub/index.ts @@ -2,5 +2,5 @@ export { Hub } from "./hub.js"; export type { MessageSource, InboundMessageEvent } from "./hub.js"; export { getHubId } from "./hub-identity.js"; export { getLocalAuth, type LocalAuthData } from "./auth-store.js"; -export { API_BASE_URL, getAuthHeaders } from "./api-client.js"; +export { getApiBaseUrl, getAuthHeaders } from "./api-client.js"; export type { HubOptions } from "./types.js";