6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
157 lines
4.7 KiB
TypeScript
157 lines
4.7 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { chat } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { and, eq, isNull } from 'drizzle-orm'
|
|
import type { NextRequest } from 'next/server'
|
|
import { createChatContract } from '@/lib/api/contracts/chats'
|
|
import { getValidationErrorMessage, parseRequest } from '@/lib/api/server'
|
|
import { getSession } from '@/lib/auth'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { performChatDeploy } from '@/lib/workflows/orchestration'
|
|
import { checkWorkflowAccessForChatCreation } from '@/app/api/chat/utils'
|
|
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
|
|
import {
|
|
ChatDeployAuthNotAllowedError,
|
|
validateChatDeployAuth,
|
|
} from '@/ee/access-control/utils/permission-check'
|
|
|
|
const logger = createLogger('ChatAPI')
|
|
|
|
export const GET = withRouteHandler(async (_request: NextRequest) => {
|
|
try {
|
|
const session = await getSession()
|
|
|
|
if (!session) {
|
|
return createErrorResponse('Unauthorized', 401)
|
|
}
|
|
|
|
// Get the user's chat deployments
|
|
const deployments = await db
|
|
.select()
|
|
.from(chat)
|
|
.where(and(eq(chat.userId, session.user.id), isNull(chat.archivedAt)))
|
|
|
|
return createSuccessResponse({
|
|
deployments: deployments.map((deployment) => ({
|
|
...deployment,
|
|
includeToolCalls: deployment.includeToolCalls ?? false,
|
|
})),
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error fetching chat deployments:', error)
|
|
return createErrorResponse(getErrorMessage(error, 'Failed to fetch chat deployments'), 500)
|
|
}
|
|
})
|
|
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
try {
|
|
const session = await getSession()
|
|
|
|
if (!session) {
|
|
return createErrorResponse('Unauthorized', 401)
|
|
}
|
|
|
|
const parsed = await parseRequest(
|
|
createChatContract,
|
|
request,
|
|
{},
|
|
{
|
|
validationErrorResponse: (error) =>
|
|
createErrorResponse(getValidationErrorMessage(error), 400, 'VALIDATION_ERROR'),
|
|
}
|
|
)
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const {
|
|
workflowId,
|
|
identifier,
|
|
title,
|
|
description = '',
|
|
customizations,
|
|
authType = 'public',
|
|
password,
|
|
allowedEmails = [],
|
|
outputConfigs = [],
|
|
includeThinking = false,
|
|
includeToolCalls = false,
|
|
} = parsed.data.body
|
|
|
|
if (authType === 'password' && !password) {
|
|
return createErrorResponse('Password is required when using password protection', 400)
|
|
}
|
|
|
|
if (authType === 'email' && (!Array.isArray(allowedEmails) || allowedEmails.length === 0)) {
|
|
return createErrorResponse(
|
|
'At least one email or domain is required when using email access control',
|
|
400
|
|
)
|
|
}
|
|
|
|
if (authType === 'sso' && (!Array.isArray(allowedEmails) || allowedEmails.length === 0)) {
|
|
return createErrorResponse(
|
|
'At least one email or domain is required when using SSO access control',
|
|
400
|
|
)
|
|
}
|
|
|
|
const [existingIdentifier, { hasAccess, workflow: workflowRecord }] = await Promise.all([
|
|
db
|
|
.select()
|
|
.from(chat)
|
|
.where(and(eq(chat.identifier, identifier), isNull(chat.archivedAt)))
|
|
.limit(1),
|
|
checkWorkflowAccessForChatCreation(workflowId, session.user.id),
|
|
])
|
|
|
|
if (existingIdentifier.length > 0) {
|
|
return createErrorResponse('Identifier already in use', 400)
|
|
}
|
|
|
|
if (!hasAccess || !workflowRecord) {
|
|
return createErrorResponse('Workflow not found or access denied', 404)
|
|
}
|
|
|
|
if (workflowRecord.workspaceId) {
|
|
try {
|
|
await validateChatDeployAuth(session.user.id, workflowRecord.workspaceId, authType)
|
|
} catch (error) {
|
|
if (error instanceof ChatDeployAuthNotAllowedError) {
|
|
return createErrorResponse(error.message, 403)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
const result = await performChatDeploy({
|
|
workflowId,
|
|
userId: session.user.id,
|
|
identifier,
|
|
title,
|
|
description,
|
|
customizations,
|
|
authType,
|
|
password,
|
|
allowedEmails,
|
|
outputConfigs,
|
|
includeThinking,
|
|
includeToolCalls,
|
|
workspaceId: workflowRecord.workspaceId,
|
|
})
|
|
|
|
if (!result.success) {
|
|
return createErrorResponse(result.error || 'Failed to deploy chat', 500)
|
|
}
|
|
|
|
return createSuccessResponse({
|
|
id: result.chatId,
|
|
chatId: result.chatId,
|
|
chatUrl: result.chatUrl,
|
|
message: 'Chat deployment created successfully',
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error creating chat deployment:', error)
|
|
return createErrorResponse(getErrorMessage(error, 'Failed to create chat deployment'), 500)
|
|
}
|
|
})
|