Files
proxycast/docs/aiprompts/lib.md
T
2026-01-30 01:31:09 +08:00

2.2 KiB

工具库

概述

前端工具库和 API 封装层。

目录结构

src/lib/
├── api/                # API 封装
│   ├── apiKeyProvider.ts
│   └── pluginUI.ts
├── config/             # 配置
│   └── providers.ts
├── types/              # 类型定义
│   └── provider.ts
├── errors/             # 错误处理
│   └── playwrightErrors.ts
├── plugin-ui/          # 插件 UI 系统
├── tauri/              # Tauri 命令封装
├── utils/              # 工具函数
├── flowEventManager.ts # 流量事件管理
├── terminal-api.ts     # 终端 API
└── utils.ts            # 通用工具

核心模块

Tauri 命令封装

// src/lib/tauri/credentials.ts
export async function addCredential(provider: string, path: string) {
    return invoke<CredentialInfo>('add_credential', { provider, filePath: path });
}

export async function listCredentials() {
    return invoke<CredentialInfo[]>('list_credentials');
}

流量事件管理

// src/lib/flowEventManager.ts
class FlowEventManager {
    private listeners: Map<string, Set<FlowEventListener>> = new Map();
    
    subscribe(event: string, listener: FlowEventListener) {
        if (!this.listeners.has(event)) {
            this.listeners.set(event, new Set());
        }
        this.listeners.get(event)!.add(listener);
        return () => this.listeners.get(event)?.delete(listener);
    }
    
    emit(event: string, data: any) {
        this.listeners.get(event)?.forEach(listener => listener(data));
    }
}

export const flowEventManager = new FlowEventManager();

工具函数

// src/lib/utils.ts
export function cn(...classes: (string | undefined)[]) {
    return classes.filter(Boolean).join(' ');
}

export function formatBytes(bytes: number) {
    if (bytes < 1024) return `${bytes} B`;
    if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
    return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
}

export function formatDuration(ms: number) {
    if (ms < 1000) return `${ms}ms`;
    return `${(ms / 1000).toFixed(2)}s`;
}

相关文档