(function(){"use strict";const f=new Set(["session_meta","response_item","event_msg","turn_context","compacted"]),p=new Set(["message","function_call","function_call_output","custom_tool_call","custom_tool_call_output","reasoning"]),y=new Set(["user_message","agent_message","agent_reasoning","context_compacted","turn_aborted","token_count"]),d=new Set(["user","assistant","system","summary","permission-mode","file-history-snapshot","last-prompt","ai-title"]),i=t=>typeof t=="object"&&t!==null,u=t=>{try{return JSON.parse(t)}catch{return null}},m=t=>{const e=[];for(const s of t){if(typeof s=="string"){const o=u(s);i(o)&&e.push(o);continue}i(s)&&e.push(s)}return e},g=t=>{const e=[];for(const s of t){if(typeof s=="string"){const o=u(s);i(o)&&e.push(o);continue}i(s)&&e.push(s)}return e},h=t=>{if(typeof t.type!="string")return!1;if(f.has(t.type)){if(t.type==="response_item"){const e=t.payload&&typeof t.payload.type=="string"?t.payload.type:null;return e?p.has(e):!0}if(t.type==="event_msg"){const e=t.payload&&typeof t.payload.type=="string"?t.payload.type:null;return e?y.has(e):!0}return!0}return t.type.startsWith("response_")||t.type.startsWith("event_")},_=t=>typeof t.type!="string"||!d.has(t.type)?!1:t.type==="user"||t.type==="assistant"?i(t.message):typeof t.sessionId=="string"||i(t.snapshot),S=t=>{if(!Array.isArray(t)||t.length===0)return!1;const e=m(t).filter(h);return e.length===0||e.length/t.length<.6?!1:e.some(o=>o.type==="session_meta")?!0:e.filter(o=>f.has(o.type??"")).length/e.length>=.6},N=t=>{if(!Array.isArray(t)||t.length===0)return!1;const e=g(t).filter(_);return e.length===0||e.filter(o=>o.type==="user"||o.type==="assistant").length===0?!1:e.length/t.length>=.4},E=t=>S(t)||N(t),l=t=>typeof t!="object"||t===null?!1:"messages"in t&&Array.isArray(t.messages),O=t=>{let e=null;if(t.length>0&&typeof t[0]=="object"&&!l(t[0])&&(e=t),t.length>0&&typeof t[0]=="string"){let s=!1;try{const o=JSON.parse(t[0]);l(o)&&(s=!0)}catch{s=!0}if(!s){e=[];for(const o of t){const n=JSON.parse(o);e.push(n)}}}if(e!==null){let s=null,o=!1;for(const n in e[0])if(typeof e[0][n]=="string")try{const r=JSON.parse(e[0][n]);if(l(r)){s=n,o=!0;break}}catch{continue}else if(l(e[0][n])){s=n;break}if(s!==null){const n=[];for(const r of e){const a=o?JSON.parse(r[s]):r[s];a.metadata??={};for(const c in r)c!==s&&(a.metadata[`euphonyTransformed-${c}`]=r[c]);n.push(a)}return n}}return null},v=t=>{const e=[];for(const[s,o]of t.entries())if(typeof o=="string"){const n=JSON.parse(o);let r=o;n.conversation_id!==void 0&&n.id===void 0&&(n.id=n.conversation_id,r=JSON.stringify(n)),t[s]=r,e.push(Array.isArray(n.messages))}else{const n=o;n.conversation_id!==void 0&&n.id===void 0&&(n.id=n.conversation_id),t[s]=n,e.push(Array.isArray(n.messages))}return e.every(Boolean)},T=t=>{const e=[];try{const s=JSON.parse(t);return e.push(s),e}catch{for(const o of t.split(`
`))try{e.push(JSON.parse(o))}catch{}}return e},J=t=>{let e=T(t);if(e.length===0)throw new Error("Failed to read any JSON or JSONL data.");if(E(e))return{dataType:"codex",codexSessionData:e};const s=O(e);if(s&&(e=s),!v(e))return{dataType:"json",jsonData:e};const o=[];for(const n of e)typeof n=="string"?o.push(JSON.parse(n)):o.push(n);return{dataType:"conversation",conversationData:o}};self.onmessage=async t=>{if(t.data.command!=="startParseData"){console.error("Worker: unknown message",t.data.command);return}const{requestID:e,sourceName:s,sourceText:o,sourceFile:n}=t.data.payload;try{const r=o??await n?.text();if(r===void 0)throw new Error("No source text or file was provided.");const a=J(r),c={command:"finishParseData",payload:{requestID:e,sourceName:s,...a}};postMessage(c)}catch(r){const a={command:"error",payload:{requestID:e,sourceName:s,message:r instanceof Error?r.message:String(r)}};postMessage(a)}}})();
