-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
916 lines (792 loc) · 28.1 KB
/
index.ts
File metadata and controls
916 lines (792 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/**
* Wright Telegram Bot -- human-in-the-loop interface.
*
* Receives dev task requests via Telegram, queues them in Supabase, and
* streams progress back to the user. When a PR is created, sends inline
* keyboard buttons for approve / reject.
*/
import { execFileSync } from 'child_process'
import { Bot, InlineKeyboard, type Context } from 'grammy'
import { JOB_STATUS, type Job, type JobEvent } from '@wright/shared'
import {
insertJob,
getJob,
getJobByPrefix,
cancelJob,
getJobEvents,
subscribeToJobEvents,
subscribeToJobUpdates,
} from './supabase.js'
// ---------------------------------------------------------------------------
// Environment validation
// ---------------------------------------------------------------------------
const BOT_TOKEN = process.env.BOT_TOKEN
if (!BOT_TOKEN) {
console.error('Fatal: BOT_TOKEN environment variable is not set.')
process.exit(1)
}
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
if (!GITHUB_TOKEN) {
console.error('Fatal: GITHUB_TOKEN environment variable is not set.')
process.exit(1)
}
const WORKER_URL = process.env.WORKER_URL || 'https://wright-worker.fly.dev'
/**
* Wake the worker by hitting its health endpoint.
* This triggers Fly.io auto-start if the machine is stopped.
* Errors are silently ignored — the request just needs to reach Fly's proxy.
*/
async function wakeWorker(): Promise<void> {
try {
await fetch(`${WORKER_URL}/health`, { signal: AbortSignal.timeout(5000) })
console.log('[wake] Worker pinged successfully')
} catch {
// Ignore — the request just needs to trigger Fly.io auto-start.
// The machine may take a few seconds to boot, so a timeout is expected.
console.log('[wake] Worker ping sent (may be booting)')
}
}
// ---------------------------------------------------------------------------
// Bot setup
// ---------------------------------------------------------------------------
const bot = new Bot(BOT_TOKEN)
// ---------------------------------------------------------------------------
// Notification mode: 'verbose' (default) or 'quiet', keyed by chat ID
// ---------------------------------------------------------------------------
// TODO: persist to Supabase so mode survives bot restarts
const chatNotifyMode = new Map<number, 'verbose' | 'quiet'>()
/** Events that are skipped entirely in quiet mode. */
const QUIET_EVENTS = new Set(['edit', 'test_run', 'cloned'])
/** Events that always deliver with notification sound regardless of mode. */
const LOUD_EVENTS = new Set(['pr_created', 'completed'])
// ---------------------------------------------------------------------------
// Authorization middleware — restrict to known Telegram users
// ---------------------------------------------------------------------------
const ALLOWED_TELEGRAM_USERS = process.env.ALLOWED_TELEGRAM_USERS
? process.env.ALLOWED_TELEGRAM_USERS.split(',')
.map((id) => parseInt(id.trim(), 10))
.filter(Number.isFinite)
: []
if (ALLOWED_TELEGRAM_USERS.length > 0) {
bot.use(async (ctx, next) => {
const userId = ctx.from?.id
if (!userId || !ALLOWED_TELEGRAM_USERS.includes(userId)) {
await ctx.reply('Unauthorized. Your user ID: ' + (userId ?? 'unknown'))
return
}
await next()
})
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Format a Job into a human-readable status block. */
function formatJobStatus(job: Job): string {
const statusEmoji: Record<string, string> = {
queued: '\u{1F7E1}', // yellow circle
claimed: '\u{1F535}', // blue circle
running: '\u{1F7E2}', // green circle
succeeded: '\u{2705}', // check mark
failed: '\u{274C}', // cross mark
}
const icon = statusEmoji[job.status] ?? '\u{2753}'
const lines = [
`${icon} <b>Job ${job.id.slice(0, 8)}</b>`,
`<b>Status:</b> ${job.status}`,
`<b>Repo:</b> <code>${job.repo_url}</code>`,
`<b>Task:</b> ${escapeHtml(job.task)}`,
`<b>Cost:</b> $${job.total_cost_usd.toFixed(4)}`,
]
if (job.pr_url) {
lines.push(`<b>PR:</b> ${job.pr_url}`)
}
if (job.error) {
lines.push(`<b>Error:</b> <code>${escapeHtml(job.error)}</code>`)
}
if (job.completed_at) {
lines.push(`<b>Completed:</b> ${job.completed_at}`)
}
return lines.join('\n')
}
/** Format a JobEvent into a one-line progress message. */
function formatJobEvent(event: JobEvent): string {
const typeLabels: Record<string, string> = {
claimed: '\u{1F4CB} Job claimed by worker',
cloned: '\u{1F4E5} Repository cloned',
loop_start: `\u{1F504} Loop ${event.loop_number ?? '?'} started`,
edit: '\u{270F}\u{FE0F} Files edited',
test_run: '\u{1F9EA} Running tests...',
test_pass: '\u{2705} Tests passed!',
test_fail: '\u{274C} Tests failed',
pr_created: '\u{1F389} Pull request created!',
completed: '\u{2705} Job completed successfully',
error: '\u{1F6A8} Error occurred',
budget_exceeded: '\u{1F4B8} Budget limit exceeded',
}
let text = typeLabels[event.event_type] ?? `Event: ${event.event_type}`
// Append payload summary for certain event types
if (event.event_type === 'test_fail' && event.payload) {
const p = event.payload as Record<string, unknown>
if (p.passed !== undefined && p.failed !== undefined) {
text += ` (${p.passed} passed, ${p.failed} failed)`
}
}
if (event.event_type === 'error' && event.payload) {
const p = event.payload as Record<string, unknown>
if (p.message) {
text += `: ${String(p.message).slice(0, 200)}`
}
}
return text
}
/** Escape HTML special characters for Telegram HTML parse mode. */
function escapeHtml(text: string): string {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
/** Validate a URL loosely -- must look like a git-cloneable repository. */
function isValidRepoUrl(url: string): boolean {
try {
// Accept https:// URLs and git@ SSH URLs
if (url.startsWith('git@')) return true
const parsed = new URL(url)
return parsed.protocol === 'https:' || parsed.protocol === 'http:'
} catch {
return false
}
}
/**
* Extract a job ID from a bot message (looks for patterns like "Job ID: <uuid>"
* or "[<short-id>]" in the message text).
*/
function extractJobIdFromMessage(text: string): string | null {
// Full UUID pattern: "Job ID: xxxxxxxx-xxxx-..."
const fullMatch = text.match(/Job ID:\s*([0-9a-f-]{36})/i)
if (fullMatch) return fullMatch[1]
// Full UUID inside "Job xxxxxxxx" (short ID in status messages)
const shortMatch = text.match(/Job\s+([0-9a-f]{8})\b/i)
if (shortMatch) return shortMatch[1]
// Bracket prefix: "[xxxxxxxx]"
const bracketMatch = text.match(/\[([0-9a-f]{8})\]/)
if (bracketMatch) return bracketMatch[1]
return null
}
/**
* Parse a GitHub PR URL into its owner, repo, and PR number components.
*
* Accepts URLs like: https://github.com/owner/repo/pull/123
*/
function parsePrUrl(prUrl: string): { owner: string; repo: string; number: number } | null {
const match = prUrl.match(/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/)
if (!match) return null
return { owner: match[1], repo: match[2], number: parseInt(match[3], 10) }
}
/**
* Merge a pull request via the GitHub REST API.
*/
async function mergePullRequest(owner: string, repo: string, prNumber: number): Promise<void> {
const res = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/merge`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify({
merge_method: 'squash',
}),
},
)
if (!res.ok) {
const body = await res.text()
throw new Error(`GitHub merge failed (${res.status}): ${body}`)
}
}
/**
* Close a pull request via the GitHub REST API (without merging).
*/
async function closePullRequest(owner: string, repo: string, prNumber: number): Promise<void> {
const res = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`,
{
method: 'PATCH',
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify({
state: 'closed',
}),
},
)
if (!res.ok) {
const body = await res.text()
throw new Error(`GitHub close failed (${res.status}): ${body}`)
}
}
/** Build PR approval inline keyboard. */
function buildPrKeyboard(jobId: string, prUrl: string): InlineKeyboard {
return new InlineKeyboard()
.url('View PR', prUrl)
.row()
.text('\u{2705} Approve & Merge', `approve:${jobId}`)
.text('\u{274C} Reject & Close', `reject:${jobId}`)
}
// ---------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------
bot.command('start', async (ctx: Context) => {
await ctx.reply(
[
'<b>Wright Dev Automation Bot</b>',
'',
'I automate dev tasks: clone a repo, apply changes with Claude, run tests, and create a PR.',
'',
'<b>Commands:</b>',
'/task <repo_url> <description> -- Submit a dev task',
'/task <pr_url> <feedback> -- Revise an existing PR',
'/revise <job_id> <feedback> -- Revise a job\'s PR',
'/status <job_id> -- Check job status',
'/cancel <job_id> -- Cancel a running job',
'/verbose -- Enable all event notifications (default)',
'/quiet -- Only send milestone notifications; silence noisy events',
'',
'You can also <b>reply</b> to any job message with feedback to revise its PR.',
'',
'When a PR is ready, I will send approve/reject buttons.',
].join('\n'),
{ parse_mode: 'HTML' },
)
})
bot.command('verbose', async (ctx: Context) => {
const chatId = ctx.chat!.id
chatNotifyMode.set(chatId, 'verbose')
await ctx.reply('\u{1F50A} Verbose mode enabled. You will receive all event notifications.')
})
bot.command('quiet', async (ctx: Context) => {
const chatId = ctx.chat!.id
chatNotifyMode.set(chatId, 'quiet')
await ctx.reply(
'\u{1F514} Quiet mode enabled. '
+ 'Only milestone notifications (loop start, test results, PR created, completed, errors) will be sent. '
+ 'Noisy events (edits, test runs, cloned) are silenced.',
)
})
bot.command('revise', async (ctx: Context) => {
const text = ctx.message?.text ?? ''
const parts = text.split(/\s+/)
if (parts.length < 3) {
await ctx.reply(
'Usage: <code>/revise <job_id> <feedback></code>\n\n'
+ 'Pushes changes to the existing PR branch based on your feedback.',
{ parse_mode: 'HTML' },
)
return
}
const jobIdInput = parts[1]
const feedback = parts.slice(2).join(' ')
try {
// Look up the original job — support both full UUID and 8-char prefix
const originalJob = await getJob(jobIdInput) ?? await getJobByPrefix(jobIdInput)
if (!originalJob) {
await ctx.reply(
`No job found with ID <code>${escapeHtml(jobIdInput)}</code>.`,
{ parse_mode: 'HTML' },
)
return
}
if (!originalJob.pr_url) {
await ctx.reply('That job has no PR yet. Cannot revise.')
return
}
// Determine the feature branch from the original job
const featureBranch = originalJob.feature_branch || `wright/${originalJob.id.slice(0, 8)}`
const ack = await ctx.reply(
`\u{1F504} Queuing revision for <code>${featureBranch}</code>...`,
{ parse_mode: 'HTML' },
)
const job = await insertJob({
repoUrl: originalJob.repo_url,
task: feedback,
chatId: ctx.chat!.id,
messageId: ack.message_id,
githubToken: GITHUB_TOKEN,
branch: originalJob.branch,
featureBranch,
parentJobId: originalJob.id,
})
// Wake the worker so it picks up the revision job
wakeWorker()
await ctx.reply(
[
'\u{1F504} <b>Revision queued!</b>',
'',
`<b>Job ID:</b> <code>${job.id}</code>`,
`<b>Revising:</b> <code>${originalJob.id.slice(0, 8)}</code>`,
`<b>Branch:</b> <code>${featureBranch}</code>`,
`<b>Feedback:</b> ${escapeHtml(feedback)}`,
'',
'The worker will push changes to the existing PR branch.',
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Failed to queue revision: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
})
bot.command('task', async (ctx: Context) => {
const text = ctx.message?.text ?? ''
// Parse: /task <repo_url> <description...>
// The command itself may include @botname, so split on whitespace
const parts = text.split(/\s+/)
// parts[0] = "/task" or "/task@botname"
if (parts.length < 3) {
await ctx.reply(
'Usage: <code>/task <repo_url> <description></code>\n\n'
+ 'Example:\n'
+ '<code>/task https://github.com/org/repo Fix the login button styling</code>',
{ parse_mode: 'HTML' },
)
return
}
const urlArg = parts[1]
const description = parts.slice(2).join(' ')
// Detect PR URL — treat as revision of that PR
const prInfo = parsePrUrl(urlArg)
if (prInfo) {
if (description.length < 5) {
await ctx.reply('Please provide feedback for the PR revision (at least 5 characters).')
return
}
const ack = await ctx.reply(
`\u{1F504} Detected PR #${prInfo.number}. Queuing revision...`,
{ parse_mode: 'HTML' },
)
try {
// Look up the PR's head branch via GitHub API
const ghEnv: Record<string, string> = {
PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin',
HOME: process.env.HOME || '/home/wright',
}
if (GITHUB_TOKEN) ghEnv.GH_TOKEN = GITHUB_TOKEN
const nwo = `${prInfo.owner}/${prInfo.repo}`
const headRef = execFileSync(
'gh',
['api', `repos/${nwo}/pulls/${prInfo.number}`, '--jq', '.head.ref'],
{ encoding: 'utf-8', env: ghEnv },
).trim()
// Try to find the original job that created this branch so we can link them
const { getSupabase } = await import('./supabase.js')
const sb = getSupabase()
const { data: parentJobs } = await sb
.from('job_queue')
.select('id')
.or(`feature_branch.eq.${headRef},id.like.${headRef.replace('wright/', '')}%`)
.not('pr_url', 'is', null)
.order('created_at', { ascending: false })
.limit(1)
const parentJobId = parentJobs?.[0]?.id
const job = await insertJob({
repoUrl: `https://github.com/${prInfo.owner}/${prInfo.repo}`,
task: description,
chatId: ctx.chat!.id,
messageId: ack.message_id,
githubToken: GITHUB_TOKEN,
featureBranch: headRef,
parentJobId,
})
// Wake the worker so it picks up the PR revision job
wakeWorker()
await ctx.reply(
[
'\u{1F504} <b>PR revision queued!</b>',
'',
`<b>Job ID:</b> <code>${job.id}</code>`,
`<b>PR:</b> #${prInfo.number}`,
`<b>Branch:</b> <code>${headRef}</code>`,
`<b>Feedback:</b> ${escapeHtml(description)}`,
'',
'The worker will push changes to the existing PR branch.',
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Failed to queue PR revision: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
return
}
// Normal task: repo URL + description
const repoUrl = urlArg
if (!isValidRepoUrl(repoUrl)) {
await ctx.reply(
'That does not look like a valid repository URL. '
+ 'Please provide an HTTPS or git@ URL.',
)
return
}
if (description.length < 5) {
await ctx.reply('Please provide a more detailed task description (at least 5 characters).')
return
}
// Send an acknowledgment message first -- we will store its message_id
const ack = await ctx.reply(
`\u{23F3} Queuing task for <code>${escapeHtml(repoUrl)}</code>...`,
{ parse_mode: 'HTML' },
)
try {
const job = await insertJob({
repoUrl,
task: description,
chatId: ctx.chat!.id,
messageId: ack.message_id,
githubToken: GITHUB_TOKEN,
})
// Wake the worker so it picks up the new job
wakeWorker()
await ctx.reply(
[
'\u{2705} <b>Job queued!</b>',
'',
`<b>Job ID:</b> <code>${job.id}</code>`,
`<b>Repo:</b> <code>${escapeHtml(job.repo_url)}</code>`,
`<b>Task:</b> ${escapeHtml(job.task)}`,
`<b>Max loops:</b> ${job.max_loops}`,
`<b>Budget:</b> $${job.max_budget_usd.toFixed(2)}`,
'',
'I will notify you as the worker picks it up and makes progress.',
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Failed to queue job: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
})
bot.command('status', async (ctx: Context) => {
const text = ctx.message?.text ?? ''
const parts = text.split(/\s+/)
if (parts.length < 2) {
await ctx.reply(
'Usage: <code>/status <job_id></code>',
{ parse_mode: 'HTML' },
)
return
}
const jobId = parts[1]
try {
const job = await getJob(jobId)
if (!job) {
await ctx.reply(`No job found with ID <code>${escapeHtml(jobId)}</code>.`, {
parse_mode: 'HTML',
})
return
}
let reply = formatJobStatus(job)
// Append recent events
const events = await getJobEvents(jobId, 10)
if (events.length > 0) {
reply += '\n\n<b>Recent events:</b>\n'
reply += events.map((e) => ` ${formatJobEvent(e)}`).join('\n')
}
// If there is a PR, include approve/reject buttons
if (job.pr_url && job.status === JOB_STATUS.SUCCEEDED) {
await ctx.reply(reply, {
parse_mode: 'HTML',
reply_markup: buildPrKeyboard(job.id, job.pr_url),
})
} else {
await ctx.reply(reply, { parse_mode: 'HTML' })
}
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Error fetching status: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
})
bot.command('cancel', async (ctx: Context) => {
const text = ctx.message?.text ?? ''
const parts = text.split(/\s+/)
if (parts.length < 2) {
await ctx.reply(
'Usage: <code>/cancel <job_id></code>',
{ parse_mode: 'HTML' },
)
return
}
const jobId = parts[1]
try {
const job = await cancelJob(jobId)
if (!job) {
await ctx.reply(
`Could not cancel job <code>${escapeHtml(jobId)}</code>. `
+ 'It may have already completed or does not exist.',
{ parse_mode: 'HTML' },
)
return
}
await ctx.reply(
`\u{1F6D1} Job <code>${job.id.slice(0, 8)}</code> has been cancelled.`,
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Error cancelling job: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
})
// ---------------------------------------------------------------------------
// Reply-based revision — reply to any job message to revise the PR
// ---------------------------------------------------------------------------
bot.on('message:text', async (ctx, next) => {
const reply = ctx.message.reply_to_message
// Only handle replies to bot messages that contain a job ID
if (!reply || reply.from?.id !== ctx.me.id) {
return next()
}
const replyText = reply.text || reply.caption || ''
const jobIdPrefix = extractJobIdFromMessage(replyText)
if (!jobIdPrefix) return next()
const feedback = ctx.message.text
// Ignore commands — let command handlers take care of those
if (feedback.startsWith('/')) return next()
if (feedback.length < 5) {
await ctx.reply('Please provide more detailed feedback (at least 5 characters).')
return
}
try {
// Look up the original job — try full ID first, then prefix match
const originalJob = await getJob(jobIdPrefix) ?? await getJobByPrefix(jobIdPrefix)
if (!originalJob) {
await ctx.reply(
`Could not find job <code>${escapeHtml(jobIdPrefix)}</code>.`,
{ parse_mode: 'HTML' },
)
return
}
if (!originalJob.pr_url) {
await ctx.reply('That job has no PR yet. Cannot revise.')
return
}
const featureBranch = originalJob.feature_branch || `wright/${originalJob.id.slice(0, 8)}`
const ack = await ctx.reply(
`\u{1F504} Queuing revision for <code>${featureBranch}</code> based on your reply...`,
{ parse_mode: 'HTML' },
)
const job = await insertJob({
repoUrl: originalJob.repo_url,
task: feedback,
chatId: ctx.chat!.id,
messageId: ack.message_id,
githubToken: GITHUB_TOKEN,
branch: originalJob.branch,
featureBranch,
parentJobId: originalJob.id,
})
// Wake the worker so it picks up the revision job
wakeWorker()
await ctx.reply(
[
'\u{1F504} <b>Revision queued from reply!</b>',
'',
`<b>Job ID:</b> <code>${job.id}</code>`,
`<b>Revising:</b> <code>${originalJob.id.slice(0, 8)}</code>`,
`<b>Branch:</b> <code>${featureBranch}</code>`,
`<b>Feedback:</b> ${escapeHtml(feedback.slice(0, 200))}`,
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.reply(
`\u{274C} Failed to queue revision: <code>${escapeHtml(msg)}</code>`,
{ parse_mode: 'HTML' },
)
}
})
// ---------------------------------------------------------------------------
// Inline keyboard callback queries (approve / reject PRs)
// ---------------------------------------------------------------------------
bot.callbackQuery(/^approve:(.+)$/, async (ctx) => {
const jobId = ctx.match![1]
try {
const job = await getJob(jobId)
if (!job || !job.pr_url) {
await ctx.answerCallbackQuery({ text: 'Job or PR not found.' })
return
}
const parsed = parsePrUrl(job.pr_url)
if (!parsed) {
await ctx.answerCallbackQuery({ text: 'Could not parse PR URL.' })
return
}
await ctx.answerCallbackQuery({ text: 'Merging PR...' })
await mergePullRequest(parsed.owner, parsed.repo, parsed.number)
await ctx.editMessageText(
[
`\u{2705} <b>PR merged successfully</b>`,
'',
`<b>Job:</b> <code>${job.id.slice(0, 8)}</code>`,
`<b>PR:</b> ${job.pr_url}`,
'',
`Merged <code>${parsed.owner}/${parsed.repo}#${parsed.number}</code> via squash merge.`,
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.answerCallbackQuery({
text: `Error: ${msg.slice(0, 180)}`,
show_alert: true,
})
}
})
bot.callbackQuery(/^reject:(.+)$/, async (ctx) => {
const jobId = ctx.match![1]
try {
const job = await getJob(jobId)
if (!job || !job.pr_url) {
await ctx.answerCallbackQuery({ text: 'Job or PR not found.' })
return
}
const parsed = parsePrUrl(job.pr_url)
if (!parsed) {
await ctx.answerCallbackQuery({ text: 'Could not parse PR URL.' })
return
}
await ctx.answerCallbackQuery({ text: 'Closing PR...' })
await closePullRequest(parsed.owner, parsed.repo, parsed.number)
await ctx.editMessageText(
[
`\u{274C} <b>PR rejected and closed</b>`,
'',
`<b>Job:</b> <code>${job.id.slice(0, 8)}</code>`,
`<b>PR:</b> ${job.pr_url}`,
'',
`Closed <code>${parsed.owner}/${parsed.repo}#${parsed.number}</code> without merging.`,
].join('\n'),
{ parse_mode: 'HTML' },
)
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
await ctx.answerCallbackQuery({
text: `Error: ${msg.slice(0, 180)}`,
show_alert: true,
})
}
})
// ---------------------------------------------------------------------------
// Supabase realtime -> Telegram bridge
// ---------------------------------------------------------------------------
/**
* Forward job events to the originating Telegram chat.
*
* When a job has telegram_chat_id set, each new event is sent as a message
* to that chat. PR creation events include the inline approval keyboard.
*/
function startRealtimeBridge(): void {
// Stream individual events
subscribeToJobEvents(async (event: JobEvent) => {
try {
// Look up the job to find the chat ID
const job = await getJob(event.job_id)
if (!job?.telegram_chat_id) return
const chatId = job.telegram_chat_id
const mode = chatNotifyMode.get(chatId) ?? 'verbose'
// In quiet mode, skip noisy events entirely
if (mode === 'quiet' && QUIET_EVENTS.has(event.event_type)) return
const text = `<b>[${job.id.slice(0, 8)}]</b> ${formatJobEvent(event)}`
// In quiet mode, deliver silently unless this is a loud event
const disableNotification = mode === 'quiet' && !LOUD_EVENTS.has(event.event_type)
// PR created events get the approval keyboard
if (event.event_type === 'pr_created' && job.pr_url) {
await bot.api.sendMessage(chatId, text, {
parse_mode: 'HTML',
reply_markup: buildPrKeyboard(job.id, job.pr_url),
disable_notification: disableNotification,
})
} else {
await bot.api.sendMessage(chatId, text, {
parse_mode: 'HTML',
disable_notification: disableNotification,
})
}
} catch (err) {
// Log but do not crash -- the subscription must stay alive
console.error('Error forwarding job event to Telegram:', err)
}
})
// Stream status changes (for terminal states)
subscribeToJobUpdates(async (job: Job) => {
if (!job.telegram_chat_id) return
const terminal = [JOB_STATUS.SUCCEEDED, JOB_STATUS.FAILED] as string[]
if (!terminal.includes(job.status)) return
try {
const chatId = job.telegram_chat_id
const mode = chatNotifyMode.get(chatId) ?? 'verbose'
const text = formatJobStatus(job)
if (job.pr_url && job.status === JOB_STATUS.SUCCEEDED) {
await bot.api.sendMessage(chatId, text, {
parse_mode: 'HTML',
reply_markup: buildPrKeyboard(job.id, job.pr_url),
})
} else {
// Terminal failure: respect quiet mode (silent delivery)
const disableNotification = mode === 'quiet' && job.status === JOB_STATUS.FAILED
await bot.api.sendMessage(chatId, text, {
parse_mode: 'HTML',
disable_notification: disableNotification,
})
}
} catch (err) {
console.error('Error forwarding job status update to Telegram:', err)
}
})
console.log('Supabase realtime bridge started.')
}
// ---------------------------------------------------------------------------
// Error handling
// ---------------------------------------------------------------------------
bot.catch((err) => {
console.error('Unhandled bot error:', err)
})
// ---------------------------------------------------------------------------
// Startup
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
console.log('Wright Telegram bot starting...')
// Start the realtime bridge (Supabase -> Telegram).
// This will throw if SUPABASE_URL / SUPABASE_KEY are missing, which is
// intentional -- we want a loud failure at startup.
startRealtimeBridge()
// Start long polling. This will block until the process is stopped.
console.log('Bot is now polling for updates.')
await bot.start({
onStart: (botInfo) => {
console.log(`Bot @${botInfo.username} is running.`)
},
})
}
main().catch((err) => {
console.error('Fatal error:', err)
process.exit(1)
})