File size: 1,675 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 관측 저장 실패를 목표 실패(false)나 보조 경고로 삼키지 않는다. 효과를 재실행하는 API는 없다.
// 설명 문서: docs/specs/batch-loop-observations.md v0.1.0
const db=require('../../knowledge-db'),record=require('../db/batch-loop-observation-record');
const isStorageError=error=>['BATCH_OBSERVATION_STORAGE_FAILED','BATCH_CHECKPOINT_STORAGE_FAILED'].includes(error?.code);
function create(run){
 const c=db.getBatchCheckpoint(run.runId),event=Object.values(c?.loopEvents||{}).at(-1);
 record.source(db,run.runId,event?.key);let saved=db.getBatchLoopObservations(run.runId,event.key);
 // 재개 시 기존 intent를 이어 쓰거나 빠진 반환을 추측하지 않는다.
 if(saved)record.fail();
 function append(value){try{saved=db.appendBatchLoopObservation(run.runId,event.key,value,saved?.hash||null);return saved.events.at(-1);}catch(cause){run.preserveExecutionLease=true;throw Object.assign(Error('배치 루프 관측 저장 실패'),{code:'BATCH_OBSERVATION_STORAGE_FAILED',retryable:false,cause});}}
 return Object.freeze({async observe(operation,input,action,serialize=value=>value){
  const start=append({kind:'intent',operation,operationId:saved?.events.length||0,data:input});
  let result;
  try{result=await action();}catch(error){
   if(isStorageError(error))throw error;
   append({kind:'threw',operation,operationId:start.index,data:{name:String(error?.name||'Error'),code:String(error?.code||''),message:String(error?.message||error).slice(0,2000)}});throw error;
  }
  append({kind:'returned',operation,operationId:start.index,data:serialize(result)});return result;
 }});
}
module.exports={create,isStorageError};