Skip to content

Commit f885d73

Browse files
committed
Fix tool execution and trace handling in provider
1 parent 7d94f16 commit f885d73

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

apps/sim/providers/avian/index.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { createLogger } from '@sim/logger'
2+
import { getErrorMessage, toError } from '@sim/utils/errors'
23
import OpenAI from 'openai'
34
import type { StreamingExecution } from '@/executor/types'
45
import { MAX_TOOL_ITERATIONS } from '@/providers'
56
import { createReadableStreamFromAvianStream } from '@/providers/avian/utils'
67
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
8+
import { enrichLastModelSegmentFromChatCompletions } from '@/providers/trace-enrichment'
79
import type {
810
ProviderConfig,
911
ProviderRequest,
@@ -244,6 +246,14 @@ export const avianProvider: ProviderConfig = {
244246
}
245247

246248
const toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls
249+
250+
enrichLastModelSegmentFromChatCompletions(
251+
timeSegments,
252+
currentResponse,
253+
toolCallsInResponse,
254+
{ model: request.model, provider: 'avian' }
255+
)
256+
247257
if (!toolCallsInResponse || toolCallsInResponse.length === 0) {
248258
break
249259
}
@@ -261,7 +271,9 @@ export const avianProvider: ProviderConfig = {
261271
if (!tool) return null
262272

263273
const { toolParams, executionParams } = prepareToolExecution(tool, toolArgs, request)
264-
const result = await executeTool(toolName, executionParams)
274+
const result = await executeTool(toolName, executionParams, {
275+
signal: request.abortSignal,
276+
})
265277
const toolCallEndTime = Date.now()
266278

267279
return {
@@ -284,7 +296,7 @@ export const avianProvider: ProviderConfig = {
284296
result: {
285297
success: false,
286298
output: undefined,
287-
error: error instanceof Error ? error.message : 'Tool execution failed',
299+
error: getErrorMessage(error, 'Tool execution failed'),
288300
},
289301
startTime: toolCallStartTime,
290302
endTime: toolCallEndTime,
@@ -428,6 +440,15 @@ export const avianProvider: ProviderConfig = {
428440

429441
iterationCount++
430442
}
443+
444+
if (iterationCount === MAX_TOOL_ITERATIONS) {
445+
enrichLastModelSegmentFromChatCompletions(
446+
timeSegments,
447+
currentResponse,
448+
currentResponse.choices[0]?.message?.tool_calls,
449+
{ model: request.model, provider: 'avian' }
450+
)
451+
}
431452
} catch (error) {
432453
logger.error('Error in Avian request:', { error })
433454
}
@@ -549,7 +570,7 @@ export const avianProvider: ProviderConfig = {
549570
duration: totalDuration,
550571
})
551572

552-
throw new ProviderError(error instanceof Error ? error.message : String(error), {
573+
throw new ProviderError(toError(error).message, {
553574
startTime: providerStartTimeISO,
554575
endTime: providerEndTimeISO,
555576
duration: totalDuration,

0 commit comments

Comments
 (0)