Personal Telegram developer console for GitHub

feat: inject owner-scoped workspace store into bot

davoodmehraban89 committed Oct 3, 2026 at 12:47 UTC 54b73808f6f78a9338c72e8fdaa7c3ed2904363f
1 file changed +46 -206
src/bot.js
+46 -206
@@ -5,6 +5,7 @@ import { PluginRegistry } from './core/plugin-registry.js';
5 import { ActionPolicy } from './core/action-policy.js';
6 import { ApprovalService } from './core/approval.js';
7 import { DurableApprovalReplayStore } from './core/durable-replay-store.js';
8 +import { DurableWorkspaceStoreClient } from './core/workspace-store.js';
9 import { AuditLogger } from './core/audit.js';
10 import { PluginRouter } from './core/router.js';
11 import { createGitHubPlugin } from './plugins/github/plugin.js';
@@ -16,12 +17,8 @@ function approvalSourceCallback(payload = {}) {
17 throw new Error('Approval input is invalid');
18 }
19
19 - if (payload.actionId === 'github.workflow.run.rerun_failed') {
20 - return `rerun:${repoId}:${runId}`;
21 - }
22 - if (payload.actionId === 'github.workflow.run.cancel') {
23 - return `cancelrun:${repoId}:${runId}`;
24 - }
20 + if (payload.actionId === 'github.workflow.run.rerun_failed') return `rerun:${repoId}:${runId}`;
21 + if (payload.actionId === 'github.workflow.run.cancel') return `cancelrun:${repoId}:${runId}`;
22 throw new Error('Approval action is not supported');
23 }
24
@@ -63,11 +60,7 @@ function approvalRejectedView(message, error) {
60 ${detail}
61
62 برای ادامه به صفحهٔ Run برگردید و در صورت نیاز یک تأیید تازه ایجاد کنید.`,
66 - reply_markup: {
67 - inline_keyboard: [
68 - [{ text: '↩️ بازگشت به Run', callback_data: fallback }]
69 - ]
70 - }
63 + reply_markup: { inline_keyboard: [[{ text: '↩️ بازگشت به Run', callback_data: fallback }]] }
64 };
65 }
66
@@ -79,11 +72,9 @@ function approvedActionFailedView(payload, error) {
72 const fallback = Number.isSafeInteger(Number(repoId)) && Number.isSafeInteger(Number(runId))
73 ? `run:${repoId}:${runId}`
74 : 'menu:main';
82 -
75 const detail = permissionDenied
76 ? 'تأیید شما معتبر بود، اما GitHub درخواست را با خطای 403 رد کرد. دسترسی توکن فعلی برای GitHub Actions WRITE کافی نیست.'
77 : 'تأیید شما معتبر بود، اما GitHub عملیات را موفق اعلام نکرد. وضعیت Run را بررسی کنید.';
86 -
78 return {
79 text:
80 `⚠️ <b>اجرای عملیات در GitHub ناموفق بود</b>
@@ -91,11 +82,7 @@ function approvedActionFailedView(payload, error) {
82 ${detail}
83
84 این تأیید یک‌بار مصرف شده و با همان دکمه دوباره اجرا نمی‌شود.`,
94 - reply_markup: {
95 - inline_keyboard: [
96 - [{ text: '↩️ بازگشت به Run', callback_data: fallback }]
97 - ]
98 - }
85 + reply_markup: { inline_keyboard: [[{ text: '↩️ بازگشت به Run', callback_data: fallback }]] }
86 };
87 }
88
@@ -110,7 +97,6 @@ function callbackReadFailedView(data, error) {
97 ? 'GitHub درخواست Issues را با خطای 403 رد کرد. در Fine-grained token، مجوز Repository permissions → Issues را روی Read قرار دهید.'
98 : 'GitHub این درخواست خواندنی را با خطای 403 رد کرد. مجوز Read مربوط به این بخش در توکن فعلی کافی نیست.'
99 : 'دریافت اطلاعات از سرویس با خطا مواجه شد. هیچ تغییری انجام نشد.';
113 -
100 return {
101 text:
102 `⚠️ <b>دریافت اطلاعات با خطا مواجه شد</b>
@@ -118,11 +104,7 @@ function callbackReadFailedView(data, error) {
104 ${detail}
105
106 این خطا هیچ عملیات تغییردهنده‌ای اجرا نکرد.`,
121 - reply_markup: {
122 - inline_keyboard: [
123 - [{ text: repoMatch ? '↩️ بازگشت به پروژه' : '↩️ منوی اصلی', callback_data: fallback }]
124 - ]
125 - }
107 + reply_markup: { inline_keyboard: [[{ text: repoMatch ? '↩️ بازگشت به پروژه' : '↩️ منوی اصلی', callback_data: fallback }]] }
108 };
109 }
110
@@ -132,33 +114,19 @@ export class NexaDevBot {
114 this.telegram = new TelegramClient(env.TELEGRAM_BOT_TOKEN || '');
115 this.github = new GitHubService(env.GITHUB_TOKEN || '', env.GITHUB_OWNER || 'davoodmehraban89');
116 this.menu = new MenuManager(this.github);
135 - this.replayStore = new DurableApprovalReplayStore({
136 - namespace: env.APPROVAL_REPLAY_GUARD
137 - });
117 + this.replayStore = new DurableApprovalReplayStore({ namespace: env.APPROVAL_REPLAY_GUARD });
118 + this.workspaceStore = new DurableWorkspaceStoreClient({ namespace: env.WORKSPACE_STORE });
119 this.approvals = new ApprovalService({
120 secret: env.APPROVAL_SIGNING_SECRET || env.TELEGRAM_WEBHOOK_SECRET || '',
121 replayStore: this.replayStore
122 });
142 -
143 - this.githubPlugin = createGitHubPlugin({
144 - github: this.github,
145 - menu: this.menu
146 - });
123 + this.githubPlugin = createGitHubPlugin({ github: this.github, menu: this.menu, workspaces: this.workspaceStore });
124 this.registry = new PluginRegistry([this.githubPlugin]);
125 this.policy = new ActionPolicy();
126 this.audit = new AuditLogger({
150 - secrets: [
151 - env.TELEGRAM_BOT_TOKEN,
152 - env.TELEGRAM_WEBHOOK_SECRET,
153 - env.GITHUB_TOKEN,
154 - env.APPROVAL_SIGNING_SECRET
155 - ]
156 - });
157 - this.router = new PluginRouter({
158 - registry: this.registry,
159 - policy: this.policy,
160 - audit: this.audit
127 + secrets: [env.TELEGRAM_BOT_TOKEN, env.TELEGRAM_WEBHOOK_SECRET, env.GITHUB_TOKEN, env.APPROVAL_SIGNING_SECRET]
128 });
129 + this.router = new PluginRouter({ registry: this.registry, policy: this.policy, audit: this.audit });
130 }
131
132 async handleUpdate(update) {
@@ -177,15 +145,8 @@ export class NexaDevBot {
145 const ownerId = query.from?.id;
146
147 let callbackAnswered = true;
180 - try {
181 - await this.telegram.answerCallbackQuery(callbackId);
182 - } catch {
183 - callbackAnswered = false;
184 - }
185 -
186 - if (!chatId || !messageId) {
187 - return { ok: false, error: 'Missing message context for callback', callbackAnswered };
188 - }
148 + try { await this.telegram.answerCallbackQuery(callbackId); } catch { callbackAnswered = false; }
149 + if (!chatId || !messageId) return { ok: false, error: 'Missing message context for callback', callbackAnswered };
150
151 try {
152 if (data.startsWith('approve:')) {
@@ -193,102 +154,45 @@ export class NexaDevBot {
154 const token = data.slice('approve:'.length);
155 let payload;
156 try {
196 - payload = await this.approvals.consume(token, {
197 - ownerId: String(ownerId),
198 - pluginId: 'github',
199 - risk: 'WRITE'
200 - });
157 + payload = await this.approvals.consume(token, { ownerId: String(ownerId), pluginId: 'github', risk: 'WRITE' });
158 } catch (error) {
202 - this.audit.log({
203 - event: 'approval_rejected',
204 - pluginId: 'github',
205 - risk: 'WRITE',
206 - outcome: 'rejected',
207 - error: error?.message || 'Approval rejected'
208 - });
159 + this.audit.log({ event: 'approval_rejected', pluginId: 'github', risk: 'WRITE', outcome: 'rejected', error: error?.message || 'Approval rejected' });
160 const view = approvalRejectedView(message, error);
210 - const res = await this.telegram.editMessageText(
211 - chatId,
212 - messageId,
213 - view.text,
214 - view.reply_markup
215 - );
216 - return {
217 - ok: false,
218 - approvalRejected: true,
219 - error: error?.message || 'Approval rejected',
220 - callbackAnswered,
221 - edited: true,
222 - res
223 - };
161 + const res = await this.telegram.editMessageText(chatId, messageId, view.text, view.reply_markup);
162 + return { ok: false, approvalRejected: true, error: error?.message || 'Approval rejected', callbackAnswered, edited: true, res };
163 }
164
226 - this.audit.log({
227 - event: 'approval_verified',
228 - pluginId: payload.pluginId,
229 - actionId: payload.actionId,
230 - risk: payload.risk,
231 - outcome: 'verified'
232 - });
233 -
165 + this.audit.log({ event: 'approval_verified', pluginId: payload.pluginId, actionId: payload.actionId, risk: payload.risk, outcome: 'verified' });
166 const sourceCallback = approvalSourceCallback(payload);
167 let routed;
168 try {
169 routed = await this.router.dispatchCallback(sourceCallback, {
170 isOwner: true,
171 + ownerId: String(ownerId),
172 approved: true
173 });
241 - if (!routed || routed.requiresApproval || routed.denied) {
242 - throw new Error('Approved action could not be dispatched');
243 - }
174 + if (!routed || routed.requiresApproval || routed.denied) throw new Error('Approved action could not be dispatched');
175 } catch (error) {
176 const view = approvedActionFailedView(payload, error);
246 - const res = await this.telegram.editMessageText(
247 - chatId,
248 - messageId,
249 - view.text,
250 - view.reply_markup
251 - );
252 - return {
253 - ok: false,
254 - approved: true,
255 - providerError: true,
256 - actionId: payload.actionId,
257 - error: error?.message || 'GitHub operation failed',
258 - callbackAnswered,
259 - edited: true,
260 - res
261 - };
177 + const res = await this.telegram.editMessageText(chatId, messageId, view.text, view.reply_markup);
178 + return { ok: false, approved: true, providerError: true, actionId: payload.actionId, error: error?.message || 'GitHub operation failed', callbackAnswered, edited: true, res };
179 }
180
264 - const res = await this.telegram.editMessageText(
265 - chatId,
266 - messageId,
267 - routed.text,
268 - routed.reply_markup
269 - );
270 - return {
271 - ok: true,
272 - approved: true,
273 - actionId: payload.actionId,
274 - callbackAnswered,
275 - edited: true,
276 - res
277 - };
181 + const res = await this.telegram.editMessageText(chatId, messageId, routed.text, routed.reply_markup);
182 + return { ok: true, approved: true, actionId: payload.actionId, callbackAnswered, edited: true, res };
183 }
184
280 - const routed = await this.router.dispatchCallback(data, { isOwner: true });
185 + const routed = await this.router.dispatchCallback(data, {
186 + isOwner: true,
187 + ownerId: ownerId === undefined || ownerId === null ? '' : String(ownerId)
188 + });
189 if (routed?.requiresApproval) {
190 const approval = routed.approval;
191 const repoId = approval?.input?.repoId;
192 const runId = approval?.input?.runId;
285 - if (!approval || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) {
286 - throw new Error('Approval metadata is missing');
287 - }
193 + if (!approval || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) throw new Error('Approval metadata is missing');
194 if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required');
289 - if (!this.approvals.isReplayProtected()) {
290 - throw new Error('Durable approval replay protection is not configured');
291 - }
195 + if (!this.approvals.isReplayProtected()) throw new Error('Durable approval replay protection is not configured');
196
197 const token = await this.approvals.issue({
198 pluginId: routed.pluginId,
@@ -297,82 +201,27 @@ export class NexaDevBot {
201 ownerId: String(ownerId),
202 input: approval.input
203 });
300 - this.audit.log({
301 - event: 'approval_issued',
302 - pluginId: routed.pluginId,
303 - actionId: routed.actionId,
304 - risk: routed.risk,
305 - outcome: 'pending'
306 - });
307 -
308 - const view = approvalView({
309 - label: approval.label || routed.actionId,
310 - token,
311 - repoId,
312 - runId
313 - });
314 - const res = await this.telegram.editMessageText(
315 - chatId,
316 - messageId,
317 - view.text,
318 - view.reply_markup
319 - );
320 - return {
321 - ok: true,
322 - approvalRequired: true,
323 - actionId: routed.actionId,
324 - risk: routed.risk,
325 - callbackAnswered,
326 - edited: true,
327 - res
328 - };
329 - }
330 - if (routed?.denied) {
331 - return {
332 - ok: false,
333 - denied: true,
334 - actionId: routed.actionId,
335 - reason: routed.reason,
336 - callbackAnswered
337 - };
204 + this.audit.log({ event: 'approval_issued', pluginId: routed.pluginId, actionId: routed.actionId, risk: routed.risk, outcome: 'pending' });
205 + const view = approvalView({ label: approval.label || routed.actionId, token, repoId, runId });
206 + const res = await this.telegram.editMessageText(chatId, messageId, view.text, view.reply_markup);
207 + return { ok: true, approvalRequired: true, actionId: routed.actionId, risk: routed.risk, callbackAnswered, edited: true, res };
208 }
209 + if (routed?.denied) return { ok: false, denied: true, actionId: routed.actionId, reason: routed.reason, callbackAnswered };
210
211 const view = routed || await this.menu.renderByCallback(data);
341 - const res = await this.telegram.editMessageText(
342 - chatId,
343 - messageId,
344 - view.text,
345 - view.reply_markup
346 - );
347 -
212 + const res = await this.telegram.editMessageText(chatId, messageId, view.text, view.reply_markup);
213 return { ok: true, edited: true, callbackAnswered, res };
214 } catch (err) {
350 - if (err.message && err.message.includes('message is not modified')) {
351 - return { ok: true, unmodified: true, callbackAnswered };
352 - }
353 -
215 + if (err.message && err.message.includes('message is not modified')) return { ok: true, unmodified: true, callbackAnswered };
216 if (!data.startsWith('approve:')) {
217 try {
218 const view = callbackReadFailedView(data, err);
357 - const res = await this.telegram.editMessageText(
358 - chatId,
359 - messageId,
360 - view.text,
361 - view.reply_markup
362 - );
363 - return {
364 - ok: false,
365 - error: err.message,
366 - providerError: true,
367 - callbackAnswered,
368 - edited: true,
369 - res
370 - };
219 + const res = await this.telegram.editMessageText(chatId, messageId, view.text, view.reply_markup);
220 + return { ok: false, error: err.message, providerError: true, callbackAnswered, edited: true, res };
221 } catch {
222 // Preserve the original provider/navigation error when feedback delivery also fails.
223 }
224 }
375 -
225 return { ok: false, error: err.message, callbackAnswered };
226 }
227 }
@@ -380,7 +229,6 @@ export class NexaDevBot {
229 async handleMessage(message) {
230 const chatId = message.chat?.id;
231 const text = (message.text || '').trim();
383 -
232 if (!chatId) return { ok: false, error: 'Missing chat_id' };
233
234 if (text === '/start' || text === '/menu' || text.startsWith('/start')) {
@@ -390,15 +238,14 @@ export class NexaDevBot {
238 }
239
240 if (text === '/repos') {
393 - const routed = await this.router.dispatchCommand(text, { isOwner: true });
241 + const ownerId = message.from?.id;
242 + const routed = await this.router.dispatchCommand(text, {
243 + isOwner: true,
244 + ownerId: ownerId === undefined || ownerId === null ? '' : String(ownerId)
245 + });
246 if (routed?.requiresApproval || routed?.denied || !routed) {
395 - return {
396 - ok: false,
397 - command: 'repos',
398 - error: routed?.reason || 'GitHub repository route unavailable'
399 - };
247 + return { ok: false, command: 'repos', error: routed?.reason || 'GitHub repository route unavailable' };
248 }
401 -
249 const res = await this.telegram.sendMessage(chatId, routed.text, routed.reply_markup);
250 return { ok: true, command: 'repos', res };
251 }
@@ -410,14 +257,7 @@ export class NexaDevBot {
257 }
258
259 const defaultText = `💡 برای دسترسی به منوهای پروژه‌ها و ابزارهای Nexa Dev، دکمه زیر را لمس کنید:`;
413 - const view = {
414 - text: defaultText,
415 - reply_markup: {
416 - inline_keyboard: [
417 - [{ text: '⚡ باز کردن منوی اصلی Nexa Dev', callback_data: 'menu:main' }]
418 - ]
419 - }
420 - };
260 + const view = { text: defaultText, reply_markup: { inline_keyboard: [[{ text: '⚡ باز کردن منوی اصلی Nexa Dev', callback_data: 'menu:main' }]] } };
261 const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup);
262 return { ok: true, default: true, res };
263 }