Skip to content

Commit 58758c6

Browse files
committed
Fix: Optimize message sending logic
1 parent 4292cc2 commit 58758c6

File tree

6 files changed

+33
-357
lines changed

6 files changed

+33
-357
lines changed

src/components/chat/ChatMessageArea.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ChatMessageAreaProps {
1919
activeConversation: Conversation | null;
2020
isLoading: boolean;
2121
error: string | null;
22-
onSendMessage: (content: string) => void;
22+
onSendMessage: (content: string, files?: File[]) => void;
2323
onSendMessageWithFiles?: (content: string, files: File[]) => void;
2424
onStopStreaming?: () => void;
2525
onRegenerateResponse?: (messageId: string) => void;
@@ -126,10 +126,12 @@ export const ChatMessageArea: React.FC<ChatMessageAreaProps> = ({
126126
e.preventDefault();
127127
if (isLoading || isCurrentlyStreaming || !inputValue.trim()) return;
128128

129-
if (selectedFiles.length > 0 && onSendMessageWithFiles) {
130-
onSendMessageWithFiles(inputValue, selectedFiles);
129+
if (selectedFiles.length > 0) {
130+
// Use the combined method with files parameter
131+
onSendMessage(inputValue, selectedFiles);
131132
setSelectedFiles([]);
132133
} else {
134+
// Use the combined method without files
133135
onSendMessage(inputValue);
134136
}
135137

src/components/pages/ChatPage.tsx

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,22 @@ export const ChatPage = () => {
224224
};
225225

226226
// Handle sending a message with streaming
227-
const handleSendMessage = async (content: string) => {
227+
const handleSendMessage = async (content: string, files?: File[]) => {
228228
if (!activeConversationId || !isServiceInitialized || !chatServiceRef.current) return;
229229

230230
try {
231231
const chatService = chatServiceRef.current;
232232

233-
// Check if there are selected MCP servers to use
234-
if (selectedMcpServers.length > 0) {
235-
// Send message with MCP tools
236-
await chatService.sendMessageWithMCPTools(
237-
content,
238-
activeConversationId,
239-
selectedMcpServers,
240-
true,
241-
(updatedConversation) => {
242-
setConversations(updatedConversation);
243-
}
244-
);
245-
} else {
246-
// Send regular message
247-
await chatService.sendMessage(
248-
content,
249-
activeConversationId,
250-
true,
251-
(updatedConversation) => {
252-
setConversations(updatedConversation);
253-
}
254-
);
255-
}
233+
// Send message with optional files
234+
await chatService.sendMessage(
235+
content,
236+
activeConversationId,
237+
true,
238+
(updatedConversation) => {
239+
setConversations(updatedConversation);
240+
},
241+
files
242+
);
256243
} catch (err) {
257244
console.error('Error sending streaming message:', err);
258245
}
@@ -375,27 +362,10 @@ export const ChatPage = () => {
375362
}
376363
};
377364

378-
// Handle sending a message with files
365+
// Handle sending a message with files (deprecated - now handled by handleSendMessage)
366+
// This method is kept for backward compatibility but delegates to handleSendMessage
379367
const handleSendMessageWithFiles = async (content: string, files: File[]) => {
380-
if (!activeConversationId || !isServiceInitialized || !chatServiceRef.current) return;
381-
382-
try {
383-
const chatService = chatServiceRef.current;
384-
385-
// Send user message with files
386-
await chatService.sendMessageWithFiles(
387-
content,
388-
files,
389-
activeConversationId,
390-
true,
391-
(updatedConversation) => {
392-
setConversations(updatedConversation);
393-
}
394-
);
395-
396-
} catch (err) {
397-
console.error('Error sending message with files:', err);
398-
}
368+
await handleSendMessage(content, files);
399369
};
400370

401371
// Toggle selection of an MCP server

src/services/ai-service.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { ProviderFactory } from './providers/provider-factory';
33
import { Message } from '../types/chat';
44
import { StreamControlHandler } from './streaming-control';
55
import { SETTINGS_CHANGE_EVENT, SettingsService } from './settings-service';
6-
import { MCPToolAdapter } from './mcp-tool-adapter';
76
import { MCPService } from './mcp-service';
8-
import { AIServiceCapability } from '../types/capabilities';
97

108
export interface ModelOption {
119
id: string;
@@ -188,9 +186,6 @@ export class AIService {
188186
const modelName = options.model;
189187
const useStreaming = options.stream;
190188

191-
// Check if there are any MCP tools to be used
192-
const mcpTools = options.mcpTools;
193-
194189
// Get provider instance
195190
const provider = this.getProvider(providerName);
196191

@@ -200,36 +195,6 @@ export class AIService {
200195
throw new Error(`Provider ${providerName} not available`);
201196
}
202197

203-
// Check if the selected model supports tool calls
204-
const modelCapabilities = provider.getModelCapabilities(modelName);
205-
const supportsTools = modelCapabilities.includes(AIServiceCapability.ToolUsage) ||
206-
modelCapabilities.includes(AIServiceCapability.FunctionCalling) ||
207-
modelCapabilities.includes(AIServiceCapability.MCPServer);
208-
209-
// If MCP tools are specified and model supports tools, get them and add to options
210-
if (mcpTools && mcpTools.length > 0 && supportsTools) {
211-
const mcpToolAdapter = MCPToolAdapter.getInstance();
212-
const allTools: Record<string, unknown> = {};
213-
214-
for (const mcpToolId of mcpTools) {
215-
try {
216-
const tools = await mcpToolAdapter.getToolsForServer(mcpToolId, streamController);
217-
Object.assign(allTools, tools);
218-
} catch (error) {
219-
console.error(`Error loading tools for MCP server ${mcpToolId}:`, error);
220-
// Continue with other tools if one fails
221-
}
222-
}
223-
224-
// Add tools to options if any were loaded
225-
if (Object.keys(allTools).length > 0) {
226-
console.log('Adding tools to request:', Object.keys(allTools));
227-
options.tools = allTools;
228-
}
229-
} else if (mcpTools && mcpTools.length > 0 && !supportsTools) {
230-
console.warn(`Model ${modelName} does not support tools, but tools were requested`);
231-
}
232-
233198
const result = await provider.getChatCompletion(
234199
messages,
235200
{

0 commit comments

Comments
 (0)