relaycode
Version:
A developer assistant that automates applying code changes from LLMs.
2 lines • 3.3 kB
JavaScript
import'relaycode-core';import {logger}from'../utils/logger';import {getDb,fromStateFile,toStateFile}from'./db';import {promises}from'fs';import {getStateDirectory}from'./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 getDb(e).query().from("transactions").where({uuid:t}).first();return !!n&&(n.status==="committed"||n.status==="undone")},D=async(e,t)=>{const a=getDb(e);await a.delete("transactions").where(s=>s.uuid===t.uuid&&s.status==="pending");const n={...fromStateFile(t),status:"pending"};await a.insert("transactions",n);},R=async(e,t)=>{const a=getDb(e),n=fromStateFile(t);(await a.update("transactions").set(n).where({uuid:t.uuid,status:"pending"})).length===0&&logger.warn(`Could not find pending transaction with uuid ${t.uuid} to update.`);},$=async(e,t)=>{(await getDb(e).update("transactions").set({status:"committed"}).where({uuid:t,status:"pending"})).length===0&&logger.warn(`Could not find pending transaction with uuid ${t} to commit.`);},I=async(e,t)=>{const a=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.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.error(`Fatal: Invalid UUID provided for deletePendingState: ${t}`);return}(await getDb(e).update("transactions").set({status:"undone"}).where({uuid:t,status:"pending"})).length===0&&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 getDb(e).query().from("transactions").where({uuid:t,status:"committed"}).first();return n?toStateFile(n):null},p=async(e=process.cwd(),t={})=>{const a=getStateDirectory(e);try{await promises.access(a);}catch{return null}let s=await getDb(e).query().from("transactions").where({status:"committed"}).all();if(!s)return [];let i=s.map(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 getDb(e).query().from("transactions").where({uuid:t,status:"committed"}).first();return s?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};export{$ as commitState,U as deletePendingState,k as findLatestStateFile,O as findStateFileByIdentifier,F as getRevertedTransactionUuid,x as hasBeenProcessed,g as isRevertTransaction,I as markTransactionsAsGitCommitted,p as readAllStateFiles,C as readStateFile,R as updatePendingState,D as writePendingState};//# sourceMappingURL=state.js.map
//# sourceMappingURL=state.js.map