Skip to content

Conversation

kubk
Copy link
Contributor

@kubk kubk commented Mar 4, 2025

Closes #1703

This PR enhances the EdgeChatAdapter by adding support for dynamic headers in API requests.

Motivation:

This change allows for dynamic generation of headers at request time, which is useful for:

  • Authentication tokens that need to be refreshed
  • Headers that depend on the current state of the application
  • Headers that require async operations to generate

Changes:

  • Added a new HeadersValue type to represent header values (either a Record or Headers object)
  • Enhanced the headers option in EdgeChatAdapterOptions to accept either:
  • A static headers object (as before)
  • A function that returns a Promise of headers
  • Updated the request handling to resolve dynamic headers when a function is provided
  • Added JSDoc comments to improve developer experience

Testing:

The implementation maintains backward compatibility with existing code while adding the new functionality.


Important

Add support for dynamic headers in EdgeChatAdapter by allowing headers to be a function returning a promise.

  • Behavior:
    • EdgeChatAdapterOptions.headers now accepts a function returning a Promise of headers, in addition to static headers.
    • run() in EdgeChatAdapter resolves dynamic headers if a function is provided.
  • Types:
    • Adds HeadersValue type for header values in EdgeChatAdapter.ts.
  • Documentation:
    • Adds JSDoc comments for headers in EdgeChatAdapterOptions.

This description was created by Ellipsis for c41ed25. It will automatically update as commits are pushed.

Copy link

vercel bot commented Mar 4, 2025

@kubk is attempting to deploy a commit to the assistant-ui Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

LGTM 👍

Copy link
Contributor

coderabbitai bot commented Mar 4, 2025

Walkthrough

The changes define a new type alias, HeadersValue, which can be a Record<string, string> or a Headers object. The EdgeChatAdapterOptions type is updated so the headers property now accepts either a HeadersValue or an asynchronous function returning a Promise of a HeadersValue, replacing its former definition limited to static values. Within the EdgeChatAdapter class, the run method now checks if this.options.headers is a function; if so, it awaits the function to retrieve the header data, otherwise, it uses the provided header value directly. These modifications allow the adapter to handle both statically defined headers and dynamically provided headers.

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/react/src/runtimes/edge/EdgeChatAdapter.ts

Oops! Something went wrong! :(

ESLint: 9.21.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 467d979 and c41ed25.

📒 Files selected for processing (1)
  • packages/react/src/runtimes/edge/EdgeChatAdapter.ts (3 hunks)
🔇 Additional comments (3)
packages/react/src/runtimes/edge/EdgeChatAdapter.ts (3)

38-38: Good abstraction for header values.

The new HeadersValue type alias is a clean way to handle the two possible formats for headers. This makes the codebase more maintainable and type-safe.


71-76: Well-documented headers property with improved flexibility.

The JSDoc comments clearly explain the two options for providing headers. This is a good enhancement that enables dynamic header generation at request time, which will be useful for token refreshing and other runtime header needs.


123-128:

✅ Verification successful

Verify proper consumption of headers across the codebase.

This change introduces dynamic header resolution which affects the timing of when headers are available. Ensure that all code paths consuming the EdgeChatAdapter properly handle this asynchronous behavior and that it doesn't introduce race conditions or timing issues.


🏁 Script executed:

#!/bin/bash
# Description: Search for usages of EdgeChatAdapter to verify compatibility with dynamic headers

# Look for imports of EdgeChatAdapter
echo "Searching for EdgeChatAdapter imports..."
rg -A 5 "import.*EdgeChatAdapter" --type ts

# Look for instantiations of EdgeChatAdapter
echo "Searching for EdgeChatAdapter instantiations..."
rg -A 10 "new EdgeChatAdapter" --type ts

# Look for usage of headers property in EdgeChatAdapterOptions
echo "Searching for headers usage in EdgeChatAdapterOptions..."
rg -A 3 "headers\s*[=:]" --type ts | grep -B 5 -A 3 "EdgeChatAdapter"

Length of output: 3189


Dynamic header resolution verification complete – no issues found.

The asynchronous resolution of headers in EdgeChatAdapter.ts is properly encapsulated within the adapter’s internal fetch call, and our grep results (especially the instantiation in packages/react/src/runtimes/edge/useEdgeRuntime.ts) show that consumers do not prematurely assume synchronous header availability. No race conditions or timing issues were evident across the code paths consuming the adapter.

  • Verified imports and instantiations in useEdgeRuntime.ts indicate that the adapter is used with proper asynchronous handling.
  • No other consumers rely on a synchronous headers value; the asynchronous invocation is fully contained within the adapter.

Copy link
Contributor

promptless bot commented Mar 4, 2025

📝 Documentation updates detected! You can review documentation updates here

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

The PR adds dynamic header support to EdgeChatAdapter, enabling runtime header updates for authentication token refreshing and other dynamic use cases.

  • Added HeadersValue type supporting both static Record<string, string> and Headers objects
  • Enhanced EdgeChatAdapterOptions.headers to accept either static headers or an async function returning headers
  • Implemented header resolution logic in run() method to await function-based headers before making requests
  • Added JSDoc documentation for the new headers functionality
  • Maintains backward compatibility while solving the token refresh issue from #1703

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Member

@Yonom Yonom left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@Yonom
Copy link
Member

Yonom commented Mar 4, 2025

need to add a changeset before I merge this

(note to self: we should use the changesets github bot that tells PR creators to add a changeset)

I can get to adding the changeset tonight or you can do it by running pnpm changeset

Copy link

trag-bot bot commented Mar 5, 2025

👋 Hey! As a free user, you're receiving reviews for every 5th PR. Upgrade to get reviews on every pull request and boost your code quality! Learn more here 🚀

@kubk
Copy link
Contributor Author

kubk commented Mar 5, 2025

@Yonom Thank you, I've added the changeset 👌

Copy link

trag-bot bot commented Mar 5, 2025

Pull request summary

  • Added support for dynamic headers in the EdgeChatAdapter to allow headers to be defined as a function returning a Promise.
  • Introduced a new type HeadersValue to represent the possible formats for headers, enhancing type safety.
  • Updated the EdgeChatAdapterOptions type definition to reflect the new headers structure, allowing for both static and dynamic headers.
  • Modified the EdgeChatAdapter class to handle the new headers format, ensuring compatibility with both static and asynchronous header definitions.
  • Ensured that the Content-Type header is consistently set to application/json when making requests.

@kubk
Copy link
Contributor Author

kubk commented Mar 14, 2025

HI @Yonom , just wanted to follow up on this PR. I think it’s ready to go and would love to get it merged. Let me know if there’s anything else you need from me. Thanks!

@Yonom Yonom merged commit f8a157a into assistant-ui:main Mar 16, 2025
2 of 3 checks passed
@Yonom
Copy link
Member

Yonom commented Mar 16, 2025

Thank you!

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.

No way to refresh headers for EdgeChatAdapter
2 participants