You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.5 KiB
72 lines
2.5 KiB
import 'dart:convert';
|
|
|
|
import '../../../data/models/appconfig_model.dart';
|
|
|
|
class AppConfig {
|
|
static late UserGetAppConfigResp config;
|
|
|
|
// 新增一个私有的静态布尔值来跟踪初始化状态
|
|
static bool _isInitialized = false;
|
|
|
|
/// 检查AppConfig单例是否已经被初始化
|
|
/// 外部通过调用此方法来安全地检查状态
|
|
static bool isInitialized() {
|
|
return _isInitialized;
|
|
}
|
|
|
|
// 可选:提供初始化方法
|
|
static void initialize(UserGetAppConfigResp resp) {
|
|
config = resp;
|
|
_isInitialized = true; // <-- 新增此行
|
|
}
|
|
|
|
//环境变量
|
|
static String? env(String key) {
|
|
return config.env[key];
|
|
}
|
|
|
|
//mcp配置
|
|
static dynamic mcpConfig() {
|
|
return {"mcpServers": config.mcps};
|
|
}
|
|
|
|
//智能体配置
|
|
///
|
|
/// 找不到该 agentId 时返回 null。**不要改回 `firstWhere`**——它没有 orElse,
|
|
/// 查不到时抛 StateError 而不是返回 null,调用方(ble_manager 的
|
|
/// startAgentService)那句 try 会把整个智能体服务的启动一起吞掉。
|
|
static String? agentsystemPrompt(String agentId) {
|
|
if (!_isInitialized) return null;
|
|
for (final agent in config.agents) {
|
|
if (agent.id == agentId) return agent.systemPrompt;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static List<String> allproductNames() {
|
|
List<String> names =
|
|
config.products.map((user) => user.devicename).toList();
|
|
return names;
|
|
}
|
|
|
|
/// 根据产品名称获取产品配置,查不到返回 null。
|
|
///
|
|
/// ⚠️ **不要改回 `firstWhere`**:它没有 orElse,查不到时抛 StateError 而不是
|
|
/// 返回 null。所有调用点写的都是 `if (product != null)`,那个 null 分支于是
|
|
/// 永远等不到,异常被外层 try 吞掉后直接走兜底——设备类型退化成 unknown、
|
|
/// 通话翻译的 broadcastpeertranslate 恒为 false,且全程不报任何错。
|
|
///
|
|
/// 未初始化(`user_getappconfig` 还没回来)时同样返回 null,而不是让
|
|
/// `late config` 抛 LateInitializationError——否则 null 契约仍然不成立。
|
|
///
|
|
/// 空设备名也返回 null:`devicename.contains('')` 恒为 true,原实现会把
|
|
/// **产品表里的第一个产品**当成匹配结果返回。调用点本来就都在紧接着判
|
|
/// `deviceName != ''`,没有一处依赖这个行为。
|
|
static DBProduct? getproduct(String name) {
|
|
if (!_isInitialized || name.isEmpty) return null;
|
|
for (final product in config.products) {
|
|
if (product.devicename.contains(name)) return product;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|