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,72 @@
# @coze-arch/bot-space-api
bot space api instance that extracts from apps/bot/src/services/api/space-api.ts
## Overview
This package is part of the Coze Studio monorepo and provides api & networking functionality. It includes store, service, plugin and more.
## Getting Started
### Installation
Add this package to your `package.json`:
```json
{
"dependencies": {
"@coze-arch/bot-space-api": "workspace:*"
}
}
```
Then run:
```bash
rush update
```
### Usage
```typescript
import { /* exported functions/components */ } from '@coze-arch/bot-space-api';
// Example usage
// TODO: Add specific usage examples
```
## Features
- Store
- Service
- Plugin
- Api
- Validation
## API Reference
### Exports
- `type SpaceRequest<T> = Omit<T, 'space_id'>;`
- `const SpaceApi = spaceApiService;`
- `SpaceApiV2`
For detailed API documentation, please refer to the TypeScript definitions.
## Development
This package is built with:
- TypeScript
- Modern JavaScript
- Vitest for testing
- ESLint for code quality
## Contributing
This package is part of the Coze Studio monorepo. Please follow the monorepo contribution guidelines.
## License
Apache-2.0

View File

@@ -0,0 +1,94 @@
/*
* 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 { globalVars } from '@coze-arch/web-context';
import { useSpaceStore } from '@coze-arch/bot-studio-store';
import { CustomError } from '@coze-arch/bot-error';
import { DeveloperApi } from '@coze-arch/bot-api';
import { SpaceApi } from '../src/index';
vi.mock('@coze-arch/bot-api', () => ({
DeveloperApi: {
CreateWorkFlow: vi.fn(),
CreateWorkflowV2: vi.fn(),
WorkflowListV2: vi.fn(),
WorkFlowList: vi.fn(),
ExecuteDraftBot: vi.fn(),
QueryCardList: vi.fn(),
},
}));
vi.mock('@coze-arch/bot-studio-store', () => ({
useSpaceStore: {
getState: vi.fn().mockReturnValue({ getSpaceId: vi.fn() }),
},
}));
vi.mock('@coze-arch/report-events', () => ({ REPORT_EVENTS: { a: 'a' } }));
vi.mock('@coze-arch/web-context', () => ({
globalVars: { LAST_EXECUTE_ID: undefined },
}));
vi.mock('@coze-arch/bot-error', () => ({ CustomError: vi.fn() }));
vi.mock('axios', () => ({ default: { defaults: { transformResponse: [] } } }));
vi.mock('../src/space-api-v2', () => ({ SpaceApiV2: vi.fn() }));
describe('space api spec', () => {
vi.clearAllMocks();
it('should rewrite space id when calling ExecuteDraftBot', async () => {
useSpaceStore.getState().getSpaceId.mockReturnValue('test-id');
await SpaceApi.ExecuteDraftBot();
expect(globalVars.LAST_EXECUTE_ID).toBe(undefined);
expect(DeveloperApi.ExecuteDraftBot).toBeCalled();
expect(DeveloperApi.ExecuteDraftBot.mock.calls[0][0]).toMatchObject({
space_id: 'test-id',
});
const axiosConfig = DeveloperApi.ExecuteDraftBot.mock.calls[0][1];
expect(axiosConfig.transformResponse[0]).toBeTypeOf('function');
axiosConfig.transformResponse[0]({}, { 'x-tt-logid': 'test log id' });
expect(globalVars.LAST_EXECUTE_ID).toBe('test log id');
});
it('should call developer api directly', async () => {
useSpaceStore.getState().getSpaceId.mockReturnValue('test-id');
await SpaceApi.QueryCardList({});
expect(DeveloperApi.QueryCardList).toBeCalled();
expect(DeveloperApi.QueryCardList.mock.calls[0][0]).toMatchObject({
space_id: 'test-id',
});
});
it('should rename WorkflowList to WorkflowListV2', async () => {
useSpaceStore.getState().getSpaceId.mockReturnValue('test-id');
await SpaceApi.WorkFlowList({});
await SpaceApi.CreateWorkFlow({});
expect(DeveloperApi.WorkflowListV2).toBeCalled();
expect(DeveloperApi.WorkflowListV2.mock.calls[0][0]).toMatchObject({
space_id: 'test-id',
});
expect(DeveloperApi.CreateWorkflowV2).toBeCalled();
});
it('should throw custom error when calling not exits func', async () => {
await expect(() => SpaceApi['func not exits']()).toThrowError();
expect(CustomError).toBeCalled();
});
});

View File

@@ -0,0 +1,62 @@
/*
* 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 { useSpaceStore } from '@coze-arch/bot-studio-store';
import { CustomError } from '@coze-arch/bot-error';
import { PlaygroundApi } from '@coze-arch/bot-api';
import { SpaceApiV2 } from '../src/space-api-v2';
vi.mock('@coze-arch/bot-api', () => ({
DeveloperApi: {
InviteMemberLink: vi.fn(),
SearchMember: vi.fn(),
},
PlaygroundApi: {
InviteMemberLinkV2: vi.fn(),
SearchMemberV2: vi.fn(),
},
}));
vi.mock('@coze-arch/bot-studio-store', () => ({
useSpaceStore: {
getState: vi.fn().mockReturnValue({ getSpaceId: vi.fn() }),
},
}));
vi.mock('@coze-arch/report-events', () => ({ REPORT_EVENTS: { a: 'a' } }));
vi.mock('@coze-arch/bot-error', () => ({ CustomError: vi.fn() }));
vi.mock('axios', () => ({ default: { defaults: { transformResponse: [] } } }));
describe('space api spec', () => {
vi.clearAllMocks();
it('should rewrite space id when calling SearchMemberV2 and fg true', async () => {
useSpaceStore.getState().getSpaceId.mockReturnValue('test-id');
await SpaceApiV2.SearchMemberV2({ search_list: ['name'] });
expect(PlaygroundApi.SearchMemberV2).toBeCalled();
expect(PlaygroundApi.SearchMemberV2.mock.calls[0][0]).toMatchObject({
space_id: 'test-id',
});
});
it('should throw custom error when calling not exits func', async () => {
await expect(() => SpaceApiV2['func not exits']()).toThrowError();
expect(CustomError).toBeCalled();
});
});

View File

@@ -0,0 +1,12 @@
{
"operationSettings": [
{
"operationName": "test:cov",
"outputFolderNames": ["coverage"]
},
{
"operationName": "ts-check",
"outputFolderNames": ["./dist"]
}
]
}

View File

@@ -0,0 +1,7 @@
const { defineConfig } = require('@coze-arch/eslint-config');
module.exports = defineConfig({
packageRoot: __dirname,
preset: 'web',
rules: {},
});

View File

@@ -0,0 +1,36 @@
{
"name": "@coze-arch/bot-space-api",
"version": "0.0.1",
"description": "bot space api instance that extracts from apps/bot/src/services/api/space-api.ts",
"license": "Apache-2.0",
"author": "fanwenjie.fe@bytedance.com",
"maintainers": [],
"main": "src/index.ts",
"scripts": {
"build": "exit 0",
"lint": "eslint ./ --cache",
"test": "vitest --run --passWithNoTests",
"test:cov": "npm run test -- --coverage"
},
"dependencies": {
"@coze-arch/bot-error": "workspace:*",
"@coze-arch/bot-api": "workspace:*",
"@coze-arch/bot-flags": "workspace:*",
"@coze-arch/bot-studio-store": "workspace:*",
"@coze-arch/report-events": "workspace:*",
"@coze-arch/web-context": "workspace:*",
"axios": "^1.4.0"
},
"devDependencies": {
"@coze-arch/bot-typings": "workspace:*",
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@coze-arch/vitest-config": "workspace:*",
"@types/node": "^18",
"@vitest/coverage-v8": "~3.0.5",
"debug": "^4.3.4",
"vitest": "~3.0.5"
},
"// deps": "debug@^4.3.4 为脚本自动补齐,请勿改动"
}

View File

@@ -0,0 +1,24 @@
/*
* 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' />
interface Window {
/**
* tea 实例
*/
Tea?: any;
}

View File

@@ -0,0 +1,149 @@
/*
* 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 axios, { type AxiosRequestConfig } from 'axios';
import { globalVars } from '@coze-arch/web-context';
import { REPORT_EVENTS as ReportEventNames } from '@coze-arch/report-events';
import { useSpaceStore } from '@coze-arch/bot-studio-store';
import { CustomError } from '@coze-arch/bot-error';
import type DeveloperApiService from '@coze-arch/bot-api/developer_api';
import { DeveloperApi, type BotAPIRequestConfig } from '@coze-arch/bot-api';
export type SpaceRequest<T> = Omit<T, 'space_id'>;
type D = DeveloperApiService<BotAPIRequestConfig>;
// 这些是暴露出来需要被调用的函数列表
// 新增函数请在该列表后面追加即可
type ExportFunctions =
| 'GetPlaygroundPluginList'
| 'GetDraftBotList'
| 'WorkFlowList'
| 'CreateWorkFlow'
| 'CopyFromTemplate'
| 'DraftBotCreate'
| 'DuplicateDraftBot'
| 'GetDraftBotInfo'
| 'UpdateDraftBot'
| 'PublishDraftBot'
| 'ExecuteDraftBot'
| 'ListDraftBotHistory'
| 'RevertDraftBot'
| 'RegisterPlugin'
| 'RegisterPluginMeta'
| 'CreateDataSet'
| 'ListDateSet'
| 'DeleteDraftBot'
| 'GetPluginList'
| 'GetApiRespStruct'
| 'GetProfileMemory'
| 'WorkFlowPublish'
| 'RunWorkFlow'
| 'GetPluginCurrentInfo'
| 'GetTypeList'
| 'NodeList'
| 'GetWorkFlowProcess'
| 'MapData'
| 'SuggestPlugin'
| 'PublishConnectorList'
| 'UnBindConnector'
| 'BindConnector'
| 'UpdateNode'
| 'CreateChatflowAgent'
| 'CopyChatflowAgent'
| 'GetBotModuleInfo'
| 'CopyWorkflowV2'
| 'WorkflowListV2'
| 'QueryWorkflowV2'
| 'CreateWorkflowV2'
| 'PublishWorkflowV2'
| 'QueryCardDetail'
| 'QueryCardList'
| 'CreateCard'
| 'GetPluginCards'
| 'GetDraftBotDisplayInfo'
| 'UpdateDraftBotDisplayInfo'
| 'TaskList'
| 'GetBindConnectorConfig'
| 'SaveBindConnectorConfig'
| 'CommitDraftBot'
| 'CheckDraftBotCommit'
| 'GetCardRespStruct';
type ExportService = {
[K in ExportFunctions]: (
// 这里主要是为了 omit 掉 space_id 这个参数,而做的二次封装
params: SpaceRequest<Parameters<D[K]>[0]>,
options?: Parameters<D[K]>[1],
) => ReturnType<D[K]>;
};
const getSpaceId = () => useSpaceStore.getState().getSpaceId();
const spaceApiService = new Proxy(Object.create(null), {
get(_, funcName: ExportFunctions) {
const spaceId = getSpaceId();
if (!DeveloperApi[funcName]) {
throw new CustomError(
ReportEventNames.parmasValidation,
`Function ${funcName} is not defined in DeveloperApi`,
);
}
const externalConfig: AxiosRequestConfig = {};
switch (funcName) {
case 'ExecuteDraftBot': {
const defaults = axios.defaults?.transformResponse;
externalConfig.transformResponse = [].concat(
// @ts-expect-error -- linter-disable-autofix
...(Array.isArray(defaults) ? defaults : [defaults]),
(data, headers) => {
globalVars.LAST_EXECUTE_ID = headers['x-tt-logid'];
return data;
},
);
break;
}
case 'WorkFlowList':
funcName = 'WorkflowListV2';
break;
case 'CreateWorkFlow':
funcName = 'CreateWorkflowV2';
break;
default: {
break;
}
}
return <S extends keyof D>(
params: SpaceRequest<Parameters<D[S]>[0]>,
options: AxiosRequestConfig = {},
): Promise<ReturnType<D[S]>> =>
DeveloperApi[funcName](
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ ...params, space_id: spaceId } as any,
{
...externalConfig,
...options,
},
) as Promise<ReturnType<D[S]>>;
},
}) as ExportService;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const SpaceApi = spaceApiService;
export { SpaceApiV2 } from './space-api-v2';

View File

@@ -0,0 +1,85 @@
/*
* 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 AxiosRequestConfig } from 'axios';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { useSpaceStore } from '@coze-arch/bot-studio-store';
import { CustomError } from '@coze-arch/bot-error';
import {
type BotAPIRequestConfig,
PlaygroundApi,
type PlaygroundApiService,
} from '@coze-arch/bot-api';
const apiList = [
'InviteMemberLinkV2',
'AddBotSpaceMemberV2',
'SearchMemberV2',
'UpdateSpaceMemberV2',
'RemoveSpaceMemberV2',
'SpaceMemberDetailV2',
'DraftBotPublishHistoryDetail',
'BotInfoAudit',
'MGetBotByVersion',
];
type ApiType =
| 'InviteMemberLinkV2'
| 'AddBotSpaceMemberV2'
| 'SearchMemberV2'
| 'RemoveSpaceMemberV2'
| 'SpaceMemberDetailV2'
| 'UpdateSpaceMemberV2'
| 'DraftBotPublishHistoryDetail'
| 'BotInfoAudit'
| 'MGetBotByVersion';
export type SpaceRequest<T> = Omit<T, 'space_id'>;
type D = PlaygroundApiService<BotAPIRequestConfig>;
type ExportSpaceService = {
[K in ApiType]: (
params: SpaceRequest<Parameters<D[K]>[0]>,
options?: Parameters<D[K]>[1],
) => ReturnType<D[K]>;
};
const getSpaceId = () => useSpaceStore.getState().getSpaceId();
// 需要注入store space id的api
// eslint-disable-next-line @typescript-eslint/naming-convention
export const SpaceApiV2 = new Proxy(Object.create(null), {
get(_, funcName: ApiType) {
const spaceId = getSpaceId();
if (!apiList.includes(funcName)) {
throw new CustomError(
REPORT_EVENTS.parmasValidation,
`Function ${funcName} is not defined in replace list`,
);
}
return <S extends keyof D>(
params: SpaceRequest<Parameters<D[S]>[0]>,
options: AxiosRequestConfig = {},
): Promise<ReturnType<D[S]>> =>
PlaygroundApi[funcName](
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ space_id: spaceId, ...params } as any,
options,
) as Promise<ReturnType<D[S]>>;
},
}) as ExportSpaceService;

View File

@@ -0,0 +1,44 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"compilerOptions": {
"types": [],
"strictNullChecks": true,
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"references": [
{
"path": "../bot-api/tsconfig.build.json"
},
{
"path": "../bot-error/tsconfig.build.json"
},
{
"path": "../bot-flags/tsconfig.build.json"
},
{
"path": "../bot-store/tsconfig.build.json"
},
{
"path": "../bot-typings/tsconfig.build.json"
},
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/tsconfig.build.json"
},
{
"path": "../report-events/tsconfig.build.json"
},
{
"path": "../web-context/tsconfig.build.json"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "./tsconfig.build.json"
},
{
"path": "./tsconfig.misc.json"
}
],
"exclude": ["**/*"]
}

View File

@@ -0,0 +1,17 @@
{
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["__tests__", "vitest.config.ts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"rootDir": "./",
"outDir": "./dist",
"types": ["vitest/globals"],
"strictNullChecks": true
}
}

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.
*/
import { defineConfig } from '@coze-arch/vitest-config';
export default defineConfig({
dirname: __dirname,
preset: 'web',
test: {
coverage: {
all: true,
},
},
});