-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.ts
More file actions
1124 lines (981 loc) · 33.7 KB
/
github.ts
File metadata and controls
1124 lines (981 loc) · 33.7 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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* GitHub Issue & PR Integration Plugin for OpenCode
*
* Two-way integration with GitHub:
* 1. Posts agent messages to the associated GitHub issue as comments.
* 2. Monitors GitHub issue/PR comments for actionable directives (ending with "Act.")
* and injects them into the active OpenCode session.
*
* Issue Detection Priority:
* 1. GitHub issue URL in first message
* 2. .github-issue file in project root
* 3. PR's closingIssuesReferences (via gh CLI)
* 4. Branch name convention (issue-123, fix/123-desc, etc.)
* 5. Create new issue with task description
*
* Configure in ~/.config/opencode/github.json:
* {
* "enabled": true,
* "postUserMessages": false,
* "postAssistantMessages": true,
* "postToolCalls": false,
* "batchInterval": 5000,
* "createIssueIfMissing": true,
* "issueLabels": ["opencode", "ai-session"],
* "monitorComments": true,
* "commentPollInterval": 30000,
* "commentTrigger": "Act."
* }
*/
import type { Plugin } from "@opencode-ai/plugin"
import { readFile, writeFile, access } from "fs/promises"
import { exec } from "child_process"
import { promisify } from "util"
import { join } from "path"
import { homedir } from "os"
const execAsync = promisify(exec)
// ==================== SENTRY ====================
let _reportError: ((err: unknown, ctx?: Record<string, any>) => void) | undefined
function reportError(err: unknown, ctx?: Record<string, any>) {
if (!_reportError) {
_reportError = (e, c) => {
import("@sentry/node")
.then(Sentry => Sentry.captureException(e, { extra: c }))
.catch(() => {})
}
}
_reportError(err, ctx)
}
// ==================== CONFIGURATION ====================
interface GitHubConfig {
enabled?: boolean
postUserMessages?: boolean
postAssistantMessages?: boolean
postToolCalls?: boolean
batchInterval?: number
maxMessageLength?: number
createIssueIfMissing?: boolean
issueLabels?: string[]
monitorComments?: boolean
commentPollInterval?: number
commentTrigger?: string
}
const CONFIG_PATH = join(homedir(), ".config", "opencode", "github.json")
const ISSUE_FILE = ".github-issue.md"
const MAX_COMMENT_LENGTH = 65000 // GitHub's limit is 65536
const DEFAULT_POLL_INTERVAL = 30000 // 30 seconds
const DEFAULT_TRIGGER = "Act."
// Debug logging (silenced — console.error corrupts the OpenCode TUI)
function debug(..._args: any[]) {}
// ==================== CONFIG LOADING ====================
async function loadConfig(): Promise<GitHubConfig> {
try {
const content = await readFile(CONFIG_PATH, "utf-8")
return JSON.parse(content)
} catch {
return {}
}
}
interface ResolvedConfig {
enabled: boolean
postUserMessages: boolean
postAssistantMessages: boolean
postToolCalls: boolean
batchInterval: number
maxMessageLength: number
createIssueIfMissing: boolean
issueLabels: string[]
monitorComments: boolean
commentPollInterval: number
commentTrigger: string
}
function getConfig(config: GitHubConfig): ResolvedConfig {
return {
enabled: config.enabled ?? true,
postUserMessages: config.postUserMessages ?? false,
postAssistantMessages: config.postAssistantMessages ?? true,
postToolCalls: config.postToolCalls ?? false,
batchInterval: config.batchInterval ?? 5000,
maxMessageLength: config.maxMessageLength ?? MAX_COMMENT_LENGTH,
createIssueIfMissing: config.createIssueIfMissing ?? true,
issueLabels: config.issueLabels ?? ["opencode", "ai-session"],
monitorComments: config.monitorComments ?? true,
commentPollInterval: config.commentPollInterval ?? DEFAULT_POLL_INTERVAL,
commentTrigger: config.commentTrigger ?? DEFAULT_TRIGGER,
}
}
// ==================== ISSUE / PR DETECTION ====================
interface IssueInfo {
owner: string
repo: string
number: number
url: string
}
interface PRInfo {
owner: string
repo: string
number: number
url: string
}
/**
* Parse GitHub issue URL from text
* Supports: https://github.com/owner/repo/issues/123
*/
function parseIssueUrl(text: string): IssueInfo | null {
const match = text.match(/github\.com\/([^\/]+)\/([^\/]+)\/issues\/(\d+)/i)
if (match) {
return {
owner: match[1],
repo: match[2],
number: parseInt(match[3]),
url: `https://github.com/${match[1]}/${match[2]}/issues/${match[3]}`
}
}
return null
}
/**
* Parse GitHub PR URL from text
*/
function parsePRUrl(text: string): PRInfo | null {
const match = text.match(/github\.com\/([^\/]+)\/([^\/]+)\/pull\/(\d+)/i)
if (match) {
return {
owner: match[1],
repo: match[2],
number: parseInt(match[3]),
url: `https://github.com/${match[1]}/${match[2]}/pull/${match[3]}`
}
}
return null
}
/**
* Extract issue number from branch name
* Supports: issue-123, fix/123-desc, feat/GH-42-desc, 123-description
*/
function extractIssueFromBranch(branchName: string): number | null {
// Pattern 1: explicit issue prefix (issue-123, issue/123)
let match = branchName.match(/issue[-\/](\d+)/i)
if (match) return parseInt(match[1])
// Pattern 2: GH-N prefix
match = branchName.match(/GH-(\d+)/i)
if (match) return parseInt(match[1])
// Pattern 3: type/N-description (fix/123-typo, feat/42-new-feature)
match = branchName.match(/^[a-z]+\/(\d+)[-_]/i)
if (match) return parseInt(match[1])
// Pattern 4: N-description at start (123-fix-bug)
match = branchName.match(/^(\d+)[-_]/)
if (match) return parseInt(match[1])
// Pattern 5: number anywhere after slash (feature/add-thing-123)
match = branchName.match(/\/.*?(\d+)/)
if (match && parseInt(match[1]) > 0 && parseInt(match[1]) < 100000) {
return parseInt(match[1])
}
return null
}
/**
* Get current git branch name
*/
async function getCurrentBranch(directory: string): Promise<string | null> {
try {
const { stdout } = await execAsync("git branch --show-current", { cwd: directory })
return stdout.trim() || null
} catch {
return null
}
}
/**
* Get git remote origin URL to extract owner/repo
*/
async function getRepoInfo(directory: string): Promise<{ owner: string; repo: string } | null> {
try {
const { stdout } = await execAsync("git remote get-url origin", { cwd: directory })
const url = stdout.trim()
// Parse SSH format: git@github.com:owner/repo.git
let match = url.match(/git@github\.com:([^\/]+)\/([^\.]+)/)
if (match) {
return { owner: match[1], repo: match[2].replace(/\.git$/, "") }
}
// Parse HTTPS format: https://github.com/owner/repo.git
match = url.match(/github\.com\/([^\/]+)\/([^\.\/]+)/)
if (match) {
return { owner: match[1], repo: match[2].replace(/\.git$/, "") }
}
return null
} catch {
return null
}
}
/**
* Check if gh CLI is available and authenticated
*/
async function isGhAvailable(): Promise<boolean> {
try {
await execAsync("gh auth status")
return true
} catch {
return false
}
}
/**
* Get issue from PR's closingIssuesReferences
*/
async function getIssueFromPR(directory: string): Promise<number | null> {
try {
const { stdout } = await execAsync(
`gh pr view --json closingIssuesReferences -q '.closingIssuesReferences[0].number'`,
{ cwd: directory }
)
const num = parseInt(stdout.trim())
return isNaN(num) ? null : num
} catch {
return null
}
}
/**
* Get current branch's open PR number
*/
async function getCurrentPR(directory: string): Promise<PRInfo | null> {
try {
const { stdout } = await execAsync(
`gh pr view --json number,url -q '{"number": .number, "url": .url}'`,
{ cwd: directory }
)
const result = JSON.parse(stdout.trim())
if (!result?.number) return null
const repoInfo = await getRepoInfo(directory)
if (!repoInfo) return null
return {
owner: repoInfo.owner,
repo: repoInfo.repo,
number: result.number,
url: result.url
}
} catch {
return null
}
}
/**
* Verify issue exists
*/
async function verifyIssue(owner: string, repo: string, number: number): Promise<boolean> {
try {
await execAsync(`gh issue view ${number} --repo ${owner}/${repo} --json number`)
return true
} catch {
return false
}
}
/**
* Read .github-issue file
*/
async function readIssueFile(directory: string): Promise<IssueInfo | null> {
const filePath = join(directory, ISSUE_FILE)
try {
await access(filePath)
const content = (await readFile(filePath, "utf-8")).trim()
// Check if it's a URL
const urlInfo = parseIssueUrl(content)
if (urlInfo) return urlInfo
// Check if it's just a number
const number = parseInt(content)
if (!isNaN(number)) {
const repoInfo = await getRepoInfo(directory)
if (repoInfo) {
return {
owner: repoInfo.owner,
repo: repoInfo.repo,
number,
url: `https://github.com/${repoInfo.owner}/${repoInfo.repo}/issues/${number}`
}
}
}
return null
} catch {
return null
}
}
/**
* Write issue info to .github-issue file
*/
async function writeIssueFile(directory: string, issue: IssueInfo): Promise<void> {
const filePath = join(directory, ISSUE_FILE)
await writeFile(filePath, issue.url + "\n", "utf-8")
debug("Wrote issue file:", filePath)
}
/**
* Create a new GitHub issue
*/
async function createIssue(
directory: string,
title: string,
body: string,
labels: string[]
): Promise<IssueInfo | null> {
const repoInfo = await getRepoInfo(directory)
if (!repoInfo) {
debug("Cannot create issue: no repo info")
return null
}
try {
// Create issue with gh CLI
const labelArgs = labels.map(l => `--label "${l}"`).join(" ")
const { stdout } = await execAsync(
`gh issue create --repo ${repoInfo.owner}/${repoInfo.repo} --title "${title.replace(/"/g, '\\"')}" --body "${body.replace(/"/g, '\\"').replace(/\n/g, '\\n')}" ${labelArgs} --json number,url`,
{ cwd: directory }
)
const result = JSON.parse(stdout)
return {
owner: repoInfo.owner,
repo: repoInfo.repo,
number: result.number,
url: result.url
}
} catch (e) {
debug("Failed to create issue:", e)
return null
}
}
/**
* Main issue detection function - tries all methods in priority order
*/
async function detectIssue(
directory: string,
firstMessage: string | null,
config: ResolvedConfig
): Promise<IssueInfo | null> {
debug("Detecting issue for directory:", directory)
// 1. Check first message for GitHub issue URL
if (firstMessage) {
const urlInfo = parseIssueUrl(firstMessage)
if (urlInfo) {
debug("Found issue URL in first message:", urlInfo.url)
// Save to file for future sessions
await writeIssueFile(directory, urlInfo)
return urlInfo
}
}
// 2. Check .github-issue file
const fileInfo = await readIssueFile(directory)
if (fileInfo) {
debug("Found issue in .github-issue file:", fileInfo.url)
return fileInfo
}
// Check if gh CLI is available for remaining methods
const ghAvailable = await isGhAvailable()
if (!ghAvailable) {
debug("gh CLI not available, skipping PR and branch checks")
} else {
// 3. Check PR's closingIssuesReferences
const prIssue = await getIssueFromPR(directory)
if (prIssue) {
const repoInfo = await getRepoInfo(directory)
if (repoInfo) {
const verified = await verifyIssue(repoInfo.owner, repoInfo.repo, prIssue)
if (verified) {
const info: IssueInfo = {
owner: repoInfo.owner,
repo: repoInfo.repo,
number: prIssue,
url: `https://github.com/${repoInfo.owner}/${repoInfo.repo}/issues/${prIssue}`
}
debug("Found issue from PR:", info.url)
await writeIssueFile(directory, info)
return info
}
}
}
// 4. Extract from branch name
const branch = await getCurrentBranch(directory)
if (branch) {
const branchIssue = extractIssueFromBranch(branch)
if (branchIssue) {
const repoInfo = await getRepoInfo(directory)
if (repoInfo) {
const verified = await verifyIssue(repoInfo.owner, repoInfo.repo, branchIssue)
if (verified) {
const info: IssueInfo = {
owner: repoInfo.owner,
repo: repoInfo.repo,
number: branchIssue,
url: `https://github.com/${repoInfo.owner}/${repoInfo.repo}/issues/${branchIssue}`
}
debug("Found issue from branch name:", info.url)
await writeIssueFile(directory, info)
return info
}
}
}
}
}
// 5. Create new issue if enabled
if (config.createIssueIfMissing && firstMessage && ghAvailable) {
debug("Creating new issue...")
// Extract title from first line or first 80 chars
const titleMatch = firstMessage.match(/^(.{1,80})/)
const title = titleMatch ? titleMatch[1].replace(/\n/g, " ").trim() : "OpenCode Session"
const body = `## Task Description
${firstMessage.slice(0, 3000)}
---
*This issue was automatically created by OpenCode to track agent session history.*`
const newIssue = await createIssue(directory, title, body, config.issueLabels)
if (newIssue) {
debug("Created new issue:", newIssue.url)
await writeIssueFile(directory, newIssue)
return newIssue
}
}
debug("No issue detected")
return null
}
// ==================== MESSAGE POSTING ====================
/**
* Post a comment to GitHub issue
*/
async function postComment(issue: IssueInfo, body: string): Promise<boolean> {
try {
// Truncate if too long
let commentBody = body
if (commentBody.length > MAX_COMMENT_LENGTH) {
commentBody = commentBody.slice(0, MAX_COMMENT_LENGTH - 100) + "\n\n*[Message truncated]*"
}
// Use gh CLI to post comment
// Using a heredoc to handle multi-line content
await execAsync(
`gh issue comment ${issue.number} --repo ${issue.owner}/${issue.repo} --body-file -`,
{
input: commentBody
} as any
)
debug("Posted comment to issue", issue.number)
return true
} catch (e) {
debug("Failed to post comment:", e)
return false
}
}
/**
* Format a message for posting to GitHub
*/
function formatMessage(
role: "user" | "assistant" | "tool",
content: string,
metadata?: { model?: string; timestamp?: Date; toolName?: string }
): string {
const timestamp = metadata?.timestamp || new Date()
const timeStr = timestamp.toISOString()
let header = ""
if (role === "user") {
header = `### User Message`
} else if (role === "assistant") {
header = `### Assistant${metadata?.model ? ` (${metadata.model})` : ""}`
} else if (role === "tool") {
header = `### Tool: ${metadata?.toolName || "unknown"}`
}
return `${header}
<sub>${timeStr}</sub>
${content}
---`
}
// ==================== COMMENT MONITORING ====================
interface GitHubComment {
id: number
body: string
user: { login: string }
created_at: string
html_url: string
}
/**
* Fetch issue comments since a given timestamp using gh API.
*/
async function fetchIssueComments(
issue: IssueInfo,
since?: string
): Promise<GitHubComment[]> {
try {
const sinceParam = since ? `&since=${since}` : ""
const { stdout } = await execAsync(
`gh api "repos/${issue.owner}/${issue.repo}/issues/${issue.number}/comments?per_page=100&sort=created&direction=desc${sinceParam}"`,
{ maxBuffer: 1024 * 1024 }
)
return JSON.parse(stdout) as GitHubComment[]
} catch {
return []
}
}
/**
* Fetch PR review comments (code-level review comments).
*/
async function fetchPRReviewComments(
pr: PRInfo,
since?: string
): Promise<GitHubComment[]> {
try {
const sinceParam = since ? `&since=${since}` : ""
const { stdout } = await execAsync(
`gh api "repos/${pr.owner}/${pr.repo}/pulls/${pr.number}/comments?per_page=100&sort=created&direction=desc${sinceParam}"`,
{ maxBuffer: 1024 * 1024 }
)
return JSON.parse(stdout) as GitHubComment[]
} catch {
return []
}
}
/**
* Check if a comment body ends with the trigger phrase.
* Trims trailing whitespace/newlines before checking.
*/
function hasCommentTrigger(body: string, trigger: string): boolean {
return body.trimEnd().endsWith(trigger)
}
/**
* Add a reaction to a comment to acknowledge it was processed.
*/
async function addCommentReaction(
owner: string,
repo: string,
commentId: number,
reaction: string = "eyes"
): Promise<void> {
try {
await execAsync(
`gh api "repos/${owner}/${repo}/issues/comments/${commentId}/reactions" -f content="${reaction}" --silent`
)
} catch {}
}
/**
* Add a reaction to a PR review comment.
*/
async function addPRReviewCommentReaction(
owner: string,
repo: string,
commentId: number,
reaction: string = "eyes"
): Promise<void> {
try {
await execAsync(
`gh api "repos/${owner}/${repo}/pulls/comments/${commentId}/reactions" -f content="${reaction}" --silent`
)
} catch {}
}
// ==================== PLUGIN ====================
export const GitHubPlugin: Plugin = async ({ client, directory }) => {
if (!client) {
return {}
}
debug("GitHub plugin initializing for directory:", directory)
// ---- Session & message posting state ----
const sessionIssues = new Map<string, IssueInfo | null>()
const pendingMessages = new Map<string, Array<{ role: string; content: string; metadata?: any }>>()
const batchTimers = new Map<string, NodeJS.Timeout>()
const processedMessages = new Set<string>()
// ---- Comment monitoring state ----
// Maps "owner/repo#number" to the most-recently-seen comment ID (for dedup)
const lastSeenIssueCommentId = new Map<string, number>()
const lastSeenPRCommentId = new Map<string, number>()
// Tracks which sessionId is associated with which issue/PR for routing
const sessionToIssue = new Map<string, IssueInfo>()
const sessionToPR = new Map<string, PRInfo>()
// Reverse lookups: "owner/repo#number" → sessionId (most recent session wins)
const issueKeyToSession = new Map<string, string>()
const prKeyToSession = new Map<string, string>()
// Active sessions (from most recent session.idle)
let activeSessionId: string | null = null
// Polling timer
let pollTimer: NodeJS.Timeout | null = null
// Processed comment IDs (prevent re-processing after restart)
const processedCommentIds = new Set<number>()
// Load config
const rawConfig = await loadConfig()
const config = getConfig(rawConfig)
if (!config.enabled) {
debug("GitHub plugin disabled")
return {}
}
// Check gh CLI availability at startup
const ghAvailable = await isGhAvailable()
if (!ghAvailable) {
debug("gh CLI not available or not authenticated - plugin will have limited functionality")
}
// ---- Helper: issue/PR key for maps ----
function issueKey(info: IssueInfo | PRInfo): string {
return `${info.owner}/${info.repo}#${info.number}`
}
/**
* Get or detect issue for a session
*/
async function getSessionIssue(sessionId: string, firstMessage?: string): Promise<IssueInfo | null> {
if (sessionIssues.has(sessionId)) {
return sessionIssues.get(sessionId) || null
}
const issue = await detectIssue(directory, firstMessage || null, config)
sessionIssues.set(sessionId, issue)
return issue
}
/**
* Queue a message for posting
*/
function queueMessage(sessionId: string, role: string, content: string, metadata?: any) {
if (!pendingMessages.has(sessionId)) {
pendingMessages.set(sessionId, [])
}
pendingMessages.get(sessionId)!.push({ role, content, metadata })
// Set up batch timer
if (!batchTimers.has(sessionId)) {
const timer = setTimeout(() => flushMessages(sessionId), config.batchInterval)
batchTimers.set(sessionId, timer)
}
}
/**
* Flush pending messages to GitHub
*/
async function flushMessages(sessionId: string) {
const messages = pendingMessages.get(sessionId)
if (!messages || messages.length === 0) return
const issue = sessionIssues.get(sessionId)
if (!issue) {
debug("No issue for session, skipping flush:", sessionId.slice(0, 8))
pendingMessages.delete(sessionId)
return
}
// Clear pending
pendingMessages.delete(sessionId)
batchTimers.delete(sessionId)
// Format all messages into one comment
const formattedMessages = messages.map(m =>
formatMessage(m.role as any, m.content, m.metadata)
)
const comment = formattedMessages.join("\n\n")
await postComment(issue, comment)
}
/**
* Extract text content from message parts
*/
function extractTextFromParts(parts: any[]): string {
const texts: string[] = []
for (const part of parts) {
if (part.type === "text" && part.text) {
texts.push(part.text)
} else if (part.type === "tool-invocation") {
if (config.postToolCalls) {
texts.push(`**Tool: ${part.toolInvocation?.toolName || "unknown"}**\n\`\`\`json\n${JSON.stringify(part.toolInvocation?.input, null, 2)}\n\`\`\``)
}
} else if (part.type === "tool-result") {
if (config.postToolCalls) {
texts.push(`**Tool Result:**\n\`\`\`\n${JSON.stringify(part.toolResult?.result, null, 2).slice(0, 1000)}\n\`\`\``)
}
}
}
return texts.join("\n\n")
}
// ==================== COMMENT MONITORING LOGIC ====================
/**
* Detect which issue & PR are associated with the current directory and
* register them for comment monitoring. Called on session.idle.
*/
async function registerSessionForMonitoring(sessionId: string) {
// Detect issue
const issue = sessionIssues.get(sessionId) || await getSessionIssue(sessionId)
if (issue) {
const key = issueKey(issue)
sessionToIssue.set(sessionId, issue)
issueKeyToSession.set(key, sessionId)
debug("Registered issue monitoring:", key, "session:", sessionId.slice(0, 8))
}
// Detect PR for current branch
if (ghAvailable) {
const pr = await getCurrentPR(directory)
if (pr) {
const key = issueKey(pr)
sessionToPR.set(sessionId, pr)
prKeyToSession.set(key, sessionId)
debug("Registered PR monitoring:", key, "session:", sessionId.slice(0, 8))
}
}
}
/**
* Poll for new comments on all monitored issues and PRs.
* Injects actionable comments (ending with trigger) into the session.
*/
async function pollForComments() {
if (!config.monitorComments) return
try {
// Poll issue comments
for (const [key, sessionId] of issueKeyToSession) {
const issue = sessionToIssue.get(sessionId)
if (!issue) continue
try {
const comments = await fetchIssueComments(issue)
const lastSeen = lastSeenIssueCommentId.get(key) || 0
// Process new comments (newest first, but we want chronological order)
const newComments = comments
.filter(c => c.id > lastSeen && !processedCommentIds.has(c.id))
.reverse() // chronological order
for (const comment of newComments) {
processedCommentIds.add(comment.id)
if (hasCommentTrigger(comment.body, config.commentTrigger)) {
debug("Actionable issue comment found:", comment.id, "from", comment.user.login)
// Verify session still exists
try {
await client.session.get({ path: { id: sessionId } })
} catch {
debug("Session gone, skipping comment injection")
continue
}
// Inject into session
const prefix = `[GitHub Issue #${issue.number} comment by @${comment.user.login}]`
try {
await client.session.promptAsync({
path: { id: sessionId },
body: { parts: [{ type: "text", text: `${prefix}\n\n${comment.body}` }] }
})
// React to acknowledge
await addCommentReaction(issue.owner, issue.repo, comment.id, "rocket")
debug("Injected issue comment into session:", sessionId.slice(0, 8))
} catch (e) {
reportError(e, { plugin: "github", op: "inject-issue-comment" })
}
}
}
// Update high-water mark
if (comments.length > 0) {
const maxId = Math.max(...comments.map(c => c.id))
if (maxId > lastSeen) {
lastSeenIssueCommentId.set(key, maxId)
}
}
} catch (e) {
reportError(e, { plugin: "github", op: "poll-issue-comments", key })
}
}
// Poll PR review comments
for (const [key, sessionId] of prKeyToSession) {
const pr = sessionToPR.get(sessionId)
if (!pr) continue
try {
const comments = await fetchPRReviewComments(pr)
const lastSeen = lastSeenPRCommentId.get(key) || 0
const newComments = comments
.filter(c => c.id > lastSeen && !processedCommentIds.has(c.id))
.reverse()
for (const comment of newComments) {
processedCommentIds.add(comment.id)
if (hasCommentTrigger(comment.body, config.commentTrigger)) {
debug("Actionable PR review comment found:", comment.id, "from", comment.user.login)
try {
await client.session.get({ path: { id: sessionId } })
} catch {
debug("Session gone, skipping comment injection")
continue
}
const prefix = `[GitHub PR #${pr.number} review comment by @${comment.user.login}]`
try {
await client.session.promptAsync({
path: { id: sessionId },
body: { parts: [{ type: "text", text: `${prefix}\n\n${comment.body}` }] }
})
await addPRReviewCommentReaction(pr.owner, pr.repo, comment.id, "rocket")
debug("Injected PR review comment into session:", sessionId.slice(0, 8))
} catch (e) {
reportError(e, { plugin: "github", op: "inject-pr-comment" })
}
}
}
if (comments.length > 0) {
const maxId = Math.max(...comments.map(c => c.id))
if (maxId > lastSeen) {
lastSeenPRCommentId.set(key, maxId)
}
}
} catch (e) {
reportError(e, { plugin: "github", op: "poll-pr-comments", key })
}
}
// Also poll PR issue-level comments (not review comments —
// these are top-level PR conversation comments)
for (const [key, sessionId] of prKeyToSession) {
const pr = sessionToPR.get(sessionId)
if (!pr) continue
// PR issue-level comments use the same API as issue comments
const prAsIssue: IssueInfo = {
owner: pr.owner,
repo: pr.repo,
number: pr.number,
url: pr.url
}
const prIssueKey = `pr-issue:${key}`
try {
const comments = await fetchIssueComments(prAsIssue)
const lastSeen = lastSeenIssueCommentId.get(prIssueKey) || 0
const newComments = comments
.filter(c => c.id > lastSeen && !processedCommentIds.has(c.id))
.reverse()
for (const comment of newComments) {
processedCommentIds.add(comment.id)
if (hasCommentTrigger(comment.body, config.commentTrigger)) {
debug("Actionable PR conversation comment found:", comment.id)
try {
await client.session.get({ path: { id: sessionId } })
} catch {
continue
}
const prefix = `[GitHub PR #${pr.number} comment by @${comment.user.login}]`
try {
await client.session.promptAsync({
path: { id: sessionId },
body: { parts: [{ type: "text", text: `${prefix}\n\n${comment.body}` }] }
})
await addCommentReaction(pr.owner, pr.repo, comment.id, "rocket")
} catch (e) {
reportError(e, { plugin: "github", op: "inject-pr-conversation-comment" })
}
}
}
if (comments.length > 0) {
const maxId = Math.max(...comments.map(c => c.id))
if (maxId > lastSeen) {
lastSeenIssueCommentId.set(prIssueKey, maxId)
}
}
} catch (e) {
reportError(e, { plugin: "github", op: "poll-pr-conversation-comments", key })
}
}
} catch (e) {
reportError(e, { plugin: "github", op: "poll-comments" })
}
}
/**
* Start the polling loop for comment monitoring.
*/
function startPolling() {
if (pollTimer) return
if (!config.monitorComments) return
debug("Starting comment polling every", config.commentPollInterval, "ms")
pollTimer = setInterval(async () => {
try {
await pollForComments()
} catch (e) {
reportError(e, { plugin: "github", op: "poll-interval" })
}
}, config.commentPollInterval)
}
/**
* Seed the high-water marks with existing comments so we don't
* re-process old comments on first run.
*/
async function seedHighWaterMarks() {
for (const [key, sessionId] of issueKeyToSession) {
const issue = sessionToIssue.get(sessionId)
if (!issue) continue
if (lastSeenIssueCommentId.has(key)) continue
try {
const comments = await fetchIssueComments(issue)
if (comments.length > 0) {
const maxId = Math.max(...comments.map(c => c.id))
lastSeenIssueCommentId.set(key, maxId)
// Also mark all existing comment IDs as processed
for (const c of comments) processedCommentIds.add(c.id)