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,66 @@
# @coze-studio/tea-adapter
get default tea init class
## Overview
This package is part of the Coze Studio monorepo and provides architecture functionality. It includes sdk.
## Getting Started
### Installation
Add this package to your `package.json`:
```json
{
"dependencies": {
"@coze-studio/tea-adapter": "workspace:*"
}
}
```
Then run:
```bash
rush update
```
### Usage
```typescript
import { /* exported functions/components */ } from '@coze-studio/tea-adapter';
// Example usage
// TODO: Add specific usage examples
```
## Features
- Sdk
## API Reference
### Exports
- `default proxy as Tea;`
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,142 @@
/*
* 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 { describe, it, expect, beforeEach } from 'vitest';
import type {
EVENT_NAMES,
ParamsTypeDefine,
} from '@coze-studio/tea-interface/events';
import type { IConfigParam } from '@coze-studio/tea-interface';
import proxy, { type Tea } from '../src/index';
describe('Tea Adapter', () => {
let teaInstance: Tea;
beforeEach(() => {
// 先初始化 Tea 实例
(proxy as Tea).init({
autoStart: true,
});
teaInstance = proxy as Tea;
});
it('should be a proxy object', () => {
expect(proxy).toBeDefined();
expect(typeof proxy).toBe('function');
});
it('should trigger apply', () => {
const result = (proxy as any)('hello', 'world');
expect(result).toBe(undefined);
});
it('should have all required Tea interface methods', () => {
const methods = [
'getInstance',
'init',
'config',
'reStart',
'getConfig',
'event',
'start',
'stop',
'sendEvent',
'resetStayParams',
'checkInstance',
];
methods.forEach(method => {
expect(typeof (proxy as Tea)[method as keyof Tea]).toBe('function');
});
});
it('should have Collector property', () => {
expect(teaInstance).toBeDefined();
});
it('should return undefined for getConfig', () => {
expect(teaInstance).toBeDefined();
expect(teaInstance.getConfig('any-key')).toBeUndefined();
});
it('should not throw when calling methods', () => {
expect(teaInstance).toBeDefined();
const methods = [
'getInstance',
'reStart',
'start',
'stop',
'checkInstance',
] as const;
methods.forEach(method => {
expect(() => {
teaInstance[method]();
}).not.toThrow();
});
});
it('should handle init with partial params', () => {
expect(teaInstance).toBeDefined();
const initParam = {
autoStart: true,
};
expect(() => teaInstance.init(initParam)).not.toThrow();
});
it('should handle config with params', () => {
expect(teaInstance).toBeDefined();
const config: IConfigParam = {
// 添加必要的配置参数
};
expect(() => teaInstance.config(config)).not.toThrow();
});
it('should handle event with string name', () => {
expect(teaInstance).toBeDefined();
expect(() => teaInstance.event('test-event')).not.toThrow();
});
it('should handle event with EVENT_NAMES and params', () => {
expect(teaInstance).toBeDefined();
const eventName = 'test-event' as EVENT_NAMES;
const params = {} as ParamsTypeDefine[typeof eventName];
expect(() => teaInstance.event(eventName, params)).not.toThrow();
});
it('should handle sendEvent with type parameters', () => {
expect(teaInstance).toBeDefined();
const eventName = 'test-event' as EVENT_NAMES;
const params = {} as ParamsTypeDefine[typeof eventName];
expect(() => teaInstance.sendEvent(eventName, params)).not.toThrow();
});
it('should handle resetStayParams with all parameters', () => {
expect(teaInstance).toBeDefined();
expect(() =>
teaInstance.resetStayParams('/test', 'Test Title', 'https://test.com'),
).not.toThrow();
});
it('should be able to call sendEvent', () => {
expect(teaInstance).toBeDefined();
const eventName = 'test-event' as EVENT_NAMES;
const params = {} as ParamsTypeDefine[typeof eventName];
expect(() => teaInstance.sendEvent(eventName, params)).not.toThrow();
});
});

View File

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

View File

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

View File

@@ -0,0 +1,28 @@
{
"name": "@coze-studio/tea-adapter",
"version": "0.0.1",
"description": "get default tea init class",
"license": "Apache-2.0",
"author": "chenjiawei.inizio@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-studio/tea-interface": "workspace:*"
},
"devDependencies": {
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@coze-arch/vitest-config": "workspace:*",
"@types/node": "^18",
"@vitest/coverage-v8": "~3.0.5",
"sucrase": "^3.32.0",
"vitest": "~3.0.5"
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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/no-explicit-any */
import {
type EVENT_NAMES,
type ParamsTypeDefine,
} from '@coze-studio/tea-interface/events';
import {
type IInitParam as RawIInitParam,
type IConfigParam as IConfigParamCN,
type IConfigParamOversea,
type Collector,
} from '@coze-studio/tea-interface';
type IInitParam = RawIInitParam & {
autoStart?: boolean;
};
export interface Tea {
// eslint-disable-next-line @typescript-eslint/naming-convention
Collector: typeof Collector;
getInstance: () => Tea;
init: (initParam: Partial<IInitParam>) => Promise<void>;
config: (config: IConfigParamOversea | IConfigParamCN) => void;
reStart: () => void;
sdkReady: boolean;
getConfig: (key: string) => string | undefined;
event: (
event: string | EVENT_NAMES,
params?: unknown | ParamsTypeDefine[EVENT_NAMES],
) => void;
start: () => void;
stop: () => void;
sendEvent: <TEventName extends EVENT_NAMES>(
event: TEventName,
params?: ParamsTypeDefine[TEventName],
) => void;
resetStayParams: (pathname: string, title: string, href: string) => void;
checkInstance: () => void;
}
const noop = () => {
// do nothing
};
const mockTea = noop;
const proxyHandler = {
get(_target: any, prop, _receiver: any) {
return mockTea[prop] || noop;
},
apply(_target: any, _thisArg: any, argumentsList: unknown[]) {
return mockTea(...(argumentsList as Parameters<typeof mockTea>));
},
};
const proxy = new Proxy(function () {
// do nothing
}, proxyHandler);
export default proxy as Tea;

View File

@@ -0,0 +1,28 @@
{
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"module": "CommonJS",
"target": "ES2020",
"moduleResolution": "node",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"exclude": ["node_modules", "dist"],
"references": [
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/tsconfig.build.json"
},
{
"path": "../tea-interface/tsconfig.build.json"
}
]
}

View File

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

View File

@@ -0,0 +1,18 @@
{
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"rootDir": "./",
"outDir": "./dist",
"module": "CommonJS",
"target": "ES2020",
"moduleResolution": "node"
},
"include": ["__tests__", "vitest.config.ts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
]
}

View File

@@ -0,0 +1,22 @@
/*
* 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: 'node',
});