Skip to content

Commit 0acdecd

Browse files
committed
validation endpoint: remove auth requirement. take an array of agentconfig objects
1 parent 9211159 commit 0acdecd

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

npm-app/src/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,7 @@ Go to https://www.codebuff.com/config for more information.`) +
15481548
}
15491549

15501550
await validateAgentConfigsIfAuthenticated(
1551-
this.user,
1552-
fileContext.agentTemplates,
1551+
Object.values(fileContext.agentTemplates),
15531552
)
15541553

15551554
this.webSocket.subscribe('init-response', (a) => {

npm-app/src/utils/agent-validation.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,25 @@ import { logger } from './logger'
66
import type { User } from '@codebuff/common/util/credentials'
77

88
/**
9-
* Validates agent configs using the REST API if user is authenticated
10-
* @param user The user object (null if not authenticated)
9+
* Validates agent configs using the REST API
1110
* @param agentConfigs The agent configs to validate
1211
*/
1312
export async function validateAgentConfigsIfAuthenticated(
14-
user: User | undefined,
15-
agentConfigs: Record<string, any> | undefined,
13+
agentConfigs: any[]
1614
): Promise<void> {
17-
// Only validate if user is authenticated and there are agent configs
18-
const agentConfigKeys = Object.keys(agentConfigs || {})
19-
20-
if (!user || !agentConfigs || agentConfigKeys.length === 0) {
15+
// Only validate if there are agent configs
16+
if (!agentConfigs || agentConfigs.length === 0) {
2117
return
2218
}
2319

2420
try {
21+
const headers: Record<string, string> = {
22+
'Content-Type': 'application/json',
23+
}
24+
2525
const response = await fetch(`${websiteUrl}/api/agents/validate`, {
2626
method: 'POST',
27-
headers: {
28-
'Content-Type': 'application/json',
29-
Cookie: `next-auth.session-token=${user.authToken}`,
30-
},
27+
headers,
3128
body: JSON.stringify({ agentConfigs }),
3229
})
3330

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
import { validateAgents } from '@codebuff/common/templates/agent-validation'
22
import { NextResponse } from 'next/server'
3-
import { getServerSession } from 'next-auth'
43

5-
import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'
64
import { logger } from '@/util/logger'
75

86
import type { NextRequest } from 'next/server'
97

108
interface ValidateAgentsRequest {
11-
agentConfigs: Record<string, any>
9+
agentConfigs: any[]
1210
}
1311

1412
export async function POST(request: NextRequest): Promise<NextResponse> {
1513
try {
16-
const session = await getServerSession(authOptions)
17-
if (!session?.user?.id) {
18-
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
19-
}
20-
2114
const body = (await request.json()) as ValidateAgentsRequest
2215
const { agentConfigs } = body
2316

24-
if (!agentConfigs || typeof agentConfigs !== 'object') {
17+
if (!agentConfigs || !Array.isArray(agentConfigs)) {
2518
return NextResponse.json(
2619
{
2720
error:
28-
'Invalid request: agentConfigs must be an object, with keys being the agent IDs and values of type AgentConfig',
21+
'Invalid request: agentConfigs must be an array of AgentConfig objects',
2922
},
3023
{ status: 400 }
3124
)
3225
}
3326

34-
const { templates: configs, validationErrors } = validateAgents(agentConfigs)
27+
const configsObject = Object.fromEntries(
28+
agentConfigs.map((config) => [config.id, config])
29+
)
30+
const { templates: configs, validationErrors } =
31+
validateAgents(configsObject)
3532

3633
if (validationErrors.length > 0) {
3734
logger.warn(
38-
{ errorCount: validationErrors.length, userId: session.user.id },
39-
'Agent config validation errors found',
35+
{ errorCount: validationErrors.length },
36+
'Agent config validation errors found'
4037
)
4138
}
4239

@@ -49,11 +46,11 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
4946
} catch (error) {
5047
logger.error(
5148
{ error: error instanceof Error ? error.message : String(error) },
52-
'Error validating agent configs',
49+
'Error validating agent configs'
5350
)
5451
return NextResponse.json(
5552
{ error: 'Internal server error' },
56-
{ status: 500 },
53+
{ status: 500 }
5754
)
5855
}
5956
}

0 commit comments

Comments
 (0)