Files
deerflow-code/frontend-web/src/core/log/y-log-config.ts
T
AdministratorandClaude Opus 4.8 d2be3b206d feat(y-log): 操作日志上报(前端直连 + 后端公开只读下发地址)
- 后端新增 y_log 公开只读配置:YLogConfig + AppConfig.y_log +
  GET /api/public/config/y-log,地址写在 config.yaml(mtime 热重载)
- 前端核心 src/core/log/:writeYLog(双门控/异步即发即忘/永不抛错/
  去ip-host/对象序列化裁剪/optResult恒0)+ 配置加载 + token1 隔离存取
- 登录(token)持久化 token1 并报登录、登出先报后清;启动预热配置
- 各 API client 成功路径埋点:敏感词/菜单/按钮/技能地址/轻应用、
  会商草稿+任务草稿+后台任务、智能体/定时任务/知识库、Word导出,
  以及问答完成钩子(ChatPage/AgentChatPage onFinish)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 17:46:08 +08:00

69 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 操作日志(Y-Log)上报的运行时配置:从后端公开只读接口
* ``GET /api/public/config/y-log`` 读取(地址 + 开关在后端 ``config.yaml`` 的
* ``y_log`` 段,改完即时生效,前端无需重建)。
*
* 仿 [[task-deeplink-config]]:
* - ``fetchYLogConfig()`` 拉取并模块级缓存(启动早期预热一次即可)。
* - ``getCachedYLogConfig()`` 同步读缓存(``writeYLog`` 即发即忘,必须同步可用)。
*
* 拉取失败 → 用内置默认值兜底(``enabled:false``,但保留默认地址),绝不抛错。
*/
import { apiFetch } from "@/core/api/fetch-client";
import { getBackendBaseURL } from "@/core/config";
export interface YLogConfig {
/** 是否启用上报。 */
enabled: boolean;
/** 上报外部接口完整地址(POST)。 */
writeUrl: string;
}
/** 内置默认地址(后端拿不到 / 未配置时的兜底地址;enabled 默认 false 不上报)。 */
const DEFAULT_WRITE_URL = "https://ch1.b.uat.4.cn/api/y-log/write";
/** 内置默认配置(拉取失败兜底;默认不上报)。 */
export const DEFAULT_Y_LOG_CONFIG: YLogConfig = {
enabled: false,
writeUrl: DEFAULT_WRITE_URL,
};
/** 后端 snake_case 响应 → 前端 camelCase;缺字段回退默认值。 */
function normalize(body: unknown): YLogConfig {
const o = body && typeof body === "object" ? (body as Record<string, unknown>) : {};
const writeUrl =
typeof o.write_url === "string" && o.write_url.trim() ? o.write_url.trim() : DEFAULT_WRITE_URL;
return {
enabled: o.enabled === true,
writeUrl,
};
}
let cached: YLogConfig | null = null;
let inflight: Promise<YLogConfig> | null = null;
/** 拉取(带模块级缓存 + 并发去重);失败回退默认值。 */
export async function fetchYLogConfig(): Promise<YLogConfig> {
if (cached) return cached;
if (inflight) return inflight;
inflight = (async () => {
try {
const res = await apiFetch(`${getBackendBaseURL()}/api/public/config/y-log`);
if (!res.ok) throw new Error(`y-log config ${res.status}`);
cached = normalize(await res.json());
} catch {
cached = { ...DEFAULT_Y_LOG_CONFIG };
} finally {
inflight = null;
}
return cached;
})();
return inflight;
}
/** 同步读缓存(未拉取过则返回默认值,默认 enabled:false 不上报)。 */
export function getCachedYLogConfig(): YLogConfig {
return cached ?? DEFAULT_Y_LOG_CONFIG;
}