Repository navigation
feat(llm): optional custom User-Agent for LLM requests - #155
Merged
Merged
Conversation
Add an optional per-profile User-Agent that overrides the header on every outbound LLM request. When empty (the default), the existing behaviour is unchanged and the default User-Agent is left untouched. The value is trimmed and validated: control characters / newlines (header injection) and values longer than 256 bytes are rejected. Wired through the profile config struct, the Postgres llm_profiles schema, the profile API/DTO, the connection-test path, and the LLM profile form, following the existing session-header field. Addresses part of #154; the fixed-full-URL mode is not included.
testLLM set cfg.UserAgent from the request (or the stored-profile fallback) with no validation, while pgSaveProfile runs agent.ValidateUserAgent and returns 400 on a bad value. A UA with a control character could therefore reach the wire from the test button though it is rejected on save. Call ValidateUserAgent on the final value (after the stored fallback) and return 400 with the same message style, mirroring pgSaveProfile; the fallback itself is unchanged. ValidateUserAgent rejects every control character (r < 0x20 or r == 0x7f), TAB included, so the header value stays a single printable token. Extend the agent tests to cover NUL, US, DEL and TAB, and that a printable slash-token value with spaces survives trimming unchanged. Add a Postgres-free server test driving testLLM through an httptest endpoint: a control-character UA is a 400 before any request leaves, a printable UA reaches the wire verbatim.
This was referenced Nov 8, 2026
Merged
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.
What
Adds an optional, per-profile custom User-Agent for outbound LLM requests.
user_agentfield on an LLM profile. When non-empty, it overrides theUser-Agentheader on every outbound LLM request built from that profile.quotaAwareTransport.RoundTrip, next to the existing custom session-header logic.Why
Issue #154 requests two options. This PR implements only the first (optional custom User-Agent). Many third-party LLM relays route or allow-list by User-Agent, and there was no way to set one.
Validation
The value is trimmed and rejected when it contains control characters / newlines (header-injection guard) or exceeds 256 bytes. Validation lives in
agent.ValidateUserAgentand is enforced on save.Where it is wired
Following the existing
session_header_keyfield end to end:agent/provider.go—Config.UserAgent, transport field +RoundTripheader set,ValidateUserAgent.db/config.go+db/schema.sql—user_agentcolumn (TEXT NOT NULL DEFAULT '', with backfillALTER TABLE).server/dto.go,server/server.go,server/server_mgmt.go,server/side_questions.go— DTO, config loaders, save validation, connection-test path, provider identity.web/...— the LLM profile form (optional text field, same pattern as the custom session header).Tested
go build ./...— clean.go vet ./agent/ ./db/ ./server/— clean.go test -race ./agent/(new tests) — pass: header set when configured (viahttptest), default UA untouched when empty, newline rejected, >256 rejected, trim/empty handling.gofmt— new/changed lines are clean. (server/server.gois flagged bygofmtonly for a pre-existing comment-alignment issue in the unrelatedTaskCreatestruct that already exists onmain; left untouched to keep the change surgical.)Not tested
Risk
The User-Agent override applies to every outbound request from the profile, including when a proxy is configured — the header is set before the request leaves
RoundTrip, so it reaches both direct and proxied endpoints. No change when the field is empty.