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,232 @@
# @coze-arch/bot-typings
A comprehensive TypeScript type definitions package extracted from the bot application, providing essential type declarations for the Coze bot platform. This package centralizes common type definitions, interfaces, and module declarations to ensure type safety across the bot ecosystem.
## Features
- **Global Type Declarations**: Provides type definitions for various file formats (images, stylesheets, SVG)
- **Common Utility Types**: Includes utility types like `Expand`, `PartialRequired`, and `Obj` for enhanced type manipulation
- **Platform-Specific Types**: Defines interfaces for browser APIs, window objects, and navigator extensions
- **User & Authentication Types**: Comprehensive type definitions for user data, authentication flows, and OAuth
- **Teamspace & Routing Types**: Type definitions for dynamic routing parameters and teamspace functionality
- **Zero Runtime Impact**: Pure TypeScript declarations with no runtime dependencies
## Get Started
### Installation
Install the package in your Rush.js monorepo:
```bash
# Add to your package.json
"@coze-arch/bot-typings": "workspace:*"
# Run Rush update to install
rush update
```
### Basic Usage
Import the main type definitions:
```typescript
// Import the main type definitions
import "@coze-arch/bot-typings";
// Import specific modules
import { BotPageFromEnum, Obj, Expand, PartialRequired } from "@coze-arch/bot-typings/common";
import { DynamicParams } from "@coze-arch/bot-typings/teamspace";
```
## API Reference
### Common Types
#### `BotPageFromEnum`
Defines the source of bot detail pages:
```typescript
enum BotPageFromEnum {
Bot = 'bot', // Bot list
Explore = 'explore', // Explore list
Store = 'store',
Template = 'template',
}
```
#### Utility Types
**`Obj`** - Generic object type:
```typescript
type Obj = Record<string, any>;
```
**`Expand<T>`** - Expands intersection types for better readability:
```typescript
type Intersection = { a: string } & { b: number };
type Result = Expand<Intersection>;
// Result: { a: string; b: number }
```
**`PartialRequired<T, K>`** - Makes specific fields required:
```typescript
interface Agent {
id?: string;
name?: string;
desc?: string;
}
type Result = PartialRequired<Agent, 'id' | 'name'>;
// Result: { id: string; name: string; desc?: string }
```
### Teamspace Types
#### `DynamicParams`
Defines route parameters for teamspace navigation:
```typescript
interface DynamicParams extends Record<string, string | undefined> {
space_id?: string;
bot_id?: string;
plugin_id?: string;
workflow_id?: string;
dataset_id?: string;
doc_id?: string;
tool_id?: string;
invite_key?: string;
product_id?: string;
mock_set_id?: string;
conversation_id: string; // Required
commit_version?: string;
scene_id?: string;
post_id?: string;
project_id?: string;
}
```
### User & Authentication Types
The package provides comprehensive types under the `DataItem` namespace:
#### `UserInfo`
Complete user information interface:
```typescript
interface UserInfo {
user_id_str: string;
name: string;
screen_name: string;
avatar_url: string;
email?: string;
// ... many more fields
}
```
#### Authentication Types
- `AuthLoginParams` - OAuth login parameters
- `AuthorizeResponse` - Authorization response structure
- `SendCodeData` - Verification code response
- `UserCheckResponse` - User validation response
### Global Module Declarations
The package automatically provides type declarations for:
**Image Files:**
```typescript
// .jpeg, .jpg, .webp, .gif, .png files
import myImage from './image.png'; // string
```
**Stylesheets:**
```typescript
// .less, .css files
import styles from './styles.less'; // { [key: string]: string }
```
**SVG Files:**
```typescript
// .svg files
import { ReactComponent } from './icon.svg'; // React.FunctionComponent
import icon from './icon.svg'; // any (depends on svgDefaultExport config)
```
### Browser API Extensions
**Window Interface Extensions:**
```typescript
// IDE plugin support
window.editorDispose?.();
// Mini-program integration
window.tt?.miniProgram.postMessage({ data: {...} });
// Coze app integration
window.__cozeapp__?.setLoading?.(true);
```
**Navigator Extensions:**
```typescript
// Standalone app detection
if (navigator.standalone) {
// Running as standalone web app
}
```
## Development
### Project Structure
```
src/
├── index.d.ts # Main type definitions and module declarations
├── common.ts # Common utility types and enums
├── teamspace.ts # Teamspace-related type definitions
├── data_item.d.ts # User and authentication type definitions
├── navigator.d.ts # Navigator API extensions
└── window.d.ts # Window object extensions
```
### Building
This package contains only TypeScript declarations and requires no build step:
```bash
npm run build # No-op (exits with code 0)
```
### Linting
```bash
npm run lint
```
### Adding New Types
1. **Module Declarations**: Add to `index.d.ts`
2. **Common Utilities**: Add to `common.ts`
3. **Domain-Specific Types**: Create new files and export from appropriate entry points
4. **Global Extensions**: Add to `window.d.ts` or `navigator.d.ts`
Remember to update the `exports` field in `package.json` for new modules.
## Dependencies
### Runtime Dependencies
None - this package contains only TypeScript type definitions.
### Development Dependencies
- **@coze-arch/bot-env**: Workspace package providing environment-specific typings
- **@coze-arch/eslint-config**: Shared ESLint configuration
- **@coze-arch/ts-config**: Shared TypeScript configuration
- **TypeScript 5.8.2**: Core TypeScript compiler
- **React Types**: For component and SVG type definitions
## License
Apache-2.0
---
**Author**: fanwenjie.fe@bytedance.com
This package is part of the Coze bot platform architecture and provides essential type safety for bot development workflows.

