feat: custom variables for agents#12694
Draft
hmiranda-pt wants to merge 2 commits intodanny-avila:mainfrom
Draft
feat: custom variables for agents#12694hmiranda-pt wants to merge 2 commits intodanny-avila:mainfrom
hmiranda-pt wants to merge 2 commits intodanny-avila:mainfrom
Conversation
Author
|
Related to discussion: #12695 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds support for custom variables in agent instructions so that external systems can pass arbitrary key-value pairs via URL query parameters (prefixed with
custom_). These are persisted on the conversation and injected into agent instructions at runtime using{{variable_name}}placeholders. This enables dynamic customization of agent behavior without modifying the agent itself.(e.g.,
?agent_id=...&custom_region=us-east&custom_user_name=Alice).The custom variables are persisted per-conversation to support multi-turn conversations.
Changes
Frontend (
client/)useQueryParams.ts: Extractscustom_*query parameters from the URL, strips the prefix, and attaches them ascustomVariableson the preset passed tonewConversation.ChatRoute.tsx: Excludescustom_*params from the parameter subset to prevent them from interfering with existing logic.Shared (
packages/data-provider/)parsers.ts: NewreplaceCustomVariables()function that substitutes placeholders in text with the provided custom variable values. Intentionally skips built-in special variables (current_date,current_user, etc.) to avoid conflicts.schemas.ts: Added acustomVariablesfield (z.record(z.string()).optional()) totConversationSchemaand included it incompactAgentsBaseSchema.types.ts: AddedcustomVariablestoTEndpointOption.Legacy Backend (
api/)api/server/services/Endpoints/agents/build.js: ExtractscustomVariablesfrom the parsed body and passes it into the agent build options.api/server/services/Endpoints/agents/initialize.js: ForwardscustomVariablesfromendpointOptionto theAgentClientoptions.api/server/controllers/agents/client.js: PassescustomVariablesthrough to the run options.Backend (
packages/api/)packages/api/src/agents/initialize.ts: CallsreplaceCustomVariableson the agent instructions after the special variable replacement, only whencustomVariablesis present on the endpoint option.Database (
packages/data-schemas/)schema/defaults.ts: AddedcustomVariablesas aMap<String>field to the conversation Mongoose schema.types/convo.ts: AddedcustomVariablesto theIConversationinterface.methods/conversation.ts: ChangedtoObject()to use{ flattenMaps: true }so thatcustomVariablesis returned as a plain object instead of a Mongoose Map. Without this change, the Mongoose Map serializes to'{}'(even when it contains values), which causes the custom variables to be cleared in multi-turn conversations.Change Type
Testing
Manual Testing
"Always begin your replies with 'Dear {{name}} ({{department}}),'"http://localhost:3080/c/new?agent_id=<AGENT_ID>&custom_name=Alice&custom_department=Engineering"Dear Alice (Engineering)".Unit Testing
cd client && npx jest useQueryParams.spec.tscd packages/api && npx jest initialize.test.tscd packages/data-provider && npx jest parsers.spec.tscd packages/data-schemas && npx jest conversation.spec.tsTest Configuration:
Checklist