|
| 1 | +const ProposalStatus = { |
| 2 | + PENDING: 'pending', |
| 3 | + APPROVED: 'approved', |
| 4 | + REJECTED: 'rejected', |
| 5 | + MODIFIED: 'modified' |
| 6 | +}; |
| 7 | + |
| 8 | +class AgentProposal { |
| 9 | + constructor({ id, files, validation, checkpointId = null }) { |
| 10 | + this.id = id; |
| 11 | + this.files = files; |
| 12 | + this.validation = validation; |
| 13 | + this.checkpointId = checkpointId; |
| 14 | + this.status = ProposalStatus.PENDING; |
| 15 | + this.createdAt = new Date(); |
| 16 | + this.approvedAt = null; |
| 17 | + this.rejectedAt = null; |
| 18 | + this.rejectionReason = null; |
| 19 | + } |
| 20 | + |
| 21 | + getSummary() { |
| 22 | + let linesAdded = 0; |
| 23 | + let createdFiles = 0; |
| 24 | + let modifiedFiles = 0; |
| 25 | + |
| 26 | + for (const file of this.files) { |
| 27 | + if (file.action === 'create') { |
| 28 | + createdFiles++; |
| 29 | + if (file.content) { |
| 30 | + linesAdded += file.content.split('\n').length; |
| 31 | + } |
| 32 | + } else if (file.action === 'modify') { |
| 33 | + modifiedFiles++; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return { |
| 38 | + fileCount: this.files.length, |
| 39 | + createdFiles, |
| 40 | + modifiedFiles, |
| 41 | + linesAdded, |
| 42 | + confidence: this.validation?.confidence, |
| 43 | + recommendation: this.validation?.recommendation |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + approve() { |
| 48 | + if (this.status === ProposalStatus.REJECTED) { |
| 49 | + throw new Error('Cannot approve rejected proposal'); |
| 50 | + } |
| 51 | + this.status = ProposalStatus.APPROVED; |
| 52 | + this.approvedAt = new Date(); |
| 53 | + } |
| 54 | + |
| 55 | + reject(reason) { |
| 56 | + this.status = ProposalStatus.REJECTED; |
| 57 | + this.rejectedAt = new Date(); |
| 58 | + this.rejectionReason = reason; |
| 59 | + } |
| 60 | + |
| 61 | + canRollback() { |
| 62 | + return this.checkpointId !== null; |
| 63 | + } |
| 64 | + |
| 65 | + toJSON() { |
| 66 | + return { |
| 67 | + id: this.id, |
| 68 | + status: this.status, |
| 69 | + files: this.files.map(f => ({ path: f.path, action: f.action })), |
| 70 | + validation: this.validation, |
| 71 | + checkpointId: this.checkpointId, |
| 72 | + summary: this.getSummary(), |
| 73 | + createdAt: this.createdAt.toISOString(), |
| 74 | + approvedAt: this.approvedAt?.toISOString(), |
| 75 | + rejectedAt: this.rejectedAt?.toISOString(), |
| 76 | + rejectionReason: this.rejectionReason |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = { AgentProposal, ProposalStatus }; |
0 commit comments