-
Notifications
You must be signed in to change notification settings - Fork 2
feat: AI sandbox admin UI with integrations management #693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
161f803
feat: AI sandbox admin UI with integrations management (#603)
2witstudios e566aae
fix: address PR #693 review feedback - security fixes, broken feature…
2witstudios e513727
fix: address PR #693 round 2 - config merge, falsy checks, error sani…
2witstudios 3cd23cb
fix: address PR #693 round 3 - a11y, shadowing, input clamping, retry…
2witstudios 2849316
fix: address PR #693 round 4 - retry revalidation, driveId assertion,…
2witstudios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
apps/web/src/app/api/drives/[driveId]/integrations/audit/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { authenticateRequestWithOptions, isAuthError } from '@/lib/auth'; | ||
| import { db, count, eq, and, integrationAuditLog } from '@pagespace/db'; | ||
| import { loggers } from '@pagespace/lib/server'; | ||
| import { getDriveAccess } from '@pagespace/lib/services/drive-service'; | ||
| import { isValidId } from '@pagespace/lib'; | ||
| import { | ||
| getAuditLogsByDrive, | ||
| getAuditLogsByConnection, | ||
| getAuditLogsBySuccess, | ||
| } from '@pagespace/lib/integrations'; | ||
|
|
||
| const AUTH_OPTIONS = { allow: ['session'] as const }; | ||
|
|
||
| /** | ||
| * GET /api/drives/[driveId]/integrations/audit | ||
| * List integration audit logs for a drive. | ||
| * Query params: limit, offset, connectionId, success | ||
| */ | ||
| export async function GET( | ||
| request: Request, | ||
| context: { params: Promise<{ driveId: string }> } | ||
| ) { | ||
| const { driveId } = await context.params; | ||
| const auth = await authenticateRequestWithOptions(request, AUTH_OPTIONS); | ||
| if (isAuthError(auth)) return auth.error; | ||
|
|
||
| try { | ||
| // Require OWNER or ADMIN | ||
| const access = await getDriveAccess(driveId, auth.userId); | ||
| if (!access.isOwner && !access.isAdmin) { | ||
| return NextResponse.json({ error: 'Admin access required' }, { status: 403 }); | ||
| } | ||
|
|
||
| const { searchParams } = new URL(request.url); | ||
| const limit = Math.min(parseInt(searchParams.get('limit') ?? '50', 10) || 50, 200); | ||
| const offset = parseInt(searchParams.get('offset') ?? '0', 10) || 0; | ||
| const connectionId = searchParams.get('connectionId'); | ||
| const successParam = searchParams.get('success'); | ||
|
|
||
| if (connectionId && !isValidId(connectionId)) { | ||
| return NextResponse.json({ error: 'Invalid connectionId format' }, { status: 400 }); | ||
| } | ||
|
|
||
| // Build where clause by accumulating conditions (always scoped to driveId) | ||
| const conditions = [eq(integrationAuditLog.driveId, driveId)]; | ||
| if (connectionId) { | ||
| conditions.push(eq(integrationAuditLog.connectionId, connectionId)); | ||
| } | ||
| if (successParam !== null) { | ||
| conditions.push(eq(integrationAuditLog.success, successParam === 'true')); | ||
| } | ||
| const whereClause = conditions.length === 1 ? conditions[0] : and(...conditions); | ||
|
|
||
| // Get total count and paginated logs in parallel | ||
| const [countResult, logs] = await Promise.all([ | ||
| db.select({ count: count() }).from(integrationAuditLog).where(whereClause), | ||
| connectionId | ||
| ? getAuditLogsByConnection(db, driveId, connectionId, { limit, offset }) | ||
| : successParam !== null | ||
| ? getAuditLogsBySuccess(db, driveId, successParam === 'true', { limit, offset }) | ||
| : getAuditLogsByDrive(db, driveId, { limit, offset }), | ||
| ]); | ||
|
|
||
| const total = Number(countResult[0]?.count ?? 0); | ||
|
|
||
| return NextResponse.json({ | ||
| logs: logs.map((log) => ({ | ||
| id: log.id, | ||
| driveId: log.driveId, | ||
| agentId: log.agentId, | ||
| userId: log.userId, | ||
| connectionId: log.connectionId, | ||
| toolName: log.toolName, | ||
| inputSummary: log.inputSummary, | ||
| success: log.success, | ||
| responseCode: log.responseCode, | ||
| errorType: log.errorType, | ||
| errorMessage: log.errorMessage, | ||
| durationMs: log.durationMs, | ||
| createdAt: log.createdAt, | ||
| })), | ||
| total, | ||
| }); | ||
| } catch (error) { | ||
| loggers.api.error('Error fetching integration audit logs:', error as Error); | ||
| return NextResponse.json({ error: 'Failed to fetch audit logs' }, { status: 500 }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
apps/web/src/app/api/integrations/providers/import-openapi/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { z } from 'zod'; | ||
| import { authenticateRequestWithOptions, isAuthError, verifyAdminAuth } from '@/lib/auth'; | ||
| import { loggers } from '@pagespace/lib/server'; | ||
| import { importOpenAPISpec } from '@pagespace/lib/integrations'; | ||
|
|
||
| const AUTH_OPTIONS = { allow: ['session'] as const, requireCSRF: true }; | ||
|
|
||
| const importSchema = z.object({ | ||
| spec: z.string().min(1, 'Spec content is required'), | ||
| selectedOperations: z.array(z.string()).optional(), | ||
| baseUrlOverride: z.string().url().optional(), | ||
| }); | ||
|
|
||
| /** | ||
| * POST /api/integrations/providers/import-openapi | ||
| * Parse an OpenAPI spec and return the generated provider config. | ||
| * Admin only. | ||
| */ | ||
| export async function POST(request: Request) { | ||
| const auth = await authenticateRequestWithOptions(request, AUTH_OPTIONS); | ||
| if (isAuthError(auth)) return auth.error; | ||
|
|
||
| const adminAuth = await verifyAdminAuth(request); | ||
| if (adminAuth instanceof NextResponse) { | ||
| return adminAuth; | ||
| } | ||
|
|
||
| try { | ||
| const body = await request.json(); | ||
| const validation = importSchema.safeParse(body); | ||
|
|
||
| if (!validation.success) { | ||
| return NextResponse.json( | ||
| { error: 'Validation failed', details: validation.error.flatten().fieldErrors }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { spec, selectedOperations, baseUrlOverride } = validation.data; | ||
|
|
||
| const result = await importOpenAPISpec(spec, { | ||
| selectedOperations, | ||
| baseUrlOverride, | ||
| }); | ||
|
|
||
| return NextResponse.json({ result }); | ||
| } catch (error) { | ||
| loggers.api.error('Error importing OpenAPI spec:', error as Error); | ||
| const message = error instanceof Error ? error.message : 'Failed to import OpenAPI spec'; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntegrationAuditLogsends bothconnectionIdandsuccesswhen both UI filters are selected, but this conditional always takes theconnectionIdpath and ignoressuccess, so users still see mixed success/failure rows and totals after choosing a status filter. The API should apply both predicates together when both query params are present to match the filtering controls.Useful? React with 👍 / 👎.