mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
Added code execution functionality for function block
This commit is contained in:
@@ -11,20 +11,21 @@ export const FunctionBlock: BlockConfig = {
|
||||
category: 'advanced',
|
||||
},
|
||||
tools: {
|
||||
access: ['function']
|
||||
access: ['function.execute'],
|
||||
config: {
|
||||
tool: () => 'function.execute'
|
||||
}
|
||||
},
|
||||
workflow: {
|
||||
outputType: 'json',
|
||||
inputs: {
|
||||
code: 'string'
|
||||
},
|
||||
subBlocks: [
|
||||
{
|
||||
id: 'code',
|
||||
title: 'Code',
|
||||
type: 'code',
|
||||
layout: 'full',
|
||||
},
|
||||
],
|
||||
},
|
||||
placeholder: 'Enter your code here...'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { ToolConfig, ToolResponse } from '../types'
|
||||
|
||||
interface CodeExecutionInput {
|
||||
code: Array<{content: string, id: string}> | string
|
||||
input?: Record<string, any>
|
||||
}
|
||||
|
||||
interface CodeExecutionOutput extends ToolResponse {
|
||||
output: Record<string, any>
|
||||
}
|
||||
|
||||
export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOutput> = {
|
||||
id: 'function.execute',
|
||||
name: 'Function Execute',
|
||||
description: 'Execute code in a sandboxed environment',
|
||||
version: '1.0.0',
|
||||
|
||||
params: {
|
||||
code: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The code to execute',
|
||||
}
|
||||
},
|
||||
|
||||
request: {
|
||||
url: 'https://emkc.org/api/v2/piston/execute',
|
||||
method: 'POST',
|
||||
headers: () => ({
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}),
|
||||
body: (params) => {
|
||||
const codeContent = Array.isArray(params.code)
|
||||
? params.code.map(c => c.content).join('\n')
|
||||
: params.code;
|
||||
|
||||
return {
|
||||
language: 'js',
|
||||
version: '*',
|
||||
files: [{
|
||||
name: 'code.js',
|
||||
content: codeContent
|
||||
}],
|
||||
stdin: '',
|
||||
args: [],
|
||||
compile_timeout: 10000,
|
||||
run_timeout: 3000,
|
||||
compile_memory_limit: -1,
|
||||
run_memory_limit: -1
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
transformResponse: async (response) => {
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(result.message || 'Execution failed');
|
||||
}
|
||||
|
||||
if (result.run?.stderr) {
|
||||
throw new Error(result.run.stderr);
|
||||
}
|
||||
|
||||
const stdout = result.run?.stdout || '';
|
||||
|
||||
try {
|
||||
// Try parsing the output as JSON
|
||||
const parsed = JSON.parse(stdout);
|
||||
return { output: parsed };
|
||||
} catch {
|
||||
// If not JSON, wrap it in a JSON object
|
||||
return {
|
||||
output: {
|
||||
result: stdout
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
transformError: (error: any) => {
|
||||
return error.message || 'Code execution failed';
|
||||
},
|
||||
}
|
||||
+4
-1
@@ -6,6 +6,7 @@ import { chatTool as xaiChat } from './xai/chat';
|
||||
import { requestTool as httpRequest } from './http/request';
|
||||
import { contactsTool as hubspotContacts } from './hubspot/contacts';
|
||||
import { opportunitiesTool as salesforceOpportunities } from './salesforce/opportunities';
|
||||
import { functionExecuteTool as functionExecute } from './function/execute';
|
||||
|
||||
// Registry of all available tools
|
||||
export const tools: Record<string, ToolConfig> = {
|
||||
@@ -18,7 +19,9 @@ export const tools: Record<string, ToolConfig> = {
|
||||
'http.request': httpRequest,
|
||||
// CRM Tools
|
||||
'hubspot.contacts': hubspotContacts,
|
||||
'salesforce.opportunities': salesforceOpportunities
|
||||
'salesforce.opportunities': salesforceOpportunities,
|
||||
// Function Tools
|
||||
'function.execute': functionExecute,
|
||||
};
|
||||
|
||||
// Get a tool by its ID
|
||||
|
||||
Reference in New Issue
Block a user