@latitude-data/telemetry 1.0.3 → 1.1.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.
package/dist/index.cjs CHANGED
@@ -13,7 +13,6 @@ var instrumentation = require('@opentelemetry/instrumentation');
13
13
  var resources = require('@opentelemetry/resources');
14
14
  var sdkTraceNode = require('@opentelemetry/sdk-trace-node');
15
15
  var instrumentationAnthropic = require('@traceloop/instrumentation-anthropic');
16
- var instrumentationAzure = require('@traceloop/instrumentation-azure');
17
16
  var instrumentationBedrock = require('@traceloop/instrumentation-bedrock');
18
17
  var instrumentationCohere = require('@traceloop/instrumentation-cohere');
19
18
  var instrumentationLangchain = require('@traceloop/instrumentation-langchain');
@@ -96,6 +95,7 @@ const DEFAULT_REDACT_SPAN_PROCESSOR = () => new RedactSpanProcessor({
96
95
  attributes: [
97
96
  /^.*auth.*$/i,
98
97
  /^.*authorization.*$/i,
98
+ /^(?!gen_ai\.).*usage.*$/i,
99
99
  /^(?!gen_ai\.).*token.*$/i,
100
100
  /^.*secret.*$/i,
101
101
  /^.*key.*$/i,
@@ -138,289 +138,6 @@ function GET_GATEWAY_BASE_URL() {
138
138
  }
139
139
  const env = { GATEWAY_BASE_URL: GET_GATEWAY_BASE_URL() };
140
140
 
141
- var SegmentSource;
142
- (function (SegmentSource) {
143
- SegmentSource["API"] = "api";
144
- SegmentSource["AgentAsTool"] = "agent_as_tool";
145
- SegmentSource["Copilot"] = "copilot";
146
- SegmentSource["EmailTrigger"] = "email_trigger";
147
- SegmentSource["Evaluation"] = "evaluation";
148
- SegmentSource["Experiment"] = "experiment";
149
- SegmentSource["Playground"] = "playground";
150
- SegmentSource["ScheduledTrigger"] = "scheduled_trigger";
151
- SegmentSource["IntegrationTrigger"] = "integration_trigger";
152
- SegmentSource["SharedPrompt"] = "shared_prompt";
153
- SegmentSource["User"] = "user";
154
- })(SegmentSource || (SegmentSource = {}));
155
- var SegmentType;
156
- (function (SegmentType) {
157
- SegmentType["Document"] = "document";
158
- SegmentType["Step"] = "step";
159
- })(SegmentType || (SegmentType = {}));
160
- const SEGMENT_SPECIFICATIONS = {
161
- [SegmentType.Document]: {
162
- name: 'Prompt',
163
- description: 'A prompt',
164
- },
165
- [SegmentType.Step]: {
166
- name: 'Step',
167
- description: 'A step in a prompt',
168
- },
169
- };
170
- const baseSegmentBaggageSchema = zod.z.object({
171
- id: zod.z.string(),
172
- parentId: zod.z.string().optional(),
173
- source: zod.z.nativeEnum(SegmentSource),
174
- });
175
- zod.z.discriminatedUnion('type', [
176
- baseSegmentBaggageSchema.extend({
177
- type: zod.z.literal(SegmentType.Document),
178
- data: zod.z.object({
179
- logUuid: zod.z.string().optional(), // TODO(tracing): temporal related log, remove when observability is ready
180
- commitUuid: zod.z.string(),
181
- documentUuid: zod.z.string(),
182
- experimentUuid: zod.z.string().optional(),
183
- externalId: zod.z.string().optional(),
184
- }),
185
- }),
186
- baseSegmentBaggageSchema.extend({
187
- type: zod.z.literal(SegmentType.Step),
188
- data: zod.z.undefined().optional(),
189
- }),
190
- ]);
191
-
192
- var SpanKind;
193
- (function (SpanKind) {
194
- SpanKind["Internal"] = "internal";
195
- SpanKind["Server"] = "server";
196
- SpanKind["Client"] = "client";
197
- SpanKind["Producer"] = "producer";
198
- SpanKind["Consumer"] = "consumer";
199
- })(SpanKind || (SpanKind = {}));
200
- // Note: loosely based on OpenTelemetry GenAI semantic conventions
201
- var SpanType;
202
- (function (SpanType) {
203
- SpanType["Tool"] = "tool";
204
- SpanType["Completion"] = "completion";
205
- SpanType["Embedding"] = "embedding";
206
- SpanType["Retrieval"] = "retrieval";
207
- SpanType["Reranking"] = "reranking";
208
- SpanType["Http"] = "http";
209
- SpanType["Segment"] = "segment";
210
- SpanType["Unknown"] = "unknown";
211
- })(SpanType || (SpanType = {}));
212
- const SPAN_SPECIFICATIONS = {
213
- [SpanType.Tool]: {
214
- name: 'Tool',
215
- description: 'A tool call',
216
- isGenAI: true,
217
- isHidden: false,
218
- },
219
- [SpanType.Completion]: {
220
- name: 'Completion',
221
- description: 'A completion call',
222
- isGenAI: true,
223
- isHidden: false,
224
- },
225
- [SpanType.Embedding]: {
226
- name: 'Embedding',
227
- description: 'An embedding call',
228
- isGenAI: true,
229
- isHidden: false,
230
- },
231
- [SpanType.Retrieval]: {
232
- name: 'Retrieval',
233
- description: 'A retrieval call',
234
- isGenAI: true,
235
- isHidden: false,
236
- },
237
- [SpanType.Reranking]: {
238
- name: 'Reranking',
239
- description: 'A reranking call',
240
- isGenAI: true,
241
- isHidden: false,
242
- },
243
- [SpanType.Http]: {
244
- name: 'HTTP',
245
- description: 'An HTTP request',
246
- isGenAI: false,
247
- isHidden: true,
248
- },
249
- [SpanType.Segment]: {
250
- name: 'Segment',
251
- description: 'A (partial) segment of a trace',
252
- isGenAI: false,
253
- isHidden: false,
254
- },
255
- [SpanType.Unknown]: {
256
- name: 'Unknown',
257
- description: 'An unknown span',
258
- isGenAI: false,
259
- isHidden: true,
260
- },
261
- };
262
- var SpanStatus;
263
- (function (SpanStatus) {
264
- SpanStatus["Unset"] = "unset";
265
- SpanStatus["Ok"] = "ok";
266
- SpanStatus["Error"] = "error";
267
- })(SpanStatus || (SpanStatus = {}));
268
-
269
- // Note: Traces are unmaterialized but this context is used to propagate the trace
270
- // See www.w3.org/TR/trace-context and w3c.github.io/baggage
271
- zod.z.object({
272
- traceparent: zod.z.string(), // <version>-<trace-id>-<span-id>-<trace-flags>
273
- tracestate: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
274
- baggage: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
275
- });
276
-
277
- /* Note: Instrumentation scopes from all language SDKs */
278
- const SCOPE_LATITUDE = 'so.latitude.instrumentation';
279
- var InstrumentationScope;
280
- (function (InstrumentationScope) {
281
- InstrumentationScope["Manual"] = "manual";
282
- InstrumentationScope["Latitude"] = "latitude";
283
- InstrumentationScope["OpenAI"] = "openai";
284
- InstrumentationScope["Anthropic"] = "anthropic";
285
- InstrumentationScope["AzureOpenAI"] = "azure";
286
- InstrumentationScope["VercelAI"] = "vercelai";
287
- InstrumentationScope["VertexAI"] = "vertexai";
288
- InstrumentationScope["AIPlatform"] = "aiplatform";
289
- InstrumentationScope["MistralAI"] = "mistralai";
290
- InstrumentationScope["Bedrock"] = "bedrock";
291
- InstrumentationScope["Sagemaker"] = "sagemaker";
292
- InstrumentationScope["TogetherAI"] = "togetherai";
293
- InstrumentationScope["Replicate"] = "replicate";
294
- InstrumentationScope["Groq"] = "groq";
295
- InstrumentationScope["Cohere"] = "cohere";
296
- InstrumentationScope["LiteLLM"] = "litellm";
297
- InstrumentationScope["Langchain"] = "langchain";
298
- InstrumentationScope["LlamaIndex"] = "llamaindex";
299
- InstrumentationScope["DSPy"] = "dspy";
300
- InstrumentationScope["Haystack"] = "haystack";
301
- InstrumentationScope["Ollama"] = "ollama";
302
- InstrumentationScope["Transformers"] = "transformers";
303
- InstrumentationScope["AlephAlpha"] = "alephalpha";
304
- })(InstrumentationScope || (InstrumentationScope = {}));
305
- /* Note: non-standard OpenTelemetry semantic conventions used in Latitude */
306
- const ATTR_LATITUDE = 'latitude';
307
- const ATTR_LATITUDE_TYPE = `${ATTR_LATITUDE}.type`;
308
- const ATTR_LATITUDE_SEGMENT_ID = `${ATTR_LATITUDE}.segment.id`;
309
- const ATTR_LATITUDE_SEGMENT_PARENT_ID = `${ATTR_LATITUDE}.segment.parent_id`;
310
- const ATTR_LATITUDE_SEGMENTS = `${ATTR_LATITUDE}.segments`;
311
- const GEN_AI_TOOL_TYPE_VALUE_FUNCTION = 'function';
312
- const ATTR_GEN_AI_TOOL_CALL_ARGUMENTS = 'gen_ai.tool.call.arguments';
313
- const ATTR_GEN_AI_TOOL_RESULT_VALUE = 'gen_ai.tool.result.value';
314
- const ATTR_GEN_AI_TOOL_RESULT_IS_ERROR = 'gen_ai.tool.result.is_error';
315
- const ATTR_GEN_AI_REQUEST = 'gen_ai.request';
316
- const ATTR_GEN_AI_REQUEST_CONFIGURATION = 'gen_ai.request.configuration';
317
- const ATTR_GEN_AI_REQUEST_TEMPLATE = 'gen_ai.request.template';
318
- const ATTR_GEN_AI_REQUEST_PARAMETERS = 'gen_ai.request.parameters';
319
- const ATTR_GEN_AI_REQUEST_MESSAGES = 'gen_ai.request.messages';
320
- const ATTR_GEN_AI_RESPONSE = 'gen_ai.response';
321
- const ATTR_GEN_AI_RESPONSE_MESSAGES = 'gen_ai.response.messages';
322
- const ATTR_GEN_AI_USAGE_PROMPT_TOKENS = 'gen_ai.usage.prompt_tokens';
323
- const ATTR_GEN_AI_USAGE_CACHED_TOKENS = 'gen_ai.usage.cached_tokens';
324
- const ATTR_GEN_AI_USAGE_REASONING_TOKENS = 'gen_ai.usage.reasoning_tokens'; // prettier-ignore
325
- const ATTR_GEN_AI_USAGE_COMPLETION_TOKENS = 'gen_ai.usage.completion_tokens'; // prettier-ignore
326
- const ATTR_GEN_AI_PROMPTS = 'gen_ai.prompt'; // gen_ai.prompt.{index}.{role/content/...}
327
- const ATTR_GEN_AI_COMPLETIONS = 'gen_ai.completion'; // gen_ai.completion.{index}.{role/content/...}
328
- const ATTR_GEN_AI_MESSAGE_ROLE = 'role';
329
- const ATTR_GEN_AI_MESSAGE_CONTENT = 'content'; // string or object
330
- const ATTR_GEN_AI_MESSAGE_TOOL_NAME = 'tool_name';
331
- const ATTR_GEN_AI_MESSAGE_TOOL_CALL_ID = 'tool_call_id';
332
- const ATTR_GEN_AI_MESSAGE_TOOL_RESULT_IS_ERROR = 'is_error';
333
- const ATTR_GEN_AI_MESSAGE_TOOL_CALLS = 'tool_calls'; // gen_ai.completion.{index}.tool_calls.{index}.{id/name/arguments}
334
- const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_ID = 'id';
335
- const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_NAME = 'name';
336
- const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_ARGUMENTS = 'arguments';
337
- const GEN_AI_RESPONSE_FINISH_REASON_VALUE_STOP = 'stop';
338
- const GEN_AI_RESPONSE_FINISH_REASON_VALUE_TOOL_CALLS = 'tool_calls';
339
- const ATTR_HTTP_REQUEST_URL = 'http.request.url';
340
- const ATTR_HTTP_REQUEST_BODY = 'http.request.body';
341
- const ATTR_HTTP_REQUEST_HEADER = 'http.request.header';
342
- const ATTR_HTTP_RESPONSE_BODY = 'http.response.body';
343
- const ATTR_HTTP_RESPONSE_HEADER = 'http.response.header';
344
- /* Note: Schemas for span ingestion following OpenTelemetry service request specification */
345
- var Otlp;
346
- (function (Otlp) {
347
- Otlp.attributeValueSchema = zod.z.object({
348
- stringValue: zod.z.string().optional(),
349
- intValue: zod.z.number().optional(),
350
- boolValue: zod.z.boolean().optional(),
351
- arrayValue: zod.z
352
- .object({
353
- values: zod.z.array(zod.z.object({
354
- stringValue: zod.z.string().optional(),
355
- intValue: zod.z.number().optional(),
356
- boolValue: zod.z.boolean().optional(),
357
- })),
358
- })
359
- .optional(),
360
- });
361
- Otlp.attributeSchema = zod.z.object({
362
- key: zod.z.string(),
363
- value: Otlp.attributeValueSchema,
364
- });
365
- Otlp.eventSchema = zod.z.object({
366
- name: zod.z.string(),
367
- timeUnixNano: zod.z.string(),
368
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
369
- });
370
- Otlp.linkSchema = zod.z.object({
371
- traceId: zod.z.string(),
372
- spanId: zod.z.string(),
373
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
374
- });
375
- (function (StatusCode) {
376
- StatusCode[StatusCode["Unset"] = 0] = "Unset";
377
- StatusCode[StatusCode["Ok"] = 1] = "Ok";
378
- StatusCode[StatusCode["Error"] = 2] = "Error";
379
- })(Otlp.StatusCode || (Otlp.StatusCode = {}));
380
- Otlp.statusSchema = zod.z.object({
381
- code: zod.z.number(),
382
- message: zod.z.string().optional(),
383
- });
384
- (function (SpanKind) {
385
- SpanKind[SpanKind["Internal"] = 0] = "Internal";
386
- SpanKind[SpanKind["Server"] = 1] = "Server";
387
- SpanKind[SpanKind["Client"] = 2] = "Client";
388
- SpanKind[SpanKind["Producer"] = 3] = "Producer";
389
- SpanKind[SpanKind["Consumer"] = 4] = "Consumer";
390
- })(Otlp.SpanKind || (Otlp.SpanKind = {}));
391
- Otlp.spanSchema = zod.z.object({
392
- traceId: zod.z.string(),
393
- spanId: zod.z.string(),
394
- parentSpanId: zod.z.string().optional(),
395
- name: zod.z.string(),
396
- kind: zod.z.number(),
397
- startTimeUnixNano: zod.z.string(),
398
- endTimeUnixNano: zod.z.string(),
399
- status: Otlp.statusSchema.optional(),
400
- events: zod.z.array(Otlp.eventSchema).optional(),
401
- links: zod.z.array(Otlp.linkSchema).optional(),
402
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
403
- });
404
- Otlp.scopeSchema = zod.z.object({
405
- name: zod.z.string(),
406
- version: zod.z.string().optional(),
407
- });
408
- Otlp.scopeSpanSchema = zod.z.object({
409
- scope: Otlp.scopeSchema,
410
- spans: zod.z.array(Otlp.spanSchema),
411
- });
412
- Otlp.resourceSchema = zod.z.object({
413
- attributes: zod.z.array(Otlp.attributeSchema),
414
- });
415
- Otlp.resourceSpanSchema = zod.z.object({
416
- resource: Otlp.resourceSchema,
417
- scopeSpans: zod.z.array(Otlp.scopeSpanSchema),
418
- });
419
- Otlp.serviceRequestSchema = zod.z.object({
420
- resourceSpans: zod.z.array(Otlp.resourceSpanSchema),
421
- });
422
- })(Otlp || (Otlp = {}));
423
-
424
141
  var StreamEventTypes;
425
142
  (function (StreamEventTypes) {
426
143
  StreamEventTypes["Latitude"] = "latitude-event";
@@ -445,17 +162,27 @@ var LatitudeTool;
445
162
  LatitudeTool["RunCode"] = "code";
446
163
  LatitudeTool["WebSearch"] = "search";
447
164
  LatitudeTool["WebExtract"] = "extract";
165
+ LatitudeTool["Think"] = "think";
166
+ LatitudeTool["TODO"] = "todo";
448
167
  })(LatitudeTool || (LatitudeTool = {}));
449
168
  var LatitudeToolInternalName;
450
169
  (function (LatitudeToolInternalName) {
451
170
  LatitudeToolInternalName["RunCode"] = "lat_tool_run_code";
452
171
  LatitudeToolInternalName["WebSearch"] = "lat_tool_web_search";
453
172
  LatitudeToolInternalName["WebExtract"] = "lat_tool_web_extract";
173
+ LatitudeToolInternalName["Think"] = "think";
174
+ LatitudeToolInternalName["TODO"] = "todo_write";
454
175
  })(LatitudeToolInternalName || (LatitudeToolInternalName = {}));
176
+ [
177
+ LatitudeTool.Think,
178
+ LatitudeTool.TODO,
179
+ ];
455
180
 
456
181
  const actualOutputConfiguration = zod.z.object({
457
182
  messageSelection: zod.z.enum(['last', 'all']), // Which assistant messages to select
458
- contentFilter: zod.z.enum(['text', 'image', 'file', 'tool_call']).optional(),
183
+ contentFilter: zod.z
184
+ .enum(['text', 'reasoning', 'image', 'file', 'tool_call'])
185
+ .optional(),
459
186
  parsingFormat: zod.z.enum(['string', 'json']),
460
187
  fieldAccessor: zod.z.string().optional(), // Field accessor to get the output from if it's a key-value format
461
188
  });
@@ -465,11 +192,11 @@ const expectedOutputConfiguration = zod.z.object({
465
192
  });
466
193
  const baseEvaluationConfiguration = zod.z.object({
467
194
  reverseScale: zod.z.boolean(), // If true, lower is better, otherwise, higher is better
468
- actualOutput: actualOutputConfiguration.optional(), // Optional for backwards compatibility
469
- expectedOutput: expectedOutputConfiguration.optional(), // Optional for backwards compatibility
195
+ actualOutput: actualOutputConfiguration,
196
+ expectedOutput: expectedOutputConfiguration.optional(),
470
197
  });
471
198
  const baseEvaluationResultMetadata = zod.z.object({
472
- // Configuration snapshot is defined in every metric specification
199
+ // configuration: Configuration snapshot is defined in every metric specification
473
200
  actualOutput: zod.z.string(),
474
201
  expectedOutput: zod.z.string().optional(),
475
202
  datasetLabel: zod.z.string().optional(),
@@ -478,7 +205,79 @@ const baseEvaluationResultError = zod.z.object({
478
205
  message: zod.z.string(),
479
206
  });
480
207
 
208
+ const compositeEvaluationConfiguration = baseEvaluationConfiguration.extend({
209
+ evaluationUuids: zod.z.array(zod.z.string()),
210
+ minThreshold: zod.z.number().optional(), // Threshold percentage
211
+ maxThreshold: zod.z.number().optional(), // Threshold percentage
212
+ defaultTarget: zod.z.boolean().optional(), // Default for optimizations and distillations
213
+ });
214
+ const compositeEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
215
+ results: zod.z.record(zod.z.string(), // Evaluation uuid
216
+ zod.z.object({
217
+ uuid: zod.z.string(), // Result uuid (for side effects)
218
+ name: zod.z.string(), // Evaluation name
219
+ score: zod.z.number(), // Normalized score
220
+ reason: zod.z.string(),
221
+ passed: zod.z.boolean(),
222
+ })),
223
+ });
224
+ const compositeEvaluationResultError = baseEvaluationResultError.extend({
225
+ errors: zod.z
226
+ .record(zod.z.string(), // Evaluation uuid
227
+ zod.z.object({
228
+ uuid: zod.z.string(), // Result uuid (for side effects)
229
+ name: zod.z.string(), // Evaluation name
230
+ message: zod.z.string(),
231
+ }))
232
+ .optional(),
233
+ });
234
+ // AVERAGE
235
+ const compositeEvaluationAverageConfiguration = compositeEvaluationConfiguration.extend({});
236
+ compositeEvaluationResultMetadata.extend({
237
+ configuration: compositeEvaluationAverageConfiguration,
238
+ });
239
+ compositeEvaluationResultError.extend({});
240
+ const CompositeEvaluationAverageSpecification = {
241
+ };
242
+ // WEIGHTED
243
+ const compositeEvaluationWeightedConfiguration = compositeEvaluationConfiguration.extend({
244
+ weights: zod.z.record(zod.z.string(), // Evaluation uuid
245
+ zod.z.number()),
246
+ });
247
+ compositeEvaluationResultMetadata.extend({
248
+ configuration: compositeEvaluationWeightedConfiguration,
249
+ });
250
+ compositeEvaluationResultError.extend({});
251
+ const CompositeEvaluationWeightedSpecification = {
252
+ };
253
+ // CUSTOM
254
+ const compositeEvaluationCustomConfiguration = compositeEvaluationConfiguration.extend({
255
+ formula: zod.z.string(),
256
+ });
257
+ compositeEvaluationResultMetadata.extend({
258
+ configuration: compositeEvaluationCustomConfiguration,
259
+ });
260
+ compositeEvaluationResultError.extend({});
261
+ const CompositeEvaluationCustomSpecification = {
262
+ };
263
+ /* ------------------------------------------------------------------------- */
264
+ var CompositeEvaluationMetric;
265
+ (function (CompositeEvaluationMetric) {
266
+ CompositeEvaluationMetric["Average"] = "average";
267
+ CompositeEvaluationMetric["Weighted"] = "weighted";
268
+ CompositeEvaluationMetric["Custom"] = "custom";
269
+ })(CompositeEvaluationMetric || (CompositeEvaluationMetric = {}));
270
+ const CompositeEvaluationSpecification = {
271
+ // prettier-ignore
272
+ metrics: {
273
+ [CompositeEvaluationMetric.Average]: CompositeEvaluationAverageSpecification,
274
+ [CompositeEvaluationMetric.Weighted]: CompositeEvaluationWeightedSpecification,
275
+ [CompositeEvaluationMetric.Custom]: CompositeEvaluationCustomSpecification,
276
+ },
277
+ };
278
+
481
279
  const humanEvaluationConfiguration = baseEvaluationConfiguration.extend({
280
+ enableControls: zod.z.boolean().optional(), // UI annotation controls
482
281
  criteria: zod.z.string().optional(),
483
282
  });
484
283
  const humanEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
@@ -619,7 +418,9 @@ const LlmEvaluationSpecification = {
619
418
  };
620
419
 
621
420
  const ruleEvaluationConfiguration = baseEvaluationConfiguration.extend({});
622
- const ruleEvaluationResultMetadata = baseEvaluationResultMetadata.extend({});
421
+ const ruleEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
422
+ reason: zod.z.string().optional(),
423
+ });
623
424
  const ruleEvaluationResultError = baseEvaluationResultError.extend({});
624
425
  // EXACT MATCH
625
426
  const ruleEvaluationExactMatchConfiguration = ruleEvaluationConfiguration.extend({
@@ -729,12 +530,14 @@ var EvaluationType;
729
530
  EvaluationType["Rule"] = "rule";
730
531
  EvaluationType["Llm"] = "llm";
731
532
  EvaluationType["Human"] = "human";
533
+ EvaluationType["Composite"] = "composite";
732
534
  })(EvaluationType || (EvaluationType = {}));
733
- const EvaluationTypeSchema = zod.z.nativeEnum(EvaluationType);
535
+ const EvaluationTypeSchema = zod.z.enum(EvaluationType);
734
536
  const EvaluationMetricSchema = zod.z.union([
735
- zod.z.nativeEnum(RuleEvaluationMetric),
736
- zod.z.nativeEnum(LlmEvaluationMetric),
737
- zod.z.nativeEnum(HumanEvaluationMetric),
537
+ zod.z.enum(RuleEvaluationMetric),
538
+ zod.z.enum(LlmEvaluationMetric),
539
+ zod.z.enum(HumanEvaluationMetric),
540
+ zod.z.enum(CompositeEvaluationMetric),
738
541
  ]);
739
542
  const EvaluationConfigurationSchema = zod.z.custom();
740
543
  // prettier-ignore
@@ -745,6 +548,7 @@ zod.z.custom();
745
548
  [EvaluationType.Rule]: RuleEvaluationSpecification,
746
549
  [EvaluationType.Llm]: LlmEvaluationSpecification,
747
550
  [EvaluationType.Human]: HumanEvaluationSpecification,
551
+ [EvaluationType.Composite]: CompositeEvaluationSpecification,
748
552
  });
