* Add agents RHS functionality for mobile Implements a dedicated agents interface accessible from the home screen, featuring: - Agent chat screen with bot selector and message input - Thread list screen showing all agent conversations - Remote actions for fetching bots and threads from plugin API - Navigation between chat, threads, and individual conversations - Integration with plus menu for easy access Follows similar pattern to playbooks with modal screens and navigation helpers. * Fix icon name for agents menu item Change robot-happy-outline to robot-happy to resolve PropType validation error. * Add agents button to sidebar and fix agent chat issues - Add AgentsButton component to channel list sidebar below Drafts - Fix thread navigation to use fetchAndSwitchToThread instead of switchToChannelById - Replace custom TextInput with PostDraft component in agent_chat - Fix icon name from message-reply-text-outline to reply-outline - Add missing i18n translations for agents UI * Implement automatic navigation to thread after sending agent message * Implement bot selector bottom sheet with avatars. Replace click-and-cycle behavior with a proper bottom sheet menu showing all available agents with their profile pictures. Add bot avatar display to the dropdown button for better visual identification. * Top bar design modifications. * Add intro graphic and text * Fix autocomplete not working * Tweak styles and icons. * Remove unused * Add version/enabled check * Remove excessive agent button * Style fixes * Use non-blocking then() for onPostCreated callback in send message hook * Add unit tests for agents product components and actions * Review feedback, offline support * I18n * Tests * Address PR review feedback: deletion handling, UI fixes, schema docs * Remove unnecessary deleteNotPresent flag from agents handlers * Fix agents archived channel, empty data guard, and stale relative time * Add smoke test for AgentChat and extend ToolCard test coverage * Fix test quality issues: remove useless tests, strengthen assertions * Address PR review: remove barrel file, add version comment * Mock reanimated in CitationsList test to fix CI failure * Fix CitationsList tests after always-mounted animation change * Address PR review: typography, Pressable, FormattedText, schema bump, and cleanup * Apply CLAUDE.md patterns across agents codebase: Pressable, typography, FormattedText, logDebug * Fix schema test to expect version 19
170 lines
5.5 KiB
TypeScript
170 lines
5.5 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import type {AIBotsResponse, AIThread, ToolCall} from '@agents/types';
|
|
import type {Agent, AgentsResponse, AgentsStatusResponse, ChannelAnalysisOptions, ChannelAnalysisResponse, RewriteRequest, RewriteResponse} from '@agents/types/api';
|
|
|
|
export type {Agent};
|
|
|
|
export interface ClientAgentsMix {
|
|
getAgentsRoute: () => string;
|
|
getAIBots: () => Promise<AIBotsResponse>;
|
|
getAIThreads: () => Promise<AIThread[]>;
|
|
getAgents: () => Promise<Agent[]>;
|
|
stopGeneration: (postId: string) => Promise<void>;
|
|
regenerateResponse: (postId: string) => Promise<void>;
|
|
doChannelAnalysis: (
|
|
channelId: string,
|
|
analysisType: string,
|
|
botUsername: string,
|
|
options?: ChannelAnalysisOptions,
|
|
) => Promise<ChannelAnalysisResponse>;
|
|
submitToolApproval: (postId: string, acceptedToolIds: string[]) => Promise<void>;
|
|
getToolCallPrivate: (postId: string) => Promise<ToolCall[]>;
|
|
getToolResultPrivate: (postId: string) => Promise<ToolCall[]>;
|
|
submitToolResult: (postId: string, acceptedToolIds: string[]) => Promise<void>;
|
|
|
|
// Rewrite methods
|
|
getRewrittenMessage: (message: string, action?: string, customPrompt?: string, agentId?: string) => Promise<string>;
|
|
getAgentsStatus: () => Promise<AgentsStatusResponse>;
|
|
}
|
|
|
|
const ClientAgents = (superclass: any) => class extends superclass {
|
|
getAgentsRoute = () => {
|
|
return '/plugins/mattermost-ai';
|
|
};
|
|
|
|
getAIBots = async () => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/ai_bots`,
|
|
{method: 'get'},
|
|
);
|
|
};
|
|
|
|
getAIThreads = async () => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/ai_threads`,
|
|
{method: 'get'},
|
|
);
|
|
};
|
|
|
|
getAgents = async (): Promise<Agent[]> => {
|
|
const response = await this.doFetch(
|
|
`${this.urlVersion}/agents`,
|
|
{method: 'get'},
|
|
);
|
|
|
|
// Handle both array response and wrapped response
|
|
if (Array.isArray(response)) {
|
|
return response;
|
|
}
|
|
return (response as AgentsResponse).agents || [];
|
|
};
|
|
|
|
stopGeneration = async (postId: string) => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/stop`,
|
|
{method: 'post'},
|
|
);
|
|
};
|
|
|
|
regenerateResponse = async (postId: string) => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/regenerate`,
|
|
{method: 'post'},
|
|
);
|
|
};
|
|
|
|
doChannelAnalysis = async (
|
|
channelId: string,
|
|
analysisType: string,
|
|
botUsername: string,
|
|
options?: ChannelAnalysisOptions,
|
|
): Promise<ChannelAnalysisResponse> => {
|
|
const {since, until, days, prompt, unreads_only} = options || {};
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/channel/${channelId}/analyze?botUsername=${encodeURIComponent(botUsername)}`,
|
|
{
|
|
method: 'post',
|
|
body: {
|
|
analysis_type: analysisType,
|
|
since,
|
|
until,
|
|
days,
|
|
prompt,
|
|
unreads_only,
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
submitToolApproval = async (postId: string, acceptedToolIds: string[]) => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/tool_call`,
|
|
{
|
|
method: 'post',
|
|
body: {accepted_tool_ids: acceptedToolIds},
|
|
},
|
|
);
|
|
};
|
|
|
|
getToolCallPrivate = async (postId: string): Promise<ToolCall[]> => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/tool_call_private`,
|
|
{method: 'get'},
|
|
);
|
|
};
|
|
|
|
getToolResultPrivate = async (postId: string): Promise<ToolCall[]> => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/tool_result_private`,
|
|
{method: 'get'},
|
|
);
|
|
};
|
|
|
|
submitToolResult = async (postId: string, acceptedToolIds: string[]) => {
|
|
return this.doFetch(
|
|
`${this.getAgentsRoute()}/post/${postId}/tool_result`,
|
|
{
|
|
method: 'post',
|
|
body: {accepted_tool_ids: acceptedToolIds},
|
|
},
|
|
);
|
|
};
|
|
|
|
// =========================================================================
|
|
// Rewrite Methods
|
|
// =========================================================================
|
|
|
|
getRewrittenMessage = async (message: string, action?: string, customPrompt?: string, agentId?: string): Promise<string> => {
|
|
const body: RewriteRequest = {
|
|
agent_id: agentId,
|
|
message,
|
|
action,
|
|
custom_prompt: customPrompt,
|
|
};
|
|
|
|
const response = await this.doFetch(
|
|
`${this.urlVersion}/posts/rewrite`,
|
|
{method: 'post', body},
|
|
true,
|
|
) as RewriteResponse;
|
|
|
|
// Handle cases where the AI returns plain text instead of the expected JSON format.
|
|
// If rewritten_text is undefined, treat the entire response as the rewritten message.
|
|
if (response.rewritten_text === undefined) {
|
|
return response as unknown as string;
|
|
}
|
|
|
|
return response.rewritten_text;
|
|
};
|
|
|
|
getAgentsStatus = async (): Promise<AgentsStatusResponse> => {
|
|
return this.doFetch(
|
|
`${this.urlVersion}/agents/status`,
|
|
{method: 'get'},
|
|
);
|
|
};
|
|
};
|
|
|
|
export default ClientAgents;
|