@arizeai/phoenix-client 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ import type { components } from "../__generated__/api/v1";
2
+ import type { ClientFn } from "../types/core";
3
+ type Span = components["schemas"]["Span"];
4
+ /**
5
+ * Input or output extracted from a root span's attributes.
6
+ *
7
+ * @experimental this interface is experimental and may change in the future
8
+ */
9
+ export interface SessionTurnIO {
10
+ /** The string value of the input or output */
11
+ value: string;
12
+ /** Optional MIME type (e.g. "text/plain", "application/json") */
13
+ mimeType?: string;
14
+ }
15
+ /**
16
+ * A single turn in a session, representing one trace's root span input/output.
17
+ *
18
+ * **Note:** A "turn" is derived from a trace's root span. For input/output to appear,
19
+ * the root span must have `input.value` and `output.value` attributes set
20
+ * (per OpenInference semantic conventions). This typically requires instrumentation
21
+ * that records these attributes on the top-level span.
22
+ *
23
+ * @experimental this interface is experimental and may change in the future
24
+ */
25
+ export interface SessionTurn {
26
+ /** The trace ID for this turn */
27
+ traceId: string;
28
+ /** ISO 8601 timestamp of when the trace started */
29
+ startTime: string;
30
+ /** ISO 8601 timestamp of when the trace ended */
31
+ endTime: string;
32
+ /** Input extracted from the root span's attributes */
33
+ input?: SessionTurnIO;
34
+ /** Output extracted from the root span's attributes */
35
+ output?: SessionTurnIO;
36
+ /** The full root span, if found */
37
+ rootSpan?: Span;
38
+ }
39
+ /**
40
+ * @experimental this interface is experimental and may change in the future
41
+ */
42
+ export interface GetSessionTurnsParams extends ClientFn {
43
+ /** The session identifier: either a GlobalID or user-provided session_id string. */
44
+ sessionId: string;
45
+ }
46
+ /**
47
+ * Get the turns (root span I/O) for a session.
48
+ *
49
+ * Returns input/output extracted from root spans for each trace, along with
50
+ * the full root span. Turns are ordered by trace start_time.
51
+ *
52
+ * **Note:** A "turn" is derived from a trace's root span. For input/output to appear,
53
+ * the root span must have `input.value` and `output.value` attributes set
54
+ * (per OpenInference semantic conventions). This typically requires instrumentation
55
+ * that records these attributes on the top-level span.
56
+ *
57
+ * @experimental this function is experimental and may change in the future
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { getSessionTurns } from "@arizeai/phoenix-client/sessions";
62
+ *
63
+ * const turns = await getSessionTurns({ sessionId: "my-session" });
64
+ * for (const turn of turns) {
65
+ * console.log(`[${turn.startTime}] Input: ${turn.input?.value}`);
66
+ * console.log(`[${turn.startTime}] Output: ${turn.output?.value}`);
67
+ * }
68
+ * ```
69
+ */
70
+ export declare function getSessionTurns({ client: _client, sessionId, }: GetSessionTurnsParams): Promise<SessionTurn[]>;
71
+ export {};
72
+ //# sourceMappingURL=getSessionTurns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSessionTurns.d.ts","sourceRoot":"","sources":["../../../src/sessions/getSessionTurns.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAI9C,KAAK,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;AAI1C;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,8CAA8C;IAC9C,KAAK,EAAE,MAAM,CAAC;IACd,iEAAiE;IACjE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,SAAS,EAAE,MAAM,CAAC;IAClB,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC;IAChB,sDAAsD;IACtD,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,uDAAuD;IACvD,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,mCAAmC;IACnC,QAAQ,CAAC,EAAE,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,QAAQ;IACrD,oFAAoF;IACpF,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,eAAe,CAAC,EACpC,MAAM,EAAE,OAAO,EACf,SAAS,GACV,EAAE,qBAAqB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAiChD"}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSessionTurns = getSessionTurns;
4
+ const openinference_semantic_conventions_1 = require("@arizeai/openinference-semantic-conventions");
5
+ const client_1 = require("../client");
6
+ const getSpans_1 = require("../spans/getSpans");
7
+ const getSession_1 = require("./getSession");
8
+ const MAX_TRACE_IDS_PER_BATCH = 50;
9
+ /**
10
+ * Get the turns (root span I/O) for a session.
11
+ *
12
+ * Returns input/output extracted from root spans for each trace, along with
13
+ * the full root span. Turns are ordered by trace start_time.
14
+ *
15
+ * **Note:** A "turn" is derived from a trace's root span. For input/output to appear,
16
+ * the root span must have `input.value` and `output.value` attributes set
17
+ * (per OpenInference semantic conventions). This typically requires instrumentation
18
+ * that records these attributes on the top-level span.
19
+ *
20
+ * @experimental this function is experimental and may change in the future
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { getSessionTurns } from "@arizeai/phoenix-client/sessions";
25
+ *
26
+ * const turns = await getSessionTurns({ sessionId: "my-session" });
27
+ * for (const turn of turns) {
28
+ * console.log(`[${turn.startTime}] Input: ${turn.input?.value}`);
29
+ * console.log(`[${turn.startTime}] Output: ${turn.output?.value}`);
30
+ * }
31
+ * ```
32
+ */
33
+ async function getSessionTurns({ client: _client, sessionId, }) {
34
+ const client = _client !== null && _client !== void 0 ? _client : (0, client_1.createClient)();
35
+ const session = await (0, getSession_1.getSession)({ client, sessionId });
36
+ const traces = session.traces;
37
+ if (traces.length === 0) {
38
+ return [];
39
+ }
40
+ const projectId = session.projectId;
41
+ const traceInfo = new Map(traces.map((t) => [t.traceId, t]));
42
+ const allTraceIds = [...traceInfo.keys()];
43
+ // Fetch root spans in batches
44
+ const rootSpansByTrace = new Map();
45
+ for (let i = 0; i < allTraceIds.length; i += MAX_TRACE_IDS_PER_BATCH) {
46
+ const traceIdBatch = allTraceIds.slice(i, i + MAX_TRACE_IDS_PER_BATCH);
47
+ const spans = await getAllRootSpansForBatch({
48
+ client,
49
+ projectId,
50
+ traceIdBatch,
51
+ });
52
+ for (const span of spans) {
53
+ const traceId = span.context.trace_id;
54
+ if (!rootSpansByTrace.has(traceId)) {
55
+ rootSpansByTrace.set(traceId, span);
56
+ }
57
+ }
58
+ }
59
+ return buildSessionTurns({ allTraceIds, traceInfo, rootSpansByTrace });
60
+ }
61
+ /**
62
+ * Fetch all root spans for a batch of trace IDs, handling pagination.
63
+ */
64
+ async function getAllRootSpansForBatch({ client, projectId, traceIdBatch, }) {
65
+ const allSpans = [];
66
+ let cursor = null;
67
+ do {
68
+ const result = await (0, getSpans_1.getSpans)(Object.assign({ client, project: { projectId }, traceIds: traceIdBatch, parentId: null, limit: traceIdBatch.length }, (cursor ? { cursor } : {})));
69
+ allSpans.push(...result.spans);
70
+ cursor = result.nextCursor;
71
+ } while (cursor != null);
72
+ return allSpans;
73
+ }
74
+ /**
75
+ * Extract a SessionTurnIO from span attributes for a given prefix.
76
+ */
77
+ function extractIO({ attrs, valueKey, mimeTypeKey, }) {
78
+ const value = attrs[valueKey];
79
+ if (value == null)
80
+ return undefined;
81
+ const io = { value: String(value) };
82
+ const mimeType = attrs[mimeTypeKey];
83
+ if (mimeType != null) {
84
+ io.mimeType = String(mimeType);
85
+ }
86
+ return io;
87
+ }
88
+ /**
89
+ * Build session turns from trace info and root spans, ordered by start_time.
90
+ */
91
+ function buildSessionTurns({ allTraceIds, traceInfo, rootSpansByTrace, }) {
92
+ var _a;
93
+ const turns = [];
94
+ for (const traceId of allTraceIds) {
95
+ const info = traceInfo.get(traceId);
96
+ if (!info)
97
+ continue;
98
+ const turn = {
99
+ traceId,
100
+ startTime: info.startTime,
101
+ endTime: info.endTime,
102
+ };
103
+ const rootSpan = rootSpansByTrace.get(traceId);
104
+ if (rootSpan) {
105
+ turn.rootSpan = rootSpan;
106
+ const attrs = (_a = rootSpan.attributes) !== null && _a !== void 0 ? _a : {};
107
+ turn.input = extractIO({
108
+ attrs,
109
+ valueKey: openinference_semantic_conventions_1.SemanticConventions.INPUT_VALUE,
110
+ mimeTypeKey: openinference_semantic_conventions_1.SemanticConventions.INPUT_MIME_TYPE,
111
+ });
112
+ turn.output = extractIO({
113
+ attrs,
114
+ valueKey: openinference_semantic_conventions_1.SemanticConventions.OUTPUT_VALUE,
115
+ mimeTypeKey: openinference_semantic_conventions_1.SemanticConventions.OUTPUT_MIME_TYPE,
116
+ });
117
+ }
118
+ turns.push(turn);
119
+ }
120
+ turns.sort((a, b) => a.startTime.localeCompare(b.startTime));
121
+ return turns;
122
+ }
123
+ //# sourceMappingURL=getSessionTurns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSessionTurns.js","sourceRoot":"","sources":["../../../src/sessions/getSessionTurns.ts"],"names":[],"mappings":";;AAkFA,0CAoCC;AAtHD,oGAAkF;AAGlF,sCAAyC;AACzC,gDAA6C;AAG7C,6CAA0C;AAI1C,MAAM,uBAAuB,GAAG,EAAE,CAAC;AA+CnC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACI,KAAK,UAAU,eAAe,CAAC,EACpC,MAAM,EAAE,OAAO,EACf,SAAS,GACa;IACtB,MAAM,MAAM,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,IAAA,qBAAY,GAAE,CAAC;IAEzC,MAAM,OAAO,GAAY,MAAM,IAAA,uBAAU,EAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;IACjE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,EAAE,CAAC;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E,IAAI,CAAC,SAAS;YACzB,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC;QAEF,MAAM,QAAQ,GAAG,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC/C,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;YACzB,MAAM,KAAK,GAAG,MAAA,QAAQ,CAAC,UAAU,mCAAI,EAAE,CAAC;YACxC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC;gBACrB,KAAK;gBACL,QAAQ,EAAE,wDAAmB,CAAC,WAAW;gBACzC,WAAW,EAAE,wDAAmB,CAAC,eAAe;aACjD,CAAC,CAAC;YACH,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;gBACtB,KAAK;gBACL,QAAQ,EAAE,wDAAmB,CAAC,YAAY;gBAC1C,WAAW,EAAE,wDAAmB,CAAC,gBAAgB;aAClD,CAAC,CAAC;QACL,CAAC;QAED,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7D,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -2,6 +2,7 @@ export * from "./addSessionAnnotation";
2
2
  export * from "./deleteSession";
3
3
  export * from "./deleteSessions";
4
4
  export * from "./getSession";
5
+ export * from "./getSessionTurns";
5
6
  export * from "./listSessions";
6
7
  export * from "./logSessionAnnotations";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/sessions/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/sessions/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC"}
@@ -18,6 +18,7 @@ __exportStar(require("./addSessionAnnotation"), exports);
18
18
  __exportStar(require("./deleteSession"), exports);
19
19
  __exportStar(require("./deleteSessions"), exports);
20
20
  __exportStar(require("./getSession"), exports);
21
+ __exportStar(require("./getSessionTurns"), exports);
21
22
  __exportStar(require("./listSessions"), exports);
22
23
  __exportStar(require("./logSessionAnnotations"), exports);
23
24
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/sessions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,kDAAgC;AAChC,mDAAiC;AACjC,+CAA6B;AAC7B,iDAA+B;AAC/B,0DAAwC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/sessions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,kDAAgC;AAChC,mDAAiC;AACjC,+CAA6B;AAC7B,oDAAkC;AAClC,iDAA+B;AAC/B,0DAAwC"}