Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor: centralize product url / path creation (#16291)

Ben Bachemfdfb68aba7ee
diff --git a/packages/shared/AGENTS.md b/packages/shared/AGENTS.md
index 7791acd883..fb10de4923 100644
--- a/packages/shared/AGENTS.md
+++ b/packages/shared/AGENTS.md
@@ -36,7 +36,8 @@
- `@langfuse/shared` via `src/index.ts`: default shared surface for
cross-runtime types, zod schemas, table definitions, domain models, prompt
- helpers, eval/model-pricing helpers, and other frontend-safe utilities.
+ helpers, eval/model-pricing helpers, product path builders, and other
+ frontend-safe utilities.
Includes the unicode-decoding JSON serialization helpers (`stringify`,
`stringifyForCsv` in `src/utils/stringify.ts`) used by both the server
trace-download route and client-side download/copy paths; the server barrel
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index fd7a3e7b10..8834199a8d 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -18,6 +18,7 @@ export * from "./utils/stringChecks";
export * from "./utils/objects";
export * from "./utils/typeChecks";
export * from "./utils/prompts";
+export * from "./utils/productUrl";
export * from "./utils/jsonSchemaValidation";
export * from "./utils/chatml";
export * from "./utils/math";
diff --git a/packages/shared/src/server/utils/productUrl.ts b/packages/shared/src/server/utils/productUrl.ts
index 50557405cf..c3eb0238e0 100644
--- a/packages/shared/src/server/utils/productUrl.ts
+++ b/packages/shared/src/server/utils/productUrl.ts
@@ -1,143 +1,19 @@
-import { TableViewPresetTableName } from "../../domain/table-view-presets";
import { type APIScoreV3 } from "../../features/scores/interfaces/api/v3/schemas";
-import { type FilterState } from "../../types";
-import { encodeFiltersGeneric } from "../../features/filters/filterQueryEncoding";
-import { getProductBaseUrl } from "./baseUrl";
import {
- rangeToString,
- type TABLE_AGGREGATION_OPTIONS,
-} from "../../utils/dateRanges";
-
-type ProductPathQuery = Record<string, string | string[] | null | undefined>;
-
-export function parseSavedViewFromURL(
- currentUrl: string,
- isV4Enabled: boolean,
-) {
- let parsedUrl: URL;
-
- try {
- parsedUrl = new URL(currentUrl, "https://langfuse.local");
- } catch {
- return undefined;
- }
-
- const pathSegments = parsedUrl.pathname.split("/").filter(Boolean);
- const projectSegmentIndex = pathSegments.indexOf("project");
- const section = pathSegments[projectSegmentIndex + 2];
- const detailId = pathSegments[projectSegmentIndex + 3];
- const viewId = parsedUrl.searchParams.get("viewId");
-
- if (projectSegmentIndex === -1 || !section || !viewId) {
- return undefined;
- }
-
- if (section === "traces" && !detailId) {
- return {
- viewId,
- tableName: isV4Enabled
- ? TableViewPresetTableName.ObservationsEvents
- : TableViewPresetTableName.Traces,
- };
- }
-
- if (section === "observations" && !detailId) {
- return {
- viewId,
- tableName: isV4Enabled
- ? TableViewPresetTableName.ObservationsEvents
- : TableViewPresetTableName.Observations,
- };
- }
-
- if (section === "sessions") {
- return {
- viewId,
- tableName: detailId
- ? TableViewPresetTableName.SessionDetail
- : TableViewPresetTableName.Sessions,
- };
- }
-
- if (section === "datasets" && !detailId) {
- return { viewId, tableName: TableViewPresetTableName.Datasets };
- }
-
- if (section === "scores" && !detailId) {
- return { viewId, tableName: TableViewPresetTableName.Scores };
- }
-
- if (section === "experiments" && detailId === "results") {
- return { viewId, tableName: TableViewPresetTableName.ExperimentItems };
- }
-
- if (section === "experiments" && !detailId) {
- return { viewId, tableName: TableViewPresetTableName.Experiments };
- }
-
- return undefined;
-}
-
-type TracesPathTimeRange =
- | { preset: (typeof TABLE_AGGREGATION_OPTIONS)[number] }
- | { from: string; to: string };
-
-type TracesPathFilters = {
- bookmarked?: boolean;
- environment?: string[];
- level?: string[];
- metadata?: Array<{ key: string; value: string }>;
- sessionId?: string[];
- tags?: string[];
- traceId?: string;
- traceName?: string[];
- userId?: string[];
- version?: string;
-};
-
-type TracesPathOrderBy = {
- column: "timestamp" | "startTime" | "traceName" | "latency";
- order: "ASC" | "DESC";
-};
-
-type TracesPathParams = {
- filters?: TracesPathFilters;
- orderBy?: TracesPathOrderBy;
- search?: {
- query: string;
- type?: string[];
- };
- timeRange?: TracesPathTimeRange;
-};
-
-export const appendProductPathQuery = (
- path: string,
- query: ProductPathQuery,
-): string => {
- const searchParams = new URLSearchParams();
-
- Object.entries(query).forEach(([key, value]) => {
- if (!value) {
- return;
- }
-
- if (Array.isArray(value)) {
- value.forEach((item) => {
- if (item) {
- searchParams.append(key, item);
- }
- });
- } else {
- searchParams.set(key, value);
- }
- });
+ buildDashboardWidgetPath,
+ buildDashboardsPath,
+ buildExperimentPath,
+ buildMonitorsPath,
+ buildSessionPath,
+ buildTracePath,
+} from "../../utils/productUrl";
+import { getProductBaseUrl } from "./baseUrl";
- const queryString = searchParams.toString();
+export * from "../../utils/productUrl";
- return queryString ? `${path}?${queryString}` : path;
-};
+type ProductUrlQuery = Record<string, string | string[] | null | undefined>;
-const buildProductUrl = (path: string, query?: ProductPathQuery) => {
+const buildProductUrl = (path: string, query?: ProductUrlQuery) => {
const baseUrl = getProductBaseUrl();
const basePath = baseUrl.pathname.replace(/\/$/, "");
const url = new URL(`${basePath}${path}`, baseUrl);
@@ -161,254 +37,6 @@ const buildProductUrl = (path: string, query?: ProductPathQuery) => {
return url.toString();
};
-export const buildProjectPath = (params: { projectId: string }) =>
- `/project/${encodeURIComponent(params.projectId)}`;
-
-export const buildDashboardsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/dashboards`;
-
-export const buildDashboardWidgetsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/widgets`;
-
-export const buildDashboardWidgetPath = (params: {
- projectId: string;
- widgetId: string;
-}) =>
- `${buildDashboardWidgetsPath(params)}/${encodeURIComponent(params.widgetId)}`;
-
-export const buildDatasetsPath = (params: {
- projectId: string;
- folder?: string;
-}) =>
- appendProductPathQuery(`${buildProjectPath(params)}/datasets`, {
- folder: params.folder,
- });
-
-export const buildEvalsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/evals`;
-
-export const buildExperimentsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/experiments`;
-
-export const buildExperimentPath = (params: {
- projectId: string;
- experimentId: string;
-}) =>
- appendProductPathQuery(`${buildExperimentsPath(params)}/results`, {
- baseline: params.experimentId,
- });
-
-export const buildModelsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/models`;
-
-export const buildMonitorsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/alerts`;
-
-export const buildPlaygroundPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/playground`;
-
-export const buildProjectMembersPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/settings/members`;
-
-export const buildProjectSettingsPath = (params: {
- projectId: string;
- page?: string;
-}) =>
- params.page && params.page !== "index"
- ? `${buildProjectPath(params)}/settings/${params.page}`
- : `${buildProjectPath(params)}/settings`;
-
-export const buildPromptsPath = (params: {
- projectId: string;
- folder?: string;
-}) =>
- appendProductPathQuery(`${buildProjectPath(params)}/prompts`, {
- folder: params.folder,
- });
-
-export const buildScoresPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/scores`;
-
-export const buildTracePath = (params: {
- projectId: string;
- traceId: string;
- timestamp?: string;
-}) =>
- appendProductPathQuery(
- `${buildProjectPath(params)}/traces/${encodeURIComponent(params.traceId)}`,
- { timestamp: params.timestamp },
- );
-
-export const buildTracesPath = (params: {
- projectId: string;
- isV4Enabled?: boolean;
- params?: TracesPathParams;
- query?: ProductPathQuery;
-}) =>
- appendProductPathQuery(
- `${buildProjectPath(params)}/traces`,
- params.params
- ? {
- ...getTracesPathQuery(params.params, Boolean(params.isV4Enabled)),
- ...params.query,
- }
- : (params.query ?? {}),
- );
-
-function getTracesPathQuery(
- params: TracesPathParams,
- isV4Enabled: boolean,
-): ProductPathQuery {
- const filters = params.filters
- ? getTracesFilterState(params.filters, isV4Enabled)
- : [];
- const orderBy = params.orderBy
- ? normalizeTracesOrderBy(params.orderBy, isV4Enabled)
- : undefined;
-
- return {
- dateRange: params.timeRange
- ? getDateRangeQueryValue(params.timeRange)
- : undefined,
- filter: filters.length > 0 ? encodeFiltersGeneric(filters) : undefined,
- orderBy: orderBy
- ? `column-${orderBy.column}_order-${orderBy.order}`
- : undefined,
- search: params.search?.query,
- searchType: params.search?.type,
- };
-}
-
-function getDateRangeQueryValue(timeRange: TracesPathTimeRange) {
- if ("preset" in timeRange) {
- return rangeToString({ range: timeRange.preset });
- }
-
- return rangeToString({
- from: new Date(timeRange.from),
- to: new Date(timeRange.to),
- });
-}
-
-function getTracesFilterState(
- filters: TracesPathFilters,
- isV4Enabled: boolean,
-): FilterState {
- const filterState: FilterState = [];
-
- const addStringOptionsFilter = (
- column: string,
- value: string[] | undefined,
- ) => {
- if (!value || value.length === 0) {
- return;
- }
-
- filterState.push({
- column,
- operator: "any of",
- type: "stringOptions",
- value,
- });
- };
-
- const addArrayOptionsFilter = (
- column: string,
- value: string[] | undefined,
- ) => {
- if (!value || value.length === 0) {
- return;
- }
-
- filterState.push({
- column,
- operator: "any of",
- type: "arrayOptions",
- value,
- });
- };
-
- addStringOptionsFilter("environment", filters.environment);
- addStringOptionsFilter("level", filters.level);
- if (!isV4Enabled) {
- addStringOptionsFilter("sessionId", filters.sessionId);
- }
- addStringOptionsFilter("traceName", filters.traceName);
- addStringOptionsFilter("userId", filters.userId);
- addArrayOptionsFilter(isV4Enabled ? "tags" : "traceTags", filters.tags);
-
- if (!isV4Enabled && filters.bookmarked !== undefined) {
- filterState.push({
- column: "bookmarked",
- operator: "=",
- type: "boolean",
- value: filters.bookmarked,
- });
- }
-
- if (filters.traceId) {
- filterState.push({
- column: isV4Enabled ? "traceId" : "id",
- operator: "=",
- type: "string",
- value: filters.traceId,
- });
- }
-
- if (filters.version) {
- if (isV4Enabled) {
- filterState.push({
- column: "version",
- operator: "any of",
- type: "stringOptions",
- value: [filters.version],
- });
- } else {
- filterState.push({
- column: "version",
- operator: "=",
- type: "string",
- value: filters.version,
- });
- }
- }
-
- for (const metadataFilter of filters.metadata ?? []) {
- filterState.push({
- column: "metadata",
- key: metadataFilter.key,
- operator: "=",
- type: "stringObject",
- value: metadataFilter.value,
- });
- }
-
- return filterState;
-}
-
-function normalizeTracesOrderBy(
- orderBy: TracesPathOrderBy,
- isV4Enabled: boolean,
-) {
- if (orderBy.column === "timestamp" || orderBy.column === "startTime") {
- return {
- column: isV4Enabled ? "startTime" : "timestamp",
- order: orderBy.order,
- };
- }
-
- return orderBy;
-}
-
-export const buildSessionPath = (params: {
- projectId: string;
- sessionId: string;
-}) =>
- `${buildProjectPath(params)}/sessions/${encodeURIComponent(params.sessionId)}`;
-
-export const buildSessionsPath = (params: { projectId: string }) =>
- `${buildProjectPath(params)}/sessions`;
-
export const buildTraceUrl = (params: { projectId: string; traceId: string }) =>
buildProductUrl(buildTracePath(params));
@@ -416,10 +44,7 @@ export const buildObservationUrl = (params: {
projectId: string;
traceId: string;
observationId: string;
-}) =>
- buildProductUrl(buildTracePath(params), {
- observation: params.observationId,
- });
+}) => buildProductUrl(buildTracePath(params));
export const buildSessionUrl = (params: {
projectId: string;
diff --git a/packages/shared/src/utils/productUrl.ts b/packages/shared/src/utils/productUrl.ts
new file mode 100644
index 0000000000..c8153ecf1e
--- /dev/null
+++ b/packages/shared/src/utils/productUrl.ts
@@ -0,0 +1,400 @@
+import { TableViewPresetTableName } from "../domain/table-view-presets";
+import { type FilterState } from "../types";
+import { encodeFiltersGeneric } from "../features/filters/filterQueryEncoding";
+import { rangeToString, type TABLE_AGGREGATION_OPTIONS } from "./dateRanges";
+
+type ProductPathQuery = Record<string, string | string[] | null | undefined>;
+
+export function parseSavedViewFromURL(
+ currentUrl: string,
+ isV4Enabled: boolean,
+) {
+ let parsedUrl: URL;
+
+ try {
+ parsedUrl = new URL(currentUrl, "https://langfuse.local");
+ } catch {
+ return undefined;
+ }
+
+ const pathSegments = parsedUrl.pathname.split("/").filter(Boolean);
+ const projectSegmentIndex = pathSegments.indexOf("project");
+ const section = pathSegments[projectSegmentIndex + 2];
+ const detailId = pathSegments[projectSegmentIndex + 3];
+ const viewId = parsedUrl.searchParams.get("viewId");
+
+ if (projectSegmentIndex === -1 || !section || !viewId) {
+ return undefined;
+ }
+
+ if (section === "traces" && !detailId) {
+ return {
+ viewId,
+ tableName: isV4Enabled
+ ? TableViewPresetTableName.ObservationsEvents
+ : TableViewPresetTableName.Traces,
+ };
+ }
+
+ if (section === "observations" && !detailId) {
+ return {
+ viewId,
+ tableName: isV4Enabled
+ ? TableViewPresetTableName.ObservationsEvents
+ : TableViewPresetTableName.Observations,
+ };
+ }
+
+ if (section === "sessions") {
+ return {
+ viewId,
+ tableName: detailId
+ ? TableViewPresetTableName.SessionDetail
+ : TableViewPresetTableName.Sessions,
+ };
+ }
+
+ if (section === "datasets" && !detailId) {
+ return { viewId, tableName: TableViewPresetTableName.Datasets };
+ }
+
+ if (section === "scores" && !detailId) {
+ return { viewId, tableName: TableViewPresetTableName.Scores };
+ }
+
+ if (section === "experiments" && detailId === "results") {
+ return { viewId, tableName: TableViewPresetTableName.ExperimentItems };
+ }
+
+ if (section === "experiments" && !detailId) {
+ return { viewId, tableName: TableViewPresetTableName.Experiments };
+ }
+
+ return undefined;
+}
+
+type TracesPathTimeRange =
+ | { preset: (typeof TABLE_AGGREGATION_OPTIONS)[number] }
+ | { from: string; to: string };
+
+type TracesPathFilters = {
+ bookmarked?: boolean;
+ environment?: string[];
+ level?: string[];
+ metadata?: Array<{ key: string; value: string }>;
+ sessionId?: string[];
+ tags?: string[];
+ traceId?: string;
+ traceName?: string[];
+ userId?: string[];
+ version?: string;
+};
+
+type TracesPathOrderBy = {
+ column: "timestamp" | "startTime" | "traceName" | "latency";
+ order: "ASC" | "DESC";
+};
+
+type TracesPathParams = {
+ filters?: TracesPathFilters;
+ orderBy?: TracesPathOrderBy;
+ search?: {
+ query: string;
+ type?: string[];
+ };
+ timeRange?: TracesPathTimeRange;
+};
+
+export const appendProductPathQuery = (
+ path: string,
+ query: ProductPathQuery,
+): string => {
+ const searchParams = new URLSearchParams();
+
+ Object.entries(query).forEach(([key, value]) => {
+ if (!value) {
+ return;
+ }
+
+ if (Array.isArray(value)) {
+ value.forEach((item) => {
+ if (item) {
+ searchParams.append(key, item);
+ }
+ });
+ } else {
+ searchParams.set(key, value);
+ }
+ });
+
+ const queryString = searchParams.toString();
+
+ return queryString ? `${path}?${queryString}` : path;
+};
+
+export const buildProjectPath = (params: { projectId: string }) =>
+ `/project/${encodeURIComponent(params.projectId)}`;
+
+export const buildDashboardsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/dashboards`;
+
+export const buildDashboardWidgetsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/widgets`;
+
+export const buildDashboardWidgetPath = (params: {
+ projectId: string;
+ widgetId: string;
+}) =>
+ `${buildDashboardWidgetsPath(params)}/${encodeURIComponent(params.widgetId)}`;
+
+export const buildDatasetsPath = (params: {
+ projectId: string;
+ folder?: string;
+}) =>
+ appendProductPathQuery(`${buildProjectPath(params)}/datasets`, {
+ folder: params.folder,
+ });
+
+export const buildEvalsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/evals`;
+
+export const buildExperimentsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/experiments`;
+
+export const buildExperimentPath = (params: {
+ projectId: string;
+ experimentId: string;
+}) =>
+ appendProductPathQuery(`${buildExperimentsPath(params)}/results`, {
+ baseline: params.experimentId,
+ });
+
+export const buildModelsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/models`;
+
+export const buildMonitorsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/alerts`;
+
+export const buildPlaygroundPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/playground`;
+
+export const buildProjectMembersPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/settings/members`;
+
+export const buildProjectSettingsPath = (params: {
+ projectId: string;
+ page?: string;
+}) =>
+ params.page && params.page !== "index"
+ ? `${buildProjectPath(params)}/settings/${params.page}`
+ : `${buildProjectPath(params)}/settings`;
+
+export const buildPromptsPath = (params: {
+ projectId: string;
+ folder?: string;
+}) =>
+ appendProductPathQuery(`${buildProjectPath(params)}/prompts`, {
+ folder: params.folder,
+ });
+
+export const buildScoresPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/scores`;
+
+export const buildTracePath = (params: {
+ projectId: string;
+ traceId: string;
+ observationId?: string | null;
+ timestamp?: Date | string | null;
+}) =>
+ appendProductPathQuery(
+ `${buildProjectPath(params)}/traces/${encodeURIComponent(params.traceId)}`,
+ {
+ observation: params.observationId,
+ timestamp:
+ params.timestamp instanceof Date
+ ? params.timestamp.toISOString()
+ : decodeURIComponentSafely(params.timestamp),
+ },
+ );
+
+const decodeURIComponentSafely = (value?: string | null) => {
+ if (!value) {
+ return undefined;
+ }
+
+ try {
+ return decodeURIComponent(value);
+ } catch {
+ return value;
+ }
+};
+
+export const buildTracesPath = (params: {
+ projectId: string;
+ isV4Enabled?: boolean;
+ params?: TracesPathParams;
+ query?: ProductPathQuery;
+}) =>
+ appendProductPathQuery(
+ `${buildProjectPath(params)}/traces`,
+ params.params
+ ? {
+ ...getTracesPathQuery(params.params, Boolean(params.isV4Enabled)),
+ ...params.query,
+ }
+ : (params.query ?? {}),
+ );
+
+function getTracesPathQuery(
+ params: TracesPathParams,
+ isV4Enabled: boolean,
+): ProductPathQuery {
+ const filters = params.filters
+ ? getTracesFilterState(params.filters, isV4Enabled)
+ : [];
+ const orderBy = params.orderBy
+ ? normalizeTracesOrderBy(params.orderBy, isV4Enabled)
+ : undefined;
+
+ return {
+ dateRange: params.timeRange
+ ? getDateRangeQueryValue(params.timeRange)
+ : undefined,
+ filter: filters.length > 0 ? encodeFiltersGeneric(filters) : undefined,
+ orderBy: orderBy
+ ? `column-${orderBy.column}_order-${orderBy.order}`
+ : undefined,
+ search: params.search?.query,
+ searchType: params.search?.type,
+ };
+}
+
+function getDateRangeQueryValue(timeRange: TracesPathTimeRange) {
+ if ("preset" in timeRange) {
+ return rangeToString({ range: timeRange.preset });
+ }
+
+ return rangeToString({
+ from: new Date(timeRange.from),
+ to: new Date(timeRange.to),
+ });
+}
+
+function getTracesFilterState(
+ filters: TracesPathFilters,
+ isV4Enabled: boolean,
+): FilterState {
+ const filterState: FilterState = [];
+
+ const addStringOptionsFilter = (
+ column: string,
+ value: string[] | undefined,
+ ) => {
+ if (!value || value.length === 0) {
+ return;
+ }
+
+ filterState.push({
+ column,
+ operator: "any of",
+ type: "stringOptions",
+ value,
+ });
+ };
+
+ const addArrayOptionsFilter = (
+ column: string,
+ value: string[] | undefined,
+ ) => {
+ if (!value || value.length === 0) {
+ return;
+ }
+
+ filterState.push({
+ column,
+ operator: "any of",
+ type: "arrayOptions",
+ value,
+ });
+ };
+
+ addStringOptionsFilter("environment", filters.environment);
+ addStringOptionsFilter("level", filters.level);
+ if (!isV4Enabled) {
+ addStringOptionsFilter("sessionId", filters.sessionId);
+ }
+ addStringOptionsFilter("traceName", filters.traceName);
+ addStringOptionsFilter("userId", filters.userId);
+ addArrayOptionsFilter(isV4Enabled ? "tags" : "traceTags", filters.tags);
+
+ if (!isV4Enabled && filters.bookmarked !== undefined) {
+ filterState.push({
+ column: "bookmarked",
+ operator: "=",
+ type: "boolean",
+ value: filters.bookmarked,
+ });
+ }
+
+ if (filters.traceId) {
+ filterState.push({
+ column: isV4Enabled ? "traceId" : "id",
+ operator: "=",
+ type: "string",
+ value: filters.traceId,
+ });
+ }
+
+ if (filters.version) {
+ if (isV4Enabled) {
+ filterState.push({
+ column: "version",
+ operator: "any of",
+ type: "stringOptions",
+ value: [filters.version],
+ });
+ } else {
+ filterState.push({
+ column: "version",
+ operator: "=",
+ type: "string",
+ value: filters.version,
+ });
+ }
+ }
+
+ for (const metadataFilter of filters.metadata ?? []) {
+ filterState.push({
+ column: "metadata",
+ key: metadataFilter.key,
+ operator: "=",
+ type: "stringObject",
+ value: metadataFilter.value,
+ });
+ }
+
+ return filterState;
+}
+
+function normalizeTracesOrderBy(
+ orderBy: TracesPathOrderBy,
+ isV4Enabled: boolean,
+) {
+ if (orderBy.column === "timestamp" || orderBy.column === "startTime") {
+ return {
+ column: isV4Enabled ? "startTime" : "timestamp",
+ order: orderBy.order,
+ };
+ }
+
+ return orderBy;
+}
+
+export const buildSessionPath = (params: {
+ projectId: string;
+ sessionId: string;
+}) =>
+ `${buildProjectPath(params)}/sessions/${encodeURIComponent(params.sessionId)}`;
+
+export const buildSessionsPath = (params: { projectId: string }) =>
+ `${buildProjectPath(params)}/sessions`;
diff --git a/web/src/__tests__/navigation.clienttest.ts b/web/src/__tests__/product-url.clienttest.ts
similarity index 83%
rename from web/src/__tests__/navigation.clienttest.ts
rename to web/src/__tests__/product-url.clienttest.ts
index 033f4646ef..9df058b5e5 100644
--- a/web/src/__tests__/navigation.clienttest.ts
+++ b/web/src/__tests__/product-url.clienttest.ts
@@ -1,11 +1,11 @@
// @vitest-environment node
-import { buildTraceDetailPath } from "@/src/utils/navigation";
+import { buildTracePath } from "@langfuse/shared";
-describe("buildTraceDetailPath", () => {
+describe("buildTracePath", () => {
it("builds a trace path without query params", () => {
expect(
- buildTraceDetailPath({
+ buildTracePath({
projectId: "project-1",
traceId: "trace/1",
}),
@@ -14,7 +14,7 @@ describe("buildTraceDetailPath", () => {
it("adds observation and timestamp query params", () => {
expect(
- buildTraceDetailPath({
+ buildTracePath({
projectId: "project-1",
traceId: "trace-1",
observationId: "observation-1",
@@ -27,7 +27,7 @@ describe("buildTraceDetailPath", () => {
it("normalizes an encoded timestamp string", () => {
expect(
- buildTraceDetailPath({
+ buildTracePath({
projectId: "project-1",
traceId: "trace-1",
observationId: "observation-1",
diff --git a/web/src/components/table/peek/peek-observation-detail.tsx b/web/src/components/table/peek/peek-observation-detail.tsx
index 312ef3c891..2dd0f5d153 100644
--- a/web/src/components/table/peek/peek-observation-detail.tsx
+++ b/web/src/components/table/peek/peek-observation-detail.tsx
@@ -9,7 +9,7 @@ import {
traceDetailTitle,
} from "@/src/features/traces";
import { resolvePeekTraceParams } from "@/src/components/table/peek/resolvePeekTraceParams";
-import { buildTraceDetailPath } from "@/src/utils/navigation";
+import { buildTracePath } from "@langfuse/shared";
import { useRouter } from "next/router";
import { useRef } from "react";
@@ -49,7 +49,7 @@ export const TablePeekViewObservationDetail = (
traceId: trace.data.id,
projectId: trace.data.projectId,
isPublic: trace.data.public,
- shareUrl: buildTraceDetailPath({
+ shareUrl: buildTracePath({
projectId: trace.data.projectId,
traceId: trace.data.id,
observationId:
diff --git a/web/src/components/table/peek/peek-trace-detail.tsx b/web/src/components/table/peek/peek-trace-detail.tsx
index 394b41b68f..506534aea1 100644
--- a/web/src/components/table/peek/peek-trace-detail.tsx
+++ b/web/src/components/table/peek/peek-trace-detail.tsx
@@ -11,7 +11,7 @@ import {
shouldClosePeekAfterDelete,
} from "@/src/components/table/peek";
import { resolvePeekTraceParams } from "@/src/components/table/peek/resolvePeekTraceParams";
-import { buildTraceDetailPath } from "@/src/utils/navigation";
+import { buildTracePath } from "@langfuse/shared";
export const TablePeekViewTraceDetail = (
props: Omit<
@@ -48,7 +48,7 @@ export const TablePeekViewTraceDetail = (
traceId: trace.data.id,
projectId: trace.data.projectId,
isPublic: trace.data.public,
- shareUrl: buildTraceDetailPath({
+ shareUrl: buildTracePath({
projectId: trace.data.projectId,
traceId: trace.data.id,
timestamp,
diff --git a/web/src/components/table/use-cases/observations.tsx b/web/src/components/table/use-cases/observations.tsx
index 52cebcf18e..32ae2ca766 100644
--- a/web/src/components/table/use-cases/observations.tsx
+++ b/web/src/components/table/use-cases/observations.tsx
@@ -97,7 +97,7 @@ import { useScoreColumns } from "@/src/features/scores/hooks/useScoreColumns";
import { scoreFilters } from "@/src/features/scores/lib/scoreColumns";
import { AddObservationsToDatasetDialog } from "@/src/features/batch-actions/components/AddObservationsToDatasetDialog/index";
import useSessionStorage from "@/src/components/useSessionStorage";
-import { buildTraceDetailPath } from "@/src/utils/navigation";
+import { buildTracePath } from "@langfuse/shared";
import { getSafeRedirectPath } from "@/src/utils/redirect";
import {
REFRESH_INTERVALS,
@@ -1537,7 +1537,7 @@ export default function ObservationsTable({
const timestamp = row.timestamp;
if (traceId) {
- const observationUrl = buildTraceDetailPath({
+ const observationUrl = buildTracePath({
projectId,
traceId,
observationId,
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 3b1e27db39..fece45d90d 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -107,7 +107,7 @@ import {
} from "@/src/features/events/hooks/useAppRootDefault";
import { getAppRootSavedViewComparisonFilters } from "@/src/features/events/lib/appRootDefaultFilterPolicy";
import { useEventsFilterOptions } from "@/src/features/events/hooks/useEventsFilterOptions";
-import { buildTraceDetailPath } from "@/src/utils/navigation";
+import { buildTracePath } from "@langfuse/shared";
import { getSafeRedirectPath } from "@/src/utils/redirect";
// Disabled for now because perhaps confusing
// import {
@@ -2368,7 +2368,7 @@ export default function ObservationsEventsTable({
const timestamp = row.timestamp;
if (traceId) {
- const observationUrl = buildTraceDetailPath({
+ const observationUrl = buildTracePath({
projectId,
traceId,
observationId,
diff --git a/web/src/utils/navigation.ts b/web/src/utils/navigation.ts
index 42d158cada..a43bb98c91 100644
--- a/web/src/utils/navigation.ts
+++ b/web/src/utils/navigation.ts
@@ -12,50 +12,3 @@ export function urlSearchParamsToQuery(
return query;
}
-
-type BuildTraceDetailPathParams = {
- projectId: string;
- traceId: string;
- observationId?: string | null;
- timestamp?: Date | string | null;
-};
-
-function normalizeTimestampParam(
- timestamp: BuildTraceDetailPathParams["timestamp"],
-) {
- if (!timestamp) {
- return null;
- }
-
- if (timestamp instanceof Date) {
- return timestamp.toISOString();
- }
-
- try {
- return decodeURIComponent(timestamp);
- } catch {
- return timestamp;
- }
-}
-
-export function buildTraceDetailPath({
- projectId,
- traceId,
- observationId,
- timestamp,
-}: BuildTraceDetailPathParams) {
- const params = new URLSearchParams();
-
- if (observationId) {
- params.set("observation", observationId);
- }
-
- const normalizedTimestamp = normalizeTimestampParam(timestamp);
- if (normalizedTimestamp) {
- params.set("timestamp", normalizedTimestamp);
- }
-
- const query = params.toString();
-
- return `/project/${projectId}/traces/${encodeURIComponent(traceId)}${query ? `?${query}` : ""}`;
-}