|
|
@@ -1,179 +0,0 @@
|
|
|
-import { isZhaoshangStagingVariant } from "@/utils/llm-model";
|
|
|
-
|
|
|
-export const ASSISTANT_STATISTICS_VISITOR_ID_KEY = "assistant_statistics_visitor_id";
|
|
|
-
|
|
|
-const ASSISTANT_STATISTICS_PAGE_VIEW_URL = "/assistant_statistics/page_view";
|
|
|
-
|
|
|
-const BUSINESS_CODE_BY_POLICY_MODE: Record<string, string> = {
|
|
|
- zhaoshang: "business_assistant",
|
|
|
- zhaoshang_staging: "business_assistant",
|
|
|
- zhaoshang_safe: "business_assistant",
|
|
|
-};
|
|
|
-
|
|
|
-const STATISTICS_ENV_BY_MODE: Record<string, string> = {
|
|
|
- qingpu: "release",
|
|
|
- production: "test",
|
|
|
-};
|
|
|
-
|
|
|
-type Fetcher = typeof fetch;
|
|
|
-
|
|
|
-type RecordAssistantPageViewOptions = {
|
|
|
- apiBaseUrl?: string;
|
|
|
- fetcher?: Fetcher;
|
|
|
- idFactory?: () => string;
|
|
|
- mode?: string;
|
|
|
- policyMode?: string;
|
|
|
-};
|
|
|
-
|
|
|
-export function resolveAssistantBusinessCode(policyMode?: string | null) {
|
|
|
- if (!policyMode) {
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- // 兼容 A/B 测试用的 staging 变体:zhaoshang_staging_1, zhaoshang_staging_2, ...
|
|
|
- if (isZhaoshangStagingVariant(policyMode)) {
|
|
|
- return "business_assistant";
|
|
|
- }
|
|
|
-
|
|
|
- return BUSINESS_CODE_BY_POLICY_MODE[policyMode] || "";
|
|
|
-}
|
|
|
-
|
|
|
-export function resolveAssistantStatisticsEnv(mode?: string | null) {
|
|
|
- return STATISTICS_ENV_BY_MODE[mode || ""] || "test";
|
|
|
-}
|
|
|
-
|
|
|
-function defaultIdFactory() {
|
|
|
- const cryptoRef = globalThis.crypto;
|
|
|
- if (cryptoRef && typeof cryptoRef.randomUUID === "function") {
|
|
|
- return cryptoRef.randomUUID();
|
|
|
- }
|
|
|
-
|
|
|
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
|
|
|
- const random = Math.floor(Math.random() * 16);
|
|
|
- const value = char === "x" ? random : (random & 0x3) | 0x8;
|
|
|
- return value.toString(16);
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-export function getOrCreateAssistantVisitorId(idFactory = defaultIdFactory) {
|
|
|
- const storage = globalThis.localStorage;
|
|
|
- const storedVisitorId = storage?.getItem(ASSISTANT_STATISTICS_VISITOR_ID_KEY);
|
|
|
-
|
|
|
- if (storedVisitorId) {
|
|
|
- return storedVisitorId;
|
|
|
- }
|
|
|
-
|
|
|
- const visitorId = idFactory();
|
|
|
- storage?.setItem(ASSISTANT_STATISTICS_VISITOR_ID_KEY, visitorId);
|
|
|
- return visitorId;
|
|
|
-}
|
|
|
-
|
|
|
-function resolvePageViewUrl(apiBaseUrl?: string) {
|
|
|
- const baseUrl = import.meta.env.VITE_ASSISTANT_STATISTICS_BASE_URL || apiBaseUrl || "";
|
|
|
- return `${baseUrl.replace(/\/$/, "")}${ASSISTANT_STATISTICS_PAGE_VIEW_URL}`;
|
|
|
-}
|
|
|
-
|
|
|
-export async function recordAssistantPageView(options: RecordAssistantPageViewOptions = {}) {
|
|
|
- const businessCode = resolveAssistantBusinessCode(options.policyMode);
|
|
|
-
|
|
|
- if (!businessCode) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- const fetcher = options.fetcher || globalThis.fetch;
|
|
|
- if (typeof fetcher !== "function") {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- const token = (globalThis as typeof globalThis & { token?: string }).token || "";
|
|
|
-
|
|
|
- const source = (globalThis as any).source;
|
|
|
- const body: Record<string, unknown> = {
|
|
|
- visitor_id: getOrCreateAssistantVisitorId(options.idFactory),
|
|
|
- business_code: businessCode,
|
|
|
- env: resolveAssistantStatisticsEnv(options.mode),
|
|
|
- };
|
|
|
- if (source) {
|
|
|
- body.source = source;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- const response = await fetcher(resolvePageViewUrl(options.apiBaseUrl), {
|
|
|
- method: "POST",
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- Authorization: token,
|
|
|
- },
|
|
|
- body: JSON.stringify(body),
|
|
|
- });
|
|
|
-
|
|
|
- return response.ok;
|
|
|
- } catch (error) {
|
|
|
- console.warn("record assistant page view failed", error);
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-type AssistantFeedbackPayload = {
|
|
|
- record_id: string;
|
|
|
- status: number;
|
|
|
-};
|
|
|
-
|
|
|
-export async function reportAssistantFeedback(
|
|
|
- payload: AssistantFeedbackPayload,
|
|
|
- options: RecordAssistantPageViewOptions = {},
|
|
|
-) {
|
|
|
- const businessCode = resolveAssistantBusinessCode(options.policyMode);
|
|
|
-
|
|
|
- if (!businessCode) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- const fetcher = options.fetcher || globalThis.fetch;
|
|
|
- if (typeof fetcher !== "function") {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- const token = (globalThis as typeof globalThis & { token?: string }).token || "";
|
|
|
-
|
|
|
- const body: Record<string, unknown> = {
|
|
|
- ...payload,
|
|
|
- visitor_id: getOrCreateAssistantVisitorId(options.idFactory),
|
|
|
- business_code: businessCode,
|
|
|
- env: resolveAssistantStatisticsEnv(options.mode),
|
|
|
- };
|
|
|
-
|
|
|
- try {
|
|
|
- const baseUrl = import.meta.env.VITE_ASSISTANT_STATISTICS_BASE_URL || options.apiBaseUrl || "";
|
|
|
- const url = `${baseUrl.replace(/\/$/, "")}/assistant_statistics/feedback`;
|
|
|
-
|
|
|
- const response = await fetcher(url, {
|
|
|
- method: "POST",
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- Authorization: token,
|
|
|
- },
|
|
|
- body: JSON.stringify(body),
|
|
|
- });
|
|
|
-
|
|
|
- return response.ok;
|
|
|
- } catch (error) {
|
|
|
- console.warn("report assistant feedback failed", error);
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function recordCurrentAssistantPageView(
|
|
|
- options: Omit<RecordAssistantPageViewOptions, "mode" | "policyMode"> = {}
|
|
|
-) {
|
|
|
- const appGlobal = globalThis as typeof globalThis & {
|
|
|
- mode?: string;
|
|
|
- policyMode?: string;
|
|
|
- };
|
|
|
-
|
|
|
- return recordAssistantPageView({
|
|
|
- ...options,
|
|
|
- mode: appGlobal.mode,
|
|
|
- policyMode: appGlobal.policyMode,
|
|
|
- });
|
|
|
-}
|