refactor(tools): use shared api-client for auth headers

Replace duplicated getLocalAuth() + manual header construction in
finance/api.ts and web-search.ts with the shared getAuthHeaders()
and API_BASE_URL from hub/api-client.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yushen 2026-02-13 18:56:26 +08:00
parent f76312511d
commit 53cffe923b
2 changed files with 9 additions and 22 deletions

View file

@ -5,9 +5,8 @@
* All endpoints use GET with query parameters.
*/
import { getLocalAuth } from "../../../../hub/auth-store.js";
import { API_BASE_URL, getAuthHeaders } from "../../../../hub/api-client.js";
const BASE_URL = "https://api-dev.copilothub.ai";
const PATH_PREFIX = "/api/v1/financial";
const TIMEOUT_MS = 30_000;
@ -23,14 +22,9 @@ export async function financeFetch<T = Record<string, unknown>>(
params: Record<string, string | string[] | number | boolean | undefined>,
signal?: AbortSignal,
): Promise<{ data: T; url: string }> {
const auth = getLocalAuth();
if (!auth) {
throw new Error(
"Not logged in. Please sign in via the Desktop app to use financial data tools.",
);
}
const authHeaders = getAuthHeaders();
const url = new URL(PATH_PREFIX + path, BASE_URL);
const url = new URL(PATH_PREFIX + path, API_BASE_URL);
for (const [key, value] of Object.entries(params)) {
if (value === undefined || value === null) continue;
if (Array.isArray(value)) {
@ -51,9 +45,7 @@ export async function financeFetch<T = Record<string, unknown>>(
method: "GET",
headers: {
Accept: "application/json",
sid: auth.sid,
"device-id": auth.deviceId,
"os-type": "3",
...authHeaders,
},
signal: combinedSignal,
});

View file

@ -1,7 +1,7 @@
import { Type } from "@sinclair/typebox";
import type { AgentTool } from "@mariozechner/pi-agent-core";
import { getLocalAuth } from "../../../hub/auth-store.js";
import { API_BASE_URL, getAuthHeaders } from "../../../hub/api-client.js";
import {
DEFAULT_CACHE_TTL_MINUTES,
DEFAULT_TIMEOUT_SECONDS,
@ -14,7 +14,7 @@ import {
import type { CacheEntry } from "./cache.js";
import { jsonResult, readStringParam } from "./param-helpers.js";
const WEB_SEARCH_ENDPOINT = "https://api-dev.copilothub.ai/api/v1/web-search";
const WEB_SEARCH_PATH = "/api/v1/web-search";
const SEARCH_CACHE = new Map<string, CacheEntry<Record<string, unknown>>>();
@ -59,18 +59,13 @@ async function runDevvSearch(params: {
snippet: string;
}>;
}> {
const auth = getLocalAuth();
if (!auth) {
throw new Error("Not logged in. Please sign in via the Desktop app to use web search.");
}
const authHeaders = getAuthHeaders();
const res = await fetch(WEB_SEARCH_ENDPOINT, {
const res = await fetch(`${API_BASE_URL}${WEB_SEARCH_PATH}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
sid: auth.sid,
"device-id": auth.deviceId,
"os-type": "3",
...authHeaders,
},
body: JSON.stringify({ q: params.query }),
signal: withTimeout(undefined, params.timeoutSeconds * 1000),