d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { NextResponse } from 'next/server'
|
|
import { validationErrorResponseFromError } from '@/lib/api/server'
|
|
import { getKnowledgeBaseById } from '@/lib/knowledge/service'
|
|
import type { KnowledgeBaseWithCounts } from '@/lib/knowledge/types'
|
|
import { type RateLimitResult, validateWorkspaceAccess } from '@/app/api/v1/middleware'
|
|
|
|
const logger = createLogger('V1KnowledgeAPI')
|
|
|
|
/**
|
|
* Fetches a KB by ID, validates it exists, belongs to the workspace,
|
|
* and the user has permission. Returns the KB or a NextResponse error.
|
|
*/
|
|
export async function resolveKnowledgeBase(
|
|
id: string,
|
|
workspaceId: string,
|
|
userId: string,
|
|
rateLimit: RateLimitResult,
|
|
level: 'read' | 'write' = 'read'
|
|
): Promise<{ kb: KnowledgeBaseWithCounts } | NextResponse> {
|
|
const accessError = await validateWorkspaceAccess(rateLimit, userId, workspaceId, level)
|
|
if (accessError) return accessError
|
|
|
|
const kb = await getKnowledgeBaseById(id)
|
|
if (!kb) {
|
|
return NextResponse.json({ error: 'Knowledge base not found' }, { status: 404 })
|
|
}
|
|
if (kb.workspaceId !== workspaceId) {
|
|
return NextResponse.json({ error: 'Knowledge base not found' }, { status: 404 })
|
|
}
|
|
return { kb }
|
|
}
|
|
|
|
/**
|
|
* Serializes a date value for JSON responses.
|
|
*/
|
|
export function serializeDate(date: Date | string | null | undefined): string | null {
|
|
if (date === null || date === undefined) return null
|
|
if (date instanceof Date) return date.toISOString()
|
|
return String(date)
|
|
}
|
|
|
|
/**
|
|
* Formats a KnowledgeBaseWithCounts into the API response shape.
|
|
*/
|
|
export function formatKnowledgeBase(kb: KnowledgeBaseWithCounts) {
|
|
return {
|
|
id: kb.id,
|
|
name: kb.name,
|
|
description: kb.description,
|
|
tokenCount: kb.tokenCount,
|
|
embeddingModel: kb.embeddingModel,
|
|
embeddingDimension: kb.embeddingDimension,
|
|
chunkingConfig: kb.chunkingConfig,
|
|
docCount: kb.docCount,
|
|
connectorTypes: kb.connectorTypes,
|
|
createdAt: serializeDate(kb.createdAt),
|
|
updatedAt: serializeDate(kb.updatedAt),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles unexpected errors with consistent logging and response.
|
|
*/
|
|
export function handleError(
|
|
requestId: string,
|
|
error: unknown,
|
|
defaultMessage: string
|
|
): NextResponse {
|
|
const validationResponse = validationErrorResponseFromError(error)
|
|
if (validationResponse) return validationResponse
|
|
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('does not have permission')) {
|
|
return NextResponse.json({ error: 'Access denied' }, { status: 403 })
|
|
}
|
|
|
|
const isStorageLimitError =
|
|
error.message.includes('Storage limit exceeded') || error.message.includes('storage limit')
|
|
if (isStorageLimitError) {
|
|
return NextResponse.json({ error: 'Storage limit exceeded' }, { status: 413 })
|
|
}
|
|
|
|
const isDuplicate = error.message.includes('already exists')
|
|
if (isDuplicate) {
|
|
return NextResponse.json({ error: 'Resource already exists' }, { status: 409 })
|
|
}
|
|
}
|
|
|
|
logger.error(`[${requestId}] ${defaultMessage}:`, error)
|
|
return NextResponse.json({ error: defaultMessage }, { status: 500 })
|
|
}
|