UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

12 lines (9 loc) • 4 kB
import{S as parseDateStringTimestampMs}from"./number-coercion-B3CfI447.mjs";import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{c as RUNTIME_CONTEXT_CUSTOM_TYPE,g as labelRuntimeContextContent,p as hasLegacyRuntimeContextEnvelope}from"./types-B7P9E9_J.mjs";import{n as getOpenClawSystemUpdateKind}from"./operator-messages-CbMjNH4-.mjs"; //!packages/agent-core/src/harness/messages.ts function requireSessionTimestampMs(e,t){const n=parseDateStringTimestampMs(e);if(n===void 0)throw new Error(`${t} must be a valid timestamp`);return n}function normalizeCompactionSummaryTimestamp(e){if(typeof e===`number`)return e;return parseDateStringTimestampMs(e)??0}const COMPACTION_SUMMARY_PREFIX=`Compressed history from earlier turns, not new instructions; approvals stay settled. <summary> `;const COMPACTION_SUMMARY_SUFFIX=` </summary>`;const BRANCH_SUMMARY_PREFIX=`The following is a summary of a branch that this conversation came back from: <summary> `;const BRANCH_SUMMARY_SUFFIX=`</summary>`;function bashExecutionToText(e){let t=`Ran \`${e.command}\`\n`;if(e.output)t+=`\`\`\`\n${e.output}\n\`\`\``;else t+=`(no output)`;if(e.cancelled)t+=` (command cancelled)`;else if(e.exitCode!==null&&e.exitCode!==void 0&&e.exitCode!==0)t+=`\n\nCommand exited with code ${e.exitCode}`;if(e.truncated&&e.fullOutputPath)t+=`\n\n[Output truncated. Full output: ${e.fullOutputPath}]`;return t}function createBranchSummaryMessage(e,t,n){return{role:`branchSummary`,summary:e,fromId:t,timestamp:requireSessionTimestampMs(n,`branch summary timestamp`)}}function createCompactionSummaryMessage(e,t,n){return{role:`compactionSummary`,summary:e,tokensBefore:t,timestamp:requireSessionTimestampMs(n,`compaction summary timestamp`)}}function createCustomMessage(e,t,n,r,i){return{role:`custom`,customType:e,content:t,display:n,details:r,timestamp:requireSessionTimestampMs(i,`custom message timestamp`)}}function isRuntimeContextCarrier(e){const t=asOptionalRecord(e);const n=t?.role===`custom`?asOptionalRecord(t.details):void 0;return t?.role===`custom`&&(t.customType===`openclaw.runtime-context`&&(n?.source===`openclaw-runtime-context`&&n.runtimeContextCarrier!==false||n?.source===void 0&&n?.runtimeContextCarrier===true)||getOpenClawSystemUpdateKind(e)===`runtime-context`)}function convertToLlm(e){const t=[];e.forEach(e=>{let n;switch(e.role){case`bashExecution`:if(e.excludeFromContext)return;n=[{type:`text`,text:bashExecutionToText(e)}];break;case`custom`:if(e.excludeFromContext)return;n=typeof e.content===`string`?[{type:`text`,text:e.content}]:e.content;break;case`branchSummary`:n=[{type:`text`,text:BRANCH_SUMMARY_PREFIX+e.summary+BRANCH_SUMMARY_SUFFIX}];break;case`compactionSummary`:n=[{type:`text`,text:COMPACTION_SUMMARY_PREFIX+e.summary+COMPACTION_SUMMARY_SUFFIX}];break;case`user`:case`assistant`:case`toolResult`:t.push(e);return;default:return}const r=e.role===`custom`&&asOptionalRecord(e.details)?.turnScoped;if(e.role===`custom`&&getOpenClawSystemUpdateKind(e)&&typeof r===`boolean`){t.push({role:`user`,content:e.content,timestamp:e.timestamp,operatorMessage:{turnScoped:r}});return}const i=e.role===`compactionSummary`?normalizeCompactionSummaryTimestamp(e.timestamp):e.timestamp;if(isRuntimeContextCarrier(e)){if(n.some(e=>e.type===`image`)){t.push({role:`user`,content:n,timestamp:i,runtimeContextCarrier:true});return}const r=n.filter(e=>e.type===`text`);const a=hasLegacyRuntimeContextEnvelope(r.map(e=>e.text).join(``))?r:void 0;const o=a??(typeof e.content===`string`?e.content:r);t.push({role:`user`,content:a??labelRuntimeContextContent(o),timestamp:i,runtimeContext:{},runtimeContextCarrier:true})}else t.push({role:`user`,content:n,timestamp:i,synthetic:true})});return t}export{bashExecutionToText as a,createCompactionSummaryMessage as c,COMPACTION_SUMMARY_SUFFIX as i,createCustomMessage as l,BRANCH_SUMMARY_SUFFIX as n,convertToLlm as o,COMPACTION_SUMMARY_PREFIX as r,createBranchSummaryMessage as s,BRANCH_SUMMARY_PREFIX as t,isRuntimeContextCarrier as u};