Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(sentiment): classify brand sentiment with a decision model through OpenRouter (#1004)
Gökhan Geyik9b3e2b62896e
diff --git a/server/.env.example b/server/.env.exampleindex 6509a123e..9f4f07b06 100644--- a/server/.env.example+++ b/server/.env.example@@ -26,9 +26,15 @@ GOOGLE_GENERATIVE_AI_API_KEY=# OPENROUTER_SITE_URL / OPENROUTER_SITE_NAME are sent as OpenRouter's optional# attribution headers; defaults point at ansvisor.com.-# Brand sentiment model (provider/model). Any configured provider works.-# Defaults to openai/gpt-5-mini — the previous hard-coded value. Set it to an-# anthropic/, google/ or openrouter/ model to drop the OpenAI requirement.+# Brand sentiment. With OPENROUTER_API_KEY set and SENTIMENT_MODEL unset, a+# decision model answers it through OpenRouter's Decisions API: it reads the+# whole answer and returns real probabilities, at a fraction of a chat model's+# cost. It falls back to openai/gpt-5-mini if the decision model fails.+# DECIDER_MODEL=perplexity/pplx-decider-v1.1-27b+#+# Setting SENTIMENT_MODEL (provider/model) picks a chat model instead. Any+# configured provider works; without OpenRouter it defaults to+# openai/gpt-5-mini — the previous hard-coded value.# SENTIMENT_MODEL=anthropic/claude-haiku-4-5DEFAULT_SUGGESTION_MODEL=google/gemini-3-flash-previewdiff --git a/server/src/lib/cloro-result-handler.js b/server/src/lib/cloro-result-handler.jsindex 8d8656393..b1da359d4 100644--- a/server/src/lib/cloro-result-handler.js+++ b/server/src/lib/cloro-result-handler.js@@ -45,7 +45,7 @@ export async function handleScraperResult({const mentionCount = countBrandMentions(aiResponse.text, brandInfo);const sentimentResult =mentionCount > 0- ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName)+ ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName, brandInfo.domains): { sentiment: 'neutral', confidence: 0, reason: 'Brand not mentioned' };const metrics = parseResponse(aiResponse, brandInfo, sentimentResult.sentiment, competitors);diff --git a/server/src/lib/decider.js b/server/src/lib/decider.jsnew file mode 100644index 000000000..799181c3e--- /dev/null+++ b/server/src/lib/decider.js@@ -0,0 +1,111 @@+/**+ * Decision models through OpenRouter's Decisions API.+ *+ * A decision model does not write text. It reads a `state` (text or JSON) and+ * answers named questions with calibrated probabilities:+ * - `choice`: one of a fixed set of options+ * - `noul`: the probability of yes+ * - `score`: a level on an ordered rubric+ * Up to 128 questions fit in one request.+ *+ * OpenRouter serves these models on a separate endpoint, not Chat Completions,+ * so they do not go through resolveModel(). The endpoint is marked alpha:+ * callers keep a fallback.+ */++import { logger } from './logger.js';++const DECISIONS_URL = 'https://openrouter.ai/api/alpha/decisions';++/** Perplexity's decision model; the default for every decision here. */+export const DEFAULT_DECIDER_MODEL = 'perplexity/pplx-decider-v1.1-27b';++const TIMEOUT_MS = 30_000;+const MAX_RETRIES = 3;+/** Requests in flight at once, so a tracking run does not burst past rate limits. */+const MAX_CONCURRENT = 8;+/** Busy or briefly unavailable: worth another try. */+const RETRYABLE = new Set([429, 502, 503, 524, 529]);++/** True when a key is configured, so decisions can run at all. */+export function isDeciderConfigured() {+ return Boolean(process.env.OPENROUTER_API_KEY);+}++let active = 0;+const waiting = [];++/** Runs `fn` once a slot is free; a finishing request hands its slot on. */+async function withSlot(fn) {+ if (active >= MAX_CONCURRENT) {+ await new Promise((resolve) => waiting.push(resolve));+ } else {+ active++;+ }+ try {+ return await fn();+ } finally {+ const next = waiting.shift();+ if (next) next();+ else active--;+ }+}++function retryDelayMs(res, attempt) {+ const seconds = Number(res.headers.get('retry-after'));+ return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : 500 * 2 ** attempt;+}++/**+ * Asks a decision model the given questions about `state`.+ *+ * @param {object} params+ * @param {string | object} params.state - the content the questions are about+ * @param {Record<string, object>} params.questions - named questions, as the API takes them+ * @param {string} [params.model] - defaults to DECIDER_MODEL, then DEFAULT_DECIDER_MODEL+ * @returns {Promise<Record<string, object>>} the answers, keyed by question name+ */+export async function decide({ state, questions, model }) {+ const apiKey = process.env.OPENROUTER_API_KEY;+ if (!apiKey) throw new Error('OPENROUTER_API_KEY is not configured');++ const body = JSON.stringify({+ model: model || process.env.DECIDER_MODEL || DEFAULT_DECIDER_MODEL,+ state,+ questions,+ });++ return withSlot(async () => {+ for (let attempt = 0; ; attempt++) {+ const res = await fetch(DECISIONS_URL, {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${apiKey}`,+ 'Content-Type': 'application/json',+ 'HTTP-Referer': process.env.OPENROUTER_SITE_URL || 'https://ansvisor.com',+ 'X-Title': process.env.OPENROUTER_SITE_NAME || 'Ansvisor',+ },+ body,+ signal: AbortSignal.timeout(TIMEOUT_MS),+ });++ if (RETRYABLE.has(res.status) && attempt < MAX_RETRIES) {+ const delay = retryDelayMs(res, attempt);+ logger.warn({ status: res.status, attempt, delay }, '[decider] retrying');+ await new Promise((resolve) => setTimeout(resolve, delay));+ continue;+ }++ if (!res.ok) {+ const text = await res.text().catch(() => '');+ throw new Error(`Decisions API ${res.status}: ${text.slice(0, 300)}`);+ }++ const { answers } = await res.json();+ if (!answers || typeof answers !== 'object') {+ throw new Error('Decisions API returned no answers');+ }+ return answers;+ }+ });+}diff --git a/server/src/lib/decider.test.js b/server/src/lib/decider.test.jsnew file mode 100644index 000000000..4ba72575e--- /dev/null+++ b/server/src/lib/decider.test.js@@ -0,0 +1,105 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';++vi.mock('./logger.js', () => ({+ logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },+}));++const { decide, isDeciderConfigured, DEFAULT_DECIDER_MODEL } = await import('./decider.js');++const ORIGINAL_KEY = process.env.OPENROUTER_API_KEY;+const fetchMock = vi.fn();++function reply(status, body, headers = {}) {+ return {+ ok: status >= 200 && status < 300,+ status,+ headers: { get: (name) => headers[name.toLowerCase()] ?? null },+ json: async () => body,+ text: async () => JSON.stringify(body),+ };+}++const QUESTIONS = { ok: { type: 'noul', instructions: 'Is it ok?' } };++describe('decide', () => {+ beforeEach(() => {+ fetchMock.mockReset();+ vi.stubGlobal('fetch', fetchMock);+ process.env.OPENROUTER_API_KEY = 'test-key';+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ if (ORIGINAL_KEY === undefined) delete process.env.OPENROUTER_API_KEY;+ else process.env.OPENROUTER_API_KEY = ORIGINAL_KEY;+ });++ it('posts the state and questions and returns the answers', async () => {+ fetchMock.mockResolvedValue(reply(200, { answers: { ok: { type: 'noul', noul: 0.9 } } }));++ const answers = await decide({ state: 'text', questions: QUESTIONS });++ expect(answers).toEqual({ ok: { type: 'noul', noul: 0.9 } });+ const [url, init] = fetchMock.mock.calls[0];+ expect(url).toBe('https://openrouter.ai/api/alpha/decisions');+ expect(init.headers.Authorization).toBe('Bearer test-key');+ expect(JSON.parse(init.body)).toEqual({+ model: DEFAULT_DECIDER_MODEL,+ state: 'text',+ questions: QUESTIONS,+ });+ });++ it('retries a rate-limited request after Retry-After', async () => {+ fetchMock+ .mockResolvedValueOnce(reply(429, { error: 'slow down' }, { 'retry-after': '0' }))+ .mockResolvedValueOnce(reply(200, { answers: { ok: { noul: 0.1 } } }));++ expect(await decide({ state: 'text', questions: QUESTIONS })).toEqual({ ok: { noul: 0.1 } });+ expect(fetchMock).toHaveBeenCalledTimes(2);+ });++ it('throws on a request the API rejects, with its status', async () => {+ fetchMock.mockResolvedValue(reply(400, { error: 'bad question' }));++ await expect(decide({ state: 'text', questions: QUESTIONS })).rejects.toThrow(+ 'Decisions API 400',+ );+ expect(fetchMock).toHaveBeenCalledTimes(1);+ });++ it('throws when the response carries no answers', async () => {+ fetchMock.mockResolvedValue(reply(200, { model: 'x' }));++ await expect(decide({ state: 'text', questions: QUESTIONS })).rejects.toThrow('no answers');+ });++ it('needs an OpenRouter key', async () => {+ delete process.env.OPENROUTER_API_KEY;++ expect(isDeciderConfigured()).toBe(false);+ await expect(decide({ state: 'text', questions: QUESTIONS })).rejects.toThrow(+ 'OPENROUTER_API_KEY',+ );+ expect(fetchMock).not.toHaveBeenCalled();+ });++ it('keeps at most 8 requests in flight', async () => {+ let inFlight = 0;+ let peak = 0;+ fetchMock.mockImplementation(async () => {+ inFlight++;+ peak = Math.max(peak, inFlight);+ await new Promise((resolve) => setTimeout(resolve, 5));+ inFlight--;+ return reply(200, { answers: {} });+ });++ await Promise.all(+ Array.from({ length: 20 }, () => decide({ state: 'text', questions: QUESTIONS })),+ );++ expect(peak).toBe(8);+ expect(fetchMock).toHaveBeenCalledTimes(20);+ });+});diff --git a/server/src/lib/response-parser.js b/server/src/lib/response-parser.jsindex 0cd0215ff..095b59648 100644--- a/server/src/lib/response-parser.js+++ b/server/src/lib/response-parser.js@@ -108,6 +108,22 @@ export function countBrandMentions(text, brand) {return count;}+/**+ * Where the brand (name or any of its domains) appears in the text, as+ * [start, end) character ranges in order. Same matching as countBrandMentions,+ * but on the text as given, so the ranges index into it.+ */+export function brandMentionRanges(text, brand) {+ const ranges = [];+ for (const term of [brand.brandName, ...(brand.domains || [])]) {+ if (!term) continue;+ for (const match of text.matchAll(nameRegex(term, 'gi'))) {+ ranges.push([match.index, match.index + match[0].length]);+ }+ }+ return ranges.sort((a, b) => a[0] - b[0]);+}+/*** First word-boundary occurrence index of a term (case-insensitive), or -1.*/diff --git a/server/src/lib/response-parser.test.js b/server/src/lib/response-parser.test.jsindex 3287b97ed..a37d1cbc4 100644--- a/server/src/lib/response-parser.test.js+++ b/server/src/lib/response-parser.test.js@@ -1,5 +1,10 @@import { describe, it, expect } from 'vitest';-import { computeMentionPosition, countBrandMentions, parseResponse } from './response-parser.js';+import {+ brandMentionRanges,+ computeMentionPosition,+ countBrandMentions,+ parseResponse,+} from './response-parser.js';describe('response-parser', () => {describe('countBrandMentions', () => {@@ -417,3 +422,16 @@ describe('response-parser', () => {});});});++describe('brandMentionRanges', () => {+ it('finds the name and the domains as whole words, in order', () => {+ const text = 'Acme leads. See acme.com. Acmeville is a town. ACME again.';++ expect(brandMentionRanges(text, { brandName: 'Acme', domains: ['acme.com'] })).toEqual([+ [0, 4],+ [16, 20],+ [16, 24],+ [47, 51],+ ]);+ });+});diff --git a/server/src/lib/sentiment.js b/server/src/lib/sentiment.jsindex a125c7faa..ce7b81eb6 100644--- a/server/src/lib/sentiment.js+++ b/server/src/lib/sentiment.js@@ -11,19 +11,37 @@* Routing now goes through resolveModel(), so SENTIMENT_MODEL accepts any* configured provider: "anthropic/claude-haiku-4-5", "google/gemini-3-flash",* "openrouter/…". Default is unchanged from the previous behaviour.+ *+ * With OPENROUTER_API_KEY set and no SENTIMENT_MODEL, a decision model+ * (decider.js) answers instead (#987): sentiment is a three-way choice, which+ * it answers with real probabilities, far cheaper, and with room for the whole+ * answer. A chat model reads at most a 3,000-character excerpt, and the+ * excerpt is built around the brand's mentions (#986): the first 3,000+ * characters missed every brand named later, and those all came back neutral.*/import { generateObject } from 'ai';import { z } from 'zod';import { resolveModel } from './ai-provider.js';+import { decide, isDeciderConfigured } from './decider.js';+import { brandMentionRanges } from './response-parser.js';import { logger } from './logger.js';/** Preserves the pre-existing default so upgrades are a no-op. */const DEFAULT_SENTIMENT_MODEL = 'openai/gpt-5-mini';-/** Longest response slice sent for analysis — unchanged from the original. */+/** Longest excerpt sent to a chat model — the original limit. */const MAX_RESPONSE_CHARS = 3000;+/** Longest excerpt sent to the decision model, well inside its context. */+const MAX_DECIDER_CHARS = 100_000;++/** Characters kept on each side of a brand mention in an excerpt. */+const MENTION_CONTEXT_CHARS = 600;++/** The opening often frames the whole answer, so excerpts keep it. */+const OPENING_CHARS = 400;+// `confidence` carries no min/max: Anthropic rejects `minimum`/`maximum` on// numeric properties in strict structured-output mode ("For 'number' type,// properties maximum, minimum are not supported"), and OpenRouter surfaces that@@ -35,6 +53,16 @@ export const sentimentSchema = z.object({reason: z.string(),});+/** What each answer means, for the decision model. */+const SENTIMENT_CRITERIA = {+ positive:+ 'The response recommends the brand, lists or ranks it among the suggested or best options, or describes it favourably.',+ neutral:+ 'The response mentions the brand only as a source, a citation or in passing, without recommending or criticising it.',+ negative:+ 'The response criticises the brand, warns against it, or ranks it poorly compared with the alternatives.',+};+/** Clamp to [0,1] — the schema cannot express it portably. */function clampConfidence(value) {if (typeof value !== 'number' || Number.isNaN(value)) return 0.5;@@ -44,28 +72,107 @@ function clampConfidence(value) {export const SENTIMENT_SYSTEM_PROMPT ='You are a brand sentiment analyzer. Given an AI-generated response and a brand name, determine the sentiment toward that brand. Base the verdict only on how the response portrays that specific brand, not on its overall tone.';+/**+ * The parts of `text` that talk about the brand, within `budget` characters:+ * the opening, then the text around each mention, with overlapping stretches+ * merged. Text that fits is returned whole; text that never names the brand+ * is cut to its first `budget` characters.+ *+ * @param {string} text+ * @param {{ brandName: string, domains?: string[] }} brand+ * @param {number} budget+ */+export function brandExcerpt(text, brand, budget) {+ if (text.length <= budget) return text;++ const ranges = brandMentionRanges(text, brand);+ if (ranges.length === 0) return text.slice(0, budget);++ const merged = [];+ for (const [start, end] of [+ [0, OPENING_CHARS],+ ...ranges.map(([s, e]) => [+ Math.max(0, s - MENTION_CONTEXT_CHARS),+ Math.min(text.length, e + MENTION_CONTEXT_CHARS),+ ]),+ ]) {+ const last = merged.at(-1);+ if (last && start <= last[1]) last[1] = Math.max(last[1], end);+ else merged.push([start, end]);+ }++ const separator = '\n…\n';+ let excerpt = '';+ for (const [start, end] of merged) {+ const gap = excerpt ? separator : '';+ const room = budget - excerpt.length - gap.length;+ if (room <= 0) break;+ excerpt += gap + text.slice(start, Math.min(end, start + room));+ }+ return excerpt;+}++async function sentimentByDecider(responseText, brand) {+ const answers = await decide({+ state: {+ brand: brand.brandName,+ ai_response: brandExcerpt(responseText, brand, MAX_DECIDER_CHARS),+ },+ questions: {+ sentiment: {+ type: 'choice',+ instructions: `How does the AI response treat the brand "${brand.brandName}"? Judge only how it treats this brand, not other brands or the response's overall tone.`,+ criteria: SENTIMENT_CRITERIA,+ },+ },+ });++ const { choice, probabilities } = answers.sentiment ?? {};+ if (!Object.hasOwn(SENTIMENT_CRITERIA, choice)) {+ throw new Error(`Unexpected sentiment answer: ${JSON.stringify(answers.sentiment)}`);+ }+ return { sentiment: choice, confidence: clampConfidence(probabilities?.[choice]), reason: '' };+}++async function sentimentByChatModel(responseText, brand) {+ const model = resolveModel(process.env.SENTIMENT_MODEL || DEFAULT_SENTIMENT_MODEL);++ const { object } = await generateObject({+ model,+ schema: sentimentSchema,+ system: SENTIMENT_SYSTEM_PROMPT,+ prompt: `Brand: "${brand.brandName}"\n\nAI Response:\n${brandExcerpt(responseText, brand, MAX_RESPONSE_CHARS)}`,+ });++ return { ...object, confidence: clampConfidence(object.confidence) };+}+/*** Analyze sentiment of an AI response toward a specific brand.** Never throws: a failure here must not lose an otherwise valid tracking- * result, so it degrades to neutral exactly as the previous implementation did.+ * result. The decision model falls back to the chat model, and the chat model+ * to neutral, exactly as the previous implementation did.** @param {string} responseText - The AI-generated response text* @param {string} brandName - The brand name to analyze sentiment for+ * @param {string[]} [domains] - The brand's domains, which also count as mentions* @returns {Promise<{ sentiment: 'positive'|'neutral'|'negative', confidence: number, reason: string }>}*/-export async function analyzeSentimentAI(responseText, brandName) {- try {- const model = resolveModel(process.env.SENTIMENT_MODEL || DEFAULT_SENTIMENT_MODEL);+export async function analyzeSentimentAI(responseText, brandName, domains = []) {+ const brand = { brandName, domains };- const { object } = await generateObject({- model,- schema: sentimentSchema,- system: SENTIMENT_SYSTEM_PROMPT,- prompt: `Brand: "${brandName}"\n\nAI Response:\n${responseText.slice(0, MAX_RESPONSE_CHARS)}`,- });+ // An explicit SENTIMENT_MODEL is a choice of chat model, and wins.+ if (!process.env.SENTIMENT_MODEL && isDeciderConfigured()) {+ try {+ return await sentimentByDecider(responseText, brand);+ } catch (err) {+ logger.warn({ err }, '[sentiment-ai] decision model failed, falling back to the chat model');+ }+ }- return { ...object, confidence: clampConfidence(object.confidence) };+ try {+ return await sentimentByChatModel(responseText, brand);} catch (err) {logger.error({ err }, '[sentiment-ai] failed, falling back to neutral');return { sentiment: 'neutral', confidence: 0, reason: 'Analysis failed' };diff --git a/server/src/lib/sentiment.test.js b/server/src/lib/sentiment.test.jsindex 60280b714..5ed1a40a7 100644--- a/server/src/lib/sentiment.test.js+++ b/server/src/lib/sentiment.test.js@@ -3,30 +3,44 @@ import { z } from 'zod';const generateObject = vi.fn();const resolveModel = vi.fn();+const decide = vi.fn();vi.mock('ai', () => ({ generateObject: (...args) => generateObject(...args) }));vi.mock('./ai-provider.js', () => ({ resolveModel: (...args) => resolveModel(...args) }));+vi.mock('./decider.js', () => ({+ decide: (...args) => decide(...args),+ isDeciderConfigured: () => Boolean(process.env.OPENROUTER_API_KEY),+}));vi.mock('./logger.js', () => ({logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },default: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },}));-const { analyzeSentimentAI, sentimentSchema } = await import('./sentiment.js');+const { analyzeSentimentAI, brandExcerpt, sentimentSchema } = await import('./sentiment.js');-describe('analyzeSentimentAI', () => {- const ORIGINAL_MODEL = process.env.SENTIMENT_MODEL;+const ORIGINAL_ENV = {+ SENTIMENT_MODEL: process.env.SENTIMENT_MODEL,+ OPENROUTER_API_KEY: process.env.OPENROUTER_API_KEY,+};++function restoreEnv() {+ for (const [key, value] of Object.entries(ORIGINAL_ENV)) {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+ }+}+describe('analyzeSentimentAI', () => {beforeEach(() => {generateObject.mockReset();resolveModel.mockReset();+ decide.mockReset();resolveModel.mockReturnValue({ id: 'stub-model' });delete process.env.SENTIMENT_MODEL;+ delete process.env.OPENROUTER_API_KEY;});- afterEach(() => {- if (ORIGINAL_MODEL === undefined) delete process.env.SENTIMENT_MODEL;- else process.env.SENTIMENT_MODEL = ORIGINAL_MODEL;- });+ afterEach(restoreEnv);it('keeps the historical OpenAI default when SENTIMENT_MODEL is unset', async () => {generateObject.mockResolvedValue({@@ -61,7 +75,7 @@ describe('analyzeSentimentAI', () => {expect(resolveModel).toHaveBeenCalledWith('openrouter/anthropic/claude-haiku-4.5');});- it('truncates long responses to 3000 chars, as the previous implementation did', async () => {+ it('sends the first 3000 chars when the brand is never named, as before', async () => {generateObject.mockResolvedValue({object: { sentiment: 'neutral', confidence: 0.5, reason: 'ok' },});@@ -127,4 +141,112 @@ describe('analyzeSentimentAI', () => {expect((await analyzeSentimentAI('meh', 'Acme')).confidence).toBe(0.5);});++ it('sends the text around a mention past the first 3000 chars (#986)', async () => {+ generateObject.mockResolvedValue({+ object: { sentiment: 'negative', confidence: 0.8, reason: 'warned against' },+ });+ const text = `${'filler '.repeat(800)}Avoid Acme Coffee, it is stale.`;++ await analyzeSentimentAI(text, 'Acme Coffee');++ const { prompt } = generateObject.mock.calls[0][0];+ expect(prompt).toContain('Avoid Acme Coffee, it is stale.');+ });+});++describe('analyzeSentimentAI with the decision model', () => {+ const answer = (choice, probabilities) => ({+ sentiment: { type: 'choice', choice, probabilities },+ });++ beforeEach(() => {+ generateObject.mockReset();+ resolveModel.mockReset();+ decide.mockReset();+ resolveModel.mockReturnValue({ id: 'stub-model' });+ delete process.env.SENTIMENT_MODEL;+ process.env.OPENROUTER_API_KEY = 'test-key';+ });++ afterEach(restoreEnv);++ it('is the default with an OpenRouter key, and reads the whole answer', async () => {+ decide.mockResolvedValue(answer('positive', { positive: 0.9, neutral: 0.08, negative: 0.02 }));+ const text = `${'filler '.repeat(800)}Acme Coffee is the best roaster.`;++ const result = await analyzeSentimentAI(text, 'Acme Coffee');++ expect(result).toEqual({ sentiment: 'positive', confidence: 0.9, reason: '' });+ expect(decide.mock.calls[0][0].state).toEqual({ brand: 'Acme Coffee', ai_response: text });+ expect(generateObject).not.toHaveBeenCalled();+ });++ it('falls back to the chat model when the decision model fails', async () => {+ decide.mockRejectedValue(new Error('Decisions API 503'));+ generateObject.mockResolvedValue({+ object: { sentiment: 'neutral', confidence: 0.6, reason: 'listed' },+ });++ const result = await analyzeSentimentAI('Acme Coffee is listed.', 'Acme Coffee');++ expect(result.sentiment).toBe('neutral');+ expect(resolveModel).toHaveBeenCalledWith('openai/gpt-5-mini');+ });++ it('falls back when the answer is not one of the three choices', async () => {+ decide.mockResolvedValue(answer('mixed', { mixed: 1 }));+ generateObject.mockResolvedValue({+ object: { sentiment: 'positive', confidence: 0.7, reason: 'ok' },+ });++ expect((await analyzeSentimentAI('Acme Coffee rocks.', 'Acme Coffee')).sentiment).toBe(+ 'positive',+ );+ });++ it('leaves an explicit SENTIMENT_MODEL on the chat model', async () => {+ process.env.SENTIMENT_MODEL = 'anthropic/claude-haiku-4-5';+ generateObject.mockResolvedValue({+ object: { sentiment: 'neutral', confidence: 0.5, reason: 'ok' },+ });++ await analyzeSentimentAI('Acme Coffee.', 'Acme Coffee');++ expect(decide).not.toHaveBeenCalled();+ expect(resolveModel).toHaveBeenCalledWith('anthropic/claude-haiku-4-5');+ });+});++describe('brandExcerpt', () => {+ const brand = { brandName: 'Acme', domains: ['acme.com'] };++ it('returns text that fits unchanged', () => {+ expect(brandExcerpt('Acme is fine.', brand, 100)).toBe('Acme is fine.');+ });++ it('keeps the opening and the text around each mention', () => {+ const text = `Intro line. ${'a '.repeat(1000)}Acme is great. ${'b '.repeat(1000)}See acme.com today.`;++ const excerpt = brandExcerpt(text, brand, 3000);++ expect(excerpt.startsWith('Intro line.')).toBe(true);+ expect(excerpt).toContain('Acme is great.');+ expect(excerpt).toContain('See acme.com today.');+ expect(excerpt.length).toBeLessThanOrEqual(3000);+ });++ it('merges mentions close enough to overlap into one stretch', () => {+ const text = `${'x '.repeat(1000)}Acme one. Acme two. ${'y '.repeat(1000)}`;++ const excerpt = brandExcerpt(text, brand, 3000);++ expect(excerpt.split('\n…\n')).toHaveLength(2);+ });++ it('never exceeds the budget, however many mentions there are', () => {+ const text = Array.from({ length: 50 }, (_, i) => `${'z '.repeat(400)}Acme ${i}.`).join(' ');++ expect(brandExcerpt(text, brand, 3000).length).toBeLessThanOrEqual(3000);+ });});diff --git a/server/src/workers/tracking-worker.js b/server/src/workers/tracking-worker.jsindex bef66f021..4a4e8847a 100644--- a/server/src/workers/tracking-worker.js+++ b/server/src/workers/tracking-worker.js@@ -702,7 +702,7 @@ export async function processTrackingJob({ brandId, promptId, promptIds, source,const mentionCount = countBrandMentions(aiResponse.text, brandInfo);const sentimentResult =mentionCount > 0- ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName)+ ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName, brandInfo.domains): { sentiment: 'neutral', confidence: 0, reason: 'Brand not mentioned' };const metrics = parseResponse(aiResponse,@@ -786,7 +786,7 @@ export async function processTrackingJob({ brandId, promptId, promptIds, source,const mentionCount = countBrandMentions(aiResponse.text, brandInfo);const sentimentResult =mentionCount > 0- ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName)+ ? await analyzeSentimentAI(aiResponse.text, brandInfo.brandName, brandInfo.domains): { sentiment: 'neutral', confidence: 0, reason: 'Brand not mentioned' };const metrics = parseResponse(aiResponse,