Skip to content

Comments

chore: Add additional PostHog events for chat repo tracking and UI interactions#922

Merged
brendan-kellam merged 5 commits intomainfrom
bkellam/posthog-chat-events-SOU-472
Feb 23, 2026
Merged

chore: Add additional PostHog events for chat repo tracking and UI interactions#922
brendan-kellam merged 5 commits intomainfrom
bkellam/posthog-chat-events-SOU-472

Conversation

@brendan-kellam
Copy link
Contributor

@brendan-kellam brendan-kellam commented Feb 23, 2026

Summary

  • Adds selectedRepos to wa_chat_message_sent (SaaS-only, only populated when EXPERIMENT_ASK_GH_ENABLED=true). Reposets are expanded to individual repo names before capture.
  • Refactors createMessageStream to accept pre-expanded repos as a parameter instead of expanding them internally, so expansion happens once before both the event and the agent call.
  • Adds three new UI interaction events: wa_chat_details_card_toggled, wa_chat_copy_answer_pressed, and wa_chat_toc_toggled.

Resolves SOU-472

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Enhanced chat telemetry: details card expand/collapse, answer copy, and table-of-contents toggle are now tracked.
    • Chat message events can optionally include selected repository names when the related experiment is enabled, improving event context for chat interactions.
  • Documentation

    • Expanded contributor guidance: Conventional Commits, changelog update steps, PR description requirements, and post-PR checklist.

brendan-kellam and others added 2 commits February 23, 2026 12:40
- 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>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 23, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dbf2739 and 1cdd831.

📒 Files selected for processing (2)
  • packages/web/src/app/api/(server)/chat/blocking/route.ts
  • packages/web/src/app/api/(server)/chat/route.ts

Walkthrough

Adds PostHog telemetry for chat UI interactions (details card toggle, copy answer, TOC toggle), extends wa_chat_message_sent with optional selectedRepos, and changes chat stream plumbing to compute and pass flattened selectedRepos instead of orgId/prisma.

Changes

Cohort / File(s) Summary
Changelog & Event types
CHANGELOG.md, packages/web/src/lib/posthogEvents.ts
Added Unreleased changelog entry. Extended PosthogEventMap with wa_chat_details_card_toggled, wa_chat_copy_answer_pressed, wa_chat_toc_toggled, and optional selectedRepos?: string[] on wa_chat_message_sent.
Chat API — request/stream flow
packages/web/src/app/api/(server)/chat/route.ts, packages/web/src/app/api/(server)/chat/blocking/route.ts
Flatten selectedSearchScopes into selectedRepos/expandedRepos and pass selectedRepos: string[] into createMessageStream. Removed orgId and prisma from stream call sites and response props. Conditionally include selectedRepos in telemetry when experiment flag enabled.
Chat UI telemetry & props
packages/web/src/features/chat/components/chatThread/detailsCard.tsx, packages/web/src/features/chat/components/chatThread/answerCard.tsx, packages/web/src/features/chat/components/chatThread/chatThreadListItem.tsx
Propagate chatId into DetailsCard. Emit PostHog events on details-card expand/collapse (wa_chat_details_card_toggled), copy answer (wa_chat_copy_answer_pressed), and TOC toggle (wa_chat_toc_toggled) via captureEvent.
Docs / contributor guidance
CLAUDE.md
Expanded PR/commit conventions and changelog update instructions for contributors.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • msukkari
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding PostHog events for chat repo tracking and UI interactions, which is the core objective of this PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bkellam/posthog-chat-events-SOU-472

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@brendan-kellam brendan-kellam changed the title Add PostHog events for chat repo tracking and UI interactions chore: Add additional PostHog events for chat repo tracking and UI interactions Feb 23, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | 🟠 Major

Scope repo lookup by orgId to avoid cross-tenant leakage.
Line 138-143 uses findFirst on 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

📥 Commits

Reviewing files that changed from the base of the PR and between fbf3984 and 2385f87.

📒 Files selected for processing (7)
  • CHANGELOG.md
  • packages/web/src/app/api/(server)/chat/blocking/route.ts
  • packages/web/src/app/api/(server)/chat/route.ts
  • packages/web/src/features/chat/components/chatThread/answerCard.tsx
  • packages/web/src/features/chat/components/chatThread/chatThreadListItem.tsx
  • packages/web/src/features/chat/components/chatThread/detailsCard.tsx
  • packages/web/src/lib/posthogEvents.ts

@brendan-kellam brendan-kellam merged commit 5be4667 into main Feb 23, 2026
8 of 9 checks passed
@brendan-kellam brendan-kellam deleted the bkellam/posthog-chat-events-SOU-472 branch February 23, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant