-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnv.cs
More file actions
174 lines (163 loc) · 8.9 KB
/
Env.cs
File metadata and controls
174 lines (163 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace TelegramSearchBot.Common {
public static class Env {
static Env() {
WorkDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TelegramSearchBot");
if (!Directory.Exists(WorkDir)) {
Directory.CreateDirectory(WorkDir);
}
var config = new Config();
try {
var loadedConfig = JsonConvert.DeserializeObject<Config>(File.ReadAllText(Path.Combine(WorkDir, "Config.json")));
if (loadedConfig is not null) {
config = loadedConfig;
}
} catch {
}
var botApiEndpoint = ResolveBotApiEndpoint(config);
EnableLocalBotAPI = config.EnableLocalBotAPI;
TelegramBotApiId = config.TelegramBotApiId;
TelegramBotApiHash = config.TelegramBotApiHash;
LocalBotApiPort = config.LocalBotApiPort;
ExternalLocalBotApiBaseUrl = botApiEndpoint.ExternalLocalBotApiBaseUrl;
BaseUrl = botApiEndpoint.BaseUrl;
IsLocalAPI = botApiEndpoint.IsLocalApi;
BotToken = config.BotToken;
AdminId = config.AdminId;
EnableAutoOCR = config.EnableAutoOCR;
EnableAutoASR = config.EnableAutoASR;
//WorkDir = config.WorkDir;
TaskDelayTimeout = config.TaskDelayTimeout;
SameServer = config.SameServer;
OllamaModelName = config.OllamaModelName;
EnableVideoASR = config.EnableVideoASR;
EnableOpenAI = config.EnableOpenAI;
OpenAIModelName = config.OpenAIModelName;
OLTPAuth = config.OLTPAuth;
OLTPAuthUrl = config.OLTPAuthUrl;
OLTPName = config.OLTPName;
BraveApiKey = config.BraveApiKey;
EnableAccounting = config.EnableAccounting;
MaxToolCycles = config.MaxToolCycles;
EnableLLMAgentProcess = config.EnableLLMAgentProcess;
AgentHeartbeatIntervalSeconds = config.AgentHeartbeatIntervalSeconds;
AgentHeartbeatTimeoutSeconds = config.AgentHeartbeatTimeoutSeconds;
AgentChunkPollingIntervalMilliseconds = config.AgentChunkPollingIntervalMilliseconds;
AgentIdleTimeoutMinutes = config.AgentIdleTimeoutMinutes;
MaxConcurrentAgents = config.MaxConcurrentAgents;
AgentTaskTimeoutSeconds = config.AgentTaskTimeoutSeconds;
AgentShutdownGracePeriodSeconds = config.AgentShutdownGracePeriodSeconds;
AgentMaxRecoveryAttempts = config.AgentMaxRecoveryAttempts;
AgentQueueBacklogWarningThreshold = config.AgentQueueBacklogWarningThreshold;
AgentProcessMemoryLimitMb = config.AgentProcessMemoryLimitMb;
}
public static BotApiEndpointSettings ResolveBotApiEndpoint(Config config) {
ArgumentNullException.ThrowIfNull(config);
var normalizedExternalLocalBotApiBaseUrl = NormalizeBaseUrl(config.ExternalLocalBotApiBaseUrl, string.Empty);
if (config.EnableLocalBotAPI) {
return new BotApiEndpointSettings(
$"http://127.0.0.1:{config.LocalBotApiPort}",
true,
normalizedExternalLocalBotApiBaseUrl);
}
if (!string.IsNullOrWhiteSpace(normalizedExternalLocalBotApiBaseUrl)) {
return new BotApiEndpointSettings(
normalizedExternalLocalBotApiBaseUrl,
true,
normalizedExternalLocalBotApiBaseUrl);
}
return new BotApiEndpointSettings(
NormalizeBaseUrl(config.BaseUrl, "https://api.telegram.org"),
config.IsLocalAPI,
normalizedExternalLocalBotApiBaseUrl);
}
private static string NormalizeBaseUrl(string? baseUrl, string fallback) {
if (string.IsNullOrWhiteSpace(baseUrl)) {
return fallback;
}
return baseUrl.Trim().TrimEnd('/');
}
public static readonly string BaseUrl = null!;
#pragma warning disable CS8604 // 引用类型参数可能为 null。
public static readonly bool IsLocalAPI;
#pragma warning restore CS8604 // 引用类型参数可能为 null。
public static readonly string BotToken = null!;
public static readonly long AdminId;
public static readonly bool EnableAutoOCR;
public static readonly bool EnableAutoASR;
public static readonly bool EnableLocalBotAPI;
public static readonly string TelegramBotApiId = null!;
public static readonly string TelegramBotApiHash = null!;
public static readonly int LocalBotApiPort;
public static readonly string ExternalLocalBotApiBaseUrl = string.Empty;
public static readonly string WorkDir = null!;
public static readonly int TaskDelayTimeout;
public static readonly bool SameServer;
public static long BotId { get; set; }
public static string OllamaModelName { get; set; } = null!;
public static bool EnableVideoASR { get; set; }
public static bool EnableOpenAI { get; set; } = false;
public static string OpenAIModelName { get; set; } = null!;
public static int SchedulerPort { get; set; }
public static string OLTPAuth { get; set; } = null!;
public static string OLTPAuthUrl { get; set; } = null!;
public static string OLTPName { get; set; } = null!;
public static string BraveApiKey { get; set; } = null!;
public static bool EnableAccounting { get; set; } = false;
public static int MaxToolCycles { get; set; }
public static bool EnableLLMAgentProcess { get; set; } = false;
public static int AgentHeartbeatIntervalSeconds { get; set; } = 10;
public static int AgentHeartbeatTimeoutSeconds { get; set; } = 60;
public static int AgentChunkPollingIntervalMilliseconds { get; set; } = 200;
public static int AgentIdleTimeoutMinutes { get; set; } = 15;
public static int MaxConcurrentAgents { get; set; } = 8;
public static int AgentTaskTimeoutSeconds { get; set; } = 300;
public static int AgentShutdownGracePeriodSeconds { get; set; } = 15;
public static int AgentMaxRecoveryAttempts { get; set; } = 2;
public static int AgentQueueBacklogWarningThreshold { get; set; } = 20;
public static int AgentProcessMemoryLimitMb { get; set; } = 256;
public static Dictionary<string, string> Configuration { get; set; } = new Dictionary<string, string>();
}
public class Config {
public string BaseUrl { get; set; } = "https://api.telegram.org";
public string BotToken { get; set; } = null!;
public long AdminId { get; set; }
public bool EnableAutoOCR { get; set; } = false;
public bool EnableAutoASR { get; set; } = false;
//public string WorkDir { get; set; } = "/data/TelegramSearchBot";
public bool IsLocalAPI { get; set; } = false;
public bool EnableLocalBotAPI { get; set; } = false;
public string ExternalLocalBotApiBaseUrl { get; set; } = string.Empty;
public string TelegramBotApiId { get; set; } = null!;
public string TelegramBotApiHash { get; set; } = null!;
public int LocalBotApiPort { get; set; } = 8081;
public bool SameServer { get; set; } = false;
public int TaskDelayTimeout { get; set; } = 1000;
public string OllamaModelName { get; set; } = "qwen2.5:72b-instruct-q2_K";
public bool EnableVideoASR { get; set; } = false;
public bool EnableOpenAI { get; set; } = false;
public string OpenAIModelName { get; set; } = "gpt-4o";
public string OLTPAuth { get; set; } = null!;
public string OLTPAuthUrl { get; set; } = null!;
public string OLTPName { get; set; } = null!;
public string BraveApiKey { get; set; } = null!;
public bool EnableAccounting { get; set; } = false;
public int MaxToolCycles { get; set; } = 25;
public bool EnableLLMAgentProcess { get; set; } = false;
public int AgentHeartbeatIntervalSeconds { get; set; } = 10;
public int AgentHeartbeatTimeoutSeconds { get; set; } = 60;
public int AgentChunkPollingIntervalMilliseconds { get; set; } = 200;
public int AgentIdleTimeoutMinutes { get; set; } = 15;
public int MaxConcurrentAgents { get; set; } = 8;
public int AgentTaskTimeoutSeconds { get; set; } = 300;
public int AgentShutdownGracePeriodSeconds { get; set; } = 15;
public int AgentMaxRecoveryAttempts { get; set; } = 2;
public int AgentQueueBacklogWarningThreshold { get; set; } = 20;
public int AgentProcessMemoryLimitMb { get; set; } = 256;
}
public sealed record BotApiEndpointSettings(string BaseUrl, bool IsLocalApi, string ExternalLocalBotApiBaseUrl);
}