/* esm.sh - @assistant-ui/core@0.3.31/dist/runtime/api/message-part-runtime */
import{createToolInteraction as n}from"../utils/tool-interactions.mjs";import{resolveToolApprovalResponse as s}from"../utils/resolveToolApprovalResponse.mjs";import{ToolResponse as l}from"../../../../../../../assistant-stream@^0.3.54?target=es2022";var p=class{get path(){return this.contentBinding.path}contentBinding;messageApi;threadApi;interactionState;interactionMessageId;interactionThreadId;interactionCore;constructor(t,o,e){this.contentBinding=t,this.messageApi=o,this.threadApi=e,this.interactionState=t.getState(),this.interactionMessageId=this.interactionState.type==="tool-call"?o?.getState().id:void 0,this.interactionThreadId=this.interactionState.type==="tool-call"?this.threadApi?.getStateState?.().threadId:void 0,this.interactionCore=this.interactionState.type==="tool-call"?e?.getState():void 0,this.__internal_bindMethods()}__internal_bindMethods(){this.addToolResult=this.addToolResult.bind(this),this.resumeToolCall=this.resumeToolCall.bind(this),this.respondToToolApproval=this.respondToToolApproval.bind(this),this.unstable_recordInteraction=this.unstable_recordInteraction.bind(this),this.getState=this.getState.bind(this),this.subscribe=this.subscribe.bind(this)}getState(){return this.contentBinding.getState()}getInteractionCore(){let t=this.interactionThreadId!==void 0&&this.threadApi?.getStateState?.().threadId===this.interactionThreadId?this.threadApi.getState():this.interactionCore;if(!t)throw new Error("Thread API is not available");return t}addToolResult(t){let o=this.interactionState;if(o.type!=="tool-call")throw new Error("Tried to add tool result to non-tool message part");if(!this.messageApi)throw new Error("Message API is not available. This is likely a bug in assistant-ui.");if(!this.threadApi)throw new Error("Thread API is not available");let e=this.interactionMessageId;if(e===void 0)throw new Error("Message is not available");let r=o.toolName,a=o.toolCallId,i=l.toResponse(t);this.getInteractionCore().addToolResult({messageId:e,toolName:r,toolCallId:a,result:i.result,isError:i.isError,...i.artifact!==void 0&&{artifact:i.artifact},...i.modelContent!==void 0&&{modelContent:i.modelContent}})}resumeToolCall(t){let o=this.interactionState;if(o.type!=="tool-call")throw new Error("Tried to resume tool call on non-tool message part");if(!this.threadApi)throw new Error("Thread API is not available");this.getInteractionCore().resumeToolCall({toolCallId:o.toolCallId,payload:t}),this.unstable_recordInteraction({type:"human-response",payload:t}).catch(()=>{})}respondToToolApproval(t){let o=this.interactionState;if(o.type!=="tool-call")throw new Error("Tried to respond to tool approval on non-tool message part");if(!this.threadApi)throw new Error("Thread API is not available");let e=this.getInteractionCore(),r=this.interactionMessageId===void 0?void 0:e.getMessageById(this.interactionMessageId)?.message.content.find(i=>i.type==="tool-call"&&i.toolCallId===o.toolCallId),a=r?.type==="tool-call"?r.approval:void 0;if(!a||a.approved!==void 0||a.resolution!==void 0)throw new Error("Tool call has no pending approval");return e.respondToToolApproval(s(a,t))}async unstable_recordInteraction(t){let o=this.interactionState;if(o.type!=="tool-call")throw new Error("Tried to record interaction on non-tool message part");let e=this.interactionMessageId;if(e===void 0)throw new Error("Message is not available");let r=this.getInteractionCore();if(!r.unstable_recordToolInteraction)throw new Error("Runtime does not support recording tool interactions.");await r.unstable_recordToolInteraction({messageId:e,toolCallId:o.toolCallId,interaction:n(t)})}subscribe(t){return this.contentBinding.subscribe(t)}};export{p as MessagePartRuntimeImpl};
//# sourceMappingURL=message-part-runtime.mjs.map