Personal Telegram developer console for GitHub

fix/main-menu-callback-fallback
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 432 lines 16 KB
Raw
1 import { DEFAULT_CONFIG } from './config.js';
2
3 function escapeHtml(value = '') {
4 return String(value)
5 .replaceAll('&', '&')
6 .replaceAll('<', '&lt;')
7 .replaceAll('>', '&gt;')
8 .replaceAll('"', '&quot;');
9 }
10
11 function decodeBase64Utf8(value = '') {
12 const binary = atob(String(value).replace(/\s/g, ''));
13 const bytes = Uint8Array.from(binary, char => char.charCodeAt(0));
14 return new TextDecoder().decode(bytes);
15 }
16
17 function callbackFits(value) {
18 return new TextEncoder().encode(String(value)).length <= 64;
19 }
20
21 function fileItemRows(repoId, items) {
22 const rows = [];
23 for (const item of items.slice(0, 30)) {
24 const action = item.type === 'dir' ? 'tree' : 'blob';
25 const callbackData = `${action}:${repoId}:${item.sha}`;
26 if (!item.sha || !callbackFits(callbackData)) continue;
27 rows.push([{
28 text: `${item.type === 'dir' ? '📁' : '📄'} ${item.name}`,
29 callback_data: callbackData
30 }]);
31 }
32 return rows;
33 }
34
35 /**
36 * Menu Router and UI Generator for Nexa Dev Bot.
37 * Views never claim external service health unless that view was built from a live API response.
38 */
39 export class MenuManager {
40 constructor(githubService) {
41 this.github = githubService;
42 }
43
44 getMainMenuView() {
45 const text =
46 `⚡ <b>Nexa Dev Bot | پنل مدیریت توسعه</b>
47 ➖➖➖➖➖➖➖➖➖➖
48 یکی از بخش‌های زیر را انتخاب کنید.
49 اتصال GitHub هنگام باز کردن مخزن‌ها به‌صورت زنده بررسی می‌شود.`;
50
51 return {
52 text,
53 reply_markup: {
54 inline_keyboard: [
55 [
56 { text: '📁 مخزن‌ها (پروژه‌ها)', callback_data: 'menu:repos' },
57 { text: '📊 وضعیت سرویس', callback_data: 'menu:status' }
58 ],
59 [
60 { text: '🔧 ابزارهای توسعه', callback_data: 'menu:tools' },
61 { text: '⚙️ تنظیمات', callback_data: 'menu:settings' }
62 ],
63 [
64 { text: '🔐 امنیت', callback_data: 'menu:security' }
65 ]
66 ]
67 }
68 };
69 }
70
71 async getReposListView() {
72 const repos = await this.github.listRepositories();
73 const text =
74 `📁 <b>فهرست مخزن‌ها و پروژه‌ها</b>
75 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها</code>
76 ➖➖➖➖➖➖➖➖➖➖
77 این فهرست از GitHub API دریافت شده است. پروژه مورد نظر را انتخاب کنید:`;
78
79 const inlineKeyboard = [];
80 for (let i = 0; i < repos.length; i += 2) {
81 const row = [];
82 row.push({
83 text: `${repos[i].private ? '🔒' : '🌐'} ${repos[i].name}`,
84 callback_data: `repo:${repos[i].id}`
85 });
86 if (repos[i + 1]) {
87 row.push({
88 text: `${repos[i + 1].private ? '🔒' : '🌐'} ${repos[i + 1].name}`,
89 callback_data: `repo:${repos[i + 1].id}`
90 });
91 }
92 inlineKeyboard.push(row);
93 }
94
95 if (!repos.length) {
96 inlineKeyboard.push([{ text: '🔄 بررسی مجدد', callback_data: 'menu:repos' }]);
97 }
98 inlineKeyboard.push([{ text: '🔙 بازگشت به منوی اصلی', callback_data: 'menu:main' }]);
99
100 return { text, reply_markup: { inline_keyboard: inlineKeyboard } };
101 }
102
103 async getRepoDetailView(repoId) {
104 const repo = await this.github.getRepository(repoId);
105 const visibility = repo.private ? '🔒 خصوصی' : '🌐 عمومی';
106 const archived = repo.archived ? '\n⚠️ وضعیت: آرشیوشده' : '';
107 const description = repo.description ? `\n📝 ${escapeHtml(repo.description)}` : '';
108 const text =
109 `📦 <b>پروژه: ${escapeHtml(repo.name)}</b>
110 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)}</code>
111 ➖➖➖➖➖➖➖➖➖➖
112 👤 مخزن: <code>${escapeHtml(repo.fullName)}</code>
113 👁 دسترسی: ${visibility}
114 🌿 شاخه پیش‌فرض: <code>${escapeHtml(repo.defaultBranch)}</code>${archived}${description}
115
116 اطلاعات بالا از GitHub API دریافت شد. یک عملیات را انتخاب کنید:`;
117
118 return {
119 text,
120 reply_markup: {
121 inline_keyboard: [
122 [
123 { text: '📂 فایل‌ها', callback_data: `files:${repoId}` },
124 { text: '🔀 Pull Requestها', callback_data: `pr:${repoId}` }
125 ],
126 [
127 { text: '📝 Commitها', callback_data: `commits:${repoId}` },
128 { text: '📊 وضعیت / تست‌ها', callback_data: `status:${repoId}` }
129 ],
130 [{ text: '🔙 بازگشت به لیست پروژه‌ها', callback_data: 'menu:repos' }]
131 ]
132 }
133 };
134 }
135
136 async getPullRequestsView(repoId) {
137 const [repo, prs] = await Promise.all([
138 this.github.getRepository(repoId),
139 this.github.listPullRequests(repoId)
140 ]);
141 const prListText = prs.length
142 ? prs.map(pr => {
143 const state = pr.merged ? '🟣 Merged' : (pr.state === 'open' ? '🟢 Open' : '🔴 Closed');
144 return `• <b>#${pr.number}</b> ${escapeHtml(pr.title)}\n └ <i>${escapeHtml(pr.author)} | ${state}</i>`;
145 }).join('\n\n')
146 : '<i>Pull Requestی در پاسخ GitHub یافت نشد.</i>';
147
148 const text =
149 `🔀 <b>Pull Requestهای پروژه ${escapeHtml(repo.name)}</b>
150 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)} &gt; 🔀 Pull Request</code>
151 ➖➖➖➖➖➖➖➖➖➖
152 ${prListText}`;
153
154 return {
155 text,
156 reply_markup: {
157 inline_keyboard: [[
158 { text: '🔄 بروزرسانی', callback_data: `pr:${repoId}` },
159 { text: '🔙 بازگشت به پروژه', callback_data: `repo:${repoId}` }
160 ]]
161 }
162 };
163 }
164
165 async getCommitsView(repoId) {
166 const [repo, commits] = await Promise.all([
167 this.github.getRepository(repoId),
168 this.github.listCommits(repoId)
169 ]);
170 const commitListText = commits.length
171 ? commits.map(c => `• <code>${escapeHtml(c.sha)}</code> - ${escapeHtml(c.message)}\n └ <i>${escapeHtml(c.author)}${c.date ? ` | ${escapeHtml(c.date)}` : ''}</i>`).join('\n\n')
172 : '<i>کامیتی در پاسخ GitHub یافت نشد.</i>';
173
174 const text =
175 `📝 <b>آخرین Commitهای پروژه ${escapeHtml(repo.name)}</b>
176 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)} &gt; 📝 Commitها</code>
177 ➖➖➖➖➖➖➖➖➖➖
178 ${commitListText}`;
179
180 return {
181 text,
182 reply_markup: {
183 inline_keyboard: [[
184 { text: '🔄 بروزرسانی', callback_data: `commits:${repoId}` },
185 { text: '🔙 بازگشت به پروژه', callback_data: `repo:${repoId}` }
186 ]]
187 }
188 };
189 }
190
191 async getFilesView(repoId, subpath = '') {
192 const [repo, files] = await Promise.all([
193 this.github.getRepository(repoId),
194 this.github.listFiles(repoId, subpath)
195 ]);
196 const visibleItems = files.slice(0, 30);
197 const itemsText = visibleItems.length
198 ? visibleItems.map(f => {
199 const icon = f.type === 'dir' ? '📁' : '📄';
200 const size = f.type === 'file' && f.size ? ` (${f.size} B)` : '';
201 return `${icon} <code>${escapeHtml(f.name)}</code>${size}`;
202 }).join('\n')
203 : '<i>پوشه خالی است.</i>';
204 const omitted = files.length > visibleItems.length
205 ? `\n\n<i>${files.length - visibleItems.length} مورد دیگر در این نما نمایش داده نشده است.</i>`
206 : '';
207
208 const text =
209 `📂 <b>ساختار فایل‌های پروژه ${escapeHtml(repo.name)}</b>
210 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)} &gt; 📂 فایل‌ها${subpath ? ` &gt; ${escapeHtml(subpath)}` : ''}</code>
211 ➖➖➖➖➖➖➖➖➖➖
212 ${itemsText}${omitted}`;
213
214 const inlineKeyboard = fileItemRows(repoId, visibleItems);
215 inlineKeyboard.push([
216 { text: '🔄 بروزرسانی', callback_data: `files:${repoId}${subpath ? `:${subpath}` : ''}` },
217 { text: '🔙 بازگشت به پروژه', callback_data: `repo:${repoId}` }
218 ]);
219
220 return { text, reply_markup: { inline_keyboard: inlineKeyboard } };
221 }
222
223 async getTreeView(repoId, sha) {
224 const [repo, items] = await Promise.all([
225 this.github.getRepository(repoId),
226 this.github.listTree(repoId, sha)
227 ]);
228 const visibleItems = items.slice(0, 30);
229 const itemsText = visibleItems.length
230 ? visibleItems.map(item => {
231 const icon = item.type === 'dir' ? '📁' : '📄';
232 const size = item.type === 'file' && item.size ? ` (${item.size} B)` : '';
233 return `${icon} <code>${escapeHtml(item.name)}</code>${size}`;
234 }).join('\n')
235 : '<i>این پوشه خالی است.</i>';
236 const omitted = items.length > visibleItems.length
237 ? `\n\n<i>${items.length - visibleItems.length} مورد دیگر در این نما نمایش داده نشده است.</i>`
238 : '';
239
240 const text =
241 `📁 <b>پوشه در ${escapeHtml(repo.name)}</b>
242 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)} &gt; 📂 فایل‌ها &gt; پوشه</code>
243 ➖➖➖➖➖➖➖➖➖➖
244 ${itemsText}${omitted}`;
245
246 const inlineKeyboard = fileItemRows(repoId, visibleItems);
247 inlineKeyboard.push([
248 { text: '🔙 ریشه فایل‌ها', callback_data: `files:${repoId}` },
249 { text: '📦 پروژه', callback_data: `repo:${repoId}` }
250 ]);
251
252 return { text, reply_markup: { inline_keyboard: inlineKeyboard } };
253 }
254
255 async getBlobView(repoId, sha) {
256 const [repo, blob] = await Promise.all([
257 this.github.getRepository(repoId),
258 this.github.getBlob(repoId, sha)
259 ]);
260
261 let body;
262 if (blob.encoding !== 'base64' || blob.size > 100000) {
263 body = '<i>این فایل برای نمایش مستقیم در تلگرام مناسب نیست.</i>';
264 } else {
265 const decoded = decodeBase64Utf8(blob.content);
266 const preview = decoded.length > 2800 ? `${decoded.slice(0, 2800)}\n…` : decoded;
267 body = `<pre>${escapeHtml(preview)}</pre>`;
268 }
269
270 const text =
271 `📄 <b>پیش‌نمایش فایل در ${escapeHtml(repo.name)}</b>
272 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(repo.name)} &gt; 📂 فایل‌ها &gt; فایل</code>
273 ➖➖➖➖➖➖➖➖➖➖
274 ${body}`;
275
276 return {
277 text,
278 reply_markup: {
279 inline_keyboard: [[
280 { text: '🔙 ریشه فایل‌ها', callback_data: `files:${repoId}` },
281 { text: '📦 پروژه', callback_data: `repo:${repoId}` }
282 ]]
283 }
284 };
285 }
286
287 async getRepoStatusView(repoId) {
288 const status = await this.github.getRepoStatus(repoId);
289 const runsText = status.lastRuns.length
290 ? status.lastRuns.map(r => {
291 const icon = r.conclusion === 'success' ? '✅' : (r.conclusion === 'failure' ? '❌' : '⏳');
292 return `• ${icon} <b>${escapeHtml(r.name)}</b>: ${escapeHtml(r.conclusion || r.status || 'unknown')}\n └ <i>${escapeHtml(r.commit || '')}</i>`;
293 }).join('\n')
294 : '<i>Workflow runی در پاسخ GitHub یافت نشد.</i>';
295
296 const label = status.repoName || repoId;
297 const text =
298 `📊 <b>وضعیت پایپ‌لاین ${escapeHtml(label)}</b>
299 <code>🏠 Nexa Dev &gt; 📁 مخزن‌ها &gt; ${escapeHtml(label)} &gt; 📊 وضعیت</code>
300 ➖➖➖➖➖➖➖➖➖➖
301 شاخه پیش‌فرض: <code>${escapeHtml(status.defaultBranch)}</code>
302 آخرین نتایج GitHub Actions:
303
304 ${runsText}`;
305
306 return {
307 text,
308 reply_markup: {
309 inline_keyboard: [[
310 { text: '🔄 بروزرسانی', callback_data: `status:${repoId}` },
311 { text: '🔙 بازگشت به پروژه', callback_data: `repo:${repoId}` }
312 ]]
313 }
314 };
315 }
316
317 getServiceStatusView() {
318 const text =
319 `📊 <b>وضعیت سیستم و سرویس‌ها</b>
320 <code>🏠 Nexa Dev &gt; 📊 وضعیت سرویس</code>
321 ➖➖➖➖➖➖➖➖➖➖
322 • <b>Cloudflare Worker:</b> این درخواست توسط Worker پاسخ داده شد.
323 • <b>Telegram Webhook:</b> secret و owner در مرز ورودی Worker enforce می‌شوند؛ مقادیر محرمانه نمایش داده نمی‌شوند.
324 • <b>GitHub API:</b> هنگام باز کردن مخزن‌ها و وضعیت پروژه به‌صورت زنده فراخوانی می‌شود.
325 • <b>CI/CD:</b> نتیجه هر مخزن از GitHub Actions خوانده می‌شود.
326
327 این صفحه SLA، latency یا سلامت سرویس خارجی را بدون اندازه‌گیری واقعی اعلام نمی‌کند.`;
328
329 return {
330 text,
331 reply_markup: {
332 inline_keyboard: [[
333 { text: '🔄 بررسی مجدد', callback_data: 'menu:status' },
334 { text: '🔙 بازگشت به منوی اصلی', callback_data: 'menu:main' }
335 ]]
336 }
337 };
338 }
339
340 getToolsView() {
341 const tools = DEFAULT_CONFIG.devTools.map(t => `• <b>${t.title}</b>\n └ <i>${t.desc}</i>`).join('\n\n');
342 const text =
343 `🔧 <b>ابزارهای توسعه</b>
344 <code>🏠 Nexa Dev &gt; 🔧 ابزارهای توسعه</code>
345 ➖➖➖➖➖➖➖➖➖➖
346 ${tools}`;
347
348 return {
349 text,
350 reply_markup: {
351 inline_keyboard: [
352 [{ text: '🏓 بررسی مسیر داخلی', callback_data: 'tool:ping' }],
353 [{ text: '🔙 بازگشت به منوی اصلی', callback_data: 'menu:main' }]
354 ]
355 }
356 };
357 }
358
359 getSettingsView() {
360 const text =
361 `⚙️ <b>تنظیمات Nexa Dev</b>
362 <code>🏠 Nexa Dev &gt; ⚙️ تنظیمات</code>
363 ➖➖➖➖➖➖➖➖➖➖
364 • مالک GitHub پیش‌فرض: <code>${escapeHtml(DEFAULT_CONFIG.defaultOwner)}</code>
365 • Runtime: <b>Cloudflare Worker</b>
366 • ناوبری: <b>Single-message / editMessageText</b>
367 • زبان رابط: <b>فارسی</b>
368
369 مقادیر Secret از این رابط نمایش داده نمی‌شوند.`;
370
371 return {
372 text,
373 reply_markup: { inline_keyboard: [[{ text: '🔙 بازگشت به منوی اصلی', callback_data: 'menu:main' }]] }
374 };
375 }
376
377 getSecurityView() {
378 const text =
379 `🔐 <b>امنیت و دسترسی‌ها</b>
380 <code>🏠 Nexa Dev &gt; 🔐 امنیت</code>
381 ➖➖➖➖➖➖➖➖➖➖
382 • Webhook secret: <b>اجباری در Worker</b>
383 • Owner-only access: <b>اجباری با OWNER_TELEGRAM_ID</b>
384 • Credentials: <b>خارج از مخزن و خارج از پیام‌های ربات</b>
385 • Diagnostic ID endpoint: <b>غیرفعال</b>
386
387 این صفحه وجود یا مقدار Secretهای runtime را افشا نمی‌کند.`;
388
389 return {
390 text,
391 reply_markup: { inline_keyboard: [[{ text: '🔙 بازگشت به منوی اصلی', callback_data: 'menu:main' }]] }
392 };
393 }
394
395 async renderByCallback(callbackData) {
396 const [action, target, ...extra] = String(callbackData || '').split(':');
397
398 switch (action) {
399 case 'menu':
400 if (target === 'repos') return this.getReposListView();
401 if (target === 'status') return this.getServiceStatusView();
402 if (target === 'tools') return this.getToolsView();
403 if (target === 'settings') return this.getSettingsView();
404 if (target === 'security') return this.getSecurityView();
405 return this.getMainMenuView();
406 case 'repo':
407 return this.getRepoDetailView(target);
408 case 'pr':
409 return this.getPullRequestsView(target);
410 case 'commits':
411 return this.getCommitsView(target);
412 case 'files':
413 return this.getFilesView(target, extra.join(':'));
414 case 'tree':
415 return this.getTreeView(target, extra[0]);
416 case 'blob':
417 return this.getBlobView(target, extra[0]);
418 case 'status':
419 return this.getRepoStatusView(target);
420 case 'tool':
421 if (target === 'ping') {
422 return {
423 text: '🏓 <b>Pong</b>\nمسیر داخلی ربات پاسخ داد. latency شبکه یا سلامت سرویس خارجی در این عملیات اندازه‌گیری نشده است.',
424 reply_markup: { inline_keyboard: [[{ text: '🔙 بازگشت به ابزارها', callback_data: 'menu:tools' }]] }
425 };
426 }
427 return this.getToolsView();
428 default:
429 return this.getMainMenuView();
430 }
431 }
432 }