feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
202
frontend/packages/arch/bot-env/README.md
Normal file
202
frontend/packages/arch/bot-env/README.md
Normal 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/).
|
||||
0
frontend/packages/arch/bot-env/__tests__/.gitkeep
Normal file
0
frontend/packages/arch/bot-env/__tests__/.gitkeep
Normal file
16
frontend/packages/arch/bot-env/config/rush-project.json
Normal file
16
frontend/packages/arch/bot-env/config/rush-project.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"operationSettings": [
|
||||
{
|
||||
"operationName": "build",
|
||||
"outputFolderNames": ["dist"]
|
||||
},
|
||||
{
|
||||
"operationName": "test:cov",
|
||||
"outputFolderNames": ["coverage"]
|
||||
},
|
||||
{
|
||||
"operationName": "ts-check",
|
||||
"outputFolderNames": ["./dist"]
|
||||
}
|
||||
]
|
||||
}
|
||||
7
frontend/packages/arch/bot-env/eslint.config.js
Normal file
7
frontend/packages/arch/bot-env/eslint.config.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const { defineConfig } = require('@coze-arch/eslint-config');
|
||||
|
||||
module.exports = defineConfig({
|
||||
packageRoot: __dirname,
|
||||
preset: 'node',
|
||||
rules: {},
|
||||
});
|
||||
48
frontend/packages/arch/bot-env/package.json
Normal file
48
frontend/packages/arch/bot-env/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
|
||||
17
frontend/packages/arch/bot-env/scripts/build.ts
Normal file
17
frontend/packages/arch/bot-env/scripts/build.ts
Normal 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';
|
||||
19
frontend/packages/arch/bot-env/scripts/index.ts
Normal file
19
frontend/packages/arch/bot-env/scripts/index.ts
Normal 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();
|
||||
50
frontend/packages/arch/bot-env/src/global.d.ts
vendored
Normal file
50
frontend/packages/arch/bot-env/src/global.d.ts
vendored
Normal 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;
|
||||
// 当前的小流量频道 ID,0 表示全流量
|
||||
canary?: 0;
|
||||
extra?: {
|
||||
/**
|
||||
* @description goofy 团队不建议依赖该字段,能不用则不用
|
||||
* 1 表示小流量
|
||||
* 3 表示灰度
|
||||
* null 表示全流量
|
||||
*/
|
||||
canaryType?: 1 | 3 | null;
|
||||
};
|
||||
};
|
||||
}
|
||||
17
frontend/packages/arch/bot-env/src/index.ts
Normal file
17
frontend/packages/arch/bot-env/src/index.ts
Normal 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';
|
||||
17
frontend/packages/arch/bot-env/src/runtime/index.ts
Normal file
17
frontend/packages/arch/bot-env/src/runtime/index.ts
Normal 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';
|
||||
17
frontend/packages/arch/bot-env/src/typings.d.ts
vendored
Normal file
17
frontend/packages/arch/bot-env/src/typings.d.ts
vendored
Normal 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' />
|
||||
24
frontend/packages/arch/bot-env/tsconfig.build.json
Normal file
24
frontend/packages/arch/bot-env/tsconfig.build.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
frontend/packages/arch/bot-env/tsconfig.json
Normal file
15
frontend/packages/arch/bot-env/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"composite": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.misc.json"
|
||||
}
|
||||
],
|
||||
"exclude": ["**/*"]
|
||||
}
|
||||
16
frontend/packages/arch/bot-env/tsconfig.misc.json
Normal file
16
frontend/packages/arch/bot-env/tsconfig.misc.json
Normal 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"]
|
||||
}
|
||||
}
|
||||
6
frontend/packages/arch/bot-env/vitest.config.mts
Normal file
6
frontend/packages/arch/bot-env/vitest.config.mts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default defineConfig({
|
||||
dirname: __dirname,
|
||||
preset: 'node',
|
||||
});
|
||||
Reference in New Issue
Block a user