@arizeai/phoenix-client 6.11.1 → 6.12.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.
Files changed (40) hide show
  1. package/README.md +26 -0
  2. package/dist/esm/__generated__/api/v1.d.ts +18 -1
  3. package/dist/esm/__generated__/api/v1.d.ts.map +1 -1
  4. package/dist/esm/spans/index.d.ts +1 -0
  5. package/dist/esm/spans/index.d.ts.map +1 -1
  6. package/dist/esm/spans/index.js +1 -0
  7. package/dist/esm/spans/index.js.map +1 -1
  8. package/dist/esm/spans/logSpans.d.ts +94 -0
  9. package/dist/esm/spans/logSpans.d.ts.map +1 -0
  10. package/dist/esm/spans/logSpans.js +216 -0
  11. package/dist/esm/spans/logSpans.js.map +1 -0
  12. package/dist/esm/testing/phoenix-test-tracking.d.ts +14 -0
  13. package/dist/esm/testing/phoenix-test-tracking.d.ts.map +1 -1
  14. package/dist/esm/testing/phoenix-test-tracking.js +49 -4
  15. package/dist/esm/testing/phoenix-test-tracking.js.map +1 -1
  16. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  17. package/dist/esm/utils/formatPromptMessages.d.ts.map +1 -1
  18. package/dist/esm/utils/getPromptBySelector.d.ts.map +1 -1
  19. package/dist/src/__generated__/api/v1.d.ts +18 -1
  20. package/dist/src/__generated__/api/v1.d.ts.map +1 -1
  21. package/dist/src/spans/index.d.ts +1 -0
  22. package/dist/src/spans/index.d.ts.map +1 -1
  23. package/dist/src/spans/index.js +1 -0
  24. package/dist/src/spans/index.js.map +1 -1
  25. package/dist/src/spans/logSpans.d.ts +94 -0
  26. package/dist/src/spans/logSpans.d.ts.map +1 -0
  27. package/dist/src/spans/logSpans.js +214 -0
  28. package/dist/src/spans/logSpans.js.map +1 -0
  29. package/dist/src/testing/phoenix-test-tracking.d.ts +14 -0
  30. package/dist/src/testing/phoenix-test-tracking.d.ts.map +1 -1
  31. package/dist/src/testing/phoenix-test-tracking.js +50 -3
  32. package/dist/src/testing/phoenix-test-tracking.js.map +1 -1
  33. package/dist/src/utils/formatPromptMessages.d.ts.map +1 -1
  34. package/dist/src/utils/getPromptBySelector.d.ts.map +1 -1
  35. package/dist/tsconfig.tsbuildinfo +1 -1
  36. package/package.json +8 -8
  37. package/src/__generated__/api/v1.ts +18 -1
  38. package/src/spans/index.ts +1 -0
  39. package/src/spans/logSpans.ts +328 -0
  40. package/src/testing/phoenix-test-tracking.ts +55 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arizeai/phoenix-client",
3
- "version": "6.11.1",
3
+ "version": "6.12.0",
4
4
  "description": "A client for the Phoenix API",
5
5
  "keywords": [
6
6
  "arize",
@@ -96,25 +96,25 @@
96
96
  },
97
97
  "dependencies": {
98
98
  "@arizeai/openinference-semantic-conventions": "^2.5.0",
99
- "@arizeai/openinference-vercel": "^2.7.8",
99
+ "@arizeai/openinference-vercel": "^2.8.1",
100
100
  "async": "^3.2.6",
101
101
  "openapi-fetch": "^0.17.0",
102
102
  "tiny-invariant": "^1.3.3",
103
103
  "zod": "^4.4.3",
104
- "@arizeai/phoenix-config": "0.1.4",
105
- "@arizeai/phoenix-otel": "1.0.2"
104
+ "@arizeai/phoenix-otel": "1.0.2",
105
+ "@arizeai/phoenix-config": "0.1.4"
106
106
  },