View File

@@ -0,0 +1,8 @@
{
"operationSettings": [
{
"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,51 @@
{
"name": "@coze-arch/bot-typings",
"version": "0.0.1",
"description": "bot typings that extract from bot/src/typings",
"license": "Apache-2.0",
"author": "fanwenjie.fe@bytedance.com",
"maintainers": [],
"exports": {
".": "./src/index.d.ts",
"./common": "./src/common.ts",
"./teamspace": "./src/teamspace.ts"
},
"main": "src/index.d.ts",
"types": "src/index.d.ts",
"typesVersions": {
".": {
"*": [
"./src/index.d.ts"
]
},
"*": {
"common": [
"./src/common.ts"
],
"teamspace": [
"./src/teamspace.ts"
]
}
},
"scripts": {
"build": "exit 0",
"lint": "eslint ./"
},
"dependencies": {},
"devDependencies": {
"@coze-arch/bot-env": "workspace:*",
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@rspack/core": "0.6.0",
"@types/node": "^18",
"debug": "^4.3.4",
"i18next": ">= 19.0.0",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"react-is": ">= 16.8.0",
"styled-components": ">= 2",
"typescript": "~5.8.2",
"webpack": "~5.91.0"
}
}

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.
*/
/** bot详情页来源目前只有bot和explore列表 */
export enum BotPageFromEnum {
Bot = 'bot', //bot列表
Explore = 'explore', //explore列表
Store = 'store',
Template = 'template',
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- 不得不 any
export type Obj = Record<string, any>;
/**
* 展示完整类型
*
* @example
* type Intersection = { a: string } & { b: number };
* type Result = Expand<Intersection>;
* // Result: { a: string; b: number }
*/
export type Expand<T extends Obj> = T extends infer U
? { [K in keyof U]: U[K] }
: never;
/**
* 只对特定字段做 required常用于修正服务端类型声明错误
*
* @example
* interface Agent {
* id?: string;
* name?: string;
* desc?: string
* }
* type Result = PartialRequired<Agent, 'id' | 'name'>;
*/
export type PartialRequired<T extends Obj, K extends keyof T> = Expand<
{
[P in K]-?: T[P];
} & Pick<T, Exclude<keyof T, K>>
>;

View File

@@ -0,0 +1,231 @@
/*
* 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.
*/
declare namespace DataItem {
interface UserConnectItem {
platform: string;
profile_image_url: string;
expired_time: number;
expires_in: number;
platform_screen_name: string;
user_id: number;
platform_uid: string;
sec_platform_uid: string;
platform_app_id: number;
modify_time: number;
access_token: string;
open_id: string;
}
interface UserInfo {
app_id: number;
/**
* @deprecated 会因为溢出丢失精度,使用 user_id_str
*/
user_id: number;
user_id_str: string;
odin_user_type: number;
name: string;
screen_name: string;
avatar_url: string;
user_verified: boolean;
email?: string;
email_collected: boolean;
expend_attrs?: Record<string, unknown>;
phone_collected: boolean;
verified_content: string;
verified_agency: string;
is_blocked: number;
is_blocking: number;
bg_img_url: string;
gender: number;
media_id: number;
user_auth_info: string;
industry: string;
area: string;
can_be_found_by_phone: number;
mobile: string;
birthday: string;
description: string;
status: number;
new_user: number;
first_login_app: number;
session_key: string;
is_recommend_allowed: number;
recommend_hint_message: string;
followings_count: number;
followers_count: number;
visit_count_recent: number;
skip_edit_profile: number;
is_manual_set_user_info: boolean;
device_id: number;
country_code: number;
has_password: number;
share_to_repost: number;
user_decoration: string;
user_privacy_extend: number;
old_user_id: number;
old_user_id_str: string;
sec_user_id: string;
sec_old_user_id: string;
vcd_account: number;
vcd_relation: number;
can_bind_visitor_account: boolean;
is_visitor_account: boolean;
is_only_bind_ins: boolean;
user_device_record_status: number;
is_kids_mode: number;
source: string;
is_employee: boolean;
passport_enterprise_user_type: number;
need_device_create: number;
need_ttwid_migration: number;
user_auth_status: number;
user_safe_mobile_2fa: string;
safe_mobile_country_code: number;
lite_user_info_string: string;
lite_user_info_demotion: number;
app_user_info: {
user_unique_name?: string;
};
need_check_bind_status: boolean;
bui_audit_info?: {
audit_info: {
user_unique_name?: string;
avatar_url?: string;
name?: string;
[key: string]: unknown;
}; // Record<string, unknown>;
// int值。1审核中2审核通过3审核不通过
audit_status: 1 | 2 | 3;
details: Record<string, unknown>;
is_auditing: boolean;
last_update_time: number;
unpass_reason: string;
};
}
/**
* 发送验证码的返回数据结构
*/
interface SendCodeData {
mobile: string;
mobile_ticket: string;
retry_time: number;
}
interface bindWithEmailLoginParams {
access_token?: string;
access_token_secret?: string;
code?: string;
openid?: number;
profile_key?: string;
platform_app_id: number;
redirect_uri?: string;
extra_params?: object;
}
interface UserCheckResponse {
app_user_info?: null;
authType: number;
error_code?: number;
in_old_process?: boolean;
oauth_platforms?: string[] | null;
platform_user_names?: Record<string, unknown>;
userType?: number;
value_ticket: string;
}
interface ValidateCodeResponse {
ticket: string;
}
interface AuthorizeResponse {
token: string;
user_info: {
user_id: number;
app_id: number;
user_name: string;
screen_name: string;
mobile: string;
email: string;
avatar_url: string;
description: string;
create_time: number;
is_new_user: boolean;
is_new_connect: boolean;
session_key: string;
session_app_id: number;
safe_mobile: string;
};
}
interface AuthLoginParams {
platform_app_id: number;
code?: string;
access_token?: string;
access_token_secret?: string;
openid?: string;
profile_key?: string;
login_only?: boolean;
extra_params?: object;
}
interface bindWithEmailLoginParams {
access_token?: string;
access_token_secret?: string;
code?: string;
openid?: number;
profile_key?: string;
platform_app_id: number;
redirect_uri?: string;
extra_params?: object;
}
interface bindWithMobileLoginParams {
code?: string;
profile_key?: string;
access_token?: string;
platform_app_id: number;
platform: string;
need_mobile?: number;
check_mobile?: number;
change_bind?: number;
extra_params?: object;
}
interface ResetByEmailTicket {
ticket: string;
}
interface AuditItem {
pass: boolean;
title: string;
text: string[];
reason: string[] | null;
}
interface CancelCheckResponse {
business_audit: AuditItem;
cancel_ticket: string;
common_audit: AuditItem;
error_code: number;
protocol: string;
punish_audit: AuditItem;
user_permission_audit: AuditItem;
}
interface UploadAvatarResponse {
web_uri: string;
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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='./data_item' />
/// <reference types='./navigator' />
/// <reference types='./window' />
/// <reference types='@coze-arch/bot-env/typings' />
declare module '*.jpeg' {
const value: string;
export default value;
}
declare module '*.jpg' {
const value: string;
export default value;
}
declare module '*.webp' {
const value: string;
export default value;
}
declare module '*.gif' {
const value: string;
export default value;
}
declare module '*.png' {
const value: string;
export default value;
}
declare module '*.less' {
const resource: { [key: string]: string };
export = resource;
}
declare module '*.css' {
const resource: { [key: string]: string };
export = resource;
}
declare module '*.svg' {
export const ReactComponent: React.FunctionComponent<
React.SVGProps<SVGSVGElement>
>;
/**
* The default export type depends on the svgDefaultExport config,
* it can be a string or a ReactComponent
* */
const content: any;
export default content;
}

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.
*/
interface Navigator {
standalone: boolean;
}

View File

@@ -0,0 +1,36 @@
/*
* 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 packages/arch/bot-typings/src/teamspace.ts
export interface DynamicParams extends Record<string, string | undefined> {
space_id?: string;
bot_id?: string;
plugin_id?: string;
workflow_id?: string;
dataset_id?: string;
doc_id?: string;
tool_id?: string;
invite_key?: string;
product_id?: string;
mock_set_id?: string;
conversation_id: string;
commit_version?: string;
/** 社会场景 */
scene_id?: string;
post_id?: string;
project_id?: string;
}

View File

@@ -0,0 +1,68 @@
/*
* 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.
*/
declare type MicroComponentsMapItem = {
version: string;
cdnUrl: string;
};
interface Window {
/**
* IDE plugin iframe 中挂载的用于卸载的方法
*/
editorDispose?: any;
MonacoEnvironment?: any;
tt?: {
miniProgram: {
postMessage: (param: {
data?: any;
success?: (res) => void;
fail?: (err) => void;
}) => void;
redirectTo: (param: {
url?: string;
success?: (res) => void;
fail?: (err) => void;
}) => void;
navigateTo: (param: {
url?: string;
success?: (res) => void;
fail?: (err) => void;
}) => void;
reLaunch: (param: {
url?: string;
success?: (res) => void;
fail?: (err) => void;
}) => void;
navigateBack: (param?: {
delta?: number;
success?: (res) => void;
fail?: (err) => void;
}) => void;
getEnv: (res) => void;
};
};
__cozeapp__?: {
props: Record<string, unknown>;
setLoading?: (loading: boolean) => void;
};
}
declare namespace process {
const env: {
[key: string]: string;
};
}

View File

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