chore: Add additional PostHog events for chat repo tracking and UI interactions#922
Conversation
- Track selected repos in `wa_chat_message_sent` (SaaS-only, gated on EXPERIMENT_ASK_GH_ENABLED) - Refactor `createMessageStream` to accept pre-expanded repos instead of expanding internally - Add `wa_chat_details_card_toggled`, `wa_chat_copy_answer_pressed`, `wa_chat_toc_toggled` events - Add @saasOnly / @experimentonly JSDoc convention for SaaS-only properties Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
WalkthroughAdds PostHog telemetry for chat UI interactions (details card toggle, copy answer, TOC toggle), extends Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant ServerAPI as Server (chat/route)
participant RepoExpander as ScopeExpander
participant Stream as createMessageStream
participant PostHog
Client->>ServerAPI: Send chat message + selectedSearchScopes
ServerAPI->>RepoExpander: Expand selectedSearchScopes -> expandedRepos
RepoExpander-->>ServerAPI: expandedRepos (list)
ServerAPI->>Stream: createMessageStream(message, selectedRepos=expandedRepos, metadata)
Stream-->>ServerAPI: streaming responses / finalMessages
ServerAPI->>PostHog: emit wa_chat_message_sent (includes selectedRepos when EXPERIMENT_ASK_GH_ENABLED)
Client->>PostHog: emit UI events (wa_chat_details_card_toggled, wa_chat_copy_answer_pressed, wa_chat_toc_toggled) with chatId
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/web/src/app/api/(server)/chat/blocking/route.ts (1)
138-170:⚠️ Potential issue | 🟠 MajorScope repo lookup by orgId to avoid cross-tenant leakage.
Line 138-143 usesfindFirston name only; in a multi-tenant DB this can resolve a repo from another org when names collide. Add orgId to the where clause.🛠️ Proposed fix
- const repoDB = await prisma.repo.findFirst({ - where: { - name: repo, - }, - }); + const repoDB = await prisma.repo.findFirst({ + where: { + orgId: org.id, + name: repo, + }, + });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/web/src/app/api/`(server)/chat/blocking/route.ts around lines 138 - 170, The repo lookup in the selectedSearchScopes mapping uses prisma.repo.findFirst with only the repo name, which can return a repo from a different tenant; modify the where clause in the prisma.repo.findFirst call inside the selectedSearchScopes mapping to include the current tenant/org id (e.g., add orgId: chat.orgId) so the query is scoped to the correct org; keep the same error handling (ServiceErrorException) and returned shape (SearchScope) when repoDB is missing.
🧹 Nitpick comments (1)
packages/web/src/app/api/(server)/chat/route.ts (1)
91-101: Consider de-duplicating expanded repos before telemetry/agent use.
Line 91-101 can emit duplicates when repos appear in multiple reposets. A Set keeps payloads stable and avoids redundant filtering.♻️ Suggested tweak
- const expandedRepos = (await Promise.all(selectedSearchScopes.map(async (scope) => { + const expandedRepos = Array.from(new Set((await Promise.all(selectedSearchScopes.map(async (scope) => { if (scope.type === 'repo') return [scope.value]; if (scope.type === 'reposet') { const reposet = await prisma.searchContext.findFirst({ where: { orgId: org.id, name: scope.value }, include: { repos: true } }); return reposet ? reposet.repos.map(r => r.name) : []; } return []; - }))).flat(); + }))).flat()));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/web/src/app/api/`(server)/chat/route.ts around lines 91 - 101, The expandedRepos array built from selectedSearchScopes can contain duplicates when repos appear in multiple reposets; after resolving expandedRepos (the Promise.all block that maps repos via prisma.searchContext and reposet.repos.map), de-duplicate it before it is used for telemetry or passed to the agent by converting to a Set and back (or otherwise filtering duplicates) so the payload is stable and avoids redundant operations; update references that use expandedRepos to use the de-duplicated version.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/web/src/app/api/`(server)/chat/blocking/route.ts:
- Around line 138-170: The repo lookup in the selectedSearchScopes mapping uses
prisma.repo.findFirst with only the repo name, which can return a repo from a
different tenant; modify the where clause in the prisma.repo.findFirst call
inside the selectedSearchScopes mapping to include the current tenant/org id
(e.g., add orgId: chat.orgId) so the query is scoped to the correct org; keep
the same error handling (ServiceErrorException) and returned shape (SearchScope)
when repoDB is missing.
---
Nitpick comments:
In `@packages/web/src/app/api/`(server)/chat/route.ts:
- Around line 91-101: The expandedRepos array built from selectedSearchScopes
can contain duplicates when repos appear in multiple reposets; after resolving
expandedRepos (the Promise.all block that maps repos via prisma.searchContext
and reposet.repos.map), de-duplicate it before it is used for telemetry or
passed to the agent by converting to a Set and back (or otherwise filtering
duplicates) so the payload is stable and avoids redundant operations; update
references that use expandedRepos to use the de-duplicated version.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
CHANGELOG.mdpackages/web/src/app/api/(server)/chat/blocking/route.tspackages/web/src/app/api/(server)/chat/route.tspackages/web/src/features/chat/components/chatThread/answerCard.tsxpackages/web/src/features/chat/components/chatThread/chatThreadListItem.tsxpackages/web/src/features/chat/components/chatThread/detailsCard.tsxpackages/web/src/lib/posthogEvents.ts
Summary
selectedRepostowa_chat_message_sent(SaaS-only, only populated whenEXPERIMENT_ASK_GH_ENABLED=true). Reposets are expanded to individual repo names before capture.createMessageStreamto accept pre-expanded repos as a parameter instead of expanding them internally, so expansion happens once before both the event and the agent call.wa_chat_details_card_toggled,wa_chat_copy_answer_pressed, andwa_chat_toc_toggled.Resolves SOU-472
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Documentation