@grinev/opencode-telegram-bot 0.1.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/.env.example +34 -0
  2. package/LICENSE +21 -0
  3. package/README.md +72 -0
  4. package/dist/agent/manager.js +92 -0
  5. package/dist/agent/types.js +26 -0
  6. package/dist/app/start-bot-app.js +26 -0
  7. package/dist/bot/commands/agent.js +16 -0
  8. package/dist/bot/commands/definitions.js +20 -0
  9. package/dist/bot/commands/help.js +7 -0
  10. package/dist/bot/commands/model.js +16 -0
  11. package/dist/bot/commands/models.js +37 -0
  12. package/dist/bot/commands/new.js +58 -0
  13. package/dist/bot/commands/opencode-start.js +87 -0
  14. package/dist/bot/commands/opencode-stop.js +46 -0
  15. package/dist/bot/commands/projects.js +104 -0
  16. package/dist/bot/commands/server-restart.js +23 -0
  17. package/dist/bot/commands/server-start.js +23 -0
  18. package/dist/bot/commands/sessions.js +240 -0
  19. package/dist/bot/commands/start.js +40 -0
  20. package/dist/bot/commands/status.js +63 -0
  21. package/dist/bot/commands/stop.js +92 -0
  22. package/dist/bot/handlers/agent.js +96 -0
  23. package/dist/bot/handlers/context.js +112 -0
  24. package/dist/bot/handlers/model.js +115 -0
  25. package/dist/bot/handlers/permission.js +158 -0
  26. package/dist/bot/handlers/question.js +294 -0
  27. package/dist/bot/handlers/variant.js +126 -0
  28. package/dist/bot/index.js +573 -0
  29. package/dist/bot/middleware/auth.js +30 -0
  30. package/dist/bot/utils/keyboard.js +66 -0
  31. package/dist/cli/args.js +97 -0
  32. package/dist/cli.js +90 -0
  33. package/dist/config.js +46 -0
  34. package/dist/index.js +26 -0
  35. package/dist/keyboard/manager.js +171 -0
  36. package/dist/keyboard/types.js +1 -0
  37. package/dist/model/manager.js +123 -0
  38. package/dist/model/types.js +26 -0
  39. package/dist/opencode/client.js +13 -0
  40. package/dist/opencode/events.js +79 -0
  41. package/dist/opencode/server.js +104 -0
  42. package/dist/permission/manager.js +78 -0
  43. package/dist/permission/types.js +1 -0
  44. package/dist/pinned/manager.js +610 -0
  45. package/dist/pinned/types.js +1 -0
  46. package/dist/pinned-message/service.js +54 -0
  47. package/dist/process/manager.js +273 -0
  48. package/dist/process/types.js +1 -0
  49. package/dist/project/manager.js +28 -0
  50. package/dist/question/manager.js +143 -0
  51. package/dist/question/types.js +1 -0
  52. package/dist/runtime/bootstrap.js +278 -0
  53. package/dist/runtime/mode.js +74 -0
  54. package/dist/runtime/paths.js +37 -0
  55. package/dist/session/manager.js +10 -0
  56. package/dist/session/state.js +24 -0
  57. package/dist/settings/manager.js +99 -0
  58. package/dist/status/formatter.js +44 -0
  59. package/dist/summary/aggregator.js +427 -0
  60. package/dist/summary/formatter.js +226 -0
  61. package/dist/utils/formatting.js +237 -0
  62. package/dist/utils/logger.js +59 -0
  63. package/dist/utils/safe-background-task.js +33 -0
  64. package/dist/variant/manager.js +103 -0
  65. package/dist/variant/types.js +1 -0
  66. package/package.json +63 -0
