Files
sim/apps/sim/executor/handlers/generic/generic-handler.ts
Waleed cf023e4d22 feat(tools): added download file tool for onedrive, google drive, and slack; added move email tool for gmail and outlook (#1785)
* feat(tools): added download file tool for onedrive, google drive, and slack

* added gmail & outlook move tools, added missing credentials descriptions to modal

* added slack delete/update message, add reaction; added gmail read/unread/label/unarchive; added outlook copy/delete/read/unread

* added threads to slack operations

* added timestamp for slack webhook trigger since api uses timestamp for updating/reacting/deleting

* cleanup

* added file info to slack read messages

* updated slack desc

* fixed downloading for onedrive, slack, and drive

* fix type check

* fix build failure

* cleanup files, fix triggers with attachments, fix integration blocks with include attachment to parse to user files, remove unused code

* fix move files tools

* fix tests

* fix build errors

* fix type error

* fix tests

* remove redundant code and filter out unecessary user file fields

* fix lint error

* remove fields from tag dropdown

* fix file upload via API

* fix pdf parse issue

---------

Co-authored-by: waleed <waleed>
Co-authored-by: Adam Gough <adamgough@Adams-MacBook-Pro.local>
Co-authored-by: Vikhyath Mondreti <vikhyath@simstudio.ai>
2025-11-05 13:00:34 -08:00

145 lines
4.0 KiB
TypeScript

import { createLogger } from '@/lib/logs/console/logger'
import { getBlock } from '@/blocks/index'
import type { BlockHandler, ExecutionContext } from '@/executor/types'
import type { SerializedBlock } from '@/serializer/types'
import { executeTool } from '@/tools'
import { getTool } from '@/tools/utils'
const logger = createLogger('GenericBlockHandler')
export class GenericBlockHandler implements BlockHandler {
canHandle(block: SerializedBlock): boolean {
return true
}
async execute(
ctx: ExecutionContext,
block: SerializedBlock,
inputs: Record<string, any>
): Promise<any> {
logger.info(`Executing block: ${block.id} (Type: ${block.metadata?.id})`)
const isMcpTool = block.config.tool?.startsWith('mcp-')
let tool = null
if (!isMcpTool) {
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)
if (blockConfig?.tools?.config?.params) {
try {
const transformedParams = blockConfig.tools.config.params(inputs)
finalInputs = { ...inputs, ...transformedParams }
logger.info(`Applied parameter transformation for block type: ${blockType}`, {
original: inputs,
transformed: transformedParams,
})
} catch (error) {
logger.warn(`Failed to apply parameter transformation for block type ${blockType}:`, {
error: error instanceof Error ? error.message : String(error),
})
}
}
}
logger.info(`[GenericBlockHandler] Calling executeTool for ${block.config.tool}`, {
blockId: block.id,
blockName: block.metadata?.name,
originalInputs: inputs,
finalInputs: finalInputs,
tool: block.config.tool,
})
try {
const result = await executeTool(
block.config.tool,
{
...finalInputs,
_context: {
workflowId: ctx.workflowId,
workspaceId: ctx.workspaceId,
executionId: ctx.executionId,
},
},
false,
false,
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(),
})
throw error
}
const output = result.output
let cost = null
if (block.config.tool?.startsWith('knowledge_') && output?.cost) {
cost = output.cost
}
if (cost) {
return {
...output,
cost: {
input: cost.input,
output: cost.output,
total: cost.total,
},
tokens: cost.tokens,
model: cost.model,
}
}
return 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}`
}
if (error.status) {
errorMessage += ` (Status: ${error.status})`
}
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
}
}
}