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

Function start

apps/sim/app/api/mothership/execute/route.ts:242–348  ·  view source on GitHub ↗
(controller)

Source from the content-addressed store, hash-verified

240
241 const stream = new ReadableStream<Uint8Array>({
242 start(controller) {
243 let forwardedAssistantContent = ''
244 const send = (event: unknown) => {
245 if (!cancelled) {
246 controller.enqueue(encodeNdjson(event))
247 }
248 }
249
250 // Flush response headers promptly and keep long headless runs from
251 // looking idle to worker/proxy HTTP stacks.
252 send({ type: 'heartbeat', timestamp: new Date().toISOString() })
253 heartbeatId = setInterval(() => {
254 send({ type: 'heartbeat', timestamp: new Date().toISOString() })
255 }, MOTHERSHIP_EXECUTE_HEARTBEAT_INTERVAL_MS)
256
257 void (async () => {
258 try {
259 const result = await runLifecycle(async (event) => {
260 if (
261 event.type === MothershipStreamV1EventType.text &&
262 event.payload.channel === MothershipStreamV1TextChannel.assistant &&
263 event.payload.text
264 ) {
265 const text = event.payload.text
266 const content = text.startsWith(forwardedAssistantContent)
267 ? text.slice(forwardedAssistantContent.length)
268 : text
269 if (content) {
270 forwardedAssistantContent += content
271 send({ type: 'chunk', content })
272 }
273 }
274 })
275 allowExplicitAbort = false
276
277 if (lifecycleAbortController.signal.aborted) {
278 send({ type: 'error', error: 'Sim execution aborted' })
279 return
280 }
281
282 if (!result.success) {
283 logger.error(
284 messageId
285 ? `Mothership execute failed [messageId:${messageId}]`
286 : 'Mothership execute failed',
287 {
288 requestId,
289 workflowId,
290 executionId,
291 error: result.error,
292 errors: result.errors,
293 }
294 )
295 send({
296 type: 'error',
297 error: result.error || 'Sim execution failed',
298 content: result.content || '',
299 })

Callers

nothing calls this directly

Calls 7

runLifecycleFunction · 0.85
errorMethod · 0.80
infoMethod · 0.80
sendFunction · 0.70
isAbortErrorFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected