OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
feat(dashboard): two-column profiles, consistent dropdowns and UI fixes (#634)
Carl-Gerhard Lindesvärd6e6ec17b5eb3
diff --git a/apps/start/src/components/ai/ai-filters.tsx b/apps/start/src/components/ai/ai-filters.tsxindex 47a7bb1f7..550e77b62 100644--- a/apps/start/src/components/ai/ai-filters.tsx+++ b/apps/start/src/components/ai/ai-filters.tsx@@ -1,77 +1,20 @@import { useQuery } from '@tanstack/react-query';-import { AlertTriangleIcon, ChevronDownIcon, XIcon } from 'lucide-react';+import {+ AlertTriangleIcon,+ CpuIcon,+ LayersIcon,+ ServerIcon,+ XIcon,+} from 'lucide-react';import { useOverviewOptions } from '@/components/overview/useOverviewOptions';import { Button } from '@/components/ui/button';import { AnimatedSearchInput } from '@/components/ui/data-table/data-table-toolbar';-import {- DropdownMenu,- DropdownMenuCheckboxItem,- DropdownMenuContent,- DropdownMenuLabel,- DropdownMenuSeparator,- DropdownMenuTrigger,-} from '@/components/ui/dropdown-menu';+import { ToolbarMultiSelect } from '@/components/ui/toolbar-select';import { useAiFilters } from '@/hooks/use-ai-filters';import { useTRPC } from '@/integrations/trpc/react';import { cn } from '@/utils/cn';-interface ValuePickerProps {- label: string;- values: { value: string; count: number }[];- selected: string[];- onChange: (next: string[]) => void;-}--function ValuePicker({ label, values, selected, onChange }: ValuePickerProps) {- const active = selected.length > 0;- return (- <DropdownMenu>- <DropdownMenuTrigger asChild>- <Button- className={cn(active && 'border-foreground/40')}- size="sm"- variant="outline"- >- {label}- {active && (- <span className="rounded bg-def-200 px-1 font-mono text-2xs">- {selected.length}- </span>- )}- <ChevronDownIcon className="size-3 opacity-60" />- </Button>- </DropdownMenuTrigger>- <DropdownMenuContent align="start" className="w-64">- <DropdownMenuLabel>{label}</DropdownMenuLabel>- <DropdownMenuSeparator />- {values.length === 0 && (- <div className="px-2 py-1.5 text-muted-foreground text-sm">- Nothing in this range- </div>- )}- {values.map((item) => (- <DropdownMenuCheckboxItem- checked={selected.includes(item.value)}- key={item.value}- onCheckedChange={(checked) =>- onChange(- checked- ? [...selected, item.value]- : selected.filter((value) => value !== item.value)- )- }- onSelect={(event) => event.preventDefault()}- >- <span className="min-w-0 flex-1 truncate">{item.value}</span>- <span className="ml-2 font-mono text-2xs text-muted-foreground">- {item.count}- </span>- </DropdownMenuCheckboxItem>- ))}- </DropdownMenuContent>- </DropdownMenu>- );-}+const NO_VALUES_TEXT = 'Nothing in this range';export function AiFilters({ projectId }: { projectId: string }) {const trpc = useTRPC();@@ -86,33 +29,38 @@ export function AiFilters({ projectId }: { projectId: string }) {return (<div className="flex flex-wrap items-center gap-2">- <ValuePicker+ <ToolbarMultiSelect+ emptyText={NO_VALUES_TEXT}+ icon={CpuIcon}label="Model"onChange={(models) =>setState({ models: models.length ? models : null })}+ options={values.data?.model ?? []}selected={state.models ?? []}- values={values.data?.model ?? []}/>- <ValuePicker+ <ToolbarMultiSelect+ emptyText={NO_VALUES_TEXT}+ icon={ServerIcon}label="Provider"onChange={(providers) =>setState({ providers: providers.length ? providers : null })}+ options={values.data?.provider ?? []}selected={state.providers ?? []}- values={values.data?.provider ?? []}/>- <ValuePicker+ <ToolbarMultiSelect+ emptyText={NO_VALUES_TEXT}+ icon={LayersIcon}label="Feature"onChange={(names) => setState({ names: names.length ? names : null })}+ options={values.data?.name ?? []}selected={state.names ?? []}- values={values.data?.name ?? []}/><ButtonclassName={cn(state.errors && 'border-destructive/60 text-destructive')}icon={AlertTriangleIcon}onClick={() => setState({ errors: state.errors ? null : true })}- size="sm"variant="outline">Errors only@@ -147,7 +95,7 @@ export function AiFilters({ projectId }: { projectId: string }) {/>)}{activeCount > 0 && (- <Button onClick={clear} size="sm" variant="ghost">+ <Button onClick={clear} variant="ghost">Clear</Button>)}@@ -157,7 +105,7 @@ export function AiFilters({ projectId }: { projectId: string }) {function ScopePill({ label, onClear }: { label: string; onClear: () => void }) {return (- <span className="row h-8 items-center gap-1 rounded-md border bg-def-100 px-2 text-xs">+ <span className="row h-9 items-center gap-1 rounded-md border bg-def-100 px-2 text-xs"><span className="max-w-48 truncate">{label}</span><buttonaria-label={`Remove ${label}`}diff --git a/apps/start/src/components/ai/ai-format.ts b/apps/start/src/components/ai/ai-format.tsindex b5cee0d4c..7b02cb9c4 100644--- a/apps/start/src/components/ai/ai-format.ts+++ b/apps/start/src/components/ai/ai-format.ts@@ -1,3 +1,5 @@+// The dashboard formats numbers in one locale (see `useNumber`), not the browser's.+const NUMBER_LOCALE = 'en-US';const SUB_CENT = 0.01;const SECOND_MS = 1000;const MINUTE_MS = 60_000;@@ -29,12 +31,12 @@ export function formatLatency(ms: number): string {export function formatTokens(value: number): string {if (value >= COMPACT_THRESHOLD) {- return new Intl.NumberFormat(undefined, {+ return new Intl.NumberFormat(NUMBER_LOCALE, {notation: 'compact',maximumFractionDigits: 1,}).format(value);}- return new Intl.NumberFormat().format(value);+ return new Intl.NumberFormat(NUMBER_LOCALE).format(value);}export const AI_TYPE_LABELS = {diff --git a/apps/start/src/components/ai/ai-metrics.tsx b/apps/start/src/components/ai/ai-metrics.tsxindex daf20f3d1..010ffb62b 100644--- a/apps/start/src/components/ai/ai-metrics.tsx+++ b/apps/start/src/components/ai/ai-metrics.tsx@@ -25,6 +25,8 @@ import { useTRPC } from '@/integrations/trpc/react';import { getChartColor } from '@/utils/theme';const PREV_LINE_COLOR = 'oklch(from var(--foreground) l c h / 0.4)';+// Room for sub-cent cost ticks such as "$0.005".+const Y_AXIS_WIDTH = 52;interface MetricDefinition {key: AiMetricKey;@@ -187,11 +189,11 @@ export function AiMetrics({ projectId }: { projectId: string }) {aspectRatio="auto"className="h-full"data={chartData}- margin={{ top: 0, right: 20, bottom: 40, left: 20 }}+ margin={{ top: 0, right: 20, bottom: 40, left: Y_AXIS_WIDTH }}xDataKey="date"><Grid horizontal />- <YAxis numTicks={4} />+ <YAxis formatValue={active.format} numTicks={4} /><XAxis /><Areaanimate={false}diff --git a/apps/start/src/components/ai/ai-usage-widget.tsx b/apps/start/src/components/ai/ai-usage-widget.tsxindex 70af6d12b..5e5527ff9 100644--- a/apps/start/src/components/ai/ai-usage-widget.tsx+++ b/apps/start/src/components/ai/ai-usage-widget.tsx@@ -5,6 +5,7 @@ import { formatCost, formatLatency, formatTokens } from './ai-format';import { ProjectLink } from '@/components/links';import { WidgetHead, WidgetTitle } from '@/components/overview/overview-widget';import { Skeleton } from '@/components/skeleton';+import { Badge } from '@/components/ui/badge';import { Widget, WidgetBody } from '@/components/widget';import { useTRPC } from '@/integrations/trpc/react';import { timeAgo } from '@/utils/date';@@ -141,27 +142,28 @@ function UsageBody({{query.isLoading && <Skeleton className="h-16 w-full" />}{data && (<>- <div className="grid grid-cols-3 gap-3">+ <dl className="grid grid-cols-2 gap-x-4 gap-y-3">{stats.map((stat) => (- <div className="col" key={stat.label}>- <span className="text-muted-foreground text-xs">+ <div className="col gap-0.5" key={stat.label}>+ <dt className="text-muted-foreground text-xs">{stat.label}- </span>- <span className="font-medium font-mono tabular-nums">+ </dt>+ <dd className="font-medium font-mono tabular-nums">{stat.value}- </span>+ </dd></div>))}- </div>+ </dl>{data.topModels.length > 0 && (<div className="row flex-wrap gap-1">{data.topModels.map((model) => (- <span- className="rounded border px-1.5 py-0.5 font-mono text-xs"+ <Badge+ className="max-w-full truncate"key={model}+ variant="secondary">{model}- </span>+ </Badge>))}</div>)}diff --git a/apps/start/src/components/events/event-list-item.tsx b/apps/start/src/components/events/event-list-item.tsxindex 9566146ed..79b7d74f5 100644--- a/apps/start/src/components/events/event-list-item.tsx+++ b/apps/start/src/components/events/event-list-item.tsx@@ -8,13 +8,17 @@ import { useAppParams } from '@/hooks/use-app-params';import { cn } from '@/utils/cn';import { getProfileName } from '@/utils/getters';-type EventListItemProps = IServiceEventMinimal | IServiceEvent;+type EventListItemProps = (IServiceEventMinimal | IServiceEvent) & {+ /** On the profile's own page the name would only repeat it. */+ hideProfile?: boolean;+};export function EventListItem(props: EventListItemProps) {const { openEvent } = useEventDrawer();const { organizationId, projectId } = useAppParams();const { createdAt, name, path, meta } = props;- const profile = 'profile' in props ? props.profile : null;+ const profile =+ 'profile' in props && !props.hideProfile ? props.profile : null;const renderName = () => {if (name === 'screen_view') {@@ -44,10 +48,10 @@ export function EventListItem(props: EventListItemProps) {}}type="button">- <div>+ <div className="min-w-0"><div className="flex items-center gap-4 text-left"><EventIcon meta={meta} name={name} size="sm" />- <span className="font-medium">{renderName()}</span>+ <span className="truncate font-medium">{renderName()}</span></div><div className="pl-10"><div className="flex origin-left scale-75 gap-1">@@ -57,7 +61,7 @@ export function EventListItem(props: EventListItemProps) {</div></div></div>- <div className="flex gap-4">+ <div className="flex shrink-0 gap-4 pl-4">{profile && (<Tooltiper asChild content={getProfileName(profile)}><Linkdiff --git a/apps/start/src/components/integrations/forms/webhook-integration.tsx b/apps/start/src/components/integrations/forms/webhook-integration.tsxindex f655361d0..36f506cd6 100644--- a/apps/start/src/components/integrations/forms/webhook-integration.tsx+++ b/apps/start/src/components/integrations/forms/webhook-integration.tsx@@ -213,7 +213,6 @@ export function WebhookIntegrationForm({label="Payload Format"><Combobox- variant="input"{...field}className="w-full"items={[diff --git a/apps/start/src/components/overview/overview-metric-card.tsx b/apps/start/src/components/overview/overview-metric-card.tsxindex 1a2ea4270..8acdee9b0 100644--- a/apps/start/src/components/overview/overview-metric-card.tsx+++ b/apps/start/src/components/overview/overview-metric-card.tsx@@ -106,7 +106,7 @@ export function OverviewMetricCard({{label}</span>{hasValue && (- <div className="absolute top-2.5 right-3">+ <div className="shrink-0"><PreviousDiffIndicatorPure{...diff}inverted={inverted}diff --git a/apps/start/src/components/profiles/latest-events.tsx b/apps/start/src/components/profiles/latest-events.tsxindex a07494394..f5d0a464f 100644--- a/apps/start/src/components/profiles/latest-events.tsx+++ b/apps/start/src/components/profiles/latest-events.tsx@@ -1,3 +1,4 @@+import { AI_INTERNAL_EVENT_NAMES } from '@openpanel/core/modules/ai/ai.constants';import { useQuery } from '@tanstack/react-query';import { useRouter } from '@tanstack/react-router';import { ActivityIcon } from 'lucide-react';@@ -13,11 +14,11 @@ import { Button } from '@/components/ui/button';import { Widget } from '@/components/widget';import { useTRPC } from '@/integrations/trpc/react';-type Props = {+interface Props {profileId: string;projectId: string;organizationId: string;-};+}export const LatestEvents = ({profileId,@@ -30,6 +31,13 @@ export const LatestEvents = ({trpc.event.events.queryOptions({projectId,profileId,+ filters: [+ {+ name: 'name',+ operator: 'isNot',+ value: [...AI_INTERNAL_EVENT_NAMES],+ },+ ],}));@@ -66,7 +74,7 @@ export const LatestEvents = ({<ScrollArea className="h-0 p-4" ref={scrollRef}>{query.data?.data?.map((event) => (<div className="mb-4" key={event.id}>- <EventListItem {...event} />+ <EventListItem {...event} hideProfile /></div>))}</ScrollArea>diff --git a/apps/start/src/components/profiles/profile-activity.tsx b/apps/start/src/components/profiles/profile-activity.tsxindex 2bcef9db0..3605c4958 100644--- a/apps/start/src/components/profiles/profile-activity.tsx+++ b/apps/start/src/components/profiles/profile-activity.tsx@@ -21,9 +21,11 @@ import { Tooltiper } from '../ui/tooltip';import { Widget, WidgetBody } from '@/components/widget';import { cn } from '@/utils/cn';-type Props = {+const VISIBLE_MONTH_OFFSETS = [1, 0];++interface Props {data: { count: number; date: string }[];-};+}function getOpacityLevel(count: number, maxCount: number): number {if (count === 0 || maxCount === 0) {@@ -130,8 +132,8 @@ export const ProfileActivity = ({ data }: Props) => {</WidgetAbsoluteButtons></WidgetHead><WidgetBody>- <div className="grid grid-cols-1 gap-4 md:grid-cols-2">- {[3, 2, 1, 0].map((offset) => (+ <div className="grid grid-cols-2 gap-4">+ {VISIBLE_MONTH_OFFSETS.map((offset) => (<MonthCalendardata={data}key={offset}diff --git a/apps/start/src/components/profiles/profile-charts.tsx b/apps/start/src/components/profiles/profile-charts.tsxindex 77562f020..6d9d3efcf 100644--- a/apps/start/src/components/profiles/profile-charts.tsx+++ b/apps/start/src/components/profiles/profile-charts.tsx@@ -1,13 +1,16 @@+import { AI_INTERNAL_EVENT_NAMES } from '@openpanel/core/modules/ai/ai.constants';import type { IReport } from '@openpanel/core/modules/report/report.constants';import { memo } from 'react';import { WidgetHead } from '../overview/overview-widget';import { ReportChart } from '@/components/report-chart';import { Widget, WidgetBody } from '@/components/widget';-type Props = {+const CHART_OPTIONS = { hideTable: true };++interface Props {profileId: string;projectId: string;-};+}export const ProfileCharts = memo(({ profileId, projectId }: Props) => {@@ -59,6 +62,12 @@ export const ProfileCharts = memo(operator: 'is',value: [profileId],},+ {+ id: 'name',+ name: 'name',+ operator: 'isNot',+ value: [...AI_INTERNAL_EVENT_NAMES],+ },],id: 'A',name: '*',@@ -81,20 +90,20 @@ export const ProfileCharts = memo(return (<>- <Widget className="col-span-6 md:col-span-3">+ <Widget><WidgetHead><span className="title">Page views</span></WidgetHead><WidgetBody>- <ReportChart report={pageViewsChart} />+ <ReportChart options={CHART_OPTIONS} report={pageViewsChart} /></WidgetBody></Widget>- <Widget className="col-span-6 md:col-span-3">+ <Widget><WidgetHead><span className="title">Events per day</span></WidgetHead><WidgetBody>- <ReportChart report={eventsChart} />+ <ReportChart options={CHART_OPTIONS} report={eventsChart} /></WidgetBody></Widget></>diff --git a/apps/start/src/components/profiles/profile-metrics.tsx b/apps/start/src/components/profiles/profile-metrics.tsxindex 00a38d51f..ec1954fc4 100644--- a/apps/start/src/components/profiles/profile-metrics.tsx+++ b/apps/start/src/components/profiles/profile-metrics.tsx@@ -7,14 +7,8 @@ interface Props {}// The card's 'min' unit takes seconds and renders them as a duration.+// First and last seen are in the properties list next to this.const PROFILE_METRICS = [- {- title: 'Total Events',- key: 'totalEvents',- unit: '',- inverted: false,- hideOnZero: false,- },{title: 'Sessions',key: 'sessions',@@ -23,75 +17,33 @@ const PROFILE_METRICS = [hideOnZero: false,},{- title: 'Page Views',- key: 'screenViews',+ title: 'Events',+ key: 'totalEvents',unit: '',inverted: false,hideOnZero: false,},{- title: 'Avg Events/Session',- key: 'avgEventsPerSession',+ title: 'Page views',+ key: 'screenViews',unit: '',inverted: false,hideOnZero: false,},{- title: 'Bounce Rate',- key: 'bounceRate',- unit: '%',- inverted: true,- hideOnZero: false,- },- {- title: 'Session Duration (Avg)',+ title: 'Avg session',key: 'durationAvg',unit: 'min',inverted: false,hideOnZero: false,},{- title: 'Session Duration (P90)',- key: 'durationP90',- unit: 'min',- inverted: false,- hideOnZero: false,- },- {- title: 'First seen',- key: 'firstSeen',- unit: 'timeAgo',- inverted: false,- hideOnZero: false,- },- {- title: 'Last seen',- key: 'lastSeen',- unit: 'timeAgo',- inverted: false,- hideOnZero: false,- },- {- title: 'Days Active',+ title: 'Days active',key: 'uniqueDaysActive',unit: '',inverted: false,hideOnZero: false,},- {- title: 'Conversion Events',- key: 'conversionEvents',- unit: '',- inverted: false,- hideOnZero: false,- },- {- title: 'Avg Time Between Sessions',- key: 'avgTimeBetweenSessions',- unit: 'min',- inverted: false,- hideOnZero: false,- },{title: 'Revenue',key: 'revenue',@@ -99,7 +51,15 @@ const PROFILE_METRICS = [inverted: false,hideOnZero: true,},+ {+ title: 'Bounce rate',+ key: 'bounceRate',+ unit: '%',+ inverted: true,+ hideOnZero: false,+ },] as const;+const VISIBLE_METRICS = 6;export const ProfileMetrics = ({ data }: Props) => {if (!data) {@@ -107,14 +67,12 @@ export const ProfileMetrics = ({ data }: Props) => {}return (- <div className="relative col-span-6 -m-4 mt-0 mb-0 md:m-0">- <div className="card grid grid-cols-2 overflow-hidden rounded-md md:grid-cols-4 lg:grid-cols-6">- {PROFILE_METRICS.filter((metric) => {- if (metric.hideOnZero && data[metric.key] === 0) {- return false;- }- return true;- }).map((metric) => (+ <div className="card grid grid-cols-2 overflow-hidden rounded-md md:grid-cols-3">+ {PROFILE_METRICS.filter(+ (metric) => !(metric.hideOnZero && data[metric.key] === 0)+ )+ .slice(0, VISIBLE_METRICS)+ .map((metric) => (<OverviewMetricCarddata={[]}id={metric.key}@@ -123,16 +81,12 @@ export const ProfileMetrics = ({ data }: Props) => {key={metric.key}label={metric.title}metric={{- current:- metric.unit === 'timeAgo' && data[metric.key]- ? new Date(data[metric.key]!).getTime()- : (data[metric.key] as number) || 0,+ current: data[metric.key] || 0,previous: null,}}unit={metric.unit}/>))}- </div></div>);};diff --git a/apps/start/src/components/profiles/profile-properties.tsx b/apps/start/src/components/profiles/profile-properties.tsxindex 4256ebb68..c7060ad39 100644--- a/apps/start/src/components/profiles/profile-properties.tsx+++ b/apps/start/src/components/profiles/profile-properties.tsx@@ -8,9 +8,9 @@ import { Widget } from '@/components/widget';import { cn } from '@/utils/cn';import { formatDateTime } from '@/utils/date';-type Props = {+interface Props {profile: IServiceProfile;-};+}export const ProfileProperties = ({ profile }: Props) => {const [tab, setTab] = useQueryState(@@ -21,7 +21,7 @@ export const ProfileProperties = ({ profile }: Props) => {return (<Widget className="w-full"><WidgetHead>- <div className="title">Profile Information</div>+ <div className="title">Profile</div><WidgetButtons>{[{@@ -48,7 +48,7 @@ export const ProfileProperties = ({ profile }: Props) => {{tab === 'profile' && profile && (<KeyValueGridclassName="border-0"- columns={3}+ columns={1}copyabledata={[{ name: 'id', value: profile.id },@@ -100,7 +100,7 @@ export const ProfileProperties = ({ profile }: Props) => {{tab === 'properties' && profile && (<KeyValueGridclassName="border-0"- columns={3}+ columns={1}copyabledata={Object.entries(profile.properties).filter(([, value]) => value !== undefined && value !== '')diff --git a/apps/start/src/components/report/ReportInterval.tsx b/apps/start/src/components/report/ReportInterval.tsxindex 8c28d65d6..da0637525 100644--- a/apps/start/src/components/report/ReportInterval.tsx+++ b/apps/start/src/components/report/ReportInterval.tsx@@ -103,8 +103,10 @@ export function ReportInterval({<DropdownMenu><DropdownMenuTrigger asChild><Button+ aria-label={`Interval: ${selectedItem?.label ?? 'none'}`}className={cn('justify-start', className)}icon={ClockIcon}+ responsivesize={size}variant={variant}>diff --git a/apps/start/src/components/report/sidebar/filters/CohortFilterItem.tsx b/apps/start/src/components/report/sidebar/filters/CohortFilterItem.tsxindex 3482ad79e..6b40060ee 100644--- a/apps/start/src/components/report/sidebar/filters/CohortFilterItem.tsx+++ b/apps/start/src/components/report/sidebar/filters/CohortFilterItem.tsx@@ -174,7 +174,6 @@ export function PureCohortFilterItem({placeholder="Select cohorts..."size="sm"value={selectedIds}- variant="input"/></div></div>diff --git a/apps/start/src/components/report/sidebar/filters/FilterItem.tsx b/apps/start/src/components/report/sidebar/filters/FilterItem.tsxindex f41e927da..9ee988d2a 100644--- a/apps/start/src/components/report/sidebar/filters/FilterItem.tsx+++ b/apps/start/src/components/report/sidebar/filters/FilterItem.tsx@@ -297,7 +297,6 @@ export function PureFilterItem({placeholder="Select..."size="sm"value={filter.value}- variant="input"/>);}diff --git a/apps/start/src/components/sidebar-link.tsx b/apps/start/src/components/sidebar-link.tsxindex e5ddc57f2..f30ec72e3 100644--- a/apps/start/src/components/sidebar-link.tsx+++ b/apps/start/src/components/sidebar-link.tsx@@ -7,7 +7,7 @@ const SIDEBAR_ICON_STROKE_WIDTH = 1.75;/** Shared look for every row in the sidebar; TanStack's Link sets `data-status`. */export const sidebarItemClassName = cn(- 'group flex h-9 items-center gap-2.5 rounded-lg px-2.5 text-foregroundish text-nav',+ 'group flex h-8 shrink-0 items-center gap-2.5 rounded-md px-2.5 text-foregroundish text-nav','transition-colors hover:bg-def-200 hover:text-foreground','data-[status=active]:bg-def-200 data-[status=active]:text-foreground');@@ -37,7 +37,7 @@ export function SidebarSectionLabel({return (<divclassName={cn(- 'mt-5 mb-1.5 px-2.5 font-medium text-muted-foreground text-xs first:mt-0',+ 'mt-4 mb-1 shrink-0 px-2.5 font-medium text-muted-foreground text-xs first:mt-0',className)}>diff --git a/apps/start/src/components/sidebar-organization-menu.tsx b/apps/start/src/components/sidebar-organization-menu.tsxindex 9c819bec1..90a294571 100644--- a/apps/start/src/components/sidebar-organization-menu.tsx+++ b/apps/start/src/components/sidebar-organization-menu.tsx@@ -136,7 +136,7 @@ export function ActionCTAButton() {}return (- <div className="mb-3">+ <div className="mb-3 shrink-0"><DropdownMenu><DropdownMenuTrigger asChild><buttondiff --git a/apps/start/src/components/sidebar-project-menu.tsx b/apps/start/src/components/sidebar-project-menu.tsxindex da23ddc17..9d6e471ce 100644--- a/apps/start/src/components/sidebar-project-menu.tsx+++ b/apps/start/src/components/sidebar-project-menu.tsx@@ -98,7 +98,7 @@ function isShortcut(event: KeyboardEvent, key: string) {function SidebarKbd({ children }: { children: React.ReactNode }) {return (- <kbd className="rounded border border-border bg-def-200 px-1.5 py-0.5 font-mono text-2xs text-muted-foreground">+ <kbd className="font-mono text-2xs text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100 group-focus-visible:opacity-100">{children}</kbd>);diff --git a/apps/start/src/components/sidebar.tsx b/apps/start/src/components/sidebar.tsxindex eb5f3c4cf..64d48c092 100644--- a/apps/start/src/components/sidebar.tsx+++ b/apps/start/src/components/sidebar.tsx@@ -1,15 +1,8 @@import type { IServiceOrganization } from '@openpanel/core';import { useQuery } from '@tanstack/react-query';import { Link, useLocation, useParams } from '@tanstack/react-router';-import {- BookOpenIcon,- HeartIcon,- MenuIcon,- SparklesIcon,- XIcon,-} from 'lucide-react';+import { BookOpenIcon, HeartIcon, MenuIcon, XIcon } from 'lucide-react';import { useEffect, useState } from 'react';-import { useChatLaunch } from './chat/chat-launch';import { FeedbackButton } from './feedback-button';import { LogoSquare } from './logo';import { ProfileToggle } from './profile-toggle';@@ -88,8 +81,6 @@ export function SidebarContainer({const [active, setActive] = useState(false);const location = useLocation();const { isSelfHosted } = useAppContext();- const { projectId } = useParams({ strict: false });- const launchChat = useChatLaunch();useEffect(() => {setActive(false);@@ -168,17 +159,6 @@ export function SidebarContainer({Docs</a><div className="flex-1" />- {projectId && (- <button- aria-label="Open AI chat"- className={cn(SIDEBAR_FOOTER_BUTTON, 'size-8')}- onClick={() => launchChat()}- title="Open AI chat (⌘J)"- type="button"- >- <SparklesIcon className="size-4" />- </button>- )}<ProfileToggle className={cn(SIDEBAR_FOOTER_BUTTON, 'size-8')} /></div></div>diff --git a/apps/start/src/components/time-window-picker.tsx b/apps/start/src/components/time-window-picker.tsxindex 9765fcb0b..f6cec70d3 100644--- a/apps/start/src/components/time-window-picker.tsx+++ b/apps/start/src/components/time-window-picker.tsx@@ -326,7 +326,7 @@ export function TimeWindowPicker({</div></PopoverContent></Popover>- <div className="flex items-center">+ <div className="hidden items-center sm:flex"><Buttonaria-label="Previous period"className={cn(className, 'rounded-r-none px-2')}diff --git a/apps/start/src/components/ui/combobox-advanced.tsx b/apps/start/src/components/ui/combobox-advanced.tsxindex 61eda0a11..f46473473 100644--- a/apps/start/src/components/ui/combobox-advanced.tsx+++ b/apps/start/src/components/ui/combobox-advanced.tsx@@ -1,4 +1,4 @@-import { ChevronsUpDownIcon } from 'lucide-react';+import { ChevronDownIcon } from 'lucide-react';import VirtualList from 'rc-virtual-list';import * as React from 'react';import { Button, type ButtonProps } from './button';@@ -42,7 +42,7 @@ export function ComboboxAdvanced({placeholder,className,size,- variant = 'outline',+ variant = 'input',children,}: ComboboxAdvancedProps) {const [open, setOpen] = React.useState(false);@@ -129,7 +129,7 @@ export function ComboboxAdvanced({);})}</div>- <ChevronsUpDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />+ <ChevronDownIcon className="ml-2 size-4 shrink-0 opacity-50" /></Button>);diff --git a/apps/start/src/components/ui/combobox-events.tsx b/apps/start/src/components/ui/combobox-events.tsxindex 42b3b070e..490fbaada 100644--- a/apps/start/src/components/ui/combobox-events.tsx+++ b/apps/start/src/components/ui/combobox-events.tsx@@ -1,7 +1,7 @@import { PopoverPortal } from '@radix-ui/react-popover';import {CheckIcon,- ChevronsUpDown,+ ChevronDown,GanttChartIcon,LayersIcon,type LucideIcon,@@ -93,7 +93,7 @@ export function ComboboxEvents<className,searchable,size,- variant = 'outline',+ variant = 'input',icon: PlaceholderIcon = GanttChartIcon,align = 'start',portal,@@ -216,7 +216,7 @@ export function ComboboxEvents<{renderTriggerContent()}</span></div>- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />+ <ChevronDown className="ml-2 size-4 shrink-0 opacity-50" /></Button></PopoverTrigger><PopoverPortal>diff --git a/apps/start/src/components/ui/combobox.tsx b/apps/start/src/components/ui/combobox.tsxindex 12519043c..e54c0ccd4 100644--- a/apps/start/src/components/ui/combobox.tsx+++ b/apps/start/src/components/ui/combobox.tsx@@ -1,6 +1,6 @@import { PopoverPortal } from '@radix-ui/react-popover';import type { LucideIcon } from 'lucide-react';-import { Check, ChevronsUpDown } from 'lucide-react';+import { Check, ChevronDown } from 'lucide-react';import VirtualList from 'rc-virtual-list';import * as React from 'react';import type { ButtonProps } from '@/components/ui/button';@@ -66,7 +66,7 @@ export function Combobox<T extends string>({searchable,icon: Icon,size,- variant = 'outline',+ variant = 'input',align = 'start',portal,error,@@ -108,7 +108,7 @@ export function Combobox<T extends string>({{value ? (find(value)?.label ?? 'No match') : placeholder}</span></div>- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />+ <ChevronDown className="ml-2 size-4 shrink-0 opacity-50" /></Button>)}</PopoverTrigger>diff --git a/apps/start/src/components/ui/data-table/data-table-date-filter.tsx b/apps/start/src/components/ui/data-table/data-table-date-filter.tsxindex 0dcc7f185..284c027a0 100644--- a/apps/start/src/components/ui/data-table/data-table-date-filter.tsx+++ b/apps/start/src/components/ui/data-table/data-table-date-filter.tsx@@ -184,7 +184,7 @@ export function DataTableDateFilter<TData>({return (<Popover><PopoverTrigger asChild>- <Button className="border-dashed" size="sm" variant="outline">+ <Button className="border-dashed" variant="outline">{hasValue ? (<divaria-label={`Clear ${title} filter`}diff --git a/apps/start/src/components/ui/data-table/data-table-faceted-filter.tsx b/apps/start/src/components/ui/data-table/data-table-faceted-filter.tsxindex 25f75ae20..23be55989 100644--- a/apps/start/src/components/ui/data-table/data-table-faceted-filter.tsx+++ b/apps/start/src/components/ui/data-table/data-table-faceted-filter.tsx@@ -75,7 +75,7 @@ export function DataTableFacetedFilter<TData, TValue>({return (<Popover onOpenChange={setOpen} open={open}><PopoverTrigger asChild>- <Button className="border-dashed" size="sm" variant="outline">+ <Button className="border-dashed" variant="outline">{selectedValues?.size > 0 ? (<divaria-label={`Clear ${title} filter`}diff --git a/apps/start/src/components/ui/data-table/data-table-slider-filter.tsx b/apps/start/src/components/ui/data-table/data-table-slider-filter.tsxindex 932c78ce6..3a534d790 100644--- a/apps/start/src/components/ui/data-table/data-table-slider-filter.tsx+++ b/apps/start/src/components/ui/data-table/data-table-slider-filter.tsx@@ -128,7 +128,7 @@ export function DataTableSliderFilter<TData>({return (<Popover><PopoverTrigger asChild>- <Button className="border-dashed" size="sm" variant="outline">+ <Button className="border-dashed" variant="outline">{columnFilterValue ? (<divaria-label={`Clear ${title} filter`}diff --git a/apps/start/src/components/ui/data-table/data-table-toolbar.tsx b/apps/start/src/components/ui/data-table/data-table-toolbar.tsxindex b287430f6..495c05ca2 100644--- a/apps/start/src/components/ui/data-table/data-table-toolbar.tsx+++ b/apps/start/src/components/ui/data-table/data-table-toolbar.tsx@@ -73,7 +73,6 @@ export function DataTableToolbar<TData>({aria-label="Reset filters"className="border-dashed"onClick={onReset}- size="sm"variant="outline"><XIcon className="mr-2 size-4" />@@ -121,11 +120,10 @@ function DataTableToolbarFilter<TData>({return (<div className="relative"><Input- className={cn('h-8 w-[120px]', columnMeta.unit && 'pr-8')}+ className={cn('w-[120px]', columnMeta.unit && 'pr-8')}inputMode="numeric"onChange={(event) => column.setFilterValue(event.target.value)}placeholder={getTitle()}- size="sm"type="number"value={(column.getFilterValue() as string) ?? ''}/>@@ -197,7 +195,7 @@ export function AnimatedSearchInput({className={cn('relative flex items-center rounded-md border border-input bg-background text-sm transition-[width] duration-300 ease-out','focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-background',- 'h-8 min-h-8',+ 'h-9 min-h-9',isExpanded ? 'w-56 lg:w-72' : 'w-32')}role="search"diff --git a/apps/start/src/components/ui/data-table/data-table-view-options.tsx b/apps/start/src/components/ui/data-table/data-table-view-options.tsxindex a69d69364..fc243d865 100644--- a/apps/start/src/components/ui/data-table/data-table-view-options.tsx+++ b/apps/start/src/components/ui/data-table/data-table-view-options.tsx@@ -15,13 +15,7 @@ import {} from '@dnd-kit/sortable';import { CSS } from '@dnd-kit/utilities';import type { Table } from '@tanstack/react-table';-import {- Check,- ChevronsUpDown,- GripVertical,- RotateCcw,- Settings2Icon,-} from 'lucide-react';+import { Check, GripVertical, RotateCcw, Settings2Icon } from 'lucide-react';import * as React from 'react';import { Button } from '@/components/ui/button';import {@@ -174,14 +168,12 @@ export function DataTableViewOptions<TData>({<PopoverTrigger asChild><Buttonaria-label="Toggle columns"- className="ml-auto hidden h-8 lg:flex"+ className="ml-auto hidden lg:flex"+ icon={Settings2Icon}role="combobox"- size="sm"variant="outline">- <Settings2Icon className="mr-2 size-4" />View- <ChevronsUpDown className="ml-2 size-4 opacity-50" /></Button></PopoverTrigger><PopoverContent align="end" className="w-52 p-0">diff --git a/apps/start/src/components/ui/toolbar-select.tsx b/apps/start/src/components/ui/toolbar-select.tsxnew file mode 100644index 000000000..3b178b2b7--- /dev/null+++ b/apps/start/src/components/ui/toolbar-select.tsx@@ -0,0 +1,176 @@+import { CheckIcon, type LucideIcon } from 'lucide-react';+import { Button } from './button';+import { CommandShortcut } from './command';+import {+ DropdownMenu,+ DropdownMenuCheckboxItem,+ DropdownMenuContent,+ DropdownMenuGroup,+ DropdownMenuItem,+ DropdownMenuLabel,+ DropdownMenuSeparator,+ DropdownMenuShortcut,+ DropdownMenuTrigger,+} from './dropdown-menu';+import { cn } from '@/utils/cn';++/**+ * The value pickers for page toolbars: an outline button showing the icon and+ * current value, opening a menu titled with what is being picked. Form fields+ * use `Select` or `Combobox variant="input"` instead.+ */++export interface ToolbarSelectOption<Value extends string> {+ value: Value;+ label: string;+}++interface ToolbarSelectProps<Value extends string> {+ icon: LucideIcon;+ /** What is being picked; titles the menu and names the button for screen readers. */+ label: string;+ value: Value;+ options: readonly ToolbarSelectOption<Value>[];+ onChange: (value: Value) => void;+ /** Show only the icon below the sm breakpoint. */+ responsive?: boolean;+ className?: string;+}++export function ToolbarSelect<Value extends string>({+ icon,+ label,+ value,+ options,+ onChange,+ responsive,+ className,+}: ToolbarSelectProps<Value>) {+ const selected = options.find((option) => option.value === value);+ return (+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button+ aria-label={`${label}: ${selected?.label ?? 'none'}`}+ className={cn('justify-start', className)}+ icon={icon}+ responsive={responsive}+ variant="outline"+ >+ {selected?.label ?? label}+ </Button>+ </DropdownMenuTrigger>+ <DropdownMenuContent align="start" className="w-56">+ <DropdownMenuLabel className="row items-center justify-between gap-4">+ {label}+ {selected && <CommandShortcut>{selected.label}</CommandShortcut>}+ </DropdownMenuLabel>+ <DropdownMenuSeparator />+ <DropdownMenuGroup>+ {options.map((option) => (+ <DropdownMenuItem+ key={option.value}+ onClick={() => onChange(option.value)}+ >+ {option.label}+ {option.value === value && (+ <DropdownMenuShortcut>+ <CheckIcon className="size-4" />+ </DropdownMenuShortcut>+ )}+ </DropdownMenuItem>+ ))}+ </DropdownMenuGroup>+ </DropdownMenuContent>+ </DropdownMenu>+ );+}++export interface ToolbarMultiSelectOption {+ value: string;+ label?: string;+ /** How often the value occurs, shown muted next to it. */+ count?: number;+}++interface ToolbarMultiSelectProps {+ icon: LucideIcon;+ label: string;+ options: readonly ToolbarMultiSelectOption[];+ selected: string[];+ onChange: (selected: string[]) => void;+ emptyText?: string;+ responsive?: boolean;+ className?: string;+}++export function ToolbarMultiSelect({+ icon,+ label,+ options,+ selected,+ onChange,+ emptyText = 'Nothing to pick',+ responsive,+ className,+}: ToolbarMultiSelectProps) {+ const hasSelection = selected.length > 0;+ const toggle = (value: string, checked: boolean) =>+ onChange(+ checked+ ? [...selected, value]+ : selected.filter((current) => current !== value)+ );++ return (+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button+ aria-label={label}+ className={cn('justify-start', className)}+ icon={icon}+ responsive={responsive}+ variant="outline"+ >+ {label}+ {hasSelection && (+ <span className="ml-2 rounded bg-def-200 px-1.5 font-mono text-2xs">+ {selected.length}+ </span>+ )}+ </Button>+ </DropdownMenuTrigger>+ <DropdownMenuContent align="start" className="w-64">+ <DropdownMenuLabel className="row items-center justify-between gap-4">+ {label}+ {hasSelection && (+ <CommandShortcut>{selected.length} selected</CommandShortcut>+ )}+ </DropdownMenuLabel>+ <DropdownMenuSeparator />+ {options.length === 0 && (+ <div className="px-2 py-1.5 text-muted-foreground text-sm">+ {emptyText}+ </div>+ )}+ {options.map((option) => (+ <DropdownMenuCheckboxItem+ checked={selected.includes(option.value)}+ key={option.value}+ onCheckedChange={(checked) => toggle(option.value, checked)}+ onSelect={(event) => event.preventDefault()}+ >+ <span className="min-w-0 flex-1 truncate">+ {option.label ?? option.value}+ </span>+ {option.count !== undefined && (+ <span className="ml-2 font-mono text-2xs text-muted-foreground">+ {option.count}+ </span>+ )}+ </DropdownMenuCheckboxItem>+ ))}+ </DropdownMenuContent>+ </DropdownMenu>+ );+}diff --git a/apps/start/src/modals/add-client.tsx b/apps/start/src/modals/add-client.tsxindex 88cbb7dab..a29676fc0 100644--- a/apps/start/src/modals/add-client.tsx+++ b/apps/start/src/modals/add-client.tsx@@ -134,7 +134,6 @@ export default function AddClient({ name, type }: AddClientProps = {}) {items={CLIENT_TYPE_ITEMS}onChange={field.onChange}placeholder="Select a client type"- variant="input"/></WithLabel>)}diff --git a/apps/start/src/modals/add-notification-rule.tsx b/apps/start/src/modals/add-notification-rule.tsxindex f3c716057..bd6456bfc 100644--- a/apps/start/src/modals/add-notification-rule.tsx+++ b/apps/start/src/modals/add-notification-rule.tsx@@ -133,7 +133,6 @@ export default function AddNotificationRule({ rule }: Props) {name="config.type"render={({ field }) => (<Combobox- variant="input"{...field}aria-describedby={typeError ? fieldMessageId(typeId) : undefineddiff --git a/apps/start/src/routes/_app.$organizationId.$projectId.groups.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.groups.tsxindex 7a20307df..dee736a97 100644--- a/apps/start/src/routes/_app.$organizationId.$projectId.groups.tsx+++ b/apps/start/src/routes/_app.$organizationId.$projectId.groups.tsx@@ -1,25 +1,20 @@import { keepPreviousData, useQuery } from '@tanstack/react-query';import { createFileRoute } from '@tanstack/react-router';-import { PlusIcon } from 'lucide-react';+import { PlusIcon, TagIcon } from 'lucide-react';import { parseAsString, useQueryState } from 'nuqs';import { GroupsTable } from '@/components/groups/table';import { PageContainer } from '@/components/page-container';import { PageHeader } from '@/components/page-header';import { Button } from '@/components/ui/button';import { useDataTablePagination } from '@/components/ui/data-table/data-table-hooks';-import {- Select,- SelectContent,- SelectItem,- SelectTrigger,- SelectValue,-} from '@/components/ui/select';+import { ToolbarSelect } from '@/components/ui/toolbar-select';import { useSearchQueryState } from '@/hooks/use-search-query-state';import { useTRPC } from '@/integrations/trpc/react';import { pushModal } from '@/modals';import { createProjectTitle } from '@/utils/title';const PAGE_SIZE = 50;+const ALL_TYPES = 'all';export const Route = createFileRoute('/_app/$organizationId/$projectId/groups')({@@ -76,22 +71,18 @@ function Component() {query={groupsQuery}toolbarLeft={types.length > 0 ? (- <Select- onValueChange={(v) => setTypeFilter(v === 'all' ? '' : v)}- value={typeFilter || 'all'}- >- <SelectTrigger className="w-40">- <SelectValue placeholder="All types" />- </SelectTrigger>- <SelectContent>- <SelectItem value="all">All types</SelectItem>- {types.map((t) => (- <SelectItem key={t} value={t}>- {t}- </SelectItem>- ))}- </SelectContent>- </Select>+ <ToolbarSelect+ icon={TagIcon}+ label="Type"+ onChange={(value) =>+ setTypeFilter(value === ALL_TYPES ? '' : value)+ }+ options={[+ { value: ALL_TYPES, label: 'All types' },+ ...types.map((type) => ({ value: type, label: type })),+ ]}+ value={typeFilter || ALL_TYPES}+ />) : null}/>diff --git a/apps/start/src/routes/_app.$organizationId.$projectId.index.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.index.tsxindex 77396a007..7c4f106b2 100644--- a/apps/start/src/routes/_app.$organizationId.$projectId.index.tsx+++ b/apps/start/src/routes/_app.$organizationId.$projectId.index.tsx@@ -43,8 +43,8 @@ function ProjectDashboard() {<ActivationBanner /><div className="sticky-header -top-px!"><div className="col gap-2 p-4">- <div className="flex justify-between gap-2">- <div className="flex gap-2">+ <div className="flex flex-wrap justify-between gap-2">+ <div className="flex flex-wrap gap-2"><OverviewRange /><OverviewInterval /><OverviewFilterButton mode="events" />diff --git a/apps/start/src/routes/_app.$organizationId.$projectId.insights.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.insights.tsxindex 1865a1c78..ba9ba4e38 100644--- a/apps/start/src/routes/_app.$organizationId.$projectId.insights.tsx+++ b/apps/start/src/routes/_app.$organizationId.$projectId.insights.tsx@@ -1,7 +1,14 @@import { INSIGHT_LIST_ALL_MAX_LIMIT } from '@openpanel/core/modules/insight/insight.constants';import { useQuery } from '@tanstack/react-query';import { createFileRoute, useNavigate } from '@tanstack/react-router';-import { SearchIcon, SparklesIcon } from 'lucide-react';+import {+ ArrowDownWideNarrowIcon,+ ArrowUpDownIcon,+ CalendarRangeIcon,+ GaugeIcon,+ SearchIcon,+ SparklesIcon,+} from 'lucide-react';import { parseAsString, parseAsStringEnum, useQueryState } from 'nuqs';import { useMemo } from 'react';import { EmptyState } from '@/components/empty-state';@@ -17,15 +24,9 @@ import {CarouselNext,CarouselPrevious,} from '@/components/ui/carousel';-import { Input } from '@/components/ui/input';-import {- Select,- SelectContent,- SelectItem,- SelectTrigger,- SelectValue,-} from '@/components/ui/select';+import { AnimatedSearchInput } from '@/components/ui/data-table/data-table-toolbar';import { TableButtons } from '@/components/ui/table';+import { ToolbarSelect } from '@/components/ui/toolbar-select';import { useRangePageContext } from '@/hooks/use-page-context-helpers';import { useTRPC } from '@/integrations/trpc/react';import { cn } from '@/utils/cn';@@ -54,6 +55,37 @@ type SortOption =| 'severity-asc'| 'recent';+const WINDOW_OPTIONS = [+ { value: 'all', label: 'All windows' },+ { value: 'yesterday', label: 'Yesterday' },+ { value: 'rolling_7d', label: '7 days' },+ { value: 'rolling_30d', label: '30 days' },+] as const;++const SEVERITY_OPTIONS = [+ { value: 'all', label: 'All severities' },+ { value: 'severe', label: 'Severe' },+ { value: 'moderate', label: 'Moderate' },+ { value: 'low', label: 'Low' },+ { value: 'none', label: 'No severity' },+] as const;++const DIRECTION_OPTIONS = [+ { value: 'all', label: 'All directions' },+ { value: 'up', label: 'Increasing' },+ { value: 'down', label: 'Decreasing' },+ { value: 'flat', label: 'Flat' },+] as const;++const SORT_OPTIONS = [+ { value: 'relevance', label: 'Relevance (AI)' },+ { value: 'impact-desc', label: 'Impact (high → low)' },+ { value: 'impact-asc', label: 'Impact (low → high)' },+ { value: 'severity-desc', label: 'Severity (high → low)' },+ { value: 'severity-asc', label: 'Severity (low → high)' },+ { value: 'recent', label: 'Most recent' },+] as const satisfies readonly { value: SortOption; label: string }[];+function getModuleDisplayName(moduleKey: string): string {const displayNames: Record<string, string> = {geo: 'Geographic',@@ -308,11 +340,11 @@ function Component() {severityFilter !== 'all' ||directionFilter !== 'all';const clearFilters = () => {- void setSearch(null);- void setModuleFilter(null);- void setWindowKindFilter(null);- void setSeverityFilter(null);- void setDirectionFilter(null);+ setSearch(null);+ setModuleFilter(null);+ setWindowKindFilter(null);+ setSeverityFilter(null);+ setDirectionFilter(null);};return (@@ -323,77 +355,39 @@ function Component() {title="Insights"/><TableButtons className="mb-8">- <Input- className="max-w-xs"- onChange={(e) => void setSearch(e.target.value || null)}- placeholder="Search insights..."+ <AnimatedSearchInput+ onChange={(value) => setSearch(value || null)}+ placeholder="Search insights"value={search ?? ''}/>- <Select- onValueChange={(v) =>- void setWindowKindFilter(v as typeof windowKindFilter)- }+ <ToolbarSelect+ icon={CalendarRangeIcon}+ label="Time window"+ onChange={setWindowKindFilter}+ options={WINDOW_OPTIONS}value={windowKindFilter ?? 'all'}- >- <SelectTrigger className="w-[140px]">- <SelectValue placeholder="Time Window" />- </SelectTrigger>- <SelectContent>- <SelectItem value="all">All Windows</SelectItem>- <SelectItem value="yesterday">Yesterday</SelectItem>- <SelectItem value="rolling_7d">7 Days</SelectItem>- <SelectItem value="rolling_30d">30 Days</SelectItem>- </SelectContent>- </Select>- <Select- onValueChange={(v) =>- void setSeverityFilter(v as typeof severityFilter)- }+ />+ <ToolbarSelect+ icon={GaugeIcon}+ label="Severity"+ onChange={setSeverityFilter}+ options={SEVERITY_OPTIONS}value={severityFilter ?? 'all'}- >- <SelectTrigger className="w-[140px]">- <SelectValue placeholder="Severity" />- </SelectTrigger>- <SelectContent>- <SelectItem value="all">All Severity</SelectItem>- <SelectItem value="severe">Severe</SelectItem>- <SelectItem value="moderate">Moderate</SelectItem>- <SelectItem value="low">Low</SelectItem>- <SelectItem value="none">No Severity</SelectItem>- </SelectContent>- </Select>- <Select- onValueChange={(v) =>- void setDirectionFilter(v as typeof directionFilter)- }+ />+ <ToolbarSelect+ icon={ArrowUpDownIcon}+ label="Direction"+ onChange={setDirectionFilter}+ options={DIRECTION_OPTIONS}value={directionFilter ?? 'all'}- >- <SelectTrigger className="w-[140px]">- <SelectValue placeholder="Direction" />- </SelectTrigger>- <SelectContent>- <SelectItem value="all">All Directions</SelectItem>- <SelectItem value="up">Increasing</SelectItem>- <SelectItem value="down">Decreasing</SelectItem>- <SelectItem value="flat">Flat</SelectItem>- </SelectContent>- </Select>- <Select- onValueChange={(v) => void setSortBy(v as SortOption)}- value={sortBy ?? 'impact-desc'}- >- <SelectTrigger className="w-[160px]">- <SelectValue placeholder="Sort by" />- </SelectTrigger>- <SelectContent>- <SelectItem value="relevance">Relevance (AI)</SelectItem>- <SelectItem value="impact-desc">Impact (High → Low)</SelectItem>- <SelectItem value="impact-asc">Impact (Low → High)</SelectItem>- <SelectItem value="severity-desc">Severity (High → Low)</SelectItem>- <SelectItem value="severity-asc">Severity (Low → High)</SelectItem>- <SelectItem value="recent">Most Recent</SelectItem>- </SelectContent>- </Select>+ />+ <ToolbarSelect+ icon={ArrowDownWideNarrowIcon}+ label="Sort by"+ onChange={setSortBy}+ options={SORT_OPTIONS}+ value={sortBy ?? 'relevance'}+ /></TableButtons>{filteredAndSorted.length === 0 && !isLoading && (diff --git a/apps/start/src/routes/_app.$organizationId.$projectId.profiles.$profileId._tabs.index.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.profiles.$profileId._tabs.index.tsxindex 7e1673acf..70ff64b18 100644--- a/apps/start/src/routes/_app.$organizationId.$projectId.profiles.$profileId._tabs.index.tsx+++ b/apps/start/src/routes/_app.$organizationId.$projectId.profiles.$profileId._tabs.index.tsx@@ -98,12 +98,8 @@ function Component() {);return (- <div className="grid grid-cols-1 gap-6 md:grid-cols-2">- <div className="col-span-1 md:col-span-2">- <ProfileMetrics data={metrics.data} />- </div>- {/* Profile properties - full width */}- <div className="col-span-1 flex flex-col gap-3 md:col-span-2">+ <div className="grid grid-cols-1 items-start gap-6 lg:grid-cols-[minmax(0,320px)_minmax(0,1fr)]">+ <aside className="col gap-6"><ProfileProperties profile={profile.data!} />{profile.data?.groups?.length ? (<ProfileGroups@@ -112,39 +108,23 @@ function Component() {projectId={projectId}/>) : null}- </div>-- <div className="col-span-1 md:col-span-2"><AiUsageWidget- className="w-full"entity={{ kind: 'profile', profileId }}projectId={projectId}/>- </div>+ </aside>- <div className="col-span-1">- <ProfileActivity data={activity.data} />- </div>-- <div className="col-span-1">- <LatestEvents- organizationId={organizationId}- profileId={profileId}- projectId={projectId}- />- </div>-- <div className="col-span-1">- <MostEvents data={mostEvents.data} />- </div>-- <div className="col-span-1">- <PopularRoutes data={popularRoutes.data} />- </div>-- {/* Charts - spans both columns */}- <div className="col-span-1 md:col-span-2">- <div className="grid grid-cols-1 gap-6 md:grid-cols-2">+ <div className="col min-w-0 gap-6">+ <ProfileMetrics data={metrics.data} />+ <div className="grid grid-cols-1 gap-6 xl:grid-cols-2">+ <ProfileActivity data={activity.data} />+ <LatestEvents+ organizationId={organizationId}+ profileId={profileId}+ projectId={projectId}+ />+ <MostEvents data={mostEvents.data} />+ <PopularRoutes data={popularRoutes.data} /><ProfileCharts profileId={profileId} projectId={projectId} /></div></div>diff --git a/apps/start/src/routes/_steps.onboarding.project.tsx b/apps/start/src/routes/_steps.onboarding.project.tsxindex 67e2889ef..5cdd38cbc 100644--- a/apps/start/src/routes/_steps.onboarding.project.tsx+++ b/apps/start/src/routes/_steps.onboarding.project.tsx@@ -511,7 +511,6 @@ function Component() {searchablesize="xl"value={field.value}- variant="input"/></WithLabel>)}diff --git a/packages/core/src/modules/ai/ai.constants.ts b/packages/core/src/modules/ai/ai.constants.tsindex a526617bd..c88776c12 100644--- a/packages/core/src/modules/ai/ai.constants.ts+++ b/packages/core/src/modules/ai/ai.constants.ts@@ -20,6 +20,13 @@ export type AiEventName = (typeof AI_EVENT_NAMES)[AiEventKind];export const AI_EVENT_NAME_LIST = Object.values(AI_EVENT_NAMES);+/** Plumbing around a model call; activity views show the `ai_generation` itself. */+export const AI_INTERNAL_EVENT_NAMES: readonly AiEventName[] = [+ AI_EVENT_NAMES.trace,+ AI_EVENT_NAMES.span,+ AI_EVENT_NAMES.embedding,+];+export function isAiEventName(name: string): name is AiEventName {return (AI_EVENT_NAME_LIST as string[]).includes(name);}diff --git a/packages/core/src/modules/ai/src/sql.ts b/packages/core/src/modules/ai/src/sql.tsindex 0d8f212cf..59339572e 100644--- a/packages/core/src/modules/ai/src/sql.ts+++ b/packages/core/src/modules/ai/src/sql.ts@@ -339,7 +339,7 @@ export function entitySummaryQuery(input: {countIf(is_error AND type = 'generation') AS errors,min(created_at) AS first_at,max(created_at) AS last_at,- topK(3)(model) AS top_models+ topKIf(3)(model, type = 'generation') AS top_modelsFROM ${sql.id(AI_EVENTS_TABLE)}WHERE project_id = ${sql.string(input.projectId)}AND ${input.where}diff --git a/packages/core/src/modules/profile/src/profile-metrics-sql.test.ts b/packages/core/src/modules/profile/src/profile-metrics-sql.test.tsindex 8905f3e67..49d89e6b6 100644--- a/packages/core/src/modules/profile/src/profile-metrics-sql.test.ts+++ b/packages/core/src/modules/profile/src/profile-metrics-sql.test.ts@@ -66,18 +66,19 @@ describe('profileMetricsQuery', () => {'WHERE id = {p1:String} AND project_id = {p2:String}');expect(query).toContain(- 'WHERE profile_id = {p3:String} AND project_id = {p4:String}'+ 'WHERE profile_id = {p3:String} AND project_id = {p4:String} AND name NOT IN {p5:Array(String)}');expect(query).toContain(- 'PREWHERE project_id = {p5:String} AND profile_id = {p6:String}'+ 'PREWHERE project_id = {p6:String} AND profile_id = {p7:String}');expect(query_params).toEqual({p1: hostileProfile,p2: hostileProject,p3: hostileProfile,p4: hostileProject,- p5: hostileProject,- p6: hostileProfile,+ p5: ['ai_trace', 'ai_span', 'ai_embedding'],+ p6: hostileProject,+ p7: hostileProfile,});});diff --git a/packages/core/src/modules/profile/src/sql.ts b/packages/core/src/modules/profile/src/sql.tsindex a2fe7564d..86c6e7d92 100644--- a/packages/core/src/modules/profile/src/sql.ts+++ b/packages/core/src/modules/profile/src/sql.ts@@ -4,6 +4,7 @@import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';import { toRangeBoundaryLiteral } from '../../../shared/ch-dates';+import { AI_INTERNAL_EVENT_NAMES } from '../../ai/ai.constants';import {type CompiledFilterClauses,compiledFilterFragments,@@ -15,6 +16,8 @@ const TABLE = {sessions: 'sessions',} as const;+const NOT_AI_INTERNAL = sql`name NOT IN ${sql.array('String', AI_INTERNAL_EVENT_NAMES)}`;+export const PROFILE_COLUMNS = ['id','first_name',@@ -148,7 +151,7 @@ export function profileMetricsQuery(query: {countIf(name NOT IN ('screen_view', 'session_start', 'session_end')) as conversionEvents,sumIf(revenue, name = 'revenue') as revenueFROM ${sql.id(TABLE.events)}- WHERE profile_id = ${profileId} AND project_id = ${projectId}+ WHERE profile_id = ${profileId} AND project_id = ${projectId} AND ${NOT_AI_INTERNAL}),sessionStats AS (SELECT@@ -529,14 +532,14 @@ export function profileActivityQuery(query: {projectId: string;profileId: string;}): SqlFragment {- return sql`SELECT count(*) as count, toStartOfDay(created_at) as date FROM ${sql.id(TABLE.events)} WHERE project_id = ${sql.string(query.projectId)} and profile_id = ${sql.string(query.profileId)} GROUP BY date ORDER BY date DESC`;+ return sql`SELECT count(*) as count, toStartOfDay(created_at) as date FROM ${sql.id(TABLE.events)} WHERE project_id = ${sql.string(query.projectId)} and profile_id = ${sql.string(query.profileId)} AND ${NOT_AI_INTERNAL} GROUP BY date ORDER BY date DESC`;}export function profileMostEventsQuery(query: {projectId: string;profileId: string;}): SqlFragment {- return sql`SELECT count(*) as count, name FROM ${sql.id(TABLE.events)} WHERE name NOT IN ('screen_view', 'session_start', 'session_end') AND project_id = ${sql.string(query.projectId)} and profile_id = ${sql.string(query.profileId)} GROUP BY name ORDER BY count DESC`;+ return sql`SELECT count(*) as count, name FROM ${sql.id(TABLE.events)} WHERE name NOT IN ('screen_view', 'session_start', 'session_end') AND project_id = ${sql.string(query.projectId)} and profile_id = ${sql.string(query.profileId)} AND ${NOT_AI_INTERNAL} GROUP BY name ORDER BY count DESC`;}const POPULAR_ROUTES_LIMIT = 10;diff --git a/packages/seed/src/archetypes/archetype.ts b/packages/seed/src/archetypes/archetype.tsindex f02dc8cd2..dbddcd159 100644--- a/packages/seed/src/archetypes/archetype.ts+++ b/packages/seed/src/archetypes/archetype.ts@@ -57,6 +57,8 @@ export interface Archetype {/** The funnels this project's data is built to show, for `.seed.json` and whoever builds reports. */funnels: readonly SeedFunnel[];journey(rng: Rng, journey: Journey, visitor: Visitor): void;+ /** Work the product's backend does on its own during `day` (UTC midnight), outside any session. */+ background?(rng: Rng, day: Date): Journey[];}const CUSTOM_EVENT_GAP_MEDIAN_SECONDS = 3;@@ -128,6 +130,10 @@ export class Journey {this.person = person;}+ get now(): Date {+ return this.cursor;+ }+get identified(): Person | null {return this.person;}@@ -174,13 +180,14 @@ export class Journey {/*** An AI event: `name` is the `events` name (`ai_generation`, `ai_span`, …),* `properties` the flat `ai_*` keys the worker would store, `ai` the typed- * row. The clock advances by the call's latency, like a user waiting.+ * row. The clock advances by `advanceSeconds`: a call's latency, or 0 for a+ * parent span that its children run inside.*/ai(name: string,properties: Record<string, unknown>,ai: SeedAiEvent,- latencySeconds: number+ advanceSeconds: number): void {this.events.push({name,@@ -190,7 +197,7 @@ export class Journey {person: this.person,ai,});- this.advance(latencySeconds);+ this.advance(advanceSeconds);}/** Amount in whole currency units, integer: `events.revenue` is UInt64. */diff --git a/packages/seed/src/archetypes/saas-ai.ts b/packages/seed/src/archetypes/saas-ai.tsnew file mode 100644index 000000000..09d69348c--- /dev/null+++ b/packages/seed/src/archetypes/saas-ai.ts@@ -0,0 +1,624 @@+// The SaaS archetype's AI assistant. Every turn is one agent trace, shaped+// like what `withAiContext` + the `@openpanel/ai` middleware send: a root+// `ai_trace`, an optional retrieval span wrapping its embedding, a model call,+// tool spans when the model asks for them, and the model call that answers.++import {+ AI_ANSWERS,+ AI_EMBEDDING_MODEL,+ AI_ERRORS,+ AI_FEATURES,+ AI_MODELS,+ AI_PREMIUM_MODEL,+ AI_QUESTIONS,+ AI_TOOL_CALLS,+ AI_TOOLS,+ type SeedAiModel,+ type SeedAiTool,+} from '../data/ai';+import type { Rng } from '../rng';+import { Journey, type SeedAiEvent } from './archetype';++/** Share of product visits that talk to the assistant, by plan. */+const AI_VISIT_CHANCE: Record<string, number> = {+ free: 0.25,+ pro: 0.5,+ team: 0.65,+ enterprise: 0.75,+};++/** The backend's hourly job that summarises what changed, with no user behind it. */+const INSIGHT_JOB_FEATURE = 'insight-summary';+const INSIGHT_JOB_NAME = 'hourly-insights';+const INSIGHT_JOB_TRACES_MAX = 3;+const INSIGHT_JOB_JITTER_SECONDS = 300;+const HOURS_PER_DAY = 24;+const MS_PER_HOUR = 3_600_000;++interface FeatureProfile {+ retrievalChance: number;+ toolChance: number;+ premiumChance: number;+}++const FEATURE_PROFILES: Record<string, FeatureProfile> = {+ 'assistant-chat': {+ retrievalChance: 0.4,+ toolChance: 0.45,+ premiumChance: 0,+ },+ 'report-draft': { retrievalChance: 0.2, toolChance: 0.7, premiumChance: 0.3 },+ 'insight-explain': { retrievalChance: 0, toolChance: 0, premiumChance: 0 },+ [INSIGHT_JOB_FEATURE]: {+ retrievalChance: 0,+ toolChance: 1,+ premiumChance: 0,+ },+};+const DEFAULT_PROFILE: FeatureProfile = {+ retrievalChance: 0,+ toolChance: 0,+ premiumChance: 0,+};++const AI_TURNS_MAX = 6;+const AI_TOOL_CALLS_MAX = 2;+const AI_ERROR_CHANCE = 0.03;+const AI_RETRY_CHANCE = 0.5;+const AI_FALLBACK_MODEL = 'claude-haiku-5-5';+const AI_CACHE_HIT_CHANCE = 0.4;+const AI_CACHE_HIT_MAX_SHARE = 0.8;+const AI_INPUT_MEDIAN_TOKENS = 1800;+const AI_INPUT_SIGMA = 0.5;+const AI_OUTPUT_SIGMA = 0.6;+const AI_LATENCY_SIGMA = 0.45;+const AI_FAILED_LATENCY_SHARE = 0.5;+const AI_STREAM_CHANCE = 0.85;+const AI_TTFT_SHARE = 0.2;+const AI_RETRIEVAL_LATENCY_MEDIAN_MS = 80;+const AI_EMBEDDING_TOKENS_MEDIAN = 24;+const AI_CONTEXT_TOKENS_PER_TURN = 400;+const AI_TEMPERATURE = 0.2;+const MS_PER_SECOND = 1000;+const COST_DECIMALS = 10;++/**+ * A provider incident every few days, in the afternoon (UTC): errors and+ * latency jump, so the error-rate and latency charts have something to show.+ */+const INCIDENT_EVERY_DAYS = 5;+const INCIDENT_START_HOUR = 13;+const INCIDENT_END_HOUR = 16;+const INCIDENT_ERROR_CHANCE = 0.35;+const INCIDENT_LATENCY_FACTOR = 2.2;+const DAY_MS = 86_400_000;++interface TurnContext {+ traceId: string;+ rootSpanId: string;+ conversationId: string;+ feature: string;+ base: Record<string, unknown>;+}++interface GenerationResult {+ failed: boolean;+ latencyMs: number;+ error: (typeof AI_ERRORS)[number]['value'] | null;+}++export function aiVisitChance(plan: string): number {+ return AI_VISIT_CHANCE[plan] ?? 0;+}++export function assistantConversation(+ rng: Rng,+ journey: Journey,+ plan: string,+ projectId: string+): void {+ const conversationId = `conv_${rng.hex(12)}`;+ const feature = rng.pick(AI_FEATURES);+ const turns = rng.int(1, AI_TURNS_MAX);+ const base = { feature, plan, project: projectId };++ journey.view(`/projects/${projectId}/ai-assistant`, 'AI assistant', {+ project: projectId,+ });+ for (let turn = 0; turn < turns; turn++) {+ const failed = agentTurn(rng, journey, turn, {+ traceId: `trace_${rng.hex(16)}`,+ rootSpanId: `span_${rng.hex(16)}`,+ conversationId,+ feature,+ base,+ });+ if (failed) {+ break;+ }+ }+ journey.event('feature_used', {+ feature: 'ai-assistant',+ project: projectId,+ });+}++/** One journey per hour of `day`, each a run of the insight job. */+export function insightJobs(+ rng: Rng,+ day: Date,+ dwellMedianSeconds: number+): Journey[] {+ const journeys: Journey[] = [];+ for (let hour = 0; hour < HOURS_PER_DAY; hour++) {+ const start = new Date(+ day.getTime() ++ hour * MS_PER_HOUR ++ rng.int(0, INSIGHT_JOB_JITTER_SECONDS) * MS_PER_SECOND+ );+ const journey = new Journey(rng, start, dwellMedianSeconds, null);+ const traces = rng.int(1, INSIGHT_JOB_TRACES_MAX);+ for (let trace = 0; trace < traces; trace++) {+ agentTurn(rng, journey, 0, {+ traceId: `trace_${rng.hex(16)}`,+ rootSpanId: `span_${rng.hex(16)}`,+ conversationId: '',+ feature: INSIGHT_JOB_FEATURE,+ base: { feature: INSIGHT_JOB_FEATURE, job: INSIGHT_JOB_NAME },+ });+ }+ journeys.push(journey);+ }+ return journeys;+}++/** Plays one turn as a trace; returns whether it ended in an error. */+function agentTurn(+ rng: Rng,+ journey: Journey,+ turn: number,+ context: TurnContext+): boolean {+ const profile = FEATURE_PROFILES[context.feature] ?? DEFAULT_PROFILE;+ const question = rng.one(AI_QUESTIONS);+ const answer = rng.one(AI_ANSWERS);+ const model = rng.chance(profile.premiumChance)+ ? AI_PREMIUM_MODEL+ : rng.pick(AI_MODELS);+ const tools = rng.chance(profile.toolChance)+ ? Array.from({ length: rng.int(1, AI_TOOL_CALLS_MAX) }, () =>+ rng.pick(AI_TOOL_CALLS)+ )+ : [];++ // Children run inside the root, so it is written first and the clock only+ // moves as they do; its latency is known once they are planned.+ const rootAt = journey.events.length;+ journey.ai(+ 'ai_trace',+ {},+ emptyAiRow(context, 'trace', context.rootSpanId, ''),+ 0+ );++ let totalMs = 0;+ if (rng.chance(profile.retrievalChance)) {+ totalMs += retrieval(rng, journey, context, question);+ }+ let contextTokens = turn * AI_CONTEXT_TOKENS_PER_TURN;+ let result = generation(rng, journey, context, {+ model,+ question,+ answer,+ contextTokens,+ toolCall: tools.length > 0,+ toolNames: tools.map((tool) => tool.name),+ turn,+ });+ totalMs += result.latencyMs;++ if (result.failed && rng.chance(AI_RETRY_CHANCE)) {+ result = generation(rng, journey, context, {+ model: fallbackModel(),+ question,+ answer,+ contextTokens,+ toolCall: false,+ toolNames: [],+ turn,+ });+ totalMs += result.latencyMs;+ } else if (!result.failed && tools.length > 0) {+ for (const tool of tools) {+ totalMs += toolSpan(rng, journey, context, tool, question);+ contextTokens += tool.resultTokens;+ }+ result = generation(rng, journey, context, {+ model,+ question,+ answer,+ contextTokens,+ toolCall: false,+ toolNames: [],+ turn,+ });+ totalMs += result.latencyMs;+ }++ fillRoot(journey, rootAt, context, {+ question,+ answer,+ totalMs,+ failed: result.failed,+ error: result.error,+ });+ return result.failed;+}++function fillRoot(+ journey: Journey,+ index: number,+ context: TurnContext,+ outcome: {+ question: string;+ answer: string;+ totalMs: number;+ failed: boolean;+ error: GenerationResult['error'];+ }+): void {+ const root = journey.events[index];+ if (!root?.ai) {+ return;+ }+ root.properties = {+ ...traceProperties(context),+ ai_name: context.feature,+ ai_latency_ms: outcome.totalMs,+ ai_is_error: outcome.failed,+ ai_error_code: outcome.error?.code,+ };+ root.ai = {+ ...root.ai,+ name: context.feature,+ latency_ms: outcome.totalMs,+ is_error: outcome.failed,+ http_status: outcome.error?.httpStatus ?? 0,+ input: JSON.stringify(outcome.question),+ output: outcome.failed ? '' : JSON.stringify(outcome.answer),+ error: outcome.error ? JSON.stringify(outcome.error) : '',+ properties: stringProperties(context.base),+ };+}++/** A `search-docs` span with the question's embedding inside it. */+function retrieval(+ rng: Rng,+ journey: Journey,+ context: TurnContext,+ question: string+): number {+ const searchSpanId = `span_${rng.hex(16)}`;+ const embeddingTokens = tokenCount(rng, AI_EMBEDDING_TOKENS_MEDIAN, 0.3);+ const embeddingCost = roundCost(+ embeddingTokens * AI_EMBEDDING_MODEL.inputPrice+ );+ const embeddingMs = latency(rng, AI_EMBEDDING_MODEL.latencyMedian);+ const searchMs = embeddingMs + latency(rng, AI_RETRIEVAL_LATENCY_MEDIAN_MS);++ journey.ai(+ 'ai_span',+ {+ ...traceProperties(context),+ ai_name: 'search-docs',+ ai_latency_ms: searchMs,+ ai_is_error: false,+ },+ {+ ...emptyAiRow(context, 'span', searchSpanId, context.rootSpanId),+ name: 'search-docs',+ latency_ms: searchMs,+ input: JSON.stringify({ query: question, limit: 5 }),+ output: JSON.stringify({ hits: rng.int(0, 5) }),+ },+ 0+ );+ journey.ai(+ 'ai_embedding',+ {+ ...traceProperties(context),+ ai_name: 'embed-question',+ ai_model: AI_EMBEDDING_MODEL.model,+ ai_provider: AI_EMBEDDING_MODEL.provider,+ ai_input_tokens: embeddingTokens,+ ai_total_tokens: embeddingTokens,+ ai_cost: embeddingCost,+ ai_latency_ms: embeddingMs,+ ai_is_error: false,+ },+ {+ ...emptyAiRow(context, 'embedding', `span_${rng.hex(16)}`, searchSpanId),+ name: 'embed-question',+ model: AI_EMBEDDING_MODEL.model,+ provider: AI_EMBEDDING_MODEL.provider,+ input_tokens: embeddingTokens,+ input_cost: embeddingCost,+ total_cost: embeddingCost,+ cost_source: 'catalog',+ latency_ms: embeddingMs,+ input: JSON.stringify([question]),+ },+ searchMs / MS_PER_SECOND+ );+ return searchMs;+}++function toolSpan(+ rng: Rng,+ journey: Journey,+ context: TurnContext,+ tool: SeedAiTool,+ question: string+): number {+ const toolMs = latency(rng, tool.latencyMedian);+ journey.ai(+ 'ai_span',+ {+ ...traceProperties(context),+ ai_name: tool.name,+ ai_latency_ms: toolMs,+ ai_is_error: false,+ },+ {+ ...emptyAiRow(context, 'span', `span_${rng.hex(16)}`, context.rootSpanId),+ name: tool.name,+ latency_ms: toolMs,+ input: JSON.stringify({ question }),+ output: JSON.stringify({ rows: rng.int(1, 40) }),+ },+ toolMs / MS_PER_SECOND+ );+ return toolMs;+}++interface GenerationInput {+ model: SeedAiModel;+ question: string;+ answer: string;+ contextTokens: number;+ toolCall: boolean;+ /** Tools the model asks for; empty when it answers directly. */+ toolNames: string[];+ turn: number;+}++function generation(+ rng: Rng,+ journey: Journey,+ context: TurnContext,+ call: GenerationInput+): GenerationResult {+ const { model } = call;+ const incident = isIncident(journey.now);+ const failed = rng.chance(incident ? INCIDENT_ERROR_CHANCE : AI_ERROR_CHANCE);+ const error = failed ? errorFor(rng, incident) : null;+ const inputTokens =+ tokenCount(rng, AI_INPUT_MEDIAN_TOKENS, AI_INPUT_SIGMA) ++ call.contextTokens;+ const cacheReadTokens =+ call.turn > 0 && rng.chance(AI_CACHE_HIT_CHANCE)+ ? Math.round(inputTokens * rng.float() * AI_CACHE_HIT_MAX_SHARE)+ : 0;+ const outputTokens = failed+ ? 0+ : tokenCount(rng, model.outputMedian, AI_OUTPUT_SIGMA);+ const reasoningTokens = Math.round(outputTokens * model.reasoningShare);+ const slowdown =+ (incident ? INCIDENT_LATENCY_FACTOR : 1) *+ (failed ? AI_FAILED_LATENCY_SHARE : 1);+ const latencyMs = Math.round(latency(rng, model.latencyMedian) * slowdown);+ const cost = generationCost(+ model,+ inputTokens,+ outputTokens,+ cacheReadTokens+ );+ const stream = rng.chance(AI_STREAM_CHANCE);+ const timeToFirstTokenMs =+ stream && !failed ? Math.round(latencyMs * AI_TTFT_SHARE) : 0;+ const stopReason = stopReasonFor(failed, call.toolCall);+ const output = failed+ ? []+ : [{ role: 'assistant', content: assistantContent(call) }];++ journey.ai(+ 'ai_generation',+ {+ ...traceProperties(context),+ ai_name: context.feature,+ ai_model: model.model,+ ai_provider: model.provider,+ ai_input_tokens: inputTokens,+ ai_output_tokens: outputTokens,+ ai_cache_read_tokens: cacheReadTokens,+ ai_reasoning_tokens: reasoningTokens || undefined,+ ai_total_tokens: inputTokens + outputTokens,+ ai_cost: cost.total,+ ai_latency_ms: latencyMs,+ ai_time_to_first_token_ms: timeToFirstTokenMs || undefined,+ ai_is_error: failed,+ ai_error_code: error?.code,+ ai_http_status: error?.httpStatus,+ ai_stop_reason: stopReason || undefined,+ ai_stream: stream,+ },+ {+ ...emptyAiRow(+ context,+ 'generation',+ `span_${rng.hex(16)}`,+ context.rootSpanId+ ),+ name: context.feature,+ model: model.model,+ provider: model.provider,+ input_tokens: inputTokens,+ output_tokens: outputTokens,+ cache_read_tokens: cacheReadTokens,+ reasoning_tokens: reasoningTokens,+ input_cost: cost.input,+ output_cost: cost.output,+ total_cost: cost.total,+ cost_source: 'catalog',+ latency_ms: latencyMs,+ time_to_first_token_ms: timeToFirstTokenMs,+ is_error: failed,+ http_status: error?.httpStatus ?? 0,+ stop_reason: stopReason,+ is_stream: stream,+ model_parameters: JSON.stringify({ temperature: AI_TEMPERATURE }),+ input: JSON.stringify([+ {+ role: 'system',+ content: 'You are the OpenPanel analytics assistant.',+ },+ { role: 'user', content: call.question },+ ]),+ output: JSON.stringify(output),+ tools: JSON.stringify(AI_TOOLS),+ error: error ? JSON.stringify(error) : '',+ },+ latencyMs / MS_PER_SECOND+ );+ return { failed, latencyMs, error };+}++function assistantContent(call: GenerationInput): unknown {+ if (!call.toolCall) {+ return call.answer;+ }+ return call.toolNames.map((toolName) => ({+ type: 'tool_call',+ toolName,+ input: { question: call.question },+ }));+}++function stopReasonFor(failed: boolean, toolCall: boolean): string {+ if (failed) {+ return '';+ }+ return toolCall ? 'tool-calls' : 'stop';+}++function errorFor(+ rng: Rng,+ incident: boolean+): (typeof AI_ERRORS)[number]['value'] {+ const rateLimit = AI_ERRORS[0]?.value;+ if (incident && rateLimit) {+ return rateLimit;+ }+ return rng.pick(AI_ERRORS);+}++function fallbackModel(): SeedAiModel {+ const fallback = AI_MODELS.find(+ (entry) => entry.value.model === AI_FALLBACK_MODEL+ );+ return fallback?.value ?? AI_PREMIUM_MODEL;+}++function isIncident(at: Date): boolean {+ const day = Math.floor(at.getTime() / DAY_MS);+ const hour = at.getUTCHours();+ return (+ day % INCIDENT_EVERY_DAYS === 0 &&+ hour >= INCIDENT_START_HOUR &&+ hour < INCIDENT_END_HOUR+ );+}++function generationCost(+ model: SeedAiModel,+ inputTokens: number,+ outputTokens: number,+ cacheReadTokens: number+): { input: number; output: number; total: number } {+ const input =+ (inputTokens - cacheReadTokens) * model.inputPrice ++ cacheReadTokens * model.cacheReadPrice;+ const output = outputTokens * model.outputPrice;+ return {+ input: roundCost(input),+ output: roundCost(output),+ total: roundCost(input + output),+ };+}++function traceProperties(context: TurnContext): Record<string, unknown> {+ return {+ ...context.base,+ ai_trace_id: context.traceId,+ ai_conversation_id: context.conversationId,+ };+}++function latency(rng: Rng, median: number): number {+ return Math.round(rng.logNormalSeconds(median, AI_LATENCY_SIGMA));+}++function tokenCount(rng: Rng, median: number, sigma: number): number {+ return Math.max(1, Math.round(rng.logNormalSeconds(median, sigma)));+}++function roundCost(value: number): number {+ return Number(value.toFixed(COST_DECIMALS));+}++function stringProperties(+ properties: Record<string, unknown>+): Record<string, string> {+ return Object.fromEntries(+ Object.entries(properties).map(([key, value]) => [key, String(value)])+ );+}++function emptyAiRow(+ context: TurnContext,+ type: SeedAiEvent['type'],+ spanId: string,+ parentId: string+): SeedAiEvent {+ return {+ type,+ trace_id: context.traceId,+ span_id: spanId,+ parent_id: parentId,+ conversation_id: context.conversationId,+ name: '',+ model: '',+ provider: '',+ input_tokens: 0,+ output_tokens: 0,+ cache_read_tokens: 0,+ cache_write_tokens: 0,+ reasoning_tokens: 0,+ input_cost: 0,+ output_cost: 0,+ total_cost: 0,+ cost_source: 'none',+ latency_ms: 0,+ time_to_first_token_ms: 0,+ is_error: false,+ http_status: 0,+ stop_reason: '',+ is_stream: false,+ model_parameters: '',+ input: '',+ output: '',+ tools: '',+ error: '',+ properties: stringProperties(context.base),+ };+}diff --git a/packages/seed/src/archetypes/saas.ts b/packages/seed/src/archetypes/saas.tsindex 9941190e0..b4b95e491 100644--- a/packages/seed/src/archetypes/saas.ts+++ b/packages/seed/src/archetypes/saas.ts@@ -4,21 +4,12 @@// subscription). Cohort-worthy behaviours: invited a teammate, exported,// connected an integration, paid plan.-import {- AI_ANSWERS,- AI_EMBEDDING_MODEL,- AI_ERRORS,- AI_FEATURES,- AI_MODELS,- AI_QUESTIONS,- AI_TOOLS,- type SeedAiModel,-} from '../data/ai';import { newPerson } from '../data/people';import { REFERRERS } from '../data/referrers';import type { Person, Visitor } from '../model';import type { Rng, Weighted } from '../rng';-import type { Archetype, Journey, SeedAiEvent } from './archetype';+import type { Archetype, Journey } from './archetype';+import { aiVisitChance, assistantConversation, insightJobs } from './saas-ai';interface Plan {name: string;@@ -31,6 +22,7 @@ const PLANS: readonly Weighted<Plan>[] = [{ weight: 9, value: { name: 'team', price: 99 } },{ weight: 3, value: { name: 'enterprise', price: 299 } },];+const SAAS_DWELL_MEDIAN_SECONDS = 45;const PAID_PLANS = PLANS.filter((plan) => plan.value.price > 0);const ROLES = ['founder', 'engineer', 'product', 'marketing', 'data'] as const;const COMPANY_SIZES = [@@ -234,318 +226,6 @@ function monetization(}}-/** Share of product sessions that talk to the assistant, by plan. */-const AI_SESSION_CHANCE: Record<string, number> = {- free: 0.12,- pro: 0.3,- team: 0.42,- enterprise: 0.5,-};-const AI_TURNS_MAX = 4;-const AI_RETRIEVAL_CHANCE = 0.35;-const AI_ERROR_CHANCE = 0.04;-const AI_TOOL_CALL_CHANCE = 0.3;-const AI_CACHE_HIT_CHANCE = 0.4;-const AI_INPUT_MEDIAN_TOKENS = 1800;-const AI_INPUT_SIGMA = 0.5;-const AI_OUTPUT_SIGMA = 0.6;-const AI_LATENCY_SIGMA = 0.45;-const AI_STREAM_CHANCE = 0.85;-const AI_TTFT_SHARE = 0.2;-const AI_RETRIEVAL_LATENCY_MEDIAN_MS = 80;-const AI_EMBEDDING_TOKENS_MEDIAN = 24;-const AI_CONTEXT_TOKENS_PER_TURN = 400;-const MS_PER_SECOND = 1000;-const COST_DECIMALS = 10;-const AI_TEMPERATURE = 0.2;--function roundCost(value: number): number {- return Number(value.toFixed(COST_DECIMALS));-}--function tokenCount(rng: Rng, median: number, sigma: number): number {- return Math.max(1, Math.round(rng.logNormalSeconds(median, sigma)));-}--interface AiCall {- model: SeedAiModel;- inputTokens: number;- outputTokens: number;- cacheReadTokens: number;- latencyMs: number;-}--function generationCost(call: AiCall): {- input: number;- output: number;- total: number;-} {- const { model } = call;- const input =- (call.inputTokens - call.cacheReadTokens) * model.inputPrice +- call.cacheReadTokens * model.cacheReadPrice;- const output = call.outputTokens * model.outputPrice;- return {- input: roundCost(input),- output: roundCost(output),- total: roundCost(input + output),- };-}--/** The flat `ai_*` keys the worker stores on the `events` row for a model call. */-function generationProperties(- call: AiCall,- context: Record<string, unknown>,- cost: number-): Record<string, unknown> {- return {- ...context,- ai_model: call.model.model,- ai_provider: call.model.provider,- ai_input_tokens: call.inputTokens,- ai_output_tokens: call.outputTokens,- ai_cache_read_tokens: call.cacheReadTokens,- ai_total_tokens: call.inputTokens + call.outputTokens,- ai_cost: cost,- ai_latency_ms: call.latencyMs,- };-}--/**- * One assistant conversation: a few turns, each one trace with an optional- * embedding + retrieval span before the generation. Mirrors what the- * `@openpanel/ai` middleware and `op.ai()` send, so the AI pages, the report- * builder and the profile timeline have realistic data.- */-function assistantConversation(- rng: Rng,- journey: Journey,- state: SaasState,- projectId: string-): void {- const plan = state.plan?.name ?? 'free';- const conversationId = `conv_${rng.hex(12)}`;- const feature = rng.pick(AI_FEATURES);- const turns = rng.int(1, AI_TURNS_MAX);- const base = { feature, plan, project: projectId };-- journey.view(`/projects/${projectId}/ai-assistant`, 'AI assistant', {- project: projectId,- });-- for (let turn = 0; turn < turns; turn++) {- const traceId = `trace_${rng.hex(16)}`;- const trace = {- ...base,- ai_trace_id: traceId,- ai_conversation_id: conversationId,- };- const question = rng.one([...AI_QUESTIONS]);- const answer = rng.one([...AI_ANSWERS]);-- if (rng.chance(AI_RETRIEVAL_CHANCE)) {- const embeddingTokens = tokenCount(rng, AI_EMBEDDING_TOKENS_MEDIAN, 0.3);- const embeddingCost = roundCost(- embeddingTokens * AI_EMBEDDING_MODEL.inputPrice- );- const embeddingLatency = Math.round(- rng.logNormalSeconds(AI_EMBEDDING_MODEL.latencyMedian, AI_LATENCY_SIGMA)- );- journey.ai(- 'ai_embedding',- {- ...trace,- ai_name: 'embed-question',- ai_model: AI_EMBEDDING_MODEL.model,- ai_provider: AI_EMBEDDING_MODEL.provider,- ai_input_tokens: embeddingTokens,- ai_total_tokens: embeddingTokens,- ai_cost: embeddingCost,- ai_latency_ms: embeddingLatency,- ai_is_error: false,- },- {- ...emptyAiRow(traceId, conversationId, 'embedding'),- name: 'embed-question',- model: AI_EMBEDDING_MODEL.model,- provider: AI_EMBEDDING_MODEL.provider,- input_tokens: embeddingTokens,- input_cost: embeddingCost,- total_cost: embeddingCost,- cost_source: 'catalog',- latency_ms: embeddingLatency,- input: JSON.stringify([question]),- properties: stringProperties(base),- },- embeddingLatency / MS_PER_SECOND- );- const retrievalLatency = Math.round(- rng.logNormalSeconds(AI_RETRIEVAL_LATENCY_MEDIAN_MS, AI_LATENCY_SIGMA)- );- journey.ai(- 'ai_span',- {- ...trace,- ai_name: 'search-docs',- ai_latency_ms: retrievalLatency,- ai_is_error: false,- },- {- ...emptyAiRow(traceId, conversationId, 'span'),- name: 'search-docs',- latency_ms: retrievalLatency,- input: JSON.stringify({ query: question, limit: 5 }),- output: JSON.stringify({ hits: rng.int(0, 5) }),- properties: stringProperties(base),- },- retrievalLatency / MS_PER_SECOND- );- }-- const model = rng.pick(AI_MODELS);- const inputTokens =- tokenCount(rng, AI_INPUT_MEDIAN_TOKENS, AI_INPUT_SIGMA) +- turn * AI_CONTEXT_TOKENS_PER_TURN;- const cacheReadTokens =- turn > 0 && rng.chance(AI_CACHE_HIT_CHANCE)- ? Math.round(inputTokens * rng.float() * 0.8)- : 0;- const failed = rng.chance(AI_ERROR_CHANCE);- const outputTokens = failed- ? 0- : tokenCount(rng, model.outputMedian, AI_OUTPUT_SIGMA);- const latencyMs = Math.round(- rng.logNormalSeconds(model.latencyMedian, AI_LATENCY_SIGMA) *- (failed ? 0.5 : 1)- );- const call: AiCall = {- model,- inputTokens,- outputTokens,- cacheReadTokens,- latencyMs,- };- const cost = generationCost(call);- const stream = rng.chance(AI_STREAM_CHANCE);- const timeToFirstTokenMs =- stream && !failed ? Math.round(latencyMs * AI_TTFT_SHARE) : 0;- const usedTool = !failed && rng.chance(AI_TOOL_CALL_CHANCE);- const stopReason = failed ? '' : usedTool ? 'tool-calls' : 'stop';- const error = failed ? rng.pick(AI_ERRORS) : null;- const input = [- { role: 'system', content: 'You are the OpenPanel analytics assistant.' },- { role: 'user', content: question },- ];- const output = failed- ? []- : [- {- role: 'assistant',- content: usedTool- ? [- {- type: 'tool_call',- toolName: 'get_report_data',- input: { question },- },- { type: 'text', text: answer },- ]- : answer,- },- ];-- journey.ai(- 'ai_generation',- {- ...generationProperties(call, trace, cost.total),- ai_name: feature,- ai_time_to_first_token_ms: timeToFirstTokenMs || undefined,- ai_is_error: failed,- ai_error_code: error?.code,- ai_http_status: error?.httpStatus,- ai_stop_reason: stopReason || undefined,- ai_stream: stream,- },- {- ...emptyAiRow(traceId, conversationId, 'generation'),- name: feature,- model: model.model,- provider: model.provider,- input_tokens: inputTokens,- output_tokens: outputTokens,- cache_read_tokens: cacheReadTokens,- input_cost: cost.input,- output_cost: cost.output,- total_cost: cost.total,- cost_source: 'catalog',- latency_ms: latencyMs,- time_to_first_token_ms: timeToFirstTokenMs,- is_error: failed,- http_status: error?.httpStatus ?? 0,- stop_reason: stopReason,- is_stream: stream,- model_parameters: JSON.stringify({ temperature: AI_TEMPERATURE }),- input: JSON.stringify(input),- output: JSON.stringify(output),- tools: JSON.stringify(AI_TOOLS),- error: error ? JSON.stringify(error) : '',- properties: stringProperties(base),- },- latencyMs / MS_PER_SECOND- );- }- journey.event('feature_used', {- feature: 'ai-assistant',- project: projectId,- });-}--function stringProperties(- properties: Record<string, unknown>-): Record<string, string> {- return Object.fromEntries(- Object.entries(properties).map(([key, value]) => [key, String(value)])- );-}--function emptyAiRow(- traceId: string,- conversationId: string,- type: SeedAiEvent['type']-): SeedAiEvent {- return {- type,- trace_id: traceId,- span_id: '',- parent_id: '',- conversation_id: conversationId,- name: '',- model: '',- provider: '',- input_tokens: 0,- output_tokens: 0,- cache_read_tokens: 0,- cache_write_tokens: 0,- reasoning_tokens: 0,- input_cost: 0,- output_cost: 0,- total_cost: 0,- cost_source: 'none',- latency_ms: 0,- time_to_first_token_ms: 0,- is_error: false,- http_status: 0,- stop_reason: '',- is_stream: false,- model_parameters: '',- input: '',- output: '',- tools: '',- error: '',- properties: {},- };-}-function usage(rng: Rng, journey: Journey, state: SaasState): void {const visits = rng.int(1, 3);for (let i = 0; i < visits; i++) {@@ -560,8 +240,9 @@ function usage(rng: Rng, journey: Journey, state: SaasState): void {});journey.event('feature_used', { feature, project: projectId });}- if (rng.chance(AI_SESSION_CHANCE[state.plan?.name ?? 'free'] ?? 0)) {- assistantConversation(rng, journey, state, projectId);+ const plan = state.plan?.name ?? 'free';+ if (rng.chance(aiVisitChance(plan))) {+ assistantConversation(rng, journey, plan, projectId);}if (rng.chance(0.6)) {journey.event('report_viewed', {@@ -644,7 +325,7 @@ export const saasArchetype: Archetype = {{ weight: 3, value: REFERRERS.linkedIn },],campaignChance: 0.03,- dwellMedianSeconds: 45,+ dwellMedianSeconds: SAAS_DWELL_MEDIAN_SECONDS,utcOffsetHours: -2,newPerson(rng) {const person = newPerson(rng, { business: true });@@ -676,6 +357,9 @@ export const saasArchetype: Archetype = {breakdowns: ['plan', 'interval', 'company_size'],},],+ background(rng, day) {+ return insightJobs(rng, day, SAAS_DWELL_MEDIAN_SECONDS);+ },journey(rng, journey, visitor) {const state = stateOf(visitor);const person = visitor.person;diff --git a/packages/seed/src/clickhouse-rows.test.ts b/packages/seed/src/clickhouse-rows.test.tsindex a0dd3cfb9..0b445bb9a 100644--- a/packages/seed/src/clickhouse-rows.test.ts+++ b/packages/seed/src/clickhouse-rows.test.ts@@ -130,3 +130,44 @@ describe('seed rows match the ClickHouse schema', () => {});}});++describe('AI traces are trees the trace view can draw', () => {+ test('one root per trace, and every other span hangs off a span in its trace', async () => {+ const sink = await sampleRows(AI_SAMPLE);+ const traces = Map.groupBy(sink.aiEvents, (row) => row.trace_id);+ expect(traces.size).toBeGreaterThan(0);+ for (const rows of traces.values()) {+ const roots = rows.filter((row) => row.type === 'trace');+ expect(roots).toHaveLength(1);+ expect(roots[0]?.parent_id).toBe('');+ expect(new Set(rows.map((row) => row.span_id)).size).toBe(rows.length);+ const parentOf = new Map(rows.map((row) => [row.span_id, row.parent_id]));+ const rootId = roots[0]?.span_id;+ for (const row of rows) {+ // Walking up must reach the root without revisiting a span (no cycles).+ const seen = new Set<string>();+ let current = row.span_id;+ while (current !== rootId) {+ expect(seen.has(current)).toBe(false);+ expect(parentOf.has(current)).toBe(true);+ seen.add(current);+ current = parentOf.get(current) ?? '';+ }+ }+ }+ });++ test('the root spans its children and carries no cost of its own', async () => {+ const sink = await sampleRows(AI_SAMPLE);+ const roots = sink.aiEvents.filter((row) => row.type === 'trace');+ for (const root of roots) {+ expect(root.total_cost).toBe(0);+ const children = sink.aiEvents.filter(+ (row) =>+ row.trace_id === root.trace_id && row.parent_id === root.span_id+ );+ const childMs = children.reduce((sum, row) => sum + row.latency_ms, 0);+ expect(root.latency_ms).toBe(childMs);+ }+ });+});diff --git a/packages/seed/src/data/ai.ts b/packages/seed/src/data/ai.tsindex 97240bdfd..e4177677c 100644--- a/packages/seed/src/data/ai.ts+++ b/packages/seed/src/data/ai.ts@@ -14,6 +14,8 @@ export interface SeedAiModel {outputMedian: number;/** Median latency in ms. */latencyMedian: number;+ /** Share of output tokens spent reasoning; reasoning models only. */+ reasoningShare: number;}export const AI_MODELS: readonly Weighted<SeedAiModel>[] = [@@ -27,6 +29,7 @@ export const AI_MODELS: readonly Weighted<SeedAiModel>[] = [cacheReadPrice: 2.5e-8,outputMedian: 180,latencyMedian: 1400,+ reasoningShare: 0,},},{@@ -39,6 +42,7 @@ export const AI_MODELS: readonly Weighted<SeedAiModel>[] = [cacheReadPrice: 1e-7,outputMedian: 320,latencyMedian: 2600,+ reasoningShare: 0.25,},},{@@ -51,6 +55,7 @@ export const AI_MODELS: readonly Weighted<SeedAiModel>[] = [cacheReadPrice: 1.25e-7,outputMedian: 400,latencyMedian: 3200,+ reasoningShare: 0.6,},},{@@ -63,10 +68,23 @@ export const AI_MODELS: readonly Weighted<SeedAiModel>[] = [cacheReadPrice: 1e-8,outputMedian: 120,latencyMedian: 900,+ reasoningShare: 0,},},];+/** The expensive model the report drafter escalates to. */+export const AI_PREMIUM_MODEL: SeedAiModel = {+ model: 'claude-opus-5-5',+ provider: 'anthropic',+ inputPrice: 1.5e-5,+ outputPrice: 7.5e-5,+ cacheReadPrice: 1.5e-6,+ outputMedian: 650,+ latencyMedian: 6500,+ reasoningShare: 0.4,+};+export const AI_EMBEDDING_MODEL: SeedAiModel = {model: 'text-embedding-3-small',provider: 'openai',@@ -75,6 +93,7 @@ export const AI_EMBEDDING_MODEL: SeedAiModel = {cacheReadPrice: 0,outputMedian: 0,latencyMedian: 120,+ reasoningShare: 0,};/** What the product uses the assistant for; `ai_name` on the generation. */@@ -109,8 +128,32 @@ export const AI_ANSWERS = [export const AI_TOOLS = [{ type: 'function', name: 'get_report_data', description: 'Run a report' },{ type: 'function', name: 'list_event_names', description: 'List events' },+ { type: 'function', name: 'get_funnel', description: 'Run a funnel' },] as const;+export interface SeedAiTool {+ name: (typeof AI_TOOLS)[number]['name'];+ /** Median tool latency in ms; queries are slower than lookups. */+ latencyMedian: number;+ /** Tokens the result adds to the next model call. */+ resultTokens: number;+}++export const AI_TOOL_CALLS: readonly Weighted<SeedAiTool>[] = [+ {+ weight: 55,+ value: { name: 'get_report_data', latencyMedian: 450, resultTokens: 1400 },+ },+ {+ weight: 25,+ value: { name: 'list_event_names', latencyMedian: 60, resultTokens: 300 },+ },+ {+ weight: 20,+ value: { name: 'get_funnel', latencyMedian: 700, resultTokens: 900 },+ },+];+export const AI_ERRORS: readonly Weighted<{message: string;code: string;diff --git a/packages/seed/src/generator.test.ts b/packages/seed/src/generator.test.tsindex 0ed935591..acc6e17b0 100644--- a/packages/seed/src/generator.test.ts+++ b/packages/seed/src/generator.test.ts@@ -110,7 +110,7 @@ describe('generate', () => {test('every session has one session_start, one session_end and a matching sessions row', async () => {const sink = await run();const bySession = new Map<string, ClickhouseEventRow[]>();- for (const event of sink.events) {+ for (const event of sessionEvents(sink)) {const list = bySession.get(event.session_id) ?? [];list.push(event);bySession.set(event.session_id, list);@@ -143,6 +143,21 @@ describe('generate', () => {}});+ test('background events carry no session or device, and stay in the past', async () => {+ const sink = await run();+ const background = sink.events.filter((event) => event.session_id === '');+ expect(background.length).toBeGreaterThan(0);+ const backgroundIds = new Set(background.map((event) => event.id));+ expect(sink.aiEvents.some((row) => backgroundIds.has(row.id))).toBe(true);+ for (const event of background) {+ expect(event.device_id).toBe('');+ expect(event.device).toBe('server');+ expect(new Date(`${event.created_at}Z`).getTime()).toBeLessThanOrEqual(+ NOW.getTime()+ );+ }+ });+test('events are ordered within a session and never in the future', async () => {const sink = await run();for (const event of sink.events) {@@ -155,7 +170,9 @@ describe('generate', () => {test('profiles cover every profile_id the events reference, once each', async () => {const sink = await run();const referenced = new Set(- sink.events.map((event) => `${event.project_id}/${event.profile_id}`)+ sessionEvents(sink).map(+ (event) => `${event.project_id}/${event.profile_id}`+ ));const written = sink.profiles.map((profile) => `${profile.project_id}/${profile.id}`@@ -173,7 +190,7 @@ describe('generate', () => {test('device and referrer fields come from the real parsers', async () => {const sink = await run();- const devices = new Set(sink.events.map((event) => event.device));+ const devices = new Set(sessionEvents(sink).map((event) => event.device));expect([...devices].every((device) =>['desktop', 'mobile', 'tablet'].includes(device)@@ -193,6 +210,11 @@ describe('generate', () => {const MS_PER_DAY = 86_400_000;+/** Background jobs write events with no session, device or visitor behind them. */+function sessionEvents(sink: MemorySink): ClickhouseEventRow[] {+ return sink.events.filter((event) => event.session_id !== '');+}+function dayOf(event: ClickhouseEventRow): string {return event.created_at.slice(0, 10);}diff --git a/packages/seed/src/generator.ts b/packages/seed/src/generator.tsindex 8af902220..397b82f36 100644--- a/packages/seed/src/generator.ts+++ b/packages/seed/src/generator.ts@@ -3,10 +3,10 @@// returning-visitor pool never hands out a visitor from the future.import type { Archetype } from './archetypes/archetype';-import { Journey } from './archetypes/archetype';+import { Journey, type JourneyEvent } from './archetypes/archetype';import { CAMPAIGNS, type Campaign } from './data/referrers';import { Rng } from './rng';-import { buildSessionRows } from './rows';+import { buildBackgroundRows, buildSessionRows } from './rows';import type { RowSink } from './sink';import { TrafficModel } from './traffic';import { World } from './world';@@ -78,6 +78,22 @@ function daysOf(config: GenerateConfig): Date[] {return days;}+/** Events up to `now`; a trace still running at `now` is left out whole, not cut. */+function settledEvents(+ events: readonly JourneyEvent[],+ now: Date+): JourneyEvent[] {+ const unfinished = new Set(+ events+ .filter((event) => event.ai && event.at > now)+ .map((event) => event.ai?.trace_id)+ );+ return events.filter(+ (event) =>+ event.at <= now && !(event.ai && unfinished.has(event.ai.trace_id))+ );+}+export function planDays(config: GenerateConfig): DayPlan[] {const plan: DayPlan[] = [];for (const { archetype } of config.projects) {@@ -219,6 +235,21 @@ export async function generate(}await sink.flushFull();}++ for (const background of archetype.background?.(rng, date) ?? []) {+ const events = settledEvents(background.events, config.now);+ const rows = buildBackgroundRows(rng, { projectId, events });+ sink.addEvents(rows.events);+ sink.addAiEvents(rows.aiEvents);+ projectStats.events += rows.events.length;+ const first = events[0]?.at;+ if (+ first &&+ (!projectStats.firstEventAt || first < projectStats.firstEventAt)+ ) {+ projectStats.firstEventAt = first;+ }+ }}world.drain();diff --git a/packages/seed/src/rows.ts b/packages/seed/src/rows.tsindex 5486fed14..12c075f61 100644--- a/packages/seed/src/rows.ts+++ b/packages/seed/src/rows.ts@@ -59,6 +59,8 @@ interface EventPayload {ai?: SeedAiEvent;}+const BACKGROUND_SDK = { name: 'node', version: '1.0.0' };+const UTM_KEYS = ['utm_medium','utm_source',@@ -308,6 +310,64 @@ export function buildSessionRows(rng: Rng, input: SessionInput): SessionRows {};}+export interface BackgroundRows {+ events: ClickhouseEventRow[];+ aiEvents: ClickhouseAiEventRow[];+}++/**+ * Events a backend sends with no browser behind them: no device, session,+ * geo or referrer, which is what `createEvent` stores for a server-side call.+ */+export function buildBackgroundRows(+ rng: Rng,+ input: { projectId: string; events: readonly JourneyEvent[] }+): BackgroundRows {+ const events: ClickhouseEventRow[] = [];+ const aiEvents: ClickhouseAiEventRow[] = [];+ for (const event of input.events) {+ const row: ClickhouseEventRow = {+ id: rng.uuid(),+ name: event.name,+ device_id: '',+ profile_id: event.person?.id ?? '',+ project_id: input.projectId,+ session_id: '',+ properties: toDots(event.properties),+ path: '',+ origin: '',+ created_at: toClickhouseDateTime(event.at),+ country: '',+ city: '',+ region: '',+ longitude: null,+ latitude: null,+ os: '',+ os_version: '',+ browser: '',+ browser_version: '',+ device: 'server',+ brand: '',+ model: '',+ duration: 0,+ referrer: '',+ referrer_name: '',+ referrer_type: '',+ imported_at: null,+ inserted_at: toClickhouseDateTime(new Date()),+ sdk_name: BACKGROUND_SDK.name,+ sdk_version: BACKGROUND_SDK.version,+ revenue: undefined,+ groups: [],+ };+ events.push(row);+ if (event.ai) {+ aiEvents.push(aiEventRow(event.ai, row));+ }+ }+ return { events, aiEvents };+}+/** The worker's `buildAiEventRow`: identity and time copied from the stored event. */function aiEventRow(ai: SeedAiEvent,diff --git a/packages/seed/src/seed.integration.test.ts b/packages/seed/src/seed.integration.test.tsindex d04d9f21f..524103b37 100644--- a/packages/seed/src/seed.integration.test.ts+++ b/packages/seed/src/seed.integration.test.ts@@ -113,7 +113,7 @@ describe('seed against real databases', () => {`SELECT count() AS value FROM sessions FINAL WHERE project_id IN (${inList()})`);const distinct = await count(- `SELECT uniqExact(session_id) AS value FROM events WHERE project_id IN (${inList()})`+ `SELECT uniqExact(session_id) AS value FROM events WHERE session_id != '' AND project_id IN (${inList()})`);const starts = await count(`SELECT count() AS value FROM events WHERE name = 'session_start' AND project_id IN (${inList()})`