Files
sim/apps/sim/app/api/tools/tts/route.ts
Waleed fcdcaed00d fix(memory): add Bun.gc, stream cancellation, and unconsumed fetch drains (#3416)
* fix(memory): add Bun.gc, stream cancellation, and unconsumed fetch drains

* fix(memory): await reader.cancel() and use non-blocking Bun.gc

* fix(memory): update Bun.gc comment to match non-blocking call

* fix(memory): use response.body.cancel() instead of response.text() for drains

* fix(executor): flush TextDecoder after streaming loop for multi-byte chars

* fix(memory): use text() drain for SecureFetchResponse which lacks body property

* fix(chat): prevent premature isExecuting=false from killing chat stream

The onExecutionCompleted/Error/Cancelled callbacks were setting
isExecuting=false as soon as the server-side SSE stream completed.
For chat executions, this triggered a useEffect in chat.tsx that
cancelled the client-side stream reader before it finished consuming
buffered data — causing empty or partial chat responses.

Skip the isExecuting=false in these callbacks for chat executions
since the chat's own finally block handles cleanup after the stream
is fully consumed.

* fix(chat): remove useEffect anti-pattern that killed chat stream on state change

The effect reacted to isExecuting becoming false to clean up streams,
but this is an anti-pattern per React guidelines — using state changes
as a proxy for events. All cleanup cases are already handled by proper
event paths: stream done (processStreamingResponse), user cancel
(handleStopStreaming), component unmount (cleanup effect), and
abort/error (catch block).

* fix(servicenow): remove invalid string comparison on numeric offset param

* upgrade turborepo
2026-03-04 17:46:20 -08:00

146 lines
4.2 KiB
TypeScript

import { createLogger } from '@sim/logger'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { checkInternalAuth } from '@/lib/auth/hybrid'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/core/execution-limits'
import { validateAlphanumericId } from '@/lib/core/security/input-validation'
import { getBaseUrl } from '@/lib/core/utils/urls'
import { StorageService } from '@/lib/uploads'
const logger = createLogger('ProxyTTSAPI')
export async function POST(request: NextRequest) {
try {
const authResult = await checkInternalAuth(request, { requireWorkflowId: false })
if (!authResult.success) {
logger.error('Authentication failed for TTS proxy:', authResult.error)
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const body = await request.json()
const {
text,
voiceId,
apiKey,
modelId = 'eleven_monolingual_v1',
workspaceId,
workflowId,
executionId,
} = body
if (!text || !voiceId || !apiKey) {
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 })
}
const voiceIdValidation = validateAlphanumericId(voiceId, 'voiceId', 255)
if (!voiceIdValidation.isValid) {
logger.error(`Invalid voice ID: ${voiceIdValidation.error}`)
return NextResponse.json({ error: voiceIdValidation.error }, { status: 400 })
}
// Check if this is an execution context (from workflow tool execution)
const hasExecutionContext = workspaceId && workflowId && executionId
logger.info('Proxying TTS request for voice:', {
voiceId,
hasExecutionContext,
workspaceId,
workflowId,
executionId,
})
const endpoint = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`
const response = await fetch(endpoint, {
method: 'POST',
headers: {
Accept: 'audio/mpeg',
'Content-Type': 'application/json',
'xi-api-key': apiKey,
},
body: JSON.stringify({
text,
model_id: modelId,
}),
signal: AbortSignal.timeout(DEFAULT_EXECUTION_TIMEOUT_MS),
})
if (!response.ok) {
await response.body?.cancel().catch(() => {})
logger.error(`Failed to generate TTS: ${response.status} ${response.statusText}`)
return NextResponse.json(
{ error: `Failed to generate TTS: ${response.status} ${response.statusText}` },
{ status: response.status }
)
}
const audioBlob = await response.blob()
if (audioBlob.size === 0) {
logger.error('Empty audio received from ElevenLabs')
return NextResponse.json({ error: 'Empty audio received' }, { status: 422 })
}
const audioBuffer = Buffer.from(await audioBlob.arrayBuffer())
const timestamp = Date.now()
// Use execution storage for workflow tool calls, copilot for chat UI
if (hasExecutionContext) {
const { uploadExecutionFile } = await import('@/lib/uploads/contexts/execution')
const fileName = `tts-${timestamp}.mp3`
const userFile = await uploadExecutionFile(
{
workspaceId,
workflowId,
executionId,
},
audioBuffer,
fileName,
'audio/mpeg',
authResult.userId
)
logger.info('TTS audio stored in execution context:', {
executionId,
fileName,
size: userFile.size,
})
return NextResponse.json({
audioFile: userFile,
audioUrl: userFile.url,
})
}
// Chat UI usage - no execution context, use copilot context
const fileName = `tts-${timestamp}.mp3`
const fileInfo = await StorageService.uploadFile({
file: audioBuffer,
fileName,
contentType: 'audio/mpeg',
context: 'copilot',
})
const audioUrl = `${getBaseUrl()}${fileInfo.path}`
logger.info('TTS audio stored in copilot context (chat UI):', {
fileName,
size: fileInfo.size,
})
return NextResponse.json({
audioUrl,
size: fileInfo.size,
})
} catch (error) {
logger.error('Error proxying TTS:', error)
return NextResponse.json(
{
error: `Internal Server Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
{ status: 500 }
)
}
}