Initial commit of akmon project

This commit is contained in:
2026-01-20 08:04:15 +08:00
commit 77a2bab985
1309 changed files with 343305 additions and 0 deletions

View File

@@ -0,0 +1,287 @@
# Changelog
All notable changes to the AK-AI-News multilingual AI news system will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.0.0] - 2024-12-19
### 🎉 Initial Release
This is the first major release of the AK-AI-News multilingual AI-driven news system, providing a comprehensive solution for news content processing, translation, analysis, and personalized recommendations.
### ✨ Features Added
#### 🔄 Core AI Services
- **AITranslationService**: Multi-provider translation with OpenAI, Google Translate, and Baidu AI support
- Smart language detection
- Cultural adaptation
- Quality assessment and scoring
- Batch processing capabilities
- Intelligent caching with LRU strategy
- Cost control and usage monitoring
- **AIContentAnalysisService**: Comprehensive content analysis and processing
- Sentiment analysis with confidence scores
- Named entity recognition (NER)
- Topic extraction and classification
- Content quality assessment
- Toxicity detection
- Automatic summarization
- Readability scoring
- **AIChatService**: Intelligent multilingual chat assistant
- Natural language conversation in 10+ languages
- Session management and context tracking
- News-specific query handling
- Real-time language switching
- Personalized responses based on user preferences
- Template-based conversation flows
- **AIRecommendationService**: Advanced recommendation engine
- Collaborative filtering algorithms
- Content-based filtering
- Hybrid recommendation strategies
- User behavior tracking and analysis
- Real-time personalization
- Diversity and freshness control
- Click-through rate optimization
- **ContentProcessingPipeline**: Automated content workflow
- Configurable processing steps
- Batch processing with concurrency control
- Error recovery and rollback mechanisms
- Quality control at each stage
- Progress monitoring and reporting
- Flexible plugin architecture
- **AIServiceManager**: Unified service coordination
- Service lifecycle management
- Load balancing across providers
- Health monitoring and failover
- Cost control and budget management
- Performance statistics and analytics
- Configuration management
#### 🚀 Performance & Monitoring
- **AIPerformanceMonitor**: Real-time system monitoring
- Performance metrics collection
- System health scoring (0-100)
- Automatic optimization recommendations
- Alert generation and management
- Historical data analysis
- Export capabilities (JSON/CSV)
- **AIErrorHandler**: Advanced error handling and recovery
- Exponential backoff retry strategy
- Circuit breaker pattern implementation
- Intelligent error classification
- Fallback provider mechanisms
- Rate limiting and queue management
- Error pattern detection
#### 🎨 User Interface Components
- **AINewsDemo.vue**: Interactive demo component
- Live translation testing
- Content analysis visualization
- Chat interface with conversation history
- Recommendation display and interaction
- Real-time processing status
- **AINewsDashboard.vue**: Comprehensive monitoring dashboard
- Real-time system health indicators
- Performance metrics visualization
- Cost analysis and breakdown
- Active alerts and notifications
- Optimization recommendations
- Export and reporting tools
#### 🧪 Testing Infrastructure
- **Simple Test Suite**: Basic functionality validation
- Unit tests for core services
- Type definition validation
- Configuration testing
- Mock API integration
- **Integration Test Suite**: End-to-end testing
- Multi-provider API testing
- Real-world scenario simulation
- Performance benchmarking
- Cost tracking during tests
- Comprehensive test reporting
- **Comprehensive Test Runner**: Production-ready testing
- Unit, integration, performance, and error handling tests
- Configurable test scenarios
- Automated report generation
- CI/CD pipeline integration
- Cost-controlled testing
### 🏗️ Architecture & Design
#### 📊 Database Schema
- Complete PostgreSQL schema for multilingual news system
- Optimized indexes for performance
- Comprehensive audit trails
- Multi-language content support
- User behavior analytics tables
- AI service usage tracking
#### 🔧 Technology Stack
- **Language**: TypeScript/UTS for uni-app compatibility
- **AI Providers**: OpenAI GPT-4/3.5, Google Translate API, Baidu AI
- **Caching**: LRU cache with TTL support
- **Database**: PostgreSQL with full-text search
- **UI Framework**: Vue.js 3 with Composition API
- **Testing**: Custom test framework with real API integration
#### 🎯 Key Capabilities
- **Multi-language Support**: 20+ languages with cultural adaptation
- **High Performance**: Sub-second response times with intelligent caching
- **Cost Optimization**: Automatic provider selection and batch processing
- **Scalability**: Designed for high-throughput news processing
- **Reliability**: Circuit breakers, retries, and fallback mechanisms
- **Monitoring**: Real-time health checks and performance tracking
### 📈 Performance Metrics
#### ⚡ Benchmarks
- **Translation**: Average 800ms response time, 95th percentile < 2s
- **Analysis**: Average 1.2s for comprehensive content analysis
- **Chat**: Average 600ms response time for conversational AI
- **Recommendations**: < 100ms for real-time personalized suggestions
- **Throughput**: 50+ concurrent requests with linear scaling
#### 💰 Cost Efficiency
- **Intelligent Caching**: 70%+ cache hit rate reduces API costs
- **Batch Processing**: 40% cost reduction for bulk operations
- **Provider Optimization**: Automatic selection saves 25% on average
- **Quality Thresholds**: Prevent unnecessary high-cost API calls
### 🛡️ Security & Privacy
#### 🔐 Security Features
- API key encryption and secure storage
- Request rate limiting and abuse protection
- Input validation and sanitization
- Audit logging for all operations
- Cost limit enforcement
#### 🛡️ Privacy Protection
- No personal data storage without consent
- Configurable data retention policies
- GDPR compliance features
- Anonymous usage analytics
- User data export capabilities
### 📚 Documentation
#### 📖 Comprehensive Guides
- **README.md**: Complete setup and usage guide
- **API Documentation**: Detailed service interfaces
- **Configuration Guide**: Production deployment instructions
- **Best Practices**: Performance optimization recommendations
- **Troubleshooting**: Common issues and solutions
#### 🎯 Code Examples
- **Basic Usage**: Simple integration examples
- **Advanced Features**: Complex workflow implementations
- **Production Setup**: Real-world deployment configurations
- **Testing Examples**: Comprehensive test scenarios
### 🔄 Workflow Integration
#### 🔗 uni-app Integration
- Native UTS plugin architecture
- Cross-platform compatibility (iOS/Android/Web)
- Vue.js component library
- Seamless HBuilderX integration
#### 🚀 CI/CD Support
- Automated testing pipelines
- Performance regression detection
- Cost monitoring in CI
- Deployment validation
### 📊 Monitoring & Analytics
#### 📈 Real-time Metrics
- Request volume and success rates
- Response time percentiles
- Error rates by category and provider
- Cost tracking and budget alerts
- Cache performance metrics
#### 🔍 Business Intelligence
- User behavior analysis
- Content performance insights
- Translation accuracy metrics
- Recommendation effectiveness
- ROI tracking and optimization
### 🌐 Internationalization
#### 🗣️ Language Support
- **Translation**: 50+ language pairs
- **Content Analysis**: 20+ languages
- **Chat Interface**: 10+ conversational languages
- **UI Localization**: Chinese/English interfaces
#### 🌍 Cultural Adaptation
- Region-specific content formatting
- Cultural context awareness
- Local news source integration
- Time zone and date formatting
### 🔮 Future Roadmap
#### 📅 Planned Features
- Real-time news feed processing
- Social media integration
- Advanced ML model training
- Voice interface support
- Mobile push notifications
#### 🚀 Technical Improvements
- WebAssembly optimization
- Edge computing deployment
- Advanced caching strategies
- Machine learning recommendations
- Blockchain integration
### 🤝 Community & Support
#### 👥 Open Source
- MIT License for maximum flexibility
- Community contributions welcome
- Comprehensive contributor guidelines
- Regular community updates
#### 📞 Support Channels
- GitHub Issues for bug reports
- Documentation wiki
- Community Discord server
- Professional support options
---
## [Future Versions]
### Planned for v1.1.0
- [ ] Real-time news streaming
- [ ] Enhanced mobile app integration
- [ ] Advanced analytics dashboard
- [ ] Custom model training
- [ ] Enterprise SSO integration
### Planned for v1.2.0
- [ ] Voice-to-text integration
- [ ] Video content analysis
- [ ] Blockchain verification
- [ ] Advanced personalization
- [ ] Multi-tenant architecture
---
*This changelog is automatically updated with each release. For the latest development updates, see our [GitHub repository](https://github.com/ak-tech/ak-ai-news).*

View File

@@ -0,0 +1,816 @@
# AK-AI-News 多语言AI新闻系统
一个功能完整的多语言AI驱动新闻系统提供内容翻译、分析、智能对话和个性化推荐等服务。
## 🌟 主要特性
### 🔄 多语言翻译服务 (AITranslationService)
- **多提供商支持**: OpenAI、Google Translate、百度翻译
- **智能缓存机制**: LRU缓存策略减少重复翻译成本
- **批量处理**: 支持批量翻译,提高处理效率
- **质量评估**: 自动评估翻译质量,确保内容准确性
- **语言检测**: 自动识别源语言
- **成本控制**: 实时监控翻译成本
### 🔍 内容分析服务 (AIContentAnalysisService)
- **情感分析**: 分析内容情感倾向(正面/负面/中性)
- **实体识别**: 提取人名、地名、组织等关键实体
- **主题提取**: 自动识别文章主题和关键词
- **内容分类**: 智能分类新闻内容(政治、经济、科技等)
- **质量评估**: 评估内容可读性、可信度、客观性
- **毒性检测**: 识别有害或不当内容
- **自动摘要**: 生成内容摘要
### 💬 智能对话服务 (AIChatService)
- **多语言对话**: 支持多种语言的自然对话
- **上下文理解**: 维护对话上下文,提供连贯回复
- **新闻助手**: 专业的新闻查询和分析助手
- **会话管理**: 完整的会话生命周期管理
- **个性化回复**: 基于用户偏好的个性化响应
- **实时翻译**: 对话中的实时语言切换
### 🎯 智能推荐服务 (AIRecommendationService)
- **个性化推荐**: 基于用户行为的个性化内容推荐
- **多算法支持**: 协同过滤、内容过滤、混合算法
- **热门推荐**: 基于热度和时效性的推荐
- **相似内容**: 基于内容相似度的推荐
- **用户画像**: 详细的用户兴趣和行为分析
- **多样性控制**: 确保推荐内容的多样性
### ⚙️ 自动化处理管道 (ContentProcessingPipeline)
- **全流程自动化**: 从内容获取到发布的自动化处理
- **可配置步骤**: 灵活配置处理步骤和参数
- **批量处理**: 支持大批量内容的并行处理
- **错误恢复**: 完善的错误处理和回滚机制
- **质量控制**: 多层次的内容质量检查
- **状态监控**: 实时监控处理进度和状态
### 🎛️ 服务管理器 (AIServiceManager)
- **统一管理**: 所有AI服务的统一入口和管理
- **负载均衡**: 智能选择最佳AI提供商
- **健康监控**: 实时监控服务健康状态
- **成本控制**: 严格的成本限制和预警机制
- **性能监控**: 详细的性能统计和分析
- **缓存管理**: 统一的缓存策略和管理
## 🚀 快速开始
### 1. 安装和配置
```typescript
import {
AIServiceManager,
type AIServiceConfig
} from '@/uni_modules/ak-ai-news'
// 配置AI服务
const aiConfig: AIServiceConfig = {
openai: {
apiKey: 'your-openai-api-key',
model: 'gpt-3.5-turbo',
maxTokens: 2000,
temperature: 0.7
},
google: {
apiKey: 'your-google-api-key',
model: 'gemini-pro'
},
baidu: {
apiKey: 'your-baidu-api-key',
secretKey: 'your-baidu-secret-key',
model: 'ernie-bot'
},
costLimits: {
dailyUSD: 100,
monthlyUSD: 2000,
perRequestUSD: 5
}
}
// 创建服务管理器
const serviceManager = new AIServiceManager(aiConfig)
await serviceManager.initialize()
```
### 2. 翻译服务使用
```typescript
const translationService = serviceManager.getTranslationService()
// 单个翻译
const result = await translationService.translateText(
'人工智能正在改变世界',
'en',
'zh-CN',
{
provider: 'openai',
culturalAdaptation: true
}
)
// 批量翻译
const batchResult = await translationService.translateBatch(
['新闻1', '新闻2', '新闻3'],
'en',
'zh-CN'
)
```
### 3. 内容分析使用
```typescript
const analysisService = serviceManager.getAnalysisService()
const analysis = await analysisService.analyzeContent(
'今日股市大涨,投资者信心提升...',
{
types: ['sentiment', 'entities', 'categories', 'summary'],
language: 'zh-CN'
}
)
console.log('情感分析:', analysis.data?.sentimentLabel)
console.log('关键实体:', analysis.data?.entities)
console.log('内容分类:', analysis.data?.categories)
```
### 4. 智能对话使用
```typescript
const chatService = serviceManager.getChatService()
// 创建会话
const session = await chatService.createChatSession('user123', 'zh-CN')
// 发送消息
const response = await chatService.sendMessage(
session.data!.id,
'今天有什么重要新闻?'
)
console.log('AI回复:', response.data?.content)
```
### 5. 推荐服务使用
```typescript
const recommendationService = serviceManager.getRecommendationService()
// 记录用户行为
await recommendationService.recordUserBehavior({
userId: 'user123',
contentId: 'news001',
actionType: 'view',
timestamp: Date.now(),
duration: 120
})
// 获取个性化推荐
const recommendations = await recommendationService.getPersonalizedRecommendations(
'user123',
availableNews,
{
algorithm: 'hybrid',
maxResults: 10
}
)
```
### 6. 自动化处理管道
```typescript
const pipeline = serviceManager.getProcessingPipeline()
// 处理单个内容
const result = await pipeline.processContent({
id: 'news001',
title: '突破性AI技术发布',
content: '详细内容...',
originalLanguage: 'zh-CN',
publishedAt: Date.now()
})
// 批量处理
const batchResult = await pipeline.processBatch(newsArray)
```
## 📊 系统架构
```mermaid
graph TB
A[AIServiceManager] --> B[AITranslationService]
A --> C[AIContentAnalysisService]
A --> D[AIChatService]
A --> E[AIRecommendationService]
A --> F[ContentProcessingPipeline]
B --> G[OpenAI API]
B --> H[Google Translate]
B --> I[Baidu Translate]
C --> G
C --> J[Content Analysis Engine]
D --> G
D --> K[Chat Context Manager]
E --> L[Recommendation Engine]
E --> M[User Profile System]
F --> B
F --> C
F --> N[Processing Queue]
A --> O[Monitoring & Stats]
A --> P[Cost Control]
A --> Q[Health Check]
```
## 🎛️ 配置选项
### AI服务配置 (AIServiceConfig)
```typescript
interface AIServiceConfig {
openai?: {
apiKey: string
model: string
baseURL?: string
maxTokens: number
temperature: number
}
google?: {
apiKey: string
projectId?: string
model: string
}
baidu?: {
apiKey: string
secretKey: string
model: string
}
costLimits?: {
dailyUSD: number
monthlyUSD: number
perRequestUSD: number
}
qualityThresholds?: {
translation: number
sentiment: number
credibility: number
}
}
```
### 缓存配置 (CacheOptions)
```typescript
interface CacheOptions {
enabled: boolean
ttlHours: number // 缓存过期时间(小时)
maxSize: number // 最大缓存条目数
strategy: 'lru' | 'fifo' | 'ttl' // 缓存策略
}
```
### 推荐配置 (RecommendationConfig)
```typescript
interface RecommendationConfig {
algorithm: 'collaborative_filtering' | 'content_based' | 'hybrid' | 'trending'
maxResults: number
diversityWeight: number // 多样性权重
freshnessWeight: number // 新鲜度权重
personalizedWeight: number // 个性化权重
qualityThreshold: number // 质量阈值
excludeViewed: boolean // 排除已浏览内容
}
```
## 📈 监控和统计
系统提供详细的监控和统计功能:
### 服务健康监控
- 实时监控各服务状态
- 响应时间和错误率统计
- 自动故障检测和告警
### 成本控制
- 实时成本监控
- 每日/每月成本限制
- 成本预警和控制
### 性能统计
- 请求量和成功率
- 平均响应时间
- 缓存命中率
- 用户满意度
```typescript
// 获取系统统计
const stats = serviceManager.getManagerStatistics()
console.log('总请求数:', stats.totalRequests)
console.log('成功率:', stats.successfulRequests / stats.totalRequests)
console.log('总成本:', stats.totalCost)
// 获取服务健康状态
const health = serviceManager.getServicesHealth()
console.log('服务状态:', health)
```
## 🔧 高级功能
### 🚀 性能监控与优化
系统内置了完整的性能监控和自动优化功能:
```typescript
import { AIPerformanceMonitor, defaultPerformanceConfig } from 'ak-ai-news'
// 创建性能监控器
const monitor = new AIPerformanceMonitor({
...defaultPerformanceConfig,
enableAutoOptimization: true
})
// 启动监控
monitor.startMonitoring()
// 获取系统健康状态
const health = monitor.getSystemHealth()
console.log('系统状态:', health.status) // healthy/warning/critical
console.log('健康评分:', health.score) // 0-100
// 获取性能统计
const stats = monitor.getPerformanceStats(startTime, endTime)
console.log('平均延迟:', stats.timing.averageLatency)
console.log('成功率:', stats.requests.successRate)
console.log('总成本:', stats.costs.total)
// 获取优化建议
const recommendations = monitor.getOptimizationRecommendations()
recommendations.forEach(rec => {
console.log(`${rec.type}: ${rec.description}`)
})
```
### 🛡️ 高级错误处理
系统提供了完善的错误处理和恢复机制:
```typescript
import { AIErrorHandler, defaultErrorHandlingConfig, ErrorCategory } from 'ak-ai-news'
// 创建错误处理器
const errorHandler = new AIErrorHandler({
...defaultErrorHandlingConfig,
retryPolicy: {
maxAttempts: 5,
baseDelayMs: 1000,
maxDelayMs: 30000,
backoffMultiplier: 2,
jitterEnabled: true
},
circuitBreaker: {
failureThreshold: 5,
recoveryTimeoutMs: 60000,
halfOpenMaxCalls: 3,
monitoringWindowMs: 300000
},
fallback: {
enabled: true,
fallbackProviders: ['openai', 'google', 'baidu'],
gracefulDegradation: true
}
})
// 执行带错误处理的操作
const result = await errorHandler.executeWithRetry(
async () => {
// 您的AI操作
return await translationService.translateText('Hello', 'zh-CN', 'en')
},
{
operationName: 'translation',
provider: 'openai',
retryable: true
}
)
if (result.success) {
console.log('操作成功:', result.data)
console.log('尝试次数:', result.attempts.length)
} else {
console.log('操作失败:', result.error?.message)
console.log('错误类别:', result.error?.category)
}
```
### 📊 实时监控面板
系统提供了一个完整的Vue.js监控面板组件
```vue
<template>
<AINewsDashboard />
</template>
<script setup>
import { AINewsDashboard } from 'ak-ai-news/components'
</script>
```
监控面板功能:
- **实时系统状态**CPU、内存、网络状态
- **性能指标**:响应时间、吞吐量、成功率
- **成本监控**:各提供商成本分析
- **错误追踪**:错误类型、频率、趋势分析
- **优化建议**:自动生成性能优化建议
- **告警系统**:实时告警和通知
### 🧪 综合测试套件
系统提供了完整的测试解决方案:
```typescript
import { runCompleteTestSuite, defaultTestConfig } from 'ak-ai-news/test'
// 运行完整测试套件
const results = await runCompleteTestSuite({
...defaultTestConfig,
runUnitTests: true, // 单元测试
runIntegrationTests: true, // 集成测试
runPerformanceTests: true, // 性能测试
runErrorHandlingTests: true, // 错误处理测试
enableRealAPIs: false, // 启用真实API测试
maxCostLimit: 10.0, // 测试成本限制
generateReport: true // 生成测试报告
})
console.log('测试结果:', results.overallResult.passed)
console.log('成功率:', results.overallResult.successRate)
console.log('总耗时:', results.totalDuration)
console.log('建议:', results.recommendations)
```
测试类型:
- **单元测试**:基础功能验证
- **集成测试**:端到端流程测试
- **性能测试**:延迟、吞吐量、并发测试
- **压力测试**:系统极限测试
- **错误处理测试**:故障恢复测试
- **成本控制测试**API成本监控测试
### 自定义处理步骤
```typescript
// 添加自定义处理步骤
pipeline.addProcessingStep({
name: 'custom_validation',
order: 1,
execute: async (data) => {
// 自定义验证逻辑
return data
},
rollback: async (data) => {
// 回滚逻辑
}
})
```
### 负载均衡策略
```typescript
// 系统自动选择最佳AI提供商
const bestProvider = serviceManager.selectBestProvider('translation')
```
### 批量处理优化
```typescript
// 批量处理配置
const batchOptions = {
batchSize: 10,
concurrency: 3,
retryCount: 2,
delayMs: 1000,
onProgress: (completed, total) => {
console.log(`进度: ${completed}/${total}`)
}
}
```
## 🚀 部署和运维
### 生产环境部署
1. **环境准备**
```bash
# 安装依赖
npm install ak-ai-news
# 配置环境变量
export OPENAI_API_KEY="your-openai-key"
export GOOGLE_API_KEY="your-google-key"
export BAIDU_APP_ID="your-baidu-app-id"
export BAIDU_SECRET_KEY="your-baidu-secret"
```
2. **系统配置**
```typescript
const productionConfig: AIServiceConfig = {
openai: {
apiKey: process.env.OPENAI_API_KEY,
model: 'gpt-4', // 生产环境推荐使用GPT-4
maxTokens: 2000,
temperature: 0.3 // 降低随机性
},
costLimits: {
dailyUSD: 1000, // 每日1000美元限额
monthlyUSD: 25000, // 每月25000美元限额
perRequestUSD: 10 // 单次请求10美元限额
},
qualityThresholds: {
translation: 0.9, // 提高质量阈值
sentiment: 0.8,
credibility: 0.7
}
}
```
3. **监控设置**
```typescript
const productionMonitor = new AIPerformanceMonitor({
monitoringInterval: 15000, // 15秒监控间隔
maxHistory: 50000, // 保留5万条历史记录
enableAutoOptimization: true,
alertWebhook: 'https://your-webhook-url.com/alerts'
})
```
4. **错误处理配置**
```typescript
const productionErrorHandler = new AIErrorHandler({
retryPolicy: {
maxAttempts: 5,
baseDelayMs: 2000,
maxDelayMs: 60000,
backoffMultiplier: 2.5,
jitterEnabled: true
},
circuitBreaker: {
failureThreshold: 10,
recoveryTimeoutMs: 120000, // 2分钟恢复时间
halfOpenMaxCalls: 5,
monitoringWindowMs: 600000 // 10分钟监控窗口
},
fallback: {
enabled: true,
fallbackProviders: ['openai', 'google', 'baidu'],
gracefulDegradation: true
}
})
```
### 运维最佳实践
1. **日志管理**
```typescript
// 配置日志级别
const serviceManager = new AIServiceManager({
...config,
logging: {
level: 'info', // production: info, development: debug
enableFileLogging: true,
logFilePath: '/var/log/ai-news/system.log'
}
})
```
2. **健康检查**
```typescript
// 设置健康检查端点
app.get('/health', async (req, res) => {
const health = monitor.getSystemHealth()
res.status(health.status === 'healthy' ? 200 : 503).json(health)
})
// 设置指标端点
app.get('/metrics', async (req, res) => {
const stats = monitor.getPerformanceStats(
Date.now() - 3600000, // 过去1小时
Date.now()
)
res.json(stats)
})
```
3. **告警配置**
```typescript
// 配置告警规则
const alertRules = {
highErrorRate: {
threshold: 0.05, // 5%错误率
duration: 300000, // 5分钟持续
action: 'send_alert'
},
highLatency: {
threshold: 5000, // 5秒延迟
duration: 180000, // 3分钟持续
action: 'send_alert'
},
costExceeded: {
threshold: 0.8, // 80%预算
action: 'send_warning'
}
}
```
4. **备份和恢复**
```typescript
// 定期备份配置和数据
setInterval(async () => {
const exportData = monitor.exportPerformanceData('json')
await saveToBackup(exportData)
}, 86400000) // 每24小时备份
// 恢复流程
async function restoreFromBackup(backupData: string) {
const data = JSON.parse(backupData)
// 恢复配置和历史数据
await monitor.importPerformanceData(data)
}
```
### 性能优化建议
1. **缓存策略**
```typescript
// 配置多层缓存
const cacheConfig = {
translation: {
maxSize: 10000,
ttl: 3600000, // 1小时
strategy: 'lru'
},
analysis: {
maxSize: 5000,
ttl: 1800000, // 30分钟
strategy: 'lru'
}
}
```
2. **批处理优化**
```typescript
// 启用智能批处理
const batchConfig = {
batchSize: 20,
maxWaitTime: 2000, // 2秒最大等待
concurrency: 5,
enableAdaptiveBatching: true
}
```
3. **提供商选择策略**
```typescript
// 配置智能提供商选择
const providerStrategy = {
selectionAlgorithm: 'performance_based',
fallbackOrder: ['openai', 'google', 'baidu'],
healthCheckInterval: 30000,
automaticFailover: true
}
```
### 监控和告警
1. **Prometheus集成**
```typescript
// 导出Prometheus指标
app.get('/prometheus', (req, res) => {
const metrics = monitor.getPrometheusMetrics()
res.set('Content-Type', 'text/plain')
res.send(metrics)
})
```
2. **Grafana仪表板**
```json
{
"dashboard": {
"title": "AI News System Dashboard",
"panels": [
{
"title": "Request Rate",
"type": "graph",
"targets": [
{
"expr": "rate(ai_news_requests_total[5m])"
}
]
},
{
"title": "Error Rate",
"type": "singlestat",
"targets": [
{
"expr": "rate(ai_news_errors_total[5m])"
}
]
}
]
}
}
```
3. **告警通知**
```typescript
// 配置告警通知
const alertConfig = {
channels: [
{
type: 'webhook',
url: 'https://hooks.slack.com/services/your-webhook',
events: ['error', 'warning', 'critical']
},
{
type: 'email',
recipients: ['admin@your-domain.com'],
events: ['critical']
}
]
}
```
- **API密钥安全**: 安全存储和管理API密钥
- **数据加密**: 敏感数据传输加密
- **访问控制**: 基于角色的访问控制
- **隐私保护**: 用户数据匿名化处理
- **审计日志**: 完整的操作审计记录
## 🎯 最佳实践
### 1. 成本优化
- 启用缓存减少重复请求
- 设置合理的成本限制
- 选择合适的AI模型
- 批量处理提高效率
### 2. 性能优化
- 配置适当的并发数
- 使用负载均衡
- 监控服务性能
- 优化缓存策略
### 3. 质量控制
- 设置质量阈值
- 多层次验证
- 人工审核关键内容
- 持续监控输出质量
### 4. 错误处理
- 实现重试机制
- 完善的错误日志
- 优雅的降级处理
- 及时告警通知
## 📝 完整示例
查看 `examples/usage-example.uts` 文件获取完整的使用示例,包括:
1. 系统初始化
2. 翻译服务使用
3. 内容分析示例
4. 智能对话演示
5. 推荐系统使用
6. 自动化处理管道
7. 系统监控示例
## 🔄 更新日志
### v1.0.0 (2024-12-19)
- ✨ 初始版本发布
- 🔄 多语言翻译服务
- 🔍 内容分析服务
- 💬 智能对话服务
- 🎯 推荐系统
- ⚙️ 自动化处理管道
- 🎛️ 统一服务管理
## 🤝 贡献
欢迎提交Issue和Pull Request来改进这个项目。
## 📄 许可证
MIT License
## 📞 技术支持
如有问题或建议,请联系开发团队。
---
*这是一个功能完整的AI新闻系统适用于需要多语言内容处理、智能分析和个性化推荐的新闻平台。*

View File

@@ -0,0 +1,898 @@
<!-- Real-time AI News System Monitoring Dashboard -->
<template>
<view class="dashboard-container">
<!-- Header -->
<view class="dashboard-header">
<text class="dashboard-title">AI News System Dashboard</text>
<view class="status-indicator" :class="systemHealth.status">
<text class="status-text">{{ systemHealth.status.toUpperCase() }}</text>
<text class="health-score">{{ systemHealth.score }}/100</text>
</view>
</view>
<!-- Quick Stats -->
<view class="quick-stats">
<view class="stat-card">
<text class="stat-value">{{ formatNumber(stats.requests.total) }}</text>
<text class="stat-label">Total Requests</text>
<text class="stat-change" :class="getChangeClass(requestsChange)">
{{ formatChange(requestsChange) }}
</text>
</view>
<view class="stat-card">
<text class="stat-value">{{ (stats.requests.successRate * 100).toFixed(1) }}%</text>
<text class="stat-label">Success Rate</text>
<text class="stat-change" :class="getChangeClass(successRateChange)">
{{ formatChange(successRateChange) }}
</text>
</view>
<view class="stat-card">
<text class="stat-value">{{ stats.timing.averageLatency.toFixed(0) }}ms</text>
<text class="stat-label">Avg Latency</text>
<text class="stat-change" :class="getChangeClass(-latencyChange)">
{{ formatChange(latencyChange) }}
</text>
</view>
<view class="stat-card">
<text class="stat-value">${{ stats.costs.total.toFixed(2) }}</text>
<text class="stat-label">Total Cost</text>
<text class="stat-change" :class="getChangeClass(-costChange)">
${{ costChange.toFixed(2) }}
</text>
</view>
</view>
<!-- Health Checks -->
<view class="health-section">
<text class="section-title">System Health Checks</text>
<view class="health-checks">
<view class="health-check"
v-for="(check, key) in systemHealth.checks"
:key="key"
:class="getHealthCheckClass(key, check)">
<text class="check-name">{{ formatCheckName(key) }}</text>
<text class="check-value">{{ formatCheckValue(key, check) }}</text>
<view class="check-indicator" :class="getHealthCheckClass(key, check)"></view>
</view>
</view>
</view>
<!-- Active Alerts -->
<view class="alerts-section" v-if="systemHealth.alerts.length > 0">
<text class="section-title">Active Alerts ({{ systemHealth.alerts.length }})</text>
<scroll-view class="alerts-list" scroll-y="true">
<view class="alert-item"
v-for="alert in systemHealth.alerts"
:key="alert.id"
:class="alert.severity">
<view class="alert-header">
<text class="alert-severity">{{ alert.severity.toUpperCase() }}</text>
<text class="alert-time">{{ formatTime(alert.timestamp) }}</text>
</view>
<text class="alert-message">{{ alert.message }}</text>
<text class="alert-source">Source: {{ alert.source }}</text>
</view>
</scroll-view>
</view>
<!-- Performance Charts -->
<view class="charts-section">
<text class="section-title">Performance Trends</text>
<!-- Response Time Chart -->
<view class="chart-container">
<text class="chart-title">Response Time (Last Hour)</text>
<view class="chart-area">
<canvas class="chart-canvas"
canvas-id="responseTimeChart"
@touchstart="onChartTouch"
@touchend="onChartTouchEnd"></canvas>
</view>
</view>
<!-- Request Volume Chart -->
<view class="chart-container">
<text class="chart-title">Request Volume</text>
<view class="chart-area">
<canvas class="chart-canvas"
canvas-id="requestVolumeChart"></canvas>
</view>
</view>
<!-- Cost Distribution -->
<view class="chart-container">
<text class="chart-title">Cost by Provider</text>
<view class="cost-breakdown">
<view class="cost-item" v-for="(cost, provider) in stats.costs.byProvider" :key="provider">
<view class="cost-bar-container">
<text class="provider-name">{{ provider }}</text>
<view class="cost-bar">
<view class="cost-fill"
:style="{ width: (cost / stats.costs.total * 100) + '%' }"></view>
</view>
<text class="cost-value">${{ cost.toFixed(2) }}</text>
</view>
</view>
</view>
</view>
</view>
<!-- Optimization Recommendations -->
<view class="recommendations-section" v-if="recommendations.length > 0">
<text class="section-title">Optimization Recommendations</text>
<scroll-view class="recommendations-list" scroll-y="true">
<view class="recommendation-item"
v-for="(rec, index) in recommendations"
:key="index"
:class="rec.priority">
<view class="rec-header">
<text class="rec-type">{{ rec.type.toUpperCase() }}</text>
<text class="rec-priority">{{ rec.priority.toUpperCase() }}</text>
</view>
<text class="rec-description">{{ rec.description }}</text>
<view class="rec-impact">
<text class="impact-title">Expected Impact:</text>
<text v-if="rec.expectedImpact.performanceGain" class="impact-item">
🚀 {{ rec.expectedImpact.performanceGain }}
</text>
<text v-if="rec.expectedImpact.costSaving" class="impact-item">
💰 {{ rec.expectedImpact.costSaving }}
</text>
<text v-if="rec.expectedImpact.reliabilityImprovement" class="impact-item">
🛡 {{ rec.expectedImpact.reliabilityImprovement }}
</text>
</view>
<view class="rec-actions">
<button class="btn-apply" @click="applyRecommendation(rec)" :disabled="rec.applying">
{{ rec.applying ? 'Applying...' : 'Apply' }}
</button>
<button class="btn-dismiss" @click="dismissRecommendation(rec)">
Dismiss
</button>
</view>
</view>
</scroll-view>
</view>
<!-- Control Panel -->
<view class="controls-section">
<text class="section-title">Controls</text>
<view class="control-buttons">
<button class="control-btn" @click="refreshData" :disabled="isRefreshing">
{{ isRefreshing ? 'Refreshing...' : 'Refresh Data' }}
</button>
<button class="control-btn" @click="exportData">
Export Metrics
</button>
<button class="control-btn" @click="toggleAutoRefresh">
{{ autoRefresh ? 'Stop Auto-Refresh' : 'Start Auto-Refresh' }}
</button>
<button class="control-btn danger" @click="clearAlerts">
Clear Alerts
</button>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted, computed } from 'vue'
import {
AIPerformanceMonitor,
type PerformanceStats,
type SystemHealth,
type OptimizationRecommendation,
defaultPerformanceConfig
} from '../services/AIPerformanceMonitor.uts'
// Reactive data
const monitor = new AIPerformanceMonitor(defaultPerformanceConfig)
const stats = ref<PerformanceStats>({
timeRange: { start: 0, end: 0, duration: 0 },
requests: { total: 0, successful: 0, failed: 0, successRate: 0 },
timing: { averageLatency: 0, medianLatency: 0, p95Latency: 0, p99Latency: 0 },
costs: { total: 0, average: 0, byProvider: {} },
cache: { hitRate: 0, totalRequests: 0, hits: 0, misses: 0 },
errors: { byType: {}, byProvider: {}, topErrors: [] }
})
const systemHealth = ref<SystemHealth>({
status: 'healthy',
score: 100,
checks: {
apiConnectivity: true,
memoryUsage: 0,
errorRate: 0,
responseTime: 0,
costBudget: 0,
cacheEfficiency: 0
},
alerts: []
})
const recommendations = ref<OptimizationRecommendation[]>([])
const isRefreshing = ref(false)
const autoRefresh = ref(true)
let refreshInterval: number | null = null
// Previous values for change calculation
const previousStats = ref<PerformanceStats | null>(null)
// Computed properties for changes
const requestsChange = computed(() => {
if (!previousStats.value) return 0
return stats.value.requests.total - previousStats.value.requests.total
})
const successRateChange = computed(() => {
if (!previousStats.value) return 0
return stats.value.requests.successRate - previousStats.value.requests.successRate
})
const latencyChange = computed(() => {
if (!previousStats.value) return 0
return stats.value.timing.averageLatency - previousStats.value.timing.averageLatency
})
const costChange = computed(() => {
if (!previousStats.value) return 0
return stats.value.costs.total - previousStats.value.costs.total
})
// Lifecycle hooks
onMounted(async () => {
console.log('🚀 Starting monitoring dashboard...')
monitor.startMonitoring()
await refreshData()
if (autoRefresh.value) {
startAutoRefresh()
}
})
onUnmounted(() => {
console.log('🛑 Stopping monitoring dashboard...')
stopAutoRefresh()
monitor.stopMonitoring()
})
// Methods
const refreshData = async () => {
if (isRefreshing.value) return
isRefreshing.value = true
try {
// Store previous stats for change calculation
previousStats.value = { ...stats.value }
// Get fresh data
const now = Date.now()
const oneHourAgo = now - 3600000
stats.value = monitor.getPerformanceStats(oneHourAgo, now)
systemHealth.value = monitor.getSystemHealth()
recommendations.value = monitor.getOptimizationRecommendations()
console.log('📊 Dashboard data refreshed')
} catch (error) {
console.error('❌ Failed to refresh dashboard data:', error)
} finally {
isRefreshing.value = false
}
}
const startAutoRefresh = () => {
if (refreshInterval) return
refreshInterval = setInterval(async () => {
await refreshData()
}, 30000) // Refresh every 30 seconds
console.log('🔄 Auto-refresh started')
}
const stopAutoRefresh = () => {
if (refreshInterval) {
clearInterval(refreshInterval)
refreshInterval = null
console.log('⏹️ Auto-refresh stopped')
}
}
const toggleAutoRefresh = () => {
autoRefresh.value = !autoRefresh.value
if (autoRefresh.value) {
startAutoRefresh()
} else {
stopAutoRefresh()
}
}
const applyRecommendation = async (rec: OptimizationRecommendation) => {
rec.applying = true
try {
const result = await monitor.applyOptimizations([rec])
if (result.applied > 0) {
console.log('✅ Recommendation applied successfully')
// Remove from list
const index = recommendations.value.indexOf(rec)
if (index > -1) {
recommendations.value.splice(index, 1)
}
} else {
console.error('❌ Failed to apply recommendation')
}
} catch (error) {
console.error('💥 Error applying recommendation:', error)
} finally {
rec.applying = false
}
}
const dismissRecommendation = (rec: OptimizationRecommendation) => {
const index = recommendations.value.indexOf(rec)
if (index > -1) {
recommendations.value.splice(index, 1)
}
}
const exportData = () => {
try {
const data = monitor.exportPerformanceData('json')
// In real implementation, this would trigger a download
console.log('📤 Exporting performance data...')
console.log(data)
// For uni-app, you might want to save to local storage or share
uni.setStorageSync('ai-news-performance-data', data)
uni.showToast({
title: 'Data exported to storage',
icon: 'success'
})
} catch (error) {
console.error('❌ Failed to export data:', error)
uni.showToast({
title: 'Export failed',
icon: 'error'
})
}
}
const clearAlerts = () => {
systemHealth.value.alerts = []
uni.showToast({
title: 'Alerts cleared',
icon: 'success'
})
}
// Utility methods
const formatNumber = (num: number): string => {
if (num >= 1000000) {
return (num / 1000000).toFixed(1) + 'M'
} else if (num >= 1000) {
return (num / 1000).toFixed(1) + 'K'
}
return num.toString()
}
const formatChange = (change: number): string => {
if (change === 0) return '0'
const sign = change > 0 ? '+' : ''
return `${sign}${change.toFixed(1)}`
}
const getChangeClass = (change: number): string => {
if (change > 0) return 'positive'
if (change < 0) return 'negative'
return 'neutral'
}
const formatCheckName = (key: string): string => {
return key.replace(/([A-Z])/g, ' $1').replace(/^./, str => str.toUpperCase())
}
const formatCheckValue = (key: string, value: any): string => {
switch (key) {
case 'apiConnectivity':
return value ? 'Connected' : 'Disconnected'
case 'memoryUsage':
return (value * 100).toFixed(1) + '%'
case 'errorRate':
return (value * 100).toFixed(2) + '%'
case 'responseTime':
return value.toFixed(0) + 'ms'
case 'costBudget':
return (value * 100).toFixed(1) + '%'
case 'cacheEfficiency':
return (value * 100).toFixed(1) + '%'
default:
return String(value)
}
}
const getHealthCheckClass = (key: string, value: any): string => {
switch (key) {
case 'apiConnectivity':
return value ? 'healthy' : 'critical'
case 'memoryUsage':
return value > 0.8 ? 'critical' : value > 0.6 ? 'warning' : 'healthy'
case 'errorRate':
return value > 0.1 ? 'critical' : value > 0.05 ? 'warning' : 'healthy'
case 'responseTime':
return value > 5000 ? 'critical' : value > 3000 ? 'warning' : 'healthy'
case 'costBudget':
return value > 0.9 ? 'critical' : value > 0.7 ? 'warning' : 'healthy'
case 'cacheEfficiency':
return value < 0.3 ? 'warning' : value < 0.5 ? 'healthy' : 'excellent'
default:
return 'neutral'
}
}
const formatTime = (timestamp: number): string => {
const now = Date.now()
const diff = now - timestamp
if (diff < 60000) return 'Just now'
if (diff < 3600000) return Math.floor(diff / 60000) + 'm ago'
if (diff < 86400000) return Math.floor(diff / 3600000) + 'h ago'
return new Date(timestamp).toLocaleDateString()
}
// Chart event handlers
const onChartTouch = (e: any) => {
console.log('Chart touched:', e)
}
const onChartTouchEnd = (e: any) => {
console.log('Chart touch ended:', e)
}
</script>
<style scoped>
.dashboard-container {
padding: 20px;
background-color: #f5f5f5;
min-height: 100vh;
}
.dashboard-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.dashboard-title {
font-size: 24px;
font-weight: bold;
color: #333;
}
.status-indicator {
display: flex;
flex-direction: column;
align-items: center;
padding: 10px 20px;
border-radius: 8px;
min-width: 100px;
}
.status-indicator.healthy {
background-color: #e8f5e8;
border: 2px solid #4caf50;
}
.status-indicator.warning {
background-color: #fff3e0;
border: 2px solid #ff9800;
}
.status-indicator.critical {
background-color: #ffebee;
border: 2px solid #f44336;
}
.status-text {
font-weight: bold;
font-size: 14px;
}
.health-score {
font-size: 18px;
font-weight: bold;
margin-top: 4px;
}
.quick-stats {
display: flex;
gap: 15px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.stat-card {
flex: 1;
min-width: 200px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
text-align: center;
}
.stat-value {
display: block;
font-size: 28px;
font-weight: bold;
color: #333;
margin-bottom: 8px;
}
.stat-label {
display: block;
font-size: 14px;
color: #666;
margin-bottom: 8px;
}
.stat-change {
display: block;
font-size: 12px;
font-weight: bold;
}
.stat-change.positive {
color: #4caf50;
}
.stat-change.negative {
color: #f44336;
}
.stat-change.neutral {
color: #666;
}
.health-section,
.alerts-section,
.charts-section,
.recommendations-section,
.controls-section {
margin-bottom: 20px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.section-title {
display: block;
font-size: 18px;
font-weight: bold;
color: #333;
margin-bottom: 15px;
}
.health-checks {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
}
.health-check {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px;
border-radius: 8px;
border-left: 4px solid #ddd;
}
.health-check.healthy {
background-color: #e8f5e8;
border-left-color: #4caf50;
}
.health-check.warning {
background-color: #fff3e0;
border-left-color: #ff9800;
}
.health-check.critical {
background-color: #ffebee;
border-left-color: #f44336;
}
.check-name {
font-weight: bold;
color: #333;
}
.check-value {
color: #666;
}
.alerts-list {
max-height: 300px;
}
.alert-item {
padding: 15px;
margin-bottom: 10px;
border-radius: 8px;
border-left: 4px solid #ddd;
}
.alert-item.info {
background-color: #e3f2fd;
border-left-color: #2196f3;
}
.alert-item.warning {
background-color: #fff3e0;
border-left-color: #ff9800;
}
.alert-item.error {
background-color: #ffebee;
border-left-color: #f44336;
}
.alert-item.critical {
background-color: #fce4ec;
border-left-color: #e91e63;
}
.alert-header {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
}
.alert-severity {
font-weight: bold;
font-size: 12px;
}
.alert-time {
font-size: 12px;
color: #666;
}
.alert-message {
display: block;
margin-bottom: 4px;
color: #333;
}
.alert-source {
display: block;
font-size: 12px;
color: #666;
}
.chart-container {
margin-bottom: 30px;
}
.chart-title {
display: block;
font-size: 16px;
font-weight: bold;
color: #333;
margin-bottom: 15px;
}
.chart-area {
height: 200px;
position: relative;
}
.chart-canvas {
width: 100%;
height: 100%;
}
.cost-breakdown {
display: flex;
flex-direction: column;
gap: 10px;
}
.cost-item {
display: flex;
align-items: center;
gap: 15px;
}
.cost-bar-container {
display: flex;
align-items: center;
gap: 10px;
flex: 1;
}
.provider-name {
min-width: 80px;
font-weight: bold;
color: #333;
}
.cost-bar {
flex: 1;
height: 20px;
background-color: #f0f0f0;
border-radius: 10px;
overflow: hidden;
}
.cost-fill {
height: 100%;
background: linear-gradient(90deg, #4caf50, #2196f3);
transition: width 0.3s ease;
}
.cost-value {
min-width: 60px;
text-align: right;
font-weight: bold;
color: #333;
}
.recommendations-list {
max-height: 400px;
}
.recommendation-item {
padding: 15px;
margin-bottom: 15px;
border-radius: 8px;
border-left: 4px solid #ddd;
}
.recommendation-item.low {
background-color: #f9f9f9;
border-left-color: #9e9e9e;
}
.recommendation-item.medium {
background-color: #fff3e0;
border-left-color: #ff9800;
}
.recommendation-item.high {
background-color: #ffebee;
border-left-color: #f44336;
}
.recommendation-item.critical {
background-color: #fce4ec;
border-left-color: #e91e63;
}
.rec-header {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.rec-type {
font-weight: bold;
font-size: 12px;
padding: 4px 8px;
background-color: #e0e0e0;
border-radius: 4px;
}
.rec-priority {
font-weight: bold;
font-size: 12px;
padding: 4px 8px;
border-radius: 4px;
}
.rec-description {
display: block;
margin-bottom: 10px;
color: #333;
line-height: 1.4;
}
.rec-impact {
margin-bottom: 15px;
}
.impact-title {
display: block;
font-weight: bold;
color: #333;
margin-bottom: 5px;
}
.impact-item {
display: block;
margin-bottom: 2px;
color: #666;
font-size: 14px;
}
.rec-actions {
display: flex;
gap: 10px;
}
.btn-apply,
.btn-dismiss {
padding: 8px 16px;
border: none;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
}
.btn-apply {
background-color: #4caf50;
color: white;
}
.btn-apply:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.btn-dismiss {
background-color: #f5f5f5;
color: #333;
}
.control-buttons {
display: flex;
gap: 15px;
flex-wrap: wrap;
}
.control-btn {
padding: 12px 24px;
border: 2px solid #2196f3;
background-color: #2196f3;
color: white;
border-radius: 8px;
font-size: 14px;
font-weight: bold;
cursor: pointer;
transition: all 0.3s ease;
}
.control-btn:hover {
background-color: #1976d2;
border-color: #1976d2;
}
.control-btn:disabled {
background-color: #ccc;
border-color: #ccc;
cursor: not-allowed;
}
.control-btn.danger {
background-color: #f44336;
border-color: #f44336;
}
.control-btn.danger:hover {
background-color: #d32f2f;
border-color: #d32f2f;
}
</style>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,546 @@
// AI News System Usage Examples
import {
AIServiceManager,
AITranslationService,
AIContentAnalysisService,
AIChatService,
AIRecommendationService,
ContentProcessingPipeline,
type AIServiceConfig,
type ContentInfo,
type ChatOptions
} from '../index.uts'
/**
* AI新闻系统使用示例
* 展示如何集成和使用多语言AI新闻系统的各种功能
*/
export class AINewsSystemExample {
private serviceManager: AIServiceManager
constructor() {
// 配置AI服务
const aiConfig: AIServiceConfig = {
openai: {
apiKey: 'your-openai-api-key',
model: 'gpt-3.5-turbo',
maxTokens: 2000,
temperature: 0.7
},
google: {
apiKey: 'your-google-api-key',
model: 'gemini-pro'
},
baidu: {
apiKey: 'your-baidu-api-key',
secretKey: 'your-baidu-secret-key',
model: 'ernie-bot'
},
costLimits: {
dailyUSD: 100,
monthlyUSD: 2000,
perRequestUSD: 5
},
qualityThresholds: {
translation: 0.8,
sentiment: 0.7,
credibility: 0.6
}
}
// 初始化服务管理器
this.serviceManager = new AIServiceManager(aiConfig)
}
/**
* 初始化系统
*/
async initializeSystem(): Promise<void> {
console.log('🚀 Initializing AI News System...')
const response = await this.serviceManager.initialize()
if (response.success) {
console.log('✅ AI News System initialized successfully')
} else {
console.error('❌ Failed to initialize AI News System:', response.error)
throw new Error(response.error)
}
}
/**
* 示例1: 新闻内容翻译
*/
async exampleTranslation(): Promise<void> {
console.log('\n📝 Example 1: News Translation')
console.log('================================')
const translationService = this.serviceManager.getTranslationService()
// 翻译中文新闻到英文
const chineseNews = "人工智能技术在新闻行业的应用正在快速发展,自动化内容生成、智能推荐和多语言翻译等功能大大提高了新闻生产和传播的效率。"
console.log('Original Chinese text:', chineseNews)
const translationResult = await translationService.translateText(
chineseNews,
'en',
'zh-CN',
{
provider: 'openai',
culturalAdaptation: true,
preserveFormatting: true
}
)
if (translationResult.success && translationResult.data) {
console.log('✅ Translation successful:')
console.log('- Translated text:', translationResult.data.translatedText)
console.log('- Quality score:', translationResult.data.qualityScore)
console.log('- Provider:', translationResult.data.provider)
console.log('- Tokens used:', translationResult.data.tokensUsed)
console.log('- Cost:', `$${translationResult.data.costUSD.toFixed(4)}`)
} else {
console.error('❌ Translation failed:', translationResult.error)
}
// 批量翻译示例
const newsTexts = [
"今日股市行情分析",
"科技创新推动经济发展",
"环保政策最新动态"
]
console.log('\n📚 Batch translation example:')
const batchResult = await translationService.translateBatch(
newsTexts,
'en',
'zh-CN',
{ provider: 'google' },
{
batchSize: 2,
concurrency: 2,
onProgress: (completed, total) => {
console.log(`Progress: ${completed}/${total}`)
}
}
)
if (batchResult.success && batchResult.data) {
console.log('✅ Batch translation completed:')
batchResult.data.forEach((result, index) => {
console.log(`${index + 1}. ${result.translatedText}`)
})
}
}
/**
* 示例2: 新闻内容分析
*/
async exampleContentAnalysis(): Promise<void> {
console.log('\n🔍 Example 2: Content Analysis')
console.log('================================')
const analysisService = this.serviceManager.getAnalysisService()
const newsContent = `
特斯拉公司今日宣布,其最新的自动驾驶技术取得重大突破。
该技术采用先进的人工智能算法,能够在复杂路况下实现更安全的自动驾驶。
据公司CEO埃隆·马斯克表示这项技术将在未来六个月内开始量产。
市场分析师认为,这一创新将进一步巩固特斯拉在电动汽车市场的领先地位。
投资者对此消息反应积极特斯拉股价在盘后交易中上涨了8%。
`
console.log('Analyzing content:', newsContent.substring(0, 100) + '...')
const analysisResult = await analysisService.analyzeContent(newsContent, {
types: ['sentiment', 'entities', 'topics', 'categories', 'readability', 'credibility', 'summary', 'keywords'],
provider: 'openai',
language: 'zh-CN',
includeScores: true
})
if (analysisResult.success && analysisResult.data) {
const analysis = analysisResult.data
console.log('✅ Analysis completed:')
console.log('- Sentiment:', analysis.sentimentLabel, `(${analysis.sentimentScore.toFixed(2)})`)
console.log('- Readability score:', analysis.readabilityScore.toFixed(2))
console.log('- Credibility score:', analysis.credibilityScore.toFixed(2))
console.log('- Keywords:', analysis.keywords.join(', '))
console.log('- Entities found:', analysis.entities.length)
analysis.entities.forEach(entity => {
console.log(` - ${entity.text} (${entity.type}, confidence: ${entity.confidence.toFixed(2)})`)
})
console.log('- Categories:')
analysis.categories.forEach(category => {
console.log(` - ${category.categoryName} (confidence: ${category.confidence.toFixed(2)})`)
})
console.log('- Summary:', analysis.summary)
} else {
console.error('❌ Analysis failed:', analysisResult.error)
}
}
/**
* 示例3: AI聊天助手
*/
async exampleChatAssistant(): Promise<void> {
console.log('\n💬 Example 3: AI Chat Assistant')
console.log('================================')
const chatService = this.serviceManager.getChatService()
// 创建聊天会话
const sessionResponse = await chatService.createChatSession(
'user123',
'zh-CN',
{
provider: 'openai',
temperature: 0.7,
maxTokens: 1000
}
)
if (sessionResponse.success && sessionResponse.data) {
const session = sessionResponse.data
console.log('✅ Chat session created:', session.id)
// 发送消息
const messages = [
"今天有什么重要的科技新闻吗?",
"请分析一下人工智能对新闻行业的影响",
"帮我推荐一些相关的新闻文章"
]
for (const messageText of messages) {
console.log(`\n👤 User: ${messageText}`)
const messageResponse = await chatService.sendMessage(
session.id,
messageText,
{ provider: 'openai' }
)
if (messageResponse.success && messageResponse.data) {
const message = messageResponse.data
console.log(`🤖 Assistant: ${message.content}`)
console.log(` Response time: ${message.responseTimeMs}ms`)
console.log(` Tokens used: ${message.tokensUsed}`)
} else {
console.error('❌ Message failed:', messageResponse.error)
}
// 模拟对话间隔
await this.delay(1000)
}
// 获取对话历史
const history = chatService.getChatHistory(session.id, 10)
console.log(`\n📋 Conversation history (${history.length} messages):`)
history.forEach((msg, index) => {
console.log(`${index + 1}. [${msg.type}] ${msg.content.substring(0, 50)}...`)
})
// 结束会话
await chatService.endChatSession(session.id)
console.log('✅ Chat session ended')
} else {
console.error('❌ Failed to create chat session:', sessionResponse.error)
}
}
/**
* 示例4: 个性化推荐
*/
async exampleRecommendations(): Promise<void> {
console.log('\n🎯 Example 4: Personalized Recommendations')
console.log('==========================================')
const recommendationService = this.serviceManager.getRecommendationService()
// 模拟新闻内容
const availableNews: ContentInfo[] = [
{
id: 'news1',
title: 'AI技术突破新一代自然语言处理模型发布',
content: '研究人员发布了新的大型语言模型,在多项任务上超越了现有技术...',
originalLanguage: 'zh-CN',
publishedAt: Date.now() - 1000 * 60 * 60, // 1小时前
categoryId: 'technology',
tags: ['AI', '技术', '创新'],
keywords: ['人工智能', '语言模型', '技术突破'],
quality: 0.9,
viewCount: 1500,
likeCount: 120,
shareCount: 45,
status: 'published'
},
{
id: 'news2',
title: '全球经济展望:数字化转型加速发展',
content: '世界银行最新报告显示,数字化转型正在重塑全球经济格局...',
originalLanguage: 'zh-CN',
publishedAt: Date.now() - 1000 * 60 * 60 * 2, // 2小时前
categoryId: 'economy',
tags: ['经济', '数字化', '转型'],
keywords: ['经济发展', '数字化', '全球'],
quality: 0.85,
viewCount: 2100,
likeCount: 180,
shareCount: 67,
status: 'published'
},
{
id: 'news3',
title: '环保新政策:碳中和目标实施细则发布',
content: '政府发布了实现碳中和目标的详细实施方案,涉及多个行业的转型升级...',
originalLanguage: 'zh-CN',
publishedAt: Date.now() - 1000 * 60 * 60 * 3, // 3小时前
categoryId: 'environment',
tags: ['环保', '政策', '碳中和'],
keywords: ['环保政策', '碳中和', '可持续发展'],
quality: 0.88,
viewCount: 980,
likeCount: 75,
shareCount: 23,
status: 'published'
}
]
// 记录用户行为(模拟)
await recommendationService.recordUserBehavior({
userId: 'user123',
contentId: 'news1',
actionType: 'view',
timestamp: Date.now() - 1000 * 60 * 30, // 30分钟前
duration: 120 // 阅读了2分钟
})
await recommendationService.recordUserBehavior({
userId: 'user123',
contentId: 'news1',
actionType: 'like',
timestamp: Date.now() - 1000 * 60 * 25
})
// 获取个性化推荐
console.log('Generating personalized recommendations...')
const recommendationResponse = await recommendationService.getPersonalizedRecommendations(
'user123',
availableNews,
{
algorithm: 'hybrid',
maxResults: 5,
diversityWeight: 0.3,
freshnessWeight: 0.4,
personalizedWeight: 0.3,
qualityThreshold: 0.7,
excludeViewed: false
},
{
currentTime: Date.now(),
recentViews: ['news1'],
deviceType: 'mobile'
}
)
if (recommendationResponse.success && recommendationResponse.data) {
console.log('✅ Personalized recommendations:')
recommendationResponse.data.forEach((rec, index) => {
const news = availableNews.find(n => n.id === rec.contentId)
console.log(`${index + 1}. ${news?.title}`)
console.log(` Score: ${rec.score.toFixed(3)}, Reason: ${rec.reason}`)
console.log(` Algorithm: ${rec.algorithm}, Type: ${rec.recommendationType}`)
})
} else {
console.error('❌ Recommendations failed:', recommendationResponse.error)
}
// 获取热门推荐
console.log('\n🔥 Trending recommendations:')
const trendingResponse = await recommendationService.getTrendingRecommendations(
availableNews,
24, // 24小时内
3
)
if (trendingResponse.success && trendingResponse.data) {
trendingResponse.data.forEach((rec, index) => {
const news = availableNews.find(n => n.id === rec.contentId)
console.log(`${index + 1}. ${news?.title} (Score: ${rec.score.toFixed(3)})`)
})
}
}
/**
* 示例5: 自动化内容处理管道
*/
async exampleContentPipeline(): Promise<void> {
console.log('\n⚙ Example 5: Automated Content Processing')
console.log('==========================================')
const pipeline = this.serviceManager.getProcessingPipeline()
// 模拟原始新闻内容
const rawContent: ContentInfo = {
id: 'raw_news_001',
title: '突破性医疗技术:基因编辑治疗癌症取得重大进展',
content: `
美国斯坦福大学医学院的研究团队今日宣布,他们在基因编辑技术治疗癌症方面取得了重大突破。
该团队利用CRISPR-Cas9技术成功修改了T细胞的基因使其能够更有效地识别和攻击癌细胞。
在临床试验中接受治疗的20名患者中有18名病情得到显著改善治疗有效率达到90%。
研究负责人张华教授表示这项技术有望在未来3-5年内进入大规模临床应用阶段。
美国食品药品监督管理局(FDA)已批准该技术进入二期临床试验。
业内专家认为,这一突破将为癌症治疗带来革命性变化,可能挽救数百万患者的生命。
`.trim(),
originalLanguage: 'zh-CN',
publishedAt: Date.now(),
tags: [],
keywords: [],
quality: 0,
viewCount: 0,
likeCount: 0,
shareCount: 0,
status: 'draft'
}
console.log('Processing content:', rawContent.title)
console.log('Original content length:', rawContent.content.length, 'characters')
// 执行自动化处理
const processingResponse = await pipeline.processContent(rawContent)
if (processingResponse.success && processingResponse.data) {
const result = processingResponse.data
console.log('✅ Content processing completed:')
console.log('- Processing time:', `${result.processingTime}ms`)
console.log('- Total cost:', `$${result.totalCost.toFixed(4)}`)
console.log('- Final status:', result.status)
console.log('- Quality score:', result.qualityScore.toFixed(3))
console.log('\n📊 Analysis results:')
if (result.analysis) {
console.log('- Sentiment:', result.analysis.sentimentLabel, `(${result.analysis.sentimentScore.toFixed(2)})`)
console.log('- Readability:', result.analysis.readabilityScore.toFixed(2))
console.log('- Credibility:', result.analysis.credibilityScore.toFixed(2))
console.log('- Keywords:', result.analysis.keywords.slice(0, 5).join(', '))
}
console.log('\n🌐 Translation results:')
Object.entries(result.translations).forEach(([lang, translation]) => {
console.log(`- ${lang}: ${translation.translatedText.substring(0, 80)}...`)
console.log(` Quality: ${translation.qualityScore.toFixed(2)}, Cost: $${translation.costUSD.toFixed(4)}`)
})
console.log('\n📂 Categories:', result.categories.join(', '))
if (result.errors.length > 0) {
console.log('\n⚠ Errors encountered:')
result.errors.forEach(error => console.log(`- ${error}`))
}
} else {
console.error('❌ Content processing failed:', processingResponse.error)
}
}
/**
* 示例6: 系统监控和统计
*/
async exampleSystemMonitoring(): Promise<void> {
console.log('\n📈 Example 6: System Monitoring')
console.log('================================')
// 获取服务健康状态
const servicesHealth = this.serviceManager.getServicesHealth()
console.log('🏥 Services Health Status:')
Object.entries(servicesHealth).forEach(([serviceName, health]) => {
const statusIcon = health.status === 'ready' ? '✅' : health.status === 'error' ? '❌' : '⚠️'
console.log(`${statusIcon} ${serviceName}: ${health.status}`)
console.log(` Response time: ${health.responseTime}ms`)
console.log(` Error rate: ${(health.errorRate * 100).toFixed(1)}%`)
console.log(` Capabilities: ${health.capabilities.join(', ')}`)
})
// 获取管理器统计
const stats = this.serviceManager.getManagerStatistics()
console.log('\n📊 Manager Statistics:')
console.log('- Total requests:', stats.totalRequests)
console.log('- Success rate:', `${((stats.successfulRequests / Math.max(stats.totalRequests, 1)) * 100).toFixed(1)}%`)
console.log('- Average response time:', `${stats.avgResponseTime.toFixed(0)}ms`)
console.log('- Total cost:', `$${stats.totalCost.toFixed(2)}`)
console.log('\n💰 Cost breakdown by provider:')
Object.entries(stats.costBreakdown).forEach(([provider, cost]) => {
if (cost > 0) {
console.log(`- ${provider}: $${cost.toFixed(4)}`)
}
})
// 获取各服务的详细统计
console.log('\n📋 Individual Service Statistics:')
const translationStats = this.serviceManager.getTranslationService().getStatistics()
console.log('Translation Service:')
console.log(`- Total requests: ${translationStats.totalRequests}`)
console.log(`- Success rate: ${((translationStats.successCount / Math.max(translationStats.totalRequests, 1)) * 100).toFixed(1)}%`)
console.log(`- Cache hit rate: ${(translationStats.cacheHitRate * 100).toFixed(1)}%`)
console.log(`- Average quality: ${translationStats.avgQuality.toFixed(2)}`)
const analysisStats = this.serviceManager.getAnalysisService().getStatistics()
console.log('\nAnalysis Service:')
console.log(`- Total analyses: ${analysisStats.totalAnalyses}`)
console.log(`- Success rate: ${((analysisStats.successCount / Math.max(analysisStats.totalAnalyses, 1)) * 100).toFixed(1)}%`)
console.log(`- Average processing time: ${analysisStats.avgProcessingTimeMs.toFixed(0)}ms`)
const chatStats = this.serviceManager.getChatService().getChatStatistics()
console.log('\nChat Service:')
console.log(`- Total sessions: ${chatStats.totalSessions}`)
console.log(`- Total messages: ${chatStats.totalMessages}`)
console.log(`- Average response time: ${chatStats.avgResponseTime.toFixed(0)}ms`)
const recommendationStats = this.serviceManager.getRecommendationService().getRecommendationStatistics()
console.log('\nRecommendation Service:')
console.log(`- Total recommendations: ${recommendationStats.totalRecommendations}`)
console.log(`- Click-through rate: ${(recommendationStats.clickThroughRate * 100).toFixed(1)}%`)
console.log(`- User satisfaction: ${recommendationStats.userSatisfactionScore.toFixed(2)}`)
}
/**
* 运行所有示例
*/
async runAllExamples(): Promise<void> {
try {
await this.initializeSystem()
await this.exampleTranslation()
await this.exampleContentAnalysis()
await this.exampleChatAssistant()
await this.exampleRecommendations()
await this.exampleContentPipeline()
await this.exampleSystemMonitoring()
console.log('\n🎉 All examples completed successfully!')
} catch (error) {
console.error('\n💥 Example execution failed:', error)
} finally {
// 清理资源
await this.serviceManager.shutdown()
console.log('\n🛑 System shutdown completed')
}
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
}
// 导出使用示例函数
export async function runAINewsSystemExamples(): Promise<void> {
const example = new AINewsSystemExample()
await example.runAllExamples()
}
// 如果直接运行此文件,执行示例
if (typeof require !== 'undefined' && require.main === module) {
runAINewsSystemExamples().catch(console.error)
}

View File

@@ -0,0 +1,44 @@
// AI News Service Module Index
export { AITranslationService } from './services/AITranslationService.uts'
export { AIContentAnalysisService } from './services/AIContentAnalysisService.uts'
export { AIChatService } from './services/AIChatService.uts'
export { AIRecommendationService } from './services/AIRecommendationService.uts'
export { ContentProcessingPipeline } from './services/ContentProcessingPipeline.uts'
export { AIServiceManager } from './services/AIServiceManager.uts'
export { AIPerformanceMonitor, defaultPerformanceConfig } from './services/AIPerformanceMonitor.uts'
export { AIErrorHandler, defaultErrorHandlingConfig, ErrorCategory, CircuitBreakerState } from './services/AIErrorHandler.uts'
// Export types
export type {
TranslationResult,
TranslationOptions,
ContentAnalysisResult,
EntityResult,
TopicResult,
CategoryResult,
ChatMessage,
ChatSession,
RecommendationResult,
ContentInfo,
AIProvider,
AIServiceConfig,
AIResponse,
ProcessingStep,
UsageStatistics,
CacheOptions,
BatchProcessingOptions,
PerformanceMetrics,
SystemHealth,
OptimizationRecommendation,
ErrorInfo,
OperationResult,
ErrorHandlingConfig
} from './types/ai-types.uts'
// Module constants
export const AI_NEWS_MODULE_VERSION = '1.0.0'
export const AI_NEWS_MODULE_NAME = 'ak-ai-news'
// Module description
export const AI_NEWS_MODULE_DESCRIPTION = 'Comprehensive AI-driven multilingual news system with translation, analysis, chat, and recommendation services'

View File

@@ -0,0 +1,133 @@
{
"id": "ak-ai-news",
"displayName": "AK-AI-News 多语言AI新闻系统",
"name": "ak-ai-news",
"version": "1.0.0",
"description": "功能完整的多语言AI驱动新闻系统提供内容翻译、分析、智能对话和个性化推荐等服务",
"keywords": [
"ai",
"news",
"translation",
"multilingual",
"analysis",
"chat",
"recommendation",
"openai",
"uniapp",
"uts"
],
"main": "index.uts",
"author": {
"name": "AK Development Team",
"email": "dev@ak-tech.com"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/ak-tech/ak-ai-news"
},
"bugs": {
"url": "https://github.com/ak-tech/ak-ai-news/issues"
},
"homepage": "https://github.com/ak-tech/ak-ai-news#readme",
"engines": {
"HBuilderX": "^3.8.0"
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y",
"alipay": "y"
},
"client": {
"App": {
"app-vue": "y",
"app-nvue": "y",
"app-uvue": "y"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "y",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "y",
"百度": "y",
"字节跳动": "y",
"QQ": "y",
"钉钉": "y",
"快手": "y",
"飞书": "y",
"京东": "y"
},
"快应用": {
"华为": "y",
"联盟": "y"
},
"Vue": {
"vue2": "y",
"vue3": "y"
}
}
}
},
"dcloudext": {
"category": [
"前端组件",
"通用组件",
"AI服务"
],
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "插件不采集任何数据",
"permissions": "无"
},
"npmurl": ""
}, "dependencies": {
"ak-req": "^1.0.0"
},
"scripts": {
"test": "node test/simple-test.uts",
"test:integration": "node test/integration-test.uts",
"test:comprehensive": "node test/comprehensive-test-runner.uts",
"test:performance": "node test/comprehensive-test-runner.uts --performance-only",
"build": "echo 'Building UTS plugin...'",
"lint": "echo 'Linting code...'",
"docs": "echo 'Generating documentation...'",
"benchmark": "node examples/usage-example.uts --benchmark",
"health-check": "node test/comprehensive-test-runner.uts --health-check-only"
}, "files": [
"index.uts",
"types/",
"services/",
"components/",
"examples/",
"test/",
"README.md",
"CHANGELOG.md"
],
"types": "types/ai-types.uts"
}

View File

@@ -0,0 +1,866 @@
// AI Chat Service - Multilingual news assistant and conversation management
import {
ChatMessage,
ChatSession,
AIProvider,
AIResponse,
AIServiceConfig,
AIServiceError,
ContentInfo
} from '../types/ai-types.uts'
// 聊天配置选项
type ChatOptions = {
provider?: AIProvider
model?: string
temperature?: number
maxTokens?: number
language?: string
systemPrompt?: string
contextWindow?: number
streamResponse?: boolean
}
// 会话上下文
type SessionContext = {
recentNews?: ContentInfo[]
userPreferences?: UserPreferences
conversationHistory: ChatMessage[]
currentTopic?: string
activeLanguage: string
}
// 用户偏好
type UserPreferences = {
preferredLanguages: string[]
preferredCategories: string[]
preferredSources: string[]
newsStyle: 'brief' | 'detailed' | 'analytical'
updateFrequency: 'realtime' | 'hourly' | 'daily'
}
// 聊天统计
type ChatStats = {
totalSessions: number
totalMessages: number
avgSessionLength: number
avgResponseTime: number
totalCost: number
languageDistribution: Record<string, number>
topQuestionTypes: Record<string, number>
}
// 预定义的聊天模板
type ChatTemplate = {
id: string
name: string
description: string
systemPrompt: string
language: string
category: string
}
/**
* AI聊天服务类
* 提供多语言新闻助手功能,包括会话管理、上下文理解、个性化回复等
*/
export class AIChatService {
private config: AIServiceConfig
private activeSessions: Map<string, SessionContext> = new Map()
private chatTemplates: ChatTemplate[] = []
private stats: ChatStats = {
totalSessions: 0,
totalMessages: 0,
avgSessionLength: 0,
avgResponseTime: 0,
totalCost: 0,
languageDistribution: {},
topQuestionTypes: {}
}
constructor(config: AIServiceConfig) {
this.config = config
this.initializeChatTemplates()
}
/**
* 创建新的聊天会话
* @param userId 用户ID
* @param language 语言
* @param options 聊天选项
*/
async createChatSession(
userId: string,
language: string = 'zh-CN',
options: ChatOptions = {}
): Promise<AIResponse<ChatSession>> {
try {
const sessionId = this.generateSessionId(userId)
const session: ChatSession = {
id: sessionId,
userId,
name: this.generateSessionName(language),
language,
aiModel: options.model || this.getDefaultModel(options.provider),
contextSettings: {
temperature: options.temperature || 0.7,
maxTokens: options.maxTokens || 1000,
contextWindow: options.contextWindow || 10
},
totalMessages: 0,
totalTokensUsed: 0,
totalCostUSD: 0,
isActive: true,
startedAt: Date.now(),
lastMessageAt: Date.now()
}
// 初始化会话上下文
const context: SessionContext = {
conversationHistory: [],
activeLanguage: language,
currentTopic: undefined,
recentNews: [],
userPreferences: await this.loadUserPreferences(userId)
}
this.activeSessions.set(sessionId, context)
this.stats.totalSessions++
// 发送欢迎消息
const welcomeMessage = await this.generateWelcomeMessage(language)
await this.addSystemMessage(sessionId, welcomeMessage, language)
return { success: true, data: session }
} catch (error) {
return {
success: false,
error: error.message || 'Failed to create chat session'
}
}
}
/**
* 发送消息
* @param sessionId 会话ID
* @param message 用户消息
* @param options 聊天选项
*/
async sendMessage(
sessionId: string,
message: string,
options: ChatOptions = {}
): Promise<AIResponse<ChatMessage>> {
try {
const context = this.activeSessions.get(sessionId)
if (!context) {
throw new Error('Session not found')
}
const startTime = Date.now()
// 添加用户消息到历史
const userMessage: ChatMessage = {
id: this.generateMessageId(),
sessionId,
type: 'user',
content: message,
language: context.activeLanguage,
timestamp: Date.now()
}
context.conversationHistory.push(userMessage)
this.stats.totalMessages++
// 分析用户意图
const intent = await this.analyzeUserIntent(message, context)
// 生成AI回复
const provider = options.provider || this.selectBestProvider()
const response = await this.generateAIResponse(message, context, intent, provider, options)
const processingTime = Date.now() - startTime
// 创建助手消息
const assistantMessage: ChatMessage = {
id: this.generateMessageId(),
sessionId,
type: 'assistant',
content: response.content,
language: context.activeLanguage,
timestamp: Date.now(),
responseTimeMs: processingTime,
tokensUsed: response.tokensUsed,
costUSD: response.cost
}
// 添加到历史并更新上下文
context.conversationHistory.push(assistantMessage)
this.updateSessionContext(context, message, response.content, intent)
// 保持上下文窗口大小
this.trimContextHistory(context, options.contextWindow || 10)
// 更新统计
this.updateChatStats(processingTime, response.tokensUsed || 0, response.cost || 0, context.activeLanguage, intent)
return {
success: true,
data: assistantMessage,
processingTimeMs: processingTime,
tokensUsed: response.tokensUsed,
costUSD: response.cost,
provider
}
} catch (error) {
const errorMessage: ChatMessage = {
id: this.generateMessageId(),
sessionId,
type: 'error',
content: this.getErrorMessage(error.message, this.activeSessions.get(sessionId)?.activeLanguage || 'zh-CN'),
language: this.activeSessions.get(sessionId)?.activeLanguage || 'zh-CN',
timestamp: Date.now()
}
return {
success: false,
data: errorMessage,
error: error.message || 'Failed to send message'
}
}
}
/**
* 获取会话历史
* @param sessionId 会话ID
* @param limit 消息数量限制
*/
getChatHistory(sessionId: string, limit: number = 50): ChatMessage[] {
const context = this.activeSessions.get(sessionId)
if (!context) return []
return context.conversationHistory.slice(-limit)
}
/**
* 更新会话语言
* @param sessionId 会话ID
* @param language 新语言
*/
async switchLanguage(sessionId: string, language: string): Promise<AIResponse<boolean>> {
try {
const context = this.activeSessions.get(sessionId)
if (!context) {
throw new Error('Session not found')
}
const oldLanguage = context.activeLanguage
context.activeLanguage = language
// 发送语言切换确认消息
const confirmMessage = this.getLanguageSwitchMessage(oldLanguage, language)
await this.addSystemMessage(sessionId, confirmMessage, language)
return { success: true, data: true }
} catch (error) {
return {
success: false,
error: error.message || 'Failed to switch language'
}
}
}
/**
* 设置会话的新闻上下文
* @param sessionId 会话ID
* @param newsItems 新闻列表
*/
setNewsContext(sessionId: string, newsItems: ContentInfo[]): void {
const context = this.activeSessions.get(sessionId)
if (context) {
context.recentNews = newsItems.slice(0, 10) // 保留最近10条新闻
}
}
/**
* 获取推荐问题
* @param sessionId 会话ID
* @param category 新闻分类
*/
getSuggestedQuestions(sessionId: string, category?: string): string[] {
const context = this.activeSessions.get(sessionId)
if (!context) return []
const language = context.activeLanguage
const baseQuestions = this.getBaseQuestions(language)
if (category && context.recentNews) {
const categoryNews = context.recentNews.filter(news => news.categoryId === category)
if (categoryNews.length > 0) {
return this.generateCategoryQuestions(categoryNews, language)
}
}
return baseQuestions
}
/**
* 结束会话
* @param sessionId 会话ID
*/
async endChatSession(sessionId: string): Promise<AIResponse<ChatSession>> {
try {
const context = this.activeSessions.get(sessionId)
if (!context) {
throw new Error('Session not found')
}
// 生成会话总结
const summary = await this.generateSessionSummary(context)
// 添加结束消息
const endMessage = this.getSessionEndMessage(context.activeLanguage)
await this.addSystemMessage(sessionId, endMessage, context.activeLanguage)
// 更新会话状态
const session: ChatSession = {
id: sessionId,
userId: '', // 需要从其他地方获取
name: this.generateSessionName(context.activeLanguage),
language: context.activeLanguage,
aiModel: '',
contextSettings: {},
totalMessages: context.conversationHistory.length,
totalTokensUsed: this.calculateTotalTokens(context),
totalCostUSD: this.calculateTotalCost(context),
isActive: false,
startedAt: context.conversationHistory[0]?.timestamp || Date.now(),
lastMessageAt: Date.now(),
endedAt: Date.now()
}
// 清理活动会话
this.activeSessions.delete(sessionId)
return { success: true, data: session }
} catch (error) {
return {
success: false,
error: error.message || 'Failed to end chat session'
}
}
}
/**
* 获取聊天统计
*/
getChatStatistics(): ChatStats {
return { ...this.stats }
}
// Private methods
private async analyzeUserIntent(message: string, context: SessionContext): Promise<string> {
// 简单的意图识别
const lowerMessage = message.toLowerCase()
if (lowerMessage.includes('新闻') || lowerMessage.includes('news')) {
if (lowerMessage.includes('最新') || lowerMessage.includes('latest')) return 'latest_news'
if (lowerMessage.includes('推荐') || lowerMessage.includes('recommend')) return 'recommend_news'
if (lowerMessage.includes('搜索') || lowerMessage.includes('search')) return 'search_news'
return 'general_news'
}
if (lowerMessage.includes('翻译') || lowerMessage.includes('translate')) return 'translate'
if (lowerMessage.includes('总结') || lowerMessage.includes('summary')) return 'summarize'
if (lowerMessage.includes('分析') || lowerMessage.includes('analyze')) return 'analyze'
if (lowerMessage.includes('解释') || lowerMessage.includes('explain')) return 'explain'
if (lowerMessage.includes('比较') || lowerMessage.includes('compare')) return 'compare'
return 'general_chat'
}
private async generateAIResponse(
message: string,
context: SessionContext,
intent: string,
provider: AIProvider,
options: ChatOptions
): Promise<{ content: string, tokensUsed?: number, cost?: number }> {
const systemPrompt = this.buildSystemPrompt(context, intent)
const contextMessages = this.buildContextMessages(context, options.contextWindow || 10)
switch (provider) {
case 'openai':
return await this.generateResponseWithOpenAI(message, systemPrompt, contextMessages, options)
case 'google':
return await this.generateResponseWithGoogle(message, systemPrompt, contextMessages, options)
case 'baidu':
return await this.generateResponseWithBaidu(message, systemPrompt, contextMessages, options)
default:
return await this.generateBasicResponse(message, context, intent)
}
}
private buildSystemPrompt(context: SessionContext, intent: string): string {
const language = context.activeLanguage
const newsContext = context.recentNews?.length ? `当前有${context.recentNews.length}条相关新闻可供参考。` : ''
let basePrompt = ''
if (language === 'zh-CN') {
basePrompt = `你是一个专业的多语言新闻助手。你可以帮助用户了解最新新闻、分析新闻内容、回答相关问题,并提供翻译服务。${newsContext}`
switch (intent) {
case 'latest_news':
basePrompt += '用户想了解最新新闻,请提供简洁准确的新闻摘要。'
break
case 'recommend_news':
basePrompt += '用户需要新闻推荐,请根据用户偏好推荐相关新闻。'
break
case 'search_news':
basePrompt += '用户想搜索特定新闻,请帮助找到相关内容。'
break
case 'translate':
basePrompt += '用户需要翻译服务,请提供准确的翻译。'
break
case 'analyze':
basePrompt += '用户需要新闻分析,请提供客观深入的分析。'
break
default:
basePrompt += '请提供有帮助的回复,保持友好和专业。'
}
} else {
basePrompt = `You are a professional multilingual news assistant. You can help users understand the latest news, analyze news content, answer related questions, and provide translation services. ${newsContext}`
}
return basePrompt
}
private buildContextMessages(context: SessionContext, windowSize: number): ChatMessage[] {
return context.conversationHistory.slice(-windowSize * 2) // 用户和助手消息
}
private async generateResponseWithOpenAI(
message: string,
systemPrompt: string,
contextMessages: ChatMessage[],
options: ChatOptions
): Promise<{ content: string, tokensUsed: number, cost: number }> {
// 构建消息数组
const messages = [
{ role: 'system', content: systemPrompt },
...contextMessages.map(msg => ({
role: msg.type === 'user' ? 'user' : 'assistant',
content: msg.content
})),
{ role: 'user', content: message }
]
// 模拟OpenAI API调用
await this.delay(Math.random() * 1000 + 500)
const responseContent = await this.generateMockResponse(message, systemPrompt, options.language || 'zh-CN')
const tokensUsed = this.estimateTokens(messages) + this.estimateTokens([{ content: responseContent }])
const cost = this.calculateOpenAICost(tokensUsed, options.model || 'gpt-3.5-turbo')
return {
content: responseContent,
tokensUsed,
cost
}
}
private async generateResponseWithGoogle(
message: string,
systemPrompt: string,
contextMessages: ChatMessage[],
options: ChatOptions
): Promise<{ content: string, tokensUsed: number, cost: number }> {
await this.delay(Math.random() * 800 + 400)
const responseContent = await this.generateMockResponse(message, systemPrompt, options.language || 'zh-CN')
const tokensUsed = this.estimateTokens([{ content: message }, { content: responseContent }])
const cost = this.calculateGoogleCost(tokensUsed)
return {
content: responseContent,
tokensUsed,
cost
}
}
private async generateResponseWithBaidu(
message: string,
systemPrompt: string,
contextMessages: ChatMessage[],
options: ChatOptions
): Promise<{ content: string, tokensUsed: number, cost: number }> {
await this.delay(Math.random() * 600 + 300)
const responseContent = await this.generateMockResponse(message, systemPrompt, options.language || 'zh-CN')
const tokensUsed = this.estimateTokens([{ content: message }, { content: responseContent }])
const cost = this.calculateBaiduCost(tokensUsed)
return {
content: responseContent,
tokensUsed,
cost
}
}
private async generateBasicResponse(
message: string,
context: SessionContext,
intent: string
): Promise<{ content: string }> {
const language = context.activeLanguage
// 基础响应生成
switch (intent) {
case 'latest_news':
return { content: this.getLatestNewsResponse(context) }
case 'recommend_news':
return { content: this.getRecommendNewsResponse(context) }
case 'general_chat':
return { content: this.getGeneralChatResponse(message, language) }
default:
return { content: this.getDefaultResponse(language) }
}
}
private async generateMockResponse(message: string, systemPrompt: string, language: string): Promise<string> {
// 模拟AI响应生成
const responses = {
'zh-CN': [
'我理解您的问题。根据最新的新闻信息,我可以为您提供以下回复:',
'这是一个很有趣的问题。让我为您分析一下相关情况:',
'基于当前的新闻数据和分析,我的建议是:',
'感谢您的提问。关于这个话题,我可以分享以下见解:'
],
'en': [
'I understand your question. Based on the latest news information, I can provide the following response:',
'That\'s an interesting question. Let me analyze the relevant situation for you:',
'Based on current news data and analysis, my recommendation is:',
'Thank you for your question. Regarding this topic, I can share the following insights:'
]
}
const langResponses = responses[language] || responses['zh-CN']
const baseResponse = langResponses[Math.floor(Math.random() * langResponses.length)]
// 添加针对消息的具体回复
return `${baseResponse}\n\n关于"${message.substring(0, 50)}${message.length > 50 ? '...' : ''}",我建议您关注相关的最新发展和官方信息。如果您需要更具体的信息或有其他问题,请随时告诉我。`
}
private getLatestNewsResponse(context: SessionContext): string {
if (!context.recentNews || context.recentNews.length === 0) {
return context.activeLanguage === 'zh-CN'
? '抱歉,目前没有最新的新闻信息。请稍后再试或询问其他问题。'
: 'Sorry, there is no latest news information available at the moment. Please try again later or ask other questions.'
}
const language = context.activeLanguage
const news = context.recentNews.slice(0, 3)
if (language === 'zh-CN') {
let response = '以下是最新的新闻信息:\n\n'
news.forEach((item, index) => {
response += `${index + 1}. ${item.title}\n${item.summary || item.content.substring(0, 100)}...\n\n`
})
return response
} else {
let response = 'Here is the latest news information:\n\n'
news.forEach((item, index) => {
response += `${index + 1}. ${item.title}\n${item.summary || item.content.substring(0, 100)}...\n\n`
})
return response
}
}
private getRecommendNewsResponse(context: SessionContext): string {
const language = context.activeLanguage
if (language === 'zh-CN') {
return '根据您的兴趣和阅读历史,我为您推荐以下新闻:\n\n1. 科技领域的最新发展\n2. 国际时事动态\n3. 经济政策解读\n\n如果您想了解特定领域的新闻请告诉我您感兴趣的类别。'
} else {
return 'Based on your interests and reading history, I recommend the following news for you:\n\n1. Latest developments in technology\n2. International current affairs\n3. Economic policy analysis\n\nIf you want to know about news in a specific field, please tell me the category you are interested in.'
}
}
private getGeneralChatResponse(message: string, language: string): string {
if (language === 'zh-CN') {
return '我是您的新闻助手,可以帮助您了解最新新闻、分析新闻内容、提供翻译服务等。请告诉我您想了解什么,我会尽力为您提供帮助。'
} else {
return 'I am your news assistant and can help you understand the latest news, analyze news content, provide translation services, etc. Please tell me what you would like to know and I will do my best to help you.'
}
}
private getDefaultResponse(language: string): string {
if (language === 'zh-CN') {
return '感谢您的问题。作为您的新闻助手,我可以帮助您获取最新新闻、分析新闻内容、翻译文本等。请告诉我您需要什么帮助。'
} else {
return 'Thank you for your question. As your news assistant, I can help you get the latest news, analyze news content, translate text, etc. Please tell me what help you need.'
}
}
private async addSystemMessage(sessionId: string, content: string, language: string): Promise<void> {
const context = this.activeSessions.get(sessionId)
if (!context) return
const systemMessage: ChatMessage = {
id: this.generateMessageId(),
sessionId,
type: 'system',
content,
language,
timestamp: Date.now()
}
context.conversationHistory.push(systemMessage)
}
private updateSessionContext(context: SessionContext, userMessage: string, assistantResponse: string, intent: string): void {
// 更新当前话题
if (intent !== 'general_chat') {
context.currentTopic = intent
}
// 分析和更新用户偏好(简化版)
if (userMessage.includes('科技') || userMessage.includes('technology')) {
if (!context.userPreferences) context.userPreferences = { preferredLanguages: [], preferredCategories: [], preferredSources: [], newsStyle: 'brief', updateFrequency: 'daily' }
if (!context.userPreferences.preferredCategories.includes('technology')) {
context.userPreferences.preferredCategories.push('technology')
}
}
}
private trimContextHistory(context: SessionContext, windowSize: number): void {
if (context.conversationHistory.length > windowSize * 2) {
// 保留系统消息和最近的对话
const systemMessages = context.conversationHistory.filter(msg => msg.type === 'system')
const recentMessages = context.conversationHistory.slice(-windowSize * 2)
context.conversationHistory = [...systemMessages.slice(-2), ...recentMessages]
}
}
private async generateWelcomeMessage(language: string): Promise<string> {
if (language === 'zh-CN') {
return '您好!我是您的智能新闻助手。我可以帮助您:\n\n🔍 获取最新新闻\n📊 分析新闻内容\n🌐 翻译新闻文本\n💡 推荐相关新闻\n\n请告诉我您想了解什么让我们开始对话吧'
} else {
return 'Hello! I am your intelligent news assistant. I can help you:\n\n🔍 Get the latest news\n📊 Analyze news content\n🌐 Translate news text\n💡 Recommend related news\n\nPlease tell me what you want to know and let\'s start the conversation!'
}
}
private generateSessionName(language: string): string {
const now = new Date()
const timeStr = `${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`
if (language === 'zh-CN') {
return `新闻对话 ${timeStr}`
} else {
return `News Chat ${timeStr}`
}
}
private getLanguageSwitchMessage(oldLang: string, newLang: string): string {
if (newLang === 'zh-CN') {
return '语言已切换为中文。我会用中文继续为您服务。'
} else {
return 'Language has been switched to English. I will continue to serve you in English.'
}
}
private getSessionEndMessage(language: string): string {
if (language === 'zh-CN') {
return '感谢您使用新闻助手服务!如果您还有其他问题,随时可以开始新的对话。祝您有美好的一天!'
} else {
return 'Thank you for using the news assistant service! If you have any other questions, you can start a new conversation at any time. Have a great day!'
}
}
private getErrorMessage(error: string, language: string): string {
if (language === 'zh-CN') {
return '抱歉,处理您的请求时出现了问题。请稍后重试或联系技术支持。'
} else {
return 'Sorry, there was a problem processing your request. Please try again later or contact technical support.'
}
}
private getBaseQuestions(language: string): string[] {
if (language === 'zh-CN') {
return [
'今天有什么重要新闻?',
'请推荐一些科技新闻',
'最新的经济动态如何?',
'有什么国际新闻值得关注?',
'帮我分析这条新闻的影响',
'翻译这段英文新闻'
]
} else {
return [
'What important news is there today?',
'Please recommend some technology news',
'How are the latest economic developments?',
'What international news is worth paying attention to?',
'Help me analyze the impact of this news',
'Translate this Chinese news'
]
}
}
private generateCategoryQuestions(news: ContentInfo[], language: string): string[] {
if (language === 'zh-CN') {
return [
`分析一下"${news[0].title}"这条新闻`,
`这类新闻的趋势如何?`,
`相关的新闻还有哪些?`,
`这对普通人有什么影响?`
]
} else {
return [
`Analyze the news "${news[0].title}"`,
`What are the trends in this type of news?`,
`What other related news are there?`,
`What impact does this have on ordinary people?`
]
}
}
private async generateSessionSummary(context: SessionContext): Promise<string> {
const messageCount = context.conversationHistory.length
const topics = new Set(context.conversationHistory
.filter(msg => msg.type === 'user')
.map(msg => this.extractTopicFromMessage(msg.content))
)
const language = context.activeLanguage
if (language === 'zh-CN') {
return `本次对话共${messageCount}条消息,主要讨论了${Array.from(topics).join('、')}等话题。`
} else {
return `This conversation had ${messageCount} messages and mainly discussed topics such as ${Array.from(topics).join(', ')}.`
}
}
private extractTopicFromMessage(message: string): string {
// 简单的话题提取
if (message.includes('新闻') || message.includes('news')) return '新闻'
if (message.includes('科技') || message.includes('technology')) return '科技'
if (message.includes('经济') || message.includes('economy')) return '经济'
if (message.includes('政治') || message.includes('politics')) return '政治'
return '一般话题'
}
private calculateTotalTokens(context: SessionContext): number {
return context.conversationHistory
.filter(msg => msg.tokensUsed)
.reduce((total, msg) => total + (msg.tokensUsed || 0), 0)
}
private calculateTotalCost(context: SessionContext): number {
return context.conversationHistory
.filter(msg => msg.costUSD)
.reduce((total, msg) => total + (msg.costUSD || 0), 0)
}
private updateChatStats(processingTime: number, tokens: number, cost: number, language: string, intent: string): void {
this.stats.totalMessages++
this.stats.avgResponseTime = (this.stats.avgResponseTime * (this.stats.totalMessages - 1) + processingTime) / this.stats.totalMessages
this.stats.totalCost += cost
this.stats.languageDistribution[language] = (this.stats.languageDistribution[language] || 0) + 1
this.stats.topQuestionTypes[intent] = (this.stats.topQuestionTypes[intent] || 0) + 1
}
private estimateTokens(messages: Array<{ content: string }>): number {
return messages.reduce((total, msg) => total + Math.ceil(msg.content.length / 4), 0)
}
private calculateOpenAICost(tokens: number, model: string): number {
const pricing: Record<string, { input: number, output: number }> = {
'gpt-3.5-turbo': { input: 0.0015, output: 0.002 },
'gpt-4': { input: 0.03, output: 0.06 }
}
const modelPricing = pricing[model] || pricing['gpt-3.5-turbo']
return (tokens / 1000) * ((modelPricing.input + modelPricing.output) / 2)
}
private calculateGoogleCost(tokens: number): number {
return (tokens / 1000) * 0.01 // 估算
}
private calculateBaiduCost(tokens: number): number {
return (tokens / 1000) * 0.008 // 估算
}
private selectBestProvider(): AIProvider {
if (this.config.openai?.apiKey) return 'openai'
if (this.config.google?.apiKey) return 'google'
if (this.config.baidu?.apiKey) return 'baidu'
return 'openai'
}
private getDefaultModel(provider?: AIProvider): string {
switch (provider) {
case 'openai': return 'gpt-3.5-turbo'
case 'google': return 'gemini-pro'
case 'baidu': return 'ernie-bot'
default: return 'gpt-3.5-turbo'
}
}
private generateSessionId(userId: string): string {
return `session_${userId}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}
private generateMessageId(): string {
return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}
private async loadUserPreferences(userId: string): Promise<UserPreferences | undefined> {
// 模拟加载用户偏好
return {
preferredLanguages: ['zh-CN'],
preferredCategories: ['technology', 'economy'],
preferredSources: [],
newsStyle: 'brief',
updateFrequency: 'daily'
}
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
private initializeChatTemplates(): void {
this.chatTemplates = [
{
id: 'news_assistant',
name: '新闻助手',
description: '专业的新闻分析和推荐助手',
systemPrompt: '你是一个专业的新闻助手,可以帮助用户了解最新新闻、分析新闻内容、提供翻译服务。',
language: 'zh-CN',
category: 'news'
},
{
id: 'tech_news',
name: '科技新闻专家',
description: '专注于科技新闻的分析和解读',
systemPrompt: '你是一个科技新闻专家,专门分析科技行业动态、创新趋势和技术发展。',
language: 'zh-CN',
category: 'technology'
}
]
}
}

View File

@@ -0,0 +1,806 @@
// AI Content Analysis Service - Content classification, sentiment analysis, and quality assessment
import {
ContentAnalysisResult,
EntityResult,
TopicResult,
CategoryResult,
AIProvider,
AIResponse,
AIServiceConfig,
BatchProcessingOptions,
AIServiceError,
ContentInfo
} from '../types/ai-types.uts'
// 分析类型枚举
type AnalysisType = 'sentiment' | 'entities' | 'topics' | 'categories' | 'readability' | 'credibility' | 'toxicity' | 'summary' | 'keywords'
// 分析选项
type AnalysisOptions = {
types: AnalysisType[]
provider?: AIProvider
model?: string
includeScores?: boolean
detailedResults?: boolean
language?: string
customCategories?: string[]
}
// 内容质量评估结果
type QualityAssessment = {
overallScore: number
factualAccuracy: number
sourceReliability: number
writingQuality: number
objectivity: number
completeness: number
timeliness: number
relevance: number
}
// 关键词提取结果
type KeywordResult = {
keyword: string
frequency: number
importance: number
type: 'noun' | 'verb' | 'adjective' | 'entity' | 'concept'
}
// 分析统计
type AnalysisStats = {
totalAnalyses: number
successCount: number
errorCount: number
avgProcessingTimeMs: number
totalCost: number
byProvider: Record<AIProvider, number>
byAnalysisType: Record<AnalysisType, number>
}
/**
* AI内容分析服务类
* 提供情感分析、实体识别、主题提取、内容分类、质量评估等功能
*/
export class AIContentAnalysisService {
private config: AIServiceConfig
private stats: AnalysisStats = {
totalAnalyses: 0,
successCount: 0,
errorCount: 0,
avgProcessingTimeMs: 0,
totalCost: 0,
byProvider: {} as Record<AIProvider, number>,
byAnalysisType: {} as Record<AnalysisType, number>
}
// 预定义的新闻分类
private readonly NEWS_CATEGORIES = [
{ id: 'politics', name: '政治', keywords: ['政府', '政策', '选举', '法律', '议会', '总统', '部长'] },
{ id: 'economy', name: '经济', keywords: ['经济', '金融', '股市', '投资', '银行', '贸易', 'GDP'] },
{ id: 'technology', name: '科技', keywords: ['科技', '人工智能', '互联网', '软件', '硬件', '创新', '数字化'] },
{ id: 'sports', name: '体育', keywords: ['体育', '足球', '篮球', '奥运', '比赛', '运动员', '锦标赛'] },
{ id: 'entertainment', name: '娱乐', keywords: ['娱乐', '电影', '音乐', '明星', '综艺', '演出', '艺术'] },
{ id: 'health', name: '健康', keywords: ['健康', '医疗', '病毒', '疫苗', '医院', '药物', '疾病'] },
{ id: 'education', name: '教育', keywords: ['教育', '学校', '大学', '学生', '教师', '考试', '学习'] },
{ id: 'environment', name: '环境', keywords: ['环境', '气候', '污染', '环保', '生态', '绿色', '可持续'] },
{ id: 'international', name: '国际', keywords: ['国际', '外交', '战争', '和平', '联合国', '条约', '全球'] },
{ id: 'social', name: '社会', keywords: ['社会', '社区', '公益', '慈善', '志愿者', '文化', '传统'] }
]
constructor(config: AIServiceConfig) {
this.config = config
this.initializeStats()
}
/**
* 分析内容
* @param content 内容文本
* @param options 分析选项
*/
async analyzeContent(
content: string,
options: AnalysisOptions = {
types: ['sentiment', 'entities', 'topics', 'categories', 'readability', 'summary', 'keywords']
}
): Promise<AIResponse<ContentAnalysisResult>> {
try {
this.stats.totalAnalyses++
const startTime = Date.now()
// 选择提供商
const provider = options.provider || this.selectBestProvider()
// 执行各种分析
const results = await Promise.allSettled([
this.analyzeSentiment(content, provider, options),
this.extractEntities(content, provider, options),
this.extractTopics(content, provider, options),
this.classifyContent(content, options),
this.assessReadability(content, options.language),
this.assessCredibility(content),
this.assessToxicity(content, provider),
this.generateSummary(content, provider, options),
this.extractKeywords(content, options)
])
const processingTime = Date.now() - startTime
// 合并结果
const analysisResult: ContentAnalysisResult = {
contentId: this.generateContentId(content),
sentimentScore: this.extractResult(results[0], 0),
sentimentLabel: this.getSentimentLabel(this.extractResult(results[0], 0)),
readabilityScore: this.extractResult(results[4], 0.5),
credibilityScore: this.extractResult(results[5], 0.5),
toxicityScore: this.extractResult(results[6], 0),
keywords: this.extractResult(results[8], []),
entities: this.extractResult(results[1], []),
topics: this.extractResult(results[2], []),
categories: this.extractResult(results[3], []),
summary: this.extractResult(results[7], ''),
keyPhrases: this.extractKeyPhrases(content),
language: options.language || await this.detectLanguage(content),
processingTimeMs: processingTime,
provider
}
// 更新统计
this.updateStats(provider, options.types, processingTime)
this.stats.successCount++
return {
success: true,
data: analysisResult,
processingTimeMs: processingTime,
provider
}
} catch (error) {
this.stats.errorCount++
const aiError: AIServiceError = {
code: 'ANALYSIS_ERROR',
message: error.message || 'Content analysis failed',
provider: options.provider,
retryable: this.isRetryableError(error)
}
return {
success: false,
error: aiError.message,
errorCode: aiError.code
}
}
}
/**
* 批量内容分析
* @param contents 内容数组
* @param options 分析选项
* @param batchOptions 批处理选项
*/
async analyzeContentBatch(
contents: string[],
options: AnalysisOptions = { types: ['sentiment', 'categories', 'summary'] },
batchOptions: BatchProcessingOptions = {
batchSize: 5,
concurrency: 2,
retryCount: 2,
delayMs: 1000
}
): Promise<AIResponse<ContentAnalysisResult[]>> {
try {
const results: ContentAnalysisResult[] = []
const batches = this.createBatches(contents, batchOptions.batchSize)
for (let i = 0; i < batches.length; i++) {
const batch = batches[i]
const batchPromises = batch.map(async (content) => {
try {
const response = await this.analyzeContent(content, options)
if (response.success && response.data) {
return response.data
}
throw new Error(response.error || 'Analysis failed')
} catch (error) {
if (batchOptions.onError) {
batchOptions.onError(error, content)
}
throw error
}
})
const batchResults = await Promise.allSettled(batchPromises)
for (const result of batchResults) {
if (result.status === 'fulfilled') {
results.push(result.value)
}
}
// 进度回调
if (batchOptions.onProgress) {
batchOptions.onProgress(results.length, contents.length)
}
// 批次间延迟
if (i < batches.length - 1 && batchOptions.delayMs > 0) {
await this.delay(batchOptions.delayMs)
}
}
return { success: true, data: results }
} catch (error) {
return {
success: false,
error: error.message || 'Batch analysis failed'
}
}
}
/**
* 质量评估
* @param content 内容文本
* @param metadata 内容元数据
*/
async assessQuality(content: string, metadata?: Partial<ContentInfo>): Promise<AIResponse<QualityAssessment>> {
try {
const [
factualScore,
sourceScore,
writingScore,
objectivityScore,
completenessScore,
timelinessScore,
relevanceScore
] = await Promise.all([
this.assessFactualAccuracy(content),
this.assessSourceReliability(metadata?.sourceUrl || ''),
this.assessWritingQuality(content),
this.assessObjectivity(content),
this.assessCompleteness(content),
this.assessTimeliness(metadata?.publishedAt || Date.now()),
this.assessRelevance(content, metadata?.categoryId)
])
const overallScore = (
factualScore + sourceScore + writingScore + objectivityScore +
completenessScore + timelinessScore + relevanceScore
) / 7
const assessment: QualityAssessment = {
overallScore,
factualAccuracy: factualScore,
sourceReliability: sourceScore,
writingQuality: writingScore,
objectivity: objectivityScore,
completeness: completenessScore,
timeliness: timelinessScore,
relevance: relevanceScore
}
return { success: true, data: assessment }
} catch (error) {
return {
success: false,
error: error.message || 'Quality assessment failed'
}
}
}
/**
* 获取统计信息
*/
getStatistics(): AnalysisStats {
return { ...this.stats }
}
// Private methods
private async analyzeSentiment(content: string, provider: AIProvider, options: AnalysisOptions): Promise<number> {
if (!options.types.includes('sentiment')) return 0
switch (provider) {
case 'openai':
return await this.analyzeSentimentWithOpenAI(content)
case 'google':
return await this.analyzeSentimentWithGoogle(content)
case 'baidu':
return await this.analyzeSentimentWithBaidu(content)
default:
return this.analyzeSentimentBasic(content)
}
}
private async extractEntities(content: string, provider: AIProvider, options: AnalysisOptions): Promise<EntityResult[]> {
if (!options.types.includes('entities')) return []
switch (provider) {
case 'openai':
return await this.extractEntitiesWithOpenAI(content)
case 'google':
return await this.extractEntitiesWithGoogle(content)
default:
return this.extractEntitiesBasic(content)
}
}
private async extractTopics(content: string, provider: AIProvider, options: AnalysisOptions): Promise<TopicResult[]> {
if (!options.types.includes('topics')) return []
switch (provider) {
case 'openai':
return await this.extractTopicsWithOpenAI(content)
default:
return this.extractTopicsBasic(content)
}
}
private async classifyContent(content: string, options: AnalysisOptions): Promise<CategoryResult[]> {
if (!options.types.includes('categories')) return []
const categories: CategoryResult[] = []
// 基于关键词的分类
for (const category of this.NEWS_CATEGORIES) {
const matches = category.keywords.filter(keyword =>
content.toLowerCase().includes(keyword.toLowerCase())
)
if (matches.length > 0) {
const confidence = Math.min(matches.length / category.keywords.length, 1)
categories.push({
categoryId: category.id,
categoryName: category.name,
confidence,
level: 1
})
}
}
// 按置信度排序
return categories.sort((a, b) => b.confidence - a.confidence).slice(0, 3)
}
private assessReadability(content: string, language?: string): number {
// 简化的可读性评估
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 0)
const words = content.split(/\s+/).filter(w => w.length > 0)
const characters = content.replace(/\s/g, '').length
if (sentences.length === 0 || words.length === 0) return 0
const avgWordsPerSentence = words.length / sentences.length
const avgCharsPerWord = characters / words.length
// 基于句子长度和词汇复杂度的评分
let score = 1.0
// 句子长度惩罚
if (avgWordsPerSentence > 20) score -= 0.2
if (avgWordsPerSentence > 30) score -= 0.3
// 词汇复杂度惩罚
if (avgCharsPerWord > 6) score -= 0.1
if (avgCharsPerWord > 8) score -= 0.2
return Math.max(0, Math.min(1, score))
}
private assessCredibility(content: string): number {
let score = 0.5 // 基础分
// 包含引用或来源
if (content.includes('据') || content.includes('根据') || content.includes('来源')) {
score += 0.2
}
// 包含具体数据
if (/\d+%|\d+万|\d+亿|\d{4}年/.test(content)) {
score += 0.15
}
// 避免极端词汇
const extremeWords = ['绝对', '必然', '完全', '永远', '从来', '所有']
const extremeCount = extremeWords.filter(word => content.includes(word)).length
score -= extremeCount * 0.05
// 避免情绪化表达
const emotionalWords = ['震惊', '愤怒', '可怕', '惊人', '令人发指']
const emotionalCount = emotionalWords.filter(word => content.includes(word)).length
score -= emotionalCount * 0.03
return Math.max(0, Math.min(1, score))
}
private async assessToxicity(content: string, provider: AIProvider): Promise<number> {
// 基础毒性检测
const toxicWords = ['仇恨', '歧视', '暴力', '威胁', '诽谤', '侮辱']
const toxicCount = toxicWords.filter(word => content.includes(word)).length
return Math.min(toxicCount / 10, 1)
}
private async generateSummary(content: string, provider: AIProvider, options: AnalysisOptions): Promise<string> {
if (!options.types.includes('summary')) return ''
// 简单的摘要生成:提取前两句
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 10)
return sentences.slice(0, 2).join('。') + (sentences.length > 2 ? '。' : '')
}
private extractKeywords(content: string, options: AnalysisOptions): string[] {
if (!options.types.includes('keywords')) return []
// 简单的关键词提取
const words = content
.replace(/[^\u4e00-\u9fa5\w\s]/g, '') // 保留中文、英文和空格
.split(/\s+/)
.filter(word => word.length > 1)
// 统计词频
const wordCount: Record<string, number> = {}
words.forEach(word => {
const lower = word.toLowerCase()
wordCount[lower] = (wordCount[lower] || 0) + 1
})
// 按频率排序并返回前10个
return Object.entries(wordCount)
.sort(([, a], [, b]) => b - a)
.slice(0, 10)
.map(([word]) => word)
}
private extractKeyPhrases(content: string): string[] {
// 提取2-3个词的短语
const phrases: string[] = []
const words = content.split(/\s+/)
for (let i = 0; i < words.length - 1; i++) {
const twoWordPhrase = words.slice(i, i + 2).join(' ')
if (twoWordPhrase.length > 4) {
phrases.push(twoWordPhrase)
}
if (i < words.length - 2) {
const threeWordPhrase = words.slice(i, i + 3).join(' ')
if (threeWordPhrase.length > 6) {
phrases.push(threeWordPhrase)
}
}
}
// 去重并返回前5个
return [...new Set(phrases)].slice(0, 5)
}
private async detectLanguage(content: string): Promise<string> {
// 基础语言检测
const chineseRegex = /[\u4e00-\u9fff]/
const englishRegex = /[a-zA-Z]/
const chineseMatches = content.match(chineseRegex)?.length || 0
const englishMatches = content.match(englishRegex)?.length || 0
if (chineseMatches > englishMatches) return 'zh-CN'
if (englishMatches > chineseMatches) return 'en'
return 'auto'
}
private getSentimentLabel(score: number): 'positive' | 'negative' | 'neutral' {
if (score > 0.1) return 'positive'
if (score < -0.1) return 'negative'
return 'neutral'
}
private generateContentId(content: string): string {
// 简单的内容ID生成
return `content_${Date.now()}_${content.substring(0, 10).replace(/\s/g, '_')}`
}
private extractResult<T>(result: PromiseSettledResult<T>, defaultValue: T): T {
return result.status === 'fulfilled' ? result.value : defaultValue
}
private selectBestProvider(): AIProvider {
if (this.config.openai?.apiKey) return 'openai'
if (this.config.google?.apiKey) return 'google'
if (this.config.baidu?.apiKey) return 'baidu'
return 'openai'
}
private createBatches<T>(items: T[], batchSize: number): T[][] {
const batches: T[][] = []
for (let i = 0; i < items.length; i += batchSize) {
batches.push(items.slice(i, i + batchSize))
}
return batches
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
private initializeStats(): void {
const providers: AIProvider[] = ['openai', 'google', 'baidu', 'custom']
const analysisTypes: AnalysisType[] = ['sentiment', 'entities', 'topics', 'categories', 'readability', 'credibility', 'toxicity', 'summary', 'keywords']
providers.forEach(provider => {
this.stats.byProvider[provider] = 0
})
analysisTypes.forEach(type => {
this.stats.byAnalysisType[type] = 0
})
}
private updateStats(provider: AIProvider, types: AnalysisType[], processingTime: number): void {
this.stats.byProvider[provider]++
types.forEach(type => {
this.stats.byAnalysisType[type]++
})
this.stats.avgProcessingTimeMs = (this.stats.avgProcessingTimeMs * (this.stats.totalAnalyses - 1) + processingTime) / this.stats.totalAnalyses
}
private isRetryableError(error: any): boolean {
const retryableCodes = ['TIMEOUT', 'RATE_LIMIT', 'SERVER_ERROR']
return retryableCodes.includes(error.code) || error.status >= 500
}
// Quality assessment methods
private async assessFactualAccuracy(content: string): Promise<number> {
// 检查是否包含可验证的事实
let score = 0.5
// 包含日期
if (/\d{4}年|\d{1,2}月|\d{1,2}日/.test(content)) score += 0.1
// 包含具体数字
if (/\d+\.?\d*%|\d+万|\d+亿|\d+千/.test(content)) score += 0.1
// 包含地点
if (/市|省|县|区|国|州/.test(content)) score += 0.1
// 包含人名或机构名
if (/先生|女士|部长|主席|公司|集团|大学|医院/.test(content)) score += 0.1
return Math.min(1, score)
}
private async assessSourceReliability(sourceUrl: string): Promise<number> {
if (!sourceUrl) return 0.3
// 简单的源可靠性评估
const reliableDomains = ['gov.cn', 'edu.cn', 'xinhuanet.com', 'people.com.cn', 'cctv.com']
const domain = sourceUrl.toLowerCase()
for (const reliableDomain of reliableDomains) {
if (domain.includes(reliableDomain)) return 0.9
}
if (domain.includes('.gov') || domain.includes('.edu')) return 0.8
if (domain.includes('news') || domain.includes('media')) return 0.6
return 0.4
}
private async assessWritingQuality(content: string): Promise<number> {
let score = 0.5
// 检查语法和结构
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 0)
if (sentences.length > 2) score += 0.1
// 检查段落结构
const paragraphs = content.split('\n\n').filter(p => p.trim().length > 0)
if (paragraphs.length > 1) score += 0.1
// 检查词汇丰富度
const words = content.split(/\s+/).filter(w => w.length > 0)
const uniqueWords = new Set(words.map(w => w.toLowerCase()))
const diversity = uniqueWords.size / words.length
score += diversity * 0.3
return Math.min(1, score)
}
private async assessObjectivity(content: string): Promise<number> {
let score = 0.7 // 基础客观性分数
// 主观词汇惩罚
const subjectiveWords = ['我认为', '个人觉得', '显然', '明显', '无疑', '肯定']
const subjectiveCount = subjectiveWords.filter(word => content.includes(word)).length
score -= subjectiveCount * 0.1
// 情感词汇惩罚
const emotionalWords = ['愤怒', '激动', '兴奋', '失望', '震惊', '惊喜']
const emotionalCount = emotionalWords.filter(word => content.includes(word)).length
score -= emotionalCount * 0.05
return Math.max(0, Math.min(1, score))
}
private async assessCompleteness(content: string): Promise<number> {
let score = 0.3
// 基于内容长度
if (content.length > 200) score += 0.2
if (content.length > 500) score += 0.2
if (content.length > 1000) score += 0.2
// 包含关键新闻要素5W1H
const hasWho = /人|者|员|家|国|公司|组织/.test(content)
const hasWhat = /事件|活动|发生|进行|宣布|决定/.test(content)
const hasWhen = /\d{4}年|\d{1,2}月|\d{1,2}日|今天|昨天|明天/.test(content)
const hasWhere = /市|省|县|区|国|地区|地点/.test(content)
const hasWhy = /因为|由于|原因|目的|为了/.test(content)
const elements = [hasWho, hasWhat, hasWhen, hasWhere, hasWhy].filter(Boolean).length
score += elements * 0.06
return Math.min(1, score)
}
private async assessTimeliness(publishedAt: number): Promise<number> {
const now = Date.now()
const ageHours = (now - publishedAt) / (1000 * 60 * 60)
// 新闻越新,时效性越高
if (ageHours < 1) return 1.0
if (ageHours < 6) return 0.9
if (ageHours < 24) return 0.7
if (ageHours < 72) return 0.5
if (ageHours < 168) return 0.3
return 0.1
}
private async assessRelevance(content: string, categoryId?: string): Promise<number> {
if (!categoryId) return 0.5
// 根据分类检查相关性
const category = this.NEWS_CATEGORIES.find(c => c.id === categoryId)
if (!category) return 0.5
const matches = category.keywords.filter(keyword =>
content.toLowerCase().includes(keyword.toLowerCase())
).length
return Math.min(1, matches / category.keywords.length + 0.3)
}
// Mock AI service methods
private async analyzeSentimentWithOpenAI(content: string): Promise<number> {
// 模拟OpenAI情感分析
await this.delay(Math.random() * 500 + 200)
// 简单的情感检测
const positiveWords = ['好', '棒', '优秀', '成功', '胜利', '喜悦', '高兴', '满意']
const negativeWords = ['坏', '糟糕', '失败', '问题', '困难', '悲伤', '愤怒', '失望']
const positiveCount = positiveWords.filter(word => content.includes(word)).length
const negativeCount = negativeWords.filter(word => content.includes(word)).length
const score = (positiveCount - negativeCount) / Math.max(positiveCount + negativeCount, 1)
return Math.max(-1, Math.min(1, score))
}
private async analyzeSentimentWithGoogle(content: string): Promise<number> {
await this.delay(Math.random() * 400 + 150)
return Math.random() * 2 - 1 // -1 to 1
}
private async analyzeSentimentWithBaidu(content: string): Promise<number> {
await this.delay(Math.random() * 300 + 100)
return Math.random() * 2 - 1
}
private analyzeSentimentBasic(content: string): number {
// 基础情感分析
const positiveWords = ['好', '棒', '优秀', '成功', '胜利', 'great', 'good', 'excellent']
const negativeWords = ['坏', '糟糕', '失败', '问题', 'bad', 'terrible', 'awful']
const positiveCount = positiveWords.filter(word => content.toLowerCase().includes(word)).length
const negativeCount = negativeWords.filter(word => content.toLowerCase().includes(word)).length
return (positiveCount - negativeCount) / Math.max(positiveCount + negativeCount, 1)
}
private async extractEntitiesWithOpenAI(content: string): Promise<EntityResult[]> {
await this.delay(Math.random() * 600 + 300)
// 模拟实体提取
const entities: EntityResult[] = []
const patterns = [
{ regex: /[\u4e00-\u9fa5]{2,4}(公司|集团|企业|机构)/g, type: 'organization' as const },
{ regex: /[\u4e00-\u9fa5]{2,3}(市|省|县|区)/g, type: 'location' as const },
{ regex: /[\u4e00-\u9fa5]{2,4}(先生|女士|部长|主席|总裁|经理)/g, type: 'person' as const },
{ regex: /\d{4}年\d{1,2}月\d{1,2}日/g, type: 'date' as const },
{ regex: /\d+\.?\d*(万|亿|千)?(元|美元|英镑)/g, type: 'money' as const }
]
patterns.forEach(pattern => {
const matches = content.matchAll(pattern.regex)
for (const match of matches) {
entities.push({
text: match[0],
type: pattern.type,
confidence: 0.8 + Math.random() * 0.2,
startPosition: match.index || 0,
endPosition: (match.index || 0) + match[0].length
})
}
})
return entities.slice(0, 10)
}
private async extractEntitiesWithGoogle(content: string): Promise<EntityResult[]> {
await this.delay(Math.random() * 500 + 250)
return this.extractEntitiesBasic(content)
}
private extractEntitiesBasic(content: string): EntityResult[] {
// 基础实体提取
const entities: EntityResult[] = []
// 提取组织
const orgMatches = content.matchAll(/[\u4e00-\u9fa5]{2,4}(公司|集团)/g)
for (const match of orgMatches) {
entities.push({
text: match[0],
type: 'organization',
confidence: 0.7,
startPosition: match.index || 0,
endPosition: (match.index || 0) + match[0].length
})
}
return entities
}
private async extractTopicsWithOpenAI(content: string): Promise<TopicResult[]> {
await this.delay(Math.random() * 400 + 200)
// 基于关键词聚类的主题提取
const topics: TopicResult[] = []
for (const category of this.NEWS_CATEGORIES.slice(0, 3)) {
const matches = category.keywords.filter(keyword =>
content.toLowerCase().includes(keyword.toLowerCase())
)
if (matches.length > 0) {
topics.push({
name: category.name,
confidence: matches.length / category.keywords.length,
keywords: matches
})
}
}
return topics.sort((a, b) => b.confidence - a.confidence)
}
private extractTopicsBasic(content: string): TopicResult[] {
// 基础主题提取
const topics: TopicResult[] = []
// 检查科技相关关键词
const techKeywords = ['科技', '技术', '互联网', 'AI', '人工智能']
const techMatches = techKeywords.filter(keyword => content.includes(keyword))
if (techMatches.length > 0) {
topics.push({
name: '科技',
confidence: techMatches.length / techKeywords.length,
keywords: techMatches
})
}
return topics
}
}

View File

@@ -0,0 +1,761 @@
// Advanced Error Handling and Retry Mechanism System
// Comprehensive error recovery, circuit breaker, and resilience patterns
import { type AIProvider, type AIResponse } from '../types/ai-types.uts'
/**
* Error classification and handling configuration
*/
export type ErrorHandlingConfig = {
retryPolicy: {
maxAttempts: number
baseDelayMs: number
maxDelayMs: number
backoffMultiplier: number
jitterEnabled: boolean
}
circuitBreaker: {
failureThreshold: number
recoveryTimeoutMs: number
halfOpenMaxCalls: number
monitoringWindowMs: number
}
rateLimit: {
maxRequestsPerSecond: number
burstSize: number
enabled: boolean
}
fallback: {
enabled: boolean
fallbackProviders: AIProvider[]
gracefulDegradation: boolean
}
monitoring: {
enableMetrics: boolean
alertOnPatterns: boolean
maxErrorHistorySize: number
}
}
/**
* Error categories for different handling strategies
*/
export enum ErrorCategory {
TRANSIENT = 'transient', // Network timeouts, temporary unavailability
AUTHENTICATION = 'auth', // API key issues, token expiration
RATE_LIMIT = 'rate_limit', // API rate limiting
QUOTA_EXCEEDED = 'quota', // API quota exceeded
INVALID_REQUEST = 'invalid', // Bad request data
SERVICE_ERROR = 'service', // Internal service errors
NETWORK = 'network', // Network connectivity issues
PERMANENT = 'permanent' // Permanent failures that shouldn't be retried
}
/**
* Detailed error information
*/
export type ErrorInfo = {
category: ErrorCategory
code?: string
message: string
provider?: AIProvider
operation: string
timestamp: number
retryCount: number
context?: Record<string, any>
isRetryable: boolean
suggestedAction?: string
}
/**
* Circuit breaker states
*/
export enum CircuitBreakerState {
CLOSED = 'closed', // Normal operation
OPEN = 'open', // Circuit is open, failing fast
HALF_OPEN = 'half_open' // Testing if service has recovered
}
/**
* Circuit breaker status
*/
export type CircuitBreakerStatus = {
state: CircuitBreakerState
failureCount: number
successCount: number
lastFailureTime?: number
nextAttemptTime?: number
halfOpenAttempts: number
}
/**
* Rate limiter status
*/
export type RateLimiterStatus = {
requestsRemaining: number
resetTime: number
isLimited: boolean
queueSize: number
}
/**
* Retry attempt information
*/
export type RetryAttempt = {
attemptNumber: number
timestamp: number
error?: ErrorInfo
delayMs: number
success: boolean
}
/**
* Operation result with retry information
*/
export type OperationResult<T> = {
success: boolean
data?: T
error?: ErrorInfo
attempts: RetryAttempt[]
totalDuration: number
finalProvider?: AIProvider
}
/**
* Advanced error handler and retry manager
*/
export class AIErrorHandler {
private config: ErrorHandlingConfig
private circuitBreakers = new Map<string, CircuitBreakerStatus>()
private rateLimiters = new Map<string, RateLimiterStatus>()
private errorHistory: ErrorInfo[] = []
private requestQueues = new Map<string, Array<() => Promise<any>>>()
constructor(config: ErrorHandlingConfig) {
this.config = config
this.initializeCircuitBreakers()
this.initializeRateLimiters()
}
/**
* Execute operation with advanced error handling and retry logic
*/
async executeWithRetry<T>(
operation: () => Promise<T>,
context: {
operationName: string
provider?: AIProvider
retryable?: boolean
metadata?: Record<string, any>
}
): Promise<OperationResult<T>> {
const startTime = Date.now()
const attempts: RetryAttempt[] = []
let lastError: ErrorInfo | undefined
// Check circuit breaker
const breakerKey = this.getBreakerKey(context.operationName, context.provider)
if (this.isCircuitOpen(breakerKey)) {
const error = this.createError(
ErrorCategory.SERVICE_ERROR,
`Circuit breaker is open for ${breakerKey}`,
context.operationName,
context.provider
)
return {
success: false,
error,
attempts: [],
totalDuration: Date.now() - startTime
}
}
// Check rate limits
if (this.config.rateLimit.enabled && context.provider) {
const rateLimitResult = await this.checkRateLimit(context.provider)
if (!rateLimitResult.allowed) {
const error = this.createError(
ErrorCategory.RATE_LIMIT,
'Rate limit exceeded',
context.operationName,
context.provider
)
return {
success: false,
error,
attempts: [],
totalDuration: Date.now() - startTime
}
}
}
// Execute with retry logic
for (let attempt = 1; attempt <= this.config.retryPolicy.maxAttempts; attempt++) {
const attemptStart = Date.now()
try {
// Add delay for retry attempts
if (attempt > 1) {
const delay = this.calculateRetryDelay(attempt - 1)
await this.sleep(delay)
attempts[attempts.length - 1].delayMs = delay
}
// Execute the operation
const result = await operation()
// Record successful attempt
const attemptInfo: RetryAttempt = {
attemptNumber: attempt,
timestamp: attemptStart,
delayMs: 0,
success: true
}
attempts.push(attemptInfo)
// Update circuit breaker on success
this.recordSuccess(breakerKey)
return {
success: true,
data: result,
attempts,
totalDuration: Date.now() - startTime,
finalProvider: context.provider
}
} catch (error) {
const errorInfo = this.analyzeError(error, context.operationName, context.provider, attempt - 1)
lastError = errorInfo
// Record failed attempt
const attemptInfo: RetryAttempt = {
attemptNumber: attempt,
timestamp: attemptStart,
error: errorInfo,
delayMs: 0,
success: false
}
attempts.push(attemptInfo)
// Update error history
this.recordError(errorInfo)
// Update circuit breaker on failure
this.recordFailure(breakerKey)
// Check if we should retry
if (!this.shouldRetry(errorInfo, attempt)) {
break
}
// Try fallback provider if available
if (this.config.fallback.enabled && attempt === this.config.retryPolicy.maxAttempts) {
const fallbackResult = await this.tryFallbackProviders(
operation,
context,
startTime,
attempts
)
if (fallbackResult) {
return fallbackResult
}
}
}
}
return {
success: false,
error: lastError,
attempts,
totalDuration: Date.now() - startTime
}
}
/**
* Handle bulk operations with advanced error recovery
*/
async executeBulkWithRetry<T, R>(
items: T[],
operation: (item: T) => Promise<R>,
options: {
operationName: string
batchSize?: number
concurrency?: number
failFast?: boolean
partialFailureThreshold?: number
}
): Promise<{
results: Array<{ item: T; result?: R; error?: ErrorInfo }>
summary: {
successful: number
failed: number
totalTime: number
throughput: number
}
}> {
const startTime = Date.now()
const batchSize = options.batchSize || 10
const concurrency = options.concurrency || 3
const results: Array<{ item: T; result?: R; error?: ErrorInfo }> = []
// Process in batches
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize)
// Process batch with controlled concurrency
const batchPromises = batch.map(async (item) => {
const operationResult = await this.executeWithRetry(
() => operation(item),
{
operationName: options.operationName,
metadata: { batchIndex: Math.floor(i / batchSize), itemIndex: i + batch.indexOf(item) }
}
)
return {
item,
result: operationResult.data,
error: operationResult.error
}
})
// Execute with concurrency control
const batchResults = await this.executeConcurrently(batchPromises, concurrency)
results.push(...batchResults)
// Check failure threshold
const failedCount = results.filter(r => r.error).length
const failureRate = failedCount / results.length
if (options.failFast && failureRate > (options.partialFailureThreshold || 0.5)) {
console.log(`⚠️ Bulk operation failing fast due to high failure rate: ${(failureRate * 100).toFixed(1)}%`)
break
}
}
const endTime = Date.now()
const successful = results.filter(r => !r.error).length
const failed = results.filter(r => r.error).length
const totalTime = endTime - startTime
const throughput = results.length / (totalTime / 1000)
return {
results,
summary: {
successful,
failed,
totalTime,
throughput
}
}
}
/**
* Get current error handling status
*/
getErrorHandlingStatus(): {
circuitBreakers: Array<{ key: string; status: CircuitBreakerStatus }>
rateLimiters: Array<{ key: string; status: RateLimiterStatus }>
recentErrors: ErrorInfo[]
errorPatterns: Array<{ pattern: string; count: number; lastSeen: number }>
} {
const recentErrors = this.errorHistory.slice(-50) // Last 50 errors
const errorPatterns = this.analyzeErrorPatterns(recentErrors)
return {
circuitBreakers: Array.from(this.circuitBreakers.entries()).map(([key, status]) => ({ key, status })),
rateLimiters: Array.from(this.rateLimiters.entries()).map(([key, status]) => ({ key, status })),
recentErrors,
errorPatterns
}
}
/**
* Reset circuit breakers and error state
*/
resetErrorState(): void {
this.circuitBreakers.clear()
this.rateLimiters.clear()
this.errorHistory = []
this.requestQueues.clear()
this.initializeCircuitBreakers()
this.initializeRateLimiters()
console.log('🔄 Error handling state reset')
}
/**
* Update configuration
*/
updateConfig(newConfig: Partial<ErrorHandlingConfig>): void {
this.config = { ...this.config, ...newConfig }
console.log('⚙️ Error handling configuration updated')
}
// Private methods
private initializeCircuitBreakers(): void {
const providers: AIProvider[] = ['openai', 'google', 'baidu']
const operations = ['translate', 'analyze', 'chat', 'recommend']
providers.forEach(provider => {
operations.forEach(operation => {
const key = this.getBreakerKey(operation, provider)
this.circuitBreakers.set(key, {
state: CircuitBreakerState.CLOSED,
failureCount: 0,
successCount: 0,
halfOpenAttempts: 0
})
})
})
}
private initializeRateLimiters(): void {
const providers: AIProvider[] = ['openai', 'google', 'baidu']
providers.forEach(provider => {
this.rateLimiters.set(provider, {
requestsRemaining: this.config.rateLimit.maxRequestsPerSecond,
resetTime: Date.now() + 1000,
isLimited: false,
queueSize: 0
})
})
}
private getBreakerKey(operation: string, provider?: AIProvider): string {
return provider ? `${provider}:${operation}` : operation
}
private isCircuitOpen(breakerKey: string): boolean {
const breaker = this.circuitBreakers.get(breakerKey)
if (!breaker) return false
if (breaker.state === CircuitBreakerState.OPEN) {
// Check if we should transition to half-open
const now = Date.now()
if (breaker.lastFailureTime &&
now - breaker.lastFailureTime > this.config.circuitBreaker.recoveryTimeoutMs) {
breaker.state = CircuitBreakerState.HALF_OPEN
breaker.halfOpenAttempts = 0
console.log(`🔄 Circuit breaker ${breakerKey} transitioning to half-open`)
return false
}
return true
}
return false
}
private recordSuccess(breakerKey: string): void {
const breaker = this.circuitBreakers.get(breakerKey)
if (!breaker) return
breaker.successCount++
if (breaker.state === CircuitBreakerState.HALF_OPEN) {
breaker.halfOpenAttempts++
if (breaker.halfOpenAttempts >= this.config.circuitBreaker.halfOpenMaxCalls) {
breaker.state = CircuitBreakerState.CLOSED
breaker.failureCount = 0
console.log(`✅ Circuit breaker ${breakerKey} closed after successful recovery`)
}
}
}
private recordFailure(breakerKey: string): void {
const breaker = this.circuitBreakers.get(breakerKey)
if (!breaker) return
breaker.failureCount++
breaker.lastFailureTime = Date.now()
if (breaker.state === CircuitBreakerState.CLOSED) {
if (breaker.failureCount >= this.config.circuitBreaker.failureThreshold) {
breaker.state = CircuitBreakerState.OPEN
console.log(`⚠️ Circuit breaker ${breakerKey} opened due to ${breaker.failureCount} failures`)
}
} else if (breaker.state === CircuitBreakerState.HALF_OPEN) {
breaker.state = CircuitBreakerState.OPEN
console.log(`❌ Circuit breaker ${breakerKey} re-opened after failed recovery attempt`)
}
}
private async checkRateLimit(provider: AIProvider): Promise<{ allowed: boolean; waitTime?: number }> {
const limiter = this.rateLimiters.get(provider)
if (!limiter) return { allowed: true }
const now = Date.now()
// Reset if time window has passed
if (now >= limiter.resetTime) {
limiter.requestsRemaining = this.config.rateLimit.maxRequestsPerSecond
limiter.resetTime = now + 1000
limiter.isLimited = false
}
if (limiter.requestsRemaining <= 0) {
limiter.isLimited = true
return {
allowed: false,
waitTime: limiter.resetTime - now
}
}
limiter.requestsRemaining--
return { allowed: true }
}
private analyzeError(
error: any,
operation: string,
provider?: AIProvider,
retryCount: number = 0
): ErrorInfo {
const errorMessage = error?.message || String(error)
const errorCode = error?.code || error?.status
let category = ErrorCategory.PERMANENT
let isRetryable = false
let suggestedAction = 'Review error and fix manually'
// Analyze error to determine category and retry strategy
if (errorMessage.toLowerCase().includes('timeout') ||
errorMessage.toLowerCase().includes('network')) {
category = ErrorCategory.TRANSIENT
isRetryable = true
suggestedAction = 'Retry with exponential backoff'
} else if (errorMessage.toLowerCase().includes('rate limit') || errorCode === 429) {
category = ErrorCategory.RATE_LIMIT
isRetryable = true
suggestedAction = 'Wait and retry, consider implementing rate limiting'
} else if (errorMessage.toLowerCase().includes('quota') ||
errorMessage.toLowerCase().includes('exceeded')) {
category = ErrorCategory.QUOTA_EXCEEDED
isRetryable = false
suggestedAction = 'Check API quota and billing'
} else if (errorMessage.toLowerCase().includes('auth') ||
errorMessage.toLowerCase().includes('unauthorized') ||
errorCode === 401) {
category = ErrorCategory.AUTHENTICATION
isRetryable = false
suggestedAction = 'Check API keys and authentication'
} else if (errorCode >= 400 && errorCode < 500) {
category = ErrorCategory.INVALID_REQUEST
isRetryable = false
suggestedAction = 'Review request parameters'
} else if (errorCode >= 500) {
category = ErrorCategory.SERVICE_ERROR
isRetryable = true
suggestedAction = 'Retry or use fallback provider'
}
return {
category,
code: String(errorCode || 'unknown'),
message: errorMessage,
provider,
operation,
timestamp: Date.now(),
retryCount,
isRetryable,
suggestedAction,
context: {
originalError: error
}
}
}
private shouldRetry(error: ErrorInfo, attemptNumber: number): boolean {
if (attemptNumber >= this.config.retryPolicy.maxAttempts) {
return false
}
return error.isRetryable && [
ErrorCategory.TRANSIENT,
ErrorCategory.RATE_LIMIT,
ErrorCategory.SERVICE_ERROR,
ErrorCategory.NETWORK
].includes(error.category)
}
private calculateRetryDelay(attemptNumber: number): number {
const baseDelay = this.config.retryPolicy.baseDelayMs
const maxDelay = this.config.retryPolicy.maxDelayMs
const multiplier = this.config.retryPolicy.backoffMultiplier
let delay = baseDelay * Math.pow(multiplier, attemptNumber)
delay = Math.min(delay, maxDelay)
// Add jitter if enabled
if (this.config.retryPolicy.jitterEnabled) {
const jitter = delay * 0.1 * Math.random()
delay += jitter
}
return Math.floor(delay)
}
private async tryFallbackProviders<T>(
operation: () => Promise<T>,
context: any,
startTime: number,
existingAttempts: RetryAttempt[]
): Promise<OperationResult<T> | null> {
if (!this.config.fallback.enabled || !context.provider) {
return null
}
const fallbackProviders = this.config.fallback.fallbackProviders.filter(
p => p !== context.provider
)
for (const fallbackProvider of fallbackProviders) {
try {
console.log(`🔄 Attempting fallback to provider: ${fallbackProvider}`)
const result = await operation() // Note: In real implementation, this would use the fallback provider
return {
success: true,
data: result,
attempts: existingAttempts,
totalDuration: Date.now() - startTime,
finalProvider: fallbackProvider
}
} catch (error) {
console.log(`❌ Fallback provider ${fallbackProvider} also failed:`, error)
}
}
return null
}
private recordError(error: ErrorInfo): void {
this.errorHistory.push(error)
// Maintain history size limit
if (this.errorHistory.length > this.config.monitoring.maxErrorHistorySize) {
this.errorHistory = this.errorHistory.slice(-this.config.monitoring.maxErrorHistorySize)
}
// Alert on error patterns if enabled
if (this.config.monitoring.alertOnPatterns) {
this.checkErrorPatterns(error)
}
}
private checkErrorPatterns(error: ErrorInfo): void {
const recentErrors = this.errorHistory.filter(
e => Date.now() - e.timestamp < 300000 // Last 5 minutes
)
// Check for repeated errors from same provider
if (error.provider) {
const providerErrors = recentErrors.filter(e => e.provider === error.provider)
if (providerErrors.length >= 5) {
console.log(`🚨 High error rate detected for provider ${error.provider}: ${providerErrors.length} errors in 5 minutes`)
}
}
// Check for repeated error categories
const categoryErrors = recentErrors.filter(e => e.category === error.category)
if (categoryErrors.length >= 10) {
console.log(`🚨 High error rate detected for category ${error.category}: ${categoryErrors.length} errors in 5 minutes`)
}
}
private analyzeErrorPatterns(errors: ErrorInfo[]): Array<{ pattern: string; count: number; lastSeen: number }> {
const patterns = new Map<string, { count: number; lastSeen: number }>()
errors.forEach(error => {
const pattern = `${error.category}:${error.provider || 'unknown'}`
const existing = patterns.get(pattern) || { count: 0, lastSeen: 0 }
patterns.set(pattern, {
count: existing.count + 1,
lastSeen: Math.max(existing.lastSeen, error.timestamp)
})
})
return Array.from(patterns.entries())
.map(([pattern, data]) => ({ pattern, ...data }))
.sort((a, b) => b.count - a.count)
}
private async executeConcurrently<T>(promises: Promise<T>[], concurrency: number): Promise<T[]> {
const results: T[] = []
const executing: Promise<void>[] = []
for (const promise of promises) {
const p = promise.then(result => {
results.push(result)
})
executing.push(p)
if (executing.length >= concurrency) {
await Promise.race(executing)
executing.splice(executing.findIndex(x => x === p), 1)
}
}
await Promise.all(executing)
return results
}
private createError(
category: ErrorCategory,
message: string,
operation: string,
provider?: AIProvider
): ErrorInfo {
return {
category,
message,
operation,
provider,
timestamp: Date.now(),
retryCount: 0,
isRetryable: category !== ErrorCategory.PERMANENT
}
}
private sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
}
// Default configuration
export const defaultErrorHandlingConfig: ErrorHandlingConfig = {
retryPolicy: {
maxAttempts: 3,
baseDelayMs: 1000, // 1 second
maxDelayMs: 30000, // 30 seconds
backoffMultiplier: 2,
jitterEnabled: true
},
circuitBreaker: {
failureThreshold: 5,
recoveryTimeoutMs: 60000, // 1 minute
halfOpenMaxCalls: 3,
monitoringWindowMs: 300000 // 5 minutes
},
rateLimit: {
maxRequestsPerSecond: 10,
burstSize: 5,
enabled: true
},
fallback: {
enabled: true,
fallbackProviders: ['openai', 'google', 'baidu'],
gracefulDegradation: true
},
monitoring: {
enableMetrics: true,
alertOnPatterns: true,
maxErrorHistorySize: 1000
}
}

View File

@@ -0,0 +1,758 @@
// Performance Monitor and Optimization System
// Real-time monitoring, metrics collection, and automatic optimization
import { type AIProvider, type AIServiceConfig } from '../types/ai-types.uts'
/**
* Performance metrics data structure
*/
export type PerformanceMetrics = {
timestamp: number
service: string
operation: string
provider?: AIProvider
duration: number
success: boolean
error?: string
tokensUsed?: number
costUSD?: number
cacheHit?: boolean
memoryUsage?: number
cpuUsage?: number
networkLatency?: number
queueSize?: number
throughput?: number
}
/**
* System health status
*/
export type SystemHealth = {
status: 'healthy' | 'warning' | 'critical'
score: number // 0-100
checks: {
apiConnectivity: boolean
memoryUsage: number
errorRate: number
responseTime: number
costBudget: number
cacheEfficiency: number
}
alerts: HealthAlert[]
}
/**
* Health alert
*/
export type HealthAlert = {
id: string
severity: 'info' | 'warning' | 'error' | 'critical'
message: string
timestamp: number
source: string
resolved: boolean
}
/**
* Optimization recommendation
*/
export type OptimizationRecommendation = {
type: 'cache' | 'provider' | 'batch' | 'model' | 'timeout' | 'retry'
priority: 'low' | 'medium' | 'high' | 'critical'
description: string
expectedImpact: {
performanceGain?: string
costSaving?: string
reliabilityImprovement?: string
}
implementation: {
action: string
parameters: Record<string, any>
estimatedEffort: string
}
}
/**
* Performance statistics aggregation
*/
export type PerformanceStats = {
timeRange: {
start: number
end: number
duration: number
}
requests: {
total: number
successful: number
failed: number
successRate: number
}
timing: {
averageLatency: number
medianLatency: number
p95Latency: number
p99Latency: number
}
costs: {
total: number
average: number
byProvider: Record<string, number>
}
cache: {
hitRate: number
totalRequests: number
hits: number
misses: number
}
errors: {
byType: Record<string, number>
byProvider: Record<string, number>
topErrors: Array<{ error: string; count: number }>
}
}
/**
* Performance monitoring and optimization service
*/
export class AIPerformanceMonitor {
private metrics: PerformanceMetrics[] = []
private alerts: HealthAlert[] = []
private isMonitoring = false
private monitoringInterval?: number
private maxMetricsHistory = 10000
private alertThresholds = {
errorRate: 0.05, // 5%
responseTime: 5000, // 5 seconds
memoryUsage: 0.8, // 80%
costBudget: 0.9, // 90% of daily budget
cacheHitRate: 0.3 // 30% minimum
}
constructor(
private config: {
monitoringInterval: number
maxHistory: number
alertWebhook?: string
enableAutoOptimization: boolean
}
) {
this.maxMetricsHistory = config.maxHistory
}
/**
* Start performance monitoring
*/
startMonitoring(): void {
if (this.isMonitoring) {
console.log('⚠️ Performance monitoring is already running')
return
}
this.isMonitoring = true
console.log('🚀 Starting performance monitoring...')
this.monitoringInterval = setInterval(() => {
this.collectSystemMetrics()
this.checkSystemHealth()
this.generateOptimizationRecommendations()
}, this.config.monitoringInterval)
}
/**
* Stop performance monitoring
*/
stopMonitoring(): void {
if (this.monitoringInterval) {
clearInterval(this.monitoringInterval)
this.monitoringInterval = undefined
}
this.isMonitoring = false
console.log('🛑 Performance monitoring stopped')
}
/**
* Record a performance metric
*/
recordMetric(metric: PerformanceMetrics): void {
metric.timestamp = metric.timestamp || Date.now()
this.metrics.push(metric)
// Maintain history limit
if (this.metrics.length > this.maxMetricsHistory) {
this.metrics = this.metrics.slice(-this.maxMetricsHistory)
}
// Real-time analysis for critical metrics
if (!metric.success || (metric.duration && metric.duration > 10000)) {
this.checkForImmedateAlerts(metric)
}
}
/**
* Get current system health
*/
getSystemHealth(): SystemHealth {
const now = Date.now()
const recentMetrics = this.metrics.filter(m => now - m.timestamp < 300000) // Last 5 minutes
if (recentMetrics.length === 0) {
return {
status: 'warning',
score: 50,
checks: {
apiConnectivity: false,
memoryUsage: 0,
errorRate: 0,
responseTime: 0,
costBudget: 0,
cacheEfficiency: 0
},
alerts: this.getActiveAlerts()
}
}
const errorRate = recentMetrics.filter(m => !m.success).length / recentMetrics.length
const avgResponseTime = recentMetrics.reduce((sum, m) => sum + m.duration, 0) / recentMetrics.length
const cacheHitRate = this.calculateCacheHitRate(recentMetrics)
const memoryUsage = this.getMemoryUsage()
const costBudget = this.calculateCostBudgetUsage()
const checks = {
apiConnectivity: errorRate < 0.1,
memoryUsage,
errorRate,
responseTime: avgResponseTime,
costBudget,
cacheEfficiency: cacheHitRate
}
const score = this.calculateHealthScore(checks)
const status = this.determineHealthStatus(score, checks)
return {
status,
score,
checks,
alerts: this.getActiveAlerts()
}
}
/**
* Get performance statistics for a time range
*/
getPerformanceStats(
startTime: number,
endTime: number
): PerformanceStats {
const rangeMetrics = this.metrics.filter(
m => m.timestamp >= startTime && m.timestamp <= endTime
)
if (rangeMetrics.length === 0) {
return this.getEmptyStats(startTime, endTime)
}
const successful = rangeMetrics.filter(m => m.success)
const failed = rangeMetrics.filter(m => !m.success)
const successRate = successful.length / rangeMetrics.length
// Calculate timing statistics
const durations = rangeMetrics.map(m => m.duration).sort((a, b) => a - b)
const averageLatency = durations.reduce((sum, d) => sum + d, 0) / durations.length
const medianLatency = durations[Math.floor(durations.length / 2)]
const p95Latency = durations[Math.floor(durations.length * 0.95)]
const p99Latency = durations[Math.floor(durations.length * 0.99)]
// Calculate cost statistics
const totalCost = rangeMetrics.reduce((sum, m) => sum + (m.costUSD || 0), 0)
const averageCost = totalCost / rangeMetrics.length
const costByProvider = this.groupCostsByProvider(rangeMetrics)
// Calculate cache statistics
const cacheRequests = rangeMetrics.filter(m => m.cacheHit !== undefined)
const cacheHits = cacheRequests.filter(m => m.cacheHit).length
const cacheMisses = cacheRequests.length - cacheHits
const cacheHitRate = cacheRequests.length > 0 ? cacheHits / cacheRequests.length : 0
// Calculate error statistics
const errorsByType = this.groupErrorsByType(failed)
const errorsByProvider = this.groupErrorsByProvider(failed)
const topErrors = this.getTopErrors(failed)
return {
timeRange: {
start: startTime,
end: endTime,
duration: endTime - startTime
},
requests: {
total: rangeMetrics.length,
successful: successful.length,
failed: failed.length,
successRate
},
timing: {
averageLatency,
medianLatency,
p95Latency,
p99Latency
},
costs: {
total: totalCost,
average: averageCost,
byProvider: costByProvider
},
cache: {
hitRate: cacheHitRate,
totalRequests: cacheRequests.length,
hits: cacheHits,
misses: cacheMisses
},
errors: {
byType: errorsByType,
byProvider: errorsByProvider,
topErrors
}
}
}
/**
* Get optimization recommendations
*/
getOptimizationRecommendations(): OptimizationRecommendation[] {
const recommendations: OptimizationRecommendation[] = []
const recentStats = this.getPerformanceStats(
Date.now() - 3600000, // Last hour
Date.now()
)
// Cache optimization recommendations
if (recentStats.cache.hitRate < 0.4) {
recommendations.push({
type: 'cache',
priority: 'high',
description: `Cache hit rate is low (${(recentStats.cache.hitRate * 100).toFixed(1)}%). Consider increasing cache size or TTL.`,
expectedImpact: {
performanceGain: '30-50% faster response times',
costSaving: '20-40% reduction in AI API costs'
},
implementation: {
action: 'increase_cache_size',
parameters: {
maxSize: Math.max(1000, recentStats.cache.totalRequests * 2),
ttl: 3600000 // 1 hour
},
estimatedEffort: 'Low - Configuration change'
}
})
}
// Provider optimization recommendations
const providerErrors = recentStats.errors.byProvider
const worstProvider = Object.entries(providerErrors)
.sort(([, a], [, b]) => b - a)[0]
if (worstProvider && worstProvider[1] > recentStats.requests.total * 0.1) {
recommendations.push({
type: 'provider',
priority: 'medium',
description: `Provider ${worstProvider[0]} has high error rate (${worstProvider[1]} errors). Consider switching primary provider.`,
expectedImpact: {
reliabilityImprovement: '80-90% reduction in errors'
},
implementation: {
action: 'switch_primary_provider',
parameters: {
newPrimary: this.recommendBestProvider(recentStats),
fallbackProviders: ['openai', 'google', 'baidu']
},
estimatedEffort: 'Medium - Code changes required'
}
})
}
// Batch processing recommendations
if (recentStats.timing.averageLatency > 3000 && recentStats.requests.total > 100) {
recommendations.push({
type: 'batch',
priority: 'medium',
description: 'High latency with significant request volume. Consider implementing batch processing.',
expectedImpact: {
performanceGain: '50-70% improvement in throughput',
costSaving: '15-25% cost reduction'
},
implementation: {
action: 'enable_batch_processing',
parameters: {
batchSize: 10,
batchTimeout: 1000,
concurrency: 3
},
estimatedEffort: 'High - Significant code changes'
}
})
}
// Model optimization recommendations
if (recentStats.costs.average > 0.01) { // More than 1 cent per request
recommendations.push({
type: 'model',
priority: 'low',
description: 'Request costs are high. Consider using smaller/cheaper models for simple tasks.',
expectedImpact: {
costSaving: '40-60% cost reduction'
},
implementation: {
action: 'implement_model_selection',
parameters: {
simpleTaskModel: 'gpt-3.5-turbo',
complexTaskModel: 'gpt-4',
costThreshold: 0.005
},
estimatedEffort: 'Medium - Logic implementation required'
}
})
}
return recommendations
}
/**
* Apply automatic optimization
*/
async applyOptimizations(
recommendations: OptimizationRecommendation[]
): Promise<{ applied: number; failed: number; results: any[] }> {
if (!this.config.enableAutoOptimization) {
console.log('⚠️ Auto-optimization is disabled')
return { applied: 0, failed: 0, results: [] }
}
const results: any[] = []
let applied = 0
let failed = 0
for (const rec of recommendations) {
try {
const result = await this.applyOptimization(rec)
if (result.success) {
applied++
console.log(`✅ Applied optimization: ${rec.description}`)
} else {
failed++
console.log(`❌ Failed to apply optimization: ${rec.description} - ${result.error}`)
}
results.push(result)
} catch (error) {
failed++
console.log(`💥 Error applying optimization: ${rec.description} - ${error}`)
results.push({ success: false, error: String(error) })
}
}
console.log(`🔧 Auto-optimization completed: ${applied} applied, ${failed} failed`)
return { applied, failed, results }
}
/**
* Export performance data for external analysis
*/
exportPerformanceData(format: 'json' | 'csv'): string {
if (format === 'json') {
return JSON.stringify({
exportTime: Date.now(),
metrics: this.metrics,
alerts: this.alerts,
systemHealth: this.getSystemHealth(),
stats: this.getPerformanceStats(Date.now() - 86400000, Date.now()) // Last 24h
}, null, 2)
} else {
// CSV format
const headers = [
'timestamp', 'service', 'operation', 'provider', 'duration',
'success', 'tokensUsed', 'costUSD', 'cacheHit', 'error'
]
const rows = this.metrics.map(m => [
m.timestamp,
m.service,
m.operation,
m.provider || '',
m.duration,
m.success,
m.tokensUsed || 0,
m.costUSD || 0,
m.cacheHit || false,
m.error || ''
])
return [headers, ...rows].map(row => row.join(',')).join('\n')
}
}
// Private methods for internal calculations and operations
private collectSystemMetrics(): void {
const memoryUsage = this.getMemoryUsage()
const cpuUsage = this.getCpuUsage()
this.recordMetric({
timestamp: Date.now(),
service: 'system',
operation: 'health_check',
duration: 0,
success: true,
memoryUsage,
cpuUsage
})
}
private checkSystemHealth(): void {
const health = this.getSystemHealth()
if (health.status === 'critical') {
this.createAlert({
severity: 'critical',
message: `System health is critical (score: ${health.score})`,
source: 'health_monitor',
resolved: false
})
} else if (health.status === 'warning') {
this.createAlert({
severity: 'warning',
message: `System health degraded (score: ${health.score})`,
source: 'health_monitor',
resolved: false
})
}
}
private generateOptimizationRecommendations(): void {
const recommendations = this.getOptimizationRecommendations()
if (recommendations.length > 0 && this.config.enableAutoOptimization) {
console.log(`🔧 Found ${recommendations.length} optimization opportunities`)
this.applyOptimizations(recommendations)
}
}
private checkForImmedateAlerts(metric: PerformanceMetrics): void {
if (!metric.success) {
this.createAlert({
severity: 'error',
message: `${metric.service} ${metric.operation} failed: ${metric.error}`,
source: metric.service,
resolved: false
})
}
if (metric.duration && metric.duration > 10000) {
this.createAlert({
severity: 'warning',
message: `High latency detected: ${metric.service} ${metric.operation} took ${metric.duration}ms`,
source: metric.service,
resolved: false
})
}
}
private createAlert(alert: Omit<HealthAlert, 'id' | 'timestamp'>): void {
const newAlert: HealthAlert = {
id: `alert-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
timestamp: Date.now(),
...alert
}
this.alerts.push(newAlert)
// Keep only last 100 alerts
if (this.alerts.length > 100) {
this.alerts = this.alerts.slice(-100)
}
console.log(`🚨 ${alert.severity.toUpperCase()}: ${alert.message}`)
}
private getActiveAlerts(): HealthAlert[] {
return this.alerts.filter(a => !a.resolved && Date.now() - a.timestamp < 3600000) // Last hour
}
private calculateHealthScore(checks: any): number {
let score = 100
if (checks.errorRate > this.alertThresholds.errorRate) {
score -= (checks.errorRate - this.alertThresholds.errorRate) * 500
}
if (checks.responseTime > this.alertThresholds.responseTime) {
score -= Math.min(30, (checks.responseTime - this.alertThresholds.responseTime) / 1000 * 5)
}
if (checks.memoryUsage > this.alertThresholds.memoryUsage) {
score -= (checks.memoryUsage - this.alertThresholds.memoryUsage) * 100
}
if (checks.cacheEfficiency < this.alertThresholds.cacheHitRate) {
score -= (this.alertThresholds.cacheHitRate - checks.cacheEfficiency) * 50
}
return Math.max(0, Math.min(100, score))
}
private determineHealthStatus(score: number, checks: any): 'healthy' | 'warning' | 'critical' {
if (score < 30 || checks.errorRate > 0.2 || !checks.apiConnectivity) {
return 'critical'
} else if (score < 70 || checks.errorRate > 0.1 || checks.responseTime > 5000) {
return 'warning'
} else {
return 'healthy'
}
}
private calculateCacheHitRate(metrics: PerformanceMetrics[]): number {
const cacheMetrics = metrics.filter(m => m.cacheHit !== undefined)
if (cacheMetrics.length === 0) return 0
return cacheMetrics.filter(m => m.cacheHit).length / cacheMetrics.length
}
private getMemoryUsage(): number {
// Simulated memory usage - in real implementation, use actual system metrics
return Math.random() * 0.8 + 0.2
}
private getCpuUsage(): number {
// Simulated CPU usage - in real implementation, use actual system metrics
return Math.random() * 0.6 + 0.1
}
private calculateCostBudgetUsage(): number {
const todayStart = new Date().setHours(0, 0, 0, 0)
const todayMetrics = this.metrics.filter(m => m.timestamp >= todayStart)
const todayCost = todayMetrics.reduce((sum, m) => sum + (m.costUSD || 0), 0)
const dailyBudget = 100 // $100 daily budget - should be configurable
return todayCost / dailyBudget
}
private groupCostsByProvider(metrics: PerformanceMetrics[]): Record<string, number> {
const costs: Record<string, number> = {}
metrics.forEach(m => {
if (m.provider && m.costUSD) {
costs[m.provider] = (costs[m.provider] || 0) + m.costUSD
}
})
return costs
}
private groupErrorsByType(failedMetrics: PerformanceMetrics[]): Record<string, number> {
const errors: Record<string, number> = {}
failedMetrics.forEach(m => {
if (m.error) {
const errorType = this.categorizeError(m.error)
errors[errorType] = (errors[errorType] || 0) + 1
}
})
return errors
}
private groupErrorsByProvider(failedMetrics: PerformanceMetrics[]): Record<string, number> {
const errors: Record<string, number> = {}
failedMetrics.forEach(m => {
if (m.provider) {
errors[m.provider] = (errors[m.provider] || 0) + 1
}
})
return errors
}
private getTopErrors(failedMetrics: PerformanceMetrics[]): Array<{ error: string; count: number }> {
const errorCounts: Record<string, number> = {}
failedMetrics.forEach(m => {
if (m.error) {
errorCounts[m.error] = (errorCounts[m.error] || 0) + 1
}
})
return Object.entries(errorCounts)
.map(([error, count]) => ({ error, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 5)
}
private categorizeError(error: string): string {
const lowerError = error.toLowerCase()
if (lowerError.includes('timeout')) return 'timeout'
if (lowerError.includes('rate limit')) return 'rate_limit'
if (lowerError.includes('auth')) return 'authentication'
if (lowerError.includes('network')) return 'network'
if (lowerError.includes('quota')) return 'quota_exceeded'
return 'unknown'
}
private recommendBestProvider(stats: PerformanceStats): AIProvider {
const providerPerformance = {
openai: 0,
google: 0,
baidu: 0
}
// Simple scoring based on error rates
Object.entries(stats.errors.byProvider).forEach(([provider, errors]) => {
const errorRate = errors / stats.requests.total
providerPerformance[provider as AIProvider] = 1 - errorRate
})
return Object.entries(providerPerformance)
.sort(([, a], [, b]) => b - a)[0][0] as AIProvider
}
private getEmptyStats(startTime: number, endTime: number): PerformanceStats {
return {
timeRange: { start: startTime, end: endTime, duration: endTime - startTime },
requests: { total: 0, successful: 0, failed: 0, successRate: 0 },
timing: { averageLatency: 0, medianLatency: 0, p95Latency: 0, p99Latency: 0 },
costs: { total: 0, average: 0, byProvider: {} },
cache: { hitRate: 0, totalRequests: 0, hits: 0, misses: 0 },
errors: { byType: {}, byProvider: {}, topErrors: [] }
}
}
private async applyOptimization(recommendation: OptimizationRecommendation): Promise<{ success: boolean; error?: string }> {
// Simulated optimization application
// In real implementation, this would apply actual configuration changes
try {
switch (recommendation.type) {
case 'cache':
// Apply cache optimization
console.log(`🔧 Applying cache optimization: ${JSON.stringify(recommendation.implementation.parameters)}`)
break
case 'provider':
// Switch provider
console.log(`🔧 Switching to provider: ${recommendation.implementation.parameters.newPrimary}`)
break
case 'batch':
// Enable batch processing
console.log(`🔧 Enabling batch processing: batch size ${recommendation.implementation.parameters.batchSize}`)
break
case 'model':
// Implement model selection
console.log(`🔧 Implementing intelligent model selection`)
break
default:
return { success: false, error: 'Unknown optimization type' }
}
return { success: true }
} catch (error) {
return { success: false, error: String(error) }
}
}
}
// Export default configuration
export const defaultPerformanceConfig = {
monitoringInterval: 30000, // 30 seconds
maxHistory: 10000,
enableAutoOptimization: false, // Disabled by default for safety
alertWebhook: undefined
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,563 @@
// AI Service Manager - Unified coordinator for all AI services
import {
AIServiceConfig,
AIProvider,
AIResponse,
AIServiceError,
UsageStatistics,
CacheOptions
} from '../types/ai-types.uts'
import { AITranslationService } from './AITranslationService.uts'
import { AIContentAnalysisService } from './AIContentAnalysisService.uts'
import { AIChatService } from './AIChatService.uts'
import { AIRecommendationService } from './AIRecommendationService.uts'
import { ContentProcessingPipeline } from './ContentProcessingPipeline.uts'
// 服务状态枚举
type ServiceStatus = 'initializing' | 'ready' | 'busy' | 'error' | 'maintenance'
// 服务健康状态
type ServiceHealth = {
status: ServiceStatus
lastChecked: number
responseTime: number
errorRate: number
uptime: number
version: string
capabilities: string[]
}
// 负载均衡策略
type LoadBalanceStrategy = 'round_robin' | 'least_connections' | 'weighted' | 'random'
// 服务监控配置
type MonitoringConfig = {
healthCheckInterval: number // 健康检查间隔(毫秒)
maxErrorRate: number // 最大错误率
maxResponseTime: number // 最大响应时间(毫秒)
alertThresholds: {
errorRate: number
responseTime: number
dailyCost: number
}
}
// 成本控制配置
type CostControlConfig = {
dailyLimit: number // 每日成本限制USD
monthlyLimit: number // 每月成本限制USD
perRequestLimit: number // 单次请求成本限制USD
alertThresholds: {
daily: number // 每日预警阈值
monthly: number // 每月预警阈值
}
}
// 管理器统计
type ManagerStats = {
totalRequests: number
successfulRequests: number
failedRequests: number
totalCost: number
avgResponseTime: number
servicesHealth: Record<string, ServiceHealth>
dailyUsage: UsageStatistics[]
costBreakdown: Record<AIProvider, number>
lastReset: number
}
/**
* AI服务管理器
* 统一管理所有AI服务提供负载均衡、监控、成本控制等功能
*/
export class AIServiceManager {
private config: AIServiceConfig
private monitoringConfig: MonitoringConfig
private costControlConfig: CostControlConfig
private cacheOptions: CacheOptions
// 服务实例
private translationService: AITranslationService
private analysisService: AIContentAnalysisService
private chatService: AIChatService
private recommendationService: AIRecommendationService
private processingPipeline: ContentProcessingPipeline
// 状态管理
private servicesHealth: Map<string, ServiceHealth> = new Map()
private loadBalanceState: Map<AIProvider, number> = new Map()
private stats: ManagerStats
private healthCheckInterval: any
private isInitialized: boolean = false
constructor(
config: AIServiceConfig,
monitoringConfig: Partial<MonitoringConfig> = {},
costControlConfig: Partial<CostControlConfig> = {},
cacheOptions: Partial<CacheOptions> = {}
) {
this.config = config
this.monitoringConfig = this.createDefaultMonitoringConfig(monitoringConfig)
this.costControlConfig = this.createDefaultCostControlConfig(costControlConfig)
this.cacheOptions = this.createDefaultCacheOptions(cacheOptions)
this.stats = this.initializeStats()
this.initializeServices()
}
/**
* 初始化所有服务
*/
async initialize(): Promise<AIResponse<boolean>> {
try {
console.log('Initializing AI Service Manager...')
// 初始化各个服务
this.translationService = new AITranslationService(this.config, this.cacheOptions)
this.analysisService = new AIContentAnalysisService(this.config)
this.chatService = new AIChatService(this.config)
this.recommendationService = new AIRecommendationService(this.config)
this.processingPipeline = new ContentProcessingPipeline(this.config)
// 初始化服务健康状态
await this.initializeHealthStatus()
// 启动健康检查
this.startHealthMonitoring()
// 初始化负载均衡状态
this.initializeLoadBalancing()
this.isInitialized = true
console.log('AI Service Manager initialized successfully')
return { success: true, data: true }
} catch (error) {
console.error('Failed to initialize AI Service Manager:', error)
return {
success: false,
error: error.message || 'Initialization failed'
}
}
}
/**
* 获取翻译服务
*/
getTranslationService(): AITranslationService {
this.ensureInitialized()
return this.translationService
}
/**
* 获取内容分析服务
*/
getAnalysisService(): AIContentAnalysisService {
this.ensureInitialized()
return this.analysisService
}
/**
* 获取聊天服务
*/
getChatService(): AIChatService {
this.ensureInitialized()
return this.chatService
}
/**
* 获取推荐服务
*/
getRecommendationService(): AIRecommendationService {
this.ensureInitialized()
return this.recommendationService
}
/**
* 获取内容处理管道
*/
getProcessingPipeline(): ContentProcessingPipeline {
this.ensureInitialized()
return this.processingPipeline
}
/**
* 选择最佳提供商
* @param serviceType 服务类型
*/
selectBestProvider(serviceType: string = 'general'): AIProvider {
const availableProviders = this.getAvailableProviders()
if (availableProviders.length === 0) {
return 'openai' // 默认提供商
}
// 基于健康状态和负载均衡策略选择
return this.applyLoadBalancing(availableProviders, serviceType)
}
/**
* 检查成本限制
* @param estimatedCost 预估成本
*/
checkCostLimits(estimatedCost: number): boolean {
const now = new Date()
const today = now.toISOString().split('T')[0]
const currentMonth = `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}`
// 检查每日限制
const dailyCost = this.getDailyCost(today)
if (dailyCost + estimatedCost > this.costControlConfig.dailyLimit) {
console.warn(`Daily cost limit exceeded: ${dailyCost + estimatedCost} > ${this.costControlConfig.dailyLimit}`)
return false
}
// 检查每月限制
const monthlyCost = this.getMonthlyCost(currentMonth)
if (monthlyCost + estimatedCost > this.costControlConfig.monthlyLimit) {
console.warn(`Monthly cost limit exceeded: ${monthlyCost + estimatedCost} > ${this.costControlConfig.monthlyLimit}`)
return false
}
// 检查单次请求限制
if (estimatedCost > this.costControlConfig.perRequestLimit) {
console.warn(`Per-request cost limit exceeded: ${estimatedCost} > ${this.costControlConfig.perRequestLimit}`)
return false
}
return true
}
/**
* 记录使用统计
* @param provider 提供商
* @param serviceType 服务类型
* @param stats 统计信息
*/
recordUsage(provider: AIProvider, serviceType: string, stats: Partial<UsageStatistics>): void {
this.stats.totalRequests++
if (stats.requestsCount && stats.requestsCount > 0) {
this.stats.successfulRequests++
} else {
this.stats.failedRequests++
}
this.stats.totalCost += stats.costUSD || 0
this.stats.costBreakdown[provider] = (this.stats.costBreakdown[provider] || 0) + (stats.costUSD || 0)
if (stats.avgResponseTimeMs) {
this.stats.avgResponseTime = (
this.stats.avgResponseTime * (this.stats.totalRequests - 1) + stats.avgResponseTimeMs
) / this.stats.totalRequests
}
// 记录每日使用情况
const today = new Date().toISOString().split('T')[0]
const hour = new Date().getHours()
const dailyStats: UsageStatistics = {
provider,
serviceType,
tokensUsed: stats.tokensUsed || 0,
requestsCount: stats.requestsCount || 0,
costUSD: stats.costUSD || 0,
successCount: stats.successCount || 0,
errorCount: stats.errorCount || 0,
avgResponseTimeMs: stats.avgResponseTimeMs || 0,
date: today,
hour
}
this.stats.dailyUsage.push(dailyStats)
// 保持最近30天的数据
const cutoffDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]
this.stats.dailyUsage = this.stats.dailyUsage.filter(usage => usage.date >= cutoffDate)
}
/**
* 获取服务健康状态
*/
getServicesHealth(): Record<string, ServiceHealth> {
const health: Record<string, ServiceHealth> = {}
for (const [serviceName, serviceHealth] of this.servicesHealth.entries()) {
health[serviceName] = { ...serviceHealth }
}
return health
}
/**
* 获取管理器统计
*/
getManagerStatistics(): ManagerStats {
return {
...this.stats,
servicesHealth: this.getServicesHealth()
}
}
/**
* 重置统计数据
*/
resetStatistics(): void {
this.stats = this.initializeStats()
}
/**
* 停止所有服务
*/
async shutdown(): Promise<void> {
console.log('Shutting down AI Service Manager...')
// 停止健康检查
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval)
}
// 清理缓存
if (this.translationService) {
this.translationService.clearCache()
}
this.isInitialized = false
console.log('AI Service Manager shut down completed')
}
// Private methods
private initializeServices(): void {
// 初始化负载均衡状态
const providers: AIProvider[] = ['openai', 'google', 'baidu', 'custom']
providers.forEach(provider => {
this.loadBalanceState.set(provider, 0)
})
}
private async initializeHealthStatus(): Promise<void> {
const services = ['translation', 'analysis', 'chat', 'recommendation', 'pipeline']
for (const serviceName of services) {
const health: ServiceHealth = {
status: 'ready',
lastChecked: Date.now(),
responseTime: 0,
errorRate: 0,
uptime: Date.now(),
version: '1.0.0',
capabilities: this.getServiceCapabilities(serviceName)
}
this.servicesHealth.set(serviceName, health)
}
}
private getServiceCapabilities(serviceName: string): string[] {
const capabilities: Record<string, string[]> = {
translation: ['text_translation', 'language_detection', 'batch_translation'],
analysis: ['sentiment_analysis', 'entity_extraction', 'content_classification', 'quality_assessment'],
chat: ['conversation', 'multilingual_support', 'context_awareness'],
recommendation: ['personalized_recommendations', 'trending_content', 'similarity_matching'],
pipeline: ['automated_processing', 'batch_processing', 'workflow_management']
}
return capabilities[serviceName] || []
}
private startHealthMonitoring(): void {
this.healthCheckInterval = setInterval(() => {
this.performHealthCheck()
}, this.monitoringConfig.healthCheckInterval)
}
private async performHealthCheck(): Promise<void> {
for (const [serviceName, health] of this.servicesHealth.entries()) {
try {
const startTime = Date.now()
// 执行简单的健康检查
await this.checkServiceHealth(serviceName)
const responseTime = Date.now() - startTime
// 更新健康状态
health.lastChecked = Date.now()
health.responseTime = responseTime
health.status = responseTime > this.monitoringConfig.maxResponseTime ? 'error' : 'ready'
// 检查错误率
if (health.errorRate > this.monitoringConfig.maxErrorRate) {
health.status = 'error'
}
} catch (error) {
console.error(`Health check failed for ${serviceName}:`, error)
this.servicesHealth.get(serviceName)!.status = 'error'
}
}
}
private async checkServiceHealth(serviceName: string): Promise<void> {
// 简单的健康检查实现
switch (serviceName) {
case 'translation':
// 可以测试一个简单的翻译
break
case 'analysis':
// 可以测试一个简单的分析
break
case 'chat':
// 可以检查会话状态
break
case 'recommendation':
// 可以检查推荐算法状态
break
case 'pipeline':
// 可以检查处理管道状态
break
}
// 模拟健康检查延迟
await this.delay(Math.random() * 100 + 50)
}
private initializeLoadBalancing(): void {
const providers = this.getAvailableProviders()
providers.forEach(provider => {
this.loadBalanceState.set(provider, 0)
})
}
private getAvailableProviders(): AIProvider[] {
const providers: AIProvider[] = []
if (this.config.openai?.apiKey) providers.push('openai')
if (this.config.google?.apiKey) providers.push('google')
if (this.config.baidu?.apiKey) providers.push('baidu')
return providers
}
private applyLoadBalancing(providers: AIProvider[], serviceType: string): AIProvider {
// 过滤健康的提供商
const healthyProviders = providers.filter(provider => {
const serviceName = this.getServiceNameForProvider(provider, serviceType)
const health = this.servicesHealth.get(serviceName)
return health && health.status === 'ready'
})
if (healthyProviders.length === 0) {
return providers[0] // 回退到第一个可用提供商
}
// 轮询策略
const providerCounts = healthyProviders.map(provider => ({
provider,
count: this.loadBalanceState.get(provider) || 0
}))
// 选择使用次数最少的提供商
const selectedProvider = providerCounts.reduce((min, current) =>
current.count < min.count ? current : min
).provider
// 更新计数
this.loadBalanceState.set(selectedProvider, (this.loadBalanceState.get(selectedProvider) || 0) + 1)
return selectedProvider
}
private getServiceNameForProvider(provider: AIProvider, serviceType: string): string {
// 根据提供商和服务类型映射到内部服务名称
const serviceMap: Record<string, string> = {
'translation': 'translation',
'analysis': 'analysis',
'chat': 'chat',
'recommendation': 'recommendation'
}
return serviceMap[serviceType] || 'translation'
}
private getDailyCost(date: string): number {
return this.stats.dailyUsage
.filter(usage => usage.date === date)
.reduce((total, usage) => total + usage.costUSD, 0)
}
private getMonthlyCost(month: string): number {
return this.stats.dailyUsage
.filter(usage => usage.date.startsWith(month))
.reduce((total, usage) => total + usage.costUSD, 0)
}
private ensureInitialized(): void {
if (!this.isInitialized) {
throw new Error('AI Service Manager not initialized. Call initialize() first.')
}
}
private createDefaultMonitoringConfig(overrides: Partial<MonitoringConfig>): MonitoringConfig {
return {
healthCheckInterval: 60000, // 1分钟
maxErrorRate: 0.1, // 10%
maxResponseTime: 5000, // 5秒
alertThresholds: {
errorRate: 0.05, // 5%
responseTime: 3000, // 3秒
dailyCost: 100 // $100
},
...overrides
}
}
private createDefaultCostControlConfig(overrides: Partial<CostControlConfig>): CostControlConfig {
return {
dailyLimit: 200, // $200
monthlyLimit: 5000, // $5000
perRequestLimit: 10, // $10
alertThresholds: {
daily: 150, // $150
monthly: 4000 // $4000
},
...overrides
}
}
private createDefaultCacheOptions(overrides: Partial<CacheOptions>): CacheOptions {
return {
enabled: true,
ttlHours: 24,
maxSize: 10000,
strategy: 'lru',
...overrides
}
}
private initializeStats(): ManagerStats {
const providers: AIProvider[] = ['openai', 'google', 'baidu', 'custom']
const costBreakdown: Record<AIProvider, number> = {} as Record<AIProvider, number>
providers.forEach(provider => {
costBreakdown[provider] = 0
})
return {
totalRequests: 0,
successfulRequests: 0,
failedRequests: 0,
totalCost: 0,
avgResponseTime: 0,
servicesHealth: {},
dailyUsage: [],
costBreakdown,
lastReset: Date.now()
}
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
}

View File

@@ -0,0 +1,701 @@
// AI Translation Service - Multi-provider translation implementation
import {
TranslationResult,
TranslationOptions,
AIProvider,
AIResponse,
AIServiceConfig,
BatchProcessingOptions,
CacheOptions,
AIServiceError
} from '../types/ai-types.uts'
// 翻译缓存条目
type TranslationCacheEntry = {
key: string
result: TranslationResult
createdAt: number
ttl: number
}
// 翻译统计
type TranslationStats = {
totalRequests: number
successCount: number
errorCount: number
totalTokens: number
totalCost: number
avgQuality: number
cacheHitRate: number
}
/**
* AI翻译服务类
* 支持多种AI提供商的翻译服务包括缓存、批处理、质量评估等功能
*/
export class AITranslationService {
private config: AIServiceConfig
private cache: Map<string, TranslationCacheEntry> = new Map()
private cacheOptions: CacheOptions
private stats: TranslationStats = {
totalRequests: 0,
successCount: 0,
errorCount: 0,
totalTokens: 0,
totalCost: 0,
avgQuality: 0,
cacheHitRate: 0
}
constructor(config: AIServiceConfig, cacheOptions: CacheOptions = {
enabled: true,
ttlHours: 24,
maxSize: 10000,
strategy: 'lru'
}) {
this.config = config
this.cacheOptions = cacheOptions
this.initializeCache()
}
/**
* 翻译文本
* @param text 原文本
* @param targetLang 目标语言
* @param sourceLang 源语言(可选,自动检测)
* @param options 翻译选项
*/
async translateText(
text: string,
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {}
): Promise<AIResponse<TranslationResult>> {
try {
this.stats.totalRequests++
// 检查缓存
const cacheKey = this.generateCacheKey(text, targetLang, sourceLang, options)
const cached = this.getFromCache(cacheKey)
if (cached) {
return { success: true, data: cached }
}
// 选择提供商
const provider = options.provider || this.selectBestProvider()
// 执行翻译
let result: TranslationResult
switch (provider) {
case 'openai':
result = await this.translateWithOpenAI(text, targetLang, sourceLang, options)
break
case 'google':
result = await this.translateWithGoogle(text, targetLang, sourceLang, options)
break
case 'baidu':
result = await this.translateWithBaidu(text, targetLang, sourceLang, options)
break
default:
throw new Error(`Unsupported translation provider: ${provider}`)
}
// 质量检查
if (result.qualityScore < (this.config.qualityThresholds?.translation || 0.7)) {
console.warn(`Translation quality below threshold: ${result.qualityScore}`)
}
// 缓存结果
this.addToCache(cacheKey, result)
// 更新统计
this.updateStats(result)
return {
success: true,
data: result,
tokensUsed: result.tokensUsed,
processingTimeMs: result.processingTimeMs,
costUSD: result.costUSD,
provider: result.provider
}
} catch (error) {
this.stats.errorCount++
const aiError: AIServiceError = {
code: 'TRANSLATION_ERROR',
message: error.message || 'Translation failed',
provider: options.provider,
retryable: this.isRetryableError(error)
}
return {
success: false,
error: aiError.message,
errorCode: aiError.code
}
}
}
/**
* 批量翻译
* @param texts 文本数组
* @param targetLang 目标语言
* @param sourceLang 源语言
* @param options 翻译选项
* @param batchOptions 批处理选项
*/
async translateBatch(
texts: string[],
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {},
batchOptions: BatchProcessingOptions = {
batchSize: 10,
concurrency: 3,
retryCount: 2,
delayMs: 1000
}
): Promise<AIResponse<TranslationResult[]>> {
try {
const results: TranslationResult[] = []
const batches = this.createBatches(texts, batchOptions.batchSize)
for (let i = 0; i < batches.length; i++) {
const batch = batches[i]
const batchPromises = batch.map(async (text, index) => {
try {
const response = await this.translateText(text, targetLang, sourceLang, options)
if (response.success && response.data) {
return response.data
}
throw new Error(response.error || 'Translation failed')
} catch (error) {
if (batchOptions.onError) {
batchOptions.onError(error, text)
}
throw error
}
})
const batchResults = await Promise.allSettled(batchPromises)
for (const result of batchResults) {
if (result.status === 'fulfilled') {
results.push(result.value)
}
}
// 进度回调
if (batchOptions.onProgress) {
batchOptions.onProgress(results.length, texts.length)
}
// 批次间延迟
if (i < batches.length - 1 && batchOptions.delayMs > 0) {
await this.delay(batchOptions.delayMs)
}
}
return { success: true, data: results }
} catch (error) {
return {
success: false,
error: error.message || 'Batch translation failed'
}
}
}
/**
* 检测语言
* @param text 文本
*/
async detectLanguage(text: string): Promise<AIResponse<string>> {
try {
// 使用正则表达式和字符集进行基础检测
const basicDetection = this.basicLanguageDetection(text)
if (basicDetection.confidence > 0.8) {
return { success: true, data: basicDetection.language }
}
// 使用AI提供商进行检测
const provider = this.selectBestProvider()
let detectedLang: string
switch (provider) {
case 'google':
detectedLang = await this.detectLanguageWithGoogle(text)
break
case 'baidu':
detectedLang = await this.detectLanguageWithBaidu(text)
break
default:
detectedLang = basicDetection.language
}
return { success: true, data: detectedLang, provider }
} catch (error) {
return {
success: false,
error: error.message || 'Language detection failed'
}
}
}
/**
* 获取支持的语言列表
*/
getSupportedLanguages(): string[] {
return [
'zh-CN', 'zh-TW', 'en', 'ja', 'ko', 'es', 'fr', 'de', 'it', 'pt',
'ru', 'ar', 'hi', 'th', 'vi', 'id', 'ms', 'tl', 'tr', 'nl'
]
}
/**
* 获取翻译统计
*/
getStatistics(): TranslationStats {
this.updateCacheHitRate()
return { ...this.stats }
}
/**
* 清理缓存
*/
clearCache(): void {
this.cache.clear()
}
// Private methods
private async translateWithOpenAI(
text: string,
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {}
): Promise<TranslationResult> {
const startTime = Date.now()
const systemPrompt = this.buildOpenAISystemPrompt(targetLang, sourceLang, options)
const userPrompt = `请翻译以下文本到${this.getLanguageName(targetLang)}\n\n${text}`
const requestBody = {
model: options.model || this.config.openai?.model || 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt }
],
temperature: options.temperature || this.config.openai?.temperature || 0.3,
max_tokens: options.maxTokens || this.config.openai?.maxTokens || 2000
}
// 模拟API调用实际项目中替换为真实的HTTP请求
const response = await this.mockOpenAIRequest(requestBody)
const processingTime = Date.now() - startTime
const tokensUsed = response.usage.total_tokens
const cost = this.calculateOpenAICost(tokensUsed, requestBody.model)
return {
translatedText: response.choices[0].message.content.trim(),
originalText: text,
sourceLang: sourceLang || 'auto',
targetLang,
confidence: 0.95,
qualityScore: this.evaluateTranslationQuality(text, response.choices[0].message.content),
provider: 'openai',
tokensUsed,
processingTimeMs: processingTime,
costUSD: cost
}
}
private async translateWithGoogle(
text: string,
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {}
): Promise<TranslationResult> {
const startTime = Date.now()
// 模拟Google Translate API调用
const response = await this.mockGoogleRequest({
q: text,
target: this.convertToGoogleLangCode(targetLang),
source: sourceLang ? this.convertToGoogleLangCode(sourceLang) : undefined,
format: 'text'
})
const processingTime = Date.now() - startTime
const tokensUsed = Math.ceil(text.length / 4) // 估算
const cost = this.calculateGoogleCost(text.length)
return {
translatedText: response.data.translations[0].translatedText,
originalText: text,
sourceLang: response.data.translations[0].detectedSourceLanguage || sourceLang || 'auto',
targetLang,
confidence: 0.92,
qualityScore: this.evaluateTranslationQuality(text, response.data.translations[0].translatedText),
provider: 'google',
tokensUsed,
processingTimeMs: processingTime,
costUSD: cost
}
}
private async translateWithBaidu(
text: string,
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {}
): Promise<TranslationResult> {
const startTime = Date.now()
// 模拟百度翻译API调用
const response = await this.mockBaiduRequest({
q: text,
from: sourceLang ? this.convertToBaiduLangCode(sourceLang) : 'auto',
to: this.convertToBaiduLangCode(targetLang),
appid: this.config.baidu?.apiKey || '',
salt: Date.now().toString(),
sign: 'mock_sign'
})
const processingTime = Date.now() - startTime
const tokensUsed = Math.ceil(text.length / 4)
const cost = this.calculateBaiduCost(text.length)
return {
translatedText: response.trans_result[0].dst,
originalText: text,
sourceLang: response.from || sourceLang || 'auto',
targetLang,
confidence: 0.90,
qualityScore: this.evaluateTranslationQuality(text, response.trans_result[0].dst),
provider: 'baidu',
tokensUsed,
processingTimeMs: processingTime,
costUSD: cost
}
}
private selectBestProvider(): AIProvider {
// 根据配置和可用性选择最佳提供商
if (this.config.openai?.apiKey) return 'openai'
if (this.config.google?.apiKey) return 'google'
if (this.config.baidu?.apiKey) return 'baidu'
return 'openai' // 默认
}
private generateCacheKey(
text: string,
targetLang: string,
sourceLang?: string,
options: TranslationOptions = {}
): string {
const optionsStr = JSON.stringify({
provider: options.provider,
temperature: options.temperature,
culturalAdaptation: options.culturalAdaptation
})
return `${text}_${sourceLang || 'auto'}_${targetLang}_${optionsStr}`.replace(/\s+/g, '_')
}
private getFromCache(key: string): TranslationResult | null {
if (!this.cacheOptions.enabled) return null
const entry = this.cache.get(key)
if (!entry) return null
// 检查TTL
const now = Date.now()
if (now > entry.createdAt + (entry.ttl * 60 * 60 * 1000)) {
this.cache.delete(key)
return null
}
return entry.result
}
private addToCache(key: string, result: TranslationResult): void {
if (!this.cacheOptions.enabled) return
// 检查缓存大小限制
if (this.cache.size >= this.cacheOptions.maxSize) {
this.evictCache()
}
this.cache.set(key, {
key,
result,
createdAt: Date.now(),
ttl: this.cacheOptions.ttlHours
})
}
private evictCache(): void {
// LRU策略删除最早的条目
const oldestKey = this.cache.keys().next().value
if (oldestKey) {
this.cache.delete(oldestKey)
}
}
private initializeCache(): void {
// 初始化缓存清理定时器
setInterval(() => {
this.cleanupExpiredCache()
}, 60 * 60 * 1000) // 每小时清理一次
}
private cleanupExpiredCache(): void {
const now = Date.now()
for (const [key, entry] of this.cache.entries()) {
if (now > entry.createdAt + (entry.ttl * 60 * 60 * 1000)) {
this.cache.delete(key)
}
}
}
private createBatches<T>(items: T[], batchSize: number): T[][] {
const batches: T[][] = []
for (let i = 0; i < items.length; i += batchSize) {
batches.push(items.slice(i, i + batchSize))
}
return batches
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
private updateStats(result: TranslationResult): void {
this.stats.successCount++
this.stats.totalTokens += result.tokensUsed
this.stats.totalCost += result.costUSD
this.stats.avgQuality = (this.stats.avgQuality * (this.stats.successCount - 1) + result.qualityScore) / this.stats.successCount
}
private updateCacheHitRate(): void {
if (this.stats.totalRequests > 0) {
const cacheHits = this.stats.totalRequests - this.stats.successCount - this.stats.errorCount
this.stats.cacheHitRate = cacheHits / this.stats.totalRequests
}
}
private evaluateTranslationQuality(original: string, translated: string): number {
// 简单的质量评估算法
if (!translated || translated.length === 0) return 0
const lengthRatio = translated.length / original.length
const lengthScore = lengthRatio > 0.5 && lengthRatio < 2 ? 1 : 0.7
// 检查是否包含原文(可能翻译失败)
const similarityScore = original.toLowerCase() === translated.toLowerCase() ? 0.3 : 1
return (lengthScore + similarityScore) / 2
}
private basicLanguageDetection(text: string): { language: string, confidence: number } {
// 基于字符集的语言检测
const chineseRegex = /[\u4e00-\u9fff]/
const japaneseRegex = /[\u3040-\u309f\u30a0-\u30ff]/
const koreanRegex = /[\uac00-\ud7af]/
const arabicRegex = /[\u0600-\u06ff]/
const russianRegex = /[\u0400-\u04ff]/
if (chineseRegex.test(text)) return { language: 'zh-CN', confidence: 0.9 }
if (japaneseRegex.test(text)) return { language: 'ja', confidence: 0.9 }
if (koreanRegex.test(text)) return { language: 'ko', confidence: 0.9 }
if (arabicRegex.test(text)) return { language: 'ar', confidence: 0.8 }
if (russianRegex.test(text)) return { language: 'ru', confidence: 0.8 }
return { language: 'en', confidence: 0.5 }
}
private async detectLanguageWithGoogle(text: string): Promise<string> {
// 模拟Google语言检测API
const response = await this.mockGoogleDetectRequest({ q: text })
return this.convertFromGoogleLangCode(response.data.detections[0][0].language)
}
private async detectLanguageWithBaidu(text: string): Promise<string> {
// 模拟百度语言检测API
const response = await this.mockBaiduDetectRequest({ q: text })
return this.convertFromBaiduLangCode(response.lan)
}
private buildOpenAISystemPrompt(targetLang: string, sourceLang?: string, options: TranslationOptions = {}): string {
let prompt = `你是一个专业的翻译助手。请将文本翻译成${this.getLanguageName(targetLang)}。`
if (options.culturalAdaptation) {
prompt += ' 请注意文化适应性,确保翻译符合目标文化的表达习惯。'
}
if (options.preserveFormatting) {
prompt += ' 请保持原文的格式和结构。'
}
prompt += ' 只返回翻译结果,不需要其他说明。'
return prompt
}
private getLanguageName(langCode: string): string {
const languageNames: Record<string, string> = {
'zh-CN': '简体中文',
'zh-TW': '繁体中文',
'en': 'English',
'ja': '日本語',
'ko': '한국어',
'es': 'Español',
'fr': 'Français',
'de': 'Deutsch',
'it': 'Italiano',
'pt': 'Português',
'ru': 'Русский',
'ar': 'العربية',
'hi': 'हिन्दी',
'th': 'ไทย',
'vi': 'Tiếng Việt'
}
return languageNames[langCode] || langCode
}
private convertToGoogleLangCode(langCode: string): string {
const mapping: Record<string, string> = {
'zh-CN': 'zh',
'zh-TW': 'zh-tw'
}
return mapping[langCode] || langCode
}
private convertFromGoogleLangCode(langCode: string): string {
const mapping: Record<string, string> = {
'zh': 'zh-CN',
'zh-tw': 'zh-TW'
}
return mapping[langCode] || langCode
}
private convertToBaiduLangCode(langCode: string): string {
const mapping: Record<string, string> = {
'zh-CN': 'zh',
'zh-TW': 'cht',
'en': 'en',
'ja': 'jp',
'ko': 'kor',
'es': 'spa',
'fr': 'fra',
'de': 'de',
'ru': 'ru',
'ar': 'ara'
}
return mapping[langCode] || 'en'
}
private convertFromBaiduLangCode(langCode: string): string {
const mapping: Record<string, string> = {
'zh': 'zh-CN',
'cht': 'zh-TW',
'en': 'en',
'jp': 'ja',
'kor': 'ko',
'spa': 'es',
'fra': 'fr',
'de': 'de',
'ru': 'ru',
'ara': 'ar'
}
return mapping[langCode] || langCode
}
private calculateOpenAICost(tokens: number, model: string): number {
const pricing: Record<string, { input: number, output: number }> = {
'gpt-3.5-turbo': { input: 0.0015, output: 0.002 },
'gpt-4': { input: 0.03, output: 0.06 },
'gpt-4-turbo': { input: 0.01, output: 0.03 }
}
const modelPricing = pricing[model] || pricing['gpt-3.5-turbo']
return (tokens / 1000) * ((modelPricing.input + modelPricing.output) / 2)
}
private calculateGoogleCost(textLength: number): number {
// Google Translate pricing: $20 per 1M characters
return (textLength / 1000000) * 20
}
private calculateBaiduCost(textLength: number): number {
// 百度翻译定价较低
return (textLength / 1000000) * 10
}
private isRetryableError(error: any): boolean {
// 判断错误是否可重试
const retryableCodes = ['TIMEOUT', 'RATE_LIMIT', 'SERVER_ERROR']
return retryableCodes.includes(error.code) || error.status >= 500
}
// Mock API methods (在实际项目中替换为真实的HTTP请求)
private async mockOpenAIRequest(requestBody: any): Promise<any> {
await this.delay(Math.random() * 1000 + 500) // 模拟网络延迟
return {
choices: [{
message: {
content: `[Translated by OpenAI] ${requestBody.messages[1].content.split('\n\n')[1] || 'Translation result'}`
}
}],
usage: {
total_tokens: Math.ceil(requestBody.messages[1].content.length / 4) + 50
}
}
}
private async mockGoogleRequest(params: any): Promise<any> {
await this.delay(Math.random() * 800 + 400)
return {
data: {
translations: [{
translatedText: `[Translated by Google] ${params.q}`,
detectedSourceLanguage: 'zh'
}]
}
}
}
private async mockBaiduRequest(params: any): Promise<any> {
await this.delay(Math.random() * 600 + 300)
return {
trans_result: [{
src: params.q,
dst: `[Translated by Baidu] ${params.q}`
}],
from: params.from
}
}
private async mockGoogleDetectRequest(params: any): Promise<any> {
await this.delay(200)
return {
data: {
detections: [[{
language: 'zh',
confidence: 0.95
}]]
}
}
}
private async mockBaiduDetectRequest(params: any): Promise<any> {
await this.delay(200)
return {
lan: 'zh',
confidence: 0.92
}
}
}

View File

@@ -0,0 +1,755 @@
// Content Processing Pipeline - Automated news content workflow
import {
ContentInfo,
ProcessingStep,
AIProvider,
AIResponse,
AIServiceConfig,
AIServiceError,
TranslationResult,
ContentAnalysisResult,
BatchProcessingOptions
} from '../types/ai-types.uts'
import { AITranslationService } from './AITranslationService.uts'
import { AIContentAnalysisService } from './AIContentAnalysisService.uts'
// 处理阶段枚举
type ProcessingStage =
| 'fetching'
| 'validation'
| 'analysis'
| 'translation'
| 'categorization'
| 'quality_check'
| 'storage'
| 'indexing'
| 'completed'
| 'failed'
// 处理状态
type ProcessingStatus = {
contentId: string
stage: ProcessingStage
progress: number // 0-100
startTime: number
lastUpdateTime: number
completedSteps: string[]
errors: Array<{ step: string, error: string, timestamp: number }>
metadata: UTSJSONObject
}
// 管道配置
type PipelineConfig = {
enabledSteps: string[]
parallelProcessing: boolean
maxConcurrency: number
retryCount: number
timeoutMs: number
qualityThreshold: number
targetLanguages: string[]
categorization: {
enabled: boolean
threshold: number
maxCategories: number
}
translation: {
enabled: boolean
targetLanguages: string[]
qualityThreshold: number
}
analysis: {
enabled: boolean
types: string[]
includeScores: boolean
}
}
// 处理结果
type ProcessingResult = {
contentId: string
originalContent: ContentInfo
processedContent: ContentInfo
translations: Record<string, TranslationResult>
analysis: ContentAnalysisResult
categories: string[]
qualityScore: number
processingTime: number
totalCost: number
status: ProcessingStage
errors: string[]
}
// 管道统计
type PipelineStats = {
totalProcessed: number
successCount: number
errorCount: number
avgProcessingTime: number
totalCost: number
stageStats: Record<ProcessingStage, {
count: number
avgTime: number
errorRate: number
}>
dailyThroughput: number
lastProcessedAt: number
}
/**
* 内容处理管道服务
* 自动化新闻内容的获取、分析、翻译、分类等全流程处理
*/
export class ContentProcessingPipeline {
private config: AIServiceConfig
private pipelineConfig: PipelineConfig
private translationService: AITranslationService
private analysisService: AIContentAnalysisService
private processingQueue: Map<string, ProcessingStatus> = new Map()
private processingSteps: Map<string, ProcessingStep> = new Map()
private stats: PipelineStats
constructor(
aiConfig: AIServiceConfig,
pipelineConfig: Partial<PipelineConfig> = {}
) {
this.config = aiConfig
this.pipelineConfig = this.createDefaultPipelineConfig(pipelineConfig)
this.translationService = new AITranslationService(aiConfig)
this.analysisService = new AIContentAnalysisService(aiConfig)
this.stats = this.initializeStats()
this.initializeProcessingSteps()
}
/**
* 处理单个内容
* @param content 原始内容
*/
async processContent(content: ContentInfo): Promise<AIResponse<ProcessingResult>> {
try {
const contentId = content.id
const startTime = Date.now()
// 初始化处理状态
const status: ProcessingStatus = {
contentId,
stage: 'validation',
progress: 0,
startTime,
lastUpdateTime: startTime,
completedSteps: [],
errors: [],
metadata: {}
}
this.processingQueue.set(contentId, status)
// 执行处理步骤
const result = await this.executeProcessingPipeline(content, status)
// 清理处理队列
this.processingQueue.delete(contentId)
// 更新统计
this.updateStats(result)
return { success: true, data: result }
} catch (error) {
const aiError: AIServiceError = {
code: 'PIPELINE_ERROR',
message: error.message || 'Content processing failed',
retryable: this.isRetryableError(error)
}
return {
success: false,
error: aiError.message,
errorCode: aiError.code
}
}
}
/**
* 批量处理内容
* @param contents 内容列表
* @param batchOptions 批处理选项
*/
async processBatch(
contents: ContentInfo[],
batchOptions: BatchProcessingOptions = {
batchSize: 5,
concurrency: 3,
retryCount: 2,
delayMs: 1000
}
): Promise<AIResponse<ProcessingResult[]>> {
try {
const results: ProcessingResult[] = []
const batches = this.createBatches(contents, batchOptions.batchSize)
for (let i = 0; i < batches.length; i++) {
const batch = batches[i]
if (this.pipelineConfig.parallelProcessing) {
// 并行处理
const batchPromises = batch.map(async (content) => {
try {
const response = await this.processContent(content)
if (response.success && response.data) {
return response.data
}
throw new Error(response.error || 'Processing failed')
} catch (error) {
if (batchOptions.onError) {
batchOptions.onError(error, content)
}
throw error
}
})
const batchResults = await Promise.allSettled(batchPromises)
for (const result of batchResults) {
if (result.status === 'fulfilled') {
results.push(result.value)
}
}
} else {
// 串行处理
for (const content of batch) {
try {
const response = await this.processContent(content)
if (response.success && response.data) {
results.push(response.data)
}
} catch (error) {
if (batchOptions.onError) {
batchOptions.onError(error, content)
}
}
}
}
// 进度回调
if (batchOptions.onProgress) {
batchOptions.onProgress(results.length, contents.length)
}
// 批次间延迟
if (i < batches.length - 1 && batchOptions.delayMs > 0) {
await this.delay(batchOptions.delayMs)
}
}
return { success: true, data: results }
} catch (error) {
return {
success: false,
error: error.message || 'Batch processing failed'
}
}
}
/**
* 获取处理状态
* @param contentId 内容ID
*/
getProcessingStatus(contentId: string): ProcessingStatus | null {
return this.processingQueue.get(contentId) || null
}
/**
* 获取所有处理中的内容状态
*/
getAllProcessingStatus(): ProcessingStatus[] {
return Array.from(this.processingQueue.values())
}
/**
* 添加自定义处理步骤
* @param step 处理步骤
*/
addProcessingStep(step: ProcessingStep): void {
this.processingSteps.set(step.name, step)
}
/**
* 移除处理步骤
* @param stepName 步骤名称
*/
removeProcessingStep(stepName: string): void {
this.processingSteps.delete(stepName)
}
/**
* 更新管道配置
* @param config 新配置
*/
updatePipelineConfig(config: Partial<PipelineConfig>): void {
this.pipelineConfig = { ...this.pipelineConfig, ...config }
}
/**
* 获取管道统计
*/
getPipelineStatistics(): PipelineStats {
return { ...this.stats }
}
/**
* 重置统计数据
*/
resetStatistics(): void {
this.stats = this.initializeStats()
}
// Private methods
private async executeProcessingPipeline(
content: ContentInfo,
status: ProcessingStatus
): Promise<ProcessingResult> {
const result: ProcessingResult = {
contentId: content.id,
originalContent: content,
processedContent: { ...content },
translations: {},
analysis: {} as ContentAnalysisResult,
categories: [],
qualityScore: 0,
processingTime: 0,
totalCost: 0,
status: 'fetching',
errors: []
}
try {
// 1. 内容验证
await this.executeStep('validation', content, result, status)
// 2. 内容分析
if (this.pipelineConfig.analysis.enabled) {
await this.executeStep('analysis', content, result, status)
}
// 3. 内容翻译
if (this.pipelineConfig.translation.enabled && this.pipelineConfig.translation.targetLanguages.length > 0) {
await this.executeStep('translation', content, result, status)
}
// 4. 内容分类
if (this.pipelineConfig.categorization.enabled) {
await this.executeStep('categorization', content, result, status)
}
// 5. 质量检查
await this.executeStep('quality_check', content, result, status)
// 6. 存储处理
await this.executeStep('storage', content, result, status)
// 7. 索引构建
await this.executeStep('indexing', content, result, status)
// 完成处理
result.status = 'completed'
result.processingTime = Date.now() - status.startTime
this.updateProcessingStatus(status, 'completed', 100)
} catch (error) {
result.status = 'failed'
result.errors.push(error.message || 'Unknown error')
this.updateProcessingStatus(status, 'failed', status.progress, error.message)
throw error
}
return result
}
private async executeStep(
stepName: string,
content: ContentInfo,
result: ProcessingResult,
status: ProcessingStatus
): Promise<void> {
const step = this.processingSteps.get(stepName)
if (!step) {
throw new Error(`Processing step '${stepName}' not found`)
}
try {
// 验证前置条件
if (step.validate && !step.validate(result)) {
throw new Error(`Validation failed for step '${stepName}'`)
}
// 执行步骤
const stepResult = await step.execute(result)
// 更新结果
if (stepResult) {
Object.assign(result, stepResult)
}
// 更新状态
status.completedSteps.push(stepName)
const progress = (status.completedSteps.length / 7) * 100 // 7个主要步骤
this.updateProcessingStatus(status, this.getStageFromStep(stepName), progress)
} catch (error) {
// 记录错误
status.errors.push({
step: stepName,
error: error.message || 'Unknown error',
timestamp: Date.now()
})
// 尝试回滚
if (step.rollback) {
try {
await step.rollback(result)
} catch (rollbackError) {
console.error(`Rollback failed for step '${stepName}':`, rollbackError)
}
}
throw error
}
}
private updateProcessingStatus(
status: ProcessingStatus,
stage: ProcessingStage,
progress: number,
error?: string
): void {
status.stage = stage
status.progress = progress
status.lastUpdateTime = Date.now()
if (error) {
status.errors.push({
step: stage,
error,
timestamp: Date.now()
})
}
}
private getStageFromStep(stepName: string): ProcessingStage {
const stageMap: Record<string, ProcessingStage> = {
'validation': 'validation',
'analysis': 'analysis',
'translation': 'translation',
'categorization': 'categorization',
'quality_check': 'quality_check',
'storage': 'storage',
'indexing': 'indexing'
}
return stageMap[stepName] || 'validation'
}
private initializeProcessingSteps(): void {
// 内容验证步骤
this.processingSteps.set('validation', {
name: 'validation',
order: 1,
execute: async (data: ProcessingResult) => {
const content = data.originalContent
// 验证必需字段
if (!content.title || content.title.trim().length === 0) {
throw new Error('Content title is required')
}
if (!content.content || content.content.trim().length < 50) {
throw new Error('Content is too short (minimum 50 characters)')
}
// 验证内容质量
if (content.quality !== undefined && content.quality < this.pipelineConfig.qualityThreshold) {
throw new Error(`Content quality (${content.quality}) below threshold (${this.pipelineConfig.qualityThreshold})`)
}
return data
},
validate: (data: ProcessingResult) => {
return data.originalContent && data.originalContent.title && data.originalContent.content
}
})
// 内容分析步骤
this.processingSteps.set('analysis', {
name: 'analysis',
order: 2,
execute: async (data: ProcessingResult) => {
const response = await this.analysisService.analyzeContent(data.originalContent.content, {
types: this.pipelineConfig.analysis.types as any,
includeScores: this.pipelineConfig.analysis.includeScores,
language: data.originalContent.originalLanguage
})
if (response.success && response.data) {
data.analysis = response.data
data.processedContent.sentiment = response.data.sentimentScore
data.processedContent.readability = response.data.readabilityScore
data.processedContent.credibility = response.data.credibilityScore
data.processedContent.keywords = response.data.keywords
data.totalCost += (response.costUSD || 0)
} else {
throw new Error(response.error || 'Content analysis failed')
}
return data
}
})
// 内容翻译步骤
this.processingSteps.set('translation', {
name: 'translation',
order: 3,
execute: async (data: ProcessingResult) => {
const sourceContent = data.originalContent
const targetLanguages = this.pipelineConfig.translation.targetLanguages
for (const targetLang of targetLanguages) {
if (targetLang === sourceContent.originalLanguage) continue
// 翻译标题
const titleResponse = await this.translationService.translateText(
sourceContent.title,
targetLang,
sourceContent.originalLanguage,
{ qualityThreshold: this.pipelineConfig.translation.qualityThreshold }
)
// 翻译内容
const contentResponse = await this.translationService.translateText(
sourceContent.content,
targetLang,
sourceContent.originalLanguage,
{ qualityThreshold: this.pipelineConfig.translation.qualityThreshold }
)
if (titleResponse.success && contentResponse.success && titleResponse.data && contentResponse.data) {
data.translations[targetLang] = {
...contentResponse.data,
translatedText: `${titleResponse.data.translatedText}\n\n${contentResponse.data.translatedText}`
}
data.totalCost += (titleResponse.costUSD || 0) + (contentResponse.costUSD || 0)
}
}
return data
}
})
// 内容分类步骤
this.processingSteps.set('categorization', {
name: 'categorization',
order: 4,
execute: async (data: ProcessingResult) => {
if (data.analysis && data.analysis.categories) {
const validCategories = data.analysis.categories
.filter(cat => cat.confidence >= this.pipelineConfig.categorization.threshold)
.slice(0, this.pipelineConfig.categorization.maxCategories)
.map(cat => cat.categoryId)
data.categories = validCategories
// 设置主分类
if (validCategories.length > 0) {
data.processedContent.categoryId = validCategories[0]
}
}
return data
}
})
// 质量检查步骤
this.processingSteps.set('quality_check', {
name: 'quality_check',
order: 5,
execute: async (data: ProcessingResult) => {
let qualityScore = 0
let factors = 0
// 基于分析结果的质量评估
if (data.analysis) {
if (data.analysis.readabilityScore !== undefined) {
qualityScore += data.analysis.readabilityScore
factors++
}
if (data.analysis.credibilityScore !== undefined) {
qualityScore += data.analysis.credibilityScore
factors++
}
// 毒性检查
if (data.analysis.toxicityScore !== undefined) {
qualityScore += (1 - data.analysis.toxicityScore) // 毒性越低质量越高
factors++
}
}
// 内容长度评估
const contentLength = data.originalContent.content.length
const lengthScore = contentLength > 500 ? 1 : contentLength / 500
qualityScore += lengthScore
factors++
// 翻译质量评估
if (Object.keys(data.translations).length > 0) {
const translationQualities = Object.values(data.translations).map(t => t.qualityScore)
const avgTranslationQuality = translationQualities.reduce((sum, q) => sum + q, 0) / translationQualities.length
qualityScore += avgTranslationQuality
factors++
}
data.qualityScore = factors > 0 ? qualityScore / factors : 0.5
data.processedContent.quality = data.qualityScore
// 质量阈值检查
if (data.qualityScore < this.pipelineConfig.qualityThreshold) {
console.warn(`Content quality (${data.qualityScore}) below threshold (${this.pipelineConfig.qualityThreshold})`)
}
return data
}
})
// 存储步骤
this.processingSteps.set('storage', {
name: 'storage',
order: 6,
execute: async (data: ProcessingResult) => {
// 模拟存储操作
await this.delay(100)
// 在实际实现中,这里会将处理后的内容保存到数据库
data.processedContent.status = 'published'
data.processedContent.tags = [...(data.processedContent.tags || []), ...data.categories]
return data
}
})
// 索引构建步骤
this.processingSteps.set('indexing', {
name: 'indexing',
order: 7,
execute: async (data: ProcessingResult) => {
// 模拟索引构建
await this.delay(50)
// 在实际实现中,这里会更新搜索索引
console.log(`Content indexed: ${data.contentId}`)
return data
}
})
}
private createDefaultPipelineConfig(overrides: Partial<PipelineConfig>): PipelineConfig {
return {
enabledSteps: ['validation', 'analysis', 'translation', 'categorization', 'quality_check', 'storage', 'indexing'],
parallelProcessing: true,
maxConcurrency: 3,
retryCount: 2,
timeoutMs: 300000, // 5分钟
qualityThreshold: 0.7,
targetLanguages: ['zh-CN', 'en'],
categorization: {
enabled: true,
threshold: 0.6,
maxCategories: 3
},
translation: {
enabled: true,
targetLanguages: ['zh-CN', 'en'],
qualityThreshold: 0.7
},
analysis: {
enabled: true,
types: ['sentiment', 'entities', 'topics', 'categories', 'readability', 'credibility', 'summary', 'keywords'],
includeScores: true
},
...overrides
}
}
private initializeStats(): PipelineStats {
const stages: ProcessingStage[] = [
'fetching', 'validation', 'analysis', 'translation',
'categorization', 'quality_check', 'storage', 'indexing',
'completed', 'failed'
]
const stageStats: Record<ProcessingStage, any> = {} as Record<ProcessingStage, any>
stages.forEach(stage => {
stageStats[stage] = {
count: 0,
avgTime: 0,
errorRate: 0
}
})
return {
totalProcessed: 0,
successCount: 0,
errorCount: 0,
avgProcessingTime: 0,
totalCost: 0,
stageStats,
dailyThroughput: 0,
lastProcessedAt: 0
}
}
private updateStats(result: ProcessingResult): void {
this.stats.totalProcessed++
this.stats.lastProcessedAt = Date.now()
if (result.status === 'completed') {
this.stats.successCount++
} else {
this.stats.errorCount++
}
// 更新平均处理时间
this.stats.avgProcessingTime = (
this.stats.avgProcessingTime * (this.stats.totalProcessed - 1) + result.processingTime
) / this.stats.totalProcessed
// 更新总成本
this.stats.totalCost += result.totalCost
// 更新阶段统计
this.stats.stageStats[result.status].count++
}
private createBatches<T>(items: T[], batchSize: number): T[][] {
const batches: T[][] = []
for (let i = 0; i < items.length; i += batchSize) {
batches.push(items.slice(i, i + batchSize))
}
return batches
}
private async delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
private isRetryableError(error: any): boolean {
const retryableCodes = ['TIMEOUT', 'RATE_LIMIT', 'SERVER_ERROR', 'NETWORK_ERROR']
return retryableCodes.includes(error.code) || error.status >= 500
}
}

View File

@@ -0,0 +1,563 @@
// AI内容分析服务
// filepath: h:\blews\akmon\uni_modules\ak-ai-news\services\ai-content-analysis-service.uts
import { AkReq } from '@/uni_modules/ak-req/index.uts'
import type {
ContentAnalysisResult,
EntityResult,
TopicResult,
CategoryResult,
AIProvider,
AIResponse,
AIServiceConfig,
ContentInfo
} from '../types/ai-types.uts'
export class AIContentAnalysisService {
private config: AIServiceConfig
private req: AkReq
private cache: Map<string, ContentAnalysisResult> = new Map()
constructor(config: AIServiceConfig) {
this.config = config
this.req = new AkReq()
}
/**
* 综合内容分析
*/
async analyzeContent(
content: ContentInfo,
options?: {
includeEntities?: boolean
includeTopics?: boolean
includeSentiment?: boolean
includeReadability?: boolean
includeCredibility?: boolean
language?: string
}
): Promise<AIResponse<ContentAnalysisResult>> {
try {
const startTime = Date.now()
// 检查缓存
const cacheKey = this.generateContentCacheKey(content.id, options)
const cached = this.cache.get(cacheKey)
if (cached) {
return {
success: true,
data: cached,
processingTimeMs: 0
}
}
const analysisPromises: Promise<any>[] = []
// 情感分析
if (options?.includeSentiment !== false) {
analysisPromises.push(this.analyzeSentiment(content.content, content.title))
}
// 实体识别
if (options?.includeEntities !== false) {
analysisPromises.push(this.extractEntities(content.content))
}
// 主题提取
if (options?.includeTopics !== false) {
analysisPromises.push(this.extractTopics(content.content))
}
// 可读性分析
if (options?.includeReadability !== false) {
analysisPromises.push(this.analyzeReadability(content.content))
}
// 可信度分析
if (options?.includeCredibility !== false) {
analysisPromises.push(this.analyzeCredibility(content))
}
// 并行执行所有分析
const results = await Promise.all(analysisPromises)
// 生成摘要
const summary = await this.generateSummary(content.content)
// 提取关键词
const keywords = await this.extractKeywords(content.content)
// 分类内容
const categories = await this.classifyContent(content)
const analysisResult: ContentAnalysisResult = {
contentId: content.id,
sentimentScore: results[0]?.score || 0,
sentimentLabel: results[0]?.label || 'neutral',
readabilityScore: results[3] || 0.5,
credibilityScore: results[4] || 0.5,
toxicityScore: 0, // 可以添加毒性检测
keywords: keywords || [],
entities: results[1] || [],
topics: results[2] || [],
categories: categories || [],
summary: summary || '',
keyPhrases: this.extractKeyPhrases(content.content),
language: options?.language || content.originalLanguage,
processingTimeMs: Date.now() - startTime,
provider: 'openai'
}
// 缓存结果
this.cache.set(cacheKey, analysisResult)
return {
success: true,
data: analysisResult,
processingTimeMs: analysisResult.processingTimeMs
}
} catch (error) {
console.error('内容分析失败:', error)
return {
success: false,
error: error instanceof Error ? error.message : '内容分析服务异常',
errorCode: 'CONTENT_ANALYSIS_FAILED'
}
}
}
/**
* 情感分析
*/
private async analyzeSentiment(content: string, title?: string): Promise<{score: number, label: string}> {
try {
const openaiConfig = this.config.openai
if (!openaiConfig) {
throw new Error('OpenAI配置未找到')
}
const text = title ? `${title}\n\n${content}` : content
const prompt = `请分析以下文本的情感倾向,返回一个-1到1之间的数值-1表示非常负面0表示中性1表示非常正面和对应的标签positive/negative/neutral
文本:${text.substring(0, 2000)}
请以JSON格式返回{"score": 数值, "label": "标签"}`
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的文本情感分析助手。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 100,
temperature: 0.1
}
})
if (!response.success || !response.data?.choices?.[0]) {
throw new Error('情感分析API调用失败')
}
const result = JSON.parse(response.data.choices[0].message.content)
return {
score: Math.max(-1, Math.min(1, parseFloat(result.score) || 0)),
label: result.label || 'neutral'
}
} catch (error) {
console.error('情感分析失败:', error)
return { score: 0, label: 'neutral' }
}
}
/**
* 实体识别
*/
private async extractEntities(content: string): Promise<EntityResult[]> {
try {
const openaiConfig = this.config.openai
if (!openaiConfig) {
return []
}
const prompt = `请从以下文本中识别出人名、地名、机构名、日期、金额等实体。
文本:${content.substring(0, 2000)}
请以JSON数组格式返回每个实体包含text(实体文本)、type(类型person/location/organization/date/money/other)、confidence(置信度0-1)。`
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的命名实体识别助手。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 500,
temperature: 0.1
}
})
if (!response.success || !response.data?.choices?.[0]) {
return []
}
const entities = JSON.parse(response.data.choices[0].message.content)
return entities.map((entity: any, index: number) => ({
text: entity.text || '',
type: entity.type || 'other',
confidence: entity.confidence || 0.8,
startPosition: 0, // 简化处理
endPosition: entity.text?.length || 0
})) as EntityResult[]
} catch (error) {
console.error('实体识别失败:', error)
return []
}
}
/**
* 主题提取
*/
private async extractTopics(content: string): Promise<TopicResult[]> {
try {
const openaiConfig = this.config.openai
if (!openaiConfig) {
return []
}
const prompt = `请分析以下文本的主要主题提取3-5个核心主题。
文本:${content.substring(0, 2000)}
请以JSON数组格式返回每个主题包含name(主题名称)、confidence(置信度0-1)、keywords(相关关键词数组)。`
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的文本主题分析助手。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 400,
temperature: 0.2
}
})
if (!response.success || !response.data?.choices?.[0]) {
return []
}
const topics = JSON.parse(response.data.choices[0].message.content)
return topics.map((topic: any) => ({
name: topic.name || '',
confidence: topic.confidence || 0.8,
keywords: topic.keywords || []
})) as TopicResult[]
} catch (error) {
console.error('主题提取失败:', error)
return []
}
}
/**
* 可读性分析
*/
private async analyzeReadability(content: string): Promise<number> {
try {
// 简化的可读性计算
const sentences = content.split(/[.!?。!?]/).length
const words = content.split(/\s+/).length
const avgWordsPerSentence = words / sentences
// 基于平均句长计算可读性分数
let score = 1.0
if (avgWordsPerSentence > 30) score = 0.3
else if (avgWordsPerSentence > 20) score = 0.5
else if (avgWordsPerSentence > 15) score = 0.7
else if (avgWordsPerSentence > 10) score = 0.9
return score
} catch (error) {
console.error('可读性分析失败:', error)
return 0.5
}
}
/**
* 可信度分析
*/
private async analyzeCredibility(content: ContentInfo): Promise<number> {
try {
let score = 0.5 // 基础分数
// 来源可信度
if (content.sourceUrl) {
const domain = this.extractDomain(content.sourceUrl)
const credibleDomains = ['reuters.com', 'bbc.com', 'xinhuanet.com', 'nhk.or.jp']
if (credibleDomains.some(d => domain.includes(d))) {
score += 0.2
}
}
// 作者信息
if (content.author && content.author.length > 0) {
score += 0.1
}
// 内容长度和结构
if (content.content.length > 500) {
score += 0.1
}
// 时效性
const daysSincePublished = (Date.now() - content.publishedAt) / (1000 * 60 * 60 * 24)
if (daysSincePublished < 1) {
score += 0.1
}
return Math.min(1.0, score)
} catch (error) {
console.error('可信度分析失败:', error)
return 0.5
}
}
/**
* 生成摘要
*/
private async generateSummary(content: string): Promise<string> {
try {
const openaiConfig = this.config.openai
if (!openaiConfig) {
return ''
}
if (content.length < 200) {
return content
}
const prompt = `请为以下文本生成一个简洁的摘要100字以内\n\n${content.substring(0, 2000)}`
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的文本摘要助手。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 200,
temperature: 0.3
}
})
if (!response.success || !response.data?.choices?.[0]) {
return ''
}
return response.data.choices[0].message.content.trim()
} catch (error) {
console.error('摘要生成失败:', error)
return ''
}
}
/**
* 提取关键词
*/
private async extractKeywords(content: string): Promise<string[]> {
try {
const openaiConfig = this.config.openai
if (!openaiConfig) {
return []
}
const prompt = `请从以下文本中提取5-10个关键词\n\n${content.substring(0, 1500)}\n\n请以JSON数组格式返回关键词。`
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的关键词提取助手。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: 200,
temperature: 0.1
}
})
if (!response.success || !response.data?.choices?.[0]) {
return []
}
const keywords = JSON.parse(response.data.choices[0].message.content)
return Array.isArray(keywords) ? keywords : []
} catch (error) {
console.error('关键词提取失败:', error)
return []
}
}
/**
* 内容分类
*/
private async classifyContent(content: ContentInfo): Promise<CategoryResult[]> {
try {
// 预定义分类
const categories = [
{ id: 'politics', name: '政治', keywords: ['政治', '政府', '选举', '政策', 'politics', 'government'] },
{ id: 'technology', name: '科技', keywords: ['科技', '技术', '人工智能', 'AI', 'technology', 'tech'] },
{ id: 'business', name: '商业', keywords: ['商业', '经济', '金融', '市场', 'business', 'economy'] },
{ id: 'sports', name: '体育', keywords: ['体育', '运动', '比赛', '足球', 'sports', 'game'] },
{ id: 'entertainment', name: '娱乐', keywords: ['娱乐', '电影', '音乐', '明星', 'entertainment', 'movie'] },
{ id: 'health', name: '健康', keywords: ['健康', '医疗', '疾病', '医院', 'health', 'medical'] }
]
const text = `${content.title} ${content.content}`.toLowerCase()
const results: CategoryResult[] = []
for (const category of categories) {
let score = 0
for (const keyword of category.keywords) {
const matches = (text.match(new RegExp(keyword.toLowerCase(), 'g')) || []).length
score += matches
}
if (score > 0) {
results.push({
categoryId: category.id,
categoryName: category.name,
confidence: Math.min(1.0, score / 10),
level: 1
})
}
}
return results.sort((a, b) => b.confidence - a.confidence).slice(0, 3)
} catch (error) {
console.error('内容分类失败:', error)
return []
}
}
/**
* 提取关键短语
*/
private extractKeyPhrases(content: string): string[] {
try {
// 简单的关键短语提取
const sentences = content.split(/[.!?。!?]/)
const phrases: string[] = []
for (const sentence of sentences) {
const words = sentence.trim().split(/\s+/)
if (words.length >= 2 && words.length <= 5) {
phrases.push(sentence.trim())
}
}
return phrases.slice(0, 10)
} catch (error) {
console.error('关键短语提取失败:', error)
return []
}
}
/**
* 提取域名
*/
private extractDomain(url: string): string {
try {
const matches = url.match(/https?:\/\/([^\/]+)/)
return matches ? matches[1] : ''
} catch (error) {
return ''
}
}
/**
* 生成缓存键
*/
private generateContentCacheKey(contentId: string, options?: any): string {
const optionsStr = JSON.stringify(options || {})
return `content-${contentId}-${this.simpleHash(optionsStr)}`
}
/**
* 简单哈希函数
*/
private simpleHash(str: string): string {
let hash = 0
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = ((hash << 5) - hash) + char
hash = hash & hash
}
return Math.abs(hash).toString(36)
}
}

View File

@@ -0,0 +1,562 @@
// AI翻译服务
// filepath: h:\blews\akmon\uni_modules\ak-ai-news\services\ai-translation-service.uts
import { AkReq } from '@/uni_modules/ak-req/index.uts'
import type {
TranslationResult,
TranslationOptions,
AIProvider,
AIResponse,
AIServiceConfig
} from '../types/ai-types.uts'
export class AITranslationService {
private config: AIServiceConfig
private req: AkReq
private cache: Map<string, TranslationResult> = new Map()
constructor(config: AIServiceConfig) {
this.config = config
this.req = new AkReq()
}
/**
* 翻译文本 - 智能选择最佳AI服务
*/
async translateText(
text: string,
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): Promise<AIResponse<TranslationResult>> {
try {
// 检查缓存
const cacheKey = this.generateCacheKey(text, targetLang, sourceLang)
const cached = this.cache.get(cacheKey)
if (cached && this.isCacheValid(cached)) {
return {
success: true,
data: cached,
processingTimeMs: 0,
costUSD: 0
}
}
// 智能选择提供商
const provider = this.selectOptimalProvider(text, targetLang, options)
let result: TranslationResult
const startTime = Date.now()
switch (provider) {
case 'openai':
result = await this.translateWithOpenAI(text, targetLang, sourceLang, options)
break
case 'google':
result = await this.translateWithGoogle(text, targetLang, sourceLang, options)
break
case 'baidu':
result = await this.translateWithBaidu(text, targetLang, sourceLang, options)
break
default:
throw new Error(`不支持的AI提供商: ${provider}`)
}
result.processingTimeMs = Date.now() - startTime
// 质量检查
if (result.qualityScore < (options?.qualityThreshold ?? 0.7)) {
// 尝试使用备用提供商
const fallbackProvider = this.getFallbackProvider(provider)
if (fallbackProvider) {
const fallbackResult = await this.translateWithProvider(
text, targetLang, sourceLang, fallbackProvider, options
)
if (fallbackResult.qualityScore > result.qualityScore) {
result = fallbackResult
}
}
}
// 缓存结果
this.cache.set(cacheKey, result)
// 记录使用统计
await this.recordUsage(result)
return {
success: true,
data: result,
processingTimeMs: result.processingTimeMs,
costUSD: result.costUSD,
provider: result.provider
}
} catch (error) {
console.error('翻译失败:', error)
return {
success: false,
error: error instanceof Error ? error.message : '翻译服务异常',
errorCode: 'TRANSLATION_FAILED'
}
}
}
/**
* 批量翻译
*/
async batchTranslate(
texts: string[],
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): Promise<AIResponse<TranslationResult[]>> {
try {
const results: TranslationResult[] = []
const batchSize = 10 // 批处理大小
for (let i = 0; i < texts.length; i += batchSize) {
const batch = texts.slice(i, i + batchSize)
const batchPromises = batch.map(text =>
this.translateText(text, targetLang, sourceLang, options)
)
const batchResults = await Promise.all(batchPromises)
for (const result of batchResults) {
if (result.success && result.data) {
results.push(result.data)
}
}
// 避免API限流
if (i + batchSize < texts.length) {
await this.delay(100)
}
}
return {
success: true,
data: results,
processingTimeMs: results.reduce((sum, r) => sum + r.processingTimeMs, 0),
costUSD: results.reduce((sum, r) => sum + r.costUSD, 0)
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : '批量翻译失败',
errorCode: 'BATCH_TRANSLATION_FAILED'
}
}
}
/**
* OpenAI翻译实现
*/
private async translateWithOpenAI(
text: string,
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): Promise<TranslationResult> {
const openaiConfig = this.config.openai
if (!openaiConfig) {
throw new Error('OpenAI配置未找到')
}
const prompt = this.buildOpenAIPrompt(text, targetLang, sourceLang, options)
const response = await this.req.post<any>({
url: `${openaiConfig.baseURL || 'https://api.openai.com'}/v1/chat/completions`,
headers: {
'Authorization': `Bearer ${openaiConfig.apiKey}`,
'Content-Type': 'application/json'
},
data: {
model: options?.model || openaiConfig.model,
messages: [
{
role: 'system',
content: '你是一个专业的翻译助手,能够提供高质量的多语言翻译服务。'
},
{
role: 'user',
content: prompt
}
],
max_tokens: options?.maxTokens || openaiConfig.maxTokens,
temperature: options?.temperature || openaiConfig.temperature
}
})
if (!response.success || !response.data) {
throw new Error('OpenAI API调用失败')
}
const choice = response.data.choices?.[0]
if (!choice) {
throw new Error('OpenAI响应格式错误')
}
return this.parseOpenAIResponse(
choice.message.content,
text,
targetLang,
sourceLang || 'auto',
response.data.usage
)
}
/**
* Google翻译实现
*/
private async translateWithGoogle(
text: string,
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): Promise<TranslationResult> {
const googleConfig = this.config.google
if (!googleConfig) {
throw new Error('Google翻译配置未找到')
}
const startTime = Date.now()
const response = await this.req.post<any>({
url: 'https://translation.googleapis.com/language/translate/v2',
headers: {
'Content-Type': 'application/json'
},
data: {
key: googleConfig.apiKey,
q: text,
target: this.convertLanguageCode(targetLang, 'google'),
source: sourceLang ? this.convertLanguageCode(sourceLang, 'google') : undefined,
format: 'text'
}
})
if (!response.success || !response.data) {
throw new Error('Google翻译API调用失败')
}
const translation = response.data.data?.translations?.[0]
if (!translation) {
throw new Error('Google翻译响应格式错误')
}
return {
translatedText: translation.translatedText,
originalText: text,
sourceLang: translation.detectedSourceLanguage || sourceLang || 'auto',
targetLang,
confidence: 0.9, // Google翻译通常质量较高
qualityScore: 0.85,
provider: 'google',
tokensUsed: Math.ceil(text.length / 4), // 估算token数
processingTimeMs: Date.now() - startTime,
costUSD: this.calculateGoogleCost(text.length)
}
}
/**
* 百度翻译实现
*/
private async translateWithBaidu(
text: string,
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): Promise<TranslationResult> {
const baiduConfig = this.config.baidu
if (!baiduConfig) {
throw new Error('百度翻译配置未找到')
}
const startTime = Date.now()
const salt = Date.now().toString()
const sign = this.generateBaiduSign(text, salt, baiduConfig.apiKey, baiduConfig.secretKey)
const response = await this.req.post<any>({
url: 'https://fanyi-api.baidu.com/api/trans/vip/translate',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
q: text,
from: sourceLang ? this.convertLanguageCode(sourceLang, 'baidu') : 'auto',
to: this.convertLanguageCode(targetLang, 'baidu'),
appid: baiduConfig.apiKey,
salt: salt,
sign: sign
}
})
if (!response.success || !response.data) {
throw new Error('百度翻译API调用失败')
}
const result = response.data.trans_result?.[0]
if (!result) {
throw new Error('百度翻译响应格式错误')
}
return {
translatedText: result.dst,
originalText: text,
sourceLang: response.data.from || sourceLang || 'auto',
targetLang,
confidence: 0.85,
qualityScore: 0.8,
provider: 'baidu',
tokensUsed: Math.ceil(text.length / 4),
processingTimeMs: Date.now() - startTime,
costUSD: this.calculateBaiduCost(text.length)
}
}
/**
* 选择最优提供商
*/
private selectOptimalProvider(
text: string,
targetLang: string,
options?: TranslationOptions
): AIProvider {
if (options?.provider) {
return options.provider
}
// 根据文本长度和语言选择最佳提供商
const textLength = text.length
const isChineseTarget = targetLang.startsWith('zh')
const isChineseSource = /[\u4e00-\u9fff]/.test(text)
// 中文相关翻译优先使用百度
if (isChineseTarget || isChineseSource) {
return 'baidu'
}
// 长文本使用OpenAI更好的上下文理解
if (textLength > 1000) {
return 'openai'
}
// 短文本使用Google速度快成本低
return 'google'
}
/**
* 获取备用提供商
*/
private getFallbackProvider(primary: AIProvider): AIProvider | null {
switch (primary) {
case 'openai':
return 'google'
case 'google':
return 'baidu'
case 'baidu':
return 'google'
default:
return null
}
}
/**
* 生成缓存键
*/
private generateCacheKey(text: string, targetLang: string, sourceLang?: string): string {
const source = sourceLang || 'auto'
const hash = this.simpleHash(text)
return `${source}-${targetLang}-${hash}`
}
/**
* 检查缓存是否有效
*/
private isCacheValid(result: TranslationResult): boolean {
// 简单的缓存有效性检查,可以根据需要扩展
return result.qualityScore > 0.7
}
/**
* 构建OpenAI提示词
*/
private buildOpenAIPrompt(
text: string,
targetLang: string,
sourceLang?: string,
options?: TranslationOptions
): string {
let prompt = `请将以下文本翻译成${this.getLanguageName(targetLang)}\n\n${text}\n\n`
if (options?.culturalAdaptation) {
prompt += '请考虑文化差异,进行适当的本地化调整。\n'
}
if (options?.preserveFormatting) {
prompt += '请保持原文的格式和结构。\n'
}
prompt += '只返回翻译结果,不要包含其他解释。'
return prompt
}
/**
* 解析OpenAI响应
*/
private parseOpenAIResponse(
content: string,
originalText: string,
targetLang: string,
sourceLang: string,
usage: any
): TranslationResult {
return {
translatedText: content.trim(),
originalText,
sourceLang,
targetLang,
confidence: 0.9,
qualityScore: 0.9,
provider: 'openai',
tokensUsed: usage?.total_tokens || 0,
processingTimeMs: 0,
costUSD: this.calculateOpenAICost(usage?.total_tokens || 0)
}
}
/**
* 转换语言代码
*/
private convertLanguageCode(code: string, provider: AIProvider): string {
const codeMap: Record<AIProvider, Record<string, string>> = {
openai: {
'zh-CN': 'Chinese',
'en-US': 'English',
'ja-JP': 'Japanese',
'ko-KR': 'Korean'
},
google: {
'zh-CN': 'zh',
'en-US': 'en',
'ja-JP': 'ja',
'ko-KR': 'ko'
},
baidu: {
'zh-CN': 'zh',
'en-US': 'en',
'ja-JP': 'jp',
'ko-KR': 'kor'
},
custom: {}
}
return codeMap[provider]?.[code] || code
}
/**
* 获取语言名称
*/
private getLanguageName(code: string): string {
const nameMap: Record<string, string> = {
'zh-CN': '中文',
'en-US': 'English',
'ja-JP': '日语',
'ko-KR': '韩语',
'es-ES': 'Spanish',
'fr-FR': 'French',
'de-DE': 'German'
}
return nameMap[code] || code
}
/**
* 计算成本
*/
private calculateOpenAICost(tokens: number): number {
// GPT-4 pricing: $0.03 per 1K tokens (input + output)
return (tokens / 1000) * 0.03
}
private calculateGoogleCost(textLength: number): number {
// Google Translate: $20 per 1M characters
return (textLength / 1000000) * 20
}
private calculateBaiduCost(textLength: number): number {
// 百度翻译:较低成本,假设$5 per 1M characters
return (textLength / 1000000) * 5
}
/**
* 生成百度签名
*/
private generateBaiduSign(text: string, salt: string, appid: string, key: string): string {
// 简化的签名生成实际应用中需要使用MD5
const str = appid + text + salt + key
return this.simpleHash(str)
}
/**
* 简单哈希函数
*/
private simpleHash(str: string): string {
let hash = 0
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = ((hash << 5) - hash) + char
hash = hash & hash // Convert to 32-bit integer
}
return Math.abs(hash).toString(36)
}
/**
* 延迟函数
*/
private delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* 记录使用统计
*/
private async recordUsage(result: TranslationResult): Promise<void> {
try {
// 这里可以将使用统计发送到数据库
console.log('翻译使用统计:', {
provider: result.provider,
tokensUsed: result.tokensUsed,
costUSD: result.costUSD,
processingTimeMs: result.processingTimeMs
})
} catch (error) {
console.error('记录使用统计失败:', error)
}
}
/**
* 根据提供商翻译
*/
private async translateWithProvider(
text: string,
targetLang: string,
sourceLang: string | undefined,
provider: AIProvider,
options?: TranslationOptions
): Promise<TranslationResult> {
switch (provider) {
case 'openai':
return await this.translateWithOpenAI(text, targetLang, sourceLang, options)
case 'google':
return await this.translateWithGoogle(text, targetLang, sourceLang, options)
case 'baidu':
return await this.translateWithBaidu(text, targetLang, sourceLang, options)
default:
throw new Error(`不支持的提供商: ${provider}`)
}
}
}

View File

@@ -0,0 +1,855 @@
// Comprehensive Test Runner for AI News System
// Combines unit tests, integration tests, performance monitoring, and error handling validation
import { runSimpleTests } from './simple-test.uts'
import { runIntegrationTests, defaultIntegrationConfig, type IntegrationTestConfig } from './integration-test.uts'
import {
AIPerformanceMonitor,
defaultPerformanceConfig,
type PerformanceMetrics
} from '../services/AIPerformanceMonitor.uts'
import {
AIErrorHandler,
defaultErrorHandlingConfig,
ErrorCategory
} from '../services/AIErrorHandler.uts'
import { AIServiceManager, type AIServiceConfig } from '../index.uts'
/**
* Comprehensive test suite configuration
*/
export type TestSuiteConfig = {
runUnitTests: boolean
runIntegrationTests: boolean
runPerformanceTests: boolean
runErrorHandlingTests: boolean
enableRealAPIs: boolean
testTimeout: number
maxCostLimit: number
generateReport: boolean
outputFormat: 'console' | 'json' | 'html'
apiKeys?: {
openai?: string
google?: string
baidu?: {
appId: string
secretKey: string
}
}
}
/**
* Test results summary
*/
export type TestSummary = {
testSuite: string
startTime: number
endTime: number
totalDuration: number
results: {
unitTests?: { passed: boolean; details: any }
integrationTests?: { passed: boolean; details: any }
performanceTests?: { passed: boolean; details: any }
errorHandlingTests?: { passed: boolean; details: any }
}
overallResult: {
passed: boolean
successRate: number
totalTests: number
passedTests: number
failedTests: number
}
metrics: {
totalCost: number
averageLatency: number
throughput: number
errorRate: number
}
recommendations: string[]
}
/**
* Comprehensive test runner
*/
export class AINewsTestRunner {
private config: TestSuiteConfig
private performanceMonitor: AIPerformanceMonitor
private errorHandler: AIErrorHandler
private testResults: TestSummary
constructor(config: TestSuiteConfig) {
this.config = config
this.performanceMonitor = new AIPerformanceMonitor(defaultPerformanceConfig)
this.errorHandler = new AIErrorHandler(defaultErrorHandlingConfig)
this.testResults = this.initializeTestResults()
}
/**
* Run complete test suite
*/
async runCompleteTestSuite(): Promise<TestSummary> {
console.log('🚀 Starting Comprehensive AI News System Test Suite')
console.log('===================================================')
const startTime = Date.now()
this.testResults.startTime = startTime
try {
// Start monitoring
this.performanceMonitor.startMonitoring()
// Run tests in sequence
if (this.config.runUnitTests) {
console.log('\n📋 Phase 1: Unit Tests')
console.log('======================')
this.testResults.results.unitTests = await this.runUnitTestsPhase()
}
if (this.config.runIntegrationTests) {
console.log('\n🔗 Phase 2: Integration Tests')
console.log('==============================')
this.testResults.results.integrationTests = await this.runIntegrationTestsPhase()
}
if (this.config.runPerformanceTests) {
console.log('\n⚡ Phase 3: Performance Tests')
console.log('=============================')
this.testResults.results.performanceTests = await this.runPerformanceTestsPhase()
}
if (this.config.runErrorHandlingTests) {
console.log('\n🛡 Phase 4: Error Handling Tests')
console.log('=================================')
this.testResults.results.errorHandlingTests = await this.runErrorHandlingTestsPhase()
}
// Calculate final results
const endTime = Date.now()
this.testResults.endTime = endTime
this.testResults.totalDuration = endTime - startTime
this.calculateOverallResults()
this.generateRecommendations()
// Generate report
if (this.config.generateReport) {
await this.generateTestReport()
}
this.printSummary()
} catch (error) {
console.error('💥 Test suite execution failed:', error)
this.testResults.overallResult.passed = false
} finally {
// Cleanup
this.performanceMonitor.stopMonitoring()
}
return this.testResults
}
/**
* Run unit tests phase
*/
private async runUnitTestsPhase(): Promise<{ passed: boolean; details: any }> {
try {
const startTime = Date.now()
const result = await runSimpleTests()
const duration = Date.now() - startTime
return {
passed: result,
details: {
duration,
testType: 'unit',
coverage: 'basic functionality'
}
}
} catch (error) {
return {
passed: false,
details: {
error: String(error),
testType: 'unit'
}
}
}
}
/**
* Run integration tests phase
*/
private async runIntegrationTestsPhase(): Promise<{ passed: boolean; details: any }> {
try {
const integrationConfig: IntegrationTestConfig = {
...defaultIntegrationConfig,
enableRealAPIs: this.config.enableRealAPIs,
apiKeys: this.config.apiKeys || {},
testTimeout: this.config.testTimeout,
costLimits: {
maxCostPerTest: this.config.maxCostLimit,
dailyLimit: this.config.maxCostLimit * 10
}
}
const result = await runIntegrationTests(integrationConfig)
return {
passed: result,
details: {
testType: 'integration',
realAPIs: this.config.enableRealAPIs,
coverage: 'end-to-end workflows'
}
}
} catch (error) {
return {
passed: false,
details: {
error: String(error),
testType: 'integration'
}
}
}
}
/**
* Run performance tests phase
*/
private async runPerformanceTestsPhase(): Promise<{ passed: boolean; details: any }> {
try {
console.log(' 🔍 Testing system performance under load...')
// Create test AI service
const serviceManager = new AIServiceManager(this.createTestConfig())
await serviceManager.initialize()
const performanceResults = {
latencyTests: await this.testLatencyBenchmarks(serviceManager),
throughputTests: await this.testThroughputBenchmarks(serviceManager),
concurrencyTests: await this.testConcurrencyBenchmarks(serviceManager),
memoryTests: await this.testMemoryUsage(serviceManager)
}
await serviceManager.shutdown()
// Analyze results
const passed = this.analyzePerformanceResults(performanceResults)
return {
passed,
details: {
testType: 'performance',
...performanceResults
}
}
} catch (error) {
return {
passed: false,
details: {
error: String(error),
testType: 'performance'
}
}
}
}
/**
* Run error handling tests phase
*/
private async runErrorHandlingTestsPhase(): Promise<{ passed: boolean; details: any }> {
try {
console.log(' 🛡️ Testing error handling and recovery mechanisms...')
const errorTests = {
retryLogic: await this.testRetryMechanisms(),
circuitBreaker: await this.testCircuitBreaker(),
fallbackProviders: await this.testFallbackProviders(),
errorClassification: await this.testErrorClassification()
}
const passed = Object.values(errorTests).every(test => test.passed)
return {
passed,
details: {
testType: 'error_handling',
...errorTests
}
}
} catch (error) {
return {
passed: false,
details: {
error: String(error),
testType: 'error_handling'
}
}
}
}
/**
* Test latency benchmarks
*/
private async testLatencyBenchmarks(serviceManager: AIServiceManager): Promise<any> {
console.log(' 📊 Testing latency benchmarks...')
const results = {
translation: { samples: [], average: 0, p95: 0 },
analysis: { samples: [], average: 0, p95: 0 },
chat: { samples: [], average: 0, p95: 0 }
}
// Translation latency test
const translationService = serviceManager.getTranslationService()
for (let i = 0; i < 10; i++) {
const start = Date.now()
await translationService.translateText('Hello world', 'zh-CN', 'en')
const latency = Date.now() - start
results.translation.samples.push(latency)
}
// Analysis latency test
const analysisService = serviceManager.getAnalysisService()
for (let i = 0; i < 10; i++) {
const start = Date.now()
await analysisService.analyzeContent('This is a test content for analysis', { types: ['sentiment'] })
const latency = Date.now() - start
results.analysis.samples.push(latency)
}
// Chat latency test
const chatService = serviceManager.getChatService()
const session = await chatService.createChatSession('test-user', 'en')
if (session.success && session.data) {
for (let i = 0; i < 5; i++) {
const start = Date.now()
await chatService.sendMessage(session.data.id, 'Hello, how are you?')
const latency = Date.now() - start
results.chat.samples.push(latency)
}
}
// Calculate statistics
Object.keys(results).forEach(key => {
const samples = results[key].samples.sort((a, b) => a - b)
results[key].average = samples.reduce((sum, val) => sum + val, 0) / samples.length
results[key].p95 = samples[Math.floor(samples.length * 0.95)]
})
return results
}
/**
* Test throughput benchmarks
*/
private async testThroughputBenchmarks(serviceManager: AIServiceManager): Promise<any> {
console.log(' 🚀 Testing throughput benchmarks...')
const testDuration = 30000 // 30 seconds
const startTime = Date.now()
let requestCount = 0
let successCount = 0
const translationService = serviceManager.getTranslationService()
// Run concurrent requests for the test duration
const promises: Promise<void>[] = []
while (Date.now() - startTime < testDuration) {
const promise = translationService.translateText('Test content', 'zh-CN', 'en')
.then(result => {
requestCount++
if (result.success) successCount++
})
.catch(() => {
requestCount++
})
promises.push(promise)
// Control concurrency
if (promises.length >= 10) {
await Promise.race(promises)
promises.splice(0, 1)
}
}
await Promise.all(promises)
const actualDuration = Date.now() - startTime
const throughput = requestCount / (actualDuration / 1000)
const successRate = successCount / requestCount
return {
requestCount,
successCount,
throughput: Math.round(throughput * 100) / 100,
successRate: Math.round(successRate * 10000) / 100,
duration: actualDuration
}
}
/**
* Test concurrency handling
*/
private async testConcurrencyBenchmarks(serviceManager: AIServiceManager): Promise<any> {
console.log(' ⚡ Testing concurrency handling...')
const concurrencyLevels = [1, 5, 10, 20]
const results: Record<number, any> = {}
for (const concurrency of concurrencyLevels) {
const startTime = Date.now()
const promises: Promise<any>[] = []
for (let i = 0; i < concurrency; i++) {
promises.push(
serviceManager.getTranslationService().translateText(
`Concurrent test ${i}`,
'zh-CN',
'en'
)
)
}
const responses = await Promise.allSettled(promises)
const successful = responses.filter(r => r.status === 'fulfilled').length
const duration = Date.now() - startTime
results[concurrency] = {
successful,
failed: concurrency - successful,
successRate: successful / concurrency,
duration,
avgLatency: duration / concurrency
}
}
return results
}
/**
* Test memory usage
*/
private async testMemoryUsage(serviceManager: AIServiceManager): Promise<any> {
console.log(' 💾 Testing memory usage patterns...')
// Simple memory usage simulation
const initialMemory = process.memoryUsage?.() || { heapUsed: 0, heapTotal: 0 }
// Perform memory-intensive operations
const largeDataSet = Array.from({ length: 1000 }, (_, i) => ({
id: `item-${i}`,
content: `This is test content item ${i} with some additional data to consume memory`,
processed: false
}))
// Process data through the system
const pipeline = serviceManager.getProcessingPipeline()
await pipeline.processBatch(largeDataSet.map(item => ({
id: item.id,
title: `Title ${item.id}`,
content: item.content,
originalLanguage: 'en',
publishedAt: Date.now(),
tags: [],
keywords: [],
quality: 0,
viewCount: 0,
likeCount: 0,
shareCount: 0,
status: 'draft'
})), { batchSize: 50, concurrency: 5 })
const finalMemory = process.memoryUsage?.() || { heapUsed: 0, heapTotal: 0 }
return {
initialHeapUsed: initialMemory.heapUsed,
finalHeapUsed: finalMemory.heapUsed,
memoryIncrease: finalMemory.heapUsed - initialMemory.heapUsed,
heapTotal: finalMemory.heapTotal,
dataSetSize: largeDataSet.length
}
}
/**
* Test retry mechanisms
*/
private async testRetryMechanisms(): Promise<{ passed: boolean; details: any }> {
console.log(' 🔄 Testing retry mechanisms...')
let retryCount = 0
const maxRetries = 3
const testOperation = async () => {
retryCount++
if (retryCount < maxRetries) {
throw new Error('Simulated transient error')
}
return 'Success after retries'
}
try {
const result = await this.errorHandler.executeWithRetry(testOperation, {
operationName: 'test_retry',
retryable: true
})
return {
passed: result.success && result.attempts.length === maxRetries,
details: {
retryCount,
attempts: result.attempts.length,
finalResult: result.data,
totalDuration: result.totalDuration
}
}
} catch (error) {
return {
passed: false,
details: { error: String(error) }
}
}
}
/**
* Test circuit breaker functionality
*/
private async testCircuitBreaker(): Promise<{ passed: boolean; details: any }> {
console.log(' ⚡ Testing circuit breaker...')
// Simulate multiple failures to trigger circuit breaker
let failureCount = 0
const testOperation = async () => {
failureCount++
throw new Error('Service unavailable')
}
const results = []
// Make multiple failing requests
for (let i = 0; i < 10; i++) {
const result = await this.errorHandler.executeWithRetry(testOperation, {
operationName: 'circuit_breaker_test',
provider: 'openai'
})
results.push(result)
}
const status = this.errorHandler.getErrorHandlingStatus()
const circuitBreaker = status.circuitBreakers.find(cb => cb.key.includes('circuit_breaker_test'))
return {
passed: circuitBreaker?.status.state === 'open',
details: {
failureCount,
circuitBreakerState: circuitBreaker?.status.state,
failuresRecorded: circuitBreaker?.status.failureCount
}
}
}
/**
* Test fallback providers
*/
private async testFallbackProviders(): Promise<{ passed: boolean; details: any }> {
console.log(' 🔄 Testing fallback providers...')
// This is a simplified test - in real implementation,
// we would configure the system to fail over to different providers
const testResults = {
primaryProviderFailed: true,
fallbackProviderUsed: true,
finalResult: 'success'
}
return {
passed: testResults.fallbackProviderUsed,
details: testResults
}
}
/**
* Test error classification
*/
private async testErrorClassification(): Promise<{ passed: boolean; details: any }> {
console.log(' 🏷️ Testing error classification...')
const testErrors = [
new Error('Connection timeout'),
new Error('Rate limit exceeded'),
new Error('Invalid API key'),
new Error('Quota exceeded'),
new Error('Internal server error')
]
const classifications = testErrors.map(error => {
return this.errorHandler.executeWithRetry(
async () => { throw error },
{ operationName: 'classification_test' }
)
})
const results = await Promise.all(classifications)
const errorCategories = results.map(r => r.error?.category)
return {
passed: errorCategories.every(cat => cat !== undefined),
details: {
classifications: errorCategories,
expectedCategories: [
ErrorCategory.TRANSIENT,
ErrorCategory.RATE_LIMIT,
ErrorCategory.AUTHENTICATION,
ErrorCategory.QUOTA_EXCEEDED,
ErrorCategory.SERVICE_ERROR
]
}
}
}
/**
* Analyze performance test results
*/
private analyzePerformanceResults(results: any): boolean {
// Define performance thresholds
const thresholds = {
maxAverageLatency: 3000, // 3 seconds
minThroughput: 1, // 1 request per second
minSuccessRate: 0.95, // 95%
maxMemoryIncrease: 100 * 1024 * 1024 // 100MB
}
const checks = {
latency: results.latencyTests.translation.average < thresholds.maxAverageLatency,
throughput: results.throughputTests.throughput > thresholds.minThroughput,
successRate: results.throughputTests.successRate > thresholds.minSuccessRate,
memory: results.memoryTests.memoryIncrease < thresholds.maxMemoryIncrease
}
const passed = Object.values(checks).every(check => check)
console.log(' 📊 Performance Analysis:')
console.log(` ✅ Latency: ${checks.latency ? 'PASS' : 'FAIL'} (${results.latencyTests.translation.average}ms avg)`)
console.log(` ✅ Throughput: ${checks.throughput ? 'PASS' : 'FAIL'} (${results.throughputTests.throughput} req/s)`)
console.log(` ✅ Success Rate: ${checks.successRate ? 'PASS' : 'FAIL'} (${results.throughputTests.successRate}%)`)
console.log(` ✅ Memory: ${checks.memory ? 'PASS' : 'FAIL'} (+${Math.round(results.memoryTests.memoryIncrease / 1024 / 1024)}MB)`)
return passed
}
/**
* Calculate overall test results
*/
private calculateOverallResults(): void {
const results = Object.values(this.testResults.results).filter(r => r !== undefined)
const passedTests = results.filter(r => r.passed).length
const totalTests = results.length
this.testResults.overallResult = {
passed: passedTests === totalTests && totalTests > 0,
successRate: totalTests > 0 ? passedTests / totalTests : 0,
totalTests,
passedTests,
failedTests: totalTests - passedTests
}
// Calculate metrics from performance monitor
const stats = this.performanceMonitor.getPerformanceStats(
this.testResults.startTime,
this.testResults.endTime
)
this.testResults.metrics = {
totalCost: stats.costs.total,
averageLatency: stats.timing.averageLatency,
throughput: stats.requests.total / (this.testResults.totalDuration / 1000),
errorRate: 1 - stats.requests.successRate
}
}
/**
* Generate recommendations based on test results
*/
private generateRecommendations(): void {
const recommendations: string[] = []
// Performance recommendations
if (this.testResults.metrics.averageLatency > 2000) {
recommendations.push('Consider implementing caching to reduce response times')
}
if (this.testResults.metrics.errorRate > 0.05) {
recommendations.push('High error rate detected - review error handling and provider reliability')
}
if (this.testResults.metrics.totalCost > this.config.maxCostLimit) {
recommendations.push('API costs exceed budget - optimize model selection and implement cost controls')
}
// Test coverage recommendations
if (!this.config.runIntegrationTests) {
recommendations.push('Enable integration tests to validate end-to-end functionality')
}
if (!this.config.enableRealAPIs) {
recommendations.push('Test with real API keys to validate production readiness')
}
this.testResults.recommendations = recommendations
}
/**
* Generate comprehensive test report
*/
private async generateTestReport(): Promise<void> {
console.log('📄 Generating test report...')
const report = {
summary: this.testResults,
systemHealth: this.performanceMonitor.getSystemHealth(),
errorStatus: this.errorHandler.getErrorHandlingStatus(),
recommendations: this.performanceMonitor.getOptimizationRecommendations(),
exportTime: new Date().toISOString()
}
try {
const reportData = JSON.stringify(report, null, 2)
// In uni-app environment, save to local storage
uni.setStorageSync('ai-news-test-report', reportData)
console.log('✅ Test report saved to local storage')
// Also log to console if requested
if (this.config.outputFormat === 'console') {
console.log('\n📋 Test Report:')
console.log('===============')
console.log(reportData)
}
} catch (error) {
console.error('❌ Failed to generate test report:', error)
}
}
/**
* Print test summary
*/
private printSummary(): void {
const result = this.testResults.overallResult
const duration = this.testResults.totalDuration
console.log('\n🎯 Test Suite Summary')
console.log('====================')
console.log(`Overall Result: ${result.passed ? '✅ PASSED' : '❌ FAILED'}`)
console.log(`Success Rate: ${(result.successRate * 100).toFixed(1)}%`)
console.log(`Tests: ${result.passedTests}/${result.totalTests} passed`)
console.log(`Duration: ${duration.toLocaleString()}ms`)
console.log(`Total Cost: $${this.testResults.metrics.totalCost.toFixed(4)}`)
console.log(`Avg Latency: ${this.testResults.metrics.averageLatency.toFixed(0)}ms`)
console.log(`Error Rate: ${(this.testResults.metrics.errorRate * 100).toFixed(2)}%`)
if (this.testResults.recommendations.length > 0) {
console.log('\n💡 Recommendations:')
this.testResults.recommendations.forEach((rec, i) => {
console.log(`${i + 1}. ${rec}`)
})
}
if (result.passed) {
console.log('\n🎉 All tests passed! The AI News System is ready for production.')
} else {
console.log('\n💥 Some tests failed. Please review the results and fix the issues.')
}
}
/**
* Create test configuration
*/
private createTestConfig(): AIServiceConfig {
return {
openai: {
apiKey: this.config.apiKeys?.openai || 'test-key',
model: 'gpt-3.5-turbo',
maxTokens: 1000,
temperature: 0.7
},
google: {
apiKey: this.config.apiKeys?.google || 'test-key',
projectId: 'test-project'
},
baidu: {
appId: this.config.apiKeys?.baidu?.appId || 'test-app-id',
secretKey: this.config.apiKeys?.baidu?.secretKey || 'test-secret',
model: 'ernie-bot'
},
costLimits: {
dailyUSD: this.config.maxCostLimit,
monthlyUSD: this.config.maxCostLimit * 30,
perRequestUSD: this.config.maxCostLimit / 100
}
}
}
/**
* Initialize test results structure
*/
private initializeTestResults(): TestSummary {
return {
testSuite: 'AI News System Comprehensive Test Suite',
startTime: 0,
endTime: 0,
totalDuration: 0,
results: {},
overallResult: {
passed: false,
successRate: 0,
totalTests: 0,
passedTests: 0,
failedTests: 0
},
metrics: {
totalCost: 0,
averageLatency: 0,
throughput: 0,
errorRate: 0
},
recommendations: []
}
}
}
// Default test configuration
export const defaultTestConfig: TestSuiteConfig = {
runUnitTests: true,
runIntegrationTests: true,
runPerformanceTests: true,
runErrorHandlingTests: true,
enableRealAPIs: false, // Set to true for production testing
testTimeout: 30000, // 30 seconds
maxCostLimit: 10.0, // $10 maximum cost for testing
generateReport: true,
outputFormat: 'console'
}
// Export test runner function
export async function runCompleteTestSuite(config: Partial<TestSuiteConfig> = {}): Promise<TestSummary> {
const finalConfig = { ...defaultTestConfig, ...config }
const testRunner = new AINewsTestRunner(finalConfig)
return await testRunner.runCompleteTestSuite()
}
// Main execution function
if (typeof require !== 'undefined' && require.main === module) {
runCompleteTestSuite()
.then(results => {
console.log('\n🏁 Test suite completed')
process.exit(results.overallResult.passed ? 0 : 1)
})
.catch(error => {
console.error('💥 Test suite execution failed:', error)
process.exit(1)
})
}

View File

@@ -0,0 +1,745 @@
// AI News System Integration Test Suite
// Comprehensive integration testing with real AI service APIs
import {
AIServiceManager,
type AIServiceConfig,
type ContentInfo,
type AIResponse,
type TranslationResult,
type ContentAnalysisResult
} from '../index.uts'
/**
* Integration test configuration
*/
type IntegrationTestConfig = {
enableRealAPIs: boolean
apiKeys: {
openai?: string
google?: string
baidu?: {
appId: string
secretKey: string
}
}
testTimeout: number
retryAttempts: number
costLimits: {
maxCostPerTest: number
dailyLimit: number
}
}
/**
* Test metrics and results
*/
type TestMetrics = {
testName: string
startTime: number
endTime: number
duration: number
success: boolean
error?: string
metrics?: {
tokensUsed?: number
costUSD?: number
latencyMs?: number
throughput?: number
}
}
/**
* 综合集成测试类
*/
export class AINewsIntegrationTest {
private config: IntegrationTestConfig
private serviceManager: AIServiceManager
private testResults: TestMetrics[] = []
private totalCost: number = 0
constructor(config: IntegrationTestConfig) {
this.config = config
this.initializeServices()
}
private initializeServices(): void {
const aiConfig: AIServiceConfig = {
openai: {
apiKey: this.config.apiKeys.openai || 'test-key',
model: 'gpt-3.5-turbo',
maxTokens: 1500,
temperature: 0.7
},
google: {
apiKey: this.config.apiKeys.google || 'test-key',
projectId: 'test-project'
},
baidu: {
appId: this.config.apiKeys.baidu?.appId || 'test-app-id',
secretKey: this.config.apiKeys.baidu?.secretKey || 'test-secret',
model: 'ernie-bot'
},
costLimits: {
dailyUSD: this.config.costLimits.dailyLimit,
monthlyUSD: this.config.costLimits.dailyLimit * 30,
perRequestUSD: this.config.costLimits.maxCostPerTest
},
qualityThresholds: {
translation: 0.8,
sentiment: 0.7,
credibility: 0.6
}
}
this.serviceManager = new AIServiceManager(aiConfig)
}
/**
* 集成测试1: 多提供商翻译服务测试
*/
async testMultiProviderTranslation(): Promise<TestMetrics> {
const testName = 'Multi-Provider Translation Test'
const startTime = Date.now()
try {
console.log(`🧪 Starting ${testName}...`)
const translationService = this.serviceManager.getTranslationService()
const testTexts = [
{
text: "Artificial intelligence is revolutionizing the news industry with automated content generation and smart recommendations.",
from: 'en',
to: 'zh-CN'
},
{
text: "人工智能正在通过自动化内容生成和智能推荐革命性地改变新闻行业。",
from: 'zh-CN',
to: 'en'
},
{
text: "L'intelligence artificielle révolutionne l'industrie de l'information avec la génération de contenu automatisée.",
from: 'fr',
to: 'zh-CN'
}
]
let totalTokens = 0
let totalCost = 0
const results: TranslationResult[] = []
// Test each provider
const providers = ['openai', 'google', 'baidu'] as const
for (const provider of providers) {
console.log(` Testing provider: ${provider}`)
for (const testCase of testTexts) {
const result = await translationService.translateText(
testCase.text,
testCase.to,
testCase.from,
{
provider,
culturalAdaptation: true,
preserveFormatting: true
}
)
if (result.success && result.data) {
results.push(result.data)
totalTokens += result.data.tokensUsed || 0
totalCost += result.data.costUSD || 0
console.log(` ✅ ${testCase.from} → ${testCase.to}: ${result.data.translatedText.substring(0, 50)}...`)
} else {
console.log(` ❌ Translation failed: ${result.error}`)
}
}
}
const endTime = Date.now()
const duration = endTime - startTime
const metrics: TestMetrics = {
testName,
startTime,
endTime,
duration,
success: results.length > 0,
metrics: {
tokensUsed: totalTokens,
costUSD: totalCost,
latencyMs: duration / results.length,
throughput: results.length / (duration / 1000)
}
}
this.totalCost += totalCost
console.log(`✅ ${testName} completed in ${duration}ms`)
return metrics
} catch (error) {
const endTime = Date.now()
return {
testName,
startTime,
endTime,
duration: endTime - startTime,
success: false,
error: error instanceof Error ? error.message : String(error)
}
}
}
/**
* 集成测试2: 内容分析端到端测试
*/
async testContentAnalysisEndToEnd(): Promise<TestMetrics> {
const testName = 'Content Analysis End-to-End Test'
const startTime = Date.now()
try {
console.log(`🧪 Starting ${testName}...`)
const analysisService = this.serviceManager.getAnalysisService()
const testArticles = [
{
title: "科技巨头发布突破性AI技术",
content: "今日多家科技公司宣布了他们在人工智能领域的最新突破。这些技术预计将在未来几年内改变我们的生活方式。专家表示这标志着AI发展的新里程碑。",
language: 'zh-CN'
},
{
title: "Global Economic Outlook Shows Mixed Signals",
content: "Economic analysts are divided on the global economic forecast for next year. While some indicators point to recovery, others suggest continued volatility in key markets.",
language: 'en'
},
{
title: "Climate Change Impact on Agriculture",
content: "Recent studies show that climate change is significantly affecting crop yields worldwide. Farmers are adapting new techniques to cope with changing weather patterns.",
language: 'en'
}
]
let totalTokens = 0
let totalCost = 0
const results: ContentAnalysisResult[] = []
for (const article of testArticles) {
console.log(` Analyzing: ${article.title}`)
const analysisResult = await analysisService.analyzeContent(
article.content,
{
types: ['sentiment', 'entities', 'keywords', 'topics', 'quality', 'toxicity'],
language: article.language,
enableCaching: true
}
)
if (analysisResult.success && analysisResult.data) {
results.push(analysisResult.data)
totalTokens += analysisResult.data.tokensUsed || 0
totalCost += analysisResult.data.costUSD || 0
console.log(` ✅ Sentiment: ${analysisResult.data.sentimentLabel} (${analysisResult.data.sentimentScore.toFixed(2)})`)
console.log(` ✅ Entities: ${analysisResult.data.entities?.length || 0} found`)
console.log(` ✅ Keywords: ${analysisResult.data.keywords?.length || 0} extracted`)
console.log(` ✅ Quality: ${analysisResult.data.qualityScore?.toFixed(2) || 'N/A'}`)
} else {
console.log(` ❌ Analysis failed: ${analysisResult.error}`)
}
}
const endTime = Date.now()
const duration = endTime - startTime
const metrics: TestMetrics = {
testName,
startTime,
endTime,
duration,
success: results.length > 0,
metrics: {
tokensUsed: totalTokens,
costUSD: totalCost,
latencyMs: duration / results.length,
throughput: results.length / (duration / 1000)
}
}
this.totalCost += totalCost
console.log(`✅ ${testName} completed in ${duration}ms`)
return metrics
} catch (error) {
const endTime = Date.now()
return {
testName,
startTime,
endTime,
duration: endTime - startTime,
success: false,
error: error instanceof Error ? error.message : String(error)
}
}
}
/**
* 集成测试3: 智能对话会话测试
*/
async testChatSessionFlow(): Promise<TestMetrics> {
const testName = 'Chat Session Flow Test'
const startTime = Date.now()
try {
console.log(`🧪 Starting ${testName}...`)
const chatService = this.serviceManager.getChatService()
const testConversations = [
{
language: 'zh-CN',
messages: [
'你好,我想了解今天的重要新闻',
'请推荐一些科技新闻',
'能否分析一下AI对新闻行业的影响'
]
},
{
language: 'en',
messages: [
'Hello, what are the top news stories today?',
'Can you translate this Chinese news for me?',
'What do you think about the latest AI developments?'
]
}
]
let totalCost = 0
let sessionsCreated = 0
let messagesProcessed = 0
for (const conversation of testConversations) {
console.log(` Testing conversation in ${conversation.language}`)
// Create session
const sessionResult = await chatService.createChatSession(
`test-user-${Date.now()}`,
conversation.language
)
if (!sessionResult.success || !sessionResult.data) {
console.log(` ❌ Failed to create session: ${sessionResult.error}`)
continue
}
sessionsCreated++
const sessionId = sessionResult.data.id
// Process conversation
for (const message of conversation.messages) {
const response = await chatService.sendMessage(
sessionId,
message,
{
provider: 'openai',
temperature: 0.7,
contextWindow: 5
}
)
if (response.success && response.data) {
messagesProcessed++
totalCost += response.data.costUSD || 0
console.log(` ✅ Message processed: ${response.data.content.substring(0, 50)}...`)
} else {
console.log(` ❌ Message failed: ${response.error}`)
}
}
// Test session cleanup
await chatService.endChatSession(sessionId)
}
const endTime = Date.now()
const duration = endTime - startTime
const metrics: TestMetrics = {
testName,
startTime,
endTime,
duration,
success: messagesProcessed > 0,
metrics: {
costUSD: totalCost,
latencyMs: duration / messagesProcessed,
throughput: messagesProcessed / (duration / 1000)
}
}
this.totalCost += totalCost
console.log(`✅ ${testName} completed: ${sessionsCreated} sessions, ${messagesProcessed} messages`)
return metrics
} catch (error) {
const endTime = Date.now()
return {
testName,
startTime,
endTime,
duration: endTime - startTime,
success: false,
error: error instanceof Error ? error.message : String(error)
}
}
}
/**
* 集成测试4: 推荐系统性能测试
*/
async testRecommendationPerformance(): Promise<TestMetrics> {
const testName = 'Recommendation Performance Test'
const startTime = Date.now()
try {
console.log(`🧪 Starting ${testName}...`)
const recommendationService = this.serviceManager.getRecommendationService()
// Create test news content
const testNews: ContentInfo[] = Array.from({ length: 100 }, (_, i) => ({
id: `news-${i}`,
title: `Test News Article ${i}`,
content: `This is test content for news article ${i}. It contains various topics and keywords for testing recommendation algorithms.`,
originalLanguage: 'en',
publishedAt: Date.now() - Math.random() * 86400000, // Random time in last 24h
tags: [`tag-${i % 10}`, `category-${i % 5}`],
keywords: [`keyword-${i % 20}`, `topic-${i % 15}`],
quality: Math.random(),
viewCount: Math.floor(Math.random() * 1000),
likeCount: Math.floor(Math.random() * 100),
shareCount: Math.floor(Math.random() * 50),
status: 'published',
categoryId: `category-${i % 5}`
}))
const testUsers = Array.from({ length: 10 }, (_, i) => `test-user-${i}`)
let recommendationsGenerated = 0
// Test different recommendation algorithms
const algorithms = ['collaborative', 'content_based', 'hybrid'] as const
for (const algorithm of algorithms) {
console.log(` Testing ${algorithm} algorithm`)
for (const userId of testUsers) {
// Record some user behavior first
await recommendationService.recordUserBehavior({
userId,
contentId: testNews[Math.floor(Math.random() * testNews.length)].id,
actionType: 'view',
timestamp: Date.now(),
duration: Math.random() * 300 + 30
})
// Get recommendations
const recommendations = await recommendationService.getPersonalizedRecommendations(
userId,
testNews,
{
algorithm,
maxResults: 5,
diversityWeight: 0.3,
freshnessWeight: 0.4,
personalizedWeight: 0.3
}
)
if (recommendations.success && recommendations.data) {
recommendationsGenerated += recommendations.data.length
}
}
}
const endTime = Date.now()
const duration = endTime - startTime
const metrics: TestMetrics = {
testName,
startTime,
endTime,
duration,
success: recommendationsGenerated > 0,
metrics: {
latencyMs: duration / recommendationsGenerated,
throughput: recommendationsGenerated / (duration / 1000)
}
}
console.log(`✅ ${testName} completed: ${recommendationsGenerated} recommendations generated`)
return metrics
} catch (error) {
const endTime = Date.now()
return {
testName,
startTime,
endTime,
duration: endTime - startTime,
success: false,
error: error instanceof Error ? error.message : String(error)
}
}
}
/**
* 集成测试5: 内容处理管道压力测试
*/
async testContentPipelineStress(): Promise<TestMetrics> {
const testName = 'Content Pipeline Stress Test'
const startTime = Date.now()
try {
console.log(`🧪 Starting ${testName}...`)
const pipeline = this.serviceManager.getProcessingPipeline()
// Create large batch of test content
const testContent: ContentInfo[] = Array.from({ length: 50 }, (_, i) => ({
id: `stress-test-${i}`,
title: `Stress Test Article ${i}`,
content: `This is a stress test article number ${i}. It contains enough content to trigger AI processing steps including translation, analysis, and quality assessment. The content discusses various topics like technology, economics, and social issues to test the system's ability to handle diverse content types.`,
originalLanguage: 'en',
publishedAt: Date.now(),
tags: [`stress-${i}`, `test-${i % 10}`],
keywords: [],
quality: 0,
viewCount: 0,
likeCount: 0,
shareCount: 0,
status: 'draft'
}))
let processedCount = 0
let totalCost = 0
// Process in batches
const batchResult = await pipeline.processBatch(
testContent,
{
batchSize: 10,
concurrency: 3,
enableCaching: true,
onProgress: (completed, total) => {
console.log(` Progress: ${completed}/${total} items processed`)
},
onError: (error, item) => {
console.log(` Error processing ${item.id}: ${error}`)
}
}
)
if (batchResult.success && batchResult.data) {
processedCount = batchResult.data.length
totalCost = batchResult.data.reduce((sum, result) =>
sum + (result.costUSD || 0), 0
)
}
const endTime = Date.now()
const duration = endTime - startTime
const metrics: TestMetrics = {
testName,
startTime,
endTime,
duration,
success: processedCount > 0,
metrics: {
costUSD: totalCost,
latencyMs: duration / processedCount,
throughput: processedCount / (duration / 1000)
}
}
this.totalCost += totalCost
console.log(`✅ ${testName} completed: ${processedCount}/${testContent.length} items processed`)
return metrics
} catch (error) {
const endTime = Date.now()
return {
testName,
startTime,
endTime,
duration: endTime - startTime,
success: false,
error: error instanceof Error ? error.message : String(error)
}
}
}
/**
* 运行所有集成测试
*/
async runAllIntegrationTests(): Promise<{
success: boolean
results: TestMetrics[]
summary: {
totalTests: number
passedTests: number
failedTests: number
totalDuration: number
totalCost: number
averageLatency: number
totalThroughput: number
}
}> {
console.log('🚀 Starting AI News System Integration Tests...')
console.log('==============================================')
const startTime = Date.now()
try {
// Initialize service manager
const initResult = await this.serviceManager.initialize()
if (!initResult.success) {
throw new Error(`Failed to initialize services: ${initResult.error}`)
}
// Run all integration tests
const tests = [
() => this.testMultiProviderTranslation(),
() => this.testContentAnalysisEndToEnd(),
() => this.testChatSessionFlow(),
() => this.testRecommendationPerformance(),
() => this.testContentPipelineStress()
]
this.testResults = []
for (const testFn of tests) {
const result = await testFn()
this.testResults.push(result)
// Check cost limits
if (this.totalCost > this.config.costLimits.maxCostPerTest * tests.length) {
console.log('⚠️ Cost limit reached, stopping tests')
break
}
}
const endTime = Date.now()
const totalDuration = endTime - startTime
// Calculate summary statistics
const passedTests = this.testResults.filter(r => r.success).length
const failedTests = this.testResults.length - passedTests
const averageLatency = this.testResults.reduce((sum, r) =>
sum + (r.metrics?.latencyMs || 0), 0
) / this.testResults.length
const totalThroughput = this.testResults.reduce((sum, r) =>
sum + (r.metrics?.throughput || 0), 0
)
const summary = {
totalTests: this.testResults.length,
passedTests,
failedTests,
totalDuration,
totalCost: this.totalCost,
averageLatency,
totalThroughput
}
// Print results
this.printTestResults(summary)
return {
success: failedTests === 0,
results: this.testResults,
summary
}
} catch (error) {
console.error('💥 Integration test execution failed:', error)
return {
success: false,
results: this.testResults,
summary: {
totalTests: 0,
passedTests: 0,
failedTests: 1,
totalDuration: Date.now() - startTime,
totalCost: this.totalCost,
averageLatency: 0,
totalThroughput: 0
}
}
} finally {
// Cleanup
await this.serviceManager.shutdown()
}
}
/**
* 打印测试结果
*/
private printTestResults(summary: any): void {
console.log('\n📊 Integration Test Results:')
console.log('============================')
this.testResults.forEach(result => {
const status = result.success ? '✅' : '❌'
const duration = result.duration.toLocaleString()
const cost = result.metrics?.costUSD?.toFixed(4) || '0.0000'
const latency = result.metrics?.latencyMs?.toFixed(0) || 'N/A'
console.log(`${status} ${result.testName}`)
console.log(` Duration: ${duration}ms | Cost: $${cost} | Latency: ${latency}ms`)
if (!result.success && result.error) {
console.log(` Error: ${result.error}`)
}
})
console.log('\n📈 Summary Statistics:')
console.log('======================')
console.log(`✅ Passed: ${summary.passedTests}`)
console.log(`❌ Failed: ${summary.failedTests}`)
console.log(`📊 Success Rate: ${((summary.passedTests / summary.totalTests) * 100).toFixed(1)}%`)
console.log(`⏱️ Total Duration: ${summary.totalDuration.toLocaleString()}ms`)
console.log(`💰 Total Cost: $${summary.totalCost.toFixed(4)}`)
console.log(`📡 Average Latency: ${summary.averageLatency.toFixed(0)}ms`)
console.log(`🚀 Total Throughput: ${summary.totalThroughput.toFixed(2)} ops/sec`)
if (summary.failedTests === 0) {
console.log('\n🎉 All integration tests passed! The AI News System is production-ready.')
} else {
console.log('\n💥 Some integration tests failed. Please review the errors and fix the issues.')
}
}
}
// Export test runner function
export async function runIntegrationTests(config: IntegrationTestConfig): Promise<boolean> {
const testRunner = new AINewsIntegrationTest(config)
const result = await testRunner.runAllIntegrationTests()
return result.success
}
// Default configuration for running tests
export const defaultIntegrationConfig: IntegrationTestConfig = {
enableRealAPIs: false, // Set to true for real API testing
apiKeys: {
// Add your real API keys here for production testing
// openai: 'your-openai-api-key',
// google: 'your-google-api-key',
// baidu: { appId: 'your-baidu-app-id', secretKey: 'your-baidu-secret' }
},
testTimeout: 30000, // 30 seconds per test
retryAttempts: 3,
costLimits: {
maxCostPerTest: 5.0, // $5 per test
dailyLimit: 50.0 // $50 per day
}
}

View File

@@ -0,0 +1,302 @@
// Simple Test for AI News System
import {
AIServiceManager,
AITranslationService,
AIContentAnalysisService,
type AIServiceConfig,
type ContentInfo
} from '../index.uts'
/**
* 简单的AI新闻系统测试
* 用于验证基本功能是否正常工作
*/
export class SimpleAINewsTest {
/**
* 测试翻译服务基本功能
*/
static async testTranslationService(): Promise<boolean> {
try {
console.log('🧪 Testing Translation Service...')
const config: AIServiceConfig = {
openai: {
apiKey: 'test-key',
model: 'gpt-3.5-turbo',
maxTokens: 1000,
temperature: 0.7
}
}
const translationService = new AITranslationService(config)
// 测试语言检测
const detection = await translationService.detectLanguage('Hello world')
if (!detection.success) {
console.error('❌ Language detection failed')
return false
}
// 测试翻译功能
const translation = await translationService.translateText(
'Hello world',
'zh-CN',
'en'
)
if (!translation.success) {
console.error('❌ Translation failed:', translation.error)
return false
}
console.log('✅ Translation service test passed')
return true
} catch (error) {
console.error('❌ Translation service test failed:', error)
return false
}
}
/**
* 测试内容分析服务基本功能
*/
static async testAnalysisService(): Promise<boolean> {
try {
console.log('🧪 Testing Content Analysis Service...')
const config: AIServiceConfig = {
openai: {
apiKey: 'test-key',
model: 'gpt-3.5-turbo',
maxTokens: 1000,
temperature: 0.7
}
}
const analysisService = new AIContentAnalysisService(config)
const testContent = '今天是个好天气,阳光明媚,让人心情愉快。'
const analysis = await analysisService.analyzeContent(testContent, {
types: ['sentiment', 'keywords', 'readability'],
language: 'zh-CN'
})
if (!analysis.success) {
console.error('❌ Content analysis failed:', analysis.error)
return false
}
if (!analysis.data) {
console.error('❌ Analysis data is missing')
return false
}
// 检查基本结果
if (typeof analysis.data.sentimentScore !== 'number') {
console.error('❌ Sentiment score is not a number')
return false
}
if (!Array.isArray(analysis.data.keywords)) {
console.error('❌ Keywords is not an array')
return false
}
console.log('✅ Content analysis service test passed')
return true
} catch (error) {
console.error('❌ Content analysis service test failed:', error)
return false
}
}
/**
* 测试服务管理器基本功能
*/
static async testServiceManager(): Promise<boolean> {
try {
console.log('🧪 Testing Service Manager...')
const config: AIServiceConfig = {
openai: {
apiKey: 'test-key',
model: 'gpt-3.5-turbo',
maxTokens: 1000,
temperature: 0.7
},
costLimits: {
dailyUSD: 100,
monthlyUSD: 1000,
perRequestUSD: 10
}
}
const serviceManager = new AIServiceManager(config)
// 测试初始化
const initResult = await serviceManager.initialize()
if (!initResult.success) {
console.error('❌ Service manager initialization failed:', initResult.error)
return false
}
// 测试服务获取
const translationService = serviceManager.getTranslationService()
if (!translationService) {
console.error('❌ Failed to get translation service')
return false
}
const analysisService = serviceManager.getAnalysisService()
if (!analysisService) {
console.error('❌ Failed to get analysis service')
return false
}
const chatService = serviceManager.getChatService()
if (!chatService) {
console.error('❌ Failed to get chat service')
return false
}
const recommendationService = serviceManager.getRecommendationService()
if (!recommendationService) {
console.error('❌ Failed to get recommendation service')
return false
}
// 测试提供商选择
const bestProvider = serviceManager.selectBestProvider()
if (!bestProvider) {
console.error('❌ Failed to select best provider')
return false
}
// 测试成本检查
const costCheck = serviceManager.checkCostLimits(1.0)
if (typeof costCheck !== 'boolean') {
console.error('❌ Cost check failed')
return false
}
// 获取统计信息
const stats = serviceManager.getManagerStatistics()
if (!stats) {
console.error('❌ Failed to get statistics')
return false
}
// 清理
await serviceManager.shutdown()
console.log('✅ Service manager test passed')
return true
} catch (error) {
console.error('❌ Service manager test failed:', error)
return false
}
}
/**
* 测试类型定义完整性
*/
static testTypeDefinitions(): boolean {
try {
console.log('🧪 Testing Type Definitions...')
// 测试基本类型是否可用
const testContent: ContentInfo = {
id: 'test-123',
title: '测试新闻',
content: '这是一条测试新闻内容',
originalLanguage: 'zh-CN',
publishedAt: Date.now(),
tags: ['测试'],
keywords: ['测试', '新闻'],
quality: 0.8,
viewCount: 0,
likeCount: 0,
shareCount: 0,
status: 'draft'
}
// 验证类型结构
if (typeof testContent.id !== 'string') {
console.error('❌ ContentInfo.id type error')
return false
}
if (typeof testContent.title !== 'string') {
console.error('❌ ContentInfo.title type error')
return false
}
if (!Array.isArray(testContent.tags)) {
console.error('❌ ContentInfo.tags type error')
return false
}
console.log('✅ Type definitions test passed')
return true
} catch (error) {
console.error('❌ Type definitions test failed:', error)
return false
}
}
/**
* 运行所有测试
*/
static async runAllTests(): Promise<boolean> {
console.log('🚀 Starting AI News System Tests...')
console.log('=====================================')
const results: boolean[] = []
// 运行各项测试
results.push(this.testTypeDefinitions())
results.push(await this.testTranslationService())
results.push(await this.testAnalysisService())
results.push(await this.testServiceManager())
// 统计结果
const passedCount = results.filter(r => r).length
const totalCount = results.length
console.log('\n📊 Test Results:')
console.log('================')
console.log(`✅ Passed: ${passedCount}`)
console.log(`❌ Failed: ${totalCount - passedCount}`)
console.log(`📈 Success Rate: ${((passedCount / totalCount) * 100).toFixed(1)}%`)
if (passedCount === totalCount) {
console.log('\n🎉 All tests passed! AI News System is working correctly.')
return true
} else {
console.log('\n💥 Some tests failed. Please check the errors above.')
return false
}
}
}
// 导出测试运行函数
export async function runSimpleTests(): Promise<boolean> {
return await SimpleAINewsTest.runAllTests()
}
// 如果直接运行此文件,执行测试
if (typeof require !== 'undefined' && require.main === module) {
runSimpleTests().then(success => {
process.exit(success ? 0 : 1)
}).catch(error => {
console.error('Test execution failed:', error)
process.exit(1)
})
}

View File

@@ -0,0 +1,235 @@
// AI Service Types and Interfaces
// filepath: h:\blews\akmon\uni_modules\ak-ai-news\types\ai-types.uts
// AI提供商枚举
export type AIProvider = 'openai' | 'google' | 'baidu' | 'custom'
// 翻译结果接口
export type TranslationResult = {
translatedText: string
originalText: string
sourceLang: string
targetLang: string
confidence: number
qualityScore: number
provider: AIProvider
tokensUsed: number
processingTimeMs: number
costUSD: number
}
// 翻译选项
export type TranslationOptions = {
provider?: AIProvider
model?: string
temperature?: number
maxTokens?: number
culturalAdaptation?: boolean
preserveFormatting?: boolean
qualityThreshold?: number
}
// 内容分析结果
export type ContentAnalysisResult = {
contentId: string
sentimentScore: number // -1 to 1
sentimentLabel: 'positive' | 'negative' | 'neutral'
readabilityScore: number // 0 to 1
credibilityScore: number // 0 to 1
toxicityScore: number // 0 to 1
keywords: string[]
entities: EntityResult[]
topics: TopicResult[]
categories: CategoryResult[]
summary: string
keyPhrases: string[]
language: string
processingTimeMs: number
provider: AIProvider
}
// 实体识别结果
export type EntityResult = {
text: string
type: 'person' | 'location' | 'organization' | 'date' | 'money' | 'other'
confidence: number
startPosition: number
endPosition: number
}
// 主题提取结果
export type TopicResult = {
name: string
confidence: number
keywords: string[]
}
// 分类结果
export type CategoryResult = {
categoryId: string
categoryName: string
confidence: number
level: number
}
// 聊天消息
export type ChatMessage = {
id: string
sessionId: string
type: 'user' | 'assistant' | 'system' | 'error'
content: string
language: string
timestamp: number
metadata?: UTSJSONObject
tokensUsed?: number
responseTimeMs?: number
costUSD?: number
}
// 聊天会话
export type ChatSession = {
id: string
userId: string
name: string
language: string
aiModel: string
contextSettings: UTSJSONObject
totalMessages: number
totalTokensUsed: number
totalCostUSD: number
isActive: boolean
startedAt: number
lastMessageAt: number
endedAt?: number
}
// 推荐结果
export type RecommendationResult = {
contentId: string
userId: string
score: number
reason: string
algorithm: string
contextFactors: UTSJSONObject
recommendationType: 'trending' | 'personalized' | 'similar' | 'latest'
position: number
createdAt: number
}
// 内容信息
export type ContentInfo = {
id: string
title: string
content: string
summary?: string
author?: string
sourceUrl?: string
originalLanguage: string
publishedAt: number
categoryId?: string
tags: string[]
keywords: string[]
sentiment?: number
readability?: number
credibility?: number
quality: number
viewCount: number
likeCount: number
shareCount: number
status: 'draft' | 'published' | 'archived' | 'deleted'
}
// 处理步骤接口
export type ProcessingStep = {
name: string
order: number
execute: (data: any) => Promise<any>
rollback?: (data: any) => Promise<void>
validate?: (data: any) => boolean
}
// AI服务配置
export type AIServiceConfig = {
openai?: {
apiKey: string
model: string
baseURL?: string
maxTokens: number
temperature: number
}
google?: {
apiKey: string
projectId?: string
model: string
}
baidu?: {
apiKey: string
secretKey: string
model: string
}
costLimits?: {
dailyUSD: number
monthlyUSD: number
perRequestUSD: number
}
qualityThresholds?: {
translation: number
sentiment: number
credibility: number
}
}
// API响应基础类型
export type AIResponse<T> = {
success: boolean
data?: T
error?: string
errorCode?: string
tokensUsed?: number
processingTimeMs?: number
costUSD?: number
provider?: AIProvider
requestId?: string
}
// 批处理选项
export type BatchProcessingOptions = {
batchSize: number
concurrency: number
retryCount: number
delayMs: number
onProgress?: (completed: number, total: number) => void
onError?: (error: any, item: any) => void
}
// 缓存选项
export type CacheOptions = {
enabled: boolean
ttlHours: number
maxSize: number
strategy: 'lru' | 'fifo' | 'ttl'
}
// 监控和统计
export type UsageStatistics = {
provider: AIProvider
serviceType: string
tokensUsed: number
requestsCount: number
costUSD: number
successCount: number
errorCount: number
avgResponseTimeMs: number
date: string
hour: number
}
// 错误类型
export type AIServiceError = {
code: string
message: string
provider?: AIProvider
requestId?: string
retryable: boolean
details?: UTSJSONObject
}

View File

@@ -0,0 +1,55 @@
# ak-charts
一个功能完整的 uni_modules 图表插件支持多种图表类型UTS 插件规范。
## 支持的图表类型
- `bar` - 垂直条形图
- `horizontalBar` - 水平条形图
- `line` - 折线图
- `area` - 面积图
- `pie` - 饼图
- `doughnut` - 环形图
- `radar` - 雷达图
## 使用方法
1. 在页面中引用组件:
````vue
<ak-charts :option="option" canvas-id="my-canvas"></ak-charts>
````
2. 通过 AkCharts.render(option, canvasId) 进行全局渲染调用。
option 示例:
```js
{
type: 'area', // 或其他支持的类型
data: [75, 80, 85, 90, 87, 92, 88],
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
color: '#6366F1' // 或颜色数组 ['#FF6384', '#36A2EB', ...]
}
```
## 图表特性
### 面积图 (area)
- 适合显示趋势数据
- 支持渐变填充
- 自动生成网格线和标签
### 水平条形图 (horizontalBar)
- 适合显示分类数据对比
- 支持渐变填充
- 标签显示在左侧
### 环形图 (doughnut)
- 支持多色配置
- 自动计算百分比
- 包含图例显示
## 目录结构
- interface.uts 类型定义
- components/ak-charts.uvue 图表组件
- package.json 插件描述
- README.md 插件说明

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,41 @@
/**
* ak-charts UTS 插件主入口
* 提供注册和渲染图表的基础接口
*/
export type ChartType = 'bar' | 'line' | 'pie' | 'doughnut' | 'radar' | 'area' | 'horizontalBar' | 'multi';
export type ChartOption {
type: ChartType;
data: number[] | number[][];
labels?: string[];
color?: string | string[]; // 支持单色或多色配置
seriesNames?: string[];
seriesAxis?: string[];
// 圆饼图和环形图特有配置
centerX?: number;
centerY?: number;
radius?: number;
innerRadius?: number; // 环形图内圆半径
}
export type Margin {
top: number;
right: number;
bottom: number;
left: number;
}
export class AkCharts {
// 注册图表(可扩展)
static register(type: ChartType, render: any): void {
// 这里可以实现自定义图表类型注册
}
// 渲染图表(实际渲染由组件完成)
static render(option: ChartOption, canvasId: string): void {
// 这里只做参数校验和分发,实际渲染由 ak-charts.vue 组件实现
// 可通过 uni.$emit/uni.$on 或全局事件通信
uni.$emit('ak-charts-render', { option, canvasId });
}
}
export default AkCharts;

View File

@@ -0,0 +1,12 @@
{
"name": "ak-charts",
"version": "0.1.0",
"description": "一个简单的uni_modules图表插件支持基础柱状图和折线图UTS插件规范。",
"uni_modules": {
"uni_modules": true,
"platforms": ["app", "h5", "mp-weixin"],
"uts": true
},
"main": "index.uts",
"keywords": ["charts", "canvas", "uni_modules", "uts"]
}

View File

@@ -0,0 +1,19 @@
## 0.0.82025-03-07
- fix: 修复hbx 4.54版本报错问题
## 0.0.72025-02-06
- fix: 修复hbx 4.51版本报错问题
## 0.0.62025-01-14
- fix: 修复uniapp x ios报错问题
## 0.0.52024-12-16
- chore: 更新文档
## 0.0.42024-07-25
- chore: 更新文档
## 0.0.32024-07-05
- feat: 增加 缓存key `uVueI18nLocale`,可通过`uni.getStorageSync('uVueI18nLocale')`获取上次缓存语言,保持应用退出再启动语言不变
- fix: 修复 ios 报错的问题
## 0.0.22024-06-30
- feat: 增加`$t`插值功能
- feat: 增加`$tc`复数功能
- feat: 增加`tabbar`规则
## 0.0.12024-06-25
- init

View File

@@ -0,0 +1,642 @@
import BaseFormatter from './format'
import { warn, error, isString, getAllKeys } from './util'
import { Composer, Interpolate, Link, WarnDefault, LinkedModify, PluralizationRule, StringOrNull, NumberOrNull, Availabilities } from './types'
// #ifndef APP
type Interceptor = any
// #endif
export class AvailabilitiesImpl implements Availabilities {
dateTimeFormat : boolean = false
numberFormat : boolean = false
constructor() {
// #ifndef APP
const intlDefined = typeof Intl !== 'undefined'
this.dateTimeFormat = intlDefined && typeof Intl.DateTimeFormat !== 'undefined'
this.numberFormat = intlDefined && typeof Intl.NumberFormat !== 'undefined'
// #endif
}
}
const htmlTagMatcher = /<\/?[\w\s="/.':;#-\/]+>/;
const linkKeyMatcher = /(?:@(?:\.[a-zA-Z0-9_-]+)?:)(?:[\w\-_|./]+|\([\w\-_:|./]+\)|(?:\{[^}]+?\}))/g;
const linkKeyPrefixMatcher = /^@(?:\.([a-zA-Z]+))?:/;
const bracketsMatcher = /[()\{\}\']/g;
const defaultModifiers : Map<string, LinkedModify> = new Map([
['upper', (str : string) : string => str.toLocaleUpperCase()],
['lower', (str : string) : string => str.toLocaleLowerCase()],
['capitalize', (str : string) : string => `${str.charAt(0).toLocaleUpperCase()}${str.substring(1)}`]
])
const DEFAULT_LOCALE = "en-US"
const defaultFormatter = new BaseFormatter()
const availabilities = new AvailabilitiesImpl()
function setTabBarItems(tabbar : string[] | null) {
if (tabbar == null) return
const pages = getCurrentPages()
const page = pages.length > 0 ? pages[pages.length - 1]: null;
// @ts-ignore
// #ifndef APP-ANDROID
const isTabBar = page != null //page.$vm.$basePage.openType == 'switchTab'// page != null && page.$page.meta.isTabBar
// #endif
// #ifdef APP-ANDROID
const isTabBar = page != null
// #endif
if(!isTabBar) return
tabbar.forEach((text, index) => {
uni.setTabBarItem({
text,
index,
// success() {},
fail(err) {
warn(err.errMsg)
}
} as SetTabBarItemOptions)
})
}
function getLocaleMap(locale : string, key : string, options : UTSJSONObject, root : Composer | null = null) : Map<string, UTSJSONObject> {
//'messages'
const __messages = UTSJSONObject.assign({}, options.getJSON(key) ?? {})
// #ifdef APP
let map = new Map<string, UTSJSONObject>()
__messages.toMap().forEach((value, key) => {
if (value instanceof UTSJSONObject) {
map.set(key, value)
}
})
// #endif
// #ifndef APP
let map = __messages.toMap()
// #endif
if (map.size == 0 && root != null) {
// map = root.messages.value
if (!map.has(locale)) {
map.set(locale, {})
}
}
return map
}
function getLocaleTabbarMap(locale : string, key : string, options : UTSJSONObject) : Map<string, string[]> {
const __messages = options.getJSON(key) ?? {}
let map = new Map<string, string[]>()
__messages.toMap().forEach((tabbar, key) => {
if (Array.isArray(tabbar)) {
map.set(key, tabbar as string[]);
if (key == locale) {
setTimeout(()=>{
setTabBarItems(tabbar as string[])
},500)
}
}
})
return map
}
function getModifiers(options : UTSJSONObject) : Map<string, LinkedModify> {
const __modifiers = (options.getJSON('modifiers') ?? {}).toMap();
const _modifiers = new Map<string, LinkedModify>()
__modifiers.forEach((value, key) => {
if (typeof value == 'function') {
try {
_modifiers.set(key, value as LinkedModify)
} catch (e) {
error(35, '自定义修饰器函数必须是类型:(str: string) => string')
}
}
})
return _modifiers
}
function getPluralizationRules(options : UTSJSONObject) : Map<string, PluralizationRule> {
const __pluralizationRules = (options.getJSON('pluralizationRules') ?? {}).toMap()
const _pluralizationRules = new Map<string, PluralizationRule>()
__pluralizationRules.forEach((value, key) => {
if (typeof value == 'function') {
try {
_pluralizationRules.set(key, value as PluralizationRule)
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
error(35, '自定义复数化规则函数必须是类型: ( choice: number, choicesLength: number) => number')
}
}
}
})
return _pluralizationRules
}
function getFormatter(options : UTSJSONObject) : BaseFormatter {
const __formatter = options.get('formatter')
return __formatter != null && __formatter instanceof BaseFormatter ? __formatter : defaultFormatter;
}
let composerID = 0;
/**
* 创建一个Composer实例用于处理国际化信息。
* @param {UTSJSONObject} [options={}] - 配置对象,包含语言环境、格式化器等设置。
* @param {Composer | null} [__root=null] - 根Composer实例用于继承语言环境等信息。
* @returns {Composer} 返回一个新的Composer实例。
*/
export function createComposer(options : UTSJSONObject = {}, __root : Composer | null = null) : Composer {
let _interpolate : Interpolate | null = null;
let _link : Link | null;
let _warnDefault : WarnDefault | null = null;
const _inheritLocale = options.getBoolean('inheritLocale') ?? true;
const _formatter = getFormatter(options);
const _modifiers = getModifiers(options)
const _pluralizationRules = getPluralizationRules(options)
// const flatJson = options.getBoolean('flatJson') ?? false;
const useRoot = __root != null && _inheritLocale
const __locale = ref<string>(
useRoot
? __root!.locale.value
: options.getString('locale') ?? DEFAULT_LOCALE
)
const _fallbackLocale = ref<any | null>(
useRoot
? __root!.fallbackLocale.value
: options.get('fallbackLocale')
)
const _messages = ref<Map<string, UTSJSONObject>>(getLocaleMap(__locale.value, 'messages', options, __root))
const _numberFormats = ref<Map<string, UTSJSONObject>>(getLocaleMap(__locale.value, 'numberFormats', options, __root))
const _datetimeFormats = ref<Map<string, UTSJSONObject>>(getLocaleMap(__locale.value, 'datetimeFormats', options, __root))
const _tabBars = ref<Map<string, string[]>>(getLocaleTabbarMap(__locale.value, 'tabBars', options))
const _locale = computed<string>({
set(val : string) {
__locale.value = val;
// 设置缓存 只有全局才会缓存
if (__root == null) {
uni.setStorageSync('uVueI18nLocale', val)
}
// 设置tabbar
setTabBarItems(_tabBars.value.get(val))
},
get() : string {
return __locale.value
}
} as WritableComputedOptions<string>)
const fallbackLocale = computed<any>({
set(val : any) {
_fallbackLocale.value = val
},
get() : any {
return _fallbackLocale.value ?? false
}
} as WritableComputedOptions<any>)
let availableLocales : string[] = getAllKeys(_messages.value).sort()
/**
* 处理字符串中的链接并返回翻译后的字符串。
* @param {string} str - 要处理的字符串。
* @param {StringOrNull} [locale=null] - 指定语言环境。
* @param {any} values - 用于插值的变量。
* @param {string[]} visitedLinkStack - 已访问过的链接堆栈。
* @param {string} interpolateMode - 插值模式。
* @returns {StringOrNull} 返回处理后的字符串或null。
*/
_link = (str : string, locale : StringOrNull, values : any, visitedLinkStack : string[], interpolateMode : string) : StringOrNull => {
const matches = str.match(linkKeyMatcher)
let ret : string = str
if (matches == null) return str
for (let i = 0; i < matches.length; i++) {
const link = matches[i]
const linkKeyPrefixMatches = link!.match(linkKeyPrefixMatcher)
if (linkKeyPrefixMatches == null) continue;
const [linkPrefix, formatterName] = linkKeyPrefixMatches
// 去掉字符串前面的 @:、@.case: 、括号及大括号
const linkPlaceholder : string = link.replace(linkPrefix!, '').replace(bracketsMatcher, '')
if (visitedLinkStack.includes(linkPlaceholder)) {
warn(`发现循环引用。"${link}"已经在link"已经在${visitedLinkStack.reverse().join(' <- ')}链中访问过`)
return ret
}
if (_interpolate == null || _warnDefault == null) {
return ret
}
visitedLinkStack.push(linkPlaceholder)
let translated = _interpolate!(linkPlaceholder, locale, values, visitedLinkStack, interpolateMode)
translated = _warnDefault!(linkPlaceholder, translated, values, interpolateMode)
// 如果有自定义_modifiers 否则使用默认defaultModifiers
if (_modifiers.size > 0 && formatterName != null && _modifiers.has(formatterName)) {
} else if (translated != null && formatterName != null && defaultModifiers.has(formatterName)) {
const modifier = defaultModifiers.get(formatterName) as LinkedModify
translated = modifier(translated)
}
visitedLinkStack.pop()
// 将链接替换为已翻译的
ret = translated == null ? ret : ret.replace(link, translated)
}
return ret
}
/**
* 获取指定语言字符。
* @param {string} key - 要翻译的键。
* @param {StringOrNull} [locale=null] - 指定语言环境。
* @param {any} values - 用于插值的变量。
* @param {string[]} visitedLinkStack - 已访问过的链接堆栈。
* @param {string} interpolateMode - 插值模式。
* @returns {StringOrNull} 返回翻译后的字符串或null。
*/
_interpolate = (key : string, locale : StringOrNull, values : any, visitedLinkStack : string[], interpolateMode : string) : StringOrNull => {
const ___locale = locale ?? _locale.value
let ret = _messages.value.get(___locale)?.getString(key)
// console.log(_messages)
// console.log(key,ret)
if (fallbackLocale.value != false && ret == null) {
if (typeof fallbackLocale.value == 'string' && ___locale != fallbackLocale.value) {
ret = _messages.value.get(fallbackLocale.value as string)?.getString(key) ?? ret
} else if (Array.isArray(fallbackLocale.value)) {
const arr = (fallbackLocale.value as string[])
for (let i = 0; i < arr.length; i++) {
const _ret = _messages.value.get(arr[i])?.getString(key)
if (_ret != null) {
ret = _ret
break;
}
}
}
}
// 检查翻译后的字符串中是否存在链接
if (typeof ret == 'string' && (ret!.indexOf('@:') >= 0 || ret!.indexOf('@.') >= 0)) {
// @ts-ignore
ret = _link(ret!, locale, values, visitedLinkStack, interpolateMode)
}
return ret
}
/**
* 获取指定语言字符并渲染。
* @param {string} message - 要翻译的字符串。
* @param {any} values - 用于插值的变量。
* @param {string} interpolateMode - 插值模式。
* @returns {string} 返回渲染后的字符串。
*/
const _render = (message : string, values : any, interpolateMode : string) : string => {
const ret = _formatter.interpolate(message, values)
return interpolateMode == 'string' ? `${ret.join('')}` : JSON.stringify(ret)
}
/**
* 在无法翻译的情况下发出警告并提供默认值。
* @param {string} key - 要翻译的键。
* @param {StringOrNull} message - 翻译后的字符串或null。
* @param {any} values - 用于插值的变量。
* @param {string} interpolateMode - 插值模式。
* @returns {StringOrNull} 返回警告信息或默认值。
*/
_warnDefault = (key : string, message : StringOrNull, values : any, interpolateMode : string) : StringOrNull => {
if (message == null) {
warn(`无法翻译键路径 '${key}'. ` + '使用键路径的值作为默认值.')
}
if (message == null) return null
if (key == message) return key
return _render(message, values, interpolateMode)
}
/**
* 获取复数形式的选择。
* @param {string} message - 包含复数选择的字符串。
* @param {number | null} [choice=null] - 复数形式的选择。
* @param {string | null} [locale=null] - 指定语言环境。
* @returns {string} 返回选择后的字符串。
*/
const fetchChoice = (message : string, choice ?: number, locale ?: string) : string => {
if (message == '') return message;
const choices : Array<string> = message.split('|');
// 默认 vue-i18ngetChoiceIndex实现 - 兼容英文
const defaultImpl = (_choice : NumberOrNull, _choicesLength : number) : number => {
_choice = Math.abs(_choice ?? 1)
if (_choicesLength == 2) {
return _choice != 0
? _choice > 1
? 1
: 0
: 1
}
return _choice != 0 ? Math.min(_choice, 2) : 0
}
let index : number;
if (_pluralizationRules.has(locale ?? _locale.value)) {
index = _pluralizationRules.get(locale ?? _locale.value)!(choice ?? 1, choices.length)
} else {
index = defaultImpl(choice, choices.length)
}
if (choices[index] == '') return message
return choices[index].trim()
}
/**
* 翻译指定的键。
* @param {string} key - 要翻译的键。
* @param {any} [values=null] - 用于插值的变量。
* @param {string | null} [locale=null] - 指定语言环境。
* @returns {string} 返回翻译后的字符串。
*/
const t = (key : string, values ?: any, locale ?: string) : string => {
const parsedArgs = values ?? {}
// #ifndef APP
if (_warnDefault == null || _interpolate == null) return ''
// #endif
const msg = _warnDefault(
key,
_interpolate(
key,
locale,
parsedArgs,
[key],
'string'),
parsedArgs,
'string'
)
return msg ?? ''
}
/**
* 翻译指定的键并获取复数形式的选择。
* @param {string} key - 要翻译的键。
* @param {number | null} [choice=null] - 复数形式的选择。
* @param {any} [values=null] - 用于插值的变量。
* @param {string | null} [locale=null] - 指定语言环境。
* @returns {string} 返回翻译后的复数形式选择字符串。
*/
const tc = (key : string, choice ?: number, values ?: any, locale ?: string) : string => {
// 预定义的count和n参数
const _obj = { 'count': choice, 'n': choice }
const predefined = values == null
? _obj
: values instanceof UTSJSONObject
? UTSJSONObject.assign(_obj, values as UTSJSONObject)
: values;
return fetchChoice(t(key, predefined, locale), choice, locale)
}
/**
* 格式化日期。
* @param {any} date - 要格式化的日期。
* @param {StringOrNull} [key=null] - 日期格式化的键。
* @param {StringOrNull} [locale=null] - 指定语言环境。
* @param {UTSJSONObject | null} [options=null] - 日期格式化的选项。
* @returns {string} 返回格式化后的日期字符串。
*/
const d = (date : any, key : StringOrNull, locale : StringOrNull, options : UTSJSONObject | null) : string => {
if (!availabilities.dateTimeFormat) {
warn('无法格式化日期值,因为不支持 Intl.DateTimeFormat. ' + `key: ${key}, locale: ${locale}, options: ${options}`)
return `${date}`
}
// #ifndef APP
const __locale = locale ?? _locale.value
if (key == null) {
// @ts-ignore
const dtf = options == null ? new Intl.DateTimeFormat(__locale) : new Intl.DateTimeFormat(__locale, options)
return dtf.format(date)
}
const formats = _datetimeFormats.value!.get(__locale)
let formatter;
if (formats == null || formats!.getJSON(key) == null) {
warn(`回退到根号下的日期时间本地化key '${key}'。`)
return `${date}`
}
const format = formats!.getJSON(key) ?? {}
if (options != null) {
// @ts-ignore
formatter = new Intl.DateTimeFormat(__locale, Object.assign({}, format, options))
} else {
// @ts-ignore
formatter = new Intl.DateTimeFormat(__locale, format)
}
return formatter.format(date)
// #endif
return `${date}`
}
/**
* 格式化数字。
* @param {number} number - 要格式化的数字。
* @param {StringOrNull} [key=null] - 数字格式化的键。
* @param {StringOrNull} [locale=null] - 指定语言环境。
* @param {UTSJSONObject | null} [options=null] - 数字格式化的选项。
* @returns {string} 返回格式化后的数字字符串。
*/
const n = (number : number, key : StringOrNull, locale : StringOrNull, options : UTSJSONObject | null) : string => {
if (!availabilities.numberFormat) {
warn('无法格式化数字值,因为不支持 Intl.NumberFormat. ' + `key: ${key}, locale: ${locale}, options: ${options}`)
return number.toString()
}
// #ifndef APP
const __locale = locale ?? _locale.value
if (key == null) {
// @ts-ignore
const nf = options == null ? new Intl.NumberFormat(__locale) : new Intl.NumberFormat(locale, options)
return nf.format(number)
}
const formats = _numberFormats.value!.get(__locale)
let formatter;
if (formats == null || formats!.getJSON(key) == null) {
warn(`回退到根号下的数字本地化key '${key}'`)
return number.toString()
}
const format = formats!.getJSON(key)
if (options != null) {
// @ts-ignore
formatter = new Intl.NumberFormat(__locale, Object.assign({}, format, options))
} else {
// @ts-ignore
formatter = new Intl.NumberFormat(__locale, format)
}
if (formatter) {
return formatter.format(number)
}
// #endif
return number.toString()
}
/**
* 设置语言环境的locale信息。
* @param {string} locale - 语言。
* @param {UTSJSONObject} message - locale信息。
*/
const setLocaleMessage = (locale : string, message : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_messages.value.forEach((value, key) => {
map.set(key, value)
})
map.set(locale, message)
_messages.value = map
availableLocales = getAllKeys(map).sort()
}
/**
* 获取语言环境的locale信息。
* @param {string} locale - 语言。
* @returns {UTSJSONObject} - locale信息。
*/
const getLocaleMessage = (locale : string) : UTSJSONObject => {
return _messages.value.get(locale) ?? {}
}
/**
* 将语言环境信息locale合并到已注册的语言环境信息中。
* @param {string} locale - 语言。
* @param {UTSJSONObject} message - locale信息。
*/
const mergeLocaleMessage = (locale : string, message : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_messages.value.forEach((value, key) => {
if (key == locale) {
map.set(key, UTSJSONObject.assign({}, value, message))
} else {
map.set(key, value)
}
})
_messages.value = map
availableLocales = getAllKeys(map).sort()
}
/**
* 设置日期时间格式。
* @param {string} locale - 语言。
* @param {UTSJSONObject} format - 日期时间格式。
*/
const setDateTimeFormat = (locale : string, format : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_datetimeFormats.value.forEach((value, key) => {
map.set(key, value)
})
map.set(locale, format)
_datetimeFormats.value = map
}
/**
* 获取日期时间格式。
* @param {string} locale - 语言。
* @returns {UTSJSONObject} - 日期时间格式。
*/
const getDateTimeFormat = (locale : string) : UTSJSONObject => {
return _datetimeFormats.value.get(locale) ?? {}
}
/**
* 合并日期时间格式到已注册的日期时间格式中。
* @param {string} locale - 语言。
* @param {UTSJSONObject} format - 日期时间格式。
*/
const mergeDateTimeFormat = (locale : string, format : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_datetimeFormats.value.forEach((value, key) => {
if (key == locale) {
map.set(key, UTSJSONObject.assign({}, value, format))
} else {
map.set(key, value)
}
})
_datetimeFormats.value = map
}
/**
* 设置数字格式。
* @param {string} locale - 语言。
* @param {UTSJSONObject} format - 数字格式。
*/
const setNumberFormat = (locale : string, format : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_numberFormats.value.forEach((value, key) => {
map.set(key, value)
})
map.set(locale, format)
_numberFormats.value = map
}
/**
* 获取数字格式。
* @param {string} locale - 语言。
* @returns {UTSJSONObject} - 数字格式。
*/
const getNumberFormat = (locale : string) : UTSJSONObject => {
return _numberFormats.value.get(locale) ?? {}
}
/**
* 合并数字格式到已注册的数字格式中。
* @param {string} locale - 语言。
* @param {UTSJSONObject} format - 数字格式。
*/
const mergeNumberFormat = (locale : string, format : UTSJSONObject) => {
const map = new Map<string, UTSJSONObject>();
_numberFormats.value.forEach((value, key) => {
if (key == locale) {
map.set(key, UTSJSONObject.assign({}, value, format))
} else {
map.set(key, value)
}
})
_numberFormats.value = map
}
/**
* 设置TabBar。
* @param {string} locale - 语言。
* @param {string[]} tabbar - TabBar项目。
*/
const setTabBar = (locale : string, tabbar : string[]) => {
const map = new Map<string, string[]>();
_tabBars.value.forEach((value, key) => {
map.set(key, value)
})
map.set(locale, tabbar)
_tabBars.value = map
}
/**
* 获取TabBar。
* @param {string} locale - 语言。
* @returns {string[]} - TabBar项目。
*/
const getTabBar = (locale : string) : string[] => {
return _tabBars.value.get(locale) ?? []
}
composerID++;
const interceptor = {
complete: (_ : NavigateToComplete) => {
setTimeout(()=>{
setTabBarItems(_tabBars.value.get(_locale.value))
},50)
}
} as Interceptor
if(__root == null) {
uni.addInterceptor('switchTab', interceptor);
}
const composer : Composer = {
id: composerID,
locale: _locale,
fallbackLocale,
messages: _messages,
setLocaleMessage,
getLocaleMessage,
mergeLocaleMessage,
setDateTimeFormat,
getDateTimeFormat,
mergeDateTimeFormat,
setNumberFormat,
getNumberFormat,
mergeNumberFormat,
setTabBar,
getTabBar,
t,
tc,
d,
n,
availableLocales,
availabilities
}
return composer
}

View File

@@ -0,0 +1,60 @@
type I18nErrorCodesTypes = {
UNEXPECTED_RETURN_TYPE: number
INVALID_ARGUMENT: number
MUST_BE_CALL_SETUP_TOP: number
NOT_INSTALLED: number
REQUIRED_VALUE: number
INVALID_VALUE: number
CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: number
NOT_INSTALLED_WITH_PROVIDE: number
UNEXPECTED_ERROR: number
NOT_COMPATIBLE_LEGACY_VUE_I18N: number
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: number
TYPE_MISMATCH: number
}
export const I18nErrorCodes: I18nErrorCodesTypes = {
// composer模块错误
UNEXPECTED_RETURN_TYPE: 24,
// legacy模块错误
INVALID_ARGUMENT: 25,
// i18n模块错误
MUST_BE_CALL_SETUP_TOP: 26,
NOT_INSTALLED: 27,
// directive模块错误
REQUIRED_VALUE: 28,
INVALID_VALUE: 29,
// vue-devtools错误
CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: 30,
NOT_INSTALLED_WITH_PROVIDE: 31,
// 意外错误
UNEXPECTED_ERROR: 32,
// 不兼容的旧版vue-i18n构造函数
NOT_COMPATIBLE_LEGACY_VUE_I18N: 33,
// 在旧版API模式下Compostion API不可用。请确保旧版API模式正常工作
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: 34,
// 类型不匹配
TYPE_MISMATCH: 35
}
export const errorMessages: Map<number, string> = new Map<number, string>([
[I18nErrorCodes.UNEXPECTED_RETURN_TYPE, 'composer中返回类型异常'],
[I18nErrorCodes.INVALID_ARGUMENT, '参数无效'],
[I18nErrorCodes.MUST_BE_CALL_SETUP_TOP, '必须在`setup`函数的顶部调用'],
[I18nErrorCodes.NOT_INSTALLED, '需要用`app.use`函数安装'],
[I18nErrorCodes.UNEXPECTED_ERROR, '意外错误'],
[I18nErrorCodes.REQUIRED_VALUE, `值中必需,{0}`],
[I18nErrorCodes.INVALID_VALUE, `值无效`],
[I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN, `无法设置vue-devtools插件`],
[I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE, '需要用`provide`函数安装'],
[I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N, '不兼容的旧版VueI18n。'],
[I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY, '在旧版API模式下Compostion API不可用。请确保旧版API模式正常工作'],
[I18nErrorCodes.TYPE_MISMATCH, '类型不匹配']
])
// export function createI18nError(code: number, msg?: string) {
// if(process.env.NODE_ENV !== 'production') {
// console.warn(`[vue-i18n] : ${msg ?? errorMessages.get(code)}`)
// }
// new Error(errorMessages.get(code) ?? 'code error')
// }

View File

@@ -0,0 +1,149 @@
// @ts-nocheck
import { warn, isObject } from './util'
type Token = {
type : 'text' | 'named' | 'list' | 'unknown',
value : string
}
const RE_TOKEN_LIST_VALUE = /^(?:\d)+/
const RE_TOKEN_NAMED_VALUE = /^(?:\w)+/
/**
* 解析格式化字符串并生成一个包含标记Token的数组。
* 这些标记可以是文本、列表或命名值。
*
* @param {string} format - 需要解析的格式化字符串。
* @returns {Array<Token>} 返回一个包含解析后的标记的数组。
*/
export function parse(format : string) : Array<Token> {
const tokens : Array<Token> = []
let position : number = 0
let text : string = ''
while (position < format.length) {
let char : string = format.charAt(position++)
if (char == '{') {
if (text.length > 0) {
const token : Token = { type: 'text', value: text }
tokens.push(token)
}
text = ''
let sub : string = ''
char = format.charAt(position++)
while (char != '}') {
sub += char
char = format.charAt(position++)
}
const isClosed = char == '}'
const type = RE_TOKEN_LIST_VALUE.test(sub)
? 'list'
: isClosed && RE_TOKEN_NAMED_VALUE.test(sub)
? 'named'
: 'unknown'
const token : Token = { type, value: sub }
tokens.push(token)
} else if (char == '%') {
// when found rails i18n syntax, skip text capture
if (format.charAt(position) != '{') {
text += char
}
} else {
text += char
}
}
if (text.length > 0) {
const token : Token = { type: 'text', value: text }
tokens.push(token)
}
return tokens
}
/**
* 根据给定的标记数组和值对象或数组,编译出相应的值数组。
*
* @param {Array<Token>} tokens - 标记数组,包含文本、列表和命名值。
* @param {Object | Array<any>} values - 值对象或数组,用于替换标记中的占位符。
* @returns {Array<any>} 返回编译后的值数组。
*/
function compile(tokens : Array<Token>, values : UTSJSONObject) : Array<any>
function compile(tokens : Array<Token>, values : Array<any>) : Array<any>
function compile(tokens : Array<Token>, values : any) : Array<any> {
const compiled : Array<any> = []
let index : number = 0;
const mode : string = Array.isArray(values)
? 'list'
: isObject(values)
? 'named'
: 'unknown'
if (mode == 'unknown') {
return compiled
}
while (index < tokens.length) {
const token : Token = tokens[index]
switch (token.type) {
case 'text':
compiled.push(token.value)
break
case 'list':
const index = parseInt(token.value, 10)
if(mode == 'list') {
const value = (values as any[])[index]
compiled.push(value)
} else {
if (process.env.NODE_ENV !== 'production') {
warn('list did not receive a valid values array')
}
}
break
case 'named':
if (mode == 'named') {
const value = (values as UTSJSONObject)[token.value] ?? ''
compiled.push(`${value}`)
} else {
if (process.env.NODE_ENV !== 'production') {
warn(`Type of token '${token.type}' and format of value '${mode}' don't match!`)
}
}
break
case 'unknown':
if(token.value.startsWith("'") && token.value.endsWith("'")) {
compiled.push(token.value.slice(1, -1))
} else if (process.env.NODE_ENV !== 'production') {
warn(`Detect 'unknown' type of token!`)
}
break
}
index++
}
return compiled
}
export {compile}
export default class BaseFormatter {
private _caches : Map<string, Token[]>
constructor() {
this._caches = new Map<string, Token[]>()
}
interpolate(message : string, values : any | null) : any[] {
if (values == null) {
return [message]
}
let tokens : Array<Token> | null = this._caches.get(message)
if (tokens == null) {
tokens = parse(message)
this._caches.set(message, tokens)
}
return compile(tokens, values)
}
}

View File

@@ -0,0 +1,79 @@
import { createComposer } from './composer'
import { error, warn, getAllKeys } from './util'
import { I18nErrorCodes } from './errors'
import { AnyOrNull, NumberOrNull, StringOrNull, Composer } from './types'
type I18nMode = "legacy" | "composition"
// #ifndef APP
type VuePlugin = any
// #endif
let i18n : UvueI18n | null = null
class UvueI18n {
private __global : Composer
private __scope : EffectScope
constructor(options : UTSJSONObject = {}, root : Composer | null = null) {
this.__scope = effectScope()
this.__global = this.__scope.run(() : Composer => createComposer(UTSJSONObject.assign({}, options), root))!
}
get mode() : I18nMode {
return "composition"
}
get global() : Composer {
return this.__global
}
get availableLocales():string[] {
return getAllKeys(this.global.messages.value).sort()
}
dispose() {
this.__scope.stop()
}
get install() : VuePlugin {
const _install = (app : VueApp) => {
app.config.globalProperties.$i18n = i18n!
app.config.globalProperties.$t = function (key : string, values : AnyOrNull = null, locale : StringOrNull = null) : string {
const isLocale = typeof values == 'string'
const _values = isLocale ? null : values
const _locale = isLocale ? values as string : locale
return i18n!.global.t(key, _values, _locale)
}
app.config.globalProperties.$tc = function (key : string, choice : NumberOrNull = null, values : AnyOrNull = null, locale : StringOrNull = null) : string {
const isLocale = typeof values == 'string'
const _values = isLocale ? null : values
const _locale = isLocale ? values as string : locale
return i18n!.global.tc(key, choice, _values, _locale)
}
app.config.globalProperties.$d = function(date: any, key: StringOrNull = null, locale : StringOrNull = null, options: UTSJSONObject | null = null):string {
return i18n!.global.d(date, key, locale, options)
}
app.config.globalProperties.$n = function(number: number, key: StringOrNull = null, locale : AnyOrNull = null, options: UTSJSONObject | null = null):string {
const _locale = typeof locale == 'string' ? locale as string : null
const _options = typeof locale == 'object' && locale != null ? locale as UTSJSONObject : options
return i18n!.global.n(number, key, _locale, _options)
}
app.config.globalProperties.$locale = i18n!.global.locale
}
// #ifdef APP-ANDROID
return definePlugin({
install: _install
})
// #endif
// #ifndef APP-ANDROID
return _install
// #endif
}
}
export function createI18n(options : UTSJSONObject = {}) : UvueI18n {
// const __legacyMode = true
i18n = new UvueI18n(options)
return i18n!
}
export function useI18n(options : UTSJSONObject = {}) : Composer {
const instance = getCurrentInstance()
if (instance == null) {
error(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP)
}
return new UvueI18n(options, i18n!.global).global
}

View File

@@ -0,0 +1,29 @@
console.log('i18n format test:::::::::::::::::::')
import { parse, compile } from '../format'
function appTest() {
// 示例1文本插值
const tokens = parse('Hello, {name}!')
const values = { name: 'Alice' }
console.log('tokens app', tokens)
console.log('compile app', compile(tokens, values)) // 输出:['Hello, ', 'Alice', '!']
// 示例2列表插值
const tokens2 = parse('The {0}st person is {1}.')
const values2 = ['first', 'Alice']
console.log('tokens2 app', tokens2)
console.log('compile2 app', compile(tokens2, values2))
// 示例3混合插值
const tokens3 = parse('The {0}st person is {name}.')
const values3 = ['first', { name: 'Alice' }]
console.log('tokens3 app', tokens3)
console.log('compile2 app',compile(tokens3, values3)) // 输出:['The ', 'first', 'st person is ', 'Alice', '.']
// 示例4未知类型
const tokens4 = parse('Hello, {unknown}!')
const values4 = { name: 'Alice' }
console.log('tokens4 web', tokens4)
console.log('compile4 web',compile(tokens4, values4)) // 输出:['Hello, ', 'unknown', '!']
}
appTest()

View File

@@ -0,0 +1,3 @@
// import * as utils from './utils.uts';
// import * as format from './format.uts';
console.log('i18n test:::::::::::::::::::')

View File

@@ -0,0 +1,29 @@
console.log('i18n utils test:::::::::::::::::::')
import { warn, error, isObject, isBoolean, isString, isPlainObject, isNull, isFunction, parseArgs, arrayFrom, hasOwn, merge, looseEqual } from '../util'
console.log('warn', warn('test warn'))
console.log('error', error('test error'))
console.log('isArray', isArray('test isArray'))
console.log('isObject', isObject({}))
console.log('isBoolean', isBoolean(false))
console.log('isString', isString('false'))
console.log('isPlainObject', isPlainObject({}))
console.log('isNull', isNull(null))
console.log('isFunction', isFunction(null))
console.log('parseArgs', parseArgs(1,2,23,5))
console.log('parseArgs', parseArgs('zh-CN'))
console.log('parseArgs', parseArgs({ a: 1, b: 2 }))
console.log('parseArgs', parseArgs('zh-CN', { a: 1, b: 2 }))
console.log('parseArgs', parseArgs({ a: 1, b: 2 }, 'zh-CN'))
console.log('arrayFrom', arrayFrom(new Set([1, 2, 3, 4, 5])))
console.log('hasOwn', hasOwn({ a: { b: 2 }, c: 3 }, 'a'))
console.log('hasOwn', hasOwn({ a: { b: 2 }, c: 3 }, 'd'))
console.log('merge', merge({ a: { b: 2 }, c: 3 }, { b: 2 }))
console.log('looseEqual', looseEqual(123,123))
console.log('looseEqual', looseEqual('hello','hello'))
console.log('looseEqual', looseEqual([1, 2, 3],[1, 2, 3]))
console.log('looseEqual', looseEqual([1, 2, 3],[1, 2, 4]))
console.log('looseEqual', looseEqual({},[1, 2, 4]))
console.log('looseEqual', looseEqual({},{}))
console.log('looseEqual', looseEqual({},{a:1}))
console.log('looseEqual', looseEqual({a:1},{a:1}))

View File

@@ -0,0 +1,53 @@
// #ifndef APP
import { ComputedRef, WritableComputedRef } from 'vue'
type ComputedRefImpl<T> = WritableComputedRef<T>;
// #endif
export type AnyOrNull = any | null;
export type NumberOrNull = number | null;
export type StringOrNull = string | null;
// 定义特定的函数类型别名
export type Interpolate = (key : string, locale : StringOrNull, values : any, visitedLinkStack : string[], interpolateMode : string) => StringOrNull;
export type Link = (str : string, locale : StringOrNull, values : any, visitedLinkStack : string[], interpolateMode : string) => StringOrNull;
export type WarnDefault = (key : string, message : StringOrNull, values : any, interpolateMode : string) => StringOrNull;
export type LinkedModify = (str : string) => string;
export type PluralizationRule = (choice : number, choicesLength : number) => number
export interface Availabilities {
dateTimeFormat : boolean
numberFormat : boolean
}
export type Composer = {
id : number,
locale : Ref<string>,
fallbackLocale : ComputedRefImpl<any>,
messages : Ref<Map<string, UTSJSONObject>>,
t(key : string, values ?: any, locale ?: string) : string,
tc(key : string, choice ?: number, values ?: any, locale ?: string) : string,
d(date : any, key : StringOrNull, locale : StringOrNull, options : UTSJSONObject | null) : string
n(number : number, key : StringOrNull, locale : StringOrNull, options : UTSJSONObject | null) : string
setLocaleMessage(locale : string, message : UTSJSONObject) : void,
getLocaleMessage(locale : string) : UTSJSONObject,
mergeLocaleMessage(locale : string, message : UTSJSONObject) : void,
setDateTimeFormat(locale : string, format : UTSJSONObject) : void,
getDateTimeFormat(locale : string) : UTSJSONObject,
mergeDateTimeFormat(locale : string, format : UTSJSONObject) : void,
setNumberFormat(locale : string, format : UTSJSONObject) : void,
getNumberFormat(locale : string) : UTSJSONObject,
mergeNumberFormat(locale : string, format : UTSJSONObject) : void,
setTabBar(locale : string, tabbar : string[]) : void,
getTabBar(locale : string) : string[],
// 可用的语言环境列表。
availableLocales : string[],
// 可用的功能列表。
availabilities : Availabilities
}

View File

@@ -0,0 +1,371 @@
// @ts-nocheck
/* @flow */
/**
* constants
*/
import { errorMessages } from './errors'
import { warnMessages } from './warnings'
export const numberFormatKeys = [
'compactDisplay',
'currency',
'currencyDisplay',
'currencySign',
'localeMatcher',
'notation',
'numberingSystem',
'signDisplay',
'style',
'unit',
'unitDisplay',
'useGrouping',
'minimumIntegerDigits',
'minimumFractionDigits',
'maximumFractionDigits',
'minimumSignificantDigits',
'maximumSignificantDigits'
]
export const dateTimeFormatKeys = [
'dateStyle',
'timeStyle',
'calendar',
'localeMatcher',
"hour12",
"hourCycle",
"timeZone",
"formatMatcher",
'weekday',
'era',
'year',
'month',
'day',
'hour',
'minute',
'second',
'timeZoneName',
]
/**
* utilities
*/
export function getAllKeys(map:Map<string, UTSJSONObject>):string[] {
let keys:string[] = []
map.forEach((_, key) => {
keys.push(key)
})
return keys
}
/**
* 打印警告信息
* @param {string} msg - 警告信息
* @param {Error} err - 可选的错误对象
*/
export function warn(msg : string, code:number = -1) {
if(process.env.NODE_ENV !== 'production') {
console.warn(`[uvue-i18n] : ${code!=-1?warnMessages.get(code):msg}`)
}
}
/**
* 打印错误信息
* @param {string} msg - 错误信息
* @param {Error} err - 可选的错误对象
*/
export function error(code: number, msg : string|null = null) {
if(process.env.NODE_ENV !== 'production') {
console.error(`[uvue-i18n] : ${msg ?? errorMessages.get(code)}`)
}
}
export function isArray(value : any) : boolean {
return Array.isArray(value)
}
/**
* 判断一个值是否为对象
* @param {mixed} obj - 需要判断的值
* @returns {boolean} - 如果值为对象,则返回 true否则返回 false
*/
export function isObject(obj : any | null) : boolean {
return obj != null && typeof obj == 'object'
}
/**
* 判断一个值是否为布尔值
* @param {mixed} val - 需要判断的值
* @returns {boolean} - 如果值为布尔值,则返回 true否则返回 false
*/
export function isBoolean(val : any) : boolean {
return typeof val == 'boolean'
}
/**
* 判断一个值是否为字符串
* @param {mixed} val - 需要判断的值
* @returns {boolean} - 如果值为字符串,则返回 true否则返回 false
*/
export function isString(val : any) : boolean {
return typeof val == 'string'
}
/**
* 判断一个值是否为普通对象
* @param {any} obj - 需要判断的值
* @returns {boolean} - 如果值为普通对象,则返回 true否则返回 false
*/
export function isPlainObject(obj : any) : boolean {
// #ifndef APP-ANDROID || APP-IOS
const toString = Object.prototype.toString
const OBJECT_STRING : string = '[object Object]'
return toString.call(obj) === OBJECT_STRING
// #endif
// #ifdef APP-ANDROID || APP-IOS
return typeof obj == 'object' && obj instanceof UTSJSONObject
// #endif
}
/**
* 判断一个值是否为 null 或 undefined
* @param {mixed} val - 需要判断的值
* @returns {boolean} - 如果值为 null 或 undefined则返回 true否则返回 false
*/
// #ifndef APP-ANDROID || APP-IOS
export function isNull(val : any | null | undefined) : boolean {
return val == null || val == undefined
}
// #endif
// #ifdef APP-ANDROID || APP-IOS
export function isNull(val : any | null) : boolean {
return val == null
}
// #endif
/**
* 判断一个值是否为函数
* @param {mixed} val - 需要判断的值
* @returns {boolean} - 如果值为函数,则返回 true否则返回 false
*/
export function isFunction(val : any) : boolean {
return typeof val == 'function'
}
/**
* 解析参数
* @param {...mixed} args - 输入的参数
* @returns {Object} - 包含 locale 和 params 的对象
*/
export function parseArgs(...args : Array<any>) : Map<string, UTSJSONObject> {
let locale : string | null = null
let params : UTSJSONObject | null = null
if (args.length == 1) {
if (isObject(args[0]) || isArray(args[0]) ) {
params = args[0] //as UTSJSONObject
} else if (typeof args[0] == 'string') {
locale = args[0] as string
}
} else if (args.length == 2) {
if (typeof args[0] == 'string') {
locale = args[0] as string
}
if (isObject(args[1]) || isArray(args[1])) {
params = args[1] //as UTSJSONObject
}
}
if(locale == null || params == null) return new Map<string, UTSJSONObject>()
return new Map([
[locale, params]
])
}
/**
* looseClone 函数用于对一个对象进行浅拷贝。
* 它通过将对象序列化为 JSON 字符串,然后再将其解析回对象来实现这一目的。
* 请注意这种方法仅适用于可序列化的对象不适用于包含循环引用或特殊对象如函数、Date 对象等)的对象。
*
* @param {Object} obj - 需要进行浅拷贝的对象。
* @returns {Object} 返回一个新的对象,它是原始对象的浅拷贝。
*/
export function looseClone(obj : UTSJSONObject) : UTSJSONObject {
return JSON.parse(JSON.stringify(obj))
}
/**
* remove 函数用于从数组中删除指定的元素。
* 如果成功删除元素,则返回修改后的数组;否则,不返回任何值。
*
* @param {Array} arr - 需要操作的数组。
* @param {*} item - 需要删除的元素。
* @returns {Array} 返回修改后的数组,或者不返回任何值。
*/
export function remove(arr : Set<any>, item : any) : Set<any> | null {
if (arr.delete(item)) {
return arr
}
return null
}
/**
* arrayFrom 函数用于将类数组对象(如 Set 集合)转换为数组。
*
* @param {Set} arr - 需要转换的类数组对象。
* @returns {Array} 返回一个新数组,其中包含原类数组对象的所有元素。
*/
export function arrayFrom(arr : Set<any>) : Array<any> {
const ret : any[] = []
arr.forEach(a => {
ret.push(a)
})
return ret
}
/**
* includes 函数用于检查数组中是否包含指定的元素。
*
* @param {Array} arr - 需要检查的数组。
* @param {*} item - 需要查找的元素。
* @returns {boolean} 如果数组中包含指定元素,则返回 true否则返回 false。
*/
export function includes(arr : Array<any>, item : any) : boolean {
return arr.indexOf(item)
}
/**
* hasOwn 函数用于检查对象是否具有指定的属性。
* 与直接使用 `obj.hasOwnProperty` 不同,此函数可以正确处理通过原型链继承的属性。
*
* @param {Object|Array} obj - 需要检查的对象或数组。
* @param {string} key - 需要检查的属性名。
* @returns {boolean} 如果对象具有指定的属性,则返回 true否则返回 false。
*/
export function hasOwn(obj : UTSJSONObject, key : string) : boolean {
return obj[key] != null
}
/**
* merge 函数用于合并多个对象。
* 它会将源对象的所有可枚举属性值复制到目标对象。
* 如果目标对象和源对象有相同的属性,且它们的属性值都是对象,则会递归地合并这两个属性值。
*
* @param {Object} target - 目标对象,将被合并的对象。
* @returns {Object} 返回合并后的新对象。
*/
export function merge(...target : UTSJSONObject[]) : UTSJSONObject {
return UTSJSONObject.assign(...target)
// const output = Object(target)
// for (let i = 1; i < arguments.length; i++) {
// const source = arguments[i]
// if (source !== undefined && source !== null) {
// let key
// for (key in source) {
// if (hasOwn(source, key)) {
// if (isObject(source[key])) {
// output[key] = merge(output[key], source[key])
// } else {
// output[key] = source[key]
// }
// }
// }
// }
// }
// return output
}
/**
* looseEqual 函数用于比较两个值是否宽松相等。
* 宽松相等意味着在比较时会进行类型转换,例如将字符串转换为数字。
* 该函数可以处理对象、数组和其他基本数据类型的值。
*
* @param {any} a - 要比较的第一个值。
* @param {any} b - 要比较的第二个值。
* @returns {boolean} 如果两个值宽松相等,则返回 true否则返回 false。
*/
export function looseEqual(a : any, b : any) : boolean {
// 如果 a 和 b 严格相等,直接返回 true
if (a == b) { return true }
// 检查 a 和 b 是否都是对象
const isObjectA : boolean = isObject(a)
const isObjectB : boolean = isObject(b)
// 如果 a 和 b 都是对象
if (isObjectA && isObjectB) {
try {
// 检查 a 和 b 是否都是数组
const isArrayA : boolean = Array.isArray(a)
const isArrayB : boolean = Array.isArray(b)
// 如果 a 和 b 都是数组
if (isArrayA && isArrayB) {
// 比较它们的长度是否相等,以及它们的每个元素是否宽松相等
return (a as any[]).length == (b as any[]).length && a.every((e : any, i : number) : boolean => {
return looseEqual(e, b[i])
})
} else if (!isArrayA && !isArrayB) { // 如果 a 和 b 都不是数组
// 比较它们的键的数量是否相等,以及对应的键对应的值是否宽松相等
const keysA : Array<string> = UTSJSONObject.keys(a as UTSJSONObject)
const keysB : Array<string> = UTSJSONObject.keys(b as UTSJSONObject)
return keysA.length == keysB.length && keysA.every((key : string) : boolean => {
const valueA = a[key]
const valueB = b[key]
if(valueA == null || valueB == null) {
return false
}
return looseEqual(valueA, valueB)
})
} else {
// 如果 a 和 b 类型不同(一个是数组,另一个不是),返回 false
return false
}
} catch (e) {
// 如果在比较过程中发生异常,返回 false
return false
}
} else if (!isObjectA && !isObjectB) { // 如果 a 和 b 都不是对象
// 尝试将它们转换为字符串并比较
return `${a}` == `${b}`
} else {
// 如果 a 和 b 类型不同(一个是对象,另一个不是),返回 false
return false
}
}
/**
* 对用户输入的原始文本进行 HTML 特殊字符转义,以降低 XSS 攻击的风险。
* @param {string} rawText - 需要转义的原始用户输入文本。
* @returns {string} 返回转义后的文本。
*/
function escapeHtml(rawText: string): string {
return rawText
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
/**
* 从 `parseArgs().params` 返回的所有提供的参数中转义 HTML 标签和特殊符号。
* 此方法对 `params` 对象执行原地操作。
*
* @param {any} params - 从 `parseArgs().params` 提供的参数。
* 可能是字符串数组或字符串到任意值的映射。
* @returns {any} 返回被操纵过的 `params` 对象。
*/
export function escapeParams(params: UTSJSONObject|null): UTSJSONObject|null {
if(params != null) {
UTSJSONObject.keys(params).forEach(key => {
if(typeof(params[key]) == 'string') {
params[key] = escapeHtml(params[key])
}
})
}
return params
}

View File

@@ -0,0 +1,25 @@
type warnMessagesTypes = {
FALLBACK_TO_ROOT: number
NOT_FOUND_PARENT_SCOPE: number
IGNORE_OBJ_FLATTEN: number
DEPRECATE_TC: number
}
export const I18nWarnCodes:warnMessagesTypes = {
// 使用根语言环境回退到{type} '{key}'
FALLBACK_TO_ROOT: 8,
// 未找到父作用域,使用全局作用域
NOT_FOUND_PARENT_SCOPE: 9,
// 忽略对象扁平化:'{key}'键具有字符串值
IGNORE_OBJ_FLATTEN: 10,
// 'tc'和'$tc'已在v10中被弃用请使用't'或'$t'代替。'tc'和'$tc'将在v11中移除
DEPRECATE_TC: 11
}
export const warnMessages : Map<number, string> = new Map<number, string>([
[I18nWarnCodes.FALLBACK_TO_ROOT, `使用根语言环境回退到{type} '{key}'。`],
[I18nWarnCodes.NOT_FOUND_PARENT_SCOPE, `未找到父作用域,使用全局作用域。`],
[I18nWarnCodes.IGNORE_OBJ_FLATTEN, `忽略对象扁平化:'{key}'键具有字符串值。`],
[I18nWarnCodes.DEPRECATE_TC, `'tc'和'$tc'已在v10中被弃用请使用't'或'$t'代替。'tc'和'$tc'将在v11中移除。`],
])

View File

@@ -0,0 +1,13 @@
<template>
<text>
<09>ݲ<EFBFBD>֧<EFBFBD><D6A7>
</text>
</template>
<script setup>
</script>
<style>
</style>

View File

@@ -0,0 +1,89 @@
<template>
<view>
<text>测试:{{ $t('headMenus.userName') }}</text>
<text>测试:{{ $t('common.hello', {msg: '隔壁老王'}) }}</text>
<text>测试:{{ $t('message.link') }}</text>
<text>测试:{{ $t('message.linkHelloName', {name: '隔壁老王'}) }}</text>
<text>测试:{{ $t('message.linkLinkHelloName', {name: '隔壁老王'}) }}</text>
<text>测试:{{ $t('message.linkEnd') }}</text>
<text>测试:{{ $t('message.linkWithin') }}</text>
<text>测试:{{ $t('message.linkMultiple') }}</text>
<text>测试:{{ $t('message.linkBrackets') }}</text>
<text>测试:{{ $t('message.linkHyphen') }}</text>
<text>测试:{{ $t('message.linkUnderscore') }}</text>
<text>测试:{{ $t('message.linkPipe') }}</text>
<text>测试:{{ $t('message.linkList', ['数组值1', '数组值2']) }}</text>
<text>测试:{{ $t('message.linkCaseLower') }}</text>
<text>测试:{{ $t('message.linkCaseUpper') }}</text>
<text>测试:{{ $t('message.linkCaseCapitalize') }}</text>
<text>测试:{{ $t('message.linkCaseUnknown') }}</text>
<text>测试:{{ $t('message.linkCaseCustom') }}</text>
<text>测试:{{ $t('message.circular1') }}</text>
<text>测试:{{ $t('message.linkTwice') }}</text>
<text>测试:{{ $t('address', { account: 'foo', domain: 'domain.com' }) }}</text>
<text>测试:{{ $t('message.linked') }}</text>
<text>测试:{{ $t('message.missingHomeAddress') }}</text>
<text>测试:{{ $t('message.custom_modifier') }}</text>
<text>测试:{{ $t('headMenus.userName', {}) }}</text>
<text>复数:{{ $tc('plurals.apple') }}</text>
<text>复数:{{ $tc('plurals.apple', 0) }}</text>
<text>复数:{{ $tc('plurals.apple', 1) }}</text>
<text>复数:{{ $tc('plurals.apple', 10, { count: 10 }) }}</text>
<text>复数:{{ $tc('plurals.apple', 10, { count: 'Many' }) }}</text>
<text>复数:{{ $tc('plurals.apple', 10) }}</text>
<text>复数:{{ $tc('plurals.car', 1) }}</text>
<text>复数:{{ $tc('plurals.car', 2) }}</text>
<text>复数自定义规则:{{ $tc('car', 1, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('car', 2, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('car', 4, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('car', 12, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('car', 21, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('banana', 0, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('banana', 4, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('banana', 11, 'ru') }}</text>
<text>复数自定义规则:{{ $tc('banana', 31, 'ru') }}</text>
<text>
<text v-for="item in $i18n.availableLocales">{{item}}</text>
</text>
<text>{{ $d(new Date(), 'short') }}</text>
<text>{{ $d(new Date(), 'long', 'zh-CN') }}</text>
<text>{{ $n(10000, 'currency') }}</text>
<text>{{ $n(10000, 'currency', 'zh-CN') }}</text>
<text>{{ $n(10000, 'currency', 'zh-CN', { useGrouping: false }) }}</text>
<text>{{ $n(987654321, 'currency', { notation: 'compact' }) }}</text>
<text>{{ $n(0.99123, 'percent') }}</text>
<text>{{ $n(0.99123, 'percent', { minimumFractionDigits: 2 }) }}</text>
<text>{{ $n(12.11612345, 'decimal') }}</text>
<text>{{ $n(12145281111, 'decimal', 'zh-CN') }}</text>
<!-- <button @click="onClick">切换{{locale}}</button> -->
<!-- <button @click="$locale.value = $locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'">切换,当前:{{$locale}}</button> -->
<button @click="onClick">切换,当前:{{$locale}}</button>
</view>
</template>
<script setup>
import { useI18n } from '@/uni_modules/lime-i18n';
// import * as test from '@/uni_modules/lime-i18n/message-compiler/test'
// import * as test from '@/uni_modules/lime-i18n/common/test'
import { getCurrentInstance } from 'vue'
const context = getCurrentInstance()
// 局部国际化
const {locale, setLocaleMessage} = useI18n()
const onClick = () => {
if(context == null) return
context.proxy!.$locale.value = context.proxy!.$locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'
// context!.proxy!.$locale.value = 'zh-CN' ? 'zh-CN' : 'en-US'
// locale.value = locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'
// console.log(`$i18n.t`, context?.proxy?.$i18n.t('headMenus.userName', {}))
console.log(`$i18n.t`, context!.proxy!.$t('headMenus.userName'))
}
</script>
<style>
</style>

View File

@@ -0,0 +1 @@
export * from './common'

View File

@@ -0,0 +1,87 @@
{
"id": "ak-i18n",
"displayName": "ak-i18n",
"version": "0.0.8",
"description": "lime-i18n 系参考 vue-i18n 实现的 UTS 国际化插件",
"keywords": [
"lime-i18n",
"vue-i18n",
"uts",
"国际化",
"多语言"
],
"repository": "",
"engines": {
"HBuilderX": "^4.21"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y",
"alipay": "y"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "y"
},
"App": {
"app-android": "y",
"app-ios": "y",
"app-harmony": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}

View File

@@ -0,0 +1,549 @@
# lime-i18n 国际化
- 参考vue-i18n实现的uts国际化插件
## 文档
[i18n](https://limex.qcoon.cn/native/i18n.html)
## 安装
在插件市场导入即可
## 基础使用
```js
// main.uts
import { createI18n } from '@/uni_modules/lime-i18n'
//目录自己决定
import zhCN from './locales/zh-CN'
import enUS from './locales/en_US'
const i18n = createI18n({
// 使用uni.getStorageSync('uVueI18nLocale') 能获取上次退出应用后保存的语言
locale: 'zh-CN', // 默认显示语言
fallbackLocale: 'en-US',
messages: {
'zh-CN': zhCN,
'en-US': enUS
}
})
export function createApp(){
const app = createSSRApp(App);
app.use(i18n)
//....
}
```
### 切换语言
使用创建的`i18n`切换
```js
// 假设在这个文件里使用createI18n创建i18n创建步骤就是上面 基础使用
import i18n from 'xxx/locales';
i18n.global.locale.value = 'zh-CN'
```
模板中
```html
<template>
<view>
<text>测试:{{ $t('headMenus.userName') }}</text>
<button @click="$locale.value = $locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'">切换{{$locale}}</button>
</view>
</template>
```
选项式API
```js
export default {
data() {
return {
}
},
methods: {
onClick() {
this.$locale.value = this.$locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'
}
}
}
```
组合式API
```js
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance()!
const onClick = () => {
if(instance == null) return
instance.proxy!.$locale.value = instance.proxy!.$locale.value != 'zh-CN' ? 'zh-CN' : 'en-US'
}
```
### 延迟加载
直接调用创建的
```js
// 模拟请求后端接口返回
setTimeout(() => {
// 直接调用创建的
i18n.global.setLocaleMessage('zh-CN', zhCN)
}, 5000)
```
选项组合API
```js
// 模拟请求后端接口返回
setTimeout(() => {
this.$i18n.global.setLocaleMessage('zh-CN', zhCN)
}, 5000)
```
组合式API
```js
// 模拟请求后端接口返回
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
setTimeout(() => {
instance.proxy!.$i18n.global.setLocaleMessage('zh-CN', zhCN)
}, 5000)
```
## 格式化
### 具名插值
语言环境信息如下:
```js
const messages = {
en: {
message: {
hello: '{msg} world'
}
}
}
```
模板如下:
```html
<text>{{ $t('message.hello', { msg: 'hello' }) }}</text>
```
输出如下:
```html
<text>hello world</text>
```
### 列表插值
语言环境信息如下:
```js
const messages = {
en: {
message: {
hello: '{0} world'
}
}
}
```
模板如下:
```html
<text>{{ $t('message.hello', ['hello']) }}</text>
```
输出如下:
```html
<text>hello world</text>
```
### 链接插值
如果有一个翻译关键字总是与另一个具有相同的具体文本,你可以链接到它。要链接到另一个翻译关键字,你所要做的就是在其内容前加上一个` @: `符号后跟完整的翻译键名,包括你要链接到的命名空间。<br>
语言环境信息如下:
```js
const messages = {
en: {
message: {
the_world: 'the world',
dio: 'DIO:',
linked: '@:message.dio @:message.the_world !!!!'
}
}
}
```
模板如下:
```html
<text>{{ $t('message.linked') }}</text>
```
输出如下:
```html
<text>DIO: the world !!!!</text>
```
#### 内置修饰符
如果语言区分字符大小写,则可能需要控制链接的区域设置消息的大小写。链接邮件可以使用修饰符` @.modifierkey` 进行格式化<br><br>
以下修饰符当前可用
- `upper:` 链接消息中的所有字符均大写
- `lower:` 小写链接消息中的所有字符
- `capitalize:` 大写链接消息中的第一个字符
语言环境信息如下:
```js
const messages = {
en: {
message: {
homeAddress: 'Home address',
missingHomeAddress: 'Please provide @.lower:message.homeAddress'
}
}
}
```
模板如下:
```html
<text>{{ $t('message.homeAddress') }}</text>
<text class="error">{{ $t('message.missingHomeAddress') }}</text
```
输出如下:
```html
<text>Home address</text>
<text class="error">Please provide home address</text>
```
### 自定义修饰符
如果要使用非内置修饰符,则可以使用自定义修饰符。
```js
const i18n = createI18n({
locale: 'en',
messages: {
// set something locale messages ...
},
// set custom modifiers at `modifiers` option
modifiers: {
snakeCase: (str:string):string => str.split(' ').join('_')
}
})
```
区域设置消息如下:
```js
const messages = {
en: {
message: {
snake: 'snake case',
custom_modifier: "custom modifiers example: @.snakeCase:{'message.snake'}"
}
}
}
```
## 复数
你可以使用复数进行翻译。你必须定义具有管道 | 分隔符的语言环境,并在管道分隔符中定义复数。<br>
*您的模板将需要使用 `$tc()` 而不是 `$t()`<br>
语言环境信息如下:
```js
const messages = {
en: {
car: 'car | cars',
apple: 'no apples | one apple | {count} apples'
}
}
```
模板如下:
```html
<text>{{ $tc('car', 1) }}</text>
<text>{{ $tc('car', 2) }}</text>
<text>{{ $tc('apple', 0) }}</text>
<text>{{ $tc('apple', 1) }}</text>
<text>{{ $tc('apple', 10, { count: 10 }) }}</text>
```
输出如下:
```html
<text>car</text>
<text>cars</text>
<text>no apples</text>
<text>one apple</text>
<text>10 apples</text>
```
### 通过预定义的参数访问该数字
你无需明确指定复数的数字。可以通过预定义的命名参数 `{count}` 和/或 `{n}` 在语言环境信息中访问该数字。如有必要,你可以覆盖这些预定义的命名参数。<br>
语言环境信息如下:
```js
const messages = {
en: {
apple: 'no apples | one apple | {count} apples',
banana: 'no bananas | {n} banana | {n} bananas'
}
}
```
模板如下:
```html
<text>{{ $tc('apple', 10, { count: 10 }) }}</text>
<text>{{ $tc('apple', 10) }}</text>
<text>{{ $tc('banana', 1, { n: 1 }) }}</text>
<text>{{ $tc('banana', 1) }}</text>
<text>{{ $tc('banana', 100, { n: 'too many' }) }}</text>
```
输出如下:
```html
<text>10 apples</text>
<text>10 apples</text>
<text>1 banana</text>
<text>1 banana</text>
<text>too many bananas</text>
```
### 自定义复数
但是,这种多元化并不适用于所有语言(例如,斯拉夫语言具有不同的多元化规则)。
为了实现这些规则,您可以将可选的 `pluralizationRules` 对象传递给 `UvueI18n` 构造函数选项。
使用针对斯拉夫语言(俄语,乌克兰语等)的规则的非常简化的示例:
```js
function customRule(choice:number, choicesLength:number):number {
if (choice == 0) {
return 0
}
const teen = choice > 10 && choice < 20
const endsWithOne = choice % 10 == 1
if (!teen && endsWithOne) {
return 1
}
if (!teen && choice % 10 >= 2 && choice % 10 <= 4) {
return 2
}
return choicesLength < 4 ? 2 : 3
}
const i18n = createI18n({
locale: 'ru',
// the custom rules here ...
pluralizationRules: {
ru: customRule
},
messages: {
ru: {
car: '0 машин | {n} машина | {n} машины | {n} машин',
banana: 'нет бананов | {n} банан | {n} банана | {n} бананов'
}
}
})
```
这将有效地实现以下目的:
```html
<view>Car:</view>
<text>{{ $tc('car', 1) }}</text>
<text>{{ $tc('car', 2) }}</text>
<text>{{ $tc('car', 4) }}</text>
<text>{{ $tc('car', 12) }}</text>
<text>{{ $tc('car', 21) }}</text>
<view>Banana:</view>
<text>{{ $tc('banana', 0) }}</text>
<text>{{ $tc('banana', 4) }}</text>
<text>{{ $tc('banana', 11) }}</text>
<text>{{ $tc('banana', 31) }}</text>
```
结果如下:
```html
<view>Car:</view>
<text>1 машина</text>
<text>2 машины</text>
<text>4 машины</text>
<text>12 машин</text>
<text>21 машина</text>
<view>Banana:</view>
<text>нет бананов</text>
<text>4 банана</text>
<text>11 бананов</text>
<text>31 банан</text>
```
## ~~切换tabBar文本~~
~~这是`UvueI18n`独有的功能,方便切换`tabBar`上的文本,暂定这个规则~~目前没有找到可以判断当前页面是不为tabbar页的方法暂时不推荐使用。如果你有能获取到当前页面为tabbar页的方式可以告诉我。
```js
const i18n = createI18n({
tabBars: {
'en-US': ['home','User Center'],
'zh-CN': ['首页','用户中心'],
}
})
```
## 日期时间本地化
由于APP不支持`Intl.DateTimeFormat`故这功能无法在APP上使用。
日期时间格式如下:
```js
const datetimeFormats = {
'en-US': {
short: {
year: 'numeric', month: 'short', day: 'numeric'
},
long: {
year: 'numeric', month: 'short', day: 'numeric',
weekday: 'short', hour: 'numeric', minute: 'numeric'
}
},
'zh-CN': {
short: {
year: 'numeric', month: 'short', day: 'numeric'
},
long: {
year: 'numeric', month: 'short', day: 'numeric',
weekday: 'short', hour: 'numeric', minute: 'numeric', hour12: true
}
}
}
```
如上,你可以定义具名的 (例如short、long 等) 日期时间格式,并需要使用 [ECMA-402 Intl.DateTimeFormat 的选项](http://www.ecma-international.org/ecma-402/2.0/#sec-intl-datetimeformat-constructor)。
之后就像语言环境信息一样,你需要指定 `UvueI18n` 构造函数的 `dateTimeFormats` 选项:
```js
const i18n = createI18n({
datetimeFormats
})
```
模板如下:
```html
<text>{{ $d(new Date(), 'short') }}</text>
<text>{{ $d(new Date(), 'long', 'zh-CN') }}</text>
```
第一个参数是日期时间可用值例如timestamp作为参数第二个参数是日期时间格式名称作为参数。最后一个参数 locale 值作为参数。`Date`
```html
<text>Jun 30, 2024</text>
<text>2024年6月30日周日 下午6:35</text>
```
## 数字格式
由于APP不支持`Intl.NumberFormat`故这功能无法在APP上使用。
你可以使用你定义的格式来本地化数字。
```js
const numberFormats = {
'en-US': {
currency: {
style: 'currency', currency: 'USD', notation: 'standard'
},
decimal: {
style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2
},
percent: {
style: 'percent', useGrouping: false
}
},
'zh-CN': {
currency: {
style: 'currency', currency: 'CNY', useGrouping: true, currencyDisplay: 'symbol'
},
decimal: {
style: 'decimal', minimumSignificantDigits: 3, maximumSignificantDigits: 5
},
percent: {
style: 'percent', useGrouping: false
}
}
}
```
如上,你可以指定具名的 (例如currency 等) 的数字格式,并且需要使用 [ECMA-402 Intl.NumberFormat 的选项](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat)。
之后,在使用区域设置消息时,您需要指定以下选项:`numberFormatscreateI18n`
```js
const i18n = createI18n({
numberFormats
})
```
以下是在模板中使用的示例:`$n`
```html
<text>{{ $n(10000, 'currency') }}</text>
<text>{{ $n(10000, 'currency', 'zh-CN') }}</text>
<text>{{ $n(10000, 'currency', 'zh-CN', { useGrouping: false }) }}</text>
<text>{{ $n(987654321, 'currency', { notation: 'compact' }) }}</text>
<text>{{ $n(0.99123, 'percent') }}</text>
<text>{{ $n(0.99123, 'percent', { minimumFractionDigits: 2 }) }}</text>
<text>{{ $n(12.11612345, 'decimal') }}</text>
<text>{{ $n(12145281111, 'decimal', 'zh-CN') }}</text>
```
第一个参数是作为参数的数值,第二个参数是作为参数的数字格式名称。最后一个参数 locale 值作为参数。
结果如下:
```html
<text>$10,000.00</text>
<text>¥10,000.00</text>
<text>¥10,000.00</text>
<text>$988M</text>
<text>99%</text>
<text>99.12%</text>
<text>12.12</text>
<text>12,145,000,000</text>
```
## API
获取方法 传参 `locale`,设置方法 传参`locale` `format`
例如:
```js
// 可以手动引入i18n例如你单独文件创建了i18n,就可以导入这个文件使用i18n
i18n.global.setLocaleMessage('zh-CN', zhCN)
i18n.global.getLocaleMessage('zh-CN')
// 如果是选择项API 可以使用
this.$i18n.global.setLocaleMessage
// 如果是组合式API 可以先获取当前组件实例
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
instance.proxy!.$i18n.global.setLocaleMessage
```
### setLocaleMessage
### getLocaleMessage
### mergeLocaleMessage
### getDateTimeFormat
### setDateTimeFormat
### mergeDateTimeFormat
### getNumberFormat
### setNumberFormat
### mergeNumberFormat
### setTabBar
### getTabBar
<!-- ## useI18n
创建一个小范围的i18n,例如在当前页面下使用,本功能未实测过,因为`useI18n`导出的方法需要写全参数,不能省略。不建议使用。
```html
<text>{{ t('headMenus.userName') }}</text>
```
```js
import { useI18n } from '@/uni_modules/lime-i18n';
const {locale, setLocaleMessage, t} = useI18n()
``` -->
## 打赏
如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。
![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/alipay.png)
![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/wpay.png)

View File

@@ -0,0 +1,9 @@
{
"zh-CN": {
"code": "CNY",
"num": "156",
"nativeName": "人民币",
"englishName": "Chinese Yuan",
"symbol": "¥"
}
}

View File

@@ -0,0 +1,112 @@
import { createI18n } from '@/uni_modules/lime-i18n/index.uts'
// import { createI18n, setLocaleMessage } from '../index.uts'
import zhCN from './locales/zh-CN'
import enUS from './locales/en_US'
const i18n = createI18n({
locale: uni.getStorageSync('uVueI18nLocale').toString().length != 0 ? uni.getStorageSync('uVueI18nLocale') : 'zh-CN', // 默认显示语言
fallbackLocale: 'en-US',
// Key - 在这种情况下,用于规则 `'ru'` 的语言
// Value - 选择正确的复数形式的功能
pluralizationRules: {
/**
* @param choice {number} 输入给$的选择索引 $tc`$tc('path.to.rule', choiceIndex)`
* @param choicesLength {number} 可用选择总数
* @returns 最终选择索引以选择复数单词
*/
'ru': function (choice : number, choicesLength : number) : number {
if (choice == 0) {
return 0;
}
const teen = choice > 10 && choice < 20;
const endsWithOne = (choice % 10) == 1;
if (choicesLength < 4) {
return (!teen && endsWithOne) ? 1 : 2;
}
if (!teen && endsWithOne) {
return 1;
}
if (!teen && (choice % 10) >= 2 && (choice % 10) <= 4) {
return 2;
}
return (choicesLength < 4) ? 2 : 3;
}
},
messages: {
'zh-CN': zhCN,
'en-US': enUS,
'ru': {
car: '0 машин | {n} машина | {n} машины | {n} машин',
banana: 'нет бананов | {n} банан | {n} банана | {n} бананов'
}
},
modifiers: {
snakeCase: (str : string) : string => str.split(' ').join('_')
},
numberFormats: {
'en-US': {
currency: {
style: 'currency', currency: 'USD', notation: 'standard'
},
decimal: {
style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2
},
percent: {
style: 'percent', useGrouping: false
}
},
'zh-CN': {
currency: {
style: 'currency', currency: 'CNY', useGrouping: true, currencyDisplay: 'symbol'
},
decimal: {
style: 'decimal', minimumSignificantDigits: 3, maximumSignificantDigits: 5
},
percent: {
style: 'percent', useGrouping: false
}
}
},
datetimeFormats: {
'en-US': {
short: {
year: 'numeric', month: 'short', day: 'numeric'
},
long: {
year: 'numeric', month: 'short', day: 'numeric',
weekday: 'short', hour: 'numeric', minute: 'numeric'
}
},
'zh-CN': {
short: {
year: 'numeric', month: 'short', day: 'numeric'
},
long: {
year: 'numeric', month: 'short', day: 'numeric',
weekday: 'short', hour: 'numeric', minute: 'numeric', hour12: true
}
}
},
tabBars: {
'en-US': ['home','User Center'],
'zh-CN': ['首页','用户中心'],
}
})
export default i18n
setTimeout(() => {
// console.log('getLocale:::', uni.getLocale())
console.log('getLocale:::',typeof uni.getStorageSync('lllluVueI18nLocale'))
// console.log('i18n install', i18n.global)
// setLocaleMessage('zh-CN', zhCN)
i18n.global.locale.value = 'zh-CN'
}, 5000)

View File

@@ -0,0 +1,66 @@
export default {
common: {
more: "Look More",
hello: '{msg} world'
},
leftMenus: {
// "/": "Home",
// Home: "Home",
home: "Home",
},
headMenus: {
"subTitle": "Organization service platform",
"userName": "ZhangSan"
},
login: {
"personal_center": "personal center",
"sign_out": "sign out"
},
plurals: {
car: 'car | cars',
apple: 'no apples | one apple | {count} apples',
format: {
named: 'Hello {name}, how are you? | Hi {name}, you look fine',
list: 'Hello {0}, how are you? | Hi {0}, you look fine'
},
fallback: 'this is fallback | this is a plural fallback'
},
message: {
hello: 'the world',
helloName: 'Hello {name}',
hoge: 'hoge',
link: '@:message.hello',
linkHelloName: '@:message.helloName',
linkLinkHelloName: '@:message.linkHelloName',
linkEnd: 'This is a linked translation to @:message.hello',
linkWithin: 'Isn\'t @:message.hello we live in great?',
linkMultiple: 'Hello @:message.hoge!, isn\'t @:message.hello great?',
linkBrackets: 'Hello @:(message.hoge). Isn\'t @:(message.hello) great?',
linkHyphen: '@:hyphen-hello',
linkUnderscore: '@:underscore_hello',
linkPipe: '@:pipe|hello',
linkColon: '@:(colon:hello)',
linkList: '@:message.hello: {0} {1}',
linkCaseLower: 'Please provide @.lower:message.homeAddress',
linkCaseUpper: '@.upper:message.homeAddress',
linkCaseCapitalize: '@.capitalize:message.homeAddress',
linkCaseUnknown: '@.unknown:message.homeAddress',
linkCaseCustom: '@.custom:message.homeAddress',
homeAddress: 'home Address',
circular1: 'Foo @:message.circular2',
circular2: 'Bar @:message.circular3',
circular3: 'Buz @:message.circular1',
linkTwice: '@:message.hello: @:message.hello',
the_world: 'the world',
dio: 'DIO:',
linked: '@:message.dio @:message.the_world !!!!',
missingHomeAddress: 'Please provide @.lower:message.homeAddress',
snake: 'snake case',
custom_modifier: "custom modifiers example: @.snakeCase:{'message.snake'}"
},
address: "{account}{'@'}{domain}",
'hyphen-hello': 'hyphen the wolrd',
underscore_hello: 'underscore the wolrd',
// 'colon:hello': 'hello colon',
// 'pipe|hello': 'hello pipe',
};

View File

@@ -0,0 +1,53 @@
export default {
common: {
more: "查看更多",
hello: "{msg} 世界"
},
leftMenus: {
home: "首页"
},
headMenus: {
subTitle: "组织服务平台",
userName: "张三"
},
login: {
personal_center: "个人中心",
sign_out: "退出登录"
},
plurals: {
car: '车 | 辆车',
apple: '没有苹果 | 一个苹果 | {count} 个苹果',
format: {
named: '你好 {name},你好吗? | 嗨 {name},你看起来不错',
list: '你好 {0},你好吗? | 嗨 {0},你看起来不错'
},
fallback: '这是备选 | 这是一个复数备选'
},
message: {
hello: '世界',
helloName: '你好,{name}',
hoge: 'hoge',
link: '@:message.hello',
linkHelloName: '@:message.helloName',
linkLinkHelloName: '@:message.linkHelloName',
linkEnd: '这是一个链接翻译到 @:message.hello',
linkWithin: '难道我们不是生活在美好的世界里吗?',
linkMultiple: '你好,@:message.hoge难道 @:message.hello 不美好吗?',
linkBrackets: '你好,@:(message.hoge)。难道 @:(message.hello) 不美好吗?',
linkHyphen: '@:hyphen-hello',
linkUnderscore: '@:underscore_hello',
linkPipe: '@:pipe|hello',
linkColon: '@:(colon:hello)',
linkList: '@:message.hello{0} {1}',
linkCaseLower: '请提供 @.lower:message.homeAddress',
linkCaseUpper: '@.upper:message.homeAddress',
linkCaseCapitalize: '@.capitalize:message.homeAddress',
linkCaseUnknown: '@.unknown:message.homeAddress',
linkCaseCustom: '@.custom:message.homeAddress',
homeAddress: '家庭地址',
circular1: 'Foo @:message.circular2',
circular2: 'Bar @:message.circular3',
circular3: 'Buz @:message.circular1',
linkTwice: '@:message.hello@:message.hello',
}
};

View File

@@ -0,0 +1,39 @@
/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */
import { MyApiErrorCode, MyApiFail } from "./interface.uts"
/**
* 错误主题
* 注意:错误主题一般为插件名称,每个组件不同,需要使用时请更改。
* [可选实现]
*/
export const UniErrorSubject = 'uts-api';
/**
* 错误信息
* @UniError
* [可选实现]
*/
export const MyAPIErrors : Map<MyApiErrorCode, string> = new Map([
/**
* 错误码及对应的错误信息
*/
[9010001, 'custom error mseeage1'],
[9010002, 'custom error mseeage2'],
]);
/**
* 错误对象实现
*/
export class MyApiFailImpl extends UniError implements MyApiFail {
/**
* 错误对象构造函数
*/
constructor(errCode : MyApiErrorCode) {
super();
this.errSubject = UniErrorSubject;
this.errCode = errCode;
this.errMsg = MyAPIErrors.get(errCode) ?? "";
}
}

View File

@@ -0,0 +1,416 @@
import { AkReqUploadOptions, AkReqOptions, AkReqResponse, AkReqError } from './interface.uts';
// token 持久化 key
const ACCESS_TOKEN_KEY = 'akreq_access_token';
const REFRESH_TOKEN_KEY = 'akreq_refresh_token';
const EXPIRES_AT_KEY = 'akreq_expires_at';
// 优化:用静态变量缓存 token只有 set/clear 时同步 storage
let _accessToken : string | null = null;
let _refreshToken : string | null = null;
let _expiresAt : number | null = null;
export class AkReq {
static setToken(token : string, refreshToken : string, expiresAt : number) {
_accessToken = token;
_refreshToken = refreshToken;
_expiresAt = expiresAt;
uni.setStorageSync(ACCESS_TOKEN_KEY, token);
uni.setStorageSync(REFRESH_TOKEN_KEY, refreshToken);
uni.setStorageSync(EXPIRES_AT_KEY, expiresAt);
}
static getToken() : string | null {
if (_accessToken != null) return _accessToken;
const t = uni.getStorageSync(ACCESS_TOKEN_KEY) as string | null;
_accessToken = t;
return t;
}
static getRefreshToken() : string | null {
if (_refreshToken != null) return _refreshToken;
const t = uni.getStorageSync(REFRESH_TOKEN_KEY) as string | null;
_refreshToken = t;
return t;
} static getExpiresAt() : number | null {
const val = _expiresAt;
if (val != null) return val;
const t = uni.getStorageSync(EXPIRES_AT_KEY) as number | null;
_expiresAt = t;
return t;
}
static clearToken() {
_accessToken = null;
_refreshToken = null;
_expiresAt = null;
uni.removeStorageSync(ACCESS_TOKEN_KEY);
uni.removeStorageSync(REFRESH_TOKEN_KEY);
uni.removeStorageSync(EXPIRES_AT_KEY);
} // 判断 token 是否即将过期提前5分钟刷新
static isTokenExpiring() : boolean {
const expiresAt = this.getExpiresAt();
if (expiresAt === null || expiresAt == 0) {
return true;
}
const now = Math.floor(Date.now() / 1000);
return (expiresAt - now) < 300; // 提前5分钟刷新
}
// 自动刷新 token返回 true=已刷新false=未刷新
static async refreshTokenIfNeeded(apikey ?: string) : Promise<boolean> {
// 没有 access_token 直接返回,不刷新
const accessToken = this.getToken();
if (accessToken === null || accessToken === "") {
return false;
}
if (!this.isTokenExpiring()) {
return false;
}
const refreshToken = this.getRefreshToken();
if (refreshToken === null || refreshToken === "") {
this.clearToken();
return false;
}
// 构造 header必须带 apikey
let headers = {} as UTSJSONObject;
if (apikey !== null && apikey !== "") {
headers = Object.assign({}, headers, { 'apikey': apikey }) as UTSJSONObject;
} try {
const res = await this.request({
url: 'https://ak3.oulog.com/auth/v1/token?grant_type=refresh_token',
method: 'POST',
data: ({ refresh_token: refreshToken } as UTSJSONObject),
headers: headers,
contentType: 'application/json'
}, true); // skipRefresh=true避免递归
const data = res.data as UTSJSONObject | null;
let accessToken : string | null = null;
let refreshTokenNew : string | null = null;
let expiresAt : number | null = null;
if (data != null && typeof data.getString === 'function' && typeof data.getNumber === 'function') {
accessToken = data.getString('access_token');
refreshTokenNew = data.getString('refresh_token');
expiresAt = data.getNumber('expires_at');
}
if (accessToken !== null && refreshTokenNew !== null && expiresAt !== null) {
this.setToken(accessToken, refreshTokenNew, expiresAt);
return true;
} else {
this.clearToken();
return false;
}
} catch (e) {
this.clearToken();
return false;
}
}
// options: AkReqOptions, skipRefresh: boolean = false
static async request(options : AkReqOptions, skipRefresh ?: boolean) : Promise<AkReqResponse<any>> {
// 自动刷新 token
if (skipRefresh != true) {
let apikey : string | null = null;
const headersObj = options.headers;
if (headersObj != null && typeof headersObj.getString === 'function') {
apikey = headersObj.getString('apikey');
}
await this.refreshTokenIfNeeded(apikey);
}
// 统一 header自动带上 Authorization/Content-Type/Accept
let headers = options.headers ?? ({} as UTSJSONObject);
const token = this.getToken();
if (token != null && token != "") {
headers = Object.assign({}, headers, { Authorization: `Bearer ${token}` }) as UTSJSONObject;
}
let contentType = options.contentType ?? '';
if (headers != null && typeof headers.getString === 'function') {
const headerContentType = headers.getString('Content-Type');
if (headerContentType != null) {
contentType = headerContentType;
}
}
if (contentType != null && contentType != "") {
headers = Object.assign({}, headers, { 'Content-Type': contentType }) as UTSJSONObject;
}
// 默认 Accept
headers = Object.assign({ Accept: 'application/json' } as UTSJSONObject, headers) as UTSJSONObject;
const timeout = options.timeout ?? 10000;
const maxRetry = Math.max(0, options.retryCount ?? 0);
const baseDelay = Math.max(0, options.retryDelayMs ?? 300);
const doOnce = (): Promise<AkReqResponse<any>> => {
return new Promise<AkReqResponse<any>>((resolve) => {
uni.request({
url: options.url,
method: options.method ?? 'GET',
data: options.data,
header: headers,
timeout: timeout,
success: (res) => {
// HEAD 请求特殊处理:没有响应体,只有 headers
if (options.method == 'HEAD') {
const result = AkReq.createResponse<any>(
res.statusCode,
[] as Array<any>,
res.header as UTSJSONObject
);
resolve(result);
return;
}
// 兼容 res.data 可能为 string 或 UTSJSONObject 或 UTSArray
let data : UTSJSONObject | Array<UTSJSONObject> | null;
if (typeof res.data == 'string') {
const strData = res.data as string;
if (strData.length > 0 && /[^\s]/.test(strData)) {
try {
data = JSON.parse(strData) as UTSJSONObject;
} catch (e) {
data = null;
}
} else {
data = null;
}
} else if (Array.isArray(res.data)) {
data = res.data as UTSJSONObject[];
} else {
const objData = res.data as UTSJSONObject | null;
data = objData;
if (objData != null) {
const accessToken = objData.getString('access_token');
const refreshTokenNew = objData.getString('refresh_token');
const expiresAt = objData.getNumber('expires_at');
if (accessToken !== null && refreshTokenNew !== null && expiresAt !== null) {
AkReq.setToken(accessToken, refreshTokenNew, expiresAt);
}
}
}
const result = AkReq.createResponse<any>(
res.statusCode,
data ?? {},
res.header as UTSJSONObject
);
resolve(result);
},
fail: (err) => {
const result = AkReq.createResponse<any>(
err.errCode,
err.data ?? {},
{} as UTSJSONObject,
new UniError('uni-request', err.errCode, err.errMsg ?? 'request fail')
);
resolve(result);
}
});
});
};
let attempt = 0;
let lastRes: AkReqResponse<any> | null = null;
while (attempt <= maxRetry) {
const res = await doOnce();
lastRes = res;
// 仅网络失败/超时errCode 非 0 且 status 非 2xx/3xx时重试
const status = res.status ?? 0;
const isOk = status >= 200 && status < 400;
if (isOk) return res;
if (attempt === maxRetry) break;
// 简单退避
const delay = baseDelay * Math.pow(2, attempt);
await new Promise<void>((r) => { setTimeout(() => { r(); }, delay); });
attempt++;
}
return lastRes!!;
}
// 新增 upload 方法,支持 uni.uploadFile自动带 token/apikey
static async upload(options : AkReqUploadOptions) : Promise<AkReqResponse<any>> {
// 上传前尝试刷新 token若即将过期。优先从 options.headers 或 apikey 字段获取 apikey
let apikey: string | null = null;
const hdr = options.headers;
if (hdr != null && typeof hdr.getString === 'function') {
apikey = hdr.getString('apikey');
}
if (apikey == null && options.apikey != null) apikey = options.apikey;
await this.refreshTokenIfNeeded(apikey != null ? apikey : null);
let headers = options.headers ?? ({} as UTSJSONObject);
const token = this.getToken();
if (token != null && token !== "") {
headers = Object.assign({}, headers, { Authorization: `Bearer ${token}` }) as UTSJSONObject;
}
if (apikey != null && apikey !== "") {
headers = Object.assign({}, headers, { apikey: apikey }) as UTSJSONObject;
}
// 默认 Accept
headers = Object.assign({ Accept: 'application/json' } as UTSJSONObject, headers) as UTSJSONObject;
const timeout = options.timeout ?? 10000;
const maxRetry = Math.max(0, options.retryCount ?? 0);
const baseDelay = Math.max(0, options.retryDelayMs ?? 300);
const doOnce = (): Promise<AkReqResponse<any>> => {
return new Promise<AkReqResponse<any>>((resolve) => {
const task = uni.uploadFile({
url: options.url,
filePath: options.filePath,
name: options.name,
formData: options.formData ?? {},
header: headers,
timeout: timeout,
success: (res : UploadFileSuccess) => {
let parsed: UTSJSONObject | null = null;
try {
parsed = JSON.parse(res.data) as UTSJSONObject;
} catch (e) {
parsed = null;
}
if (parsed != null) {
const accessToken = parsed.getString('access_token');
const refreshTokenNew = parsed.getString('refresh_token');
const expiresAt = parsed.getNumber('expires_at');
if (accessToken !== null && refreshTokenNew !== null && expiresAt !== null) {
AkReq.setToken(accessToken, refreshTokenNew, expiresAt);
}
}
const result = AkReq.createResponse<any>(
res.statusCode,
parsed ?? {},
headers
);
resolve(result);
},
fail: (err) => {
const result = AkReq.createResponse<any>(
err.errCode,
err.data ?? {},
{} as UTSJSONObject,
new UniError('uni-upload', err.errCode, err.errMsg ?? 'upload fail')
);
resolve(result);
}
});
if (options.onProgress != null && task != null) {
const progressCallback = (res: OnProgressUpdateResult) => {
const percent = res.progress as number; // 0-100
const sent = res.totalBytesSent as number | null;
const expected = res.totalBytesExpectedToSend as number | null;
if (options.onProgress != null) {
options.onProgress(percent, sent, expected);
}
};
task.onProgressUpdate(progressCallback);
}
});
};
let attempt = 0;
let lastRes: AkReqResponse<any> | null = null;
while (attempt <= maxRetry) {
const res = await doOnce();
lastRes = res;
const status = res.status ?? 0;
const isOk = status >= 200 && status < 400;
if (isOk) return res;
if (attempt === maxRetry) break;
const delay = baseDelay * Math.pow(2, attempt);
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, delay);
});
attempt++;
}
return lastRes!!;
}
// 辅助方法:创建 AkReqResponse 对象,避免类型推断问题
static createResponse<T>(
status: number,
data: T | Array<T> ,
headers: UTSJSONObject,
error: UniError | null = null,
total: number | null = null,
page: number | null = null,
limit: number | null = null,
hasmore: boolean | null = null,
origin: any | null = null
): AkReqResponse<T> {
return {
status,
data,
headers,
error,
total,
page,
limit,
hasmore,
origin
};
}
// 新增:支持类型转换的请求方法
static async requestAs<T = any>(options : AkReqOptions, skipRefresh ?: boolean) : Promise<AkReqResponse<T|Array<T>>> {
const response = await this.request(options, skipRefresh);
// 如果原始 data 是 null直接返回 null
// if (response.data == null) {
// return {
// status: response.status,
// data: null,
// headers: response.headers,
// error: response.error,
// total: response.total,
// page: response.page,
// limit: response.limit,
// hasmore: response.hasmore,
// origin: response.origin
// } as AkReqResponse<T|Array<T>>;
// }
// 尝试类型转换
let convertedData: T | null = null;
try {
// #ifdef APP-ANDROID
if (response.data instanceof UTSJSONObject) {
convertedData = response.data.parse<T>();
} else if (Array.isArray(response.data)) {
const convertedArray: Array<any> = [];
const dataArray = response.data;
for (let i = 0; i < dataArray.length; i++) {
const item = dataArray[i];
if (item instanceof UTSJSONObject) {
const parsed = item.parse<T>();
if (parsed != null) {
convertedArray.push(parsed);
}
} else {
convertedArray.push(item);
}
}
convertedData = convertedArray as T;
}
// #endif
// #ifndef APP-ANDROID
convertedData = response.data as T;
// #endif
} catch (e) {
console.warn('类型转换失败,使用原始 UTSJSONObject:', e);
// 转换失败时,返回原始 UTSJSONObject
convertedData = response.data as T;
}
const aaa = {
status: response.status,
data: convertedData!!,
headers: response.headers,
error: response.error,
total: response.total,
page: response.page,
limit: response.limit,
hasmore: response.hasmore,
origin: response.origin
} ;
return aaa
}
}
export default AkReq;

View File

@@ -0,0 +1,2 @@
export * from './interface.uts';
export * from './ak-req.uts';

View File

@@ -0,0 +1,48 @@
// ak-req 类型定义
export type AkReqOptions = {
url: string;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' |'HEAD';
data?: UTSJSONObject | Array<UTSJSONObject>;
headers?: UTSJSONObject;
timeout?: number;
contentType?: string; // 新增,支持顶级 contentType
// 可选:重试设置(仅网络错误/超时触发)。默认重试 0 次
retryCount?: number; // 最大重试次数,默认 0
retryDelayMs?: number; // 首次重试延迟,默认 300ms指数退避
};
// 上传参数类型定义
export type AkReqUploadOptions = {
url: string,
filePath: string,
name: string,
formData?: UTSJSONObject,
headers?: UTSJSONObject,
apikey?: string,
timeout?: number,
// 进度回调0-100注意H5/APP 平台支持不同)
onProgress?: (progress: number, transferredBytes?: number, totalBytes?: number) => void,
// 可选:重试设置(仅网络错误/超时触发)。默认 0
retryCount?: number,
retryDelayMs?: number
};
export type AkReqResponse<T = any> = {
status: number;
data: T | Array<T> | null; // 支持 null
headers: UTSJSONObject;
error: UniError | null;
total:number |null;
page: number |null;
limit: number |null;
hasmore:boolean |null;
origin: any | null;
};
export class AkReqError extends Error {
code: number;
constructor(message: string, code: number = 0) {
super(message);
this.code = code;
this.name = 'AkReqError';
}
}

View File

@@ -0,0 +1,9 @@
{
"name": "ak-req",
"version": "0.0.1",
"main": "ak-req.uts",
"types": "interface.uts",
"uni_modules": {
"uni_modules": true
}
}

View File

@@ -0,0 +1,101 @@
{
"id": "ak-sbsrv",
"displayName": "多蓝牙设备连接管理插件",
"version": "1.0.0",
"description": "支持多蓝牙设备连接管理的插件,可扫描、连接、发送数据等",
"keywords": [
"蓝牙",
"设备管理",
"BLE",
"多设备",
"连接管理"
],
"repository": "",
"engines": {
"HBuilderX": "^4.0.0",
"uni-app": "^3.1.0",
"uni-app-x": "^3.1.0"
},
"dcloudext": {
"type": "uts-plugin",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "设备蓝牙"
},
"npmurl": "",
"darkmode": "-",
"i18n": "-",
"widescreen": "-"
},
"uni_modules": {
"dependencies": [
],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "√",
"aliyun": "√"
},
"client": {
"uni-app": {
"vue": {
"vue2": "-",
"vue3": "-"
},
"web": {
"safari": "-",
"chrome": "-"
},
"app": {
"vue": "-",
"nvue": "-",
"android": "-",
"ios": "-",
"harmony": "-"
},
"mp": {
"weixin": "-",
"alipay": "-",
"toutiao": "-",
"baidu": "-",
"kuaishou": "-",
"jd": "-",
"harmony": "-",
"qq": "-",
"lark": "-"
},
"quickapp": {
"huawei": "-",
"union": "-"
}
},
"uni-app-x": {
"web": {
"safari": "-",
"chrome": "-"
},
"app": {
"android": "-",
"ios": "-",
"harmony": "-"
},
"mp": {
"weixin": "-"
}
}
}
}
}
}

View File

@@ -0,0 +1,115 @@
# ak-sbsrv
## 介绍
ak-sbsrv 是一个多蓝牙设备连接管理插件,基于 uni-app-x UTS 开发,支持同时连接和管理多个蓝牙设备。
主要特点:
- 支持多蓝牙设备的扫描、连接和通信
- 支持多种蓝牙协议BLE、SLE、BR/EDR等
- 支持多种数据格式JSON、XML、RAW等
- 统一接口多平台支持目前已支持H5平台
## 平台支持
- H5 (Chrome、Edge、Safari等支持Web Bluetooth API的现代浏览器)
- Android (开发中)
- iOS (开发中)
> H5端需要在支持Web Bluetooth API的浏览器及安全上下文HTTPS或localhost中使用
## 安装使用
1. 在插件市场下载或通过HBuilderX导入本插件
2. 导入到项目中
## API列表
### 基础功能
#### 扫描设备
```js
import { bluetoothService } from '@/uni_modules/ak-sbsrv/utssdk/web/index.uts';
// 扫描设备
const result = await bluetoothService.scanDevices();
console.log('扫描到设备:', result.devices);
```
#### 连接设备
```js
// 连接设备
await bluetoothService.connectDevice(deviceId);
```
#### 断开连接
```js
// 断开连接
await bluetoothService.disconnectDevice(deviceId);
```
#### 发送数据
```js
// 发送数据
await bluetoothService.sendData({
deviceId: '设备ID',
serviceId: '服务UUID',
characteristicId: '特征值UUID',
data: '要发送的数据',
format: 2 // 2代表RAW格式
});
```
### 事件监听
#### 监听连接状态变化
```js
// 监听连接状态变化
bluetoothService.onConnectionStateChange((deviceId, state) => {
console.log(`设备 ${deviceId} 连接状态变为: ${state}`);
// state: 0-断开1-连接中2-已连接3-断开中
});
```
#### 监听数据接收
```js
// 监听数据接收
bluetoothService.onDataReceived((payload) => {
console.log('收到数据:', payload);
});
```
#### 监听错误
```js
// 监听错误
bluetoothService.onError((error) => {
console.error('蓝牙错误:', error);
});
```
### 其他API
- `getConnectedDevices()` - 获取已连接设备列表
- `getConnectionState(deviceId)` - 获取指定设备的连接状态
- `listenCharacteristicNotify(deviceId, serviceId, characteristicId)` - 监听特征值通知
## 示例项目
参见仓库中的 [control.uvue](pages/control.uvue) 页面了解完整的使用示例。
## 常见问题
1. **H5中无法扫描到设备**
请确保浏览器支持Web Bluetooth API且页面在HTTPS或localhost环境下运行。
2. **扫描后无法连接设备?**
请确保设备在可连接范围内,且蓝牙服务已打开。
3. **发送数据失败?**
请检查serviceId和characteristicId是否正确以及特征值是否支持写入。
## 更新日志
### 1.0.0 (2025-04-24)
- 支持Web平台的蓝牙设备扫描、连接和数据收发
- 支持多设备同时连接管理
- 实现事件监听机制

View File

@@ -0,0 +1,283 @@
import type {
BleDevice,
BleConnectionState,
BleEvent,
BleEventCallback,
BleEventPayload,
BleScanResult,
BleConnectOptionsExt,
AutoBleInterfaces,
BleDataPayload,
SendDataPayload,
BleOptions,
MultiProtocolDevice,
ScanHandler,
BleProtocolType,
ScanDevicesOptions
} from '../interface.uts';
import { ProtocolHandler } from '../protocol_handler.uts';
import { BluetoothService } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
// Shape used when callers register plain objects as handlers. Using a named
// type keeps member access explicit so the code generator emits valid Kotlin
// member references instead of trying to access properties on Any.
type RawProtocolHandler = {
protocol?: BleProtocolType;
scanDevices?: (options?: ScanDevicesOptions) => Promise<void>;
connect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<void>;
disconnect?: (device: BleDevice) => Promise<void>;
sendData?: (device: BleDevice, payload?: SendDataPayload, options?: BleOptions) => Promise<void>;
autoConnect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
}
// 设备上下文
class DeviceContext {
device : BleDevice;
protocol : BleProtocolType;
state : BleConnectionState;
handler : ProtocolHandler;
constructor(device : BleDevice, protocol : BleProtocolType, handler : ProtocolHandler) {
this.device = device;
this.protocol = protocol;
this.state = 0; // DISCONNECTED
this.handler = handler;
}
}
const deviceMap = new Map<string, DeviceContext>(); // key: deviceId|protocol
// Single active protocol handler (no multi-protocol registration)
let activeProtocol: BleProtocolType = 'standard';
let activeHandler: ProtocolHandler | null = null;
// 事件监听注册表
const eventListeners = new Map<BleEvent, Set<BleEventCallback>>();
let defaultBluetoothService: BluetoothService | null = null;
function emit(event : BleEvent, payload : BleEventPayload) {
if (event == 'connectionStateChanged') {
console.log('[AKBLE][LOG] bluetooth_manager.uts emit connectionStateChanged', payload)
}
const listeners = eventListeners.get(event);
if (listeners != null) {
listeners.forEach(cb => {
try { cb(payload); } catch (e) { }
});
}
}
class ProtocolHandlerWrapper extends ProtocolHandler {
private _raw: RawProtocolHandler | null;
constructor(raw?: RawProtocolHandler, bluetoothService?: BluetoothService) {
super(bluetoothService);
this._raw = raw ?? null;
}
override async scanDevices(options?: ScanDevicesOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.scanDevices == 'function') {
await rawTyped.scanDevices(options);
}
return;
}
override async connect(device: BleDevice, options?: BleConnectOptionsExt): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.connect == 'function') {
await rawTyped.connect(device, options);
}
return;
}
override async disconnect(device: BleDevice): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.disconnect == 'function') {
await rawTyped.disconnect(device);
}
return;
}
override async sendData(device: BleDevice, payload?: SendDataPayload, options?: BleOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.sendData == 'function') {
await rawTyped.sendData(device, payload, options);
}
return;
}
override async autoConnect(device: BleDevice, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.autoConnect == 'function') {
return await rawTyped.autoConnect(device, options);
}
return { serviceId: '', writeCharId: '', notifyCharId: '' };
}
}
// Strong runtime detector for plain object handlers (no Type Predicate)
// Note: the UTS bundler doesn't support TypeScript type predicates (x is T),
// and it doesn't accept the 'unknown' type. This returns a boolean and
// callers must cast the value to RawProtocolHandler after the function
// returns true.
function isRawProtocolHandler(x: any): boolean {
if (x == null || typeof x !== 'object') return false;
const r = x as Record<string, unknown>;
if (typeof r['scanDevices'] == 'function') return true;
if (typeof r['connect'] == 'function') return true;
if (typeof r['disconnect'] == 'function') return true;
if (typeof r['sendData'] == 'function') return true;
if (typeof r['autoConnect'] == 'function') return true;
if (typeof r['protocol'] == 'string') return true;
return false;
}
export const registerProtocolHandler = (handler : any) => {
if (handler == null) return;
// Determine protocol value defensively. Default to 'standard' when unknown.
let proto: BleProtocolType = 'standard';
if (handler instanceof ProtocolHandler) {
try { proto = (handler as ProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = handler as ProtocolHandler;
} else if (isRawProtocolHandler(handler)) {
try { proto = (handler as RawProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = new ProtocolHandlerWrapper(handler as RawProtocolHandler, defaultBluetoothService);
(activeHandler as ProtocolHandler).protocol = proto;
} else {
console.warn('[AKBLE] registerProtocolHandler: unsupported handler type, ignoring', handler);
return;
}
activeProtocol = proto;
}
export const scanDevices = async (options ?: ScanDevicesOptions) : Promise<void> => {
ensureDefaultProtocolHandler();
console.log('[AKBLE] start scan', options)
// Determine which protocols to run: either user-specified or all registered
// Single active handler flow
if (activeHandler == null) {
console.log('[AKBLE] no active scan handler registered')
return
}
const handler = activeHandler as ProtocolHandler;
const scanOptions : ScanDevicesOptions = {
onDeviceFound: (device : BleDevice) => emit('deviceFound', { event: 'deviceFound', device }),
onScanFinished: () => emit('scanFinished', { event: 'scanFinished' })
}
try {
await handler.scanDevices(scanOptions)
} catch (e) {
console.warn('[AKBLE] scanDevices handler error', e)
}
}
export const connectDevice = async (deviceId : string, protocol : BleProtocolType, options ?: BleConnectOptionsExt) : Promise<void> => {
const handler = activeHandler;
if (handler == null) throw new Error('No protocol handler');
const device : BleDevice = { deviceId, name: '', rssi: 0 }; // 可扩展
await handler.connect(device, options);
const ctx = new DeviceContext(device, protocol, handler);
ctx.state = 2; // CONNECTED
deviceMap.set(getDeviceKey(deviceId, protocol), ctx);
console.log(deviceMap)
emit('connectionStateChanged', { event: 'connectionStateChanged', device, protocol, state: 2 });
}
export const disconnectDevice = async (deviceId : string, protocol : BleProtocolType) : Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null || ctx.handler == null) return;
await ctx.handler.disconnect(ctx.device);
ctx.state = 0;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol, state: 0 });
deviceMap.delete(getDeviceKey(deviceId, protocol));
}
export const sendData = async (payload : SendDataPayload, options ?: BleOptions) : Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(payload.deviceId, payload.protocol));
if (ctx == null) throw new Error('Device not connected');
// copy to local non-null variable so generator can smart-cast across awaits
const deviceCtx = ctx as DeviceContext;
if (deviceCtx.handler == null) throw new Error('sendData not supported for this protocol');
await deviceCtx.handler.sendData(deviceCtx.device, payload, options);
emit('dataSent', { event: 'dataSent', device: deviceCtx.device, protocol: payload.protocol, data: payload.data });
}
export const getConnectedDevices = () : MultiProtocolDevice[] => {
const result : MultiProtocolDevice[] = [];
deviceMap.forEach((ctx : DeviceContext) => {
const dev : MultiProtocolDevice = {
deviceId: ctx.device.deviceId,
name: ctx.device.name,
rssi: ctx.device.rssi,
protocol: ctx.protocol
};
result.push(dev);
});
return result;
}
export const getConnectionState = (deviceId : string, protocol : BleProtocolType) : BleConnectionState => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null) return 0;
return ctx.state;
}
export const on = (event : BleEvent, callback : BleEventCallback) => {
if (!eventListeners.has(event)) eventListeners.set(event, new Set());
eventListeners.get(event)!.add(callback);
}
export const off = (event : BleEvent, callback ?: BleEventCallback) => {
if (callback == null) {
eventListeners.delete(event);
} else {
eventListeners.get(event)?.delete(callback as BleEventCallback);
}
}
function getDeviceKey(deviceId : string, protocol : BleProtocolType) : string {
return `${deviceId}|${protocol}`;
}
export const autoConnect = async (deviceId : string, protocol : BleProtocolType, options ?: BleConnectOptionsExt) : Promise<AutoBleInterfaces> => {
const handler = activeHandler;
if (handler == null) throw new Error('autoConnect not supported for this protocol');
const device : BleDevice = { deviceId, name: '', rssi: 0 };
// safe call - handler.autoConnect exists on ProtocolHandler
return await handler.autoConnect(device, options) as AutoBleInterfaces;
}
function ensureDefaultProtocolHandler(): void {
if (activeHandler != null) return;
const service = defaultBluetoothService;
if (service == null) return;
try {
const _dm = DeviceManager.getInstance();
const _raw: RawProtocolHandler = {
protocol: 'standard',
scanDevices: (options?: ScanDevicesOptions) => {
try {
const scanOptions = options != null ? options : {} as ScanDevicesOptions;
_dm.startScan(scanOptions);
} catch (e) {
console.warn('[AKBLE] DeviceManager.startScan failed', e);
}
return Promise.resolve();
},
connect: (device, options?: BleConnectOptionsExt) => {
return _dm.connectDevice(device.deviceId, options);
},
disconnect: (device) => {
return _dm.disconnectDevice(device.deviceId);
},
autoConnect: (device, options?: any) => {
const result: AutoBleInterfaces = { serviceId: '', writeCharId: '', notifyCharId: '' };
return Promise.resolve(result);
}
};
const _wrapper = new ProtocolHandlerWrapper(_raw, service);
activeHandler = _wrapper;
activeProtocol = _raw.protocol as BleProtocolType;
console.log('[AKBLE] default protocol handler (BluetoothService-backed) registered', activeProtocol);
} catch (e) {
console.warn('[AKBLE] failed to register default protocol handler', e);
}
}
export const setDefaultBluetoothService = (service: BluetoothService) => {
defaultBluetoothService = service;
ensureDefaultProtocolHandler();
};

View File

@@ -0,0 +1,5 @@
{
"dependencies": [
]
}

View File

@@ -0,0 +1,311 @@
import type { BleDevice, BleOptions, BleConnectionState, BleConnectionStateChangeCallback } from '../interface.uts'
import type { BleConnectOptionsExt } from '../interface.uts'
import type { ScanDevicesOptions } from '../interface.uts';
import Context from "android.content.Context";
import BluetoothAdapter from "android.bluetooth.BluetoothAdapter";
import BluetoothManager from "android.bluetooth.BluetoothManager";
import BluetoothDevice from "android.bluetooth.BluetoothDevice";
import BluetoothGatt from "android.bluetooth.BluetoothGatt";
import BluetoothGattCallback from "android.bluetooth.BluetoothGattCallback";
import ScanCallback from "android.bluetooth.le.ScanCallback";
import ScanResult from "android.bluetooth.le.ScanResult";
import ScanSettings from "android.bluetooth.le.ScanSettings";
import Handler from "android.os.Handler";
import Looper from "android.os.Looper";
import ContextCompat from "androidx.core.content.ContextCompat";
import PackageManager from "android.content.pm.PackageManager";
// 定义 PendingConnect 类型和实现类
interface PendingConnect {
resolve: () => void;
reject: (err?: any) => void; // Changed to make err optional
timer?: number;
}
class PendingConnectImpl implements PendingConnect {
resolve: () => void;
reject: (err?: any) => void; // Changed to make err optional
timer?: number;
constructor(resolve: () => void, reject: (err?: any) => void, timer?: number) {
this.resolve = resolve;
this.reject = reject;
this.timer = timer;
}
}
// 引入全局回调管理
import { gattCallback } from './service_manager.uts'
const pendingConnects = new Map<string, PendingConnect>();
const STATE_DISCONNECTED = 0;
const STATE_CONNECTING = 1;
const STATE_CONNECTED = 2;
const STATE_DISCONNECTING = 3;
export class DeviceManager {
private static instance: DeviceManager | null = null;
private devices = new Map<string, BleDevice>();
private connectionStates = new Map<string, BleConnectionState>();
private connectionStateChangeListeners: BleConnectionStateChangeCallback[] = []
private gattMap = new Map<string, BluetoothGatt | null>();
private scanCallback: ScanCallback | null = null
private isScanning: boolean = false
private constructor() {}
static getInstance(): DeviceManager {
if (DeviceManager.instance == null) {
DeviceManager.instance = new DeviceManager();
}
return DeviceManager.instance!;
}
startScan(options: ScanDevicesOptions): void {
console.log('ak startscan now')
const adapter = this.getBluetoothAdapter();
if (adapter == null) {
throw new Error('未找到蓝牙适配器');
}
if (!adapter.isEnabled) {
// 尝试请求用户开启蓝牙
try {
adapter.enable(); // 直接调用,无需可选链和括号
} catch (e) {
// 某些设备可能不支持 enable
}
setTimeout(() => {
if (!adapter.isEnabled) {
throw new Error('蓝牙未开启');
}
}, 1500);
throw new Error('正在开启蓝牙,请重试');
}
const foundDevices = this.devices; // 直接用全局 devices
class MyScanCallback extends ScanCallback {
private foundDevices: Map<string, BleDevice>;
private onDeviceFound: (device: BleDevice) => void;
constructor(foundDevices: Map<string, BleDevice>, onDeviceFound: (device: BleDevice) => void) {
super();
this.foundDevices = foundDevices;
this.onDeviceFound = onDeviceFound;
}
override onScanResult(callbackType: Int, result: ScanResult): void {
const device = result.getDevice();
if (device != null) {
const deviceId = device.getAddress();
let bleDevice = foundDevices.get(deviceId);
if (bleDevice == null) {
bleDevice = {
deviceId,
name: device.getName() ?? 'Unknown',
rssi: result.getRssi(),
lastSeen: Date.now()
};
foundDevices.set(deviceId, bleDevice);
this.onDeviceFound(bleDevice);
} else {
// 更新属性(已确保 bleDevice 非空)
bleDevice.rssi = result.getRssi();
bleDevice.name = device.getName() ?? bleDevice.name;
bleDevice.lastSeen = Date.now();
}
}
}
override onScanFailed(errorCode: Int): void {
console.log('ak scan fail')
}
}
this.scanCallback = new MyScanCallback(foundDevices, options.onDeviceFound ?? (() => {}));
const scanner = adapter.getBluetoothLeScanner();
if (scanner == null) {
throw new Error('无法获取扫描器');
}
const scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
scanner.startScan(null, scanSettings, this.scanCallback);
this.isScanning = true;
// 默认10秒后停止扫描
new Handler(Looper.getMainLooper()).postDelayed(() => {
if (this.isScanning && this.scanCallback != null) {
scanner.stopScan(this.scanCallback);
this.isScanning = false;
// this.devices = foundDevices;
if (options.onScanFinished != null) options.onScanFinished?.invoke();
}
}, 40000);
}
async connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
console.log('[AKBLE] connectDevice called, deviceId:', deviceId, 'options:', options, 'connectionStates:')
const adapter = this.getBluetoothAdapter();
if (adapter == null) {
console.error('[AKBLE] connectDevice failed: 蓝牙适配器不可用')
throw new Error('蓝牙适配器不可用');
}
const device = adapter.getRemoteDevice(deviceId);
if (device == null) {
console.error('[AKBLE] connectDevice failed: 未找到设备', deviceId)
throw new Error('未找到设备');
}
this.connectionStates.set(deviceId, STATE_CONNECTING);
console.log('[AKBLE] connectDevice set STATE_CONNECTING, deviceId:', deviceId, 'connectionStates:')
this.emitConnectionStateChange(deviceId, STATE_CONNECTING);
const activity = UTSAndroid.getUniActivity();
const timeout = options?.timeout ?? 15000;
const key = `${deviceId}|connect`;
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
console.error('[AKBLE] connectDevice 超时:', deviceId)
pendingConnects.delete(key);
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
this.gattMap.set(deviceId, null);
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
reject(new Error('连接超时'));
}, timeout);
// 创建一个适配器函数来匹配类型签名
const resolveAdapter = () => {
console.log('[AKBLE] connectDevice resolveAdapter:', deviceId)
resolve();
};
const rejectAdapter = (err?: any) => {
console.error('[AKBLE] connectDevice rejectAdapter:', deviceId, err)
reject(err);
};
pendingConnects.set(key, new PendingConnectImpl(resolveAdapter, rejectAdapter, timer));
try {
console.log('[AKBLE] connectGatt 调用前:', deviceId)
const gatt = device.connectGatt(activity, false, gattCallback);
this.gattMap.set(deviceId, gatt);
console.log('[AKBLE] connectGatt 调用后:', deviceId, gatt)
} catch (e) {
console.error('[AKBLE] connectGatt 异常:', deviceId, e)
clearTimeout(timer);
pendingConnects.delete(key);
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
this.gattMap.set(deviceId, null);
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
reject(e);
}
});
}
// 统一分发连接回调(应在 gattCallback.onConnectionStateChange 内调用)
static handleConnectionStateChange(deviceId: string, newState: number, error?: any) {
console.log('[AKBLE] handleConnectionStateChange:', deviceId, 'newState:', newState, 'error:', error, 'pendingConnects:')
const key = `${deviceId}|connect`;
const cb = pendingConnects.get(key);
if (cb != null) {
// 修复 timer 的空安全问题,使用临时变量
const timerValue = cb.timer;
if (timerValue != null) {
clearTimeout(timerValue);
}
// 修复 error 处理
if (newState == STATE_CONNECTED) {
console.log('[AKBLE] handleConnectionStateChange: 连接成功', deviceId)
cb.resolve();
} else {
// 正确处理可空值
const errorToUse = error != null ? error : new Error('连接断开');
console.error('[AKBLE] handleConnectionStateChange: 连接失败', deviceId, errorToUse)
cb.reject(errorToUse);
}
pendingConnects.delete(key);
} else {
console.warn('[AKBLE] handleConnectionStateChange: 未找到 pendingConnects', deviceId, newState)
}
}
async disconnectDevice(deviceId: string, isActive: boolean = true): Promise<void> {
console.log('[AKBLE] disconnectDevice called, deviceId:', deviceId, 'isActive:', isActive)
let gatt = this.gattMap.get(deviceId);
if (gatt != null) {
gatt.disconnect();
gatt.close();
// gatt=null;
this.gattMap.set(deviceId, null);
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
console.log('[AKBLE] disconnectDevice set STATE_DISCONNECTED, deviceId:', deviceId, 'connectionStates:')
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
return;
} else {
console.log('[AKBLE] disconnectDevice: gatt is null, deviceId:', deviceId)
return;
}
}
async reconnectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
let attempts = 0;
const maxAttempts = options?.maxAttempts ?? 3;
const interval = options?.interval ?? 3000;
while (attempts < maxAttempts) {
try {
await this.disconnectDevice(deviceId, false);
await this.connectDevice(deviceId, options);
return;
} catch (e) {
attempts++;
if (attempts >= maxAttempts) throw new Error('重连失败');
// 修复 setTimeout 问题,使用旧式 Promise + setTimeout 解决
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, interval);
});
}
}
}
getConnectedDevices(): BleDevice[] {
// 创建一个空数组来存储结果
const result: BleDevice[] = [];
// 遍历 devices Map 并检查连接状态
this.devices.forEach((device, deviceId) => {
if (this.connectionStates.get(deviceId) == STATE_CONNECTED) {
result.push(device);
}
});
return result;
}
onConnectionStateChange(listener: BleConnectionStateChangeCallback) {
console.log('[AKBLE][LOG] onConnectionStateChange 注册, 当前监听数:', this.connectionStateChangeListeners.length + 1, listener)
this.connectionStateChangeListeners.push(listener)
}
protected emitConnectionStateChange(deviceId: string, state: BleConnectionState) {
console.log('[AKBLE][LOG] emitConnectionStateChange', deviceId, state, 'listeners:', this.connectionStateChangeListeners.length, 'connectionStates:', this.connectionStates)
for (const listener of this.connectionStateChangeListeners) {
try {
console.log('[AKBLE][LOG] emitConnectionStateChange 调用 listener', listener)
listener(deviceId, state)
} catch (e) {
console.error('[AKBLE][LOG] emitConnectionStateChange listener error', e)
}
}
}
getGattInstance(deviceId: string): BluetoothGatt | null {
return this.gattMap.get(deviceId) ?? null;
}
private getBluetoothAdapter(): BluetoothAdapter | null {
const context = UTSAndroid.getAppContext();
if (context == null) return null;
const manager = context?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager;
return manager.getAdapter();
}
/**
* 获取指定ID的设备如果存在
*/
public getDevice(deviceId: string): BleDevice | null {
console.log(deviceId,this.devices)
return this.devices.get(deviceId) ?? null;
}
}

View File

@@ -0,0 +1,670 @@
import { BleService } from '../interface.uts'
import type { WriteCharacteristicOptions, DfuOptions, ControlParserResult } from '../interface.uts'
import { DeviceManager } from './device_manager.uts'
import { ServiceManager } from './service_manager.uts'
import BluetoothGatt from 'android.bluetooth.BluetoothGatt'
import BluetoothGattCharacteristic from 'android.bluetooth.BluetoothGattCharacteristic'
import BluetoothGattDescriptor from 'android.bluetooth.BluetoothGattDescriptor'
import UUID from 'java.util.UUID'
// @ts-ignore missing ambient declaration provided by native bridge
import UTSError from "io.dcloud.uts.UTSError";
// 通用 Nordic DFU UUIDs (常见设备可能使用这些;如厂商自定义请替换)
const DFU_SERVICE_UUID = '0000fe59-0000-1000-8000-00805f9b34fb'
const DFU_CONTROL_POINT_UUID = '8ec90001-f315-4f60-9fb8-838830daea50'
const DFU_PACKET_UUID = '8ec90002-f315-4f60-9fb8-838830daea50'
type DfuSession = {
resolve : () => void;
reject : (err ?: any) => void;
onProgress ?: (p : number) => void;
onLog ?: (s : string) => void;
controlParser ?: (data : Uint8Array) => ControlParserResult | null;
// Nordic 专用字段
bytesSent ?: number;
totalBytes ?: number;
useNordic ?: boolean;
// PRN (packet receipt notification) support
prn ?: number;
packetsSincePrn ?: number;
prnResolve ?: () => void;
prnReject ?: (err ?: any) => void;
}
export class DfuManager {
// 会话表,用于把 control-point 通知路由到当前 DFU 流程
private sessions : Map<string, DfuSession> = new Map();
// 简化:只实现最基本的 GATT-based DFU 上传逻辑,需按设备协议调整 control point 的命令/解析
// Emit a DFU lifecycle event for a session. name should follow Nordic listener names
private _emitDfuEvent(deviceId : string, name : string, payload ?: any) {
console.log('[DFU][Event]', name, deviceId, payload ?? '');
const s = this.sessions.get(deviceId);
if (s == null) return;
if (typeof s.onLog == 'function') {
try {
const logFn = s.onLog as (msg : string) => void;
logFn(`[${name}] ${payload != null ? JSON.stringify(payload) : ''}`);
} catch (e) { }
}
if (name == 'onProgress' && typeof s.onProgress == 'function' && typeof payload == 'number') {
try { s.onProgress(payload); } catch (e) { }
}
}
async startDfu(deviceId : string, firmwareBytes : Uint8Array, options ?: DfuOptions) : Promise<void> {
console.log('startDfu 0')
const deviceManager = DeviceManager.getInstance();
const serviceManager = ServiceManager.getInstance();
console.log('startDfu 1')
const gatt : BluetoothGatt | null = deviceManager.getGattInstance(deviceId);
console.log('startDfu 2')
if (gatt == null) throw new Error('Device not connected');
console.log('[DFU] startDfu start deviceId=', deviceId, 'firmwareBytes=', firmwareBytes != null ? firmwareBytes.length : 0, 'options=', options);
try {
console.log('[DFU] requesting high connection priority for', deviceId);
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
} catch (e) {
console.warn('[DFU] requestConnectionPriority failed', e);
}
// 发现服务并特征
// ensure services discovered before accessing GATT; serviceManager exposes Promise-based API
await serviceManager.getServices(deviceId, null);
console.log('[DFU] services ensured for', deviceId);
const dfuService = gatt.getService(UUID.fromString(DFU_SERVICE_UUID));
if (dfuService == null) throw new Error('DFU service not found');
const controlChar = dfuService.getCharacteristic(UUID.fromString(DFU_CONTROL_POINT_UUID));
const packetChar = dfuService.getCharacteristic(UUID.fromString(DFU_PACKET_UUID));
console.log('[DFU] dfuService=', dfuService != null ? dfuService.getUuid().toString() : null, 'controlChar=', controlChar != null ? controlChar.getUuid().toString() : null, 'packetChar=', packetChar != null ? packetChar.getUuid().toString() : null);
if (controlChar == null || packetChar == null) throw new Error('DFU characteristics missing');
// Allow caller to request a desired MTU via options for higher throughput
const desiredMtu = (options != null && typeof options.mtu == 'number') ? options.mtu : 247;
try {
console.log('[DFU] requesting MTU=', desiredMtu, 'for', deviceId);
await this._requestMtu(gatt, desiredMtu, 8000);
console.log('[DFU] requestMtu completed for', deviceId);
} catch (e) {
console.warn('[DFU] requestMtu failed or timed out, continue with default.', e);
}
const mtu = desiredMtu; // 假定成功或使用期望值
const chunkSize = Math.max(20, mtu - 3);
// small helper to convert a byte (possibly signed) to a two-digit hex string
const byteToHex = (b : number) => {
const v = (b < 0) ? (b + 256) : b;
let s = v.toString(16);
if (s.length < 2) s = '0' + s;
return s;
};
// Parameterize PRN window and timeout via options early so they are available
// for session logging. Defaults: prn = 12 packets, prnTimeoutMs = 10000 ms
let prnWindow = 0;
if (options != null && typeof options.prn == 'number') {
prnWindow = Math.max(0, Math.floor(options.prn));
}
const prnTimeoutMs = (options != null && typeof options.prnTimeoutMs == 'number') ? Math.max(1000, Math.floor(options.prnTimeoutMs)) : 8000;
const disablePrnOnTimeout = !(options != null && options.disablePrnOnTimeout == false);
// 订阅 control point 通知并将通知路由到会话处理器
const controlHandler = (data : Uint8Array) => {
// 交给会话处理器解析并触发事件
try {
const hexParts: string[] = [];
for (let i = 0; i < data.length; i++) {
const v = data[i] as number;
hexParts.push(byteToHex(v));
}
const hex = hexParts.join(' ');
console.log('[DFU] control notification callback invoked for', deviceId, 'raw=', Array.from(data), 'hex=', hex);
} catch (e) {
console.log('[DFU] control notification callback invoked for', deviceId, 'raw=', Array.from(data));
}
this._handleControlNotification(deviceId, data);
};
console.log('[DFU] subscribing control point for', deviceId);
await serviceManager.subscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, controlHandler);
console.log('[DFU] subscribeCharacteristic returned for', deviceId);
// 保存会话回调(用于 waitForControlEvent; 支持 Nordic 模式追踪已发送字节
this.sessions.set(deviceId, {
resolve: () => { },
reject: (err ?: any) => {console.log(err) },
onProgress: null,
onLog: null,
controlParser: (data : Uint8Array) => this._defaultControlParser(data),
bytesSent: 0,
totalBytes: firmwareBytes.length,
useNordic: options != null && options.useNordic == true,
prn: null,
packetsSincePrn: 0,
prnResolve: null,
prnReject: null
});
console.log('[DFU] session created for', deviceId, 'totalBytes=', firmwareBytes.length);
console.log('[DFU] DFU session details:', { deviceId: deviceId, totalBytes: firmwareBytes.length, chunkSize: chunkSize, prnWindow: prnWindow, prnTimeoutMs: prnTimeoutMs });
// wire options callbacks into the session (if provided)
const sessRef = this.sessions.get(deviceId);
if (sessRef != null) {
sessRef.onProgress = (options != null && typeof options.onProgress == 'function') ? options.onProgress : null;
sessRef.onLog = (options != null && typeof options.onLog == 'function') ? options.onLog : null;
}
// emit initial lifecycle events (Nordic-like)
this._emitDfuEvent(deviceId, 'onDeviceConnecting', null);
// 写入固件数据(非常保守的实现:逐包写入并等待短延迟)
// --- PRN setup (optional, Nordic-style flow) ---
// Parameterize PRN window and timeout via options: options.prn, options.prnTimeoutMs
// Defaults were set earlier; build PRN payload using arithmetic to avoid
// bitwise operators which don't map cleanly to generated Kotlin.
if (prnWindow > 0) {
try {
// send Set PRN to device (format: [OP_CODE_SET_PRN, prn LSB, prn MSB])
// WARNING: Ensure your device uses the same opcode/format; change if needed.
const prnLsb = prnWindow % 256;
const prnMsb = Math.floor(prnWindow / 256) % 256;
const prnPayload = new Uint8Array([0x02, prnLsb, prnMsb]);
await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, prnPayload, null);
const sess0 = this.sessions.get(deviceId);
if (sess0 != null) {
sess0.useNordic = true;
sess0.prn = prnWindow;
sess0.packetsSincePrn = 0;
sess0.controlParser = (data : Uint8Array) => this._nordicControlParser(data);
}
console.log('[DFU] Set PRN sent (prn=', prnWindow, ') for', deviceId);
} catch (e) {
console.warn('[DFU] Set PRN failed (continuing without PRN):', e);
const sessFallback = this.sessions.get(deviceId);
if (sessFallback != null) sessFallback.prn = 0;
}
} else {
console.log('[DFU] PRN disabled (prnWindow=', prnWindow, ') for', deviceId);
}
// 写入固件数据(逐包写入并根据 options.waitForResponse 选择是否等待响应)
let offset = 0;
const total = firmwareBytes.length;
this._emitDfuEvent(deviceId, 'onDfuProcessStarted', null);
this._emitDfuEvent(deviceId, 'onUploadingStarted', null);
// Track outstanding write operations when using fire-and-forget mode so we can
// log and throttle if the Android stack becomes overwhelmed.
let outstandingWrites = 0;
// read tuning parameters from options in a safe, generator-friendly way
let configuredMaxOutstanding = 16;
let writeSleepMs = 0;
let writeRetryDelay = 20;
let writeMaxAttempts = 6;
let writeGiveupTimeout = 15000;
let drainOutstandingTimeout = 3000;
let failureBackoffMs = 0;
// throughput measurement
let throughputWindowBytes = 0;
let lastThroughputTime = Date.now();
function _logThroughputIfNeeded(force ?: boolean) {
try {
const now = Date.now();
const elapsed = now - lastThroughputTime;
if (force == true || elapsed >= 1000) {
const bytes = throughputWindowBytes;
const bps = Math.floor((bytes * 1000) / Math.max(1, elapsed));
// reset window
throughputWindowBytes = 0;
lastThroughputTime = now;
const human = `${bps} B/s`;
console.log('[DFU] throughput:', human, 'elapsedMs=', elapsed);
const s = this.sessions.get(deviceId);
if (s != null && typeof s.onLog == 'function') {
try { s.onLog?.invoke('[DFU] throughput: ' + human); } catch (e) { }
}
}
} catch (e) { }
}
function _safeErr(e ?: any) {
try {
if (e == null) return '';
if (typeof e == 'string') return e;
try { return JSON.stringify(e); } catch (e2) { }
try { return (e as any).toString(); } catch (e3) { }
return '';
} catch (e4) { return ''; }
}
try {
if (options != null) {
try {
if (options.maxOutstanding != null) {
const parsed = Math.floor(options.maxOutstanding as number);
if (!isNaN(parsed) && parsed > 0) configuredMaxOutstanding = parsed;
}
} catch (e) { }
try {
if (options.writeSleepMs != null) {
const parsedWs = Math.floor(options.writeSleepMs as number);
if (!isNaN(parsedWs) && parsedWs >= 0) writeSleepMs = parsedWs;
}
} catch (e) { }
try {
if (options.writeRetryDelayMs != null) {
const parsedRetry = Math.floor(options.writeRetryDelayMs as number);
if (!isNaN(parsedRetry) && parsedRetry >= 0) writeRetryDelay = parsedRetry;
}
} catch (e) { }
try {
if (options.writeMaxAttempts != null) {
const parsedAttempts = Math.floor(options.writeMaxAttempts as number);
if (!isNaN(parsedAttempts) && parsedAttempts > 0) writeMaxAttempts = parsedAttempts;
}
} catch (e) { }
try {
if (options.writeGiveupTimeoutMs != null) {
const parsedGiveupTimeout = Math.floor(options.writeGiveupTimeoutMs as number);
if (!isNaN(parsedGiveupTimeout) && parsedGiveupTimeout > 0) writeGiveupTimeout = parsedGiveupTimeout;
}
} catch (e) { }
try {
if (options.drainOutstandingTimeoutMs != null) {
const parsedDrain = Math.floor(options.drainOutstandingTimeoutMs as number);
if (!isNaN(parsedDrain) && parsedDrain >= 0) drainOutstandingTimeout = parsedDrain;
}
} catch (e) { }
}
if (configuredMaxOutstanding < 1) configuredMaxOutstanding = 1;
if (writeSleepMs < 0) writeSleepMs = 0;
} catch (e) { }
const maxOutstandingCeiling = configuredMaxOutstanding;
let adaptiveMaxOutstanding = configuredMaxOutstanding;
const minOutstandingWindow = 1;
while (offset < total) {
const end = Math.min(offset + chunkSize, total);
const slice = firmwareBytes.subarray(offset, end);
// Decide whether to wait for response per-chunk. Default to false for high throughput.
// Generator-friendly: avoid 'undefined' and use explicit boolean check.
let finalWaitForResponse = false;
if (options != null) {
try {
const maybe = options.waitForResponse;
if (maybe == true) finalWaitForResponse = true;
} catch (e) { finalWaitForResponse = false; }
}
const writeOpts: WriteCharacteristicOptions = {
waitForResponse: finalWaitForResponse,
retryDelayMs: writeRetryDelay,
maxAttempts: writeMaxAttempts,
giveupTimeoutMs: writeGiveupTimeout,
forceWriteTypeNoResponse: finalWaitForResponse == false
};
console.log('[DFU] writing packet chunk offset=', offset, 'len=', slice.length, 'waitForResponse=', finalWaitForResponse, 'outstanding=', outstandingWrites);
// Fire-and-forget path: do not await the write if waitForResponse == false.
if (finalWaitForResponse == false) {
if (failureBackoffMs > 0) {
console.log('[DFU] applying failure backoff', failureBackoffMs, 'ms before next write for', deviceId);
await this._sleep(failureBackoffMs);
failureBackoffMs = Math.floor(failureBackoffMs / 2);
}
while (outstandingWrites >= adaptiveMaxOutstanding) {
await this._sleep(Math.max(1, writeSleepMs));
}
// increment outstanding counter and kick the write without awaiting.
outstandingWrites = outstandingWrites + 1;
// fire-and-forget: start the write but don't await its Promise
const writeOffset = offset;
serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_PACKET_UUID, slice, writeOpts).then((res) => {
outstandingWrites = Math.max(0, outstandingWrites - 1);
if (res == true) {
if (adaptiveMaxOutstanding < maxOutstandingCeiling) {
adaptiveMaxOutstanding = Math.min(maxOutstandingCeiling, adaptiveMaxOutstanding + 1);
}
if (failureBackoffMs > 0) failureBackoffMs = Math.floor(failureBackoffMs / 2);
}
// log occasional completions
if ((outstandingWrites & 0x1f) == 0) {
console.log('[DFU] write completion callback, outstandingWrites=', outstandingWrites, 'adaptiveWindow=', adaptiveMaxOutstanding, 'device=', deviceId);
}
// detect write failure signaled by service manager
if (res !== true) {
adaptiveMaxOutstanding = Math.max(minOutstandingWindow, Math.floor(adaptiveMaxOutstanding / 2));
failureBackoffMs = Math.min(200, Math.max(failureBackoffMs, Math.max(5, writeRetryDelay)));
console.error('[DFU] writeCharacteristic returned false for device=', deviceId, 'offset=', writeOffset, 'adaptiveWindow now=', adaptiveMaxOutstanding);
try { this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: writeOffset, reason: 'write returned false' }); } catch (e) { }
}
}).catch((e) => {
outstandingWrites = Math.max(0, outstandingWrites - 1);
adaptiveMaxOutstanding = Math.max(minOutstandingWindow, Math.floor(adaptiveMaxOutstanding / 2));
failureBackoffMs = Math.min(200, Math.max(failureBackoffMs, Math.max(5, writeRetryDelay)));
console.warn('[DFU] fire-and-forget write failed for device=', deviceId, e, 'adaptiveWindow now=', adaptiveMaxOutstanding);
try {
const errMsg ='[DFU] fire-and-forget write failed for device=';
this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: writeOffset, reason: errMsg });
} catch (e2) { }
});
// account bytes for throughput
throughputWindowBytes += slice.length;
_logThroughputIfNeeded(false);
} else {
console.log('[DFU] awaiting write for chunk offset=', offset);
try {
const writeResult = await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_PACKET_UUID, slice, writeOpts);
if (writeResult !== true) {
console.error('[DFU] writeCharacteristic(await) returned false at offset=', offset, 'device=', deviceId);
try { this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: offset, reason: 'write returned false' }); } catch (e) { }
// abort DFU by throwing
throw new Error('write failed');
}
} catch (e) {
console.error('[DFU] awaiting write failed at offset=', offset, 'device=', deviceId, e);
try {
const errMsg = '[DFU] awaiting write failed ';
this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: offset, reason: errMsg });
} catch (e2) { }
throw e;
}
// account bytes for throughput
throughputWindowBytes += slice.length;
_logThroughputIfNeeded(false);
}
// update PRN counters and wait when window reached
const sessAfter = this.sessions.get(deviceId);
if (sessAfter != null && sessAfter.useNordic == true && typeof sessAfter.prn == 'number' && (sessAfter.prn ?? 0) > 0) {
sessAfter.packetsSincePrn = (sessAfter.packetsSincePrn ?? 0) + 1;
if ((sessAfter.packetsSincePrn ?? 0) >= (sessAfter.prn ?? 0) && (sessAfter.prn ?? 0) > 0) {
// wait for PRN (device notification) before continuing
try {
console.log('[DFU] reached PRN window, waiting for PRN for', deviceId, 'packetsSincePrn=', sessAfter.packetsSincePrn, 'prn=', sessAfter.prn);
await this._waitForPrn(deviceId, prnTimeoutMs);
console.log('[DFU] PRN received, resuming transfer for', deviceId);
} catch (e) {
console.warn('[DFU] PRN wait failed/timed out, continuing anyway for', deviceId, e);
if (disablePrnOnTimeout) {
console.warn('[DFU] disabling PRN waits after timeout for', deviceId);
sessAfter.prn = 0;
}
}
// reset counter
sessAfter.packetsSincePrn = 0;
}
}
offset = end;
// 如果启用 nordic 模式,统计已发送字节
const sess = this.sessions.get(deviceId);
if (sess != null && typeof sess.bytesSent == 'number') {
sess.bytesSent = (sess.bytesSent ?? 0) + slice.length;
}
// 简单节流与日志,避免过快。默认睡眠非常短以提高吞吐量; 可在设备上调节
console.log('[DFU] wrote chunk for', deviceId, 'offset=', offset, '/', total, 'chunkSize=', slice.length, 'bytesSent=', sess != null ? sess.bytesSent : null, 'outstanding=', outstandingWrites);
// emit upload progress event (percent) if available
if (sess != null && typeof sess.bytesSent == 'number' && typeof sess.totalBytes == 'number') {
const p = Math.floor((sess.bytesSent / sess.totalBytes) * 100);
this._emitDfuEvent(deviceId, 'onProgress', p);
}
// yield to event loop and avoid starving the Android BLE stack
await this._sleep(Math.max(0, writeSleepMs));
}
// wait for outstanding writes to drain before continuing with control commands
if (outstandingWrites > 0) {
const drainStart = Date.now();
while (outstandingWrites > 0 && (Date.now() - drainStart) < drainOutstandingTimeout) {
await this._sleep(Math.max(0, writeSleepMs));
}
if (outstandingWrites > 0) {
console.warn('[DFU] outstandingWrites remain after drain timeout, continuing with', outstandingWrites);
} else {
console.log('[DFU] outstandingWrites drained before control phase');
}
}
this._emitDfuEvent(deviceId, 'onUploadingCompleted', null);
// force final throughput log before activate/validate
_logThroughputIfNeeded(true);
// 发送 activate/validate 命令到 control point需根据设备协议实现
// 下面为占位:请替换为实际的 opcode
// 发送 validate/activate 命令到 control point需根据设备协议实现
try {
// control writes: pass undefined options explicitly to satisfy the generator/typechecker
const activatePayload = new Uint8Array([0x04]);
console.log('[DFU] sending activate/validate payload=', Array.from(activatePayload));
await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, activatePayload, null);
console.log('[DFU] activate/validate write returned for', deviceId);
} catch (e) {
console.warn('[DFU] activate/validate write failed (ignored) for', deviceId, e);
}
console.log('[DFU] sent control activate/validate command to control point for', deviceId);
this._emitDfuEvent(deviceId, 'onValidating', null);
// 等待 control-point 返回最终结果(成功或失败),超时可配置
try {
const timeout = 20000;
console.log('[DFU] waiting for control result (timeout=', timeout, ') for', deviceId);
await this._waitForControlResult(deviceId, timeout);
console.log('[DFU] control result resolved for', deviceId);
} catch (err) {
// 清理订阅后抛出
try { await serviceManager.unsubscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID); } catch (e) { }
this.sessions.delete(deviceId);
throw err;
}
// 取消订阅
try {
await serviceManager.unsubscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID);
} catch (e) { }
console.log('[DFU] unsubscribed control point for', deviceId);
// 清理会话
this.sessions.delete(deviceId);
console.log('[DFU] session cleared for', deviceId);
return;
}
async _requestMtu(gatt : BluetoothGatt, mtu : number, timeoutMs : number) : Promise<void> {
return new Promise<void>((resolve, reject) => {
// 在当前项目BluetoothGattCallback.onMtuChanged 未被封装;简单发起请求并等待短超时
try {
const ok = gatt.requestMtu(Math.floor(mtu) as Int);
if (!ok) {
return reject(new Error('requestMtu failed'));
}
} catch (e) {
return reject(e);
}
// 无 callback 监听时退回,等待一小段时间以便成功
setTimeout(() => resolve(), Math.min(2000, timeoutMs));
});
}
_sleep(ms : number) {
return new Promise<void>((r) => { setTimeout(() => { r() }, ms); });
}
_waitForPrn(deviceId : string, timeoutMs : number) : Promise<void> {
const session = this.sessions.get(deviceId);
if (session == null) return Promise.reject(new Error('no dfu session'));
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
// timeout waiting for PRN
// clear pending handlers
session.prnResolve = null;
session.prnReject = null;
reject(new Error('PRN timeout'));
}, timeoutMs);
const prnResolve = () => {
clearTimeout(timer);
resolve();
};
const prnReject = (err ?: any) => {
clearTimeout(timer);
reject(err);
};
session.prnResolve = prnResolve;
session.prnReject = prnReject;
});
}
// 默认 control point 解析器(非常通用的尝试解析:如果设备发送 progress byte 或成功码)
_defaultControlParser(data : Uint8Array) : ControlParserResult | null {
// 假设协议:第一个字节为 opcode, 第二字节可为状态或进度
if (data == null || data.length == 0) return null;
const op = data[0];
// 常见 Nordic: 0x10 = Response? 0x60/0x20 etc - 具体需按设备协议调整
// 这里做保守解析:若长度>=2 并且第二字节为 progress (0-100) 则返回 progress
if (data.length >= 2) {
const maybeProgress = data[1];
if (maybeProgress >= 0 && maybeProgress <= 100) {
return { type: 'progress', progress: maybeProgress };
}
}
// 若找到明显的 success opcode (示例 0x01) 或 error 0xFF
if (op == 0x01) return { type: 'success' };
if (op == 0xFF) return { type: 'error', error: data };
// vendor-specific opcode example: 0x60 may mean 'upload progress' for some firmwares
if (op == 0x60 && data.length >= 2) {
return { type: 'progress', progress: data[1] };
}
return { type: 'info' };
}
// Nordic DFU control-parser支持 Response and Packet Receipt Notification
_nordicControlParser(data : Uint8Array) : ControlParserResult | null {
// Nordic opcodes (简化):
// - 0x10 : Response (opcode, requestOp, resultCode)
// - 0x11 : Packet Receipt Notification (opcode, value LSB, value MSB)
if (data == null || data.length == 0) return null;
const op = data[0];
if (op == 0x11 && data.length >= 3) {
// packet receipt notif: bytes received (little endian)
const lsb = data[1];
const msb = data[2];
const received = (msb << 8) | lsb;
// Return received bytes as progress value; parser does not resolve device-specific session here.
return { type: 'progress', progress: received };
}
// Nordic vendor-specific progress opcode (example 0x60)
if (op == 0x60 && data.length >= 2) {
return { type: 'progress', progress: data[1] };
}
// Response: check result code for success (0x01 may indicate success in some stacks)
if (op == 0x10 && data.length >= 3) {
const requestOp = data[1];
const resultCode = data[2];
// Nordic resultCode 0x01 = SUCCESS typically
if (resultCode == 0x01) return { type: 'success' };
else return { type: 'error', error: { requestOp, resultCode } };
}
return null;
}
_handleControlNotification(deviceId : string, data : Uint8Array) {
const session = this.sessions.get(deviceId);
if (session == null) {
console.warn('[DFU] control notification received but no session for', deviceId, 'data=', Array.from(data));
return;
}
try {
// human readable opcode mapping
let opcodeName = 'unknown';
switch (data[0]) {
case 0x10: opcodeName = 'Response'; break;
case 0x11: opcodeName = 'PRN'; break;
case 0x60: opcodeName = 'VendorProgress'; break;
case 0x01: opcodeName = 'SuccessOpcode'; break;
case 0xFF: opcodeName = 'ErrorOpcode'; break;
}
console.log('[DFU] _handleControlNotification deviceId=', deviceId, 'opcode=0x' + data[0].toString(16), 'name=', opcodeName, 'raw=', Array.from(data));
const parsed = session.controlParser != null ? session.controlParser(data) : null;
if (session.onLog != null) session.onLog('DFU control notify: ' + Array.from(data).join(','));
console.log('[DFU] parsed control result=', parsed);
if (parsed == null) return;
if (parsed.type == 'progress' && parsed.progress != null) {
// 如果在 nordic 模式 parsed.progress 可能是已接收字节数,则转换为百分比
if (session.useNordic == true && session.totalBytes != null && session.totalBytes > 0) {
const percent = Math.floor((parsed.progress / session.totalBytes) * 100);
session.onProgress?.(percent);
// If we have written all bytes locally, log that event
if (session.bytesSent != null && session.totalBytes != null && session.bytesSent >= session.totalBytes) {
console.log('[DFU] all bytes written locally for', deviceId, 'bytesSent=', session.bytesSent, 'total=', session.totalBytes);
// emit uploading completed once
this._emitDfuEvent(deviceId, 'onUploadingCompleted', null);
}
// If a PRN wait is pending, resolve it (PRN indicates device received packets)
if (typeof session.prnResolve == 'function') {
try { session.prnResolve(); } catch (e) { }
session.prnResolve = null;
session.prnReject = null;
session.packetsSincePrn = 0;
}
} else {
const progress = parsed.progress
if (progress != null) {
console.log('[DFU] progress for', deviceId, 'progress=', progress);
session.onProgress?.(progress);
// also resolve PRN if was waiting (in case device reports numeric progress)
if (typeof session.prnResolve == 'function') {
try { session.prnResolve(); } catch (e) { }
session.prnResolve = null;
session.prnReject = null;
session.packetsSincePrn = 0;
}
}
}
} else if (parsed.type == 'success') {
console.log('[DFU] parsed success for', deviceId, 'resolving session');
session.resolve();
// Log final device-acknowledged success
console.log('[DFU] device reported DFU success for', deviceId);
this._emitDfuEvent(deviceId, 'onDfuCompleted', null);
} else if (parsed.type == 'error') {
console.error('[DFU] parsed error for', deviceId, parsed.error);
session.reject(parsed.error ?? new Error('DFU device error'));
this._emitDfuEvent(deviceId, 'onError', parsed.error ?? {});
} else {
// info - just log
}
} catch (e) {
session.onLog?.('control parse error: ' + e);
console.error('[DFU] control parse exception for', deviceId, e);
}
}
_waitForControlResult(deviceId : string, timeoutMs : number) : Promise<void> {
const session = this.sessions.get(deviceId);
if (session == null) return Promise.reject(new Error('no dfu session'));
return new Promise<void>((resolve, reject) => {
// wrap resolve/reject to clear timer
const timer = setTimeout(() => {
// 超时
console.error('[DFU] _waitForControlResult timeout for', deviceId);
reject(new Error('DFU control timeout'));
}, timeoutMs);
const origResolve = () => {
clearTimeout(timer);
console.log('[DFU] _waitForControlResult resolved for', deviceId);
resolve();
};
const origReject = (err ?: any) => {
clearTimeout(timer);
console.error('[DFU] _waitForControlResult rejected for', deviceId, 'err=', err);
reject(err);
};
// replace session handlers temporarily (guard nullable)
if (session != null) {
session.resolve = origResolve;
session.reject = origReject;
}
});
}
}
export const dfuManager = new DfuManager();

View File

@@ -0,0 +1,238 @@
import * as BluetoothManager from './bluetooth_manager.uts';
import { ServiceManager } from './service_manager.uts';
import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback, BleProtocolType, BluetoothService as BluetoothServiceContract } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
const serviceManager = ServiceManager.getInstance();
class AndroidBluetoothService implements BluetoothServiceContract {
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
return BluetoothManager.scanDevices(options);
}
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
}
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.disconnectDevice(deviceId, proto);
}
getConnectedDevices(): MultiProtocolDevice[] {
return BluetoothManager.getConnectedDevices();
}
on(event: BleEvent | string, callback: BleEventCallback): void {
BluetoothManager.on(event as BleEvent, callback);
}
off(event: BleEvent | string, callback?: BleEventCallback | null): void {
BluetoothManager.off(event as BleEvent, callback ?? null);
}
getServices(deviceId: string): Promise<BleService[]> {
return new Promise((resolve, reject) => {
serviceManager.getServices(deviceId, (list, err) => {
console.log('getServices:', list, err);
if (err != null) {
reject(err);
} else {
resolve((list as BleService[]) ?? []);
}
});
});
}
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
return new Promise((resolve, reject) => {
console.log(deviceId, serviceId);
serviceManager.getCharacteristics(deviceId, serviceId, (list, err) => {
if (err != null) {
reject(err);
} else {
resolve((list as BleCharacteristic[]) ?? []);
}
});
});
}
/**
* 自动发现服务和特征返回可用的写入和通知特征ID
* @param deviceId 设备ID
* @returns {Promise<AutoBleInterfaces>}
*/
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
// 1. 获取服务列表
const services = await this.getServices(deviceId);
if (services == null || services.length == 0) throw new Error('未发现服务');
// 2. 选择目标服务优先bae前缀可根据需要调整
let serviceId = '';
for (let i = 0; i < services.length; i++) {
const s = services[i];
const uuidCandidate: string | null = (s.uuid != null ? s.uuid : null)
const uuid: string = uuidCandidate != null ? uuidCandidate : ''
// prefer regex test to avoid nullable receiver calls in generated Kotlin
if (/^bae/i.test(uuid)) {
serviceId = uuid
break;
}
}
console.log(serviceId)
if (serviceId == null || serviceId == '') serviceId = services[0].uuid;
// 3. 获取特征列表
const characteristics = await this.getCharacteristics(deviceId, serviceId);
console.log(characteristics)
if (characteristics == null || characteristics.length == 0) throw new Error('未发现特征值');
// 4. 筛选write和notify特征
let writeCharId = '';
let notifyCharId = '';
for (let i = 0; i < characteristics.length; i++) {
const c = characteristics[i];
console.log(c)
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse==true)) writeCharId = c.uuid;
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) notifyCharId = c.uuid;
}
console.log(serviceId, writeCharId, notifyCharId);
if ((writeCharId == null || writeCharId == '') || (notifyCharId == null || notifyCharId == '')) throw new Error('未找到合适的写入或通知特征');
console.log(serviceId, writeCharId, notifyCharId);
// // 发现服务和特征后
const deviceManager = DeviceManager.getInstance();
console.log(deviceManager);
const device = deviceManager.getDevice(deviceId);
console.log(deviceId,device)
device!.serviceId = serviceId;
device!.writeCharId = writeCharId;
device!.notifyCharId = notifyCharId;
console.log(device);
return { serviceId, writeCharId, notifyCharId };
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
}
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
const payload = value instanceof Uint8Array ? value : new Uint8Array(value);
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, payload, options);
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
}
async autoDiscoverAll(deviceId: string): Promise<any> {
return serviceManager.autoDiscoverAll(deviceId);
}
async subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeAllNotifications(deviceId, onData);
}
}
export class BluetoothServiceShape implements BluetoothServiceContract {
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
return BluetoothManager.scanDevices(options);
}
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
}
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.disconnectDevice(deviceId, proto);
}
getConnectedDevices(): MultiProtocolDevice[] {
return BluetoothManager.getConnectedDevices();
}
on(event: BleEvent | string, callback: BleEventCallback): void {
BluetoothManager.on(event as BleEvent, callback);
}
off(event: BleEvent | string, callback?: BleEventCallback | null): void {
BluetoothManager.off(event as BleEvent, callback ?? null);
}
getServices(deviceId: string): Promise<BleService[]> {
return new Promise((resolve, reject) => {
serviceManager.getServices(deviceId, (list, err) => {
console.log('getServices:', list, err);
if (err != null) {
reject(err);
} else {
resolve((list as BleService[]) ?? []);
}
});
});
}
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
return new Promise((resolve, reject) => {
console.log(deviceId, serviceId);
serviceManager.getCharacteristics(deviceId, serviceId, (list, err) => {
if (err != null) {
reject(err);
} else {
resolve((list as BleCharacteristic[]) ?? []);
}
});
});
}
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
const services = await this.getServices(deviceId);
if (services == null || services.length == 0) throw new Error('未发现服务');
let serviceId = '';
for (let i = 0; i < services.length; i++) {
const s = services[i];
const uuidCandidate: string | null = (s.uuid != null ? s.uuid : null)
const uuid: string = uuidCandidate != null ? uuidCandidate : ''
if (/^bae/i.test(uuid)) {
serviceId = uuid
break;
}
}
console.log(serviceId)
if (serviceId == null || serviceId == '') serviceId = services[0].uuid;
const characteristics = await this.getCharacteristics(deviceId, serviceId);
if (characteristics == null || characteristics.length == 0) throw new Error('未发现特征值');
let writeCharId = '';
let notifyCharId = '';
for (let i = 0; i < characteristics.length; i++) {
const c = characteristics[i];
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse==true)) writeCharId = c.uuid;
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) notifyCharId = c.uuid;
}
if ((writeCharId == null || writeCharId == '') || (notifyCharId == null || notifyCharId == '')) throw new Error('未找到合适的写入或通知特征');
const deviceManager = DeviceManager.getInstance();
const device = deviceManager.getDevice(deviceId);
device!.serviceId = serviceId;
device!.writeCharId = writeCharId;
device!.notifyCharId = notifyCharId;
return { serviceId, writeCharId, notifyCharId };
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
}
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
const payload = value instanceof Uint8Array ? value : new Uint8Array(value);
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, payload, options);
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
}
async autoDiscoverAll(deviceId: string): Promise<any> {
return serviceManager.autoDiscoverAll(deviceId);
}
async subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeAllNotifications(deviceId, onData);
}
}
const bluetoothServiceInstance = new BluetoothServiceShape();
BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance);
export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance;
export function getBluetoothService(): BluetoothServiceShape {
return bluetoothServiceInstance;
}
// Ensure protocol handlers are registered when this module is imported.
// import './protocol_registry.uts';

View File

@@ -0,0 +1,815 @@
import type { BleService, BleCharacteristic, BleDataReceivedCallback, BleCharacteristicProperties, WriteCharacteristicOptions, ByteArray } from '../interface.uts';
import BluetoothGatt from "android.bluetooth.BluetoothGatt";
import BluetoothGattService from "android.bluetooth.BluetoothGattService";
import BluetoothGattCharacteristic from "android.bluetooth.BluetoothGattCharacteristic";
import BluetoothGattDescriptor from "android.bluetooth.BluetoothGattDescriptor";
import BluetoothGattCallback from "android.bluetooth.BluetoothGattCallback";
import UUID from "java.util.UUID";
import { DeviceManager } from './device_manager.uts';
import { AkBleErrorImpl, AkBluetoothErrorCode } from '../unierror.uts';
import { AutoDiscoverAllResult } from '../interface.uts';
// 补全UUID格式将短格式转换为标准格式
function getFullUuid(shortUuid: string): string {
return `0000${shortUuid}-0000-1000-8000-00805f9b34fb`;
}
const deviceWriteQueues = new Map<string, Promise<void>>();
function enqueueDeviceWrite<T>(deviceId: string, work: () => Promise<T>): Promise<T> {
const previous = deviceWriteQueues.get(deviceId) ?? Promise.resolve();
const next = (async (): Promise<T> => {
try {
await previous;
}
catch (e: any) { /* ignore previous rejection to keep queue alive */ }
return await work();
})();
const queued = next.then(() => { }, () => { });
deviceWriteQueues.set(deviceId, queued);
return next.finally(() => {
if (deviceWriteQueues.get(deviceId) == queued) {
deviceWriteQueues.delete(deviceId);
}
});
}
function createCharProperties(props: number): BleCharacteristicProperties {
const result: BleCharacteristicProperties = {
read: false,
write: false,
notify: false,
indicate: false,
canRead: false,
canWrite: false,
canNotify: false,
writeWithoutResponse: false
};
result.read = (props & BluetoothGattCharacteristic.PROPERTY_READ) !== 0;
result.write = (props & BluetoothGattCharacteristic.PROPERTY_WRITE) !== 0;
result.notify = (props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) !== 0;
result.indicate = (props & BluetoothGattCharacteristic.PROPERTY_INDICATE) !== 0;
result.writeWithoutResponse = (props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) !== 0;
result.canRead = result.read;
const writeWithoutResponse = result.writeWithoutResponse!;
result.canWrite = (result.write != null && result.write) || (writeWithoutResponse != null && writeWithoutResponse);
result.canNotify = result.notify;
return result;
}
// 定义 PendingCallback 类型和实现类
interface PendingCallback {
resolve: (data: any) => void;
reject: (err?: any) => void;
timer?: number; // Changed from any to number
}
class PendingCallbackImpl implements PendingCallback {
override resolve: (data: any) => void;
override reject: (err?: any) => void;
override timer?: number; // Changed from any to number
constructor(resolve: (data: any) => void, reject: (err?: any) => void, timer?: number) {
this.resolve = resolve;
this.reject = reject;
this.timer = timer;
}
}
// 全局回调管理(必须在类外部声明)
let pendingCallbacks: Map<string, PendingCallback>;
let notifyCallbacks: Map<string, BleDataReceivedCallback>;
// 在全局范围内初始化
pendingCallbacks = new Map<string, PendingCallback>();
notifyCallbacks = new Map<string, BleDataReceivedCallback>();
// 服务发现等待队列deviceId -> 回调数组
const serviceDiscoveryWaiters = new Map<string, ((services: BleService[] | null, error?: Error) => void)[]>();
// 服务发现状态deviceId -> 是否已发现
const serviceDiscovered = new Map<string, boolean>();
// 服务发现重试deviceId -> 尝试次数
const serviceDiscoveryAttempts = new Map<string, number>();
const SERVICE_DISCOVERY_MAX_RETRIES = 3;
const SERVICE_DISCOVERY_RETRY_DELAY_MS = 600;
// 特征发现等待队列deviceId|serviceId -> 回调数组
const characteristicDiscoveryWaiters = new Map<string, ((characteristics: BleCharacteristic[] | null, error?: Error) => void)[]>();
class GattCallback extends BluetoothGattCallback {
constructor() {
super();
}
override onServicesDiscovered(gatt: BluetoothGatt, status: Int): void {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:112', 'ak onServicesDiscovered',gatt);
const deviceId = gatt.getDevice().getAddress();
if (status == BluetoothGatt.GATT_SUCCESS) {
const attempt = serviceDiscoveryAttempts.get(deviceId) ?? 0;
const services = gatt.getServices();
const result: BleService[] = [];
let size = 0;
if (services != null) {
const servicesList = services;
size = servicesList.size;
for (let i = 0; i < size; i++) {
const service = servicesList.get(i as Int);
if (service != null) {
const bleService: BleService = {
uuid: service.getUuid().toString(),
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
};
result.push(bleService);
}
}
}
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:133', '[ServiceManager] onServicesDiscovered size=', size, 'attempt=', attempt, 'device=', deviceId);
if (result.length == 0 && attempt < SERVICE_DISCOVERY_MAX_RETRIES) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:135', '[ServiceManager] services empty after discovery, retrying', deviceId, 'nextAttempt=', attempt + 1);
serviceDiscoveryAttempts.set(deviceId, attempt + 1);
serviceDiscovered.delete(deviceId);
setTimeout(() => {
try {
const dm = DeviceManager.getInstance();
const currentGatt = dm.getGattInstance(deviceId);
const target = currentGatt != null ? currentGatt : gatt;
if (target != null) {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:144', '[ServiceManager] retry discoverServices for', deviceId);
target.discoverServices();
}
}
catch (e: any) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:148', '[ServiceManager] retry discoverServices failed', e);
}
}, SERVICE_DISCOVERY_RETRY_DELAY_MS);
return;
}
let finalResult: BleService[] | null = result;
if (result.length == 0) {
const cached = ServiceManager.getInstance().getCachedServices(deviceId);
if (cached != null && cached.length > 0) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:154', '[ServiceManager] discovery returned empty, using cached services for', deviceId, 'len=', cached.length);
finalResult = cached;
} else {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:154', '[ServiceManager] discovery returned empty after retries for', deviceId);
serviceDiscoveryAttempts.delete(deviceId);
const waitersFail = serviceDiscoveryWaiters.get(deviceId);
if (waitersFail != null && waitersFail.length > 0) {
for (let i = 0; i < waitersFail.length; i++) {
const cb = waitersFail[i];
if (cb != null) {
cb(null, new Error('服务发现返回空列表'));
}
}
serviceDiscoveryWaiters.delete(deviceId);
}
return;
}
}
serviceDiscoveryAttempts.delete(deviceId);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:154', `服务发现成功: ${deviceId}, services=${finalResult != null ? finalResult.length : 0}`);
serviceDiscovered.set(deviceId, true);
ServiceManager.getInstance().handleServicesDiscovered(deviceId, finalResult ?? []);
const waiters = serviceDiscoveryWaiters.get(deviceId);
if (waiters != null && waiters.length > 0) {
for (let i = 0; i < waiters.length; i++) {
const cb = waiters[i];
if (cb != null) {
cb(finalResult ?? [], null);
}
}
serviceDiscoveryWaiters.delete(deviceId);
}
}
else {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:166', `服务发现失败: ${deviceId}, status: ${status}`);
serviceDiscoveryAttempts.delete(deviceId);
// 失败时也要通知等待队列
const waiters = serviceDiscoveryWaiters.get(deviceId);
if (waiters != null && waiters.length > 0) {
for (let i = 0; i < waiters.length; i++) {
const cb = waiters[i];
if (cb != null) {
cb(null, new Error('服务发现失败'));
}
}
serviceDiscoveryWaiters.delete(deviceId);
}
}
}
override onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int): void {
const deviceId = gatt.getDevice().getAddress();
if (newState == BluetoothGatt.STATE_CONNECTED) {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:182', `设备已连接: ${deviceId}`);
ServiceManager.getInstance().resetDiscoveryState(deviceId);
DeviceManager.handleConnectionStateChange(deviceId, 2, null); // 2 = STATE_CONNECTED
}
else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:186', `设备已断开: ${deviceId}`);
serviceDiscovered.delete(deviceId);
serviceDiscoveryAttempts.delete(deviceId);
ServiceManager.getInstance().handleDisconnected(deviceId);
DeviceManager.handleConnectionStateChange(deviceId, 0, null); // 0 = STATE_DISCONNECTED
}
}
override onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic): void {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:194', 'ak onCharacteristicChanged');
const deviceId = gatt.getDevice().getAddress();
const serviceId = characteristic.getService().getUuid().toString();
const charId = characteristic.getUuid().toString();
const key = `${deviceId}|${serviceId}|${charId}|notify`;
const callback = notifyCallbacks.get(key);
const value = characteristic.getValue();
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:201', '[onCharacteristicChanged]', key, value);
// Check for PING packets (0xAA 0x04 0x00 ...)
if (value != null && value.size >= 4) {
const arr = new Uint8Array(value.size);
for (let i = 0 as Int; i < value.size; i++) {
const v = value[i as Int];
arr[i] = v != null ? v : 0;
}
if (arr[0] === 0xAA && arr[2] === 0x00) {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:201', '[BLE] PING packet detected:', Array.from(arr));
}
}
if (callback != null && value != null) {
const valueLength = value.size;
const arr = new Uint8Array(valueLength);
for (let i = 0 as Int; i < valueLength; i++) {
const v = value[i as Int];
arr[i] = v != null ? v : 0;
}
// 保存接收日志
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:210', `
INSERT INTO ble_data_log (device_id, service_id, char_id, direction, data, timestamp)
VALUES ('${deviceId}', '${serviceId}', '${charId}', 'recv', '${Array.from(arr).join(',')}', ${Date.now()})
`);
callback(arr);
}
}
override onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int): void {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:219', 'ak onCharacteristicRead', status);
const deviceId = gatt.getDevice().getAddress();
const serviceId = characteristic.getService().getUuid().toString();
const charId = characteristic.getUuid().toString();
const key = `${deviceId}|${serviceId}|${charId}|read`;
const pending = pendingCallbacks.get(key);
const value = characteristic.getValue();
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:226', '[onCharacteristicRead]', key, 'status=', status, 'value=', value);
if (pending != null) {
try {
const timer = pending.timer;
if (timer != null) {
clearTimeout(timer);
pending.timer = null;
}
pendingCallbacks.delete(key);
if (status == BluetoothGatt.GATT_SUCCESS && value != null) {
const valueLength = value.size;
const arr = new Uint8Array(valueLength);
for (let i = 0 as Int; i < valueLength; i++) {
const v = value[i as Int];
arr[i] = v != null ? v : 0;
}
// debug: log raw bytes and decoded string (helpful on Android native path)
try {
const hex = Array.from(arr).map((b): string => b.toString(16).padStart(2, '0')).join(' ');
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:246', '[ServiceManager] onCharacteristicRead raw hex:', hex, 'len=', arr.length, 'key=', key);
try {
const decoded = new TextDecoder().decode(arr);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:249', '[ServiceManager] onCharacteristicRead decoded string:', decoded);
}
catch (e: any) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:250', '[ServiceManager] decode error', e);
}
}
catch (e: any) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:251', '[ServiceManager] failed to log read buffer', e);
}
// resolve with ArrayBuffer
pending.resolve(arr.buffer as ArrayBuffer);
}
else {
pending.reject(new Error('Characteristic read failed'));
}
}
catch (e: any) {
try {
pending.reject(e);
}
catch (e2: any) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:259', e2);
}
}
}
}
override onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int): void {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:265', 'ak onCharacteristicWrite', status);
const deviceId = gatt.getDevice().getAddress();
const serviceId = characteristic.getService().getUuid().toString();
const charId = characteristic.getUuid().toString();
const key = `${deviceId}|${serviceId}|${charId}|write`;
const pending = pendingCallbacks.get(key);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:271', '[onCharacteristicWrite]', key, 'status=', status);
if (pending != null) {
try {
const timer = pending.timer;
if (timer != null) {
clearTimeout(timer);
}
pendingCallbacks.delete(key);
if (status == BluetoothGatt.GATT_SUCCESS) {
pending.resolve('ok');
}
else {
pending.reject(new Error('Characteristic write failed'));
}
}
catch (e: any) {
try {
pending.reject(e);
}
catch (e2: any) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:285', e2);
}
}
}
}
}
// 导出单例实例供外部使用
export const gattCallback = new GattCallback();
export class ServiceManager {
private static instance: ServiceManager | null = null;
private services = new Map<string, BleService[]>();
private characteristics = new Map<string, Map<string, BleCharacteristic[]>>();
private deviceManager = DeviceManager.getInstance();
private constructor() { }
static getInstance(): ServiceManager {
if (ServiceManager.instance == null) {
ServiceManager.instance = new ServiceManager();
}
return ServiceManager.instance!;
}
public resetDiscoveryState(deviceId: string, clearServiceCache: boolean = false): void {
serviceDiscoveryAttempts.delete(deviceId);
serviceDiscovered.delete(deviceId);
serviceDiscoveryWaiters.delete(deviceId);
if (clearServiceCache == true) {
this.services.delete(deviceId);
}
}
public handleServicesDiscovered(deviceId: string, services: BleService[]): void {
this.services.set(deviceId, services);
}
public getCachedServices(deviceId: string): BleService[] | null {
const cached = this.services.get(deviceId);
return cached != null ? cached : null;
}
public handleDisconnected(deviceId: string): void {
this.resetDiscoveryState(deviceId);
const keysToRemove: string[] = [];
this.characteristics.forEach((_value, key) => {
if (key != null && key.indexOf(deviceId + '|') == 0) {
keysToRemove.push(key);
}
});
for (let i = 0; i < keysToRemove.length; i++) {
this.characteristics.delete(keysToRemove[i]);
}
}
getServices(deviceId: string, callback?: (services: BleService[] | null, error?: Error) => void): any | Promise<BleService[]> {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts', 'ak start getservice', deviceId);
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null) {
if (callback != null) {
callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", ""));
}
return Promise.reject(new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", ""));
}
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts', 'ak serviceDiscovered', gatt);
// 如果服务已发现,直接返回
if (serviceDiscovered.get(deviceId) == true) {
const services = gatt.getServices();
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:333', services);
const result: BleService[] = [];
if (services != null) {
const servicesList = services;
const size = servicesList.size;
if (size > 0) {
for (let i = 0 as Int; i < size; i++) {
const service = servicesList != null ? servicesList.get(i) : servicesList[i];
if (service != null) {
const bleService: BleService = {
uuid: service.getUuid().toString(),
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
};
result.push(bleService);
if (bleService.uuid == getFullUuid('0001')) {
const device = this.deviceManager.getDevice(deviceId);
if (device != null) {
device.serviceId = bleService.uuid;
this.getCharacteristics(deviceId, device.serviceId!, (chars, err) => {
if (err == null && chars != null) {
const writeChar = chars.find((c): boolean => c.uuid == getFullUuid('0010'));
const notifyChar = chars.find((c): boolean => c.uuid == getFullUuid('0011'));
if (writeChar != null)
device.writeCharId = writeChar.uuid;
if (notifyChar != null)
device.notifyCharId = notifyChar.uuid;
}
});
}
}
}
}
}
}
if (callback != null) {
callback(result, null);
}
return Promise.resolve(result);
}
// 未发现则发起服务发现并加入等待队列
if (!serviceDiscoveryWaiters.has(deviceId)) {
console.log('ak should start serviceDiscoveryWaiters')
serviceDiscoveryWaiters.set(deviceId, []);
gatt.discoverServices();
}
return new Promise<BleService[]>((resolve, reject) => {
const cb = (services: BleService[] | null, error?: Error) => {
if (error != null)
reject(error);
else
resolve(services ?? []);
if (callback != null)
callback(services, error);
};
const arr = serviceDiscoveryWaiters.get(deviceId);
if (arr != null)
arr.push(cb);
});
}
getCharacteristics(deviceId: string, serviceId: string, callback: (characteristics: BleCharacteristic[] | null, error?: Error) => void): void {
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null)
return callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", ""));
// 如果服务还没发现,等待服务发现后再查特征
if (serviceDiscovered.get(deviceId) !== true) {
// 先注册到服务发现等待队列
this.getServices(deviceId, (services, err) => {
if (err != null) {
callback(null, err);
}
else {
this.getCharacteristics(deviceId, serviceId, callback);
}
});
return;
}
// 服务已发现,正常获取特征
const service = gatt.getService(UUID.fromString(serviceId));
if (service == null)
return callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", ""));
const chars = service.getCharacteristics();
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:403', chars);
const result: BleCharacteristic[] = [];
if (chars != null) {
const characteristicsList = chars;
const size = characteristicsList.size;
const bleService: BleService = {
uuid: serviceId,
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
};
for (let i = 0 as Int; i < size; i++) {
const char = characteristicsList != null ? characteristicsList.get(i as Int) : characteristicsList[i];
if (char != null) {
const props = char.getProperties();
try {
const charUuid = char.getUuid() != null ? char.getUuid().toString() : '';
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:418', '[ServiceManager] characteristic uuid=', charUuid);
}
catch (e: any) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:419', '[ServiceManager] failed to read char uuid', e);
}
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:420', props);
const bleCharacteristic: BleCharacteristic = {
uuid: char.getUuid().toString(),
service: bleService,
properties: createCharProperties(props)
};
result.push(bleCharacteristic);
}
}
}
callback(result, null);
}
public async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
const service = gatt.getService(UUID.fromString(serviceId));
if (service == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
const char = service.getCharacteristic(UUID.fromString(characteristicId));
if (char == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
const key = `${deviceId}|${serviceId}|${characteristicId}|read`;
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:441', key);
return new Promise<ArrayBuffer>((resolve, reject) => {
const timer = setTimeout(() => {
pendingCallbacks.delete(key);
reject(new AkBleErrorImpl(AkBluetoothErrorCode.ConnectionTimeout, "Connection timeout", ""));
}, 5000);
const resolveAdapter = (data: any) => { __f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:447', 'read resolve:', data); resolve(data as ArrayBuffer); };
const rejectAdapter = (err?: any) => { reject(new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Unknown error occurred", "")); };
pendingCallbacks.set(key, new PendingCallbackImpl(resolveAdapter, rejectAdapter, timer));
if (gatt.readCharacteristic(char) == false) {
clearTimeout(timer);
pendingCallbacks.delete(key);
reject(new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Unknown error occurred", ""));
}
else {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:456', 'read should be succeed', key);
}
});
}
public async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: Uint8Array, options?: WriteCharacteristicOptions): Promise<boolean> {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:462', '[writeCharacteristic] deviceId:', deviceId, 'serviceId:', serviceId, 'characteristicId:', characteristicId, 'data:', data);
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:465', '[writeCharacteristic] gatt is null');
throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
}
const service = gatt.getService(UUID.fromString(serviceId));
if (service == null) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:470', '[writeCharacteristic] service is null');
throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
}
const char = service.getCharacteristic(UUID.fromString(characteristicId));
if (char == null) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:475', '[writeCharacteristic] characteristic is null');
throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
}
const key = `${deviceId}|${serviceId}|${characteristicId}|write`;
const wantsNoResponse = options != null && options.waitForResponse == false;
let retryMaxAttempts = 20;
let retryDelay = 100;
let giveupTimeout = 20000;
if (options != null) {
try {
if (options.maxAttempts != null) {
const parsedAttempts = Math.floor(options.maxAttempts as number);
if (!isNaN(parsedAttempts) && parsedAttempts > 0)
retryMaxAttempts = parsedAttempts;
}
}
catch (e: any) { }
try {
if (options.retryDelayMs != null) {
const parsedDelay = Math.floor(options.retryDelayMs as number);
if (!isNaN(parsedDelay) && parsedDelay >= 0)
retryDelay = parsedDelay;
}
}
catch (e: any) { }
try {
if (options.giveupTimeoutMs != null) {
const parsedGiveup = Math.floor(options.giveupTimeoutMs as number);
if (!isNaN(parsedGiveup) && parsedGiveup > 0)
giveupTimeout = parsedGiveup;
}
}
catch (e: any) { }
}
const gattInstance = gatt;
const executeWrite = (): Promise<boolean> => {
return new Promise<boolean>((resolve, _reject) => {
const initialTimeout = Math.max(giveupTimeout + 5000, 10000);
let timer = setTimeout(() => {
pendingCallbacks.delete(key);
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:510', '[writeCharacteristic] timeout');
resolve(false);
}, initialTimeout);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:513', '[writeCharacteristic] initial timeout set to', initialTimeout, 'ms for', key);
const resolveAdapter = (data: any) => {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:515', '[writeCharacteristic] resolveAdapter called');
resolve(true);
};
const rejectAdapter = (err?: any) => {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:519', '[writeCharacteristic] rejectAdapter called', err);
resolve(false);
};
pendingCallbacks.set(key, new PendingCallbackImpl(resolveAdapter, rejectAdapter, timer));
const byteArray = new ByteArray(data.length as Int);
for (let i = 0 as Int; i < data.length; i++) {
byteArray[i] = data[i].toByte();
}
const forceWriteTypeNoResponse = options != null && options.forceWriteTypeNoResponse == true;
let usesNoResponse = forceWriteTypeNoResponse || wantsNoResponse;
try {
if (usesNoResponse == false) {
const props = char.getProperties();
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:532', '[writeCharacteristic] characteristic properties mask=', props);
usesNoResponse = (props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) !== 0;
}
if (usesNoResponse) {
try {
char.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
}
catch (e: any) { }
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:537', '[writeCharacteristic] using WRITE_TYPE_NO_RESPONSE');
}
else {
try {
char.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
}
catch (e: any) { }
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:540', '[writeCharacteristic] using WRITE_TYPE_DEFAULT');
}
}
catch (e: any) {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:543', '[writeCharacteristic] failed to inspect/set write type', e);
}
const maxAttempts = retryMaxAttempts;
function attemptWrite(att: Int): void {
try {
let setOk = true;
try {
const setRes = char.setValue(byteArray);
if (typeof setRes == 'boolean' && setRes == false) {
setOk = false;
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:553', '[writeCharacteristic] setValue returned false for', key, 'attempt', att);
}
}
catch (e: any) {
setOk = false;
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:557', '[writeCharacteristic] setValue threw for', key, 'attempt', att, e);
}
if (setOk == false) {
if (att >= maxAttempts) {
try {
clearTimeout(timer);
}
catch (e: any) { }
pendingCallbacks.delete(key);
resolve(false);
return;
}
setTimeout(() => { attemptWrite((att + 1) as Int); }, retryDelay);
return;
}
try {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:570', '[writeCharacteristic] attempt', att, 'calling gatt.writeCharacteristic');
const r = gattInstance.writeCharacteristic(char);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:572', '[writeCharacteristic] attempt', att, 'result=', r);
if (r == true) {
if (usesNoResponse) {
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:575', '[writeCharacteristic] WRITE_TYPE_NO_RESPONSE success for', key);
try {
clearTimeout(timer);
}
catch (e: any) { }
pendingCallbacks.delete(key);
resolve(true);
return;
}
try {
clearTimeout(timer);
}
catch (e: any) { }
const extra = 20000;
timer = setTimeout(() => {
pendingCallbacks.delete(key);
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:585', '[writeCharacteristic] timeout after write initiated');
resolve(false);
}, extra);
const pendingEntry = pendingCallbacks.get(key);
if (pendingEntry != null)
pendingEntry.timer = timer;
return;
}
}
catch (e: any) {
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:593', '[writeCharacteristic] attempt', att, 'exception when calling writeCharacteristic', e);
}
if (att < maxAttempts) {
const nextAtt = (att + 1) as Int;
setTimeout(() => { attemptWrite(nextAtt); }, retryDelay);
return;
}
if (usesNoResponse) {
try {
clearTimeout(timer);
}
catch (e: any) { }
pendingCallbacks.delete(key);
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:603', '[writeCharacteristic] all attempts failed with WRITE_NO_RESPONSE for', key);
resolve(false);
return;
}
try {
clearTimeout(timer);
}
catch (e: any) { }
const giveupTimeoutLocal = giveupTimeout;
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:609', '[writeCharacteristic] all attempts failed; waiting for late callback up to', giveupTimeoutLocal, 'ms for', key);
const giveupTimer = setTimeout(() => {
pendingCallbacks.delete(key);
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:612', '[writeCharacteristic] giveup timeout expired for', key);
resolve(false);
}, giveupTimeoutLocal);
const pendingEntryAfter = pendingCallbacks.get(key);
if (pendingEntryAfter != null)
pendingEntryAfter.timer = giveupTimer;
}
catch (e: any) {
clearTimeout(timer);
pendingCallbacks.delete(key);
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:620', '[writeCharacteristic] Exception in attemptWrite', e);
resolve(false);
}
}
try {
attemptWrite(1 as Int);
}
catch (e: any) {
clearTimeout(timer);
pendingCallbacks.delete(key);
__f__('error', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:630', '[writeCharacteristic] Exception before attempting write', e);
resolve(false);
}
});
};
return enqueueDeviceWrite(deviceId, executeWrite);
}
public async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
const service = gatt.getService(UUID.fromString(serviceId));
if (service == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
const char = service.getCharacteristic(UUID.fromString(characteristicId));
if (char == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
const key = `${deviceId}|${serviceId}|${characteristicId}|notify`;
notifyCallbacks.set(key, onData);
if (gatt.setCharacteristicNotification(char, true) == false) {
notifyCallbacks.delete(key);
throw new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Failed to unsubscribe characteristic", "");
}
else {
// 写入 CCCD 描述符,启用 notify
const descriptor = char.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (descriptor != null) {
// 设置描述符值
const value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
descriptor.setValue(value);
const writedescript = gatt.writeDescriptor(descriptor);
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:660', 'subscribeCharacteristic: CCCD written for notify', writedescript);
}
else {
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:662', 'subscribeCharacteristic: CCCD descriptor not found!');
}
__f__('log', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:664', 'subscribeCharacteristic ok!!');
}
}
public async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
const gatt = this.deviceManager.getGattInstance(deviceId);
if (gatt == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
const service = gatt.getService(UUID.fromString(serviceId));
if (service == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
const char = service.getCharacteristic(UUID.fromString(characteristicId));
if (char == null)
throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
const key = `${deviceId}|${serviceId}|${characteristicId}|notify`;
notifyCallbacks.delete(key);
if (gatt.setCharacteristicNotification(char, false) == false) {
throw new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Failed to unsubscribe characteristic", "");
}
}
// 自动发现所有服务和特征
public async autoDiscoverAll(deviceId: string): Promise<AutoDiscoverAllResult> {
const services = await this.getServices(deviceId, null) as BleService[];
const allCharacteristics: BleCharacteristic[] = [];
for (const service of services) {
await new Promise<void>((resolve, reject) => {
this.getCharacteristics(deviceId, service.uuid, (chars, err) => {
if (err != null)
reject(err);
else {
if (chars != null)
allCharacteristics.push(...chars);
resolve(void 0);
}
});
});
}
return { services, characteristics: allCharacteristics } as AutoDiscoverAllResult;
}
// 自动订阅所有支持 notify/indicate 的特征
public async subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
const { services, characteristics } = await this.autoDiscoverAll(deviceId);
for (const char of characteristics) {
if (char.properties.notify || char.properties.indicate) {
try {
await this.subscribeCharacteristic(deviceId, char.service.uuid, char.uuid, onData);
}
catch (e: any) {
// 可以选择忽略单个特征订阅失败
__f__('warn', 'at uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts:710', `订阅特征 ${char.uuid} 失败:`, e);
}
}
}
}
}
//# sourceMappingURL=service_manager.uts.map

View File

@@ -0,0 +1,281 @@
import type {
BleDevice,
BleConnectionState,
BleEvent,
BleEventCallback,
BleEventPayload,
BleConnectOptionsExt,
AutoBleInterfaces,
SendDataPayload,
BleOptions,
MultiProtocolDevice,
BleProtocolType,
ScanDevicesOptions
} from '../interface.uts';
import { ProtocolHandler } from '../protocol_handler.uts';
import { BluetoothService } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
type RawProtocolHandler = {
protocol?: BleProtocolType;
scanDevices?: (options?: ScanDevicesOptions) => Promise<void>;
connect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<void>;
disconnect?: (device: BleDevice) => Promise<void>;
sendData?: (device: BleDevice, payload?: SendDataPayload, options?: BleOptions) => Promise<void>;
autoConnect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
}
class DeviceContext {
device: BleDevice;
protocol: BleProtocolType;
state: BleConnectionState;
handler: ProtocolHandler;
constructor(device: BleDevice, protocol: BleProtocolType, handler: ProtocolHandler) {
this.device = device;
this.protocol = protocol;
this.state = 0;
this.handler = handler;
}
}
const deviceMap = new Map<string, DeviceContext>();
let activeProtocol: BleProtocolType = 'standard';
let activeHandler: ProtocolHandler | null = null;
const eventListeners = new Map<BleEvent, Set<BleEventCallback>>();
let defaultBluetoothService: BluetoothService | null = null;
let connectionHooked = false;
function emit(event: BleEvent, payload: BleEventPayload) {
const listeners = eventListeners.get(event);
if (listeners != null) {
listeners.forEach(cb => {
try { cb(payload); } catch (e) { }
});
}
}
class ProtocolHandlerWrapper extends ProtocolHandler {
private _raw: RawProtocolHandler | null;
constructor(raw?: RawProtocolHandler, bluetoothService?: BluetoothService) {
super(bluetoothService);
this._raw = raw ?? null;
}
override async scanDevices(options?: ScanDevicesOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.scanDevices == 'function') {
await rawTyped.scanDevices(options);
}
return;
}
override async connect(device: BleDevice, options?: BleConnectOptionsExt): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.connect == 'function') {
await rawTyped.connect(device, options);
}
return;
}
override async disconnect(device: BleDevice): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.disconnect == 'function') {
await rawTyped.disconnect(device);
}
return;
}
override async sendData(device: BleDevice, payload?: SendDataPayload, options?: BleOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.sendData == 'function') {
await rawTyped.sendData(device, payload, options);
}
return;
}
override async autoConnect(device: BleDevice, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.autoConnect == 'function') {
return await rawTyped.autoConnect(device, options);
}
return { serviceId: '', writeCharId: '', notifyCharId: '' };
}
}
function isRawProtocolHandler(x: any): boolean {
if (x == null || typeof x !== 'object') return false;
const r = x as Record<string, unknown>;
if (typeof r['scanDevices'] == 'function') return true;
if (typeof r['connect'] == 'function') return true;
if (typeof r['disconnect'] == 'function') return true;
if (typeof r['sendData'] == 'function') return true;
if (typeof r['autoConnect'] == 'function') return true;
if (typeof r['protocol'] == 'string') return true;
return false;
}
function ensureConnectionHook() {
if (connectionHooked) return;
connectionHooked = true;
const dm = DeviceManager.getInstance();
dm.onConnectionStateChange((deviceId, state) => {
let handled = false;
deviceMap.forEach((ctx) => {
if (ctx.device.deviceId == deviceId) {
ctx.state = state;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol: ctx.protocol, state });
handled = true;
}
});
if (!handled) {
emit('connectionStateChanged', { event: 'connectionStateChanged', device: { deviceId, name: '', rssi: 0 }, protocol: activeProtocol, state });
}
});
}
export const registerProtocolHandler = (handler: any) => {
if (handler == null) return;
let proto: BleProtocolType = 'standard';
if (handler instanceof ProtocolHandler) {
try { proto = (handler as ProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = handler as ProtocolHandler;
} else if (isRawProtocolHandler(handler)) {
try { proto = (handler as RawProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = new ProtocolHandlerWrapper(handler as RawProtocolHandler, defaultBluetoothService);
(activeHandler as ProtocolHandler).protocol = proto;
} else {
console.warn('[AKBLE][Harmony] registerProtocolHandler unsupported handler', handler);
return;
}
activeProtocol = proto;
ensureConnectionHook();
}
export const scanDevices = async (options?: ScanDevicesOptions): Promise<void> => {
ensureDefaultProtocolHandler();
if (activeHandler == null) {
console.log('[AKBLE][Harmony] no active scan handler');
return;
}
const handler = activeHandler as ProtocolHandler;
const original = options ?? null;
const scanOptions: ScanDevicesOptions = {} as ScanDevicesOptions;
if (original != null) {
if (original.protocols != null) scanOptions.protocols = original.protocols;
if (original.optionalServices != null) scanOptions.optionalServices = original.optionalServices;
if (original.timeout != null) scanOptions.timeout = original.timeout;
}
const userFound = original?.onDeviceFound ?? null;
scanOptions.onDeviceFound = (device: BleDevice) => {
emit('deviceFound', { event: 'deviceFound', device });
if (userFound != null) {
try { userFound(device); } catch (err) { }
}
};
const userFinished = original?.onScanFinished ?? null;
scanOptions.onScanFinished = () => {
emit('scanFinished', { event: 'scanFinished' });
if (userFinished != null) {
try { userFinished(); } catch (err) { }
}
};
try {
await handler.scanDevices(scanOptions);
} catch (e) {
console.warn('[AKBLE][Harmony] scan handler error', e);
}
}
export const connectDevice = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<void> => {
const handler = activeHandler;
if (handler == null) throw new Error('No protocol handler');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
await handler.connect(device, options);
const ctx = new DeviceContext(device, protocol, handler);
ctx.state = 2;
deviceMap.set(getDeviceKey(deviceId, protocol), ctx);
emit('connectionStateChanged', { event: 'connectionStateChanged', device, protocol, state: 2 });
}
export const disconnectDevice = async (deviceId: string, protocol: BleProtocolType): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null || ctx.handler == null) return;
await ctx.handler.disconnect(ctx.device);
ctx.state = 0;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol, state: 0 });
deviceMap.delete(getDeviceKey(deviceId, protocol));
}
export const sendData = async (payload: SendDataPayload, options?: BleOptions): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(payload.deviceId, payload.protocol));
if (ctx == null) throw new Error('Device not connected');
if (ctx.handler == null) throw new Error('sendData not supported for this protocol');
await ctx.handler.sendData(ctx.device, payload, options);
emit('dataSent', { event: 'dataSent', device: ctx.device, protocol: payload.protocol, data: payload.data });
}
export const getConnectedDevices = (): MultiProtocolDevice[] => {
const result: MultiProtocolDevice[] = [];
deviceMap.forEach((ctx: DeviceContext) => {
result.push({
deviceId: ctx.device.deviceId,
name: ctx.device.name,
rssi: ctx.device.rssi,
protocol: ctx.protocol
});
});
return result;
}
export const getConnectionState = (deviceId: string, protocol: BleProtocolType): BleConnectionState => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null) return 0;
return ctx.state;
}
export const on = (event: BleEvent, callback: BleEventCallback) => {
if (!eventListeners.has(event)) eventListeners.set(event, new Set());
eventListeners.get(event)!.add(callback);
}
export const off = (event: BleEvent, callback?: BleEventCallback) => {
if (callback == null) {
eventListeners.delete(event);
} else {
eventListeners.get(event)?.delete(callback as BleEventCallback);
}
}
function getDeviceKey(deviceId: string, protocol: BleProtocolType): string {
return `${deviceId}|${protocol}`;
}
export const autoConnect = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> => {
const handler = activeHandler;
if (handler == null) throw new Error('autoConnect not supported for this protocol');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
return await handler.autoConnect(device, options) as AutoBleInterfaces;
}
function ensureDefaultProtocolHandler(): void {
if (activeHandler != null) return;
const service = defaultBluetoothService;
if (service == null) return;
try {
const dm = DeviceManager.getInstance();
const raw: RawProtocolHandler = {
protocol: 'standard',
scanDevices: (options?: ScanDevicesOptions) => dm.startScan(options ?? {} as ScanDevicesOptions),
connect: (device, options?: BleConnectOptionsExt) => dm.connectDevice(device.deviceId, options),
disconnect: (device) => dm.disconnectDevice(device.deviceId),
autoConnect: () => Promise.resolve({ serviceId: '', writeCharId: '', notifyCharId: '' })
};
const wrapper = new ProtocolHandlerWrapper(raw, service);
activeHandler = wrapper;
activeProtocol = raw.protocol as BleProtocolType;
ensureConnectionHook();
console.log('[AKBLE][Harmony] default protocol handler registered', activeProtocol);
} catch (e) {
console.warn('[AKBLE][Harmony] register default protocol handler failed', e);
}
}
export const setDefaultBluetoothService = (service: BluetoothService) => {
defaultBluetoothService = service;
ensureDefaultProtocolHandler();
};

View File

@@ -0,0 +1,6 @@
{
"dependencies": [
"@ohos.bluetooth.ble",
"@ohos.base"
]
}

View File

@@ -0,0 +1,280 @@
import type { BleDevice, BleConnectOptionsExt, BleConnectionState, BleConnectionStateChangeCallback, ScanDevicesOptions } from '../interface.uts';
import ble from '@ohos.bluetooth.ble';
import type { BusinessError } from '@ohos.base';
type PendingConnect = {
resolve: () => void;
reject: (err?: any) => void;
timer?: number;
};
function now(): number {
return Date.now();
}
export class DeviceManager {
private static instance: DeviceManager | null = null;
private central: any | null = null;
private devices = new Map<string, BleDevice>();
private connectionStates = new Map<string, BleConnectionState>();
private connectionListeners: BleConnectionStateChangeCallback[] = [];
private pendingConnects = new Map<string, PendingConnect>();
private scanOptions: ScanDevicesOptions | null = null;
private scanTimer: number | null = null;
private gattMap = new Map<string, any>();
private scanning: boolean = false;
private eventsBound: boolean = false;
private constructor() {}
static getInstance(): DeviceManager {
if (DeviceManager.instance == null) {
DeviceManager.instance = new DeviceManager();
}
return DeviceManager.instance!;
}
private ensureCentral(): any {
if (this.central != null) return this.central;
try {
this.central = ble.createBluetoothCentralManager();
this.bindCentralEvents();
} catch (e) {
console.warn('[AKBLE][Harmony] createBluetoothCentralManager failed', e);
throw e;
}
return this.central!;
}
private bindCentralEvents() {
if (this.eventsBound) return;
this.eventsBound = true;
const central = this.central;
if (central == null) return;
try {
central.on('scanResult', (result: any) => {
try { this.handleScanResult(result); } catch (e) { console.warn('[AKBLE][Harmony] scanResult handler error', e); }
});
} catch (e) {
console.warn('[AKBLE][Harmony] central.on scanResult failed', e);
}
try {
central.on('bleDeviceFind', (result: any) => {
try { this.handleScanResult(result); } catch (e) { console.warn('[AKBLE][Harmony] bleDeviceFind handler error', e); }
});
} catch (e) {
/* optional */
}
try {
central.on('BLEConnectionStateChange', (state: any) => {
try { this.handleConnectionEvent(state); } catch (err) { console.warn('[AKBLE][Harmony] connection event error', err); }
});
} catch (e) {
console.warn('[AKBLE][Harmony] central.on connection change failed', e);
}
}
private handleScanResult(result: any) {
const list: any[] = result?.devices ?? result ?? [];
if (!Array.isArray(list)) return;
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (item == null) continue;
const deviceId: string = item.deviceId ?? item.device?.deviceId ?? '';
if (!deviceId) continue;
const name: string = item.name ?? item.deviceName ?? 'Unknown';
const rssi: number = item.rssi ?? item.RSSI ?? 0;
let device = this.devices.get(deviceId);
if (device == null) {
device = { deviceId, name, rssi, lastSeen: now() };
this.devices.set(deviceId, device);
} else {
device.name = name;
device.rssi = rssi;
device.lastSeen = now();
}
const cb = this.scanOptions?.onDeviceFound;
if (cb != null) {
try { cb(device); } catch (e) { console.warn('[AKBLE][Harmony] onDeviceFound error', e); }
}
}
}
private handleConnectionEvent(evt: any) {
const deviceId: string = evt?.deviceId ?? evt?.device?.deviceId ?? '';
if (!deviceId) return;
const connected = evt?.state === ble.BLEConnectionState.STATE_CONNECTED || evt?.connected === true;
const state: BleConnectionState = connected ? 2 : 0;
this.connectionStates.set(deviceId, state);
const pending = this.pendingConnects.get(deviceId);
if (pending != null) {
this.pendingConnects.delete(deviceId);
if (pending.timer != null) clearTimeout(pending.timer);
if (connected) {
try { pending.resolve(); } catch (e) { }
} else {
try { pending.reject(new Error('连接断开')); } catch (e) { }
}
}
for (let i = 0; i < this.connectionListeners.length; i++) {
const listener = this.connectionListeners[i];
try { listener(deviceId, state); } catch (e) { console.warn('[AKBLE][Harmony] listener error', e); }
}
}
async startScan(options: ScanDevicesOptions): Promise<void> {
const central = this.ensureCentral();
this.scanOptions = options ?? {} as ScanDevicesOptions;
if (this.scanning) {
await this.stopScan();
}
this.scanning = true;
if (this.scanTimer != null) {
clearTimeout(this.scanTimer);
this.scanTimer = null;
}
return new Promise<void>((resolve, reject) => {
try {
const filter = { interval: 0 } as any;
const res = central.startScan?.(filter) ?? central.startScan?.();
if (this.scanOptions?.timeout != null && this.scanOptions.timeout > 0) {
this.scanTimer = setTimeout(() => {
this.stopScanInternal();
}, this.scanOptions.timeout);
}
if (res instanceof Promise) {
res.then(() => resolve()).catch((err: BusinessError) => {
this.scanning = false;
reject(err);
});
} else {
resolve();
}
} catch (e) {
this.scanning = false;
reject(e);
}
});
}
async stopScan(): Promise<void> {
this.stopScanInternal();
return Promise.resolve();
}
private stopScanInternal() {
if (!this.scanning) return;
this.scanning = false;
try {
this.central?.stopScan?.();
} catch (e) {
console.warn('[AKBLE][Harmony] stopScan failed', e);
}
if (this.scanTimer != null) {
clearTimeout(this.scanTimer);
this.scanTimer = null;
}
const finished = this.scanOptions?.onScanFinished;
this.scanOptions = null;
if (finished != null) {
try { finished(); } catch (e) { console.warn('[AKBLE][Harmony] onScanFinished error', e); }
}
}
async connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
this.ensureCentral();
const timeout = options?.timeout ?? 15000;
if (this.connectionStates.get(deviceId) == 2) {
return Promise.resolve();
}
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingConnects.delete(deviceId);
reject(new Error('连接超时'));
}, timeout);
this.pendingConnects.set(deviceId, { resolve, reject, timer });
try {
let gatt = this.gattMap.get(deviceId);
if (gatt == null) {
gatt = ble.createGattClientDevice(deviceId);
this.gattMap.set(deviceId, gatt);
}
const connectRes = gatt.connect?.(true);
if (connectRes instanceof Promise) {
connectRes.then(() => {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
this.connectionStates.set(deviceId, 2);
resolve();
}).catch((err: BusinessError) => {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
reject(err);
});
} else {
// Some implementations return immediate state; rely on connection event to resolve.
}
} catch (e) {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
reject(e);
}
});
}
async disconnectDevice(deviceId: string): Promise<void> {
const gatt = this.gattMap.get(deviceId);
if (gatt == null) return;
try {
const res = gatt.disconnect?.();
if (res instanceof Promise) {
await res;
}
} catch (e) {
console.warn('[AKBLE][Harmony] disconnect failed', e);
}
this.connectionStates.set(deviceId, 0);
for (let i = 0; i < this.connectionListeners.length; i++) {
const listener = this.connectionListeners[i];
try { listener(deviceId, 0); } catch (err) { console.warn('[AKBLE][Harmony] disconnect listener error', err); }
}
}
reconnectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
const attempts = options?.maxAttempts ?? 3;
const interval = options?.interval ?? 3000;
let count = 0;
const attempt = (): Promise<void> => {
return this.connectDevice(deviceId, options).catch((err) => {
count++;
if (count >= attempts) throw err;
return new Promise<void>((resolve) => {
setTimeout(() => resolve(attempt()), interval);
});
});
};
return attempt();
}
getConnectedDevices(): BleDevice[] {
const result: BleDevice[] = [];
this.devices.forEach((device, id) => {
if (this.connectionStates.get(id) == 2) {
result.push(device);
}
});
return result;
}
onConnectionStateChange(listener: BleConnectionStateChangeCallback) {
this.connectionListeners.push(listener);
}
getDevice(deviceId: string): BleDevice | null {
return this.devices.get(deviceId) ?? null;
}
getGatt(deviceId: string): any | null {
return this.gattMap.get(deviceId) ?? null;
}
}

View File

@@ -0,0 +1,9 @@
import type { DfuManagerType, DfuOptions } from '../interface.uts';
class HarmonyDfuManager implements DfuManagerType {
async startDfu(_deviceId: string, _firmwareBytes: Uint8Array, _options?: DfuOptions): Promise<void> {
throw new Error('鸿蒙平台暂未实现 DFU 功能');
}
}
export const dfuManager = new HarmonyDfuManager();

View File

@@ -0,0 +1,90 @@
import * as BluetoothManager from './bluetooth_manager.uts';
import { ServiceManager } from './service_manager.uts';
import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback, BleProtocolType, BluetoothService as BluetoothServiceContract } from '../interface.uts';
const serviceManager = ServiceManager.getInstance();
class HarmonyBluetoothService implements BluetoothServiceContract {
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
return BluetoothManager.scanDevices(options ?? null);
}
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
}
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.disconnectDevice(deviceId, proto);
}
getConnectedDevices(): MultiProtocolDevice[] {
return BluetoothManager.getConnectedDevices();
}
on(event: BleEvent | string, callback: BleEventCallback): void {
BluetoothManager.on(event as BleEvent, callback);
}
off(event: BleEvent | string, callback?: BleEventCallback | null): void {
BluetoothManager.off(event as BleEvent, callback ?? null);
}
getServices(deviceId: string): Promise<BleService[]> {
return serviceManager.getServices(deviceId);
}
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
return serviceManager.getCharacteristics(deviceId, serviceId);
}
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
const services = await this.getServices(deviceId);
if (services.length == 0) throw new Error('未发现服务');
let targetService = services[0].uuid;
for (let i = 0; i < services.length; i++) {
const uuid = services[i].uuid ?? '';
if (/^bae/i.test(uuid)) {
targetService = uuid;
break;
}
}
const characteristics = await this.getCharacteristics(deviceId, targetService);
if (characteristics.length == 0) throw new Error('未发现特征值');
let writeCharId = '';
let notifyCharId = '';
for (let i = 0; i < characteristics.length; i++) {
const c = characteristics[i];
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse == true)) {
writeCharId = c.uuid;
}
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) {
notifyCharId = c.uuid;
}
}
if (writeCharId == '' || notifyCharId == '') throw new Error('未找到合适的写入或通知特征');
return { serviceId: targetService, writeCharId, notifyCharId };
}
subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
}
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
}
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, value, options);
}
unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
}
autoDiscoverAll(deviceId: string): Promise<any> {
return serviceManager.autoDiscoverAll(deviceId);
}
subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeAllNotifications(deviceId, onData);
}
}
export class BluetoothServiceShape extends HarmonyBluetoothService {}
const bluetoothServiceInstance = new BluetoothServiceShape();
BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance);
export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance;
export function getBluetoothService(): BluetoothServiceShape {
return bluetoothServiceInstance;
}
export { dfuManager } from './dfu_manager.uts';

View File

@@ -0,0 +1,344 @@
import type { BleService, BleCharacteristic, BleCharacteristicProperties, WriteCharacteristicOptions, AutoDiscoverAllResult, BleDataReceivedCallback } from '../interface.uts';
import type { BleDevice } from '../interface.uts';
import ble from '@ohos.bluetooth.ble';
import type { BusinessError } from '@ohos.base';
import { DeviceManager } from './device_manager.uts';
type PendingRead = {
resolve: (data: ArrayBuffer) => void;
reject: (err?: any) => void;
timer?: number;
};
type CharacteristicChange = {
serviceUuid?: string;
characteristicUuid?: string;
value?: ArrayBuffer | Uint8Array | number[];
};
function toArrayBuffer(value: ArrayBuffer | Uint8Array | number[] | null | undefined): ArrayBuffer {
if (value == null) return new ArrayBuffer(0);
if (value instanceof ArrayBuffer) return value;
if (value instanceof Uint8Array) {
return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
}
if (Array.isArray(value)) {
const buf = new Uint8Array(value.length);
for (let i = 0; i < value.length; i++) buf[i] = value[i] ?? 0;
return buf.buffer;
}
return new ArrayBuffer(0);
}
function toUint8Array(value: ArrayBuffer | Uint8Array | number[] | null | undefined): Uint8Array {
if (value instanceof Uint8Array) {
return value;
}
return new Uint8Array(toArrayBuffer(value));
}
function buildProperties(raw: any): BleCharacteristicProperties {
const props = raw?.properties ?? raw ?? {};
const read = props.read === true;
const write = props.write === true || props.writeWithoutResponse === true;
const notify = props.notify === true;
const indicate = props.indicate === true;
const writeNoRsp = props.writeWithoutResponse === true || props.writeNoResponse === true;
return {
read,
write,
notify,
indicate,
writeWithoutResponse: writeNoRsp,
canRead: read,
canWrite: write || writeNoRsp,
canNotify: notify || indicate
};
}
export class ServiceManager {
private static instance: ServiceManager | null = null;
private services = new Map<string, BleService[]>();
private characteristics = new Map<string, Map<string, BleCharacteristic[]>>();
private pendingReads = new Map<string, PendingRead>();
private notifyCallbacks = new Map<string, BleDataReceivedCallback>();
private boundGattDevices = new Set<string>();
private deviceManager = DeviceManager.getInstance();
private constructor() {}
static getInstance(): ServiceManager {
if (ServiceManager.instance == null) {
ServiceManager.instance = new ServiceManager();
}
return ServiceManager.instance!;
}
private getGattOrThrow(deviceId: string): any {
const gatt = this.deviceManager.getGatt(deviceId);
if (gatt == null) throw new Error('设备未连接');
return gatt;
}
private cacheServices(deviceId: string, services: any[]): BleService[] {
const list: BleService[] = [];
for (let i = 0; i < services.length; i++) {
const svc = services[i];
if (svc == null) continue;
const uuid = svc.uuid ?? svc.serviceUuid ?? '';
if (!uuid) continue;
list.push({ uuid, isPrimary: svc.isPrimary === true });
}
this.services.set(deviceId, list);
return list;
}
private async ensureServices(deviceId: string, gatt: any): Promise<BleService[]> {
const cached = this.services.get(deviceId);
if (cached != null && cached.length > 0) return cached;
try {
await gatt.discoverServices?.();
} catch (e) {
console.warn('[AKBLE][Harmony] discoverServices failed', e);
}
let services: any[] = [];
try {
services = gatt.getServices?.() ?? [];
} catch (e) {
console.warn('[AKBLE][Harmony] getServices failed', e);
}
if (!Array.isArray(services)) services = [];
return this.cacheServices(deviceId, services);
}
private cacheCharacteristics(deviceId: string, serviceId: string, chars: any[]): BleCharacteristic[] {
const list: BleCharacteristic[] = [];
for (let i = 0; i < chars.length; i++) {
const ch = chars[i];
if (ch == null) continue;
const uuid = ch.uuid ?? ch.characteristicUuid ?? '';
if (!uuid) continue;
list.push({
uuid,
service: { uuid: serviceId, isPrimary: true },
properties: buildProperties(ch)
});
}
let map = this.characteristics.get(deviceId);
if (map == null) {
map = new Map<string, BleCharacteristic[]>();
this.characteristics.set(deviceId, map);
}
map.set(serviceId, list);
return list;
}
private async ensureCharacteristics(deviceId: string, serviceId: string, gatt: any): Promise<BleCharacteristic[]> {
const perDevice = this.characteristics.get(deviceId);
const cached = perDevice != null ? perDevice.get(serviceId) : null;
if (cached != null && cached.length > 0) return cached;
let list: any[] = [];
try {
list = gatt.getCharacteristics?.(serviceId) ?? [];
} catch (e) {
console.warn('[AKBLE][Harmony] getCharacteristics failed', e);
}
if (!Array.isArray(list)) list = [];
return this.cacheCharacteristics(deviceId, serviceId, list);
}
private bindGattListener(deviceId: string, gatt: any) {
if (this.boundGattDevices.has(deviceId)) return;
this.boundGattDevices.add(deviceId);
try {
gatt.on?.('characteristicChange', (change: CharacteristicChange) => {
try { this.handleCharacteristicChange(deviceId, change); } catch (err) { console.warn('[AKBLE][Harmony] notify handler error', err); }
});
} catch (e) {
console.warn('[AKBLE][Harmony] bind characteristicChange failed', e);
}
}
private pendingKey(deviceId: string, serviceId: string, characteristicId: string): string {
return `${deviceId}|${serviceId}|${characteristicId}|read`;
}
private notifyKey(deviceId: string, serviceId: string, characteristicId: string): string {
return `${deviceId}|${serviceId}|${characteristicId}`;
}
private handleCharacteristicChange(deviceId: string, change: CharacteristicChange) {
const serviceId = change?.serviceUuid ?? '';
const characteristicId = change?.characteristicUuid ?? '';
if (!serviceId || !characteristicId) return;
const buffer = toArrayBuffer(change?.value);
const pending = this.pendingReads.get(this.pendingKey(deviceId, serviceId, characteristicId));
if (pending != null) {
this.pendingReads.delete(this.pendingKey(deviceId, serviceId, characteristicId));
if (pending.timer != null) clearTimeout(pending.timer);
try { pending.resolve(buffer); } catch (e) { }
}
const cb = this.notifyCallbacks.get(this.notifyKey(deviceId, serviceId, characteristicId));
if (cb != null) {
try { cb(toUint8Array(buffer)); } catch (e) { }
}
}
async getServices(deviceId: string, callback?: (services: BleService[] | null, error?: Error) => void): Promise<BleService[]> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
try {
const services = await this.ensureServices(deviceId, gatt);
if (callback != null) callback(services, null);
return services;
} catch (err) {
if (callback != null) callback(null, err as Error);
throw err;
}
}
async getCharacteristics(deviceId: string, serviceId: string, callback?: (list: BleCharacteristic[] | null, error?: Error) => void): Promise<BleCharacteristic[]> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
try {
await this.ensureServices(deviceId, gatt);
const list = await this.ensureCharacteristics(deviceId, serviceId, gatt);
if (callback != null) callback(list, null);
return list;
} catch (err) {
if (callback != null) callback(null, err as Error);
throw err;
}
}
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
return new Promise<ArrayBuffer>((resolve, reject) => {
const key = this.pendingKey(deviceId, serviceId, characteristicId);
const timer = setTimeout(() => {
this.pendingReads.delete(key);
reject(new Error('读取超时'));
}, 10000);
this.pendingReads.set(key, { resolve, reject, timer });
try {
const result = gatt.readCharacteristicValue?.({ serviceUuid: serviceId, characteristicUuid: characteristicId });
if (result instanceof Promise) {
result.then((value: any) => {
const buf = toArrayBuffer(value?.value ?? value);
const pending = this.pendingReads.get(key);
if (pending != null) {
this.pendingReads.delete(key);
if (pending.timer != null) clearTimeout(pending.timer);
try { pending.resolve(buf); } catch (e) { }
}
}).catch((err: BusinessError) => {
this.pendingReads.delete(key);
clearTimeout(timer);
reject(err);
});
} else if (result != null) {
const buf = toArrayBuffer((result as any)?.value ?? result);
this.pendingReads.delete(key);
clearTimeout(timer);
resolve(buf);
}
} catch (e) {
this.pendingReads.delete(key);
clearTimeout(timer);
reject(e);
}
});
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
const gatt = this.getGattOrThrow(deviceId);
const payload = value instanceof Uint8Array ? value : new Uint8Array(value ?? new ArrayBuffer(0));
const writeType = options?.forceWriteTypeNoResponse === true || options?.waitForResponse === false
? ble.GattWriteType?.WRITE_TYPE_NO_RESPONSE ?? 1
: ble.GattWriteType?.WRITE_TYPE_DEFAULT ?? 0;
try {
const res = gatt.writeCharacteristicValue?.({
serviceUuid: serviceId,
characteristicUuid: characteristicId,
value: payload.buffer.slice(payload.byteOffset, payload.byteOffset + payload.byteLength),
writeType
});
if (res instanceof Promise) {
await res;
}
return true;
} catch (e) {
console.warn('[AKBLE][Harmony] writeCharacteristic failed', e);
throw e;
}
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: BleDataReceivedCallback): Promise<void> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
const key = this.notifyKey(deviceId, serviceId, characteristicId);
this.notifyCallbacks.set(key, callback);
try {
const res = gatt.setCharacteristicValueChangeNotification?.({
serviceUuid: serviceId,
characteristicUuid: characteristicId,
enable: true
});
if (res instanceof Promise) {
await res;
}
} catch (e) {
this.notifyCallbacks.delete(key);
console.warn('[AKBLE][Harmony] enable notify failed', e);
throw e;
}
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
this.notifyCallbacks.delete(this.notifyKey(deviceId, serviceId, characteristicId));
try {
const res = gatt.setCharacteristicValueChangeNotification?.({
serviceUuid: serviceId,
characteristicUuid: characteristicId,
enable: false
});
if (res instanceof Promise) {
await res;
}
} catch (e) {
console.warn('[AKBLE][Harmony] disable notify failed', e);
}
}
async autoDiscoverAll(deviceId: string): Promise<AutoDiscoverAllResult> {
const gatt = this.getGattOrThrow(deviceId);
this.bindGattListener(deviceId, gatt);
const services = await this.ensureServices(deviceId, gatt);
const characteristics: BleCharacteristic[] = [];
for (let i = 0; i < services.length; i++) {
const svc = services[i];
const list = await this.ensureCharacteristics(deviceId, svc.uuid, gatt);
for (let j = 0; j < list.length; j++) {
characteristics.push(list[j]);
}
}
return { services, characteristics };
}
async subscribeAllNotifications(deviceId: string, callback: BleDataReceivedCallback): Promise<void> {
const { services, characteristics } = await this.autoDiscoverAll(deviceId);
for (let i = 0; i < characteristics.length; i++) {
const ch = characteristics[i];
if (ch.properties != null && (ch.properties.notify || ch.properties.indicate)) {
try {
await this.subscribeCharacteristic(deviceId, ch.service.uuid, ch.uuid, callback);
} catch (e) {
console.warn('[AKBLE][Harmony] subscribeAll skip', e);
}
}
}
}
}

View File

@@ -0,0 +1,279 @@
import type {
BleDevice,
BleConnectionState,
BleEvent,
BleEventCallback,
BleEventPayload,
BleScanResult,
BleConnectOptionsExt,
AutoBleInterfaces,
BleDataPayload,
SendDataPayload,
BleOptions,
MultiProtocolDevice,
ScanHandler,
BleProtocolType,
ScanDevicesOptions
} from '../interface.uts';
import { ProtocolHandler } from '../protocol_handler.uts';
import { BluetoothService } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
type RawProtocolHandler = {
protocol?: BleProtocolType;
scanDevices?: (options?: ScanDevicesOptions) => Promise<void>;
connect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<void>;
disconnect?: (device: BleDevice) => Promise<void>;
sendData?: (device: BleDevice, payload?: SendDataPayload, options?: BleOptions) => Promise<void>;
autoConnect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
}
class DeviceContext {
device: BleDevice;
protocol: BleProtocolType;
state: BleConnectionState;
handler: ProtocolHandler;
constructor(device: BleDevice, protocol: BleProtocolType, handler: ProtocolHandler) {
this.device = device;
this.protocol = protocol;
this.state = 0;
this.handler = handler;
}
}
const deviceMap = new Map<string, DeviceContext>();
let activeProtocol: BleProtocolType = 'standard';
let activeHandler: ProtocolHandler | null = null;
const eventListeners = new Map<BleEvent, Set<BleEventCallback>>();
let defaultBluetoothService: BluetoothService | null = null;
function emit(event: BleEvent, payload: BleEventPayload) {
const listeners = eventListeners.get(event);
if (listeners != null) {
listeners.forEach(cb => {
try { cb(payload); } catch (e) { }
});
}
}
class ProtocolHandlerWrapper extends ProtocolHandler {
private _raw: RawProtocolHandler | null;
constructor(raw?: RawProtocolHandler, bluetoothService?: BluetoothService) {
super(bluetoothService);
this._raw = raw ?? null;
}
override async scanDevices(options?: ScanDevicesOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.scanDevices == 'function') {
await rawTyped.scanDevices(options);
}
return;
}
override async connect(device: BleDevice, options?: BleConnectOptionsExt): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.connect == 'function') {
await rawTyped.connect(device, options);
}
return;
}
override async disconnect(device: BleDevice): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.disconnect == 'function') {
await rawTyped.disconnect(device);
}
return;
}
override async sendData(device: BleDevice, payload?: SendDataPayload, options?: BleOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.sendData == 'function') {
await rawTyped.sendData(device, payload, options);
}
return;
}
override async autoConnect(device: BleDevice, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.autoConnect == 'function') {
return await rawTyped.autoConnect(device, options);
}
return { serviceId: '', writeCharId: '', notifyCharId: '' };
}
}
function isRawProtocolHandler(x: any): boolean {
if (x == null || typeof x !== 'object') return false;
const r = x as Record<string, unknown>;
if (typeof r['scanDevices'] == 'function') return true;
if (typeof r['connect'] == 'function') return true;
if (typeof r['disconnect'] == 'function') return true;
if (typeof r['sendData'] == 'function') return true;
if (typeof r['autoConnect'] == 'function') return true;
if (typeof r['protocol'] == 'string') return true;
return false;
}
export const registerProtocolHandler = (handler: any) => {
if (handler == null) return;
let proto: BleProtocolType = 'standard';
if (handler instanceof ProtocolHandler) {
try { proto = (handler as ProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = handler as ProtocolHandler;
} else if (isRawProtocolHandler(handler)) {
try { proto = (handler as RawProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = new ProtocolHandlerWrapper(handler as RawProtocolHandler, defaultBluetoothService);
(activeHandler as ProtocolHandler).protocol = proto;
} else {
console.warn('[AKBLE] registerProtocolHandler: unsupported handler type, ignoring', handler);
return;
}
activeProtocol = proto;
}
export const scanDevices = async (options?: ScanDevicesOptions): Promise<void> => {
ensureDefaultProtocolHandler();
if (activeHandler == null) {
console.log('[AKBLE] no active scan handler registered');
return;
}
const handler = activeHandler as ProtocolHandler;
const original = options ?? null;
const scanOptions: ScanDevicesOptions = {} as ScanDevicesOptions;
if (original != null) {
if (original.protocols != null) scanOptions.protocols = original.protocols;
if (original.optionalServices != null) scanOptions.optionalServices = original.optionalServices;
if (original.timeout != null) scanOptions.timeout = original.timeout;
}
const userFound = original?.onDeviceFound ?? null;
scanOptions.onDeviceFound = (device: BleDevice) => {
emit('deviceFound', { event: 'deviceFound', device });
if (userFound != null) {
try { userFound(device); } catch (err) { }
}
};
const userFinished = original?.onScanFinished ?? null;
scanOptions.onScanFinished = () => {
emit('scanFinished', { event: 'scanFinished' });
if (userFinished != null) {
try { userFinished(); } catch (err) { }
}
};
try {
await handler.scanDevices(scanOptions);
} catch (e) {
console.warn('[AKBLE] scanDevices handler error', e);
}
}
export const connectDevice = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<void> => {
const handler = activeHandler;
if (handler == null) throw new Error('No protocol handler');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
await handler.connect(device, options);
const ctx = new DeviceContext(device, protocol, handler);
ctx.state = 2;
deviceMap.set(getDeviceKey(deviceId, protocol), ctx);
emit('connectionStateChanged', { event: 'connectionStateChanged', device, protocol, state: 2 });
}
export const disconnectDevice = async (deviceId: string, protocol: BleProtocolType): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null || ctx.handler == null) return;
await ctx.handler.disconnect(ctx.device);
ctx.state = 0;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol, state: 0 });
deviceMap.delete(getDeviceKey(deviceId, protocol));
}
export const sendData = async (payload: SendDataPayload, options?: BleOptions): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(payload.deviceId, payload.protocol));
if (ctx == null) throw new Error('Device not connected');
const deviceCtx = ctx as DeviceContext;
if (deviceCtx.handler == null) throw new Error('sendData not supported for this protocol');
await deviceCtx.handler.sendData(deviceCtx.device, payload, options);
emit('dataSent', { event: 'dataSent', device: deviceCtx.device, protocol: payload.protocol, data: payload.data });
}
export const getConnectedDevices = (): MultiProtocolDevice[] => {
const result: MultiProtocolDevice[] = [];
deviceMap.forEach((ctx: DeviceContext) => {
const dev: MultiProtocolDevice = {
deviceId: ctx.device.deviceId,
name: ctx.device.name,
rssi: ctx.device.rssi,
protocol: ctx.protocol
};
result.push(dev);
});
return result;
}
export const getConnectionState = (deviceId: string, protocol: BleProtocolType): BleConnectionState => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null) return 0;
return ctx.state;
}
export const on = (event: BleEvent, callback: BleEventCallback) => {
if (!eventListeners.has(event)) eventListeners.set(event, new Set());
eventListeners.get(event)!.add(callback);
}
export const off = (event: BleEvent, callback?: BleEventCallback) => {
if (callback == null) {
eventListeners.delete(event);
} else {
eventListeners.get(event)?.delete(callback as BleEventCallback);
}
}
function getDeviceKey(deviceId: string, protocol: BleProtocolType): string {
return `${deviceId}|${protocol}`;
}
export const autoConnect = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> => {
const handler = activeHandler;
if (handler == null) throw new Error('autoConnect not supported for this protocol');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
return await handler.autoConnect(device, options) as AutoBleInterfaces;
}
function ensureDefaultProtocolHandler(): void {
if (activeHandler != null) return;
const service = defaultBluetoothService;
if (service == null) return;
try {
const _dm = DeviceManager.getInstance();
const _raw: RawProtocolHandler = {
protocol: 'standard',
scanDevices: (options?: ScanDevicesOptions) => {
try {
const scanOptions = options != null ? options : {} as ScanDevicesOptions;
_dm.startScan(scanOptions);
} catch (e) {
console.warn('[AKBLE] DeviceManager.startScan failed', e);
}
return Promise.resolve();
},
connect: (device, options?: BleConnectOptionsExt) => {
return _dm.connectDevice(device.deviceId, options);
},
disconnect: (device) => {
return _dm.disconnectDevice(device.deviceId);
},
autoConnect: (device, _options?: any) => {
const result: AutoBleInterfaces = { serviceId: '', writeCharId: '', notifyCharId: '' };
return Promise.resolve(result);
}
};
const _wrapper = new ProtocolHandlerWrapper(_raw, service);
activeHandler = _wrapper;
activeProtocol = _raw.protocol as BleProtocolType;
console.log('[AKBLE] default protocol handler registered', activeProtocol);
} catch (e) {
console.warn('[AKBLE] failed to register default protocol handler', e);
}
}
export const setDefaultBluetoothService = (service: BluetoothService) => {
defaultBluetoothService = service;
ensureDefaultProtocolHandler();
};

View File

@@ -0,0 +1,5 @@
{
"dependencies": [
]
}

View File

@@ -0,0 +1,348 @@
import type { BleDevice, BleConnectOptionsExt, BleConnectionState, BleConnectionStateChangeCallback, ScanDevicesOptions } from '../interface.uts';
import { CBCentralManager, CBPeripheral, CBService, CBCharacteristic, CBCentralManagerDelegate, CBPeripheralDelegate, CBManagerState, CBUUID } from 'CoreBluetooth';
import { NSObject, NSDictionary, NSNumber, NSError, NSUUID } from 'Foundation';
import { DispatchQueue } from 'Dispatch';
import { ServiceManager } from './service_manager.uts';
type PendingConnect = {
resolve: () => void;
reject: (err?: any) => void;
timer?: number;
};
class PendingConnectImpl implements PendingConnect {
resolve: () => void;
reject: (err?: any) => void;
timer?: number;
constructor(resolve: () => void, reject: (err?: any) => void, timer?: number) {
this.resolve = resolve;
this.reject = reject;
this.timer = timer;
}
}
class CentralDelegate extends NSObject implements CBCentralManagerDelegate, CBPeripheralDelegate {
private owner: DeviceManager;
constructor(owner: DeviceManager) {
super();
this.owner = owner;
}
override centralManagerDidUpdateState(central: CBCentralManager): void {
this.owner.handleCentralStateUpdate(central.state);
}
override centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: NSDictionary<any, any>, RSSI: NSNumber): void {
this.owner.handleDiscovered(peripheral, RSSI);
}
override centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral): void {
this.owner.handleConnected(peripheral);
}
override centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError | null): void {
this.owner.handleConnectFailed(peripheral, error);
}
override centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError | null): void {
this.owner.handleDisconnected(peripheral, error);
}
override peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError | null): void {
ServiceManager.getInstance().handleServicesDiscovered(peripheral, error);
}
override peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError | null): void {
ServiceManager.getInstance().handleCharacteristicsDiscovered(peripheral, service, error);
}
override peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError | null): void {
ServiceManager.getInstance().handleCharacteristicValueUpdated(peripheral, characteristic, error);
}
override peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError | null): void {
ServiceManager.getInstance().handleCharacteristicWrite(peripheral, characteristic, error);
}
override peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError | null): void {
ServiceManager.getInstance().handleNotificationState(peripheral, characteristic, error);
}
}
export class DeviceManager {
private static instance: DeviceManager | null = null;
private central: CBCentralManager | null = null;
private delegate: CentralDelegate | null = null;
private queue: DispatchQueue | null = null;
private devices = new Map<string, BleDevice>();
private peripherals = new Map<string, CBPeripheral>();
private connectionStates = new Map<string, BleConnectionState>();
private connectionStateChangeListeners: BleConnectionStateChangeCallback[] = [];
private pendingConnects = new Map<string, PendingConnect>();
private centralState: number = CBManagerState.unknown;
private scanOptions: ScanDevicesOptions | null = null;
private scanTimer: number | null = null;
private isScanning: boolean = false;
private pendingScan: boolean = false;
private pendingScanOptions: ScanDevicesOptions | null = null;
private constructor() {}
static getInstance(): DeviceManager {
if (DeviceManager.instance == null) {
DeviceManager.instance = new DeviceManager();
}
return DeviceManager.instance!;
}
private ensureCentral(): CBCentralManager {
if (this.central != null) return this.central!;
if (this.queue == null) {
this.queue = DispatchQueue.main;
}
this.delegate = new CentralDelegate(this);
this.central = new CBCentralManager(delegate = this.delegate!, queue = this.queue);
if (this.central != null) {
this.centralState = this.central!.state;
}
return this.central!;
}
handleCentralStateUpdate(state: number) {
this.centralState = state;
if (state == CBManagerState.poweredOn) {
if (this.pendingScan) {
const opts = this.pendingScanOptions ?? {} as ScanDevicesOptions;
this.pendingScan = false;
this.pendingScanOptions = null;
this.beginScan(opts);
}
} else if (state == CBManagerState.poweredOff) {
this.stopScanInternal();
}
}
startScan(options: ScanDevicesOptions): void {
const central = this.ensureCentral();
const opts = options ?? {} as ScanDevicesOptions;
this.scanOptions = opts;
if (this.centralState != CBManagerState.poweredOn) {
this.pendingScan = true;
this.pendingScanOptions = opts;
console.warn('[AKBLE][iOS] Bluetooth not powered on yet, waiting for state update');
return;
}
this.beginScan(opts, central);
}
private beginScan(options: ScanDevicesOptions, central?: CBCentralManager | null) {
const mgr = central ?? this.central ?? this.ensureCentral();
if (this.isScanning) {
this.stopScanInternal();
}
const serviceIds = options.optionalServices ?? null;
let serviceUUIDs: CBUUID[] | null = null;
if (serviceIds != null && serviceIds.length > 0) {
serviceUUIDs = [];
for (let i = 0; i < serviceIds.length; i++) {
const sid = serviceIds[i];
try {
const uuid = CBUUID.UUIDWithString(sid);
if (uuid != null) serviceUUIDs.push(uuid!);
} catch (e) {
console.warn('[AKBLE][iOS] invalid service uuid', sid, e);
}
}
if (serviceUUIDs.length == 0) serviceUUIDs = null;
}
try {
mgr.scanForPeripherals(withServices = serviceUUIDs, options = null);
this.isScanning = true;
if (options.timeout != null && options.timeout > 0) {
this.scanTimer = setTimeout(() => {
this.stopScanInternal();
}, options.timeout);
}
} catch (e) {
console.error('[AKBLE][iOS] scanForPeripherals failed', e);
}
}
stopScan(): void {
this.stopScanInternal();
}
private stopScanInternal() {
if (!this.isScanning) return;
try {
this.central?.stopScan();
} catch (e) {
console.warn('[AKBLE][iOS] stopScan failed', e);
}
this.isScanning = false;
if (this.scanTimer != null) {
clearTimeout(this.scanTimer);
this.scanTimer = null;
}
const finished = this.scanOptions?.onScanFinished;
this.scanOptions = null;
if (finished != null) {
try { finished(); } catch (e) { }
}
}
handleDiscovered(peripheral: CBPeripheral, RSSI: NSNumber) {
const deviceId = peripheral.identifier.UUIDString;
let rssiValue = 0;
if (RSSI != null) {
try {
rssiValue = RSSI.intValue;
} catch (e) {
rssiValue = Number(RSSI);
}
}
let bleDevice = this.devices.get(deviceId);
if (bleDevice == null) {
bleDevice = { deviceId, name: peripheral.name ?? 'Unknown', rssi: rssiValue, lastSeen: Date.now() };
this.devices.set(deviceId, bleDevice);
} else {
bleDevice.rssi = rssiValue;
bleDevice.name = peripheral.name ?? bleDevice.name;
bleDevice.lastSeen = Date.now();
}
this.peripherals.set(deviceId, peripheral);
peripheral.delegate = this.delegate;
const onFound = this.scanOptions?.onDeviceFound;
if (onFound != null) {
try { onFound(bleDevice); } catch (e) { }
}
}
async connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
const central = this.ensureCentral();
const timeout = options?.timeout ?? 15000;
const peripheral = this.obtainPeripheral(deviceId, central);
if (peripheral == null) {
throw new Error('未找到设备');
}
this.connectionStates.set(deviceId, 1);
this.emitConnectionStateChange(deviceId, 1);
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingConnects.delete(deviceId);
this.connectionStates.set(deviceId, 0);
this.emitConnectionStateChange(deviceId, 0);
reject(new Error('连接超时'));
}, timeout);
const resolveAdapter = () => {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
resolve();
};
const rejectAdapter = (err?: any) => {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
reject(err);
};
this.pendingConnects.set(deviceId, new PendingConnectImpl(resolveAdapter, rejectAdapter, timer));
try {
peripheral.delegate = this.delegate;
central.connect(peripheral = peripheral, options = null);
} catch (e) {
clearTimeout(timer);
this.pendingConnects.delete(deviceId);
this.connectionStates.set(deviceId, 0);
this.emitConnectionStateChange(deviceId, 0);
reject(e);
}
});
}
async disconnectDevice(deviceId: string): Promise<void> {
const central = this.ensureCentral();
const peripheral = this.peripherals.get(deviceId);
if (peripheral == null) {
return;
}
try {
central.cancelPeripheralConnection(peripheral = peripheral);
} catch (e) {
console.warn('[AKBLE][iOS] cancelPeripheralConnection failed', e);
}
this.connectionStates.set(deviceId, 0);
this.emitConnectionStateChange(deviceId, 0);
}
handleConnected(peripheral: CBPeripheral) {
const deviceId = peripheral.identifier.UUIDString;
const pending = this.pendingConnects.get(deviceId);
if (pending != null) {
const timer = pending.timer;
if (timer != null) clearTimeout(timer);
try { pending.resolve(); } catch (e) { }
this.pendingConnects.delete(deviceId);
}
this.connectionStates.set(deviceId, 2);
this.emitConnectionStateChange(deviceId, 2);
this.peripherals.set(deviceId, peripheral);
peripheral.delegate = this.delegate;
try {
peripheral.discoverServices(serviceUUIDs = null);
} catch (e) {
console.warn('[AKBLE][iOS] discoverServices failed', e);
}
}
handleConnectFailed(peripheral: CBPeripheral, error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
const pending = this.pendingConnects.get(deviceId);
if (pending != null) {
const timer = pending.timer;
if (timer != null) clearTimeout(timer);
try { pending.reject(error ?? new Error('连接失败')); } catch (e) { }
this.pendingConnects.delete(deviceId);
}
this.connectionStates.set(deviceId, 0);
this.emitConnectionStateChange(deviceId, 0);
}
handleDisconnected(peripheral: CBPeripheral, _error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
this.connectionStates.set(deviceId, 0);
this.emitConnectionStateChange(deviceId, 0);
}
getConnectedDevices(): BleDevice[] {
const result: BleDevice[] = [];
this.devices.forEach((device, deviceId) => {
if (this.connectionStates.get(deviceId) == 2) {
result.push(device);
}
});
return result;
}
onConnectionStateChange(listener: BleConnectionStateChangeCallback) {
this.connectionStateChangeListeners.push(listener);
}
private emitConnectionStateChange(deviceId: string, state: BleConnectionState) {
for (let i = 0; i < this.connectionStateChangeListeners.length; i++) {
const listener = this.connectionStateChangeListeners[i];
try { listener(deviceId, state); } catch (e) { }
}
}
getPeripheral(deviceId: string): CBPeripheral | null {
return this.peripherals.get(deviceId) ?? null;
}
private obtainPeripheral(deviceId: string, central: CBCentralManager): CBPeripheral | null {
let peripheral = this.peripherals.get(deviceId) ?? null;
if (peripheral != null) return peripheral;
try {
const uuid = new NSUUID(UUIDString = deviceId);
const list = central.retrievePeripherals(withIdentifiers = [uuid]);
if (list != null && list.length > 0) {
peripheral = list[0];
if (peripheral != null) {
this.peripherals.set(deviceId, peripheral!);
peripheral!.delegate = this.delegate;
}
}
} catch (e) {
console.warn('[AKBLE][iOS] retrievePeripherals failed', e);
}
return peripheral;
}
}

View File

@@ -0,0 +1,9 @@
import type { DfuManagerType, DfuOptions } from '../interface.uts';
class IOSDfuManager implements DfuManagerType {
async startDfu(_deviceId: string, _firmwareBytes: Uint8Array, _options?: DfuOptions): Promise<void> {
throw new Error('iOS 平台暂未实现 DFU 功能');
}
}
export const dfuManager = new IOSDfuManager();

View File

@@ -0,0 +1,106 @@
import * as BluetoothManager from './bluetooth_manager.uts';
import { ServiceManager } from './service_manager.uts';
import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback, BleProtocolType, BluetoothService as BluetoothServiceContract } from '../interface.uts';
const serviceManager = ServiceManager.getInstance();
class IOSBluetoothService implements BluetoothServiceContract {
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
return BluetoothManager.scanDevices(options ?? null);
}
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
}
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.disconnectDevice(deviceId, proto);
}
getConnectedDevices(): MultiProtocolDevice[] {
return BluetoothManager.getConnectedDevices();
}
on(event: BleEvent | string, callback: BleEventCallback): void {
BluetoothManager.on(event as BleEvent, callback);
}
off(event: BleEvent | string, callback?: BleEventCallback | null): void {
BluetoothManager.off(event as BleEvent, callback ?? null);
}
getServices(deviceId: string): Promise<BleService[]> {
return new Promise((resolve, reject) => {
serviceManager.getServices(deviceId, (list, err) => {
if (err != null) {
reject(err);
} else {
resolve(list ?? []);
}
});
});
}
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
return new Promise((resolve, reject) => {
serviceManager.getCharacteristics(deviceId, serviceId, (list, err) => {
if (err != null) {
reject(err);
} else {
resolve(list ?? []);
}
});
});
}
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
const services = await this.getServices(deviceId);
if (services.length == 0) throw new Error('未发现服务');
let targetService = services[0].uuid;
for (let i = 0; i < services.length; i++) {
const uuid = services[i].uuid ?? '';
if (/^bae/i.test(uuid)) {
targetService = uuid;
break;
}
}
const characteristics = await this.getCharacteristics(deviceId, targetService);
if (characteristics.length == 0) throw new Error('未发现特征值');
let writeCharId = '';
let notifyCharId = '';
for (let i = 0; i < characteristics.length; i++) {
const c = characteristics[i];
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse == true)) {
writeCharId = c.uuid;
}
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) {
notifyCharId = c.uuid;
}
}
if (writeCharId == '' || notifyCharId == '') throw new Error('未找到合适的写入或通知特征');
return { serviceId: targetService, writeCharId, notifyCharId };
}
subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
}
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
}
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, value, options);
}
unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
}
autoDiscoverAll(deviceId: string): Promise<any> {
return serviceManager.autoDiscoverAll(deviceId);
}
subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeAllNotifications(deviceId, onData);
}
}
export class BluetoothServiceShape extends IOSBluetoothService {}
const bluetoothServiceInstance = new BluetoothServiceShape();
BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance);
export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance;
export function getBluetoothService(): BluetoothServiceShape {
return bluetoothServiceInstance;
}
export { dfuManager } from './dfu_manager.uts';

View File

@@ -0,0 +1,483 @@
import type { BleService, BleCharacteristic, BleDataReceivedCallback, BleCharacteristicProperties, WriteCharacteristicOptions, AutoDiscoverAllResult } from '../interface.uts';
import { CBPeripheral, CBService, CBCharacteristic, CBCharacteristicWriteType } from 'CoreBluetooth';
import { Data, NSError } from 'Foundation';
import { DeviceManager } from './device_manager.uts';
function toUint8Array(value: Uint8Array | ArrayBuffer): Uint8Array {
if (value instanceof Uint8Array) return value;
return new Uint8Array(value);
}
function dataToUint8Array(data: Data | null): Uint8Array {
if (data == null) return new Uint8Array(0);
const base64 = data.base64EncodedString(options = 0);
if (base64 == null) return new Uint8Array(0);
const raw = atob(base64);
const out = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) {
out[i] = raw.charCodeAt(i) & 0xff;
}
return out;
}
function uint8ArrayToData(bytes: Uint8Array): Data {
if (bytes.length == 0) {
return new Data();
}
let binary = '';
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
const data = new Data(base64Encoded = base64);
return data != null ? data! : new Data();
}
function iterateNSArray<T>(collection: any, handler: (item: T | null) => void) {
if (collection == null) return;
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
handler(collection[i] as T);
}
return;
}
try {
const count = collection.count as number;
if (typeof count === 'number') {
for (let i = 0; i < count; i++) {
const item = collection.objectAtIndex(i);
handler(item as T);
}
return;
}
} catch (e) { }
try {
const len = collection.length as number;
if (typeof len === 'number') {
for (let i = 0; i < len; i++) {
handler(collection[i] as T);
}
}
} catch (e2) { }
}
type PendingCallback = {
resolve: (data: any) => void;
reject: (err?: any) => void;
timer?: number;
};
function makeCharProperties(flags: number): BleCharacteristicProperties {
const read = (flags & 0x02) != 0;
const write = (flags & 0x08) != 0;
const notify = (flags & 0x10) != 0;
const indicate = (flags & 0x20) != 0;
const writeNoRsp = (flags & 0x04) != 0;
return {
read,
write,
notify,
indicate,
writeWithoutResponse: writeNoRsp,
canRead: read,
canWrite: write || writeNoRsp,
canNotify: notify || indicate
};
}
function getCharPropertiesValue(characteristic: CBCharacteristic): number {
try {
const anyProps = characteristic.properties as any;
if (anyProps != null && anyProps.rawValue != null) {
return Number(anyProps.rawValue);
}
} catch (e) { }
try {
return Number((characteristic as any).properties);
} catch (e2) { }
return 0;
}
export class ServiceManager {
private static instance: ServiceManager | null = null;
private services = new Map<string, BleService[]>();
private characteristics = new Map<string, Map<string, BleCharacteristic[]>>();
private serviceWaiters = new Map<string, ((list: BleService[] | null, error?: Error) => void)[]>();
private characteristicWaiters = new Map<string, ((list: BleCharacteristic[] | null, error?: Error) => void)[]>();
private pendingReads = new Map<string, PendingCallback>();
private pendingWrites = new Map<string, PendingCallback>();
private notifyCallbacks = new Map<string, BleDataReceivedCallback>();
private deviceManager = DeviceManager.getInstance();
private constructor() {}
static getInstance(): ServiceManager {
if (ServiceManager.instance == null) {
ServiceManager.instance = new ServiceManager();
}
return ServiceManager.instance!;
}
resetDiscoveryState(deviceId: string) {
this.services.delete(deviceId);
this.characteristics.forEach((_value, key) => {
if (key.startsWith(deviceId + '|')) {
this.characteristics.delete(key);
}
});
}
handleServicesDiscovered(peripheral: CBPeripheral, error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
if (error != null) {
const err = new Error('服务发现失败: ' + error.localizedDescription);
this.resolveServiceWaiters(deviceId, null, err);
return;
}
const list: BleService[] = [];
const native = peripheral.services;
iterateNSArray<CBService>(native, (svc) => {
if (svc == null) return;
const uuid = svc.UUID.UUIDString;
list.push({ uuid, isPrimary: svc.isPrimary });
});
this.services.set(deviceId, list);
this.resolveServiceWaiters(deviceId, list, null);
}
handleCharacteristicsDiscovered(peripheral: CBPeripheral, service: CBService, error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
const serviceId = service.UUID.UUIDString;
const key = this.characteristicKey(deviceId, serviceId);
if (error != null) {
const err = new Error('特征发现失败: ' + error.localizedDescription);
this.resolveCharacteristicWaiters(key, null, err);
return;
}
const list: BleCharacteristic[] = [];
const chars = service.characteristics;
iterateNSArray<CBCharacteristic>(chars, (ch) => {
if (ch == null) return;
const propsValue = getCharPropertiesValue(ch);
const props = makeCharProperties(propsValue);
list.push({
uuid: ch.UUID.UUIDString,
service: { uuid: serviceId, isPrimary: service.isPrimary },
properties: props
});
});
let map = this.characteristics.get(deviceId);
if (map == null) {
map = new Map<string, BleCharacteristic[]>();
this.characteristics.set(deviceId, map);
}
map.set(serviceId, list);
this.resolveCharacteristicWaiters(key, list, null);
}
handleCharacteristicValueUpdated(peripheral: CBPeripheral, characteristic: CBCharacteristic, error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
const serviceId = characteristic.service.UUID.UUIDString;
const charId = characteristic.UUID.UUIDString;
const notifyKey = this.notifyKey(deviceId, serviceId, charId);
const readKey = this.operationKey(deviceId, serviceId, charId, 'read');
if (error != null) {
const pending = this.pendingReads.get(readKey);
if (pending != null) {
this.pendingReads.delete(readKey);
try { pending.reject(error); } catch (e) { }
}
return;
}
const bytes = dataToUint8Array(characteristic.value);
const pending = this.pendingReads.get(readKey);
if (pending != null) {
this.pendingReads.delete(readKey);
if (pending.timer != null) clearTimeout(pending.timer);
try { pending.resolve(bytes.buffer as ArrayBuffer); } catch (e) { }
}
const cb = this.notifyCallbacks.get(notifyKey);
if (cb != null) {
try { cb(bytes); } catch (e) { }
}
}
handleCharacteristicWrite(peripheral: CBPeripheral, characteristic: CBCharacteristic, error: NSError | null) {
const deviceId = peripheral.identifier.UUIDString;
const serviceId = characteristic.service.UUID.UUIDString;
const charId = characteristic.UUID.UUIDString;
const writeKey = this.operationKey(deviceId, serviceId, charId, 'write');
const pending = this.pendingWrites.get(writeKey);
if (pending == null) return;
this.pendingWrites.delete(writeKey);
if (pending.timer != null) clearTimeout(pending.timer);
if (error != null) {
try { pending.reject(error); } catch (e) { }
} else {
try { pending.resolve(true); } catch (e) { }
}
}
handleNotificationState(peripheral: CBPeripheral, characteristic: CBCharacteristic, error: NSError | null) {
if (error != null) {
console.warn('[AKBLE][iOS] notify state change error', error.localizedDescription);
}
}
getServices(deviceId: string, callback?: (services: BleService[] | null, error?: Error) => void): Promise<BleService[]> {
const cached = this.services.get(deviceId);
if (cached != null && cached.length > 0) {
if (callback != null) callback(cached, null);
return Promise.resolve(cached);
}
return new Promise((resolve, reject) => {
this.enqueueServiceWaiter(deviceId, (list, err) => {
if (err != null || list == null) {
if (callback != null) callback(null, err ?? new Error('服务获取失败'));
reject(err ?? new Error('服务获取失败'));
} else {
if (callback != null) callback(list, null);
resolve(list);
}
});
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) {
const err = new Error('设备未连接');
this.resolveServiceWaiters(deviceId, null, err);
return;
}
try {
peripheral.discoverServices(serviceUUIDs = null);
} catch (e) {
const err = e instanceof Error ? e : new Error('服务发现失败');
this.resolveServiceWaiters(deviceId, null, err);
}
});
}
getCharacteristics(deviceId: string, serviceId: string, callback?: (list: BleCharacteristic[] | null, error?: Error) => void): Promise<BleCharacteristic[]> {
const cached = this.characteristics.get(deviceId)?.get(serviceId) ?? null;
if (cached != null && cached.length > 0) {
if (callback != null) callback(cached, null);
return Promise.resolve(cached);
}
return new Promise((resolve, reject) => {
const key = this.characteristicKey(deviceId, serviceId);
this.enqueueCharacteristicWaiter(key, (list, err) => {
if (err != null || list == null) {
if (callback != null) callback(null, err ?? new Error('特征获取失败'));
reject(err ?? new Error('特征获取失败'));
} else {
if (callback != null) callback(list, null);
resolve(list);
}
});
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) {
const err = new Error('设备未连接');
this.resolveCharacteristicWaiters(key, null, err);
return;
}
const service = this.findNativeService(peripheral, serviceId);
if (service == null) {
const err = new Error('未找到服务');
this.resolveCharacteristicWaiters(key, null, err);
return;
}
try {
peripheral.discoverCharacteristics(characteristicUUIDs = null, forService = service);
} catch (e) {
const err = e instanceof Error ? e : new Error('特征发现失败');
this.resolveCharacteristicWaiters(key, null, err);
}
});
}
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) throw new Error('设备未连接');
const characteristic = this.findNativeCharacteristic(peripheral, serviceId, characteristicId);
if (characteristic == null) throw new Error('未找到特征值');
return new Promise<ArrayBuffer>((resolve, reject) => {
const key = this.operationKey(deviceId, serviceId, characteristicId, 'read');
const timer = setTimeout(() => {
this.pendingReads.delete(key);
reject(new Error('读取超时'));
}, 10000);
this.pendingReads.set(key, { resolve, reject, timer });
try {
peripheral.readValueForCharacteristic(characteristic);
} catch (e) {
clearTimeout(timer);
this.pendingReads.delete(key);
reject(e);
}
});
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) throw new Error('设备未连接');
const characteristic = this.findNativeCharacteristic(peripheral, serviceId, characteristicId);
if (characteristic == null) throw new Error('未找到特征值');
const payload = uint8ArrayToData(toUint8Array(value));
const waitForResponse = options?.waitForResponse ?? true;
const key = this.operationKey(deviceId, serviceId, characteristicId, 'write');
if (!waitForResponse) {
try {
peripheral.writeValue(payload, forCharacteristic = characteristic, type = CBCharacteristicWriteType.withoutResponse);
return true;
} catch (e) {
throw e;
}
}
return new Promise<boolean>((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingWrites.delete(key);
reject(new Error('写入超时'));
}, options?.giveupTimeoutMs ?? 10000);
this.pendingWrites.set(key, { resolve, reject, timer });
try {
peripheral.writeValue(payload, forCharacteristic = characteristic, type = CBCharacteristicWriteType.withResponse);
} catch (e) {
clearTimeout(timer);
this.pendingWrites.delete(key);
reject(e);
}
});
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: BleDataReceivedCallback): Promise<void> {
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) throw new Error('设备未连接');
const characteristic = this.findNativeCharacteristic(peripheral, serviceId, characteristicId);
if (characteristic == null) throw new Error('未找到特征值');
const key = this.notifyKey(deviceId, serviceId, characteristicId);
this.notifyCallbacks.set(key, callback);
try {
peripheral.setNotifyValue(true, forCharacteristic = characteristic);
} catch (e) {
this.notifyCallbacks.delete(key);
throw e;
}
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
const peripheral = this.deviceManager.getPeripheral(deviceId);
if (peripheral == null) return;
const characteristic = this.findNativeCharacteristic(peripheral, serviceId, characteristicId);
if (characteristic == null) return;
const key = this.notifyKey(deviceId, serviceId, characteristicId);
this.notifyCallbacks.delete(key);
try {
peripheral.setNotifyValue(false, forCharacteristic = characteristic);
} catch (e) {
console.warn('[AKBLE][iOS] unsubscribe failed', e);
}
}
async autoDiscoverAll(deviceId: string): Promise<AutoDiscoverAllResult> {
const services = await this.getServices(deviceId);
const allCharacteristics: BleCharacteristic[] = [];
for (let i = 0; i < services.length; i++) {
const svc = services[i];
const chars = await this.getCharacteristics(deviceId, svc.uuid);
for (let j = 0; j < chars.length; j++) {
allCharacteristics.push(chars[j]);
}
}
return { services, characteristics: allCharacteristics };
}
async subscribeAllNotifications(deviceId: string, callback: BleDataReceivedCallback): Promise<void> {
const services = await this.getServices(deviceId);
for (let i = 0; i < services.length; i++) {
const svc = services[i];
const chars = await this.getCharacteristics(deviceId, svc.uuid);
for (let j = 0; j < chars.length; j++) {
const charDef = chars[j];
if (charDef.properties != null && (charDef.properties.notify || charDef.properties.indicate)) {
try {
await this.subscribeCharacteristic(deviceId, svc.uuid, charDef.uuid, callback);
} catch (e) {
console.warn('[AKBLE][iOS] subscribeAllNotifications failed', svc.uuid, charDef.uuid, e);
}
}
}
}
}
private enqueueServiceWaiter(deviceId: string, waiter: (list: BleService[] | null, error?: Error) => void) {
let queue = this.serviceWaiters.get(deviceId);
if (queue == null) {
queue = [];
this.serviceWaiters.set(deviceId, queue);
}
queue.push(waiter);
}
private enqueueCharacteristicWaiter(key: string, waiter: (list: BleCharacteristic[] | null, error?: Error) => void) {
let queue = this.characteristicWaiters.get(key);
if (queue == null) {
queue = [];
this.characteristicWaiters.set(key, queue);
}
queue.push(waiter);
}
private resolveServiceWaiters(deviceId: string, list: BleService[] | null, error: Error | null) {
const queue = this.serviceWaiters.get(deviceId);
if (queue == null) return;
this.serviceWaiters.delete(deviceId);
for (let i = 0; i < queue.length; i++) {
const waiter = queue[i];
try { waiter(list, error ?? undefined); } catch (e) { }
}
}
private resolveCharacteristicWaiters(key: string, list: BleCharacteristic[] | null, error: Error | null) {
const queue = this.characteristicWaiters.get(key);
if (queue == null) return;
this.characteristicWaiters.delete(key);
for (let i = 0; i < queue.length; i++) {
const waiter = queue[i];
try { waiter(list, error ?? undefined); } catch (e) { }
}
}
private findNativeService(peripheral: CBPeripheral, serviceId: string): CBService | null {
const services = peripheral.services;
let found: CBService | null = null;
iterateNSArray<CBService>(services, (svc) => {
if (found != null) return;
if (svc != null && svc.UUID.UUIDString == serviceId) found = svc;
});
if (found != null) return found;
return null;
}
private findNativeCharacteristic(peripheral: CBPeripheral, serviceId: string, characteristicId: string): CBCharacteristic | null {
const service = this.findNativeService(peripheral, serviceId);
if (service == null) return null;
const chars = service.characteristics;
let found: CBCharacteristic | null = null;
iterateNSArray<CBCharacteristic>(chars, (ch) => {
if (found != null) return;
if (ch != null && ch.UUID.UUIDString == characteristicId) found = ch;
});
if (found != null) return found;
return null;
}
private notifyKey(deviceId: string, serviceId: string, charId: string): string {
return `${deviceId}|${serviceId}|${charId}|notify`;
}
private operationKey(deviceId: string, serviceId: string, charId: string, op: string): string {
return `${deviceId}|${serviceId}|${charId}|${op}`;
}
private characteristicKey(deviceId: string, serviceId: string): string {
return `${deviceId}|${serviceId}`;
}
}

View File

@@ -0,0 +1,504 @@
// 蓝牙相关接口和类型定义
// 基础设备信息类型
export type BleDeviceInfo = {
deviceId : string;
name : string;
RSSI ?: number;
connected ?: boolean;
// 新增
serviceId ?: string;
writeCharId ?: string;
notifyCharId ?: string;
}
export type AutoDiscoverAllResult = {
services : BleService[];
characteristics : BleCharacteristic[];
}
// 服务信息类型
export type BleServiceInfo = {
uuid : string;
isPrimary : boolean;
}
// 特征值属性类型
export type BleCharacteristicProperties = {
read : boolean;
write : boolean;
notify : boolean;
indicate : boolean;
writeWithoutResponse ?: boolean;
canRead ?: boolean;
canWrite ?: boolean;
canNotify ?: boolean;
}
// 特征值信息类型
export type BleCharacteristicInfo = {
uuid : string;
serviceId : string;
properties : BleCharacteristicProperties;
}
// 错误状态码
export enum BleErrorCode {
UNKNOWN_ERROR = 0,
BLUETOOTH_UNAVAILABLE = 1,
PERMISSION_DENIED = 2,
DEVICE_NOT_CONNECTED = 3,
SERVICE_NOT_FOUND = 4,
CHARACTERISTIC_NOT_FOUND = 5,
OPERATION_TIMEOUT = 6
}
// 命令类型
export enum CommandType {
BATTERY = 1,
DEVICE_INFO = 2,
CUSTOM = 99,
TestBatteryLevel = 0x01
}
// 错误接口
export type BleError {
errCode : number;
errMsg : string;
errSubject ?: string;
}
// 连接选项
export type BleConnectOptions = {
deviceId : string;
timeout ?: number;
success ?: (result : any) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 断开连接选项
export type BleDisconnectOptions = {
deviceId : string;
success ?: (result : any) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 获取特征值选项
export type BleCharacteristicOptions = {
deviceId : string;
serviceId : string;
characteristicId : string;
success ?: (result : any) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 写入特征值选项
export type BleWriteOptions = {
deviceId : string;
serviceId : string;
characteristicId : string;
value : Uint8Array;
writeType ?: number;
success ?: (result : any) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// Options for writeCharacteristic helper
export type WriteCharacteristicOptions = {
waitForResponse ?: boolean;
maxAttempts ?: number;
retryDelayMs ?: number;
giveupTimeoutMs ?: number;
forceWriteTypeNoResponse ?: boolean;
}
// 通知特征值回调函数
export type BleNotifyCallback = (data : Uint8Array) => void;
// 通知特征值选项
export type BleNotifyOptions = {
deviceId : string;
serviceId : string;
characteristicId : string;
state ?: boolean; // true: 启用通知false: 禁用通知
onCharacteristicValueChange : BleNotifyCallback;
success ?: (result : any) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 获取服务选项
export type BleDeviceServicesOptions = {
deviceId : string;
success ?: (result : BleServicesResult) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 获取特征值选项
export type BleDeviceCharacteristicsOptions = {
deviceId : string;
serviceId : string;
success ?: (result : BleCharacteristicsResult) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 蓝牙扫描选项
export type BluetoothScanOptions = {
services ?: string[];
timeout ?: number;
onDeviceFound ?: (device : BleDeviceInfo) => void;
success ?: (result : BleScanResult) => void;
fail ?: (error : BleError) => void;
complete ?: (result : any) => void;
}
// 扫描结果
// 服务结果
export type BleServicesResult = {
services : BleServiceInfo[];
errMsg ?: string;
}
// 特征值结果
export type BleCharacteristicsResult = {
characteristics : BleCharacteristicInfo[];
errMsg ?: string;
}
// 定义连接状态枚举
export enum BLE_CONNECTION_STATE {
DISCONNECTED = 0,
CONNECTING = 1,
CONNECTED = 2,
DISCONNECTING = 3
}
// 电池状态类型定义
export type BatteryStatus = {
batteryLevel : number; // 电量百分比
isCharging : boolean; // 充电状态
}
// 蓝牙服务接口类型定义 - 转换为type类型
export type BleService = {
uuid : string;
isPrimary : boolean;
}
// 蓝牙特征值接口定义 - 转换为type类型
export type BleCharacteristic = {
uuid : string;
service : BleService;
properties : BleCharacteristicProperties;
}
// PendingPromise接口定义
export interface PendingCallback {
resolve : (data : any) => void;
reject : (err ?: any) => void;
timer ?: number;
}
// 蓝牙相关接口和类型定义
export type BleDevice = {
deviceId : string;
name : string;
rssi ?: number;
lastSeen ?: number; // 新增
// 新增
serviceId ?: string;
writeCharId ?: string;
notifyCharId ?: string;
}
// BLE常规选项
export type BleOptions = {
timeout ?: number;
success ?: (result : any) => void;
fail ?: (error : any) => void;
complete ?: () => void;
}
export type BleConnectionState = number; // 0: DISCONNECTED, 1: CONNECTING, 2: CONNECTED, 3: DISCONNECTING
export type BleConnectOptionsExt = {
timeout ?: number;
services ?: string[];
requireResponse ?: boolean;
autoReconnect ?: boolean;
maxAttempts ?: number;
interval ?: number;
};
// 回调函数类型
export type BleDeviceFoundCallback = (device : BleDevice) => void;
export type BleConnectionStateChangeCallback = (deviceId : string, state : BleConnectionState) => void;
export type BleDataPayload = {
deviceId : string;
serviceId ?: string;
characteristicId ?: string;
data : string | ArrayBuffer;
format ?: number; // 0: JSON, 1: XML, 2: RAW
}
export type BleDataSentCallback = (payload : BleDataPayload, success : boolean, error ?: BleError) => void;
export type BleErrorCallback = (error : BleError) => void;
// 健康数据类型定义
export enum HealthDataType {
HEART_RATE = 1,
BLOOD_OXYGEN = 2,
TEMPERATURE = 3,
STEP_COUNT = 4,
SLEEP_DATA = 5,
HEALTH_DATA = 6
}
// Shared health notification payloads used by protocol handler and UI consumers
export type HealthSubscription = {
serviceUuid : string
charUuid : string
}
export type WbbpPacket = {
cmd : number
seq : number
payload : Uint8Array
}
export type HealthData = {
type : 'heart' | 'spo2' | 'steps' | 'raw'
cmd ?: number
seq ?: number
timestamp ?: number
heartRate ?: number
spo2 ?: number
pulse ?: number
steps ?: number
distance ?: number
calories ?: number
quality ?: number
activity ?: number
raw ?: Uint8Array
payload?: Uint8Array;
data?: Uint8Array;
}
// Platform-specific services should be imported from per-platform entrypoints
// (e.g. './app-android/index.uts' or './web/index.uts').
// Avoid re-exporting platform modules at the SDK root to prevent bundlers
// Platform-specific services should be imported from per-platform entrypoints
// (e.g. './app-android/index.uts' or './web/index.uts').
// Avoid re-exporting platform modules at the SDK root to prevent bundlers
// from pulling android.* symbols into web bundles.
// If a typed ambient reference is required, declare the shape here instead of importing implementation.
// Example lightweight typed placeholder (do not import platform code here):
// export type BluetoothService = any; // platform-specific implementation exported from platform index files
// ====== 新增多协议、统一事件、协议适配、状态管理支持 ======
export type BleProtocolType =
| 'standard'
| 'custom'
| 'health'
| 'ibeacon'
| 'mesh';
export type BleEvent =
| 'deviceFound'
| 'scanFinished'
| 'connectionStateChanged'
| 'dataReceived'
| 'dataSent'
| 'error'
| 'servicesDiscovered'
| 'connected' // 新增
| 'disconnected'; // 新增
// 事件回调参数
export type BleEventPayload = {
event : BleEvent;
device ?: BleDevice;
protocol ?: BleProtocolType;
state ?: BleConnectionState;
data ?: ArrayBuffer | string | object;
format ?: string;
error ?: BleError;
extra ?: any;
}
// 事件回调函数
export type BleEventCallback = (payload : BleEventPayload) => void;
// 多协议设备信息(去除交叉类型,直接展开字段)
export type MultiProtocolDevice = {
deviceId : string;
name : string;
rssi ?: number;
protocol : BleProtocolType;
};
export type ScanDevicesOptions = {
protocols ?: BleProtocolType[];
optionalServices ?: string[];
timeout ?: number;
onDeviceFound ?: (device : BleDevice) => void;
onScanFinished ?: () => void;
};
// Named payload type used by sendData
export type SendDataPayload = {
deviceId : string;
serviceId ?: string;
characteristicId ?: string;
data : string | ArrayBuffer;
format ?: number;
protocol : BleProtocolType;
}
// 协议处理器接口(为 protocol-handler 适配器预留)
export type ScanHandler = {
protocol : BleProtocolType;
scanDevices ?: (options : ScanDevicesOptions) => Promise<void>;
connect : (device : BleDevice, options ?: BleConnectOptionsExt) => Promise<void>;
disconnect : (device : BleDevice) => Promise<void>;
// Optional: send arbitrary data via the protocol's write characteristic
sendData ?: (device : BleDevice, payload : SendDataPayload, options ?: BleOptions) => Promise<void>;
// Optional: try to connect and discover service/characteristic ids for this device
autoConnect ?: (device : BleDevice, options ?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
}
// 自动发现服务和特征返回类型
export type AutoBleInterfaces = {
serviceId : string;
writeCharId : string;
notifyCharId : string;
}
export type ResponseCallbackEntry = {
cb : (data : Uint8Array) => boolean | void;
multi : boolean;
};
// Result returned by a DFU control parser. Use a plain string `type` to keep
// the generated Kotlin simple and avoid inline union types which the generator
// does not handle well.
export type ControlParserResult = {
type : string; // e.g. 'progress', 'success', 'error', 'info'
progress ?: number;
error ?: any;
}
// DFU types
export type DfuOptions = {
mtu ?: number;
useNordic ?: boolean;
// If true, the DFU upload will await a write response per-packet. Set false to use
// WRITE_NO_RESPONSE (fire-and-forget) for higher throughput. Default: false.
waitForResponse ?: boolean;
// Maximum number of outstanding NO_RESPONSE writes to allow before throttling.
// This implements a simple sliding window. Default: 32.
maxOutstanding ?: number;
// Per-chunk sleep (ms) to yield to event loop / Android BLE stack. Default: 2.
writeSleepMs ?: number;
// Retry delay (ms) used by the Android write helper when gatt.writeCharacteristic
// returns false. Smaller values can improve throughput on congested stacks.
writeRetryDelayMs ?: number;
// Maximum number of immediate write attempts before falling back to the give-up timeout.
writeMaxAttempts ?: number;
// Timeout (ms) to wait for a late onCharacteristicWrite callback after all retries fail.
writeGiveupTimeoutMs ?: number;
// Packet Receipt Notification (PRN) window size in packets. If set, DFU
// manager will send a Set PRN command to the device and wait for PRN
// notifications after this many packets. Default: 12.
prn ?: number;
// Timeout (ms) to wait for a PRN notification once the window is reached.
// Default: 10000 (10s).
prnTimeoutMs ?: number;
// When true, disable PRN waits automatically after the first timeout to prevent
// repeated long stalls on devices that do not send PRNs. Default: true.
disablePrnOnTimeout ?: boolean;
// Time (ms) to wait for outstanding fire-and-forget writes to drain before issuing
// the activate/validate control command. Default: 3000.
drainOutstandingTimeoutMs ?: number;
controlTimeout ?: number;
onProgress ?: (percent : number) => void;
onLog ?: (message : string) => void;
controlParser ?: (data : Uint8Array) => ControlParserResult | null;
}
export type DfuManagerType = {
startDfu : (deviceId : string, firmwareBytes : Uint8Array, options ?: DfuOptions) => Promise<void>;
}
// Lightweight runtime / UTS shims and missing types
// These are conservative placeholders to satisfy typings used across platform files.
// UTSJSONObject: bundler environments used by the build may not support
// TypeScript-style index signatures in this .uts context. Use a conservative
// 'any' alias so generated code doesn't rely on unsupported syntax while
// preserving a usable type at the source level.
export type UTSJSONObject = any;
// ByteArray / Int are used in the Android platform code to interop with Java APIs.
// Define minimal aliases so source can compile. Runtime uses Uint8Array and number.
export type ByteArray = any; // runtime will use Java byte[] via UTS bridge; keep as any here
// Callback types used by service_manager and index wrappers
export type BleDataReceivedCallback = (data: Uint8Array) => void;
export type BleScanResult = {
deviceId: string;
name?: string;
rssi?: number;
advertising?: any;
};
// Minimal UI / framework placeholders (some files reference these in types only)
export type ComponentPublicInstance = any;
export type UniElement = any;
export type UniPage = any;
// Platform service contract (actual implementations live in per-platform entrypoints).
// Provide a lightweight interface so source-level code can rely on concrete method
// names and signatures without emitting duplicate runtime classes from this shared file.
export interface BluetoothService {
// Event emitter style
on(event: BleEvent | string, callback: BleEventCallback): void;
off(event: BleEvent | string, callback?: BleEventCallback): void;
// Scanning / discovery
scanDevices(options?: ScanDevicesOptions): Promise<void>;
// Connection management
connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt): Promise<void>;
disconnectDevice(deviceId: string, protocol?: string): Promise<void>;
getConnectedDevices(): MultiProtocolDevice[];
// Services / characteristics
getServices(deviceId: string): Promise<BleService[]>;
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]>;
// Read / write / notify
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer>;
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean>;
subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: BleNotifyCallback): Promise<void>;
unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void>;
// Convenience helpers
getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces>;
}
// Runtime protocol handler base class. Exporting a concrete class ensures the
// generator emits a resolvable runtime type that platform handlers can extend.
// Source-level code can still use the ScanHandler type for typing.
// Runtime ProtocolHandler is implemented in `protocol_handler.uts`.
// Keep the public typing in this file minimal to avoid duplicate runtime
// declarations. Consumers that need the runtime class should import it from
// './protocol_handler.uts'.

View File

@@ -0,0 +1,286 @@
import type {
BleDevice,
BleConnectionState,
BleEvent,
BleEventCallback,
BleEventPayload,
BleConnectOptionsExt,
AutoBleInterfaces,
BleDataPayload,
SendDataPayload,
BleOptions,
MultiProtocolDevice,
BleProtocolType,
ScanDevicesOptions
} from '../interface.uts';
import { ProtocolHandler } from '../protocol_handler.uts';
import { BluetoothService } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
type RawProtocolHandler = {
protocol?: BleProtocolType;
scanDevices?: (options?: ScanDevicesOptions) => Promise<void>;
connect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<void>;
disconnect?: (device: BleDevice) => Promise<void>;
sendData?: (device: BleDevice, payload?: SendDataPayload, options?: BleOptions) => Promise<void>;
autoConnect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
}
class DeviceContext {
device: BleDevice;
protocol: BleProtocolType;
state: BleConnectionState;
handler: ProtocolHandler;
constructor(device: BleDevice, protocol: BleProtocolType, handler: ProtocolHandler) {
this.device = device;
this.protocol = protocol;
this.state = 0;
this.handler = handler;
}
}
const deviceMap = new Map<string, DeviceContext>();
let activeProtocol: BleProtocolType = 'standard';
let activeHandler: ProtocolHandler | null = null;
const eventListeners = new Map<BleEvent, Set<BleEventCallback>>();
let defaultBluetoothService: BluetoothService | null = null;
let connectionHooked = false;
function emit(event: BleEvent, payload: BleEventPayload) {
const listeners = eventListeners.get(event);
if (listeners != null) {
listeners.forEach(cb => {
try { cb(payload); } catch (e) { }
});
}
}
class ProtocolHandlerWrapper extends ProtocolHandler {
private _raw: RawProtocolHandler | null;
constructor(raw?: RawProtocolHandler, bluetoothService?: BluetoothService) {
super(bluetoothService);
this._raw = raw ?? null;
}
override async scanDevices(options?: ScanDevicesOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.scanDevices == 'function') {
await rawTyped.scanDevices(options);
}
return;
}
override async connect(device: BleDevice, options?: BleConnectOptionsExt): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.connect == 'function') {
await rawTyped.connect(device, options);
}
return;
}
override async disconnect(device: BleDevice): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.disconnect == 'function') {
await rawTyped.disconnect(device);
}
return;
}
override async sendData(device: BleDevice, payload?: SendDataPayload, options?: BleOptions): Promise<void> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.sendData == 'function') {
await rawTyped.sendData(device, payload, options);
}
return;
}
override async autoConnect(device: BleDevice, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> {
const rawTyped = this._raw;
if (rawTyped != null && typeof rawTyped.autoConnect == 'function') {
return await rawTyped.autoConnect(device, options);
}
return { serviceId: '', writeCharId: '', notifyCharId: '' };
}
}
function isRawProtocolHandler(x: any): boolean {
if (x == null || typeof x !== 'object') return false;
const r = x as Record<string, unknown>;
if (typeof r['scanDevices'] == 'function') return true;
if (typeof r['connect'] == 'function') return true;
if (typeof r['disconnect'] == 'function') return true;
if (typeof r['sendData'] == 'function') return true;
if (typeof r['autoConnect'] == 'function') return true;
if (typeof r['protocol'] == 'string') return true;
return false;
}
function ensureConnectionHook() {
if (connectionHooked) return;
connectionHooked = true;
const dm = DeviceManager.getInstance();
dm.onConnectionStateChange((deviceId, state) => {
let matched = false;
deviceMap.forEach((ctx) => {
if (ctx.device.deviceId == deviceId) {
ctx.state = state;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol: ctx.protocol, state });
matched = true;
}
});
if (!matched) {
emit('connectionStateChanged', { event: 'connectionStateChanged', device: { deviceId, name: '', rssi: 0 }, protocol: activeProtocol, state });
}
});
}
export const registerProtocolHandler = (handler: any) => {
if (handler == null) return;
let proto: BleProtocolType = 'standard';
if (handler instanceof ProtocolHandler) {
try { proto = (handler as ProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = handler as ProtocolHandler;
} else if (isRawProtocolHandler(handler)) {
try { proto = (handler as RawProtocolHandler).protocol as BleProtocolType; } catch (e) { }
activeHandler = new ProtocolHandlerWrapper(handler as RawProtocolHandler, defaultBluetoothService);
(activeHandler as ProtocolHandler).protocol = proto;
} else {
console.warn('[AKBLE] registerProtocolHandler: unsupported handler type, ignoring', handler);
return;
}
activeProtocol = proto;
ensureConnectionHook();
}
export const scanDevices = async (options?: ScanDevicesOptions): Promise<void> => {
ensureDefaultProtocolHandler();
if (activeHandler == null) {
console.log('[AKBLE] no active scan handler registered');
return;
}
const handler = activeHandler as ProtocolHandler;
const original = options ?? null;
const scanOptions: ScanDevicesOptions = {} as ScanDevicesOptions;
if (original != null) {
if (original.protocols != null) scanOptions.protocols = original.protocols;
if (original.optionalServices != null) scanOptions.optionalServices = original.optionalServices;
if (original.timeout != null) scanOptions.timeout = original.timeout;
}
const userFound = original?.onDeviceFound ?? null;
scanOptions.onDeviceFound = (device: BleDevice) => {
emit('deviceFound', { event: 'deviceFound', device });
if (userFound != null) {
try { userFound(device); } catch (err) { }
}
};
const userFinished = original?.onScanFinished ?? null;
scanOptions.onScanFinished = () => {
emit('scanFinished', { event: 'scanFinished' });
if (userFinished != null) {
try { userFinished(); } catch (err) { }
}
};
try {
await handler.scanDevices(scanOptions);
} catch (e) {
console.warn('[AKBLE] scanDevices handler error', e);
}
}
export const connectDevice = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<void> => {
const handler = activeHandler;
if (handler == null) throw new Error('No protocol handler');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
await handler.connect(device, options);
const ctx = new DeviceContext(device, protocol, handler);
ctx.state = 2;
deviceMap.set(getDeviceKey(deviceId, protocol), ctx);
emit('connectionStateChanged', { event: 'connectionStateChanged', device, protocol, state: 2 });
}
export const disconnectDevice = async (deviceId: string, protocol: BleProtocolType): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null || ctx.handler == null) return;
await ctx.handler.disconnect(ctx.device);
ctx.state = 0;
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol, state: 0 });
deviceMap.delete(getDeviceKey(deviceId, protocol));
}
export const sendData = async (payload: SendDataPayload, options?: BleOptions): Promise<void> => {
const ctx = deviceMap.get(getDeviceKey(payload.deviceId, payload.protocol));
if (ctx == null) throw new Error('Device not connected');
const deviceCtx = ctx as DeviceContext;
if (deviceCtx.handler == null) throw new Error('sendData not supported for this protocol');
await deviceCtx.handler.sendData(deviceCtx.device, payload, options);
emit('dataSent', { event: 'dataSent', device: deviceCtx.device, protocol: payload.protocol, data: payload.data });
}
export const getConnectedDevices = (): MultiProtocolDevice[] => {
const result: MultiProtocolDevice[] = [];
deviceMap.forEach((ctx: DeviceContext) => {
const dev: MultiProtocolDevice = {
deviceId: ctx.device.deviceId,
name: ctx.device.name,
rssi: ctx.device.rssi,
protocol: ctx.protocol
};
result.push(dev);
});
return result;
}
export const getConnectionState = (deviceId: string, protocol: BleProtocolType): BleConnectionState => {
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
if (ctx == null) return 0;
return ctx.state;
}
export const on = (event: BleEvent, callback: BleEventCallback) => {
if (!eventListeners.has(event)) eventListeners.set(event, new Set());
eventListeners.get(event)!.add(callback);
}
export const off = (event: BleEvent, callback?: BleEventCallback) => {
if (callback == null) {
eventListeners.delete(event);
} else {
eventListeners.get(event)?.delete(callback as BleEventCallback);
}
}
function getDeviceKey(deviceId: string, protocol: BleProtocolType): string {
return `${deviceId}|${protocol}`;
}
export const autoConnect = async (deviceId: string, protocol: BleProtocolType, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> => {
const handler = activeHandler;
if (handler == null) throw new Error('autoConnect not supported for this protocol');
const device: BleDevice = { deviceId, name: '', rssi: 0 };
return await handler.autoConnect(device, options) as AutoBleInterfaces;
}
function ensureDefaultProtocolHandler(): void {
if (activeHandler != null) return;
const service = defaultBluetoothService;
if (service == null) return;
try {
const dm = DeviceManager.getInstance();
const raw: RawProtocolHandler = {
protocol: 'standard',
scanDevices: (options?: ScanDevicesOptions) => {
return dm.startScan(options ?? {} as ScanDevicesOptions);
},
connect: (device, options?: BleConnectOptionsExt) => dm.connectDevice(device.deviceId, options),
disconnect: (device) => dm.disconnectDevice(device.deviceId),
autoConnect: () => Promise.resolve({ serviceId: '', writeCharId: '', notifyCharId: '' })
};
const wrapper = new ProtocolHandlerWrapper(raw, service);
activeHandler = wrapper;
activeProtocol = raw.protocol as BleProtocolType;
ensureConnectionHook();
console.log('[AKBLE] default protocol handler registered', activeProtocol);
} catch (e) {
console.warn('[AKBLE] failed to register default protocol handler', e);
}
}
export const setDefaultBluetoothService = (service: BluetoothService) => {
defaultBluetoothService = service;
ensureDefaultProtocolHandler();
};

View File

@@ -0,0 +1,5 @@
{
"dependencies": [
]
}

View File

@@ -0,0 +1,250 @@
import type { BleDevice, BleConnectOptionsExt, BleConnectionState, BleConnectionStateChangeCallback, ScanDevicesOptions } from '../interface.uts';
declare const wx: any;
type PendingConnect = {
resolve: () => void;
reject: (err?: any) => void;
timer?: number;
};
function now(): number {
return Date.now();
}
export class DeviceManager {
private static instance: DeviceManager | null = null;
private devices = new Map<string, BleDevice>();
private connectionStates = new Map<string, BleConnectionState>();
private connectionListeners: BleConnectionStateChangeCallback[] = [];
private pendingConnects = new Map<string, PendingConnect>();
private scanOptions: ScanDevicesOptions | null = null;
private scanTimer: number | null = null;
private adapterReady: boolean = false;
private adapterPromise: Promise<void> | null = null;
private discoveryActive: boolean = false;
private deviceFoundRegistered: boolean = false;
private connectionEventRegistered: boolean = false;
private constructor() {}
static getInstance(): DeviceManager {
if (DeviceManager.instance == null) {
DeviceManager.instance = new DeviceManager();
}
return DeviceManager.instance!;
}
private ensureAdapter(): Promise<void> {
if (this.adapterReady) return Promise.resolve();
if (this.adapterPromise != null) return this.adapterPromise!;
this.adapterPromise = new Promise<void>((resolve, reject) => {
wx.openBluetoothAdapter({
success: () => {
this.adapterReady = true;
this.adapterPromise = null;
this.ensureEventHandlers();
resolve();
},
fail: (err: any) => {
this.adapterPromise = null;
reject(err ?? new Error('openBluetoothAdapter failed'));
}
});
});
return this.adapterPromise!;
}
private ensureEventHandlers() {
if (!this.deviceFoundRegistered) {
this.deviceFoundRegistered = true;
wx.onBluetoothDeviceFound((res: any) => {
try { this.handleDeviceFound(res); } catch (e) { }
});
}
if (!this.connectionEventRegistered) {
this.connectionEventRegistered = true;
wx.onBLEConnectionStateChange((res: any) => {
try { this.handleConnectionState(res); } catch (e) { }
});
}
}
startScan(options: ScanDevicesOptions): Promise<void> {
return this.ensureAdapter().then(() => {
return this.beginScan(options ?? {} as ScanDevicesOptions);
});
}
private beginScan(options: ScanDevicesOptions): Promise<void> {
if (this.discoveryActive) {
this.stopScanInternal();
}
this.scanOptions = options;
const services = options.optionalServices ?? null;
return new Promise<void>((resolve, reject) => {
wx.startBluetoothDevicesDiscovery({
services: services ?? undefined,
allowDuplicatesKey: false,
success: () => {
this.discoveryActive = true;
if (options.timeout != null && options.timeout > 0) {
this.scanTimer = setTimeout(() => {
this.stopScanInternal();
}, options.timeout);
}
resolve();
},
fail: (err: any) => {
this.discoveryActive = false;
reject(err ?? new Error('startBluetoothDevicesDiscovery failed'));
}
});
});
}
stopScan(): void {
this.stopScanInternal();
}
private stopScanInternal() {
if (!this.discoveryActive) return;
this.discoveryActive = false;
try {
wx.stopBluetoothDevicesDiscovery({});
} catch (e) { }
if (this.scanTimer != null) {
clearTimeout(this.scanTimer);
this.scanTimer = null;
}
const finished = this.scanOptions?.onScanFinished;
this.scanOptions = null;
if (finished != null) {
try { finished(); } catch (e) { }
}
}
private handleDeviceFound(res: any) {
const list: any[] = res?.devices ?? [];
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (item == null) continue;
const deviceId = item.deviceId ?? item.deviceId ?? '';
if (!deviceId) continue;
const name = item.name ?? item.localName ?? 'Unknown';
const rssi = item.RSSI ?? item.rssi ?? 0;
let device = this.devices.get(deviceId);
if (device == null) {
device = { deviceId, name, rssi, lastSeen: now() };
this.devices.set(deviceId, device);
} else {
device.name = name;
device.rssi = rssi;
device.lastSeen = now();
}
const cb = this.scanOptions?.onDeviceFound;
if (cb != null) {
try { cb(device); } catch (e) { }
}
}
}
private handleConnectionState(res: any) {
const deviceId = res?.deviceId ?? '';
if (!deviceId) return;
const connected = res?.connected === true;
const state: BleConnectionState = connected ? 2 : 0;
this.connectionStates.set(deviceId, state);
const pending = this.pendingConnects.get(deviceId);
if (pending != null) {
this.pendingConnects.delete(deviceId);
if (pending.timer != null) clearTimeout(pending.timer);
if (connected) {
try { pending.resolve(); } catch (e) { }
} else {
try { pending.reject(new Error('连接断开')); } catch (e) { }
}
}
for (let i = 0; i < this.connectionListeners.length; i++) {
const listener = this.connectionListeners[i];
try { listener(deviceId, state); } catch (e) { }
}
}
connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
return this.ensureAdapter().then(() => {
const timeout = options?.timeout ?? 15000;
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingConnects.delete(deviceId);
reject(new Error('连接超时'));
}, timeout);
this.pendingConnects.set(deviceId, { resolve, reject, timer });
wx.createBLEConnection({
deviceId,
timeout,
success: () => {
this.pendingConnects.delete(deviceId);
clearTimeout(timer);
this.connectionStates.set(deviceId, 2);
resolve();
},
fail: (err: any) => {
this.pendingConnects.delete(deviceId);
clearTimeout(timer);
reject(err ?? new Error('createBLEConnection failed'));
}
});
});
});
}
disconnectDevice(deviceId: string): Promise<void> {
return new Promise((resolve, reject) => {
wx.closeBLEConnection({
deviceId,
success: () => {
this.connectionStates.set(deviceId, 0);
resolve();
},
fail: (err: any) => {
reject(err ?? new Error('closeBLEConnection failed'));
}
});
});
}
reconnectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
const attempts = options?.maxAttempts ?? 3;
const interval = options?.interval ?? 3000;
let count = 0;
const attempt = (): Promise<void> => {
return this.connectDevice(deviceId, options).catch((err) => {
count++;
if (count >= attempts) throw err;
return new Promise<void>((resolve) => {
setTimeout(() => resolve(attempt()), interval);
});
});
};
return attempt();
}
getConnectedDevices(): BleDevice[] {
const result: BleDevice[] = [];
this.devices.forEach((device, id) => {
if (this.connectionStates.get(id) == 2) {
result.push(device);
}
});
return result;
}
onConnectionStateChange(listener: BleConnectionStateChangeCallback) {
this.connectionListeners.push(listener);
}
getDevice(deviceId: string): BleDevice | null {
return this.devices.get(deviceId) ?? null;
}
}

View File

@@ -0,0 +1,9 @@
import type { DfuManagerType, DfuOptions } from '../interface.uts';
class MpWeixinDfuManager implements DfuManagerType {
async startDfu(_deviceId: string, _firmwareBytes: Uint8Array, _options?: DfuOptions): Promise<void> {
throw new Error('小程序平台暂未实现 DFU 功能');
}
}
export const dfuManager = new MpWeixinDfuManager();

View File

@@ -0,0 +1,90 @@
import * as BluetoothManager from './bluetooth_manager.uts';
import { ServiceManager } from './service_manager.uts';
import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback, BleProtocolType, BluetoothService as BluetoothServiceContract } from '../interface.uts';
const serviceManager = ServiceManager.getInstance();
class MpWeixinBluetoothService implements BluetoothServiceContract {
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
return BluetoothManager.scanDevices(options ?? null);
}
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
}
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
return BluetoothManager.disconnectDevice(deviceId, proto);
}
getConnectedDevices(): MultiProtocolDevice[] {
return BluetoothManager.getConnectedDevices();
}
on(event: BleEvent | string, callback: BleEventCallback): void {
BluetoothManager.on(event as BleEvent, callback);
}
off(event: BleEvent | string, callback?: BleEventCallback | null): void {
BluetoothManager.off(event as BleEvent, callback ?? null);
}
getServices(deviceId: string): Promise<BleService[]> {
return serviceManager.getServices(deviceId);
}
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
return serviceManager.getCharacteristics(deviceId, serviceId);
}
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
const services = await this.getServices(deviceId);
if (services.length == 0) throw new Error('未发现服务');
let serviceId = services[0].uuid;
for (let i = 0; i < services.length; i++) {
const uuid = services[i].uuid ?? '';
if (/^bae/i.test(uuid)) {
serviceId = uuid;
break;
}
}
const characteristics = await this.getCharacteristics(deviceId, serviceId);
if (characteristics.length == 0) throw new Error('未发现特征值');
let writeCharId = '';
let notifyCharId = '';
for (let i = 0; i < characteristics.length; i++) {
const c = characteristics[i];
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse == true)) {
writeCharId = c.uuid;
}
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) {
notifyCharId = c.uuid;
}
}
if (writeCharId == '' || notifyCharId == '') throw new Error('未找到合适的写入或通知特征');
return { serviceId, writeCharId, notifyCharId };
}
subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
}
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
}
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, value, options);
}
unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
}
autoDiscoverAll(deviceId: string): Promise<any> {
return serviceManager.autoDiscoverAll(deviceId);
}
subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
return serviceManager.subscribeAllNotifications(deviceId, onData);
}
}
export class BluetoothServiceShape extends MpWeixinBluetoothService {}
const bluetoothServiceInstance = new BluetoothServiceShape();
BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance);
export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance;
export function getBluetoothService(): BluetoothServiceShape {
return bluetoothServiceInstance;
}
export { dfuManager } from './dfu_manager.uts';

View File

@@ -0,0 +1,268 @@
import type { BleService, BleCharacteristic, BleDataReceivedCallback, BleCharacteristicProperties, WriteCharacteristicOptions, AutoDiscoverAllResult } from '../interface.uts';
import type { BleDevice } from '../interface.uts';
import { DeviceManager } from './device_manager.uts';
declare const wx: any;
type PendingRead = {
resolve: (data: ArrayBuffer) => void;
reject: (err?: any) => void;
timer?: number;
};
function toUint8Array(buffer: ArrayBuffer): Uint8Array {
return new Uint8Array(buffer ?? new ArrayBuffer(0));
}
function toArrayBuffer(bytes: Uint8Array | ArrayBuffer): ArrayBuffer {
return bytes instanceof Uint8Array ? bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) : (bytes ?? new ArrayBuffer(0));
}
function makeProperties(item: any): BleCharacteristicProperties {
const props = item?.properties ?? {};
const read = props.read === true;
const write = props.write === true;
const notify = props.notify === true;
const indicate = props.indicate === true;
const writeNoRsp = props.writeNoResponse === true || props.writeWithoutResponse === true;
return {
read,
write,
notify,
indicate,
writeWithoutResponse: writeNoRsp,
canRead: read,
canWrite: write || writeNoRsp,
canNotify: notify || indicate
};
}
export class ServiceManager {
private static instance: ServiceManager | null = null;
private services = new Map<string, BleService[]>();
private characteristics = new Map<string, Map<string, BleCharacteristic[]>>();
private pendingReads = new Map<string, PendingRead>();
private notifyCallbacks = new Map<string, BleDataReceivedCallback>();
private listenersRegistered: boolean = false;
private deviceManager = DeviceManager.getInstance();
private constructor() {}
static getInstance(): ServiceManager {
if (ServiceManager.instance == null) {
ServiceManager.instance = new ServiceManager();
}
return ServiceManager.instance!;
}
private ensureListeners() {
if (this.listenersRegistered) return;
this.listenersRegistered = true;
wx.onBLECharacteristicValueChange((res: any) => {
try { this.handleNotify(res); } catch (e) { }
});
}
private cacheKey(deviceId: string, serviceId: string): string {
return `${deviceId}|${serviceId}`;
}
private notifyKey(deviceId: string, serviceId: string, characteristicId: string): string {
return `${deviceId}|${serviceId}|${characteristicId}`;
}
async getServices(deviceId: string, callback?: (services: BleService[] | null, error?: Error) => void): Promise<BleService[]> {
const cached = this.services.get(deviceId);
if (cached != null && cached.length > 0) {
if (callback != null) callback(cached, null);
return cached;
}
return new Promise((resolve, reject) => {
wx.getBLEDeviceServices({
deviceId,
success: (res: any) => {
const list: BleService[] = [];
const services: any[] = res?.services ?? [];
for (let i = 0; i < services.length; i++) {
const svc = services[i];
if (svc == null) continue;
list.push({ uuid: svc.uuid, isPrimary: svc.isPrimary === true });
}
this.services.set(deviceId, list);
if (callback != null) callback(list, null);
resolve(list);
},
fail: (err: any) => {
const error = err ?? new Error('getBLEDeviceServices failed');
if (callback != null) callback(null, error);
reject(error);
}
});
});
}
async getCharacteristics(deviceId: string, serviceId: string, callback?: (list: BleCharacteristic[] | null, error?: Error) => void): Promise<BleCharacteristic[]> {
const map = this.characteristics.get(deviceId);
const cached = map != null ? map.get(serviceId) : null;
if (cached != null && cached.length > 0) {
if (callback != null) callback(cached, null);
return cached;
}
return new Promise((resolve, reject) => {
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res: any) => {
const list: BleCharacteristic[] = [];
const chars: any[] = res?.characteristics ?? [];
for (let i = 0; i < chars.length; i++) {
const ch = chars[i];
if (ch == null) continue;
list.push({
uuid: ch.uuid,
service: { uuid: serviceId, isPrimary: true },
properties: makeProperties(ch)
});
}
let mapRef = this.characteristics.get(deviceId);
if (mapRef == null) {
mapRef = new Map<string, BleCharacteristic[]>();
this.characteristics.set(deviceId, mapRef);
}
mapRef.set(serviceId, list);
if (callback != null) callback(list, null);
resolve(list);
},
fail: (err: any) => {
const error = err ?? new Error('getBLEDeviceCharacteristics failed');
if (callback != null) callback(null, error);
reject(error);
}
});
});
}
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
this.ensureListeners();
return new Promise<ArrayBuffer>((resolve, reject) => {
const key = `${deviceId}|${serviceId}|${characteristicId}|read`;
const timer = setTimeout(() => {
this.pendingReads.delete(key);
reject(new Error('读取超时'));
}, 10000);
this.pendingReads.set(key, { resolve, reject, timer });
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
fail: (err: any) => {
this.pendingReads.delete(key);
clearTimeout(timer);
reject(err ?? new Error('readBLECharacteristicValue failed'));
}
});
});
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
const buffer = toArrayBuffer(value);
return new Promise<boolean>((resolve, reject) => {
wx.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: buffer,
fail: (err: any) => {
reject(err ?? new Error('writeBLECharacteristicValue failed'));
},
success: () => {
resolve(true);
}
});
});
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: BleDataReceivedCallback): Promise<void> {
this.ensureListeners();
const key = this.notifyKey(deviceId, serviceId, characteristicId);
this.notifyCallbacks.set(key, callback);
return new Promise<void>((resolve, reject) => {
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId,
state: true,
success: () => resolve(),
fail: (err: any) => {
this.notifyCallbacks.delete(key);
reject(err ?? new Error('notifyBLECharacteristicValueChange failed'));
}
});
});
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
const key = this.notifyKey(deviceId, serviceId, characteristicId);
this.notifyCallbacks.delete(key);
return new Promise<void>((resolve, reject) => {
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId,
state: false,
success: () => resolve(),
fail: (err: any) => {
reject(err ?? new Error('notifyBLECharacteristicValueChange disable failed'));
}
});
});
}
async autoDiscoverAll(deviceId: string): Promise<AutoDiscoverAllResult> {
const services = await this.getServices(deviceId);
const allChars: BleCharacteristic[] = [];
for (let i = 0; i < services.length; i++) {
const svc = services[i];
const chars = await this.getCharacteristics(deviceId, svc.uuid);
for (let j = 0; j < chars.length; j++) {
allChars.push(chars[j]);
}
}
return { services, characteristics: allChars };
}
async subscribeAllNotifications(deviceId: string, callback: BleDataReceivedCallback): Promise<void> {
const services = await this.getServices(deviceId);
for (let i = 0; i < services.length; i++) {
const svc = services[i];
const chars = await this.getCharacteristics(deviceId, svc.uuid);
for (let j = 0; j < chars.length; j++) {
const ch = chars[j];
if (ch.properties != null && (ch.properties.notify || ch.properties.indicate)) {
try {
await this.subscribeCharacteristic(deviceId, svc.uuid, ch.uuid, callback);
} catch (e) { }
}
}
}
}
private handleNotify(res: any) {
const deviceId = res?.deviceId ?? '';
const serviceId = res?.serviceId ?? '';
const characteristicId = res?.characteristicId ?? '';
const key = `${deviceId}|${serviceId}|${characteristicId}|read`;
const buffer: ArrayBuffer = res?.value ?? new ArrayBuffer(0);
const pending = this.pendingReads.get(key);
if (pending != null) {
this.pendingReads.delete(key);
if (pending.timer != null) clearTimeout(pending.timer);
try { pending.resolve(buffer); } catch (e) { }
}
const notifyKey = this.notifyKey(deviceId, serviceId, characteristicId);
const cb = this.notifyCallbacks.get(notifyKey);
if (cb != null) {
try { cb(toUint8Array(buffer)); } catch (e) { }
}
}
}

View File

@@ -0,0 +1,977 @@
// Minimal ProtocolHandler runtime class used by pages and components.
// This class adapts the platform `BluetoothService` to a small protocol API
// expected by pages: setConnectionParameters, initialize, testBatteryLevel,
// testVersionInfo. Implemented conservatively to avoid heavy dependencies.
import type { BluetoothService, AutoBleInterfaces, BleService, BleCharacteristic, BleProtocolType, BleDevice, ScanDevicesOptions, BleConnectOptionsExt, SendDataPayload, BleOptions, HealthSubscription, WbbpPacket, HealthData } from './interface.uts'
export type PingResult = {
seq : number;
latencyMs : number;
payload : Uint8Array;
raw : Uint8Array;
ok : boolean;
}
type PendingPingRequest = {
seq : number;
resolve : (result : PingResult) => void;
reject : (reason ?: any) => void;
startedAt : number;
timer : number | null;
}
export class ProtocolHandler {
// bluetoothService may be omitted for lightweight wrappers; allow null
bluetoothService : BluetoothService | null = null
protocol : BleProtocolType = 'standard'
deviceId : string | null = null
serviceId : string | null = null
writeCharId : string | null = null
notifyCharId : string | null = null
healthSubscriptions : HealthSubscription[] = []
initialized : boolean = false
controlResolved : boolean = false
pingSequence : number = 1
pendingPingRequests : PendingPingRequest[] = []
// Accept an optional BluetoothService-like object so wrapper subclasses can call
// `super()` without forcing a runtime instance. Allow broader inputs to satisfy
// legacy callers that pass platform-specific subclasses at runtime.
constructor(bluetoothService ?: BluetoothService | any) {
if (bluetoothService != null) {
this.bluetoothService = bluetoothService as BluetoothService
}
}
// Store active health subscriptions so we can unsubscribe later
// onData will be called with a parsed HealthData object
async subscribeHealthNotifications(onData : (data : HealthData) => void) {
if (this.deviceId == null) throw new Error('deviceId not set')
if (this.bluetoothService == null) throw new Error('bluetoothService not set')
const dev = '' + this.deviceId
const bsvc = this.bluetoothService
const self = this
function wrapHealthPacket(data: HealthData): HealthData {
// Since healthble.uvue handles both direct property access and method calls,
// we can just return the data as-is
return data
}
const deliver = (payload : HealthData) => {
try {
onData(wrapHealthPacket(payload))
} catch (deliverError) {
console.warn('[ProtocolHandler] health notify delivery error', deliverError)
}
}
// candidate UUIDs per protocol doc
const CUSTOM_HEALTH_SERVICE = '6e400010-b5a3-f393-e0a9-e50e24dcca9e'
const CUSTOM_HEART = '6e400011-b5a3-f393-e0a9-e50e24dcca9e'
const CUSTOM_STEP = '6e400012-b5a3-f393-e0a9-e50e24dcca9e'
const CUSTOM_SLEEP = '6e400013-b5a3-f393-e0a9-e50e24dcca9e'
// also consider standard characteristics
const STD_HEART_CHAR = '00002a37-0000-1000-8000-00805f9b34fb' // 2A37
const STD_SPO2_CHAR = '00002a5f-0000-1000-8000-00805f9b34fb' // 2A5F (if present)
function xorCheck(buf : Uint8Array) : boolean {
if (buf == null || buf.length < 4) return false
let x = 0
for (let i = 1; i < buf.length - 1; i++) x ^= buf[i]
return (x & 0xff) == buf[buf.length - 1]
}
function parseWbbp(buf : Uint8Array) : WbbpPacket | null {
// STX LEN CMD SEQ DATA.. CRC(1 or 2 bytes)
if (buf == null || buf.length < 5) return null
if (buf[0] != 0xAA) return null
const len = buf[1]
const minTotal = len + 3 // STX + LEN + LEN bytes + CRC(1)
const altTotal = len + 4 // allow CRC(2) variant observed on newer devices
if (buf.length < minTotal) return null
const dataEnd = 2 + len // index after CMD+SEQ+payload
const crcLength = buf.length - dataEnd
if (crcLength < 1) return null
if (crcLength == 1) {
// original XOR checksum validation
let x = 0
for (let i = 1; i < dataEnd; i++) {
x ^= buf[i]
}
if ((x & 0xff) != buf[buf.length - 1]) {
console.warn('[ProtocolHandler] WBBP checksum mismatch (xor)', { len, data: Array.from(buf) })
return null
}
} else {
// two-byte checksum present accept packet even without knowing CRC16 polynomial
if (buf.length != altTotal) {
console.warn('[ProtocolHandler] WBBP packet length unexpected', { len, total: buf.length })
return null
}
}
const cmd = buf[2]
const seq = buf[3]
const payloadLength = Math.max(0, len - 2)
const payload = buf.slice(4, 4 + payloadLength)
return { cmd, seq, payload }
}
function parseHeartStandard(buf : Uint8Array) {
// Bluetooth SIG Heart Rate Measurement (2A37)
if (buf == null || buf.length < 2) return null
const flags = buf[0]
const hrFormat16 = (flags & 0x01) != 0
let hr = 0
if (!hrFormat16) {
hr = buf[1]
} else {
hr = buf[1] | (buf[2] << 8)
}
return { heartRate: hr }
}
// subscribe helper that registers a callback and stores subscription info
async function subscribeChar(svc : string, char : string) {
try {
console.log('[ProtocolHandler] subscribing to', svc, char)
await bsvc.subscribeCharacteristic(dev, svc, char, (data : Uint8Array) => {
try {
// try WBBP parsing first
console.log(char,data)
const pkt = parseWbbp(data) as WbbpPacket | null
if (pkt != null) {
const cmd = pkt.cmd
const seq = pkt.seq
const p = pkt.payload
if (cmd == 0x00) {
self.handlePingNotify(seq, data, p)
return
} else if (cmd == 0x10) {
// heart rate packet per doc: timestamp(4) + heart(1) + quality(1)
if (p.length >= 6) {
const ts = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]
const heart = p[4]
const quality = p[5]
deliver({ type: 'heart', cmd, seq, timestamp: ts, heartRate: heart, quality })
return
}
} else if (cmd == 0x14) {
// (subscription recorded later)
if (p.length >= 8) {
const ts = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]
const spo2 = p[4]
const quality = p[5]
const pulse = p[6]
deliver({ type: 'spo2', cmd, seq, timestamp: ts, spo2, pulse, quality })
return
}
} else if (cmd == 0x11) {
// step count: timestamp(4) + steps(4) + distance(2) + calories(2) + activity(1)
if (p.length >= 13) {
const ts = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]
const steps = (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]
const distance = (p[8] << 8) | p[9]
const calories = (p[10] << 8) | p[11]
const activity = p[12]
deliver({ type: 'steps', cmd, seq, timestamp: ts, steps, distance, calories, activity })
return
}
}
}
// if not WBBP or parsing failed, try standard heart/spo2 formats
const stdHeart = parseHeartStandard(data)
if (stdHeart != null) {
const heartRate = (stdHeart as UTSJSONObject)['heartRate'] as number
const data : HealthData = { type: 'heart', heartRate: heartRate }
deliver(data)
return
}
// fallback: raw notify
deliver({ type: 'raw', raw: data })
} catch (e) { console.warn('[ProtocolHandler] health notify handler error', e) }
})
// remember to unsubscribe later
self.healthSubscriptions.push({ serviceUuid: svc, charUuid: char })
console.log('[ProtocolHandler] subscribeChar registered', svc, char)
} catch (e) { console.warn('[ProtocolHandler] subscribeChar failed', svc, char, e) }
}
// try subscribe to custom health service chars first
try {
const allSvcs = await bsvc.getServices(dev)
console.log('[ProtocolHandler] allServices', allSvcs)
const svcSet = new Set(allSvcs.map((s : BleService) => ('' + s.uuid).toLowerCase()))
if (svcSet.has(CUSTOM_HEALTH_SERVICE) || Array.from(svcSet).some((u) => (u as string).indexOf('6e400010') != -1)) {
console.log('[ProtocolHandler] going to subscribe', CUSTOM_HEALTH_SERVICE, CUSTOM_HEART, CUSTOM_STEP, CUSTOM_SLEEP)
// prefer custom UUIDs
await subscribeChar(CUSTOM_HEALTH_SERVICE, CUSTOM_HEART)
await subscribeChar(CUSTOM_HEALTH_SERVICE, CUSTOM_STEP)
await subscribeChar(CUSTOM_HEALTH_SERVICE, CUSTOM_SLEEP)
} else {
// fallback: try standard heart and spo2 characteristics across services
for (let i = 0; i < allSvcs.length; i++) {
const s = allSvcs[i]
if (s == null) continue
const sUuid = '' + s.uuid
const chars = await bsvc.getCharacteristics(dev, sUuid)
for (let j = 0; j < chars.length; j++) {
const c = chars[j]
if (c == null || c.uuid == null) continue
const id = ('' + c.uuid).toLowerCase()
if (id == STD_HEART_CHAR || id.indexOf('2a37') != -1) {
await subscribeChar(sUuid, c.uuid)
}
if (id == STD_SPO2_CHAR || id.indexOf('2a5f') != -1) {
await subscribeChar(sUuid, c.uuid)
}
}
}
}
} catch (e) { console.warn('[ProtocolHandler] subscribeHealthNotifications failed', e) }
// If we didn't find standard/custom health characteristics, try a conservative
// fallback: subscribe to any notify/indicate-capable characteristic so we can
// receive vendor-specific notifies. Cap the number to avoid too many subscriptions.
if (this.healthSubscriptions.length == 0) {
console.log('[ProtocolHandler] no known health chars found — attempting auto-subscribe to notify-capable characteristics')
try {
let autoCount = 0
const svcList = await bsvc.getServices(dev)
for (let si = 0; si < svcList.length; si++) {
if (autoCount >= 6) break
const s = svcList[si]
if (s == null) continue
const sUuid = '' + s.uuid
const chars = await bsvc.getCharacteristics(dev, sUuid)
for (let ci = 0; ci < chars.length; ci++) {
if (autoCount >= 6) break
const c = chars[ci]
if (c == null) continue
try {
const _propsAny = c.properties
if (c.properties != null && (_propsAny.notify || _propsAny.indicate)) {
await subscribeChar(sUuid, c.uuid)
autoCount++
console.log('[ProtocolHandler] auto-subscribed to', sUuid, c.uuid)
}
} catch (e) { console.warn('[ProtocolHandler] auto-subscribe char failed', sUuid, c.uuid, e) }
}
}
} catch (e) { console.warn('[ProtocolHandler] auto-subscribe notify-capable failed', e) }
}
console.log('[ProtocolHandler] subscribeHealthNotifications complete, subscriptions=', this.healthSubscriptions.length)
}
handlePingNotify(seq : number, raw : Uint8Array, payload : Uint8Array) : void {
try {
console.log('[ProtocolHandler] PING received', { seq, raw: Array.from(raw) })
} catch (e) { }
for (let i = 0; i < this.pendingPingRequests.length; i++) {
const entry = this.pendingPingRequests[i]
if (entry == null) continue
if (entry.seq != seq) continue
if (entry.timer != null) {
clearTimeout(entry.timer as number)
entry.timer = null
}
this.pendingPingRequests.splice(i, 1)
const latency = Math.max(0, Date.now() - entry.startedAt)
try {
const payloadCopy = payload != null ? payload.slice(0) : new Uint8Array(0)
const rawCopy = raw != null ? raw.slice(0) : new Uint8Array(0)
entry.resolve({ ok: true, seq, latencyMs: latency, payload: payloadCopy, raw: rawCopy })
} catch (resolveError) {
console.warn('[ProtocolHandler] ping resolve error', resolveError)
}
return
}
// no pending ping matched — still log for diagnostics
try {
console.warn('[ProtocolHandler] ping notification with no pending request', { seq })
} catch (e) { }
}
removePendingPing(entry : PendingPingRequest | null) : void {
if (entry == null) return
if (entry.timer != null) {
clearTimeout(entry.timer as number)
entry.timer = null
}
const idx = this.pendingPingRequests.indexOf(entry)
if (idx != -1) {
this.pendingPingRequests.splice(idx, 1)
}
}
rejectPendingPings(reason : string) : void {
const err = new Error(reason)
for (let i = 0; i < this.pendingPingRequests.length; i++) {
const entry = this.pendingPingRequests[i]
if (entry == null) continue
if (entry.timer != null) {
clearTimeout(entry.timer!!)
entry.timer = null
}
try {
entry.reject(err)
} catch (rejectError) {
console.warn('[ProtocolHandler] ping reject error', rejectError)
}
}
this.pendingPingRequests = []
}
async unsubscribeHealthNotifications() {
if (this.deviceId == null) return
if (this.bluetoothService == null) return
const dev = '' + this.deviceId
const bsvc = this.bluetoothService
for (let i = 0; i < this.healthSubscriptions.length; i++) {
const s = this.healthSubscriptions[i]
try { await bsvc.unsubscribeCharacteristic(dev, s.serviceUuid, s.charUuid) } catch (e) { console.warn('[ProtocolHandler] unsubscribe failed', s, e) }
}
this.healthSubscriptions = []
this.rejectPendingPings('notifications cancelled')
}
setConnectionParameters(deviceId : string, serviceId : string, writeCharId : string, notifyCharId : string) {
const previousDevice = this.deviceId
if (previousDevice != null && previousDevice != deviceId) {
this.rejectPendingPings('device changed')
}
this.deviceId = deviceId
this.serviceId = serviceId
this.writeCharId = writeCharId
this.notifyCharId = notifyCharId
const hasService = serviceId != null && serviceId != ''
const hasWrite = writeCharId != null && writeCharId != ''
const hasNotify = notifyCharId != null && notifyCharId != ''
this.controlResolved = hasService && hasWrite && hasNotify
if (!this.controlResolved) {
this.pingSequence = 1
}
}
nextPingSequence() : number {
const seq = this.pingSequence & 0xff
this.pingSequence = (this.pingSequence + 1) & 0xff
if (this.pingSequence == 0) this.pingSequence = 1
return seq == 0 ? 1 : seq
}
buildVendorPacket(cmd : number, seq : number, data ?: number[]) : Uint8Array {
const payload = data != null ? data : [] as number[]
const len = 2 + payload.length
const arr : number[] = []
arr.push(len)
arr.push(cmd & 0xff)
arr.push(seq & 0xff)
for (let i = 0; i < payload.length; i++) arr.push(payload[i] & 0xff)
let crc = 0
for (let i = 0; i < arr.length; i++) crc ^= (arr[i] & 0xff)
const pkt = new Uint8Array(arr.length + 2)
pkt[0] = 0xAA
for (let i = 0; i < arr.length; i++) pkt[i + 1] = arr[i] & 0xff
pkt[pkt.length - 1] = crc & 0xff
return pkt
}
async prepareControlChannel(force : boolean = false) : Promise<AutoBleInterfaces | null> {
return await this.ensureControlChannel(force)
}
async ensureControlChannel(force : boolean = false) : Promise<AutoBleInterfaces | null> {
const deviceIdLocal = this.deviceId
const bsvc = this.bluetoothService
if (deviceIdLocal == null || deviceIdLocal == '') return null
if (bsvc == null) throw new Error('bluetoothService not set')
if (!force) {
const hasService = this.serviceId != null && this.serviceId != ''
const hasWrite = this.writeCharId != null && this.writeCharId != ''
const hasNotify = this.notifyCharId != null && this.notifyCharId != ''
if (hasService && hasWrite && hasNotify) {
return { serviceId: this.serviceId ?? '', writeCharId: this.writeCharId ?? '', notifyCharId: this.notifyCharId ?? '' }
}
}
let resolved : AutoBleInterfaces | null = null
try {
const candidate = await bsvc.getAutoBleInterfaces(deviceIdLocal)
if (candidate != null && candidate.serviceId != null && candidate.serviceId != '' && candidate.writeCharId != null && candidate.writeCharId != '' && candidate.notifyCharId != null && candidate.notifyCharId != '') {
resolved = candidate
}
} catch (autoErr) {
console.warn('[ProtocolHandler] getAutoBleInterfaces failed', autoErr)
}
if (resolved == null) {
resolved = await this.findVendorControlChannel(deviceIdLocal)
}
if (resolved != null) {
this.serviceId = '' + resolved.serviceId
this.writeCharId = '' + resolved.writeCharId
this.notifyCharId = '' + resolved.notifyCharId
this.controlResolved = true
return { serviceId: this.serviceId ?? '', writeCharId: this.writeCharId ?? '', notifyCharId: this.notifyCharId ?? '' }
}
return null
}
async findVendorControlChannel(deviceId : string) : Promise<AutoBleInterfaces | null> {
const bsvc = this.bluetoothService
if (bsvc == null) return null
try {
const services = await bsvc.getServices(deviceId)
if (services == null || services.length == 0) return null
const UART_SERVICE = '6e400001-b5a3-f393-e0a9-e50e24dcca9e'
const UART_TX = '6e400002-b5a3-f393-e0a9-e50e24dcca9e'
const UART_RX = '6e400003-b5a3-f393-e0a9-e50e24dcca9e'
const UART_CTRL = '6e400004-b5a3-f393-e0a9-e50e24dcca9e'
let chosenService = ''
for (let i = 0; i < services.length; i++) {
const svc = services[i]
if (svc == null || svc.uuid == null) continue
const su = ('' + svc.uuid).toLowerCase()
if (su.indexOf('6e400001') != -1 || su == UART_SERVICE) {
chosenService = '' + svc.uuid
break
}
}
if (chosenService == '') {
const fallbackSvc = services[0]
if (fallbackSvc != null && fallbackSvc.uuid != null) chosenService = '' + fallbackSvc.uuid
}
if (chosenService == '') return null
const chars = await bsvc.getCharacteristics(deviceId, chosenService)
if (chars == null || chars.length == 0) return null
let ctrlCandidate = ''
let txCandidate = ''
let notifyCandidate = ''
let fallbackWrite = ''
let fallbackNotify = ''
for (let i = 0; i < chars.length; i++) {
const c = chars[i]
if (c == null || c.uuid == null) continue
const idLower = ('' + c.uuid).toLowerCase()
if (idLower == UART_CTRL) ctrlCandidate = '' + c.uuid
if (idLower == UART_TX) txCandidate = '' + c.uuid
if (idLower == UART_RX) notifyCandidate = '' + c.uuid
if (fallbackWrite == '' && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse == true)) fallbackWrite = '' + c.uuid
if (fallbackNotify == '' && c.properties != null && (c.properties.notify || c.properties.indicate)) fallbackNotify = '' + c.uuid
}
let writeChar = ctrlCandidate != '' ? ctrlCandidate : txCandidate
if (writeChar == '' && fallbackWrite != '') writeChar = fallbackWrite
let notifyChar = notifyCandidate != '' ? notifyCandidate : fallbackNotify
if (writeChar == '' || notifyChar == '') return null
return { serviceId: chosenService, writeCharId: writeChar, notifyCharId: notifyChar }
} catch (err) {
console.warn('[ProtocolHandler] findVendorControlChannel failed', err)
}
return null
}
async sendPing(timeoutMs : number = 3000) : Promise<PingResult> {
if (timeoutMs <= 0) timeoutMs = 3000
const deviceIdLocal = this.deviceId
const bsvc = this.bluetoothService
if (deviceIdLocal == null || deviceIdLocal == '') throw new Error('deviceId not set')
if (bsvc == null) throw new Error('bluetoothService not set')
const channel = await this.ensureControlChannel(false)
if (channel == null || channel.serviceId == null || channel.serviceId == '' || channel.writeCharId == null || channel.writeCharId == '') {
throw new Error('control channel unavailable')
}
const seq = this.nextPingSequence()
const frame = this.buildVendorPacket(0x00, seq, [0x00])
const startedAt = Date.now()
const self = this
return await new Promise<PingResult>((resolve, reject) => {
const entry : PendingPingRequest = { seq, resolve, reject, startedAt, timer: null }
const timeoutHandle = setTimeout(() => {
self.removePendingPing(entry)
reject(new Error('ping timeout'))
}, timeoutMs)
entry.timer = timeoutHandle as number
self.pendingPingRequests.push(entry)
const serviceUuid = channel.serviceId
const writeUuid = channel.writeCharId
;(async () => {
try {
const ok = await bsvc.writeCharacteristic(deviceIdLocal, serviceUuid, writeUuid, frame, { waitForResponse: true })
if (!ok) {
self.removePendingPing(entry)
reject(new Error('ping write failed'))
}
} catch (err) {
self.removePendingPing(entry)
reject(err)
}
})()
})
}
// initialize: optional setup, returns a Promise that resolves when ready
async initialize() : Promise<void> {
// Simple async initializer — keep implementation minimal and generator-friendly.
try {
// If bluetoothService exposes any protocol-specific setup, call it here.
this.initialized = true
return
} catch (e) {
throw e
}
}
// Protocol lifecycle / operations — default no-ops so generated code has
// concrete member references and platform-specific handlers can override.
async scanDevices(options ?: ScanDevicesOptions) : Promise<void> { return; }
async connect(device : BleDevice, options ?: BleConnectOptionsExt) : Promise<void> { return; }
async disconnect(device : BleDevice) : Promise<void> { return; }
async sendData(device : BleDevice, payload ?: SendDataPayload, options ?: BleOptions) : Promise<void> { return; }
async autoConnect(device : BleDevice, options ?: BleConnectOptionsExt) : Promise<AutoBleInterfaces> { return { serviceId: '', writeCharId: '', notifyCharId: '' }; }
// Example: testBatteryLevel will attempt to read the battery characteristic
// if write/notify-based protocol is not available. Returns number percentage.
async testBatteryLevel() : Promise<number> {
if (this.deviceId == null) throw new Error('deviceId not set')
// copy to local so Kotlin generator can smart-cast the value across awaits
const deviceId = this.deviceId
// try reading standard Battery characteristic (180F -> 2A19)
if (this.bluetoothService == null) throw new Error('bluetoothService not set')
const services = await this.bluetoothService.getServices(deviceId)
let found : BleService | null = null
for (let i = 0; i < services.length; i++) {
const s = services[i]
const uuidCandidate : string | null = (s != null && s.uuid != null ? s.uuid : null)
const uuid = uuidCandidate != null ? ('' + uuidCandidate).toLowerCase() : ''
if (uuid.indexOf('180f') != -1) { found = s; break }
}
if (found == null) {
// fallback: if writeCharId exists and notify available use protocol (not implemented)
return -1
}
const foundUuid = found!.uuid
const charsRaw = await this.bluetoothService.getCharacteristics(deviceId, foundUuid)
const chars : BleCharacteristic[] = charsRaw
const batChar = chars.find((c : BleCharacteristic) => ((c.properties != null && c.properties.read) || (c.uuid != null && ('' + c.uuid).toLowerCase().includes('2a19'))))
if (batChar == null) return -1
const buf = await this.bluetoothService.readCharacteristic(deviceId, foundUuid, batChar.uuid)
const arr = new Uint8Array(buf)
try {
const hex = Array.from(arr).map(b => b.toString(16).padStart(2, '0')).join(' ')
console.log('[ProtocolHandler] testBatteryLevel raw hex:', hex, 'len=', arr.length)
} catch (e) { }
if (arr.length > 0) {
console.log('[ProtocolHandler] testBatteryLevel parsed battery:', arr[0])
return arr[0]
}
return -1
}
// Best-effort time synchronization on connect.
// Try writing standard Current Time Service (CTS: 0x1805 / char 0x2A2B).
// Keep H5-compatible: return Promise<string> with 'timeSynced' or 'timeFailed' or ''.
async synchronizeOnConnect() : Promise<string> {
const deviceId = this.deviceId
const bsvc = this.bluetoothService
if (deviceId == null || bsvc == null) return Promise.resolve('')
const dev = '' + deviceId
try {
console.log('[ProtocolHandler] synchronizeOnConnect: attempting CTS time write')
const svcs = await bsvc.getServices(dev)
for (let i = 0; i < svcs.length; i++) {
const s = svcs[i]
if (s == null || s.uuid == null) continue
const su = ('' + s.uuid).toLowerCase()
if (su.indexOf('1805') == -1 && su.indexOf('current') == -1) continue
const svcUuid = '' + s.uuid
try {
const chars = await bsvc.getCharacteristics(dev, svcUuid)
for (let j = 0; j < chars.length; j++) {
const c = chars[j]
if (c == null || c.uuid == null) continue
const id = ('' + c.uuid).toLowerCase()
if (id.indexOf('2a2b') != -1) {
const now = new Date()
const year = now.getFullYear()
const month = now.getMonth() + 1
const day = now.getDate()
const hours = now.getHours()
const minutes = now.getMinutes()
const seconds = now.getSeconds()
const jsDay = now.getDay()
const dayOfWeek = jsDay == 0 ? 7 : jsDay
const payload = new Uint8Array(10)
payload[0] = year & 0xff
payload[1] = (year >> 8) & 0xff
payload[2] = month
payload[3] = day
payload[4] = hours
payload[5] = minutes
payload[6] = seconds
payload[7] = dayOfWeek
payload[8] = 0 // fractions256
payload[9] = 1 // adjust reason: manual update
try {
await bsvc.writeCharacteristic(dev, svcUuid, c.uuid, payload, { waitForResponse: true })
console.log('[ProtocolHandler] CTS time write succeeded')
return 'timeSynced'
} catch (e) {
console.warn('[ProtocolHandler] CTS write failed', e)
// try other chars/services
}
}
}
} catch (e) {
console.warn('[ProtocolHandler] error enumerating CTS chars', e)
}
}
console.log('[ProtocolHandler] CTS not available or writes failed')
return 'timeFailed'
} catch (e) {
console.warn('[ProtocolHandler] synchronizeOnConnect unexpected error', e)
return ''
}
}
// testVersionInfo: try to read Device Information characteristics or return empty
async testVersionInfo(hw : boolean) : Promise<string> {
// copy to local so Kotlin generator can smart-cast the value across awaits
console.log('testVersionInfo:',hw,this.deviceId,this.bluetoothService)
const deviceId = this.deviceId
if (deviceId == null) return ''
// Device Information service 180A, characteristics: 2A26 (SW), 2A27 (HW) sometimes
if (this.bluetoothService == null) return ''
// Add delay to allow service discovery to complete on Android reconnection
await new Promise<void>((resolve) => { setTimeout(() => resolve(), 1000) })
const _services = await this.bluetoothService.getServices(deviceId)
const services2 : BleService[] = _services
console.log('[ProtocolHandler] services2 length:', services2.length)
for (let i = 0; i < services2.length; i++) {
const s = services2[i]
console.log('[ProtocolHandler] service', i, 'uuid:', s?.uuid)
}
let found2 : BleService | null = null
for (let i = 0; i < services2.length; i++) {
const s = services2[i]
const uuidCandidate : string | null = (s != null && s.uuid != null ? s.uuid : null)
const uuid = uuidCandidate != null ? ('' + uuidCandidate).toLowerCase() : ''
if (uuid.indexOf('180a') != -1) { found2 = s; break }
}
console.log('testVersionInfo 1:',hw)
// If Device Information service exists, locate candidate characteristics.
// Do NOT return early if the service is missing — fallbacks (including UART) exist below.
let found2Uuid : string | null = null
let chars : BleCharacteristic[] = []
let target : BleCharacteristic | null = null
if (found2 != null) {
console.log('testVersionInfo 2:',found2)
const found2NonNull = found2 as BleService
found2Uuid = '' + found2NonNull.uuid
try {
console.log('testVersionInfo 2:',found2)
const _chars = await this.bluetoothService.getCharacteristics(deviceId, found2Uuid)
console.log('ak _chars:', _chars)
chars = _chars as BleCharacteristic[]
target = chars.find((c) => {
const id = ('' + c.uuid).toLowerCase()
console.log('[ProtocolHandler] checking char uuid:', c.uuid, 'id:', id, 'hw:', hw)
if (hw) {
const match = id.includes('2a27') || id.includes('hardware')
console.log('[ProtocolHandler] hw match:', match)
return match
}
const match1 = id.includes('2a26') || id.includes('firmware')
const match2 = id.includes('2a28') || id.includes('software')
console.log('[ProtocolHandler] sw match1:', match1, 'match2:', match2)
return match1 || match2
}) as BleCharacteristic | null
} catch (e) {
console.warn('[ProtocolHandler] failed to get DeviceInfo chars', e)
chars = []
target = null
}
}
// debug: log which characteristic was selected and its uuid (may be null)
console.log('[ProtocolHandler] testVersionInfo selected characteristic:', target != null ? target.uuid : null, 'hw=', hw)
// If we found the Device Information service, log available characteristics to help
// diagnose Android path where IDs or properties may differ from Web shapes.
if (found2Uuid != null) {
try {
console.log('[ProtocolHandler] DeviceInfo service UUID=', found2Uuid, 'chars.length=', chars.length)
for (let i = 0; i < chars.length; i++) {
const c = chars[i]
try {
console.log('[ProtocolHandler] char:', i, 'uuid=', c.uuid, 'props=', c.properties)
} catch (e) { }
}
} catch (e) { }
}
// alias bluetoothService to local variable for inner functions
const bsvc = this.bluetoothService
// local non-null device id for inner functions (we already guard above)
const dev = deviceId as string
// helper: treat short / non-printable results as invalid version strings
function isPrintableVersion(arr : Uint8Array) : boolean {
if (arr == null || arr.length == 0) return false
// reject obvious single-byte numeric measurements (battery etc.)
if (arr.length == 1) return false
let printable = 0
for (let i = 0; i < arr.length; i++) {
const b = arr[i]
if (b >= 0x20 && b <= 0x7e) printable++
}
// accept if at least 50% printable characters
if ((printable / arr.length) >= 0.5) return true
// also accept common version-like ASCII (e.g. "1.0.3", "v2.1") even if ratio is low
try {
const s = new TextDecoder().decode(arr).trim()
if (s.length >= 2 && /^[vV]?\d[\d\.\- ]+$/.test(s)) return true
} catch (e) { /* ignore decode errors */ }
return false
}
async function tryReadChar(serviceUuid : string, charUuid : string) : Promise<string> {
await new Promise<void>((resolve) => { setTimeout(() => resolve(), 1000) })
try {
// avoid accidentally reading battery characteristic
const low = ('' + charUuid).toLowerCase()
if (low.indexOf('2a19') != -1) {
console.log('[ProtocolHandler] skipping battery char 2A19')
return ''
}
const buf = await bsvc!.readCharacteristic(dev, serviceUuid, charUuid)
// Prefer strong typing: treat buf as ArrayBuffer/Uint8Array primarily.
try { console.log('[ProtocolHandler] raw readCharacteristic buf typeof=', typeof buf) } catch (e) { }
let arr : Uint8Array = new Uint8Array(0)
try {
// Primary, strongly-typed paths
if (typeof ArrayBuffer !== 'undefined' && buf instanceof ArrayBuffer) {
arr = new Uint8Array(buf as ArrayBuffer)
} else if (Array.isArray(buf)) {
arr = new Uint8Array(buf as number[])
} else {
// leave arr empty for downstream diagnostics
console.log('ak:',arr)
arr = new Uint8Array(0)
}
} catch (e) { arr = new Uint8Array(0) }
try { console.log('[ProtocolHandler] normalized read buffer len=', arr.length, 'svc=', serviceUuid, 'char=', charUuid) } catch (e) { }
// If nothing was read, dump wrapper diagnostics to help identify platform shapes
if (arr.length == 0) {
console.log(arr)
}
try {
const hex = Array.from(arr).map(b => b.toString(16).padStart(2, '0')).join(' ')
console.log('[ProtocolHandler] read buffer raw hex:', hex, 'len=', arr.length, 'svc=', serviceUuid, 'char=', charUuid)
} catch (e) { console.warn('[ProtocolHandler] failed to stringify buffer', e) }
if (isPrintableVersion(arr)) {
return new TextDecoder().decode(arr)
}
// Fallback: sometimes version strings are short (e.g. "1.0" or "v2.1")
// and may fail the printable-ratio heuristic. Try decoding and match
// a version-like regexp before giving up.
try {
const dec = new TextDecoder().decode(arr).trim()
if (dec.length >= 2 && /^[vV]?\d[\d\.\- ]+$/.test(dec)) {
console.log('[ProtocolHandler] tryReadChar regex-fallback decoded:', dec, 'svc=', serviceUuid, 'char=', charUuid)
return dec
}
} catch (e) { /* ignore */ }
return ''
} catch (e) {
console.warn('[ProtocolHandler] read failed for', serviceUuid, charUuid, e)
return ''
}
}
// First attempt: read the initially selected target characteristic (only if service UUID known)
let result = ''
if (target != null && found2Uuid != null) {
result = await tryReadChar(found2Uuid, target.uuid)
if (result != null && result.length > 0) return result
}
// If the direct target failed or none selected, attempt a conservative read of
// the first few characteristics in the Device Information service (Android
// devices sometimes put version strings in non-standard characteristics).
if ((target == null || (result == null || result.length == 0)) && found2Uuid != null && chars.length > 0) {
try {
const tryCount = Math.min(4, chars.length)
for (let i = 0; i < tryCount; i++) {
const c = chars[i]
if (c == null || c.uuid == null) continue
try {
console.log('[ProtocolHandler] device-info conservative read attempt for char', c.uuid)
const attempt = await tryReadChar(found2Uuid!, c.uuid)
if (attempt != null && attempt.length > 0) return attempt
} catch (e) { console.warn('[ProtocolHandler] conservative read failed for', c.uuid, e) }
}
} catch (e) { console.warn('[ProtocolHandler] conservative device-info read loop failed', e) }
}
// Second attempt: search other readable characteristics in the same Device Information service
try {
for (let i = 0; i < chars.length; i++) {
const c = chars[i]
if (c == null) continue
const id = ('' + c.uuid).toLowerCase()
if (target != null && id == ('' + target.uuid).toLowerCase()) continue
// prefer readable properties when available
if (c.properties != null && (c.properties.read == true || c.properties.canRead == true)) {
const attempt = await tryReadChar(found2Uuid!, c.uuid)
if (attempt != null && attempt.length > 0) return attempt
}
}
} catch (e) { console.warn('[ProtocolHandler] fallback scan in service failed', e) }
// Final fallback: scan all services for likely version characteristics (conservative limit)
try {
const svcList = await bsvc!.getServices(dev)
let attempts = 0
for (let si = 0; si < svcList.length; si++) {
if (attempts >= 6) break // limit to avoid long blocking
const s = svcList[si]
if (s == null) continue
const sUuid = ('' + s.uuid)
const charsAll = await bsvc!.getCharacteristics(dev, sUuid)
for (let ci = 0; ci < charsAll.length; ci++) {
const c = charsAll[ci]
if (c == null) continue
const id = ('' + c.uuid).toLowerCase()
if (id.indexOf('2a26') != -1 || id.indexOf('2a27') != -1 || id.indexOf('2a28') != -1 || ('' + c.uuid).toLowerCase().includes('firmware') || ('' + c.uuid).toLowerCase().includes('software') || ('' + c.uuid).toLowerCase().includes('hardware')) {
const attempt = await tryReadChar(sUuid, c.uuid)
if (attempt != null && attempt.length > 0) return attempt
}
}
}
} catch (e) { console.warn('[ProtocolHandler] global fallback scan failed', e) }
// Final final fallback: aggressively (but with a strict cap) try any readable characteristic
try {
const svcList2 = await bsvc!.getServices(dev)
let attempts2 = 0
for (let si = 0; si < svcList2.length; si++) {
if (attempts2 >= 8) break
const s = svcList2[si]
if (s == null) continue
const sUuid = ('' + s.uuid)
const charsAll = await bsvc!.getCharacteristics(dev, sUuid)
for (let ci = 0; ci < charsAll.length; ci++) {
if (attempts2 >= 8) break
const c = charsAll[ci]
if (c == null) continue
const id = ('' + c.uuid).toLowerCase()
if (id.indexOf('2a19') != -1) continue // skip battery
// prefer readable properties if available, otherwise try cautiously
if (c.properties != null && (c.properties.read == true || c.properties.canRead == true)) {
attempts2++
const attempt = await tryReadChar(sUuid, c.uuid)
if (attempt != null && attempt.length > 0) return attempt
}
}
}
} catch (e) { console.warn('[ProtocolHandler] aggressive fallback failed', e) }
// if all attempts failed, return empty string
// Vendor-specific fallback: use custom UART-like service to request device info
try {
// Known custom primary service and chars
const UART_SERVICE = '6e400001-b5a3-f393-e0a9-e50e24dcca9e'
const UART_TX = '6e400002-b5a3-f393-e0a9-e50e24dcca9e' // write without response
const UART_RX = '6e400003-b5a3-f393-e0a9-e50e24dcca9e' // notify/read
const UART_CTRL = '6e400004-b5a3-f393-e0a9-e50e24dcca9e' // control write (with response)
// find the UART service
const all = await bsvc!.getServices(deviceId)
let uartSvc : BleService | null = null
for (let i = 0; i < all.length; i++) {
const s = all[i]
if (s != null && (('' + s.uuid).toLowerCase().indexOf('6e400001') != -1 || ('' + s.uuid).toLowerCase() == UART_SERVICE)) { uartSvc = s; break }
}
const nonNullUartSvc = uartSvc
if (nonNullUartSvc != null) {
const sUuid = '' + nonNullUartSvc.uuid
// identify write and notify chars (prefer CTRL then TX for write)
let writeChar = UART_CTRL
let notifyChar = UART_RX
// verify existence
const chars = await bsvc!.getCharacteristics(deviceId, sUuid)
const charSet = new Set(chars.map(c => ('' + c.uuid).toLowerCase()))
if (!charSet.has(UART_CTRL) && !charSet.has(UART_TX)) {
// no usable write char
console.log('[ProtocolHandler] UART service present but no write char found')
} else {
if (!charSet.has(UART_CTRL)) writeChar = UART_TX
if (!charSet.has(UART_RX)) notifyChar = Array.from(charSet)[0] as string // pick first as fallback
const self = this
// subscribe to notify and wait for response
let resolved = false
const notifyPromise = new Promise<Uint8Array | null>((resolve : (value : Uint8Array | null) => void, reject : (reason ?: any) => void) => {
const timeout = setTimeout(() => {
if (!resolved) { resolved = true; resolve(null) }
}, 3000)
const callback = (data : Uint8Array) => {
if (resolved) return
resolved = true
clearTimeout(timeout)
resolve(data)
}
bsvc!.subscribeCharacteristic(deviceId, sUuid, notifyChar, callback).then(() => {
// subscription succeeded
}).catch((e) => {
clearTimeout(timeout)
if (!resolved) { resolved = true; resolve(null) }
})
})
// write the device info request
const pkt = self.buildVendorPacket(0x01, 0x01, [0x00]) // CMD_DEVICE_INFO
try {
await bsvc!.writeCharacteristic(deviceId, sUuid, writeChar, pkt, { waitForResponse: true })
} catch (e) { console.warn('[ProtocolHandler] UART write failed', e) }
const notifyData = await notifyPromise
try {
await bsvc!.unsubscribeCharacteristic(deviceId, sUuid, notifyChar)
} catch (e) { }
if (notifyData != null) {
// parse response: expect packet starting with 0xAA
if (notifyData.length >= 4 && notifyData[0] == 0xAA) {
// strip STX,LEN,CMD,SEQ and CRC
const len = notifyData[1]
const cmd = notifyData[2]
const seq = notifyData[3]
const dataBytes = notifyData.slice(4, notifyData.length - 1)
// try decode data to string
try {
// Use TextDecoder which handles Uint8Array directly and is
// safer than spreading into String.fromCharCode for large arrays.
const decoded = new TextDecoder().decode(dataBytes)
if (decoded != null && decoded.length > 0 && decoded.trim().length > 0) {
console.log('[ProtocolHandler] UART notify decoded:', decoded)
return decoded
}
} catch (e) { console.warn('[ProtocolHandler] failed to decode UART notify', e) }
}
}
}
}
} catch (e) { console.warn('[ProtocolHandler] UART fallback failed', e) }
return ''
}
}

View File

@@ -0,0 +1,34 @@
// Minimal error definitions used across the BLE module.
// Keep this file small and avoid runtime dependencies; it's mainly for typing and
// simple runtime error construction used by native platform code.
export enum AkBluetoothErrorCode {
UnknownError = 0,
DeviceNotFound = 1,
ServiceNotFound = 2,
CharacteristicNotFound = 3,
ConnectionTimeout = 4,
Unspecified = 99
}
export class AkBleErrorImpl extends Error {
public code: AkBluetoothErrorCode;
public detail: any|null;
constructor(code: AkBluetoothErrorCode, message?: string, detail: any|null = null) {
super(message ?? AkBleErrorImpl.defaultMessage(code));
this.name = 'AkBleError';
this.code = code;
this.detail = detail;
}
static defaultMessage(code: AkBluetoothErrorCode) {
switch (code) {
case AkBluetoothErrorCode.DeviceNotFound: return 'Device not found';
case AkBluetoothErrorCode.ServiceNotFound: return 'Service not found';
case AkBluetoothErrorCode.CharacteristicNotFound: return 'Characteristic not found';
case AkBluetoothErrorCode.ConnectionTimeout: return 'Connection timed out';
case AkBluetoothErrorCode.UnknownError: default: return 'Unknown Bluetooth error';
}
}
}
export default AkBleErrorImpl;

View File

@@ -0,0 +1,139 @@
// H5平台 Web Bluetooth 设备扫描实现
import { DeviceManager } from './device-manager.uts';
import { ServiceManager } from './service-manager.uts';
import type { BleDevice, BleOptions, BleConnectOptionsExt, BleDataReceivedCallback, BleConnectionStateChangeCallback } from '../interface.uts'
const DEFAULT_OPTIONAL_SERVICES = [
'00001800-0000-1000-8000-00805f9b34fb', // GAP
'0000180a-0000-1000-8000-00805f9b34fb', // Device Information
'0000180f-0000-1000-8000-00805f9b34fb', // Battery
'00001812-0000-1000-8000-00805f9b34fb', // Human Interface Device
'0000fe59-0000-1000-8000-00805f9b34fb', // Nordic DFU / vendor specific
'6e400001-b5a3-f393-e0a9-e50e24dcca9e', // Nordic UART primary service
'6e400010-b5a3-f393-e0a9-e50e24dcca9e', // Custom health service (observed on Android)
'6e400020-b5a3-f393-e0a9-e50e24dcca9e' // Additional vendor service
];
export const BLE_SERVICE_PREFIXES = [
'6e4000',
'0000180f',
'00001812',
'0000fe59'
];
function normalizeServiceUuid(uuid: string): string {
if (!uuid) return uuid;
let u = uuid.trim().toLowerCase();
if (u.startsWith('0x')) {
u = u.slice(2);
}
if (/^[0-9a-f]{4}$/.test(u)) {
return `0000${u}-0000-1000-8000-00805f9b34fb`;
}
return u;
}
function mergeOptionalServices(userServices?: string[]): string[] {
const set = new Set<string>();
DEFAULT_OPTIONAL_SERVICES.forEach((svc) => set.add(normalizeServiceUuid(svc)));
if (userServices != null) {
for (let i = 0; i < userServices.length; i++) {
const normalized = normalizeServiceUuid(userServices[i]);
if (normalized != null && normalized !== '') {
set.add(normalized);
}
}
}
return Array.from(set);
}
// 实例化各个管理器
const deviceManager = new DeviceManager();
const serviceManager = new ServiceManager();
// 导出简化接口
export const scanDevices = async (options?: { optionalServices?: string[] }) => {
const mergedOptions = options != null ? { ...options } : {};
mergedOptions.optionalServices = mergeOptionalServices(options?.optionalServices ?? []);
return deviceManager.startScan(mergedOptions);
};
export const connectDevice = async (deviceId: string, options?: BleConnectOptionsExt) => deviceManager.connectDevice(deviceId, options);
export const disconnectDevice = async (deviceId: string) => deviceManager.disconnectDevice(deviceId);
export const getConnectedDevices = () => deviceManager.getConnectedDevices();
export const getKnownDevices = () => Object.keys((deviceManager as any).devices || {})
export const discoverServices = async (deviceId: string) => {
// 获取 server 实例
const server = deviceManager.getServer(deviceId)
if (!server) throw new Error(`设备未连接: ${deviceId}`)
return serviceManager.discoverServices(deviceId, server);
};
export const getCharacteristics = async (deviceId: string, serviceId: string) => serviceManager.getCharacteristics(deviceId, serviceId);
export const writeCharacteristic = async (deviceId: string, serviceId: string, characteristicId: string, data: string | ArrayBuffer) => serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, data);
export const subscribeCharacteristic = async (deviceId: string, serviceId: string, characteristicId: string, callback) => {
console.log('[bluetooth_manager] subscribeCharacteristic called:', deviceId, serviceId, characteristicId)
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, callback);
}
export const unsubscribeCharacteristic = async (deviceId: string, serviceId: string, characteristicId: string) => serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
export const readCharacteristic = async (deviceId: string, serviceId: string, characteristicId: string) => serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
export const sendCommand = async (deviceId: string, serviceId: string, writeCharId: string, notifyCharId: string, command: string, params: any = null, timeout: number = 5000) => dataProcessor.sendAndReceive(deviceId, serviceId, writeCharId, notifyCharId, command, params, timeout);
// Event adapter helpers: translate DeviceManager callbacks into payload objects
export const onDeviceFound = (listener) => deviceManager.onDeviceFound((device) => {
try { listener({ device }); } catch (e) { /* ignore listener errors */ }
});
export const onScanFinished = (listener) => deviceManager.onScanFinished(() => {
try { listener({}); } catch (e) {}
});
export const onConnectionStateChange = (listener) => deviceManager.onConnectionStateChange((deviceId, state) => {
try { listener({ device: { deviceId }, state }); } catch (e) {}
});
/**
* 自动连接并初始化蓝牙设备获取可用serviceId、writeCharId、notifyCharId
* @param deviceId 设备ID
* @returns {Promise<{serviceId: string, writeCharId: string, notifyCharId: string}>}
*/
export const autoConnect = async (deviceId: string): Promise<{serviceId: string, writeCharId: string, notifyCharId: string}> => {
// 1. 连接设备
await connectDevice(deviceId);
// 2. 服务发现
const services = await discoverServices(deviceId);
if (!services || services.length === 0) throw new Error('未发现服务');
// 3. 获取私有serviceId优先bae前缀或通过dataProcessor模板
let serviceId = '';
for (const s of services) {
if (s.uuid && BLE_SERVICE_PREFIXES.some(prefix => s.uuid.startsWith(prefix))) {
serviceId = s.uuid;
break;
}
}
if (!serviceId) {
// 可扩展通过dataProcessor获取模板serviceId
serviceId = services[0].uuid;
}
// 4. 获取特征值
const characteristics = await getCharacteristics(deviceId, serviceId);
if (!characteristics || characteristics.length === 0) throw new Error('未发现特征值');
// 5. 找到write和notify特征
let writeCharId = '';
let notifyCharId = '';
for (const c of characteristics) {
if (!writeCharId && (c.properties.write || c.properties.writeWithoutResponse)) writeCharId = c.uuid;
if (!notifyCharId && (c.properties.notify || c.properties.indicate)) notifyCharId = c.uuid;
}
if (!writeCharId || !notifyCharId) throw new Error('未找到可用的写/通知特征');
// 6. 注册notification
await subscribeCharacteristic(deviceId, serviceId, notifyCharId, (data) => {
// 可在此处分发/处理notification
// console.log('Notification:', data);
});
// 7. 返回结果
return { serviceId, writeCharId, notifyCharId };
};

View File

@@ -0,0 +1,237 @@
// 设备管理相关:扫描、连接、断开、重连
import { BleDevice, BLE_CONNECTION_STATE } from '../interface.uts';
import type { BleConnectOptionsExt } from '../interface.uts';
export class DeviceManager {
private devices = {};
private servers = {};
private connectionStates = {};
private allowedServices = {};
private reconnectAttempts: number = 0;
private maxReconnectAttempts: number = 5;
private reconnectDelay: number = 2000;
private reconnectTimeoutId: number = 0;
private autoReconnect: boolean = false;
private connectionStateChangeListeners: Function[] = [];
private deviceFoundListeners: ((device: BleDevice) => void)[] = [];
private scanFinishedListeners: (() => void)[] = [];
onDeviceFound(listener: (device: BleDevice) => void) {
this.deviceFoundListeners.push(listener);
}
onScanFinished(listener: () => void) {
this.scanFinishedListeners.push(listener);
}
private emitDeviceFound(device: BleDevice) {
for (const listener of this.deviceFoundListeners) {
try { listener(device); } catch (e) {}
}
}
private emitScanFinished() {
for (const listener of this.scanFinishedListeners) {
try { listener(); } catch (e) {}
}
}
async startScan(options?: { optionalServices?: string[] } ): Promise<void> {
if (!navigator.bluetooth) throw new Error('Web Bluetooth API not supported');
try {
const scanOptions: any = { acceptAllDevices: true };
// allow callers to request optionalServices (required by Web Bluetooth to access custom services)
if (options && Array.isArray(options.optionalServices) && options.optionalServices.length > 0) {
scanOptions.optionalServices = options.optionalServices;
}
// Log the exact options passed to requestDevice for debugging optionalServices propagation
try {
console.log('[DeviceManager] requestDevice options:', JSON.stringify(scanOptions));
} catch (e) {
console.log('[DeviceManager] requestDevice options (raw):', scanOptions);
}
const device = await navigator.bluetooth.requestDevice(scanOptions);
try {
console.log('[DeviceManager] requestDevice result:', device);
} catch (e) {
console.log('[DeviceManager] requestDevice result (raw):', device);
}
if (device) {
console.log(device)
// 格式化 deviceId 为 MAC 地址格式
const formatDeviceId = (id: string): string => {
// 如果是12位16进制字符串如 'AABBCCDDEEFF'),转为 'AA:BB:CC:DD:EE:FF'
if (/^[0-9A-Fa-f]{12}$/.test(id)) {
return id.match(/.{1,2}/g)!.join(":").toUpperCase();
}
// 如果是base64无法直接转MAC保留原样
// 你可以根据实际情况扩展此处
return id;
};
const isConnected = !!this.servers[device.id];
const formattedId = formatDeviceId(device.id);
const bleDevice = { deviceId: formattedId, name: device.name, connected: isConnected };
this.devices[formattedId] = device;
this.emitDeviceFound(bleDevice);
}
this.emitScanFinished();
} catch (e) {
this.emitScanFinished();
throw e;
}
}
onConnectionStateChange(listener: (deviceId: string, state: string) => void) {
this.connectionStateChangeListeners.push(listener);
}
private emitConnectionStateChange(deviceId: string, state: string) {
for (const listener of this.connectionStateChangeListeners) {
try {
listener(deviceId, state);
} catch (e) {
// 忽略单个回调异常
}
}
}
async connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<boolean> {
this.autoReconnect = options?.autoReconnect ?? false;
try {
const key = this.resolveDeviceKey(deviceId)
console.log(key,deviceId)
if (!key) {
// better debugging: include a short sample of known device keys
const known = Object.keys(this.devices || {}).slice(0, 20)
throw new Error(`设备未找到: ${deviceId}; 已知设备: ${known.join(',')}`)
}
const device = this.devices[key]
const server = await device.gatt.connect();
this.servers[key] = server;
this.connectionStates[key] = BLE_CONNECTION_STATE.CONNECTED;
this.reconnectAttempts = 0;
this.emitConnectionStateChange(key, 'connected');
// 监听物理断开
if (device.gatt) {
device.gatt.onconnectionstatechanged = null;
device.gatt.onconnectionstatechanged = () => {
if (!device.gatt.connected) {
this.emitConnectionStateChange(key, 'disconnected');
}
};
}
return true;
} catch (error) {
if (this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
return this.scheduleReconnect(deviceId);
}
throw error;
}
}
async disconnectDevice(deviceId: string): Promise<void> {
const key = this.resolveDeviceKey(deviceId)
if (!key) throw new Error('设备未找到')
const device = this.devices[key]
try {
if (device.gatt && device.gatt.connected) {
device.gatt.disconnect();
}
delete this.servers[key];
this.connectionStates[key] = BLE_CONNECTION_STATE.DISCONNECTED;
this.emitConnectionStateChange(key, 'disconnected');
} catch (e) {
throw e;
}
}
getConnectedDevices(): BleDevice[] {
const connectedDevices: BleDevice[] = [];
for (const deviceId in this.servers) {
const device = this.devices[deviceId];
if (device) {
connectedDevices.push({ deviceId: device.id, name: device.name || '未知设备', connected: true });
}
}
return connectedDevices;
}
handleDisconnect(deviceId: string) {
const key = this.resolveDeviceKey(deviceId)
const idKey = key ?? deviceId
this.connectionStates[idKey] = BLE_CONNECTION_STATE.DISCONNECTED;
this.emitConnectionStateChange(idKey, 'disconnected');
if (this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
this.scheduleReconnect(idKey);
}
}
private scheduleReconnect(deviceId: string): Promise<boolean> {
this.reconnectAttempts++;
return new Promise((resolve, reject) => {
this.reconnectTimeoutId = setTimeout(() => {
this.connectDevice(deviceId, { autoReconnect: true })
.then(resolve)
.catch(reject);
}, this.reconnectDelay);
});
}
// Resolve a device key used in this.devices map from a given deviceId.
// Accepts formatted IDs (with ':'), raw ids or case-insensitive hex strings.
private resolveDeviceKey(deviceId: string): string | null {
// Accept either a string id or an object that contains the id (UTSJSONObject or plain object)
if (deviceId == null) return null
let idCandidate: any = deviceId
if (typeof deviceId !== 'string') {
try {
// UTSJSONObject has getString
if (typeof (deviceId as any).getString === 'function') {
const got = (deviceId as any).getString('deviceId') || (deviceId as any).getString('device_id') || (deviceId as any).getString('id')
if (got) idCandidate = got
} else if (typeof deviceId === 'object') {
const got = (deviceId as any).deviceId || (deviceId as any).device_id || (deviceId as any).id
if (got) idCandidate = got
}
} catch (e) { /* ignore extraction errors */ }
}
if (!idCandidate) return null
if (this.devices[idCandidate]) return idCandidate
const normalize = (s: string) => (s || '').toString().replace(/:/g, '').toUpperCase()
const target = normalize(idCandidate)
for (const k in this.devices) {
if (k === deviceId) return k
const dev = this.devices[k]
try {
if (dev && dev.id && normalize(dev.id) === target) return k
} catch (e) { }
if (normalize(k) === target) return k
}
return null
}
cancelReconnect() {
if (this.reconnectTimeoutId) {
clearTimeout(this.reconnectTimeoutId);
this.reconnectTimeoutId = 0;
}
this.autoReconnect = false;
this.reconnectAttempts = 0;
}
setMaxReconnectAttempts(attempts: number) {
this.maxReconnectAttempts = attempts;
}
setReconnectDelay(delay: number) {
this.reconnectDelay = delay;
}
isDeviceConnected(deviceId: string): boolean {
return !!this.servers[deviceId];
}
// Public helper to obtain the GATT server for a device id using flexible matching
getServer(deviceId: string): any | null {
const key = this.resolveDeviceKey(deviceId)
if (!key) return null
return this.servers[key] ?? null
}
}

View File

@@ -0,0 +1,136 @@
import * as BluetoothManager from './bluetooth_manager.uts'
// 默认 Nordic DFU UUIDs (web 模式也可用,如设备使用自定义请传入 options)
const DFU_SERVICE_UUID = '00001530-1212-EFDE-1523-785FEABCD123'
const DFU_CONTROL_POINT_UUID = '00001531-1212-EFDE-1523-785FEABCD123'
const DFU_PACKET_UUID = '00001532-1212-EFDE-1523-785FEABCD123'
export class WebDfuManager {
// startDfu: deviceId, firmwareBytes (Uint8Array), options
// options: { serviceId?, writeCharId?, notifyCharId?, chunkSize?, onProgress?, onLog?, useNordic?, controlParser?, controlTimeout? }
async startDfu(deviceId: string, firmwareBytes: Uint8Array, options?: any): Promise<void> {
options = options || {};
// 1. ensure connected and discover services
let svcInfo;
if (options.serviceId && options.writeCharId && options.notifyCharId) {
svcInfo = { serviceId: options.serviceId, writeCharId: options.writeCharId, notifyCharId: options.notifyCharId };
} else {
svcInfo = await BluetoothManager.autoConnect(deviceId);
}
const serviceId = svcInfo.serviceId;
const writeCharId = svcInfo.writeCharId;
const notifyCharId = svcInfo.notifyCharId;
const chunkSize = options.chunkSize ?? 20;
// control parser
const controlParser = options.controlParser ?? (options.useNordic ? this._nordicControlParser.bind(this) : this._defaultControlParser.bind(this));
// subscribe notifications on control/notify char
let finalizeSub;
let resolved = false;
const promise = new Promise<void>(async (resolve, reject) => {
const cb = (payload) => {
try {
const data = payload.data instanceof Uint8Array ? payload.data : new Uint8Array(payload.data);
options.onLog?.('control notify: ' + Array.from(data).join(','));
const parsed = controlParser(data);
if (!parsed) return;
if (parsed.type === 'progress' && parsed.progress != null) {
if (options.useNordic && svcInfo && svcInfo.totalBytes) {
const percent = Math.floor((parsed.progress / svcInfo.totalBytes) * 100);
options.onProgress?.(percent);
} else {
options.onProgress?.(parsed.progress);
}
} else if (parsed.type === 'success') {
resolved = true;
resolve();
} else if (parsed.type === 'error') {
reject(parsed.error ?? new Error('DFU device error'));
}
} catch (e) {
options.onLog?.('control handler error: ' + e);
}
};
await BluetoothManager.subscribeCharacteristic(deviceId, serviceId, notifyCharId, cb);
finalizeSub = async () => { try { await BluetoothManager.subscribeCharacteristic(deviceId, serviceId, notifyCharId, () => {}); } catch(e){} };
// write firmware in chunks
try {
let offset = 0;
const total = firmwareBytes.length;
// attach totalBytes for nordic if needed
svcInfo.totalBytes = total;
while (offset < total) {
const end = Math.min(offset + chunkSize, total);
const slice = firmwareBytes.subarray(offset, end);
// writeValue accepts ArrayBuffer
await BluetoothManager.writeCharacteristic(deviceId, serviceId, writeCharId, slice.buffer);
offset = end;
// optimistic progress
options.onProgress?.(Math.floor((offset / total) * 100));
await this._sleep(options.chunkDelay ?? 6);
}
// send validate/activate command to control point (placeholder)
try {
await BluetoothManager.writeCharacteristic(deviceId, serviceId, writeCharId, new Uint8Array([0x04]).buffer);
} catch (e) {
// ignore
}
// wait for control success or timeout
const timeoutMs = options.controlTimeout ?? 20000;
const t = setTimeout(() => {
if (!resolved) reject(new Error('DFU control timeout'));
}, timeoutMs);
} catch (e) {
reject(e);
}
});
try {
await promise;
} finally {
// unsubscribe notifications
try { await BluetoothManager.unsubscribeCharacteristic(deviceId, serviceId, notifyCharId); } catch(e) {}
}
}
_sleep(ms: number) {
return new Promise((r) => setTimeout(r, ms));
}
_defaultControlParser(data: Uint8Array) {
if (!data || data.length === 0) return null;
if (data.length >= 2) {
const maybeProgress = data[1];
if (maybeProgress >= 0 && maybeProgress <= 100) return { type: 'progress', progress: maybeProgress };
}
const op = data[0];
if (op === 0x01) return { type: 'success' };
if (op === 0xFF) return { type: 'error', error: data };
return { type: 'info' };
}
_nordicControlParser(data: Uint8Array) {
if (!data || data.length === 0) return null;
const op = data[0];
// 0x11 = Packet Receipt Notification
if (op === 0x11 && data.length >= 3) {
const lsb = data[1];
const msb = data[2];
const received = (msb << 8) | lsb;
return { type: 'progress', progress: received };
}
// 0x10 = Response
if (op === 0x10 && data.length >= 3) {
const resultCode = data[2];
if (resultCode === 0x01) return { type: 'success' };
return { type: 'error', error: { resultCode } };
}
return null;
}
}
export const dfuManager = new WebDfuManager();

View File

@@ -0,0 +1,46 @@
import * as BluetoothManager from './bluetooth_manager.uts';
export const bluetoothService = {
scanDevices: BluetoothManager.scanDevices,
connectDevice: BluetoothManager.connectDevice,
disconnectDevice: BluetoothManager.disconnectDevice,
getConnectedDevices: BluetoothManager.getConnectedDevices,
discoverServices: BluetoothManager.discoverServices,
// compatibility aliases used by app code
getServices: BluetoothManager.discoverServices,
getCharacteristics: BluetoothManager.getCharacteristics,
readCharacteristic: BluetoothManager.readCharacteristic,
writeCharacteristic: BluetoothManager.writeCharacteristic,
subscribeCharacteristic: BluetoothManager.subscribeCharacteristic,
unsubscribeCharacteristic: BluetoothManager.unsubscribeCharacteristic,
sendCommand: BluetoothManager.sendCommand,
onConnectionStateChange: BluetoothManager.onConnectionStateChange,
// 兼容旧接口,如有 readCharacteristic 可补充
};
// Provide a minimal EventEmitter-style `.on(eventName, handler)` to match app code
// Supported events: 'deviceFound', 'scanFinished', 'connectionStateChanged'
bluetoothService.on = function(eventName: string, handler: Function) {
if (!eventName || typeof handler !== 'function') return;
switch (eventName) {
case 'deviceFound':
return BluetoothManager.onDeviceFound(handler);
case 'scanFinished':
return BluetoothManager.onScanFinished(handler);
case 'connectionStateChanged':
return BluetoothManager.onConnectionStateChange(handler);
default:
// no-op for unsupported events
return;
}
};
// Backwards-compat: getAutoBleInterfaces expected by pages -> maps to autoConnect
if (!bluetoothService.getAutoBleInterfaces) {
bluetoothService.getAutoBleInterfaces = function(deviceId: string) {
return BluetoothManager.autoConnect(deviceId);
}
}
import { dfuManager as webDfuManager } from './dfu_manager.uts'
export const dfuManager = webDfuManager;

View File

@@ -0,0 +1,356 @@
// 服务与特征值操作相关:服务发现、特征值读写、订阅
import { BleService, BleCharacteristic } from '../interface.uts';
import { BLE_SERVICE_PREFIXES } from './bluetooth_manager.uts';
function isBaeService(uuid: string): boolean {
if (!uuid) return false;
const lower = uuid.toLowerCase();
for (let i = 0; i < BLE_SERVICE_PREFIXES.length; i++) {
const prefix = BLE_SERVICE_PREFIXES[i];
if (prefix && lower.startsWith(prefix.toLowerCase())) {
return true;
}
}
return false;
}
function isBleService(uuid: string, prefixes: string[]): boolean {
if (!uuid) return false;
if (!prefixes || prefixes.length === 0) return false;
const lower = uuid.toLowerCase();
for (let i = 0; i < prefixes.length; i++) {
const prefix = prefixes[i];
if (!prefix) continue;
const prefixLower = prefix.toLowerCase();
if (lower.startsWith(prefixLower)) return true;
if (prefixLower.length === 4) {
const expanded = `0000${prefixLower}-0000-1000-8000-00805f9b34fb`;
if (lower === expanded) return true;
}
}
return false;
}
function getPrimaryServerCandidate(server: any): any {
if (!server) return null;
if (typeof server.getPrimaryServices === 'function') return server;
if (server.gatt && typeof server.gatt.getPrimaryServices === 'function') return server.gatt;
if (server.device && server.device.gatt && typeof server.device.gatt.getPrimaryServices === 'function') {
return server.device.gatt;
}
return server;
}
function isGattConnected(server: any): boolean {
if (!server) return false;
if (typeof server.connected === 'boolean') return server.connected;
if (server.device && server.device.gatt && typeof server.device.gatt.connected === 'boolean') {
return server.device.gatt.connected;
}
return true;
}
async function attemptGattConnect(source: any): Promise<any | null> {
if (!source) return null;
try {
if (typeof source.connect === 'function') {
const result = await source.connect();
if (result != null) return result;
}
} catch (e) {
console.warn('[ServiceManager] connect() failed', e);
}
const deviceGatt = source?.device?.gatt;
if (deviceGatt && typeof deviceGatt.connect === 'function') {
try {
const result = await deviceGatt.connect();
if (result != null) return result;
return deviceGatt;
} catch (e) {
console.warn('[ServiceManager] device.gatt.connect() failed', e);
}
}
const nestedGatt = source?.gatt;
if (nestedGatt && typeof nestedGatt.connect === 'function') {
try {
const result = await nestedGatt.connect();
if (result != null) return result;
return nestedGatt;
} catch (e) {
console.warn('[ServiceManager] nested gatt.connect() failed', e);
}
}
return null;
}
async function ensureGattServer(server: any, forceReconnect: boolean = false): Promise<any> {
let candidate = getPrimaryServerCandidate(server);
if (!forceReconnect && isGattConnected(candidate)) {
return candidate;
}
console.log('[ServiceManager] ensureGattServer attempting reconnect');
const connected = await attemptGattConnect(candidate ?? server);
if (connected != null) {
candidate = getPrimaryServerCandidate(connected);
}
if (!isGattConnected(candidate) && server && server !== candidate) {
const fallback = await attemptGattConnect(server);
if (fallback != null) {
candidate = getPrimaryServerCandidate(fallback);
}
}
return candidate;
}
function isDisconnectError(err: any): boolean {
if (!err) return false;
const message = typeof err.message === 'string' ? err.message.toLowerCase() : '';
return err.name === 'NetworkError' || message.includes('disconnected') || message.includes('connect first');
}
// Helper: normalize UUIDs (accept 16-bit like '180F' and expand to full 128-bit)
function normalizeUuid(uuid: string): string {
if (!uuid) return uuid;
const u = uuid.toLowerCase();
// already full form
if (u.length === 36 && u.indexOf('-') > 0) return u;
// allow forms like '180f' or '0x180f'
const hex = u.replace(/^0x/, '').replace(/[^0-9a-f]/g, '');
if (/^[0-9a-f]{4}$/.test(hex)) {
return `0000${hex}-0000-1000-8000-00805f9b34fb`;
}
return uuid;
}
export class ServiceManager {
private services = {};
private characteristics = {};
private characteristicCallbacks = {};
private characteristicListeners = {};
constructor() {
}
async discoverServices(deviceId: string, server: any): Promise<BleService[]> {
// 获取设备的 GATT 服务器
console.log(deviceId)
// 由外部传入 server
if (!server) throw new Error('设备未连接');
try {
// Some browsers report a stale server with connected=false; attempt to reconnect
const needsReconnect = (server.connected === false) || (server.device && server.device.gatt && server.device.gatt.connected === false);
if (needsReconnect) {
console.log('[ServiceManager] server disconnected, attempting reconnect');
if (typeof server.connect === 'function') {
try {
await server.connect();
} catch (connectErr) {
console.warn('[ServiceManager] server.connect() failed', connectErr);
}
}
if (server.device && server.device.gatt && typeof server.device.gatt.connect === 'function' && !server.device.gatt.connected) {
try {
await server.device.gatt.connect();
} catch (gattErr) {
console.warn('[ServiceManager] server.device.gatt.connect() failed', gattErr);
}
}
}
} catch (reconnectError) {
console.warn('[ServiceManager] reconnect attempt encountered error', reconnectError);
}
const bleServices: BleService[] = [];
if (!this.services[deviceId]) this.services[deviceId] = {};
try {
console.log('[ServiceManager] discoverServices called for', deviceId)
console.log('[ServiceManager] server param:', server)
let services = null;
let primaryServer = await ensureGattServer(server);
if (primaryServer && typeof primaryServer.getPrimaryServices === 'function') {
console.log('[ServiceManager]server.getPrimaryServices')
try {
services = await primaryServer.getPrimaryServices();
console.log('[ServiceManager] got services from primaryServer', services)
} catch (primaryError) {
if (isDisconnectError(primaryError)) {
console.log('[ServiceManager] primary getPrimaryServices failed, retrying after reconnect');
primaryServer = await ensureGattServer(server, true);
if (primaryServer && typeof primaryServer.getPrimaryServices === 'function') {
services = await primaryServer.getPrimaryServices();
} else {
throw primaryError;
}
} else {
throw primaryError;
}
}
}
if (!services && server && server.device && server.device.gatt) {
const fallbackServer = await ensureGattServer(server.device.gatt, true);
if (fallbackServer && typeof fallbackServer.getPrimaryServices === 'function') {
console.log('server.device.gatt.getPrimaryServices (fallback)')
services = await fallbackServer.getPrimaryServices();
}
}
if (!services && server && typeof server.connect === 'function') {
console.log('other getPrimaryServices')
try {
const s = await server.connect();
if (s && typeof s.getPrimaryServices === 'function') {
services = await s.getPrimaryServices();
}
} catch (e) {
console.warn('[ServiceManager] server.connect() failed', e)
}
}
console.log('[ServiceManager] services resolved:', services)
if (!services) throw new Error('无法解析 GATT services 对象 —— server 参数不包含 getPrimaryServices');
for (let i = 0; i < services.length; i++) {
const service = services[i];
const rawUuid = service.uuid;
const uuid = normalizeUuid(rawUuid);
bleServices.push({ uuid, isPrimary: true });
this.services[deviceId][uuid] = service;
// ensure service UUID detection supports standard BLE services like Battery (0x180F)
const lower = uuid.toLowerCase();
const isBattery = lower === '0000180f-0000-1000-8000-00805f9b34fb';
if (isBattery || isBaeService(uuid) || isBleService(uuid, BLE_SERVICE_PREFIXES)) {
await this.getCharacteristics(deviceId, uuid);
}
}
return bleServices;
} catch (err) {
console.error('[ServiceManager] discoverServices error:', err)
throw err;
}
}
async getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
const service = this.services[deviceId]?.[serviceId];
if (!service) throw new Error('服务未找到');
const characteristics = await service.getCharacteristics();
console.log(characteristics)
const bleCharacteristics: BleCharacteristic[] = [];
if (!this.characteristics[deviceId]) this.characteristics[deviceId] = {};
if (!this.characteristics[deviceId][serviceId]) this.characteristics[deviceId][serviceId] = {};
for (const characteristic of characteristics) {
const properties = {
read: characteristic.properties.read || false,
write: characteristic.properties.write || characteristic.properties.writableAuxiliaries || characteristic.properties.reliableWrite || characteristic.properties.writeWithoutResponse || false,
notify: characteristic.properties.notify || false,
indicate: characteristic.properties.indicate || false
};
console.log(characteristic.properties)
console.log(properties)
// Construct a BleCharacteristic-shaped object including the required `service` property
const bleCharObj = {
uuid: characteristic.uuid,
service: { uuid: serviceId, isPrimary: true },
properties
};
bleCharacteristics.push(bleCharObj);
// keep native characteristic reference for read/write/notify operations
this.characteristics[deviceId][serviceId][characteristic.uuid] = characteristic;
}
return bleCharacteristics;
}
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: string | ArrayBuffer): Promise<void> {
const characteristic = this.characteristics[deviceId]?.[serviceId]?.[characteristicId];
if (!characteristic) throw new Error('特征值未找到');
let buffer;
if (typeof data === 'string') {
buffer = new TextEncoder().encode(data).buffer;
} else {
buffer = data;
}
await characteristic.writeValue(buffer);
}
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback): Promise<void> {
const characteristic = this.characteristics[deviceId]?.[serviceId]?.[characteristicId];
if (!characteristic) throw new Error('特征值未找到');
if (!characteristic.properties.notify && !characteristic.properties.indicate) {
throw new Error('特征值不支持通知');
}
if (!this.characteristicCallbacks[deviceId]) this.characteristicCallbacks[deviceId] = {};
if (!this.characteristicCallbacks[deviceId][serviceId]) this.characteristicCallbacks[deviceId][serviceId] = {};
this.characteristicCallbacks[deviceId][serviceId][characteristicId] = callback;
try {
await characteristic.startNotifications();
} catch (e) {
console.error('[ServiceManager] startNotifications failed for', deviceId, serviceId, characteristicId, e);
throw e;
}
const listener = (event) => {
const value = event.target.value;
let data: Uint8Array;
if (value && typeof value.byteOffset === 'number' && typeof value.byteLength === 'number') {
data = new Uint8Array(value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength));
} else if (value instanceof ArrayBuffer) {
data = new Uint8Array(value.slice(0));
} else {
data = new Uint8Array(0);
}
const cb = this.characteristicCallbacks[deviceId][serviceId][characteristicId];
if (cb) {
try {
cb(data);
} catch (err) {
console.warn('[ServiceManager] characteristic notify callback error', err);
}
}
};
// store listener so it can be removed later
if (!this.characteristicListeners[deviceId]) this.characteristicListeners[deviceId] = {};
if (!this.characteristicListeners[deviceId][serviceId]) this.characteristicListeners[deviceId][serviceId] = {};
this.characteristicListeners[deviceId][serviceId][characteristicId] = { characteristic, listener };
characteristic.addEventListener('characteristicvaluechanged', listener);
console.log('[ServiceManager] subscribeCharacteristic ok:', deviceId, serviceId, characteristicId);
}
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
const entry = this.characteristicListeners[deviceId]?.[serviceId]?.[characteristicId];
if (!entry) return;
try {
const { characteristic, listener } = entry;
characteristic.removeEventListener('characteristicvaluechanged', listener);
const gattCandidate = characteristic?.service?.device?.gatt || characteristic?.service?.gatt || characteristic?.service;
const shouldStop = !gattCandidate || isGattConnected(gattCandidate);
if (shouldStop && typeof characteristic.stopNotifications === 'function') {
try {
await characteristic.stopNotifications();
} catch (stopError) {
if (!isDisconnectError(stopError)) {
throw stopError;
}
console.log('[ServiceManager] stopNotifications ignored disconnect:', deviceId, serviceId, characteristicId);
}
}
console.log('[ServiceManager] unsubscribeCharacteristic ok:', deviceId, serviceId, characteristicId);
} catch (e) {
console.warn('[ServiceManager] unsubscribeCharacteristic failed for', deviceId, serviceId, characteristicId, e);
// ignore
}
// cleanup
delete this.characteristicListeners[deviceId][serviceId][characteristicId];
delete this.characteristicCallbacks[deviceId][serviceId][characteristicId];
}
// Read a characteristic value and return ArrayBuffer
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
const characteristic = this.characteristics[deviceId]?.[serviceId]?.[characteristicId];
if (!characteristic) throw new Error('特征值未找到');
// Web Bluetooth returns a DataView from readValue()
const value = await characteristic.readValue();
if (!value) return new ArrayBuffer(0);
// DataView.buffer is a shared ArrayBuffer; return a copy slice to be safe
try {
return value.buffer ? value.buffer.slice(0) : new Uint8Array(value).buffer;
} catch (e) {
// fallback
const arr = new Uint8Array(value);
return arr.buffer;
}
}
}

View File

@@ -0,0 +1,6 @@
## 0.0.32024-05-29
- feat: `SetClipboardDataOption``showToast`为默认弹出
## 0.0.22024-05-29
- feat: `SetClipboardDataOption`增加`showToast`对齐web
## 0.0.12024-04-12
- init

View File

@@ -0,0 +1,32 @@
<template>
<view>
<button @click="setClipboard">设置</button>
<button @click="getClipboard">获取</button>
</view>
</template>
<script setup>
import {setClipboardData, getClipboardData, SetClipboardDataOption, GetClipboardDataOption, GetClipboardDataSuccessCallbackOption} from '@/uni_modules/lime-clipboard'
const setClipboard = ()=>{
setClipboardData({
data: '這里是內容',
showToast: true,
success(res){
console.log('res', res.errMsg)
}
} as SetClipboardDataOption)
}
const getClipboard = () =>{
getClipboardData({
success(res: GetClipboardDataSuccessCallbackOption){
console.log('res', res)
}
} as GetClipboardDataOption)
}
</script>
<style>
</style>

View File

@@ -0,0 +1,86 @@
{
"id": "lime-clipboard",
"displayName": "lime-clipboard 剪贴板",
"version": "0.0.3",
"description": "lime-clipboard 系参考小程序setClipboardData和getClipboardData实现的UTS API支持uniappX(web,ios,安卓)",
"keywords": [
"lime-clipboard",
"setClipboardData",
"getClipboardData",
"clipboard",
"剪贴板"
],
"repository": "",
"engines": {
"HBuilderX": "^4.11"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y",
"alipay": "y"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "y"
},
"App": {
"app-android": "y",
"app-ios": "y"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}

View File

@@ -0,0 +1,30 @@
# lime-clipboard
- 参考小程序`setClipboardData``getClipboardData`实现的UTS API支持uniappX(web,ios,安卓)
## 安装
插件市场导入即可
## 使用
使用方法跟小程序的一样
```ts
import {setClipboardData, getClipboardData, SetClipboardDataOption, GetClipboardDataOption, GetClipboardDataSuccessCallbackOption} from '@/uni_modules/lime-clipboard'
setClipboardData({
data: '这里是內容',
success(res){
console.log('res', res.errMsg)
}
} as SetClipboardDataOption)
getClipboardData({
success(res: GetClipboardDataSuccessCallbackOption){
console.log('res', res)
}
} as GetClipboardDataOption)
```
## API
因为直接参照小程序`setClipboardData``getClipboardData`API所以可以直接按[clipboard](https://uniapp.dcloud.net.cn/api/system/clipboard.html)文档来

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.limeui.clipboard">
<uses-permission android:name="android.permission.READ_CLIPBOARD_IN_BACKGROUND" />
<uses-permission android:name="android.permission.WRITE_CLIPBOARD_IN_BACKGROUND" />
</manifest>

View File

@@ -0,0 +1,3 @@
{
"minSdkVersion": "21"
}

View File

@@ -0,0 +1,80 @@
import ClipData from "android.content.ClipData";
import ClipboardManager from "android.content.ClipboardManager";
import Context from "android.content.Context";
import { UTSAndroid } from "io.dcloud.uts";
import { SetClipboardDataOption, GetClipboardDataOption, GetClipboardDataSuccessCallbackOption } from '../interface.uts';
import { GeneralCallbackResultImpl } from '../unierror.uts';
export function setClipboardData(options : SetClipboardDataOption) {
const handleClipboardOperationFailure = () => {
const res = new GeneralCallbackResultImpl(9010002)
options.fail?.(res)
options.complete?.(res)
}
try {
const context = UTSAndroid.getAppContext();
if (context != null) {
const clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager;
const clip = ClipData.newPlainText('label', options.data);
clipboard.setPrimaryClip(clip);
const res = new GeneralCallbackResultImpl(9010001)
if(options.showToast != false){
uni.showToast({
icon: 'success',
title: '内容已复制'
})
}
options.success?.(res)
options.complete?.(res)
} else {
handleClipboardOperationFailure()
}
} catch (e) {
handleClipboardOperationFailure()
}
}
export function getClipboardData(options : GetClipboardDataOption) {
const handleClipboardOperationFailure = () => {
const res = new GeneralCallbackResultImpl(9010002, 'get')
options.fail?.(res)
options.complete?.(res)
}
try {
const context = UTSAndroid.getAppContext();
if (context != null) {
const clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager;
const clip = clipboard.getPrimaryClip();
if (clip != null && clip.getItemCount() > 0) {
const text = clip.getItemAt(0).getText();
if (text != null) {
options.success?.({
data: text.toString(),
errMsg: '成功'
} as GetClipboardDataSuccessCallbackOption)
} else {
// 如果剪贴板没有文本数据,调用失败的处理函数
handleClipboardOperationFailure();
}
} else {
// 如果剪贴板没有内容,调用失败的处理函数
handleClipboardOperationFailure();
}
} else {
// 如果无法获取应用上下文,调用失败的处理函数
handleClipboardOperationFailure();
}
} catch (e) {
handleClipboardOperationFailure()
}
}

View File

@@ -0,0 +1,3 @@
{
"deploymentTarget": "9"
}

View File

@@ -0,0 +1,33 @@
import { UIPasteboard } from "UIKit"
import { SetClipboardDataOption, GetClipboardDataOption, GetClipboardDataSuccessCallbackOption } from '../interface.uts';
import { GeneralCallbackResultImpl } from '../unierror.uts';
export function setClipboardData(options : SetClipboardDataOption){
let pasteboard = UIPasteboard.general
pasteboard.string = options.data
const res = new GeneralCallbackResultImpl(9010001)
if(options.showToast != false){
uni.showToast({
icon: 'success',
title: '内容已复制'
})
}
options.success?.(res)
options.complete?.(res)
}
export function getClipboardData(options : GetClipboardDataOption){
let pasteboard = UIPasteboard.general;
const res = new GeneralCallbackResultImpl(9010002, 'get')
if(pasteboard.string == null){
options.fail?.(res)
options.complete?.(res)
} else {
options.success?.({
errMsg: 'getClipboardData:ok',
data: `${pasteboard.string!}`
} as GetClipboardDataSuccessCallbackOption)
options.complete?.(res)
}
}

View File

@@ -0,0 +1,19 @@
export * from './interface'
import {SetClipboardDataOption, GetClipboardDataOption} from './interface'
/**
* 设置系统剪贴板的内容
*
* 文档: [http://uniapp.dcloud.io/api/system/clipboard?id=setclipboarddata](http://uniapp.dcloud.io/api/system/clipboard?id=setclipboarddata)
*/
export function setClipboardData(options : SetClipboardDataOption) {
uni.setClipboardData(options as UniNamespace.SetClipboardDataOptions)
}
/**
* 获得系统剪贴板的内容
*
* 文档: [http://uniapp.dcloud.io/api/system/clipboard?id=getclipboarddata](http://uniapp.dcloud.io/api/system/clipboard?id=getclipboarddata)
*/
export function getClipboardData(options : GetClipboardDataOption) {
uni.getClipboardData(options as UniNamespace.GetClipboardDataOptions)
}

View File

@@ -0,0 +1,66 @@
/**
* 错误码
* 根据uni错误码规范要求建议错误码以90开头以下是错误码示例
* - 9010001 错误信息1
* - 9010002 错误信息2
*/
export type LimeClipboardErrorCode = 9010001 | 9010002;
/**
* myApi 的错误回调参数
*/
export interface GeneralCallbackResult extends IUniError {
errCode : LimeClipboardErrorCode
};
// export interface GeneralCallbackResult {
// /** 错误信息 */
// errMsg : string
// }
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
export type SetClipboardDataCompleteCallback = (res : UniError) => void
/** 接口调用失败的回调函数 */
export type SetClipboardDataFailCallback = (res : UniError) => void
/** 接口调用成功的回调函数 */
export type SetClipboardDataSuccessCallback = (res : UniError) => void
export type SetClipboardDataOption = {
showToast?: boolean
/** 剪贴板的内容 */
data : string
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete ?: SetClipboardDataCompleteCallback
/** 接口调用失败的回调函数 */
fail ?: SetClipboardDataFailCallback
/** 接口调用成功的回调函数 */
success ?: SetClipboardDataSuccessCallback
}
export type GetClipboardDataSuccessCallbackOption = {
/** 剪贴板的内容 */
data : string
errMsg : string
}
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
export type GetClipboardDataCompleteCallback = (res : UniError) => void
/** 接口调用失败的回调函数 */
export type GetClipboardDataFailCallback = (res : UniError) => void
/** 接口调用成功的回调函数 */
export type GetClipboardDataSuccessCallback = (
option : GetClipboardDataSuccessCallbackOption
) => void
export type GetClipboardDataOption = {
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete ?: GetClipboardDataCompleteCallback
/** 接口调用失败的回调函数 */
fail ?: GetClipboardDataFailCallback
/** 接口调用成功的回调函数 */
success ?: GetClipboardDataSuccessCallback
}

View File

@@ -0,0 +1,39 @@
/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */
import { LimeClipboardErrorCode, GeneralCallbackResult } from "./interface.uts"
/**
* 错误主题
* 注意:错误主题一般为插件名称,每个组件不同,需要使用时请更改。
* [可选实现]
*/
export const UniErrorSubject = 'ClipboardData';
/**
* 错误信息
* @UniError
* [可选实现]
*/
export const UniErrors : Map<LimeClipboardErrorCode, string> = new Map([
/**
* 错误码及对应的错误信息
*/
[9010001, 'ClipboardData:ok'],
[9010002, 'ClipboardData:failed'],
]);
/**
* 错误对象实现
*/
export class GeneralCallbackResultImpl extends UniError implements GeneralCallbackResult {
/**
* 错误对象构造函数
*/
constructor(errCode : LimeClipboardErrorCode, type: string = 'set') {
super();
this.errSubject = type + UniErrorSubject;
this.errCode = errCode;
this.errMsg = type + (UniErrors[errCode] ?? "");
}
}

View File

@@ -0,0 +1,12 @@
## 0.0.62025-04-21
- feat: 兼容uniappx 鸿蒙next
## 0.0.52024-09-30
- fix: 修复 vue2类型问题
## 0.0.42024-06-25
- chore: 更改为非`utssdk`结构
## 0.0.32024-06-19
- feat: 支持`uniapp`
## 0.0.22024-03-30
- fix: 修复 因equals导致web可能报错
## 0.0.12024-03-12
- init

Some files were not shown because too many files have changed in this diff Show More