feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
/*
* 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.
*/
// extract from apps/bot/src/constant/app.ts
export enum BaseEnum {
Home = 'home', //首页
Explore = 'explore', //探索
Store = 'store', // 商店
Model = 'model', //模型竞技场
Space = 'space', //空间内
Workflow = 'work_flow', //兼容 workflow 编辑页
Invite = 'invite', // 邀请链接
Token = 'token', // token
Open = 'open', // 开放平台
PluginMockSet = 'plugin_mock_set',
Search = 'search', // 搜索
Premium = 'premium', // 订阅服务
User = 'user', // 个人主页
Enterprise = 'enterprise', // 企业管理
}
export enum SpaceAppEnum {
BOT = 'bot',
DOUYIN_BOT = 'douyin-bot',
DEVELOP = 'develop',
LIBRARY = 'library',
MODEL = 'model',
PLUGIN = 'plugin',
OCEAN_PROJECT = 'ocean-project',
WORKFLOW = 'workflow',
KNOWLEDGE = 'knowledge',
TEAM = 'team',
PERSONAL = 'personal',
WIDGET = 'widget',
EVALUATION = 'evaluation',
EVALUATE = 'evaluate',
SOCIAL_SCENE = 'social-scene',
IMAGEFLOW = 'imageflow',
DATABASE = 'database',
PROJECT_IDE = 'project-ide',
PUBLISH = 'publish',
}

View File

@@ -0,0 +1,20 @@
/*
* 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 { uniqueId } from 'lodash-es';
export const defaultConversationKey = -1;
export const defaultConversationUniqId = uniqueId();

View File

@@ -0,0 +1,30 @@
/*
* 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.
*/
// extract from apps/bot/src/constant/custom.ts
const enum CozeTokenInsufficientErrorCode {
WORKFLOW = '702095072',
BOT = '702082020',
}
/**
* Coze Token不足错误码
* 当出现该错误码的时候,需要额外进行停止拉流操作
*/
export const COZE_TOKEN_INSUFFICIENT_ERROR_CODE = [
CozeTokenInsufficientErrorCode.BOT,
CozeTokenInsufficientErrorCode.WORKFLOW,
];

View File

@@ -0,0 +1,113 @@
/*
* 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 EventEmitter from 'eventemitter3';
interface EventWithData<T extends EventEmitter.ValidEventTypes> {
event: EventEmitter.EventNames<T>;
args: Parameters<EventEmitter.EventListener<T, EventEmitter.EventNames<T>>>;
}
type ValidEventTypes = EventEmitter.ValidEventTypes;
export class GlobalEventBus<T extends ValidEventTypes> {
private eventEmitter = new EventEmitter<T>();
private started = true;
private buffer: EventWithData<T>[] = [];
private static instances = new Map<string, GlobalEventBus<ValidEventTypes>>();
static create<T extends ValidEventTypes>(key: string): GlobalEventBus<T> {
if (GlobalEventBus.instances.has(key)) {
return GlobalEventBus.instances.get(key) as unknown as GlobalEventBus<T>;
}
const instance = new GlobalEventBus<T>();
GlobalEventBus.instances.set(
key,
instance as unknown as GlobalEventBus<ValidEventTypes>,
);
return instance;
}
/**
* 触发事件
* @param event 事件名称
* @param args 参数
*/
emit<P extends EventEmitter.EventNames<T>>(
event: P,
...args: Parameters<EventEmitter.EventListener<T, P>>
) {
if (!this.started) {
this.buffer.push({
event,
args,
});
return;
}
this.eventEmitter.emit(event, ...args);
}
/**
* 订阅事件
* @param event 事件名称
* @param fn 事件回调
*/
on<P extends EventEmitter.EventNames<T>>(
event: P,
fn: EventEmitter.EventListener<T, P>,
) {
this.eventEmitter.on(event, fn);
}
/**
* 取消订阅事件
* @param event 事件名称
* @param fn 事件回调
*/
off<P extends EventEmitter.EventNames<T>>(
event: P,
fn: EventEmitter.EventListener<T, P>,
) {
this.eventEmitter.off(event, fn);
}
/**
* 开启缓存事件订阅器,开启时会将关闭时收到的事件对应的回调按顺序逐一触发
*/
start() {
this.started = true;
for (const { event, args } of this.buffer) {
this.emit(event, ...args);
}
}
/**
* 关闭缓存事件订阅器,在关闭时收到的事件会被缓存并延迟到下次开启时触发
*/
stop() {
this.started = false;
}
/**
* 清除缓存事件订阅器缓存的事件使得在重新开启start时不会触发在关闭stop时收到的事件对应的回调
*/
clear() {
this.buffer = [];
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.
*/
/* eslint-disable @typescript-eslint/naming-convention */
// 需要全局共享的值,可以提前到这里注册类型
interface GlobalVars {
/**
* Last Execute ID that extracts from apps/bot/src/store/bot-detail/utils/execute-draft-bot-request-id.ts
*
* 用于 debug 记录对话接口的 log id不需要响应式所以直接存 const 里了
*/
LAST_EXECUTE_ID: string;
[key: string | symbol]: unknown;
}
const createGlobalVarsStorage = () => {
const storage = new Map();
return new Proxy<GlobalVars>(Object.create(null), {
get<T extends keyof GlobalVars>(_: unknown, prop: T): GlobalVars[T] {
if (storage.has(prop)) {
return storage.get(prop as string);
}
// add more logic for dev mode
return undefined;
},
set<T extends keyof GlobalVars>(_: unknown, prop: T, value: GlobalVars[T]) {
storage.set(prop, value);
return true;
},
}) as GlobalVars;
};
/**
* 通用全局变量
*/
export const globalVars = createGlobalVarsStorage();

View File

@@ -0,0 +1,27 @@
/*
* 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.
*/
export { redirect } from './location';
export { GlobalEventBus } from './event-bus';
export { globalVars } from './global-var';
export { COZE_TOKEN_INSUFFICIENT_ERROR_CODE } from './const/custom';
export { BaseEnum, SpaceAppEnum } from './const/app';
// community bot store detail业务场景专用key值
export {
defaultConversationKey,
defaultConversationUniqId,
} from './const/community';

View File

@@ -0,0 +1,25 @@
/*
* 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.
*/
// The redirect function is designed to redirect the user to a new URL.
// It takes a single argument href which is a string representing the URL.
// Upon invocation, it sets location.href to the provided URL, thereby navigating to the webpage.
// While no validation logic is currently implemented prior to redirection,
// there is the potential for such checks to be included in the future as per your requirements.
export const redirect = (href: string) => {
// 这里后续可以补充一些校验逻辑
location.href = href;
};