opencode-telegram-group-topics-bot 0.11.2

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 (101) hide show
  1. package/.env.example +74 -0
  2. package/LICENSE +21 -0
  3. package/README.md +305 -0
  4. package/dist/agent/manager.js +60 -0
  5. package/dist/agent/types.js +26 -0
  6. package/dist/app/start-bot-app.js +47 -0
  7. package/dist/bot/commands/abort.js +116 -0
  8. package/dist/bot/commands/commands.js +389 -0
  9. package/dist/bot/commands/constants.js +20 -0
  10. package/dist/bot/commands/definitions.js +25 -0
  11. package/dist/bot/commands/help.js +27 -0
  12. package/dist/bot/commands/models.js +38 -0
  13. package/dist/bot/commands/new.js +247 -0
  14. package/dist/bot/commands/opencode-start.js +85 -0
  15. package/dist/bot/commands/opencode-stop.js +44 -0
  16. package/dist/bot/commands/projects.js +304 -0
  17. package/dist/bot/commands/rename.js +173 -0
  18. package/dist/bot/commands/sessions.js +491 -0
  19. package/dist/bot/commands/start.js +67 -0
  20. package/dist/bot/commands/status.js +138 -0
  21. package/dist/bot/constants.js +49 -0
  22. package/dist/bot/handlers/agent.js +127 -0
  23. package/dist/bot/handlers/context.js +125 -0
  24. package/dist/bot/handlers/document.js +65 -0
  25. package/dist/bot/handlers/inline-menu.js +124 -0
  26. package/dist/bot/handlers/model.js +152 -0
  27. package/dist/bot/handlers/permission.js +281 -0
  28. package/dist/bot/handlers/prompt.js +263 -0
  29. package/dist/bot/handlers/question.js +285 -0
  30. package/dist/bot/handlers/variant.js +147 -0
  31. package/dist/bot/handlers/voice.js +173 -0
  32. package/dist/bot/index.js +945 -0
  33. package/dist/bot/message-patterns.js +4 -0
  34. package/dist/bot/middleware/auth.js +30 -0
  35. package/dist/bot/middleware/interaction-guard.js +80 -0
  36. package/dist/bot/middleware/unknown-command.js +22 -0
  37. package/dist/bot/scope.js +222 -0
  38. package/dist/bot/telegram-constants.js +3 -0
  39. package/dist/bot/telegram-rate-limiter.js +263 -0
  40. package/dist/bot/utils/commands.js +21 -0
  41. package/dist/bot/utils/file-download.js +91 -0
  42. package/dist/bot/utils/keyboard.js +85 -0
  43. package/dist/bot/utils/send-with-markdown-fallback.js +57 -0
  44. package/dist/bot/utils/session-error-filter.js +34 -0
  45. package/dist/bot/utils/topic-link.js +29 -0
  46. package/dist/cli/args.js +98 -0
  47. package/dist/cli.js +80 -0
  48. package/dist/config.js +103 -0
  49. package/dist/i18n/de.js +330 -0
  50. package/dist/i18n/en.js +330 -0
  51. package/dist/i18n/es.js +330 -0
  52. package/dist/i18n/index.js +102 -0
  53. package/dist/i18n/ru.js +330 -0
  54. package/dist/i18n/zh.js +330 -0
  55. package/dist/index.js +28 -0
  56. package/dist/interaction/cleanup.js +24 -0
  57. package/dist/interaction/constants.js +25 -0
  58. package/dist/interaction/guard.js +100 -0
  59. package/dist/interaction/manager.js +113 -0
  60. package/dist/interaction/types.js +1 -0
  61. package/dist/keyboard/manager.js +115 -0
  62. package/dist/keyboard/types.js +1 -0
  63. package/dist/model/capabilities.js +62 -0
  64. package/dist/model/manager.js +257 -0
  65. package/dist/model/types.js +24 -0
  66. package/dist/opencode/client.js +13 -0
  67. package/dist/opencode/events.js +159 -0
  68. package/dist/opencode/prompt-submit-error.js +101 -0
  69. package/dist/permission/manager.js +92 -0
  70. package/dist/permission/types.js +1 -0
  71. package/dist/pinned/manager.js +405 -0
  72. package/dist/pinned/types.js +1 -0
  73. package/dist/process/manager.js +273 -0
  74. package/dist/process/types.js +1 -0
  75. package/dist/project/manager.js +88 -0
  76. package/dist/question/manager.js +186 -0
  77. package/dist/question/types.js +1 -0
  78. package/dist/rename/manager.js +64 -0
  79. package/dist/runtime/bootstrap.js +350 -0
  80. package/dist/runtime/mode.js +74 -0
  81. package/dist/runtime/paths.js +37 -0
  82. package/dist/runtime/process-error-handlers.js +24 -0
  83. package/dist/session/cache-manager.js +455 -0
  84. package/dist/session/manager.js +87 -0
  85. package/dist/settings/manager.js +283 -0
  86. package/dist/stt/client.js +64 -0
  87. package/dist/summary/aggregator.js +625 -0
  88. package/dist/summary/formatter.js +417 -0
  89. package/dist/summary/tool-message-batcher.js +277 -0
  90. package/dist/topic/colors.js +8 -0
  91. package/dist/topic/constants.js +10 -0
  92. package/dist/topic/manager.js +161 -0
  93. package/dist/topic/title-constants.js +2 -0
  94. package/dist/topic/title-format.js +10 -0
  95. package/dist/topic/title-sync.js +17 -0
  96. package/dist/utils/error-format.js +29 -0
  97. package/dist/utils/logger.js +175 -0
  98. package/dist/utils/safe-background-task.js +33 -0
  99. package/dist/variant/manager.js +103 -0
  100. package/dist/variant/types.js +1 -0
  101. package/package.json +76 -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,88 @@
