fix: webhook response format

This commit is contained in:
Emir Karabeg
2025-03-11 15:05:53 -07:00
parent 8e37172377
commit 47f16cd8c8
3 changed files with 59 additions and 5 deletions
+26 -5
View File
@@ -426,14 +426,35 @@ async function processWebhook(
// Process the block states to ensure response formats are properly parsed
const processedBlockStates = Object.entries(currentBlockStates).reduce(
(acc, [blockId, blockState]) => {
// Create a copy of the block state to avoid modifying the original
const processedState = { ...blockState }
// Check if this block has a responseFormat that needs to be parsed
if (blockState.responseFormat && typeof blockState.responseFormat === 'string') {
if (processedState.responseFormat) {
try {
const parsedResponseFormat = JSON.parse(blockState.responseFormat)
acc[blockId] = {
...blockState,
responseFormat: parsedResponseFormat,
// If responseFormat is a string, parse it
if (typeof processedState.responseFormat === 'string') {
processedState.responseFormat = JSON.parse(processedState.responseFormat)
}
// Ensure the responseFormat is properly structured for OpenAI
// This ensures compatibility with the provider's expected format
if (
processedState.responseFormat &&
typeof processedState.responseFormat === 'object'
) {
// Make sure it has the required properties
if (!processedState.responseFormat.schema && !processedState.responseFormat.name) {
// If it's just a raw schema, wrap it properly
processedState.responseFormat = {
name: 'response_schema',
schema: processedState.responseFormat,
strict: true,
}
}
}
acc[blockId] = processedState
} catch (error) {
console.warn(`Failed to parse responseFormat for block ${blockId}:`, error)
acc[blockId] = blockState
+18
View File
@@ -175,6 +175,24 @@ export class AgentBlockHandler implements BlockHandler {
: inputs.responseFormat
console.log(`[AgentBlockHandler Debug] Response format after parsing:`, responseFormat)
// Ensure the responseFormat is properly structured
if (responseFormat && typeof responseFormat === 'object') {
// If it's just a raw schema without the expected wrapper properties,
// wrap it properly for the provider
if (!responseFormat.schema && !responseFormat.name) {
console.log(`[AgentBlockHandler Debug] Wrapping raw schema in proper format`)
responseFormat = {
name: 'response_schema',
schema: responseFormat,
strict: true,
}
}
console.log(
`[AgentBlockHandler Debug] Final response format structure:`,
responseFormat
)
}
} catch (error: any) {
console.error(`[AgentBlockHandler Error] Failed to parse response format:`, error)
throw new Error(`Invalid response format: ${error.message}`)
+15
View File
@@ -78,6 +78,15 @@ export const openaiProvider: ProviderConfig = {
// Add response format for structured output if specified
if (request.responseFormat) {
// Log detailed information about the responseFormat
console.log(`[OpenAI Provider Debug] Response format details:`, {
type: typeof request.responseFormat,
isString: typeof request.responseFormat === 'string',
hasSchema: request.responseFormat.schema !== undefined,
hasName: request.responseFormat.name !== undefined,
value: JSON.stringify(request.responseFormat),
})
// Use OpenAI's JSON schema format
payload.response_format = {
type: 'json_schema',
@@ -87,6 +96,12 @@ export const openaiProvider: ProviderConfig = {
strict: request.responseFormat.strict !== false,
},
}
// Log the final response_format being sent to OpenAI
console.log(
`[OpenAI Provider Debug] Final response_format:`,
JSON.stringify(payload.response_format)
)
}
// Add tools if provided