Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 9 additions & 5 deletions apps/sim/lib/copilot/tools/client-tools/run-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ export class RunWorkflowTool extends BaseTool {
// Execution failed
const errorMessage = (result as any)?.error || 'Workflow execution failed'

// Notify server of error
await this.notify(toolCall.id, 'errored', `Workflow execution failed: ${errorMessage}`)
// Check if error message is exactly 'skipped' to notify 'rejected' instead of 'errored'
const targetState = errorMessage === 'skipped' ? 'rejected' : 'errored'
const message = targetState === 'rejected' ? `Workflow execution skipped: ${errorMessage}` : `Workflow execution failed: ${errorMessage}`
await this.notify(toolCall.id, targetState, message)

options?.onStateChange?.('errored')
options?.onStateChange?.(targetState)

return {
success: false,
Expand All @@ -184,9 +186,11 @@ export class RunWorkflowTool extends BaseTool {

const errorMessage = error?.message || 'An unknown error occurred'

await this.notify(toolCall.id, 'errored', `Workflow execution failed: ${errorMessage}`)
// Check if error message is exactly 'skipped' to notify 'rejected' instead of 'errored'
const targetState = errorMessage === 'skipped' ? 'rejected' : 'errored'
await this.notify(toolCall.id, targetState, `Workflow execution failed: ${errorMessage}`)

options?.onStateChange?.('errored')
options?.onStateChange?.(targetState)

return {
success: false,
Expand Down
7 changes: 5 additions & 2 deletions apps/sim/lib/copilot/tools/inline-tool-call.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ async function clientAcceptTool(
}
} catch (error) {
console.error('Error executing client tool:', error)
setToolCallState(toolCall, 'errored', {
error: error instanceof Error ? error.message : 'Tool execution failed',
const errorMessage = error instanceof Error ? error.message : 'Tool execution failed'
// Check if error message is exactly 'skipped' to set 'rejected' state instead of 'errored'
const targetState = errorMessage === 'skipped' ? 'rejected' : 'errored'
setToolCallState(toolCall, targetState, {
error: errorMessage,
})
}
}
Expand Down
52 changes: 48 additions & 4 deletions apps/sim/stores/copilot/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ function handleToolFailure(toolCall: any, error: string): void {
return
}

toolCall.state = 'errored'
// Check if error message is exactly 'skipped' to set 'rejected' state instead of 'errored'
toolCall.state = error === 'skipped' ? 'rejected' : 'errored'
toolCall.error = error

// Update displayName to match the error state
Expand Down Expand Up @@ -488,7 +489,12 @@ function setToolCallState(
break

case 'rejected':
// User rejected the tool
// User rejected the tool or tool was skipped
toolCall.endTime = Date.now()
toolCall.duration = toolCall.endTime - toolCall.startTime
if (error) {
toolCall.error = error
}
break

case 'background':
Expand Down Expand Up @@ -687,7 +693,16 @@ const sseHandlers: Record<string, SSEHandler> = {

// Handle tool result events - simplified
tool_result: (data, context, get, set) => {
const { toolCallId, result, success } = data
const { toolCallId, result, success, error } = data

// Console log the tool result for debugging
console.log('🔧 Tool Result:', {
toolCallId,
success,
result: typeof result === 'string' ? result : JSON.stringify(result, null, 2),
error,
timestamp: new Date().toISOString()
})

if (!toolCallId) return

Expand All @@ -704,6 +719,14 @@ const sseHandlers: Record<string, SSEHandler> = {
return
}

// Console log the tool call details
console.log('🔧 Tool Call Found:', {
id: toolCall.id,
name: toolCall.name,
currentState: toolCall.state,
input: toolCall.input
})

// Ensure tool call is in context for updates
if (!context.toolCalls.find((tc) => tc.id === toolCallId)) {
context.toolCalls.push(toolCall)
Expand All @@ -722,6 +745,12 @@ const sseHandlers: Record<string, SSEHandler> = {
})()
: result

console.log('✅ Tool Success - Setting state to success:', {
toolCallId,
toolName: toolCall.name,
parsedResult: typeof parsedResult === 'string' ? parsedResult : JSON.stringify(parsedResult, null, 2)
})

// NEW LOGIC: Use centralized state management
setToolCallState(toolCall, 'success', { result: parsedResult })

Expand All @@ -734,7 +763,22 @@ const sseHandlers: Record<string, SSEHandler> = {
// finalizeToolCall(toolCall, true, parsedResult, get)
} else {
// NEW LOGIC: Use centralized state management
setToolCallState(toolCall, 'errored', { error: result || 'Tool execution failed' })
// Check if error message is exactly 'skipped' to set 'rejected' state instead of 'errored'
// Use the error field first, then fall back to result field, then default message
const errorMessage = error || result || 'Tool execution failed'
const targetState = errorMessage === 'skipped' ? 'rejected' : 'errored'

console.log(`❌ Tool ${targetState === 'rejected' ? 'Skipped' : 'Failed'} - Setting state to ${targetState}:`, {
toolCallId,
toolName: toolCall.name,
errorMessage,
targetState,
wasSkipped: errorMessage === 'skipped',
errorField: error,
resultField: result
})

setToolCallState(toolCall, targetState, { error: errorMessage })

// COMMENTED OUT OLD LOGIC:
// handleToolFailure(toolCall, result || 'Tool execution failed')
Expand Down
Loading