cli
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
node_modules
|
node_modules
|
||||||
.env
|
.env
|
||||||
|
logs
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"serve": "node restSearch.js",
|
"serve": "node restSearch.js",
|
||||||
|
"search": "node searchCLI.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
80
searchCLI.js
Normal file
80
searchCLI.js
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import dotenv from 'dotenv';
|
||||||
|
import { createClients } from './src/clients.js';
|
||||||
|
import { getConfig, validateConfig } from './src/config/env.js';
|
||||||
|
import { createSearchService } from './src/services/searchService.js';
|
||||||
|
|
||||||
|
// Load environment variables from .env file
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
function printUsage() {
|
||||||
|
console.log('Usage: node searchCLI.js <question>');
|
||||||
|
console.log('');
|
||||||
|
console.log('Example:');
|
||||||
|
console.log(' node searchCLI.js "What are the latest developments in AI?"');
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSimpleBroadcast() {
|
||||||
|
return (message, type = 'info', data = null) => {
|
||||||
|
const timestamp = new Date().toLocaleTimeString();
|
||||||
|
const prefix = type === 'error' ? '❌' : type === 'warning' ? '⚠️' : type === 'success' ? '✅' : 'ℹ️';
|
||||||
|
console.log(`[${timestamp}] ${prefix} ${message}`);
|
||||||
|
if (data) {
|
||||||
|
console.log(` Data: ${JSON.stringify(data, null, 2)}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runCLI() {
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
|
||||||
|
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
|
||||||
|
printUsage();
|
||||||
|
process.exit(args.includes('--help') || args.includes('-h') ? 0 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const question = args.join(' ');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const config = getConfig();
|
||||||
|
validateConfig(config);
|
||||||
|
|
||||||
|
const broadcast = createSimpleBroadcast();
|
||||||
|
const clients = createClients(config);
|
||||||
|
const searchService = createSearchService({
|
||||||
|
...clients,
|
||||||
|
broadcast,
|
||||||
|
});
|
||||||
|
|
||||||
|
broadcast(`Starting search for: "${question}"`, 'info');
|
||||||
|
|
||||||
|
const result = await searchService.search(question);
|
||||||
|
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
console.log('');
|
||||||
|
console.log('═══════════════════════════════════════════════════════════');
|
||||||
|
console.log('FINAL ANSWER:');
|
||||||
|
console.log('═══════════════════════════════════════════════════════════');
|
||||||
|
console.log(result.fullAnswerHTMLSnippet || result.answer || 'No answer generated');
|
||||||
|
console.log('');
|
||||||
|
|
||||||
|
if (result.mostRelevantSources && result.mostRelevantSources.length > 0) {
|
||||||
|
console.log('SOURCES:');
|
||||||
|
result.mostRelevantSources.forEach((source, index) => {
|
||||||
|
console.log(` ${index + 1}. ${source}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
console.log('═══════════════════════════════════════════════════════════');
|
||||||
|
|
||||||
|
process.exit(0);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('');
|
||||||
|
console.error('❌ Error:', error.message);
|
||||||
|
if (error.details) {
|
||||||
|
console.error(' Details:', error.details);
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runCLI();
|
||||||
@@ -10,6 +10,7 @@ export function getConfig() {
|
|||||||
host: process.env.HOST || '0.0.0.0',
|
host: process.env.HOST || '0.0.0.0',
|
||||||
exaApiKey: process.env.EXA_API_KEY,
|
exaApiKey: process.env.EXA_API_KEY,
|
||||||
openRouterApiKey: process.env.OPENROUTER_API_KEY,
|
openRouterApiKey: process.env.OPENROUTER_API_KEY,
|
||||||
|
maintenanceMode: process.env.MAINTENANCE_MODE === 'true',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
55
src/maintenanceApp.js
Normal file
55
src/maintenanceApp.js
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import express from 'express';
|
||||||
|
|
||||||
|
export function createMaintenanceApp() {
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.get('/{*path}', (req, res) => {
|
||||||
|
res.status(503).send(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Under Construction</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
text-align: center;
|
||||||
|
padding: 2rem;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
font-size: 3rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
font-size: 1.2rem;
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
.icon {
|
||||||
|
font-size: 4rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="icon">🚧</div>
|
||||||
|
<h1>Under Construction</h1>
|
||||||
|
<p>Please return later.</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
@@ -1,37 +1,50 @@
|
|||||||
import { createApp } from './app.js';
|
import { createApp } from './app.js';
|
||||||
|
import { createMaintenanceApp } from './maintenanceApp.js';
|
||||||
import { createClients } from './clients.js';
|
import { createClients } from './clients.js';
|
||||||
import { getConfig, validateConfig } from './config/env.js';
|
import { getConfig, validateConfig } from './config/env.js';
|
||||||
import { createSseHub } from './lib/sseHub.js';
|
import { createSseHub } from './lib/sseHub.js';
|
||||||
import { createSearchService } from './services/searchService.js';
|
import { createSearchService } from './services/searchService.js';
|
||||||
|
|
||||||
function logStartup(host, port) {
|
function logStartup(host, port, isMaintenance) {
|
||||||
console.log(`REST Search Service running at http://${host}:${port}`);
|
if (isMaintenance) {
|
||||||
console.log(`Web UI: http://localhost:${port}`);
|
console.log(`🚧 Maintenance Mode running at http://${host}:${port}`);
|
||||||
console.log('API Documentation:');
|
console.log('All requests will receive "Under Construction" page');
|
||||||
console.log(' POST /search - Search for a query and return summarized results');
|
} else {
|
||||||
console.log(' GET /health - Health check endpoint');
|
console.log(`REST Search Service running at http://${host}:${port}`);
|
||||||
console.log(' GET /stream - SSE endpoint for streaming log messages');
|
console.log(`Web UI: http://localhost:${port}`);
|
||||||
console.log('\nExample:');
|
console.log('API Documentation:');
|
||||||
console.log(` curl -X POST http://${host}:${port}/search \\`);
|
console.log(' POST /search - Search for a query and return summarized results');
|
||||||
console.log(' -H "Content-Type: application/json" \\');
|
console.log(' GET /health - Health check endpoint');
|
||||||
console.log(' -d \'{"question": "What are the latest developments in AI?"}\'');
|
console.log(' GET /stream - SSE endpoint for streaming log messages');
|
||||||
console.log('\nSSE Log Stream:');
|
console.log('\nExample:');
|
||||||
console.log(` Open http://localhost:${port}/stream in a browser or use EventSource in JavaScript`);
|
console.log(` curl -X POST http://${host}:${port}/search \\`);
|
||||||
|
console.log(' -H "Content-Type: application/json" \\');
|
||||||
|
console.log(' -d \'{"question": "What are the latest developments in AI?"}\'');
|
||||||
|
console.log('\nSSE Log Stream:');
|
||||||
|
console.log(` Open http://localhost:${port}/stream in a browser or use EventSource in JavaScript`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startServer() {
|
export function startServer() {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
validateConfig(config);
|
validateConfig(config);
|
||||||
|
|
||||||
const clients = createClients(config);
|
const isMaintenance = config.maintenanceMode;
|
||||||
const sseHub = createSseHub();
|
|
||||||
const searchService = createSearchService({
|
let app;
|
||||||
...clients,
|
if (isMaintenance) {
|
||||||
broadcast: sseHub.broadcast,
|
app = createMaintenanceApp();
|
||||||
});
|
} else {
|
||||||
const app = createApp({ searchService, sseHub });
|
const clients = createClients(config);
|
||||||
|
const sseHub = createSseHub();
|
||||||
|
const searchService = createSearchService({
|
||||||
|
...clients,
|
||||||
|
broadcast: sseHub.broadcast,
|
||||||
|
});
|
||||||
|
app = createApp({ searchService, sseHub });
|
||||||
|
}
|
||||||
|
|
||||||
app.listen(config.port, config.host, () => {
|
app.listen(config.port, config.host, () => {
|
||||||
logStartup(config.host, config.port);
|
logStartup(config.host, config.port, isMaintenance);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ const __filename = fileURLToPath(import.meta.url);
|
|||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
const LOG_DIR = path.join(__dirname, '../../logs');
|
const LOG_DIR = path.join(__dirname, '../../logs');
|
||||||
const LOG_FILE = path.join(LOG_DIR, 'openrouter.log');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures the log directory exists
|
* Ensures the log directory exists
|
||||||
@@ -27,14 +26,6 @@ function formatTimestamp() {
|
|||||||
return new Date().toISOString();
|
return new Date().toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Safely truncates a string
|
|
||||||
*/
|
|
||||||
function truncate(str, maxLength = 1000) {
|
|
||||||
if (!str) return '';
|
|
||||||
return str.length > maxLength ? str.substring(0, maxLength) + '...' : str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs an OpenRouter API call with params and response
|
* Logs an OpenRouter API call with params and response
|
||||||
* @param {string} operation - The operation name (e.g., 'summarizeSources', 'summarizeFinalAnswer')
|
* @param {string} operation - The operation name (e.g., 'summarizeSources', 'summarizeFinalAnswer')
|
||||||
@@ -47,47 +38,12 @@ export async function logOpenRouterCall(operation, params, response, error = nul
|
|||||||
await ensureLogDirectory();
|
await ensureLogDirectory();
|
||||||
|
|
||||||
const timestamp = formatTimestamp();
|
const timestamp = formatTimestamp();
|
||||||
|
const logFileName = `openrouter-${timestamp.replace(/[:.]/g, '-')}.log`;
|
||||||
// Sanitize and truncate params for logging
|
const logFilePath = path.join(LOG_DIR, logFileName);
|
||||||
const sanitizedParams = {
|
|
||||||
model: params.model,
|
|
||||||
messages: params.messages?.map((msg) => ({
|
|
||||||
role: msg.role,
|
|
||||||
contentPreview: truncate(msg.content, 500),
|
|
||||||
})),
|
|
||||||
reasoning: params.reasoning,
|
|
||||||
responseFormat: params.responseFormat?.jsonSchema?.name,
|
|
||||||
stream: params.stream,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Sanitize response
|
const logContent = `${timestamp} | ${operation}\n\nParams:\n${JSON.stringify(params, null, 2)}\n\nResponse:\n${JSON.stringify(response, null, 2)}${error ? `\n\nError: ${error.message}` : ''}\n`;
|
||||||
const sanitizedResponse = response
|
|
||||||
? {
|
|
||||||
choices: response.choices?.map((choice) => ({
|
|
||||||
messagePreview: truncate(choice.message?.content, 500),
|
|
||||||
finishReason: choice.finish_reason,
|
|
||||||
})),
|
|
||||||
model: response.model,
|
|
||||||
usage: response.usage,
|
|
||||||
}
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const logEntry = {
|
await fs.writeFile(logFilePath, logContent, 'utf8');
|
||||||
timestamp,
|
|
||||||
operation,
|
|
||||||
params: sanitizedParams,
|
|
||||||
response: sanitizedResponse,
|
|
||||||
error: error
|
|
||||||
? {
|
|
||||||
name: error.name,
|
|
||||||
message: error.message,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const logLine = `${timestamp} | ${operation} | Params: ${JSON.stringify(logEntry.params)} | Response: ${JSON.stringify(logEntry.response)}${error ? ` | Error: ${error.message}` : ''}\n`;
|
|
||||||
|
|
||||||
await fs.appendFile(LOG_FILE, logLine, 'utf8');
|
|
||||||
} catch (logError) {
|
} catch (logError) {
|
||||||
console.error('Failed to log OpenRouter call:', logError);
|
console.error('Failed to log OpenRouter call:', logError);
|
||||||
// Don't throw - logging should not break the main functionality
|
// Don't throw - logging should not break the main functionality
|
||||||
@@ -95,8 +51,8 @@ export async function logOpenRouterCall(operation, params, response, error = nul
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the path to the OpenRouter log file
|
* Gets the path to the OpenRouter log directory
|
||||||
*/
|
*/
|
||||||
export function getOpenRouterLogFilePath() {
|
export function getOpenRouterLogDirPath() {
|
||||||
return LOG_FILE;
|
return LOG_DIR;
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { logOpenRouterCall } from './openRouterLogger.js';
|
import { logOpenRouterCall } from './openRouterLogger.js';
|
||||||
|
|
||||||
function parseResponse(response) {
|
function parseResponse(response) {
|
||||||
return JSON.parse(response.choices[0].message.content);
|
return { cost: response.usage.cost, data: JSON.parse(response.choices[0].message.content) };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function summarizeSources({ openrouter, text, question }) {
|
export async function summarizeSources({ openrouter, text, question }) {
|
||||||
@@ -18,9 +18,9 @@ export async function summarizeSources({ openrouter, text, question }) {
|
|||||||
{ role: 'user', content: text },
|
{ role: 'user', content: text },
|
||||||
],
|
],
|
||||||
reasoning: { effort: 'low' },
|
reasoning: { effort: 'low' },
|
||||||
responseFormat: {
|
response_format: {
|
||||||
type: 'json_schema',
|
type: 'json_schema',
|
||||||
jsonSchema: {
|
json_schema: {
|
||||||
name: 'search_summaries',
|
name: 'search_summaries',
|
||||||
strict: true,
|
strict: true,
|
||||||
schema: {
|
schema: {
|
||||||
@@ -47,7 +47,17 @@ export async function summarizeSources({ openrouter, text, question }) {
|
|||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await openrouter.chat.send({ chatRequest: params });
|
// Using direct fetch API instead of OpenRouter SDK
|
||||||
|
const apiKey = process.env.OPENROUTER_API_KEY;
|
||||||
|
const fetchResponse = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(params),
|
||||||
|
});
|
||||||
|
const response = await fetchResponse.json();
|
||||||
await logOpenRouterCall('summarizeSources', params, response);
|
await logOpenRouterCall('summarizeSources', params, response);
|
||||||
return parseResponse(response);
|
return parseResponse(response);
|
||||||
}
|
}
|
||||||
@@ -67,9 +77,9 @@ export async function summarizeFinalAnswer({ openrouter, text, question }) {
|
|||||||
{ role: 'user', content: text },
|
{ role: 'user', content: text },
|
||||||
],
|
],
|
||||||
reasoning: { effort: 'none' },
|
reasoning: { effort: 'none' },
|
||||||
responseFormat: {
|
response_format: {
|
||||||
type: 'json_schema',
|
type: 'json_schema',
|
||||||
jsonSchema: {
|
json_schema: {
|
||||||
name: 'response',
|
name: 'response',
|
||||||
strict: true,
|
strict: true,
|
||||||
schema: {
|
schema: {
|
||||||
@@ -88,7 +98,19 @@ export async function summarizeFinalAnswer({ openrouter, text, question }) {
|
|||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await openrouter.chat.send({ chatRequest: params });
|
// Using direct fetch API instead of OpenRouter SDK
|
||||||
|
const apiKey = process.env.OPENROUTER_API_KEY;
|
||||||
|
const fetchResponse = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(params),
|
||||||
|
});
|
||||||
|
const response = await fetchResponse.json();
|
||||||
|
|
||||||
await logOpenRouterCall('summarizeFinalAnswer', params, response);
|
await logOpenRouterCall('summarizeFinalAnswer', params, response);
|
||||||
|
|
||||||
return parseResponse(response);
|
return parseResponse(response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ export function createSearchService({ exa, openrouter, broadcast }) {
|
|||||||
broadcast(`✅ Found ${result.results.length} results`, 'success', {
|
broadcast(`✅ Found ${result.results.length} results`, 'success', {
|
||||||
count: result.results.length,
|
count: result.results.length,
|
||||||
});
|
});
|
||||||
|
let cost = result.costDollars.total;
|
||||||
|
|
||||||
const extractedContent = extractContent(result);
|
const extractedContent = extractContent(result);
|
||||||
|
|
||||||
@@ -95,12 +96,15 @@ export function createSearchService({ exa, openrouter, broadcast }) {
|
|||||||
broadcast('📝 Generating summary with OpenRouter...', 'info');
|
broadcast('📝 Generating summary with OpenRouter...', 'info');
|
||||||
|
|
||||||
let summary;
|
let summary;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
summary = await summarizeSources({
|
const summaryResult = await summarizeSources({
|
||||||
openrouter,
|
openrouter,
|
||||||
text: extractedContent,
|
text: extractedContent,
|
||||||
question,
|
question,
|
||||||
});
|
});
|
||||||
|
summary = summaryResult.data;
|
||||||
|
cost += summaryResult.cost;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new SearchServiceError('Failed to generate summary', 500, error);
|
throw new SearchServiceError('Failed to generate summary', 500, error);
|
||||||
}
|
}
|
||||||
@@ -128,11 +132,13 @@ export function createSearchService({ exa, openrouter, broadcast }) {
|
|||||||
|
|
||||||
let finalSummary;
|
let finalSummary;
|
||||||
try {
|
try {
|
||||||
finalSummary = await summarizeFinalAnswer({
|
const finalSummaryResult = await summarizeFinalAnswer({
|
||||||
openrouter,
|
openrouter,
|
||||||
text: formatSummarySources(summary.sources),
|
text: formatSummarySources(summary.sources),
|
||||||
question,
|
question,
|
||||||
});
|
});
|
||||||
|
finalSummary = finalSummaryResult.data;
|
||||||
|
cost += finalSummaryResult.cost;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new SearchServiceError('Failed to generate final summary', 500, error);
|
throw new SearchServiceError('Failed to generate final summary', 500, error);
|
||||||
}
|
}
|
||||||
@@ -145,6 +151,7 @@ export function createSearchService({ exa, openrouter, broadcast }) {
|
|||||||
answerLength: finalSummary.fullAnswerHTMLSnippet?.length || 0,
|
answerLength: finalSummary.fullAnswerHTMLSnippet?.length || 0,
|
||||||
sourcesCount: finalSummary.mostRelevantSources?.length || 0,
|
sourcesCount: finalSummary.mostRelevantSources?.length || 0,
|
||||||
});
|
});
|
||||||
|
console.log(`Total cost of API calls: $${cost.toFixed(6)}`);
|
||||||
|
|
||||||
return finalSummary;
|
return finalSummary;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user