shennian 0.3.53 → 0.3.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import j from"node:fs";import
|
|
1
|
+
import j from"node:fs";import w from"node:os";import I from"node:path";import{createHash as Z}from"node:crypto";import{buildApprovalPendingPayload as P,buildUserMessagePayload as k,isSystemControlPayload as M,isToolPayload as W}from"@shennian/wire";import{buildImagePayload as D,buildToolPayload as K,inferMimeType as N,makeCursor as q,makeEventKey as H,normalizeText as h,parseStructuredString as O,readJsonlLines as U,readTimestamp as tt,safeParse as R}from"./parser-common.js";const et=new Set(["untitled","new chat","new conversation","new task","new thread","codex","\u65E0\u6807\u9898","\u672A\u547D\u540D","\u65B0\u4EFB\u52A1","\u65B0\u4F1A\u8BDD","\u65B0\u5BF9\u8BDD"]);function nt(t){if(typeof t=="string"){const i=t.trim();return i?{path:i,name:I.basename(i)||"image.png",mimeType:N(i),kind:"image"}:null}if(typeof t!="object"||t===null)return null;const r=t,n=typeof r.path=="string"?r.path:typeof r.file_path=="string"?r.file_path:typeof r.saved_path=="string"?r.saved_path:"";if(!n.trim())return null;const e=typeof r.name=="string"&&r.name.trim()?r.name:I.basename(n)||"image.png",o=typeof r.mimeType=="string"&&r.mimeType.trim()?r.mimeType:N(n);return{path:n,name:e,mimeType:o,kind:"image"}}function z(t){return Array.isArray(t)?t.map(n=>{if(typeof n=="string")return n;if(typeof n=="object"&&n!==null){const e=n;return typeof e.text=="string"?e.text:""}return""}).map(n=>h(n)).filter(Boolean).join(`
|
|
2
2
|
|
|
3
|
-
`):""}function
|
|
3
|
+
`):""}function F(t){const r=h(t);if(!r)return{text:"",strippedWrapper:!1};const n=r.match(/My request for Codex:\s*([\s\S]*)$/i);return n&&n[1]?{text:rt(n[1]),strippedWrapper:!0}:{text:r,strippedWrapper:!1}}function rt(t){return h(t.replace(/<image>\s*<\/image>/gi,""))}function L(t){const r=h(t);if(!r)return!1;if(M(r)||/^<environment_context>[\s\S]*<\/environment_context>$/i.test(r)&&r.includes("<current_date>")&&r.includes("<timezone>")&&r.includes("<workspace_roots>"))return!0;const n=r.startsWith("<recommended_plugins>")&&r.includes("</recommended_plugins>"),e=r.includes("# AGENTS.md instructions for ")&&r.includes("<INSTRUCTIONS>")&&r.includes("</INSTRUCTIONS>"),o=r.includes("<environment_context>")&&r.includes("</environment_context>");return e&&o&&(r.startsWith("# AGENTS.md instructions for ")||n)}function B(t){const r=z(t.text_elements),n=Array.isArray(t.input)?z(t.input):"",e=typeof t.message=="string"?F(t.message):{text:"",strippedWrapper:!1},o=e.text,i=r||n||o,u=e.strippedWrapper?"codex_app_context_wrapper":void 0,a=Array.isArray(t.local_images)?t.local_images.map(nt).filter(f=>f!=null):[];return!i&&a.length===0||M(i)&&a.length===0||L(i)&&a.length===0?null:a.length>0?{payload:k(i,a),titleText:i,repairHint:u}:{payload:i,titleText:i,repairHint:u}}function ot(t){return t==="turn_completed"||t==="turn_complete"||t==="task_complete"}function it(t){return/approval|permission|confirm|user[_-]?input|input[_-]?required/i.test(t)}function st(t){const r=typeof t.command=="string"?t.command:Array.isArray(t.command)?t.command.join(" "):"",n=typeof t.prompt=="string"?t.prompt:typeof t.reason=="string"?t.reason:typeof t.message=="string"?t.message:r?`Codex \u8BF7\u6C42\u6267\u884C\u547D\u4EE4\uFF1A${r}`:"Codex \u6B63\u5728\u7B49\u5F85\u7528\u6237\u786E\u8BA4\u540E\u7EE7\u7EED\u3002";return P({title:"\u9700\u8981\u7528\u6237\u786E\u8BA4",content:n,source:"codex",actionHint:"\u5F53\u524D\u795E\u5FF5\u6682\u4E0D\u652F\u6301\u8FDC\u7A0B\u786E\u8BA4\uFF0C\u8BF7\u56DE\u5230\u8FD0\u884C Codex \u7684\u7535\u8111/\u7EC8\u7AEF\u5B8C\u6210\u786E\u8BA4\u3002"})}function G(t,r){return Array.isArray(t)?h(t.map(n=>{if(typeof n=="string")return n;if(typeof n!="object"||n===null)return"";const e=n;return e.type!==r?"":typeof e.text=="string"?e.text:""}).filter(Boolean).join(`
|
|
4
4
|
|
|
5
|
-
`)):""}function
|
|
6
|
-
(exit ${String(l)})`:""].join("");x(
|
|
5
|
+
`)):""}function ct(t){if(!Array.isArray(t))return[];const r=[];for(const n of t){if(typeof n!="object"||n===null)continue;const e=n;if(e.type!=="input_image")continue;const o=typeof e.path=="string"?e.path:typeof e.file_path=="string"?e.file_path:typeof e.saved_path=="string"?e.saved_path:typeof e.image_path=="string"?e.image_path:"";o.trim()&&r.push({path:o,name:I.basename(o)||"image.png",mimeType:N(o),kind:"image"})}return r}function J(t){if(t.type!=="message")return null;const r=t.role==="assistant"?"agent":t.role==="user"?"user":null;if(!r)return null;if(r==="agent"){const i=G(t.content,"output_text");return M(i)?null:i?{role:r,payload:i,titleText:i}:null}const n=F(G(t.content,"input_text")),e=n.text,o=ct(t.content);return!e&&o.length===0||M(e)&&o.length===0||L(e)&&o.length===0?null:{role:r,payload:o.length>0?k(e,o):e,titleText:e,repairHint:n.strippedWrapper?"codex_app_context_wrapper":void 0}}function X(t){if(!t)return"";try{const r=JSON.parse(t);if(typeof r=="object"&&r!==null){const n=r;if(n.type==="user"&&typeof n.content=="string")return h(n.content)}}catch{}return h(t)}function ut(t,r,n,e){return t.agentType!=="codex"||t.role!==r||W(t.payload)||Math.abs(t.ts-e)>300*1e3?!1:X(t.payload)===X(n)}function $(t,r,n,e){for(let o=t.length-1;o>=0;o-=1){const i=t[o];if(i){if(ut(i,r,n,e))return o;if(i.agentType==="codex"&&Math.abs(i.ts-e)>300*1e3)break}}return-1}function v(t,r,n,e,o,i,u,a,f,c,s="agent",l=!1,m){u&&t.push({agentType:"codex",sourceMode:"codex_rollout_import",sourceSessionKey:o,sourceEventKey:H(r,n,e),cursor:q(r,n),role:s,ts:i,payload:u,title:a,modelId:f,workDir:c,terminal:l,repairHint:m})}function x(t,r,n,e,o,i,u,a,f,c,s,l,m){const A=H(r,n,e),y=typeof m?.callId=="string"&&m.callId.trim()?m.callId.trim():null,T=y?Z("sha256").update(`${o}\0${y}`).digest("hex").slice(0,32):null,p=T?`tool:${T}`:void 0,C=T?`native:tool:${T}`:`native:${A}`,g=m?.completed&&T?2:1,E=l!==void 0?"completed":"running";t.push({agentType:"codex",sourceMode:"codex_rollout_import",sourceSessionKey:o,sourceEventKey:A,cursor:q(r,n),role:"agent",ts:i,payload:K({name:u,toolCallId:C,detailRevision:g,status:E}),title:a,modelId:f,workDir:c,...p?{canonicalMessageKey:p,canonicalMutation:m?.completed?"upsert":"append"}:{},localToolDetail:{toolCallId:C,detailRevision:g,name:u,...s!==void 0?{args:s}:{},...l!==void 0?{result:l}:{},status:E}})}function at(t,r,n,e,o,i,u,a,f){const c=typeof e.type=="string"?e.type:"";if(c){if(c==="message"){const s=J(e);if(!s||$(t,s.role,s.payload,i)>=0)return;v(t,r,n,`${c}:${s.role}`,o,i,s.payload,u,a,f,s.role,!1,s.repairHint);return}if(c==="function_call"){const s=typeof e.name=="string"?e.name:"function_call";x(t,r,n,c,o,i,s,u,a,f,O(e.arguments),void 0,{callId:e.call_id});return}if(c==="function_call_output"){x(t,r,n,c,o,i,"function_call_output",u,a,f,void 0,e.output,{callId:e.call_id,completed:!0});return}if(c==="custom_tool_call"){const s=typeof e.name=="string"?e.name:"custom_tool_call",l=e.input!==void 0?{input:e.input}:void 0;x(t,r,n,c,o,i,s,u,a,f,l,void 0,{callId:e.call_id});return}if(c==="custom_tool_call_output"){const s=typeof e.name=="string"?e.name:"custom_tool_call_output",l=e.output??e.result??e.content;x(t,r,n,c,o,i,s,u,a,f,void 0,l,{callId:e.call_id,completed:!0});return}if(c==="web_search_call"){const s=typeof e.action=="object"&&e.action!==null?e.action:null;x(t,r,n,c,o,i,"web_search",u,a,f,s??e.query);return}}}function ft(t,r,n,e,o,i,u,a,f){const c=typeof e.type=="string"?e.type:"";if(c){if(c==="user_message"){const s=B(e);if(!s)return;v(t,r,n,c,o,i,s.payload,u,a,f,"user",!1,s.repairHint);return}if(c==="agent_message"){const s=typeof e.message=="string"?h(e.message):"";if(!s||M(s))return;v(t,r,n,c,o,i,s,u,a,f);return}if(c==="turn_aborted"||c==="error"){const s=typeof e.message=="string"?h(e.message):typeof e.error=="string"?h(e.error):"";if(!s)return;v(t,r,n,c,o,i,s,u,a,f,"agent",!0);return}if(it(c)){v(t,r,n,c,o,i,st(e),u,a,f,"agent");return}if(c==="exec_command_end"){const s=Array.isArray(e.command)?e.command:e.parsed_cmd??e.command,l=e.exit_code??e.exitCode,m=[typeof e.aggregated_output=="string"?e.aggregated_output:"",l!=null?`
|
|
6
|
+
(exit ${String(l)})`:""].join("");x(t,r,n,c,o,i,"command_execution",u,a,f,{command:s},m);return}if(c==="patch_apply_end"){x(t,r,n,c,o,i,"apply_patch",u,a,f,e.changes,{success:e.success,stdout:e.stdout,stderr:e.stderr});return}if(c==="mcp_tool_call_end"){const s=typeof e.invocation=="object"&&e.invocation!==null?e.invocation:null,l=typeof s?.tool=="string"?s.tool:"mcp_tool_call",m={server:s?.server,...typeof s?.arguments=="object"&&s.arguments!==null?s.arguments:{}};x(t,r,n,c,o,i,l,u,a,f,m,e.result);return}if(c==="dynamic_tool_call_request"){const s=typeof e.tool=="string"?e.tool:"dynamic_tool_call";x(t,r,n,c,o,i,s,u,a,f,e.arguments,void 0,{callId:e.callId??e.call_id});return}if(c==="dynamic_tool_call_response"){const s=typeof e.tool=="string"?e.tool:"dynamic_tool_call";x(t,r,n,c,o,i,s,u,a,f,void 0,e.result??e.output,{callId:e.callId??e.call_id,completed:!0});return}if(c==="web_search_end"){const s=typeof e.action=="object"&&e.action!==null?e.action:null;x(t,r,n,c,o,i,"web_search",u,a,f,s??e.query,e.query);return}if(c==="image_generation_end"){const s=typeof e.saved_path=="string"?e.saved_path.trim():"";if(!s)return;v(t,r,n,c,o,i,D(s),u,a,f);return}c==="view_image_tool_call"&&x(t,r,n,c,o,i,"view_image",u,a,f,e.path??e.image_path??e)}}function S(t=!1){return{sourceSessionKey:"",workDir:"",modelId:null,title:"",skipImport:t}}function Q(t){if((typeof t.thread_source=="string"?t.thread_source.trim().toLowerCase():"")==="subagent")return!0;const n=typeof t.source=="object"&&t.source!==null?t.source:null;if(n&&typeof n.subagent=="object"&&n.subagent!==null)return!0;const e=typeof t.parent_thread_id=="string"&&t.parent_thread_id.trim().length>0,o=typeof t.agent_nickname=="string"||typeof t.agent_role=="string"||typeof t.multi_agent_version=="string";return e&&o}function lt(t){const r=j.openSync(t,"r");try{const n=[];let e=0,o="";for(;e<1024*1024;){const f=Buffer.alloc(16384),c=j.readSync(r,f,0,f.length,e);if(c<=0)break;const s=f.subarray(0,c);n.push(s),e+=c;const l=Buffer.concat(n),m=l.indexOf(10);if(m>=0){o=l.subarray(0,m).toString("utf8");break}}!o&&n.length>0&&(o=Buffer.concat(n).toString("utf8"));const i=R(o);if(!i||i.type!=="session_meta")return S();const u=typeof i.payload=="object"&&i.payload!==null?i.payload:null;if(!u)return S();if(Q(u))return S(!0);const a=typeof u.id=="string"?u.id:"";return{sourceSessionKey:a,workDir:typeof u.cwd=="string"?u.cwd:"",modelId:V(u),title:Y(a)??"",skipImport:!1}}finally{j.closeSync(r)}return S()}function V(t){return(typeof t.model=="string"?t.model.trim():"")||null}function mt(t,r){if((typeof t.id=="string"?t.id:typeof t.thread_id=="string"?t.thread_id:typeof t.session_id=="string"?t.session_id:"")!==r)return null;const o=(typeof t.thread_name=="string"?t.thread_name:typeof t.title=="string"?t.title:"").trim();return o&&!pt(o)?o.slice(0,120):null}function pt(t){const r=t.trim().toLocaleLowerCase().replace(/[\s_-]+/g," ");return et.has(r)}function Y(t){if(!t)return null;const r=I.join(w.homedir(),".codex","session_index.jsonl");if(!j.existsSync(r))return null;let n=null;return U(r,0,e=>{if(!e.trim())return;const o=R(e);if(!o)return;const i=mt(o,t);i&&(n=i)}),n}function Tt(){const t=[I.join(w.homedir(),".codex","sessions"),I.join(w.homedir(),".codex","archived_sessions")],r=[],n=e=>{for(const o of j.readdirSync(e,{withFileTypes:!0})){const i=I.join(e,o.name);o.isDirectory()?n(i):o.isFile()&&o.name.endsWith(".jsonl")&&r.push(i)}};for(const e of t)j.existsSync(e)&&n(e);return[...new Set(r)].sort()}function Ct(t,r){const n=[],e=lt(t);let{sourceSessionKey:o,workDir:i,modelId:u,title:a,skipImport:f}=e,c=e.title;if(f)return{nextOffset:j.statSync(t).size,events:[]};let s=!1;const l=U(t,r,(m,A)=>{const y=R(m);if(!y)return;const T=typeof y.type=="string"?y.type:"",p=typeof y.payload=="object"&&y.payload!==null?y.payload:null,C=tt(y.timestamp);if(!(!p||!C)){if(T==="session_meta"){if(Q(p)){n.length=0,f=!0;return}o=typeof p.id=="string"?p.id:o,i=typeof p.cwd=="string"?p.cwd:i,u=V(p)??u;const g=Y(o);g&&(c=g,a=g);return}if(!f&&o){if(T==="response_item"){const g=J(p);g?.role==="user"&&g.titleText&&!a&&(a=g.titleText.slice(0,80)),at(n,t,A,p,o,C,a,u,i);return}if(T==="event_msg"){const g=typeof p.type=="string"?p.type:"";if(ot(g)){let d=!1;for(let _=n.length-1;_>=0;_-=1){const b=n[_];if(b?.agentType==="codex"&&b.role==="agent"&&!W(b.payload)){b.terminal=!0,d=!0,s=!1;break}}if(!d)for(let _=n.length-1;_>=0;_-=1){const b=n[_];if(!(b?.agentType!=="codex"||b.role!=="agent")){b.terminal=!0,d=!0,s=!1;break}}d||(s=!0);return}if(g==="user_message"){const d=B(p);if(d?.titleText&&!a&&(a=d.titleText.slice(0,80)),d){const _=$(n,"user",d.payload,C);_>=0&&n.splice(_,1)}}else if(g==="agent_message"){const d=typeof p.message=="string"?h(p.message):"";if(d&&!M(d)){const _=$(n,"agent",d,C);_>=0&&n.splice(_,1)}}const E=n.length;ft(n,t,A,p,o,C,a,u,i),s&&n.length>E&&(n[n.length-1].terminal=!0,s=!1)}}}});for(const m of n)m.titleSource=c&&m.title===c?"agent_native":"first_user_fallback";return{nextOffset:l,events:n}}export{Tt as listCodexRolloutFiles,Y as lookupCodexThreadName,Ct as parseCodexRolloutChunk};
|