Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ describe('NewWidgetBuilder', () => {
expect(data.visualize).toEqual(['count()']);
expect(data.fields).toEqual(['browser.name']);
expect(data.query).toEqual(['browser.name:Firefox']);
expect(data.dashboardTitle).toBe('Dashboard');
expect(data.dashboardWidgetCount).toBe(0);
expect(data.dashboardFilters).toEqual([]);
});

it('does not register LLM context when the builder is closed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ function WidgetBuilderSlideoutInner({
useLLMContext({
contextHint:
'Sentry widget builder. The user is configuring a dashboard widget. visualize is the y-axis metrics (timeseries) or the aggregate (big number/table). fields are group-by columns (timeseries) or visible columns (table). query filters the data and sort controls ordering.',
dashboardTitle: dashboard.title,
dashboardWidgetCount: dashboard.widgets.length,
dashboardFilters: dashboard.filters,
mode: isEditing ? 'editing' : 'creating',
title: state.title,
description: state.description,
Expand Down
55 changes: 55 additions & 0 deletions static/app/views/seerExplorer/hooks/useSeerExplorer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {OrganizationFixture} from 'sentry-fixture/organization';

import {act, renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';

import {useLLMContext} from 'sentry/views/seerExplorer/contexts/llmContext';
import {usePageReferrer} from 'sentry/views/seerExplorer/utils';

import {useSeerExplorer} from './useSeerExplorer';
Expand All @@ -11,13 +12,21 @@ jest.mock('sentry/views/seerExplorer/utils', () => ({
usePageReferrer: jest.fn(),
}));

jest.mock('sentry/views/seerExplorer/contexts/llmContext', () => ({
...jest.requireActual('sentry/views/seerExplorer/contexts/llmContext'),
useLLMContext: jest.fn(),
}));

describe('useSeerExplorer', () => {
beforeEach(() => {
MockApiClient.clearMockResponses();
sessionStorage.clear();
(usePageReferrer as jest.Mock).mockReturnValue({
getPageReferrer: () => '/issues/',
});
(useLLMContext as jest.Mock).mockReturnValue({
getLLMContext: () => ({version: 0, nodes: []}),
});
});

const organization = OrganizationFixture({
Expand Down Expand Up @@ -148,6 +157,52 @@ describe('useSeerExplorer', () => {
});
});

it('filters to only widget-builder nodes on widget builder routes', async () => {
(usePageReferrer as jest.Mock).mockReturnValue({
getPageReferrer: () => '/dashboard/:dashboardId/widget-builder/widget/new/',
});
(useLLMContext as jest.Mock).mockReturnValue({
getLLMContext: () => ({
version: 1,
nodes: [
{nodeType: 'dashboard', data: {title: 'My Dashboard'}, children: []},
{nodeType: 'widget-builder', data: {mode: 'creating'}, children: []},
],
}),
});
const org = OrganizationFixture({
features: ['seer-explorer', 'context-engine-structured-page-context'],
});
MockApiClient.addMockResponse({
url: `/organizations/${org.slug}/seer/explorer-chat/`,
method: 'GET',
body: {session: null},
});
const postMock = MockApiClient.addMockResponse({
url: `/organizations/${org.slug}/seer/explorer-chat/`,
method: 'POST',
body: {run_id: 1},
});
MockApiClient.addMockResponse({
url: `/organizations/${org.slug}/seer/explorer-chat/1/`,
method: 'GET',
body: {session: {blocks: [], run_id: 1, status: 'completed', updated_at: ''}},
});

const {result} = renderHookWithProviders(() => useSeerExplorer(), {
organization: org,
});
act(() => {
result.current.sendMessage('q');
});

await waitFor(() => {
const ctx = JSON.parse(postMock.mock.calls[0][1].data.on_page_context);
expect(ctx.nodes).toHaveLength(1);
expect(ctx.nodes[0].nodeType).toBe('widget-builder');
});
});

it('falls back to ASCII screenshot on non-dashboard page', async () => {
const org = OrganizationFixture({
features: ['seer-explorer', 'seer-explorer-context-engine'],
Expand Down
15 changes: 14 additions & 1 deletion static/app/views/seerExplorer/hooks/useSeerExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ const NEW_STRUCTURED_CONTEXT_ROUTES = new Set([
'/dashboard/:dashboardId/widget-builder/widget/:widgetIndex/edit/',
]);

/** Widget builder routes — only the builder node is relevant, not the full dashboard tree. */
const WIDGET_BUILDER_ROUTES = new Set([
'/dashboard/:dashboardId/widget-builder/widget/new/',
'/dashboard/:dashboardId/widget-builder/widget/:widgetIndex/edit/',
]);
Comment thread
Mihir-Mavalankar marked this conversation as resolved.

function supportsStructuredContext(
referrer: string,
organization: {features: string[]} | null | undefined
Expand Down Expand Up @@ -397,7 +403,14 @@ export const useSeerExplorer = () => {
let screenshot: string | undefined;
if (supportsStructuredContext(getPageReferrer(), organization)) {
try {
screenshot = JSON.stringify(getLLMContext());
let snapshot = getLLMContext();
if (WIDGET_BUILDER_ROUTES.has(getPageReferrer())) {
snapshot = {
...snapshot,
nodes: snapshot.nodes.filter(n => n.nodeType === 'widget-builder'),
};
}
screenshot = JSON.stringify(snapshot);
Comment on lines -400 to +413

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might make sense to have a mechanism to figure out node priority based on the node's properties (e.g., a priority property, or something similar) rather than using routes, but this makes sense in a pinch 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah it's a bit hacky for sure. I'll think on this a bit and come back to it with some priority based system.

} catch (e) {
Sentry.captureException(e);
screenshot = captureAsciiSnapshot?.();
Expand Down
Loading