Personal Telegram developer console for GitHub
spec/repository-workspace-editor
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
| 1 | import { |
| 2 | buildUnifiedDiff, |
| 3 | digestText, |
| 4 | makeTargetBranch, |
| 5 | normalizeCommitMessage, |
| 6 | validateEditableFile |
| 7 | } from '../../core/workspace-policy.js'; |
| 8 | import { commitWorkspaceIntent } from './workspace-commit.js'; |
| 9 | |
| 10 | export { commitWorkspaceIntent } from './workspace-commit.js'; |
| 11 | |
| 12 | const HANDLE_10 = '[A-Za-z0-9_-]{10}'; |
| 13 | const INTENT_8 = '[A-Za-z0-9_-]{8}'; |
| 14 | const escapeHtml = value => String(value ?? '') |
| 15 | .replaceAll('&', '&') |
| 16 | .replaceAll('<', '<') |
| 17 | .replaceAll('>', '>') |
| 18 | .replaceAll('"', '"'); |
| 19 | const short = (value, max = 70) => { |
| 20 | const text = String(value ?? ''); |
| 21 | return text.length > max ? `${text.slice(0, max - 1)}…` : text; |
| 22 | }; |
| 23 | |
| 24 | const ownerFrom = context => { |
| 25 | const ownerId = String(context?.ownerId ?? ''); |
| 26 | if (!ownerId) throw new Error('Workspace owner is required'); |
| 27 | return ownerId; |
| 28 | }; |
| 29 | |
| 30 | function parseCallback(data) { |
| 31 | const value = String(data || ''); |
| 32 | let match = value.match(/^(?:files):(\d+)$/); |
| 33 | if (match) return { actionId: 'github.files.list', kind: 'root', repoId: Number(match[1]) }; |
| 34 | match = value.match(new RegExp(`^wnav:(${HANDLE_10})$`)); |
| 35 | if (match) return { actionId: 'github.workspace.navigate', kind: 'navigate', refId: match[1] }; |
| 36 | match = value.match(new RegExp(`^wedit:(${HANDLE_10})$`)); |
| 37 | if (match) return { actionId: 'github.workspace.create', kind: 'edit', refId: match[1] }; |
| 38 | match = value.match(new RegExp(`^ws:(${HANDLE_10})$`)); |
| 39 | if (match) return { actionId: 'github.workspace.view', kind: 'workspace', workspaceId: match[1] }; |
| 40 | match = value.match(new RegExp(`^wtext:(${HANDLE_10})$`)); |
| 41 | if (match) return { actionId: 'github.workspace.input.prepare', kind: 'input', workspaceId: match[1], mode: 'draft' }; |
| 42 | match = value.match(new RegExp(`^wmsg:(${HANDLE_10})$`)); |
| 43 | if (match) return { actionId: 'github.workspace.input.prepare', kind: 'input', workspaceId: match[1], mode: 'commit_message' }; |
| 44 | match = value.match(new RegExp(`^wdiff:(${HANDLE_10})$`)); |
| 45 | if (match) return { actionId: 'github.workspace.diff', kind: 'diff', workspaceId: match[1] }; |
| 46 | match = value.match(new RegExp(`^wready:(${HANDLE_10})$`)); |
| 47 | if (match) return { actionId: 'github.workspace.ready', kind: 'ready', workspaceId: match[1] }; |
| 48 | match = value.match(new RegExp(`^wcancel:(${HANDLE_10})$`)); |
| 49 | if (match) return { actionId: 'github.workspace.cancel', kind: 'cancel', workspaceId: match[1] }; |
| 50 | match = value.match(new RegExp(`^wscommit:(\\d+):(${INTENT_8})$`)); |
| 51 | if (match) return { |
| 52 | actionId: 'github.workspace.commit_branch', |
| 53 | kind: 'commit', |
| 54 | repoId: Number(match[1]), |
| 55 | intentId: match[2] |
| 56 | }; |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | function workspaceButtons(workspace) { |
| 61 | return [ |
| 62 | [ |
| 63 | { text: '📝 متن Draft', callback_data: `wtext:${workspace.id}` }, |
| 64 | { text: '✍️ پیام Commit', callback_data: `wmsg:${workspace.id}` } |
| 65 | ], |
| 66 | [ |
| 67 | { text: '🔍 Diff', callback_data: `wdiff:${workspace.id}` }, |
| 68 | { text: '✅ آماده Commit', callback_data: `wready:${workspace.id}` } |
| 69 | ], |
| 70 | [ |
| 71 | { text: '🗑 لغو Draft', callback_data: `wcancel:${workspace.id}` }, |
| 72 | { text: '🔙 فایلها', callback_data: `files:${workspace.repoId}` } |
| 73 | ] |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | function workspaceCommitResultView(repoId, intent, result) { |
| 78 | const rows = [ |
| 79 | [{ text: '🧪 CI / Actions', callback_data: `runs:${repoId}` }] |
| 80 | ]; |
| 81 | if (intent?.workspaceId) { |
| 82 | rows.push([{ text: '📝 Workspace', callback_data: `ws:${intent.workspaceId}` }]); |
| 83 | } |
| 84 | rows.push([{ text: '📦 پروژه', callback_data: `repo:${repoId}` }]); |
| 85 | return { |
| 86 | text: `✅ <b>Commit روی branch جدید ایجاد شد</b>\nBranch: <code>${escapeHtml(result.branch)}</code>\nSHA: <code>${escapeHtml(result.commitSha)}</code>${result.idempotent ? '\nوضعیت: <b>reconciled / idempotent</b>' : ''}\n\nشاخهٔ پیشفرض تغییر نکرده است.`, |
| 87 | reply_markup: { inline_keyboard: rows } |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | export function createWorkspaceController({ github, workspaces } = {}) { |
| 92 | if (!github) throw new Error('Workspace controller requires GitHub service'); |
| 93 | if (!workspaces) throw new Error('Workspace controller requires durable workspace store'); |
| 94 | |
| 95 | const requireWorkspace = async (workspaceId, ownerId) => { |
| 96 | const workspace = await workspaces.getWorkspace({ ownerId, workspaceId }); |
| 97 | if (!workspace) throw new Error('Workspace not found or expired'); |
| 98 | return workspace; |
| 99 | }; |
| 100 | |
| 101 | const renderDirectory = async (repoId, path, ownerId) => { |
| 102 | const [repo, items] = await Promise.all([ |
| 103 | github.getRepository(repoId), |
| 104 | github.listFiles(repoId, path) |
| 105 | ]); |
| 106 | const visible = items.slice(0, 30); |
| 107 | const handles = await workspaces.createNavigationRefs({ |
| 108 | ownerId, |
| 109 | refs: visible.map(item => ({ repoId, path: item.path, type: item.type, sha: item.sha })) |
| 110 | }); |
| 111 | const body = visible.length |
| 112 | ? visible.map(item => `${item.type === 'dir' ? '📁' : '📄'} <code>${escapeHtml(short(item.name, 100))}</code>${item.type === 'file' && item.size ? ` (${Number(item.size).toLocaleString()} B)` : ''}`).join('\n') |
| 113 | : '<i>این پوشه خالی است.</i>'; |
| 114 | const rows = visible.map((item, index) => [{ |
| 115 | text: `${item.type === 'dir' ? '📁' : '📄'} ${short(item.name, 46)}`, |
| 116 | callback_data: `wnav:${handles[index].refId}` |
| 117 | }]); |
| 118 | rows.push([ |
| 119 | { text: '🔄 بروزرسانی', callback_data: `files:${repoId}` }, |
| 120 | { text: '📦 پروژه', callback_data: `repo:${repoId}` } |
| 121 | ]); |
| 122 | return { |
| 123 | text: `📂 <b>فایلهای ${escapeHtml(repo.name)}</b>\n${path ? `مسیر: <code>${escapeHtml(path)}</code>\n` : ''}➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 124 | reply_markup: { inline_keyboard: rows } |
| 125 | }; |
| 126 | }; |
| 127 | |
| 128 | const getPinnedFile = async ref => { |
| 129 | const repo = await github.getRepository(ref.repoId); |
| 130 | const snapshot = await github.getBranchSnapshot(ref.repoId, repo.defaultBranch); |
| 131 | const file = await github.getFileAtRef(ref.repoId, ref.path, snapshot.headSha); |
| 132 | return { repo, snapshot, file }; |
| 133 | }; |
| 134 | |
| 135 | const renderFile = async (refId, ownerId) => { |
| 136 | const ref = await workspaces.resolveNavigationRef({ ownerId, refId }); |
| 137 | if (!ref || ref.type !== 'file') throw new Error('Workspace file reference is missing or expired'); |
| 138 | const { repo, snapshot, file } = await getPinnedFile(ref); |
| 139 | const policy = validateEditableFile({ path: file.path, text: file.content, byteLength: file.size }); |
| 140 | const preview = file.content.length > 2600 ? `${file.content.slice(0, 2600)}\n…` : file.content; |
| 141 | const reason = repo.archived |
| 142 | ? 'این مخزن آرشیوشده است و ویرایش فایل در آن غیرفعال است.' |
| 143 | : policy.reason; |
| 144 | const rows = []; |
| 145 | if (!repo.archived && policy.editable) rows.push([{ text: '✏️ Edit', callback_data: `wedit:${refId}` }]); |
| 146 | rows.push([{ text: '🔙 فایلها', callback_data: `files:${ref.repoId}` }, { text: '📦 پروژه', callback_data: `repo:${ref.repoId}` }]); |
| 147 | return { |
| 148 | text: `📄 <b>پیشنمایش فایل — ${escapeHtml(repo.name)}</b>\nمسیر: <code>${escapeHtml(file.path)}</code>\nRef: <code>${escapeHtml(snapshot.headSha.slice(0, 10))}</code>\n${reason ? `⚠️ ${escapeHtml(reason)}\n` : ''}➖➖➖➖➖➖➖➖➖➖\n<pre>${escapeHtml(preview)}</pre>`, |
| 149 | reply_markup: { inline_keyboard: rows } |
| 150 | }; |
| 151 | }; |
| 152 | |
| 153 | const createDraft = async (refId, ownerId) => { |
| 154 | const ref = await workspaces.resolveNavigationRef({ ownerId, refId }); |
| 155 | if (!ref || ref.type !== 'file') throw new Error('Workspace file reference is missing or expired'); |
| 156 | const { repo, snapshot, file } = await getPinnedFile(ref); |
| 157 | if (repo.archived) throw new Error('Archived repository is not editable'); |
| 158 | const policy = validateEditableFile({ path: file.path, text: file.content, byteLength: file.size }); |
| 159 | if (!policy.editable) throw new Error(policy.reason || 'File is not editable'); |
| 160 | const originalDigest = await digestText(file.content); |
| 161 | const workspace = await workspaces.createWorkspace({ |
| 162 | ownerId, |
| 163 | repoId: Number(ref.repoId), |
| 164 | repoFullName: repo.fullName, |
| 165 | baseBranch: repo.defaultBranch, |
| 166 | baseHeadSha: snapshot.headSha, |
| 167 | baseTreeSha: snapshot.treeSha, |
| 168 | path: file.path, |
| 169 | blobSha: file.sha, |
| 170 | originalText: file.content, |
| 171 | originalDigest |
| 172 | }); |
| 173 | return { |
| 174 | text: `📝 <b>Draft ساخته شد</b>\nمخزن: <code>${escapeHtml(repo.fullName)}</code>\nفایل: <code>${escapeHtml(file.path)}</code>\nBase: <code>${escapeHtml(snapshot.headSha.slice(0, 10))}</code>\n\nهنوز هیچ تغییری در GitHub انجام نشده است.`, |
| 175 | reply_markup: { inline_keyboard: [[{ text: '📝 باز کردن Draft', callback_data: `ws:${workspace.id}` }], [{ text: '🔙 فایلها', callback_data: `files:${ref.repoId}` }]] } |
| 176 | }; |
| 177 | }; |
| 178 | |
| 179 | const renderWorkspace = async (workspaceId, ownerId) => { |
| 180 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 181 | const changed = workspace.draftText !== workspace.originalText; |
| 182 | const commitMessage = workspace.commitMessage |
| 183 | ? `<code>${escapeHtml(short(workspace.commitMessage, 120))}</code>` |
| 184 | : '<i>ثبت نشده</i>'; |
| 185 | return { |
| 186 | text: `📝 <b>Repository Workspace</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\nوضعیت: <b>${escapeHtml(workspace.state || 'DRAFT')}</b>\nBase: <code>${escapeHtml(String(workspace.baseHeadSha || '').slice(0, 10))}</code>\nتغییر محلی: <b>${changed ? 'دارد' : 'ندارد'}</b>\nپیام Commit: ${commitMessage}\n\nتا قبل از تأیید نهایی، هیچ تغییری در GitHub انجام نمیشود.`, |
| 187 | reply_markup: { inline_keyboard: workspaceButtons(workspace) } |
| 188 | }; |
| 189 | }; |
| 190 | |
| 191 | const prepareInput = async (workspaceId, mode, ownerId) => { |
| 192 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 193 | await workspaces.setPendingInput({ ownerId, workspaceId, mode }); |
| 194 | const draftMode = mode === 'draft'; |
| 195 | return { |
| 196 | text: draftMode |
| 197 | ? `📝 <b>متن جدید Draft را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nکل محتوای فایل جایگزین میشود. سقف: 3,000 کاراکتر Unicode و 12,000 بایت UTF-8. پیام بعدی فقط یکبار مصرف میشود.` |
| 198 | : `✍️ <b>پیام Commit را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nپیام باید غیرخالی و حداکثر 120 کاراکتر باشد. پیام بعدی فقط یکبار مصرف میشود.`, |
| 199 | reply_markup: { inline_keyboard: [[{ text: '↩️ بازگشت به Draft', callback_data: `ws:${workspace.id}` }]] } |
| 200 | }; |
| 201 | }; |
| 202 | |
| 203 | const applyPendingInput = async ({ pending, text }, ownerId) => { |
| 204 | const workspaceId = String(pending?.workspaceId || ''); |
| 205 | const mode = String(pending?.mode || ''); |
| 206 | if (!new RegExp(`^${HANDLE_10}$`).test(workspaceId) || !['draft', 'commit_message'].includes(mode)) { |
| 207 | throw new Error('Pending workspace input is invalid'); |
| 208 | } |
| 209 | |
| 210 | const consumed = await workspaces.consumePendingInput({ ownerId, workspaceId, mode }); |
| 211 | if (!consumed) throw new Error('Pending workspace input was already consumed or expired'); |
| 212 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 213 | |
| 214 | if (mode === 'draft') { |
| 215 | const policy = validateEditableFile({ path: workspace.path, text }); |
| 216 | if (!policy.editable) { |
| 217 | return { |
| 218 | text: `⚠️ <b>Draft تغییر نکرد</b>\n${escapeHtml(policy.reason || 'متن ارسالشده مجاز نیست.')}\n\nاین ورودی مصرف شد؛ برای تلاش دوباره «متن Draft» را بزنید.`, |
| 219 | reply_markup: { inline_keyboard: [[{ text: '📝 بازگشت به Draft', callback_data: `ws:${workspace.id}` }]] } |
| 220 | }; |
| 221 | } |
| 222 | const digest = await digestText(policy.text); |
| 223 | const updated = await workspaces.updateDraft({ ownerId, workspaceId, text: policy.text, digest }); |
| 224 | return { |
| 225 | text: `✅ <b>Draft محلی ذخیره شد</b>\nفایل: <code>${escapeHtml(updated.path)}</code>\nکاراکتر: <b>${policy.charLength}</b> · بایت UTF-8: <b>${policy.byteLength}</b>\n\nهنوز هیچ تغییری در GitHub انجام نشده است.`, |
| 226 | reply_markup: { inline_keyboard: workspaceButtons(updated) } |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | let message; |
| 231 | try { |
| 232 | message = normalizeCommitMessage(text); |
| 233 | } catch (error) { |
| 234 | return { |
| 235 | text: `⚠️ <b>پیام Commit ذخیره نشد</b>\n${escapeHtml(error?.message || 'Commit message is invalid')}\n\nاین ورودی مصرف شد؛ برای تلاش دوباره «پیام Commit» را بزنید.`, |
| 236 | reply_markup: { inline_keyboard: [[{ text: '📝 بازگشت به Draft', callback_data: `ws:${workspace.id}` }]] } |
| 237 | }; |
| 238 | } |
| 239 | const digest = await digestText(message); |
| 240 | const updated = await workspaces.setCommitMessage({ ownerId, workspaceId, message, digest }); |
| 241 | return { |
| 242 | text: `✅ <b>پیام Commit ذخیره شد</b>\n<code>${escapeHtml(message)}</code>\n\nهنوز هیچ تغییری در GitHub انجام نشده است.`, |
| 243 | reply_markup: { inline_keyboard: workspaceButtons(updated) } |
| 244 | }; |
| 245 | }; |
| 246 | |
| 247 | const renderDiff = async (workspaceId, ownerId) => { |
| 248 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 249 | const diff = buildUnifiedDiff({ |
| 250 | path: workspace.path, |
| 251 | before: workspace.originalText, |
| 252 | after: workspace.draftText, |
| 253 | maxChars: 2400 |
| 254 | }); |
| 255 | const truncation = diff.truncated ? '\n⚠️ نمایش Diff کوتاه شده؛ شمارشها بر اساس محتوای کامل هستند.' : ''; |
| 256 | return { |
| 257 | text: `🔍 <b>Diff محلی</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n+${diff.additions} / -${diff.deletions}${truncation}\n➖➖➖➖➖➖➖➖➖➖\n<pre>${escapeHtml(diff.text)}</pre>`, |
| 258 | reply_markup: { inline_keyboard: [[{ text: '📝 Draft', callback_data: `ws:${workspace.id}` }, { text: '✅ آماده Commit', callback_data: `wready:${workspace.id}` }]] } |
| 259 | }; |
| 260 | }; |
| 261 | |
| 262 | const sealReadyIntent = async (workspaceId, ownerId) => { |
| 263 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 264 | if (workspace.draftText === workspace.originalText) throw new Error('No draft change to commit'); |
| 265 | const commitMessage = normalizeCommitMessage(workspace.commitMessage); |
| 266 | const contentDigest = await digestText(workspace.draftText); |
| 267 | const commitMessageDigest = await digestText(commitMessage); |
| 268 | const targetBranch = makeTargetBranch(workspace.id); |
| 269 | const createdAt = Number(workspace.createdAt); |
| 270 | const commitTimestamp = new Date(Number.isFinite(createdAt) ? createdAt : 0).toISOString(); |
| 271 | const material = { |
| 272 | repoId: Number(workspace.repoId), |
| 273 | workspaceId: workspace.id, |
| 274 | baseBranch: workspace.baseBranch, |
| 275 | baseHeadSha: workspace.baseHeadSha, |
| 276 | baseTreeSha: workspace.baseTreeSha, |
| 277 | path: workspace.path, |
| 278 | sourceBlobSha: workspace.blobSha, |
| 279 | contentDigest, |
| 280 | commitMessageDigest, |
| 281 | targetBranch, |
| 282 | commitTimestamp |
| 283 | }; |
| 284 | const intentDigest = await digestText(JSON.stringify(material)); |
| 285 | const intent = await workspaces.sealIntent({ |
| 286 | ownerId, |
| 287 | workspaceId, |
| 288 | intent: { ...material, intentDigest } |
| 289 | }); |
| 290 | if (!intent?.id || !/^[A-Za-z0-9_-]{8}$/.test(intent.id)) throw new Error('Commit intent ID is invalid'); |
| 291 | return { |
| 292 | text: `✅ <b>آمادهٔ تأیید Commit</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\nBranch: <code>${escapeHtml(targetBranch)}</code>\nBase: <code>${escapeHtml(String(workspace.baseHeadSha).slice(0, 10))}</code>\n\nمرحلهٔ بعد فقط درخواست تأیید امن را میسازد؛ تا تأیید معتبر مصرف نشود GitHub WRITE اجرا نمیشود.`, |
| 293 | reply_markup: { |
| 294 | inline_keyboard: [ |
| 295 | [{ text: '✅ تأیید و Commit', callback_data: `wscommit:${workspace.repoId}:${intent.id}` }], |
| 296 | [{ text: '🔍 Diff', callback_data: `wdiff:${workspace.id}` }, { text: '📝 Draft', callback_data: `ws:${workspace.id}` }] |
| 297 | ] |
| 298 | } |
| 299 | }; |
| 300 | }; |
| 301 | |
| 302 | const cancelDraft = async (workspaceId, ownerId) => { |
| 303 | const workspace = await requireWorkspace(workspaceId, ownerId); |
| 304 | await workspaces.cancelWorkspace({ ownerId, workspaceId }); |
| 305 | return { |
| 306 | text: `🗑 <b>Draft لغو شد</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\nهیچ تغییری در GitHub انجام نشد.`, |
| 307 | reply_markup: { inline_keyboard: [[{ text: '🔙 فایلها', callback_data: `files:${workspace.repoId}` }]] } |
| 308 | }; |
| 309 | }; |
| 310 | |
| 311 | return { |
| 312 | resolveCallback(data, context = {}) { |
| 313 | const parsed = parseCallback(data); |
| 314 | if (!parsed) return null; |
| 315 | |
| 316 | if (parsed.kind === 'commit') { |
| 317 | return { |
| 318 | actionId: parsed.actionId, |
| 319 | approval: { |
| 320 | input: { repoId: parsed.repoId, intentId: parsed.intentId }, |
| 321 | label: 'Commit روی branch جدید', |
| 322 | cancelCallback: `repo:${parsed.repoId}` |
| 323 | }, |
| 324 | audit: { repoId: parsed.repoId, intentId: parsed.intentId }, |
| 325 | execute: async () => { |
| 326 | const ownerId = ownerFrom(context); |
| 327 | const result = await commitWorkspaceIntent({ |
| 328 | github, |
| 329 | workspaces, |
| 330 | ownerId, |
| 331 | repoId: parsed.repoId, |
| 332 | intentId: parsed.intentId |
| 333 | }); |
| 334 | const intent = await workspaces.getIntent({ ownerId, intentId: parsed.intentId }); |
| 335 | const view = workspaceCommitResultView(parsed.repoId, intent, result); |
| 336 | return { |
| 337 | ...view, |
| 338 | audit: { |
| 339 | repoId: parsed.repoId, |
| 340 | intentId: parsed.intentId, |
| 341 | workspaceId: intent?.workspaceId, |
| 342 | path: intent?.path, |
| 343 | contentDigest: intent?.contentDigest, |
| 344 | commitMessageDigest: intent?.commitMessageDigest, |
| 345 | baseHeadSha: intent?.baseHeadSha, |
| 346 | targetBranch: intent?.targetBranch, |
| 347 | commitSha: result.commitSha, |
| 348 | branch: result.branch, |
| 349 | idempotent: result.idempotent |
| 350 | } |
| 351 | }; |
| 352 | } |
| 353 | }; |
| 354 | } |
| 355 | |
| 356 | return { |
| 357 | actionId: parsed.actionId, |
| 358 | execute: async () => { |
| 359 | const ownerId = ownerFrom(context); |
| 360 | if (parsed.kind === 'root') return renderDirectory(parsed.repoId, '', ownerId); |
| 361 | if (parsed.kind === 'workspace') return renderWorkspace(parsed.workspaceId, ownerId); |
| 362 | if (parsed.kind === 'edit') return createDraft(parsed.refId, ownerId); |
| 363 | if (parsed.kind === 'input') return prepareInput(parsed.workspaceId, parsed.mode, ownerId); |
| 364 | if (parsed.kind === 'diff') return renderDiff(parsed.workspaceId, ownerId); |
| 365 | if (parsed.kind === 'ready') return sealReadyIntent(parsed.workspaceId, ownerId); |
| 366 | if (parsed.kind === 'cancel') return cancelDraft(parsed.workspaceId, ownerId); |
| 367 | const ref = await workspaces.resolveNavigationRef({ ownerId, refId: parsed.refId }); |
| 368 | if (!ref) throw new Error('Workspace navigation reference is missing or expired'); |
| 369 | return ref.type === 'dir' |
| 370 | ? renderDirectory(ref.repoId, ref.path, ownerId) |
| 371 | : renderFile(parsed.refId, ownerId); |
| 372 | } |
| 373 | }; |
| 374 | }, |
| 375 | |
| 376 | resolveMessage(payload, context = {}) { |
| 377 | const pending = payload?.pending; |
| 378 | if (!pending || !['draft', 'commit_message'].includes(String(pending.mode || ''))) return null; |
| 379 | if (!new RegExp(`^${HANDLE_10}$`).test(String(pending.workspaceId || ''))) return null; |
| 380 | return { |
| 381 | actionId: 'github.workspace.input.apply', |
| 382 | execute: async () => applyPendingInput({ pending, text: String(payload?.text ?? '') }, ownerFrom(context)) |
| 383 | }; |
| 384 | } |
| 385 | }; |
| 386 | } |