Skip to content

feat(llm): optional custom User-Agent for LLM requests - #155

Merged
BaaRai merged 2 commits into
mainfrom
feat/llm-user-agent
Nov 8, 2026
Merged

BaaRai merged 2 commits into
mainfrom
feat/llm-user-agent

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 8, 2026

Copy link
Copy Markdown
Owner

What

Adds an optional, per-profile custom User-Agent for outbound LLM requests.

  • New user_agent field on an LLM profile. When non-empty, it overrides the User-Agent header on every outbound LLM request built from that profile.
  • When empty (the default), nothing changes — the underlying Go/Norma default User-Agent is left untouched, so existing profiles behave exactly as before.
  • Set only in 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.

Note: the fixed-full-URL / BASEURL mode from #154 is intentionally NOT part of this PR and is left for a follow-up.

Validation

The value is trimmed and rejected when it contains control characters / newlines (header-injection guard) or exceeds 256 bytes. Validation lives in agent.ValidateUserAgent and is enforced on save.

Where it is wired

Following the existing session_header_key field end to end:

  • agent/provider.go — Config.UserAgent, transport field + RoundTrip header set, ValidateUserAgent.
  • db/config.go + db/schema.sql — user_agent column (TEXT NOT NULL DEFAULT '', with backfill ALTER 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 (via httptest), default UA untouched when empty, newline rejected, >256 rejected, trim/empty handling.
  • gofmt — new/changed lines are clean. (server/server.go is flagged by gofmt only for a pre-existing comment-alignment issue in the unrelated TaskCreate struct that already exists on main; left untouched to keep the change surgical.)

Not tested

  • The web UI change was not run through a frontend build; it mirrors the existing session-header field exactly.
  • DB-backed tests (require Postgres) were not run.

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.

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.
@BaaRai
BaaRai merged commit 9bf5d81 into main Nov 8, 2026
3 checks passed
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.

1 participant