feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
17
frontend/packages/arch/bot-tea/src/global.d.ts
vendored
Normal file
17
frontend/packages/arch/bot-tea/src/global.d.ts
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/// <reference types='@coze-arch/bot-typings' />
|
||||
93
frontend/packages/arch/bot-tea/src/index.ts
Normal file
93
frontend/packages/arch/bot-tea/src/index.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import TeaNew, {
|
||||
type EVENT_NAMES,
|
||||
type UserGrowthEventParams,
|
||||
type ParamsTypeDefine,
|
||||
} from '@coze-arch/tea';
|
||||
import { logger } from '@coze-arch/logger';
|
||||
|
||||
export {
|
||||
EVENT_NAMES,
|
||||
AddWorkflowToStoreEntry,
|
||||
ExploreBotCardCommonParams,
|
||||
ShareRecallPageFrom,
|
||||
PluginMockSetCommonParams,
|
||||
SideNavClickCommonParams,
|
||||
AddPluginToStoreEntry,
|
||||
AddBotToStoreEntry,
|
||||
PublishAction,
|
||||
BotDetailPageAction,
|
||||
PluginPrivacyAction,
|
||||
PluginMockDataGenerateMode,
|
||||
ParamsTypeDefine,
|
||||
BotShareConversationClick,
|
||||
FlowStoreType,
|
||||
FlowResourceFrom,
|
||||
FlowDuplicateType,
|
||||
/** product event types */
|
||||
ProductEventSource,
|
||||
ProductEventFilterTag,
|
||||
ProductEventEntityType,
|
||||
ProductShowFrontParams,
|
||||
DocClickCommonParams,
|
||||
} from '@coze-arch/tea';
|
||||
|
||||
export const LANDING_PAGE_URL_KEY = 'coze_landing_page_url';
|
||||
|
||||
/**
|
||||
* UG 期望上报的 LandingPageUrl 是“网民最初点击到的页面完整 URL”,
|
||||
* 即使打开了新的页面,也应该上报第一次打开的落地页 URL
|
||||
*
|
||||
*/
|
||||
export const initBotLandingPageUrl = () => {
|
||||
const saved = window.sessionStorage.getItem(LANDING_PAGE_URL_KEY);
|
||||
if (!saved) {
|
||||
window.sessionStorage.setItem(LANDING_PAGE_URL_KEY, location.href);
|
||||
}
|
||||
};
|
||||
|
||||
export const getBotLandingPageUrl = () => {
|
||||
const saved = window.sessionStorage.getItem(LANDING_PAGE_URL_KEY);
|
||||
return saved ?? location.href;
|
||||
};
|
||||
|
||||
export const sendTeaEvent = <TEventName extends EVENT_NAMES>(
|
||||
event: TEventName,
|
||||
rawParams?: ParamsTypeDefine[TEventName],
|
||||
) => {
|
||||
let params = rawParams;
|
||||
if (FEATURE_ENABLE_TEA_UG) {
|
||||
const ugParams: UserGrowthEventParams = {
|
||||
LandingPageUrl: getBotLandingPageUrl(),
|
||||
// 与 UG 约定的 AppId,固定值
|
||||
AppId: 510023,
|
||||
EventName: event,
|
||||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- 秒时间戳
|
||||
EventTs: Math.floor(Date.now() / 1000),
|
||||
growth_deepevent: '4',
|
||||
};
|
||||
// @ts-expect-error -- UG 额外参数
|
||||
params = { ...ugParams, ...(rawParams ?? {}) };
|
||||
}
|
||||
logger.info({
|
||||
message: 'send-tea-event',
|
||||
meta: { event, params },
|
||||
});
|
||||
TeaNew.sendEvent(event, params);
|
||||
};
|
||||
|
||||
67
frontend/packages/arch/bot-tea/src/utils.ts
Normal file
67
frontend/packages/arch/bot-tea/src/utils.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import queryString from 'query-string';
|
||||
import { type EVENT_NAMES, type ParamsTypeDefine } from '@coze-arch/tea';
|
||||
import {
|
||||
ProductEntityType,
|
||||
type ProductInfo,
|
||||
} from '@coze-arch/bot-api/product_api';
|
||||
|
||||
export function convertTemplateType(
|
||||
entityType?: ProductEntityType,
|
||||
): ParamsTypeDefine[EVENT_NAMES.template_action_front]['template_type'] {
|
||||
switch (entityType) {
|
||||
case ProductEntityType.WorkflowTemplateV2:
|
||||
return 'workflow';
|
||||
case ProductEntityType.ImageflowTemplateV2:
|
||||
return 'imageflow';
|
||||
case ProductEntityType.BotTemplate:
|
||||
return 'bot';
|
||||
case ProductEntityType.ProjectTemplate:
|
||||
return 'project';
|
||||
default:
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
export function extractTemplateActionCommonParams(detail?: ProductInfo) {
|
||||
const queryParams = queryString.parse(location.search);
|
||||
const from = (queryParams?.from ?? '') as string;
|
||||
|
||||
return {
|
||||
template_id: detail?.meta_info.id || '',
|
||||
entity_id: detail?.meta_info.entity_id || '',
|
||||
template_name: detail?.meta_info.name || '',
|
||||
template_type: convertTemplateType(detail?.meta_info.entity_type),
|
||||
...(detail?.meta_info.entity_type === ProductEntityType.ProjectTemplate && {
|
||||
entity_copy_id: detail?.project_extra?.template_project_id,
|
||||
}),
|
||||
|
||||
template_tag_professional: detail?.meta_info.is_professional
|
||||
? 'professional'
|
||||
: 'basic',
|
||||
...(detail?.meta_info?.is_free
|
||||
? ({
|
||||
template_tag_prize: 'free',
|
||||
} as const)
|
||||
: ({
|
||||
template_tag_prize: 'paid',
|
||||
template_prize_detail: Number(detail?.meta_info?.price?.amount) || 0,
|
||||
} as const)),
|
||||
from,
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user