749
553
  zod.z.object({
750
554
  name: zod.z.string(),
@@ -758,7 +562,6 @@ zod.z.object({
758
562
  enableSuggestions: zod.z.boolean().nullable().optional(),
759
563
  autoApplySuggestions: zod.z.boolean().nullable().optional(),
760
564
  });
761
- Object.values(SegmentSource).filter((source) => source !== SegmentSource.Evaluation && source !== SegmentSource.Experiment);
762
565
 
763
566
  var LegacyChainEventTypes;
764
567
  (function (LegacyChainEventTypes) {
@@ -779,17 +582,71 @@ var ChainEventTypes;
779
582
  ChainEventTypes["StepCompleted"] = "step-completed";
780
583
  ChainEventTypes["StepStarted"] = "step-started";
781
584
  ChainEventTypes["ToolCompleted"] = "tool-completed";
782
- ChainEventTypes["ToolsRequested"] = "tools-requested";
783
585
  ChainEventTypes["ToolResult"] = "tool-result";
784
586
  ChainEventTypes["ToolsStarted"] = "tools-started";
785
587
  })(ChainEventTypes || (ChainEventTypes = {}));
786
588
 
589
+ zod.z.object({
590
+ name: zod.z.string(),
591
+ provider: zod.z.string(),
592
+ model: zod.z.string(),
593
+ temperature: zod.z.number(),
594
+ });
595
+ // Experiment ran from a dataset
596
+ const experimentDatasetSourceSchema = zod.z.object({
597
+ source: zod.z.literal('dataset'),
598
+ datasetId: zod.z.number(),
599
+ fromRow: zod.z.number(),
600
+ toRow: zod.z.number(),
601
+ datasetLabels: zod.z.record(zod.z.string(), zod.z.string()),
602
+ parametersMap: zod.z.record(zod.z.string(), zod.z.number()),
603
+ });
604
+ // Experiment ran from last logs (from commit and creation time of experiment)
605
+ const experimentLogsSourceSchema = zod.z.object({
606
+ source: zod.z.literal('logs'),
607
+ count: zod.z.number(),
608
+ });
609
+ // Experiment ran with manual parameters (currently only used for prompts with no parameters)
610
+ const experimentManualSourceSchema = zod.z.object({
611
+ source: zod.z.literal('manual'),
612
+ count: zod.z.number(),
613
+ parametersMap: zod.z.record(zod.z.string(), zod.z.number()),
614
+ });
615
+ zod.z.discriminatedUnion('source', [
616
+ experimentDatasetSourceSchema,
617
+ experimentLogsSourceSchema,
618
+ experimentManualSourceSchema,
619
+ ]);
620
+
621
+ var QuotaType;
622
+ (function (QuotaType) {
623
+ QuotaType["Seats"] = "seats";
624
+ QuotaType["Runs"] = "runs";
625
+ QuotaType["Credits"] = "credits";
626
+ })(QuotaType || (QuotaType = {}));
627
+ var GrantSource;
628
+ (function (GrantSource) {
629
+ GrantSource["System"] = "system";
630
+ GrantSource["Subscription"] = "subscription";
631
+ GrantSource["Purchase"] = "purchase";
632
+ GrantSource["Reward"] = "reward";
633
+ GrantSource["Promocode"] = "promocode";
634
+ })(GrantSource || (GrantSource = {}));
635
+
636
+ var ModifiedDocumentType;
637
+ (function (ModifiedDocumentType) {
638
+ ModifiedDocumentType["Created"] = "created";
639
+ ModifiedDocumentType["Updated"] = "updated";
640
+ ModifiedDocumentType["UpdatedPath"] = "updated_path";
641
+ ModifiedDocumentType["Deleted"] = "deleted";
642
+ })(ModifiedDocumentType || (ModifiedDocumentType = {}));
643
+
787
644
  var IntegrationType;
788
645
  (function (IntegrationType) {
789
646
  IntegrationType["Latitude"] = "latitude";
790
647
  IntegrationType["ExternalMCP"] = "custom_mcp";
791
- IntegrationType["HostedMCP"] = "mcp_server";
792
648
  IntegrationType["Pipedream"] = "pipedream";
649
+ IntegrationType["HostedMCP"] = "mcp_server";
793
650
  })(IntegrationType || (IntegrationType = {}));
794
651
  var HostedIntegrationType;
795
652
  (function (HostedIntegrationType) {
@@ -886,13 +743,40 @@ var HostedIntegrationType;
886
743
  // Loops = 'loops', // Does not exist
887
744
  })(HostedIntegrationType || (HostedIntegrationType = {}));
888
745
 
889
- // TODO(evalsv2): Remove
890
- var EvaluationResultableType;
891
- (function (EvaluationResultableType) {
892
- EvaluationResultableType["Boolean"] = "evaluation_resultable_booleans";
893
- EvaluationResultableType["Text"] = "evaluation_resultable_texts";
894
- EvaluationResultableType["Number"] = "evaluation_resultable_numbers";
895
- })(EvaluationResultableType || (EvaluationResultableType = {}));
746
+ var LogSources;
747
+ (function (LogSources) {
748
+ LogSources["API"] = "api";
749
+ LogSources["AgentAsTool"] = "agent_as_tool";
750
+ LogSources["Copilot"] = "copilot";
751
+ LogSources["EmailTrigger"] = "email_trigger";
752
+ LogSources["Evaluation"] = "evaluation";
753
+ LogSources["Experiment"] = "experiment";
754
+ LogSources["IntegrationTrigger"] = "integration_trigger";
755
+ LogSources["Playground"] = "playground";
756
+ LogSources["ScheduledTrigger"] = "scheduled_trigger";
757
+ LogSources["SharedPrompt"] = "shared_prompt";
758
+ LogSources["ShadowTest"] = "shadow_test";
759
+ LogSources["ABTestChallenger"] = "ab_test_challenger";
760
+ LogSources["User"] = "user";
761
+ })(LogSources || (LogSources = {}));
762
+
763
+ var RunSourceGroup;
764
+ (function (RunSourceGroup) {
765
+ RunSourceGroup["Production"] = "production";
766
+ RunSourceGroup["Playground"] = "playground";
767
+ })(RunSourceGroup || (RunSourceGroup = {}));
768
+ ({
769
+ [RunSourceGroup.Production]: [
770
+ LogSources.API,
771
+ LogSources.Copilot,
772
+ LogSources.EmailTrigger,
773
+ LogSources.IntegrationTrigger,
774
+ LogSources.ScheduledTrigger,
775
+ LogSources.SharedPrompt,
776
+ LogSources.User,
777
+ ],
778
+ [RunSourceGroup.Playground]: [LogSources.Playground, LogSources.Experiment],
779
+ });
896
780
 
897
781
  var MessageRole;
898
782
  (function (MessageRole) {
@@ -902,13 +786,272 @@ var MessageRole;
902
786
  MessageRole["tool"] = "tool";
903
787
  })(MessageRole || (MessageRole = {}));
904
788
 
905
- var ModifiedDocumentType;
906
- (function (ModifiedDocumentType) {
907
- ModifiedDocumentType["Created"] = "created";
908
- ModifiedDocumentType["Updated"] = "updated";
909
- ModifiedDocumentType["UpdatedPath"] = "updated_path";
910
- ModifiedDocumentType["Deleted"] = "deleted";
911
- })(ModifiedDocumentType || (ModifiedDocumentType = {}));
789
+ var SpanKind;
790
+ (function (SpanKind) {
791
+ SpanKind["Internal"] = "internal";
792
+ SpanKind["Server"] = "server";
793
+ SpanKind["Client"] = "client";
794
+ SpanKind["Producer"] = "producer";
795
+ SpanKind["Consumer"] = "consumer";
796
+ })(SpanKind || (SpanKind = {}));
797
+ // Note: loosely based on OpenTelemetry GenAI semantic conventions
798
+ var SpanType;
799
+ (function (SpanType) {
800
+ SpanType["Tool"] = "tool";
801
+ SpanType["Completion"] = "completion";
802
+ SpanType["Embedding"] = "embedding";
803
+ SpanType["Retrieval"] = "retrieval";
804
+ SpanType["Reranking"] = "reranking";
805
+ SpanType["Http"] = "http";
806
+ SpanType["Unknown"] = "unknown";
807
+ SpanType["Prompt"] = "prompt";
808
+ SpanType["Chat"] = "chat";
809
+ SpanType["External"] = "external";
810
+ SpanType["UnresolvedExternal"] = "unresolved_external";
811
+ SpanType["Step"] = "step";
812
+ })(SpanType || (SpanType = {}));
813
+ const SPAN_SPECIFICATIONS = {
814
+ [SpanType.Tool]: {
815
+ name: 'Tool',
816
+ description: 'A tool call',
817
+ isGenAI: true,
818
+ isHidden: false,
819
+ },
820
+ [SpanType.Completion]: {
821
+ name: 'Completion',
822
+ description: 'A completion call',
823
+ isGenAI: true,
824
+ isHidden: false,
825
+ },
826
+ [SpanType.Embedding]: {
827
+ name: 'Embedding',
828
+ description: 'An embedding call',
829
+ isGenAI: true,
830
+ isHidden: false,
831
+ },
832
+ [SpanType.Retrieval]: {
833
+ name: 'Retrieval',
834
+ description: 'A retrieval call',
835
+ isGenAI: true,
836
+ isHidden: false,
837
+ },
838
+ [SpanType.Reranking]: {
839
+ name: 'Reranking',
840
+ description: 'A reranking call',
841
+ isGenAI: true,
842
+ isHidden: false,
843
+ },
844
+ [SpanType.Http]: {
845
+ name: 'HTTP',
846
+ description: 'An HTTP request',
847
+ isGenAI: false,
848
+ isHidden: true,
849
+ },
850
+ [SpanType.Unknown]: {
851
+ name: 'Unknown',
852
+ description: 'An unknown span',
853
+ isGenAI: false,
854
+ isHidden: true,
855
+ },
856
+ [SpanType.Prompt]: {
857
+ name: 'Prompt',
858
+ description: 'A prompt span',
859
+ isGenAI: false,
860
+ isHidden: false,
861
+ },
862
+ [SpanType.Chat]: {
863
+ name: 'Chat',
864
+ description: 'A chat continuation span',
865
+ isGenAI: false,
866
+ isHidden: false,
867
+ },
868
+ [SpanType.External]: {
869
+ name: 'External',
870
+ description: 'An external capture span',
871
+ isGenAI: false,
872
+ isHidden: false,
873
+ },
874
+ [SpanType.UnresolvedExternal]: {
875
+ name: 'Unresolved External',
876
+ description: 'An external span that needs path resolution before storage',
877
+ isGenAI: false,
878
+ isHidden: true,
879
+ },
880
+ [SpanType.Step]: {
881
+ name: 'Step',
882
+ description: 'A step span',
883
+ isGenAI: false,
884
+ isHidden: false,
885
+ },
886
+ };
887
+ var SpanStatus;
888
+ (function (SpanStatus) {
889
+ SpanStatus["Unset"] = "unset";
890
+ SpanStatus["Ok"] = "ok";
891
+ SpanStatus["Error"] = "error";
892
+ })(SpanStatus || (SpanStatus = {}));
893
+
894
+ // Note: Traces are unmaterialized but this context is used to propagate the trace
895
+ // See www.w3.org/TR/trace-context and w3c.github.io/baggage
896
+ zod.z.object({
897
+ traceparent: zod.z.string(), // <version>-<trace-id>-<span-id>-<trace-flags>
898
+ tracestate: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
899
+ baggage: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
900
+ });
901
+
902
+ /* Note: Instrumentation scopes from all language SDKs */
903
+ const SCOPE_LATITUDE = 'so.latitude.instrumentation';
904
+ var InstrumentationScope;
905
+ (function (InstrumentationScope) {
906
+ InstrumentationScope["Manual"] = "manual";
907
+ InstrumentationScope["Latitude"] = "latitude";
908
+ InstrumentationScope["OpenAI"] = "openai";
909
+ InstrumentationScope["Anthropic"] = "anthropic";
910
+ InstrumentationScope["AzureOpenAI"] = "azure";
911
+ InstrumentationScope["VercelAI"] = "vercelai";
912
+ InstrumentationScope["VertexAI"] = "vertexai";
913
+ InstrumentationScope["AIPlatform"] = "aiplatform";
914
+ InstrumentationScope["MistralAI"] = "mistralai";
915
+ InstrumentationScope["Bedrock"] = "bedrock";
916
+ InstrumentationScope["Sagemaker"] = "sagemaker";
917
+ InstrumentationScope["TogetherAI"] = "togetherai";
918
+ InstrumentationScope["Replicate"] = "replicate";
919
+ InstrumentationScope["Groq"] = "groq";
920
+ InstrumentationScope["Cohere"] = "cohere";
921
+ InstrumentationScope["LiteLLM"] = "litellm";
922
+ InstrumentationScope["Langchain"] = "langchain";
923
+ InstrumentationScope["LlamaIndex"] = "llamaindex";
924
+ InstrumentationScope["DSPy"] = "dspy";
925
+ InstrumentationScope["Haystack"] = "haystack";
926
+ InstrumentationScope["Ollama"] = "ollama";
927
+ InstrumentationScope["Transformers"] = "transformers";
928
+ InstrumentationScope["AlephAlpha"] = "alephalpha";
929
+ })(InstrumentationScope || (InstrumentationScope = {}));
930
+ /* Note: non-standard OpenTelemetry semantic conventions used in Latitude */
931
+ const ATTR_LATITUDE = 'latitude';
932
+ const ATTR_LATITUDE_TYPE = `${ATTR_LATITUDE}.type`;
933
+ const ATTR_LATITUDE_DOCUMENT_UUID = `${ATTR_LATITUDE}.document_uuid`;
934
+ const ATTR_LATITUDE_PROMPT_PATH = `${ATTR_LATITUDE}.prompt_path`;
935
+ const ATTR_LATITUDE_COMMIT_UUID = `${ATTR_LATITUDE}.commit_uuid`;
936
+ const ATTR_LATITUDE_DOCUMENT_LOG_UUID = `${ATTR_LATITUDE}.document_log_uuid`;
937
+ const ATTR_LATITUDE_PROJECT_ID = `${ATTR_LATITUDE}.project_id`;
938
+ const ATTR_LATITUDE_EXPERIMENT_UUID = `${ATTR_LATITUDE}.experiment_uuid`;
939
+ const ATTR_LATITUDE_SOURCE = `${ATTR_LATITUDE}.source`;
940
+ const ATTR_LATITUDE_EXTERNAL_ID = `${ATTR_LATITUDE}.external_id`;
941
+ const ATTR_LATITUDE_TEST_DEPLOYMENT_ID = `${ATTR_LATITUDE}.test_deployment_id`;
942
+ const ATTR_LATITUDE_PREVIOUS_TRACE_ID = `${ATTR_LATITUDE}.previous_trace_id`;
943
+ const GEN_AI_TOOL_TYPE_VALUE_FUNCTION = 'function';
944
+ const ATTR_GEN_AI_TOOL_CALL_ARGUMENTS = 'gen_ai.tool.call.arguments';
945
+ const ATTR_GEN_AI_TOOL_RESULT_VALUE = 'gen_ai.tool.result.value';
946
+ const ATTR_GEN_AI_TOOL_RESULT_IS_ERROR = 'gen_ai.tool.result.is_error';
947
+ const ATTR_GEN_AI_REQUEST = 'gen_ai.request';
948
+ const ATTR_GEN_AI_REQUEST_CONFIGURATION = 'gen_ai.request.configuration';
949
+ const ATTR_GEN_AI_REQUEST_TEMPLATE = 'gen_ai.request.template';
950
+ const ATTR_GEN_AI_REQUEST_PARAMETERS = 'gen_ai.request.parameters';
951
+ const ATTR_GEN_AI_REQUEST_MESSAGES = 'gen_ai.request.messages';
952
+ const ATTR_GEN_AI_RESPONSE = 'gen_ai.response';
953
+ const ATTR_GEN_AI_RESPONSE_MESSAGES = 'gen_ai.response.messages';
954
+ const ATTR_GEN_AI_USAGE_PROMPT_TOKENS = 'gen_ai.usage.prompt_tokens';
955
+ const ATTR_GEN_AI_USAGE_CACHED_TOKENS = 'gen_ai.usage.cached_tokens';
956
+ const ATTR_GEN_AI_USAGE_REASONING_TOKENS = 'gen_ai.usage.reasoning_tokens'; // prettier-ignore
957
+ const ATTR_GEN_AI_USAGE_COMPLETION_TOKENS = 'gen_ai.usage.completion_tokens'; // prettier-ignore
958
+ const ATTR_GEN_AI_PROMPTS = 'gen_ai.prompt'; // gen_ai.prompt.{index}.{role/content/...}
959
+ const ATTR_GEN_AI_COMPLETIONS = 'gen_ai.completion'; // gen_ai.completion.{index}.{role/content/...}
960
+ const ATTR_GEN_AI_MESSAGE_ROLE = 'role';
961
+ const ATTR_GEN_AI_MESSAGE_CONTENT = 'content'; // string or object
962
+ const ATTR_GEN_AI_MESSAGE_TOOL_NAME = 'tool_name';
963
+ const ATTR_GEN_AI_MESSAGE_TOOL_CALL_ID = 'tool_call_id';
964
+ const ATTR_GEN_AI_MESSAGE_TOOL_RESULT_IS_ERROR = 'is_error';
965
+ const ATTR_GEN_AI_MESSAGE_TOOL_CALLS = 'tool_calls'; // gen_ai.completion.{index}.tool_calls.{index}.{id/name/arguments}
966
+ const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_ID = 'id';
967
+ const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_NAME = 'name';
968
+ const ATTR_GEN_AI_MESSAGE_TOOL_CALLS_ARGUMENTS = 'arguments';
969
+ const GEN_AI_RESPONSE_FINISH_REASON_VALUE_STOP = 'stop';
970
+ const GEN_AI_RESPONSE_FINISH_REASON_VALUE_TOOL_CALLS = 'tool_calls';
971
+ const ATTR_HTTP_REQUEST_URL = 'http.request.url';
972
+ const ATTR_HTTP_REQUEST_BODY = 'http.request.body';
973
+ const ATTR_HTTP_REQUEST_HEADER = 'http.request.header';
974
+ const ATTR_HTTP_RESPONSE_BODY = 'http.response.body';
975
+ const ATTR_HTTP_RESPONSE_HEADER = 'http.response.header';
976
+ /* Note: Schemas for span ingestion following OpenTelemetry service request specification */
977
+ var Otlp;
978
+ (function (Otlp) {
979
+ Otlp.attributeValueSchema = zod.z.object({
980
+ stringValue: zod.z.string().optional(),
981
+ intValue: zod.z.number().optional(),
982
+ boolValue: zod.z.boolean().optional(),
983
+ arrayValue: zod.z
984
+ .object({
985
+ values: zod.z.array(zod.z.object({
986
+ stringValue: zod.z.string().optional(),
987
+ intValue: zod.z.number().optional(),
988
+ boolValue: zod.z.boolean().optional(),
989
+ })),
990
+ })
991
+ .optional(),
992
+ });
993
+ Otlp.attributeSchema = zod.z.object({
994
+ key: zod.z.string(),
995
+ value: Otlp.attributeValueSchema,
996
+ });
997
+ Otlp.eventSchema = zod.z.object({
998
+ name: zod.z.string(),
999
+ timeUnixNano: zod.z.string(),
1000
+ attributes: zod.z.array(Otlp.attributeSchema).optional(),
1001
+ });
1002
+ Otlp.linkSchema = zod.z.object({
1003
+ traceId: zod.z.string(),
1004
+ spanId: zod.z.string(),
1005
+ attributes: zod.z.array(Otlp.attributeSchema).optional(),
1006
+ });
1007
+ (function (StatusCode) {
1008
+ StatusCode[StatusCode["Unset"] = 0] = "Unset";
1009
+ StatusCode[StatusCode["Ok"] = 1] = "Ok";
1010
+ StatusCode[StatusCode["Error"] = 2] = "Error";
1011
+ })(Otlp.StatusCode || (Otlp.StatusCode = {}));
1012
+ Otlp.statusSchema = zod.z.object({
1013
+ code: zod.z.number(),
1014
+ message: zod.z.string().optional(),
1015
+ });
1016
+ (function (SpanKind) {
1017
+ SpanKind[SpanKind["Internal"] = 0] = "Internal";
1018
+ SpanKind[SpanKind["Server"] = 1] = "Server";
1019
+ SpanKind[SpanKind["Client"] = 2] = "Client";
1020
+ SpanKind[SpanKind["Producer"] = 3] = "Producer";
1021
+ SpanKind[SpanKind["Consumer"] = 4] = "Consumer";
1022
+ })(Otlp.SpanKind || (Otlp.SpanKind = {}));
1023
+ Otlp.spanSchema = zod.z.object({
1024
+ traceId: zod.z.string(),
1025
+ spanId: zod.z.string(),
1026
+ parentSpanId: zod.z.string().optional(),
1027
+ name: zod.z.string(),
1028
+ kind: zod.z.number(),
1029
+ startTimeUnixNano: zod.z.string(),
1030
+ endTimeUnixNano: zod.z.string(),
1031
+ status: Otlp.statusSchema.optional(),
1032
+ events: zod.z.array(Otlp.eventSchema).optional(),
1033
+ links: zod.z.array(Otlp.linkSchema).optional(),
1034
+ attributes: zod.z.array(Otlp.attributeSchema).optional(),
1035
+ });
1036
+ Otlp.scopeSchema = zod.z.object({
1037
+ name: zod.z.string(),
1038
+ version: zod.z.string().optional(),
1039
+ });
1040
+ Otlp.scopeSpanSchema = zod.z.object({
1041
+ scope: Otlp.scopeSchema,
1042
+ spans: zod.z.array(Otlp.spanSchema),
1043
+ });
1044
+ Otlp.resourceSchema = zod.z.object({
1045
+ attributes: zod.z.array(Otlp.attributeSchema),
1046
+ });
1047
+ Otlp.resourceSpanSchema = zod.z.object({
1048
+ resource: Otlp.resourceSchema,
1049
+ scopeSpans: zod.z.array(Otlp.scopeSpanSchema),
1050
+ });
1051
+ Otlp.serviceRequestSchema = zod.z.object({
1052
+ resourceSpans: zod.z.array(Otlp.resourceSpanSchema),
1053
+ });
1054
+ })(Otlp || (Otlp = {}));
912
1055
 
913
1056
  // TODO(tracing): deprecated
914
1057
  const HEAD_COMMIT = 'live';
@@ -939,6 +1082,12 @@ var DocumentTriggerType;
939
1082
  DocumentTriggerType["Scheduled"] = "scheduled";
940
1083
  DocumentTriggerType["Integration"] = "integration";
941
1084
  })(DocumentTriggerType || (DocumentTriggerType = {}));
1085
+ var DocumentTriggerStatus;
1086
+ (function (DocumentTriggerStatus) {
1087
+ DocumentTriggerStatus["Pending"] = "pending";
1088
+ DocumentTriggerStatus["Deployed"] = "deployed";
1089
+ DocumentTriggerStatus["Deprecated"] = "deprecated";
1090
+ })(DocumentTriggerStatus || (DocumentTriggerStatus = {}));
942
1091
  var DocumentTriggerParameters;
943
1092
  (function (DocumentTriggerParameters) {
944
1093
  DocumentTriggerParameters["SenderEmail"] = "senderEmail";
@@ -947,14 +1096,13 @@ var DocumentTriggerParameters;
947
1096
  DocumentTriggerParameters["Body"] = "body";
948
1097
  DocumentTriggerParameters["Attachments"] = "attachments";
949
1098
  })(DocumentTriggerParameters || (DocumentTriggerParameters = {}));
1099
+ const DOCUMENT_PATH_REGEXP = /^([\w-]+\/)*([\w-.])+$/;
950
1100
 
951
1101
  class ManualInstrumentation {
952
1102
  enabled;
953
- source;
954
1103
  tracer;
955
- constructor(source, tracer) {
1104
+ constructor(tracer) {
956
1105
  this.enabled = false;
957
- this.source = source;
958
1106
  this.tracer = tracer;
959
1107
  }
960
1108
  isEnabled() {
@@ -966,95 +1114,34 @@ class ManualInstrumentation {
966
1114
  disable() {
967
1115
  this.enabled = false;
968
1116
  }
969
- baggage(ctx) {
970
- if ('traceparent' in ctx) {
971
- ctx = otel.propagation.extract(otel__namespace.ROOT_CONTEXT, ctx);
1117
+ resume(ctx) {
1118
+ const parts = ctx.traceparent.split('-');
1119
+ if (parts.length !== 4) {
1120
+ return otel__namespace.ROOT_CONTEXT;
972
1121
  }
973
- const baggage = Object.fromEntries(otel.propagation.getBaggage(ctx)?.getAllEntries() || []);
974
- if (!(ATTR_LATITUDE_SEGMENT_ID in baggage) ||
975
- !(ATTR_LATITUDE_SEGMENTS in baggage)) {
976
- return undefined;
1122
+ const [, traceId, spanId, flags] = parts;
1123
+ if (!traceId || !spanId) {
1124
+ return otel__namespace.ROOT_CONTEXT;
977
1125
  }
978
- const segment = {
979
- id: baggage[ATTR_LATITUDE_SEGMENT_ID].value,
980
- parentId: baggage[ATTR_LATITUDE_SEGMENT_PARENT_ID]?.value,
1126
+ const spanContext = {
1127
+ traceId,
1128
+ spanId,
1129
+ traceFlags: parseInt(flags ?? '01', 16),
1130
+ isRemote: true,
981
1131
  };
982
- let segments = [];
983
- try {
984
- segments = JSON.parse(baggage[ATTR_LATITUDE_SEGMENTS].value);
985
- }
986
- catch (error) {
987
- return undefined;
988
- }
989
- if (segments.length < 1) {
990
- return undefined;
991
- }
992
- return { segment, segments };
993
- }
994
- setBaggage(ctx, baggage, extra) {
995
- let parent = Object.fromEntries(otel.propagation.getBaggage(ctx)?.getAllEntries() || []);
996
- parent = Object.fromEntries(Object.entries(parent).filter(([attribute]) => attribute !== ATTR_LATITUDE_SEGMENT_ID &&
997
- attribute !== ATTR_LATITUDE_SEGMENT_PARENT_ID &&
998
- attribute !== ATTR_LATITUDE_SEGMENTS));
999
- if (!baggage) {
1000
- const payload = otel.propagation.createBaggage({ ...parent, ...(extra || {}) });
1001
- return otel.propagation.setBaggage(ctx, payload);
1002
- }
1003
- let jsonSegments = '';
1004
- try {
1005
- jsonSegments = JSON.stringify(baggage.segments);
1006
- }
1007
- catch (error) {
1008
- jsonSegments = '[]';
1009
- }
1010
- const payload = otel.propagation.createBaggage({
1011
- ...parent,
1012
- [ATTR_LATITUDE_SEGMENT_ID]: { value: baggage.segment.id },
1013
- ...(baggage.segment.parentId && {
1014
- [ATTR_LATITUDE_SEGMENT_PARENT_ID]: { value: baggage.segment.parentId },
1015
- }),
1016
- [ATTR_LATITUDE_SEGMENTS]: { value: jsonSegments },
1017
- ...(extra || {}),
1018
- });
1019
- return otel.propagation.setBaggage(ctx, payload);
1020
- }
1021
- pause(ctx) {
1022
- const baggage = this.baggage(ctx);
1023
- if (baggage) {
1024
- baggage.segments.at(-1).paused = true;
1132
+ let context = otel.trace.setSpanContext(otel__namespace.ROOT_CONTEXT, spanContext);
1133
+ if (ctx.baggage) {
1134
+ const baggageEntries = {};
1135
+ for (const pair of ctx.baggage.split(',')) {
1136
+ const [key, value] = pair.split('=');
1137
+ if (key && value) {
1138
+ baggageEntries[key] = { value: decodeURIComponent(value) };
1139
+ }
1140
+ }
1141
+ const baggage = otel.propagation.createBaggage(baggageEntries);
1142
+ context = otel.propagation.setBaggage(context, baggage);
1025
1143
  }
1026
- ctx = this.setBaggage(ctx, baggage);
1027
- let carrier = {};
1028
- otel.propagation.inject(ctx, carrier);
1029
- return carrier;
1030
- }
1031
- resume(ctx) {
1032
- return otel.propagation.extract(otel__namespace.ROOT_CONTEXT, ctx);
1033
- }
1034
- restored(ctx) {
1035
- const baggage = this.baggage(ctx);
1036
- return !baggage?.segments.some((segment) => segment.paused);
1037
- }
1038
- restore(ctx) {
1039
- let baggage = this.baggage(ctx);
1040
- if (!baggage)
1041
- return ctx;
1042
- const segments = baggage.segments;
1043
- while (segments.at(-1)?.paused)
1044
- segments.pop();
1045
- const segment = segments.at(-1);
1046
- if (!segment)
1047
- return otel__namespace.ROOT_CONTEXT;
1048
- baggage = {
1049
- segment: { id: segment.id, parentId: segment.parentId },
1050
- segments: segments,
1051
- };
1052
- ctx = this.setBaggage(ctx, baggage);
1053
- let carrier = {};
1054
- otel.propagation.inject(ctx, carrier);
1055
- carrier.traceparent = segment.traceparent;
1056
- carrier.tracestate = segment.tracestate;
1057
- return this.resume(carrier);
1144
+ return context;
1058
1145
  }
1059
1146
  capitalize(str) {
1060
1147
  if (str.length === 0)
@@ -1361,7 +1448,7 @@ class ManualInstrumentation {
1361
1448
  completion(ctx, options) {
1362
1449
  const start = options;
1363
1450
  const configuration = {
1364
- ...start.configuration,
1451
+ ...(start.configuration ?? {}),
1365
1452
  model: start.model,
1366
1453
  };
1367
1454
  let jsonConfiguration = '';
@@ -1372,14 +1459,15 @@ class ManualInstrumentation {
1372
1459
  jsonConfiguration = '{}';
1373
1460
  }
1374
1461
  const attrConfiguration = this.attribifyConfiguration('input', configuration);
1462
+ const input = start.input ?? [];
1375
1463
  let jsonInput = '';
1376
1464
  try {
1377
- jsonInput = JSON.stringify(start.input);
1465
+ jsonInput = JSON.stringify(input);
1378
1466
  }
1379
1467
  catch (error) {
1380
1468
  jsonInput = '[]';
1381
1469
  }
1382
- const attrInput = this.attribifyMessages('input', start.input);
1470
+ const attrInput = this.attribifyMessages('input', input);
1383
1471
  const span = this.span(ctx, start.name || `${start.provider} / ${start.model}`, SpanType.Completion, {
1384
1472
  attributes: {
1385
1473
  [incubating.ATTR_GEN_AI_SYSTEM]: start.provider,
@@ -1388,34 +1476,45 @@ class ManualInstrumentation {
1388
1476
  [ATTR_GEN_AI_REQUEST_MESSAGES]: jsonInput,
1389
1477
  ...attrInput,
1390
1478
  ...(start.attributes || {}),
1479
+ [ATTR_LATITUDE_COMMIT_UUID]: start.versionUuid,
1480
+ [ATTR_LATITUDE_DOCUMENT_UUID]: start.promptUuid,
1481
+ [ATTR_LATITUDE_EXPERIMENT_UUID]: start.experimentUuid,
1391
1482
  },
1392
1483
  });
1393
1484
  return {
1394
1485
  context: span.context,
1395
1486
  end: (options) => {
1396
- const end = options;
1487
+ const end = options ?? {};
1488
+ const output = end.output ?? [];
1397
1489
  let jsonOutput = '';
1398
1490
  try {
1399
- jsonOutput = JSON.stringify(end.output);
1491
+ jsonOutput = JSON.stringify(output);
1400
1492
  }
1401
1493
  catch (error) {
1402
1494
  jsonOutput = '[]';
1403
1495
  }
1404
- const attrOutput = this.attribifyMessages('output', end.output);
1405
- const inputTokens = end.tokens.prompt + end.tokens.cached;
1406
- const outputTokens = end.tokens.reasoning + end.tokens.completion;
1496
+ const attrOutput = this.attribifyMessages('output', output);
1497
+ const tokens = {
1498
+ prompt: end.tokens?.prompt ?? 0,
1499
+ cached: end.tokens?.cached ?? 0,
1500
+ reasoning: end.tokens?.reasoning ?? 0,
1501
+ completion: end.tokens?.completion ?? 0,
1502
+ };
1503
+ const inputTokens = tokens.prompt + tokens.cached;
1504
+ const outputTokens = tokens.reasoning + tokens.completion;
1505
+ const finishReason = end.finishReason ?? '';
1407
1506
  span.end({
1408
1507
  attributes: {
1409
1508
  [ATTR_GEN_AI_RESPONSE_MESSAGES]: jsonOutput,
1410
1509
  ...attrOutput,
1411
1510
  [incubating.ATTR_GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
1412
- [ATTR_GEN_AI_USAGE_PROMPT_TOKENS]: end.tokens.prompt,
1413
- [ATTR_GEN_AI_USAGE_CACHED_TOKENS]: end.tokens.cached,
1414
- [ATTR_GEN_AI_USAGE_REASONING_TOKENS]: end.tokens.reasoning,
1415
- [ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]: end.tokens.completion,
1511
+ [ATTR_GEN_AI_USAGE_PROMPT_TOKENS]: tokens.prompt,
1512
+ [ATTR_GEN_AI_USAGE_CACHED_TOKENS]: tokens.cached,
1513
+ [ATTR_GEN_AI_USAGE_REASONING_TOKENS]: tokens.reasoning,
1514
+ [ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]: tokens.completion,
1416
1515
  [incubating.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
1417
1516
  [incubating.ATTR_GEN_AI_RESPONSE_MODEL]: start.model,
1418
- [incubating.ATTR_GEN_AI_RESPONSE_FINISH_REASONS]: [end.finishReason],
1517
+ [incubating.ATTR_GEN_AI_RESPONSE_FINISH_REASONS]: [finishReason],
1419
1518
  ...(end.attributes || {}),
1420
1519
  },
1421
1520
  });
@@ -1502,49 +1601,7 @@ class ManualInstrumentation {
1502
1601
  fail: span.fail,
1503
1602
  };
1504
1603
  }
1505
- segment(ctx, type, data, options) {
1506
- options = options || {};
1507
- let baggage = this.baggage(ctx);
1508
- const parent = baggage?.segments.at(-1);
1509
- const segments = baggage?.segments || [];
1510
- segments.push({
1511
- ...{
1512
- id: options._internal?.id || uuid.v4(),
1513
- ...(parent?.id && { parentId: parent.id }),
1514
- source: options._internal?.source || parent?.source || this.source,
1515
- type: type,
1516
- data: data,
1517
- },
1518
- traceparent: 'undefined',
1519
- tracestate: undefined,
1520
- });
1521
- const segment = segments.at(-1);
1522
- baggage = {
1523
- segment: { id: segment.id, parentId: segment.parentId },
1524
- segments: segments,
1525
- };
1526
- ctx = this.setBaggage(ctx, baggage, options.baggage);
1527
- // Dummy wrapper to force the same trace and carry on some segment attributes
1528
- const span = this.span(ctx, SEGMENT_SPECIFICATIONS[type].name, SpanType.Segment, { attributes: options.attributes });
1529
- let carrier = {};
1530
- otel.propagation.inject(span.context, carrier);
1531
- baggage.segments.at(-1).traceparent = carrier.traceparent;
1532
- baggage.segments.at(-1).tracestate = carrier.tracestate;
1533
- // Fix current segment span segments attribute now that we know the trace
1534
- otel.trace
1535
- .getSpan(span.context)
1536
- .setAttribute(ATTR_LATITUDE_SEGMENTS, JSON.stringify(baggage.segments));
1537
- ctx = this.setBaggage(span.context, baggage, options.baggage);
1538
- return { context: ctx, end: span.end, fail: span.fail };
1539
- }
1540
- prompt(ctx, { logUuid, versionUuid, promptUuid, experimentUuid, externalId, template, parameters, ...rest }) {
1541
- const baggage = {
1542
- ...(logUuid && { logUuid }), // TODO(tracing): temporal related log, remove when observability is ready
1543
- commitUuid: versionUuid || HEAD_COMMIT,
1544
- documentUuid: promptUuid,
1545
- ...(experimentUuid && { experimentUuid }),
1546
- ...(externalId && { externalId }),
1547
- };
1604
+ prompt(ctx, { documentLogUuid, versionUuid, promptUuid, projectId, experimentUuid, testDeploymentId, externalId, template, parameters, name, source, ...rest }) {
1548
1605
  let jsonParameters = '';
1549
1606
  try {
1550
1607
  jsonParameters = JSON.stringify(parameters || {});
@@ -1555,34 +1612,79 @@ class ManualInstrumentation {
1555
1612
  const attributes = {
1556
1613
  [ATTR_GEN_AI_REQUEST_TEMPLATE]: template,
1557
1614
  [ATTR_GEN_AI_REQUEST_PARAMETERS]: jsonParameters,
1615
+ [ATTR_LATITUDE_COMMIT_UUID]: versionUuid || HEAD_COMMIT,
1616
+ [ATTR_LATITUDE_DOCUMENT_UUID]: promptUuid,
1617
+ [ATTR_LATITUDE_PROJECT_ID]: projectId,
1618
+ [ATTR_LATITUDE_DOCUMENT_LOG_UUID]: documentLogUuid,
1619
+ ...(experimentUuid && {
1620
+ [ATTR_LATITUDE_EXPERIMENT_UUID]: experimentUuid,
1621
+ }),
1622
+ ...(testDeploymentId && {
1623
+ [ATTR_LATITUDE_TEST_DEPLOYMENT_ID]: testDeploymentId,
1624
+ }),
1625
+ ...(externalId && { [ATTR_LATITUDE_EXTERNAL_ID]: externalId }),
1626
+ ...(source && { [ATTR_LATITUDE_SOURCE]: source }),
1558
1627
  ...(rest.attributes || {}),
1559
1628
  };
1560
- return this.segment(ctx, SegmentType.Document, baggage, {
1561
- ...rest,
1629
+ return this.span(ctx, name || `prompt-${promptUuid}`, SpanType.Prompt, {
1562
1630
  attributes,
1563
1631
  });
1564
1632
  }
1565
1633
  step(ctx, options) {
1566
- return this.segment(ctx, SegmentType.Step, undefined, options);
1634
+ return this.span(ctx, 'step', SpanType.Step, options);
1635
+ }
1636
+ chat(ctx, { documentLogUuid, previousTraceId, source, name, ...rest }) {
1637
+ const attributes = {
1638
+ [ATTR_LATITUDE_DOCUMENT_LOG_UUID]: documentLogUuid,
1639
+ [ATTR_LATITUDE_PREVIOUS_TRACE_ID]: previousTraceId,
1640
+ ...(source && { [ATTR_LATITUDE_SOURCE]: source }),
1641
+ ...(rest.attributes || {}),
1642
+ };
1643
+ return this.span(ctx, name || 'chat', SpanType.Chat, { attributes });
1644
+ }
1645
+ external(ctx, { promptUuid, documentLogUuid, source, versionUuid, externalId, name, ...rest }) {
1646
+ const attributes = {
1647
+ [ATTR_LATITUDE_DOCUMENT_UUID]: promptUuid,
1648
+ [ATTR_LATITUDE_DOCUMENT_LOG_UUID]: documentLogUuid,
1649
+ [ATTR_LATITUDE_SOURCE]: source ?? LogSources.API,
1650
+ ...(versionUuid && { [ATTR_LATITUDE_COMMIT_UUID]: versionUuid }),
1651
+ ...(externalId && { [ATTR_LATITUDE_EXTERNAL_ID]: externalId }),
1652
+ ...(rest.attributes || {}),
1653
+ };
1654
+ return this.span(ctx, name || `external-${promptUuid}`, SpanType.External, {
1655
+ attributes,
1656
+ });
1657
+ }
1658
+ unresolvedExternal(ctx, { path, projectId, versionUuid, conversationUuid, name, ...rest }) {
1659
+ const attributes = {
1660
+ [ATTR_LATITUDE_PROMPT_PATH]: path,
1661
+ [ATTR_LATITUDE_PROJECT_ID]: projectId,
1662
+ ...(versionUuid && { [ATTR_LATITUDE_COMMIT_UUID]: versionUuid }),
1663
+ ...(conversationUuid && {
1664
+ [ATTR_LATITUDE_DOCUMENT_LOG_UUID]: conversationUuid,
1665
+ }),
1666
+ ...(rest.attributes || {}),
1667
+ };
1668
+ return this.span(ctx, name || `capture-${path}`, SpanType.UnresolvedExternal, { attributes });
1567
1669
  }
1568
1670
  }
1569
1671
 
1570
1672
  class LatitudeInstrumentation {
1571
1673
  options;
1572
- telemetry;
1573
- constructor(source, tracer, options) {
1574
- this.telemetry = new ManualInstrumentation(source, tracer);
1674
+ manualTelemetry;
1675
+ constructor(tracer, options) {
1676
+ this.manualTelemetry = new ManualInstrumentation(tracer);
1575
1677
  this.options = options;
1576
1678
  }
1577
1679
  isEnabled() {
1578
- return this.telemetry.isEnabled();
1680
+ return this.manualTelemetry.isEnabled();
1579
1681
  }
1580
1682
  enable() {
1581
1683
  this.options.module.instrument(this);
1582
- this.telemetry.enable();
1684
+ this.manualTelemetry.enable();
1583
1685
  }
1584
1686
  disable() {
1585
- this.telemetry.disable();
1687
+ this.manualTelemetry.disable();
1586
1688
  this.options.module.uninstrument();
1587
1689
  }
1588
1690
  countTokens(messages) {
@@ -1606,7 +1708,8 @@ class LatitudeInstrumentation {
1606
1708
  }
1607
1709
  async wrapRenderChain(fn, ...args) {
1608
1710
  const { prompt, parameters } = args[0];
1609
- const $prompt = this.telemetry.prompt(otel.context.active(), {
1711
+ const $prompt = this.manualTelemetry.prompt(otel.context.active(), {
1712
+ documentLogUuid: uuid.v4(),
1610
1713
  versionUuid: prompt.versionUuid,
1611
1714
  promptUuid: prompt.uuid,
1612
1715
  template: prompt.content,
@@ -1624,7 +1727,7 @@ class LatitudeInstrumentation {
1624
1727
  return result;
1625
1728
  }
1626
1729
  async wrapRenderStep(fn, ...args) {
1627
- const $step = this.telemetry.step(otel.context.active());
1730
+ const $step = this.manualTelemetry.step(otel.context.active());
1628
1731
  let result;
1629
1732
  try {
1630
1733
  result = await otel.context.with($step.context, async () => await fn(...args));
@@ -1642,7 +1745,7 @@ class LatitudeInstrumentation {
1642
1745
  }
1643
1746
  const { provider, config, messages } = args[0];
1644
1747
  const model = config.model || 'unknown';
1645
- const $completion = this.telemetry.completion(otel.context.active(), {
1748
+ const $completion = this.manualTelemetry.completion(otel.context.active(), {
1646
1749
  name: `${provider} / ${model}`,
1647
1750
  provider: provider,
1648
1751
  model: model,
@@ -1676,7 +1779,7 @@ class LatitudeInstrumentation {
1676
1779
  }
1677
1780
  async wrapRenderTool(fn, ...args) {
1678
1781
  const { toolRequest } = args[0];
1679
- const $tool = this.telemetry.tool(otel.context.active(), {
1782
+ const $tool = this.manualTelemetry.tool(otel.context.active(), {
1680
1783
  name: toolRequest.toolName,
1681
1784
  call: {
1682
1785
  id: toolRequest.toolCallId,
@@ -1701,10 +1804,165 @@ class LatitudeInstrumentation {
1701
1804
  }
1702
1805
  }
1703
1806
 
1807
+ var LatitudeErrorCodes;
1808
+ (function (LatitudeErrorCodes) {
1809
+ LatitudeErrorCodes["UnexpectedError"] = "UnexpectedError";
1810
+ LatitudeErrorCodes["OverloadedError"] = "OverloadedError";
1811
+ LatitudeErrorCodes["RateLimitError"] = "RateLimitError";
1812
+ LatitudeErrorCodes["UnauthorizedError"] = "UnauthorizedError";
1813
+ LatitudeErrorCodes["ForbiddenError"] = "ForbiddenError";
1814
+ LatitudeErrorCodes["BadRequestError"] = "BadRequestError";
1815
+ LatitudeErrorCodes["NotFoundError"] = "NotFoundError";
1816
+ LatitudeErrorCodes["ConflictError"] = "ConflictError";
1817
+ LatitudeErrorCodes["UnprocessableEntityError"] = "UnprocessableEntityError";
1818
+ LatitudeErrorCodes["NotImplementedError"] = "NotImplementedError";
1819
+ LatitudeErrorCodes["PaymentRequiredError"] = "PaymentRequiredError";
1820
+ LatitudeErrorCodes["AbortedError"] = "AbortedError";
1821
+ })(LatitudeErrorCodes || (LatitudeErrorCodes = {}));
1822
+ // NOTE: If you add a new error code, please add it to the pg enum in models/runErrors.ts
1823
+ var RunErrorCodes;
1824
+ (function (RunErrorCodes) {
1825
+ RunErrorCodes["AIProviderConfigError"] = "ai_provider_config_error";
1826
+ RunErrorCodes["AIRunError"] = "ai_run_error";
1827
+ RunErrorCodes["ChainCompileError"] = "chain_compile_error";
1828
+ RunErrorCodes["DefaultProviderExceededQuota"] = "default_provider_exceeded_quota_error";
1829
+ RunErrorCodes["DefaultProviderInvalidModel"] = "default_provider_invalid_model_error";
1830
+ RunErrorCodes["DocumentConfigError"] = "document_config_error";
1831
+ RunErrorCodes["ErrorGeneratingMockToolResult"] = "error_generating_mock_tool_result";
1832
+ RunErrorCodes["FailedToWakeUpIntegrationError"] = "failed_to_wake_up_integration_error";
1833
+ RunErrorCodes["InvalidResponseFormatError"] = "invalid_response_format_error";
1834
+ RunErrorCodes["MaxStepCountExceededError"] = "max_step_count_exceeded_error";
1835
+ RunErrorCodes["MissingProvider"] = "missing_provider_error";
1836
+ RunErrorCodes["RateLimit"] = "rate_limit_error";
1837
+ RunErrorCodes["Unknown"] = "unknown_error";
1838
+ RunErrorCodes["UnsupportedProviderResponseTypeError"] = "unsupported_provider_response_type_error";
1839
+ RunErrorCodes["PaymentRequiredError"] = "payment_required_error";
1840
+ RunErrorCodes["AbortError"] = "abort_error";
1841
+ // DEPRECATED, but do not delete
1842
+ RunErrorCodes["EvaluationRunMissingProviderLogError"] = "ev_run_missing_provider_log_error";
1843
+ RunErrorCodes["EvaluationRunMissingWorkspaceError"] = "ev_run_missing_workspace_error";
1844
+ RunErrorCodes["EvaluationRunResponseJsonFormatError"] = "ev_run_response_json_format_error";
1845
+ RunErrorCodes["EvaluationRunUnsupportedResultTypeError"] = "ev_run_unsupported_result_type_error";
1846
+ })(RunErrorCodes || (RunErrorCodes = {}));
1847
+ var ApiErrorCodes;
1848
+ (function (ApiErrorCodes) {
1849
+ ApiErrorCodes["HTTPException"] = "http_exception";
1850
+ ApiErrorCodes["InternalServerError"] = "internal_server_error";
1851
+ })(ApiErrorCodes || (ApiErrorCodes = {}));
1852
+
1853
+ class LatitudeError extends Error {
1854
+ statusCode = 500;
1855
+ name = LatitudeErrorCodes.UnexpectedError;
1856
+ headers = {};
1857
+ details;
1858
+ constructor(message, details, status, name) {
1859
+ super(message);
1860
+ this.details = details ?? {};
1861
+ this.statusCode = status ?? this.statusCode;
1862
+ this.name = name ?? this.constructor.name;
1863
+ }
1864
+ serialize() {
1865
+ return {
1866
+ name: this.name,
1867
+ code: this.name,
1868
+ status: this.statusCode,
1869
+ message: this.message,
1870
+ details: this.details,
1871
+ };
1872
+ }
1873
+ static deserialize(json) {
1874
+ return new LatitudeError(json.message, json.details, json.status, json.name);
1875
+ }
1876
+ }
1877
+ class BadRequestError extends LatitudeError {
1878
+ statusCode = 400;
1879
+ name = LatitudeErrorCodes.BadRequestError;
1880
+ }
1881
+
1704
1882
  const TRACES_URL = `${env.GATEWAY_BASE_URL}/api/v3/traces`;
1705
1883
  const SERVICE_NAME = process.env.npm_package_name || 'unknown';
1706
1884
  const SCOPE_VERSION = process.env.npm_package_version || 'unknown';
1707
1885
  const BACKGROUND = () => otel__namespace.ROOT_CONTEXT;
1886
+ class SpanFactory {
1887
+ telemetry;
1888
+ constructor(telemetry) {
1889
+ this.telemetry = telemetry;
1890
+ }
1891
+ tool(options, ctx) {
1892
+ return this.telemetry.tool(ctx ?? otel.context.active(), options);
1893
+ }
1894
+ completion(options, ctx) {
1895
+ return this.telemetry.completion(ctx ?? otel.context.active(), options);
1896
+ }
1897
+ embedding(options, ctx) {
1898
+ return this.telemetry.embedding(ctx ?? otel.context.active(), options);
1899
+ }
1900
+ retrieval(options, ctx) {
1901
+ return this.telemetry.retrieval(ctx ?? otel.context.active(), options);
1902
+ }
1903
+ reranking(options, ctx) {
1904
+ return this.telemetry.reranking(ctx ?? otel.context.active(), options);
1905
+ }
1906
+ http(options, ctx) {
1907
+ return this.telemetry.http(ctx ?? otel.context.active(), options);
1908
+ }
1909
+ prompt(options, ctx) {
1910
+ return this.telemetry.prompt(ctx ?? otel.context.active(), options);
1911
+ }
1912
+ step(options, ctx) {
1913
+ return this.telemetry.step(ctx ?? otel.context.active(), options);
1914
+ }
1915
+ chat(options, ctx) {
1916
+ return this.telemetry.chat(ctx ?? otel.context.active(), options);
1917
+ }
1918
+ external(options, ctx) {
1919
+ return this.telemetry.external(ctx ?? otel.context.active(), options);
1920
+ }
1921
+ }
1922
+ class ContextManager {
1923
+ telemetry;
1924
+ constructor(telemetry) {
1925
+ this.telemetry = telemetry;
1926
+ }
1927
+ resume(ctx) {
1928
+ return this.telemetry.resume(ctx);
1929
+ }
1930
+ active() {
1931
+ return otel.context.active();
1932
+ }
1933
+ }
1934
+ class InstrumentationManager {
1935
+ instrumentations;
1936
+ constructor(instrumentations) {
1937
+ this.instrumentations = instrumentations;
1938
+ }
1939
+ enable() {
1940
+ this.instrumentations.forEach((instrumentation) => {
1941
+ if (!instrumentation.isEnabled())
1942
+ instrumentation.enable();
1943
+ });
1944
+ }
1945
+ disable() {
1946
+ this.instrumentations.forEach((instrumentation) => {
1947
+ if (instrumentation.isEnabled())
1948
+ instrumentation.disable();
1949
+ });
1950
+ }
1951
+ }
1952
+ class TracerManager {
1953
+ nodeProvider;
1954
+ scopeVersion;
1955
+ constructor(nodeProvider, scopeVersion) {
1956
+ this.nodeProvider = nodeProvider;
1957
+ this.scopeVersion = scopeVersion;
1958
+ }
1959
+ get(scope) {
1960
+ return this.provider(scope).getTracer('');
1961
+ }
1962
+ provider(scope) {
1963
+ return new ScopedTracerProvider(`${SCOPE_LATITUDE}.${scope}`, this.scopeVersion, this.nodeProvider);
1964
+ }
1965
+ }
1708
1966
  class ScopedTracerProvider {
1709
1967
  scope;
1710
1968
  version;
@@ -1729,24 +1987,26 @@ const DEFAULT_SPAN_EXPORTER = (apiKey) => new exporterTraceOtlpHttp.OTLPTraceExp
1729
1987
  // Note: Only exporting typescript instrumentations
1730
1988
  exports.Instrumentation = void 0;
1731
1989
  (function (Instrumentation) {
1732
- Instrumentation["Latitude"] = "latitude";
1733
- Instrumentation["OpenAI"] = "openai";
1734
1990
  Instrumentation["Anthropic"] = "anthropic";
1735
- Instrumentation["AzureOpenAI"] = "azure";
1736
- Instrumentation["VercelAI"] = "vercelai";
1737
- Instrumentation["VertexAI"] = "vertexai";
1738
1991
  Instrumentation["AIPlatform"] = "aiplatform";
1739
1992
  Instrumentation["Bedrock"] = "bedrock";
1740
- Instrumentation["TogetherAI"] = "togetherai";
1741
1993
  Instrumentation["Cohere"] = "cohere";
1742
1994
  Instrumentation["Langchain"] = "langchain";
1995
+ Instrumentation["Latitude"] = "latitude";
1743
1996
  Instrumentation["LlamaIndex"] = "llamaindex";
1997
+ Instrumentation["OpenAI"] = "openai";
1998
+ Instrumentation["TogetherAI"] = "togetherai";
1999
+ Instrumentation["VertexAI"] = "vertexai";
1744
2000
  })(exports.Instrumentation || (exports.Instrumentation = {}));
1745
2001
  class LatitudeTelemetry {
1746
2002
  options;
1747
- provider;
1748
- telemetry;
1749
- instrumentations;
2003
+ nodeProvider;
2004
+ manualInstrumentation;
2005
+ instrumentationsList;
2006
+ span;
2007
+ context;
2008
+ instrumentation;
2009
+ tracer;
1750
2010
  constructor(apiKey, options) {
1751
2011
  this.options = options || {};
1752
2012
  if (!this.options.exporter) {
@@ -1760,234 +2020,100 @@ class LatitudeTelemetry {
1760
2020
  new core.W3CBaggagePropagator(),
1761
2021
  ],
1762
2022
  }));
1763
- this.provider = new sdkTraceNode.NodeTracerProvider({
2023
+ this.nodeProvider = new sdkTraceNode.NodeTracerProvider({
1764
2024
  resource: new resources.Resource({ [semanticConventions.ATTR_SERVICE_NAME]: SERVICE_NAME }),
1765
2025
  });
1766
2026
  // Note: important, must run before the exporter span processors
1767
- this.provider.addSpanProcessor(new baggageSpanProcessor.BaggageSpanProcessor(baggageSpanProcessor.ALLOW_ALL_BAGGAGE_KEYS));
2027
+ this.nodeProvider.addSpanProcessor(new baggageSpanProcessor.BaggageSpanProcessor(baggageSpanProcessor.ALLOW_ALL_BAGGAGE_KEYS));
1768
2028
  if (this.options.processors) {
1769
2029
  this.options.processors.forEach((processor) => {
1770
- this.provider.addSpanProcessor(processor);
2030
+ this.nodeProvider.addSpanProcessor(processor);
1771
2031
  });
1772
2032
  }
1773
2033
  else {
1774
- this.provider.addSpanProcessor(DEFAULT_REDACT_SPAN_PROCESSOR());
2034
+ this.nodeProvider.addSpanProcessor(DEFAULT_REDACT_SPAN_PROCESSOR());
1775
2035
  }
1776
2036
  if (this.options.disableBatch) {
1777
- this.provider.addSpanProcessor(new sdkTraceNode.SimpleSpanProcessor(this.options.exporter));
2037
+ this.nodeProvider.addSpanProcessor(new sdkTraceNode.SimpleSpanProcessor(this.options.exporter));
1778
2038
  }
1779
2039
  else {
1780
- this.provider.addSpanProcessor(new sdkTraceNode.BatchSpanProcessor(this.options.exporter));
2040
+ this.nodeProvider.addSpanProcessor(new sdkTraceNode.BatchSpanProcessor(this.options.exporter));
1781
2041
  }
1782
- this.provider.register();
2042
+ this.nodeProvider.register();
1783
2043
  process.on('SIGTERM', async () => this.shutdown);
1784
2044
  process.on('SIGINT', async () => this.shutdown);
1785
- this.telemetry = null;
1786
- this.instrumentations = [];
2045
+ this.manualInstrumentation = null;
2046
+ this.instrumentationsList = [];
2047
+ this.tracer = new TracerManager(this.nodeProvider, SCOPE_VERSION);
1787
2048
  this.initInstrumentations();
1788
- this.instrument();
2049
+ this.instrumentation = new InstrumentationManager(this.instrumentationsList);
2050
+ this.instrumentation.enable();
2051
+ this.span = new SpanFactory(this.manualInstrumentation);
2052
+ this.context = new ContextManager(this.manualInstrumentation);
1789
2053
  }
1790
2054
  async flush() {
1791
- await this.provider.forceFlush();
2055
+ await this.nodeProvider.forceFlush();
1792
2056
  await this.options.exporter.forceFlush?.();
1793
2057
  }
1794
2058
  async shutdown() {
1795
2059
  await this.flush();
1796
- await this.provider.shutdown();
2060
+ await this.nodeProvider.shutdown();
1797
2061
  await this.options.exporter.shutdown?.();
1798
2062
  }
1799
- tracerProvider(instrumentation) {
1800
- return new ScopedTracerProvider(`${SCOPE_LATITUDE}.${instrumentation}`, SCOPE_VERSION, this.provider);
1801
- }
1802
- tracer(instrumentation) {
1803
- return this.tracerProvider(instrumentation).getTracer('');
1804
- }
1805
2063
  // TODO(tracing): auto instrument outgoing HTTP requests
1806
2064
  initInstrumentations() {
1807
- this.instrumentations = [];
1808
- const tracer = this.tracer(InstrumentationScope.Manual);
1809
- this.telemetry = new ManualInstrumentation(SegmentSource.API, tracer);
1810
- this.instrumentations.push(this.telemetry);
2065
+ this.instrumentationsList = [];
2066
+ const tracer = this.tracer.get(InstrumentationScope.Manual);
2067
+ this.manualInstrumentation = new ManualInstrumentation(tracer);
2068
+ this.instrumentationsList.push(this.manualInstrumentation);
1811
2069
  const latitude = this.options.instrumentations?.latitude;
1812
2070
  if (latitude) {
1813
- const tracer = this.tracer(exports.Instrumentation.Latitude);
1814
- const instrumentation = new LatitudeInstrumentation(SegmentSource.API, tracer, typeof latitude === 'object' ? latitude : { module: latitude });
1815
- this.instrumentations.push(instrumentation);
1816
- }
1817
- const openai = this.options.instrumentations?.openai;
1818
- if (openai) {
1819
- const provider = this.tracerProvider(exports.Instrumentation.OpenAI);
1820
- const instrumentation$1 = new instrumentationOpenai.OpenAIInstrumentation({ enrichTokens: true });
1821
- instrumentation$1.setTracerProvider(provider);
1822
- instrumentation$1.manuallyInstrument(openai);
1823
- instrumentation.registerInstrumentations({
1824
- instrumentations: [instrumentation$1],
1825
- tracerProvider: provider,
1826
- });
1827
- this.instrumentations.push(instrumentation$1);
1828
- }
1829
- const anthropic = this.options.instrumentations?.anthropic;
1830
- if (anthropic) {
1831
- const provider = this.tracerProvider(exports.Instrumentation.Anthropic);
1832
- const instrumentation$1 = new instrumentationAnthropic.AnthropicInstrumentation();
1833
- instrumentation$1.setTracerProvider(provider);
1834
- instrumentation$1.manuallyInstrument(anthropic);
1835
- instrumentation.registerInstrumentations({
1836
- instrumentations: [instrumentation$1],
1837
- tracerProvider: provider,
1838
- });
1839
- this.instrumentations.push(instrumentation$1);
1840
- }
1841
- const azure = this.options.instrumentations?.azure;
1842
- if (azure) {
1843
- const provider = this.tracerProvider(exports.Instrumentation.AzureOpenAI);
1844
- const instrumentation$1 = new instrumentationAzure.AzureOpenAIInstrumentation();
1845
- instrumentation$1.setTracerProvider(provider);
1846
- instrumentation$1.manuallyInstrument(azure);
1847
- instrumentation.registerInstrumentations({
1848
- instrumentations: [instrumentation$1],
1849
- tracerProvider: provider,
1850
- });
1851
- this.instrumentations.push(instrumentation$1);
1852
- }
1853
- const vertexai = this.options.instrumentations?.vertexai;
1854
- if (vertexai) {
1855
- const provider = this.tracerProvider(exports.Instrumentation.VertexAI);
1856
- const instrumentation$1 = new instrumentationVertexai.VertexAIInstrumentation();
1857
- instrumentation$1.setTracerProvider(provider);
1858
- instrumentation$1.manuallyInstrument(vertexai);
1859
- instrumentation.registerInstrumentations({
1860
- instrumentations: [instrumentation$1],
1861
- tracerProvider: provider,
1862
- });
1863
- this.instrumentations.push(instrumentation$1);
1864
- }
1865
- const aiplatform = this.options.instrumentations?.aiplatform;
1866
- if (aiplatform) {
1867
- const provider = this.tracerProvider(exports.Instrumentation.AIPlatform);
1868
- const instrumentation$1 = new instrumentationVertexai.AIPlatformInstrumentation();
1869
- instrumentation$1.setTracerProvider(provider);
1870
- instrumentation$1.manuallyInstrument(aiplatform);
1871
- instrumentation.registerInstrumentations({
1872
- instrumentations: [instrumentation$1],
1873
- tracerProvider: provider,
1874
- });
1875
- this.instrumentations.push(instrumentation$1);
1876
- }
1877
- const bedrock = this.options.instrumentations?.bedrock;
1878
- if (bedrock) {
1879
- const provider = this.tracerProvider(exports.Instrumentation.Bedrock);
1880
- const instrumentation$1 = new instrumentationBedrock.BedrockInstrumentation();
1881
- instrumentation$1.setTracerProvider(provider);
1882
- instrumentation$1.manuallyInstrument(bedrock);
1883
- instrumentation.registerInstrumentations({
1884
- instrumentations: [instrumentation$1],
1885
- tracerProvider: provider,
1886
- });
1887
- this.instrumentations.push(instrumentation$1);
2071
+ const tracer = this.tracer.get(exports.Instrumentation.Latitude);
2072
+ const instrumentation = new LatitudeInstrumentation(tracer, typeof latitude === 'object' ? latitude : { module: latitude });
2073
+ this.instrumentationsList.push(instrumentation);
1888
2074
  }
1889
- const togetherai = this.options.instrumentations?.togetherai;
1890
- if (togetherai) {
1891
- const provider = this.tracerProvider(exports.Instrumentation.TogetherAI);
1892
- const instrumentation$1 = new instrumentationTogether.TogetherInstrumentation({
1893
- enrichTokens: true,
1894
- });
2075
+ const configureInstrumentation = (instrumentationType, InstrumentationConstructor, instrumentationOptions) => {
2076
+ const providerPkg = this.options.instrumentations?.[instrumentationType];
2077
+ const provider = this.tracer.provider(instrumentationType);
2078
+ const instrumentation$1 = new InstrumentationConstructor(instrumentationOptions); // prettier-ignore
1895
2079
  instrumentation$1.setTracerProvider(provider);
1896
- instrumentation$1.manuallyInstrument(togetherai);
2080
+ if (providerPkg) {
2081
+ instrumentation$1.manuallyInstrument(providerPkg);
2082
+ }
1897
2083
  instrumentation.registerInstrumentations({
1898
2084
  instrumentations: [instrumentation$1],
1899
2085
  tracerProvider: provider,
1900
2086
  });
1901
- this.instrumentations.push(instrumentation$1);
2087
+ this.instrumentationsList.push(instrumentation$1);
2088
+ };
2089
+ configureInstrumentation(exports.Instrumentation.Anthropic, instrumentationAnthropic.AnthropicInstrumentation); // prettier-ignore
2090
+ configureInstrumentation(exports.Instrumentation.AIPlatform, instrumentationVertexai.AIPlatformInstrumentation); // prettier-ignore
2091
+ configureInstrumentation(exports.Instrumentation.Bedrock, instrumentationBedrock.BedrockInstrumentation); // prettier-ignore
2092
+ configureInstrumentation(exports.Instrumentation.Cohere, instrumentationCohere.CohereInstrumentation); // prettier-ignore
2093
+ configureInstrumentation(exports.Instrumentation.Langchain, instrumentationLangchain.LangChainInstrumentation); // prettier-ignore
2094
+ configureInstrumentation(exports.Instrumentation.LlamaIndex, instrumentationLlamaindex.LlamaIndexInstrumentation); // prettier-ignore
2095
+ configureInstrumentation(exports.Instrumentation.OpenAI, instrumentationOpenai.OpenAIInstrumentation, { enrichTokens: true }); // prettier-ignore
2096
+ configureInstrumentation(exports.Instrumentation.TogetherAI, instrumentationTogether.TogetherInstrumentation, { enrichTokens: true }); // prettier-ignore
2097
+ configureInstrumentation(exports.Instrumentation.VertexAI, instrumentationVertexai.VertexAIInstrumentation); // prettier-ignore
2098
+ }
2099
+ async capture(options, fn) {
2100
+ if (!DOCUMENT_PATH_REGEXP.test(options.path)) {
2101
+ throw new BadRequestError("Invalid path, no spaces. Only letters, numbers, '.', '-' and '_'");
1902
2102
  }
1903
- const cohere = this.options.instrumentations?.cohere;
1904
- if (cohere) {
1905
- const provider = this.tracerProvider(exports.Instrumentation.Cohere);
1906
- const instrumentation$1 = new instrumentationCohere.CohereInstrumentation();
1907
- instrumentation$1.setTracerProvider(provider);
1908
- instrumentation$1.manuallyInstrument(cohere);
1909
- instrumentation.registerInstrumentations({
1910
- instrumentations: [instrumentation$1],
1911
- tracerProvider: provider,
1912
- });
1913
- this.instrumentations.push(instrumentation$1);
2103
+ const span = this.manualInstrumentation.unresolvedExternal(otel__namespace.ROOT_CONTEXT, options);
2104
+ try {
2105
+ const result = await otel.context.with(span.context, () => fn(span.context));
2106
+ span.end();
2107
+ return result;
1914
2108
  }
1915
- const langchain = this.options.instrumentations?.langchain;
1916
- if (langchain) {
1917
- const provider = this.tracerProvider(exports.Instrumentation.Langchain);
1918
- const instrumentation$1 = new instrumentationLangchain.LangChainInstrumentation();
1919
- instrumentation$1.setTracerProvider(provider);
1920
- instrumentation$1.manuallyInstrument(langchain);
1921
- instrumentation.registerInstrumentations({
1922
- instrumentations: [instrumentation$1],
1923
- tracerProvider: provider,
1924
- });
1925
- this.instrumentations.push(instrumentation$1);
2109
+ catch (error) {
2110
+ span.fail(error);
2111
+ throw error;
1926
2112
  }
1927
- const llamaindex = this.options.instrumentations?.llamaindex;
1928
- if (llamaindex) {
1929
- const provider = this.tracerProvider(exports.Instrumentation.LlamaIndex);
1930
- const instrumentation$1 = new instrumentationLlamaindex.LlamaIndexInstrumentation();
1931
- instrumentation$1.setTracerProvider(provider);
1932
- instrumentation$1.manuallyInstrument(llamaindex);
1933
- instrumentation.registerInstrumentations({
1934
- instrumentations: [instrumentation$1],
1935
- tracerProvider: provider,
1936
- });
1937
- this.instrumentations.push(instrumentation$1);
2113
+ finally {
2114
+ await this.flush();
1938
2115
  }
1939
2116
  }
1940
- instrument() {
1941
- this.instrumentations.forEach((instrumentation) => {
1942
- if (!instrumentation.isEnabled())
1943
- instrumentation.enable();
1944
- });
1945
- }
1946
- uninstrument() {
1947
- this.instrumentations.forEach((instrumentation) => {
1948
- if (instrumentation.isEnabled())
1949
- instrumentation.disable();
1950
- });
1951
- }
1952
- baggage(ctx) {
1953
- return this.telemetry.baggage(ctx);
1954
- }
1955
- pause(ctx) {
1956
- return this.telemetry.pause(ctx);
1957
- }
1958
- resume(ctx) {
1959
- return this.telemetry.resume(ctx);
1960
- }
1961
- restored(ctx) {
1962
- return this.telemetry.restored(ctx);
1963
- }
1964
- restore(ctx) {
1965
- return this.telemetry.restore(ctx);
1966
- }
1967
- tool(ctx, options) {
1968
- return this.telemetry.tool(ctx, options);
1969
- }
1970
- completion(ctx, options) {
1971
- return this.telemetry.completion(ctx, options);
1972
- }
1973
- embedding(ctx, options) {
1974
- return this.telemetry.embedding(ctx, options);
1975
- }
1976
- retrieval(ctx, options) {
1977
- return this.telemetry.retrieval(ctx, options);
1978
- }
1979
- reranking(ctx, options) {
1980
- return this.telemetry.reranking(ctx, options);
1981
- }
1982
- http(ctx, options) {
1983
- return this.telemetry.http(ctx, options);
1984
- }
1985
- prompt(ctx, options) {
1986
- return this.telemetry.prompt(ctx, options);
1987
- }
1988
- step(ctx, options) {
1989
- return this.telemetry.step(ctx, options);
1990
- }
1991
2117
  }
1992
2118
 
1993
2119
  exports.BACKGROUND = BACKGROUND;