UNPKG

gcloud-sonar-ai

Version:

A production-ready NPM package that provides a Perplexity Sonar alternative using Google Cloud Vertex AI and Gemini models.

96 lines (93 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SonarUtils = void 0; class SonarUtils { static extractSources(metadata, maxResults = 10) { if (!metadata) return []; const sources = []; const seenUrls = new Set(); if (metadata.searchEntryPoint) { try { const content = metadata.searchEntryPoint.renderedContent || ''; const urlRegex = /https?:\/\/[^\s<>"{}|\\^`[\]]+/g; const urls = content.match(urlRegex) || []; urls.forEach((url) => { if (!seenUrls.has(url) && sources.length < maxResults) { sources.push({ url, title: `Source from entry point ${sources.length + 1}`, snippet: 'Retrieved from grounding metadata.', domain: new URL(url).hostname }); seenUrls.add(url); } }); } catch (e) { // Ignore errors from URL parsing } } const attributions = metadata.groundingAttributions; if (attributions) { for (const attribution of attributions) { const source = attribution.web || attribution.retrieval; if (source?.uri && !seenUrls.has(source.uri) && sources.length < maxResults) { sources.push({ url: source.uri, title: source.title || `Source ${sources.length + 1}`, snippet: 'Retrieved from grounding attributions.', domain: new URL(source.uri).hostname, }); seenUrls.add(source.uri); } } } return this.sortSources(sources); } static sortSources(sources) { return sources.sort((a, b) => (b.relevanceScore || 0) - (a.relevanceScore || 0)); } static calculateTokenUsage(response) { try { const usage = response.usageMetadata || {}; const input = usage.promptTokenCount || 0; const output = usage.candidatesTokenCount || 0; const thinking = usage.thinkingTokenCount; return { input, output, thinking, total: input + output + (thinking || 0) }; } catch { return { input: 0, output: 0, total: 0 }; } } static buildEnhancedPrompt(query, customInstructions) { const basePrompt = `You are an expert AI assistant with access to real-time information through web search. Provide comprehensive, accurate, and well-cited responses to user queries. Guidelines: - Include relevant sources and citations - Provide detailed, factual information - Structure responses clearly with appropriate formatting - Be comprehensive but concise - If information is time-sensitive, mention current context ${customInstructions ? `Additional Instructions: ${customInstructions}` : ''} User Query: ${query}`; return basePrompt; } static formatTimestamp() { return new Date().toISOString(); } static sanitizeConfig(config) { const sanitized = { ...config }; if (sanitized.apiKey) { sanitized.apiKey = '***REDACTED***'; } return sanitized; } } exports.SonarUtils = SonarUtils; //# sourceMappingURL=utils.js.map