From 598f94793addff649a880aa5eb954fdab94ecb41 Mon Sep 17 00:00:00 2001 From: Lembot Date: Fri, 9 Jan 2026 20:48:00 -0500 Subject: [PATCH] feat: add environment-based local auth mode for ai-delegate Add `isLocalAuthEnabled()` utility function that controls the `allowLocalMode` option for ai-delegate initialization: - Explicit override via NEXT_PUBLIC_AI_DELEGATE_LOCAL_AUTH env var - Defaults to development mode (NODE_ENV === 'development') - Adds console logging showing local auth mode status This allows local auth mode to be disabled in development or enabled in production as needed via environment configuration. Closes #136 Co-Authored-By: Claude Opus 4.5 --- ratings-ui-demo/src/services/ai-service.ts | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ratings-ui-demo/src/services/ai-service.ts b/ratings-ui-demo/src/services/ai-service.ts index 021b71d..92c7722 100644 --- a/ratings-ui-demo/src/services/ai-service.ts +++ b/ratings-ui-demo/src/services/ai-service.ts @@ -19,12 +19,40 @@ import { let aiDelegateInitialized = false; let promptsRegistered = false; +/** + * Check if local auth mode should be enabled for ai-delegate. + * Local auth mode allows direct API calls without going through the server proxy. + * + * Priority: + * 1. NEXT_PUBLIC_AI_DELEGATE_LOCAL_AUTH env var (explicit override) + * 2. Development mode (NODE_ENV === 'development') + */ +function isLocalAuthEnabled(): boolean { + const envOverride = process.env.NEXT_PUBLIC_AI_DELEGATE_LOCAL_AUTH; + + // If explicit env var is set, use that + if (envOverride !== undefined && envOverride !== '') { + return envOverride === 'true' || envOverride === '1'; + } + + // Default to development mode + return process.env.NODE_ENV === 'development'; +} + /** * Ensure AiDelegate is initialized and prompts are registered */ async function ensureAiDelegateInitialized(): Promise { if (!aiDelegateInitialized) { - await AiDelegate.init({ endpoint: '/ai-delegate' }); + const allowLocalMode = isLocalAuthEnabled(); + + console.log( + `[ai-service] Initializing ai-delegate: allowLocalMode=${allowLocalMode}`, + `(NODE_ENV=${process.env.NODE_ENV},`, + `NEXT_PUBLIC_AI_DELEGATE_LOCAL_AUTH=${process.env.NEXT_PUBLIC_AI_DELEGATE_LOCAL_AUTH ?? 'not set'})` + ); + + await AiDelegate.init({ endpoint: '/ai-delegate', allowLocalMode }); aiDelegateInitialized = true; } -- GitLab