diff --git a/weave-js/src/components/PagePanelComponents/Home/Browse3.tsx b/weave-js/src/components/PagePanelComponents/Home/Browse3.tsx index dba9b4ce9d2c..cdc38eed6bb3 100644 --- a/weave-js/src/components/PagePanelComponents/Home/Browse3.tsx +++ b/weave-js/src/components/PagePanelComponents/Home/Browse3.tsx @@ -38,8 +38,10 @@ import { baseContext, browse2Context, Browse3WeaveflowRouteContextProvider, - PATH_PARAM, + DESCENDENT_CALL_ID_PARAM, + HIDE_TRACETREE_PARAM, PEEK_PARAM, + SHOW_FEEDBACK_PARAM, useClosePeek, usePeekLocation, useWeaveflowCurrentRouteContext, @@ -613,53 +615,135 @@ const useParamsDecoded = () => { }, [params]); }; -// TODO(tim/weaveflow_improved_nav): Generalize this -const CallPageBinding = () => { - useCallPeekRedirect(); +const getOptionalBoolean = ( + dict: Record, + key: string +): boolean | undefined => { + const value = dict[key]; + if (value == null) { + return undefined; + } + return value === '1'; +}; + +const getOptionalString = ( + dict: Record, + key: string +): string | undefined => { + const value = dict[key]; + if (value == null) { + return undefined; + } + return value; +}; + +const useURLBackedCallPageState = () => { const params = useParamsDecoded(); const query = useURLSearchParamsDict(); const history = useHistory(); const currentRouter = useWeaveflowCurrentRouteContext(); - - const [callId, setCallIdDirect] = useState(params.itemName); + const [rootCallId, setRootCallId] = useState(params.itemName); useEffect(() => { - setCallIdDirect(params.itemName); + setRootCallId(params.itemName); }, [params.itemName]); + const [descendentCallId, setDescendentCallId] = useState( + getOptionalString(query, DESCENDENT_CALL_ID_PARAM) + ); + useEffect(() => { + setDescendentCallId(getOptionalString(query, DESCENDENT_CALL_ID_PARAM)); + }, [query]); + + const [showFeedback, setShowFeedback] = useState( + getOptionalBoolean(query, SHOW_FEEDBACK_PARAM) + ); + useEffect(() => { + setShowFeedback(getOptionalBoolean(query, SHOW_FEEDBACK_PARAM)); + }, [query]); + + const [hideTracetree, setHideTracetree] = useState( + getOptionalBoolean(query, HIDE_TRACETREE_PARAM) + ); + useEffect(() => { + setHideTracetree(getOptionalBoolean(query, HIDE_TRACETREE_PARAM)); + }, [query]); + const debouncedHistoryPush = useMemo(() => { return debounce((path: string) => { - history.push(path); - }, 1000); + if (history.location.pathname !== path) { + history.push(path); + } + }, 500); }, [history]); - const setCallId = useCallback( - (newCallId: string) => { - setCallIdDirect(newCallId); + useEffect(() => { + debouncedHistoryPush( + currentRouter.callUIUrl( + params.entity, + params.project, + '', + rootCallId, + descendentCallId, + hideTracetree, + showFeedback + ) + ); + return () => { + debouncedHistoryPush.cancel(); + }; + }, [ + currentRouter, + debouncedHistoryPush, + params.entity, + params.project, + rootCallId, + descendentCallId, + showFeedback, + hideTracetree, + ]); + + return { + entity: params.entity, + project: params.project, + rootCallId, + descendentCallId, + showFeedback, + hideTracetree, + setRootCallId, + setDescendentCallId, + setShowFeedback, + setHideTracetree, + }; +}; - // TODO: Handle this navigation more gracefully - ideally - // we implement a generalized state management system for - // navigating between different views - debouncedHistoryPush( - currentRouter.callUIUrl( - params.entity, - params.project, - '', - newCallId, - '', - true - ) - ); - }, - [currentRouter, debouncedHistoryPush, params.entity, params.project] - ); +// TODO(tim/weaveflow_improved_nav): Generalize this +const CallPageBinding = () => { + useCallPeekRedirect(); + const { + entity, + project, + rootCallId, + descendentCallId, + showFeedback, + hideTracetree, + setRootCallId, + setDescendentCallId, + setShowFeedback, + setHideTracetree, + } = useURLBackedCallPageState(); return ( ); }; diff --git a/weave-js/src/components/PagePanelComponents/Home/Browse3/context.tsx b/weave-js/src/components/PagePanelComponents/Home/Browse3/context.tsx index 1f0565fb829f..1f6186ecc5e1 100644 --- a/weave-js/src/components/PagePanelComponents/Home/Browse3/context.tsx +++ b/weave-js/src/components/PagePanelComponents/Home/Browse3/context.tsx @@ -100,8 +100,9 @@ export const browse2Context = { projectName: string, traceId: string, callId: string, - path?: string | null, - tracetree?: boolean + descendentCallId?: string, + hideTracetree?: boolean, + showFeedback?: boolean ) => { return `/${entityName}/${projectName}/trace/${traceId}/${callId}`; }, @@ -350,20 +351,20 @@ export const browse3ContextGen = ( projectName: string, traceId: string, callId: string, - path?: string | null, - tracetree?: boolean, - feedbackExpand?: boolean + descendentCallId?: string, + hideTracetree?: boolean, + showFeedback?: boolean ) => { let url = `${projectRoot(entityName, projectName)}/calls/${callId}`; const params = new URLSearchParams(); - if (path) { - params.set(PATH_PARAM, path); + if (descendentCallId) { + params.set(DESCENDENT_CALL_ID_PARAM, descendentCallId); } - if (tracetree !== undefined) { - params.set(TRACETREE_PARAM, tracetree ? '1' : '0'); + if (hideTracetree !== undefined) { + params.set(HIDE_TRACETREE_PARAM, hideTracetree ? '1' : '0'); } - if (feedbackExpand !== undefined) { - params.set(FEEDBACK_EXPAND_PARAM, feedbackExpand ? '1' : '0'); + if (showFeedback !== undefined) { + params.set(SHOW_FEEDBACK_PARAM, showFeedback ? '1' : '0'); } if (params.toString()) { url += '?' + params.toString(); @@ -540,9 +541,9 @@ type RouteType = { projectName: string, traceId: string, callId: string, - path?: string | null, - tracetree?: boolean, - feedbackExpand?: boolean + descendentCallId?: string, + hideTracetree?: boolean, + showFeedback?: boolean ) => string; tracesUIUrl: (entityName: string, projectName: string) => string; callsUIUrl: ( @@ -623,9 +624,9 @@ const useSetSearchParam = () => { }; export const PEEK_PARAM = 'peekPath'; -export const TRACETREE_PARAM = 'tracetree'; -export const FEEDBACK_EXPAND_PARAM = 'feedbackExpand'; -export const PATH_PARAM = 'path'; +export const HIDE_TRACETREE_PARAM = 'hideTracetree'; +export const SHOW_FEEDBACK_PARAM = 'showFeedback'; +export const DESCENDENT_CALL_ID_PARAM = 'descendentCallId'; export const baseContext = browse3ContextGen( (entityName: string, projectName: string) => { diff --git a/weave-js/src/components/PagePanelComponents/Home/Browse3/pages/CallPage/CallPage.tsx b/weave-js/src/components/PagePanelComponents/Home/Browse3/pages/CallPage/CallPage.tsx index beb268d40770..52c822b06997 100644 --- a/weave-js/src/components/PagePanelComponents/Home/Browse3/pages/CallPage/CallPage.tsx +++ b/weave-js/src/components/PagePanelComponents/Home/Browse3/pages/CallPage/CallPage.tsx @@ -9,7 +9,6 @@ import React, { useRef, useState, } from 'react'; -import {useHistory} from 'react-router-dom'; import {makeRefCall} from '../../../../../../util/refs'; import {Button} from '../../../../../Button'; @@ -17,12 +16,7 @@ import {Tailwind} from '../../../../../Tailwind'; import {Browse2OpDefCode} from '../../../Browse2/Browse2OpDefCode'; import {TableRowSelectionContext} from '../../../TableRowSelectionContext'; import {TraceNavigator} from '../../components/TraceNavigator/TraceNavigator'; -import { - FEEDBACK_EXPAND_PARAM, - TRACETREE_PARAM, - useWeaveflowCurrentRouteContext, - WeaveflowPeekContext, -} from '../../context'; +import {WeaveflowPeekContext} from '../../context'; import {FeedbackGrid} from '../../feedback/FeedbackGrid'; import {ScorerFeedbackGrid} from '../../feedback/ScorerFeedbackGrid'; import {FeedbackSidebar} from '../../feedback/StructuredFeedback/FeedbackSidebar'; @@ -36,7 +30,6 @@ import { SimplePageLayoutWithHeader, } from '../common/SimplePageLayout'; import {CompareEvaluationsPageContent} from '../CompareEvaluationsPage/CompareEvaluationsPage'; -import {useURLSearchParamsDict} from '../util'; import {useWFHooks} from '../wfReactInterface/context'; import {CallSchema} from '../wfReactInterface/wfDataModelHooksInterface'; import {CallChat} from './CallChat'; @@ -46,20 +39,37 @@ import {CallSummary} from './CallSummary'; import {PaginationControls} from './PaginationControls'; import {TabUseCall} from './TabUseCall'; -export const CallPage: FC<{ +type CallPageProps = { entity: string; project: string; - callId: string; - setCallId: (callId: string) => void; - path?: string; -}> = props => { + rootCallId: string; + setRootCallId: (callId: string) => void; + descendentCallId?: string; + setDescendentCallId: (descendentCallId: string | undefined) => void; + hideTracetree?: boolean; + setHideTracetree: (hideTracetree: boolean | undefined) => void; + showFeedback?: boolean; + setShowFeedback: (showFeedback: boolean | undefined) => void; +}; + +type CallPageInnerProps = CallPageProps & { + descendentCallId: string; + setDescendentCallId: (descendentCallId: string) => void; + descendentCall: CallSchema; +}; + +export const CallPage: FC = props => { const {useCall} = useWFHooks(); - const call = useCall({ - entity: props.entity, - project: props.project, - callId: props.callId, - }); + const descendentCallId = props.descendentCallId ?? props.rootCallId; + const call = useCall( + { + entity: props.entity, + project: props.project, + callId: descendentCallId, + } + // , {includeCosts: true} + ); // TODO: CLean this up! const lastResult = useRef(call.result); useEffect(() => { @@ -75,8 +85,8 @@ export const CallPage: FC<{ return ( ); } @@ -87,8 +97,8 @@ export const CallPage: FC<{ return ( ); } @@ -230,73 +240,28 @@ const useCallTabs = (call: CallSchema) => { ]; }; -const CallPageInnerVertical: FC<{ - call: CallSchema; - callId: string; - setCallById: (callId: string) => void; - path?: string; -}> = ({call, callId, setCallById: setCallId, path}) => { +const CallPageInnerVertical: FC = ({ + descendentCall: call, + descendentCallId: callId, + setRootCallId: setCallId, + setHideTracetree, + setShowFeedback, + showFeedback, + hideTracetree, +}) => { useViewTraceEvent(call); - const history = useHistory(); - const currentRouter = useWeaveflowCurrentRouteContext(); - - const query = useURLSearchParamsDict(); const showTraceTree = - TRACETREE_PARAM in query - ? query[TRACETREE_PARAM] === '1' - : !isEvaluateOp(call.spanName); - const showFeedbackExpand = - FEEDBACK_EXPAND_PARAM in query - ? query[FEEDBACK_EXPAND_PARAM] === '1' - : false; + hideTracetree != null ? !hideTracetree : !isEvaluateOp(call.spanName); + const showFeedbackExpand = showFeedback != null ? showFeedback : false; const onToggleTraceTree = useCallback(() => { - history.replace( - currentRouter.callUIUrl( - call.entity, - call.project, - call.traceId, - callId, - path, - !showTraceTree, - showFeedbackExpand ? true : undefined - ) - ); - }, [ - callId, - call.entity, - call.project, - call.traceId, - currentRouter, - history, - path, - showTraceTree, - showFeedbackExpand, - ]); + setHideTracetree(showTraceTree); + }, [setHideTracetree, showTraceTree]); const onToggleFeedbackExpand = useCallback(() => { - history.replace( - currentRouter.callUIUrl( - call.entity, - call.project, - call.traceId, - callId, - path, - showTraceTree, - !showFeedbackExpand ? true : undefined - ) - ); - }, [ - history, - currentRouter, - call.entity, - call.project, - call.traceId, - callId, - path, - showTraceTree, - showFeedbackExpand, - ]); + setShowFeedback(!showFeedbackExpand); + }, [setShowFeedback, showFeedbackExpand]); + const {humanAnnotationSpecs, specsLoading} = useHumanAnnotationSpecs( call.entity, call.project @@ -313,6 +278,7 @@ const CallPageInnerVertical: FC<{ // callId: selectedCall.callId, // }); const callCompleteWithCosts = callComplete; + // console.log(callCompleteWithCosts) // useMemo(() => { // if (callComplete.result?.traceCall == null) { // return callComplete.result; @@ -334,15 +300,15 @@ const CallPageInnerVertical: FC<{ // }; // }, [callComplete.result, selectedCall]); - const assumeCallIsSelectedCall = path == null || path === ''; + // const assumeCallIsSelectedCall = path == null || path === ''; const [currentCall, setCurrentCall] = useState(call); const callLoading = call.callId !== callId; - useEffect(() => { - if (assumeCallIsSelectedCall) { - setCurrentCall(selectedCall); - } - }, [assumeCallIsSelectedCall, selectedCall]); + // useEffect(() => { + // if (assumeCallIsSelectedCall) { + // setCurrentCall(selectedCall); + // } + // }, [assumeCallIsSelectedCall, selectedCall]); useEffect(() => { if (callCompleteWithCosts != null) { @@ -367,7 +333,7 @@ const CallPageInnerVertical: FC<{ alignItems: 'center', }}> {showPaginationControls && ( - + )}