Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

336 lines
12 KiB
TypeScript

import { isDeepStrictEqual } from 'node:util'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { isPlainRecord } from '@sim/utils/object'
import { getBlock } from '@/blocks/index'
import { isMcpTool } from '@/executor/constants'
import type { BlockHandler, ExecutionContext } from '@/executor/types'
import { readStatusCode } from '@/executor/utils/errors'
import { prepareResolvedSecretProjectedInputs } from '@/executor/utils/resolved-secret-input-projection'
import type { ResolvedSecretInputPath } from '@/executor/utils/resolved-secret-trace-registry'
import type { SerializedBlock } from '@/serializer/types'
import { executeTool } from '@/tools'
import type { ToolConfig } from '@/tools/types'
import { getTool } from '@/tools/utils'
const logger = createLogger('GenericBlockHandler')
interface BlockBoundaryPaths {
paths: ResolvedSecretInputPath[]
requiredProjectionRoots: Set<string>
}
function selectBlockBoundaryPaths(
tool: ToolConfig,
params: Record<string, unknown>
): BlockBoundaryPaths | undefined {
try {
const paths: ResolvedSecretInputPath[] = []
const requiredProjectionRoots = new Set<string>()
const modelInput = tool.request.modelInput
if (modelInput?.mode === 'project') {
const selected = modelInput.select(params)
if (!isPlainRecord(selected)) return undefined
for (const key of Object.keys(selected)) {
requiredProjectionRoots.add(key)
paths.push([key])
}
const privateInputPaths = modelInput.privateInputPaths?.(params) ?? []
paths.push(...privateInputPaths)
for (const path of privateInputPaths) {
if (path[0]) requiredProjectionRoots.add(path[0])
}
} else if (modelInput?.mode === 'private-provenance') {
const privateInputPaths = modelInput.inputPaths(params)
paths.push(...privateInputPaths)
for (const path of privateInputPaths) {
if (path[0]) requiredProjectionRoots.add(path[0])
}
}
for (const selection of tool.request.secretProvenance?.request?.(params) ?? []) {
paths.push(...selection.inputPaths)
for (const path of selection.inputPaths) {
if (path[0]) requiredProjectionRoots.add(path[0])
}
}
const uniquePaths = new Map<string, ResolvedSecretInputPath>()
for (const path of paths) {
if (path.length > 0) uniquePaths.set(JSON.stringify(path), path)
}
return { paths: [...uniquePaths.values()], requiredProjectionRoots }
} catch {
return undefined
}
}
function canonicalPlaceholder(value: unknown): string | undefined {
if (typeof value !== 'string') return undefined
const match = /^\{\{([A-Za-z0-9_]+)\}\}$/.exec(value.trim())
return match ? value.trim() : undefined
}
function isFileBoundaryPath(tool: ToolConfig, path: ResolvedSecretInputPath): boolean {
return Boolean(path[0] && tool.params[path[0]]?.type === 'file')
}
function projectScalarLeaves(
value: unknown,
placeholder: string
): { value: unknown; projectedLeaves: number } | undefined {
if (value === null || typeof value !== 'object') {
return { value: placeholder, projectedLeaves: 1 }
}
if (!Array.isArray(value) && !isPlainRecord(value)) return undefined
const root: unknown[] | Record<string, unknown> = Array.isArray(value) ? [] : {}
const pending: Array<{
source: unknown[] | Record<string, unknown>
target: unknown[] | Record<string, unknown>
}> = [{ source: value as unknown[] | Record<string, unknown>, target: root }]
const visited = new WeakSet<object>()
let projectedLeaves = 0
while (pending.length > 0) {
const { source, target } = pending.pop()!
if (visited.has(source)) return undefined
visited.add(source)
for (const [key, child] of Object.entries(source)) {
if (child !== null && typeof child === 'object') {
if (!Array.isArray(child) && !isPlainRecord(child)) return undefined
const projectedChild: unknown[] | Record<string, unknown> = Array.isArray(child) ? [] : {}
;(target as Record<string, unknown>)[key] = projectedChild
pending.push({
source: child as unknown[] | Record<string, unknown>,
target: projectedChild,
})
} else {
;(target as Record<string, unknown>)[key] = placeholder
projectedLeaves += 1
}
}
}
return { value: root, projectedLeaves }
}
function createStructuredModelProjection(
tool: ToolConfig,
finalInputs: Record<string, unknown>,
sourcePath: ResolvedSecretInputPath,
projectedSourceValue: unknown
): Record<string, unknown> | undefined {
const modelInput = tool.request.modelInput
const sourceKey = sourcePath.length === 1 ? sourcePath[0] : undefined
const placeholder = canonicalPlaceholder(projectedSourceValue)
if (modelInput?.mode !== 'project' || !modelInput.applyProjected || !sourceKey || !placeholder) {
return undefined
}
try {
const selected = modelInput.select(finalInputs)
if (!isPlainRecord(selected) || !Object.hasOwn(selected, sourceKey)) return undefined
const projectedValue = projectScalarLeaves(selected[sourceKey], placeholder)
if (!projectedValue || projectedValue.projectedLeaves === 0) return undefined
const projectedSelection = { ...selected, [sourceKey]: projectedValue.value }
const selectedParams = Object.fromEntries(
Object.keys(selected).map((key) => [key, finalInputs[key]])
)
const patch = modelInput.applyProjected(structuredClone(selectedParams), projectedSelection)
if (!isPlainRecord(patch)) return undefined
const projectedInputs = { ...finalInputs, ...patch }
if (!isDeepStrictEqual(modelInput.select(projectedInputs), projectedSelection)) return undefined
return projectedInputs
} catch {
return undefined
}
}
export class GenericBlockHandler implements BlockHandler {
canHandle(block: SerializedBlock): boolean {
return true
}
async execute(
ctx: ExecutionContext,
block: SerializedBlock,
inputs: Record<string, any>
): Promise<any> {
const isMcp = block.config.tool ? isMcpTool(block.config.tool) : false
let tool = null
if (!isMcp) {
tool = getTool(block.config.tool)
if (!tool) {
throw new Error(`Tool not found: ${block.config.tool}`)
}
}
let finalInputs = { ...inputs }
const blockType = block.metadata?.id
if (blockType) {
const blockConfig = getBlock(blockType)
const registry = ctx.resolvedSecretTraceRegistry
if (blockConfig?.tools?.config?.params) {
const transformedParams = blockConfig.tools.config.params(inputs)
finalInputs = { ...inputs, ...transformedParams }
}
if (blockConfig?.inputs) {
for (const [key, inputSchema] of Object.entries(blockConfig.inputs)) {
const value = finalInputs[key]
if (typeof value === 'string' && value.trim().length > 0) {
const inputType = typeof inputSchema === 'object' ? inputSchema.type : inputSchema
if (inputType === 'json' || inputType === 'array') {
try {
finalInputs[key] = JSON.parse(value.trim())
} catch (error) {
logger.warn(`Failed to parse ${inputType} field "${key}":`, {
error: toError(error).message,
})
}
}
}
}
}
const boundary = tool ? selectBlockBoundaryPaths(tool, finalInputs) : undefined
const projectedInputs =
boundary && boundary.paths.length > 0 && registry?.hasResolvedInputProjections()
? registry.projectResolvedInputSelections(inputs)
: undefined
if (projectedInputs?.complete === false) registry?.markIncomplete()
if (projectedInputs?.complete && boundary && tool && registry) {
for (const projection of projectedInputs.values) {
const preserveFileDescriptorGrammar =
isFileBoundaryPath(tool, projection.path) ||
boundary.paths.some((path) => isFileBoundaryPath(tool, path))
let projectedFinalInputs = prepareResolvedSecretProjectedInputs(
projection.value,
blockConfig?.inputs,
inputs,
{ preserveFileDescriptorGrammar }
)
try {
if (blockConfig?.tools?.config?.params) {
projectedFinalInputs = {
...projectedFinalInputs,
...blockConfig.tools.config.params(projectedFinalInputs),
}
}
} catch {
const structuredProjection = createStructuredModelProjection(
tool,
finalInputs,
projection.path,
projection.projectedValue
)
if (structuredProjection) {
registry.recordTransformedInputProjection(finalInputs, structuredProjection, {
targetPaths: boundary.paths,
})
continue
}
if (boundary.requiredProjectionRoots.has(projection.path[0])) {
registry.markIncomplete()
}
continue
}
if (blockConfig?.inputs) {
projectedFinalInputs = prepareResolvedSecretProjectedInputs(
projectedFinalInputs,
blockConfig.inputs,
finalInputs,
{ preserveFileDescriptorGrammar }
)
for (const [key, inputSchema] of Object.entries(blockConfig.inputs)) {
const value = projectedFinalInputs[key]
if (typeof value !== 'string' || value.trim().length === 0) continue
const inputType = typeof inputSchema === 'object' ? inputSchema.type : inputSchema
if (inputType !== 'json' && inputType !== 'array') continue
try {
projectedFinalInputs[key] = JSON.parse(value.trim())
} catch {}
}
}
registry.recordTransformedInputProjection(finalInputs, projectedFinalInputs, {
targetPaths: boundary.paths,
})
}
}
}
try {
const result = await executeTool(
block.config.tool,
{
...finalInputs,
_context: {
workflowId: ctx.workflowId,
workspaceId: ctx.workspaceId,
executionId: ctx.executionId,
userId: ctx.userId,
isDeployedContext: ctx.isDeployedContext,
enforceCredentialAccess: ctx.enforceCredentialAccess,
},
},
{ executionContext: ctx }
)
if (!result.success) {
const errorDetails = []
if (result.error) errorDetails.push(result.error)
const errorMessage =
errorDetails.length > 0
? errorDetails.join(' - ')
: `Block execution of ${tool?.name || block.config.tool} failed with no error message`
const error = new Error(errorMessage)
Object.assign(error, {
toolId: block.config.tool,
toolName: tool?.name || 'Unknown tool',
blockId: block.id,
blockName: block.metadata?.name || 'Unnamed Block',
output: result.output || {},
timestamp: new Date().toISOString(),
// `executeTool` flattens a thrown error into a result, so Sim's own
// status (hosted-key 429/503) would be lost here. Carry it onto the
// error so `getExecutionErrorStatus` can still reach the API caller.
...(typeof result.statusCode === 'number' ? { statusCode: result.statusCode } : {}),
})
throw error
}
return result.output
} catch (error: any) {
if (!error.message || error.message === 'undefined (undefined)') {
let errorMessage = `Block execution of ${tool?.name || block.config.tool} failed`
if (block.metadata?.name) {
errorMessage += `: ${block.metadata.name}`
}
const statusCode = readStatusCode(error)
if (statusCode !== undefined) {
errorMessage += ` (Status: ${statusCode})`
}
error.message = errorMessage
}
if (typeof error === 'object' && error !== null) {
if (!error.toolId) error.toolId = block.config.tool
if (!error.blockName) error.blockName = block.metadata?.name || 'Unnamed Block'
}
throw error
}
}
}