Skip to content

fix: preserve auth on edit regenerate#1634

Merged
arpitgupta1214 merged 1 commit into
testfrom
codex/fix-edit-regenerate-auth-headers
Apr 3, 2026
Merged

fix: preserve auth on edit regenerate#1634
arpitgupta1214 merged 1 commit into
testfrom
codex/fix-edit-regenerate-auth-headers

Conversation

@arpitgupta1214

@arpitgupta1214 arpitgupta1214 commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • wrap regenerate in useVercelChat with handleReload
  • pass fresh auth headers from getHeaders() on reload
  • pass chatRequestBody to keep regenerate request context consistent

Why

Editing a message deletes trailing messages correctly, but the following regenerate call could run without auth headers, causing unauthorized chat requests.

Summary by CodeRabbit

  • Bug Fixes
    • Improved chat regeneration reliability by ensuring proper header handling during reload operations.

@vercel

vercel Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
recoup-chat Ready Ready Preview Apr 2, 2026 11:51pm

Request Review

@coderabbitai

coderabbitai Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The useVercelChat hook's reload action now wraps the regenerate function with a memoized callback that asynchronously fetches fresh headers before regeneration, replacing direct exposure of the regenerate function.

Changes

Cohort / File(s) Summary
Header Refresh in Reload
hooks/useVercelChat.ts
Added useCallback wrapper handleReload that fetches headers via getHeaders() before invoking regenerate with the chat request body and computed headers. Updated hook's returned reload action to point to handleReload instead of directly exposing regenerate.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🔄 Headers fresh, like morning dew,
Before the chat regenerates anew,
A callback wrapped in memoized care,
Ensures the reload's always fair. ✨

🚥 Pre-merge checks | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Solid & Clean Code ⚠️ Warning PR introduces repeated 'await getHeaders() then execute' pattern without consolidating existing duplicates, violating DRY principle requirements. Extract repeated pattern into reusable utility like executeWithHeaders to eliminate duplication across handleSubmit, append, handleReload, and handleSendQueryMessages.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-edit-regenerate-auth-headers

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
hooks/useVercelChat.ts (1)

265-268: Clean implementation that follows the Single Responsibility Principle.

The handleReload callback correctly encapsulates the responsibility of fetching fresh auth headers before regeneration. The dependency array is complete, and the pattern is consistent with other message-sending functions in this hook (e.g., handleSubmit, append, handleSendQueryMessages).

One observation: getHeaders() can reject if the underlying getAccessToken() throws (per useChatTransport.ts:13-16). Currently, no try-catch wraps this call, which is consistent with other usages in this file. The error will propagate to the caller. If you want graceful degradation (e.g., showing a toast on auth failure), you could wrap this in a try-catch similar to:

💡 Optional: Add error handling for auth failures
 const handleReload = useCallback(async () => {
+  try {
     const headers = await getHeaders();
     await regenerate({ body: chatRequestBody, headers });
+  } catch (e) {
+    console.error("Failed to reload message", e);
+    toast.error("Failed to reload message. Please try again.");
+  }
 }, [getHeaders, regenerate, chatRequestBody]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hooks/useVercelChat.ts` around lines 265 - 268, Wrap the getHeaders() call
inside handleReload in a try-catch to gracefully handle auth failures (since
getHeaders can reject when getAccessToken throws); on catch, call the existing
UI error handling (e.g., showToast or setError) and return early instead of
letting the error propagate to the caller, then call regenerate({ body:
chatRequestBody, headers }) only on success—update the handleReload function to
reference getHeaders, regenerate, chatRequestBody, and getAccessToken
accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@hooks/useVercelChat.ts`:
- Around line 265-268: Wrap the getHeaders() call inside handleReload in a
try-catch to gracefully handle auth failures (since getHeaders can reject when
getAccessToken throws); on catch, call the existing UI error handling (e.g.,
showToast or setError) and return early instead of letting the error propagate
to the caller, then call regenerate({ body: chatRequestBody, headers }) only on
success—update the handleReload function to reference getHeaders, regenerate,
chatRequestBody, and getAccessToken accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 20084cd6-fcbb-46e3-8feb-c3284e435697

📥 Commits

Reviewing files that changed from the base of the PR and between 5ba372d and 2b07ffa.

📒 Files selected for processing (1)
  • hooks/useVercelChat.ts

@arpitgupta1214

Copy link
Copy Markdown
Collaborator Author

https://jam.dev/c/24e5e2ba-8721-4a6d-99ce-10ff31ce8e14
Editing messages succeeds

@sweetmantech

Copy link
Copy Markdown
Collaborator

screenshot

@sweetmantech

Copy link
Copy Markdown
Collaborator

Browser Test: Edit Message → Regenerate with Auth Headers

Tested on preview: recoup-chat-git-codex-fix-edit-regen-396b19-recoupable-ad724970.vercel.app

Test Steps

  1. Opened a new chat as AZALI artist
  2. Sent message: "What time is it?"
  3. Received response: "It's 2026-04-03 15:34:58 UTC." (via Get Local Time tool call)
  4. Clicked Edit on the user message
  5. Changed message to: "What day is it today?"
  6. Clicked Send to submit the edit

Result: PASS

The edit triggered the following sequence (verified via Chrome DevTools network inspection):

Step Request Status Auth Header
1. Delete trailing messages DELETE /api/chats/:id/messages/trailing?from_message_id=... 200 N/A (server-side)
2. Regenerate with edited message POST /api/chat 200 Bearer eyJ...

Key Verification

The regenerate request (POST /api/chat, reqid=570) was inspected and confirmed:

  • Authorization header present: Bearer eyJhbGciOiJFUzI1NiIs... (valid Privy JWT)
  • Request body includes "trigger":"regenerate-message" and the updated message text
  • Response: Successfully streamed back "Today is Friday, April 3, 2026." with 200 status

Before this fix

The regenerate call after editing could run without auth headers, causing unauthorized (401) chat requests. This is now fixed — handleReload passes fresh auth headers from getHeaders().

🤖 Generated with Claude Code using Chrome DevTools MCP

@arpitgupta1214 arpitgupta1214 merged commit 9aabc10 into test Apr 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants