π Open source, privacy-first web analytics β a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
AI chat behavioural fixes
Blue Mousef9103cdb0535
diff --git a/backend/apps/cloud/src/ai/ai-chat.service.ts b/backend/apps/cloud/src/ai/ai-chat.service.tsindex 683a0c08f..432c11bcb 100644--- a/backend/apps/cloud/src/ai/ai-chat.service.ts+++ b/backend/apps/cloud/src/ai/ai-chat.service.ts@@ -142,7 +142,8 @@ export class AiChatService {.andWhere("chat.tags <> ''").getRawMany<{ tags: string | null }>()- const set = new Set<string>()+ // Dedupe case-insensitively while preserving the first-seen casing+ const map = new Map<string, string>()for (const row of rows) {if (!row.tags) continue// simple-array is comma-separated@@ -150,10 +151,13 @@ export class AiChatService {.split(',').map((t) => t.trim()).filter(Boolean)- for (const p of parts) set.add(p)+ for (const part of parts) {+ const key = part.toLowerCase()+ if (!map.has(key)) map.set(key, part)+ }}- return Array.from(set).sort((a, b) =>+ return Array.from(map.values()).sort((a, b) =>a.localeCompare(b, undefined, { sensitivity: 'base' }),)}@@ -189,6 +193,33 @@ export class AiChatService {return { id: parent.id, name: parent.name }}+ /**+ * Atomically updates the chat name only when the current value still matches+ * `expectedName`. Used by background title generation so it can't clobber a+ * user-provided rename that happened concurrently. Returns true if the row+ * was actually updated.+ */+ async updateIfNameEquals(+ id: string,+ expectedName: string | null | undefined,+ data: { name: string },+ ): Promise<boolean> {+ const qb = this.aiChatRepository+ .createQueryBuilder()+ .update(AiChat)+ .set({ name: data.name })+ .where('id = :id', { id })++ if (expectedName === null || expectedName === undefined) {+ qb.andWhere('name IS NULL')+ } else {+ qb.andWhere('name = :expectedName', { expectedName })+ }++ const result = await qb.execute()+ return (result.affected ?? 0) > 0+ }+async update(id: string,data: { messages?: ChatMessage[]; name?: string },@@ -216,9 +247,12 @@ export class AiChatService {* - enforces per-tag length cap* - dedupes case-insensitively (keeping first occurrence)* - caps total tags at MAX_TAGS_PER_CHAT+ *+ * Returns null for an empty result so the simple-array column persists as NULL+ * (avoids round-tripping `[]` β `''` β `['']`).*/- sanitiseTags(input: unknown): string[] {- if (!Array.isArray(input)) return []+ sanitiseTags(input: unknown): string[] | null {+ if (!Array.isArray(input)) return nullconst seen = new Set<string>()const out: string[] = []for (const raw of input) {@@ -232,7 +266,7 @@ export class AiChatService {out.push(trimmed)if (out.length >= MAX_TAGS_PER_CHAT) break}- return out+ return out.length === 0 ? null : out}async updateMeta(diff --git a/backend/apps/cloud/src/ai/ai.controller.ts b/backend/apps/cloud/src/ai/ai.controller.tsindex 433d9b6f3..52f590a23 100644--- a/backend/apps/cloud/src/ai/ai.controller.ts+++ b/backend/apps/cloud/src/ai/ai.controller.ts@@ -9,6 +9,7 @@ import {Query,Res,Headers,+ BadRequestException,NotFoundException,ForbiddenException,HttpException,@@ -401,18 +402,27 @@ export class AiController {// Generate follow-up suggestions only when the assistant produced a real// textual answer and the client is still connected. Capped with a short- // timeout so a slow model never delays the `done` event.+ // timeout so a slow model never delays the `done` event; on timeout we+ // abort the in-flight OpenRouter request to avoid wasting quota.if (!clientClosed && !streamErrored && assistantText.trim().length > 0) {+ const FOLLOW_UPS_TIMEOUT_MS = 5_000+ const controller = new AbortController()+ const timeoutHandle = setTimeout(+ () => controller.abort(),+ FOLLOW_UPS_TIMEOUT_MS,+ )try {- const FOLLOW_UPS_TIMEOUT_MS = 5_000const followUps = await Promise.race([this.aiService.generateFollowUps([...messages, { role: 'assistant', content: assistantText }],project,+ controller.signal,),- new Promise<string[]>((resolve) =>- setTimeout(() => resolve([]), FOLLOW_UPS_TIMEOUT_MS),- ),+ new Promise<string[]>((resolve) => {+ controller.signal.addEventListener('abort', () => resolve([]), {+ once: true,+ })+ }),])if (!clientClosed && followUps.length > 0) {res.write(@@ -424,6 +434,8 @@ export class AiController {{ err, pid, uid },'Follow-up suggestion generation threw',)+ } finally {+ clearTimeout(timeoutHandle)}}@@ -674,9 +686,12 @@ export class AiController {createChatDto.parentChatId,pid,)- if (parent) {- parentChatId = parent.id+ if (!parent) {+ throw new BadRequestException(+ 'Invalid parentChatId: parent chat does not exist in this project',+ )}+ parentChatId = parent.id}const chat = await this.aiChatService.create({@@ -704,9 +719,14 @@ export class AiController {(m) => m.role === 'user',)?.contentif (firstUserMsg) {+ const expectedName = chat.namethis.aiService.generateChatTitle(firstUserMsg)- .then((title) => this.aiChatService.update(chat.id, { name: title }))+ .then((title) =>+ this.aiChatService.updateIfNameEquals(chat.id, expectedName, {+ name: title,+ }),+ ).catch((err) =>this.logger.warn({ err, chatId: chat.id },diff --git a/backend/apps/cloud/src/ai/ai.service.ts b/backend/apps/cloud/src/ai/ai.service.tsindex cfe1972d1..8dfedd362 100644--- a/backend/apps/cloud/src/ai/ai.service.ts+++ b/backend/apps/cloud/src/ai/ai.service.ts@@ -394,6 +394,7 @@ export class AiService {async generateFollowUps(messages: ModelMessage[],project: Project,+ abortSignal?: AbortSignal,): Promise<string[]> {if (!process.env.OPENROUTER_API_KEY) {return []@@ -403,6 +404,10 @@ export class AiService {return []}+ if (abortSignal?.aborted) {+ return []+ }+// Take the tail of the conversation to keep the prompt cheapconst tail = messages.slice(-8)const transcript = tail@@ -447,6 +452,7 @@ export class AiService {'Given this analytics conversation, suggest up to 3 short follow-up questions the user might naturally ask next. Each must be a complete question under 70 chars, specific to the data discussed, and answerable by the same toolset (analytics, performance, errors, goals, funnels, sessions, profiles, feature flags, A/B experiments, custom events). Avoid duplicates and avoid restating questions the user already asked. Return STRICT JSON: {"followUps": string[]}. Do not wrap in markdown.',prompt: `Project: "${project.name}"\n\nConversation:\n${transcript}`,temperature: 0.4,+ abortSignal,})const cleaned = (text || '').trim().replace(/^```(?:json)?|```$/g, '')diff --git a/backend/apps/cloud/src/ai/dto/chat.dto.ts b/backend/apps/cloud/src/ai/dto/chat.dto.tsindex 295de2351..3e28954e4 100644--- a/backend/apps/cloud/src/ai/dto/chat.dto.ts+++ b/backend/apps/cloud/src/ai/dto/chat.dto.ts@@ -13,6 +13,9 @@ import {ArrayMaxSize,MaxLength,MinLength,+ registerDecorator,+ ValidationOptions,+ ValidationArguments,} from 'class-validator'import { Type, Transform } from 'class-transformer'@@ -24,6 +27,66 @@ const MAX_TOOL_NAME_LENGTH = 100export const MAX_TAGS_PER_CHAT = 5export const MAX_TAG_LENGTH = 30+const MAX_TOOL_ARGS_JSON_LENGTH = 4000+const MAX_TOOL_ARGS_DEPTH = 8++const measureJsonDepth = (value: unknown, depth = 0): number => {+ if (depth > MAX_TOOL_ARGS_DEPTH) return depth+ if (value === null || typeof value !== 'object') return depth+ let max = depth+ if (Array.isArray(value)) {+ for (const item of value) {+ const d = measureJsonDepth(item, depth + 1)+ if (d > max) max = d+ if (max > MAX_TOOL_ARGS_DEPTH) return max+ }+ return max+ }+ for (const key of Object.keys(value as Record<string, unknown>)) {+ const d = measureJsonDepth(+ (value as Record<string, unknown>)[key],+ depth + 1,+ )+ if (d > max) max = d+ if (max > MAX_TOOL_ARGS_DEPTH) return max+ }+ return max+}++/**+ * Validates that a value is JSON-serialisable, has bounded serialised length,+ * and bounded nesting depth. Used to defend the persisted chat payload from+ * arbitrarily large/nested user-supplied tool args.+ */+function IsBoundedJson(validationOptions?: ValidationOptions) {+ return function (object: object, propertyName: string) {+ registerDecorator({+ name: 'isBoundedJson',+ target: object.constructor,+ propertyName,+ options: validationOptions,+ validator: {+ validate(value: unknown) {+ if (value === undefined || value === null) return true+ let json: string+ try {+ json = JSON.stringify(value)+ } catch {+ return false+ }+ if (typeof json !== 'string') return false+ if (json.length > MAX_TOOL_ARGS_JSON_LENGTH) return false+ if (measureJsonDepth(value) > MAX_TOOL_ARGS_DEPTH) return false+ return true+ },+ defaultMessage(args: ValidationArguments) {+ return `${args.property} must be JSON-serialisable, at most ${MAX_TOOL_ARGS_JSON_LENGTH} chars when stringified, and nested no deeper than ${MAX_TOOL_ARGS_DEPTH} levels`+ },+ },+ })+ }+}+class ChatMessageToolCallDto {@ApiProperty({ description: 'Tool name that was invoked' })@IsNotEmpty()@@ -35,6 +98,7 @@ class ChatMessageToolCallDto {description: 'Arguments the tool was called with (arbitrary JSON)',})@IsOptional()+ @IsBoundedJson()args?: unknown@ApiProperty({diff --git a/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts b/backend/apps/cloud/src/ai/entity/ai-chat.entity.tsindex f720d160d..d6382bc2c 100644--- a/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts+++ b/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts@@ -42,9 +42,9 @@ export class AiChat {@Column('boolean', { default: false })pinned: boolean- @ApiProperty({ type: [String] })+ @ApiProperty({ type: [String], nullable: true })@Column('simple-array', { nullable: true, default: null })- tags: string[]+ tags: string[] | null@ApiProperty({ type: () => Project })@ManyToOne(() => Project, { onDelete: 'CASCADE' })diff --git a/web/app/hooks/useSpeechRecognition.ts b/web/app/hooks/useSpeechRecognition.tsindex 976dca4f5..66a910608 100644--- a/web/app/hooks/useSpeechRecognition.ts+++ b/web/app/hooks/useSpeechRecognition.ts@@ -50,7 +50,7 @@ interface UseSpeechRecognitionResult {}const useSpeechRecognition = (): UseSpeechRecognitionResult => {- const [isSupported] = useState<boolean>(() => getSpeechRecognition() !== null)+ const [isSupported, setIsSupported] = useState<boolean>(false)const [isListening, setIsListening] = useState(false)const [transcript, setTranscript] = useState('')const [interimTranscript, setInterimTranscript] = useState('')@@ -62,6 +62,7 @@ const useSpeechRecognition = (): UseSpeechRecognitionResult => {useEffect(() => {const Ctor = getSpeechRecognition()if (!Ctor) return+ setIsSupported(true)const recognition = new Ctor()recognition.lang =diff --git a/web/app/pages/Project/tabs/AskAI/AIChart.tsx b/web/app/pages/Project/tabs/AskAI/AIChart.tsxindex d8e25b5bf..741df3df8 100644--- a/web/app/pages/Project/tabs/AskAI/AIChart.tsx+++ b/web/app/pages/Project/tabs/AskAI/AIChart.tsx@@ -26,6 +26,7 @@ import React, {Fragment,memo,useCallback,+ useEffect,useMemo,useRef,useState,@@ -543,6 +544,12 @@ const AIChart: React.FC<AIChartProps> = ({ chart, projectId }) => {const [displayType, setDisplayType] = useState<AIChartType>(chart.chartType)const [chartReady, setChartReady] = useState(false)+ // Reset the user-selected display type whenever a new chart is provided so we+ // don't carry stale compatibility decisions across distinct charts.+ useEffect(() => {+ setDisplayType(chart.chartType)+ }, [chart.chartType])+const handleChartReady = useCallback((instance: Chart | null) => {setChartReady(instance !== null)}, [])diff --git a/web/app/pages/Project/tabs/AskAI/contentSegments.ts b/web/app/pages/Project/tabs/AskAI/contentSegments.tsindex c5c32badf..b91b34759 100644--- a/web/app/pages/Project/tabs/AskAI/contentSegments.ts+++ b/web/app/pages/Project/tabs/AskAI/contentSegments.ts@@ -66,7 +66,10 @@ export const parseSegments = (content: string): ContentSegment[] => {segments.push({ kind: 'text', text: jsonString })}} catch {- segments.push({ kind: 'chart', chart: null, pending: true })+ // We have a fully balanced JSON-looking slice but it didn't parse.+ // Treat it as plain text so we don't get stuck on a "pending" placeholder+ // for content that will never become a valid chart.+ segments.push({ kind: 'text', text: jsonString })}cursor = endIndex + 1}diff --git a/web/app/pages/Project/tabs/AskAI/exportHelpers.ts b/web/app/pages/Project/tabs/AskAI/exportHelpers.tsindex b42b090a8..7e9b96d8c 100644--- a/web/app/pages/Project/tabs/AskAI/exportHelpers.ts+++ b/web/app/pages/Project/tabs/AskAI/exportHelpers.ts@@ -65,7 +65,14 @@ const renderTextWithCharts = (text: string): string => {if (segment.chart) {const label = getChartLabel(segment.chart)const json = JSON.stringify(segment.chart, null, 2)- out.push(`_[Chart: ${label}]_\n\n\`\`\`json\n${json}\n\`\`\``)+ // Use a code fence longer than any backtick run inside the JSON so the+ // closing fence cannot be terminated prematurely by the content.+ const longestBacktickRun = (json.match(/`+/g) || []).reduce(+ (max, run) => Math.max(max, run.length),+ 0,+ )+ const fence = '`'.repeat(Math.max(3, longestBacktickRun + 1))+ out.push(`_[Chart: ${label}]_\n\n${fence}json\n${json}\n${fence}`)} else {out.push('_[Chart: pending]_')}diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsxindex 150da68b1..89248cb3e 100644--- a/web/app/routes/projects.$id.tsx+++ b/web/app/routes/projects.$id.tsx@@ -1267,7 +1267,10 @@ export async function action({ request, params }: ActionFunctionArgs) {try {body.tags = JSON.parse(tagsRaw)} catch {- body.tags = []+ return data<ProjectViewActionData>(+ { intent, error: 'Malformed tags payload' },+ { status: 400 },+ )}}if (name !== undefined) body.name = name