/* esm.sh - @assistant-ui/core@0.3.31/dist/runtime/api/message-runtime */
import{getThreadMessageText as g}from"../../utils/text.mjs";import{NestedSubscriptionSubject as p,SKIP_UPDATE as o,ShallowMemoizeSubject as a}from"../../subscribable/subscribable.mjs";import{symbolInnerMessage as h}from"../utils/external-store-message.mjs";import{toMessagePartStatus as u}from"../../utils/normalizePartStatus.mjs";import{reportRunFailure as m}from"../../utils/report-run-failure.mjs";import{MessageAttachmentRuntimeImpl as b}from"./attachment-runtime.mjs";import{EditComposerRuntimeImpl as S}from"./composer-runtime.mjs";import{MessagePartRuntimeImpl as c}from"./message-part-runtime.mjs";var d=(t,e)=>{let s=t.content[e];if(!s)return o;let i=u(t,e,s);return Object.freeze({...s,[h]:s[h],status:i})},I=class{get path(){return this._core.path}_core;_threadBinding;constructor(t,e){this._core=t,this._threadBinding=e,this.composer=new S(new p({path:{...this.path,ref:`${this.path.ref}.composer`,composerSource:"edit"},getState:this._getEditComposerRuntimeCore,subscribe:s=>this._threadBinding.subscribe(s)}),()=>this._threadBinding.getState().beginEdit(this._core.getState().id)),this.__internal_bindMethods()}__internal_bindMethods(){this.reload=this.reload.bind(this),this.delete=this.delete.bind(this),this.getState=this.getState.bind(this),this.subscribe=this.subscribe.bind(this),this.getMessagePartByIndex=this.getMessagePartByIndex.bind(this),this.getMessagePartByToolCallId=this.getMessagePartByToolCallId.bind(this),this.getAttachmentByIndex=this.getAttachmentByIndex.bind(this),this.unstable_getCopyText=this.unstable_getCopyText.bind(this),this.speak=this.speak.bind(this),this.stopSpeaking=this.stopSpeaking.bind(this),this.submitFeedback=this.submitFeedback.bind(this),this.switchToBranch=this.switchToBranch.bind(this)}composer;_getEditComposerRuntimeCore=()=>this._threadBinding.getState().getEditComposer(this._core.getState().id);getState(){return this._core.getState()}delete(){let t=this._core.getState();return this._threadBinding.getState().deleteMessage(t.id)}reload(t={}){let e=this._getEditComposerRuntimeCore(),s=e??this._threadBinding.getState().composer,i=e??s,{runConfig:r=i.runConfig}=t,n=this._core.getState();if(n.role!=="assistant")throw new Error("Can only reload assistant messages");m("Message reload",this._threadBinding.getState().startRun({parentId:n.parentId,sourceId:n.id,runConfig:r}))}speak(){let t=this._core.getState();return this._threadBinding.getState().speak(t.id)}stopSpeaking(){let t=this._core.getState();if(this._threadBinding.getState().speech?.messageId===t.id)this._threadBinding.getState().stopSpeaking();else throw new Error("Message is not being spoken")}submitFeedback({type:t,comment:e}){let s=this._core.getState();this._threadBinding.getState().submitFeedback({messageId:s.id,type:t,...e!==void 0?{comment:e}:void 0})}switchToBranch({position:t,branchId:e}){let s=this._core.getState();if(e&&t)throw new Error("May not specify both branchId and position");if(!e&&!t)throw new Error("Must specify either branchId or position");let i=this._threadBinding.getState().getBranches(s.id),r=e;if(t==="previous"?r=i[s.branchNumber-2]:t==="next"&&(r=i[s.branchNumber]),!r)throw new Error("Branch not found");this._threadBinding.getState().switchToBranch(r)}unstable_getCopyText(){return g(this.getState())}subscribe(t){return this._core.subscribe(t)}getMessagePartByIndex(t){if(t<0)throw new Error("Message part index must be >= 0");return new c(new a({path:{...this.path,ref:`${this.path.ref}.content[${t}]`,messagePartSelector:{type:"index",index:t}},getState:()=>d(this.getState(),t),subscribe:e=>this._core.subscribe(e)}),this._core,this._threadBinding)}getMessagePartByToolCallId(t){return new c(new a({path:{...this.path,ref:`${this.path.ref}.content[toolCallId=${JSON.stringify(t)}]`,messagePartSelector:{type:"toolCallId",toolCallId:t}},getState:()=>{let e=this._core.getState(),s=e.content.findIndex(i=>i.type==="tool-call"&&i.toolCallId===t);return s===-1?o:d(e,s)},subscribe:e=>this._core.subscribe(e)}),this._core,this._threadBinding)}getAttachmentByIndex(t){return new b(new a({path:{...this.path,ref:`${this.path.ref}.attachments[${t}]`,attachmentSource:"message",attachmentSelector:{type:"index",index:t}},getState:()=>{let e=this.getState().attachments?.[t];return e?{...e,source:"message"}:o},subscribe:e=>this._core.subscribe(e)}))}};export{I as MessageRuntimeImpl};
//# sourceMappingURL=message-runtime.mjs.map