Skip to content

chore(trace ai queries): Search with free text directly #96722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 31, 2025
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
13 changes: 13 additions & 0 deletions static/app/components/searchQueryBuilder/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import useOrganization from 'sentry/utils/useOrganization';

interface SearchQueryBuilderContextData {
actionBarRef: React.RefObject<HTMLDivElement | null>;
autoSubmitSeer: boolean;
committedQuery: string;
currentInputValue: string;
disabled: boolean;
disallowFreeText: boolean;
disallowWildcard: boolean;
Expand All @@ -49,6 +51,8 @@ interface SearchQueryBuilderContextData {
parsedQuery: ParseResult | null;
query: string;
searchSource: string;
setAutoSubmitSeer: (enabled: boolean) => void;
setCurrentInputValue: (value: string) => void;
setDisplaySeerResults: (enabled: boolean) => void;
size: 'small' | 'normal';
wrapperRef: React.RefObject<HTMLDivElement | null>;
Expand Down Expand Up @@ -108,6 +112,8 @@ export function SearchQueryBuilderProvider({
const {setupAcknowledgement} = useOrganizationSeerSetup({enabled: enableAISearch});

const [displaySeerResults, setDisplaySeerResults] = useState(false);
const [autoSubmitSeer, setAutoSubmitSeer] = useState(false);
const [currentInputValue, setCurrentInputValue] = useState('');

const {state, dispatch} = useQueryBuilderState({
initialQuery,
Expand Down Expand Up @@ -191,16 +197,21 @@ export function SearchQueryBuilderProvider({
portalTarget,
displaySeerResults,
setDisplaySeerResults,
autoSubmitSeer,
setAutoSubmitSeer,
replaceRawSearchKeys,
filterKeyAliases,
gaveSeerConsent: setupAcknowledgement.orgHasAcknowledged,
currentInputValue,
setCurrentInputValue,
};
}, [
disabled,
disallowFreeText,
disallowWildcard,
dispatch,
displaySeerResults,
autoSubmitSeer,
enableAISearch,
filterKeyAliases,
filterKeyMenuWidth,
Expand All @@ -220,6 +231,8 @@ export function SearchQueryBuilderProvider({
stableFilterKeys,
stableGetSuggestedFilterKey,
state,
currentInputValue,
setCurrentInputValue,
]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
filterKeys,
getFieldDefinition,
setDisplaySeerResults,
setAutoSubmitSeer,
enableAISearch,
gaveSeerConsent,
currentInputValue,
} = useSearchQueryBuilder();
const {sectionedItems} = useFilterKeyItems();
const recentFilters = useRecentSearchFilters();
Expand Down Expand Up @@ -391,6 +393,12 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
action: 'opened',
});
setDisplaySeerResults(true);

if (currentInputValue?.trim()) {
setAutoSubmitSeer(true);
} else {
setAutoSubmitSeer(false);
}
return;
}

Expand All @@ -403,7 +411,13 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
return;
}
},
[organization, seerAcknowledgeMutate, setDisplaySeerResults]
[
organization,
seerAcknowledgeMutate,
setDisplaySeerResults,
setAutoSubmitSeer,
currentInputValue,
]
);

return {
Expand Down
13 changes: 8 additions & 5 deletions static/app/components/searchQueryBuilder/tokens/freeText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,6 @@ function SearchQueryBuilderInputInternal({
setSelectionIndex(inputRef.current?.selectionStart ?? 0);
}, []);

const resetInputValue = useCallback(() => {
setInputValue(trimmedTokenValue);
updateSelectionIndex();
}, [trimmedTokenValue, updateSelectionIndex]);

const filterValue = getWordAtCursorPosition(inputValue, selectionIndex);

const {
Expand All @@ -270,8 +265,15 @@ function SearchQueryBuilderInputInternal({
placeholder,
searchSource,
recentSearches,
setCurrentInputValue,
} = useSearchQueryBuilder();

const resetInputValue = useCallback(() => {
setInputValue(trimmedTokenValue);
setCurrentInputValue(trimmedTokenValue);
updateSelectionIndex();
}, [trimmedTokenValue, updateSelectionIndex, setCurrentInputValue]);

const {customMenu, sectionItems, maxOptions, onKeyDownCapture, handleOptionSelected} =
useFilterKeyListBox({
filterValue,
Expand Down Expand Up @@ -617,6 +619,7 @@ function SearchQueryBuilderInputInternal({
}

setInputValue(e.target.value);
setCurrentInputValue(e.target.value);
setSelectionIndex(e.target.selectionStart ?? 0);
}}
onKeyDown={onKeyDown}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export function SeerComboBox({initialQuery, ...props}: SeerComboBoxProps) {
);

const openForm = useFeedbackForm();
const {setDisplaySeerResults} = useSearchQueryBuilder();
const {setDisplaySeerResults, autoSubmitSeer, setAutoSubmitSeer} =
useSearchQueryBuilder();
const {rawResult, submitQuery, isPending} = useSeerSearch();
const applySeerSearchQuery = useApplySeerSearchQuery();
const organization = useOrganization();
Expand Down Expand Up @@ -321,6 +322,17 @@ export function SeerComboBox({initialQuery, ...props}: SeerComboBoxProps) {
}
}, [state]);

useLayoutEffect(() => {
if (autoSubmitSeer && searchQuery.trim()) {
trackAnalytics('trace.explorer.ai_query_submitted', {
organization,
natural_language_query: searchQuery.trim(),
});
submitQuery(searchQuery.trim());
setAutoSubmitSeer(false);
}
}, [autoSubmitSeer, searchQuery, organization, submitQuery, setAutoSubmitSeer]);

return (
<Wrapper ref={containerRef} isDropdownOpen={state.isOpen}>
<PositionedSearchIconContainer>
Expand Down
15 changes: 13 additions & 2 deletions static/app/views/explore/spans/spansTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,21 @@ function SpansSearchBar({
}: {
eapSpanSearchQueryBuilderProps: EAPSpanSearchQueryBuilderProps;
}) {
const {displaySeerResults, query} = useSearchQueryBuilder();
const {displaySeerResults, query, currentInputValue} = useSearchQueryBuilder();

const initialSeerQuery = (() => {
const committedQuery = query.trim();
const inputValue = currentInputValue.trim();

if (!inputValue) return committedQuery;

if (!committedQuery) return inputValue;

return `${committedQuery} ${inputValue}`;
})();

return displaySeerResults ? (
<SeerComboBox initialQuery={query} />
<SeerComboBox initialQuery={initialSeerQuery} />
) : (
<EAPSpanSearchQueryBuilder autoFocus {...eapSpanSearchQueryBuilderProps} />
);
Expand Down
16 changes: 12 additions & 4 deletions static/app/views/explore/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,26 @@ export function formatQueryToNaturalLanguage(query: string): string {
return formattedTokens.reduce((result, token, index) => {
if (index === 0) return token;

const prevToken = formattedTokens[index - 1];
if (!prevToken) return `${result}, ${token}`;
const currentOriginalToken = tokens[index] || '';
const prevOriginalToken = tokens[index - 1] || '';

const isLogicalOp = token.toUpperCase() === 'AND' || token.toUpperCase() === 'OR';
const prevIsLogicalOp =
prevToken.toUpperCase() === 'AND' || prevToken.toUpperCase() === 'OR';
formattedTokens[index - 1]?.toUpperCase() === 'AND' ||
formattedTokens[index - 1]?.toUpperCase() === 'OR';

if (isLogicalOp || prevIsLogicalOp) {
return `${result} ${token}`;
}

return `${result}, ${token}`;
const isCurrentFilter = /[:>=<!]/.test(currentOriginalToken);
const isPrevFilter = /[:>=<!]/.test(prevOriginalToken);

if (isCurrentFilter && isPrevFilter) {
return `${result}, ${token}`;
}

return `${result} ${token}`;
}, '');
}

Expand Down
Loading