Repository navigation
feat(core): expose request headers to dynamic agents #1218
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| "@voltagent/server-core": patch | ||
| "@voltagent/server-hono": patch | ||
| "@voltagent/server-elysia": patch | ||
| "@voltagent/serverless-hono": patch | ||
| --- | ||
|
|
||
| feat(agent): expose request headers to dynamic agent configuration | ||
|
|
||
| Dynamic `instructions`, `model`, and `tools` functions now receive a `headers` map in | ||
| `DynamicValueOptions` when an agent is called through the built-in HTTP endpoints. This makes it | ||
| possible to configure tenant-aware models and request-scoped tools from headers such as | ||
| `authorization`, `x-tenant-id`, or `x-user-id` without manually copying them into | ||
| `options.context`. | ||
|
|
||
| Header names are normalized to lowercase: | ||
|
|
||
| ```ts | ||
| const agent = new Agent({ | ||
| name: "Tenant Agent", | ||
| instructions: "You are a tenant-aware assistant.", | ||
| model: ({ headers }) => { | ||
| return headers?.["x-tenant-id"] === "enterprise" ? "openai/gpt-4o" : "openai/gpt-4o-mini"; | ||
| }, | ||
| tools: ({ headers }) => { | ||
| return headers?.authorization ? [createTenantTool(headers.authorization)] : []; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| For direct in-process calls, pass `requestHeaders`: | ||
|
|
||
| ```ts | ||
| await agent.generateText("Hello", { | ||
| requestHeaders: { | ||
| authorization: "Bearer token", | ||
| "x-tenant-id": "tenant-1", | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Fixes #1201 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1078,6 +1078,9 @@ export interface CommonGenerateOptions { | |
| // Optional user-defined context to be passed from a parent operation | ||
| context?: UserContext; | ||
|
|
||
| // HTTP request headers associated with this generation call, when available. | ||
| requestHeaders?: Record<string, string>; | ||
|
Comment on lines
+1081
to
+1082
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize direct-call Server adapters normalize through server-core, but direct 🐛 Proposed implementation shape+function normalizeRequestHeaders(
+ headers?: Record<string, string>,
+): Record<string, string> | undefined {
+ if (!headers) {
+ return undefined;
+ }
+
+ return Object.fromEntries(
+ Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value]),
+ );
+}
+
// In createOperationContext / operation context construction:
- requestHeaders: options?.requestHeaders ?? options?.parentOperationContext?.requestHeaders,
+ requestHeaders: normalizeRequestHeaders(
+ options?.requestHeaders ?? options?.parentOperationContext?.requestHeaders,
+ ),Also applies to: 1312-1313 🤖 Prompt for AI Agents |
||
|
|
||
| // Optional hooks to be included only during the operation call and not persisted in the agent | ||
| hooks?: AgentHooks; | ||
| } | ||
|
|
@@ -1306,6 +1309,9 @@ export type OperationContext = { | |
| /** User-managed context map for this operation */ | ||
| readonly context: Map<string | symbol, unknown>; | ||
|
|
||
| /** HTTP request headers associated with this operation, when available */ | ||
| readonly requestHeaders?: Record<string, string>; | ||
|
|
||
| /** System-managed context map for internal operation tracking */ | ||
| readonly systemContext: Map<string | symbol, unknown>; | ||
|
|
||
|
|
||
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.
Normalize direct-call
requestHeadersbefore storing them.Server adapters may already lowercase names, but direct in-process calls can pass mixed-case keys. Store normalized keys here so
headers["x-tenant-id"]behaves consistently across all entrypoints.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents