feat(y-log): 问答按 LLM 调用各记一条 + 全量补齐各 API 操作埋点

- 问答日志从「每轮 onFinish 一条」改为 useThreadStream onLangChainEvent
  监听 on_chat_model_end,每次大模型调用结束各上报一条(含子代理)
- 广覆盖各 API client 成功路径:查看(view)/查询(search)/新增(create)/
  编辑(edit)/删除(delete)/启停(enabled·disabled)/导入导出,补齐此前
  整模块零埋点与漏掉的单条详情 GET;跳过高频 typeahead/轮询/bootstrap

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
AdministratorandClaude Opus 4.8 committed 2026-06-23 20:42:54 +08:00
1 parent 320f2ef814
commit df9c59e21c
35 files changed
+1569 -224

No files matched your search

+157 -55
View File
@@ -1,5 +1,6 @@
import { apiFetch } from "@/core/api";
import { getBackendBaseURL } from "@/core/config";
import { OptType, writeYLog } from "@/core/log/y-log";
/** A conversation thread owned by some user (admin view). */
export interface AdminThread {
@@ -172,22 +173,32 @@ async function parse<T>(res: Response, what: string): Promise<T> {
}
export async function listUserThreads(userId: string): Promise<AdminThread[]> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/threads?limit=200`),
"Failed to load threads",
);
const url = `${base()}/${encodeURIComponent(userId)}/threads?limit=200`;
const result = await parse<AdminThread[]>(await apiFetch(url), "Failed to load threads");
writeYLog({
optType: OptType.search,
optDetail: "B1-CM助手-管理后台-查询用户会话列表",
url,
reqParam: { userId },
returnParam: { count: result.length },
});
return result;
}
export async function listUserThreadMessages(
userId: string,
threadId: string,
): Promise<AdminMessage[]> {
return parse(
await apiFetch(
`${base()}/${encodeURIComponent(userId)}/threads/${encodeURIComponent(threadId)}/messages`,
),
"Failed to load messages",
);
const url = `${base()}/${encodeURIComponent(userId)}/threads/${encodeURIComponent(threadId)}/messages`;
const result = await parse<AdminMessage[]>(await apiFetch(url), "Failed to load messages");
writeYLog({
optType: OptType.view,
optDetail: "B1-CM助手-管理后台-查看会话消息",
url,
reqParam: { userId, threadId },
returnParam: { count: result.length },
});
return result;
}
export async function listUserQa(
@@ -200,19 +211,30 @@ export async function listUserQa(
});
const query = (params.q ?? "").trim();
if (query) search.set("q", query);
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/qa?${search}`),
"Failed to load user Q&A",
);
const url = `${base()}/${encodeURIComponent(userId)}/qa?${search}`;
const result = await parse<AdminUserQaPageResponse>(await apiFetch(url), "Failed to load user Q&A");
writeYLog({
optType: OptType.search,
optDetail: "B1-CM助手-管理后台-查询用户问答",
url,
reqParam: { userId, ...params },
returnParam: { total: result.total, count: result.items.length },
});
return result;
}
export async function getUserMemory(
userId: string,
): Promise<AdminMemoryResponse> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory`),
"Failed to load memory",
);
const url = `${base()}/${encodeURIComponent(userId)}/memory`;
const result = await parse<AdminMemoryResponse>(await apiFetch(url), "Failed to load memory");
writeYLog({
optType: OptType.view,
optDetail: "B1-CM助手-管理后台-查看用户记忆",
url,
reqParam: { userId },
});
return result;
}
export async function getUserLeaderboard(
@@ -229,10 +251,15 @@ export async function getUserLeaderboard(
until: range.until,
limit: String(limit),
});
return parse(
await apiFetch(`${base()}/leaderboard?${params}`),
"Failed to load leaderboard",
);
const url = `${base()}/leaderboard?${params}`;
const result = await parse<AdminLeaderboardResponse>(await apiFetch(url), "Failed to load leaderboard");
writeYLog({
optType: OptType.search,
optDetail: "B1-CM助手-管理后台-查询用户排行榜",
url,
reqParam: { ...range, limit },
});
return result;
}
/**
@@ -253,12 +280,18 @@ export async function refreshTodayLeaderboard(
until: range.until,
limit: String(limit),
});
return parse(
await apiFetch(`${base()}/leaderboard/refresh-today?${params}`, {
method: "POST",
}),
const url = `${base()}/leaderboard/refresh-today?${params}`;
const result = await parse<AdminLeaderboardResponse>(
await apiFetch(url, { method: "POST" }),
"Failed to refresh today's leaderboard",
);
writeYLog({
optType: OptType.other,
optDetail: "B1-CM助手-管理后台-刷新今日排行榜",
url,
reqParam: { ...range, limit },
});
return result;
}
export async function getUserAnalytics(
@@ -272,10 +305,15 @@ export async function getUserAnalytics(
until: range.until,
limit: String(limit),
});
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/analytics?${params}`),
"Failed to load user analytics",
);
const url = `${base()}/${encodeURIComponent(userId)}/analytics?${params}`;
const result = await parse<AdminUserAnalyticsResponse>(await apiFetch(url), "Failed to load user analytics");
writeYLog({
optType: OptType.view,
optDetail: "B1-CM助手-管理后台-查看用户分析",
url,
reqParam: { userId, ...range, limit },
});
return result;
}
export async function generateUserLeaderboardReport(
@@ -290,12 +328,19 @@ export async function generateUserLeaderboardReport(
limit: String(limit),
});
if (modelName) params.set("model_name", modelName);
return parse(
await apiFetch(`${base()}/leaderboard/report?${params}`, {
method: "POST",
}),
const url = `${base()}/leaderboard/report?${params}`;
const result = await parse<AdminLeaderboardReportResponse>(
await apiFetch(url, { method: "POST" }),
"Failed to generate leaderboard report",
);
writeYLog({
optType: OptType.other,
optDetail: "B1-CM助手-管理后台-生成排行榜报告",
url,
reqParam: { ...range, limit, modelName },
returnParam: { generated_at: result.generated_at, model_name: result.model_name },
});
return result;
}
function parseSseFrame(frame: string): AdminLeaderboardReportStreamEvent | null {
@@ -335,7 +380,8 @@ export async function streamUserLeaderboardReport({
limit: String(limit),
});
if (modelName) params.set("model_name", modelName);
const response = await apiFetch(`${base()}/leaderboard/report/stream?${params}`, {
const url = `${base()}/leaderboard/report/stream?${params}`;
const response = await apiFetch(url, {
method: "POST",
signal,
});
@@ -366,6 +412,12 @@ export async function streamUserLeaderboardReport({
const parsed = parseSseFrame(tail);
if (parsed) onEvent(parsed);
}
writeYLog({
optType: OptType.other,
optDetail: "B1-CM助手-管理后台-流式生成排行榜报告",
url,
reqParam: { ...range, limit, modelName },
});
}
export async function addAdminMemoryEntry(
@@ -373,14 +425,22 @@ export async function addAdminMemoryEntry(
content: string,
agent: string | null,
): Promise<AdminMemoryBucket> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory/entries`, {
const url = `${base()}/${encodeURIComponent(userId)}/memory/entries`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content, agent }),
}),
"Failed to add memory entry",
);
writeYLog({
optType: OptType.create,
optDetail: "B1-CM助手-管理后台-新增记忆条目",
url,
reqParam: { userId, content, agent },
});
return result;
}
export async function replaceAdminMemoryEntry(
@@ -389,14 +449,22 @@ export async function replaceAdminMemoryEntry(
content: string,
agent: string | null,
): Promise<AdminMemoryBucket> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory/entries`, {
const url = `${base()}/${encodeURIComponent(userId)}/memory/entries`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ old_text: oldText, content, agent }),
}),
"Failed to replace memory entry",
);
writeYLog({
optType: OptType.edit,
optDetail: "B1-CM助手-管理后台-编辑记忆条目",
url,
reqParam: { userId, old_text: oldText, content, agent },
});
return result;
}
export async function deleteAdminMemoryEntry(
@@ -406,13 +474,18 @@ export async function deleteAdminMemoryEntry(
): Promise<AdminMemoryBucket> {
const params = new URLSearchParams({ old_text: oldText });
if (agent) params.set("agent", agent);
return parse(
await apiFetch(
`${base()}/${encodeURIComponent(userId)}/memory/entries?${params}`,
{ method: "DELETE" },
),
const url = `${base()}/${encodeURIComponent(userId)}/memory/entries?${params}`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, { method: "DELETE" }),
"Failed to delete memory entry",
);
writeYLog({
optType: OptType.delete,
optDetail: "B1-CM助手-管理后台-删除记忆条目",
url,
reqParam: { userId, old_text: oldText, agent },
});
return result;
}
// ── V1 Memory CRUD ──────────────────────────────────────────────────────────
@@ -424,14 +497,22 @@ export async function updateAdminV1Section(
summary: string,
agent: string | null,
): Promise<AdminMemoryBucket> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory/v1/sections`, {
const url = `${base()}/${encodeURIComponent(userId)}/memory/v1/sections`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ section_type: sectionType, field, summary, agent }),
}),
"Failed to update V1 section",
);
writeYLog({
optType: OptType.edit,
optDetail: "B1-CM助手-管理后台-编辑记忆段落",
url,
reqParam: { userId, section_type: sectionType, field, summary, agent },
});
return result;
}
export async function addAdminV1Fact(
@@ -441,14 +522,22 @@ export async function addAdminV1Fact(
confidence: number,
agent: string | null,
): Promise<AdminMemoryBucket> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory/v1/facts`, {
const url = `${base()}/${encodeURIComponent(userId)}/memory/v1/facts`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content, category, confidence, agent }),
}),
"Failed to add V1 fact",
);
writeYLog({
optType: OptType.create,
optDetail: "B1-CM助手-管理后台-新增记忆事实",
url,
reqParam: { userId, content, category, confidence, agent },
});
return result;
}
export async function updateAdminV1Fact(
@@ -457,14 +546,22 @@ export async function updateAdminV1Fact(
updates: { content?: string; category?: string; confidence?: number },
agent: string | null,
): Promise<AdminMemoryBucket> {
return parse(
await apiFetch(`${base()}/${encodeURIComponent(userId)}/memory/v1/facts/${encodeURIComponent(factId)}`, {
const url = `${base()}/${encodeURIComponent(userId)}/memory/v1/facts/${encodeURIComponent(factId)}`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...updates, agent }),
}),
"Failed to update V1 fact",
);
writeYLog({
optType: OptType.edit,
optDetail: "B1-CM助手-管理后台-编辑记忆事实",
url,
reqParam: { userId, factId, ...updates, agent },
});
return result;
}
export async function deleteAdminV1Fact(
@@ -475,11 +572,16 @@ export async function deleteAdminV1Fact(
const params = new URLSearchParams();
if (agent) params.set("agent", agent);
const qs = params.toString() ? `?${params}` : "";
return parse(
await apiFetch(
`${base()}/${encodeURIComponent(userId)}/memory/v1/facts/${encodeURIComponent(factId)}${qs}`,
{ method: "DELETE" },
),
const url = `${base()}/${encodeURIComponent(userId)}/memory/v1/facts/${encodeURIComponent(factId)}${qs}`;
const result = await parse<AdminMemoryBucket>(
await apiFetch(url, { method: "DELETE" }),
"Failed to delete V1 fact",
);
writeYLog({
optType: OptType.delete,
optDetail: "B1-CM助手-管理后台-删除记忆事实",
url,
reqParam: { userId, factId, agent },
});
return result;
}