-
Notifications
You must be signed in to change notification settings - Fork 123
feat(ask_sb): Add headers
param to config to allow users to specify custom headers
#449
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
Conversation
WalkthroughThis change introduces an optional Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ChatAPI
participant Actions
participant LMProvider
participant TokenResolver
User->>ChatAPI: Initiate chat request
ChatAPI->>Actions: _getAISDKLanguageModelAndOptions(config, orgId)
Actions->>Actions: Check config.headers
alt headers present
Actions->>TokenResolver: extractLanguageModelHeaders(headers, orgId, db)
TokenResolver-->>Actions: Resolved headers (string-to-string)
Actions->>LMProvider: Initialize with resolved headers
else headers absent
Actions->>LMProvider: Initialize without headers
end
Actions-->>ChatAPI: Return model and providerOptions
ChatAPI-->>User: Stream/chat response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
Note 🔌 MCP (Model Context Protocol) integration is now available in Early Access!Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
This comment has been minimized.
This comment has been minimized.
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.
Actionable comments posted: 4
🔭 Outside diff range comments (1)
packages/schemas/src/v3/languageModel.type.ts (1)
1-1
: Auto-generated interfaces are missing theheaders
propertyThe
LanguageModelHeaders
interface is defined in your schemas, but none of the generated language model interfaces include an optionalheaders?: LanguageModelHeaders;property. Please update your source JSON schemas (or generator templates) to add
headers
to every language model interface, then re-generate.Affected files:
- packages/schemas/src/v3/languageModel.type.ts
- packages/schemas/src/v3/index.type.ts
After regeneration, re-run the verification script to confirm each interface now includes
headers?: LanguageModelHeaders;
.
♻️ Duplicate comments (11)
schemas/v3/languageModel.json (11)
77-80
: Same observation as Lines 42-45 – duplication can be eliminated by introducing a shared “common” schema.
120-123
: See earlier comment regarding factoring out theheaders
property.
156-158
: See earlier comment regarding factoring out theheaders
property.
191-193
: See earlier comment regarding factoring out theheaders
property.
242-244
: See earlier comment regarding factoring out theheaders
property.
295-297
: See earlier comment regarding factoring out theheaders
property.
330-332
: See earlier comment regarding factoring out theheaders
property.
381-383
: See earlier comment regarding factoring out theheaders
property.
419-421
: See earlier comment regarding factoring out theheaders
property.
455-457
: See earlier comment regarding factoring out theheaders
property.
494-496
: See earlier comment regarding factoring out theheaders
property.
🧹 Nitpick comments (3)
schemas/v3/languageModel.json (1)
42-45
: Avoid property duplication – consider hoistingheaders
into a common schema.
headers
is now repeated verbatim across every provider definition. Embedding the new field throughallOf
with a single “LanguageModelCommon” schema (or similar) would:• Eliminate copy-pasted blocks
• Reduce the chance of future drift when the field changes
• Keep the file shorter and more maintainableExample sketch:
"LanguageModelCommon": { "type": "object", "properties": { "baseUrl": { ... }, "headers": { "$ref": "./shared.json#/definitions/LanguageModelHeaders" } } }Then each provider could simply use:
"allOf": [ { "$ref": "#/definitions/LanguageModelCommon" }, { /* provider-specific props */ } ]packages/web/src/features/chat/actions.ts (1)
602-604
: Remove redundant null checkThe
headers
parameter null check is redundant since this function is only called whenconfig.headers
exists (see all the conditional calls in the provider cases above).- if (!headers) { - return resolvedHeaders; - } -docs/snippets/schemas/v3/index.schema.mdx (1)
1303-1342
: AddadditionalProperties: false
inside theheaders
objectWithout this flag, values that fail the
patternProperties
check still pass validation, which defeats the purpose of the strict pattern. One-line fix:"headers": { "type": "object", "description": "Optional headers to use with the model.", + "additionalProperties": false, "patternProperties": { "^[a-zA-Z0-9_-]+$": { /* … */ } } }
This keeps the schema tight and prevents accidental typos in header names.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (14)
docs/snippets/schemas/v3/index.schema.mdx
(30 hunks)docs/snippets/schemas/v3/languageModel.schema.mdx
(30 hunks)docs/snippets/schemas/v3/shared.schema.mdx
(1 hunks)packages/schemas/src/v3/index.schema.ts
(30 hunks)packages/schemas/src/v3/index.type.ts
(12 hunks)packages/schemas/src/v3/languageModel.schema.ts
(30 hunks)packages/schemas/src/v3/languageModel.type.ts
(12 hunks)packages/schemas/src/v3/shared.schema.ts
(1 hunks)packages/schemas/src/v3/shared.type.ts
(1 hunks)packages/web/src/app/api/(server)/chat/route.ts
(1 hunks)packages/web/src/features/chat/actions.ts
(13 hunks)packages/web/src/features/chat/agent.ts
(0 hunks)schemas/v3/languageModel.json
(12 hunks)schemas/v3/shared.json
(1 hunks)
💤 Files with no reviewable changes (1)
- packages/web/src/features/chat/agent.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*
📄 CodeRabbit Inference Engine (.cursor/rules/style.mdc)
Filenames should always be camelCase. Exception: if there are filenames in the same directory with a format other than camelCase, use that format to keep things consistent.
Files:
schemas/v3/shared.json
docs/snippets/schemas/v3/shared.schema.mdx
packages/web/src/app/api/(server)/chat/route.ts
packages/schemas/src/v3/languageModel.schema.ts
packages/schemas/src/v3/index.type.ts
packages/schemas/src/v3/shared.type.ts
schemas/v3/languageModel.json
packages/schemas/src/v3/shared.schema.ts
packages/schemas/src/v3/languageModel.type.ts
packages/web/src/features/chat/actions.ts
docs/snippets/schemas/v3/index.schema.mdx
packages/schemas/src/v3/index.schema.ts
docs/snippets/schemas/v3/languageModel.schema.mdx
🧬 Code Graph Analysis (3)
packages/schemas/src/v3/index.type.ts (1)
packages/schemas/src/v3/languageModel.type.ts (1)
LanguageModelHeaders
(75-96)
packages/schemas/src/v3/shared.type.ts (2)
packages/schemas/src/v3/index.type.ts (1)
LanguageModelHeaders
(504-525)packages/schemas/src/v3/languageModel.type.ts (1)
LanguageModelHeaders
(75-96)
packages/schemas/src/v3/languageModel.type.ts (2)
packages/schemas/src/v3/index.type.ts (1)
LanguageModelHeaders
(504-525)packages/schemas/src/v3/shared.type.ts (1)
LanguageModelHeaders
(46-52)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
🔇 Additional comments (22)
schemas/v3/shared.json (1)
76-91
: Pattern validation successful for LanguageModelHeadersThe regex
^[a-zA-Z0-9_-]+$
was tested against a variety of standard HTTP headers (e.g., Authorization, Content-Type, User-Agent, X-API-Key, Cache-Control, X-Custom_Header, api-key) and matches all of them. No updates to the pattern are necessary.docs/snippets/schemas/v3/shared.schema.mdx (1)
78-120
: Documentation inlines Token definitions consistentlyA search across
docs/snippets/schemas
found no$ref
usages—inline object definitions are the intentional, consistent approach for clarity in the documentation. No changes required.packages/web/src/app/api/(server)/chat/route.ts (1)
91-91
: Header handling is correctly centralized in the SDK initializer– In packages/web/src/features/chat/actions.ts, each
createX
call now includesheaders: config.headers ? await extractLanguageModelHeaders(config.headers, orgId, prisma) : undefined,ensuring that resolved headers are baked into the SDK client.
– The_getAISDKLanguageModelAndOptions
function (in the same file) builds and returns a model whose internal request logic already applies these headers.
– Removing the standaloneheaders
destructuring in route.ts and dropping it fromcreateAgentStream
is intentional and safe: themodel
returned by the helper now owns header injection.No further changes required here.
packages/schemas/src/v3/shared.schema.ts (1)
77-119
: Schema structure correctly mirrors the JSON definition.The TypeScript schema constant properly defines the
LanguageModelHeaders
structure with inlined Token definitions, maintaining consistency with the JSON schema.packages/schemas/src/v3/index.schema.ts (3)
1301-1343
: Headers property implementation looks well-structured.The
headers
property implementation follows a consistent pattern across all language model providers. The schema correctly allows both direct string values and secure references viasecret
orenv
objects, maintaining consistency with the existingtoken
property patterns.
1394-1396
: Good structural improvement to token descriptions.Moving the
description
field from inside theanyOf
array to a sibling property improves schema readability and follows JSON Schema best practices. This change makes the descriptions more accessible for documentation generation while maintaining the same validation behavior.Also applies to: 1500-1502, 1606-1608
1305-1305
: Header name pattern validated – no violations found.The regex
^[a-zA-Z0-9_-]+$
covers all current header keys (e.g.Content-Type
,X-Org-Domain
,stripe-signature
, etc.) and enforces a safe, consistent subset of token characters. No existing headers in the codebase use characters outside of letters, digits, underscores, or hyphens.If in the future you need to support additional token characters (such as dots), you can revisit and extend this pattern then.
packages/schemas/src/v3/languageModel.schema.ts (3)
98-140
: LGTM: Consistent headers implementation for Amazon Bedrock provider.The headers property implementation is well-structured and follows established patterns in the codebase. The pattern
^[a-zA-Z0-9_-]+$
for header names is appropriate for HTTP headers, and the support for both string values and secure references (secret/env) maintains consistency with existing token handling.
191-192
: Improved schema structure by moving token descriptions.Moving the description fields to be siblings of the
anyOf
arrays rather than nested within them improves the JSON Schema structure. This makes it clearer that the description applies to the entire token property regardless of which variant (secret or env) is used.Also applies to: 297-298, 403-404, 505-506, 623-624, 743-744, 845-846, 953-954, 1065-1066, 1171-1172, 1277-1278, 1523-1524, 1629-1630, 1735-1736, 1837-1838, 1955-1956, 2075-2076, 2177-2178, 2285-2286, 2397-2398, 2503-2504, 2609-2610
1337-2668
: LGTM: Consistent headers implementation in oneOf schema variants.The headers property has been consistently implemented across all language model provider variants in the oneOf section, maintaining the same structure and validation rules as in the definitions section. This ensures schema consistency and proper validation regardless of how the schema is used.
packages/schemas/src/v3/index.type.ts (2)
499-499
: Consistent addition of optional headers propertyThe
headers
property has been consistently added as an optional field to all 12 language model interfaces. This maintains backward compatibility while enabling the new custom headers feature.Also applies to: 559-559, 602-602, 637-637, 672-672, 715-715, 758-758, 793-793, 832-832, 867-867, 902-902, 937-937
1-1
: Ignore duplication warning—interface defined once; headers property is consistentVerified that
LanguageModelHeaders
is declared only once inpackages/schemas/src/v3/index.type.ts
and that every language model inlanguageModel.schema.ts
includes an optionalheaders
field. Because this file is auto-generated, please make any changes in the source schema (packages/schemas/src/v3/languageModel.schema.ts
) and runyarn generate
rather than editingindex.type.ts
directly.Likely an incorrect or invalid review comment.
packages/web/src/features/chat/actions.ts (2)
392-394
: Consistent headers extraction across all providersThe headers extraction has been consistently implemented for all 12 language model providers using the same pattern and helper function. This ensures uniform behavior across all providers.
Also applies to: 407-409, 430-432, 443-445, 458-460, 476-478, 502-504, 517-519, 532-534, 553-555, 568-570, 583-585
595-619
: Well-structured headers extraction implementationThe function correctly handles both direct string values and secure token references (via secrets or environment variables), maintaining consistency with the existing token retrieval pattern used throughout the codebase.
docs/snippets/schemas/v3/index.schema.mdx (2)
1301-1344
: Extract theheaders
schema into a single shared$ref
to avoid massive duplicationThe exact same
headers
object (pattern, anyOf, secret/env resolution) is inlined for every provider configuration and then duplicated again in theoneOf
list.
This balloons the generated schema >2×, hurts diff-readability, and increases the risk that future edits forget one copy.Prefer defining it once, e.g.:
"definitions": { "LanguageModelHeaders": { "type": "object", "description": "Optional headers to use with the model.", "patternProperties": { "^[a-zA-Z0-9_-]+$": { /* … current anyOf … */ } }, "additionalProperties": false }, /* existing defs … */ }and then reference it:
- "headers": { /* huge inline block */ } + "headers": { "$ref": "#/definitions/LanguageModelHeaders" }Doing so trims >3 000 lines from this file, speeds up downstream validators, and ensures one-shot maintenance.
[Suggest_essential_refactor]
1303-1342
: Header names are treated as case-sensitive – confirm this is intentionalJSON-object keys are case-sensitive, whereas HTTP header field-names are explicitly case-insensitive (RFC 7230 §3.2).
With the current schema"Authorization"
and"authorization"
are two distinct entries and the latter could silently override the former at runtime.If the implementation lower-cases keys before dispatch, add
"propertyNames": { "transform": "lowercase" }
in the canonical schema or document the requirement to use lower-case names only.
Otherwise consumers may configure duplicate headers without realising.packages/schemas/src/v3/languageModel.type.ts (3)
70-70
: LGTM!The addition of the optional
headers
property to theAmazonBedrockLanguageModel
interface is consistent with the pattern established for other optional properties.
72-96
: Well-structured interface definition.The
LanguageModelHeaders
interface properly supports both direct string values and secure references (secrets/environment variables). The regex pattern^[a-zA-Z0-9_-]+$
for header keys is appropriate and the interface aligns with the existing codebase patterns.
130-130
: Consistent implementation across all language model interfaces.The addition of the optional
headers
property to all remaining language model interfaces (Anthropic, Azure, DeepSeek, Google Generative AI, Google Vertex Anthropic, Google Vertex, Mistral, OpenAI, OpenAI Compatible, OpenRouter, and XAI) follows a consistent pattern and maintains type safety.Also applies to: 173-173, 208-208, 243-243, 286-286, 329-329, 364-364, 403-403, 438-438, 473-473, 508-508
docs/snippets/schemas/v3/languageModel.schema.mdx (3)
1-1
: Auto-generated schema documentation is properly updated.The systematic addition of
headers
properties across all language model schemas indicates the generation process is working correctly.
99-141
: Comprehensive JSON schema validation for headers.The headers property schema provides robust validation with:
- Pattern-based key validation (
^[a-zA-Z0-9_-]+$
)- Flexible value types (string or secret/env references)
- Strict validation with
additionalProperties: false
This structure ensures proper validation while maintaining flexibility for different header value types.
192-193
: Systematic schema updates maintain consistency.The relocation of token descriptions and consistent application of headers properties across all language model schemas ensures uniform validation behavior. The changes properly maintain the JSON schema structure with appropriate duplication between definitions and oneOf sections.
Also applies to: 298-299, 404-405, 506-507, 624-625, 744-745, 846-847, 954-955, 1066-1067, 1172-1173, 1278-1279, 1524-1525, 1630-1631, 1736-1737, 1838-1839, 1956-1957, 2076-2077, 2178-2179, 2286-2287, 2398-2399, 2504-2505, 2610-2611
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.
Nothing to add beyond bot's comments.
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.
Actionable comments posted: 2
♻️ Duplicate comments (1)
docs/snippets/schemas/v3/languageModel.schema.mdx (1)
194-202
: Apply the same minLength: 1 fix across all headers blocksThis is the same pattern repeated for each provider. Please ensure the generator emits "minLength": 1 for the string branch in every headers patternProperties across all provider schemas.
Also applies to: 300-313, 408-416, 511-519, 630-638, 751-759, 854-862, 963-981, 1076-1087, 1183-1191, 1290-1298, 1442-1486, 1537-1547, 1644-1657, 1752-1762, 1854-1863, 1974-1982, 2096-2103, 2198-2206, 2306-2325, 2420-2431, 2527-2535, 2634-2642
🧹 Nitpick comments (2)
packages/web/src/features/chat/actions.ts (1)
392-395
: Ensure Header Name Normalization & Precedence in extractLanguageModelHeadersPlease confirm and, if needed, implement the following in your header‐plumbing helper and docs:
• Normalize all header names to lowercase before merging to avoid duplicates (e.g., “Authorization” vs “authorization”).
• Merge SDK-generated auth headers (from apiKey/org credentials) with user-provided headers so that user headers always override on conflicts.
• Add or update a brief note in the code/docs explaining that custom headers take precedence over built-in auth headers.Locations to review:
- packages/web/src/features/chat/actions.ts (invokes extractLanguageModelHeaders)
- extractLanguageModelHeaders implementation (e.g. in packages/web/src/features/chat/helpers.ts or similar)
Example normalization & merge pattern:
// Normalize user headers const userNormalized = Object.entries(configHeaders || {}).reduce( (acc, [name, value]) => { acc[name.toLowerCase()] = value; return acc; }, {} as Record<string, string> ); // Build SDK headers, then merge user overrides const sdkHeaders = await buildSdkAuthHeaders(...); return { ...sdkHeaders, ...userNormalized };docs/snippets/schemas/v3/languageModel.schema.mdx (1)
1-8
: Auto-generated notice acknowledgedGiven this is auto-generated, the above suggestions should be implemented in the source generator, not by manual edits here.
Add a brief note in the generator’s README about header precedence (user headers vs SDK-auth headers) to set expectations.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
docs/snippets/schemas/v3/index.schema.mdx
(30 hunks)docs/snippets/schemas/v3/languageModel.schema.mdx
(30 hunks)docs/snippets/schemas/v3/shared.schema.mdx
(1 hunks)packages/schemas/src/v3/index.schema.ts
(30 hunks)packages/schemas/src/v3/index.type.ts
(12 hunks)packages/schemas/src/v3/languageModel.schema.ts
(30 hunks)packages/schemas/src/v3/languageModel.type.ts
(12 hunks)packages/schemas/src/v3/shared.schema.ts
(1 hunks)packages/schemas/src/v3/shared.type.ts
(1 hunks)packages/web/src/features/chat/actions.ts
(13 hunks)schemas/v3/shared.json
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
- packages/schemas/src/v3/shared.type.ts
- schemas/v3/shared.json
- packages/schemas/src/v3/shared.schema.ts
- docs/snippets/schemas/v3/shared.schema.mdx
- packages/schemas/src/v3/index.schema.ts
- packages/schemas/src/v3/languageModel.schema.ts
- docs/snippets/schemas/v3/index.schema.mdx
- packages/schemas/src/v3/index.type.ts
- packages/schemas/src/v3/languageModel.type.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*
📄 CodeRabbit Inference Engine (.cursor/rules/style.mdc)
Filenames should always be camelCase. Exception: if there are filenames in the same directory with a format other than camelCase, use that format to keep things consistent.
Files:
packages/web/src/features/chat/actions.ts
docs/snippets/schemas/v3/languageModel.schema.mdx
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
🔇 Additional comments (1)
packages/web/src/features/chat/actions.ts (1)
23-24
: Import changes look correctPrismaClient is used by the new helper; LanguageModelHeaders is used for typing. No issues.
For example:
Fixes #441
Summary by CodeRabbit
New Features
Bug Fixes
Documentation