MCPcopy Index your code
hub / github.com/simstudioai/sim / executeCode

Function executeCode

apps/sim/lib/execution/isolated-vm-worker.cjs:163–558  ·  view source on GitHub ↗

* Execute code in isolated-vm

(request, executionId)

Source from the content-addressed store, hash-verified

161 * Execute code in isolated-vm
162 */
163async function executeCode(request, executionId) {
164 const { code, params, envVars, contextVariables, timeoutMs, requestId } = request
165 const stdoutChunks = []
166 let stdoutLength = 0
167 let stdoutTruncated = false
168 let isolate = null
169
170 const appendStdout = (line) => {
171 if (stdoutTruncated || !line) return
172
173 const remaining = MAX_STDOUT_CHARS - stdoutLength
174 if (remaining <= 0) {
175 stdoutTruncated = true
176 stdoutChunks.push('[stdout truncated]\n')
177 return
178 }
179
180 if (line.length <= remaining) {
181 stdoutChunks.push(line)
182 stdoutLength += line.length
183 return
184 }
185
186 stdoutChunks.push(line.slice(0, remaining))
187 stdoutChunks.push('\n[stdout truncated]\n')
188 stdoutLength = MAX_STDOUT_CHARS
189 stdoutTruncated = true
190 }
191
192 let context = null
193 let bootstrapScript = null
194 let userScript = null
195 let logCallback = null
196 let errorCallback = null
197 let fetchCallback = null
198 let brokerCallback = null
199 const externalCopies = []
200
201 try {
202 isolate = new ivm.Isolate({ memoryLimit: 128 })
203 if (executionId !== undefined) activeIsolates.set(executionId, isolate)
204 context = await isolate.createContext()
205 const jail = context.global
206
207 await jail.set('global', jail.derefInto())
208
209 logCallback = new ivm.Callback((...args) => {
210 const message = args.map((arg) => stringifyLogValue(arg)).join(' ')
211 appendStdout(`${message}\n`)
212 })
213 await jail.set('__log', logCallback)
214
215 errorCallback = new ivm.Callback((...args) => {
216 const message = args.map((arg) => stringifyLogValue(arg)).join(' ')
217 appendStdout(`ERROR: ${message}\n`)
218 })
219 await jail.set('__error', errorCallback)
220

Callers 1

Calls 15

stringifyLogValueFunction · 0.85
appendStdoutFunction · 0.85
sendIpcRequestFunction · 0.85
convertToCompatibleErrorFunction · 0.85
joinMethod · 0.80
parseMethod · 0.80
releaseMethod · 0.80
setMethod · 0.65
deleteMethod · 0.65
getMethod · 0.65
resolveMethod · 0.65
runMethod · 0.65

Tested by

no test coverage detected