Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f066a2

Browse files
authored
feat: export non-trace scores via posthog integration (#9103)
1 parent 664bd35 commit 9f066a2

File tree

1 file changed

+33
-5
lines changed
  • packages/shared/src/server/repositories

1 file changed

+33
-5
lines changed

packages/shared/src/server/repositories/scores.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@ export const getScoresForPostHog = async function* (
14531453
s.data_type as data_type,
14541454
s.comment as comment,
14551455
s.environment as environment,
1456+
s.trace_id as score_trace_id,
1457+
s.session_id as score_session_id,
1458+
s.dataset_run_id as score_dataset_run_id,
14561459
t.id as trace_id,
14571460
t.name as trace_name,
14581461
t.session_id as trace_session_id,
@@ -1464,11 +1467,21 @@ export const getScoresForPostHog = async function* (
14641467
FROM scores s FINAL
14651468
LEFT JOIN ${traceTable} t FINAL ON s.trace_id = t.id AND s.project_id = t.project_id
14661469
WHERE s.project_id = {projectId: String}
1467-
AND t.project_id = {projectId: String}
14681470
AND s.timestamp >= {minTimestamp: DateTime64(3)}
14691471
AND s.timestamp <= {maxTimestamp: DateTime64(3)}
1470-
AND t.timestamp >= {minTimestamp: DateTime64(3)} - INTERVAL 7 DAY
1471-
AND t.timestamp <= {maxTimestamp: DateTime64(3)}
1472+
AND (
1473+
s.trace_id IS NOT NULL
1474+
OR s.session_id IS NOT NULL
1475+
OR s.dataset_run_id IS NOT NULL
1476+
)
1477+
AND (
1478+
t.project_id IS NULL
1479+
OR (
1480+
t.project_id = {projectId: String}
1481+
AND t.timestamp >= {minTimestamp: DateTime64(3)} - INTERVAL 7 DAY
1482+
AND t.timestamp <= {maxTimestamp: DateTime64(3)}
1483+
)
1484+
)
14721485
`;
14731486

14741487
const records = queryClickhouseStream<Record<string, unknown>>({
@@ -1496,6 +1509,13 @@ export const getScoresForPostHog = async function* (
14961509

14971510
const baseUrl = env.NEXTAUTH_URL?.replace("/api/auth", "");
14981511
for await (const record of records) {
1512+
// Determine the effective session_id based on score attachment
1513+
const effectiveSessionId =
1514+
record.score_session_id || record.trace_session_id;
1515+
1516+
// Determine the effective trace_id (could be null for session-only or dataset-run-only scores)
1517+
const effectiveTraceId = record.score_trace_id || null;
1518+
14991519
yield {
15001520
timestamp: record.timestamp,
15011521
langfuse_score_name: record.name,
@@ -1505,15 +1525,23 @@ export const getScoresForPostHog = async function* (
15051525
langfuse_score_string_value: record.string_value,
15061526
langfuse_score_data_type: record.data_type,
15071527
langfuse_trace_name: record.trace_name,
1508-
langfuse_trace_id: record.trace_id,
1528+
langfuse_trace_id: effectiveTraceId,
15091529
langfuse_id: record.id,
1510-
langfuse_session_id: record.trace_session_id,
1530+
langfuse_session_id: effectiveSessionId,
15111531
langfuse_project_id: projectId,
15121532
langfuse_user_id: record.trace_user_id || null,
15131533
langfuse_release: record.trace_release,
15141534
langfuse_tags: record.trace_tags,
15151535
langfuse_environment: record.environment,
15161536
langfuse_event_version: "1.0.0",
1537+
langfuse_score_entity_type: record.score_trace_id
1538+
? "trace"
1539+
: record.score_session_id
1540+
? "session"
1541+
: record.score_dataset_run_id
1542+
? "dataset_run"
1543+
: "unknown",
1544+
langfuse_dataset_run_id: record.score_dataset_run_id,
15171545
$session_id: record.posthog_session_id ?? null,
15181546
...(record.trace_user_id
15191547
? {

0 commit comments

Comments
 (0)