import{r as l,j as e}from"./main-Be2qqUUW.js";import{D as S,k as C,l as q,m as D,n as F,T as i,o as u,p as n,q as t,s as B,t as E,u as I,v as O,w as V,x as z,y as v,z as d,A as R}from"./ProxyViewerContainer-WRenRpeh.js";import{ResponseView as h}from"./ResponseView-MogToC4i.js";import"./json-viewer-BicGakI5.js";const M=z({success:v(),error:R().optional(),responseStatus:d().optional(),responseText:R().optional(),inputTokens:d().optional(),outputTokens:d().optional(),elapsedMs:d().optional(),streaming:v().optional()});function H({log:a,open:y,onOpenChange:g}){const[j,w]=l.useState(()=>a.rawRequestBody??"{}"),[s,p]=l.useState(null),[f,c]=l.useState(!1),[x,r]=l.useState(null);async function k(){c(!0),r(null),p(null);try{const N=await(await fetch(`/api/logs/${a.id}/replay`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({modifiedBody:j})})).json(),T=M.safeParse(N);if(!T.success){r("Invalid response from server"),c(!1);return}const m=T.data;p(m),m.success||r(m.error??"Replay failed")}catch(o){r(o instanceof Error?o.message:"Network error")}finally{c(!1)}}function b(){p(null),r(null),g(!1)}return e.jsx(S,{open:y,onOpenChange:b,children:e.jsxs(C,{className:"max-w-4xl max-h-[85vh] overflow-auto",children:[e.jsx(q,{children:e.jsxs(D,{className:"flex items-center gap-2",children:[e.jsx(F,{className:"size-4"}),"Replay Request #",a.id]})}),e.jsxs(i,{defaultValue:"modified",children:[e.jsxs(u,{children:[e.jsx(n,{value:"modified",children:"Modified Request"}),e.jsx(n,{value:"original",children:"Original Response"}),s&&e.jsx(n,{value:"replay",children:"Replay Response"})]}),e.jsxs(t,{value:"modified",className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium mb-2 block",children:"Request Body (JSON)"}),e.jsx(B,{children:e.jsxs(E,{children:[e.jsx(I,{asChild:!0,children:e.jsx("textarea",{className:"w-full h-64 p-3 font-mono text-xs bg-muted rounded-md border border-input resize-none focus:outline-none focus:ring-2 focus:ring-ring",value:j,onChange:o=>w(o.target.value),spellCheck:!1})}),e.jsx(O,{children:"Edit the request body before re-sending to the provider"})]})})]}),x!==null&&x!==""&&e.jsx("div",{className:"text-sm text-destructive bg-destructive/10 px-3 py-2 rounded-md",children:x}),e.jsx("div",{className:"flex justify-end",children:e.jsx(V,{onClick:()=>{k()},disabled:f,children:f?"Replaying...":"Replay"})}),s&&s.success&&e.jsxs(i,{defaultValue:"parsed",children:[e.jsxs(u,{children:[e.jsx(n,{value:"parsed",children:"Response"}),e.jsx(n,{value:"raw",children:"Raw Response"})]}),e.jsx(t,{value:"parsed",children:e.jsx(h,{responseText:s.responseText??null,responseStatus:s.responseStatus??null,streaming:s.streaming??!1,inputTokens:s.inputTokens??null,outputTokens:s.outputTokens??null,apiFormat:a.apiFormat})}),e.jsx(t,{value:"raw",children:e.jsx("pre",{className:"font-mono text-xs whitespace-pre-wrap bg-muted p-3 rounded-md max-h-96 overflow-auto",children:s.responseText??"No response"})})]})]}),e.jsx(t,{value:"original",children:a.responseText!==null?e.jsxs(i,{defaultValue:"parsed",children:[e.jsxs(u,{children:[e.jsx(n,{value:"parsed",children:"Response"}),e.jsx(n,{value:"raw",children:"Raw Response"})]}),e.jsx(t,{value:"parsed",children:e.jsx(h,{responseText:a.responseText,responseStatus:a.responseStatus,streaming:a.streaming,inputTokens:a.inputTokens,outputTokens:a.outputTokens,cacheCreationInputTokens:a.cacheCreationInputTokens,cacheReadInputTokens:a.cacheReadInputTokens,apiFormat:a.apiFormat})}),e.jsx(t,{value:"raw",children:e.jsx("pre",{className:"font-mono text-xs whitespace-pre-wrap bg-muted p-3 rounded-md max-h-96 overflow-auto",children:a.responseText})})]}):e.jsx("p",{className:"text-sm text-muted-foreground italic",children:"No original response"})}),s&&s.success&&e.jsx(t,{value:"replay",children:s.responseText!==null?e.jsxs(i,{defaultValue:"parsed",children:[e.jsxs(u,{children:[e.jsx(n,{value:"parsed",children:"Response"}),e.jsx(n,{value:"raw",children:"Raw Response"})]}),e.jsx(t,{value:"parsed",children:e.jsx(h,{responseText:s.responseText??null,responseStatus:s.responseStatus??null,streaming:s.streaming??!1,inputTokens:s.inputTokens??null,outputTokens:s.outputTokens??null,apiFormat:a.apiFormat})}),e.jsx(t,{value:"raw",children:e.jsx("pre",{className:"font-mono text-xs whitespace-pre-wrap bg-muted p-3 rounded-md max-h-96 overflow-auto",children:s.responseText})})]}):e.jsx("p",{className:"text-sm text-muted-foreground italic",children:"No replay response"})})]})]})})}export{H as ReplayDialog};
