Skip to main content
Monitors messages for completed tool calls and triggers action callbacks. Used internally by useAssistant.

Import

import { useToolExecution } from "modifywithai"

Usage

useToolExecution({
    messages,
    onAction: (action) => {
        console.log("Executing:", action.name, action.options)
    },
})

Options

OptionTypeRequiredDescription
messagesMessage[]YesMessages from the assistant
onAction(action: ToolAction) => voidYesCalled for each action

How It Works

  1. Watches messages for new tool invocations
  2. Filters for performActions tool calls with state "result"
  3. Extracts the action from the result
  4. Calls onAction with the extracted action

When to Use

Usually you don’t need this directly—useAssistant handles it. Use it when:
  • Building a custom assistant implementation
  • Need finer control over action execution timing
  • Processing messages from a different source

Example

import { useToolExecution } from "modifywithai"

function CustomAssistant({ messages }) {
    useToolExecution({
        messages,
        onAction: (action) => {
            switch (action.name) {
                case "createItem":
                    createItem(action.options)
                    break
                case "deleteItem":
                    deleteItem(action.options.id)
                    break
            }
        },
    })

    return <div>{/* Custom UI */}</div>
}