Repository navigation
feat(endpoint): per-endpoint custom system prompt injection #5022
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -132,3 +132,56 @@ export function injectSystemPrompt(body) { | |||||
|
|
||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Inject a per-request custom system prompt into the request body. | ||||||
| * | ||||||
| * Unlike injectSystemPrompt (which reads from globalThis config), this | ||||||
| * function takes an explicit prompt string and appends it as a suffix | ||||||
| * after any existing system content — mirroring the caveman/ponytail | ||||||
| * injection pattern but driven by per-endpoint settings. | ||||||
| * | ||||||
| * @param body - Translated request body (OpenAI/Claude/Gemini format) | ||||||
| * @param prompt - The custom system prompt text to inject | ||||||
| * @returns Modified body with prompt appended to the system message | ||||||
| */ | ||||||
| export function injectCustomSystemPrompt(body: Record<string, unknown>, prompt: string) { | ||||||
| if (!prompt || typeof prompt !== "string") return body; | ||||||
| if (!body || typeof body !== "object") return body; | ||||||
|
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. In JavaScript/TypeScript,
Suggested change
|
||||||
| if (body._skipSystemPrompt) return body; | ||||||
|
|
||||||
| const result = { ...body }; | ||||||
|
|
||||||
| // OpenAI/Claude messages[] format | ||||||
| if (result.messages && Array.isArray(result.messages)) { | ||||||
| const sysIdx = (result.messages as Array<{ role: string; content: unknown }>).findIndex( | ||||||
| (m) => m.role === "system" || m.role === "developer" | ||||||
| ); | ||||||
| result.messages = [...(result.messages as Array<{ role: string; content: unknown }>)]; | ||||||
| if (sysIdx >= 0) { | ||||||
| const msg = { ...(result.messages as Array<{ role: string; content: unknown }>)[sysIdx] }; | ||||||
| if (Array.isArray(msg.content)) { | ||||||
| msg.content = [...(msg.content as unknown[]), { type: "text", text: prompt }]; | ||||||
| } else { | ||||||
| msg.content = (msg.content ? msg.content + "\n\n" : "") + prompt; | ||||||
| } | ||||||
| (result.messages as Array<{ role: string; content: unknown }>)[sysIdx] = msg; | ||||||
| } else { | ||||||
| result.messages = [ | ||||||
| { role: "system", content: prompt }, | ||||||
| ...(result.messages as Array<{ role: string; content: unknown }>), | ||||||
| ]; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Claude direct system field | ||||||
| if (result.system !== undefined) { | ||||||
| if (typeof result.system === "string") { | ||||||
| result.system = result.system ? result.system + "\n\n" + prompt : prompt; | ||||||
| } else if (Array.isArray(result.system)) { | ||||||
| result.system = [...(result.system as unknown[]), { type: "text", text: prompt }]; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return result; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import { useState, useEffect, useMemo, useCallback } from "react"; | ||
| import Link from "next/link"; | ||
| import { Card, Button, Input, Modal, CardSkeleton, SegmentedControl } from "@/shared/components"; | ||
| import Toggle from "@/shared/components/Toggle"; | ||
| import { useCopyToClipboard } from "@/shared/hooks/useCopyToClipboard"; | ||
| import { useDisplayBaseUrl } from "@/shared/hooks"; | ||
| import { AI_PROVIDERS, getProviderByAlias } from "@/shared/constants/providers"; | ||
|
|
@@ -189,6 +190,8 @@ export default function APIPageClient({ machineId }: Readonly<APIPageClientProps | |
| const [lanUrls, setLanUrls] = useState<string[]>([]); | ||
| const [tailscaleIpUrl, setTailscaleIpUrl] = useState<string | null>(null); | ||
| const [activeEndpointTab, setActiveEndpointTab] = useState<EndpointTab>("apis"); | ||
| const [customSystemPromptEnabled, setCustomSystemPromptEnabled] = useState(false); | ||
| const [customSystemPrompt, setCustomSystemPrompt] = useState(""); | ||
|
|
||
| const { copied, copy } = useCopyToClipboard(); | ||
|
|
||
|
|
@@ -496,6 +499,8 @@ export default function APIPageClient({ machineId }: Readonly<APIPageClientProps | |
| setShowTailscaleFunnel(tunnelVisibility.showTailscaleFunnel); | ||
| setShowNgrokTunnel(tunnelVisibility.showNgrokTunnel); | ||
| if (data.ngrokAuthToken) setNgrokToken(data.ngrokAuthToken); | ||
| setCustomSystemPromptEnabled(!!data.customSystemPromptEnabled); | ||
| setCustomSystemPrompt(data.customSystemPrompt || ""); | ||
|
|
||
| if (!tunnelVisibility.showCloudflaredTunnel) { | ||
| setCloudflaredStatus(null); | ||
|
|
@@ -519,6 +524,24 @@ export default function APIPageClient({ machineId }: Readonly<APIPageClientProps | |
| return DEFAULT_TUNNEL_VISIBILITY; | ||
| }; | ||
|
|
||
| const handleCustomSystemPromptEnabledChange = (value: boolean) => { | ||
| setCustomSystemPromptEnabled(value); | ||
| void fetch("/api/settings", { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ customSystemPromptEnabled: value }), | ||
| }); | ||
| }; | ||
|
|
||
| const handleCustomSystemPromptChange = (value: string) => { | ||
| setCustomSystemPrompt(value); | ||
| void fetch("/api/settings", { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ customSystemPrompt: value }), | ||
| }); | ||
| }; | ||
|
Comment on lines
+536
to
+543
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. Firing a network request to |
||
|
|
||
| const handleCloudToggle = (checked) => { | ||
| if (checked) { | ||
| if (!cloudConfigured) { | ||
|
|
@@ -1749,6 +1772,31 @@ export default function APIPageClient({ machineId }: Readonly<APIPageClientProps | |
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {/* Custom System Prompt */} | ||
| <div className="flex items-center justify-between pt-4 mt-4 border-t border-border gap-4 flex-wrap"> | ||
| <div className="min-w-0 flex-1"> | ||
| <p className="font-medium text-sm">{t("customSystemPromptTitle")}</p> | ||
| <p className="text-sm text-text-muted">{t("customSystemPromptDescription")}</p> | ||
| </div> | ||
| <div className="flex items-center gap-3 shrink-0"> | ||
| {customSystemPromptEnabled && ( | ||
| <Input | ||
| type="text" | ||
| value={customSystemPrompt} | ||
| onChange={(e) => handleCustomSystemPromptChange(e.target.value)} | ||
| placeholder={t("customSystemPromptPlaceholder")} | ||
| className="w-64 text-xs" | ||
| /> | ||
|
Comment on lines
+1784
to
+1790
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. Bind the newly created |
||
| )} | ||
| <Toggle | ||
| checked={customSystemPromptEnabled} | ||
| onChange={handleCustomSystemPromptEnabledChange} | ||
| ariaLabel={t("customSystemPromptTitle")} | ||
| size="sm" | ||
| /> | ||
| </div> | ||
| </div> | ||
| </Card> | ||
|
|
||
| <Card> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3002,7 +3002,10 @@ | |||||||||||||
| "tierAlwaysProtected": "__MISSING__:Always Protected", | ||||||||||||||
| "tierPublic": "__MISSING__:Public", | ||||||||||||||
| "hideInternal": "__MISSING__:Hide Internal", | ||||||||||||||
| "showInternal": "__MISSING__:Show Internal" | ||||||||||||||
| "showInternal": "__MISSING__:Show Internal", | ||||||||||||||
| "customSystemPromptTitle": "Custom System Prompt", | ||||||||||||||
| "customSystemPromptDescription": "Inject a custom system prompt into every model request", | ||||||||||||||
| "customSystemPromptPlaceholder": "e.g. Always respond in pirate speak..." | ||||||||||||||
|
Comment on lines
+3006
to
+3008
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. To adhere to the project's internationalization standards and allow the translation pipeline to automatically detect untranslated keys, new English strings added to non-English locale files should be prefixed with
Suggested change
|
||||||||||||||
| }, | ||||||||||||||
| "endpoints": { | ||||||||||||||
| "tabProxy": "Endpoint Proxy", | ||||||||||||||
|
|
||||||||||||||
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.
Accessing properties on
_swithout a null/undefined check can lead to a runtime crash if bothcachedSettingsandgetCachedSettings()resolve to a nullish value. Use optional chaining to safely accesscustomSystemPromptEnabledandcustomSystemPrompt.