Skip to content

addToolApprovalResponse on sendAutomaticallyWhen doesn't send body #11423

@noyofi

Description

@noyofi

Description

const { messages, sendMessage, addToolApprovalResponse } = useChat({
  sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses, // will be turned off later (see below)
})

const model = 'gpt-4o'

// I'm sending model info to backend
sendMessage({ text: 'hey' }, { body: { model }})

// ...later, on approval
// seems like `addToolApprovalResponse` on `sendAutomaticallyWhen` doesn't send `body` like sendMessage does
addToolApprovalResponse({ id, approved: true })
// so as a workaround, I turned off `sendAutomaticalyWhen` and sendMessage manually after addToolApprovalResponse
sendMessage(undefined, { body: { model }})

Why this happens?

packages\ai\src\ui\chat.ts > AbstractChat > sendMessage executes makeRequest with options which has .body in it

    await this.makeRequest({
      trigger: 'submit-message',
      messageId: message.messageId,
      ...options,
    });

but AbstractChat > addToolApprovalResponse does not.

// automatically send the message if the sendAutomaticallyWhen function returns true
      if (
        this.status !== 'streaming' &&
        this.status !== 'submitted' &&
        this.sendAutomaticallyWhen?.({ messages: this.state.messages })
      ) {
        // no await to avoid deadlocking
        this.makeRequest({
          trigger: 'submit-message',
          messageId: this.lastMessage?.id,
          // no options here! <-
        });
      }

Idea

You can keep previous body in AbstractChat.body property?

Related

AI SDK Version

"ai": "^6.0.0-beta.151",

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions