Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ libraries/**/**/.build
# Android sounds
android/app/src/main/res/raw/*
.aider*

# Claude Code
.claude/settings.local.json
366 changes: 366 additions & 0 deletions CLAUDE.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions app/actions/websocket/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {handleAgentPostUpdate} from '@agents/actions/websocket';

import * as bookmark from '@actions/local/channel_bookmark';
import * as scheduledPost from '@actions/websocket/scheduled_post';
import * as calls from '@calls/connection/websocket_event_handlers';
Expand Down Expand Up @@ -301,6 +303,11 @@ export async function handleWebSocketEvent(serverUrl: string, msg: WebSocketMess
case WebsocketEvents.CUSTOM_PROFILE_ATTRIBUTES_FIELD_DELETED:
handleCustomProfileAttributesFieldDeletedEvent(serverUrl, msg);
break;

// Agents
case WebsocketEvents.AGENTS_POST_UPDATE:
handleAgentPostUpdate(msg);
break;
}
handlePlaybookEvents(serverUrl, msg);
}
8 changes: 7 additions & 1 deletion app/actions/websocket/posts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import streamingStore from '@agents/store/streaming_store';
import {isAgentPost} from '@agents/utils';
import {DeviceEventEmitter} from 'react-native';

import {storeMyChannelsForTeam, markChannelAsUnread, markChannelAsViewed, updateLastPostAt} from '@actions/local/channel';
Expand Down Expand Up @@ -267,7 +269,11 @@ export async function handlePostEdited(serverUrl: string, msg: WebSocketMessage)
});
models.push(...postModels);

operator.batchRecords(models, 'handlePostEdited');
await operator.batchRecords(models, 'handlePostEdited');

if (isAgentPost(post)) {
streamingStore.removePost(post.id);
}
}

export async function handlePostDeleted(serverUrl: string, msg: WebSocketMessage) {
Expand Down
4 changes: 4 additions & 0 deletions app/client/rest/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import ClientAgents, {type ClientAgentsMix} from '@agents/client/rest';

import ClientCalls, {type ClientCallsMix} from '@calls/client/rest';
import ClientPlugins, {type ClientPluginsMix} from '@client/rest/plugins';
import ClientPlaybooks, {type ClientPlaybooksMix} from '@playbooks/client/rest';
Expand Down Expand Up @@ -30,6 +32,7 @@ import ClientUsers, {type ClientUsersMix} from './users';
import type {APIClientInterface} from '@mattermost/react-native-network-client';

interface Client extends ClientBase,
ClientAgentsMix,
ClientAppsMix,
ClientCategoriesMix,
ClientChannelsMix,
Expand Down Expand Up @@ -57,6 +60,7 @@ interface Client extends ClientBase,
}

class Client extends mix(ClientBase).with(
ClientAgents,
ClientApps,
ClientCategories,
ClientChannels,
Expand Down
15 changes: 15 additions & 0 deletions app/components/post_list/post/post.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import AgentPost from '@agents/components/agent_post';
import {isAgentPost} from '@agents/utils';
import React, {type ReactNode, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {useIntl} from 'react-intl';
import {Keyboard, Platform, type StyleProp, View, type ViewStyle, TouchableHighlight} from 'react-native';
Expand Down Expand Up @@ -157,6 +159,7 @@ const Post = ({
const isFailed = isPostFailed(post);
const isSystemPost = isSystemMessage(post);
const isCallsPost = isCallsCustomMessage(post);
const isAgentPostType = isAgentPost(post);
const hasBeenDeleted = (post.deleteAt !== 0);
const isWebHook = isFromWebhook(post);
const hasSameRoot = useMemo(() => {
Expand Down Expand Up @@ -251,6 +254,8 @@ const Post = ({
clearTimeout(t);
}
};

// eslint-disable-next-line react-hooks/exhaustive-deps -- Timer only needs to reset when post.id changes, not on other prop updates
}, [post.id]);

useEffect(() => {
Expand All @@ -264,6 +269,8 @@ const Post = ({

PerformanceMetricsManager.finishLoad(location === 'Thread' ? 'THREAD' : 'CHANNEL', serverUrl);
PerformanceMetricsManager.endMetric('mobile_channel_switch', serverUrl);

// eslint-disable-next-line react-hooks/exhaustive-deps -- Performance metrics should only run once on mount
}, []);

const highlightSaved = isSaved && !skipSavedHeader;
Expand Down Expand Up @@ -356,6 +363,14 @@ const Post = ({
joiningChannelId={null}
/>
);
} else if (isAgentPostType && !hasBeenDeleted) {
body = (
<AgentPost
post={post}
currentUserId={currentUser?.id}
location={location}
/>
);
} else {
body = (
<Body
Expand Down
2 changes: 2 additions & 0 deletions app/constants/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const PostTypes = {
SYSTEM_AUTO_RESPONDER: 'system_auto_responder',
CUSTOM_CALLS: 'custom_calls',
CUSTOM_CALLS_RECORDING: 'custom_calls_recording',
CUSTOM_LLMBOT: 'custom_llmbot',
CUSTOM_LLM_POSTBACK: 'custom_llm_postback',
} as const;

export const PostPriorityColors = {
Expand Down
33 changes: 33 additions & 0 deletions app/constants/snack_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ 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,
CODE_COPIED: null,
FAVORITE_CHANNEL: null,
FOLLOW_THREAD: null,
Expand Down Expand Up @@ -46,6 +49,18 @@ const messages = defineMessages({
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',
},
CODE_COPIED: {
id: 'snack.bar.code.copied',
defaultMessage: 'Code copied to clipboard',
Expand Down Expand Up @@ -110,6 +125,24 @@ export const SNACK_BAR_CONFIG: Record<string, SnackBarConfig> = {
iconName: 'check',
canUndo: false,
},
AGENT_STOP_ERROR: {
message: messages.AGENT_STOP_ERROR,
iconName: 'alert-outline',
canUndo: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_REGENERATE_ERROR: {
message: messages.AGENT_REGENERATE_ERROR,
iconName: 'alert-outline',
canUndo: false,
type: MESSAGE_TYPE.ERROR,
},
AGENT_TOOL_APPROVAL_ERROR: {
message: messages.AGENT_TOOL_APPROVAL_ERROR,
iconName: 'alert-outline',
canUndo: false,
type: MESSAGE_TYPE.ERROR,
},
CODE_COPIED: {
message: messages.CODE_COPIED,
iconName: 'content-copy',
Expand Down
3 changes: 3 additions & 0 deletions app/constants/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ const WebsocketEvents = {
CUSTOM_PROFILE_ATTRIBUTES_FIELD_UPDATED: 'custom_profile_attributes_field_updated',
CUSTOM_PROFILE_ATTRIBUTES_FIELD_CREATED: 'custom_profile_attributes_field_created',
CUSTOM_PROFILE_ATTRIBUTES_FIELD_DELETED: 'custom_profile_attributes_field_deleted',

// Agents
AGENTS_POST_UPDATE: 'custom_mattermost-ai_postupdate',
};

export default WebsocketEvents;
83 changes: 83 additions & 0 deletions app/products/agents/actions/remote/generation_controls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import NetworkManager from '@managers/network_manager';
import {getFullErrorMessage} from '@utils/errors';
import {logError} from '@utils/log';

import {
stopGeneration,
regenerateResponse,
} from './generation_controls';

jest.mock('@managers/network_manager');
jest.mock('@utils/errors');
jest.mock('@utils/log');

const serverUrl = 'https://test.mattermost.com';
const postId = 'test-post-id';

const mockClient = {
stopGeneration: jest.fn(),
regenerateResponse: jest.fn(),
};

beforeAll(() => {
jest.mocked(NetworkManager.getClient).mockReturnValue(mockClient as any);
});

beforeEach(() => {
jest.clearAllMocks();
});

describe('stopGeneration', () => {
it('should call client.stopGeneration and return empty object on success', async () => {
mockClient.stopGeneration.mockResolvedValue(undefined);

const result = await stopGeneration(serverUrl, postId);

expect(NetworkManager.getClient).toHaveBeenCalledWith(serverUrl);
expect(mockClient.stopGeneration).toHaveBeenCalledWith(postId);
expect(result).toEqual({});
expect(result.error).toBeUndefined();
});

it('should return error object and log error on failure', async () => {
const error = new Error('Network error');
const errorMessage = 'Network error occurred';
mockClient.stopGeneration.mockRejectedValue(error);
jest.mocked(getFullErrorMessage).mockReturnValue(errorMessage);

const result = await stopGeneration(serverUrl, postId);

expect(logError).toHaveBeenCalledWith('[stopGeneration]', error);
expect(getFullErrorMessage).toHaveBeenCalledWith(error);
expect(result).toEqual({error: errorMessage});
});
});

describe('regenerateResponse', () => {
it('should call client.regenerateResponse and return empty object on success', async () => {
mockClient.regenerateResponse.mockResolvedValue(undefined);

const result = await regenerateResponse(serverUrl, postId);

expect(NetworkManager.getClient).toHaveBeenCalledWith(serverUrl);
expect(mockClient.regenerateResponse).toHaveBeenCalledWith(postId);
expect(result).toEqual({});
expect(result.error).toBeUndefined();
});

it('should return error object and log error on failure', async () => {
const error = new Error('Network error');
const errorMessage = 'Network error occurred';
mockClient.regenerateResponse.mockRejectedValue(error);
jest.mocked(getFullErrorMessage).mockReturnValue(errorMessage);

const result = await regenerateResponse(serverUrl, postId);

expect(logError).toHaveBeenCalledWith('[regenerateResponse]', error);
expect(getFullErrorMessage).toHaveBeenCalledWith(error);
expect(result).toEqual({error: errorMessage});
});
});
46 changes: 46 additions & 0 deletions app/products/agents/actions/remote/generation_controls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import NetworkManager from '@managers/network_manager';
import {getFullErrorMessage} from '@utils/errors';
import {logError} from '@utils/log';

/**
* Stop the current generation for a post
* @param serverUrl The server URL
* @param postId The post ID to stop generation for
* @returns {error} on failure
*/
export async function stopGeneration(
serverUrl: string,
postId: string,
): Promise<{error?: unknown}> {
try {
const client = NetworkManager.getClient(serverUrl);
await client.stopGeneration(postId);
return {};
} catch (error) {
logError('[stopGeneration]', error);
return {error: getFullErrorMessage(error)};
}
}

/**
* Regenerate a response for a post
* @param serverUrl The server URL
* @param postId The post ID to regenerate response for
* @returns {error} on failure
*/
export async function regenerateResponse(
serverUrl: string,
postId: string,
): Promise<{error?: unknown}> {
try {
const client = NetworkManager.getClient(serverUrl);
await client.regenerateResponse(postId);
return {};
} catch (error) {
logError('[regenerateResponse]', error);
return {error: getFullErrorMessage(error)};
}
}
54 changes: 54 additions & 0 deletions app/products/agents/actions/remote/tool_approval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import NetworkManager from '@managers/network_manager';
import {getFullErrorMessage} from '@utils/errors';
import {logError} from '@utils/log';

import {submitToolApproval} from './tool_approval';

jest.mock('@managers/network_manager');
jest.mock('@utils/errors');
jest.mock('@utils/log');

const serverUrl = 'https://test.mattermost.com';
const postId = 'post123';
const acceptedToolIds = ['tool1', 'tool2', 'tool3'];

const mockClient = {
submitToolApproval: jest.fn(),
};

beforeAll(() => {
jest.mocked(NetworkManager.getClient).mockReturnValue(mockClient as any);
});

beforeEach(() => {
jest.clearAllMocks();
});

describe('submitToolApproval', () => {
it('should call client.submitToolApproval and return empty object on success', async () => {
mockClient.submitToolApproval.mockResolvedValue(undefined);

const result = await submitToolApproval(serverUrl, postId, acceptedToolIds);

expect(NetworkManager.getClient).toHaveBeenCalledWith(serverUrl);
expect(mockClient.submitToolApproval).toHaveBeenCalledWith(postId, acceptedToolIds);
expect(result).toEqual({});
expect(result.error).toBeUndefined();
});

it('should return error object and log error on failure', async () => {
const error = new Error('Network error');
const errorMessage = 'Network error occurred';
mockClient.submitToolApproval.mockRejectedValue(error);
jest.mocked(getFullErrorMessage).mockReturnValue(errorMessage);

const result = await submitToolApproval(serverUrl, postId, acceptedToolIds);

expect(logError).toHaveBeenCalledWith('[submitToolApproval]', error);
expect(getFullErrorMessage).toHaveBeenCalledWith(error);
expect(result).toEqual({error: errorMessage});
});
});
Loading