107
107
  "devDependencies": {
108
- "@ai-sdk/openai": "^3.0.73",
108
+ "@ai-sdk/openai": "^3.0.75",
109
109
  "@anthropic-ai/sdk": "^0.102.0",
110
110
  "@opentelemetry/api": "^1.9.1",
111
111
  "@opentelemetry/sdk-trace-node": "^2.8.0",
112
112
  "@types/async": "^3.2.25",
113
113
  "@types/node": "^25.9.4",
114
- "ai": "^6.0.208",
115
- "dotenv": "^16.4.7",
114
+ "ai": "^6.0.211",
115
+ "dotenv": "^16.6.1",
116
116
  "jest": "^29.7.0",
117
- "openai": "^6.44.0",
117
+ "openai": "^6.45.0",
118
118
  "openapi-typescript": "^7.13.0",
119
119
  "tsx": "^4.22.4",
120
120
  "vitest": "^4.1.9",
@@ -1307,7 +1307,24 @@ export interface paths {
1307
1307
  };
1308
1308
  get?: never;
1309
1309
  put?: never;
1310
- /** Run Server Agent */
1310
+ /**
1311
+ * Run Server Agent
1312
+ * @description Stream a chat turn from the GraphQL server agent.
1313
+ *
1314
+ * This is the endpoint the PXI CLI talks to directly (no pre-configured
1315
+ * agent record): it builds a fresh server agent per request from the
1316
+ * caller-supplied model and contexts, then streams the reply back as
1317
+ * Vercel-AI chunks.
1318
+ *
1319
+ * The request contexts gate capabilities — GraphQL mutations, web access,
1320
+ * and subagents — and mutations are refused for viewer users. When trace
1321
+ * recording is enabled (and permitted by system settings), the run is
1322
+ * traced; locally ingested traces are persisted to the agent's project
1323
+ * once the stream completes.
1324
+ *
1325
+ * Returns ``403`` if agents or the server agent are disabled, or if a
1326
+ * viewer requests mutations.
1327
+ */
1311
1328
  post: operations["run_server_agent_agents_server_sessions__session_id__chat_post"];
1312
1329
  delete?: never;
1313
1330
  options?: never;
@@ -7,3 +7,4 @@ export * from "./getSpans";
7
7
  export type { SpanKindFilter, SpanStatusCode } from "../types/spans";
8
8
  export * from "./getSpanAnnotations";
9
9
  export * from "./deleteSpan";
10
+ export * from "./logSpans";
@@ -0,0 +1,328 @@
1
+ import type { paths } from "../__generated__/api/v1";
2
+ import { createClient } from "../client";
3
+ import type { ClientFn } from "../types/core";
4
+ import type { ProjectIdentifier } from "../types/projects";
5
+ import { resolveProjectIdentifier } from "../types/projects";
6
+ import { formatApiError } from "../utils/apiErrorUtils";
7
+ import { isObject } from "../utils/isObject";
8
+ import { safelyParseJSON } from "../utils/safelyParseJSON";
9
+
10
+ type CreateSpansRequestData =
11
+ paths["/v1/projects/{project_identifier}/spans"]["post"]["requestBody"]["content"]["application/json"]["data"];
12
+
13
+ /**
14
+ * A span in Phoenix's simplified span structure, as accepted by {@link logSpans}.
15
+ * This is the same shape returned by `getSpans`, which makes it possible to read
16
+ * spans from one project and log them into another.
17
+ */
18
+ export type Span = CreateSpansRequestData[number];
19
+
20
+ /** Information about a span that failed validation and was not queued. */
21
+ export interface InvalidSpanInfo {
22
+ spanId: string;
23
+ traceId: string;
24
+ error: string;
25
+ }
26
+
27
+ /** Information about a span that was rejected because it already exists. */
28
+ export interface DuplicateSpanInfo {
29
+ spanId: string;
30
+ traceId: string;
31
+ }
32
+
33
+ /**
34
+ * Raised by {@link logSpans} when one or more spans in the request are invalid
35
+ * or duplicates. If any span in a request fails, none of the spans in that
36
+ * request are queued.
37
+ */
38
+ export class SpanCreationError extends Error {
39
+ readonly invalidSpans: InvalidSpanInfo[];
40
+ readonly duplicateSpans: DuplicateSpanInfo[];
41
+ readonly totalReceived: number;
42
+ readonly totalQueued: number;
43
+
44
+ constructor(params: {
45
+ message: string;
46
+ invalidSpans?: InvalidSpanInfo[];
47
+ duplicateSpans?: DuplicateSpanInfo[];
48
+ totalReceived?: number;
49
+ totalQueued?: number;
50
+ }) {
51
+ super(params.message);
52
+ this.name = "SpanCreationError";
53
+ this.invalidSpans = params.invalidSpans ?? [];
54
+ this.duplicateSpans = params.duplicateSpans ?? [];
55
+ this.totalReceived = params.totalReceived ?? 0;
56
+ this.totalQueued = params.totalQueued ?? 0;
57
+ }
58
+
59
+ /** Number of spans rejected as invalid. */
60
+ get totalInvalid(): number {
61
+ return this.invalidSpans.length;
62
+ }
63
+
64
+ /** Number of spans rejected as duplicates. */
65
+ get totalDuplicates(): number {
66
+ return this.duplicateSpans.length;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Parameters to log spans to a project
72
+ */
73
+ export interface LogSpansParams extends ClientFn {
74
+ /** The project to log spans into */
75
+ project: ProjectIdentifier;
76
+ /** The spans to log */
77
+ spans: Span[];
78
+ }
79
+
80
+ /**
81
+ * Statistics about a {@link logSpans} call. When successful, `totalQueued`
82
+ * equals `totalReceived`.
83
+ */
84
+ export interface LogSpansResult {
85
+ totalReceived: number;
86
+ totalQueued: number;
87
+ }
88
+
89
+ const MAX_ERRORS_TO_SHOW = 5;
90
+
91
+ function formatLogSpansErrorMessage({
92
+ invalidSpans,
93
+ duplicateSpans,
94
+ }: {
95
+ invalidSpans: InvalidSpanInfo[];
96
+ duplicateSpans: DuplicateSpanInfo[];
97
+ }): string {
98
+ const parts: string[] = [];
99
+
100
+ if (invalidSpans.length > 0) {
101
+ parts.push(`Failed to queue ${invalidSpans.length} invalid spans:`);
102
+ for (const span of invalidSpans.slice(0, MAX_ERRORS_TO_SHOW)) {
103
+ parts.push(` - Span ${span.spanId}: ${span.error}`);
104
+ }
105
+ if (invalidSpans.length > MAX_ERRORS_TO_SHOW) {
106
+ parts.push(
107
+ ` ... and ${invalidSpans.length - MAX_ERRORS_TO_SHOW} more invalid spans`
108
+ );
109
+ }
110
+ }
111
+
112
+ if (duplicateSpans.length > 0) {
113
+ if (parts.length > 0) parts.push("");
114
+ parts.push(`Found ${duplicateSpans.length} duplicate spans:`);
115
+ for (const span of duplicateSpans.slice(0, MAX_ERRORS_TO_SHOW)) {
116
+ parts.push(` - Span ${span.spanId}`);
117
+ }
118
+ if (duplicateSpans.length > MAX_ERRORS_TO_SHOW) {
119
+ parts.push(
120
+ ` ... and ${duplicateSpans.length - MAX_ERRORS_TO_SHOW} more duplicates`
121
+ );
122
+ }
123
+ }
124
+
125
+ return parts.join("\n");
126
+ }
127
+
128
+ function toSpanIdAndTraceId(item: unknown): {
129
+ spanId: string;
130
+ traceId: string;
131
+ } {
132
+ const record = isObject(item) ? (item as Record<string, unknown>) : {};
133
+ return {
134
+ spanId: typeof record.span_id === "string" ? record.span_id : "unknown",
135
+ traceId: typeof record.trace_id === "string" ? record.trace_id : "unknown",
136
+ };
137
+ }
138
+
139
+ function toInvalidSpanInfoList(value: unknown): InvalidSpanInfo[] {
140
+ if (!Array.isArray(value)) return [];
141
+ return value.map((item) => {
142
+ const record = isObject(item) ? (item as Record<string, unknown>) : {};
143
+ return {
144
+ ...toSpanIdAndTraceId(item),
145
+ error:
146
+ typeof record.error === "string" ? record.error : "Validation error",
147
+ };
148
+ });
149
+ }
150
+
151
+ function toDuplicateSpanInfoList(value: unknown): DuplicateSpanInfo[] {
152
+ if (!Array.isArray(value)) return [];
153
+ return value.map(toSpanIdAndTraceId);
154
+ }
155
+
156
+ /**
157
+ * Builds a {@link SpanCreationError} from already-normalized invalid/duplicate
158
+ * span lists, computing the shared message once.
159
+ */
160
+ function makeSpanCreationError(params: {
161
+ invalidSpans: InvalidSpanInfo[];
162
+ duplicateSpans: DuplicateSpanInfo[];
163
+ totalReceived: number;
164
+ totalQueued: number;
165
+ }): SpanCreationError {
166
+ return new SpanCreationError({
167
+ message: formatLogSpansErrorMessage(params),
168
+ ...params,
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Builds a {@link SpanCreationError} from a parsed error payload matching the
174
+ * shape returned by the server for invalid/duplicate spans:
175
+ * `{ total_received, total_queued, total_invalid, total_duplicates, invalid_spans, duplicate_spans }`.
176
+ */
177
+ function buildSpanCreationError(
178
+ payload: Record<string, unknown>
179
+ ): SpanCreationError {
180
+ return makeSpanCreationError({
181
+ invalidSpans: toInvalidSpanInfoList(payload.invalid_spans),
182
+ duplicateSpans: toDuplicateSpanInfoList(payload.duplicate_spans),
183
+ totalReceived:
184
+ typeof payload.total_received === "number" ? payload.total_received : 0,
185
+ totalQueued:
186
+ typeof payload.total_queued === "number" ? payload.total_queued : 0,
187
+ });
188
+ }
189
+
190
+ function isSpanCreationErrorPayload(
191
+ value: unknown
192
+ ): value is Record<string, unknown> {
193
+ return (
194
+ isObject(value) && ("invalid_spans" in value || "duplicate_spans" in value)
195
+ );
196
+ }
197
+
198
+ /**
199
+ * Extracts invalid span info from a FastAPI request-validation error array
200
+ * (returned with a 422 status when a span in the payload is malformed).
201
+ */
202
+ function extractInvalidSpansFromValidationErrors(
203
+ errors: unknown[],
204
+ spans: Span[]
205
+ ): InvalidSpanInfo[] {
206
+ const invalidSpans: InvalidSpanInfo[] = [];
207
+
208
+ for (const rawError of errors) {
209
+ if (!isObject(rawError)) continue;
210
+ const loc = (rawError as { loc?: unknown }).loc;
211
+ if (
212
+ !Array.isArray(loc) ||
213
+ loc.length < 3 ||
214
+ loc[0] !== "body" ||
215
+ loc[1] !== "data" ||
216
+ typeof loc[2] !== "number"
217
+ ) {
218
+ continue;
219
+ }
220
+
221
+ const span = spans[loc[2]];
222
+ if (!span) continue;
223
+
224
+ const msg = (rawError as { msg?: unknown }).msg;
225
+ invalidSpans.push({
226
+ spanId: span.context?.span_id ?? "unknown",
227
+ traceId: span.context?.trace_id ?? "unknown",
228
+ error: typeof msg === "string" ? msg : "Validation error",
229
+ });
230
+ }
231
+
232
+ return invalidSpans;
233
+ }
234
+
235
+ function parseLogSpansError(error: unknown, spans: Span[]): Error {
236
+ if (isObject(error)) {
237
+ const detail = (error as { detail?: unknown }).detail;
238
+
239
+ if (typeof detail === "string") {
240
+ const { json: parsed } = safelyParseJSON(detail);
241
+ if (isSpanCreationErrorPayload(parsed)) {
242
+ return buildSpanCreationError(parsed);
243
+ }
244
+ } else if (Array.isArray(detail)) {
245
+ const invalidSpans = extractInvalidSpansFromValidationErrors(
246
+ detail,
247
+ spans
248
+ );
249
+ if (invalidSpans.length > 0) {
250
+ return makeSpanCreationError({
251
+ invalidSpans,
252
+ duplicateSpans: [],
253
+ totalReceived: spans.length,
254
+ // A FastAPI 422 rejects the entire request body, so no spans are
255
+ // queued — matching the all-or-nothing contract and the 400 path.
256
+ totalQueued: 0,
257
+ });
258
+ }
259
+ } else if (isSpanCreationErrorPayload(error)) {
260
+ return buildSpanCreationError(error);
261
+ }
262
+ }
263
+
264
+ return new Error(`Failed to log spans: ${formatApiError(error)}`);
265
+ }
266
+
267
+ /**
268
+ * Log spans to a project using Phoenix's simplified span structure.
269
+ *
270
+ * If any span in the request is invalid or a duplicate of a span that already
271
+ * exists, none of the spans in the request are queued and a
272
+ * {@link SpanCreationError} is thrown with details about the failures.
273
+ *
274
+ * @experimental this function is experimental and may change in the future
275
+ *
276
+ * @param params - The parameters to log spans
277
+ * @returns Statistics about the operation. When successful, `totalQueued`
278
+ * equals `totalReceived`.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * const result = await logSpans({
283
+ * project: { projectName: "my-project" },
284
+ * spans: [
285
+ * {
286
+ * name: "test",
287
+ * context: { trace_id: "123", span_id: "456" },
288
+ * span_kind: "CHAIN",
289
+ * start_time: "2024-01-01T00:00:00Z",
290
+ * end_time: "2024-01-01T00:00:01Z",
291
+ * status_code: "OK",
292
+ * },
293
+ * ],
294
+ * });
295
+ * console.log(`Queued ${result.totalQueued} spans`);
296
+ * ```
297
+ */
298
+ export async function logSpans({
299
+ client: _client,
300
+ project,
301
+ spans,
302
+ }: LogSpansParams): Promise<LogSpansResult> {
303
+ const client = _client ?? createClient();
304
+ const projectIdentifier = resolveProjectIdentifier(project);
305
+
306
+ const { data, error } = await client.POST(
307
+ "/v1/projects/{project_identifier}/spans",
308
+ {
309
+ params: {
310
+ path: {
311
+ project_identifier: projectIdentifier,
312
+ },
313
+ },
314
+ body: {
315
+ data: spans,
316
+ },
317
+ }
318
+ );
319
+
320
+ if (error) {
321
+ throw parseLogSpansError(error, spans);
322
+ }
323
+
324
+ return {
325
+ totalReceived: data?.total_received ?? spans.length,
326
+ totalQueued: data?.total_queued ?? 0,
327
+ };
328
+ }
@@ -28,22 +28,66 @@ import {
28
28
  import { currentRun, type RunState, type SuiteState } from "./state";
29
29
  import type { Annotation, KVMap } from "./types";
30
30
 
31
- function isFalsyFlag(value: string | undefined): boolean {
32
- const v = (value ?? "").toLowerCase();
31
+ export function isFalsyFlag(value: string | undefined): boolean {
32
+ // Tolerate surrounding whitespace and matching quotes that survive some
33
+ // shells and `.env` loaders (e.g. `PHOENIX_TEST_TRACKING="false"` or a value
34
+ // with a trailing newline). Without this, such a value reads as truthy and
35
+ // silently re-enables recording even though the user asked to disable it.
36
+ const v = (value ?? "")
37
+ .trim()
38
+ .replace(/^(['"])(.*)\1$/, "$2")
39
+ .trim()
40
+ .toLowerCase();
33
41
  return v === "false" || v === "0" || v === "off" || v === "no";
34
42
  }
35
43
 
44
+ /**
45
+ * Snapshot of `PHOENIX_TEST_TRACKING` as it was when this module first loaded.
46
+ *
47
+ * When the flag is exported on the command line (the documented `eval:offline`
48
+ * workflow), this captures the user's intent at process start and is immune to
49
+ * any later in-process mutation of `process.env` by a sibling suite or setup
50
+ * file. That mutation is what made tracking leak across suites in #13930: one
51
+ * suite flipped the env var and re-enabled recording for the others.
52
+ */
53
+ const trackingDisabledAtLoad = isFalsyFlag(process.env.PHOENIX_TEST_TRACKING);
54
+
55
+ /**
56
+ * Latches to `true` the first time tracking is observed disabled in this
57
+ * process. Recording is opt-out and shared process-wide, so a single falsy
58
+ * reading turns the whole run off and keeps it off — later suites cannot
59
+ * re-enable recording regardless of declaration or execution order.
60
+ */
61
+ let trackingLatchedOff = trackingDisabledAtLoad;
62
+
63
+ /**
64
+ * Reset the process-level tracking latch. Test-only seam so unit tests can
65
+ * exercise the enable/disable transitions in isolation; not part of the
66
+ * public API.
67
+ *
68
+ * @internal
69
+ */
70
+ export function __resetTrackingLatchForTests(): void {
71
+ trackingLatchedOff = isFalsyFlag(process.env.PHOENIX_TEST_TRACKING);
72
+ }
73
+
36
74
  /**
37
75
  * Decide whether tests should sync to Phoenix.
38
76
  *
39
77
  * Tracing is enabled by default. It can be disabled globally by setting
40
78
  * `PHOENIX_TEST_TRACKING=false`, or per suite via `SuiteConfig.dryRun`.
79
+ *
80
+ * The global disable is sticky for the lifetime of the process: once the flag
81
+ * is seen falsy — at load time or on any later call — tracking stays off for
82
+ * every suite. This keeps offline mode deterministic regardless of which
83
+ * suites are included in a run, or the order they execute in.
41
84
  */
42
85
  export function isTrackingEnabled(suite?: SuiteState): {
43
86
  enabled: boolean;
44
87
  reason?: string;
45
88
  } {
46
- if (isFalsyFlag(process.env.PHOENIX_TEST_TRACKING)) {
89
+ if (trackingLatchedOff || isFalsyFlag(process.env.PHOENIX_TEST_TRACKING)) {
90
+ trackingLatchedOff = true;
47
91
  return { enabled: false, reason: "PHOENIX_TEST_TRACKING is disabled" };
48
92
  }
49
93
  if (suite?.config.dryRun) {
@@ -478,6 +522,7 @@ export async function postExperimentRun(
478
522
  if (
479
523
  run.dryRun ||
480
524
  suite.trackingDisabled ||
525
+ !isTrackingEnabled(suite).enabled ||
481
526
  !suite.client ||
482
527
  !suite.experimentId
483
528
  ) {
@@ -524,7 +569,13 @@ export async function postAnnotation(
524
569
  runId: string | undefined,
525
570
  annotation: Annotation
526
571
  ): Promise<void> {
527
- if (suite.trackingDisabled || !suite.client || !runId) return;
572
+ if (
573
+ suite.trackingDisabled ||
574
+ !isTrackingEnabled(suite).enabled ||
575
+ !suite.client ||
576
+ !runId
577
+ )
578
+ return;
528
579
  const start = new Date();
529
580
  const end = new Date();
530
581
  try {