mattermost-mobile/app/constants/snack_bar.ts
Nick Misasi 9e0735c00d
Add two-phase tool call approval for agents in channels (#9506)
* Add two-phase tool call approval for agents in channels

Implements the mobile counterpart to the webapp's multiplayer tool calling
feature. When a bot is @mentioned in a channel, tool call arguments and
results are redacted from other members. Only the invoker can approve/reject
tool execution (Phase 1) and decide whether to share results with the
channel (Phase 2).

See mattermost/mattermost-plugin-agents#491 for the server/webapp changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Add tests for channel tool calling utilities and remote actions

Tests for isToolCallRedacted, isPendingToolResult, getToolApprovalStage,
mergeToolCalls utility functions and fetchToolCallPrivate,
fetchToolResultPrivate, submitToolResult remote actions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Add e2e tests and testIDs for agent tool calls in channels

Adds detox e2e tests covering tool call card rendering, approval buttons,
result approval phase, and multi-tool-call scenarios. Adds testID props
to ToolApprovalSet and ToolCard components to support the e2e tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Address PR review feedback for two-phase tool call approval

- Fix mergeToolCalls to preserve public-only tools instead of dropping them
- Fix stale closure race in handleToolDecision with functional setState
- Add forceLogoutIfNecessary to tool_private and tool_result remote actions
- Show snackbar on submit failure using existing error types
- Reset isDM in catch block to prevent stale state
- Sync animation shared values when isCollapsed changes externally
- Clear private data on streaming-to-persisted transition
- Wrap action buttons with usePreventDoubleTap
- Use toolCalls reference instead of toolCalls.length in effect dependency
- Fix grammar in warning callout ("its" -> "their")
- Change fontWeight from number to string per RN conventions

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback: withObservables HOC, memoization, typography

- Refactor AgentPost to use withObservables HOC for channel observation
  instead of useEffect+subscribe, providing isDM as a prop
- Memoize undecidedCount in ToolApprovalSet and move before early return
- Replace manual font styles with typography() utility in ToolCard

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-19 11:20:25 -05:00

290 lines
8.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {defineMessages, type MessageDescriptor} from 'react-intl';
import keyMirror from '@utils/key_mirror';
export const SNACK_BAR_TYPE = keyMirror({
ADD_CHANNEL_MEMBERS: null,
AGENT_STOP_ERROR: null,
AGENT_REGENERATE_ERROR: null,
AGENT_TOOL_APPROVAL_ERROR: null,
AGENT_TOOL_RESULT_ERROR: null,
AGENT_FETCH_PRIVATE_ERROR: null,
CODE_COPIED: null,
FAVORITE_CHANNEL: null,
FILE_DOWNLOAD_REJECTED: null,
FOLLOW_THREAD: null,
INFO_COPIED: null,
LINK_COPIED: null,
LINK_COPY_FAILED: null,
MESSAGE_COPIED: null,
MUTE_CHANNEL: null,
PLUGIN_TOAST: null,
REMOVE_CHANNEL_USER: null,
TEXT_COPIED: null,
UNFAVORITE_CHANNEL: null,
UNMUTE_CHANNEL: null,
UNFOLLOW_THREAD: null,
CREATE_POST_ERROR: null,
CONNECTION_ERROR: null,
SCHEDULED_POST_CREATION_ERROR: null,
RESCHEDULED_POST: null,
DELETE_SCHEDULED_POST_ERROR: null,
PLAYBOOK_ERROR: null,
ENABLE_TRANSLATION: null,
BOR_POST_EXPIRED: null,
});
export const MESSAGE_TYPE = {
SUCCESS: 'success',
ERROR: 'error',
DEFAULT: 'default',
};
export type SnackBarConfig = {
message: MessageDescriptor;
iconName: string;
hasAction: boolean;
type?: typeof MESSAGE_TYPE[keyof typeof MESSAGE_TYPE];
};
const messages = defineMessages({
ADD_CHANNEL_MEMBERS: {
id: 'snack.bar.channel.members.added',
defaultMessage: '{numMembers, number} {numMembers, plural, one {member} other {members}} added',
},
AGENT_STOP_ERROR: {
id: 'snack.bar.agent.stop.error',
defaultMessage: 'Failed to stop generation',
},
AGENT_REGENERATE_ERROR: {
id: 'snack.bar.agent.regenerate.error',
defaultMessage: 'Failed to regenerate response',
},
AGENT_TOOL_APPROVAL_ERROR: {
id: 'snack.bar.agent.tool.approval.error',
defaultMessage: 'Failed to submit tool approval',
},
AGENT_TOOL_RESULT_ERROR: {
id: 'snack.bar.agent.tool.result.error',
defaultMessage: 'Failed to submit tool result',
},
AGENT_FETCH_PRIVATE_ERROR: {
id: 'snack.bar.agent.fetch.private.error',
defaultMessage: 'Failed to fetch private data',
},
CODE_COPIED: {
id: 'snack.bar.code.copied',
defaultMessage: 'Code copied to clipboard',
},
FAVORITE_CHANNEL: {
id: 'snack.bar.favorited.channel',
defaultMessage: 'This channel was favorited',
},
FILE_DOWNLOAD_REJECTED: {
id: 'snack.bar.file.download.rejected',
defaultMessage: 'File access blocked by plugin',
},
FOLLOW_THREAD: {
id: 'snack.bar.following.thread',
defaultMessage: 'Thread followed',
},
INFO_COPIED: {
id: 'snack.bar.info.copied',
defaultMessage: 'Info copied to clipboard',
},
LINK_COPIED: {
id: 'snack.bar.link.copied',
defaultMessage: 'Link copied to clipboard',
},
LINK_COPY_FAILED: {
id: 'gallery.copy_link.failed',
defaultMessage: 'Failed to copy link to clipboard',
},
MESSAGE_COPIED: {
id: 'snack.bar.message.copied',
defaultMessage: 'Text copied to clipboard',
},
MUTE_CHANNEL: {
id: 'snack.bar.mute.channel',
defaultMessage: 'This channel was muted',
},
REMOVE_CHANNEL_USER: {
id: 'snack.bar.remove.user',
defaultMessage: '1 member was removed from the channel',
},
TEXT_COPIED: {
id: 'snack.bar.text.copied',
defaultMessage: 'Copied to clipboard',
},
UNFAVORITE_CHANNEL: {
id: 'snack.bar.unfavorite.channel',
defaultMessage: 'This channel was unfavorited',
},
UNMUTE_CHANNEL: {
id: 'snack.bar.unmute.channel',
defaultMessage: 'This channel was unmuted',
},
UNFOLLOW_THREAD: {
id: 'snack.bar.unfollow.thread',
defaultMessage: 'Thread unfollowed',
},
PLUGIN_TOAST: {
id: 'snack.bar.plugin.toast',
defaultMessage: 'Notification',
},
PLAYBOOK_ERROR: {
id: 'snack.bar.playbook.error',
defaultMessage: 'Unable to perform action. Please try again later.',
},
ENABLE_TRANSLATION: {
id: 'snack.bar.enable.translation',
defaultMessage: 'Enable auto-translation?',
},
BOR_POST_EXPIRED: {
id: 'snack.bar.bor_post_expired.error',
defaultMessage: 'This burn-on-read post has expired and can no longer be revealed.',
},
});
export const SNACK_BAR_CONFIG: Record<string, SnackBarConfig> = {
ADD_CHANNEL_MEMBERS: {
message: messages.ADD_CHANNEL_MEMBERS,
iconName: 'check',
hasAction: false,
},
AGENT_STOP_ERROR: {
message: messages.AGENT_STOP_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_REGENERATE_ERROR: {
message: messages.AGENT_REGENERATE_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_TOOL_APPROVAL_ERROR: {
message: messages.AGENT_TOOL_APPROVAL_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_TOOL_RESULT_ERROR: {
message: messages.AGENT_TOOL_RESULT_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_FETCH_PRIVATE_ERROR: {
message: messages.AGENT_FETCH_PRIVATE_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
CODE_COPIED: {
message: messages.CODE_COPIED,
iconName: 'content-copy',
hasAction: false,
},
FAVORITE_CHANNEL: {
message: messages.FAVORITE_CHANNEL,
iconName: 'star',
hasAction: true,
},
FILE_DOWNLOAD_REJECTED: {
message: messages.FILE_DOWNLOAD_REJECTED,
iconName: 'alert-circle-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
FOLLOW_THREAD: {
message: messages.FOLLOW_THREAD,
iconName: 'check',
hasAction: true,
},
INFO_COPIED: {
message: messages.INFO_COPIED,
iconName: 'content-copy',
hasAction: false,
},
LINK_COPIED: {
message: messages.LINK_COPIED,
iconName: 'link-variant',
hasAction: false,
type: MESSAGE_TYPE.SUCCESS,
},
LINK_COPY_FAILED: {
message: messages.LINK_COPY_FAILED,
iconName: 'link-variant',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
MESSAGE_COPIED: {
message: messages.MESSAGE_COPIED,
iconName: 'content-copy',
hasAction: false,
},
MUTE_CHANNEL: {
message: messages.MUTE_CHANNEL,
iconName: 'bell-off-outline',
hasAction: true,
},
REMOVE_CHANNEL_USER: {
message: messages.REMOVE_CHANNEL_USER,
iconName: 'check',
hasAction: true,
},
TEXT_COPIED: {
message: messages.TEXT_COPIED,
iconName: 'content-copy',
hasAction: false,
type: MESSAGE_TYPE.SUCCESS,
},
UNFAVORITE_CHANNEL: {
message: messages.UNFAVORITE_CHANNEL,
iconName: 'star-outline',
hasAction: true,
},
UNMUTE_CHANNEL: {
message: messages.UNMUTE_CHANNEL,
iconName: 'bell-outline',
hasAction: true,
},
UNFOLLOW_THREAD: {
message: messages.UNFOLLOW_THREAD,
iconName: 'check',
hasAction: true,
},
PLUGIN_TOAST: {
message: messages.PLUGIN_TOAST,
iconName: 'information-outline',
hasAction: false,
type: MESSAGE_TYPE.DEFAULT,
},
PLAYBOOK_ERROR: {
message: messages.PLAYBOOK_ERROR,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
ENABLE_TRANSLATION: {
message: messages.ENABLE_TRANSLATION,
iconName: 'globe',
hasAction: true,
},
BOR_POST_EXPIRED: {
message: messages.BOR_POST_EXPIRED,
iconName: 'alert-outline',
hasAction: false,
type: MESSAGE_TYPE.ERROR,
},
};
export default {
SNACK_BAR_TYPE,
SNACK_BAR_CONFIG,
};