Skip to content

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

chore: add traces domain layer (#6253)

Max Deichmanncb900c61f801
diff --git a/packages/shared/src/domain.ts b/packages/shared/src/domain.ts
new file mode 100644
index 0000000000..cce6afa3e1
--- /dev/null
+++ b/packages/shared/src/domain.ts
@@ -0,0 +1,30 @@
+import z from "zod";
+import { jsonSchema, jsonSchemaNullable } from "./utils/zod";
+
+const MetadataDomain = z.record(
+ z.string(),
+ jsonSchemaNullable.or(z.undefined()),
+);
+
+// to be used across the application in frontend and backend.
+export const TraceDomain = z.object({
+ id: z.string(),
+ name: z.string().nullable(),
+ timestamp: z.date(),
+ environment: z.string(),
+ tags: z.array(z.string()),
+ bookmarked: z.boolean(),
+ public: z.boolean(),
+ release: z.string().nullable(),
+ version: z.string().nullable(),
+ input: jsonSchema.nullable(),
+ output: jsonSchema.nullable(),
+ metadata: MetadataDomain,
+ createdAt: z.date(),
+ updatedAt: z.date(),
+ sessionId: z.string().nullable(),
+ userId: z.string().nullable(),
+ projectId: z.string(),
+});
+
+export type TraceDomain = z.infer<typeof TraceDomain>;
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index be3d2f5ac8..506986409f 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -50,3 +50,6 @@ export * from "./server/repositories/types";
export * from "./errors/index";
export * from "./utils/environment";
+
+// domain
+export * from "./domain";
diff --git a/packages/shared/src/server/repositories/traces_converters.ts b/packages/shared/src/server/repositories/traces_converters.ts
index 7a4c604016..c7214fa90d 100644
--- a/packages/shared/src/server/repositories/traces_converters.ts
+++ b/packages/shared/src/server/repositories/traces_converters.ts
@@ -1,12 +1,11 @@
-import { Prisma } from "@prisma/client";
import { parseClickhouseUTCDateTimeFormat } from "./clickhouse";
import { TraceRecordReadType } from "./definitions";
import { convertDateToClickhouseDateTime } from "../clickhouse/client";
import { parseJsonPrioritised } from "../../utils/json";
-import { Trace } from "./types";
+import { TraceDomain } from "../../domain";
export const convertTraceDomainToClickhouse = (
- trace: Trace,
+ trace: TraceDomain,
): TraceRecordReadType => {
return {
id: trace.id,
@@ -33,7 +32,7 @@ export const convertTraceDomainToClickhouse = (
export const convertClickhouseToDomain = (
record: TraceRecordReadType,
-): Trace => {
+): TraceDomain => {
return {
id: record.id,
projectId: record.project_id,
@@ -47,22 +46,19 @@ export const convertClickhouseToDomain = (
userId: record.user_id ?? null,
sessionId: record.session_id ?? null,
public: record.public,
- input: (record.input
- ? parseJsonPrioritised(record.input)
- : null) as Prisma.JsonValue | null,
- output: (record.output
- ? parseJsonPrioritised(record.output)
- : null) as Prisma.JsonValue | null,
- metadata:
- record.metadata &&
- Object.fromEntries(
- Object.entries(record.metadata ?? {}).map(([key, val]) => [
- key,
- val && parseJsonPrioritised(val),
- ]),
- ),
+ input: record.input ? (parseJsonPrioritised(record.input) ?? null) : null,
+ output: record.output
+ ? (parseJsonPrioritised(record.output) ?? null)
+ : null,
+ metadata: record.metadata
+ ? Object.fromEntries(
+ Object.entries(record.metadata).map(([key, val]) => [
+ key,
+ val === null ? null : parseJsonPrioritised(val),
+ ]),
+ )
+ : {},
createdAt: parseClickhouseUTCDateTimeFormat(record.created_at),
updatedAt: parseClickhouseUTCDateTimeFormat(record.updated_at),
- externalId: null,
};
};
diff --git a/packages/shared/src/server/repositories/types.ts b/packages/shared/src/server/repositories/types.ts
index ed64681c6e..380cc37b6b 100644
--- a/packages/shared/src/server/repositories/types.ts
+++ b/packages/shared/src/server/repositories/types.ts
@@ -121,24 +121,3 @@ export type Score = {
updatedAt: Date;
dataType: ScoreDataType;
};
-
-export type Trace = {
- id: string;
- timestamp: Date;
- name: string | null;
- userId: string | null;
- environment: string;
- metadata: Prisma.JsonValue | null;
- release: string | null;
- version: string | null;
- projectId: string;
- public: boolean;
- bookmarked: boolean;
- tags: string[];
- input: unknown | null;
- output: unknown | null;
- sessionId: string | null;
- createdAt: Date;
- updatedAt: Date;
- externalId: string | null;
-};
diff --git a/packages/shared/src/server/services/traces-ui-table-service.ts b/packages/shared/src/server/services/traces-ui-table-service.ts
index f6af339715..c58629d243 100644
--- a/packages/shared/src/server/services/traces-ui-table-service.ts
+++ b/packages/shared/src/server/services/traces-ui-table-service.ts
@@ -23,6 +23,7 @@ import {
parseClickhouseUTCDateTimeFormat,
queryClickhouse,
} from "../repositories";
+import { TraceDomain } from "../../domain";
export type TracesTableReturnType = Pick<
TraceRecordReadType,
@@ -40,20 +41,21 @@ export type TracesTableReturnType = Pick<
| "public"
>;
-export type TracesAllUiReturnType = {
- id: string;
- timestamp: Date;
- name: string | null;
- projectId: string;
- userId: string | null;
- release: string | null;
- version: string | null;
- public: boolean;
- bookmarked: boolean;
- environment: string | null;
- sessionId: string | null;
- tags: string[];
-};
+export type TracesTableUiReturnType = Pick<
+ TraceDomain,
+ | "id"
+ | "projectId"
+ | "timestamp"
+ | "tags"
+ | "bookmarked"
+ | "name"
+ | "release"
+ | "version"
+ | "userId"
+ | "environment"
+ | "sessionId"
+ | "public"
+>;
export type TracesMetricsUiReturnType = {
id: string;
@@ -78,7 +80,7 @@ export type TracesMetricsUiReturnType = {
export const convertToUiTableRows = (
row: TracesTableReturnType,
-): TracesAllUiReturnType => {
+): TracesTableUiReturnType => {
return {
id: row.id,
projectId: row.project_id,
diff --git a/web/src/components/trace/ObservationTree.tsx b/web/src/components/trace/ObservationTree.tsx
index 1c79a5ed26..c9fe543185 100644
--- a/web/src/components/trace/ObservationTree.tsx
+++ b/web/src/components/trace/ObservationTree.tsx
@@ -2,10 +2,10 @@ import { type NestedObservation } from "@/src/utils/types";
import { cn } from "@/src/utils/tailwind";
import {
type APIScore,
- type Trace,
ObservationLevel,
type ObservationLevelType,
type ObservationType,
+ type TraceDomain,
} from "@langfuse/shared";
import { GroupedScoreBadges } from "@/src/components/grouped-score-badge";
import { Fragment, useMemo, useRef, useEffect } from "react";
@@ -43,7 +43,7 @@ export const ObservationTree = ({
toggleCollapsedObservation: (id: string) => void;
collapseAll: () => void;
expandAll: () => void;
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
latency?: number;
input: string | undefined;
output: string | undefined;
@@ -129,7 +129,7 @@ export const ObservationTree = ({
};
const ObservationTreeTraceNode = (props: {
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
input: string | undefined;
output: string | undefined;
latency?: number;
diff --git a/web/src/components/trace/TracePreview.tsx b/web/src/components/trace/TracePreview.tsx
index 28cd762d73..5d93c9e6e9 100644
--- a/web/src/components/trace/TracePreview.tsx
+++ b/web/src/components/trace/TracePreview.tsx
@@ -1,7 +1,7 @@
import { JSONView } from "@/src/components/ui/CodeJsonViewer";
import {
type APIScore,
- type Trace,
+ type TraceDomain,
AnnotationQueueObjectType,
} from "@langfuse/shared";
import { AggUsageBadge } from "@/src/components/token-usage-badge";
@@ -44,7 +44,7 @@ export const TracePreview = ({
commentCounts,
viewType = "detailed",
}: {
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
latency?: number;
input: string | undefined;
output: string | undefined;
diff --git a/web/src/components/trace/TraceTimelineView.tsx b/web/src/components/trace/TraceTimelineView.tsx
index 3c153195ec..9ee33438db 100644
--- a/web/src/components/trace/TraceTimelineView.tsx
+++ b/web/src/components/trace/TraceTimelineView.tsx
@@ -2,7 +2,7 @@ import { type ObservationReturnTypeWithMetadata } from "@/src/server/api/routers
import {
isPresent,
type APIScore,
- type Trace,
+ type TraceDomain,
ObservationLevel,
type ObservationLevelType,
} from "@langfuse/shared";
@@ -412,7 +412,7 @@ export function TraceTimelineView({
minLevel,
setMinLevel,
}: {
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
latency?: number;
input: string | undefined;
output: string | undefined;
diff --git a/web/src/components/trace/index.tsx b/web/src/components/trace/index.tsx
index 9f59e92d77..4c8b1b0ecb 100644
--- a/web/src/components/trace/index.tsx
+++ b/web/src/components/trace/index.tsx
@@ -1,4 +1,4 @@
-import { type ObservationLevelType, type Trace } from "@langfuse/shared";
+import { type ObservationLevelType, type TraceDomain } from "@langfuse/shared";
import { ObservationTree } from "./ObservationTree";
import { ObservationPreview } from "./ObservationPreview";
import { TracePreview } from "./TracePreview";
@@ -53,7 +53,7 @@ const getNestedObservationKeys = (
export function Trace(props: {
observations: Array<ObservationReturnTypeWithMetadata>;
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
input: string | undefined;
output: string | undefined;
};
diff --git a/web/src/features/trace-graph-view/components/TraceGraphView.tsx b/web/src/features/trace-graph-view/components/TraceGraphView.tsx
index d47251cdf9..c1242ead65 100644
--- a/web/src/features/trace-graph-view/components/TraceGraphView.tsx
+++ b/web/src/features/trace-graph-view/components/TraceGraphView.tsx
@@ -1,6 +1,6 @@
import React, { useEffect, useState, useMemo, useCallback } from "react";
import { StringParam, useQueryParam } from "use-query-params";
-import type { APIScore, Trace } from "@langfuse/shared";
+import type { APIScore, TraceDomain } from "@langfuse/shared";
import type { ObservationReturnTypeWithMetadata } from "@/src/server/api/routers/traces";
import { TraceGraphCanvas } from "./TraceGraphCanvas";
@@ -14,7 +14,7 @@ import {
type TraceGraphViewProps = {
observations: ObservationReturnTypeWithMetadata[];
- trace: Omit<Trace, "input" | "output"> & {
+ trace: Omit<TraceDomain, "input" | "output"> & {
input: string | undefined;
output: string | undefined;
};
diff --git a/web/src/pages/api/public/sessions/[sessionId].ts b/web/src/pages/api/public/sessions/[sessionId].ts
index ff6d28af62..d6adfccf05 100644
--- a/web/src/pages/api/public/sessions/[sessionId].ts
+++ b/web/src/pages/api/public/sessions/[sessionId].ts
@@ -42,7 +42,10 @@ export default withMiddlewares({
return {
...session,
- traces,
+ traces: traces.map((trace) => ({
+ ...trace,
+ externalId: null,
+ })),
};
},
}),
diff --git a/web/src/pages/api/public/traces/[traceId].ts b/web/src/pages/api/public/traces/[traceId].ts
index 6aaa7c6f83..f84e628e46 100644
--- a/web/src/pages/api/public/traces/[traceId].ts
+++ b/web/src/pages/api/public/traces/[traceId].ts
@@ -121,6 +121,7 @@ export default withMiddlewares({
: undefined;
return {
...trace,
+ externalId: null,
scores: validatedScores,
latency: latencyMs !== undefined ? latencyMs / 1000 : 0,
observations: outObservations,
diff --git a/web/src/pages/api/public/traces/index.ts b/web/src/pages/api/public/traces/index.ts
index 41f2486101..4d93138e99 100644
--- a/web/src/pages/api/public/traces/index.ts
+++ b/web/src/pages/api/public/traces/index.ts
@@ -87,7 +87,10 @@ export default withMiddlewares({
const finalCount = count || 0;
return {
- data: items,
+ data: items.map((item) => ({
+ ...item,
+ externalId: null,
+ })),
meta: {
page: query.page,
limit: query.limit,