Skip to content

📈 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!

Fix broken AI chat

Blue Mouse1b384b844090
diff --git a/backend/apps/cloud/src/ai/ai.controller.ts b/backend/apps/cloud/src/ai/ai.controller.ts
index d8821bbf6..c22ef4696 100644
--- a/backend/apps/cloud/src/ai/ai.controller.ts
+++ b/backend/apps/cloud/src/ai/ai.controller.ts
@@ -146,9 +146,11 @@ export class AiController {
res.setHeader('X-Accel-Buffering', 'no')
res.flushHeaders()
+ const abortController = new AbortController()
let clientClosed = false
res.on('close', () => {
clientClosed = true
+ abortController.abort()
})
const messages = chatDto.messages
@@ -174,6 +176,7 @@ export class AiController {
project,
messages,
chatDto.timezone || 'UTC',
+ abortController.signal,
)
let hasContent = false
@@ -259,9 +262,6 @@ export class AiController {
{ error: part.error, pid, uid },
'Error event during AI stream',
)
- res.write(
- `data: ${JSON.stringify({ type: 'error', content: 'A temporary error occurred, continuing...' })}\n\n`,
- )
} else if (part.type === 'finish') {
const totalUsage = (part as any)?.totalUsage ?? {}
const durationMs = Date.now() - streamStartedAt
@@ -388,16 +388,21 @@ export class AiController {
{ error: streamError, pid, uid },
'Exception during AI stream iteration',
)
+ }
- if (hasContent) {
- res.write(
- `data: ${JSON.stringify({ type: 'error', content: 'The response was interrupted due to a provider error.' })}\n\n`,
- )
- } else {
- res.write(
- `data: ${JSON.stringify({ type: 'error', content: 'Failed to get a response from the AI provider. Please try again.' })}\n\n`,
- )
- }
+ if (clientClosed) return
+
+ if (streamErrored || !assistantText.trim()) {
+ res.write(
+ `data: ${JSON.stringify({
+ type: 'error',
+ content: assistantText.trim()
+ ? 'The response was interrupted. Please try again.'
+ : 'Could not complete the response. Please try again.',
+ })}\n\n`,
+ )
+ res.end()
+ return
}
// Generate follow-up suggestions only when the assistant produced a real
diff --git a/backend/apps/cloud/src/ai/ai.service.ts b/backend/apps/cloud/src/ai/ai.service.ts
index 679708679..8463b0207 100644
--- a/backend/apps/cloud/src/ai/ai.service.ts
+++ b/backend/apps/cloud/src/ai/ai.service.ts
@@ -26,6 +26,7 @@ import { clickhouse } from '../common/integrations/clickhouse'
import { Project } from '../project/entity/project.entity'
import { TimeBucketType } from '../analytics/dto/getData.dto'
import { PlanFeatureCode } from '../user/entities/user.entity'
+import { toJsonToolOutput } from './tool-output'
const OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1'
@@ -354,6 +355,7 @@ export class AiService {
project: Project,
messages: ModelMessage[],
timezone: string = 'UTC',
+ abortSignal?: AbortSignal,
): Promise<
StreamTextResult<
typeof this.buildTools extends (...args: any[]) => infer R ? R : never,
@@ -381,6 +383,7 @@ export class AiService {
model: this.openrouter.chat(PRIMARY_MODEL),
system: systemPrompt,
messages,
+ abortSignal,
tools: this.buildTools(project, timezone),
stopWhen: stepCountIs(15),
// Kept low deliberately: reasoning tokens bill at the output rate and
@@ -670,6 +673,7 @@ Chart "link" field (REQUIRED whenever the chart corresponds to data the user can
return {
getProjectInfo: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get basic information about the current project: name, created date, whether CAPTCHA is enabled, plus the available funnels, goals, feature flags and experiments. Call this early in a conversation to discover what entities exist before asking for stats on them.',
inputSchema: z.object({}),
@@ -701,6 +705,7 @@ Chart "link" field (REQUIRED whenever the chart corresponds to data the user can
}),
getData: tool({
+ toModelOutput: toJsonToolOutput,
description: `Query analytics-style data for the project. Returns overall counts plus chart data and panel breakdowns (top pages, countries, browsers, etc.).
Use dataType to choose the dataset:
@@ -814,6 +819,7 @@ Filter modifiers:
}),
getGoalStats: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get goal conversion statistics including conversions, unique sessions, and per-goal totals. Call without goalId to get totals for every active goal.',
inputSchema: z.object({
@@ -851,6 +857,7 @@ Filter modifiers:
}),
getFunnelData: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get funnel analysis data showing step-by-step conversions and drop-off for a specific funnel.',
inputSchema: z.object({
@@ -900,6 +907,7 @@ Filter modifiers:
}),
getFeatureFlagStats: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get evaluation stats for feature flags: total evaluations, unique profiles exposed, true vs false counts and percentages. Pass a flagId for a specific flag, or omit to get a summary of all flags in the project.',
inputSchema: z.object({
@@ -935,6 +943,7 @@ Filter modifiers:
}),
getExperimentResults: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get exposures and conversion counts per variant for an A/B experiment. Returns variant exposures, conversions, and conversion rate so you can advise on which variant is winning. Omit experimentId to list all experiments with their basic config.',
inputSchema: z.object({
@@ -972,6 +981,7 @@ Filter modifiers:
}),
getSessionsList: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get a list of recent user sessions (psid, country, OS, browser, started/ended timestamps, page count) for inspection. Useful for finding examples of user behaviour or debugging. Capped at 25 sessions per call.',
inputSchema: z.object({
@@ -1012,6 +1022,7 @@ Filter modifiers:
}),
getProfilesOverview: tool({
+ toModelOutput: toJsonToolOutput,
description:
'Get a high-level overview of profiles (returning visitors): unique profile count, sessions per profile, and top page paths. Useful for understanding audience composition and stickiness.',
inputSchema: z.object({
diff --git a/backend/apps/cloud/src/ai/tool-output.test.mjs b/backend/apps/cloud/src/ai/tool-output.test.mjs
new file mode 100644
index 000000000..fc772ddff
--- /dev/null
+++ b/backend/apps/cloud/src/ai/tool-output.test.mjs
@@ -0,0 +1,111 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { modelMessageSchema, streamText, stepCountIs, tool } from 'ai'
+import { MockLanguageModelV4 } from 'ai/test'
+import { z } from 'zod'
+import { toJsonToolOutput } from './tool-output.ts'
+
+const projectInfo = {
+ name: 'Test project',
+ created: new Date('2021-08-16T00:49:16Z'),
+ experiments: [{ startedAt: new Date('2026-09-01T12:00:00Z'), endedAt: null }],
+ optionalField: undefined,
+}
+
+const toolMessage = (output) => ({
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call-1',
+ toolName: 'getProjectInfo',
+ output,
+ },
+ ],
+})
+
+test('normalizes database dates and omitted values into a valid model message', () => {
+ assert.equal(
+ modelMessageSchema.safeParse(
+ toolMessage({ type: 'json', value: projectInfo }),
+ ).success,
+ false,
+ )
+ const output = toJsonToolOutput({ output: projectInfo })
+ assert.equal(modelMessageSchema.safeParse(toolMessage(output)).success, true)
+ assert.deepEqual(output.value, {
+ name: 'Test project',
+ created: '2021-08-16T00:49:16.000Z',
+ experiments: [{ startedAt: '2026-09-01T12:00:00.000Z', endedAt: null }],
+ })
+ assert.ok(projectInfo.created instanceof Date)
+})
+
+test('streams an answer after a tool returns database Date objects', async () => {
+ const usage = { inputTokens: { total: 1 }, outputTokens: { total: 1 } }
+ const model = new MockLanguageModelV4({
+ doStream: [
+ {
+ stream: new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ type: 'tool-call',
+ toolCallId: 'call-1',
+ toolName: 'getProjectInfo',
+ input: '{}',
+ })
+ controller.enqueue({
+ type: 'finish',
+ finishReason: { unified: 'tool-calls', raw: 'tool_calls' },
+ usage,
+ })
+ controller.close()
+ },
+ }),
+ },
+ {
+ stream: new ReadableStream({
+ start(controller) {
+ controller.enqueue({ type: 'text-start', id: 'text-1' })
+ controller.enqueue({
+ type: 'text-delta',
+ id: 'text-1',
+ delta: 'Your project was created in August 2021.',
+ })
+ controller.enqueue({ type: 'text-end', id: 'text-1' })
+ controller.enqueue({
+ type: 'finish',
+ finishReason: { unified: 'stop', raw: 'stop' },
+ usage,
+ })
+ controller.close()
+ },
+ }),
+ },
+ ],
+ })
+ const result = streamText({
+ model,
+ prompt: 'When was my project created?',
+ tools: {
+ getProjectInfo: tool({
+ inputSchema: z.object({}),
+ execute: async () => projectInfo,
+ toModelOutput: toJsonToolOutput,
+ }),
+ },
+ stopWhen: stepCountIs(2),
+ })
+ const parts = []
+ for await (const part of result.fullStream) parts.push(part)
+ assert.equal(
+ parts.some((part) => part.type === 'error'),
+ false,
+ )
+ assert.equal(await result.text, 'Your project was created in August 2021.')
+ assert.equal(model.doStreamCalls.length, 2)
+ const output = model.doStreamCalls[1].prompt.find(
+ (message) => message.role === 'tool',
+ ).content[0].output
+ assert.equal(output.value.created, '2021-08-16T00:49:16.000Z')
+})
diff --git a/backend/apps/cloud/src/ai/tool-output.ts b/backend/apps/cloud/src/ai/tool-output.ts
new file mode 100644
index 000000000..dbefa0ffb
--- /dev/null
+++ b/backend/apps/cloud/src/ai/tool-output.ts
@@ -0,0 +1,6 @@
+import type { JSONValue } from 'ai'
+
+export const toJsonToolOutput = ({ output }: { output: unknown }) => ({
+ type: 'json' as const,
+ value: JSON.parse(JSON.stringify(output ?? null)) as JSONValue,
+})