@@ -125,6 +125,8 @@ app.get('/config/project-overrides', (req, res) => {
125125 name : 'evaluationAnalyticsServerUrl' ,
126126 value : process . env . EVALUATION_ANALYTICS_SERVER_URL ,
127127 } ,
128+ { name : 'gramProjectSlug' , value : process . env . GRAM_PROJECT_SLUG } ,
129+ { name : 'gramMcpUrl' , value : process . env . GRAM_MCP_URL } ,
128130 ]
129131 let output = values . map ( getVariable ) . join ( '' )
130132 res . setHeader ( 'Cache-Control' , 's-max-age=1, stale-while-revalidate' )
@@ -234,6 +236,47 @@ if (process.env.FLAGSMITH_PROXY_API_URL) {
234236}
235237
236238app . use ( bodyParser . json ( ) )
239+
240+ // Gram Elements chat session endpoint
241+ if ( process . env . GRAM_API_KEY ) {
242+ const { createElementsServerHandlers } = require ( '@gram-ai/elements/server' )
243+ const gramHandlers = createElementsServerHandlers ( )
244+
245+ app . post ( '/api/gram/session' , async ( req , res ) => {
246+ const token = req . headers . authorization
247+ if ( ! token ) {
248+ return res . status ( 401 ) . json ( { error : 'Authentication required' } )
249+ }
250+
251+ const apiUrl = process . env . FLAGSMITH_PROXY_API_URL
252+ ? process . env . FLAGSMITH_PROXY_API_URL . replace ( / \/ ? $ / , '/' )
253+ : process . env . FLAGSMITH_API_URL || 'https://api.flagsmith.com/'
254+
255+ try {
256+ const userResponse = await fetch ( `${ apiUrl } api/v1/auth/users/me/` , {
257+ headers : { Authorization : token } ,
258+ } )
259+ if ( ! userResponse . ok ) {
260+ return res . status ( 401 ) . json ( { error : 'Invalid authentication token' } )
261+ }
262+ const user = await userResponse . json ( )
263+
264+ return gramHandlers . session ( req , res , {
265+ embedOrigin :
266+ req . headers . origin ||
267+ ( req . headers . referer && req . headers . referer . replace ( / \/ $ / , '' ) ) ||
268+ '*' ,
269+ userIdentifier : String ( user . id ) ,
270+ expiresAfter : 3600 ,
271+ } )
272+ } catch ( err ) {
273+ // eslint-disable-next-line
274+ console . error ( 'Gram session error:' , err )
275+ return res . status ( 500 ) . json ( { error : 'Failed to create chat session' } )
276+ }
277+ } )
278+ }
279+
237280app . use ( spm )
238281const genericWebsite = ( url ) => {
239282 if ( ! url ) return true
0 commit comments