55 */
66
77import { db } from '@sim/db'
8- import { a2aAgent , workflow , workspace } from '@sim/db/schema'
8+ import { a2aAgent , workflow } from '@sim/db/schema'
99import { createLogger } from '@sim/logger'
1010import { and , eq , sql } from 'drizzle-orm'
1111import { type NextRequest , NextResponse } from 'next/server'
@@ -16,6 +16,7 @@ import { sanitizeAgentName } from '@/lib/a2a/utils'
1616import { checkHybridAuth } from '@/lib/auth/hybrid'
1717import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
1818import { hasValidStartBlockInState } from '@/lib/workflows/triggers/trigger-utils'
19+ import { getWorkspaceById } from '@/lib/workspaces/permissions/utils'
1920
2021const logger = createLogger ( 'A2AAgentsAPI' )
2122
@@ -38,18 +39,11 @@ export async function GET(request: NextRequest) {
3839 return NextResponse . json ( { error : 'workspaceId is required' } , { status : 400 } )
3940 }
4041
41- // Verify workspace access
42- const [ ws ] = await db
43- . select ( { id : workspace . id } )
44- . from ( workspace )
45- . where ( eq ( workspace . id , workspaceId ) )
46- . limit ( 1 )
47-
42+ const ws = await getWorkspaceById ( workspaceId )
4843 if ( ! ws ) {
4944 return NextResponse . json ( { error : 'Workspace not found' } , { status : 404 } )
5045 }
5146
52- // Get agents with workflow info
5347 const agents = await db
5448 . select ( {
5549 id : a2aAgent . id ,
@@ -108,7 +102,6 @@ export async function POST(request: NextRequest) {
108102 )
109103 }
110104
111- // Verify workflow exists and belongs to workspace
112105 const [ wf ] = await db
113106 . select ( {
114107 id : workflow . id ,
@@ -128,7 +121,6 @@ export async function POST(request: NextRequest) {
128121 )
129122 }
130123
131- // Check if agent already exists for this workflow
132124 const [ existing ] = await db
133125 . select ( { id : a2aAgent . id } )
134126 . from ( a2aAgent )
@@ -142,7 +134,6 @@ export async function POST(request: NextRequest) {
142134 )
143135 }
144136
145- // Verify workflow has a start block
146137 const workflowData = await loadWorkflowFromNormalizedTables ( workflowId )
147138 if ( ! workflowData || ! hasValidStartBlockInState ( workflowData ) ) {
148139 return NextResponse . json (
@@ -151,10 +142,8 @@ export async function POST(request: NextRequest) {
151142 )
152143 }
153144
154- // Generate skills from workflow
155145 const skills = generateSkillsFromWorkflow ( name || wf . name , description || wf . description )
156146
157- // Create agent
158147 const agentId = uuidv4 ( )
159148 const agentName = name || sanitizeAgentName ( wf . name )
160149
0 commit comments