package main
import("encoding/json";"fmt";"io";"net/http";"os";"strings";"time";"github.com/smolnikov-k/capyagent/core/internal/memory";"github.com/smolnikov-k/capyagent/core/internal/llm")
func must(err error){if err!=nil{panic(err)}}
func barrier(){fmt.Println("READY");for{time.Sleep(time.Hour)}}
func main(){mode,dir:=os.Args[1],os.Args[2];s,err:=memory.NewFile(dir);must(err);chat:="synthetic:crash-study"
 if mode=="read"{hist,err:=s.History(chat,0);must(err);tools,known,unknown:=0,0,0;for _,m:=range hist{if m.Role==llm.RoleTool{tools++;if strings.Contains(m.Content,"effect_receipt"){known++};if strings.Contains(m.Content,"Неизвестно"){unknown++}}};must(json.NewEncoder(os.Stdout).Encode(map[string]int{"history_rows":len(hist),"tool_rows":tools,"known_receipts":known,"unknown_markers":unknown}));return}
 phase,url,key:=os.Args[3],os.Args[4],os.Args[5];must(s.BeginTurn(chat,"Выполнить одно синтетическое действие"));if phase=="before_start"{barrier()}
 call:=llm.ToolCall{ID:key,Name:"synthetic_effect",Arguments:map[string]any{"operation_key":key}};must(s.CheckpointToolStart(chat,call));if phase=="before_request"{barrier()}
 client:=http.Client{Timeout:5*time.Second};res,err:=client.Post(url,"application/json",strings.NewReader(fmt.Sprintf(`{"key":%q}`,key)));must(err);body,err:=io.ReadAll(res.Body);must(err);must(res.Body.Close());if res.StatusCode!=200{panic(string(body))};if phase=="after_effect"{barrier()}
 _,err=s.CheckpointToolResult(chat,call,string(body),false);must(err);barrier()
}
