Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🧐 feat: Preview Langfuse Prompts in Settings (#16854)
Atef Bellaaj202756b37f44
diff --git a/.env.example b/.env.exampleindex 2b6d620042..47c47c2002 100644--- a/.env.example+++ b/.env.example@@ -299,6 +299,11 @@ NODE_MAX_OLD_SPACE_SIZE=6144# Trace-level sample rate from 0 to 1. Sampled-out traces do not receive scores.# LANGFUSE_SAMPLE_RATE=1+# Lets tenant admins turn on Langfuse prompt sync in Settings > Langfuse.+# LANGFUSE_PROMPT_SYNC_AVAILABLE=false+# Langfuse prompt API request timeout in milliseconds, limited to 1000-60000.+# LANGFUSE_PROMPT_SYNC_TIMEOUT_MS=10000+# In single-tenant deployments without environment credentials, an admin can# configure one encrypted Langfuse connection in the application settings.# Complete environment credentials take precedence and hide those settings.diff --git a/api/server/routes/__tests__/config.spec.js b/api/server/routes/__tests__/config.spec.jsindex 57282f228d..51e8db8ada 100644--- a/api/server/routes/__tests__/config.spec.js+++ b/api/server/routes/__tests__/config.spec.js@@ -111,6 +111,7 @@ afterEach(() => {delete process.env.ANALYTICS_GTM_ID;delete process.env.CUSTOM_FOOTER;delete process.env.HELP_AND_FAQ_URL;+ delete process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE;delete process.env.LANGFUSE_FANOUT_ENABLED;delete process.env.LANGFUSE_FANOUT_COLLECTOR_URL;delete process.env.LANGFUSE_FANOUT_TENANT_EXPORT_DISABLED;@@ -928,6 +929,46 @@ describe('GET /api/config', () => {expect(mockHasCapability).not.toHaveBeenCalled();});+ it.each([+ ['environment credentials', { LANGFUSE_PUBLIC_KEY: 'pk-env', LANGFUSE_SECRET_KEY: 'sk-env' }],+ ['tracing disabled', { LANGFUSE_TRACING_ENABLED: 'false' }],+ ['zero sampling', { LANGFUSE_SAMPLE_RATE: '0' }],+ ['strict tenants without fanout', { TENANT_ISOLATION_STRICT: 'true' }],+ [+ 'tenant export disabled',+ {+ LANGFUSE_FANOUT_ENABLED: 'true',+ LANGFUSE_FANOUT_COLLECTOR_URL: 'http://collector:4318',+ LANGFUSE_FANOUT_TENANT_EXPORT_DISABLED: 'true',+ },+ ],+ ])('advertises prompt access with %s without advertising trace access', async (_mode, env) => {+ Object.assign(process.env, env, { LANGFUSE_PROMPT_SYNC_AVAILABLE: 'true' });+ mockGetAppConfig.mockResolvedValue(baseAppConfig);+ mockHasCapability.mockResolvedValue(true);+ mockHasConfigCapability.mockResolvedValue(true);+ const response = await request(createApp(mockUser)).get('/api/config');+ expect(response.status).toBe(200);+ expect(response.body.langfusePromptSyncAccess).toBe(true);+ expect(response.body.langfuseConnectionAccess).toBe(false);+ expect(mockHasCapability).toHaveBeenCalledTimes(1);+ expect(mockHasConfigCapability).toHaveBeenCalledTimes(1);+ });++ it.each([+ [false, true],+ [true, false],+ ])('requires admin=%s and Langfuse manage=%s for prompt access', async (admin, manage) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ process.env.LANGFUSE_TRACING_ENABLED = 'false';+ mockGetAppConfig.mockResolvedValue(baseAppConfig);+ mockHasCapability.mockResolvedValue(admin);+ mockHasConfigCapability.mockResolvedValue(manage);+ const response = await request(createApp(mockUser)).get('/api/config');+ expect(response.body.langfusePromptSyncAccess).toBe(false);+ expect(response.body.langfuseConnectionAccess).toBe(false);+ });+it('should include post-login informational fields', async () => {process.env.ANALYTICS_GTM_ID = 'GTM-XYZ';process.env.CUSTOM_FOOTER = 'authenticated footer text';diff --git a/api/server/routes/admin/langfuse.js b/api/server/routes/admin/langfuse.jsindex bcfc9bf897..10e91ea444 100644--- a/api/server/routes/admin/langfuse.js+++ b/api/server/routes/admin/langfuse.js@@ -53,5 +53,8 @@ router.get('/connection', handlers.getConnection);router.get('/connection/session/:conversationId', handlers.getSessionLink);router.put('/connection', handlers.updateConnection);router.post('/connection/test', handlers.testConnection);+router.put('/prompt-sync', handlers.updatePromptSync);+router.get('/prompts', handlers.listPrompts);+router.get('/prompts/:name', handlers.getPrompt);module.exports = router;diff --git a/api/server/routes/admin/langfuse.test.js b/api/server/routes/admin/langfuse.test.jsindex 52a1338691..fde9b5be96 100644--- a/api/server/routes/admin/langfuse.test.js+++ b/api/server/routes/admin/langfuse.test.js@@ -22,6 +22,11 @@ const mockHandlers = {getSessionLink: jest.fn((_req, res) => res.status(200).json({ handler: 'session' })),updateConnection: jest.fn((_req, res) => res.status(200).json({ handler: 'update' })),testConnection: jest.fn((_req, res) => res.status(200).json({ handler: 'test' })),+ updatePromptSync: jest.fn((_req, res) => res.status(200).json({ handler: 'prompt-sync' })),+ listPrompts: jest.fn((_req, res) => res.status(200).json({ handler: 'prompts' })),+ getPrompt: jest.fn((req, res) =>+ res.status(200).json({ handler: 'prompt', name: req.params.name }),+ ),};jest.mock('@librechat/data-schemas', () => ({@@ -98,27 +103,41 @@ describe('admin Langfuse routes', () => {});it.each([- ['GET', '/api/admin/langfuse/connection/session/conversation-1', 'getSessionLink'],- ['PUT', '/api/admin/langfuse/connection', 'updateConnection'],- ['POST', '/api/admin/langfuse/connection/test', 'testConnection'],- ])('requires Langfuse manage access for %s %s', async (method, path, handlerName) => {- const app = createApp();- const response = await request(app)[method.toLowerCase()](path).send({}).expect(200);- const expectedHandlers = {- getSessionLink: 'session',- updateConnection: 'update',- testConnection: 'test',- };-- expect(response.body).toEqual({ handler: expectedHandlers[handlerName] });- expect(middlewareCalls).toEqual(['jwt', 'access:admin', 'config']);- expect(mockHandlers[handlerName]).toHaveBeenCalledTimes(1);- });+ [+ 'GET',+ '/api/admin/langfuse/connection/session/conversation-1',+ 'getSessionLink',+ { handler: 'session' },+ ],+ ['PUT', '/api/admin/langfuse/connection', 'updateConnection', { handler: 'update' }],+ ['POST', '/api/admin/langfuse/connection/test', 'testConnection', { handler: 'test' }],+ ['PUT', '/api/admin/langfuse/prompt-sync', 'updatePromptSync', { handler: 'prompt-sync' }],+ ['GET', '/api/admin/langfuse/prompts', 'listPrompts', { handler: 'prompts' }],+ [+ 'GET',+ '/api/admin/langfuse/prompts/greeting',+ 'getPrompt',+ { handler: 'prompt', name: 'greeting' },+ ],+ ])(+ 'requires Langfuse manage access for %s %s',+ async (method, path, handlerName, expectedBody) => {+ const app = createApp();+ const response = await request(app)[method.toLowerCase()](path).send({}).expect(200);++ expect(response.body).toEqual(expectedBody);+ expect(middlewareCalls).toEqual(['jwt', 'access:admin', 'config']);+ expect(mockHandlers[handlerName]).toHaveBeenCalledTimes(1);+ },+ );it.each([['GET', '/api/admin/langfuse/connection/session/conversation-1', 'getSessionLink'],['PUT', '/api/admin/langfuse/connection', 'updateConnection'],['POST', '/api/admin/langfuse/connection/test', 'testConnection'],+ ['PUT', '/api/admin/langfuse/prompt-sync', 'updatePromptSync'],+ ['GET', '/api/admin/langfuse/prompts', 'listPrompts'],+ ['GET', '/api/admin/langfuse/prompts/greeting', 'getPrompt'],])('blocks %s %s without Langfuse manage access', async (method, path, handlerName) => {canManageLangfuse = false;@@ -127,6 +146,39 @@ describe('admin Langfuse routes', () => {expect(mockHandlers[handlerName]).not.toHaveBeenCalled();});+ it.each([+ ['GET', '/api/admin/langfuse/prompts', 'listPrompts'],+ ['GET', '/api/admin/langfuse/prompts/greeting', 'getPrompt'],+ ])('rejects %s %s for an unauthenticated caller', async (method, path, handlerName) => {+ mockRequireJwtAuth.mockImplementationOnce((_req, res) => {+ middlewareCalls.push('jwt');+ res.status(401).json({ message: 'Authentication required' });+ });++ await request(createApp())[method.toLowerCase()](path).expect(401);++ expect(mockHandlers[handlerName]).not.toHaveBeenCalled();+ });++ it.each([+ ['GET', '/api/admin/langfuse/prompts', 'listPrompts'],+ ['GET', '/api/admin/langfuse/prompts/greeting', 'getPrompt'],+ ])('rejects %s %s for a non-admin caller', async (method, path, handlerName) => {+ deniedCapability = 'access:admin';++ await request(createApp())[method.toLowerCase()](path).expect(403);++ expect(mockHandlers[handlerName]).not.toHaveBeenCalled();+ });++ it('decodes an encoded slash in the prompt name before it reaches the handler', async () => {+ await request(createApp()).get('/api/admin/langfuse/prompts/a%2Fb').expect(200);++ expect(mockHandlers.getPrompt).toHaveBeenCalledTimes(1);+ const [req] = mockHandlers.getPrompt.mock.calls[0];+ expect(req.params.name).toBe('a/b');+ });+/*** Credential verification reads the deployment's Langfuse headers off* `req.config`. The handler unit tests inject `config` into their mockdiff --git a/api/server/routes/config.js b/api/server/routes/config.jsindex 65d1b26c22..b7908e24ea 100644--- a/api/server/routes/config.js+++ b/api/server/routes/config.js@@ -3,7 +3,8 @@ const {isEnabled,resolveEmailChangeSettings,checkEmailConfig,- isLangfuseConnectionAvailable,+ isLangfuseSettingsAvailable,+ getLangfuseSettingsAccess,isLangfuseFanoutEnabled,getBalanceConfig,getCloudFrontConfig,@@ -275,10 +276,10 @@ router.get('/', async function (req, res) {const balanceConfig = getBalanceConfig(appConfig);const cloudFront = buildCloudFrontStartupConfig();const langfuseFanoutEnabled = isLangfuseFanoutEnabled();- const langfuseConnectionAvailable = isLangfuseConnectionAvailable();- let langfuseConnectionAccess = false;+ const langfuseSettingsAvailable = isLangfuseSettingsAvailable();+ let canManageLangfuse = false;- if (langfuseConnectionAvailable) {+ if (langfuseSettingsAvailable) {try {const userId = req.user.id ?? req.user._id?.toString();if (userId) {@@ -293,7 +294,7 @@ router.get('/', async function (req, res) {SystemCapabilities.ACCESS_ADMIN,);if (hasAdminAccess) {- langfuseConnectionAccess = await hasConfigCapability(capabilityUser, 'langfuse');+ canManageLangfuse = await hasConfigCapability(capabilityUser, 'langfuse');}}} catch (err) {@@ -329,7 +330,7 @@ router.get('/', async function (req, res) {? parseInt(process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES, 10): 0,langfuseFanoutEnabled,- langfuseConnectionAccess,+ ...getLangfuseSettingsAccess(canManageLangfuse),insightsEnabled: isEnabled(process.env.ENABLE_INSIGHTS),/** Lets the client skip the pull request lookup entirely when the feature is off. */...resolvePullRequestCapabilities(appConfig, process.env),diff --git a/client/src/components/Nav/Settings/__tests__/Sidebar.spec.tsx b/client/src/components/Nav/Settings/__tests__/Sidebar.spec.tsxindex 0c42e897c0..57c63974bd 100644--- a/client/src/components/Nav/Settings/__tests__/Sidebar.spec.tsx+++ b/client/src/components/Nav/Settings/__tests__/Sidebar.spec.tsx@@ -64,6 +64,11 @@ describe('Sidebar', () => {expect(screen.getByText('Langfuse')).toBeInTheDocument();});+ it('shows the Langfuse tab with prompt access and no connection access', () => {+ setup({ langfuseConnectionAccess: false, langfusePromptSyncAccess: true });+ expect(screen.getByText('Langfuse')).toBeInTheDocument();+ });+it('hides the Langfuse tab without Langfuse connection access', () => {setup({ langfuseConnectionAccess: false });expect(screen.queryByText('Langfuse')).not.toBeInTheDocument();diff --git a/client/src/components/Nav/Settings/__tests__/registry.spec.ts b/client/src/components/Nav/Settings/__tests__/registry.spec.tsindex fd7d4d7de4..4514c0085d 100644--- a/client/src/components/Nav/Settings/__tests__/registry.spec.ts+++ b/client/src/components/Nav/Settings/__tests__/registry.spec.ts@@ -137,6 +137,16 @@ describe('settings registry', () => {).toBe(true);});+ it('shows prompt settings without connection access', () => {+ expect(+ langfuseEntry?.show?.({+ ...settingsContext,+ langfuseConnectionAccess: false,+ langfusePromptSyncAccess: true,+ }),+ ).toBe(true);+ });+it('hides the connection without Langfuse config access', () => {expect(langfuseEntry?.show?.({diff --git a/client/src/components/Nav/Settings/context.tsx b/client/src/components/Nav/Settings/context.tsxindex 157cc20dd6..66e90f5cb9 100644--- a/client/src/components/Nav/Settings/context.tsx+++ b/client/src/components/Nav/Settings/context.tsx@@ -36,6 +36,7 @@ export function useSettingsContext(): SettingsContextValue {const balanceEnabled = startupConfig?.balance?.enabled === true;const langfuseConnectionAccess = startupConfig?.langfuseConnectionAccess === true;+ const langfusePromptSyncAccess = startupConfig?.langfusePromptSyncAccess === true;const adminPanelURL = startupConfig?.adminPanelURL ?? '';const isLocalProvider = user?.provider === 'local';const emailEnabled = startupConfig?.emailEnabled === true;@@ -92,6 +93,7 @@ export function useSettingsContext(): SettingsContextValue {mascotAllowed,engineTTS,langfuseConnectionAccess,+ langfusePromptSyncAccess,adminPanelURL,replyTabBadgeAllowed,replyNotificationsAllowed,@@ -119,6 +121,7 @@ export function useSettingsContext(): SettingsContextValue {mascotAllowed,engineTTS,langfuseConnectionAccess,+ langfusePromptSyncAccess,adminPanelURL,replyTabBadgeAllowed,replyNotificationsAllowed,diff --git a/client/src/components/Nav/Settings/registry.tsx b/client/src/components/Nav/Settings/registry.tsxindex e1a0bea798..aafd7688aa 100644--- a/client/src/components/Nav/Settings/registry.tsx+++ b/client/src/components/Nav/Settings/registry.tsx@@ -768,7 +768,7 @@ export const registry: SettingEntry[] = [section: 'langfuse',labelKey: 'com_ui_langfuse_title',keywords: ['langfuse', 'observability', 'tracing', 'telemetry', 'traces'],- show: (ctx) => ctx.langfuseConnectionAccess,+ show: (ctx) => ctx.langfuseConnectionAccess || ctx.langfusePromptSyncAccess === true,Component: LangfuseConnection,},// Data controls · Danger zonediff --git a/client/src/components/Nav/Settings/types.ts b/client/src/components/Nav/Settings/types.tsindex 098c6851f6..31b0d7a7d9 100644--- a/client/src/components/Nav/Settings/types.ts+++ b/client/src/components/Nav/Settings/types.ts@@ -60,6 +60,7 @@ export interface SettingsContextValue {mascotAllowed: boolean;engineTTS: string;langfuseConnectionAccess: boolean;+ langfusePromptSyncAccess?: boolean;adminPanelURL: string;replyTabBadgeAllowed: boolean;replyNotificationsAllowed: boolean;@@ -148,7 +149,7 @@ export const TABS: TabMeta[] = [icon: createLangfuseIcon('h-3.5 w-3.5'),},],- show: (ctx) => ctx.langfuseConnectionAccess,+ show: (ctx) => ctx.langfuseConnectionAccess || ctx.langfusePromptSyncAccess === true,},{id: SettingsTabValues.DATA,diff --git a/client/src/components/Nav/SettingsTabs/Integrations/LangfuseConnection.tsx b/client/src/components/Nav/SettingsTabs/Integrations/LangfuseConnection.tsxindex 0287214e84..ca0117e842 100644--- a/client/src/components/Nav/SettingsTabs/Integrations/LangfuseConnection.tsx+++ b/client/src/components/Nav/SettingsTabs/Integrations/LangfuseConnection.tsx@@ -8,6 +8,7 @@ import {Label,SecretInput,Spinner,+ Switch,ESide,useToastContext,} from '@librechat/client';@@ -20,7 +21,9 @@ import {useGetLangfuseConnectionQuery,useUpdateLangfuseConnectionMutation,useTestLangfuseConnectionMutation,+ useUpdateLangfusePromptSyncMutation,} from '~/data-provider';+import LangfusePromptsDialog from './LangfusePromptsDialog';import { useLocalize } from '~/hooks';type ConnectionTestState = 'idle' | 'unverified' | 'checking' | 'connected' | 'failed';@@ -110,6 +113,7 @@ export default function LangfuseConnection() {} = useGetLangfuseConnectionQuery();const updateMutation = useUpdateLangfuseConnectionMutation();const testMutation = useTestLangfuseConnectionMutation();+ const promptSyncMutation = useUpdateLangfusePromptSyncMutation();const [connectionStatus, setConnectionStatus] = useState<TLangfuseConnectionStatus>();const [destination, setDestination] = useState('');@@ -119,6 +123,8 @@ export default function LangfuseConnection() {const [isEditingSecretKey, setIsEditingSecretKey] = useState(false);const [connectionTestState, setConnectionTestState] = useState<ConnectionTestState>('idle');const [connectionTestMessage, setConnectionTestMessage] = useState('');+ const [promptSyncError, setPromptSyncError] = useState(false);+ const [isPromptsDialogOpen, setIsPromptsDialogOpen] = useState(false);const autoTestedConnectionRef = useRef<string>();const connectionTestRequestRef = useRef(0);const publicKeyInputRef = useRef<HTMLInputElement>(null);@@ -143,14 +149,15 @@ export default function LangfuseConnection() {setConnectionStatus(status);}, [status]);+ // Scoped to the two fields this syncs, not the whole `connectionStatus` object,+ // so an unrelated update (e.g. toggling prompt sync) cannot reset unsaved edits+ // to the destination or public key.useEffect(() => {- if (!connectionStatus) {- return;- }- setDestination(connectionStatus.destination ?? '');- setPublicKey(connectionStatus.publicKey ?? '');- }, [connectionStatus]);+ setDestination(connectionStatus?.destination ?? '');+ setPublicKey(connectionStatus?.publicKey ?? '');+ }, [connectionStatus?.destination, connectionStatus?.publicKey]);+ const connectionAvailable = connectionStatus?.connectionAvailable !== false;const secretConfigured = connectionStatus?.configured === true;const destinations = connectionStatus?.destinations ?? [];const connectionDestinationAvailable = destinations.some(@@ -189,11 +196,12 @@ export default function LangfuseConnection() {destination !== '' &&trimmedPublicKey !== '' &&((!connectionCredentialsChanged && secretConfigured) || trimmedSecretKey !== '');- const busy = testMutation.isLoading || updateMutation.isLoading;+ const isSavingStatus = updateMutation.isLoading || promptSyncMutation.isLoading;+ const busy = testMutation.isLoading || isSavingStatus;useEffect(() => {const storedConnectionTestKey = getStoredConnectionTestKey(connectionStatus);- if (!connectionStatus) {+ if (!connectionStatus || !connectionAvailable) {return;}if (!storedConnectionTestKey) {@@ -238,7 +246,7 @@ export default function LangfuseConnection() {},},);- }, [connectionStatus, localize, testMutation]);+ }, [connectionStatus, connectionAvailable, localize, testMutation]);const connectionStatusLabel =connectionTestState === 'failed' && connectionTestMessage !== ''@@ -364,6 +372,25 @@ export default function LangfuseConnection() {saveEnabledState();};+ const handlePromptSyncChange = (nextEnabled: boolean) => {+ setPromptSyncError(false);+ // The dialog unmounts once the switch is off; reset `open` so re-enabling keeps it closed.+ if (!nextEnabled) {+ setIsPromptsDialogOpen(false);+ }+ promptSyncMutation.mutate(+ { enabled: nextEnabled },+ {+ onSuccess: (nextStatus) => {+ setConnectionStatus(nextStatus);+ },+ onError: () => {+ setPromptSyncError(true);+ },+ },+ );+ };+if (isConnectionLoading && connectionStatus == null) {return (<div@@ -406,146 +433,189 @@ export default function LangfuseConnection() {</span></InfoHoverCard></div>- <div- data-testid="langfuse-connection-status"- className={`ms-auto flex max-w-full shrink-0 items-start justify-end gap-1.5 text-end text-xs sm:max-w-[50%] ${connectionStatusTextClass}`}- aria-live="polite"- title={connectionStatusTitle}- >- {connectionTestState === 'checking' ? (- <Spinner className="h-3 w-3" />- ) : (- <span className={`mt-1 h-2 w-2 shrink-0 rounded-full ${connectionStatusDotClass}`} />- )}- <span>{connectionStatusLabel}</span>- </div>- </div>-- <div className="flex flex-col gap-1.5">- <Label id="langfuse-destination-label">{localize('com_ui_langfuse_destination')}</Label>- <Dropdown- value={destination}- label={destination === '' ? localize('com_ui_select') : ''}- onChange={handleDestinationChange}- options={destinationOptions}- disabled={destinations.length === 0 || busy}- className="w-full"- triggerClassName="w-full"- sizeClasses="z-50 w-[var(--popover-anchor-width)]"- testId="langfuse-destination"- aria-labelledby="langfuse-destination-label"- />- </div>-- <div className="flex flex-col gap-1.5">- <Label htmlFor="langfuse-public-token">{localize('com_ui_langfuse_public_key')}</Label>- {secretConfigured && !isEditingPublicKey && (- <button- type="button"- className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"- aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_public_key')}`}- disabled={busy}- onClick={() => setIsEditingPublicKey(true)}+ {connectionAvailable && (+ <div+ data-testid="langfuse-connection-status"+ className={`ms-auto flex max-w-full shrink-0 items-start justify-end gap-1.5 text-end text-xs sm:max-w-[50%] ${connectionStatusTextClass}`}+ aria-live="polite"+ title={connectionStatusTitle}>- <code className="text-text-primary block min-w-0 truncate font-mono text-sm">- {displayPublicKey}- </code>- </button>- )}- {publicKeyInputVisible && (- <Input- ref={publicKeyInputRef}- id="langfuse-public-token"- autoComplete="off"- data-lpignore="true"- data-1p-ignore="true"- data-bwignore="true"- data-form-type="other"- value={publicKey}- disabled={busy}- placeholder="pk-lf-..."- onChange={(e) => {- connectionTestRequestRef.current += 1;- const nextPublicKey = e.target.value;- setPublicKey(nextPublicKey);- if (- secretConfigured &&- nextPublicKey.trim() !== (connectionStatus?.publicKey ?? '')- ) {- setIsEditingSecretKey(true);- }- setConnectionTestState('unverified');- setConnectionTestMessage('');- }}- />+ {connectionTestState === 'checking' ? (+ <Spinner className="h-3 w-3" />+ ) : (+ <span className={`mt-1 h-2 w-2 shrink-0 rounded-full ${connectionStatusDotClass}`} />+ )}+ <span>{connectionStatusLabel}</span>+ </div>)}</div>- <div className="flex flex-col gap-1.5">- <Label htmlFor="langfuse-private-token">{localize('com_ui_langfuse_secret_key')}</Label>- {secretConfigured && !isEditingSecretKey && (- <button- type="button"- className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"- aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_secret_key')}`}- disabled={busy}- onClick={() => setIsEditingSecretKey(true)}- >- <code className="text-text-primary block min-w-0 truncate font-mono text-sm">- {connectionStatus?.secretKeyPreview}- </code>- </button>- )}- {secretInputVisible && (- <SecretInput- ref={secretKeyInputRef}- id="langfuse-private-token"- autoComplete="off"- data-lpignore="true"- data-1p-ignore="true"- data-bwignore="true"- data-form-type="other"- value={secretKey}- disabled={busy}- placeholder="sk-lf-..."- onChange={(e) => {- connectionTestRequestRef.current += 1;- setSecretKey(e.target.value);- setConnectionTestState('unverified');- setConnectionTestMessage('');- }}- />- )}- </div>+ {connectionAvailable && (+ <>+ <div className="flex flex-col gap-1.5">+ <Label id="langfuse-destination-label">{localize('com_ui_langfuse_destination')}</Label>+ <Dropdown+ value={destination}+ label={destination === '' ? localize('com_ui_select') : ''}+ onChange={handleDestinationChange}+ options={destinationOptions}+ disabled={destinations.length === 0 || busy}+ className="w-full"+ triggerClassName="w-full"+ sizeClasses="z-50 w-[var(--popover-anchor-width)]"+ testId="langfuse-destination"+ aria-labelledby="langfuse-destination-label"+ />+ </div>- <div className="flex min-h-9 items-center justify-end gap-2">- {isEditing ? (- <Button variant="submit" disabled={!canSubmit || busy} onClick={handleSave}>- {testMutation.isLoading ? (- <span className="flex items-center gap-2">- <Spinner className="h-4 w-4" />- {localize('com_ui_langfuse_testing')}- </span>- ) : (- localize('com_ui_save')+ <div className="flex flex-col gap-1.5">+ <Label htmlFor="langfuse-public-token">{localize('com_ui_langfuse_public_key')}</Label>+ {secretConfigured && !isEditingPublicKey && (+ <button+ type="button"+ className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"+ aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_public_key')}`}+ disabled={busy}+ onClick={() => setIsEditingPublicKey(true)}+ >+ <code className="text-text-primary block min-w-0 truncate font-mono text-sm">+ {displayPublicKey}+ </code>+ </button>)}- </Button>- ) : (- <Button- variant={connectionStatus?.enabled === true ? 'outline' : 'submit'}- disabled={- busy || (connectionStatus?.enabled !== true && !connectionDestinationAvailable)- }- onClick={handleEnabledChange}- >- {localize(- connectionStatus?.enabled === true- ? 'com_ui_langfuse_disable'- : 'com_ui_langfuse_enable',+ {publicKeyInputVisible && (+ <Input+ ref={publicKeyInputRef}+ id="langfuse-public-token"+ autoComplete="off"+ data-lpignore="true"+ data-1p-ignore="true"+ data-bwignore="true"+ data-form-type="other"+ value={publicKey}+ disabled={busy}+ placeholder="pk-lf-..."+ onChange={(e) => {+ connectionTestRequestRef.current += 1;+ const nextPublicKey = e.target.value;+ setPublicKey(nextPublicKey);+ if (+ secretConfigured &&+ nextPublicKey.trim() !== (connectionStatus?.publicKey ?? '')+ ) {+ setIsEditingSecretKey(true);+ }+ setConnectionTestState('unverified');+ setConnectionTestMessage('');+ }}+ />)}- </Button>- )}- </div>+ </div>++ <div className="flex flex-col gap-1.5">+ <Label htmlFor="langfuse-private-token">{localize('com_ui_langfuse_secret_key')}</Label>+ {secretConfigured && !isEditingSecretKey && (+ <button+ type="button"+ className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"+ aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_secret_key')}`}+ disabled={busy}+ onClick={() => setIsEditingSecretKey(true)}+ >+ <code className="text-text-primary block min-w-0 truncate font-mono text-sm">+ {connectionStatus?.secretKeyPreview}+ </code>+ </button>+ )}+ {secretInputVisible && (+ <SecretInput+ ref={secretKeyInputRef}+ id="langfuse-private-token"+ autoComplete="off"+ data-lpignore="true"+ data-1p-ignore="true"+ data-bwignore="true"+ data-form-type="other"+ value={secretKey}+ disabled={busy}+ placeholder="sk-lf-..."+ onChange={(e) => {+ connectionTestRequestRef.current += 1;+ setSecretKey(e.target.value);+ setConnectionTestState('unverified');+ setConnectionTestMessage('');+ }}+ />+ )}+ </div>++ <div className="flex min-h-9 items-center justify-end gap-2">+ {isEditing ? (+ <Button variant="submit" disabled={!canSubmit || busy} onClick={handleSave}>+ {testMutation.isLoading ? (+ <span className="flex items-center gap-2">+ <Spinner className="h-4 w-4" />+ {localize('com_ui_langfuse_testing')}+ </span>+ ) : (+ localize('com_ui_save')+ )}+ </Button>+ ) : (+ <Button+ variant={connectionStatus?.enabled === true ? 'outline' : 'submit'}+ disabled={+ busy || (connectionStatus?.enabled !== true && !connectionDestinationAvailable)+ }+ onClick={handleEnabledChange}+ >+ {localize(+ connectionStatus?.enabled === true+ ? 'com_ui_langfuse_disable'+ : 'com_ui_langfuse_enable',+ )}+ </Button>+ )}+ </div>+ </>+ )}++ {connectionStatus?.promptSync?.available === true && (+ <div className="border-border-light flex flex-col gap-2 border-t pt-4">+ <div className="flex items-center justify-between gap-4">+ <div className="flex flex-col gap-0.5">+ <Label id="langfuse-prompt-sync-label">+ {localize('com_ui_langfuse_prompt_sync')}+ </Label>+ <span className="text-text-secondary text-xs">+ {localize('com_ui_langfuse_prompt_sync_description')}+ </span>+ </div>+ <Switch+ checked={connectionStatus.promptSync.enabled}+ onCheckedChange={handlePromptSyncChange}+ disabled={isSavingStatus}+ aria-labelledby="langfuse-prompt-sync-label"+ />+ </div>+ {promptSyncError && (+ <p className="text-text-destructive text-xs">+ {localize('com_ui_langfuse_prompt_sync_error')}+ </p>+ )}+ {connectionStatus.promptSync.enabled && (+ <div>+ <Button variant="outline" size="sm" onClick={() => setIsPromptsDialogOpen(true)}>+ {localize('com_ui_langfuse_prompt_sync_open')}+ </Button>+ </div>+ )}+ </div>+ )}++ {connectionStatus?.promptSync?.enabled === true && (+ <LangfusePromptsDialog open={isPromptsDialogOpen} onOpenChange={setIsPromptsDialogOpen} />+ )}</div>);}diff --git a/client/src/components/Nav/SettingsTabs/Integrations/LangfusePromptsDialog.tsx b/client/src/components/Nav/SettingsTabs/Integrations/LangfusePromptsDialog.tsxnew file mode 100644index 0000000000..8f1862c91b--- /dev/null+++ b/client/src/components/Nav/SettingsTabs/Integrations/LangfusePromptsDialog.tsx@@ -0,0 +1,456 @@+import { useId, useState } from 'react';+import axios from 'axios';+import {+ Button,+ Input,+ Label,+ OGDialog,+ OGDialogContent,+ OGDialogHeader,+ OGDialogTitle,+ Radio,+ Spinner,+ Table,+ TableBody,+ TableCell,+ TableHead,+ TableHeader,+ TableRow,+ Textarea,+} from '@librechat/client';+import type {+ TLangfusePromptErrorBody,+ TLangfusePromptErrorCode,+ TLangfusePromptGetParams,+ TLangfusePromptListParams,+} from 'librechat-data-provider';+import type { FormEvent } from 'react';+import type { TranslationKeys } from '~/hooks';+import { useLangfusePromptQuery, useLangfusePromptsQuery } from '~/data-provider';+import { getResponseErrorCode } from '~/utils';+import { useLocalize } from '~/hooks';++type LangfusePromptsDialogProps = {+ open: boolean;+ onOpenChange: (open: boolean) => void;+};++type GetMode = 'production' | 'version';++const DEFAULT_PAGE = 1;+const DEFAULT_LIMIT = 10;+const MAX_LIMIT = 100;++function isGetMode(value: string): value is GetMode {+ return value === 'production' || value === 'version';+}++/** Codes whose whole copy is one localized sentence; `upstream` is handled separately below+ * because its sentence needs the Langfuse HTTP status when the server sent one. */+const PROMPT_ERROR_MESSAGE_KEYS: Partial<Record<TLangfusePromptErrorCode, TranslationKeys>> = {+ not_configured: 'com_ui_langfuse_prompt_error_not_configured',+ unauthorized: 'com_ui_langfuse_prompt_error_unauthorized',+ timeout: 'com_ui_langfuse_prompt_error_timeout',+ invalid_response: 'com_ui_langfuse_prompt_error_invalid_response',+ not_found: 'com_ui_langfuse_prompt_error_not_found',+ unsupported_type: 'com_ui_langfuse_prompt_error_unsupported_type',+ invalid_request: 'com_ui_langfuse_prompt_error_invalid_request',+};++function getUpstreamStatus(error: unknown): number | undefined {+ if (!axios.isAxiosError<TLangfusePromptErrorBody>(error)) {+ return undefined;+ }+ const status = error.response?.data?.status;+ return typeof status === 'number' ? status : undefined;+}++/** Maps a failed list or get call to one short localized line, with a generic fallback for an+ * unmapped or missing code (for example a bare 404 with no body, which means the feature is off). */+function getPromptErrorMessage(error: unknown, localize: ReturnType<typeof useLocalize>): string {+ const code = getResponseErrorCode<TLangfusePromptErrorCode>(error);+ if (code === 'upstream') {+ const status = getUpstreamStatus(error);+ return status != null+ ? localize('com_ui_langfuse_prompt_error_upstream_status', { 0: status })+ : localize('com_ui_langfuse_prompt_error_upstream');+ }+ const messageKey = code != null ? PROMPT_ERROR_MESSAGE_KEYS[code] : undefined;+ return localize(messageKey ?? 'com_ui_langfuse_prompt_error_fallback');+}++/** A whole, positive integer, the way page, limit and version numbers must read. Rejects+ * fractional input (`"1.5"`) and blanks instead of silently rounding or defaulting. */+function parsePositiveInt(value: string): number | undefined {+ const parsed = Number(value);+ return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;+}++function toIsoDateTime(value: string): string | undefined {+ if (value.trim() === '') {+ return undefined;+ }+ const date = new Date(value);+ return Number.isNaN(date.getTime()) ? undefined : date.toISOString();+}++function buildListParams(fields: {+ name: string;+ label: string;+ tag: string;+ page: number;+ limit: number;+ fromUpdatedAt: string;+ toUpdatedAt: string;+}): TLangfusePromptListParams {+ const params: TLangfusePromptListParams = {+ page: fields.page,+ limit: fields.limit,+ };+ if (fields.name.trim() !== '') {+ params.name = fields.name.trim();+ }+ if (fields.label.trim() !== '') {+ params.label = fields.label.trim();+ }+ if (fields.tag.trim() !== '') {+ params.tag = fields.tag.trim();+ }+ const fromUpdatedAt = toIsoDateTime(fields.fromUpdatedAt);+ if (fromUpdatedAt != null) {+ params.fromUpdatedAt = fromUpdatedAt;+ }+ const toUpdatedAt = toIsoDateTime(fields.toUpdatedAt);+ if (toUpdatedAt != null) {+ params.toUpdatedAt = toUpdatedAt;+ }+ return params;+}++/**+ * A test-only, read-only "dry run" of the two Langfuse prompt calls: list prompts with filters,+ * and resolve one prompt by Production or an exact version. Nothing is written. Shown from+ * Settings ▸ Langfuse once the admin turns the "Langfuse prompts" switch on.+ */+export default function LangfusePromptsDialog({ open, onOpenChange }: LangfusePromptsDialogProps) {+ const localize = useLocalize();+ const dryRunNoticeId = useId();+ const listHeadingId = useId();+ const getHeadingId = useId();+ const getModeLabelId = useId();+ const listNameId = useId();+ const listLabelId = useId();+ const listTagId = useId();+ const listPageId = useId();+ const listLimitId = useId();+ const listFromId = useId();+ const listToId = useId();+ const getNameId = useId();++ const [listName, setListName] = useState('');+ const [listLabel, setListLabel] = useState('');+ const [listTag, setListTag] = useState('');+ const [listPage, setListPage] = useState(String(DEFAULT_PAGE));+ const [listLimit, setListLimit] = useState(String(DEFAULT_LIMIT));+ const [listFromUpdatedAt, setListFromUpdatedAt] = useState('');+ const [listToUpdatedAt, setListToUpdatedAt] = useState('');++ const [submittedListParams, setSubmittedListParams] = useState<TLangfusePromptListParams | null>(+ null,+ );+ const [listSubmitCount, setListSubmitCount] = useState(0);++ const parsedListPage = parsePositiveInt(listPage);+ const parsedListLimit = parsePositiveInt(listLimit);+ const canSubmitList =+ parsedListPage != null && parsedListLimit != null && parsedListLimit <= MAX_LIMIT;++ const listQuery = useLangfusePromptsQuery(submittedListParams ?? undefined, listSubmitCount);++ const handleListSubmit = (event: FormEvent<HTMLFormElement>) => {+ event.preventDefault();+ if (!canSubmitList || parsedListPage == null || parsedListLimit == null) {+ return;+ }+ setSubmittedListParams(+ buildListParams({+ name: listName,+ label: listLabel,+ tag: listTag,+ page: parsedListPage,+ limit: parsedListLimit,+ fromUpdatedAt: listFromUpdatedAt,+ toUpdatedAt: listToUpdatedAt,+ }),+ );+ setListSubmitCount((count) => count + 1);+ };++ const [getName, setGetName] = useState('');+ const [getMode, setGetMode] = useState<GetMode>('production');+ const [getVersion, setGetVersion] = useState('');++ const [submittedGetParams, setSubmittedGetParams] = useState<TLangfusePromptGetParams | null>(+ null,+ );+ const [getSubmitCount, setGetSubmitCount] = useState(0);++ const trimmedGetName = getName.trim();+ const parsedGetVersion = parsePositiveInt(getVersion);+ const getVersionValid = parsedGetVersion != null;+ const canSubmitGet = trimmedGetName !== '' && (getMode === 'production' || getVersionValid);++ const getQuery = useLangfusePromptQuery(submittedGetParams ?? undefined, getSubmitCount);++ const handleGetSubmit = (event: FormEvent<HTMLFormElement>) => {+ event.preventDefault();+ if (!canSubmitGet) {+ return;+ }+ setSubmittedGetParams(+ getMode === 'version' && parsedGetVersion != null+ ? { name: trimmedGetName, version: parsedGetVersion }+ : { name: trimmedGetName },+ );+ setGetSubmitCount((count) => count + 1);+ };++ const listItems = listQuery.data?.items ?? [];+ const listMeta = listQuery.data?.meta;+ const listErrorMessage = listQuery.isError+ ? getPromptErrorMessage(listQuery.error, localize)+ : undefined;+ const getErrorMessage = getQuery.isError+ ? getPromptErrorMessage(getQuery.error, localize)+ : undefined;++ return (+ <OGDialog open={open} onOpenChange={onOpenChange}>+ <OGDialogContent className="w-11/12 max-w-3xl" aria-describedby={dryRunNoticeId}>+ <OGDialogHeader>+ <OGDialogTitle>{localize('com_ui_langfuse_prompts_dialog_title')}</OGDialogTitle>+ </OGDialogHeader>+ <p id={dryRunNoticeId} className="text-text-secondary text-xs">+ {localize('com_ui_langfuse_prompts_dry_run_notice')}+ </p>++ <section+ className="border-border-light flex flex-col gap-3 border-t pt-4"+ aria-labelledby={listHeadingId}+ >+ <h3 id={listHeadingId} className="text-text-primary text-sm font-medium">+ {localize('com_ui_langfuse_prompts_list_title')}+ </h3>+ <form onSubmit={handleListSubmit} className="flex flex-col gap-3">+ <div className="grid grid-cols-2 gap-3 sm:grid-cols-3">+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listNameId}>{localize('com_ui_langfuse_prompts_list_name')}</Label>+ <Input+ id={listNameId}+ value={listName}+ onChange={(e) => setListName(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listLabelId}>+ {localize('com_ui_langfuse_prompts_list_label')}+ </Label>+ <Input+ id={listLabelId}+ value={listLabel}+ onChange={(e) => setListLabel(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listTagId}>{localize('com_ui_langfuse_prompts_list_tag')}</Label>+ <Input+ id={listTagId}+ value={listTag}+ onChange={(e) => setListTag(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listPageId}>{localize('com_ui_langfuse_prompts_list_page')}</Label>+ <Input+ id={listPageId}+ type="number"+ min={1}+ value={listPage}+ onChange={(e) => setListPage(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listLimitId}>+ {localize('com_ui_langfuse_prompts_list_limit')}+ </Label>+ <Input+ id={listLimitId}+ type="number"+ min={1}+ max={MAX_LIMIT}+ value={listLimit}+ onChange={(e) => setListLimit(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listFromId}>{localize('com_ui_langfuse_prompts_list_from')}</Label>+ <Input+ id={listFromId}+ type="datetime-local"+ value={listFromUpdatedAt}+ onChange={(e) => setListFromUpdatedAt(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={listToId}>{localize('com_ui_langfuse_prompts_list_to')}</Label>+ <Input+ id={listToId}+ type="datetime-local"+ value={listToUpdatedAt}+ onChange={(e) => setListToUpdatedAt(e.target.value)}+ />+ </div>+ </div>+ <div>+ <Button type="submit" disabled={!canSubmitList || listQuery.isFetching}>+ {listQuery.isFetching && <Spinner className="h-4 w-4" />}+ {localize('com_ui_langfuse_prompts_list_button')}+ </Button>+ </div>+ </form>+ <div aria-live="polite" className="flex flex-col gap-2">+ {listErrorMessage != null && (+ <p className="text-text-destructive text-sm">{listErrorMessage}</p>+ )}+ {listErrorMessage == null && listQuery.isFetched && listItems.length === 0 && (+ <p className="text-text-secondary text-sm">+ {localize('com_ui_langfuse_prompts_list_empty')}+ </p>+ )}+ {listErrorMessage == null && listItems.length > 0 && (+ <>+ <Table>+ <TableHeader>+ <TableRow>+ <TableHead scope="col">+ {localize('com_ui_langfuse_prompts_column_name')}+ </TableHead>+ <TableHead scope="col">+ {localize('com_ui_langfuse_prompts_column_type')}+ </TableHead>+ <TableHead scope="col">+ {localize('com_ui_langfuse_prompts_column_versions')}+ </TableHead>+ <TableHead scope="col">+ {localize('com_ui_langfuse_prompts_column_labels')}+ </TableHead>+ <TableHead scope="col">+ {localize('com_ui_langfuse_prompts_column_tags')}+ </TableHead>+ </TableRow>+ </TableHeader>+ <TableBody>+ {listItems.map((item) => (+ <TableRow key={item.name}>+ <TableCell>{item.name}</TableCell>+ <TableCell>{item.type}</TableCell>+ <TableCell>{item.versions.join(', ')}</TableCell>+ <TableCell>{item.labels.join(', ')}</TableCell>+ <TableCell>{item.tags.join(', ')}</TableCell>+ </TableRow>+ ))}+ </TableBody>+ </Table>+ {listMeta != null && (+ <p className="text-text-secondary text-xs">+ {localize('com_ui_langfuse_prompts_list_footer', {+ 0: listMeta.page,+ 1: listMeta.totalPages,+ 2: listMeta.totalItems,+ })}+ </p>+ )}+ </>+ )}+ </div>+ </section>++ <section+ className="border-border-light flex flex-col gap-3 border-t pt-4"+ aria-labelledby={getHeadingId}+ >+ <h3 id={getHeadingId} className="text-text-primary text-sm font-medium">+ {localize('com_ui_langfuse_prompts_get_title')}+ </h3>+ <form onSubmit={handleGetSubmit} className="flex flex-col gap-3">+ <div className="flex flex-col gap-1.5">+ <Label htmlFor={getNameId}>{localize('com_ui_langfuse_prompts_get_name')}</Label>+ <Input id={getNameId} value={getName} onChange={(e) => setGetName(e.target.value)} />+ </div>+ <div className="flex flex-col gap-1.5">+ <Label id={getModeLabelId}>{localize('com_ui_langfuse_prompts_get_resolve')}</Label>+ <div className="flex flex-wrap items-center gap-3">+ <Radio+ aria-labelledby={getModeLabelId}+ value={getMode}+ onChange={(value) => {+ if (isGetMode(value)) {+ setGetMode(value);+ }+ }}+ options={[+ {+ value: 'production',+ label: localize('com_ui_langfuse_prompts_get_production'),+ },+ {+ value: 'version',+ label: localize('com_ui_langfuse_prompts_get_version'),+ },+ ]}+ />+ {getMode === 'version' && (+ <Input+ type="number"+ min={1}+ aria-label={localize('com_ui_langfuse_prompts_get_version')}+ value={getVersion}+ onChange={(e) => setGetVersion(e.target.value)}+ className="w-24"+ />+ )}+ </div>+ </div>+ <div>+ <Button type="submit" disabled={!canSubmitGet || getQuery.isFetching}>+ {getQuery.isFetching && <Spinner className="h-4 w-4" />}+ {localize('com_ui_langfuse_prompts_get_button')}+ </Button>+ </div>+ </form>+ <div aria-live="polite" className="flex flex-col gap-2">+ {getErrorMessage != null && (+ <p className="text-text-destructive text-sm">{getErrorMessage}</p>+ )}+ {getErrorMessage == null && getQuery.data != null && (+ <div className="flex flex-col gap-2">+ <p className="text-text-secondary text-sm">+ {localize('com_ui_langfuse_prompts_get_result', {+ 0: getQuery.data.version,+ 1: getQuery.data.labels.join(', '),+ })}+ </p>+ <Textarea+ readOnly+ value={getQuery.data.prompt}+ aria-label={localize('com_ui_langfuse_prompts_get_prompt_label')}+ className="min-h-32"+ />+ </div>+ )}+ </div>+ </section>+ </OGDialogContent>+ </OGDialog>+ );+}diff --git a/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfuseConnection.spec.tsx b/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfuseConnection.spec.tsxindex 4f8ddde969..b1759c4902 100644--- a/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfuseConnection.spec.tsx+++ b/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfuseConnection.spec.tsx@@ -5,7 +5,12 @@ import LangfuseConnection from '../LangfuseConnection';const mockGet = jest.fn();const mockUpdate = jest.fn();const mockTest = jest.fn();+const mockUpdatePromptSync = jest.fn();const mockRefetch = jest.fn();+// Mutable so a test can simulate an in-flight mutation; `jest.mock` factories may only+// reference identifiers whose name starts with `mock`.+let mockUpdateIsLoading = false;+let mockPromptSyncIsLoading = false;const destinationLabels = {eu: 'eu - https://cloud.langfuse.com',us: 'us - https://us.cloud.langfuse.com',@@ -18,8 +23,15 @@ async function selectDestination(destination: keyof typeof destinationLabels) {jest.mock('~/data-provider', () => ({useGetLangfuseConnectionQuery: () => mockGet(),- useUpdateLangfuseConnectionMutation: () => ({ mutate: mockUpdate, isLoading: false }),+ useUpdateLangfuseConnectionMutation: () => ({+ mutate: mockUpdate,+ isLoading: mockUpdateIsLoading,+ }),useTestLangfuseConnectionMutation: () => ({ mutate: mockTest, isLoading: false }),+ useUpdateLangfusePromptSyncMutation: () => ({+ mutate: mockUpdatePromptSync,+ isLoading: mockPromptSyncIsLoading,+ }),}));jest.mock('~/hooks', () => ({@@ -31,6 +43,15 @@ jest.mock('@librechat/client', () => ({useToastContext: () => ({ showToast: jest.fn() }),}));+// Covered on its own in LangfusePromptsDialog.spec.tsx; stubbed here so these tests do not+// depend on the dialog's own list/get hooks. A jest.fn stub (rather than a bare `() => null`)+// lets a test tell "unmounted" from "mounted but closed" by whether React called it at all.+const mockPromptsDialog = jest.fn((_props: Record<string, unknown>) => null);+jest.mock('../LangfusePromptsDialog', () => ({+ __esModule: true,+ default: (props: Record<string, unknown>) => mockPromptsDialog(props),+}));+beforeEach(() => {global.ResizeObserver = class MockedResizeObserver {observe = jest.fn();@@ -40,7 +61,11 @@ beforeEach(() => {mockGet.mockReset();mockUpdate.mockReset();mockTest.mockReset();+ mockUpdatePromptSync.mockReset();mockRefetch.mockReset();+ mockPromptsDialog.mockClear();+ mockUpdateIsLoading = false;+ mockPromptSyncIsLoading = false;mockTest.mockImplementation((_payload, options) => {options?.onSuccess?.({ success: true });});@@ -56,6 +81,7 @@ beforeEach(() => {{ key: 'eu', baseUrl: 'https://cloud.langfuse.com' },{ key: 'us', baseUrl: 'https://us.cloud.langfuse.com' },],+ promptSync: { available: false, enabled: false },},});});@@ -612,4 +638,277 @@ describe('LangfuseConnection', () => {expect(screen.queryByText('com_ui_save')).not.toBeInTheDocument();expect(screen.getByRole('button', { name: 'com_ui_langfuse_disable' })).toBeEnabled();});++ it('hides the Langfuse prompts switch when prompt sync is not available', () => {+ render(<LangfuseConnection />);++ expect(screen.queryByRole('switch')).not.toBeInTheDocument();+ expect(+ screen.queryByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }),+ ).not.toBeInTheDocument();+ });++ it('shows the Langfuse prompts switch when prompt sync is available', () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ },+ });++ render(<LangfuseConnection />);++ expect(screen.getByRole('switch')).toBeInTheDocument();+ expect(screen.getByText('com_ui_langfuse_prompt_sync')).toBeInTheDocument();+ });++ it('shows the sync button only when prompt sync is enabled', () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ },+ });+ const { rerender } = render(<LangfuseConnection />);++ expect(+ screen.queryByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }),+ ).not.toBeInTheDocument();++ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: true },+ },+ });+ rerender(<LangfuseConnection />);++ expect(+ screen.getByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }),+ ).toBeInTheDocument();+ });++ it('calls the prompt sync mutation with the next enabled value when the switch is toggled', async () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ },+ });++ render(<LangfuseConnection />);+ await userEvent.click(screen.getByRole('switch'));++ expect(mockUpdatePromptSync).toHaveBeenCalledWith({ enabled: true }, expect.any(Object));+ });++ it('keeps an unsaved public key edit when the prompt sync switch is toggled', async () => {+ mockUpdatePromptSync.mockImplementation((_payload, options) => {+ options?.onSuccess?.({+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: true },+ });+ });+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ },+ });++ render(<LangfuseConnection />);+ fireEvent.change(screen.getByLabelText('com_ui_langfuse_public_key'), {+ target: { value: 'pk-lf-unsaved' },+ });++ await userEvent.click(screen.getByRole('switch'));++ expect(mockUpdatePromptSync).toHaveBeenCalledWith({ enabled: true }, expect.any(Object));+ expect(screen.getByLabelText('com_ui_langfuse_public_key')).toHaveValue('pk-lf-unsaved');+ });++ it('unmounts the Langfuse prompts dialog, dropping its submitted state, when prompt sync is turned off', async () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: true },+ },+ });++ render(<LangfuseConnection />);++ // Mounted, closed by default.+ expect(mockPromptsDialog).toHaveBeenCalledWith(expect.objectContaining({ open: false }));++ // The admin opens it for a dry run, the way the bug was reported.+ await userEvent.click(screen.getByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }));+ expect(mockPromptsDialog).toHaveBeenLastCalledWith(expect.objectContaining({ open: true }));++ // Turning the switch off resolves with the disabled status, the way the+ // real mutation's `onSuccess` would.+ mockUpdatePromptSync.mockImplementation((_payload, options) => {+ options?.onSuccess?.({+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ });+ });+ mockPromptsDialog.mockClear();+ await userEvent.click(screen.getByRole('switch'));++ // Unmounted entirely — not merely passed `open={false}` — so its submitted+ // list/get state (and any in-flight query) is gone rather than just hidden.+ expect(mockPromptsDialog).not.toHaveBeenCalled();+ expect(+ screen.queryByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }),+ ).not.toBeInTheDocument();++ // Re-enabling must not pop the dialog back open from leftover `open` state.+ mockUpdatePromptSync.mockImplementation((_payload, options) => {+ options?.onSuccess?.({+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: true },+ });+ });+ await userEvent.click(screen.getByRole('switch'));++ expect(mockPromptsDialog).toHaveBeenLastCalledWith(expect.objectContaining({ open: false }));+ });++ it('disables the prompt-sync switch while a connection save is in flight', () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: false,+ enabled: false,+ destinations: [],+ promptSync: { available: true, enabled: false },+ },+ });+ mockUpdateIsLoading = true;++ render(<LangfuseConnection />);++ expect(screen.getByRole('switch')).toBeDisabled();+ });++ it('disables the Save control while the prompt-sync mutation is in flight', async () => {+ const { rerender } = render(<LangfuseConnection />);+ await selectDestination('us');+ fireEvent.change(screen.getByLabelText('com_ui_langfuse_public_key'), {+ target: { value: 'pk-lf-1' },+ });+ fireEvent.change(screen.getByLabelText(/com_ui_langfuse_secret_key/), {+ target: { value: 'sk-lf-secret' },+ });+ expect(screen.getByRole('button', { name: 'com_ui_save' })).toBeEnabled();++ mockPromptSyncIsLoading = true;+ rerender(<LangfuseConnection />);++ expect(screen.getByRole('button', { name: 'com_ui_save' })).toBeDisabled();+ });++ it('disables the Enable/Disable control while the prompt-sync mutation is in flight', () => {+ mockGet.mockReturnValue({+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: mockRefetch,+ data: {+ configured: true,+ enabled: false,+ destinations: [{ key: 'eu', baseUrl: 'https://cloud.langfuse.com' }],+ destination: 'eu',+ publicKey: 'pk-lf-1',+ secretKeyPreview: 'sk-lf-...515f',+ },+ });+ mockPromptSyncIsLoading = true;++ render(<LangfuseConnection />);++ expect(screen.getByRole('button', { name: 'com_ui_langfuse_enable' })).toBeDisabled();+ });+});++describe('prompt-only Langfuse settings', () => {+ it('can toggle prompts and open the dialog without showing or testing the stored trace connection', async () => {+ const data = {+ ...mockGet().data,+ connectionAvailable: false,+ configured: true,+ enabled: true,+ destination: 'eu',+ publicKey: 'pk-stored',+ secretKeyPreview: 'sk-...stored',+ promptSync: { available: true, enabled: false },+ };+ mockGet.mockReturnValue({ ...mockGet(), data });+ mockUpdatePromptSync.mockImplementation(({ enabled }, options) => {+ options.onSuccess({ ...data, promptSync: { available: true, enabled } });+ });+ render(<LangfuseConnection />);++ expect(screen.queryByTestId('langfuse-destination')).not.toBeInTheDocument();+ expect(screen.queryByLabelText('com_ui_langfuse_public_key')).not.toBeInTheDocument();+ expect(screen.queryByLabelText('com_ui_langfuse_secret_key')).not.toBeInTheDocument();+ expect(screen.queryByTestId('langfuse-connection-status')).not.toBeInTheDocument();+ expect(screen.queryByRole('button', { name: 'com_ui_save' })).not.toBeInTheDocument();+ expect(+ screen.queryByRole('button', { name: 'com_ui_langfuse_disable' }),+ ).not.toBeInTheDocument();+ expect(mockTest).not.toHaveBeenCalled();++ await userEvent.click(screen.getByRole('switch', { name: 'com_ui_langfuse_prompt_sync' }));+ expect(mockUpdatePromptSync).toHaveBeenCalledWith({ enabled: true }, expect.any(Object));+ await userEvent.click(screen.getByRole('button', { name: 'com_ui_langfuse_prompt_sync_open' }));+ expect(mockPromptsDialog).toHaveBeenLastCalledWith(expect.objectContaining({ open: true }));+ expect(mockTest).not.toHaveBeenCalled();+ expect(mockUpdate).not.toHaveBeenCalled();+ });});diff --git a/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfusePromptsDialog.spec.tsx b/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfusePromptsDialog.spec.tsxnew file mode 100644index 0000000000..35b4dec12d--- /dev/null+++ b/client/src/components/Nav/SettingsTabs/Integrations/__tests__/LangfusePromptsDialog.spec.tsx@@ -0,0 +1,411 @@+import userEvent from '@testing-library/user-event';+import { act, render, screen, waitFor } from '@testing-library/react';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import type {+ TLangfusePromptListParams,+ TLangfusePromptListResponse,+ TLangfusePromptGetParams,+ TLangfusePromptGetResponse,+} from 'librechat-data-provider';+import type { ReactNode } from 'react';+import LangfusePromptsDialog from '../LangfusePromptsDialog';++/**+ * These tests mock `dataService` at the HTTP-service boundary (the only external call the+ * dialog makes) and drive the dialog through a real `QueryClient`, so the query key, the+ * `enabled` gating and the submitted-vs-live-input split are exercised the same way the+ * browser exercises them. Mocking `~/data-provider`'s hooks instead would hide exactly the+ * bug this file guards against: a stale query key that drops the shown result out from+ * under the user while they keep typing.+ */++const mockGetLangfusePrompts = jest.fn();+const mockGetLangfusePrompt = jest.fn();++jest.mock('librechat-data-provider', () => {+ const actual =+ jest.requireActual<typeof import('librechat-data-provider')>('librechat-data-provider');+ return {+ ...actual,+ dataService: {+ ...actual.dataService,+ getLangfusePrompts: (...args: unknown[]) => mockGetLangfusePrompts(...args),+ getLangfusePrompt: (...args: unknown[]) => mockGetLangfusePrompt(...args),+ },+ };+});++// The mock interpolates `{{0}}`-style i18next options by appending their values, so a test can+// assert that a localized line actually carried the page, status or label values it was given.+jest.mock('~/hooks', () => ({+ useLocalize: () => (key: string, options?: Record<string, unknown>) =>+ options ? `${key}:${Object.values(options).join(',')}` : key,+}));++/** An axios-style rejection the way the real backend sends one: an outer HTTP status plus a+ * `{ code, status? }` body. `getPromptErrorMessage` (the dialog's error mapper) reads+ * `error.response.data.code` for every code, and `error.response.data.status` only for the+ * `upstream` code, where it holds the Langfuse HTTP status LibreChat forwarded. */+function axiosError(outerStatus: number, data: { code: string; status?: number }) {+ return { isAxiosError: true, response: { status: outerStatus, data } };+}++function listResponse(+ overrides: Partial<TLangfusePromptListResponse> = {},+): TLangfusePromptListResponse {+ return {+ items: [+ {+ name: 'greeting',+ type: 'text',+ versions: [1, 2, 3],+ labels: ['production'],+ tags: ['demo'],+ lastUpdatedAt: '2026-01-01T00:00:00.000Z',+ },+ {+ name: 'support-chat',+ type: 'chat',+ versions: [1],+ labels: ['production'],+ tags: [],+ lastUpdatedAt: '2026-01-02T00:00:00.000Z',+ },+ ],+ meta: { page: 1, limit: 10, totalItems: 25, totalPages: 3 },+ ...overrides,+ };+}++function getResponse(+ overrides: Partial<TLangfusePromptGetResponse> = {},+): TLangfusePromptGetResponse {+ return {+ name: 'greeting',+ version: 2,+ labels: ['production'],+ prompt: 'Hello {{name}}',+ ...overrides,+ };+}++function createClient() {+ return new QueryClient({ defaultOptions: { queries: { retry: false } } });+}++function Wrapper({ children, queryClient }: { children: ReactNode; queryClient: QueryClient }) {+ return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;+}++function renderDialog(queryClient: QueryClient = createClient()) {+ render(+ <Wrapper queryClient={queryClient}>+ <LangfusePromptsDialog open onOpenChange={jest.fn()} />+ </Wrapper>,+ );+ return queryClient;+}++function lastListParams(): TLangfusePromptListParams {+ const calls = mockGetLangfusePrompts.mock.calls;+ return calls[calls.length - 1][0] as TLangfusePromptListParams;+}++function lastGetParams(): TLangfusePromptGetParams {+ const calls = mockGetLangfusePrompt.mock.calls;+ return calls[calls.length - 1][0] as TLangfusePromptGetParams;+}++const listButton = () =>+ screen.getByRole('button', { name: 'com_ui_langfuse_prompts_list_button' });+const getButton = () => screen.getByRole('button', { name: 'com_ui_langfuse_prompts_get_button' });++/** The real `Spinner` renders an `aria-hidden` SVG with no accessible role, so the only way+ * to assert its presence is the class the component itself owns. */+const hasSpinner = (button: HTMLElement) => button.querySelector('svg.spinner') != null;++beforeEach(() => {+ global.ResizeObserver = class MockedResizeObserver {+ observe = jest.fn();+ unobserve = jest.fn();+ disconnect = jest.fn();+ };+ mockGetLangfusePrompts.mockReset();+ mockGetLangfusePrompt.mockReset();+});++describe('LangfusePromptsDialog', () => {+ describe('list', () => {+ it('disables the submit button and shows a spinner while a valid request is in flight', async () => {+ let resolveRequest: ((value: TLangfusePromptListResponse) => void) | undefined;+ mockGetLangfusePrompts.mockImplementation(+ () =>+ new Promise<TLangfusePromptListResponse>((resolve) => {+ resolveRequest = resolve;+ }),+ );+ renderDialog();++ expect(listButton()).toBeEnabled();+ await userEvent.click(listButton());++ await waitFor(() => expect(listButton()).toBeDisabled());+ expect(hasSpinner(listButton())).toBe(true);++ await act(async () => {+ resolveRequest?.(listResponse());+ });++ await waitFor(() => expect(listButton()).toBeEnabled());+ });++ it('renders rows, a chat prompt type, and the page footer on success', async () => {+ mockGetLangfusePrompts.mockResolvedValue(listResponse());+ renderDialog();++ await userEvent.click(listButton());++ expect(await screen.findByText('greeting')).toBeInTheDocument();+ expect(screen.getByText('support-chat')).toBeInTheDocument();+ expect(screen.getByText('chat')).toBeInTheDocument();+ expect(screen.getByText('1, 2, 3')).toBeInTheDocument();+ expect(screen.getByText('com_ui_langfuse_prompts_list_footer:1,3,25')).toBeInTheDocument();+ });++ it('shows the empty-results state when the list comes back with no items', async () => {+ mockGetLangfusePrompts.mockResolvedValue(+ listResponse({ items: [], meta: { page: 1, limit: 10, totalItems: 0, totalPages: 0 } }),+ );+ renderDialog();++ await userEvent.click(listButton());++ expect(await screen.findByText('com_ui_langfuse_prompts_list_empty')).toBeInTheDocument();+ });++ it('maps a failure code to its localized copy and never retries', async () => {+ // No `retry: false` on this client: if the hook itself did not set `retry: false`,+ // the default client would retry a failed query several times before giving up.+ const queryClient = new QueryClient();+ mockGetLangfusePrompts.mockRejectedValue(axiosError(409, { code: 'not_configured' }));+ renderDialog(queryClient);++ await userEvent.click(listButton());++ expect(+ await screen.findByText('com_ui_langfuse_prompt_error_not_configured'),+ ).toBeInTheDocument();+ expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(1);+ });++ it('includes the HTTP status for an upstream failure', async () => {+ mockGetLangfusePrompts.mockRejectedValue(axiosError(502, { code: 'upstream', status: 400 }));+ renderDialog();++ await userEvent.click(listButton());++ expect(+ await screen.findByText('com_ui_langfuse_prompt_error_upstream_status:400'),+ ).toBeInTheDocument();+ });++ it('omits empty filter fields and keeps the page and limit defaults', async () => {+ mockGetLangfusePrompts.mockResolvedValue(listResponse());+ renderDialog();++ await userEvent.click(listButton());++ await waitFor(() => expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(1));+ expect(lastListParams()).toEqual({ page: 1, limit: 10 });+ });++ it('sends the typed filters and converts the date field to an exact ISO instant', async () => {+ mockGetLangfusePrompts.mockResolvedValue(listResponse());+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_list_name'), 'greeting');+ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_list_label'), 'prod');+ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_list_tag'), 'demo');+ await userEvent.type(+ screen.getByLabelText('com_ui_langfuse_prompts_list_from'),+ '2026-01-01T00:00',+ );+ await userEvent.click(listButton());++ await waitFor(() => expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(1));+ const params = lastListParams();+ expect(params).toMatchObject({+ name: 'greeting',+ label: 'prod',+ tag: 'demo',+ page: 1,+ limit: 10,+ });+ expect(params.fromUpdatedAt).toBe(new Date('2026-01-01T00:00').toISOString());+ expect(params).not.toHaveProperty('toUpdatedAt');+ });++ it('keeps the result visible while the user edits inputs after a submit', async () => {+ mockGetLangfusePrompts.mockResolvedValue(listResponse());+ renderDialog();++ await userEvent.click(listButton());+ expect(await screen.findByText('greeting')).toBeInTheDocument();++ await userEvent.type(+ screen.getByLabelText('com_ui_langfuse_prompts_list_name'),+ 'still-typing',+ );++ expect(screen.getByText('greeting')).toBeInTheDocument();+ expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(1);+ });++ it('issues a new request when resubmitted with identical inputs', async () => {+ mockGetLangfusePrompts.mockResolvedValue(listResponse());+ renderDialog();++ await userEvent.click(listButton());+ await waitFor(() => expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(1));++ await userEvent.click(listButton());+ await waitFor(() => expect(mockGetLangfusePrompts).toHaveBeenCalledTimes(2));+ });++ it('cannot be submitted with a limit over 100', async () => {+ renderDialog();++ const limitInput = screen.getByLabelText('com_ui_langfuse_prompts_list_limit');+ await userEvent.clear(limitInput);+ await userEvent.type(limitInput, '101');++ expect(listButton()).toBeDisabled();+ await userEvent.click(listButton());+ expect(mockGetLangfusePrompts).not.toHaveBeenCalled();+ });+ });++ describe('get', () => {+ it('disables the submit button until a name is entered', async () => {+ renderDialog();++ expect(getButton()).toBeDisabled();+ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ expect(getButton()).toBeEnabled();+ });++ it('disables the submit button and shows a spinner while a request is in flight', async () => {+ let resolveRequest: ((value: TLangfusePromptGetResponse) => void) | undefined;+ mockGetLangfusePrompt.mockImplementation(+ () =>+ new Promise<TLangfusePromptGetResponse>((resolve) => {+ resolveRequest = resolve;+ }),+ );+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ await waitFor(() => expect(getButton()).toBeDisabled());+ expect(hasSpinner(getButton())).toBe(true);++ await act(async () => {+ resolveRequest?.(getResponse());+ });++ await waitFor(() => expect(getButton()).toBeEnabled());+ });++ it('renders the version and prompt text on success', async () => {+ mockGetLangfusePrompt.mockResolvedValue(getResponse());+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ expect(+ await screen.findByText('com_ui_langfuse_prompts_get_result:2,production'),+ ).toBeInTheDocument();+ expect(screen.getByDisplayValue('Hello {{name}}')).toBeInTheDocument();+ });++ it('resolves Production without a version in the request', async () => {+ mockGetLangfusePrompt.mockResolvedValue(getResponse());+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ await waitFor(() => expect(mockGetLangfusePrompt).toHaveBeenCalledTimes(1));+ expect(lastGetParams()).toEqual({ name: 'greeting' });+ });++ it('resolves an exact version when Version is selected', async () => {+ mockGetLangfusePrompt.mockResolvedValue(getResponse());+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(+ screen.getByRole('radio', { name: 'com_ui_langfuse_prompts_get_version' }),+ );+ await userEvent.type(+ screen.getByRole('spinbutton', { name: 'com_ui_langfuse_prompts_get_version' }),+ '2',+ );+ await userEvent.click(getButton());++ await waitFor(() => expect(mockGetLangfusePrompt).toHaveBeenCalledTimes(1));+ expect(lastGetParams()).toEqual({ name: 'greeting', version: 2 });+ });++ it('cannot be submitted with a fractional version', async () => {+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(+ screen.getByRole('radio', { name: 'com_ui_langfuse_prompts_get_version' }),+ );+ await userEvent.type(+ screen.getByRole('spinbutton', { name: 'com_ui_langfuse_prompts_get_version' }),+ '1.5',+ );++ expect(getButton()).toBeDisabled();+ await userEvent.click(getButton());+ expect(mockGetLangfusePrompt).not.toHaveBeenCalled();+ });++ it('maps unsupported_type to its localized copy', async () => {+ mockGetLangfusePrompt.mockRejectedValue(axiosError(422, { code: 'unsupported_type' }));+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ expect(+ await screen.findByText('com_ui_langfuse_prompt_error_unsupported_type'),+ ).toBeInTheDocument();+ });++ it('maps not_found to its localized copy', async () => {+ mockGetLangfusePrompt.mockRejectedValue(axiosError(404, { code: 'not_found' }));+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ expect(await screen.findByText('com_ui_langfuse_prompt_error_not_found')).toBeInTheDocument();+ });++ it('falls back to the generic copy for an unmapped code', async () => {+ mockGetLangfusePrompt.mockRejectedValue(axiosError(500, { code: 'something_unexpected' }));+ renderDialog();++ await userEvent.type(screen.getByLabelText('com_ui_langfuse_prompts_get_name'), 'greeting');+ await userEvent.click(getButton());++ expect(await screen.findByText('com_ui_langfuse_prompt_error_fallback')).toBeInTheDocument();+ });+ });+});diff --git a/client/src/data-provider/Langfuse/__tests__/mutations.spec.tsx b/client/src/data-provider/Langfuse/__tests__/mutations.spec.tsxnew file mode 100644index 0000000000..ccd9484be7--- /dev/null+++ b/client/src/data-provider/Langfuse/__tests__/mutations.spec.tsx@@ -0,0 +1,128 @@+import { QueryKeys } from 'librechat-data-provider';+import { act, renderHook } from '@testing-library/react';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import type { TLangfuseConnectionStatus } from 'librechat-data-provider';+import type { ReactNode } from 'react';+import { useUpdateLangfuseConnectionMutation, useUpdateLangfusePromptSyncMutation } from '../index';++const mockUpdateLangfuseConnection = jest.fn();+const mockUpdateLangfusePromptSync = jest.fn();++jest.mock('librechat-data-provider', () => {+ const actual = jest.requireActual('librechat-data-provider');+ return {+ ...actual,+ dataService: {+ ...actual.dataService,+ updateLangfuseConnection: (...args: unknown[]) => mockUpdateLangfuseConnection(...args),+ updateLangfusePromptSync: (...args: unknown[]) => mockUpdateLangfusePromptSync(...args),+ },+ };+});++function connectionStatus(overrides: Partial<TLangfuseConnectionStatus> = {}) {+ return {+ configured: true,+ enabled: true,+ destinations: [{ key: 'us', baseUrl: 'https://us.cloud.langfuse.com' }],+ destination: 'us',+ publicKey: 'pk-lf-new',+ promptSync: { available: true, enabled: true },+ ...overrides,+ } as TLangfuseConnectionStatus;+}++/** Seeds the always-mounted prompts dialog's cached list/get results, the way a real+ * submit of its list/get forms would, so a test can assert they are gone afterward. */+function seedCachedPrompts(queryClient: QueryClient) {+ queryClient.setQueryData([QueryKeys.langfusePrompts, { page: 1 }, 1], {+ items: [{ name: 'greeting' }],+ meta: { page: 1, limit: 10, totalItems: 1, totalPages: 1 },+ });+ queryClient.setQueryData([QueryKeys.langfusePrompt, { name: 'greeting' }, 1], {+ name: 'greeting',+ version: 1,+ labels: [],+ prompt: 'Hello',+ });+}++function cachedPromptKeys(queryClient: QueryClient) {+ return queryClient+ .getQueryCache()+ .findAll()+ .map((query) => query.queryKey[0]);+}++describe('Langfuse prompt cache invalidation', () => {+ let queryClient: QueryClient;+ let wrapper: ({ children }: { children: ReactNode }) => JSX.Element;++ beforeEach(() => {+ jest.resetAllMocks();+ queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false }, mutations: { retry: false } },+ });+ wrapper = ({ children }: { children: ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+ });++ it('removes cached prompt list/get results once the prompt-sync mutation succeeds', async () => {+ seedCachedPrompts(queryClient);+ expect(cachedPromptKeys(queryClient)).toEqual(+ expect.arrayContaining([QueryKeys.langfusePrompts, QueryKeys.langfusePrompt]),+ );++ mockUpdateLangfusePromptSync.mockResolvedValueOnce(connectionStatus());+ const { result } = renderHook(() => useUpdateLangfusePromptSyncMutation(), { wrapper });++ await act(async () => {+ await result.current.mutateAsync({ enabled: false });+ });++ expect(cachedPromptKeys(queryClient)).not.toEqual(+ expect.arrayContaining([QueryKeys.langfusePrompts]),+ );+ expect(cachedPromptKeys(queryClient)).not.toEqual(+ expect.arrayContaining([QueryKeys.langfusePrompt]),+ );+ });++ it('removes cached prompt list/get results once the connection update mutation succeeds', async () => {+ seedCachedPrompts(queryClient);++ mockUpdateLangfuseConnection.mockResolvedValueOnce(connectionStatus());+ const { result } = renderHook(() => useUpdateLangfuseConnectionMutation(), { wrapper });++ await act(async () => {+ await result.current.mutateAsync({+ enabled: true,+ destination: 'us',+ publicKey: 'pk-lf-new',+ secretKey: 'sk-lf-new',+ });+ });++ expect(cachedPromptKeys(queryClient)).not.toEqual(+ expect.arrayContaining([QueryKeys.langfusePrompts]),+ );+ expect(cachedPromptKeys(queryClient)).not.toEqual(+ expect.arrayContaining([QueryKeys.langfusePrompt]),+ );+ });++ it('leaves other cached queries untouched', async () => {+ seedCachedPrompts(queryClient);+ queryClient.setQueryData([QueryKeys.langfuseConnection], connectionStatus());++ mockUpdateLangfusePromptSync.mockResolvedValueOnce(connectionStatus());+ const { result } = renderHook(() => useUpdateLangfusePromptSyncMutation(), { wrapper });++ await act(async () => {+ await result.current.mutateAsync({ enabled: true });+ });++ expect(queryClient.getQueryData([QueryKeys.langfuseConnection])).toEqual(connectionStatus());+ });+});diff --git a/client/src/data-provider/Langfuse/index.ts b/client/src/data-provider/Langfuse/index.tsindex 7c267134ed..47dc3f3822 100644--- a/client/src/data-provider/Langfuse/index.ts+++ b/client/src/data-provider/Langfuse/index.ts@@ -6,6 +6,11 @@ import type {TLangfuseConnectionTestRequest,TLangfuseConnectionTestResponse,TLangfuseSessionLinkResponse,+ TUpdateLangfusePromptSyncRequest,+ TLangfusePromptListParams,+ TLangfusePromptListResponse,+ TLangfusePromptGetParams,+ TLangfusePromptGetResponse,} from 'librechat-data-provider';import type { UseQueryResult, UseMutationResult } from '@tanstack/react-query';@@ -41,6 +46,11 @@ export const useUpdateLangfuseConnectionMutation = (): UseMutationResult<onSuccess: (data) => {queryClient.setQueryData([QueryKeys.langfuseConnection], data);queryClient.removeQueries([QueryKeys.langfuseSessionLink]);+ // A rotated destination or credential invalidates any prompt list/get+ // results already cached under the old connection — the always-mounted+ // prompts dialog must never show a previous connection's prompts.+ queryClient.removeQueries({ queryKey: [QueryKeys.langfusePrompts] });+ queryClient.removeQueries({ queryKey: [QueryKeys.langfusePrompt] });},},);@@ -55,3 +65,61 @@ export const useTestLangfuseConnectionMutation = (): UseMutationResult<(payload: TLangfuseConnectionTestRequest) => dataService.testLangfuseConnection(payload),{ mutationKey: [MutationKeys.testLangfuseConnection] },);++export const useUpdateLangfusePromptSyncMutation = (): UseMutationResult<+ TLangfuseConnectionStatus,+ unknown,+ TUpdateLangfusePromptSyncRequest+> => {+ const queryClient = useQueryClient();+ return useMutation(+ (payload: TUpdateLangfusePromptSyncRequest) => dataService.updateLangfusePromptSync(payload),+ {+ mutationKey: [MutationKeys.updateLangfusePromptSync],+ onSuccess: (data) => {+ queryClient.setQueryData([QueryKeys.langfuseConnection], data);+ // Toggling the switch off and back on must never resurface a previous+ // project's cached prompt list/get results in the always-mounted dialog.+ queryClient.removeQueries({ queryKey: [QueryKeys.langfusePrompts] });+ queryClient.removeQueries({ queryKey: [QueryKeys.langfusePrompt] });+ },+ },+ );+};++/**+ * Lists Langfuse prompts for the test dialog, keyed on the last SUBMITTED filters rather+ * than the live form state, so editing the inputs after a submit never changes the key out+ * from under the shown result. Idle until the dialog submits once; `submitCount` forces a+ * new request on an identical resubmit, since the params alone would hash to the same key.+ * Never retried: each call reaches the tenant's Langfuse project and a failure there is+ * deterministic.+ */+export const useLangfusePromptsQuery = (+ params: TLangfusePromptListParams | undefined,+ submitCount: number,+): UseQueryResult<TLangfusePromptListResponse> =>+ useQuery<TLangfusePromptListResponse>(+ [QueryKeys.langfusePrompts, params, submitCount],+ () => dataService.getLangfusePrompts(params),+ { enabled: params != null, retry: false, refetchOnWindowFocus: false },+ );++/**+ * Resolves one Langfuse prompt (Production or an exact version) for the test dialog, keyed+ * the same way as {@link useLangfusePromptsQuery} and for the same reason. Never retried.+ */+export const useLangfusePromptQuery = (+ params: TLangfusePromptGetParams | undefined,+ submitCount: number,+): UseQueryResult<TLangfusePromptGetResponse> =>+ useQuery<TLangfusePromptGetResponse>(+ [QueryKeys.langfusePrompt, params, submitCount],+ () => {+ if (params == null) {+ throw new Error('Langfuse prompt query is disabled');+ }+ return dataService.getLangfusePrompt(params);+ },+ { enabled: params != null, retry: false, refetchOnWindowFocus: false },+ );diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.jsonindex a4b44ef8ab..e6443965bb 100644--- a/client/src/locales/en/translation.json+++ b/client/src/locales/en/translation.json@@ -1940,6 +1940,46 @@"com_ui_langfuse_disable": "Disable","com_ui_langfuse_enable": "Enable","com_ui_langfuse_load_error": "Failed to load the Langfuse connection",+ "com_ui_langfuse_prompt_error_fallback": "Something went wrong. Try again.",+ "com_ui_langfuse_prompt_error_invalid_request": "Check the values and try again.",+ "com_ui_langfuse_prompt_error_invalid_response": "Langfuse returned an unexpected response.",+ "com_ui_langfuse_prompt_error_not_configured": "Connect a Langfuse project first.",+ "com_ui_langfuse_prompt_error_not_found": "No prompt with this name and version.",+ "com_ui_langfuse_prompt_error_timeout": "Langfuse did not respond in time.",+ "com_ui_langfuse_prompt_error_unauthorized": "Langfuse rejected the saved keys.",+ "com_ui_langfuse_prompt_error_unsupported_type": "This is a chat prompt. Only text prompts can be used.",+ "com_ui_langfuse_prompt_error_upstream": "Could not reach Langfuse.",+ "com_ui_langfuse_prompt_error_upstream_status": "Langfuse returned HTTP {{0}}.",+ "com_ui_langfuse_prompt_sync": "Langfuse prompts",+ "com_ui_langfuse_prompt_sync_description": "Try listing and resolving prompts from the connected Langfuse project. Nothing is written.",+ "com_ui_langfuse_prompt_sync_error": "Failed to update the Langfuse prompts setting",+ "com_ui_langfuse_prompt_sync_open": "Sync prompts (test)",+ "com_ui_langfuse_prompts_column_labels": "Labels",+ "com_ui_langfuse_prompts_column_name": "Name",+ "com_ui_langfuse_prompts_column_tags": "Tags",+ "com_ui_langfuse_prompts_column_type": "Type",+ "com_ui_langfuse_prompts_column_versions": "Versions",+ "com_ui_langfuse_prompts_dialog_title": "Sync prompts (test)",+ "com_ui_langfuse_prompts_dry_run_notice": "This is a dry run. Nothing is written.",+ "com_ui_langfuse_prompts_get_button": "Get",+ "com_ui_langfuse_prompts_get_name": "Name",+ "com_ui_langfuse_prompts_get_production": "Production",+ "com_ui_langfuse_prompts_get_prompt_label": "Prompt text",+ "com_ui_langfuse_prompts_get_resolve": "Resolve",+ "com_ui_langfuse_prompts_get_result": "Version {{0}} · labels: {{1}}",+ "com_ui_langfuse_prompts_get_title": "Get prompt",+ "com_ui_langfuse_prompts_get_version": "Version",+ "com_ui_langfuse_prompts_list_button": "List",+ "com_ui_langfuse_prompts_list_empty": "No prompts match these filters.",+ "com_ui_langfuse_prompts_list_footer": "Page {{0}} of {{1}} · {{2}} prompts",+ "com_ui_langfuse_prompts_list_from": "Updated from",+ "com_ui_langfuse_prompts_list_label": "Label",+ "com_ui_langfuse_prompts_list_limit": "Limit",+ "com_ui_langfuse_prompts_list_name": "Name",+ "com_ui_langfuse_prompts_list_page": "Page",+ "com_ui_langfuse_prompts_list_tag": "Tag",+ "com_ui_langfuse_prompts_list_title": "List prompts",+ "com_ui_langfuse_prompts_list_to": "Updated to","com_ui_langfuse_public_key": "Public key","com_ui_langfuse_save_error": "Failed to save the Langfuse connection","com_ui_langfuse_saved": "Langfuse connection saved",diff --git a/packages/api/src/admin/langfuse.handler.spec.ts b/packages/api/src/admin/langfuse.handler.spec.tsindex b99f0608b6..69265096f8 100644--- a/packages/api/src/admin/langfuse.handler.spec.ts+++ b/packages/api/src/admin/langfuse.handler.spec.ts@@ -14,6 +14,11 @@ let encryptV3: typeof import('@librechat/data-schemas').encryptV3;let ConfigRevisionConflictError: typeof import('@librechat/data-schemas').ConfigRevisionConflictError;let createAdminLangfuseHandlers: typeof import('./langfuse').createAdminLangfuseHandlers;let getLangfuseDestinationId: typeof import('../langfuse/destinations').getLangfuseDestinationId;+// Imported as a namespace, rather than destructured, so a test can `jest.spyOn`+// the live binding `./langfuse` calls through — the only way to exercise its+// defensive catch-all, since every real fetch failure already arrives wrapped+// as a `LangfusePromptRequestError`.+let promptsModule: typeof import('../langfuse/prompts');const realFetch = global.fetch;function projectResponse(projectId = 'project-1') {@@ -28,6 +33,7 @@ beforeAll(async () => {({ encryptV3, ConfigRevisionConflictError } = await import('@librechat/data-schemas'));({ createAdminLangfuseHandlers } = await import('./langfuse'));({ getLangfuseDestinationId } = await import('../langfuse/destinations'));+ promptsModule = await import('../langfuse/prompts');});beforeEach(() => {@@ -47,9 +53,67 @@ afterEach(() => {delete process.env.LANGFUSE_TRACING_ENABLED;delete process.env.LANGFUSE_SAMPLE_RATE;delete process.env.TENANT_ISOLATION_STRICT;+ delete process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE;global.fetch = realFetch;});+/** A stored tenant connection valid enough for `resolveLangfusePromptConnection`+ * to resolve (the suite's `beforeEach` keeps multi-tenant routing on, so this+ * always resolves through the tenant's own destination, never the central env+ * project). */+function tenantLangfuseConnection(overrides: Record<string, unknown> = {}) {+ return {+ destination: 'eu',+ publicKey: 'pk-lf-1',+ secretKey: encryptV3('sk-lf-secret'),+ ...overrides,+ };+}++function withPromptSync(enabled: boolean, connection: Record<string, unknown> = {}) {+ return { ...tenantLangfuseConnection(connection), promptSync: { enabled } };+}++/** The stored base config the list/get prompt routes' tenant-switch gate+ * reads, independent of whatever `req.config` a test also sets up for+ * `resolveLangfusePromptConnection`. Most tests want both in agreement. */+function createPromptSyncHandlers(storedEnabled: boolean, overrides: Record<string, unknown> = {}) {+ return createHandlers({+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc(withPromptSync(storedEnabled))),+ ...overrides,+ });+}++function fetchJsonResponse(body: unknown, status = 200) {+ return { ok: status >= 200 && status < 300, status, json: jest.fn().mockResolvedValue(body) };+}++function promptListBody() {+ return {+ data: [+ {+ name: 'greeting',+ type: 'text',+ versions: [1, 2],+ labels: ['production'],+ tags: ['demo'],+ lastUpdatedAt: '2026-01-01T00:00:00.000Z',+ },+ {+ name: 'support-chat',+ type: 'chat',+ versions: [1],+ labels: [],+ tags: [],+ lastUpdatedAt: '2026-01-02T00:00:00.000Z',+ },+ ],+ meta: { page: 1, limit: 10, totalItems: 2, totalPages: 1 },+ };+}+function mockReq(overrides = {}) {return {user: { id: 'u1', role: 'ADMIN', tenantId: 't1' },@@ -138,6 +202,32 @@ function unavailable(reason: TLangfuseUnavailableReason): TLangfuseUnavailableRedescribe('createAdminLangfuseHandlers', () => {describe('connection availability gate', () => {+ it('allows prompt status and updates while keeping trace connection operations unavailable', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ delete process.env.LANGFUSE_FANOUT_ENABLED;+ const { handlers, deps } = createHandlers();+ const res = mockRes();+ await handlers.getConnection(mockReq(), res);+ expect(res.statusCode).toBe(200);+ expect(res.body).toMatchObject({+ connectionAvailable: false,+ promptSync: { available: true, enabled: false },+ });+ expect(deps.findConfigByPrincipal).toHaveBeenCalledTimes(1);++ const updated = mockRes();+ await handlers.updatePromptSync(mockReq({ body: { enabled: true } }), updated);+ expect(updated.statusCode).toBe(200);+ deps.patchConfigFields.mockClear();+ for (const handler of ['updateConnection', 'testConnection', 'getSessionLink'] as const) {+ const denied = mockRes();+ await handlers[handler](mockReq(), denied);+ expect(denied.statusCode).toBe(404);+ }+ expect(deps.patchConfigFields).not.toHaveBeenCalled();+ expect(global.fetch).not.toHaveBeenCalled();+ });+it('rejects connection reads when deployment fanout is disabled', async () => {delete process.env.LANGFUSE_FANOUT_ENABLED;const { handlers, deps } = createHandlers();@@ -1298,4 +1388,968 @@ describe('createAdminLangfuseHandlers', () => {},);});++ describe('buildStatus promptSync', () => {+ it.each([+ [false, undefined, { available: false, enabled: false }],+ [false, true, { available: false, enabled: false }],+ [true, undefined, { available: true, enabled: false }],+ [true, false, { available: true, enabled: false }],+ [true, true, { available: true, enabled: true }],+ ])('available=%s stored enabled=%s -> %j', async (available, storedEnabled, expected) => {+ if (available) {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ }+ const { handlers } = createHandlers({+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(+ baseConfigDoc(+ tenantLangfuseConnection(+ storedEnabled === undefined ? {} : { promptSync: { enabled: storedEnabled } },+ ),+ ),+ ),+ });+ const res = mockRes();++ await handlers.getConnection(mockReq(), res);++ expect(res.body?.promptSync).toEqual(expected);+ });++ it('reports unavailable promptSync when no base config exists', async () => {+ const { handlers } = createHandlers();+ const res = mockRes();++ await handlers.getConnection(mockReq(), res);++ expect(res.body?.promptSync).toEqual({ available: false, enabled: false });+ });+ });++ describe('updatePromptSync', () => {+ it.each([true, false])(+ 'audits the saved prompt switch value %s after cache invalidation',+ async (enabled) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const order: string[] = [];+ const recordAuditEntry = jest.fn(async () => {+ order.push('audit');+ });+ const invalidateConfigCaches = jest.fn(async () => {+ order.push('invalidate');+ });+ const { handlers } = createHandlers({ recordAuditEntry, invalidateConfigCaches });+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body: { enabled } }), res);++ expect(res.statusCode).toBe(200);+ expect(order).toEqual(['invalidate', 'audit']);+ expect(recordAuditEntry).toHaveBeenCalledWith(+ expect.objectContaining({+ action: 'langfuse.updated',+ severity: 'warning',+ target: { type: 'langfuse', id: 'prompt-sync' },+ metadata: { enabled },+ tenantId: 't1',+ actor: expect.objectContaining({ id: 'u1' }),+ }),+ );+ },+ );++ it('does not audit a rejected update or a failed save', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const recordAuditEntry = jest.fn();+ const { handlers, deps } = createHandlers({+ recordAuditEntry,+ patchConfigFields: jest.fn().mockRejectedValue(new Error('save failed')),+ });+ const invalid = mockRes();+ await handlers.updatePromptSync(mockReq({ body: {} }), invalid);+ expect(invalid.statusCode).toBe(400);+ expect(deps.patchConfigFields).not.toHaveBeenCalled();+ const failed = mockRes();+ await handlers.updatePromptSync(mockReq({ body: { enabled: true } }), failed);+ expect(failed.statusCode).toBe(500);+ expect(recordAuditEntry).not.toHaveBeenCalled();+ });++ it('returns 404 when prompt sync is not available', async () => {+ const { handlers, deps } = createHandlers();+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body: { enabled: true } }), res);++ expect(res.statusCode).toBe(404);+ expect(deps.patchConfigFields).not.toHaveBeenCalled();+ });++ it.each([[{}], [{ enabled: 'true' }], [{ enabled: true, extra: 'nope' }], [{ enabled: null }]])(+ 'rejects an invalid body %j with 400 invalid_request',+ async (body) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers, deps } = createHandlers();+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body }), res);++ expect(res.statusCode).toBe(400);+ expect(res.body).toEqual({ code: 'invalid_request' });+ expect(deps.patchConfigFields).not.toHaveBeenCalled();+ },+ );++ it('saves only langfuse.promptSync.enabled and clears the config caches', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const stored = tenantLangfuseConnection();+ const { handlers, deps } = createHandlers({+ findConfigByPrincipal: jest.fn().mockResolvedValue(baseConfigDoc(stored)),+ // The shared `createHandlers` default replaces the whole document from+ // the patched fields alone, which is accurate only when a caller patches+ // every connection field at once (as `updateConnection` always does).+ // This handler patches a single nested field, so the double here merges+ // onto the existing document the way the real patch does.+ patchConfigFields: jest+ .fn()+ .mockImplementation((_pt, _pid, _pm, fields: Record<string, unknown>) =>+ Promise.resolve(+ baseConfigDoc({+ ...stored,+ promptSync: { enabled: fields['langfuse.promptSync.enabled'] },+ }),+ ),+ ),+ });+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body: { enabled: true } }), res);++ expect(res.statusCode).toBe(200);+ expect(deps.patchConfigFields).toHaveBeenCalledTimes(1);+ expect(deps.patchConfigFields.mock.calls[0].slice(0, 3)).toEqual([+ 'role',+ '__base__',+ 'Role',+ ]);+ expect(deps.patchConfigFields.mock.calls[0][3]).toEqual({+ 'langfuse.promptSync.enabled': true,+ });+ expect(deps.invalidateConfigCaches).toHaveBeenCalledWith('t1');+ expect(res.body).toMatchObject({ promptSync: { available: true, enabled: true } });+ });++ it('keeps an inactive base config inactive, preserves its priority, and reports what GET /connection would report', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const stored = tenantLangfuseConnection({ promptSync: { enabled: false } });+ const inactiveExisting = { ...baseConfigDoc(stored), isActive: false, priority: 50 };+ const inactiveUpdated = {+ ...baseConfigDoc({ ...stored, promptSync: { enabled: true } }),+ isActive: false,+ priority: 50,+ };+ const { handlers, deps } = createHandlers({+ findConfigByPrincipal: jest.fn().mockResolvedValue(inactiveExisting),+ patchConfigFields: jest.fn().mockResolvedValue(inactiveUpdated),+ });+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body: { enabled: true } }), res);++ expect(deps.findConfigByPrincipal).not.toHaveBeenCalled();+ expect(deps.patchConfigFields.mock.calls[0][4]).toBeUndefined();+ expect(deps.toggleConfigActive).not.toHaveBeenCalled();+ expect(res.statusCode).toBe(200);++ const { handlers: getConnectionHandlers } = createHandlers({+ findConfigByPrincipal: jest.fn().mockResolvedValue(null),+ });+ const getConnectionRes = mockRes();+ await getConnectionHandlers.getConnection(mockReq(), getConnectionRes);++ expect(res.body).toEqual(getConnectionRes.body);+ });++ it('returns the fresh connection status shape, same as GET /connection', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const stored = tenantLangfuseConnection();+ const { handlers } = createHandlers({+ findConfigByPrincipal: jest.fn().mockResolvedValue(baseConfigDoc(stored)),+ patchConfigFields: jest+ .fn()+ .mockImplementation((_pt, _pid, _pm, fields: Record<string, unknown>) =>+ Promise.resolve(+ baseConfigDoc({+ ...stored,+ promptSync: { enabled: fields['langfuse.promptSync.enabled'] },+ }),+ ),+ ),+ });+ const res = mockRes();++ await handlers.updatePromptSync(mockReq({ body: { enabled: false } }), res);++ expect(res.body).toMatchObject({+ configured: true,+ destination: 'eu',+ publicKey: 'pk-lf-1',+ promptSync: { available: true, enabled: false },+ });+ expect(res.body?.secretKey).toBeUndefined();+ });+ });++ describe('stored prompt credentials', () => {+ describe.each(['listPrompts', 'getPrompt'] as const)('%s', (handler) => {+ it.each(['all', 'destination', 'publicKey', 'secretKey'])(+ 'rejects deployment credentials when stored %s is missing',+ async (missing) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const stored: Record<string, unknown> =+ missing === 'all' ? { promptSync: { enabled: true } } : withPromptSync(true);+ delete stored[missing];+ const { handlers, deps } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest.fn().mockResolvedValue(baseConfigDoc(stored)),+ });+ const res = mockRes();++ await handlers[handler](+ mockReq({+ params: { name: 'greeting' },+ config: { langfuse: withPromptSync(true) },+ }),+ res,+ );++ expect(res.statusCode).toBe(409);+ expect(res.body).toEqual({ code: 'not_configured' });+ expect(global.fetch).not.toHaveBeenCalled();+ expect(deps.findConfigByPrincipal).toHaveBeenCalledTimes(1);+ },+ );+ });++ it('keeps deployment proxy headers with the stored connection', async () => {+ delete process.env.TENANT_ISOLATION_STRICT;+ delete process.env.LANGFUSE_FANOUT_ENABLED;+ delete process.env.LANGFUSE_FANOUT_COLLECTOR_URL;+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ process.env.LANGFUSE_FANOUT_TENANT_EU_BASE_URL = 'https://langfuse.example.com';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();+ await handlers.listPrompts(+ mockReq({+ config: {+ langfuse: {+ headers: { 'X-Proxy-Key': 'proxy-key' },+ },+ },+ }),+ res,+ );+ expect(res.statusCode).toBe(200);+ expect(global.fetch).toHaveBeenCalledWith(+ expect.stringContaining('https://langfuse.example.com/'),+ expect.objectContaining({+ headers: expect.objectContaining({ 'X-Proxy-Key': 'proxy-key' }),+ }),+ );+ });+ });++ describe('listPrompts', () => {+ it('returns 404 when prompt sync is not available at all', async () => {+ const { handlers } = createHandlers();+ const res = mockRes();++ await handlers.listPrompts(mockReq(), res);++ expect(res.statusCode).toBe(404);+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('returns 404 when available but the tenant switch is off', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(false);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(false) } }), res);++ expect(res.statusCode).toBe(404);+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('returns 409 not_configured when both switches are on but no connection resolves', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc({ promptSync: { enabled: true } })),+ });+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({ config: { langfuse: { promptSync: { enabled: true } } } }),+ res,+ );++ expect(res.statusCode).toBe(409);+ expect(res.body).toEqual({ code: 'not_configured' });+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('returns 200 with the list when both switches are on and the connection is valid', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(200);+ const items = res.body?.items as Array<Record<string, unknown>>;+ expect(items).toHaveLength(2);+ expect(items[1]).toMatchObject({ name: 'support-chat', type: 'chat' });+ expect(res.body?.meta).toEqual({ page: 1, limit: 10, totalItems: 2, totalPages: 1 });+ });++ it('passes the query params through to Langfuse and never sends filter', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({+ config: { langfuse: withPromptSync(true) },+ query: { name: 'greeting', label: 'production', tag: 'demo', page: '2', limit: '5' },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('name=greeting');+ expect(url).toContain('label=production');+ expect(url).toContain('tag=demo');+ expect(url).toContain('page=2');+ expect(url).toContain('limit=5');+ expect(url).not.toContain('filter');+ });++ it.each([+ [{ page: '0' }],+ [{ page: 'abc' }],+ [{ limit: '0' }],+ [{ limit: '101' }],+ [{ name: '' }],+ [{ name: 'x'.repeat(257) }],+ [{ fromUpdatedAt: 'not-a-date' }],+ ])('rejects an invalid query %j with 400 invalid_request', async (query) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({ config: { langfuse: withPromptSync(true) }, query }),+ res,+ );++ expect(res.statusCode).toBe(400);+ expect(res.body).toEqual({ code: 'invalid_request' });+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it.each([+ [401, 'unauthorized', 502],+ [403, 'unauthorized', 502],+ [500, 'upstream', 502],+ [503, 'upstream', 502],+ ])(+ 'maps a Langfuse %i response to %s with status %i',+ async (upstreamStatus, code, httpStatus) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(+ fetchJsonResponse({ message: 'MARKER_SECRET_LEAK_TOKEN' }, upstreamStatus),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(httpStatus);+ expect(res.body?.code).toBe(code);+ if (code === 'upstream') {+ expect(res.body?.status).toBe(upstreamStatus);+ } else {+ expect(res.body?.status).toBeUndefined();+ }+ expect(JSON.stringify(res.body)).not.toContain('MARKER_SECRET_LEAK_TOKEN');+ expect(JSON.stringify(res.body)).not.toContain('pk-lf-1');+ },+ );++ it('maps a Langfuse timeout to 504 timeout', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const timeoutError = new Error('The operation was aborted due to timeout');+ timeoutError.name = 'TimeoutError';+ global.fetch = jest.fn().mockRejectedValue(timeoutError) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(504);+ expect(res.body).toEqual({ code: 'timeout' });+ });++ it('maps an invalid Langfuse response body to 502 invalid_response', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse({ data: 'not-an-array' })) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(502);+ expect(res.body).toEqual({ code: 'invalid_response' });+ });++ /**+ * Every real fetch failure is already wrapped as a `LangfusePromptRequestError`+ * by `prompts.ts`, so a plain `Error` can only reach the handler's catch-all+ * through a defensive, not-normally-reachable path. Spying on the live+ * binding is the only way to exercise that branch.+ */+ it('maps an unexpected thrown error to a generic 500 without leaking its message', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ jest+ .spyOn(promptsModule, 'listLangfusePrompts')+ .mockRejectedValueOnce(new Error('MARKER_SECRET_LEAK_TOKEN'));+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(500);+ expect(res.body).toEqual({ code: 'upstream' });+ expect(JSON.stringify(res.body)).not.toContain('MARKER_SECRET_LEAK_TOKEN');+ });++ describe('tenant-switch gate reads the stored base config, not req.config', () => {+ it('returns 404 when req.config says enabled but the stored base config says disabled', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(false);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(true) } }), res);++ expect(res.statusCode).toBe(404);+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('is allowed when req.config says disabled but the stored base config says enabled', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(mockReq({ config: { langfuse: withPromptSync(false) } }), res);++ expect(res.statusCode).toBe(200);+ });++ it('fetches with the stored destination and credentials, never a stale req.config connection', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const findConfigByPrincipal = jest.fn().mockResolvedValue(+ baseConfigDoc(+ withPromptSync(true, {+ destination: 'us',+ publicKey: 'pk-lf-new',+ secretKey: encryptV3('sk-lf-new'),+ }),+ ),+ );+ const { handlers, deps } = createHandlers({ findConfigByPrincipal });+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({+ config: {+ langfuse: withPromptSync(true, {+ destination: 'eu',+ publicKey: 'pk-lf-old',+ secretKey: encryptV3('sk-lf-old'),+ }),+ },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url, init] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('https://us.cloud.langfuse.com');+ expect(+ Buffer.from(init.headers.Authorization.replace('Basic ', ''), 'base64').toString(),+ ).toBe('pk-lf-new:sk-lf-new');+ expect(deps.findConfigByPrincipal).toHaveBeenCalledTimes(1);+ });+ });+ });++ describe('getPrompt', () => {+ it('returns 404 when prompt sync is not available at all', async () => {+ const { handlers } = createHandlers();+ const res = mockRes();++ await handlers.getPrompt(mockReq({ params: { name: 'greeting' } }), res);++ expect(res.statusCode).toBe(404);+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('returns 404 when available but the tenant switch is off', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(false);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(false) }, params: { name: 'greeting' } }),+ res,+ );++ expect(res.statusCode).toBe(404);+ });++ it('returns 409 not_configured when both switches are on but no connection resolves', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc({ promptSync: { enabled: true } })),+ });+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({+ config: { langfuse: { promptSync: { enabled: true } } },+ params: { name: 'greeting' },+ }),+ res,+ );++ expect(res.statusCode).toBe(409);+ expect(res.body).toEqual({ code: 'not_configured' });+ });++ it.each([[{ name: '' }], [{ name: 'x'.repeat(257) }]])(+ 'rejects an invalid name %j with 400 invalid_request',+ async (params) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params }),+ res,+ );++ expect(res.statusCode).toBe(400);+ expect(res.body).toEqual({ code: 'invalid_request' });+ expect(global.fetch).not.toHaveBeenCalled();+ },+ );++ it.each([[{ version: '0' }], [{ version: '-1' }], [{ version: 'abc' }]])(+ 'rejects an invalid version query %j with 400 invalid_request',+ async (query) => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({+ config: { langfuse: withPromptSync(true) },+ params: { name: 'greeting' },+ query,+ }),+ res,+ );++ expect(res.statusCode).toBe(400);+ expect(res.body).toEqual({ code: 'invalid_request' });+ expect(global.fetch).not.toHaveBeenCalled();+ },+ );++ it('gets by the production label when no version is given', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'greeting',+ version: 3,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'greeting' } }),+ res,+ );++ expect(res.statusCode).toBe(200);+ expect(res.body).toEqual({+ name: 'greeting',+ version: 3,+ labels: ['production'],+ prompt: 'Hello {{name}}',+ });+ const [url] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('label=production');+ expect(url).not.toContain('version=');+ });++ it('gets by an exact version when one is given', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'greeting',+ version: 2,+ type: 'text',+ labels: [],+ prompt: 'Hi there',+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({+ config: { langfuse: withPromptSync(true) },+ params: { name: 'greeting' },+ query: { version: '2' },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ expect(res.body).toEqual({ name: 'greeting', version: 2, labels: [], prompt: 'Hi there' });+ const [url] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('version=2');+ });++ it('gets a prompt whose name contains a path separator', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'folder/name',+ version: 1,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'folder/name' } }),+ res,+ );++ expect(res.statusCode).toBe(200);+ expect(res.body).toEqual({+ name: 'folder/name',+ version: 1,+ labels: ['production'],+ prompt: 'Hello {{name}}',+ });+ const [url] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('/prompts/folder%2Fname');+ });++ it('maps a 404 from Langfuse to not_found', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(null, 404)) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'missing' } }),+ res,+ );++ expect(res.statusCode).toBe(404);+ expect(res.body).toEqual({ code: 'not_found' });+ });++ it('maps a chat prompt to 422 unsupported_type without its content', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'support-chat',+ version: 1,+ type: 'chat',+ labels: ['production'],+ prompt: [{ role: 'system', content: 'secret system content' }],+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'support-chat' } }),+ res,+ );++ expect(res.statusCode).toBe(422);+ expect(res.body).toEqual({ code: 'unsupported_type' });+ expect(JSON.stringify(res.body)).not.toContain('secret system content');+ });++ it('maps an upstream failure the same way as listPrompts', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest+ .fn()+ .mockResolvedValue(+ fetchJsonResponse({ message: 'MARKER_SECRET_LEAK_TOKEN' }, 500),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'greeting' } }),+ res,+ );++ expect(res.statusCode).toBe(502);+ expect(res.body).toEqual({ code: 'upstream', status: 500 });+ expect(JSON.stringify(res.body)).not.toContain('MARKER_SECRET_LEAK_TOKEN');+ });++ describe('tenant-switch gate reads the stored base config, not req.config', () => {+ it('returns 404 when req.config says enabled but the stored base config says disabled', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { handlers } = createPromptSyncHandlers(false);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(true) }, params: { name: 'greeting' } }),+ res,+ );++ expect(res.statusCode).toBe(404);+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('is allowed when req.config says disabled but the stored base config says enabled', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'greeting',+ version: 3,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({ config: { langfuse: withPromptSync(false) }, params: { name: 'greeting' } }),+ res,+ );++ expect(res.statusCode).toBe(200);+ });++ it('fetches with the stored destination and credentials, never a stale req.config connection', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'greeting',+ version: 3,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ ) as unknown as typeof fetch;+ const findConfigByPrincipal = jest.fn().mockResolvedValue(+ baseConfigDoc(+ withPromptSync(true, {+ destination: 'us',+ publicKey: 'pk-lf-new',+ secretKey: encryptV3('sk-lf-new'),+ }),+ ),+ );+ const { handlers, deps } = createHandlers({ findConfigByPrincipal });+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({+ config: {+ langfuse: withPromptSync(true, {+ destination: 'eu',+ publicKey: 'pk-lf-old',+ secretKey: encryptV3('sk-lf-old'),+ }),+ },+ params: { name: 'greeting' },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url, init] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('https://us.cloud.langfuse.com');+ expect(+ Buffer.from(init.headers.Authorization.replace('Basic ', ''), 'base64').toString(),+ ).toBe('pk-lf-new:sk-lf-new');+ expect(deps.findConfigByPrincipal).toHaveBeenCalledTimes(1);+ });+ });+ });++ describe('listPrompts and getPrompt never use the central env project for a tenant', () => {+ /** Strict isolation and fanout off, with central env keys configured — the+ * single-tenant topology where a tenant admin could otherwise read the+ * operator's central prompts. */+ function setUpSingleTenantWithEnvKeys() {+ delete process.env.TENANT_ISOLATION_STRICT;+ delete process.env.LANGFUSE_FANOUT_ENABLED;+ delete process.env.LANGFUSE_FANOUT_COLLECTOR_URL;+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';+ }++ it('listPrompts fetches from the tenant destination with the tenant Basic auth, never the env keys', async () => {+ setUpSingleTenantWithEnvKeys();+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc(withPromptSync(true, { destination: 'us' }))),+ });+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({ config: { langfuse: withPromptSync(true, { destination: 'us' }) } }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url, init] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('https://us.cloud.langfuse.com');+ expect(+ Buffer.from(init.headers.Authorization.replace('Basic ', ''), 'base64').toString(),+ ).toBe('pk-lf-1:sk-lf-secret');+ });++ it('getPrompt fetches from the tenant destination with the tenant Basic auth, never the env keys', async () => {+ setUpSingleTenantWithEnvKeys();+ global.fetch = jest.fn().mockResolvedValue(+ fetchJsonResponse({+ name: 'greeting',+ version: 3,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ ) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc(withPromptSync(true, { destination: 'us' }))),+ });+ const res = mockRes();++ await handlers.getPrompt(+ mockReq({+ config: { langfuse: withPromptSync(true, { destination: 'us' }) },+ params: { name: 'greeting' },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url, init] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toContain('https://us.cloud.langfuse.com');+ expect(+ Buffer.from(init.headers.Authorization.replace('Basic ', ''), 'base64').toString(),+ ).toBe('pk-lf-1:sk-lf-secret');+ });++ it('returns 409 not_configured for a tenant user with no stored connection, even though env keys are set', async () => {+ setUpSingleTenantWithEnvKeys();+ const { handlers } = createPromptSyncHandlers(true, {+ findConfigByPrincipal: jest+ .fn()+ .mockResolvedValue(baseConfigDoc({ promptSync: { enabled: true } })),+ });+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({ config: { langfuse: { promptSync: { enabled: true } } } }),+ res,+ );++ expect(res.statusCode).toBe(409);+ expect(res.body).toEqual({ code: 'not_configured' });+ expect(global.fetch).not.toHaveBeenCalled();+ });++ it('uses the env keys for a request without a tenantId in that same single-tenant setup', async () => {+ setUpSingleTenantWithEnvKeys();+ global.fetch = jest+ .fn()+ .mockResolvedValue(fetchJsonResponse(promptListBody())) as unknown as typeof fetch;+ const { handlers } = createPromptSyncHandlers(true);+ const res = mockRes();++ await handlers.listPrompts(+ mockReq({+ user: { id: 'u1', role: 'ADMIN' },+ config: { langfuse: { promptSync: { enabled: true } } },+ }),+ res,+ );++ expect(res.statusCode).toBe(200);+ const [url, init] = (global.fetch as unknown as jest.Mock).mock.calls[0];+ expect(url).toBe('https://cloud.langfuse.com/api/public/v2/prompts');+ expect(+ Buffer.from(init.headers.Authorization.replace('Basic ', ''), 'base64').toString(),+ ).toBe('env-public:env-secret');+ });+ });});diff --git a/packages/api/src/admin/langfuse.ts b/packages/api/src/admin/langfuse.tsindex 29ae9305af..ae45cf0159 100644--- a/packages/api/src/admin/langfuse.ts+++ b/packages/api/src/admin/langfuse.ts@@ -1,3 +1,4 @@+import { z } from 'zod';import { PrincipalType, PrincipalModel } from 'librechat-data-provider';import {logger,@@ -13,19 +14,39 @@ import type {TLangfuseConnectionTestResponse,TLangfuseSessionLinkResponse,TLangfuseUnavailableResponse,+ TUpdateLangfusePromptSyncRequest,+ TLangfusePromptListResponse,+ TLangfusePromptGetResponse,+ TLangfusePromptErrorBody,} from 'librechat-data-provider';-import type { ConfigRecord, MessageMethods } from '@librechat/data-schemas';+import type { AppConfig, ConfigRecord, MessageMethods } from '@librechat/data-schemas';import type { Response } from 'express';+import type {+ LangfusePromptConnection,+ LangfuseTextPromptSelector,+ LangfusePromptRequestErrorCode,+} from '~/langfuse/prompts';import type { LangfuseTenantDestination } from '~/langfuse/tenantDestinations';import type { ServerRequest } from '~/types/http';import type { RecordAuditEntry } from './audit';+import {+ isLangfuseConnectionAvailable,+ isLangfusePromptSyncAvailable,+ getLangfusePromptSyncTimeoutMs,+ getLangfuseConnectionAvailability,+} from '~/langfuse/policy';+import {+ resolveLangfusePromptConnection,+ listLangfusePrompts,+ getLangfuseTextPrompt,+ LangfusePromptRequestError,+} from '~/langfuse/prompts';import {getLangfuseTenantDestinations,resolveLangfuseTenantDestination,} from '~/langfuse/tenantDestinations';import { redirectPolicyFor, resolveLangfuseHeaders } from '~/langfuse/utils';import { decryptConfigSecret, encryptConfigSecretFields } from './secrets';-import { getLangfuseConnectionAvailability } from '~/langfuse/policy';import { scopeHeadersToDestination } from '~/langfuse/destinations';import { resolveLangfuseSession } from '~/langfuse/session';import { getSafeErrorMetadata } from '~/utils/errors';@@ -80,7 +101,7 @@ export interface AdminLangfuseDeps {getMessages: MessageMethods['getMessages'];invalidateConfigCaches?: (tenantId?: string) => Promise<void>;recordConnectionUpdate?: (event: LangfuseConnectionEvent) => void;- /** Optional audit emission for connection updates; fail-open unless `auditFailClosed`. */+ /** Optional audit emission for connection and prompt sync updates; fail-open unless `auditFailClosed`. */recordAuditEntry?: RecordAuditEntry;auditFailClosed?: boolean;}@@ -100,7 +121,9 @@ function readStoredLangfuse(config: ConfigRecord | null): TCustomConfig['langfusfunction buildStatus(config: ConfigRecord | null): TLangfuseConnectionStatus {const stored = readStoredLangfuse(config);const configured = Boolean(stored?.publicKey && stored?.secretKey);+ const promptSyncAvailable = isLangfusePromptSyncAvailable();return {+ connectionAvailable: isLangfuseConnectionAvailable(),configured,enabled: configured && stored?.enabled === true,destinations: getLangfuseTenantDestinations(),@@ -108,6 +131,10 @@ function buildStatus(config: ConfigRecord | null): TLangfuseConnectionStatus {publicKey: stored?.publicKey,secretKeyPreview: stored?.secretKeyPreview,updatedAt: config?.updatedAt ? new Date(config.updatedAt).toISOString() : undefined,+ promptSync: {+ available: promptSyncAvailable,+ enabled: promptSyncAvailable && stored?.promptSync?.enabled === true,+ },};}@@ -156,6 +183,56 @@ function rejectWhenConnectionUnavailable(res: Response): Response | undefined {return res.status(404).json(body);}+const promptSyncUpdateSchema: z.ZodType<TUpdateLangfusePromptSyncRequest> = z+ .object({ enabled: z.boolean() })+ .strict();++const trimmedPromptString = (maxLength: number) => z.string().trim().min(1).max(maxLength);++const promptListQuerySchema = z.object({+ name: trimmedPromptString(256).optional(),+ label: trimmedPromptString(256).optional(),+ tag: trimmedPromptString(256).optional(),+ page: z.coerce.number().int().positive().max(Number.MAX_SAFE_INTEGER).optional(),+ limit: z.coerce.number().int().min(1).max(100).optional(),+ fromUpdatedAt: z.string().datetime().optional(),+ toUpdatedAt: z.string().datetime().optional(),+});++const promptNameParamSchema = trimmedPromptString(256);++const promptVersionQuerySchema = z.coerce+ .number()+ .int()+ .positive()+ .max(Number.MAX_SAFE_INTEGER)+ .optional();++/** HTTP status for a `LangfusePromptRequestError`. A Langfuse `unauthorized`+ * never maps to 401/403: the LibreChat client treats either as its own+ * session expiring and reacts by refreshing the token or signing out. */+function promptRequestErrorStatus(code: LangfusePromptRequestErrorCode): number {+ return code === 'timeout' ? 504 : 502;+}++/** Maps a thrown Langfuse prompt request failure to the stable `{ code }`+ * contract. Never forwards the upstream body, headers or credentials; logs+ * only the error's own code/status/message, which `prompts.ts` guarantees+ * are free of secrets. */+function respondToPromptRequestError(res: Response, error: unknown, logPrefix: string): Response {+ if (error instanceof LangfusePromptRequestError) {+ logger.error(logPrefix, error);+ const body: TLangfusePromptErrorBody =+ error.code === 'upstream' && error.status != null+ ? { code: error.code, status: error.status }+ : { code: error.code };+ return res.status(promptRequestErrorStatus(error.code)).json(body);+ }++ logger.error(logPrefix, error);+ return res.status(500).json({ code: 'upstream' } satisfies TLangfusePromptErrorBody);+}+type LangfuseVerificationFailure = {errorCode: TLangfuseConnectionTestErrorCode;message: string;@@ -305,6 +382,9 @@ export function createAdminLangfuseHandlers(deps: AdminLangfuseDeps): {getSessionLink: (req: ServerRequest, res: Response) => Promise<Response>;updateConnection: (req: ServerRequest, res: Response) => Promise<Response>;testConnection: (req: ServerRequest, res: Response) => Promise<Response>;+ updatePromptSync: (req: ServerRequest, res: Response) => Promise<Response>;+ listPrompts: (req: ServerRequest, res: Response) => Promise<Response>;+ getPrompt: (req: ServerRequest, res: Response) => Promise<Response>;} {const {findConfigByPrincipal,@@ -329,10 +409,65 @@ export function createAdminLangfuseHandlers(deps: AdminLangfuseDeps): {: findConfigByPrincipal(PrincipalType.ROLE, BASE_CONFIG_PRINCIPAL_ID);}- async function getConnection(req: ServerRequest, res: Response): Promise<Response> {- const disabledResponse = rejectWhenConnectionUnavailable(res);+ /** Shared 404 gate for the prompt-sync routes. `updatePromptSync` only+ * needs the env-availability check: it is itself the write path for the+ * stored switch, so it has nothing stored to compare against. */+ function rejectWhenPromptSyncUnavailable(res: Response): Response | undefined {+ if (!isLangfusePromptSyncAvailable()) {+ return res.status(404).json({ error: 'Langfuse prompt sync is not available' });+ }+ return undefined;+ }++ /** Tenant-switch gate for the list/get prompt routes. Reads the stored base+ * config once — the same source `buildStatus` reads — and hands that one+ * document back to the caller so it can also become the connection's+ * tenant-owned fields (destination, keys, `promptSync`) instead of the+ * cached `req.config`: a rotated destination or key can then never be+ * paired with a stale credential from another instance's config cache.+ * Checking `promptSync.enabled` off this same read also keeps this gate+ * from disagreeing with the toggle's own status response through the+ * config cache's TTL or across instances. */+ async function gatePromptSyncTenantSwitch(+ res: Response,+ ): Promise<+ | { response: Response; stored?: undefined }+ | { response?: undefined; stored: TCustomConfig['langfuse'] }+ > {+ const disabledResponse = rejectWhenPromptSyncUnavailable(res);if (disabledResponse) {- return disabledResponse;+ return { response: disabledResponse };+ }++ const stored = readStoredLangfuse(await findBaseConfig());+ if (stored?.promptSync?.enabled !== true) {+ return {+ response: res.status(404).json({ error: 'Langfuse prompt sync is not available' }),+ };+ }++ return { stored };+ }++ /** Prompt reads use the same stored document for their switch and credentials.+ * Only proxy headers come from the deployment config. */+ function resolvePromptSyncConnection(+ req: ServerRequest,+ stored: TCustomConfig['langfuse'],+ ): LangfusePromptConnection | null {+ const appConfig = {+ ...req.config,+ langfuse: { ...stored, headers: req.config?.langfuse?.headers },+ } as AppConfig;+ return resolveLangfusePromptConnection(appConfig, { tenantId: getTenantId(req) });+ }++ async function getConnection(req: ServerRequest, res: Response): Promise<Response> {+ if (!isLangfusePromptSyncAvailable()) {+ const disabledResponse = rejectWhenConnectionUnavailable(res);+ if (disabledResponse) {+ return disabledResponse;+ }}try {@@ -624,5 +759,119 @@ export function createAdminLangfuseHandlers(deps: AdminLangfuseDeps): {}}- return { getConnection, getSessionLink, updateConnection, testConnection };+ async function updatePromptSync(req: ServerRequest, res: Response): Promise<Response> {+ const disabledResponse = rejectWhenPromptSyncUnavailable(res);+ if (disabledResponse) {+ return disabledResponse;+ }++ const parsedBody = promptSyncUpdateSchema.safeParse(req.body);+ if (!parsedBody.success) {+ return res.status(400).json({ code: 'invalid_request' } satisfies TLangfusePromptErrorBody);+ }++ try {+ // Omitting priority preserves the stored value. The patch also keeps+ // an inactive base config inactive.+ const updated = await patchConfigFields(+ PrincipalType.ROLE,+ BASE_CONFIG_PRINCIPAL_ID,+ PrincipalModel.ROLE,+ { 'langfuse.promptSync.enabled': parsedBody.data.enabled },+ );++ invalidateConfigCaches?.(getTenantId(req))?.catch((err) =>+ logger.error('[adminLangfuse] Cache invalidation failed after prompt sync update:', err),+ );++ await emitAudit(req, {+ action: 'langfuse.updated',+ severity: 'warning',+ target: { type: 'langfuse', id: 'prompt-sync' },+ metadata: { enabled: parsedBody.data.enabled },+ });++ // An inactive config is invisible to `getConnection` (which reads+ // without `includeInactive`), so report the same empty status here+ // rather than echoing back fields nobody else can see.+ return res.status(200).json(buildStatus(updated?.isActive === false ? null : updated));+ } catch (error) {+ logger.error('[adminLangfuse] updatePromptSync error:', error);+ return res.status(500).json({ error: 'Failed to update Langfuse prompt sync setting' });+ }+ }++ async function listPrompts(req: ServerRequest, res: Response): Promise<Response> {+ const gate = await gatePromptSyncTenantSwitch(res);+ if (gate.response) {+ return gate.response;+ }++ const parsedQuery = promptListQuerySchema.safeParse(req.query);+ if (!parsedQuery.success) {+ return res.status(400).json({ code: 'invalid_request' } satisfies TLangfusePromptErrorBody);+ }++ const connection = resolvePromptSyncConnection(req, gate.stored);+ if (!connection) {+ return res.status(409).json({ code: 'not_configured' } satisfies TLangfusePromptErrorBody);+ }++ try {+ const result = await listLangfusePrompts(connection, parsedQuery.data, {+ timeoutMs: getLangfusePromptSyncTimeoutMs(),+ });+ return res.status(200).json(result satisfies TLangfusePromptListResponse);+ } catch (error) {+ return respondToPromptRequestError(res, error, '[adminLangfuse] listPrompts error:');+ }+ }++ async function getPrompt(req: ServerRequest, res: Response): Promise<Response> {+ const gate = await gatePromptSyncTenantSwitch(res);+ if (gate.response) {+ return gate.response;+ }++ const parsedName = promptNameParamSchema.safeParse((req.params as { name?: string }).name);+ const parsedVersion = promptVersionQuerySchema.safeParse(+ (req.query as { version?: unknown }).version,+ );+ if (!parsedName.success || !parsedVersion.success) {+ return res.status(400).json({ code: 'invalid_request' } satisfies TLangfusePromptErrorBody);+ }++ const connection = resolvePromptSyncConnection(req, gate.stored);+ if (!connection) {+ return res.status(409).json({ code: 'not_configured' } satisfies TLangfusePromptErrorBody);+ }++ const selector: LangfuseTextPromptSelector =+ parsedVersion.data != null ? { version: parsedVersion.data } : { label: 'production' };++ try {+ const result = await getLangfuseTextPrompt(connection, parsedName.data, selector, {+ timeoutMs: getLangfusePromptSyncTimeoutMs(),+ });+ if (!result.ok) {+ const status = result.error.code === 'not_found' ? 404 : 422;+ return res+ .status(status)+ .json({ code: result.error.code } satisfies TLangfusePromptErrorBody);+ }+ return res.status(200).json(result.value satisfies TLangfusePromptGetResponse);+ } catch (error) {+ return respondToPromptRequestError(res, error, '[adminLangfuse] getPrompt error:');+ }+ }++ return {+ getConnection,+ getSessionLink,+ updateConnection,+ testConnection,+ updatePromptSync,+ listPrompts,+ getPrompt,+ };}diff --git a/packages/api/src/langfuse/destinations.ts b/packages/api/src/langfuse/destinations.tsindex d237ec8609..f1c40fa110 100644--- a/packages/api/src/langfuse/destinations.ts+++ b/packages/api/src/langfuse/destinations.ts@@ -84,7 +84,7 @@ export function getLangfuseDestinationId(baseUrl: string, projectId: string): st.digest('hex');}-function getCentralEnvBaseUrl(): string {+export function getCentralEnvBaseUrl(): string {return (normalizeString(process.env.LANGFUSE_BASE_URL) ??normalizeString(process.env.LANGFUSE_HOST) ??diff --git a/packages/api/src/langfuse/index.ts b/packages/api/src/langfuse/index.tsindex d7e23bbf5c..63074258e0 100644--- a/packages/api/src/langfuse/index.ts+++ b/packages/api/src/langfuse/index.ts@@ -1,6 +1,7 @@export * from './destinations';export * from './feedback';export * from './policy';+export * from './prompts';export * from './reader';export * from './trace';export * from './session';diff --git a/packages/api/src/langfuse/policy.spec.ts b/packages/api/src/langfuse/policy.spec.tsindex bbac970538..ed2255f26c 100644--- a/packages/api/src/langfuse/policy.spec.ts+++ b/packages/api/src/langfuse/policy.spec.ts@@ -7,6 +7,8 @@ const envKeys = ['LANGFUSE_FANOUT_COLLECTOR_URL','LANGFUSE_FANOUT_TENANT_EXPORT_DISABLED','TENANT_ISOLATION_STRICT',+ 'LANGFUSE_PROMPT_SYNC_AVAILABLE',+ 'LANGFUSE_PROMPT_SYNC_TIMEOUT_MS',];function clearEnv() {@@ -118,4 +120,87 @@ describe('Langfuse policy', () => {process.env.LANGFUSE_SAMPLE_RATE = 'invalid';expect(getLangfuseSampleRate()).toBe(1);});++ it('hides Langfuse prompt sync by default', async () => {+ const { isLangfusePromptSyncAvailable } = await import('./policy');++ expect(isLangfusePromptSyncAvailable()).toBe(false);+ });++ it('makes Langfuse prompt sync available when the environment switch is on', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ const { isLangfusePromptSyncAvailable } = await import('./policy');++ expect(isLangfusePromptSyncAvailable()).toBe(true);+ });++ it('hides Langfuse prompt sync when the environment switch is explicitly off', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'false';+ const { isLangfusePromptSyncAvailable } = await import('./policy');++ expect(isLangfusePromptSyncAvailable()).toBe(false);+ });++ it('defaults the Langfuse prompt sync timeout to 10 seconds', async () => {+ const { getLangfusePromptSyncTimeoutMs } = await import('./policy');++ expect(getLangfusePromptSyncTimeoutMs()).toBe(10000);+ });++ it('uses a configured Langfuse prompt sync timeout', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_TIMEOUT_MS = '5000';+ const { getLangfusePromptSyncTimeoutMs } = await import('./policy');++ expect(getLangfusePromptSyncTimeoutMs()).toBe(5000);+ });++ it('clamps the Langfuse prompt sync timeout to 1000-60000', async () => {+ const { getLangfusePromptSyncTimeoutMs } = await import('./policy');++ process.env.LANGFUSE_PROMPT_SYNC_TIMEOUT_MS = '1';+ expect(getLangfusePromptSyncTimeoutMs()).toBe(1000);++ process.env.LANGFUSE_PROMPT_SYNC_TIMEOUT_MS = '1000000';+ expect(getLangfusePromptSyncTimeoutMs()).toBe(60000);+ });++ it('falls back to the default Langfuse prompt sync timeout for non-numeric values', async () => {+ process.env.LANGFUSE_PROMPT_SYNC_TIMEOUT_MS = 'invalid';+ const { getLangfusePromptSyncTimeoutMs } = await import('./policy');++ expect(getLangfusePromptSyncTimeoutMs()).toBe(10000);+ });+});++describe('Langfuse settings availability', () => {+ beforeEach(clearEnv);+ afterEach(clearEnv);++ it.each([+ ['environment credentials', { LANGFUSE_PUBLIC_KEY: 'pk-env', LANGFUSE_SECRET_KEY: 'sk-env' }],+ ['tracing disabled', { LANGFUSE_TRACING_ENABLED: 'false' }],+ ['zero sampling', { LANGFUSE_SAMPLE_RATE: '0' }],+ ['strict tenants without fanout', { TENANT_ISOLATION_STRICT: 'true' }],+ [+ 'tenant export disabled',+ {+ LANGFUSE_FANOUT_ENABLED: 'true',+ LANGFUSE_FANOUT_COLLECTOR_URL: 'http://collector:4318',+ LANGFUSE_FANOUT_TENANT_EXPORT_DISABLED: 'true',+ },+ ],+ ])('offers prompt settings with %s without enabling connection editing', async (_mode, env) => {+ Object.assign(process.env, env);+ const { isLangfuseSettingsAvailable, isLangfuseConnectionAvailable } = await import('./policy');+ expect(isLangfuseSettingsAvailable()).toBe(false);+ process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE = 'true';+ expect(isLangfuseSettingsAvailable()).toBe(true);+ expect(isLangfuseConnectionAvailable()).toBe(false);+ });++ it('offers connection settings while prompt sync is unavailable', async () => {+ const { isLangfuseSettingsAvailable, isLangfusePromptSyncAvailable } = await import('./policy');+ expect(isLangfuseSettingsAvailable()).toBe(true);+ expect(isLangfusePromptSyncAvailable()).toBe(false);+ });});diff --git a/packages/api/src/langfuse/policy.ts b/packages/api/src/langfuse/policy.tsindex 6e1af0365f..13aeab0f38 100644--- a/packages/api/src/langfuse/policy.ts+++ b/packages/api/src/langfuse/policy.ts@@ -1,5 +1,5 @@import { isForcedTemporaryRetention } from 'librechat-data-provider';-import type { TLangfuseUnavailableReason } from 'librechat-data-provider';+import type { TStartupConfig, TLangfuseUnavailableReason } from 'librechat-data-provider';import type { AppConfig } from '@librechat/data-schemas';import { isFalseEnv, isTrueEnv } from './utils';import { normalizeString } from '~/utils/text';@@ -95,6 +95,29 @@ export function isLangfuseTraceSampled(traceId: string): boolean {return traceIdAccumulation(traceId) < Math.floor(sampleRate * MAX_TRACE_ID_ACCUMULATION);}+const DEFAULT_PROMPT_SYNC_TIMEOUT_MS = 10_000;+const MIN_PROMPT_SYNC_TIMEOUT_MS = 1_000;+const MAX_PROMPT_SYNC_TIMEOUT_MS = 60_000;++export function isLangfusePromptSyncAvailable(): boolean {+ return isTrueEnv(process.env.LANGFUSE_PROMPT_SYNC_AVAILABLE);+}++export function getLangfusePromptSyncTimeoutMs(): number {+ const value = normalizeString(process.env.LANGFUSE_PROMPT_SYNC_TIMEOUT_MS);+ if (value == null) {+ return DEFAULT_PROMPT_SYNC_TIMEOUT_MS;+ }++ const timeoutMs = Number(value);+ if (!Number.isFinite(timeoutMs)) {+ return DEFAULT_PROMPT_SYNC_TIMEOUT_MS;+ }+ return Math.round(+ Math.min(MAX_PROMPT_SYNC_TIMEOUT_MS, Math.max(MIN_PROMPT_SYNC_TIMEOUT_MS, timeoutMs)),+ );+}+/** Whether the deployment offers Langfuse connection settings, and if not, why. */export type LangfuseConnectionAvailability =| { available: true }@@ -128,3 +151,18 @@ export function getLangfuseConnectionAvailability(): LangfuseConnectionAvailabilexport function isLangfuseConnectionAvailable(): boolean {return getLangfuseConnectionAvailability().available;}++/** Settings remain available for prompt reads when trace connection editing is disabled. */+export function isLangfuseSettingsAvailable(): boolean {+ return isLangfuseConnectionAvailable() || isLangfusePromptSyncAvailable();+}++/** One permission check supplies both settings gates. Trace links keep their connection gate. */+export function getLangfuseSettingsAccess(+ canManage: boolean,+): Pick<TStartupConfig, 'langfuseConnectionAccess' | 'langfusePromptSyncAccess'> {+ return {+ langfuseConnectionAccess: canManage && isLangfuseConnectionAvailable(),+ langfusePromptSyncAccess: canManage && isLangfusePromptSyncAvailable(),+ };+}diff --git a/packages/api/src/langfuse/prompts.spec.ts b/packages/api/src/langfuse/prompts.spec.tsnew file mode 100644index 0000000000..d5b8b52005--- /dev/null+++ b/packages/api/src/langfuse/prompts.spec.ts@@ -0,0 +1,553 @@+process.env.CREDS_KEY =+ process.env.CREDS_KEY ?? '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';++const langfuseEnvKeys = [+ 'LANGFUSE_PUBLIC_KEY',+ 'LANGFUSE_SECRET_KEY',+ 'LANGFUSE_PROJECT_ID',+ 'LANGFUSE_BASE_URL',+ 'LANGFUSE_HOST',+ 'LANGFUSE_BASEURL',+ 'LANGFUSE_FANOUT_ENABLED',+ 'LANGFUSE_FANOUT_COLLECTOR_URL',+ 'LANGFUSE_FANOUT_TENANT_DESTINATIONS',+ 'LANGFUSE_FANOUT_TENANT_EU_BASE_URL',+ 'LANGFUSE_FANOUT_TENANT_US_BASE_URL',+ 'LANGFUSE_FANOUT_TENANT_JP_BASE_URL',+ 'TENANT_ISOLATION_STRICT',+];++function clearLangfuseEnv() {+ for (const key of langfuseEnvKeys) {+ delete process.env[key];+ }+}++// Cleared before the module graph loads: another spec file running earlier in+// this worker may have left Langfuse env vars set, and `./destinations` reads+// them at import time for its own warm-up lookup.+clearLangfuseEnv();++jest.mock(+ '@librechat/data-schemas',+ () => ({+ logger: { debug: jest.fn(), error: jest.fn(), warn: jest.fn(), info: jest.fn() },+ }),+ { virtual: true },+);++jest.mock('~/admin/secrets', () => ({+ decryptConfigSecret: jest.fn((value: string) =>+ value === 'v3:test:tenant-secret-key' ? 'tenant-secret-key' : undefined,+ ),+}));++import type { AppConfig } from '@librechat/data-schemas';+import {+ listLangfusePrompts,+ getLangfuseTextPrompt,+ resolveLangfusePromptConnection,+ LangfusePromptRequestError,+ type LangfusePromptConnection,+} from './prompts';++function jsonResponse(body: unknown, status = 200): Response {+ return new Response(JSON.stringify(body), {+ status,+ headers: { 'Content-Type': 'application/json' },+ });+}++function makeConnection(+ overrides: Partial<LangfusePromptConnection> = {},+): LangfusePromptConnection {+ return {+ baseUrl: 'https://cloud.langfuse.test',+ authorization: 'Basic cGs6c2s=',+ ...overrides,+ };+}++function appConfigWithLangfuse(langfuse?: AppConfig['langfuse']): AppConfig {+ return { langfuse } as AppConfig;+}++let fetchMock: jest.SpiedFunction<typeof fetch>;++beforeEach(() => {+ clearLangfuseEnv();+ fetchMock = jest.spyOn(global, 'fetch');+});++afterEach(() => {+ clearLangfuseEnv();+ fetchMock.mockRestore();+ jest.clearAllMocks();+});++describe('listLangfusePrompts', () => {+ it('passes through only the defined query params, sends no filter, and keeps every type', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({+ data: [+ {+ name: 'greeting',+ type: 'text',+ versions: [1, 2],+ labels: ['production'],+ tags: ['demo'],+ lastUpdatedAt: '2026-01-01T00:00:00.000Z',+ },+ {+ name: 'support-chat',+ type: 'chat',+ versions: [1],+ labels: [],+ tags: [],+ lastUpdatedAt: '2026-01-02T00:00:00.000Z',+ },+ ],+ meta: { page: 1, limit: 10, totalItems: 2, totalPages: 1 },+ }),+ );++ const result = await listLangfusePrompts(+ conn,+ { name: 'greeting', label: 'production', page: 1, limit: 10 },+ { timeoutMs: 5000 },+ );++ expect(fetchMock).toHaveBeenCalledTimes(1);+ const [url, init] = fetchMock.mock.calls[0];+ expect(url).toBe(+ 'https://cloud.langfuse.test/api/public/v2/prompts?name=greeting&label=production&page=1&limit=10',+ );+ expect(url).not.toContain('filter');+ expect(init).toMatchObject({+ method: 'GET',+ headers: expect.objectContaining({ Authorization: conn.authorization }),+ });+ expect(result.items).toHaveLength(2);+ expect(result.items[0]).toMatchObject({ name: 'greeting', type: 'text' });+ expect(result.items[1]).toMatchObject({ name: 'support-chat', type: 'chat' });+ expect(result.meta).toEqual({ page: 1, limit: 10, totalItems: 2, totalPages: 1 });+ });++ it('omits undefined query params entirely', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({ data: [], meta: { page: 1, limit: 10, totalItems: 0, totalPages: 0 } }),+ );++ await listLangfusePrompts(conn, {}, { timeoutMs: 5000 });++ const [url] = fetchMock.mock.calls[0];+ expect(url).toBe('https://cloud.langfuse.test/api/public/v2/prompts');+ });++ it('sends tag, fromUpdatedAt and toUpdatedAt as given', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({ data: [], meta: { page: 1, limit: 10, totalItems: 0, totalPages: 0 } }),+ );++ await listLangfusePrompts(+ conn,+ {+ tag: 'demo',+ fromUpdatedAt: '2026-01-01T00:00:00.000Z',+ toUpdatedAt: '2026-02-01T00:00:00.000Z',+ },+ { timeoutMs: 5000 },+ );++ const [url] = fetchMock.mock.calls[0];+ expect(url).toBe(+ 'https://cloud.langfuse.test/api/public/v2/prompts?tag=demo&fromUpdatedAt=2026-01-01T00%3A00%3A00.000Z&toUpdatedAt=2026-02-01T00%3A00%3A00.000Z',+ );+ });+});++describe('getLangfuseTextPrompt', () => {+ it('gets a text prompt by the production label and URL-encodes the name', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({+ name: 'a/b c',+ version: 3,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ );++ const result = await getLangfuseTextPrompt(+ conn,+ 'a/b c',+ { label: 'production' },+ { timeoutMs: 5000 },+ );++ expect(fetchMock).toHaveBeenCalledWith(+ 'https://cloud.langfuse.test/api/public/v2/prompts/a%2Fb%20c?label=production',+ expect.objectContaining({+ method: 'GET',+ headers: expect.objectContaining({ Authorization: conn.authorization }),+ }),+ );+ expect(result).toEqual({+ ok: true,+ value: { name: 'a/b c', version: 3, labels: ['production'], prompt: 'Hello {{name}}' },+ });+ });++ it('URL-encodes a prompt name containing a path separator', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({+ name: 'folder/name',+ version: 1,+ type: 'text',+ labels: ['production'],+ prompt: 'Hello {{name}}',+ }),+ );++ const result = await getLangfuseTextPrompt(+ conn,+ 'folder/name',+ { label: 'production' },+ { timeoutMs: 5000 },+ );++ expect(fetchMock).toHaveBeenCalledWith(+ 'https://cloud.langfuse.test/api/public/v2/prompts/folder%2Fname?label=production',+ expect.anything(),+ );+ expect(result).toEqual({+ ok: true,+ value: { name: 'folder/name', version: 1, labels: ['production'], prompt: 'Hello {{name}}' },+ });+ });++ it('gets a text prompt by an exact version', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({ name: 'greeting', version: 2, type: 'text', labels: [], prompt: 'Hi there' }),+ );++ const result = await getLangfuseTextPrompt(+ conn,+ 'greeting',+ { version: 2 },+ { timeoutMs: 5000 },+ );++ expect(fetchMock).toHaveBeenCalledWith(+ 'https://cloud.langfuse.test/api/public/v2/prompts/greeting?version=2',+ expect.anything(),+ );+ expect(result).toEqual({+ ok: true,+ value: { name: 'greeting', version: 2, labels: [], prompt: 'Hi there' },+ });+ });++ it('rejects a chat prompt as unsupported_type without returning its content', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(+ jsonResponse({+ name: 'support-chat',+ version: 1,+ type: 'chat',+ labels: ['production'],+ prompt: [{ role: 'system', content: 'secret system content' }],+ }),+ );++ const result = await getLangfuseTextPrompt(+ conn,+ 'support-chat',+ { label: 'production' },+ { timeoutMs: 5000 },+ );++ expect(result).toEqual({ ok: false, error: { code: 'unsupported_type' } });+ expect(JSON.stringify(result)).not.toContain('secret system content');+ });++ it('reports not_found on a 404', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(new Response(null, { status: 404 }));++ const result = await getLangfuseTextPrompt(+ conn,+ 'missing',+ { label: 'production' },+ { timeoutMs: 5000 },+ );++ expect(result).toEqual({ ok: false, error: { code: 'not_found' } });+ });+});++describe('Langfuse prompt request errors', () => {+ it.each([401, 403])('maps a %i response to unauthorized', async (status) => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(new Response(null, { status }));++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'unauthorized',+ });+ });++ it('maps a 500 response to upstream with the status, never forwarding the body', async () => {+ const conn = makeConnection({ authorization: 'Basic cGs6c2VjcmV0LWtleQ==' });+ fetchMock.mockResolvedValue(jsonResponse({ message: 'MARKER_SECRET_LEAK_TOKEN' }, 500));++ const error: unknown = await listLangfusePrompts(conn, {}, { timeoutMs: 5000 }).catch(+ (caught) => caught,+ );++ expect(error).toBeInstanceOf(LangfusePromptRequestError);+ const requestError = error as LangfusePromptRequestError;+ expect(requestError.code).toBe('upstream');+ expect(requestError.status).toBe(500);+ expect(requestError.message).not.toContain('MARKER_SECRET_LEAK_TOKEN');+ expect(requestError.message).not.toContain(conn.authorization);+ expect(JSON.stringify(requestError)).not.toContain('MARKER_SECRET_LEAK_TOKEN');+ expect(JSON.stringify(requestError)).not.toContain(conn.authorization);+ });++ it('maps a timeout to timeout', async () => {+ const conn = makeConnection();+ const timeout = new Error('The operation was aborted due to timeout');+ timeout.name = 'TimeoutError';+ fetchMock.mockRejectedValue(timeout);++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'timeout',+ });+ });++ it('maps an abort to timeout', async () => {+ const conn = makeConnection();+ const abort = new Error('The operation was aborted');+ abort.name = 'AbortError';+ fetchMock.mockRejectedValue(abort);++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'timeout',+ });+ });++ it('maps a body read aborted by timeout to timeout, not invalid_response', async () => {+ const conn = makeConnection();+ const abort = new Error('The operation was aborted due to timeout');+ abort.name = 'TimeoutError';+ fetchMock.mockResolvedValue({+ ok: true,+ status: 200,+ json: jest.fn().mockRejectedValue(abort),+ } as unknown as Response);++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'timeout',+ });+ });++ it('maps a network error to upstream without a status', async () => {+ const conn = makeConnection();+ fetchMock.mockRejectedValue(new TypeError('fetch failed'));++ const error: unknown = await listLangfusePrompts(conn, {}, { timeoutMs: 5000 }).catch(+ (caught) => caught,+ );++ expect(error).toBeInstanceOf(LangfusePromptRequestError);+ const requestError = error as LangfusePromptRequestError;+ expect(requestError.code).toBe('upstream');+ expect(requestError.status).toBeUndefined();+ });++ it('maps unparseable JSON to invalid_response', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(new Response('not json', { status: 200 }));++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'invalid_response',+ });+ });++ it('maps a response with the wrong shape to invalid_response', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(jsonResponse({ data: 'not-an-array' }));++ await expect(listLangfusePrompts(conn, {}, { timeoutMs: 5000 })).rejects.toMatchObject({+ code: 'invalid_response',+ });+ });++ it('maps a get response with the wrong shape to invalid_response', async () => {+ const conn = makeConnection();+ fetchMock.mockResolvedValue(jsonResponse({ name: 'greeting' }));++ await expect(+ getLangfuseTextPrompt(conn, 'greeting', { label: 'production' }, { timeoutMs: 5000 }),+ ).rejects.toMatchObject({ code: 'invalid_response' });+ });+});++describe('resolveLangfusePromptConnection', () => {+ it('resolves the central env project in single-tenant mode when env keys are set', () => {+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';++ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'us',+ }),+ );++ expect(conn).toEqual({+ baseUrl: 'https://cloud.langfuse.com',+ authorization: `Basic ${Buffer.from('env-public:env-secret').toString('base64')}`,+ });+ });++ it('resolves the tenant stored connection in single-tenant mode without env keys', () => {+ process.env.LANGFUSE_FANOUT_TENANT_DESTINATIONS = 'us=https://us.cloud.langfuse.example';++ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'us',+ }),+ );++ expect(conn).toEqual({+ baseUrl: 'https://us.cloud.langfuse.example',+ authorization: `Basic ${Buffer.from('tenant-public-key:tenant-secret-key').toString('base64')}`,+ });+ });++ it('uses only the tenant connection in multi-tenant mode, even when env keys are set', () => {+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';+ process.env.TENANT_ISOLATION_STRICT = 'true';+ process.env.LANGFUSE_FANOUT_ENABLED = 'true';+ process.env.LANGFUSE_FANOUT_COLLECTOR_URL = 'http://collector:4318';+ process.env.LANGFUSE_FANOUT_TENANT_DESTINATIONS = 'us=https://us.cloud.langfuse.example';++ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'us',+ }),+ );++ expect(conn).toEqual({+ baseUrl: 'https://us.cloud.langfuse.example',+ authorization: `Basic ${Buffer.from('tenant-public-key:tenant-secret-key').toString('base64')}`,+ });+ });++ it('uses only the tenant connection for a tenant user while strict isolation is off', () => {+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';+ process.env.LANGFUSE_FANOUT_TENANT_DESTINATIONS = 'us=https://us.cloud.langfuse.example';++ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'us',+ }),+ { tenantId: 'tenant-a' },+ );++ expect(conn).toEqual({+ baseUrl: 'https://us.cloud.langfuse.example',+ authorization: `Basic ${Buffer.from('tenant-public-key:tenant-secret-key').toString('base64')}`,+ });+ });++ it('returns null for a tenant user with no stored connection, even when env keys exist', () => {+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';++ const conn = resolveLangfusePromptConnection(appConfigWithLangfuse(undefined), {+ tenantId: 'tenant-a',+ });++ expect(conn).toBeNull();+ });++ it('returns null in multi-tenant mode with no stored connection, even when env keys exist', () => {+ process.env.LANGFUSE_PUBLIC_KEY = 'env-public';+ process.env.LANGFUSE_SECRET_KEY = 'env-secret';+ process.env.TENANT_ISOLATION_STRICT = 'true';++ const conn = resolveLangfusePromptConnection(appConfigWithLangfuse(undefined));++ expect(conn).toBeNull();+ });++ it('returns null when the stored connection has no destination', () => {+ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ }),+ );++ expect(conn).toBeNull();+ });++ it('returns null when the stored connection names an unknown destination', () => {+ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'not-a-real-destination',+ }),+ );++ expect(conn).toBeNull();+ });++ it('returns null when the stored secret cannot be decrypted', () => {+ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ publicKey: 'tenant-public-key',+ secretKey: 'not-encrypted',+ destination: 'us',+ }),+ );++ expect(conn).toBeNull();+ });++ it('resolves the stored connection even when tracing is disabled for the tenant', () => {+ process.env.LANGFUSE_FANOUT_TENANT_DESTINATIONS = 'us=https://us.cloud.langfuse.example';++ const conn = resolveLangfusePromptConnection(+ appConfigWithLangfuse({+ enabled: false,+ publicKey: 'tenant-public-key',+ secretKey: 'v3:test:tenant-secret-key',+ destination: 'us',+ }),+ );++ expect(conn).toEqual({+ baseUrl: 'https://us.cloud.langfuse.example',+ authorization: `Basic ${Buffer.from('tenant-public-key:tenant-secret-key').toString('base64')}`,+ });+ });+});diff --git a/packages/api/src/langfuse/prompts.ts b/packages/api/src/langfuse/prompts.tsnew file mode 100644index 0000000000..89520bad2e--- /dev/null+++ b/packages/api/src/langfuse/prompts.ts@@ -0,0 +1,318 @@+import { z } from 'zod';+import type { TLangfusePromptListResponse } from 'librechat-data-provider';+import type { AppConfig } from '@librechat/data-schemas';+import {+ redirectPolicyFor,+ resolveLangfuseHeaders,+ resolveTenantCredentials,+ toBasicAuthorization,+} from './utils';+import { hasLangfuseEnvCredentials, usesLangfuseMultiTenantRouting } from './policy';+import { scopeHeadersToDestination, getCentralEnvBaseUrl } from './destinations';+import { resolveLangfuseTenantDestination } from './tenantDestinations';+import { mergeHeaders } from '~/utils/headers';+import { normalizeString } from '~/utils/text';++const PROMPTS_PATH = '/api/public/v2/prompts';++export type LangfusePromptConnection = {+ baseUrl: string;+ authorization: string;+ headers?: Record<string, string>;+};++export type LangfusePromptListQuery = {+ name?: string;+ label?: string;+ tag?: string;+ page?: number;+ limit?: number;+ fromUpdatedAt?: string;+ toUpdatedAt?: string;+};++export type LangfuseTextPromptSelector = { label: 'production' } | { version: number };++export type LangfuseTextPromptValue = {+ name: string;+ version: number;+ labels: string[];+ prompt: string;+};++export type LangfuseTextPromptResult =+ | { ok: true; value: LangfuseTextPromptValue }+ | { ok: false; error: { code: 'not_found' | 'unsupported_type' } };++export type LangfusePromptRequestErrorCode =+ | 'unauthorized'+ | 'timeout'+ | 'upstream'+ | 'invalid_response';++export class LangfusePromptRequestError extends Error {+ readonly code: LangfusePromptRequestErrorCode;+ readonly status?: number;++ constructor(code: LangfusePromptRequestErrorCode, message: string, status?: number) {+ super(message);+ this.name = 'LangfusePromptRequestError';+ this.code = code;+ if (status != null) {+ this.status = status;+ }+ }+}++const promptListItemSchema = z.object({+ name: z.string().min(1),+ type: z.enum(['text', 'chat']),+ versions: z.array(z.number()),+ labels: z.array(z.string()),+ tags: z.array(z.string()),+ lastUpdatedAt: z.string(),+});++const promptListResponseSchema = z.object({+ data: z.array(promptListItemSchema),+ meta: z.object({+ page: z.number(),+ limit: z.number(),+ totalItems: z.number(),+ totalPages: z.number(),+ }),+});++const promptBaseSchema = z.object({+ name: z.string().min(1),+ version: z.number(),+ type: z.enum(['text', 'chat']),+ labels: z.array(z.string()),+});++const textPromptSchema = promptBaseSchema.extend({+ type: z.literal('text'),+ prompt: z.string(),+});++/** Resolves the deployment's single Langfuse project from the environment keys. */+function resolveCentralEnvConnection(+ headers?: Record<string, string>,+): LangfusePromptConnection | null {+ const publicKey = normalizeString(process.env.LANGFUSE_PUBLIC_KEY);+ const secretKey = normalizeString(process.env.LANGFUSE_SECRET_KEY);+ if (!publicKey || !secretKey) {+ return null;+ }+ const baseUrl = getCentralEnvBaseUrl();+ const scopedHeaders = scopeHeadersToDestination(headers, baseUrl);+ return {+ baseUrl,+ authorization: toBasicAuthorization(publicKey, secretKey),+ ...(scopedHeaders ? { headers: scopedHeaders } : {}),+ };+}++/** Resolves the tenant's own Langfuse project from its stored connection. */+function resolveTenantConnection(+ appConfig?: AppConfig,+ headers?: Record<string, string>,+): LangfusePromptConnection | null {+ const config = appConfig?.langfuse;+ const credentials = resolveTenantCredentials(config);+ const destination = resolveLangfuseTenantDestination(config?.destination);+ if (!credentials || !destination) {+ return null;+ }+ const scopedHeaders = scopeHeadersToDestination(headers, destination.baseUrl);+ return {+ baseUrl: destination.baseUrl,+ authorization: toBasicAuthorization(credentials.publicKey, credentials.secretKey),+ ...(scopedHeaders ? { headers: scopedHeaders } : {}),+ };+}++/**+ * The connection prompt reads use. A request from a tenant user, or any request+ * in multi-tenant mode (`usesLangfuseMultiTenantRouting`), uses only the+ * tenant's stored connection: prompt reads return data, so a tenant must never+ * read the central env project, even while `TENANT_ISOLATION_STRICT` is off.+ * Unlike tracing, this does not require `langfuse.enabled` or fanout.+ */+export function resolveLangfusePromptConnection(+ appConfig?: AppConfig,+ options?: { tenantId?: string },+): LangfusePromptConnection | null {+ const headers = resolveLangfuseHeaders(appConfig?.langfuse?.headers);+ if (options?.tenantId || usesLangfuseMultiTenantRouting()) {+ return resolveTenantConnection(appConfig, headers);+ }+ return hasLangfuseEnvCredentials()+ ? resolveCentralEnvConnection(headers)+ : resolveTenantConnection(appConfig, headers);+}++function isTimeout(error: unknown): boolean {+ return error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError');+}++/** Node's fetch returns a connection to its pool only once the body is consumed or cancelled. */+async function release(response: Response): Promise<void> {+ await response.body?.cancel().catch(() => undefined);+}++function statusError(status: number): LangfusePromptRequestError {+ if (status === 401 || status === 403) {+ return new LangfusePromptRequestError(+ 'unauthorized',+ `Langfuse responded with ${status}`,+ status,+ );+ }+ return new LangfusePromptRequestError('upstream', `Langfuse responded with ${status}`, status);+}++async function requestLangfuse(+ conn: LangfusePromptConnection,+ url: string,+ timeoutMs: number,+): Promise<Response> {+ const signal = AbortSignal.timeout(timeoutMs);+ try {+ return await fetch(url, {+ method: 'GET',+ headers: mergeHeaders(conn.headers, { Authorization: conn.authorization }),+ signal,+ ...redirectPolicyFor(conn.headers),+ });+ } catch (error) {+ throw isTimeout(error)+ ? new LangfusePromptRequestError('timeout', 'Langfuse did not respond in time')+ : new LangfusePromptRequestError('upstream', 'Langfuse request failed');+ }+}++/** Reads and parses a response's JSON body. The signal that bounds the+ * request also bounds reading its body, so a parse failure can itself be the+ * deadline firing mid-read rather than a malformed payload. */+async function readLangfuseJsonBody(response: Response): Promise<unknown> {+ try {+ return await response.json();+ } catch (error) {+ throw isTimeout(error)+ ? new LangfusePromptRequestError('timeout', 'Langfuse did not respond in time')+ : new LangfusePromptRequestError('invalid_response', 'Langfuse returned an invalid response');+ }+}++async function requestLangfuseJson(+ conn: LangfusePromptConnection,+ url: string,+ timeoutMs: number,+): Promise<unknown> {+ const response = await requestLangfuse(conn, url, timeoutMs);+ if (!response.ok) {+ await release(response);+ throw statusError(response.status);+ }+ return readLangfuseJsonBody(response);+}++function appendDefined(+ params: URLSearchParams,+ key: string,+ value: string | number | undefined,+): void {+ if (value != null) {+ params.set(key, String(value));+ }+}++/**+ * Lists prompt metadata from Langfuse. Query params are sent as-is, only those+ * defined; `filter` is never sent (deferred to the sync slice), and every+ * prompt type — including `chat` — is returned.+ */+export async function listLangfusePrompts(+ conn: LangfusePromptConnection,+ query: LangfusePromptListQuery,+ { timeoutMs }: { timeoutMs: number },+): Promise<TLangfusePromptListResponse> {+ const params = new URLSearchParams();+ appendDefined(params, 'name', query.name);+ appendDefined(params, 'label', query.label);+ appendDefined(params, 'tag', query.tag);+ appendDefined(params, 'page', query.page);+ appendDefined(params, 'limit', query.limit);+ appendDefined(params, 'fromUpdatedAt', query.fromUpdatedAt);+ appendDefined(params, 'toUpdatedAt', query.toUpdatedAt);++ const search = params.size > 0 ? `?${params.toString()}` : '';+ const url = `${conn.baseUrl.replace(/\/+$/, '')}${PROMPTS_PATH}${search}`;+ const body = await requestLangfuseJson(conn, url, timeoutMs);+ const parsed = promptListResponseSchema.safeParse(body);+ if (!parsed.success) {+ throw new LangfusePromptRequestError(+ 'invalid_response',+ 'Langfuse returned an unexpected prompt list response',+ );+ }+ return { items: parsed.data.data, meta: parsed.data.meta };+}++/**+ * Resolves one text prompt. A chat prompt is rejected as `unsupported_type`+ * and its message content is never parsed or returned.+ */+export async function getLangfuseTextPrompt(+ conn: LangfusePromptConnection,+ name: string,+ selector: LangfuseTextPromptSelector,+ { timeoutMs }: { timeoutMs: number },+): Promise<LangfuseTextPromptResult> {+ const params = new URLSearchParams();+ if ('label' in selector) {+ params.set('label', selector.label);+ } else {+ params.set('version', String(selector.version));+ }+ const url = `${conn.baseUrl.replace(/\/+$/, '')}${PROMPTS_PATH}/${encodeURIComponent(name)}?${params.toString()}`;++ const response = await requestLangfuse(conn, url, timeoutMs);+ if (response.status === 404) {+ await release(response);+ return { ok: false, error: { code: 'not_found' } };+ }+ if (!response.ok) {+ await release(response);+ throw statusError(response.status);+ }+ const body = await readLangfuseJsonBody(response);++ const base = promptBaseSchema.safeParse(body);+ if (!base.success) {+ throw new LangfusePromptRequestError(+ 'invalid_response',+ 'Langfuse returned an unexpected prompt response',+ );+ }+ if (base.data.type !== 'text') {+ return { ok: false, error: { code: 'unsupported_type' } };+ }+ const text = textPromptSchema.safeParse(body);+ if (!text.success) {+ throw new LangfusePromptRequestError(+ 'invalid_response',+ 'Langfuse returned an unexpected prompt response',+ );+ }+ return {+ ok: true,+ value: {+ name: text.data.name,+ version: text.data.version,+ labels: text.data.labels,+ prompt: text.data.prompt,+ },+ };+}diff --git a/packages/data-provider/src/api-endpoints.ts b/packages/data-provider/src/api-endpoints.tsindex 68fa7891fd..bf33ec8ee0 100644--- a/packages/data-provider/src/api-endpoints.ts+++ b/packages/data-provider/src/api-endpoints.ts@@ -1,5 +1,6 @@import type { StartupConfigContext } from './config';import type { AssistantsEndpoint } from './schemas';+import type * as t from './types';import { ResourceType } from './accessPermissions';import * as q from './types/queries';@@ -597,6 +598,11 @@ export const adminLangfuseConnection = () => `${BASE_URL}/api/admin/langfuse/conexport const adminLangfuseConnectionTest = () => `${adminLangfuseConnection()}/test`;export const adminLangfuseSessionLink = (conversationId: string) =>`${adminLangfuseConnection()}/session/${encodeURIComponent(conversationId)}`;+export const adminLangfusePromptSync = () => `${BASE_URL}/api/admin/langfuse/prompt-sync`;+export const adminLangfusePrompts = (params: t.TLangfusePromptListParams = {}) =>+ `${BASE_URL}/api/admin/langfuse/prompts${buildQuery(params)}`;+export const adminLangfusePrompt = (name: string, version?: number) =>+ `${BASE_URL}/api/admin/langfuse/prompts/${encodeURIComponent(name)}${buildQuery({ version })}`;/* Combined Pinned-section display order: favorite and pinned-chat entry keys interleaved. */export const pinnedOrder = () => `${BASE_URL}/api/user/settings/pinned-order`;diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.tsindex 6748eeea4f..da7d74b98b 100644--- a/packages/data-provider/src/config.ts+++ b/packages/data-provider/src/config.ts@@ -3606,6 +3606,7 @@ export type TStartupConfig = {socialLogins?: string[];langfuseFanoutEnabled?: boolean;langfuseConnectionAccess?: boolean;+ langfusePromptSyncAccess?: boolean;insightsEnabled?: boolean;/** Whether pull requests are active (on unless turned off, with a token and a repository scope);* the header does not ask for a pull request without it. */@@ -4259,6 +4260,13 @@ export const langfuseConfigSchema = z.object({headers: z.record(z.string()).optional(),/** Trace user identity and allowlisted user/request metadata. */trace: langfuseTraceConfigSchema.optional(),+ /** Tenant admin switch for Langfuse prompt sync. Effective only when the+ * deployment sets LANGFUSE_PROMPT_SYNC_AVAILABLE. */+ promptSync: z+ .object({+ enabled: z.boolean().optional(),+ })+ .optional(),});export type LangfuseConfig = z.infer<typeof langfuseConfigSchema>;diff --git a/packages/data-provider/src/data-service.ts b/packages/data-provider/src/data-service.tsindex a6e18531d2..629d9fd586 100644--- a/packages/data-provider/src/data-service.ts+++ b/packages/data-provider/src/data-service.ts@@ -96,6 +96,24 @@ export function getLangfuseSessionLink(return request.get(endpoints.adminLangfuseSessionLink(conversationId));}+export function updateLangfusePromptSync(+ payload: t.TUpdateLangfusePromptSyncRequest,+): Promise<t.TLangfuseConnectionStatus> {+ return request.put(endpoints.adminLangfusePromptSync(), payload);+}++export function getLangfusePrompts(+ params: t.TLangfusePromptListParams = {},+): Promise<t.TLangfusePromptListResponse> {+ return request.get(endpoints.adminLangfusePrompts(params));+}++export function getLangfusePrompt(+ params: t.TLangfusePromptGetParams,+): Promise<t.TLangfusePromptGetResponse> {+ return request.get(endpoints.adminLangfusePrompt(params.name, params.version));+}+export function revokeUserKey(name: string): Promise<unknown> {return request.delete(endpoints.revokeUserKey(name));}diff --git a/packages/data-provider/src/keys.ts b/packages/data-provider/src/keys.tsindex c960b0772c..7d2e91f5f1 100644--- a/packages/data-provider/src/keys.ts+++ b/packages/data-provider/src/keys.ts@@ -16,6 +16,8 @@ export enum QueryKeys {searchEnabled = 'searchEnabled',langfuseConnection = 'langfuseConnection',langfuseSessionLink = 'langfuseSessionLink',+ langfusePrompts = 'langfusePrompts',+ langfusePrompt = 'langfusePrompt',conversationTraceAvailability = 'conversationTraceAvailability',conversationTraceRecords = 'conversationTraceRecords',conversationTraceRecord = 'conversationTraceRecord',@@ -147,6 +149,7 @@ export enum MutationKeys {updatePinnedOrder = 'updatePinnedOrder',updateLangfuseConnection = 'updateLangfuseConnection',testLangfuseConnection = 'testLangfuseConnection',+ updateLangfusePromptSync = 'updateLangfusePromptSync',createAgentApiKey = 'createAgentApiKey',deleteAgentApiKey = 'deleteAgentApiKey',fileUpload = 'fileUpload',diff --git a/packages/data-provider/src/types.ts b/packages/data-provider/src/types.tsindex eef6a008cb..22e9444c98 100644--- a/packages/data-provider/src/types.ts+++ b/packages/data-provider/src/types.ts@@ -1321,6 +1321,8 @@ export type TUpdateSkillNodeRequest = {};export type TLangfuseConnectionStatus = {+ /** Whether connection editing and testing are available. Omitted by older servers. */+ connectionAvailable?: boolean;configured: boolean;enabled: boolean;destinations: TLangfuseDestinationOption[];@@ -1328,6 +1330,13 @@ export type TLangfuseConnectionStatus = {publicKey?: string;secretKeyPreview?: string;updatedAt?: string;+ promptSync: TLangfusePromptSyncStatus;+};++/** Whether DevOps made Langfuse prompt sync available, and whether the tenant admin turned it on. */+export type TLangfusePromptSyncStatus = {+ available: boolean;+ enabled: boolean;};/**@@ -1391,3 +1400,63 @@ export type TLangfuseSessionLinkResponse = {/** Opaque identity of the project `url` opens, so a caller can tell whether it holds what it showed. */destinationId?: string;};++export type TUpdateLangfusePromptSyncRequest = {+ enabled: boolean;+};++export type TLangfusePromptListParams = {+ name?: string;+ label?: string;+ tag?: string;+ page?: number;+ limit?: number;+ fromUpdatedAt?: string;+ toUpdatedAt?: string;+};++export type TLangfusePromptListItem = {+ name: string;+ type: 'text' | 'chat';+ versions: number[];+ labels: string[];+ tags: string[];+ lastUpdatedAt: string;+};++export type TLangfusePromptListResponse = {+ items: TLangfusePromptListItem[];+ meta: {+ page: number;+ limit: number;+ totalItems: number;+ totalPages: number;+ };+};++export type TLangfusePromptGetParams = {+ name: string;+ version?: number;+};++export type TLangfusePromptGetResponse = {+ name: string;+ version: number;+ labels: string[];+ prompt: string;+};++export type TLangfusePromptErrorCode =+ | 'not_configured'+ | 'unauthorized'+ | 'timeout'+ | 'upstream'+ | 'invalid_response'+ | 'not_found'+ | 'unsupported_type'+ | 'invalid_request';++export type TLangfusePromptErrorBody = {+ code: TLangfusePromptErrorCode;+ status?: number;+};