87 lines
3.5 KiB
TypeScript
87 lines
3.5 KiB
TypeScript
import { CliError } from "../../core/errors.js";
|
|
import { redactSensitiveText } from "../../core/redact.js";
|
|
import type { LangfuseConfig } from "./config.js";
|
|
|
|
export type HttpQuery = Record<string, string | number | boolean | undefined>;
|
|
|
|
/**
|
|
* Read-only Langfuse Public API client.
|
|
*
|
|
* The embedded key pair is not scope-limited upstream (Langfuse project keys
|
|
* carry full API access), so "read-only" is a guarantee of THIS client: it
|
|
* exposes `getJson` only, issues `GET` exclusively, and has no method that can
|
|
* mutate remote state. Keep it that way — adding a POST/PUT/DELETE helper here
|
|
* breaks the documented trust boundary in SECURITY.md.
|
|
*
|
|
* Failures map to the CLI exit-code contract: 401/403 → 11 (authentication),
|
|
* any other upstream failure → 1 (upstream API failure). Messages pass through
|
|
* `redactSensitiveText` so the Basic credential can never leak via an error.
|
|
*/
|
|
export class LangfuseClient {
|
|
constructor(private readonly config: LangfuseConfig) {}
|
|
|
|
get baseUrl(): string {
|
|
return this.config.baseUrl;
|
|
}
|
|
|
|
getProjectId(): string | undefined {
|
|
return this.config.projectId;
|
|
}
|
|
|
|
async getJson<T>(path: string, query: HttpQuery = {}): Promise<T> {
|
|
const url = new URL(`${this.config.baseUrl}${path}`);
|
|
for (const [key, value] of Object.entries(query)) {
|
|
if (value !== undefined) url.searchParams.set(key, String(value));
|
|
}
|
|
|
|
const credential = Buffer.from(
|
|
`${this.config.publicKey}:${this.config.secretKey}`,
|
|
"utf8"
|
|
).toString("base64");
|
|
|
|
let response: Response;
|
|
try {
|
|
response = await fetch(url, {
|
|
method: "GET",
|
|
headers: {
|
|
Authorization: `Basic ${credential}`,
|
|
Accept: "application/json"
|
|
},
|
|
signal: AbortSignal.timeout(30_000)
|
|
});
|
|
} catch (error) {
|
|
const detail = redactSensitiveText(error instanceof Error ? error.message : String(error));
|
|
throw new CliError(`Langfuse API unreachable: ${detail}`, 1);
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const body = redactSensitiveText(await response.text());
|
|
const detail = `Langfuse API ${response.status} ${response.statusText}: ${body.slice(0, 500)}`;
|
|
if (response.status === 401 || response.status === 403) {
|
|
throw new CliError(detail, 11, "检查注入的 Langfuse 密钥是否仍然有效");
|
|
}
|
|
if (response.status === 404) {
|
|
const isProjectMismatch = body.includes("authorized project") || body.includes("not found");
|
|
const suggestion = isProjectMismatch
|
|
? "当前使用内置生产 Langfuse 凭据。若单据来自测试或 Staging 环境(如 staging.sealai.cc),其 Trace 属于 Staging 项目,无法通过内置生产凭据查询。可设置 LANGFUSE_BASE_URL、LANGFUSE_PUBLIC_KEY、LANGFUSE_SECRET_KEY 环境变量临时覆盖凭据。"
|
|
: "确认 Trace ID / Session ID 是否正确,或已过生命周期保留期";
|
|
throw new CliError(detail, 1, suggestion);
|
|
}
|
|
throw new CliError(detail, 1);
|
|
}
|
|
|
|
// A 200 that is not JSON (captive portal, HTML error page from a proxy)
|
|
// must degrade like any other upstream failure — exit 1, never a raw
|
|
// SyntaxError bubbling up as a misleading "invalid config" exit 13.
|
|
try {
|
|
return (await response.json()) as T;
|
|
} catch {
|
|
throw new CliError(
|
|
"Langfuse API returned a non-JSON response (upstream or network proxy problem).",
|
|
1,
|
|
"用 seal-use langfuse check 复测连通性,或改用 Langfuse Web 后台"
|
|
);
|
|
}
|
|
}
|
|
}
|