mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
* feat(analytics): add PostHog product analytics * fix(posthog): fix workspace group via URL params, type errors, and clean up comments * fix(posthog): address PR review - fix pre-tx event, auth_method, paused executions, enterprise cancellation, settings double-fire * chore(posthog): remove unused identifyServerPerson * fix(posthog): isolate processQueuedResumes errors, simplify settings posthog deps * fix(posthog): correctly classify SSO auth_method, fix phantom empty-string workspace groups * fix(posthog): remove usePostHog from memo'd TemplateCard, fix copilot chat phantom workspace group * fix(posthog): eliminate all remaining phantom empty-string workspace groups * fix(posthog): fix cancel route phantom group, remove redundant workspaceId shadow in catch block * fix(posthog): use ids.length for block_removed guard to handle container blocks with descendants * chore(posthog): remove unused removedBlockTypes variable * fix(posthog): remove phantom $set person properties from subscription events * fix(posthog): add passedKnowledgeBaseName to knowledge_base_opened effect deps * fix(posthog): capture currentWorkflowId synchronously before async import to avoid stale closure * fix(posthog): add typed captureEvent wrapper for React components, deduplicate copilot_panel_opened * feat(posthog): add task_created and task_message_sent events, remove copilot_panel_opened * feat(posthog): track task_renamed, task_deleted, task_marked_read, task_marked_unread * feat(analytics): expand posthog event coverage with source tracking and lifecycle events * fix(analytics): flush posthog events on SIGTERM before ECS task termination * fix(analytics): fix posthog in useCallback deps and fire block events for bulk operations
208 lines
7.5 KiB
TypeScript
208 lines
7.5 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { captureServerEvent } from '@/lib/posthog/server'
|
|
import { deleteSkill, listSkills, upsertSkills } from '@/lib/workflows/skills/operations'
|
|
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
|
|
|
const logger = createLogger('SkillsAPI')
|
|
|
|
const SkillSchema = z.object({
|
|
skills: z.array(
|
|
z.object({
|
|
id: z.string().optional(),
|
|
name: z
|
|
.string()
|
|
.min(1, 'Skill name is required')
|
|
.max(64)
|
|
.regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'Name must be kebab-case (e.g. my-skill)'),
|
|
description: z.string().min(1, 'Description is required').max(1024),
|
|
content: z.string().min(1, 'Content is required').max(50000, 'Content is too large'),
|
|
})
|
|
),
|
|
workspaceId: z.string().optional(),
|
|
source: z.enum(['settings', 'tool_input']).optional(),
|
|
})
|
|
|
|
/** GET - Fetch all skills for a workspace */
|
|
export async function GET(request: NextRequest) {
|
|
const requestId = generateRequestId()
|
|
const searchParams = request.nextUrl.searchParams
|
|
const workspaceId = searchParams.get('workspaceId')
|
|
|
|
try {
|
|
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(`[${requestId}] Unauthorized skills access attempt`)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = authResult.userId
|
|
|
|
if (!workspaceId) {
|
|
logger.warn(`[${requestId}] Missing workspaceId`)
|
|
return NextResponse.json({ error: 'workspaceId is required' }, { status: 400 })
|
|
}
|
|
|
|
const userPermission = await getUserEntityPermissions(userId, 'workspace', workspaceId)
|
|
if (!userPermission) {
|
|
logger.warn(`[${requestId}] User ${userId} does not have access to workspace ${workspaceId}`)
|
|
return NextResponse.json({ error: 'Access denied' }, { status: 403 })
|
|
}
|
|
|
|
const result = await listSkills({ workspaceId })
|
|
|
|
return NextResponse.json({ data: result }, { status: 200 })
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error fetching skills:`, error)
|
|
return NextResponse.json({ error: 'Failed to fetch skills' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
/** POST - Create or update skills */
|
|
export async function POST(req: NextRequest) {
|
|
const requestId = generateRequestId()
|
|
|
|
try {
|
|
const authResult = await checkSessionOrInternalAuth(req, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(`[${requestId}] Unauthorized skills update attempt`)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = authResult.userId
|
|
const body = await req.json()
|
|
|
|
try {
|
|
const { skills, workspaceId, source } = SkillSchema.parse(body)
|
|
|
|
if (!workspaceId) {
|
|
logger.warn(`[${requestId}] Missing workspaceId in request body`)
|
|
return NextResponse.json({ error: 'workspaceId is required' }, { status: 400 })
|
|
}
|
|
|
|
const userPermission = await getUserEntityPermissions(userId, 'workspace', workspaceId)
|
|
if (!userPermission || (userPermission !== 'admin' && userPermission !== 'write')) {
|
|
logger.warn(
|
|
`[${requestId}] User ${userId} does not have write permission for workspace ${workspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Write permission required' }, { status: 403 })
|
|
}
|
|
|
|
const resultSkills = await upsertSkills({
|
|
skills,
|
|
workspaceId,
|
|
userId,
|
|
requestId,
|
|
})
|
|
|
|
for (const skill of resultSkills) {
|
|
recordAudit({
|
|
workspaceId,
|
|
actorId: userId,
|
|
action: AuditAction.SKILL_CREATED,
|
|
resourceType: AuditResourceType.SKILL,
|
|
resourceId: skill.id,
|
|
resourceName: skill.name,
|
|
description: `Created/updated skill "${skill.name}"`,
|
|
})
|
|
captureServerEvent(
|
|
userId,
|
|
'skill_created',
|
|
{ skill_id: skill.id, skill_name: skill.name, workspace_id: workspaceId, source },
|
|
{ groups: { workspace: workspaceId } }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({ success: true, data: resultSkills })
|
|
} catch (validationError) {
|
|
if (validationError instanceof z.ZodError) {
|
|
logger.warn(`[${requestId}] Invalid skills data`, {
|
|
errors: validationError.errors,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request data', details: validationError.errors },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
if (validationError instanceof Error && validationError.message.includes('already exists')) {
|
|
return NextResponse.json({ error: validationError.message }, { status: 409 })
|
|
}
|
|
throw validationError
|
|
}
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error updating skills`, error)
|
|
return NextResponse.json({ error: 'Failed to update skills' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
/** DELETE - Delete a skill by ID */
|
|
export async function DELETE(request: NextRequest) {
|
|
const requestId = generateRequestId()
|
|
const searchParams = request.nextUrl.searchParams
|
|
const skillId = searchParams.get('id')
|
|
const workspaceId = searchParams.get('workspaceId')
|
|
const sourceParam = searchParams.get('source')
|
|
const source =
|
|
sourceParam === 'settings' || sourceParam === 'tool_input' ? sourceParam : undefined
|
|
|
|
try {
|
|
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(`[${requestId}] Unauthorized skill deletion attempt`)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = authResult.userId
|
|
|
|
if (!skillId) {
|
|
logger.warn(`[${requestId}] Missing skill ID for deletion`)
|
|
return NextResponse.json({ error: 'Skill ID is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!workspaceId) {
|
|
logger.warn(`[${requestId}] Missing workspaceId for deletion`)
|
|
return NextResponse.json({ error: 'workspaceId is required' }, { status: 400 })
|
|
}
|
|
|
|
const userPermission = await getUserEntityPermissions(userId, 'workspace', workspaceId)
|
|
if (!userPermission || (userPermission !== 'admin' && userPermission !== 'write')) {
|
|
logger.warn(
|
|
`[${requestId}] User ${userId} does not have write permission for workspace ${workspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Write permission required' }, { status: 403 })
|
|
}
|
|
|
|
const deleted = await deleteSkill({ skillId, workspaceId })
|
|
if (!deleted) {
|
|
logger.warn(`[${requestId}] Skill not found: ${skillId}`)
|
|
return NextResponse.json({ error: 'Skill not found' }, { status: 404 })
|
|
}
|
|
|
|
recordAudit({
|
|
workspaceId,
|
|
actorId: authResult.userId,
|
|
action: AuditAction.SKILL_DELETED,
|
|
resourceType: AuditResourceType.SKILL,
|
|
resourceId: skillId,
|
|
description: `Deleted skill`,
|
|
})
|
|
|
|
captureServerEvent(
|
|
userId,
|
|
'skill_deleted',
|
|
{ skill_id: skillId, workspace_id: workspaceId, source },
|
|
{ groups: { workspace: workspaceId } }
|
|
)
|
|
|
|
logger.info(`[${requestId}] Deleted skill: ${skillId}`)
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error deleting skill:`, error)
|
|
return NextResponse.json({ error: 'Failed to delete skill' }, { status: 500 })
|
|
}
|
|
}
|