Elffuss BraidHub

kikocis/continue public

5cf29b825e2d · 2.6 KiBRawPermalink
1/**
2 * Tracks pending edit interactions until they are accepted or rejected,
3 * then emits editOutcome events with the final result.
4 */
5export 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
20class 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 },
69 });
70
71 // Clean up the pending edit
72 this.pendingEdits.delete(streamId);
73 }
74
75 /**
76 * Clean up pending edits that might have been abandoned
77 */
78 public cleanupOldPendingEdits(maxAgeMs: number = 30 * 60 * 1000): void {
79 const now = Date.now();
80 for (const [streamId, edit] of this.pendingEdits.entries()) {
81 const editTime = new Date(edit.timestamp).getTime();
82 if (now - editTime > maxAgeMs) {
83 this.pendingEdits.delete(streamId);
84 }
85 }
86 }
87
88 /**
89 * Get count of pending edits (for debugging/monitoring)
90 */
91 public getPendingEditCount(): number {
92 return this.pendingEdits.size;
93 }
94}
95
96export const editOutcomeTracker = EditOutcomeTracker.getInstance();