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,131 @@
/*
* 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 {
type FC,
type PropsWithChildren,
createContext,
useContext,
useEffect,
useState,
useMemo,
} from 'react';
import EventEmitter from 'eventemitter3';
import { type BotMode } from '@coze-arch/bot-api/developer_api';
import { isValidContext } from '../utils/is-valid-context';
import { type IAbilityStoreState } from '../typings/store';
import { type IEventCenterEventName } from '../typings/scoped-events';
import { type Nullable } from '../typings/index';
import { type IEventCallbacks } from '../typings/event-callbacks';
import { type ToolAreaStore } from '../store/tool-area';
import { type AgentAreaStore } from '../store/agent-area';
import { AbilityStoreProvider } from '../hooks/store/use-ability-store-context';
import { useCreateStore } from '../hooks/builtin/use-create-store';
type IAbilityAreaContext = Nullable<{
store: {
useToolAreaStore: ToolAreaStore;
useAgentAreaStore: AgentAreaStore;
};
scopedEventBus: EventEmitter<IEventCenterEventName>;
eventCallbacks: Partial<IEventCallbacks>;
}>;
const DEFAULT_ABILITY_AREA: IAbilityAreaContext = {
store: null,
scopedEventBus: null,
eventCallbacks: null,
};
const AbilityAreaContext =
createContext<IAbilityAreaContext>(DEFAULT_ABILITY_AREA);
export const AbilityAreaContextProvider: FC<
PropsWithChildren<{
eventCallbacks?: Partial<IEventCallbacks>;
mode: BotMode;
modeSwitching: boolean;
isInit: boolean;
}>
> = ({ children, eventCallbacks = {}, mode, modeSwitching, isInit }) => {
const store = useCreateStore();
const scopedEventBus = useMemo(() => new EventEmitter<string>(), []);
const { useToolAreaStore, useAgentAreaStore } = store;
const clearAgentAreaStore = useAgentAreaStore(state => state.clearStore);
const {
updateIsInitialed,
updateIsModeSwitching,
clearStore: clearToolAreaStore,
} = useToolAreaStore.getState();
/**
* 清除
*/
useEffect(() => {
updateIsModeSwitching(modeSwitching);
if (modeSwitching || !isInit) {
return;
}
updateIsInitialed(true);
eventCallbacks?.onInitialed?.();
const cleanUp = () => {
updateIsInitialed(false);
eventCallbacks?.onDestroy?.();
clearToolAreaStore();
clearAgentAreaStore();
};
return cleanUp;
}, [mode, modeSwitching, isInit]);
return (
<AbilityAreaContext.Provider
value={{
store,
scopedEventBus,
eventCallbacks,
}}
>
<AbilityStore>{children}</AbilityStore>
</AbilityAreaContext.Provider>
);
};
const AbilityStore: FC<PropsWithChildren> = ({ children }) => {
const [state, setState] = useState<IAbilityStoreState>({});
return (
<AbilityStoreProvider state={state} setState={setState}>
{children}
</AbilityStoreProvider>
);
};
export const useAbilityAreaContext = () => {
const toolAreaContext = useContext(AbilityAreaContext);
if (!isValidContext(toolAreaContext)) {
throw new Error('toolAreaContext is not valid');
}
return toolAreaContext;
};

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.
*/
import {
type FC,
type PropsWithChildren,
createContext,
useContext,
} from 'react';
import {
type AbilityKey,
type AbilityScope,
} from '@coze-agent-ide/tool-config';
interface IAbilityConfigContext {
abilityKey?: AbilityKey;
scope?: AbilityScope;
}
const DEFAULT_ABILITY_CONFIG = {
abilityKey: undefined,
scope: undefined,
};
const AbilityConfigContext = createContext<IAbilityConfigContext>(
DEFAULT_ABILITY_CONFIG,
);
export const AbilityConfigContextProvider: FC<
PropsWithChildren<IAbilityConfigContext>
> = props => {
const { children, ...rest } = props;
return (
<AbilityConfigContext.Provider value={rest}>
{children}
</AbilityConfigContext.Provider>
);
};
export const useAbilityConfigContext = () => useContext(AbilityConfigContext);

View File

@@ -0,0 +1,51 @@
/*
* 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 {
type FC,
type PropsWithChildren,
createContext,
useContext,
} from 'react';
import { type AgentSkillKey } from '@coze-agent-ide/tool-config';
interface IAgentSkillConfigContext {
agentSkillKey?: AgentSkillKey;
}
const DEFAULT_AGENT_SKILL_CONFIG = {
agentSkillKey: undefined,
};
const AgentSkillConfigContext = createContext<IAgentSkillConfigContext>(
DEFAULT_AGENT_SKILL_CONFIG,
);
export const AgentSkillConfigContextProvider: FC<
PropsWithChildren<IAgentSkillConfigContext>
> = props => {
const { children, ...rest } = props;
return (
<AgentSkillConfigContext.Provider value={rest}>
{children}
</AgentSkillConfigContext.Provider>
);
};
export const useAgentSkillConfigContext = () =>
useContext(AgentSkillConfigContext);

View File

@@ -0,0 +1,56 @@
/*
* 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 {
type FC,
type PropsWithChildren,
createContext,
useContext,
} from 'react';
import { merge } from 'lodash-es';
export interface IPreferenceContext {
/**
* 是否开启Tool隐藏模式
*/
enableToolHiddenMode: boolean;
/**
* 是否只读状态
*/
isReadonly: boolean;
}
const DEFAULT_PREFERENCE: IPreferenceContext = {
enableToolHiddenMode: false,
isReadonly: false,
};
const PreferenceContext = createContext<IPreferenceContext>(DEFAULT_PREFERENCE);
export const PreferenceContextProvider: FC<
PropsWithChildren<Partial<IPreferenceContext>>
> = props => {
const { children, ...rest } = props;
return (
<PreferenceContext.Provider value={merge({}, DEFAULT_PREFERENCE, rest)}>
{children}
</PreferenceContext.Provider>
);
};
export const usePreference = () => useContext(PreferenceContext);

View File

@@ -0,0 +1,58 @@
/*
* 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 {
type FC,
type PropsWithChildren,
createContext,
useContext,
useState,
} from 'react';
import { merge } from 'lodash-es';
export interface IToolItemContext {
isForceShowAction: boolean;
setIsForceShowAction: (visible: boolean) => void;
}
const DEFAULT_TOOL_ITEM_CONTEXT: IToolItemContext = {
isForceShowAction: false,
setIsForceShowAction: (visible: boolean) => false,
};
const ToolItemContext = createContext<IToolItemContext>(
DEFAULT_TOOL_ITEM_CONTEXT,
);
export const ToolItemContextProvider: FC<PropsWithChildren> = props => {
const { children } = props;
const [_isForceShowAction, _setIsForceShowAction] = useState(false);
return (
<ToolItemContext.Provider
value={merge({}, DEFAULT_TOOL_ITEM_CONTEXT, {
isForceShowAction: _isForceShowAction,
setIsForceShowAction: _setIsForceShowAction,
})}
>
{children}
</ToolItemContext.Provider>
);
};
export const useToolItemContext = () => useContext(ToolItemContext);