function coalesceTurnInputs(e,t){let n=coalesceInputResponses({a:e.inputResponses,b:t.inputResponses}),r=coalesceMessage({a:e.message,b:t.message}),i=coalesceContext({a:e.context,b:t.context}),a=t.outputSchema??e.outputSchema,o={};return n!==void 0&&(o.inputResponses=n),r!==void 0&&(o.message=r),i!==void 0&&(o.context=i),a!==void 0&&(o.outputSchema=a),o}function normalizeUserContent(e){if(e===void 0)return;if(typeof e==`string`)return e.trim().length>0?e:void 0;let t=e.filter(e=>e.type!==`text`||e.text.trim().length>0);if(t.length!==0)return t.length===e.length?e:t}function resolveAssistantStepText(e,t){for(let t=e.length-1;t>=0;--t){let n=e[t];if(n?.role!==`assistant`)continue;let r=extractMessageText(n);if(r.trim().length>0)return r}return t!==void 0&&t.trim().length>0?t:null}function extractMessageText(e){return typeof e.content==`string`?e.content:Array.isArray(e.content)?e.content.flatMap(e=>typeof e==`string`?[e]:`type`in e&&e.type===`text`&&typeof e.text==`string`?[e.text]:[]).join(``):``}function coalesceInputResponses(e){let t=e.a??[],n=e.b??[];if(!(t.length===0&&n.length===0))return[...t,...n]}function coalesceContext(e){let t=e.a??[],n=e.b??[];if(!(t.length===0&&n.length===0))return[...t,...n]}function coalesceMessage(e){let t=normalizeUserContent(e.a),n=normalizeUserContent(e.b);return t===void 0?n:n===void 0?t:appendUserContent({appended:n,existing:t})}function appendUserContent(e){return typeof e.existing==`string`&&typeof e.appended==`string`?`${e.existing}\n\n${e.appended}`:[...toUserContentArray(e.existing),...toUserContentArray(e.appended)]}function toUserContentArray(e){return typeof e==`string`?e.length>0?[{type:`text`,text:e}]:[]:Array.isArray(e)?[...e]:[]}function coalesceDeliveries(e){let[t,...n]=e;if(t===void 0)throw Error(`Cannot coalesce an empty delivery batch.`);let r=t.auth,i=[...t.payloads];for(let e of n)e.auth!==void 0&&(r=e.auth),i.push(...e.payloads);return{...t,auth:r,payloads:i}}export{appendUserContent,coalesceDeliveries,coalesceTurnInputs,normalizeUserContent,resolveAssistantStepText};