forked from grinev/opencode-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
127 lines (108 loc) · 3.97 KB
/
config.ts
File metadata and controls
127 lines (108 loc) · 3.97 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
import dotenv from "dotenv";
import { getRuntimePaths } from "./runtime/paths.js";
import { normalizeLocale, type Locale } from "./i18n/index.js";
const runtimePaths = getRuntimePaths();
dotenv.config({ path: runtimePaths.envFilePath, quiet: true });
export type MessageFormatMode = "raw" | "markdown";
function getEnvVar(key: string, required: boolean = true): string {
const value = process.env[key];
if (required && !value) {
throw new Error(
`Missing required environment variable: ${key} (expected in ${runtimePaths.envFilePath})`,
);
}
return value || "";
}
function getOptionalPositiveIntEnvVar(key: string, defaultValue: number): number {
const value = getEnvVar(key, false);
if (!value) {
return defaultValue;
}
const parsedValue = Number.parseInt(value, 10);
if (Number.isNaN(parsedValue) || parsedValue <= 0) {
return defaultValue;
}
return parsedValue;
}
function getOptionalLocaleEnvVar(key: string, defaultValue: Locale): Locale {
const value = getEnvVar(key, false);
return normalizeLocale(value, defaultValue);
}
function getOptionalBooleanEnvVar(key: string, defaultValue: boolean): boolean {
const value = getEnvVar(key, false);
if (!value) {
return defaultValue;
}
const normalized = value.trim().toLowerCase();
if (["1", "true", "yes", "on"].includes(normalized)) {
return true;
}
if (["0", "false", "no", "off"].includes(normalized)) {
return false;
}
return defaultValue;
}
function getOptionalMessageFormatModeEnvVar(
key: string,
defaultValue: MessageFormatMode,
): MessageFormatMode {
const value = getEnvVar(key, false);
if (!value) {
return defaultValue;
}
const normalized = value.trim().toLowerCase();
if (normalized === "raw" || normalized === "markdown") {
return normalized;
}
return defaultValue;
}
export const config = {
telegram: {
token: getEnvVar("TELEGRAM_BOT_TOKEN"),
allowedUserId: parseInt(getEnvVar("TELEGRAM_ALLOWED_USER_ID"), 10),
proxyUrl: getEnvVar("TELEGRAM_PROXY_URL", false),
},
opencode: {
apiUrl: getEnvVar("OPENCODE_API_URL", false) || "http://localhost:4096",
username: getEnvVar("OPENCODE_SERVER_USERNAME", false) || "opencode",
password: getEnvVar("OPENCODE_SERVER_PASSWORD", false),
model: {
provider: getEnvVar("OPENCODE_MODEL_PROVIDER", true), // Required
modelId: getEnvVar("OPENCODE_MODEL_ID", true), // Required
},
},
server: {
logLevel: getEnvVar("LOG_LEVEL", false) || "info",
},
bot: {
sessionsListLimit: getOptionalPositiveIntEnvVar("SESSIONS_LIST_LIMIT", 10),
projectsListLimit: getOptionalPositiveIntEnvVar("PROJECTS_LIST_LIMIT", 10),
commandsListLimit: getOptionalPositiveIntEnvVar("COMMANDS_LIST_LIMIT", 10),
taskLimit: getOptionalPositiveIntEnvVar("TASK_LIMIT", 10),
responseStreamThrottleMs: getOptionalPositiveIntEnvVar("RESPONSE_STREAM_THROTTLE_MS", 500),
bashToolDisplayMaxLength: getOptionalPositiveIntEnvVar("BASH_TOOL_DISPLAY_MAX_LENGTH", 128),
locale: getOptionalLocaleEnvVar("BOT_LOCALE", "en"),
hideThinkingMessages: getOptionalBooleanEnvVar("HIDE_THINKING_MESSAGES", false),
hideToolCallMessages: getOptionalBooleanEnvVar("HIDE_TOOL_CALL_MESSAGES", false),
hideToolFileMessages: getOptionalBooleanEnvVar("HIDE_TOOL_FILE_MESSAGES", false),
messageFormatMode: getOptionalMessageFormatModeEnvVar("MESSAGE_FORMAT_MODE", "markdown"),
},
files: {
maxFileSizeKb: parseInt(getEnvVar("CODE_FILE_MAX_SIZE_KB", false) || "100", 10),
},
open: {
browserRoots: getEnvVar("OPEN_BROWSER_ROOTS", false),
},
stt: {
apiUrl: getEnvVar("STT_API_URL", false),
apiKey: getEnvVar("STT_API_KEY", false),
model: getEnvVar("STT_MODEL", false) || "whisper-large-v3-turbo",
language: getEnvVar("STT_LANGUAGE", false),
},
tts: {
apiUrl: getEnvVar("TTS_API_URL", false),
apiKey: getEnvVar("TTS_API_KEY", false),
model: getEnvVar("TTS_MODEL", false) || "gpt-4o-mini-tts",
voice: getEnvVar("TTS_VOICE", false) || "alloy",
},
};