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
17 changes: 5 additions & 12 deletions apps/sim/providers/anthropic/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
supportsNativeStructuredOutputs,
} from '@/providers/models'
import type { ProviderRequest, ProviderResponse, TimeSegment } from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -842,15 +843,11 @@ export async function executeAnthropicProviderRequest(
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
}

Expand Down Expand Up @@ -1299,14 +1296,10 @@ export async function executeAnthropicProviderRequest(
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
}
11 changes: 4 additions & 7 deletions apps/sim/providers/azure-openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -251,7 +252,7 @@ async function executeChatCompletionsRequest(
output: currentResponse.usage?.completion_tokens || 0,
total: currentResponse.usage?.total_tokens || 0,
}
const toolCalls: (FunctionCallResponse & { success: boolean })[] = []
const toolCalls: FunctionCallResponse[] = []
const toolResults: Record<string, unknown>[] = []
const currentMessages = [...allMessages]
let iterationCount = 0
Expand Down Expand Up @@ -577,15 +578,11 @@ async function executeChatCompletionsRequest(
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore - Adding timing property to the error
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
}

Expand Down
16 changes: 7 additions & 9 deletions apps/sim/providers/bedrock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import {
} from '@/providers/bedrock/utils'
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
import type {
FunctionCallResponse,
ProviderConfig,
ProviderRequest,
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -419,8 +421,8 @@ export const bedrockProvider: ProviderConfig = {
pricing: initialCost.pricing,
}

const toolCalls: any[] = []
const toolResults: any[] = []
const toolCalls: FunctionCallResponse[] = []
const toolResults: Record<string, unknown>[] = []
const currentMessages = [...messages]
let iterationCount = 0
let hasUsedForcedTool = false
Expand Down Expand Up @@ -561,7 +563,7 @@ export const bedrockProvider: ProviderConfig = {

let resultContent: any
if (result.success) {
toolResults.push(result.output)
toolResults.push(result.output!)
resultContent = result.output
} else {
resultContent = {
Expand Down Expand Up @@ -903,15 +905,11 @@ export const bedrockProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/cerebras/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -539,15 +540,11 @@ export const cerebrasProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore - Adding timing property to error for debugging
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/deepseek/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -538,15 +539,11 @@ export const deepseekProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/groq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -496,15 +497,11 @@ export const groqProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/mistral/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -551,15 +552,11 @@ export const mistralProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore - Adding timing property to error for debugging
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/ollama/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import { calculateCost, prepareToolExecution } from '@/providers/utils'
import { useProvidersStore } from '@/stores/providers'
import { executeTool } from '@/tools'
Expand Down Expand Up @@ -554,15 +555,11 @@ export const ollamaProvider: ProviderConfig = {
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
},
}
9 changes: 3 additions & 6 deletions apps/sim/providers/openai/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type OpenAI from 'openai'
import type { StreamingExecution } from '@/executor/types'
import { MAX_TOOL_ITERATIONS } from '@/providers'
import type { Message, ProviderRequest, ProviderResponse, TimeSegment } from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
prepareToolExecution,
Expand Down Expand Up @@ -806,14 +807,10 @@ export async function executeResponsesProviderRequest(
duration: totalDuration,
})

const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore - Adding timing property to the error
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}

throw enhancedError
})
}
}
18 changes: 9 additions & 9 deletions apps/sim/providers/openrouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
supportsNativeStructuredOutputs,
} from '@/providers/openrouter/utils'
import type {
FunctionCallResponse,
Message,
ProviderConfig,
ProviderRequest,
ProviderResponse,
TimeSegment,
} from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
generateSchemaInstructions,
Expand Down Expand Up @@ -90,7 +93,7 @@ export const openRouterProvider: ProviderConfig = {
stream: !!request.stream,
})

const allMessages = [] as any[]
const allMessages: Message[] = []

if (request.systemPrompt) {
allMessages.push({ role: 'system', content: request.systemPrompt })
Expand Down Expand Up @@ -237,8 +240,8 @@ export const openRouterProvider: ProviderConfig = {
output: currentResponse.usage?.completion_tokens || 0,
total: currentResponse.usage?.total_tokens || 0,
}
const toolCalls = [] as any[]
const toolResults = [] as any[]
const toolCalls: FunctionCallResponse[] = []
const toolResults: Record<string, unknown>[] = []
const currentMessages = [...allMessages]
let iterationCount = 0
let modelTime = firstResponseTime
Expand Down Expand Up @@ -352,7 +355,7 @@ export const openRouterProvider: ProviderConfig = {

let resultContent: any
if (result.success) {
toolResults.push(result.output)
toolResults.push(result.output!)
resultContent = result.output
} else {
resultContent = {
Expand Down Expand Up @@ -593,14 +596,11 @@ export const openRouterProvider: ProviderConfig = {
}

logger.error('Error in OpenRouter request:', errorDetails)
const enhancedError = new Error(error instanceof Error ? error.message : String(error))
// @ts-ignore
enhancedError.timing = {
throw new ProviderError(error instanceof Error ? error.message : String(error), {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
}
throw enhancedError
})
}
},
}
18 changes: 18 additions & 0 deletions apps/sim/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface FunctionCallResponse {
result?: Record<string, any>
output?: Record<string, any>
input?: Record<string, any>
success?: boolean
}

export interface TimeSegment {
Expand Down Expand Up @@ -177,4 +178,21 @@ export interface ProviderRequest {
previousInteractionId?: string
}

/**
* Typed error class for provider failures that includes timing information.
*/
export class ProviderError extends Error {
timing: {
startTime: string
endTime: string
duration: number
}

constructor(message: string, timing: { startTime: string; endTime: string; duration: number }) {
super(message)
this.name = 'ProviderError'
this.timing = timing
}
}

export const providers: Record<string, ProviderConfig> = {}
Loading