| 1 | /** |
| 2 | * Tracks pending edit interactions until they are accepted or rejected, |
| 3 | * then emits editOutcome events with the final result. |
| 4 | */ |
| 5 | export interface PendingEditData { |
| 6 | streamId: string; |
| 7 | timestamp: string; |
| 8 | modelProvider: string; |
| 9 | modelTitle: string; |
| 10 | prompt: string; |
| 11 | completion: string; |
| 12 | previousCode: string; |
| 13 | newCode: string; |
| 14 | filepath: string; |
| 15 | previousCodeLines: number; |
| 16 | newCodeLines: number; |
| 17 | lineChange: number; |
| 18 | } |
| 19 | |
| 20 | class EditOutcomeTracker { |
| 21 | private static instance: EditOutcomeTracker; |
| 22 | private pendingEdits: Map<string, PendingEditData> = new Map(); |
| 23 | |
| 24 | private constructor() {} |
| 25 | |
| 26 | public static getInstance(): EditOutcomeTracker { |
| 27 | if (!EditOutcomeTracker.instance) { |
| 28 | EditOutcomeTracker.instance = new EditOutcomeTracker(); |
| 29 | } |
| 30 | return EditOutcomeTracker.instance; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Store a pending edit interaction for later outcome tracking |
| 35 | */ |
| 36 | public trackEditInteraction(data: PendingEditData): void { |
| 37 | this.pendingEdits.set(data.streamId, data); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Record the outcome of an edit interaction and emit the editOutcome event |
| 42 | */ |
| 43 | public async recordEditOutcome( |
| 44 | streamId: string, |
| 45 | accepted: boolean, |
| 46 | dataLogger: any, |
| 47 | ): Promise<void> { |
| 48 | const pendingEdit = this.pendingEdits.get(streamId); |
| 49 | if (!pendingEdit) { |
| 50 | console.warn(`No pending edit found for streamId: ${streamId}`); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Emit the editOutcome event |
| 55 | await dataLogger.logDevData({ |
| 56 | name: "editOutcome", |
| 57 | data: { |
| 58 | modelProvider: pendingEdit.modelProvider, |
| 59 | modelTitle: pendingEdit.modelTitle, |
| 60 | prompt: pendingEdit.prompt, |
| 61 | completion: pendingEdit.completion, |
| 62 | previousCode: pendingEdit.previousCode, |
| 63 | newCode: pendingEdit.newCode, |
| 64 | previousCodeLines: pendingEdit.previousCodeLines, |
| 65 | newCodeLines: pendingEdit.newCodeLines, |
| 66 | lineChange: pendingEdit.lineChange, |
| 67 | accepted, |
| 68 | filepath: pendingEdit.filepath, |
| 69 | }, |
| 70 | }); |
| 71 | |
| 72 | // Clean up the pending edit |
| 73 | this.pendingEdits.delete(streamId); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Clean up pending edits that might have been abandoned |
| 78 | */ |
| 79 | public cleanupOldPendingEdits(maxAgeMs: number = 30 * 60 * 1000): void { |
| 80 | const now = Date.now(); |
| 81 | for (const [streamId, edit] of this.pendingEdits.entries()) { |
| 82 | const editTime = new Date(edit.timestamp).getTime(); |
| 83 | if (now - editTime > maxAgeMs) { |
| 84 | this.pendingEdits.delete(streamId); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get count of pending edits (for debugging/monitoring) |
| 91 | */ |
| 92 | public getPendingEditCount(): number { |
| 93 | return this.pendingEdits.size; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export const editOutcomeTracker = EditOutcomeTracker.getInstance(); |