relaycode
Version:
A developer assistant that automates applying code changes from LLMs.
2 lines • 3.41 kB
JavaScript
;require('relaycode-core');var logger=require('../utils/logger'),db=require('./db'),fs=require('fs'),config=require('./config');const g=e=>e.reasoning.some(t=>t.startsWith("Reverting transaction")),F=e=>{for(const t of e.reasoning){const a=t.match(/^Reverting transaction ([\w-]+)\./);if(a&&a[1])return a[1]}return null},h=e=>/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(e),S=(e,t)=>new Date(t.createdAt).getTime()-new Date(e.createdAt).getTime(),x=async(e,t)=>{const n=await db.getDb(e).query().from("transactions").where({uuid:t}).first();return !!n&&(n.status==="committed"||n.status==="undone")},D=async(e,t)=>{const a=db.getDb(e);await a.delete("transactions").where(s=>s.uuid===t.uuid&&s.status==="pending");const n={...db.fromStateFile(t),status:"pending"};await a.insert("transactions",n);},R=async(e,t)=>{const a=db.getDb(e),n=db.fromStateFile(t);(await a.update("transactions").set(n).where({uuid:t.uuid,status:"pending"})).length===0&&logger.logger.warn(`Could not find pending transaction with uuid ${t.uuid} to update.`);},$=async(e,t)=>{(await db.getDb(e).update("transactions").set({status:"committed"}).where({uuid:t,status:"pending"})).length===0&&logger.logger.warn(`Could not find pending transaction with uuid ${t} to commit.`);},I=async(e,t)=>{const a=db.getDb(e),n=new Date().toISOString();for(const s of t)(await a.update("transactions").set({gitCommittedAt:n}).where({uuid:s,status:"committed"})).length===0&&logger.logger.warn(`Could not find committed transaction with uuid ${s} to mark as git-committed.`);},U=async(e,t)=>{if(!t||typeof t!="string"||!t.match(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/)){logger.logger.error(`Fatal: Invalid UUID provided for deletePendingState: ${t}`);return}(await db.getDb(e).update("transactions").set({status:"undone"}).where({uuid:t,status:"pending"})).length===0&&logger.logger.debug(`Could not find pending transaction with uuid ${t} to mark as undone. It might have been committed or already undone.`);},C=async(e,t)=>{const n=await db.getDb(e).query().from("transactions").where({uuid:t,status:"committed"}).first();return n?db.toStateFile(n):null},p=async(e=process.cwd(),t={})=>{const a=config.getStateDirectory(e);try{await fs.promises.access(a);}catch{return null}let s=await db.getDb(e).query().from("transactions").where({status:"committed"}).all();if(!s)return [];let i=s.map(db.toStateFile);if(t.skipReverts){const u=new Set;i.forEach(o=>{if(g(o)){const l=F(o);l&&u.add(l);}}),i=i.filter(o=>!g(o)&&!u.has(o.uuid));}return i.sort(S),i},k=async(e=process.cwd(),t={})=>(await p(e,t))?.[0]??null,O=async(e,t,a={})=>{if(h(t)){const s=await db.getDb(e).query().from("transactions").where({uuid:t,status:"committed"}).first();return s?db.toStateFile(s):null}if(/^-?\d+$/.test(t)){const n=Math.abs(parseInt(t,10));if(isNaN(n)||n<=0)return null;const s=await p(e,a);return s&&s.length>=n?s[n-1]??null:null}return null};exports.commitState=$;exports.deletePendingState=U;exports.findLatestStateFile=k;exports.findStateFileByIdentifier=O;exports.getRevertedTransactionUuid=F;exports.hasBeenProcessed=x;exports.isRevertTransaction=g;exports.markTransactionsAsGitCommitted=I;exports.readAllStateFiles=p;exports.readStateFile=C;exports.updatePendingState=R;exports.writePendingState=D;//# sourceMappingURL=state.cjs.map
//# sourceMappingURL=state.cjs.map