feat: 增加AI配置文件与TTS自动朗读,修复二级页面返回按钮与音频播放
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'models.dart';
|
||||
|
||||
class AiConfigFile {
|
||||
const AiConfigFile({
|
||||
required this.provider,
|
||||
required this.endpoint,
|
||||
required this.model,
|
||||
this.apiKey,
|
||||
this.description,
|
||||
});
|
||||
|
||||
final AiProviderType provider;
|
||||
final String endpoint;
|
||||
final String model;
|
||||
final String? apiKey;
|
||||
final String? description;
|
||||
|
||||
static const String defaultAssetPath = 'assets/config/ai_config.json';
|
||||
|
||||
factory AiConfigFile.fromJson(Map<String, dynamic> json) {
|
||||
final providerStr = json['provider'] as String? ?? 'compatible';
|
||||
final provider = AiProviderType.values.firstWhere(
|
||||
(p) => p.name.toLowerCase() == providerStr.toLowerCase(),
|
||||
orElse: () => AiProviderType.compatible,
|
||||
);
|
||||
return AiConfigFile(
|
||||
provider: provider,
|
||||
endpoint: (json['endpoint'] as String? ?? '').trim(),
|
||||
model: (json['model'] as String? ?? '').trim(),
|
||||
apiKey: json['apiKey'] as String?,
|
||||
description: json['description'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
factory AiConfigFile.parse(String rawJson) {
|
||||
final data = jsonDecode(rawJson) as Map<String, dynamic>;
|
||||
return AiConfigFile.fromJson(data);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'provider': provider.name,
|
||||
'endpoint': endpoint,
|
||||
'model': model,
|
||||
if (apiKey != null) 'apiKey': apiKey,
|
||||
if (description != null) 'description': description,
|
||||
};
|
||||
|
||||
static Future<AiConfigFile?> loadFromAsset([
|
||||
String path = defaultAssetPath,
|
||||
]) async {
|
||||
try {
|
||||
final content = await rootBundle.loadString(path);
|
||||
return AiConfigFile.parse(content);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
AiConfigFile copyWith({
|
||||
AiProviderType? provider,
|
||||
String? endpoint,
|
||||
String? model,
|
||||
String? apiKey,
|
||||
String? description,
|
||||
}) => AiConfigFile(
|
||||
provider: provider ?? this.provider,
|
||||
endpoint: endpoint ?? this.endpoint,
|
||||
model: model ?? this.model,
|
||||
apiKey: apiKey ?? this.apiKey,
|
||||
description: description ?? this.description,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user