From a8afbd323a8065e53b3bd743899cd5a5b54c4c6c Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Fri, 13 Jun 2025 11:15:28 -0700 Subject: [PATCH] fix: enhance error logging and validation in app --- src/agent.ts | 2 +- src/app.ts | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index f386b39..45bd191 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -824,7 +824,7 @@ But then you realized you have asked them before. You decided to to think out of } as AnswerAction; candidateAnswers = subproblemResponses.map(r => (r.result as AnswerAction).mdAnswer).filter(a => a) as string[]; // dedup references by their urls - const uniqueURLs = new Set(thisStep.references.map(r => r.url)); + const uniqueURLs = new Set(thisStep.references.filter(r => r?.url).map(r => r.url)); thisStep.references = Array.from(uniqueURLs).map(url => (thisStep as AnswerAction).references.find(r => r?.url === url)) as Reference[]; // aggregate urls diff --git a/src/app.ts b/src/app.ts index 2cdbfeb..0923458 100644 --- a/src/app.ts +++ b/src/app.ts @@ -314,8 +314,8 @@ if (secret) { app.use((req, res, next) => { const authHeader = req.headers.authorization; if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.split(' ')[1] !== secret) { - logError('[chat/completions] Unauthorized request'); - res.status(401).json({ error: 'Unauthorized' }); + logError('Unauthorized request'); + res.status(401).json({ error: 'Unauthorized. Please provide a valid Jina API key.' }); return; } @@ -385,15 +385,15 @@ const validationRules = [ app.post('/v1/chat/completions', validationRules, (async (req: Request, res: Response) => { const clientIp = req.headers['cf-connecting-ip'] || - req.headers['x-forwarded-for'] || - req.ip || - req.socket.remoteAddress || - 'unknown'; + req.headers['x-forwarded-for'] || + req.ip || + req.socket.remoteAddress || + 'unknown'; // Validate request body const errors = validationResult(req); if (!errors.isEmpty()) { - logError('[chat/completions] Validation errors:', { errors: errors.array(), ip: clientIp }); + logError('Validation errors:', { errors: errors.array(), ip: clientIp }); return res.status(400).json({ error: 'Invalid request body', details: errors.array() }); } @@ -401,14 +401,14 @@ app.post('/v1/chat/completions', validationRules, (async (req: Request, res: Res if (secret) { const authHeader = req.headers.authorization; if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.split(' ')[1] !== secret) { - logError('[chat/completions] Unauthorized request'); - res.status(401).json({ error: 'Unauthorized' }); + logError('Unauthorized request'); + res.status(401).json({ error: 'Unauthorized. Please provide a valid Jina API key.' }); return; } } // Log request details (excluding sensitive data) - logInfo('[chat/completions] Request:', { + logInfo('[chat/completions] Start:', { model: req.body.model, stream: req.body.stream, messageCount: req.body.messages?.length, @@ -466,12 +466,12 @@ app.post('/v1/chat/completions', validationRules, (async (req: Request, res: Res }); if (!body.messages?.length) { - logError('[chat/completions] Messages array is required and must not be empty', { messages: body.messages }); + logError('Messages array is required and must not be empty', { messages: body.messages }); return res.status(400).json({ error: 'Messages array is required and must not be empty' }); } const lastMessage = body.messages[body.messages.length - 1]; if (lastMessage.role !== 'user') { - logError('[chat/completions] Last message must be from user', { messages: body.messages }); + logError('Last message must be from user', { messages: body.messages }); return res.status(400).json({ error: 'Last message must be from user' }); }