@@ -0,0 +1,273 @@
1
+ import { spawn, exec } from "child_process";
2
+ import { promisify } from "util";
3
+ import { getServerProcess, setServerProcess, clearServerProcess } from "../settings/manager.js";
4
+ import { logger } from "../utils/logger.js";
5
+ const execAsync = promisify(exec);
6
+ /**
7
+ * Singleton manager for OpenCode server process
8
+ * Handles starting, stopping, and monitoring the server process
9
+ * Persists PID to settings.json for recovery after bot restart
10
+ */
11
+ class ProcessManager {
12
+ state = {
13
+ process: null,
14
+ pid: null,
15
+ startTime: null,
16
+ isRunning: false,
17
+ };
18
+ /**
19
+ * Initialize the manager by restoring state from settings
20
+ * Checks if the stored process is still alive
21
+ */
22
+ async initialize() {
23
+ const savedProcess = getServerProcess();
24
+ if (!savedProcess) {
25
+ logger.debug("[ProcessManager] No saved process found in settings");
26
+ return;
27
+ }
28
+ logger.info(`[ProcessManager] Found saved process: PID=${savedProcess.pid}`);
29
+ // Check if the process is still alive
30
+ if (this.isProcessAlive(savedProcess.pid)) {
31
+ logger.info(`[ProcessManager] Process PID=${savedProcess.pid} is still alive, restoring state`);
32
+ this.state = {
33
+ process: null, // Cannot recover ChildProcess reference
34
+ pid: savedProcess.pid,
35
+ startTime: new Date(savedProcess.startTime),
36
+ isRunning: true,
37
+ };
38
+ }
39
+ else {
40
+ logger.warn(`[ProcessManager] Process PID=${savedProcess.pid} is dead, cleaning up`);
41
+ clearServerProcess();
42
+ }
43
+ }
44
+ /**
45
+ * Start the OpenCode server process
46
+ */
47
+ async start() {
48
+ if (this.state.isRunning) {
49
+ return {
50
+ success: false,
51
+ error: "Process already running",
52
+ };
53
+ }
54
+ try {
55
+ logger.info("[ProcessManager] Starting OpenCode server process...");
56
+ const isWindows = process.platform === "win32";
57
+ const command = isWindows ? "cmd.exe" : "opencode";
58
+ const args = isWindows ? ["/c", "opencode", "serve"] : ["serve"];
59
+ // Spawn the process
60
+ // Windows: use cmd.exe to resolve npm-installed global commands
61
+ // Unix-like: run opencode directly
62
+ const childProcess = spawn(command, args, {
63
+ detached: false,
64
+ stdio: ["ignore", "pipe", "pipe"],
65
+ windowsHide: isWindows,
66
+ });
67
+ if (!childProcess.pid) {
68
+ throw new Error("Failed to start OpenCode server process. Ensure 'opencode' is installed and available in PATH.");
69
+ }
70
+ // Setup event handlers
71
+ childProcess.on("error", (err) => {
72
+ logger.error("[ProcessManager] Process error:", err);
73
+ this.cleanup();
74
+ });
75
+ childProcess.on("exit", (code, signal) => {
76
+ logger.info(`[ProcessManager] Process exited: code=${code}, signal=${signal}`);
77
+ this.cleanup();
78
+ });
79
+ // Log stdout/stderr
80
+ if (childProcess.stdout) {
81
+ childProcess.stdout.on("data", (data) => {
82
+ logger.debug(`[OpenCode Server] ${data.toString().trim()}`);
83
+ });
84
+ }
85
+ if (childProcess.stderr) {
86
+ childProcess.stderr.on("data", (data) => {
87
+ logger.warn(`[OpenCode Server Error] ${data.toString().trim()}`);
88
+ });
89
+ }
90
+ // Save state in memory
91
+ const startTime = new Date();
92
+ this.state = {
93
+ process: childProcess,
94
+ pid: childProcess.pid,
95
+ startTime,
96
+ isRunning: true,
97
+ };
98
+ // Persist to settings.json
99
+ setServerProcess({
100
+ pid: childProcess.pid,
101
+ startTime: startTime.toISOString(),
102
+ });
103
+ logger.info(`[ProcessManager] OpenCode server started with PID=${childProcess.pid}`);
104
+ return { success: true };
105
+ }
106
+ catch (err) {
107
+ const errorMessage = err instanceof Error ? err.message : String(err);
108
+ logger.error("[ProcessManager] Failed to start process:", err);
109
+ this.cleanup();
110
+ return { success: false, error: errorMessage };
111
+ }
112
+ }
113
+ /**
114
+ * Stop the OpenCode server process
115
+ * Sends SIGINT (Ctrl+C) and waits for graceful shutdown
116
+ * Falls back to SIGKILL if timeout is exceeded
117
+ */
118
+ async stop(timeoutMs = 5000) {
119
+ if (!this.state.isRunning || !this.state.pid) {
120
+ return {
121
+ success: false,
122
+ error: "Process not running",
123
+ };
124
+ }
125
+ try {
126
+ const pid = this.state.pid;
127
+ logger.info(`[ProcessManager] Stopping process PID=${pid}...`);
128
+ // On Windows, use taskkill to kill the entire process tree
129
+ // This is necessary because cmd.exe spawns child processes
130
+ if (process.platform === "win32") {
131
+ try {
132
+ // /F = force terminate, /T = terminate tree, /PID = process id
133
+ logger.debug(`[ProcessManager] Using taskkill to terminate process tree for PID=${pid}`);
134
+ await execAsync(`taskkill /F /T /PID ${pid}`);
135
+ logger.info(`[ProcessManager] Process tree terminated successfully for PID=${pid}`);
136
+ }
137
+ catch (err) {
138
+ // taskkill returns error if process not found, which is ok
139
+ const error = err;
140
+ if (error.message?.includes("not found")) {
141
+ logger.debug(`[ProcessManager] Process PID=${pid} already terminated`);
142
+ }
143
+ else {
144
+ logger.warn(`[ProcessManager] taskkill error for PID=${pid}:`, err);
145
+ }
146
+ }
147
+ // Wait a bit for cleanup
148
+ await new Promise((resolve) => setTimeout(resolve, 1000));
149
+ }
150
+ else {
151
+ // Unix-like systems: use SIGINT/SIGKILL
152
+ if (this.state.process) {
153
+ const childProcess = this.state.process;
154
+ // Send SIGINT (Ctrl+C)
155
+ logger.debug(`[ProcessManager] Sending SIGINT to PID=${pid}`);
156
+ childProcess.kill("SIGINT");
157
+ // Wait for graceful shutdown
158
+ const gracefulExit = await this.waitForProcessExit(childProcess, timeoutMs);
159
+ if (!gracefulExit && this.state.isRunning) {
160
+ logger.warn(`[ProcessManager] Graceful shutdown failed, sending SIGKILL to PID=${pid}`);
161
+ childProcess.kill("SIGKILL");
162
+ await new Promise((resolve) => setTimeout(resolve, 2000));
163
+ }
164
+ }
165
+ else {
166
+ // No ChildProcess reference (recovered from settings)
167
+ logger.debug(`[ProcessManager] Sending SIGTERM to PID=${pid}`);
168
+ try {
169
+ process.kill(pid, "SIGTERM");
170
+ }
171
+ catch (err) {
172
+ logger.debug(`[ProcessManager] Failed to send SIGTERM to PID=${pid}:`, err);
173
+ }
174
+ // Wait for process to die
175
+ await new Promise((resolve) => setTimeout(resolve, timeoutMs));
176
+ // Check if still alive
177
+ if (this.isProcessAlive(pid)) {
178
+ logger.warn(`[ProcessManager] Graceful shutdown failed, sending SIGKILL to PID=${pid}`);
179
+ try {
180
+ process.kill(pid, "SIGKILL");
181
+ }
182
+ catch (err) {
183
+ logger.error(`[ProcessManager] Failed to send SIGKILL to PID=${pid}:`, err);
184
+ }
185
+ await new Promise((resolve) => setTimeout(resolve, 2000));
186
+ }
187
+ }
188
+ }
189
+ this.cleanup();
190
+ logger.info(`[ProcessManager] Process PID=${pid} stopped successfully`);
191
+ return { success: true };
192
+ }
193
+ catch (err) {
194
+ const errorMessage = err instanceof Error ? err.message : String(err);
195
+ logger.error("[ProcessManager] Failed to stop process:", err);
196
+ return { success: false, error: errorMessage };
197
+ }
198
+ }
199
+ /**
200
+ * Check if the process is running
201
+ * Validates that the process with stored PID is actually alive
202
+ */
203
+ isRunning() {
204
+ if (!this.state.isRunning || !this.state.pid) {
205
+ return false;
206
+ }
207
+ // Verify that the process is actually alive
208
+ if (!this.isProcessAlive(this.state.pid)) {
209
+ logger.warn(`[ProcessManager] Process PID=${this.state.pid} appears dead, cleaning up`);
210
+ this.cleanup();
211
+ return false;
212
+ }
213
+ return true;
214
+ }
215
+ /**
216
+ * Get the process ID of the running server
217
+ */
218
+ getPID() {
219
+ return this.state.pid;
220
+ }
221
+ /**
222
+ * Get the uptime of the server in milliseconds
223
+ */
224
+ getUptime() {
225
+ if (!this.state.startTime || !this.state.isRunning) {
226
+ return null;
227
+ }
228
+ return Date.now() - this.state.startTime.getTime();
229
+ }
230
+ /**
231
+ * Check if a process with given PID is alive
232
+ * Uses process.kill(pid, 0) which checks existence without killing
233
+ */
234
+ isProcessAlive(pid) {
235
+ try {
236
+ process.kill(pid, 0);
237
+ return true;
238
+ }
239
+ catch {
240
+ return false;
241
+ }
242
+ }
243
+ /**
244
+ * Wait for process to exit
245
+ */
246
+ async waitForProcessExit(childProcess, timeoutMs) {
247
+ return new Promise((resolve) => {
248
+ const exitHandler = () => {
249
+ logger.debug("[ProcessManager] Process exited gracefully");
250
+ resolve(true);
251
+ };
252
+ childProcess.once("exit", exitHandler);
253
+ setTimeout(() => {
254
+ childProcess.removeListener("exit", exitHandler);
255
+ resolve(false);
256
+ }, timeoutMs);
257
+ });
258
+ }
259
+ /**
260
+ * Clean up state and settings
261
+ */
262
+ cleanup() {
263
+ this.state = {
264
+ process: null,
265
+ pid: null,
266
+ startTime: null,
267
+ isRunning: false,
268
+ };
269
+ clearServerProcess();
270
+ }
271
+ }
272
+ // Export singleton instance
273
+ export const processManager = new ProcessManager();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ import { opencodeClient } from "../opencode/client.js";
2
+ export async function getProjects() {
3
+ const { data: projects, error } = await opencodeClient.project.list();
4
+ if (error || !projects) {
5
+ throw error || new Error("No data received from server");
6
+ }
7
+ return projects.map((project) => ({
8
+ id: project.id,
9
+ worktree: project.worktree,
10
+ name: project.name || project.worktree,
11
+ }));
12
+ }
13
+ export async function getProjectById(id) {
14
+ const projects = await getProjects();
15
+ const project = projects.find((p) => p.id === id);
16
+ if (!project) {
17
+ throw new Error(`Project with id ${id} not found`);
18
+ }
19
+ return project;
20
+ }
21
+ export async function getProjectByWorktree(worktree) {
22
+ const projects = await getProjects();
23
+ const project = projects.find((p) => p.worktree === worktree);
24
+ if (!project) {
25
+ throw new Error(`Project with worktree ${worktree} not found`);
26
+ }
27
+ return project;
28
+ }
@@ -0,0 +1,143 @@
1
+ import { logger } from "../utils/logger.js";
2
+ class QuestionManager {
3
+ state = {
4
+ questions: [],
5
+ currentIndex: 0,
6
+ selectedOptions: new Map(),
7
+ customAnswers: new Map(),
8
+ messageIds: [],
9
+ isActive: false,
10
+ requestID: null,
11
+ };
12
+ startQuestions(questions, requestID) {
13
+ logger.debug(`[QuestionManager] startQuestions called: isActive=${this.state.isActive}, currentQuestions=${this.state.questions.length}, newQuestions=${questions.length}, requestID=${requestID}`);
14
+ if (this.state.isActive) {
15
+ logger.info(`[QuestionManager] Poll already active! Forcing reset before starting new poll.`);
16
+ // Принудительно сбрасываем старый опрос чтобы начать новый
17
+ this.clear();
18
+ }
19
+ logger.info(`[QuestionManager] Starting new poll with ${questions.length} questions, requestID=${requestID}`);
20
+ this.state = {
21
+ questions,
22
+ currentIndex: 0,
23
+ selectedOptions: new Map(),
24
+ customAnswers: new Map(),
25
+ messageIds: [],
26
+ isActive: true,
27
+ requestID,
28
+ };
29
+ }
30
+ getRequestID() {
31
+ return this.state.requestID;
32
+ }
33
+ getCurrentQuestion() {
34
+ if (this.state.currentIndex >= this.state.questions.length) {
35
+ return null;
36
+ }
37
+ return this.state.questions[this.state.currentIndex];
38
+ }
39
+ selectOption(questionIndex, optionIndex) {
40
+ if (!this.state.isActive) {
41
+ return;
42
+ }
43
+ const question = this.state.questions[questionIndex];
44
+ if (!question) {
45
+ return;
46
+ }
47
+ const selected = this.state.selectedOptions.get(questionIndex) || new Set();
48
+ if (question.multiple) {
49
+ if (selected.has(optionIndex)) {
50
+ selected.delete(optionIndex);
51
+ }
52
+ else {
53
+ selected.add(optionIndex);
54
+ }
55
+ }
56
+ else {
57
+ selected.clear();
58
+ selected.add(optionIndex);
59
+ }
60
+ this.state.selectedOptions.set(questionIndex, selected);
61
+ logger.debug(`[QuestionManager] Selected options for question ${questionIndex}: ${Array.from(selected).join(", ")}`);
62
+ }
63
+ getSelectedOptions(questionIndex) {
64
+ return this.state.selectedOptions.get(questionIndex) || new Set();
65
+ }
66
+ getSelectedAnswer(questionIndex) {
67
+ const question = this.state.questions[questionIndex];
68
+ if (!question) {
69
+ return "";
70
+ }
71
+ const selected = this.state.selectedOptions.get(questionIndex) || new Set();
72
+ const options = Array.from(selected)
73
+ .map((idx) => question.options[idx])
74
+ .filter((opt) => opt)
75
+ .map((opt) => `* ${opt.label}: ${opt.description}`);
76
+ return options.join("\n");
77
+ }
78
+ setCustomAnswer(questionIndex, answer) {
79
+ logger.debug(`[QuestionManager] Custom answer received for question ${questionIndex}: ${answer}`);
80
+ this.state.customAnswers.set(questionIndex, answer);
81
+ }
82
+ getCustomAnswer(questionIndex) {
83
+ return this.state.customAnswers.get(questionIndex);
84
+ }
85
+ hasCustomAnswer(questionIndex) {
86
+ return this.state.customAnswers.has(questionIndex);
87
+ }
88
+ nextQuestion() {
89
+ this.state.currentIndex++;
90
+ logger.debug(`[QuestionManager] Moving to next question: ${this.state.currentIndex}/${this.state.questions.length}`);
91
+ }
92
+ hasNextQuestion() {
93
+ return this.state.currentIndex < this.state.questions.length;
94
+ }
95
+ getCurrentIndex() {
96
+ return this.state.currentIndex;
97
+ }
98
+ getTotalQuestions() {
99
+ return this.state.questions.length;
100
+ }
101
+ addMessageId(messageId) {
102
+ this.state.messageIds.push(messageId);
103
+ }
104
+ getMessageIds() {
105
+ return [...this.state.messageIds];
106
+ }
107
+ isActive() {
108
+ logger.debug(`[QuestionManager] isActive check: ${this.state.isActive}, questions=${this.state.questions.length}, currentIndex=${this.state.currentIndex}`);
109
+ return this.state.isActive;
110
+ }
111
+ cancel() {
112
+ logger.info("[QuestionManager] Poll cancelled");
113
+ this.state.isActive = false;
114
+ }
115
+ clear() {
116
+ this.state = {
117
+ questions: [],
118
+ currentIndex: 0,
119
+ selectedOptions: new Map(),
120
+ customAnswers: new Map(),
121
+ messageIds: [],
122
+ isActive: false,
123
+ requestID: null,
124
+ };
125
+ }
126
+ getAllAnswers() {
127
+ const answers = [];
128
+ for (let i = 0; i < this.state.questions.length; i++) {
129
+ const question = this.state.questions[i];
130
+ const selectedAnswer = this.getSelectedAnswer(i);
131
+ const customAnswer = this.getCustomAnswer(i);
132
+ const finalAnswer = customAnswer || selectedAnswer;
133
+ if (finalAnswer) {
134
+ answers.push({
135
+ question: question.question,
136
+ answer: finalAnswer,
137
+ });
138
+ }
139
+ }
140
+ return answers;
141
+ }
142
+ }
143
+ export const questionManager = new QuestionManager();
@@ -0,0 +1 @@
1
+ export {};