d25d482dc2
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
111 lines
3.3 KiB
TypeScript
111 lines
3.3 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { BlockType, HTTP } from '@/executor/constants'
|
|
import type { BlockHandler, ExecutionContext, NormalizedBlockOutput } from '@/executor/types'
|
|
import {
|
|
convertBuilderDataToJson,
|
|
convertBuilderDataToJsonString,
|
|
} from '@/executor/utils/builder-data'
|
|
import { parseObjectStrings } from '@/executor/utils/json'
|
|
import type { SerializedBlock } from '@/serializer/types'
|
|
|
|
const logger = createLogger('ResponseBlockHandler')
|
|
|
|
export class ResponseBlockHandler implements BlockHandler {
|
|
canHandle(block: SerializedBlock): boolean {
|
|
return block.metadata?.id === BlockType.RESPONSE
|
|
}
|
|
|
|
async execute(
|
|
ctx: ExecutionContext,
|
|
block: SerializedBlock,
|
|
inputs: Record<string, any>
|
|
): Promise<NormalizedBlockOutput> {
|
|
logger.info(`Executing response block: ${block.id}`)
|
|
|
|
try {
|
|
const responseData = this.parseResponseData(inputs)
|
|
const statusCode = this.parseStatus(inputs.status)
|
|
const responseHeaders = this.parseHeaders(inputs.headers)
|
|
|
|
logger.info('Response prepared', {
|
|
status: statusCode,
|
|
dataKeys: Object.keys(responseData),
|
|
headerKeys: Object.keys(responseHeaders),
|
|
})
|
|
|
|
return {
|
|
data: responseData,
|
|
status: statusCode,
|
|
headers: responseHeaders,
|
|
}
|
|
} catch (error: any) {
|
|
logger.error('Response block execution failed:', error)
|
|
return {
|
|
data: {
|
|
error: 'Response block execution failed',
|
|
message: error.message || 'Unknown error',
|
|
},
|
|
status: HTTP.STATUS.SERVER_ERROR,
|
|
headers: { 'Content-Type': HTTP.CONTENT_TYPE.JSON },
|
|
}
|
|
}
|
|
}
|
|
|
|
private parseResponseData(inputs: Record<string, any>): any {
|
|
const dataMode = inputs.dataMode || 'structured'
|
|
|
|
if (dataMode === 'json' && inputs.data) {
|
|
if (typeof inputs.data === 'string') {
|
|
try {
|
|
return JSON.parse(inputs.data)
|
|
} catch (error) {
|
|
logger.warn('Failed to parse JSON data, returning as string:', error)
|
|
return inputs.data
|
|
}
|
|
} else if (typeof inputs.data === 'object' && inputs.data !== null) {
|
|
return inputs.data
|
|
}
|
|
return inputs.data
|
|
}
|
|
|
|
if (dataMode === 'structured' && inputs.builderData) {
|
|
const convertedData = convertBuilderDataToJson(inputs.builderData)
|
|
return parseObjectStrings(convertedData)
|
|
}
|
|
|
|
return inputs.data || {}
|
|
}
|
|
|
|
static convertBuilderDataToJsonString(builderData: any[]): string {
|
|
return convertBuilderDataToJsonString(builderData)
|
|
}
|
|
|
|
private parseStatus(status?: string): number {
|
|
if (!status) return HTTP.STATUS.OK
|
|
const parsed = Number(status)
|
|
if (Number.isNaN(parsed) || parsed < 100 || parsed > 599) {
|
|
return HTTP.STATUS.OK
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
private parseHeaders(
|
|
headers: {
|
|
id: string
|
|
cells: { Key: string; Value: string }
|
|
}[]
|
|
): Record<string, string> {
|
|
const defaultHeaders = { 'Content-Type': HTTP.CONTENT_TYPE.JSON }
|
|
if (!headers) return defaultHeaders
|
|
|
|
const headerObj = headers.reduce((acc: Record<string, string>, header) => {
|
|
if (header?.cells?.Key && header?.cells?.Value) {
|
|
acc[header.cells.Key] = header.cells.Value
|
|
}
|
|
return acc
|
|
}, {})
|
|
|
|
return { ...defaultHeaders, ...headerObj }
|
|
}
|
|
}
|