|
| 1 | +import type { JobSpec } from './plan-types'; |
| 2 | +import { sendPrompt, waitForServer } from './sdk-client'; |
| 3 | + |
| 4 | +export interface RelayContext { |
| 5 | + finding: string; |
| 6 | + filePath?: string; |
| 7 | + lineNumber?: number; |
| 8 | + severity?: 'info' | 'warning' | 'error'; |
| 9 | +} |
| 10 | + |
| 11 | +export interface RelayMessage { |
| 12 | + from: string; |
| 13 | + to: string; |
| 14 | + context: RelayContext; |
| 15 | + timestamp: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class JobComms { |
| 19 | + private messageBus: Map<string, RelayMessage[]> = new Map(); |
| 20 | + private relayPatterns: Map<string, Bun.Glob[]> = new Map(); |
| 21 | + private relayPatternSources: Map<string, string[]> = new Map(); |
| 22 | + |
| 23 | + registerJob(job: JobSpec): void { |
| 24 | + if (job.relayPatterns && job.relayPatterns.length > 0) { |
| 25 | + const patterns: Bun.Glob[] = []; |
| 26 | + const sources: string[] = []; |
| 27 | + for (const pattern of job.relayPatterns) { |
| 28 | + const normalized = pattern.endsWith('/') ? `${pattern}**` : pattern; |
| 29 | + patterns.push(new Bun.Glob(normalized)); |
| 30 | + sources.push(pattern); |
| 31 | + } |
| 32 | + this.relayPatterns.set(job.name, patterns); |
| 33 | + this.relayPatternSources.set(job.name, sources); |
| 34 | + } |
| 35 | + if (!this.messageBus.has(job.name)) { |
| 36 | + this.messageBus.set(job.name, []); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + unregisterJob(jobName: string): void { |
| 41 | + this.relayPatterns.delete(jobName); |
| 42 | + this.relayPatternSources.delete(jobName); |
| 43 | + this.messageBus.delete(jobName); |
| 44 | + } |
| 45 | + |
| 46 | + relayFinding(from: string, to: string, context: RelayContext): void { |
| 47 | + const message: RelayMessage = { |
| 48 | + from, |
| 49 | + to, |
| 50 | + context, |
| 51 | + timestamp: new Date().toISOString(), |
| 52 | + }; |
| 53 | + |
| 54 | + const messages = this.messageBus.get(to) ?? []; |
| 55 | + messages.push(message); |
| 56 | + this.messageBus.set(to, messages); |
| 57 | + } |
| 58 | + |
| 59 | + getMessagesForJob(jobName: string): RelayMessage[] { |
| 60 | + return this.messageBus.get(jobName) ?? []; |
| 61 | + } |
| 62 | + |
| 63 | + clearMessagesForJob(jobName: string): void { |
| 64 | + this.messageBus.set(jobName, []); |
| 65 | + } |
| 66 | + |
| 67 | + shouldRelayForFile(jobName: string, filePath: string): boolean { |
| 68 | + const patterns = this.relayPatterns.get(jobName); |
| 69 | + if (!patterns || patterns.length === 0) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + return patterns.some((pattern) => pattern.match(filePath)); |
| 73 | + } |
| 74 | + |
| 75 | + async deliverMessages( |
| 76 | + job: JobSpec, |
| 77 | + options?: { filterFrom?: string[] }, |
| 78 | + ): Promise<number> { |
| 79 | + const messages = this.getMessagesForJob(job.name); |
| 80 | + if (messages.length === 0) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + const filtered = options?.filterFrom |
| 85 | + ? messages.filter((m) => options.filterFrom!.includes(m.from)) |
| 86 | + : messages; |
| 87 | + |
| 88 | + if (filtered.length === 0) { |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + if (!job.port) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + const client = await waitForServer(job.port, { timeoutMs: 5000 }); |
| 98 | + |
| 99 | + for (const message of filtered) { |
| 100 | + const prompt = this.formatRelayPrompt(message); |
| 101 | + await sendPrompt(client, job.launchSessionID ?? '', prompt); |
| 102 | + } |
| 103 | + |
| 104 | + this.clearMessagesForJob(job.name); |
| 105 | + return filtered.length; |
| 106 | + } catch { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private formatRelayPrompt(message: RelayMessage): string { |
| 112 | + const { from, context } = message; |
| 113 | + const { finding, filePath, lineNumber, severity } = context; |
| 114 | + |
| 115 | + const parts: string[] = [`[Inter-Job Communication from ${from}]`]; |
| 116 | + |
| 117 | + if (severity) { |
| 118 | + parts.push(`Severity: ${severity.toUpperCase()}`); |
| 119 | + } |
| 120 | + |
| 121 | + parts.push(`Finding: ${finding}`); |
| 122 | + |
| 123 | + if (filePath) { |
| 124 | + parts.push(`File: ${filePath}`); |
| 125 | + } |
| 126 | + |
| 127 | + if (lineNumber) { |
| 128 | + parts.push(`Line: ${lineNumber}`); |
| 129 | + } |
| 130 | + |
| 131 | + parts.push('\nConsider how this finding may affect your current work.'); |
| 132 | + |
| 133 | + return parts.join('\n'); |
| 134 | + } |
| 135 | + |
| 136 | + getAllRegisteredJobs(): string[] { |
| 137 | + return Array.from(this.messageBus.keys()); |
| 138 | + } |
| 139 | + |
| 140 | + getRelayPatternsForJob(jobName: string): string[] | undefined { |
| 141 | + return this.relayPatternSources.get(jobName); |
| 142 | + } |
| 143 | +} |
0 commit comments