-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(copilot): add user feedback options #867
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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,149 @@ | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { | ||
| authenticateCopilotRequestSessionOnly, | ||
| createBadRequestResponse, | ||
| createInternalServerErrorResponse, | ||
| createRequestTracker, | ||
| createUnauthorizedResponse, | ||
| } from '@/lib/copilot/auth' | ||
| import { createLogger } from '@/lib/logs/console/logger' | ||
| import { db } from '@/db' | ||
| import { copilotFeedback } from '@/db/schema' | ||
|
|
||
| const logger = createLogger('CopilotFeedbackAPI') | ||
|
|
||
| // Schema for feedback submission | ||
| const FeedbackSchema = z.object({ | ||
| userQuery: z.string().min(1, 'User query is required'), | ||
| agentResponse: z.string().min(1, 'Agent response is required'), | ||
| isPositiveFeedback: z.boolean(), | ||
| feedback: z.string().optional(), | ||
| workflowYaml: z.string().optional(), // Optional workflow YAML when edit/build workflow tools were used | ||
| }) | ||
|
|
||
| /** | ||
| * POST /api/copilot/feedback | ||
| * Submit feedback for a copilot interaction | ||
| */ | ||
| export async function POST(req: NextRequest) { | ||
| const tracker = createRequestTracker() | ||
|
|
||
| try { | ||
| // Authenticate user using the same pattern as other copilot routes | ||
| const { userId: authenticatedUserId, isAuthenticated } = | ||
| await authenticateCopilotRequestSessionOnly() | ||
|
|
||
| if (!isAuthenticated || !authenticatedUserId) { | ||
| return createUnauthorizedResponse() | ||
| } | ||
|
|
||
| const body = await req.json() | ||
| const { userQuery, agentResponse, isPositiveFeedback, feedback, workflowYaml } = | ||
| FeedbackSchema.parse(body) | ||
|
|
||
| logger.info(`[${tracker.requestId}] Processing copilot feedback submission`, { | ||
| userId: authenticatedUserId, | ||
| isPositiveFeedback, | ||
| userQueryLength: userQuery.length, | ||
| agentResponseLength: agentResponse.length, | ||
| hasFeedback: !!feedback, | ||
| hasWorkflowYaml: !!workflowYaml, | ||
| workflowYamlLength: workflowYaml?.length || 0, | ||
| }) | ||
|
|
||
| // Insert feedback into the database | ||
| const [feedbackRecord] = await db | ||
| .insert(copilotFeedback) | ||
| .values({ | ||
| userQuery, | ||
| agentResponse, | ||
| isPositive: isPositiveFeedback, | ||
| feedback: feedback || null, | ||
| workflowYaml: workflowYaml || null, | ||
| }) | ||
Sg312 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .returning() | ||
|
|
||
| logger.info(`[${tracker.requestId}] Successfully saved copilot feedback`, { | ||
| feedbackId: feedbackRecord.feedbackId, | ||
| userId: authenticatedUserId, | ||
| isPositive: isPositiveFeedback, | ||
| duration: tracker.getDuration(), | ||
| }) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| feedbackId: feedbackRecord.feedbackId, | ||
| message: 'Feedback submitted successfully', | ||
| metadata: { | ||
| requestId: tracker.requestId, | ||
| duration: tracker.getDuration(), | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| const duration = tracker.getDuration() | ||
|
|
||
| if (error instanceof z.ZodError) { | ||
| logger.error(`[${tracker.requestId}] Validation error:`, { | ||
| duration, | ||
| errors: error.errors, | ||
| }) | ||
| return createBadRequestResponse( | ||
| `Invalid request data: ${error.errors.map((e) => e.message).join(', ')}` | ||
| ) | ||
| } | ||
|
|
||
| logger.error(`[${tracker.requestId}] Error submitting copilot feedback:`, { | ||
| duration, | ||
| error: error instanceof Error ? error.message : 'Unknown error', | ||
| stack: error instanceof Error ? error.stack : undefined, | ||
| }) | ||
|
|
||
| return createInternalServerErrorResponse('Failed to submit feedback') | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * GET /api/copilot/feedback | ||
| * Get all feedback records (for analytics) | ||
| */ | ||
| export async function GET(req: NextRequest) { | ||
| const tracker = createRequestTracker() | ||
|
|
||
| try { | ||
| // Authenticate user | ||
| const { userId: authenticatedUserId, isAuthenticated } = | ||
| await authenticateCopilotRequestSessionOnly() | ||
|
|
||
| if (!isAuthenticated || !authenticatedUserId) { | ||
| return createUnauthorizedResponse() | ||
| } | ||
|
|
||
| // Get all feedback records | ||
| const feedbackRecords = await db | ||
| .select({ | ||
| feedbackId: copilotFeedback.feedbackId, | ||
| userQuery: copilotFeedback.userQuery, | ||
| agentResponse: copilotFeedback.agentResponse, | ||
| isPositive: copilotFeedback.isPositive, | ||
| feedback: copilotFeedback.feedback, | ||
| workflowYaml: copilotFeedback.workflowYaml, | ||
Sg312 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| createdAt: copilotFeedback.createdAt, | ||
| }) | ||
| .from(copilotFeedback) | ||
|
|
||
| logger.info(`[${tracker.requestId}] Retrieved ${feedbackRecords.length} feedback records`) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| feedback: feedbackRecords, | ||
| metadata: { | ||
| requestId: tracker.requestId, | ||
| duration: tracker.getDuration(), | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| logger.error(`[${tracker.requestId}] Error retrieving copilot feedback:`, error) | ||
| return createInternalServerErrorResponse('Failed to retrieve feedback') | ||
| } | ||
| } | ||
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
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,14 @@ | ||
| CREATE TABLE "copilot_feedback" ( | ||
| "feedback_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
| "user_query" text NOT NULL, | ||
| "agent_response" text NOT NULL, | ||
| "is_positive" boolean NOT NULL, | ||
| "feedback" text, | ||
| "workflow_yaml" text, | ||
| "created_at" timestamp DEFAULT now() NOT NULL, | ||
| "updated_at" timestamp DEFAULT now() NOT NULL | ||
| ); | ||
| --> statement-breakpoint | ||
| ALTER TABLE "user_stats" ALTER COLUMN "current_usage_limit" SET DEFAULT '10';--> statement-breakpoint | ||
| CREATE INDEX "copilot_feedback_is_positive_idx" ON "copilot_feedback" USING btree ("is_positive");--> statement-breakpoint | ||
| CREATE INDEX "copilot_feedback_created_at_idx" ON "copilot_feedback" USING btree ("created_at"); |
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.
Uh oh!
There was an error while loading. Please reload this page.