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,202 @@
# @coze-arch/bot-env
> A TypeScript-based environment variable management system for the bot-studio monorepo, providing compile-time type safety and runtime environment configuration.
## Features
- 🔧 **Compile-time Environment Variables**: Access to environment variables with full TypeScript support
- 🏗️ **Multi-environment Support**: Handles different deployment environments (BOE, production, development)
- 🌍 **Multi-region Configuration**: Supports CN, SG, and VA regions with region-specific configurations
- 🎯 **Feature Flags**: Built-in feature toggle system for conditional functionality
- 📝 **Auto-generated Types**: Automatically generates TypeScript declarations from environment configuration
- 🔒 **Type Safety**: Full TypeScript support with strict typing for all environment variables
-**Runtime Environment Access**: Provides runtime environment detection and configuration
## Get Started
### Installation
```bash
# Install the package
rush add -p @coze-arch/bot-env
# Update dependencies
rush update
```
### Basic Usage
```typescript
// Import compile-time environment variables
import { GLOBAL_ENVS } from '@coze-arch/bot-env';
// Access environment variables with full type safety
console.log(GLOBAL_ENVS.REGION); // 'cn' | 'sg' | 'va'
console.log(GLOBAL_ENVS.IS_PROD); // boolean
console.log(GLOBAL_ENVS.BUILD_TYPE); // 'local' | 'online' | 'offline' | 'test'
// Import runtime environment utilities
import { runtimeEnv } from '@coze-arch/bot-env/runtime';
// Access runtime environment information
console.log(runtimeEnv.isPPE); // boolean
```
### TypeScript Declarations
```typescript
// Import TypeScript declarations
/// <reference types="@coze-arch/bot-env/typings" />
// All environment variables are available as global constants
declare const REGION: 'cn' | 'sg' | 'va';
declare const IS_PROD: boolean;
declare const FEATURE_ENABLE_SSO: boolean;
// ... and many more
```
## API Reference
### GLOBAL_ENVS
The main export containing all environment variables organized by category:
#### Base Environment Variables
- `BUILD_TYPE`: Build environment type (`'local' | 'online' | 'offline' | 'test'`)
- `CUSTOM_VERSION`: Version type (`'inhouse' | 'release'`)
- `REGION`: Deployment region (`'cn' | 'sg' | 'va'`)
- `NODE_ENV`: Node environment (`'production' | 'development' | 'test'`)
- `IS_PROD`: Production environment flag
- `IS_BOE`: BOE environment flag
- `IS_OVERSEA`: Overseas deployment flag
- `IS_RELEASE_VERSION`: Release version flag
#### Feature Flags
- `FEATURE_ENABLE_SSO`: Single Sign-On feature
- `FEATURE_ENABLE_APP_GUIDE`: Application guide feature
- `FEATURE_ENABLE_MSG_DEBUG`: Message debugging feature
- `FEATURE_AWEME_LOGIN`: Aweme login integration
- `FEATURE_GOOGLE_LOGIN`: Google login integration
- And many more feature toggles...
#### Configuration Variables
- `CDN`: Content delivery network URL
- `UPLOAD_CDN`: Upload CDN configuration
- `COZE_DOMAIN`: Coze service domain
- `APP_ID`: Application identifier
- `APP_KEY`: Application key
- Various service-specific configurations
### Runtime Environment
```typescript
import { runtimeEnv } from '@coze-arch/bot-env/runtime';
// Runtime environment detection
runtimeEnv.isPPE // boolean - Production-like environment detection
```
### Build Scripts
```typescript
import { build } from '@coze-arch/bot-env/build';
// Generate TypeScript declarations from environment configuration
build();
```
## Development
### Project Structure
```
src/
├── index.ts # Main exports
├── runtime/ # Runtime environment utilities
├── typings.d.ts # Auto-generated TypeScript declarations
└── global.d.ts # Global type definitions
scripts/
├── build.ts # Build script exports
└── index.ts # Build script runner
```
### Environment Configuration
This package acts as a wrapper around `@coze-studio/bot-env-adapter`, which provides the actual environment configuration logic. The adapter package includes:
- **Base configuration**: Core environment variables like region, build type, and deployment flags
- **Feature flags**: Toggle switches for various application features
- **Business configs**: Service-specific configuration values
- **Configuration helpers**: Utilities for environment-specific value extraction
### Auto-generated Types
The package automatically generates TypeScript declarations based on the environment configuration. The build process:
1. Analyzes the `envs` object in the source code
2. Extracts type information for each environment variable
3. Generates corresponding TypeScript declarations
4. Updates the `typings.d.ts` file with the latest types
### Environment-specific Configuration
Use the `extractEnvValue` utility for environment-specific configurations:
```typescript
const API_ENDPOINT = extractEnvValue<string>({
cn: {
boe: 'https://boe.api.example.com',
inhouse: 'https://inhouse.api.example.com',
release: 'https://api.example.com'
},
sg: {
inhouse: 'https://sg-inhouse.api.example.com',
release: 'https://sg.api.example.com'
},
va: {
release: 'https://va.api.example.com'
}
});
```
### Running Tests
```bash
# Run tests
rush test -t @coze-arch/bot-env
# Run tests with coverage
rush test:cov -t @coze-arch/bot-env
```
### Building
```bash
# Build the package
rush build -t @coze-arch/bot-env
# Generate type definitions
rush build:types -t @coze-arch/bot-env
```
## Dependencies
### Runtime Dependencies
- `@coze-studio/bot-env-adapter`: Core environment configuration adapter
### Development Dependencies
- `@coze-arch/eslint-config`: ESLint configuration
- `@coze-arch/ts-config`: TypeScript configuration
- `@coze-arch/vitest-config`: Vitest testing configuration
- `ts-morph`: TypeScript AST manipulation for type generation
- `sucrase`: Fast TypeScript/JSX compiler
- `vitest`: Testing framework
## License
Apache-2.0 License - see package.json for details.
---
For more information about environment configuration patterns and best practices, refer to the [bot-studio monorepo documentation](../../docs/).

View File

@@ -0,0 +1,16 @@
{
"operationSettings": [
{
"operationName": "build",
"outputFolderNames": ["dist"]
},
{
"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: 'node',
rules: {},
});

View File

@@ -0,0 +1,48 @@
{
"name": "@coze-arch/bot-env",
"version": "0.0.1",
"description": "compiler env vars for bot-studio",
"license": "Apache-2.0",
"author": "fanwenjie.fe@bytedance.com",
"maintainers": [],
"exports": {
".": "./src/index.ts",
"./build": "./scripts/build.ts",
"./typings": "./src/typings.d.ts",
"./runtime": "./src/runtime/index.ts"
},
"main": "src/index.ts",
"typesVersions": {
"*": {
"build": [
"./scripts/build.ts"
],
"typings": [
"./src/typings.ts"
],
"runtime": [
"./src/runtime/index.ts"
]
}
},
"scripts": {
"build": "node -r sucrase/register scripts/index.ts && tsc -b tsconfig.build.json",
"lint": "eslint ./ --cache",
"test": "vitest --run --passWithNoTests",
"test:cov": "npm run test -- --coverage"
},
"dependencies": {
"@coze-studio/bot-env-adapter": "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",
"ts-morph": "^20.0.0",
"vitest": "~3.0.5"
}
}

View 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.
*/
export { build } from '@coze-studio/bot-env-adapter/build';

View File

@@ -0,0 +1,19 @@
/*
* 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 { build } from '@coze-studio/bot-env-adapter/build';
build();

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.
*/
// copy from @byted/uploader
type TUploaderRegion =
| 'cn-north-1'
| 'us-east-1'
| 'ap-singapore-1'
| 'us-east-red'
| 'boe'
| 'boei18n'
| 'US-TTP'
| 'gcp';
interface Window {
gfdatav1?: {
// 部署区域
region?: string;
// SCM 版本
ver?: number | string;
// 当前环境, 取值为 boe 或 prod
env?: 'boe' | 'prod';
// 环境标识,如 prod 或 ppe_*
envName?: string;
// 当前的小流量频道 ID0 表示全流量
canary?: 0;
extra?: {
/**
* @description goofy 团队不建议依赖该字段,能不用则不用
* 1 表示小流量
* 3 表示灰度
* null 表示全流量
*/
canaryType?: 1 | 3 | null;
};
};
}

View 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.
*/
export { GLOBAL_ENVS } from '@coze-studio/bot-env-adapter';

View 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.
*/
export { runtimeEnv } from '@coze-studio/bot-env-adapter/runtime';

View 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-studio/bot-env-adapter/typings' />

View File

@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"references": [
{
"path": "../bot-env-adapter/tsconfig.build.json"
},
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/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,16 @@
{
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["__tests__", "scripts", "vitest.config.mts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"rootDir": "./",
"outDir": "./dist",
"types": ["vitest/globals"]
}
}

View File

@@ -0,0 +1,6 @@
import { defineConfig } from '@coze-arch/vitest-config';
export default defineConfig({
dirname: __dirname,
preset: 'node',
});