1
+ import { readFile, stat } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { opencodeClient } from "../opencode/client.js";
4
+ import { getCachedSessionProjects } from "../session/cache-manager.js";
5
+ import { logger } from "../utils/logger.js";
6
+ async function isLinkedGitWorktree(worktree) {
7
+ if (worktree === "/") {
8
+ return false;
9
+ }
10
+ const gitPath = path.join(worktree, ".git");
11
+ try {
12
+ const gitStat = await stat(gitPath);
13
+ if (!gitStat.isFile()) {
14
+ return false;
15
+ }
16
+ const gitPointer = (await readFile(gitPath, "utf-8")).trim();
17
+ const match = gitPointer.match(/^gitdir:\s*(.+)$/i);
18
+ if (!match) {
19
+ return false;
20
+ }
21
+ const gitDir = path.resolve(worktree, match[1].trim()).replace(/\\/g, "/").toLowerCase();
22
+ return gitDir.includes("/.git/worktrees/");
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ }
28
+ function worktreeKey(worktree) {
29
+ if (process.platform === "win32") {
30
+ return worktree.toLowerCase();
31
+ }
32
+ return worktree;
33
+ }
34
+ export async function getProjects() {
35
+ const { data: projects, error } = await opencodeClient.project.list();
36
+ if (error || !projects) {
37
+ throw error || new Error("No data received from server");
38
+ }
39
+ const apiProjects = projects.map((project) => ({
40
+ id: project.id,
41
+ worktree: project.worktree,
42
+ name: project.name || project.worktree,
43
+ lastUpdated: project.time?.updated ?? 0,
44
+ }));
45
+ const cachedProjects = await getCachedSessionProjects();
46
+ const mergedByWorktree = new Map();
47
+ for (const apiProject of apiProjects) {
48
+ mergedByWorktree.set(worktreeKey(apiProject.worktree), apiProject);
49
+ }
50
+ for (const cachedProject of cachedProjects) {
51
+ const key = worktreeKey(cachedProject.worktree);
52
+ const existing = mergedByWorktree.get(key);
53
+ if (existing) {
54
+ if ((cachedProject.lastUpdated ?? 0) > existing.lastUpdated) {
55
+ existing.lastUpdated = cachedProject.lastUpdated;
56
+ }
57
+ continue;
58
+ }
59
+ mergedByWorktree.set(key, {
60
+ id: cachedProject.id,
61
+ worktree: cachedProject.worktree,
62
+ name: cachedProject.name,
63
+ lastUpdated: cachedProject.lastUpdated ?? 0,
64
+ });
65
+ }
66
+ const projectList = Array.from(mergedByWorktree.values()).sort((left, right) => right.lastUpdated - left.lastUpdated);
67
+ const linkedWorktreeFlags = await Promise.all(projectList.map((project) => isLinkedGitWorktree(project.worktree)));
68
+ const visibleProjects = projectList.filter((_, index) => !linkedWorktreeFlags[index]);
69
+ const hiddenLinkedWorktrees = projectList.length - visibleProjects.length;
70
+ logger.debug(`[ProjectManager] Projects resolved: api=${projects.length}, cached=${cachedProjects.length}, hiddenLinkedWorktrees=${hiddenLinkedWorktrees}, total=${visibleProjects.length}`);
71
+ return visibleProjects.map(({ id, worktree, name }) => ({ id, worktree, name }));
72
+ }
73
+ export async function getProjectById(id) {
74
+ const projects = await getProjects();
75
+ const project = projects.find((p) => p.id === id);
76
+ if (!project) {
77
+ throw new Error(`Project with id ${id} not found`);
78
+ }
79
+ return project;
80
+ }
81
+ export async function getProjectByWorktree(worktree) {
82
+ const projects = await getProjects();
83
+ const project = projects.find((p) => p.worktree === worktree);
84
+ if (!project) {
85
+ throw new Error(`Project with worktree ${worktree} not found`);
86
+ }
87
+ return project;
88
+ }
@@ -0,0 +1,186 @@
1
+ import { logger } from "../utils/logger.js";
2
+ class QuestionManager {
3
+ stateByScope = new Map();
4
+ createDefaultState() {
5
+ return {
6
+ questions: [],
7
+ currentIndex: 0,
8
+ selectedOptions: new Map(),
9
+ customAnswers: new Map(),
10
+ customInputQuestionIndex: null,
11
+ activeMessageId: null,
12
+ messageIds: [],
13
+ isActive: false,
14
+ requestID: null,
15
+ };
16
+ }
17
+ getState(scopeKey) {
18
+ const state = this.stateByScope.get(scopeKey);
19
+ if (state) {
20
+ return state;
21
+ }
22
+ const next = this.createDefaultState();
23
+ this.stateByScope.set(scopeKey, next);
24
+ return next;
25
+ }
26
+ startQuestions(questions, requestID, scopeKey = "global") {
27
+ const state = this.getState(scopeKey);
28
+ logger.debug(`[QuestionManager] startQuestions called: isActive=${state.isActive}, currentQuestions=${state.questions.length}, newQuestions=${questions.length}, requestID=${requestID}`);
29
+ if (state.isActive) {
30
+ logger.info(`[QuestionManager] Poll already active! Forcing reset before starting new poll.`);
31
+ this.clear(scopeKey);
32
+ }
33
+ logger.info(`[QuestionManager] Starting new poll with ${questions.length} questions, requestID=${requestID}`);
34
+ this.stateByScope.set(scopeKey, {
35
+ questions,
36
+ currentIndex: 0,
37
+ selectedOptions: new Map(),
38
+ customAnswers: new Map(),
39
+ customInputQuestionIndex: null,
40
+ activeMessageId: null,
41
+ messageIds: [],
42
+ isActive: true,
43
+ requestID,
44
+ });
45
+ }
46
+ getRequestID(scopeKey = "global") {
47
+ return this.getState(scopeKey).requestID;
48
+ }
49
+ getCurrentQuestion(scopeKey = "global") {
50
+ const state = this.getState(scopeKey);
51
+ if (state.currentIndex >= state.questions.length) {
52
+ return null;
53
+ }
54
+ return state.questions[state.currentIndex];
55
+ }
56
+ selectOption(questionIndex, optionIndex, scopeKey = "global") {
57
+ const state = this.getState(scopeKey);
58
+ if (!state.isActive) {
59
+ return;
60
+ }
61
+ const question = state.questions[questionIndex];
62
+ if (!question) {
63
+ return;
64
+ }
65
+ const selected = state.selectedOptions.get(questionIndex) || new Set();
66
+ if (question.multiple) {
67
+ if (selected.has(optionIndex)) {
68
+ selected.delete(optionIndex);
69
+ }
70
+ else {
71
+ selected.add(optionIndex);
72
+ }
73
+ }
74
+ else {
75
+ selected.clear();
76
+ selected.add(optionIndex);
77
+ }
78
+ state.selectedOptions.set(questionIndex, selected);
79
+ logger.debug(`[QuestionManager] Selected options for question ${questionIndex}: ${Array.from(selected).join(", ")}`);
80
+ }
81
+ getSelectedOptions(questionIndex, scopeKey = "global") {
82
+ return this.getState(scopeKey).selectedOptions.get(questionIndex) || new Set();
83
+ }
84
+ getSelectedAnswer(questionIndex, scopeKey = "global") {
85
+ const state = this.getState(scopeKey);
86
+ const question = state.questions[questionIndex];
87
+ if (!question) {
88
+ return "";
89
+ }
90
+ const selected = state.selectedOptions.get(questionIndex) || new Set();
91
+ const options = Array.from(selected)
92
+ .map((idx) => question.options[idx])
93
+ .filter((opt) => opt)
94
+ .map((opt) => `* ${opt.label}: ${opt.description}`);
95
+ return options.join("\n");
96
+ }
97
+ setCustomAnswer(questionIndex, answer, scopeKey = "global") {
98
+ logger.debug(`[QuestionManager] Custom answer received for question ${questionIndex}: ${answer}`);
99
+ this.getState(scopeKey).customAnswers.set(questionIndex, answer);
100
+ }
101
+ getCustomAnswer(questionIndex, scopeKey = "global") {
102
+ return this.getState(scopeKey).customAnswers.get(questionIndex);
103
+ }
104
+ hasCustomAnswer(questionIndex, scopeKey = "global") {
105
+ return this.getState(scopeKey).customAnswers.has(questionIndex);
106
+ }
107
+ nextQuestion(scopeKey = "global") {
108
+ const state = this.getState(scopeKey);
109
+ state.currentIndex++;
110
+ state.customInputQuestionIndex = null;
111
+ state.activeMessageId = null;
112
+ logger.debug(`[QuestionManager] Moving to next question: ${state.currentIndex}/${state.questions.length}`);
113
+ }
114
+ hasNextQuestion(scopeKey = "global") {
115
+ const state = this.getState(scopeKey);
116
+ return state.currentIndex < state.questions.length;
117
+ }
118
+ getCurrentIndex(scopeKey = "global") {
119
+ return this.getState(scopeKey).currentIndex;
120
+ }
121
+ getTotalQuestions(scopeKey = "global") {
122
+ return this.getState(scopeKey).questions.length;
123
+ }
124
+ addMessageId(messageId, scopeKey = "global") {
125
+ this.getState(scopeKey).messageIds.push(messageId);
126
+ }
127
+ setActiveMessageId(messageId, scopeKey = "global") {
128
+ this.getState(scopeKey).activeMessageId = messageId;
129
+ }
130
+ getActiveMessageId(scopeKey = "global") {
131
+ return this.getState(scopeKey).activeMessageId;
132
+ }
133
+ isActiveMessage(messageId, scopeKey = "global") {
134
+ const state = this.getState(scopeKey);
135
+ return state.isActive && state.activeMessageId !== null && messageId === state.activeMessageId;
136
+ }
137
+ startCustomInput(questionIndex, scopeKey = "global") {
138
+ const state = this.getState(scopeKey);
139
+ if (!state.isActive || !state.questions[questionIndex]) {
140
+ return;
141
+ }
142
+ state.customInputQuestionIndex = questionIndex;
143
+ }
144
+ clearCustomInput(scopeKey = "global") {
145
+ this.getState(scopeKey).customInputQuestionIndex = null;
146
+ }
147
+ isWaitingForCustomInput(questionIndex, scopeKey = "global") {
148
+ return this.getState(scopeKey).customInputQuestionIndex === questionIndex;
149
+ }
150
+ getMessageIds(scopeKey = "global") {
151
+ return [...this.getState(scopeKey).messageIds];
152
+ }
153
+ isActive(scopeKey = "global") {
154
+ const state = this.getState(scopeKey);
155
+ logger.debug(`[QuestionManager] isActive check: ${state.isActive}, questions=${state.questions.length}, currentIndex=${state.currentIndex}`);
156
+ return state.isActive;
157
+ }
158
+ cancel(scopeKey = "global") {
159
+ const state = this.getState(scopeKey);
160
+ logger.info("[QuestionManager] Poll cancelled");
161
+ state.isActive = false;
162
+ state.customInputQuestionIndex = null;
163
+ state.activeMessageId = null;
164
+ }
165
+ clear(scopeKey = "global") {
166
+ this.stateByScope.set(scopeKey, this.createDefaultState());
167
+ }
168
+ getAllAnswers(scopeKey = "global") {
169
+ const state = this.getState(scopeKey);
170
+ const answers = [];
171
+ for (let i = 0; i < state.questions.length; i++) {
172
+ const question = state.questions[i];
173
+ const selectedAnswer = this.getSelectedAnswer(i, scopeKey);
174
+ const customAnswer = this.getCustomAnswer(i, scopeKey);
175
+ const finalAnswer = customAnswer || selectedAnswer;
176
+ if (finalAnswer) {
177
+ answers.push({
178
+ question: question.question,
179
+ answer: finalAnswer,
180
+ });
181
+ }
182
+ }
183
+ return answers;
184
+ }
185
+ }
186
+ export const questionManager = new QuestionManager();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ import { logger } from "../utils/logger.js";
2
+ class RenameManager {
3
+ stateByScope = new Map();
4
+ getState(scopeKey) {
5
+ const state = this.stateByScope.get(scopeKey);
6
+ if (state) {
7
+ return state;
8
+ }
9
+ const nextState = {
10
+ isWaiting: false,
11
+ sessionId: null,
12
+ sessionDirectory: null,
13
+ currentTitle: null,
14
+ messageId: null,
15
+ };
16
+ this.stateByScope.set(scopeKey, nextState);
17
+ return nextState;
18
+ }
19
+ startWaiting(sessionId, directory, currentTitle, scopeKey = "global") {
20
+ logger.info(`[RenameManager] Starting rename flow for session: ${sessionId}`);
21
+ this.stateByScope.set(scopeKey, {
22
+ isWaiting: true,
23
+ sessionId,
24
+ sessionDirectory: directory,
25
+ currentTitle,
26
+ messageId: null,
27
+ });
28
+ }
29
+ setMessageId(messageId, scopeKey = "global") {
30
+ this.getState(scopeKey).messageId = messageId;
31
+ }
32
+ getMessageId(scopeKey = "global") {
33
+ return this.getState(scopeKey).messageId;
34
+ }
35
+ isActiveMessage(messageId, scopeKey = "global") {
36
+ const state = this.getState(scopeKey);
37
+ return state.isWaiting && state.messageId !== null && state.messageId === messageId;
38
+ }
39
+ isWaitingForName(scopeKey = "global") {
40
+ return this.getState(scopeKey).isWaiting;
41
+ }
42
+ getSessionInfo(scopeKey = "global") {
43
+ const state = this.getState(scopeKey);
44
+ if (!state.isWaiting || !state.sessionId) {
45
+ return null;
46
+ }
47
+ return {
48
+ sessionId: state.sessionId,
49
+ directory: state.sessionDirectory,
50
+ currentTitle: state.currentTitle,
51
+ };
52
+ }
53
+ clear(scopeKey = "global") {
54
+ logger.debug("[RenameManager] Clearing rename state");
55
+ this.stateByScope.set(scopeKey, {
56
+ isWaiting: false,
57
+ sessionId: null,
58
+ sessionDirectory: null,
59
+ currentTitle: null,
60
+ messageId: null,
61
+ });
62
+ }
63
+ }
64
+ export const renameManager = new RenameManager();