@@ -5,12 +5,14 @@ import fs from "fs";
55import path from "path" ;
66import redis from "../helpers/redis" ;
77import {
8+ get_llm_usage_by_uuid ,
89 get_user_llm_usage ,
910 init_user_llm_usage ,
1011 get_llm_model_config ,
1112 log_access_key_usage ,
1213 check_access_key_usage ,
1314} from "../hasura/llm" ;
15+ import authenticate from "../middlewares/authenticate" ;
1416
1517const router = express . Router ( ) ;
1618
@@ -218,6 +220,75 @@ router.post("/verify", async (req, res) => {
218220 }
219221} ) ;
220222
223+ router . post ( "/verify_new" , authenticate ( ) , async ( req , res ) => {
224+ try {
225+ const uuid : string | undefined = req . auth . user . uuid ;
226+ const email : string | undefined = req . auth . user . email ;
227+ const role : string | undefined = req . auth . user . role ;
228+
229+ if ( ! uuid ) {
230+ return res . status ( 400 ) . json ( { error : "Missing uuid in auth user" } ) ;
231+ }
232+
233+ // Invalidate old sessions for this uuid
234+ const now = Math . floor ( Date . now ( ) / 1000 ) ;
235+ await redis . set ( `llm_min_iat:${ uuid } ` , now ) ;
236+
237+ // Check llm_usage row by authenticated uuid.
238+ const dbUsage = await get_llm_usage_by_uuid ( uuid ) ;
239+ if ( ! dbUsage ) {
240+ return res . status ( 401 ) . json ( {
241+ error : "No llm_usage record found for authenticated uuid" ,
242+ } ) ;
243+ }
244+
245+ // Sync limit to Redis by uuid.
246+ const dbLimit = dbUsage . token_limit || 0 ;
247+ if ( dbLimit > 0 ) {
248+ await redis . set ( `llm_limit:${ uuid } ` , dbLimit ) ;
249+ } else {
250+ await redis . del ( `llm_limit:${ uuid } ` ) ;
251+ }
252+
253+ // Sync usage from DB to Redis if missing (e.g. Redis restart)
254+ const currentUsage = await redis . get ( `llm_usage:${ uuid } ` ) ;
255+ if ( ! currentUsage ) {
256+ await redis . set ( `llm_usage:${ uuid } ` , dbUsage . total_tokens_used || 0 ) ;
257+ }
258+
259+ // Issue LLM session token using authenticated identity
260+ const sessionToken = jwt . sign (
261+ {
262+ sub : uuid ,
263+ email : email ,
264+ role : role ,
265+ type : "llm_session" ,
266+ } ,
267+ JWT_SECRET ,
268+ { expiresIn : SESSION_EXPIRY } ,
269+ ) ;
270+
271+ return res . json ( {
272+ token : sessionToken ,
273+ user : {
274+ uuid,
275+ email,
276+ role,
277+ } ,
278+ quota : {
279+ tokenLimit : dbLimit ,
280+ totalTokensUsed : dbUsage . total_tokens_used || 0 ,
281+ } ,
282+ } ) ;
283+ } catch ( err : any ) {
284+ console . error ( "verify_new failed:" , err ) ;
285+ return res . status ( 500 ) . json ( {
286+ error : "Failed to verify authenticated user for LLM" ,
287+ details : err ?. message ,
288+ } ) ;
289+ }
290+ } ) ;
291+
221292// 2. Chat Endpoint
222293router . post ( "/chat" , verifySession , async ( req , res ) => {
223294 const { messages, model } = req . body ;
@@ -343,7 +414,8 @@ router.post("/chat", verifySession, async (req, res) => {
343414 if ( enableThinking ) {
344415 requestOptions . enable_thinking = true ;
345416 }
346-
417+ // console.log("LLM Request Options:", requestOptions);
418+ // console.log(`Base URL: ${baseURL}`);
347419 const stream = ( await client . chat . completions . create ( requestOptions , {
348420 signal : controller . signal ,
349421 } ) ) as any ;
0 commit comments