mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
* improvement(processing): reduce redundant DB queries in execution preprocessing * improvement(processing): add defensive ID check for prefetched workflow record * improvement(processing): fix type safety in execution error logging Replace `as any` cast in non-SSE error path with proper `buildTraceSpans()` transformation, matching the SSE error path. Remove redundant `as any` cast in preprocessing.ts where the types already align. * improvement(processing): replace `as any` casts with proper types in logging - logger.ts: cast JSONB cost column to `WorkflowExecutionLog['cost']` instead of `any` in both `completeWorkflowExecution` and `getWorkflowExecution` - logger.ts: replace `(orgUsageBefore as any)?.toString?.()` with `String()` since COALESCE guarantees a non-null SQL aggregate value - logging-session.ts: cast JSONB cost to `AccumulatedCost` (the local interface) instead of `any` in `loadExistingCost` * improvement(processing): use exported HighestPrioritySubscription type in usage.ts Replace inline `Awaited<ReturnType<typeof getHighestPrioritySubscription>>` with the already-exported `HighestPrioritySubscription` type alias. * improvement(processing): replace remaining `as any` casts with proper types - preprocessing.ts: use exported `HighestPrioritySubscription` type instead of redeclaring via `Awaited<ReturnType<...>>` - deploy/route.ts, status/route.ts: cast `hasWorkflowChanged` args to `WorkflowState` instead of `any` (JSONB + object literal narrowing) - state/route.ts: type block sanitization and save with `BlockState` and `WorkflowState` instead of `any` - search-suggestions.ts: remove 8 unnecessary `as any` casts on `'date'` literal that already satisfies the `Suggestion['category']` union * fix(processing): prevent double-billing race in LoggingSession completion When executeWorkflowCore throws, its catch block fire-and-forgets safeCompleteWithError, then re-throws. The caller's catch block also fire-and-forgets safeCompleteWithError on the same LoggingSession. Both check this.completed (still false) before either's async DB write resolves, so both proceed to completeWorkflowExecution which uses additive SQL for billing — doubling the charged cost on every failed execution. Fix: add a synchronous `completing` flag set immediately before the async work begins. This blocks concurrent callers at the guard check. On failure, the flag is reset so the safe* fallback path (completeWithCostOnlyLog) can still attempt recovery. * fix(processing): unblock error responses and isolate run-count failures Remove unnecessary `await waitForCompletion()` from non-SSE and SSE error paths where no `markAsFailed()` follows — these were blocking error responses on log persistence for no reason. Wrap `updateWorkflowRunCounts` in its own try/catch so a run-count DB failure cannot prevent session completion, billing, and trace span persistence. * improvement(processing): remove dead setupExecutor method The method body was just a debug log with an `any` parameter — logging now works entirely through trace spans with no executor integration. * remove logger.debug * fix(processing): guard completionPromise as write-once (singleton promise) Prevent concurrent safeComplete* calls from overwriting completionPromise with a no-op. The guard now lives at the assignment site — if a completion is already in-flight, return its promise instead of starting a new one. This ensures waitForCompletion() always awaits the real work. * improvement(processing): remove empty else/catch blocks left by debug log cleanup * fix(processing): enforce waitForCompletion inside markAsFailed to prevent completion races Move waitForCompletion() into markAsFailed() so every call site is automatically safe against in-flight fire-and-forget completions. Remove the now-redundant external waitForCompletion() calls in route.ts. * fix(processing): reset completing flag on fallback failure, clean up empty catch - completeWithCostOnlyLog now resets this.completing = false when the fallback itself fails, preventing a permanently stuck session - Use _disconnectError in MCP test-connection to signal intentional ignore * fix(processing): restore disconnect error logging in MCP test-connection Revert unrelated debug log removal — this file isn't part of the processing improvements and the log aids connection leak detection. * fix(processing): address audit findings across branch - preprocessing.ts: use undefined (not null) for failed subscription fetch so getUserUsageLimit does a fresh lookup instead of silently falling back to free-tier limits - deployed/route.ts: log warning on loadDeployedWorkflowState failure instead of silently swallowing the error - schedule-execution.ts: remove dead successLog parameter and all call-site arguments left over from logger.debug cleanup - mcp/middleware.ts: drop unused error binding in empty catch - audit/log.ts, wand.ts: promote logger.debug to logger.warn in catch blocks where these are the only failure signal * revert: undo unnecessary subscription null→undefined change getHighestPrioritySubscription never throws (it catches internally and returns null), so the catch block in preprocessExecution is dead code. The null vs undefined distinction doesn't matter and the coercions added unnecessary complexity. * improvement(processing): remove dead try/catch around getHighestPrioritySubscription getHighestPrioritySubscription catches internally and returns null on error, so the wrapping try/catch was unreachable dead code. * improvement(processing): remove dead getSnapshotByHash method No longer called after createSnapshotWithDeduplication was refactored to use a single upsert instead of select-then-insert. ---------
409 lines
13 KiB
TypeScript
409 lines
13 KiB
TypeScript
import { db, workflow, workflowDeploymentVersion } from '@sim/db'
|
|
import { createLogger } from '@sim/logger'
|
|
import { and, desc, eq } from 'drizzle-orm'
|
|
import type { NextRequest } from 'next/server'
|
|
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { removeMcpToolsForWorkflow, syncMcpToolsForWorkflow } from '@/lib/mcp/workflow-mcp-sync'
|
|
import {
|
|
cleanupWebhooksForWorkflow,
|
|
restorePreviousVersionWebhooks,
|
|
saveTriggerWebhooksForDeploy,
|
|
} from '@/lib/webhooks/deploy'
|
|
import {
|
|
deployWorkflow,
|
|
loadWorkflowFromNormalizedTables,
|
|
undeployWorkflow,
|
|
} from '@/lib/workflows/persistence/utils'
|
|
import {
|
|
cleanupDeploymentVersion,
|
|
createSchedulesForDeploy,
|
|
validateWorkflowSchedules,
|
|
} from '@/lib/workflows/schedules'
|
|
import { validateWorkflowPermissions } from '@/lib/workflows/utils'
|
|
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
|
|
import type { WorkflowState } from '@/stores/workflows/workflow/types'
|
|
|
|
const logger = createLogger('WorkflowDeployAPI')
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
export const runtime = 'nodejs'
|
|
|
|
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const requestId = generateRequestId()
|
|
const { id } = await params
|
|
|
|
try {
|
|
const { error, workflow: workflowData } = await validateWorkflowPermissions(
|
|
id,
|
|
requestId,
|
|
'read'
|
|
)
|
|
if (error) {
|
|
return createErrorResponse(error.message, error.status)
|
|
}
|
|
|
|
if (!workflowData.isDeployed) {
|
|
logger.info(`[${requestId}] Workflow is not deployed: ${id}`)
|
|
return createSuccessResponse({
|
|
isDeployed: false,
|
|
deployedAt: null,
|
|
apiKey: null,
|
|
needsRedeployment: false,
|
|
isPublicApi: workflowData.isPublicApi ?? false,
|
|
})
|
|
}
|
|
|
|
let needsRedeployment = false
|
|
const [active] = await db
|
|
.select({ state: workflowDeploymentVersion.state })
|
|
.from(workflowDeploymentVersion)
|
|
.where(
|
|
and(
|
|
eq(workflowDeploymentVersion.workflowId, id),
|
|
eq(workflowDeploymentVersion.isActive, true)
|
|
)
|
|
)
|
|
.orderBy(desc(workflowDeploymentVersion.createdAt))
|
|
.limit(1)
|
|
|
|
if (active?.state) {
|
|
const { loadWorkflowFromNormalizedTables } = await import('@/lib/workflows/persistence/utils')
|
|
const normalizedData = await loadWorkflowFromNormalizedTables(id)
|
|
if (normalizedData) {
|
|
const [workflowRecord] = await db
|
|
.select({ variables: workflow.variables })
|
|
.from(workflow)
|
|
.where(eq(workflow.id, id))
|
|
.limit(1)
|
|
|
|
const currentState = {
|
|
blocks: normalizedData.blocks,
|
|
edges: normalizedData.edges,
|
|
loops: normalizedData.loops,
|
|
parallels: normalizedData.parallels,
|
|
variables: workflowRecord?.variables || {},
|
|
}
|
|
const { hasWorkflowChanged } = await import('@/lib/workflows/comparison')
|
|
needsRedeployment = hasWorkflowChanged(
|
|
currentState as WorkflowState,
|
|
active.state as WorkflowState
|
|
)
|
|
}
|
|
}
|
|
|
|
logger.info(`[${requestId}] Successfully retrieved deployment info: ${id}`)
|
|
|
|
const responseApiKeyInfo = workflowData.workspaceId ? 'Workspace API keys' : 'Personal API keys'
|
|
|
|
return createSuccessResponse({
|
|
apiKey: responseApiKeyInfo,
|
|
isDeployed: workflowData.isDeployed,
|
|
deployedAt: workflowData.deployedAt,
|
|
needsRedeployment,
|
|
isPublicApi: workflowData.isPublicApi ?? false,
|
|
})
|
|
} catch (error: any) {
|
|
logger.error(`[${requestId}] Error fetching deployment info: ${id}`, error)
|
|
return createErrorResponse(error.message || 'Failed to fetch deployment information', 500)
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const requestId = generateRequestId()
|
|
const { id } = await params
|
|
|
|
try {
|
|
const {
|
|
error,
|
|
session,
|
|
workflow: workflowData,
|
|
} = await validateWorkflowPermissions(id, requestId, 'admin')
|
|
if (error) {
|
|
return createErrorResponse(error.message, error.status)
|
|
}
|
|
|
|
const actorUserId: string | null = session?.user?.id ?? null
|
|
if (!actorUserId) {
|
|
logger.warn(`[${requestId}] Unable to resolve actor user for workflow deployment: ${id}`)
|
|
return createErrorResponse('Unable to determine deploying user', 400)
|
|
}
|
|
|
|
const normalizedData = await loadWorkflowFromNormalizedTables(id)
|
|
if (!normalizedData) {
|
|
return createErrorResponse('Failed to load workflow state', 500)
|
|
}
|
|
|
|
const scheduleValidation = validateWorkflowSchedules(normalizedData.blocks)
|
|
if (!scheduleValidation.isValid) {
|
|
logger.warn(
|
|
`[${requestId}] Schedule validation failed for workflow ${id}: ${scheduleValidation.error}`
|
|
)
|
|
return createErrorResponse(`Invalid schedule configuration: ${scheduleValidation.error}`, 400)
|
|
}
|
|
|
|
const [currentActiveVersion] = await db
|
|
.select({ id: workflowDeploymentVersion.id })
|
|
.from(workflowDeploymentVersion)
|
|
.where(
|
|
and(
|
|
eq(workflowDeploymentVersion.workflowId, id),
|
|
eq(workflowDeploymentVersion.isActive, true)
|
|
)
|
|
)
|
|
.limit(1)
|
|
const previousVersionId = currentActiveVersion?.id
|
|
|
|
const deployResult = await deployWorkflow({
|
|
workflowId: id,
|
|
deployedBy: actorUserId,
|
|
workflowName: workflowData!.name,
|
|
})
|
|
|
|
if (!deployResult.success) {
|
|
return createErrorResponse(deployResult.error || 'Failed to deploy workflow', 500)
|
|
}
|
|
|
|
const deployedAt = deployResult.deployedAt!
|
|
const deploymentVersionId = deployResult.deploymentVersionId
|
|
|
|
if (!deploymentVersionId) {
|
|
await undeployWorkflow({ workflowId: id })
|
|
return createErrorResponse('Failed to resolve deployment version', 500)
|
|
}
|
|
|
|
const triggerSaveResult = await saveTriggerWebhooksForDeploy({
|
|
request,
|
|
workflowId: id,
|
|
workflow: workflowData,
|
|
userId: actorUserId,
|
|
blocks: normalizedData.blocks,
|
|
requestId,
|
|
deploymentVersionId,
|
|
previousVersionId,
|
|
})
|
|
|
|
if (!triggerSaveResult.success) {
|
|
await cleanupDeploymentVersion({
|
|
workflowId: id,
|
|
workflow: workflowData as Record<string, unknown>,
|
|
requestId,
|
|
deploymentVersionId,
|
|
})
|
|
await undeployWorkflow({ workflowId: id })
|
|
return createErrorResponse(
|
|
triggerSaveResult.error?.message || 'Failed to save trigger configuration',
|
|
triggerSaveResult.error?.status || 500
|
|
)
|
|
}
|
|
|
|
let scheduleInfo: { scheduleId?: string; cronExpression?: string; nextRunAt?: Date } = {}
|
|
const scheduleResult = await createSchedulesForDeploy(
|
|
id,
|
|
normalizedData.blocks,
|
|
db,
|
|
deploymentVersionId
|
|
)
|
|
if (!scheduleResult.success) {
|
|
logger.error(
|
|
`[${requestId}] Failed to create schedule for workflow ${id}: ${scheduleResult.error}`
|
|
)
|
|
await cleanupDeploymentVersion({
|
|
workflowId: id,
|
|
workflow: workflowData as Record<string, unknown>,
|
|
requestId,
|
|
deploymentVersionId,
|
|
})
|
|
if (previousVersionId) {
|
|
await restorePreviousVersionWebhooks({
|
|
request,
|
|
workflow: workflowData as Record<string, unknown>,
|
|
userId: actorUserId,
|
|
previousVersionId,
|
|
requestId,
|
|
})
|
|
}
|
|
await undeployWorkflow({ workflowId: id })
|
|
return createErrorResponse(scheduleResult.error || 'Failed to create schedule', 500)
|
|
}
|
|
if (scheduleResult.scheduleId) {
|
|
scheduleInfo = {
|
|
scheduleId: scheduleResult.scheduleId,
|
|
cronExpression: scheduleResult.cronExpression,
|
|
nextRunAt: scheduleResult.nextRunAt,
|
|
}
|
|
logger.info(
|
|
`[${requestId}] Schedule created for workflow ${id}: ${scheduleResult.scheduleId}`
|
|
)
|
|
}
|
|
|
|
if (previousVersionId && previousVersionId !== deploymentVersionId) {
|
|
try {
|
|
logger.info(`[${requestId}] Cleaning up previous version ${previousVersionId} DB records`)
|
|
await cleanupDeploymentVersion({
|
|
workflowId: id,
|
|
workflow: workflowData as Record<string, unknown>,
|
|
requestId,
|
|
deploymentVersionId: previousVersionId,
|
|
skipExternalCleanup: true,
|
|
})
|
|
} catch (cleanupError) {
|
|
logger.error(
|
|
`[${requestId}] Failed to clean up previous version ${previousVersionId}`,
|
|
cleanupError
|
|
)
|
|
// Non-fatal - continue with success response
|
|
}
|
|
}
|
|
|
|
logger.info(`[${requestId}] Workflow deployed successfully: ${id}`)
|
|
|
|
// Sync MCP tools with the latest parameter schema
|
|
await syncMcpToolsForWorkflow({ workflowId: id, requestId, context: 'deploy' })
|
|
|
|
recordAudit({
|
|
workspaceId: workflowData?.workspaceId || null,
|
|
actorId: actorUserId,
|
|
actorName: session?.user?.name,
|
|
actorEmail: session?.user?.email,
|
|
action: AuditAction.WORKFLOW_DEPLOYED,
|
|
resourceType: AuditResourceType.WORKFLOW,
|
|
resourceId: id,
|
|
resourceName: workflowData?.name,
|
|
description: `Deployed workflow "${workflowData?.name || id}"`,
|
|
metadata: { version: deploymentVersionId },
|
|
request,
|
|
})
|
|
|
|
const responseApiKeyInfo = workflowData!.workspaceId
|
|
? 'Workspace API keys'
|
|
: 'Personal API keys'
|
|
|
|
return createSuccessResponse({
|
|
apiKey: responseApiKeyInfo,
|
|
isDeployed: true,
|
|
deployedAt,
|
|
schedule: scheduleInfo.scheduleId
|
|
? {
|
|
id: scheduleInfo.scheduleId,
|
|
cronExpression: scheduleInfo.cronExpression,
|
|
nextRunAt: scheduleInfo.nextRunAt,
|
|
}
|
|
: undefined,
|
|
warnings: triggerSaveResult.warnings,
|
|
})
|
|
} catch (error: any) {
|
|
logger.error(`[${requestId}] Error deploying workflow: ${id}`, {
|
|
error: error.message,
|
|
stack: error.stack,
|
|
name: error.name,
|
|
cause: error.cause,
|
|
fullError: error,
|
|
})
|
|
return createErrorResponse(error.message || 'Failed to deploy workflow', 500)
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const requestId = generateRequestId()
|
|
const { id } = await params
|
|
|
|
try {
|
|
const { error, session } = await validateWorkflowPermissions(id, requestId, 'admin')
|
|
if (error) {
|
|
return createErrorResponse(error.message, error.status)
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { isPublicApi } = body
|
|
|
|
if (typeof isPublicApi !== 'boolean') {
|
|
return createErrorResponse('Invalid request body: isPublicApi must be a boolean', 400)
|
|
}
|
|
|
|
if (isPublicApi) {
|
|
const { validatePublicApiAllowed, PublicApiNotAllowedError } = await import(
|
|
'@/ee/access-control/utils/permission-check'
|
|
)
|
|
try {
|
|
await validatePublicApiAllowed(session?.user?.id)
|
|
} catch (err) {
|
|
if (err instanceof PublicApiNotAllowedError) {
|
|
return createErrorResponse('Public API access is disabled', 403)
|
|
}
|
|
throw err
|
|
}
|
|
}
|
|
|
|
await db.update(workflow).set({ isPublicApi }).where(eq(workflow.id, id))
|
|
|
|
logger.info(`[${requestId}] Updated isPublicApi for workflow ${id} to ${isPublicApi}`)
|
|
|
|
return createSuccessResponse({ isPublicApi })
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Failed to update deployment settings'
|
|
logger.error(`[${requestId}] Error updating deployment settings: ${id}`, { error })
|
|
return createErrorResponse(message, 500)
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const requestId = generateRequestId()
|
|
const { id } = await params
|
|
|
|
try {
|
|
const {
|
|
error,
|
|
session,
|
|
workflow: workflowData,
|
|
} = await validateWorkflowPermissions(id, requestId, 'admin')
|
|
if (error) {
|
|
return createErrorResponse(error.message, error.status)
|
|
}
|
|
|
|
// Clean up external webhook subscriptions before undeploying
|
|
await cleanupWebhooksForWorkflow(id, workflowData as Record<string, unknown>, requestId)
|
|
|
|
const result = await undeployWorkflow({ workflowId: id })
|
|
if (!result.success) {
|
|
return createErrorResponse(result.error || 'Failed to undeploy workflow', 500)
|
|
}
|
|
|
|
await removeMcpToolsForWorkflow(id, requestId)
|
|
|
|
logger.info(`[${requestId}] Workflow undeployed successfully: ${id}`)
|
|
|
|
try {
|
|
const { PlatformEvents } = await import('@/lib/core/telemetry')
|
|
PlatformEvents.workflowUndeployed({ workflowId: id })
|
|
} catch (_e) {
|
|
// Silently fail
|
|
}
|
|
|
|
recordAudit({
|
|
workspaceId: workflowData?.workspaceId || null,
|
|
actorId: session!.user.id,
|
|
actorName: session?.user?.name,
|
|
actorEmail: session?.user?.email,
|
|
action: AuditAction.WORKFLOW_UNDEPLOYED,
|
|
resourceType: AuditResourceType.WORKFLOW,
|
|
resourceId: id,
|
|
resourceName: workflowData?.name,
|
|
description: `Undeployed workflow "${workflowData?.name || id}"`,
|
|
request,
|
|
})
|
|
|
|
return createSuccessResponse({
|
|
isDeployed: false,
|
|
deployedAt: null,
|
|
apiKey: null,
|
|
})
|
|
} catch (error: any) {
|
|
logger.error(`[${requestId}] Error undeploying workflow: ${id}`, error)
|
|
return createErrorResponse(error.message || 'Failed to undeploy workflow', 500)
|
|
}
|
|
}
|