Skip to content

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

fix(api): add logging to sessions router (#683)

Max Deichmann248bd1ea885b
diff --git a/src/server/api/routers/sessions.ts b/src/server/api/routers/sessions.ts
index 60380623c2..ce56320820 100644
--- a/src/server/api/routers/sessions.ts
+++ b/src/server/api/routers/sessions.ts
@@ -29,23 +29,24 @@ export const sessionRouter = createTRPCRouter({
all: protectedProjectProcedure
.input(SessionFilterOptions)
.query(async ({ input, ctx }) => {
- const filterCondition = filterToPrismaSql(
- input.filter ?? [],
- sessionsViewCols,
- );
+ try {
+ const filterCondition = filterToPrismaSql(
+ input.filter ?? [],
+ sessionsViewCols,
+ );
- const sessions = await ctx.prisma.$queryRaw<
- Array<{
- id: string;
- createdAt: Date;
- bookmarked: boolean;
- public: boolean;
- countTraces: number;
- userIds: string[];
- totalCount: number;
- sessionDuration: number | null;
- }>
- >(Prisma.sql`
+ const sessions = await ctx.prisma.$queryRaw<
+ Array<{
+ id: string;
+ createdAt: Date;
+ bookmarked: boolean;
+ public: boolean;
+ countTraces: number;
+ userIds: string[];
+ totalCount: number;
+ sessionDuration: number | null;
+ }>
+ >(Prisma.sql`
WITH observation_metrics AS (
SELECT
t.session_id,
@@ -88,45 +89,60 @@ export const sessionRouter = createTRPCRouter({
LIMIT ${input.limit}
OFFSET ${input.page * input.limit}
`);
- return sessions.map((s) => ({
- ...s,
- userIds: s.userIds.filter((t) => t !== null),
- }));
+ return sessions.map((s) => ({
+ ...s,
+ userIds: s.userIds.filter((t) => t !== null),
+ }));
+ } catch (e) {
+ console.error(e);
+ throw new TRPCError({
+ code: "INTERNAL_SERVER_ERROR",
+ message: "unable to get sessions",
+ });
+ }
}),
byId: protectedGetSessionProcedure
.input(z.object({ projectId: z.string(), sessionId: z.string() }))
.query(async ({ input, ctx }) => {
- const session = await ctx.prisma.traceSession.findFirst({
- where: {
- id: input.sessionId,
- projectId: input.projectId,
- },
- include: {
- traces: {
- orderBy: {
- timestamp: "asc",
- },
- include: {
- scores: true,
+ try {
+ const session = await ctx.prisma.traceSession.findFirst({
+ where: {
+ id: input.sessionId,
+ projectId: input.projectId,
+ },
+ include: {
+ traces: {
+ orderBy: {
+ timestamp: "asc",
+ },
+ include: {
+ scores: true,
+ },
},
},
- },
- });
- if (!session) {
+ });
+ if (!session) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Session not found in project",
+ });
+ }
+
+ return {
+ ...session,
+ users: [
+ ...new Set(
+ session.traces.map((t) => t.userId).filter((t) => t !== null),
+ ),
+ ],
+ };
+ } catch (e) {
+ console.error(e);
throw new TRPCError({
- code: "NOT_FOUND",
- message: "Session not found in project",
+ code: "INTERNAL_SERVER_ERROR",
+ message: "unable to get session",
});
}
-
- return {
- ...session,
- users: [
- ...new Set(
- session.traces.map((t) => t.userId).filter((t) => t !== null),
- ),
- ],
- };
}),
bookmark: protectedProjectProcedure
.input(
@@ -137,25 +153,33 @@ export const sessionRouter = createTRPCRouter({
}),
)
.mutation(async ({ input, ctx }) => {
- throwIfNoAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "objects:bookmark",
- });
-
- const session = await ctx.prisma.traceSession.update({
- where: {
- id: input.sessionId,
+ try {
+ throwIfNoAccess({
+ session: ctx.session,
projectId: input.projectId,
- },
- data: {
- bookmarked: input.bookmarked,
- },
- });
- if (!session) {
- throw new Error("Session not found in project");
+ scope: "objects:bookmark",
+ });
+
+ const session = await ctx.prisma.traceSession.update({
+ where: {
+ id: input.sessionId,
+ projectId: input.projectId,
+ },
+ data: {
+ bookmarked: input.bookmarked,
+ },
+ });
+ if (!session) {
+ throw new Error("Session not found in project");
+ }
+ return session;
+ } catch (e) {
+ console.error(e);
+ throw new TRPCError({
+ code: "INTERNAL_SERVER_ERROR",
+ message: "unable to set bookmark on session",
+ });
}
- return session;
}),
publish: protectedProjectProcedure
.input(
@@ -166,19 +190,27 @@ export const sessionRouter = createTRPCRouter({
}),
)
.mutation(async ({ input, ctx }) => {
- throwIfNoAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "objects:publish",
- });
- return ctx.prisma.traceSession.update({
- where: {
- id: input.sessionId,
+ try {
+ throwIfNoAccess({
+ session: ctx.session,
projectId: input.projectId,
- },
- data: {
- public: input.public,
- },
- });
+ scope: "objects:publish",
+ });
+ return ctx.prisma.traceSession.update({
+ where: {
+ id: input.sessionId,
+ projectId: input.projectId,
+ },
+ data: {
+ public: input.public,
+ },
+ });
+ } catch (e) {
+ console.error(e);
+ throw new TRPCError({
+ code: "INTERNAL_SERVER_ERROR",
+ message: "unable to publish session",
+ });
+ }
}),
});