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,258 @@
# @coze-studio/slardar-interface
> TypeScript interface definitions for Slardar monitoring and error reporting
## Overview
`@coze-studio/slardar-interface` provides standardized TypeScript interface definitions for integrating with Slardar monitoring services. This package serves as a contract layer that defines the structure and behavior of Slardar instances used throughout the Coze Studio ecosystem.
## Features
-**Type Safety** - Complete TypeScript interface definitions for Slardar functionality
- 🔧 **Event Management** - Strongly typed event handling with overloaded methods
- 📊 **Error Tracking** - Comprehensive error capturing with React support
- 📈 **Metrics & Logging** - Structured event and log reporting interfaces
- ⚙️ **Configuration** - Flexible configuration management
- 🎯 **Event System** - Event listener registration and management
## Get Started
### Installation
Since this is a workspace package, add it to your `package.json`:
```json
{
"dependencies": {
"@coze-studio/slardar-interface": "workspace:*"
}
}
```
Then run:
```bash
rush update
```
### Basic Usage
```typescript
import type { Slardar, SlardarConfig, SlardarInstance } from '@coze-studio/slardar-interface';
// Implementing a Slardar instance
class MySlardarImplementation implements Slardar {
// Implementation details...
}
// Using as a constraint
function useSlardar(slardar: SlardarInstance) {
// Configure the instance
slardar.config({ sessionId: 'user-session-123' });
// Send events
slardar('sendEvent', {
name: 'user_action',
metrics: { duration: 150 },
categories: { page: 'dashboard' }
});
}
```
## API Reference
### Interfaces
#### `SlardarConfig`
Configuration options for Slardar instance:
```typescript
interface SlardarConfig {
sessionId?: string;
[key: string]: unknown;
}
```
#### `Slardar`
Main Slardar interface with overloaded methods for different event types:
```typescript
interface Slardar {
// Generic event method
(event: string, params?: Record<string, unknown>): void;
// Error capturing
(
event: 'captureException',
error?: Error,
meta?: Record<string, string>,
reactInfo?: { version: string; componentStack: string }
): void;
// Event reporting
(
event: 'sendEvent',
params: {
name: string;
metrics: Record<string, number>;
categories: Record<string, string>;
}
): void;
// Log reporting
(
event: 'sendLog',
params: {
level: string;
content: string;
extra: Record<string, string | number>;
}
): void;
// Context management
(event: 'context.set', key: string, value: string): void;
// Configuration
config: (() => SlardarConfig) & ((options: Partial<SlardarConfig>) => void);
// Event listeners
on: (event: string, callback: (...args: unknown[]) => void) => void;
off: (event: string, callback: (...args: unknown[]) => void) => void;
}
```
### Event Types
#### Error Capturing
```typescript
slardar('captureException', new Error('Something went wrong'), {
userId: '12345',
context: 'checkout'
}, {
version: '18.2.0',
componentStack: 'CheckoutForm > PaymentSection'
});
```
#### Event Tracking
```typescript
slardar('sendEvent', {
name: 'button_click',
metrics: {
loadTime: 250,
clickCount: 1
},
categories: {
component: 'nav-button',
section: 'header'
}
});
```
#### Logging
```typescript
slardar('sendLog', {
level: 'info',
content: 'User performed action',
extra: {
userId: 'user123',
timestamp: Date.now()
}
});
```
#### Context Management
```typescript
slardar('context.set', 'userId', 'user-12345');
slardar('context.set', 'environment', 'production');
```
### Configuration Management
```typescript
// Get current config
const currentConfig = slardar.config();
// Update config
slardar.config({
sessionId: 'new-session-id',
customField: 'value'
});
```
### Event Listeners
```typescript
// Register event listener
const handleError = (error: Error) => {
console.log('Error captured:', error);
};
slardar.on('error', handleError);
// Remove event listener
slardar.off('error', handleError);
```
## Development
### Project Structure
```
src/
├── index.ts # Main interface definitions
```
### Building
This package uses a no-op build process since it only contains TypeScript interfaces:
```bash
npm run build # exits with code 0
```
### Testing
```bash
npm test
```
### Linting
```bash
npm run lint
```
## Dependencies
### Runtime Dependencies
None - this package only provides TypeScript interface definitions.
### Development Dependencies
- `@coze-arch/eslint-config` - Shared ESLint configuration
- `@coze-arch/ts-config` - Shared TypeScript configuration
- `@coze-arch/vitest-config` - Shared Vitest configuration
- `@types/node` - Node.js type definitions
- `@vitest/coverage-v8` - Coverage reporting
- `vitest` - Testing framework
## Related Packages
- `@coze-studio/slardar-adapter` - Adapter implementation using these interfaces
- `@coze-studio/default-slardar` - Default Slardar implementation
## License
Apache-2.0
---
> This package is part of the Coze Studio monorepo and provides foundational type definitions for Slardar monitoring integration.

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: 'node',
rules: {},
});

View File

@@ -0,0 +1,26 @@
{
"name": "@coze-studio/slardar-interface",
"version": "0.0.1",
"description": "interface that descripts how to use slardar ",
"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": {},
"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,61 @@
/*
* 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 interface SlardarConfig {
sessionId?: string;
[key: string]: unknown;
}
export type SlardarEvents =
| 'captureException'
| 'sendEvent'
| 'sendLog'
| 'context.set';
export interface Slardar {
(event: string, params?: Record<string, unknown>): void;
(
event: 'captureException',
error?: Error,
meta?: Record<string, string>,
reactInfo?: { version: string; componentStack: string },
): void;
(
event: 'sendEvent',
params: {
name: string;
metrics: Record<string, number>;
categories: Record<string, string>;
},
): void;
(
event: 'sendLog',
params: {
level: string;
content: string;
extra: Record<string, string | number>;
},
): void;
(event: 'context.set', key: string, value: string): void;
config: (() => SlardarConfig) & ((options: Partial<SlardarConfig>) => void);
on: (event: string, callback: (...args: unknown[]) => void) => void;
off: (event: string, callback: (...args: unknown[]) => void) => void;
}
// 可用于约束传入的slardar实例类型
export type SlardarInstance = Slardar;
export type { Slardar as default };

View File

@@ -0,0 +1,25 @@
{
"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"
}
]
}

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',
});