JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(trajectory): stabilize timeline navigation
alan_cheng1980ee4f9acc
diff --git a/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.module.css b/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.module.cssindex 9b71bc95bc..4bb607967e 100644--- a/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.module.css+++ b/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.module.css@@ -64,6 +64,10 @@cursor: grabbing;}+.track[data-pannable='true']:not([data-panning='true']) {+ cursor: grab;+}+.earlierHistory {position: absolute;z-index: 5;diff --git a/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.tsx b/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.tsxindex b578193ccc..b2dada50ae 100644--- a/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.tsx+++ b/jiuwenswarm/channels/web/frontend/src/features/trajectory/client/TrajectoryTimeline.tsx@@ -7,8 +7,8 @@*/import {- memo, useEffect, useMemo, useRef, useState, type CSSProperties, type KeyboardEvent,- type PointerEvent,+ memo, useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties,+ type KeyboardEvent, type PointerEvent,} from 'react'import { Tooltip } from '../primitives/index.ts'import type { TrajectoryTurnModel } from '../trajectory/model.ts'@@ -17,10 +17,12 @@ import { formatTokenCount, liveElapsedSeconds } from '../trajectory/record.ts'import {deriveTrajectoryTimeline,formatTimelineOffset,+ panTrajectoryTimelineViewport,type TrajectoryTimelineMode,type TrajectoryTimelineSegment,type TrajectoryTimelineSpan,type TrajectoryTimeRange,+ zoomTrajectoryTimelineViewport,} from '../trajectory/timeline.ts'import css from './TrajectoryTimeline.module.css'@@ -32,6 +34,7 @@ const EDGE_PAN_ZONE_FRACTION = 0.08const EDGE_PAN_STEP_FRACTION = 0.025const MAXIMUM_EDGE_PAN_PX = 32const TIMELINE_TOOLTIP_DELAY_MS = 500+const WHEEL_SETTLE_MS = 140interface TimelineRecordDetail {decodingMs?: number@@ -58,9 +61,13 @@ interface HoverPoint {interface PanGesture {anchorClientX: numberanchorStart: number+ anchorTime: number+ button: number+ duration: numbermoved: booleanpannable: booleanpointerId: number+ spanKey: string | null}function assistantTimingDetail(@@ -284,6 +291,50 @@ function rangeFraction(}}+function viewportGeometry(+ model: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+): { domainDuration: number; domainStart: number; fullDuration: number } {+ const fullDuration = Math.max(1, model.end - model.start)+ const domainDuration = viewport === null+ ? fullDuration+ : Math.min(fullDuration, Math.max(1, viewport.end - viewport.start))+ const domainStart = viewport === null+ ? model.start+ : Math.min(Math.max(viewport.start, model.start), model.end - domainDuration)+ return { domainDuration, domainStart, fullDuration }+}++function projectedDomainStyle(+ model: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+): CSSProperties {+ const { domainDuration, domainStart, fullDuration } = viewportGeometry(model, viewport)+ return {+ '--trajectory-domain-left': `${-(domainStart - model.start) / domainDuration * 100}%`,+ '--trajectory-domain-width': `${fullDuration / domainDuration * 100}%`,+ } as CSSProperties+}++function applyProjectedDomain(+ track: HTMLElement,+ model: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+): void {+ const { domainDuration, domainStart, fullDuration } = viewportGeometry(model, viewport)+ track.style.setProperty(+ '--trajectory-domain-left',+ `${-(domainStart - model.start) / domainDuration * 100}%`,+ )+ track.style.setProperty(+ '--trajectory-domain-width',+ `${fullDuration / domainDuration * 100}%`,+ )+ track.querySelectorAll<HTMLElement>('[data-animate-viewport]').forEach((element) => {+ element.removeAttribute('data-animate-viewport')+ })+}+function LaneLabels({ mode }: { mode: TrajectoryTimelineMode }) {if (mode === 'tokens') {return (@@ -339,7 +390,7 @@ function EarlierHistoryBoundary({)}-/** Overview renderer with drag ranges, click-sized focus, and Escape reset. */+/** Overview renderer with deferred wheel zoom, horizontal panning, range focus, and reset. */export const TrajectoryTimeline = memo(function TrajectoryTimeline({turns,mode,@@ -376,6 +427,8 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({const panRef = useRef<PanGesture | null>(null)const rootRef = useRef<HTMLElement | null>(null)const trackRef = useRef<HTMLDivElement | null>(null)+ const pendingViewportRef = useRef<TrajectoryTimeRange | null | undefined>(undefined)+ const wheelCommitTimerRef = useRef<number | null>(null)const [draft, setDraft] = useState<TrajectoryTimeRange | null>(null)const [hover, setHover] = useState<HoverPoint | null>(null)const loadingEarlierRef = useRef<Promise<boolean> | null>(null)@@ -387,6 +440,12 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({const [panning, setPanning] = useState(false)const [viewport, setViewport] = useState<TrajectoryTimeRange | null>(null)const [animateViewport, setAnimateViewport] = useState(false)+ const modelRef = useRef(model)+ const viewportRef = useRef(viewport)+ const modeRef = useRef(mode)+ modelRef.current = model+ viewportRef.current = viewport+ modeRef.current = modeuseEffect(() => {if (model !== null@@ -404,6 +463,11 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({? null: current)}, [model])+ useLayoutEffect(() => {+ const pending = pendingViewportRef.current+ if (model === null || pending === undefined || trackRef.current === null) return+ applyProjectedDomain(trackRef.current, model, pending)+ }, [model])useEffect(() => {if (model === null || selectedIndex === null) returnconst selectedSpan = model.spans.find(span => spanCovers(span, selectedIndex))@@ -427,19 +491,10 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({return { start: nextStart, end: nextStart + duration }})}, [model, selectedIndex])- const fullDuration = Math.max(1, (model?.end ?? 0) - (model?.start ?? 0))- const viewportDuration = Math.min(- fullDuration,- Math.max(1, (viewport?.end ?? 0) - (viewport?.start ?? 0)),- )- const viewportStart = model === null || viewport === null- ? model?.start ?? 0- : Math.min(- Math.max(viewport.start, model.start),- model.end - viewportDuration,- )- const domainDuration = viewport === null ? fullDuration : viewportDuration- const domainStart = viewport === null ? model?.start ?? 0 : viewportStart+ const geometry = model === null+ ? { domainDuration: 1, domainStart: 0, fullDuration: 1 }+ : viewportGeometry(model, viewport)+ const { domainDuration, domainStart, fullDuration } = geometryconst showsEarlierBoundary = hasEarlierRecords&& model !== null&& domainStart === model.start@@ -457,13 +512,9 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({setLocalOlderHistoryLoading(false)})}- const projectedDomainStyle = model === null+ const domainStyle = model === null? undefined- : {- '--trajectory-domain-left':- `${-(domainStart - model.start) / domainDuration * 100}%`,- '--trajectory-domain-width': `${fullDuration / domainDuration * 100}%`,- } as CSSProperties+ : projectedDomainStyle(model, viewport)const committed = model === null || range === null? null: rangeFraction(range, domainStart, domainDuration, model.start, model.end)@@ -476,34 +527,59 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({const root = rootRef.currentif (root === null) returnconst onWheel = (event: globalThis.WheelEvent): void => {- event.preventDefault()const track = trackRef.current- if (track === null || model === null) return- setAnimateViewport(false)+ const currentModel = modelRef.current+ if (track === null || currentModel === null) returnconst rect = track.getBoundingClientRect()- const anchorFraction =- clampFraction((event.clientX - rect.left) / Math.max(1, rect.width))- const nextDuration = Math.min(- fullDuration,- Math.max(- Math.min(minimumZoomDomain(mode), fullDuration),- domainDuration * Math.exp(event.deltaY * 0.0015),- ),- )- if (nextDuration >= fullDuration * 0.999) {- setViewport(null)- return+ const unit = event.deltaMode === globalThis.WheelEvent.DOM_DELTA_LINE+ ? 16+ : event.deltaMode === globalThis.WheelEvent.DOM_DELTA_PAGE ? rect.width : 1+ const horizontal = event.shiftKey && Math.abs(event.deltaX) < Math.abs(event.deltaY)+ ? event.deltaY+ : event.deltaX+ const pansHorizontally = event.shiftKey+ || Math.abs(event.deltaX) > Math.abs(event.deltaY)+ const currentViewport = pendingViewportRef.current === undefined+ ? viewportRef.current+ : pendingViewportRef.current+ const nextViewport = pansHorizontally+ ? panTrajectoryTimelineViewport(+ currentModel,+ currentViewport,+ horizontal * unit / Math.max(1, rect.width),+ )+ : zoomTrajectoryTimelineViewport(+ currentModel,+ currentViewport,+ clampFraction((event.clientX - rect.left) / Math.max(1, rect.width)),+ event.deltaY * unit,+ minimumZoomDomain(modeRef.current),+ )+ if (pansHorizontally && currentViewport === null) return+ event.preventDefault()+ pendingViewportRef.current = nextViewport+ applyProjectedDomain(track, currentModel, nextViewport)+ if (wheelCommitTimerRef.current !== null) {+ window.clearTimeout(wheelCommitTimerRef.current)}- const anchorTime = domainStart + anchorFraction * domainDuration- const nextStart = Math.min(- Math.max(anchorTime - anchorFraction * nextDuration, model.start),- model.end - nextDuration,- )- setViewport({ start: nextStart, end: nextStart + nextDuration })+ wheelCommitTimerRef.current = window.setTimeout(() => {+ const pending = pendingViewportRef.current+ wheelCommitTimerRef.current = null+ pendingViewportRef.current = undefined+ if (pending === undefined) return+ setAnimateViewport(false)+ setViewport(pending)+ }, WHEEL_SETTLE_MS)}root.addEventListener('wheel', onWheel, { passive: false })- return () => { root.removeEventListener('wheel', onWheel) }- }, [domainDuration, domainStart, fullDuration, mode, model])+ return () => {+ root.removeEventListener('wheel', onWheel)+ if (wheelCommitTimerRef.current !== null) {+ window.clearTimeout(wheelCommitTimerRef.current)+ wheelCommitTimerRef.current = null+ }+ }+ }, [])if (model === null) {return (@@ -555,19 +631,36 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({}const onPointerDown = (event: PointerEvent<HTMLDivElement>) => {- if (event.button === 2) {+ const currentViewport = pendingViewportRef.current === undefined+ ? viewport+ : pendingViewportRef.current+ const currentGeometry = viewportGeometry(model, currentViewport)+ const startsPan = event.button === 1+ || event.button === 2+ || (event.button === 0 && currentViewport !== null && !event.shiftKey)+ if (startsPan) {panRef.current = {anchorClientX: event.clientX,- anchorStart: domainStart,+ anchorStart: currentGeometry.domainStart,+ anchorTime: currentGeometry.domainStart+ + fractionAt(event) * currentGeometry.domainDuration,+ button: event.button,+ duration: currentGeometry.domainDuration,moved: false,- pannable: viewport !== null,+ pannable: currentViewport !== null,pointerId: event.pointerId,+ spanKey: spanKeyAt(event),+ }+ if (wheelCommitTimerRef.current !== null) {+ window.clearTimeout(wheelCommitTimerRef.current)+ wheelCommitTimerRef.current = null}- if (viewport !== null) setAnimateViewport(false)- setPanning(true)+ setHover(null)+ if (currentViewport !== null) setPanning(true)if (typeof event.currentTarget.setPointerCapture === 'function') {event.currentTarget.setPointerCapture(event.pointerId)}+ event.preventDefault()return}if (event.button !== 0) return@@ -590,22 +683,24 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({const onPointerMove = (event: PointerEvent<HTMLDivElement>) => {const rect = event.currentTarget.getBoundingClientRect()- const fraction = fractionAt(event)- setHover({ fraction, recordIndex: recordIndexAt(event) })const pan = panRef.currentif (pan !== null && pan.pointerId === event.pointerId) {if (Math.abs(event.clientX - pan.anchorClientX) >= MINIMUM_DRAG_PX) {pan.moved = true}- if (!pan.pannable) return+ if (!pan.pannable || !pan.moved) returnconst delta = (event.clientX - pan.anchorClientX) / Math.max(1, rect.width)- const nextStart = Math.min(- Math.max(pan.anchorStart - delta * domainDuration, model.start),- model.end - domainDuration,+ const nextViewport = panTrajectoryTimelineViewport(+ model,+ { start: pan.anchorStart, end: pan.anchorStart + pan.duration },+ -delta,)- setViewport({ start: nextStart, end: nextStart + domainDuration })+ pendingViewportRef.current = nextViewport+ applyProjectedDomain(event.currentTarget, model, nextViewport)return}+ const fraction = fractionAt(event)+ setHover({ fraction, recordIndex: recordIndexAt(event) })const drag = dragRef.currentif (drag === null || drag.pointerId !== event.pointerId) returnlet nextDomainStart = domainStart@@ -650,7 +745,52 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({|| Math.abs(event.clientX - pan.anchorClientX) >= MINIMUM_DRAG_PXpanRef.current = nullsetPanning(false)- if (!moved) onRangeChange(null)+ if (moved && pan.pannable) {+ const pending = pendingViewportRef.current+ pendingViewportRef.current = undefined+ if (pending !== undefined) {+ setAnimateViewport(false)+ setViewport(pending)+ }+ return+ }+ pendingViewportRef.current = undefined+ applyProjectedDomain(event.currentTarget, model, viewport)+ if (pan.button === 2) {+ onRangeChange(null)+ return+ }+ if (pan.button !== 0) return+ const clickedSpan = pan.spanKey === null+ ? undefined+ : model.spans.find(span => spanKey(span) === pan.spanKey)+ if (clickedSpan !== undefined) {+ if (clickedSpan.segment === 'input') {+ onRangeChange({ start: clickedSpan.start, end: clickedSpan.end })+ onRecordFocus?.(clickedSpan.coveredIndexes?.[0] ?? clickedSpan.index)+ return+ }+ onRangeChange(null)+ onRecordSelect?.(clickedSpan.index)+ return+ }+ const clickRange = centeredRange(+ pan.anchorTime,+ Math.min(pan.duration, fullDuration / model.spans.length),+ model.start,+ model.end,+ )+ commit(clickRange)+ const nearest = model.spans.reduce((candidate, span) => {+ const candidateDistance = pan.anchorTime < candidate.start+ ? candidate.start - pan.anchorTime+ : pan.anchorTime > candidate.end ? pan.anchorTime - candidate.end : 0+ const spanDistance = pan.anchorTime < span.start+ ? span.start - pan.anchorTime+ : pan.anchorTime > span.end ? pan.anchorTime - span.end : 0+ return spanDistance < candidateDistance ? span : candidate+ })+ onRecordFocus?.(nearest.index)return}const drag = dragRef.current@@ -710,6 +850,14 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({const onPointerCancel = () => {dragRef.current = nullpanRef.current = null+ pendingViewportRef.current = undefined+ if (wheelCommitTimerRef.current !== null) {+ window.clearTimeout(wheelCommitTimerRef.current)+ wheelCommitTimerRef.current = null+ }+ if (trackRef.current !== null && modelRef.current !== null) {+ applyProjectedDomain(trackRef.current, modelRef.current, viewportRef.current)+ }setDraft(null)setHover(null)setPanning(false)@@ -723,7 +871,9 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({ref={trackRef}className={css.track}data-panning={panning || undefined}- aria-label="Timeline overview; drag horizontally to focus events"+ data-pannable={viewport !== null || undefined}+ aria-label="Timeline overview; scroll or drag horizontally to pan, wheel to zoom, Shift-drag to focus events"+ style={domainStyle}tabIndex={0}onKeyDown={onKeyDown}onPointerDown={onPointerDown}@@ -784,13 +934,9 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({className={css.turnBoundaries}data-animate-viewport={animateViewport || undefined}aria-hidden="true"- style={projectedDomainStyle}>{model.turnBoundaries- .filter(boundary =>- boundary.time > model.start- && boundary.time >= domainStart- && boundary.time <= domainStart + domainDuration)+ .filter(boundary => boundary.time > model.start).map(boundary => (<spanclassName={css.turnBoundary}@@ -807,13 +953,8 @@ export const TrajectoryTimeline = memo(function TrajectoryTimeline({className={css.lanes}data-animate-viewport={animateViewport || undefined}data-timeline-domain- style={projectedDomainStyle}>- {model.spans- .filter(span =>- spanCovers(span, selectedIndex)- || (span.end >= domainStart && span.start <= domainStart + domainDuration))- .map((span) => {+ {model.spans.map((span) => {const left = (span.start - model.start) / fullDurationconst width = (span.end - span.start) / fullDurationconst widthPercent = width * 100diff --git a/jiuwenswarm/channels/web/frontend/src/features/trajectory/trajectory/timeline.ts b/jiuwenswarm/channels/web/frontend/src/features/trajectory/trajectory/timeline.tsindex 7f654c77c8..e05dcbb07f 100644--- a/jiuwenswarm/channels/web/frontend/src/features/trajectory/trajectory/timeline.ts+++ b/jiuwenswarm/channels/web/frontend/src/features/trajectory/trajectory/timeline.ts@@ -56,6 +56,80 @@ export interface TrajectoryTimelineModel extends TrajectoryTimeRange {turnBoundaries: readonly TrajectoryTimelineTurnBoundary[]}+function clamp(value: number, minimum: number, maximum: number): number {+ return Math.min(maximum, Math.max(minimum, value))+}++function boundedViewport(+ fullRange: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+): TrajectoryTimeRange {+ const fullDuration = Math.max(0, fullRange.end - fullRange.start)+ if (viewport === null) return { ...fullRange }+ const duration = clamp(viewport.end - viewport.start, 0, fullDuration)+ const start = clamp(viewport.start, fullRange.start, fullRange.end - duration)+ return { start, end: start + duration }+}++/**+ * Zoom a timeline viewport around the pointer without moving its anchored time.+ * @param fullRange - Complete timeline domain.+ * @param viewport - Current viewport, or null for the complete domain.+ * @param anchorFraction - Pointer position inside the viewport, in the range zero to one.+ * @param wheelDelta - Normalized vertical wheel delta; negative values zoom in.+ * @param minimumDuration - Smallest duration allowed for this projection.+ * @returns The next bounded viewport, or null when zoomed fully out.+ */+export function zoomTrajectoryTimelineViewport(+ fullRange: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+ anchorFraction: number,+ wheelDelta: number,+ minimumDuration: number,+): TrajectoryTimeRange | null {+ const fullDuration = Math.max(0, fullRange.end - fullRange.start)+ if (fullDuration === 0) return null+ const current = boundedViewport(fullRange, viewport)+ const currentDuration = current.end - current.start+ const nextDuration = clamp(+ currentDuration * Math.exp(wheelDelta * 0.0015),+ Math.min(minimumDuration, fullDuration),+ fullDuration,+ )+ if (nextDuration >= fullDuration * 0.999) return null+ const anchor = clamp(anchorFraction, 0, 1)+ const anchorTime = current.start + anchor * currentDuration+ const nextStart = clamp(+ anchorTime - anchor * nextDuration,+ fullRange.start,+ fullRange.end - nextDuration,+ )+ return { start: nextStart, end: nextStart + nextDuration }+}++/**+ * Pan a zoomed timeline viewport by a fraction of its visible width.+ * @param fullRange - Complete timeline domain.+ * @param viewport - Current viewport, or null for the complete domain.+ * @param deltaFraction - Signed horizontal travel in viewport widths.+ * @returns The next bounded viewport, or null when the full domain is visible.+ */+export function panTrajectoryTimelineViewport(+ fullRange: TrajectoryTimeRange,+ viewport: TrajectoryTimeRange | null,+ deltaFraction: number,+): TrajectoryTimeRange | null {+ if (viewport === null) return null+ const current = boundedViewport(fullRange, viewport)+ const duration = current.end - current.start+ const nextStart = clamp(+ current.start + deltaFraction * duration,+ fullRange.start,+ fullRange.end - duration,+ )+ return { start: nextStart, end: nextStart + duration }+}+/*** Format a timeline duration as an integer-millisecond label.* @param milliseconds - Non-negative duration in milliseconds.diff --git a/jiuwenswarm/channels/web/frontend/tests/trajectoryTimeline.test.mjs b/jiuwenswarm/channels/web/frontend/tests/trajectoryTimeline.test.mjsindex 4e9b98d941..5fdc4bd178 100644--- a/jiuwenswarm/channels/web/frontend/tests/trajectoryTimeline.test.mjs+++ b/jiuwenswarm/channels/web/frontend/tests/trajectoryTimeline.test.mjs@@ -5,8 +5,10 @@ import test from 'node:test';import {deriveTrajectoryTimeline,+ panTrajectoryTimelineViewport,resolveTimelineMode,trajectoryTimelineFocusIndexes,+ zoomTrajectoryTimelineViewport,} from '../node_modules/.cache/trajectory-timeline/timeline.mjs';function cell(index, kind, extra = {}) {@@ -204,3 +206,48 @@ test('token cost wins over the recorded-duration and complete-time switches', ()'time',);});++test('zoom keeps the pointed timeline time anchored and respects the minimum width', () => {+ const next = zoomTrajectoryTimelineViewport(+ { start: 0, end: 1_000 },+ null,+ 0.75,+ -500,+ 100,+ );++ assert.ok(next !== null);+ assert.ok(next.end - next.start >= 100);+ assert.ok(Math.abs(next.start + 0.75 * (next.end - next.start) - 750) < 0.000_001);+});++test('zooming back to the complete domain clears the viewport', () => {+ assert.equal(+ zoomTrajectoryTimelineViewport(+ { start: 0, end: 1_000 },+ { start: 250, end: 750 },+ 0.5,+ 10_000,+ 100,+ ),+ null,+ );+});++test('horizontal scrolling pans a zoomed viewport and clamps both edges', () => {+ const fullRange = { start: 0, end: 1_000 };++ assert.deepEqual(+ panTrajectoryTimelineViewport(fullRange, { start: 200, end: 600 }, 0.25),+ { start: 300, end: 700 },+ );+ assert.deepEqual(+ panTrajectoryTimelineViewport(fullRange, { start: 200, end: 600 }, -10),+ { start: 0, end: 400 },+ );+ assert.deepEqual(+ panTrajectoryTimelineViewport(fullRange, { start: 200, end: 600 }, 10),+ { start: 600, end: 1_000 },+ );+ assert.equal(panTrajectoryTimelineViewport(fullRange, null, 0.25), null);+});