// EnvironmentCredentialManager.cs
using Convai.Runtime.Components;
using Convai.Runtime.Core;
using Convai.Runtime.Core.Configuration;
using UnityEngine;
public class EnvironmentCredentialManager : ConvaiManager
{
protected override ConvaiRuntimeBuilder CreateRuntimeBuilder()
{
ConvaiRuntimeBuilder builder = base.CreateRuntimeBuilder();
string apiKey = ResolveApiKey();
string serverUrl = ResolveServerUrl();
if (string.IsNullOrEmpty(apiKey))
{
Debug.LogError("[EnvironmentCredentialManager] 未找到 API 密钥。" +
"请检查你的环境或 secrets 配置。");
}
builder.UseConfig(new ConvaiBootstrapConfigSnapshot(
apiKey: apiKey,
serverUrl: serverUrl
));
return builder;
}
private static string ResolveApiKey()
{
// 从环境变量读取(CI、Docker、云运行)。
string key = System.Environment.GetEnvironmentVariable("CONVAI_API_KEY");
if (!string.IsNullOrEmpty(key)) return key;
// 回退到 ConvaiSettings(编辑器 / 本地开发)。
return ConvaiSettings.Instance?.ApiKey ?? string.Empty;
}
private static string ResolveServerUrl()
{
return System.Environment.GetEnvironmentVariable("CONVAI_SERVER_URL")
?? ConvaiSettings.Instance?.ServerUrl
?? "https://live.convai.com";
}
}