Files
GEOAgentArticleOptimizer/src/components/architecture/api-client.ts
T
2026-07-16 12:31:16 +08:00

45 lines
1.2 KiB
TypeScript

import type { LlmTraceManifest } from "../../lib/llm/trace-types";
function traceHeaders(apiAccessKey: string): Record<string, string> {
return apiAccessKey ? { "x-api-key": apiAccessKey } : {};
}
async function traceFetch<T>(path: string, apiAccessKey: string): Promise<T> {
const response = await fetch(path, {
credentials: "same-origin",
headers: traceHeaders(apiAccessKey),
cache: "no-store",
});
const body = await response.json().catch(() => ({})) as T & {
error?: string;
};
if (!response.ok) throw new Error(body.error ?? "读取后台追踪失败");
return body;
}
export function getLatestTrace(apiAccessKey: string) {
return traceFetch<LlmTraceManifest | { run: null; calls: [] }>(
"/api/llm-traces/latest",
apiAccessKey,
);
}
export function getJobTrace(jobId: string, apiAccessKey: string) {
return traceFetch<LlmTraceManifest>(
`/api/jobs/${encodeURIComponent(jobId)}/llm-trace`,
apiAccessKey,
);
}
export function getTracePayload(
jobId: string,
callId: string,
kind: "request" | "response",
apiAccessKey: string,
) {
return traceFetch<unknown>(
`/api/jobs/${encodeURIComponent(jobId)}/llm-trace/${encodeURIComponent(callId)}/${kind}`,
apiAccessKey,
);
}