chore: import upstream snapshot with attribution
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
+827
View File
@@ -0,0 +1,827 @@
import type { Logger } from '@sim/logger'
import { getErrorMessage, toError } from '@sim/utils/errors'
import type OpenAI from 'openai'
import type { IterationToolCall, StreamingExecution } from '@/executor/types'
import { MAX_TOOL_ITERATIONS } from '@/providers'
import { createStreamingExecution } from '@/providers/streaming-execution'
import { adaptOpenAIChatToolSchema } from '@/providers/tool-schema-adapter'
import { enrichLastModelSegment, parseToolCallArguments } from '@/providers/trace-enrichment'
import type { Message, ProviderRequest, ProviderResponse, TimeSegment } from '@/providers/types'
import { ProviderError } from '@/providers/types'
import {
calculateCost,
enforceStrictSchema,
prepareToolExecution,
prepareToolsWithUsageControl,
sumToolCosts,
trackForcedToolUsage,
} from '@/providers/utils'
import { executeTool } from '@/tools'
import {
buildResponsesInputFromMessages,
convertResponseOutputToInputItems,
convertToolsToResponses,
createReadableStreamFromResponses,
extractResponseReasoning,
extractResponseText,
extractResponseToolCalls,
parseResponsesUsage,
type ResponsesInputItem,
type ResponsesToolCall,
toResponsesToolChoice,
} from './utils'
type PreparedTools = ReturnType<typeof prepareToolsWithUsageControl>
type ToolChoice = PreparedTools['toolChoice']
export interface ResponsesProviderConfig {
providerId: string
providerLabel: string
modelName: string
endpoint: string
headers: Record<string, string>
logger: Logger
/**
* Optional fetch implementation. Used to pin the connection to a pre-validated
* IP (DNS-rebinding/SSRF protection) when the endpoint is user-supplied.
* Defaults to the global fetch.
*/
fetch?: typeof fetch
}
/**
* Executes a Responses API request with tool-loop handling and streaming support.
*/
export async function executeResponsesProviderRequest(
request: ProviderRequest,
config: ResponsesProviderConfig
): Promise<ProviderResponse | StreamingExecution> {
const { logger } = config
const fetchImpl = config.fetch ?? fetch
logger.info(`Preparing ${config.providerLabel} request`, {
model: request.model,
hasSystemPrompt: !!request.systemPrompt,
hasMessages: !!request.messages?.length,
hasTools: !!request.tools?.length,
toolCount: request.tools?.length || 0,
hasResponseFormat: !!request.responseFormat,
stream: !!request.stream,
})
const allMessages: Message[] = []
if (request.systemPrompt) {
allMessages.push({
role: 'system',
content: request.systemPrompt,
})
}
if (request.context) {
allMessages.push({
role: 'user',
content: request.context,
})
}
if (request.messages) {
allMessages.push(...request.messages)
}
const initialInput = buildResponsesInputFromMessages(allMessages, config.providerId)
const basePayload: Record<string, unknown> = {
model: config.modelName,
}
if (request.temperature !== undefined) basePayload.temperature = request.temperature
if (request.maxTokens != null) basePayload.max_output_tokens = request.maxTokens
if (request.reasoningEffort !== undefined && request.reasoningEffort !== 'auto') {
basePayload.reasoning = {
effort: request.reasoningEffort,
summary: 'auto',
}
}
if (request.verbosity !== undefined && request.verbosity !== 'auto') {
basePayload.text = {
...((basePayload.text as Record<string, unknown>) ?? {}),
verbosity: request.verbosity,
}
}
// Store response format config - for Azure with tools, we defer applying it until after tool calls complete
let deferredTextFormat: OpenAI.Responses.ResponseFormatTextJSONSchemaConfig | undefined
const hasTools = !!request.tools?.length
const isAzure = config.providerId === 'azure-openai'
if (request.responseFormat) {
const isStrict = request.responseFormat.strict !== false
const rawSchema = request.responseFormat.schema || request.responseFormat
// OpenAI strict mode requires additionalProperties: false on ALL nested objects
const cleanedSchema = isStrict ? enforceStrictSchema(rawSchema) : rawSchema
const textFormat = {
type: 'json_schema' as const,
name: request.responseFormat.name || 'response_schema',
schema: cleanedSchema,
strict: isStrict,
}
// Azure OpenAI has issues combining tools + response_format in the same request
// Defer the format until after tool calls complete for Azure
if (isAzure && hasTools) {
deferredTextFormat = textFormat
logger.info(
`Deferring JSON schema response format for ${config.providerLabel} (will apply after tool calls complete)`
)
} else {
basePayload.text = {
...((basePayload.text as Record<string, unknown>) ?? {}),
format: textFormat,
}
logger.info(`Added JSON schema response format to ${config.providerLabel} request`)
}
}
const tools = request.tools?.length
? request.tools.map((tool) => adaptOpenAIChatToolSchema(tool))
: undefined
let preparedTools: PreparedTools | null = null
let responsesToolChoice: ReturnType<typeof toResponsesToolChoice> | undefined
let trackingToolChoice: ToolChoice | undefined
if (tools?.length) {
preparedTools = prepareToolsWithUsageControl(tools, request.tools, logger, config.providerId)
const { tools: filteredTools, toolChoice } = preparedTools
trackingToolChoice = toolChoice
if (filteredTools?.length) {
const convertedTools = convertToolsToResponses(filteredTools)
if (!convertedTools.length) {
throw new Error('All tools have empty names')
}
basePayload.tools = convertedTools
basePayload.parallel_tool_calls = true
}
if (toolChoice) {
responsesToolChoice = toResponsesToolChoice(toolChoice)
if (responsesToolChoice) {
basePayload.tool_choice = responsesToolChoice
}
logger.info(`${config.providerLabel} request configuration:`, {
toolCount: filteredTools?.length || 0,
toolChoice:
typeof toolChoice === 'string'
? toolChoice
: toolChoice.type === 'function'
? `force:${toolChoice.function?.name}`
: toolChoice.type === 'tool'
? `force:${toolChoice.name}`
: toolChoice.type === 'any'
? `force:${toolChoice.any?.name || 'unknown'}`
: 'unknown',
model: config.modelName,
})
}
}
const createRequestBody = (
input: ResponsesInputItem[],
overrides: Record<string, unknown> = {}
) => ({
...basePayload,
input,
...overrides,
})
const parseErrorResponse = async (response: Response): Promise<string> => {
const text = await response.text()
try {
const payload = JSON.parse(text)
return payload?.error?.message || text
} catch {
return text
}
}
const postResponses = async (
body: Record<string, unknown>
): Promise<OpenAI.Responses.Response> => {
const response = await fetchImpl(config.endpoint, {
method: 'POST',
headers: config.headers,
body: JSON.stringify(body),
signal: request.abortSignal,
})
if (!response.ok) {
const message = await parseErrorResponse(response)
throw new Error(`${config.providerLabel} API error (${response.status}): ${message}`)
}
return response.json()
}
const providerStartTime = Date.now()
const providerStartTimeISO = new Date(providerStartTime).toISOString()
try {
if (request.stream && (!tools || tools.length === 0)) {
logger.info(`Using streaming response for ${config.providerLabel} request`)
const streamResponse = await fetchImpl(config.endpoint, {
method: 'POST',
headers: config.headers,
body: JSON.stringify(createRequestBody(initialInput, { stream: true })),
signal: request.abortSignal,
})
if (!streamResponse.ok) {
const message = await parseErrorResponse(streamResponse)
throw new Error(`${config.providerLabel} API error (${streamResponse.status}): ${message}`)
}
const streamingResult = createStreamingExecution({
model: request.model,
providerStartTime,
providerStartTimeISO,
timing: { kind: 'simple', segmentName: request.model },
initialTokens: { input: 0, output: 0, total: 0 },
initialCost: { input: 0, output: 0, total: 0 },
createStream: ({ output, finalizeTiming }) =>
createReadableStreamFromResponses(streamResponse, (content, usage) => {
output.content = content
output.tokens = {
input: usage?.promptTokens || 0,
output: usage?.completionTokens || 0,
total: usage?.totalTokens || 0,
}
const costResult = calculateCost(
request.model,
usage?.promptTokens || 0,
usage?.completionTokens || 0
)
output.cost = {
input: costResult.input,
output: costResult.output,
total: costResult.total,
}
finalizeTiming()
}),
})
return streamingResult
}
const initialCallTime = Date.now()
const forcedTools = preparedTools?.forcedTools || []
let usedForcedTools: string[] = []
let hasUsedForcedTool = false
let currentToolChoice = responsesToolChoice
let currentTrackingToolChoice = trackingToolChoice
const checkForForcedToolUsage = (
toolCallsInResponse: ResponsesToolCall[],
toolChoice: ToolChoice | undefined
) => {
if (typeof toolChoice === 'object' && toolCallsInResponse.length > 0) {
const result = trackForcedToolUsage(
toolCallsInResponse,
toolChoice,
logger,
config.providerId,
forcedTools,
usedForcedTools
)
hasUsedForcedTool = result.hasUsedForcedTool
usedForcedTools = result.usedForcedTools
}
}
const currentInput: ResponsesInputItem[] = [...initialInput]
let currentResponse = await postResponses(
createRequestBody(currentInput, { tool_choice: currentToolChoice })
)
const firstResponseTime = Date.now() - initialCallTime
const initialUsage = parseResponsesUsage(currentResponse.usage)
const tokens = {
input: initialUsage?.promptTokens || 0,
output: initialUsage?.completionTokens || 0,
total: initialUsage?.totalTokens || 0,
}
const toolCalls = []
const toolResults: Record<string, unknown>[] = []
let iterationCount = 0
let modelTime = firstResponseTime
let toolsTime = 0
let content = extractResponseText(currentResponse.output) || ''
const timeSegments: TimeSegment[] = [
{
type: 'model',
name: request.model,
startTime: initialCallTime,
endTime: initialCallTime + firstResponseTime,
duration: firstResponseTime,
},
]
checkForForcedToolUsage(
extractResponseToolCalls(currentResponse.output),
currentTrackingToolChoice
)
while (iterationCount < MAX_TOOL_ITERATIONS) {
const responseText = extractResponseText(currentResponse.output)
if (responseText) {
content = responseText
}
const toolCallsInResponse = extractResponseToolCalls(currentResponse.output)
enrichLastModelSegmentFromOpenAIResponse(
timeSegments,
currentResponse,
responseText,
toolCallsInResponse,
{ model: request.model }
)
if (!toolCallsInResponse.length) {
break
}
const outputInputItems = convertResponseOutputToInputItems(currentResponse.output)
if (outputInputItems.length) {
currentInput.push(...outputInputItems)
}
logger.info(
`Processing ${toolCallsInResponse.length} tool calls in parallel (iteration ${
iterationCount + 1
}/${MAX_TOOL_ITERATIONS})`
)
const toolsStartTime = Date.now()
const toolExecutionPromises = toolCallsInResponse.map(async (toolCall) => {
const toolCallStartTime = Date.now()
const toolName = toolCall.name
try {
const toolArgs = toolCall.arguments ? JSON.parse(toolCall.arguments) : {}
const tool = request.tools?.find((t) => t.id === toolName)
if (!tool) {
return null
}
const { toolParams, executionParams } = prepareToolExecution(tool, toolArgs, request)
const result = await executeTool(toolName, executionParams, {
signal: request.abortSignal,
})
const toolCallEndTime = Date.now()
return {
toolCall,
toolName,
toolParams,
result,
startTime: toolCallStartTime,
endTime: toolCallEndTime,
duration: toolCallEndTime - toolCallStartTime,
}
} catch (error) {
const toolCallEndTime = Date.now()
logger.error('Error processing tool call:', { error, toolName })
return {
toolCall,
toolName,
toolParams: {},
result: {
success: false,
output: undefined,
error: getErrorMessage(error, 'Tool execution failed'),
},
startTime: toolCallStartTime,
endTime: toolCallEndTime,
duration: toolCallEndTime - toolCallStartTime,
}
}
})
const executionResults = await Promise.allSettled(toolExecutionPromises)
for (const settledResult of executionResults) {
if (settledResult.status === 'rejected' || !settledResult.value) continue
const { toolCall, toolName, toolParams, result, startTime, endTime, duration } =
settledResult.value
timeSegments.push({
type: 'tool',
name: toolName,
startTime: startTime,
endTime: endTime,
duration: duration,
toolCallId: toolCall.id,
})
let resultContent: Record<string, unknown>
if (result.success && result.output) {
toolResults.push(result.output)
resultContent = result.output as Record<string, unknown>
} else {
resultContent = {
error: true,
message: result.error || 'Tool execution failed',
tool: toolName,
}
}
toolCalls.push({
name: toolName,
arguments: toolParams,
startTime: new Date(startTime).toISOString(),
endTime: new Date(endTime).toISOString(),
duration: duration,
result: resultContent,
success: result.success,
})
currentInput.push({
type: 'function_call_output',
call_id: toolCall.id,
output: JSON.stringify(resultContent),
})
}
const thisToolsTime = Date.now() - toolsStartTime
toolsTime += thisToolsTime
if (typeof currentToolChoice === 'object' && hasUsedForcedTool && forcedTools.length > 0) {
const remainingTools = forcedTools.filter((tool) => !usedForcedTools.includes(tool))
if (remainingTools.length > 0) {
currentToolChoice = {
type: 'function',
name: remainingTools[0],
}
currentTrackingToolChoice = {
type: 'function',
function: { name: remainingTools[0] },
}
logger.info(`Forcing next tool: ${remainingTools[0]}`)
} else {
currentToolChoice = 'auto'
currentTrackingToolChoice = 'auto'
logger.info('All forced tools have been used, switching to auto tool_choice')
}
}
const nextModelStartTime = Date.now()
currentResponse = await postResponses(
createRequestBody(currentInput, { tool_choice: currentToolChoice })
)
checkForForcedToolUsage(
extractResponseToolCalls(currentResponse.output),
currentTrackingToolChoice
)
const latestText = extractResponseText(currentResponse.output)
if (latestText) {
content = latestText
}
const nextModelEndTime = Date.now()
const thisModelTime = nextModelEndTime - nextModelStartTime
timeSegments.push({
type: 'model',
name: request.model,
startTime: nextModelStartTime,
endTime: nextModelEndTime,
duration: thisModelTime,
})
modelTime += thisModelTime
const usage = parseResponsesUsage(currentResponse.usage)
if (usage) {
tokens.input += usage.promptTokens
tokens.output += usage.completionTokens
tokens.total += usage.totalTokens
}
iterationCount++
}
if (iterationCount === MAX_TOOL_ITERATIONS) {
const trailingText = extractResponseText(currentResponse.output)
const trailingToolCalls = extractResponseToolCalls(currentResponse.output)
enrichLastModelSegmentFromOpenAIResponse(
timeSegments,
currentResponse,
trailingText,
trailingToolCalls,
{ model: request.model }
)
}
// For Azure with deferred format: make a final call with the response format applied
// This happens whenever we have a deferred format, even if no tools were called
// (the initial call was made without the format, so we need to apply it now)
let appliedDeferredFormat = false
if (deferredTextFormat) {
logger.info(
`Applying deferred JSON schema response format for ${config.providerLabel} (iterationCount: ${iterationCount})`
)
const finalFormatStartTime = Date.now()
// Determine what input to use for the formatted call
let formattedInput: ResponsesInputItem[]
if (iterationCount > 0) {
// Tools were called - include the conversation history with tool results
const lastOutputItems = convertResponseOutputToInputItems(currentResponse.output)
if (lastOutputItems.length) {
currentInput.push(...lastOutputItems)
}
formattedInput = currentInput
} else {
// No tools were called - just retry the initial call with format applied
// Don't include the model's previous unformatted response
formattedInput = initialInput
}
// Make final call with the response format - build payload without tools
const finalPayload: Record<string, unknown> = {
model: config.modelName,
input: formattedInput,
text: {
...((basePayload.text as Record<string, unknown>) ?? {}),
format: deferredTextFormat,
},
}
// Copy over non-tool related settings
if (request.temperature !== undefined) finalPayload.temperature = request.temperature
if (request.maxTokens != null) finalPayload.max_output_tokens = request.maxTokens
if (request.reasoningEffort !== undefined && request.reasoningEffort !== 'auto') {
finalPayload.reasoning = {
effort: request.reasoningEffort,
summary: 'auto',
}
}
if (request.verbosity !== undefined && request.verbosity !== 'auto') {
finalPayload.text = {
...((finalPayload.text as Record<string, unknown>) ?? {}),
verbosity: request.verbosity,
}
}
currentResponse = await postResponses(finalPayload)
const finalFormatEndTime = Date.now()
const finalFormatDuration = finalFormatEndTime - finalFormatStartTime
timeSegments.push({
type: 'model',
name: 'Final formatted response',
startTime: finalFormatStartTime,
endTime: finalFormatEndTime,
duration: finalFormatDuration,
})
modelTime += finalFormatDuration
const finalUsage = parseResponsesUsage(currentResponse.usage)
if (finalUsage) {
tokens.input += finalUsage.promptTokens
tokens.output += finalUsage.completionTokens
tokens.total += finalUsage.totalTokens
}
// Update content with the formatted response
const formattedText = extractResponseText(currentResponse.output)
if (formattedText) {
content = formattedText
}
enrichLastModelSegmentFromOpenAIResponse(
timeSegments,
currentResponse,
formattedText,
extractResponseToolCalls(currentResponse.output),
{ model: request.model }
)
appliedDeferredFormat = true
}
// Skip streaming if we already applied deferred format - we have the formatted content
// Making another streaming call would lose the formatted response
if (request.stream && !appliedDeferredFormat) {
logger.info('Using streaming for final response after tool processing')
const accumulatedCost = calculateCost(request.model, tokens.input, tokens.output)
// For Azure with deferred format in streaming mode, include the format in the streaming call
const streamOverrides: Record<string, unknown> = { stream: true, tool_choice: 'auto' }
if (deferredTextFormat) {
streamOverrides.text = {
...((basePayload.text as Record<string, unknown>) ?? {}),
format: deferredTextFormat,
}
}
const streamResponse = await fetchImpl(config.endpoint, {
method: 'POST',
headers: config.headers,
body: JSON.stringify(createRequestBody(currentInput, streamOverrides)),
signal: request.abortSignal,
})
if (!streamResponse.ok) {
const message = await parseErrorResponse(streamResponse)
throw new Error(`${config.providerLabel} API error (${streamResponse.status}): ${message}`)
}
const streamingResult = createStreamingExecution({
model: request.model,
providerStartTime,
providerStartTimeISO,
timing: {
kind: 'accumulated',
modelTime,
toolsTime,
firstResponseTime,
iterations: iterationCount + 1,
timeSegments,
},
initialTokens: { input: tokens.input, output: tokens.output, total: tokens.total },
initialCost: {
input: accumulatedCost.input,
output: accumulatedCost.output,
total: accumulatedCost.total,
},
toolCalls: toolCalls.length > 0 ? { list: toolCalls, count: toolCalls.length } : undefined,
createStream: ({ output }) =>
createReadableStreamFromResponses(streamResponse, (content, usage) => {
output.content = content
output.tokens = {
input: tokens.input + (usage?.promptTokens || 0),
output: tokens.output + (usage?.completionTokens || 0),
total: tokens.total + (usage?.totalTokens || 0),
}
const streamCost = calculateCost(
request.model,
usage?.promptTokens || 0,
usage?.completionTokens || 0
)
const tc = sumToolCosts(toolResults)
output.cost = {
input: accumulatedCost.input + streamCost.input,
output: accumulatedCost.output + streamCost.output,
toolCost: tc || undefined,
total: accumulatedCost.total + streamCost.total + tc,
}
}),
})
return streamingResult
}
const providerEndTime = Date.now()
const providerEndTimeISO = new Date(providerEndTime).toISOString()
const totalDuration = providerEndTime - providerStartTime
return {
content,
model: request.model,
tokens,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
toolResults: toolResults.length > 0 ? toolResults : undefined,
timing: {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
modelTime: modelTime,
toolsTime: toolsTime,
firstResponseTime: firstResponseTime,
iterations: iterationCount + 1,
timeSegments: timeSegments,
},
}
} catch (error) {
const providerEndTime = Date.now()
const providerEndTimeISO = new Date(providerEndTime).toISOString()
const totalDuration = providerEndTime - providerStartTime
logger.error(`Error in ${config.providerLabel} request:`, {
error,
duration: totalDuration,
})
throw new ProviderError(toError(error).message, {
startTime: providerStartTimeISO,
endTime: providerEndTimeISO,
duration: totalDuration,
})
}
}
/**
* Determines a finish reason for an OpenAI Responses API response.
* Maps to conventional values: 'tool_calls' | 'length' | 'stop'.
*/
function deriveOpenAIFinishReason(
response: OpenAI.Responses.Response,
toolCalls: ResponsesToolCall[]
): string | undefined {
const incompleteReason = response.incomplete_details?.reason
if (incompleteReason === 'max_output_tokens') return 'length'
if (incompleteReason === 'content_filter') return 'content_filter'
if (toolCalls.length > 0) return 'tool_calls'
if (incompleteReason) return incompleteReason
if (response.status === 'failed') return 'error'
if (response.status === 'incomplete') return 'length'
if (response.status && response.status !== 'completed') return response.status
return 'stop'
}
/**
* Enriches the last model segment with per-iteration content extracted from an
* OpenAI Responses API response: assistant text, tool calls, finish reason,
* and token usage for the iteration.
*/
function enrichLastModelSegmentFromOpenAIResponse(
timeSegments: TimeSegment[],
response: OpenAI.Responses.Response,
assistantText: string,
toolCallsInResponse: ResponsesToolCall[],
extras?: {
model?: string
ttft?: number
errorType?: string
errorMessage?: string
}
): void {
const toolCalls: IterationToolCall[] = toolCallsInResponse.map((tc) => ({
id: tc.id,
name: tc.name,
arguments:
typeof tc.arguments === 'string' ? parseToolCallArguments(tc.arguments) : tc.arguments,
}))
const usage = parseResponsesUsage(response.usage)
const thinkingContent = extractResponseReasoning(response.output)
let cost: { input: number; output: number; total: number } | undefined
if (extras?.model && usage) {
const full = calculateCost(
extras.model,
usage.promptTokens,
usage.completionTokens,
usage.cachedTokens > 0
)
cost = { input: full.input, output: full.output, total: full.total }
}
enrichLastModelSegment(timeSegments, {
assistantContent: assistantText || undefined,
thinkingContent: thinkingContent || undefined,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
finishReason: deriveOpenAIFinishReason(response, toolCallsInResponse),
tokens: usage
? {
input: usage.promptTokens,
output: usage.completionTokens,
total: usage.totalTokens,
...(usage.cachedTokens > 0 && { cacheRead: usage.cachedTokens }),
...(usage.reasoningTokens > 0 && { reasoning: usage.reasoningTokens }),
}
: undefined,
cost,
provider: 'openai',
ttft: extras?.ttft,
errorType: extras?.errorType,
errorMessage: extras?.errorMessage,
})
}
+38
View File
@@ -0,0 +1,38 @@
import { createLogger } from '@sim/logger'
import type { StreamingExecution } from '@/executor/types'
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
import type { ProviderConfig, ProviderRequest, ProviderResponse } from '@/providers/types'
import { executeResponsesProviderRequest } from './core'
const logger = createLogger('OpenAIProvider')
const responsesEndpoint = 'https://api.openai.com/v1/responses'
export const openaiProvider: ProviderConfig = {
id: 'openai',
name: 'OpenAI',
description: "OpenAI's GPT models",
version: '1.0.0',
models: getProviderModels('openai'),
defaultModel: getProviderDefaultModel('openai'),
executeRequest: async (
request: ProviderRequest
): Promise<ProviderResponse | StreamingExecution> => {
if (!request.apiKey) {
throw new Error('API key is required for OpenAI')
}
return executeResponsesProviderRequest(request, {
providerId: 'openai',
providerLabel: 'OpenAI',
modelName: request.model,
endpoint: responsesEndpoint,
headers: {
Authorization: `Bearer ${request.apiKey}`,
'Content-Type': 'application/json',
'OpenAI-Beta': 'responses=v1',
},
logger,
})
},
}
+41
View File
@@ -0,0 +1,41 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { buildResponsesInputFromMessages } from '@/providers/openai/utils'
describe('buildResponsesInputFromMessages', () => {
it('should convert user message files to Responses multipart content', () => {
const input = buildResponsesInputFromMessages([
{
role: 'user',
content: 'Analyze this image',
files: [
{
id: 'file-1',
key: 'workspace/ws-1/example.png',
name: 'example.png',
url: '/api/files/serve/workspace%2Fws-1%2Fexample.png?context=workspace',
size: 128,
type: 'image/png',
base64: 'iVBORw0KGgo=',
},
],
},
])
expect(input).toEqual([
{
role: 'user',
content: [
{ type: 'input_text', text: 'Analyze this image' },
{
type: 'input_image',
image_url: 'data:image/png;base64,iVBORw0KGgo=',
detail: 'auto',
},
],
},
])
})
})
+537
View File
@@ -0,0 +1,537 @@
import { createLogger } from '@sim/logger'
import type OpenAI from 'openai'
import { buildOpenAIMessageContent } from '@/providers/attachments'
import type { Message } from '@/providers/types'
const logger = createLogger('ResponsesUtils')
export interface ResponsesUsageTokens {
promptTokens: number
completionTokens: number
totalTokens: number
cachedTokens: number
reasoningTokens: number
}
export interface ResponsesToolCall {
id: string
name: string
arguments: string
}
export type ResponsesInputItem =
| {
role: 'system' | 'user' | 'assistant'
content: string | OpenAI.Responses.ResponseInputContent[]
}
| {
type: 'function_call'
call_id: string
name: string
arguments: string
}
| {
type: 'function_call_output'
call_id: string
output: string
}
export interface ResponsesToolDefinition {
type: 'function'
name: string
description?: string
parameters?: Record<string, unknown>
}
/**
* Converts chat-style messages into Responses API input items.
*/
export function buildResponsesInputFromMessages(
messages: Message[],
providerId = 'openai'
): ResponsesInputItem[] {
const input: ResponsesInputItem[] = []
for (const message of messages) {
if (message.role === 'tool' && message.tool_call_id) {
input.push({
type: 'function_call_output',
call_id: message.tool_call_id,
output: message.content ?? '',
})
continue
}
if (message.role === 'system' || message.role === 'user' || message.role === 'assistant') {
const content =
message.role === 'user'
? buildOpenAIMessageContent(message.content, message.files, providerId)
: (message.content ?? '')
if (
(typeof content === 'string' && !content) ||
(Array.isArray(content) && content.length === 0)
) {
continue
}
input.push({
role: message.role,
content,
})
}
if (message.tool_calls?.length) {
for (const toolCall of message.tool_calls) {
input.push({
type: 'function_call',
call_id: toolCall.id,
name: toolCall.function.name,
arguments: toolCall.function.arguments,
})
}
}
}
return input
}
/**
* Converts tool definitions to the Responses API format.
*/
export function convertToolsToResponses(
tools: Array<{
type?: string
name?: string
description?: string
parameters?: Record<string, unknown>
function?: { name: string; description?: string; parameters?: Record<string, unknown> }
}>
): ResponsesToolDefinition[] {
return tools
.map((tool) => {
const name = tool.function?.name ?? tool.name
if (!name) {
return null
}
return {
type: 'function' as const,
name,
description: tool.function?.description ?? tool.description,
parameters: tool.function?.parameters ?? tool.parameters,
}
})
.filter(Boolean) as ResponsesToolDefinition[]
}
/**
* Converts tool_choice to the Responses API format.
*/
export function toResponsesToolChoice(
toolChoice:
| 'auto'
| 'none'
| { type: 'function'; function?: { name: string }; name?: string }
| { type: 'tool'; name: string }
| { type: 'any'; any: { model: string; name: string } }
| undefined
): 'auto' | 'none' | { type: 'function'; name: string } | undefined {
if (!toolChoice) {
return undefined
}
if (typeof toolChoice === 'string') {
return toolChoice
}
if (toolChoice.type === 'function') {
const name = toolChoice.name ?? toolChoice.function?.name
return name ? { type: 'function', name } : undefined
}
return 'auto'
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function extractTextFromMessageItem(item: unknown): string {
if (!isRecord(item)) {
return ''
}
if (typeof item.content === 'string') {
return item.content
}
if (!Array.isArray(item.content)) {
return ''
}
const textParts: string[] = []
for (const part of item.content) {
if (!isRecord(part)) {
continue
}
if ((part.type === 'output_text' || part.type === 'text') && typeof part.text === 'string') {
textParts.push(part.text)
continue
}
if (part.type === 'output_json') {
if (typeof part.text === 'string') {
textParts.push(part.text)
} else if (part.json !== undefined) {
textParts.push(JSON.stringify(part.json))
}
}
}
return textParts.join('')
}
/**
* Extracts plain text from Responses API output items.
*/
export function extractResponseText(output: OpenAI.Responses.ResponseOutputItem[]): string {
if (!Array.isArray(output)) {
return ''
}
const textParts: string[] = []
for (const item of output) {
if (item?.type !== 'message') {
continue
}
const text = extractTextFromMessageItem(item)
if (text) {
textParts.push(text)
}
}
return textParts.join('')
}
/**
* Extracts reasoning summary text from Responses API output items. Reasoning
* items (emitted by o1/o3/gpt-5) carry a `summary[]` of `{ type, text }` entries
* — we join the text for trace display. The raw `encrypted_content` is left
* alone; it's opaque plumbing for round-tripping across turns.
*/
export function extractResponseReasoning(output: OpenAI.Responses.ResponseOutputItem[]): string {
if (!Array.isArray(output)) return ''
const parts: string[] = []
for (const item of output) {
if (!item || item.type !== 'reasoning') continue
const summary = (item as unknown as { summary?: Array<{ text?: string | null } | null> })
.summary
if (!Array.isArray(summary)) continue
for (const entry of summary) {
const text = entry?.text
if (typeof text === 'string' && text.length > 0) parts.push(text)
}
}
return parts.join('\n\n')
}
/**
* Converts Responses API output items into input items for subsequent calls.
*/
export function convertResponseOutputToInputItems(
output: OpenAI.Responses.ResponseOutputItem[]
): ResponsesInputItem[] {
if (!Array.isArray(output)) {
return []
}
const items: ResponsesInputItem[] = []
for (const item of output) {
if (!isRecord(item)) {
continue
}
if (item.type === 'message') {
const text = extractTextFromMessageItem(item)
if (text) {
items.push({
role: 'assistant',
content: text,
})
}
// Handle Chat Completions-style tool_calls nested under message items
const toolCalls = Array.isArray(item.tool_calls) ? item.tool_calls : []
for (const toolCall of toolCalls) {
const tc = toolCall as Record<string, unknown>
const fn = tc.function as Record<string, unknown> | undefined
const callId = tc.id as string | undefined
const name = (fn?.name ?? tc.name) as string | undefined
if (!callId || !name) {
continue
}
const argumentsValue =
typeof fn?.arguments === 'string' ? fn.arguments : JSON.stringify(fn?.arguments ?? {})
items.push({
type: 'function_call',
call_id: callId,
name,
arguments: argumentsValue,
})
}
continue
}
if (item.type === 'function_call') {
const fc = item as OpenAI.Responses.ResponseFunctionToolCall
const callId = fc.call_id ?? (typeof item.id === 'string' ? item.id : undefined)
const name =
fc.name ??
(isRecord(item.function) && typeof item.function.name === 'string'
? item.function.name
: undefined)
if (!callId || !name) {
continue
}
const argumentsValue =
typeof fc.arguments === 'string' ? fc.arguments : JSON.stringify(fc.arguments ?? {})
items.push({
type: 'function_call',
call_id: callId,
name,
arguments: argumentsValue,
})
}
}
return items
}
/**
* Extracts tool calls from Responses API output items.
*/
export function extractResponseToolCalls(
output: OpenAI.Responses.ResponseOutputItem[]
): ResponsesToolCall[] {
if (!Array.isArray(output)) {
return []
}
const toolCalls: ResponsesToolCall[] = []
for (const item of output) {
if (!isRecord(item)) {
continue
}
if (item.type === 'function_call') {
const fc = item as OpenAI.Responses.ResponseFunctionToolCall
const callId = fc.call_id ?? (typeof item.id === 'string' ? item.id : undefined)
const name =
fc.name ??
(isRecord(item.function) && typeof item.function.name === 'string'
? item.function.name
: undefined)
if (!callId || !name) {
continue
}
const argumentsValue =
typeof fc.arguments === 'string' ? fc.arguments : JSON.stringify(fc.arguments ?? {})
toolCalls.push({
id: callId,
name,
arguments: argumentsValue,
})
continue
}
// Handle Chat Completions-style tool_calls nested under message items
if (item.type === 'message' && Array.isArray(item.tool_calls)) {
for (const toolCall of item.tool_calls) {
const tc = toolCall as Record<string, unknown>
const fn = tc.function as Record<string, unknown> | undefined
const callId = tc.id as string | undefined
const name = (fn?.name ?? tc.name) as string | undefined
if (!callId || !name) {
continue
}
const argumentsValue =
typeof fn?.arguments === 'string' ? fn.arguments : JSON.stringify(fn?.arguments ?? {})
toolCalls.push({
id: callId,
name,
arguments: argumentsValue,
})
}
}
}
return toolCalls
}
/**
* Maps Responses API usage data to prompt/completion token counts.
*
* Note: output_tokens is expected to include reasoning tokens; fall back to reasoning_tokens
* when output_tokens is missing or zero.
*/
export function parseResponsesUsage(
usage: OpenAI.Responses.ResponseUsage | undefined
): ResponsesUsageTokens | undefined {
if (!usage) {
return undefined
}
const inputTokens = usage.input_tokens ?? 0
const outputTokens = usage.output_tokens ?? 0
const cachedTokens = usage.input_tokens_details?.cached_tokens ?? 0
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0
const completionTokens = Math.max(outputTokens, reasoningTokens)
const totalTokens = inputTokens + completionTokens
return {
promptTokens: inputTokens,
completionTokens,
totalTokens,
cachedTokens,
reasoningTokens,
}
}
/**
* Creates a ReadableStream from a Responses API SSE stream.
*/
export function createReadableStreamFromResponses(
response: Response,
onComplete?: (content: string, usage?: ResponsesUsageTokens) => void
): ReadableStream<Uint8Array> {
let fullContent = ''
let finalUsage: ResponsesUsageTokens | undefined
let activeEventType: string | undefined
const encoder = new TextEncoder()
return new ReadableStream<Uint8Array>({
async start(controller) {
const reader = response.body?.getReader()
if (!reader) {
controller.close()
return
}
const decoder = new TextDecoder()
let buffer = ''
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n')
buffer = lines.pop() || ''
for (const line of lines) {
const trimmed = line.trim()
if (!trimmed) {
continue
}
if (trimmed.startsWith('event:')) {
activeEventType = trimmed.slice(6).trim()
continue
}
if (!trimmed.startsWith('data:')) {
continue
}
const data = trimmed.slice(5).trim()
if (data === '[DONE]') {
continue
}
let event: Record<string, unknown>
try {
event = JSON.parse(data)
} catch (error) {
logger.debug('Skipping non-JSON response stream chunk', {
data: data.slice(0, 200),
error,
})
continue
}
const eventType = event?.type ?? activeEventType
if (
eventType === 'response.error' ||
eventType === 'error' ||
eventType === 'response.failed'
) {
const errorObj = event.error as Record<string, unknown> | undefined
const message = (errorObj?.message as string) || 'Responses API stream error'
controller.error(new Error(message))
return
}
if (
eventType === 'response.output_text.delta' ||
eventType === 'response.output_json.delta'
) {
let deltaText = ''
const delta = event.delta as string | Record<string, unknown> | undefined
if (typeof delta === 'string') {
deltaText = delta
} else if (delta && typeof delta.text === 'string') {
deltaText = delta.text
} else if (delta && delta.json !== undefined) {
deltaText = JSON.stringify(delta.json)
} else if (event.json !== undefined) {
deltaText = JSON.stringify(event.json)
} else if (typeof event.text === 'string') {
deltaText = event.text
}
if (deltaText.length > 0) {
fullContent += deltaText
controller.enqueue(encoder.encode(deltaText))
}
}
if (eventType === 'response.completed') {
const responseObj = event.response as Record<string, unknown> | undefined
const usageData = (responseObj?.usage ?? event.usage) as
| OpenAI.Responses.ResponseUsage
| undefined
finalUsage = parseResponsesUsage(usageData)
}
}
}
if (onComplete) {
onComplete(fullContent, finalUsage)
}
controller.close()
} catch (error) {
controller.error(error)
} finally {
reader.releaseLock()
}
},
})
}