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