Skip to content

Commit 8d0ef5e

Browse files
authored
Merge pull request #6726 from remix-project-org/fix_tool_cb
more display time
2 parents 152ba97 + 3891861 commit 8d0ef5e

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

libs/remix-ai-core/src/helpers/streamHandler.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
147147
const toolCallsArray = Array.from(toolCalls.values());
148148
const response = await tool_callback(toolCallsArray, uiToolCallback)
149149

150+
// Preserve the uiToolCallback from the response if it exists (from subsequent calls)
150151
if (response && typeof response === 'object') {
151-
response.uiToolCallback = uiToolCallback;
152+
if (!response.uiToolCallback && uiToolCallback) {
153+
response.uiToolCallback = uiToolCallback;
154+
}
152155
}
153156
cb("\n\n");
154157
HandleOpenAIResponse(response, cb, done_cb)
@@ -244,8 +247,11 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
244247
const toolCalls = json.choices[0].delta.tool_calls;
245248
const response = await tool_callback(toolCalls, uiToolCallback)
246249

250+
// Preserve the uiToolCallback from the response if it exists (from subsequent calls)
247251
if (response && typeof response === 'object') {
248-
response.uiToolCallback = uiToolCallback;
252+
if (!response.uiToolCallback && uiToolCallback) {
253+
response.uiToolCallback = uiToolCallback;
254+
}
249255
}
250256
HandleMistralAIResponse(response, cb, done_cb)
251257
} else if (json.choices[0].delta.content){
@@ -346,12 +352,11 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
346352
}));
347353

348354
if (toolCalls.length > 0) {
349-
uiToolCallback?.(true);
350-
const response = await tool_callback(toolCalls)
351-
uiToolCallback?.(false);
352-
// Keep the callback attached for recursive calls
355+
const response = await tool_callback(toolCalls, uiToolCallback)
353356
if (response && typeof response === 'object') {
354-
response.uiToolCallback = uiToolCallback;
357+
if (!response.uiToolCallback && uiToolCallback) {
358+
response.uiToolCallback = uiToolCallback;
359+
}
355360
}
356361
cb("\n\n");
357362
HandleAnthropicResponse(response, cb, done_cb)
@@ -417,8 +422,11 @@ export const HandleOllamaResponse = async (aiResponse: IAIStreamResponse | any,
417422
const toolCalls = parsed.message.tool_calls;
418423
const response = await tool_callback(toolCalls, uiToolCallback)
419424
// Keep the callback attached for recursive calls
425+
// Preserve the uiToolCallback from the response if it exists (from subsequent calls)
420426
if (response && typeof response === 'object') {
421-
response.uiToolCallback = uiToolCallback;
427+
if (!response.uiToolCallback && uiToolCallback) {
428+
response.uiToolCallback = uiToolCallback;
429+
}
422430
}
423431
cb("\n\n");
424432
HandleOllamaResponse(response, cb, done_cb, reasoning_cb)

libs/remix-ai-core/src/inferencers/mcp/mcpInferencer.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,23 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
506506

507507
const followUpOptions = {
508508
...enhancedOptions,
509-
toolsMessages: toolsMessagesArray
509+
toolsMessages: toolsMessagesArray,
510+
chatHistory: options.provider === 'anthropic'
511+
? [...(enhancedOptions.chatHistory || []), { role: 'user', content: prompt }]
512+
: enhancedOptions.chatHistory
510513
};
511514

512-
// Send empty prompt - the tool results are in toolsMessages
513-
// Don't add extra prompts as they cause Anthropic to summarize instead of using full tool results
514515
if (options.provider === 'openai' || options.provider === 'mistralai') {
515516
return {
516517
streamResponse: await this.baseInferencer.answer(prompt, followUpOptions),
517-
callback: toolExecutionStatusCallback
518+
callback: toolExecutionStatusCallback,
519+
uiToolCallback: uiCallback
518520
} as IAIStreamResponse;
519521
} else {
520522
return {
521523
streamResponse: await this.baseInferencer.answer("", followUpOptions),
522-
callback: toolExecutionStatusCallback
524+
callback: toolExecutionStatusCallback,
525+
uiToolCallback: uiCallback
523526
} as IAIStreamResponse;
524527
}
525528
}
@@ -832,7 +835,6 @@ ${toolsList}`,
832835
}
833836

834837
if (uiCallback){
835-
console.log('on UI tool callback', innerToolCall.name, innerToolCall.arguments)
836838
uiCallback(true, innerToolCall.name, innerToolCall.arguments);
837839
}
838840
const result = await this.executeTool(targetServer, innerToolCall);

libs/remix-ai-core/src/inferencers/remote/remoteInference.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ export class RemoteInferencer implements ICompletions, IGeneration {
188188
}
189189

190190
async answer(prompt, options:IParams=GenerationParams): Promise<any> {
191-
options.chatHistory = buildChatPrompt()
191+
if (!options.toolsMessages) {
192+
options.chatHistory = buildChatPrompt()
193+
}
192194
const payload = { 'prompt': prompt, "endpoint":"answer", ...options }
193195
if (options.stream_result) return this._streamInferenceRequest(payload, AIRequestType.GENERAL)
194196
else return this._makeRequest(payload, AIRequestType.GENERAL)

libs/remix-ui/remix-ai-assistant/src/components/remix-ui-remix-ai-assistant.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ export const RemixUiRemixAiAssistant = React.forwardRef<
466466
let toolExecutionStartTime: number | null = null
467467

468468
const uiToolCallback = (isExecuting: boolean, toolName?: string, toolArgs?: Record<string, any>) => {
469-
const MIN_DISPLAY_TIME = 2000 // 2 seconds
469+
const MIN_DISPLAY_TIME = 30000 // 30 seconds
470470

471471
// Clear any pending timeout
472472
if (clearToolTimeout) {
@@ -475,16 +475,17 @@ export const RemixUiRemixAiAssistant = React.forwardRef<
475475
}
476476

477477
if (isExecuting) {
478-
// Tool execution starting or updating - show immediately
479478
if (!toolExecutionStartTime) {
480479
toolExecutionStartTime = Date.now()
481480
}
481+
482482
setMessages(prev =>
483483
prev.map(m => (m.id === assistantId ? {
484484
...m,
485-
isExecutingTools: isExecuting,
486-
executingToolName: toolName,
487-
executingToolArgs: toolArgs
485+
// Only show tool execution indicator if no content has arrived yet
486+
isExecutingTools: m.content.length === 0 ? isExecuting : m.isExecutingTools,
487+
executingToolName: m.content.length === 0 ? toolName : m.executingToolName,
488+
executingToolArgs: m.content.length === 0 ? toolArgs : m.executingToolArgs
488489
} : m))
489490
)
490491
} else {
@@ -601,7 +602,8 @@ export const RemixUiRemixAiAssistant = React.forwardRef<
601602
}
602603
)
603604
}
604-
setIsStreaming(false)
605+
// Note: setIsStreaming(false) is called in each handler's completion callback
606+
// DO NOT call it here as it would stop the spinner before the response completes
605607
}
606608
catch (error) {
607609
console.error('Error sending prompt:', error)

0 commit comments

Comments
 (0)