chore: import upstream snapshot with attribution
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

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
+391
View File
@@ -0,0 +1,391 @@
/**
* @vitest-environment node
*
* Function Execute Tool Unit Tests
*
* This file contains unit tests for the Function Execute tool,
* which runs JavaScript code in a secure sandbox.
*/
import { ToolTester } from '@sim/testing/builders'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { functionExecuteTool } from '@/tools/function/execute'
describe('Function Execute Tool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let tester: ToolTester<any, any>
beforeEach(() => {
tester = new ToolTester(functionExecuteTool as any)
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000'
})
afterEach(() => {
tester.cleanup()
vi.resetAllMocks()
process.env.NEXT_PUBLIC_APP_URL = undefined
})
describe('Request Construction', () => {
it.concurrent('should set correct URL for code execution', () => {
expect(tester.getRequestUrl({})).toBe('/api/function/execute')
})
it.concurrent('should include correct headers for JSON payload', () => {
const headers = tester.getRequestHeaders({
code: 'return 42',
})
expect(headers['Content-Type']).toBe('application/json')
})
it.concurrent('should format single string code correctly', () => {
const body = tester.getRequestBody({
code: 'return 42',
envVars: {},
isCustomTool: false,
timeout: 5000,
workflowId: undefined,
})
expect(body).toEqual({
code: 'return 42',
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
contextVariables: {},
isCustomTool: false,
language: 'javascript',
outputFormat: undefined,
outputMimeType: undefined,
overwriteFileId: undefined,
outputPath: undefined,
outputSandboxPath: undefined,
outputTable: undefined,
title: undefined,
timeout: 5000,
workflowId: undefined,
executionId: undefined,
workspaceId: undefined,
userId: undefined,
})
})
it.concurrent('should format array of code blocks correctly', () => {
const body = tester.getRequestBody({
code: [
{ content: 'const x = 40;', id: 'block1' },
{ content: 'const y = 2;', id: 'block2' },
{ content: 'return x + y;', id: 'block3' },
],
envVars: {},
isCustomTool: false,
timeout: 10000,
workflowId: undefined,
})
expect(body).toEqual({
code: 'const x = 40;\nconst y = 2;\nreturn x + y;',
timeout: 10000,
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
contextVariables: {},
isCustomTool: false,
language: 'javascript',
outputFormat: undefined,
outputMimeType: undefined,
overwriteFileId: undefined,
outputPath: undefined,
outputSandboxPath: undefined,
outputTable: undefined,
title: undefined,
workflowId: undefined,
executionId: undefined,
workspaceId: undefined,
userId: undefined,
})
})
it.concurrent('should use default timeout and memory limit when not provided', () => {
const body = tester.getRequestBody({
code: 'return 42',
})
expect(body).toEqual({
code: 'return 42',
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
contextVariables: {},
isCustomTool: false,
language: 'javascript',
outputFormat: undefined,
outputMimeType: undefined,
overwriteFileId: undefined,
outputPath: undefined,
outputSandboxPath: undefined,
outputTable: undefined,
title: undefined,
workflowId: undefined,
executionId: undefined,
workspaceId: undefined,
userId: undefined,
})
})
})
describe('Response Handling', () => {
it.concurrent('should process successful code execution response', async () => {
tester.setup({
success: true,
output: {
result: 42,
stdout: 'console.log output',
},
})
const result = await tester.execute({
code: 'console.log("output"); return 42;',
})
expect(result.success).toBe(true)
expect(result.output.result).toBe(42)
expect(result.output.stdout).toBe('console.log output')
})
it.concurrent('should handle execution errors', async () => {
tester.setup(
{
success: false,
error: 'Syntax error in code',
},
{ ok: false, status: 400 }
)
const result = await tester.execute({
code: 'invalid javascript code!!!',
})
expect(result.success).toBe(false)
expect(result.error).toBeDefined()
expect(result.error).toBe('Syntax error in code')
})
it.concurrent('should handle timeout errors', async () => {
tester.setup(
{
success: false,
error: 'Code execution timed out',
},
{ ok: false, status: 408 }
)
const result = await tester.execute({
code: 'while(true) {}',
timeout: 1000,
})
expect(result.success).toBe(false)
expect(result.error).toBe('Code execution timed out')
})
})
describe('Error Handling', () => {
it.concurrent('should handle syntax error with line content', async () => {
tester.setup(
{
success: false,
error:
'Syntax Error: Line 3: `description: "This has a missing closing quote` - Invalid or unexpected token (Check for missing quotes, brackets, or semicolons)',
output: {
result: null,
stdout: '',
executionTime: 5,
},
debug: {
line: 3,
column: undefined,
errorType: 'SyntaxError',
lineContent: 'description: "This has a missing closing quote',
stack: 'user-function.js:5\n description: "This has a missing closing quote\n...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = {\n name: "test",\n description: "This has a missing closing quote\n};\nreturn obj;',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Syntax Error')
expect(result.error).toContain('Line 3')
expect(result.error).toContain('description: "This has a missing closing quote')
expect(result.error).toContain('Invalid or unexpected token')
expect(result.error).toContain('(Check for missing quotes, brackets, or semicolons)')
})
it.concurrent('should handle runtime error with line and column', async () => {
tester.setup(
{
success: false,
error:
"Type Error: Line 2:16: `return obj.someMethod();` - Cannot read properties of null (reading 'someMethod')",
output: {
result: null,
stdout: 'ERROR: {}\n',
executionTime: 12,
},
debug: {
line: 2,
column: 16,
errorType: 'TypeError',
lineContent: 'return obj.someMethod();',
stack: 'TypeError: Cannot read properties of null...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = null;\nreturn obj.someMethod();',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Type Error')
expect(result.error).toContain('Line 2:16')
expect(result.error).toContain('return obj.someMethod();')
expect(result.error).toContain('Cannot read properties of null')
})
it.concurrent('should handle error information in tool response', async () => {
tester.setup(
{
success: false,
error: 'Reference Error: Line 1: `return undefinedVar` - undefinedVar is not defined',
output: {
result: null,
stdout: '',
executionTime: 3,
},
debug: {
line: 1,
column: 7,
errorType: 'ReferenceError',
lineContent: 'return undefinedVar',
stack: 'ReferenceError: undefinedVar is not defined...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'return undefinedVar',
})
expect(result.success).toBe(false)
expect(result.error).toBe(
'Reference Error: Line 1: `return undefinedVar` - undefinedVar is not defined'
)
})
it.concurrent('should preserve debug information in error object', async () => {
tester.setup(
{
success: false,
error: 'Syntax Error: Line 2 - Invalid syntax',
debug: {
line: 2,
column: 5,
errorType: 'SyntaxError',
lineContent: 'invalid syntax here',
stack: 'SyntaxError: Invalid syntax...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'valid line\ninvalid syntax here',
})
expect(result.success).toBe(false)
expect(result.error).toBe('Syntax Error: Line 2 - Invalid syntax')
})
it.concurrent('should handle enhanced error without line information', async () => {
tester.setup(
{
success: false,
error: 'Generic error message',
debug: {
errorType: 'Error',
stack: 'Error: Generic error message...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'return "test";',
})
expect(result.success).toBe(false)
expect(result.error).toBe('Generic error message')
})
it.concurrent('should provide line-specific error message when available', async () => {
tester.setup(
{
success: false,
error:
'Type Error: Line 5:20: `obj.nonExistentMethod()` - obj.nonExistentMethod is not a function',
debug: {
line: 5,
column: 20,
errorType: 'TypeError',
lineContent: 'obj.nonExistentMethod()',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = {};\nobj.nonExistentMethod();',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Line 5:20')
expect(result.error).toContain('obj.nonExistentMethod()')
})
})
describe('Edge Cases', () => {
it.concurrent('should handle empty code input', async () => {
await tester.execute({
code: '',
})
const body = tester.getRequestBody({ code: '' }) as { code: string }
expect(body.code).toBe('')
})
it.concurrent('should handle extremely short timeout', async () => {
const body = tester.getRequestBody({
code: 'return 42',
timeout: 1,
}) as { timeout: number }
expect(body.timeout).toBe(1)
})
})
})
+208
View File
@@ -0,0 +1,208 @@
import {
normalizeRecord,
normalizeRecordMap,
normalizeStringRecord,
normalizeWorkflowVariables,
} from '@/lib/core/utils/records'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
import type { CodeExecutionInput, CodeExecutionOutput } from '@/tools/function/types'
import type { ToolConfig } from '@/tools/types'
export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOutput> = {
id: 'function_execute',
name: 'Function Execute',
description:
'Execute JavaScript, Python, or shell scripts in a secure sandbox. For JS: fetch() is available, code runs in async IIFE wrapper. For shell: workspace env vars available as $VAR_NAME, pre-installed CLI tools (jq, curl, awscli, psql, gh, etc.). Use outputPath/outputTable to persist returned data, or outputSandboxPath + outputPath to export a file created inside the sandbox into the workspace.',
version: '1.0.0',
params: {
code: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description:
'Raw JavaScript statements (NOT a function). Code is auto-wrapped in async context. MUST use fetch() for HTTP (NOT xhr/axios/request libs). Write like: await fetch(url) then return result. NO import/require statements.',
},
language: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Language to execute (javascript, python, or shell)',
default: DEFAULT_CODE_LANGUAGE,
},
timeout: {
type: 'number',
required: false,
visibility: 'hidden',
description: 'Execution timeout in milliseconds',
default: DEFAULT_EXECUTION_TIMEOUT_MS,
},
title: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'Short user-visible label for this execution.',
},
outputPath: {
type: 'string',
required: false,
visibility: 'hidden',
description:
'Write the tool result back to a workspace file, e.g. "files/result.json" or "files/report.csv". Use for text/JSON/CSV/markdown/html outputs.',
},
outputFormat: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'Optional format override for outputPath (json, csv, txt, md, html).',
},
outputTable: {
type: 'string',
required: false,
visibility: 'hidden',
description:
'Overwrite a workspace table with the code result. The code must return an array of objects.',
},
outputSandboxPath: {
type: 'string',
required: false,
visibility: 'hidden',
description:
'Export a file created inside the sandbox to the workspace. Provide the sandbox file path here and also set outputPath to the workspace destination.',
},
outputMimeType: {
type: 'string',
required: false,
visibility: 'hidden',
description:
'MIME type for the exported file. Required for binary files (e.g. "image/png", "application/pdf"). If omitted, inferred from outputPath extension for text formats.',
},
overwriteFileId: {
type: 'string',
required: false,
visibility: 'hidden',
description:
'Overwrite this existing workspace file ID instead of creating a duplicate output file.',
},
envVars: {
type: 'object',
required: false,
visibility: 'hidden',
description: 'Environment variables to make available during execution',
default: {},
},
blockData: {
type: 'object',
required: false,
visibility: 'hidden',
description: 'Block output data for variable resolution',
default: {},
},
blockNameMapping: {
type: 'object',
required: false,
visibility: 'hidden',
description: 'Mapping of block names to block IDs',
default: {},
},
blockOutputSchemas: {
type: 'object',
required: false,
visibility: 'hidden',
description: 'Mapping of block IDs to their output schemas for validation',
default: {},
},
workflowVariables: {
type: 'object',
required: false,
visibility: 'hidden',
description: 'Workflow variables for <variable.name> resolution',
default: {},
},
},
request: {
url: '/api/function/execute',
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
}),
body: (params: CodeExecutionInput) => {
const codeContent = Array.isArray(params.code)
? params.code.map((c: { content: string }) => c.content).join('\n')
: params.code
const body: Record<string, unknown> = {
code: codeContent,
sourceCode: params.sourceCode,
language: params.language || DEFAULT_CODE_LANGUAGE,
timeout: params.timeout || DEFAULT_EXECUTION_TIMEOUT_MS,
title: params.title,
outputPath: params.outputPath,
outputFormat: params.outputFormat,
outputTable: params.outputTable,
outputSandboxPath: params.outputSandboxPath,
outputMimeType: params.outputMimeType,
overwriteFileId: params.overwriteFileId,
inputs: params.inputs,
outputs: params.outputs,
envVars: normalizeStringRecord(params.envVars),
workflowVariables: normalizeWorkflowVariables(params.workflowVariables),
blockData: normalizeRecord(params.blockData),
blockNameMapping: normalizeStringRecord(params.blockNameMapping),
blockOutputSchemas: normalizeRecordMap(params.blockOutputSchemas),
contextVariables: normalizeRecord(params.contextVariables),
workflowId: params._context?.workflowId,
executionId: params._context?.executionId,
largeValueExecutionIds: params._context?.largeValueExecutionIds,
largeValueKeys: params._context?.largeValueKeys,
fileKeys: params._context?.fileKeys,
allowLargeValueWorkflowScope: params._context?.allowLargeValueWorkflowScope,
userId: params._context?.userId,
workspaceId: params._context?.workspaceId,
isCustomTool: params.isCustomTool || false,
}
if (params._sandboxFiles) {
body._sandboxFiles = params._sandboxFiles
}
return body
},
},
transformResponse: async (response: Response): Promise<CodeExecutionOutput> => {
const result = await response.json()
if (!result.success) {
return {
success: false,
output: {
result: null,
stdout: result.output?.stdout || '',
},
error: result.error,
resources: result.resources,
largeValueKeys: result.largeValueKeys,
fileKeys: result.fileKeys,
}
}
return {
success: true,
output: {
result: result.output.result,
stdout: result.output.stdout,
},
resources: result.resources,
largeValueKeys: result.largeValueKeys,
fileKeys: result.fileKeys,
}
},
outputs: {
result: { type: 'string', description: 'The result of the code execution' },
stdout: { type: 'string', description: 'The standard output of the code execution' },
},
}
+3
View File
@@ -0,0 +1,3 @@
import { functionExecuteTool } from '@/tools/function/execute'
export { functionExecuteTool }
+67
View File
@@ -0,0 +1,67 @@
import type { CodeLanguage } from '@/lib/execution/languages'
import type { ToolResponse } from '@/tools/types'
export interface CodeExecutionInput {
code: Array<{ content: string; id: string }> | string
/** Original user-authored code used for error display after execution-time reference resolution. */
sourceCode?: string
language?: CodeLanguage
useLocalVM?: boolean
/**
* Workflow Function blocks pass milliseconds. Copilot/Mothership tool calls pass seconds
* and are converted at the request boundary.
*/
timeout?: number
memoryLimit?: number
title?: string
outputPath?: string
outputFormat?: 'json' | 'csv' | 'txt' | 'md' | 'html'
outputTable?: string
outputSandboxPath?: string
outputMimeType?: string
overwriteFileId?: string
inputs?: {
files?: Array<{ path: string; sandboxPath?: string }>
directories?: Array<{ path: string; sandboxPath?: string }>
tables?: Array<{ path?: string; tableId?: string; sandboxPath?: string }>
}
outputs?: {
files?: Array<{
path: string
mode: 'create' | 'overwrite'
sandboxPath?: string
format?: 'json' | 'csv' | 'txt' | 'md' | 'html'
mimeType?: string
}>
}
envVars?: Record<string, string>
workflowVariables?: Record<string, unknown>
blockData?: Record<string, unknown>
blockNameMapping?: Record<string, string>
blockOutputSchemas?: Record<string, Record<string, unknown>>
/** Pre-resolved block output variables from the executor, injected as VM globals. */
contextVariables?: Record<string, unknown>
_context?: {
workflowId?: string
executionId?: string
largeValueExecutionIds?: string[]
largeValueKeys?: string[]
fileKeys?: string[]
allowLargeValueWorkflowScope?: boolean
userId?: string
workspaceId?: string
copilotToolExecution?: boolean
}
isCustomTool?: boolean
_sandboxFiles?: Array<
| { type?: 'content'; path: string; content: string; encoding?: 'base64' }
| { type: 'url'; path: string; url: string }
>
}
export interface CodeExecutionOutput extends ToolResponse {
output: {
result: any
stdout: string
}
}