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

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.
This commit is contained in:
BaaRai
2026-11-08 17:11:43 +01:00
parent a908e1ec17
commit 13dde2da15
13 changed files with 184 additions and 18 deletions
+19
View File
@@ -335,6 +335,7 @@ function ProfileSheet({
const [maxTokens, setMaxTokens] = React.useState("0"); // per-reply output cap; 0 = not sent
const [maxTokensField, setMaxTokensField] = React.useState(NONE); // which field name carries the cap; NONE = max_tokens
const [sessionHeaderKey, setSessionHeaderKey] = React.useState(""); // custom session header name; empty = not sent
const [userAgent, setUserAgent] = React.useState(""); // custom User-Agent; empty = keep the default UA
const [retry, setRetry] = React.useState<LLMRetryOverride>(ZERO_OVERRIDE); // retry override for this profile; all 0 = follow the global
const [testing, setTesting] = React.useState(false);
const [saving, setSaving] = React.useState(false);
@@ -366,6 +367,7 @@ function ProfileSheet({
setMaxTokens(String(profile?.max_tokens ?? 0));
setMaxTokensField(fromStore(profile?.max_tokens_field));
setSessionHeaderKey(profile?.session_header_key ?? "");
setUserAgent(profile?.user_agent ?? "");
setRetry(profile?.retry ?? ZERO_OVERRIDE);
setApiKey("");
setKeyHint(profile?.api_key_hint ?? "");
@@ -412,6 +414,7 @@ function ProfileSheet({
profileId,
streaming,
sessionHeaderKey.trim(),
userAgent.trim(),
);
// Show the reply too: only when the model demonstrably spoke is this the same as working inside a session.
if (r.ok)
@@ -455,6 +458,7 @@ function ProfileSheet({
// and the backend re-applies the same normalization; this just keeps the UI from sending contradictory values.
max_tokens_field: format === "openai" ? toStore(maxTokensField) : "",
session_header_key: sessionHeaderKey.trim(),
user_agent: userAgent.trim(),
retry,
});
if (isNew) toast.success(`Created: ${name.trim()} (use "Set as active" on its card to enable it)`);
@@ -613,6 +617,21 @@ function ProfileSheet({
</p>
</div>
<div className="grid gap-2">
<Label htmlFor="p-user-agent">Custom User-Agent (optional)</Label>
<Input
id="p-user-agent"
className="font-mono"
placeholder="e.g. my-app/1.0 (empty = default UA)"
value={userAgent}
onChange={(e) => setUserAgent(e.target.value)}
/>
<p className="text-muted-foreground text-xs">
Overrides the <b>User-Agent</b> header on every outbound LLM request. Useful for gateways that route or allow-list by
User-Agent. Leave empty to keep the default User-Agent unchanged.
</p>
</div>
<div className="grid gap-2">
<Label htmlFor="p-api-key">
{format === "ollama" ? "API Key (optional for Ollama)" : "API Key"}
+3
View File
@@ -949,6 +949,7 @@ export const api = {
profile_id?: number,
streaming = true, // test with the profile's real streaming mode so "streaming works, non-streaming does not" never leaks into conversations
session_header_key = "", // non-empty = the test request also carries this custom session header (value is a one-off session id)
user_agent = "", // non-empty = the test overrides the User-Agent header with this value
) =>
// reply = the model's actual reply (truncated); the backend marks a profile that replies with nothing as failed
post<{ ok: boolean; error?: string; latency_ms?: number; model?: string; reply?: string }>("/llm/test", {
@@ -962,6 +963,7 @@ export const api = {
profile_id,
streaming,
session_header_key,
user_agent,
}),
llmProfiles: () => get<{ profiles: LLMProfile[] }>("/llm/profiles").then((r) => arr(r.profiles)),
saveLLMProfile: (p: {
@@ -983,6 +985,7 @@ export const api = {
max_tokens?: number; // output cap per reply; 0 = not sent, the server default decides
max_tokens_field?: string; // ""=max_tokens (default) | "max_completion_tokens" (openai format only)
session_header_key?: string; // non-empty = send this HTTP header on every request, value = the current session id; "" = not sent
user_agent?: string; // non-empty = override the User-Agent header on every request; "" = keep the default UA
retry?: LLMRetryOverride; // this profile's retry override; all zeros = follow the global retry policy
}) => post<{ id: number }>("/llm/profiles", p),
deleteLLMProfile: (id: string) => del<{ deleted: number }>(`/llm/profiles/${id}`),
+2
View File
@@ -1061,6 +1061,8 @@ export interface LLMProfile {
// Custom session header name: when non-empty every request carries this HTTP header, value = the current session/intent session id.
// "" = not sent. For gateways that do prompt caching/sticky routing on a session-id header.
session_header_key?: string;
// Custom User-Agent: when non-empty, every outbound LLM request overrides its User-Agent header with this value. "" = do not override (keep the default UA).
user_agent?: string;
// This profile's retry overrides (connect / empty response / same-provider safety window). Empty / all zeros = follow the global policy.
retry?: LLMRetryOverride;
}