diff --git a/apps/webapp/app/presenters/v3/SpanPresenter.server.ts b/apps/webapp/app/presenters/v3/SpanPresenter.server.ts index 156cf74a961..86a4fd6bf3e 100644 --- a/apps/webapp/app/presenters/v3/SpanPresenter.server.ts +++ b/apps/webapp/app/presenters/v3/SpanPresenter.server.ts @@ -62,6 +62,7 @@ import { startedSessionExternalId, traceRootRunId, } from "./startedSession.server"; +import { extractSessionStreamEntity } from "./sessionStreamEntity"; export type PromptSpanData = { slug: string; @@ -950,41 +951,30 @@ export class SpanPresenter extends BasePresenter { }; } case "session-stream": { - if (!span.entity.id) { - logger.error(`SpanPresenter: No session stream id`, { - spanId, - sessionStreamId: span.entity.id, - }); - return { ...data, entity: null }; - } - - const parts = span.entity.id.split(":"); - const io = parts.at(-1); - const channel = parts.at(-2) ?? ""; - const sessionId = parts.at(-3); + const sessionStream = extractSessionStreamEntity({ + entityId: span.entity.id, + entityMetadata: span.entity.metadata, + properties: span.properties, + }); - if (!sessionId || (io !== "out" && io !== "in")) { - logger.error(`SpanPresenter: Invalid session stream id`, { - spanId, - sessionStreamId: span.entity.id, - }); + if (!sessionStream) { + logger.error( + span.entity.id + ? `SpanPresenter: Invalid session stream id` + : `SpanPresenter: No session stream id`, + { + spanId, + sessionStreamId: span.entity.id, + } + ); return { ...data, entity: null }; } - const metadata = span.entity.metadata - ? (safeJsonParse(span.entity.metadata) as Record | undefined) - : undefined; - return { ...data, entity: { type: "session-stream" as const, - object: { - sessionId, - channel: channel.length > 0 ? channel : undefined, - io, - metadata, - }, + object: sessionStream, }, }; } diff --git a/apps/webapp/app/presenters/v3/sessionStreamEntity.ts b/apps/webapp/app/presenters/v3/sessionStreamEntity.ts new file mode 100644 index 00000000000..a52eaa820e7 --- /dev/null +++ b/apps/webapp/app/presenters/v3/sessionStreamEntity.ts @@ -0,0 +1,73 @@ +import { safeJsonParse } from "~/utils/json"; + +type SessionStreamEntityObject = { + sessionId: string; + channel: string | undefined; + io: "out" | "in"; + metadata: Record | undefined; +}; + +/** + * Extracts `session-stream` span entity fields. + * + * Prefers unsplit `session`, `channel`, and `io` span properties because + * session keys may contain `:` (e.g. `clerk:user_123:v2`) and `"in"` stream + * entity IDs carry a leading `runId:` prefix. + */ +export function extractSessionStreamEntity({ + entityId, + entityMetadata, + properties, +}: { + entityId?: string | null; + entityMetadata?: string | null; + properties?: unknown; +}): SessionStreamEntityObject | null { + const props = + properties && typeof properties === "object" + ? (properties as Record) + : undefined; + + const propSessionId = + typeof props?.session === "string" && props.session.length > 0 ? props.session : undefined; + const propIo = props?.io === "out" || props?.io === "in" ? props.io : undefined; + const propChannel = + typeof props?.channel === "string" && props.channel.length > 0 ? props.channel : undefined; + + let sessionId = propSessionId; + let io: string | undefined = propIo; + let channel = propChannel; + + if (!sessionId || !io) { + if (!entityId) { + return null; + } + + const parts = entityId.split(":"); + const parsedIo = parts.at(-1); + const parsedChannel = parts.at(-2) ?? ""; + const parsedSessionId = + parsedIo === "in" && parts.length >= 4 && parts[0]?.startsWith("run_") + ? parts.slice(1, -2).join(":") + : parts.slice(0, -2).join(":"); + + sessionId = sessionId ?? (parsedSessionId.length > 0 ? parsedSessionId : undefined); + io = io ?? parsedIo; + channel = channel ?? (parsedChannel.length > 0 ? parsedChannel : undefined); + } + + if (!sessionId || (io !== "out" && io !== "in")) { + return null; + } + + const metadata = entityMetadata + ? (safeJsonParse(entityMetadata) as Record | undefined) + : undefined; + + return { + sessionId, + channel, + io, + metadata, + }; +} diff --git a/apps/webapp/test/sessionStreamEntity.test.ts b/apps/webapp/test/sessionStreamEntity.test.ts new file mode 100644 index 00000000000..be1fb1cd6e6 --- /dev/null +++ b/apps/webapp/test/sessionStreamEntity.test.ts @@ -0,0 +1,122 @@ +import { describe, expect, it } from "vitest"; +import { extractSessionStreamEntity } from "~/presenters/v3/sessionStreamEntity"; + +describe("extractSessionStreamEntity", () => { + it("preserves colons in session keys for out spans using properties", () => { + expect( + extractSessionStreamEntity({ + entityId: "clerk:user_123:v2::out", + properties: { + session: "clerk:user_123:v2", + io: "out", + }, + }) + ).toEqual({ + sessionId: "clerk:user_123:v2", + channel: undefined, + io: "out", + metadata: undefined, + }); + }); + + it("extracts channel and metadata when session key contains colons", () => { + expect( + extractSessionStreamEntity({ + entityId: "org:42:thread:9:tokens:out", + entityMetadata: JSON.stringify({ model: "gpt-4o" }), + properties: { + session: "org:42:thread:9", + channel: "tokens", + io: "out", + }, + }) + ).toEqual({ + sessionId: "org:42:thread:9", + channel: "tokens", + io: "out", + metadata: { model: "gpt-4o" }, + }); + }); + + it("does not include the runId prefix in sessionId for in spans", () => { + expect( + extractSessionStreamEntity({ + entityId: "run_abc123:clerk:user_123:v2:approvals:in", + properties: { + session: "clerk:user_123:v2", + channel: "approvals", + io: "in", + }, + }) + ).toEqual({ + sessionId: "clerk:user_123:v2", + channel: "approvals", + io: "in", + metadata: undefined, + }); + }); + + it("preserves session keys starting with run_ when properties are present", () => { + expect( + extractSessionStreamEntity({ + entityId: "run_outer:run_custom:session:1::in", + properties: { + session: "run_custom:session:1", + io: "in", + }, + }) + ).toEqual({ + sessionId: "run_custom:session:1", + channel: undefined, + io: "in", + metadata: undefined, + }); + }); + + it("resolves session stream fields from properties when entityId is absent", () => { + expect( + extractSessionStreamEntity({ + entityId: undefined, + properties: { + session: "user:abc:def", + io: "in", + }, + }) + ).toEqual({ + sessionId: "user:abc:def", + channel: undefined, + io: "in", + metadata: undefined, + }); + }); + + it("falls back to entityId parsing when properties are missing", () => { + expect( + extractSessionStreamEntity({ + entityId: "clerk:user_123:v2::out", + }) + ).toEqual({ + sessionId: "clerk:user_123:v2", + channel: undefined, + io: "out", + metadata: undefined, + }); + + expect( + extractSessionStreamEntity({ + entityId: "run_abc123:clerk:user_123:v2:side:in", + }) + ).toEqual({ + sessionId: "clerk:user_123:v2", + channel: "side", + io: "in", + metadata: undefined, + }); + }); + + it("returns null for missing ids or invalid io directions", () => { + expect(extractSessionStreamEntity({ entityId: undefined })).toBeNull(); + expect(extractSessionStreamEntity({ entityId: "sess_1::invalid" })).toBeNull(); + expect(extractSessionStreamEntity({ entityId: ":channel:out" })).toBeNull(); + }); +});