@latitude-data/telemetry 2.0.4 → 3.0.0-alpha.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
@@ -1,2113 +1,433 @@
1
- 'use strict';
2
-
3
- var zod = require('zod');
4
- var incubating = require('@opentelemetry/semantic-conventions/incubating');
5
- var semanticConventions = require('@opentelemetry/semantic-conventions');
6
- var otel = require('@opentelemetry/api');
7
- var rosettaAi = require('rosetta-ai');
8
- var uuid = require('uuid');
9
- var baggageSpanProcessor = require('@opentelemetry/baggage-span-processor');
10
- var contextAsyncHooks = require('@opentelemetry/context-async-hooks');
11
- var core = require('@opentelemetry/core');
12
- var exporterTraceOtlpHttp = require('@opentelemetry/exporter-trace-otlp-http');
13
- var instrumentation = require('@opentelemetry/instrumentation');
14
- var resources = require('@opentelemetry/resources');
15
- var sdkTraceNode = require('@opentelemetry/sdk-trace-node');
16
- var instrumentationAnthropic = require('@traceloop/instrumentation-anthropic');
17
- var instrumentationBedrock = require('@traceloop/instrumentation-bedrock');
18
- var instrumentationCohere = require('@traceloop/instrumentation-cohere');
19
- var instrumentationLangchain = require('@traceloop/instrumentation-langchain');
20
- var instrumentationLlamaindex = require('@traceloop/instrumentation-llamaindex');
21
- var instrumentationOpenai = require('@traceloop/instrumentation-openai');
22
- var instrumentationTogether = require('@traceloop/instrumentation-together');
23
- var instrumentationVertexai = require('@traceloop/instrumentation-vertexai');
24
-
25
- function _interopNamespaceDefault(e) {
26
- var n = Object.create(null);
27
- if (e) {
28
- Object.keys(e).forEach(function (k) {
29
- if (k !== 'default') {
30
- var d = Object.getOwnPropertyDescriptor(e, k);
31
- Object.defineProperty(n, k, d.get ? d : {
32
- enumerable: true,
33
- get: function () { return e[k]; }
34
- });
35
- }
36
- });
37
- }
38
- n.default = e;
39
- return Object.freeze(n);
40
- }
41
-
42
- var otel__namespace = /*#__PURE__*/_interopNamespaceDefault(otel);
43
-
44
- class RedactSpanProcessor {
45
- options;
46
- constructor(options) {
47
- this.options = options;
48
- if (!options.mask) {
49
- this.options.mask = (_attribute, _value) => '******';
50
- }
51
- }
52
- onStart(_span, _context) {
53
- // Noop
54
- }
55
- onEnd(span) {
56
- Object.assign(span.attributes, this.redactAttributes(span.attributes));
57
- for (const event of span.events) {
58
- if (!event.attributes)
59
- continue;
60
- Object.assign(event.attributes, this.redactAttributes(event.attributes));
61
- }
62
- for (const link of span.links) {
63
- if (!link.attributes)
64
- continue;
65
- Object.assign(link.attributes, this.redactAttributes(link.attributes));
66
- }
67
- }
68
- forceFlush() {
69
- return Promise.resolve();
70
- }
71
- shutdown() {
72
- return Promise.resolve();
73
- }
74
- shouldRedact(attribute) {
75
- return this.options.attributes.some((pattern) => {
76
- if (typeof pattern === 'string') {
77
- return attribute === pattern;
78
- }
79
- else if (pattern instanceof RegExp) {
80
- return pattern.test(attribute);
81
- }
82
- return false;
83
- });
84
- }
85
- redactAttributes(attributes) {
86
- const redacted = {};
87
- for (const [key, value] of Object.entries(attributes)) {
88
- if (this.shouldRedact(key)) {
89
- redacted[key] = this.options.mask(key, value);
90
- }
91
- }
92
- return redacted;
93
- }
94
- }
95
- const DEFAULT_REDACT_SPAN_PROCESSOR = () => new RedactSpanProcessor({
96
- attributes: [
97
- /^.*auth.*$/i,
98
- /^.*authorization.*$/i,
99
- /^(?!gen_ai\.).*usage.*$/i,
100
- /^(?!gen_ai\.).*token.*$/i,
101
- /^.*secret.*$/i,
102
- /^.*key.*$/i,
103
- /^.*password.*$/i,
104
- /^.*cookie.*$/i,
105
- /^.*session.*$/i,
106
- /^.*credential.*$/i,
107
- /^.*signature.*$/i,
108
- /^.*oauth.*$/i,
109
- /^.*saml.*$/i,
110
- /^.*openid.*$/i,
111
- /^.*refresh.*$/i,
112
- /^.*jwt.*$/i,
113
- /^.*otp.*$/i,
114
- /^.*mfa.*$/i,
115
- /^.*csrf.*$/i,
116
- /^.*xsrf.*$/i,
117
- /^.*refresh.*$/i,
118
- /^.*x[-_]forwarded[-_]for.*$/i,
119
- /^.*x[-_]real[-_]ip.*$/i,
120
- ],
121
- });
122
-
123
- const DEFAULT_GATEWAY_BASE_URL = {
124
- production: 'https://gateway.latitude.so',
125
- development: 'http://localhost:8787',
126
- test: 'http://localhost:8787',
127
- }["production"];
128
- function GET_GATEWAY_BASE_URL() {
129
- if (process.env.GATEWAY_BASE_URL) {
130
- return process.env.GATEWAY_BASE_URL;
131
- }
132
- if (!process.env.GATEWAY_HOSTNAME) {
133
- return DEFAULT_GATEWAY_BASE_URL;
134
- }
135
- const protocol = process.env.GATEWAY_SSL ? 'https' : 'http';
136
- const port = process.env.GATEWAY_PORT ?? (process.env.GATEWAY_SSL ? 443 : 80);
137
- const hostname = process.env.GATEWAY_HOSTNAME;
138
- return `${protocol}://${hostname}:${port}`;
139
- }
140
- const env = { GATEWAY_BASE_URL: GET_GATEWAY_BASE_URL() };
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ DEFAULT_REDACT_SPAN_PROCESSOR: () => DEFAULT_REDACT_SPAN_PROCESSOR,
34
+ DEFAULT_SPAN_EXPORTER: () => DEFAULT_SPAN_EXPORTER,
35
+ Instrumentation: () => Instrumentation,
36
+ LatitudeTelemetry: () => LatitudeTelemetry,
37
+ RedactSpanProcessor: () => RedactSpanProcessor
38
+ });
39
+ module.exports = __toCommonJS(index_exports);
40
+
41
+ // src/sdk/redact.ts
42
+ var RedactSpanProcessor = class {
43
+ options;
44
+ constructor(options) {
45
+ this.options = options;
46
+ if (!options.mask) {
47
+ this.options.mask = (_attribute, _value) => "******";
48
+ }
49
+ }
50
+ onStart(_span, _context) {
51
+ }
52
+ onEnd(span) {
53
+ Object.assign(span.attributes, this.redactAttributes(span.attributes));
54
+ for (const event of span.events) {
55
+ if (!event.attributes) continue;
56
+ Object.assign(event.attributes, this.redactAttributes(event.attributes));
57
+ }
58
+ for (const link of span.links) {
59
+ if (!link.attributes) continue;
60
+ Object.assign(link.attributes, this.redactAttributes(link.attributes));
61
+ }
62
+ }
63
+ forceFlush() {
64
+ return Promise.resolve();
65
+ }
66
+ shutdown() {
67
+ return Promise.resolve();
68
+ }
69
+ shouldRedact(attribute) {
70
+ return this.options.attributes.some((pattern) => {
71
+ if (typeof pattern === "string") {
72
+ return attribute === pattern;
73
+ } else if (pattern instanceof RegExp) {
74
+ return pattern.test(attribute);
75
+ }
76
+ return false;
77
+ });
78
+ }
79
+ redactAttributes(attributes) {
80
+ const redacted = {};
81
+ for (const [key, value] of Object.entries(attributes)) {
82
+ if (this.shouldRedact(key)) {
83
+ redacted[key] = this.options.mask?.(key, value);
84
+ }
85
+ }
86
+ return redacted;
87
+ }
88
+ };
89
+ var DEFAULT_REDACT_SPAN_PROCESSOR = () => new RedactSpanProcessor({
90
+ attributes: [
91
+ /^http\.request\.header\.authorization$/i,
92
+ /^http\.request\.header\.cookie$/i,
93
+ /^http\.request\.header\.x[-_]api[-_]key$/i,
94
+ /^db\.statement$/i
95
+ ]
96
+ });
97
+
98
+ // src/sdk/sdk.ts
99
+ var otel2 = __toESM(require("@opentelemetry/api"), 1);
100
+ var import_api2 = require("@opentelemetry/api");
101
+ var import_baggage_span_processor = require("@opentelemetry/baggage-span-processor");
102
+ var import_context_async_hooks = require("@opentelemetry/context-async-hooks");
103
+ var import_core = require("@opentelemetry/core");
104
+ var import_exporter_trace_otlp_http = require("@opentelemetry/exporter-trace-otlp-http");
105
+ var import_instrumentation = require("@opentelemetry/instrumentation");
106
+ var import_resources = require("@opentelemetry/resources");
107
+ var import_sdk_trace_node = require("@opentelemetry/sdk-trace-node");
108
+ var import_semantic_conventions = require("@opentelemetry/semantic-conventions");
109
+ var import_instrumentation_anthropic = require("@traceloop/instrumentation-anthropic");
110
+ var import_instrumentation_bedrock = require("@traceloop/instrumentation-bedrock");
111
+ var import_instrumentation_cohere = require("@traceloop/instrumentation-cohere");
112
+ var import_instrumentation_langchain = require("@traceloop/instrumentation-langchain");
113
+ var import_instrumentation_llamaindex = require("@traceloop/instrumentation-llamaindex");
114
+ var import_instrumentation_openai = require("@traceloop/instrumentation-openai");
115
+ var import_instrumentation_together = require("@traceloop/instrumentation-together");
116
+ var import_instrumentation_vertexai = require("@traceloop/instrumentation-vertexai");
117
+
118
+ // src/constants/attributes.ts
119
+ var ATTRIBUTES = {
120
+ tags: "latitude.tags",
121
+ metadata: "latitude.metadata",
122
+ sessionId: "session.id",
123
+ userId: "user.id"
124
+ };
141
125
 
142
- function toSnakeCase(str) {
143
- return str
144
- .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
145
- .replace(/[^A-Za-z0-9]+/g, '_')
146
- .replace(/_+/g, '_')
147
- .replace(/^_+|_+$/g, '')
148
- .toLowerCase();
149
- }
150
- function toKebabCase(input) {
151
- return input
152
- .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
153
- .replace(/[^A-Za-z0-9]+/g, '-')
154
- .replace(/-+/g, '-')
155
- .replace(/^-+|-+$/g, '')
156
- .toLowerCase();
126
+ // src/constants/scope.ts
127
+ var SCOPE_LATITUDE = "so.latitude.instrumentation";
128
+
129
+ // src/env/env.ts
130
+ var DEFAULT_EXPORTER_URL = {
131
+ production: "https://ingest.latitude.so",
132
+ development: "http://localhost:3002",
133
+ test: "http://localhost:3002"
134
+ }[process.env.NODE_ENV ?? "development"] ?? "http://localhost:3002";
135
+ function getExporterUrl() {
136
+ if (process.env.LATITUDE_TELEMETRY_URL) {
137
+ return process.env.LATITUDE_TELEMETRY_URL;
138
+ }
139
+ return DEFAULT_EXPORTER_URL;
157
140
  }
158
-
159
- var StreamEventTypes;
160
- (function (StreamEventTypes) {
161
- StreamEventTypes["Latitude"] = "latitude-event";
162
- StreamEventTypes["Provider"] = "provider-event";
163
- })(StreamEventTypes || (StreamEventTypes = {}));
164
- zod.z.object({
165
- id: zod.z.string(),
166
- name: zod.z.string(),
167
- result: zod.z.unknown(),
168
- isError: zod.z.boolean().optional(),
169
- text: zod.z.string().optional(),
170
- });
171
- zod.z.object({
172
- inputTokens: zod.z.number(),
173
- outputTokens: zod.z.number(),
174
- promptTokens: zod.z.number(),
175
- completionTokens: zod.z.number(),
176
- totalTokens: zod.z.number(),
177
- reasoningTokens: zod.z.number(),
178
- cachedInputTokens: zod.z.number(),
179
- });
180
-
181
- var ParameterType;
182
- (function (ParameterType) {
183
- ParameterType["Text"] = "text";
184
- ParameterType["Image"] = "image";
185
- ParameterType["File"] = "file";
186
- })(ParameterType || (ParameterType = {}));
187
- var LatitudeTool;
188
- (function (LatitudeTool) {
189
- LatitudeTool["RunCode"] = "code";
190
- LatitudeTool["WebSearch"] = "search";
191
- LatitudeTool["WebExtract"] = "extract";
192
- LatitudeTool["Think"] = "think";
193
- LatitudeTool["TODO"] = "todo";
194
- })(LatitudeTool || (LatitudeTool = {}));
195
- var LatitudeToolInternalName;
196
- (function (LatitudeToolInternalName) {
197
- LatitudeToolInternalName["RunCode"] = "lat_tool_run_code";
198
- LatitudeToolInternalName["WebSearch"] = "lat_tool_web_search";
199
- LatitudeToolInternalName["WebExtract"] = "lat_tool_web_extract";
200
- LatitudeToolInternalName["Think"] = "think";
201
- LatitudeToolInternalName["TODO"] = "todo_write";
202
- })(LatitudeToolInternalName || (LatitudeToolInternalName = {}));
203
- [
204
- LatitudeTool.Think,
205
- LatitudeTool.TODO,
206
- ];
207
-
208
- const actualOutputConfiguration = zod.z.object({
209
- messageSelection: zod.z.enum(['last', 'all']), // Which assistant messages to select
210
- contentFilter: zod.z
211
- .enum(['text', 'reasoning', 'image', 'file', 'tool_call'])
212
- .optional(),
213
- parsingFormat: zod.z.enum(['string', 'json']),
214
- fieldAccessor: zod.z.string().optional(), // Field accessor to get the output from if it's a key-value format
215
- });
216
- const expectedOutputConfiguration = zod.z.object({
217
- parsingFormat: zod.z.enum(['string', 'json']),
218
- fieldAccessor: zod.z.string().optional(), // Field accessor to get the output from if it's a key-value format
219
- });
220
- const EVALUATION_TRIGGER_TARGETS = ['first', 'every', 'last'];
221
- const LAST_INTERACTION_DEBOUNCE_MIN_SECONDS = 30;
222
- const LAST_INTERACTION_DEBOUNCE_MAX_SECONDS = 60 * 60 * 24; // 1 day
223
- const MIN_EVALUATION_SAMPLE_RATE = 0; // 0%
224
- const MAX_EVALUATION_SAMPLE_RATE = 100; // 100%
225
- const triggerConfiguration = zod.z.object({
226
- target: zod.z.enum(EVALUATION_TRIGGER_TARGETS),
227
- lastInteractionDebounce: zod.z
228
- .number()
229
- .min(LAST_INTERACTION_DEBOUNCE_MIN_SECONDS)
230
- .max(LAST_INTERACTION_DEBOUNCE_MAX_SECONDS)
231
- .optional(),
232
- sampleRate: zod.z
233
- .number()
234
- .int()
235
- .min(MIN_EVALUATION_SAMPLE_RATE)
236
- .max(MAX_EVALUATION_SAMPLE_RATE)
237
- .optional(),
238
- });
239
- const baseEvaluationConfiguration = zod.z.object({
240
- reverseScale: zod.z.boolean(), // If true, lower is better, otherwise, higher is better
241
- actualOutput: actualOutputConfiguration,
242
- expectedOutput: expectedOutputConfiguration.optional(),
243
- trigger: triggerConfiguration.optional(),
244
- });
245
- const baseEvaluationResultMetadata = zod.z.object({
246
- // configuration: Configuration snapshot is defined in every metric specification
247
- actualOutput: zod.z.string(),
248
- expectedOutput: zod.z.string().optional(),
249
- datasetLabel: zod.z.string().optional(),
250
- });
251
- const baseEvaluationResultError = zod.z.object({
252
- message: zod.z.string(),
253
- });
254
-
255
- const compositeEvaluationConfiguration = baseEvaluationConfiguration.extend({
256
- evaluationUuids: zod.z.array(zod.z.string()),
257
- minThreshold: zod.z.number().optional(), // Threshold percentage
258
- maxThreshold: zod.z.number().optional(), // Threshold percentage
259
- });
260
- const compositeEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
261
- results: zod.z.record(zod.z.string(), // Evaluation uuid
262
- zod.z.object({
263
- uuid: zod.z.string(), // Result uuid (for side effects)
264
- name: zod.z.string(), // Evaluation name
265
- score: zod.z.number(), // Normalized score
266
- reason: zod.z.string(),
267
- passed: zod.z.boolean(),
268
- tokens: zod.z.number().optional(), // Optional llm evaluation usage
269
- })),
270
- });
271
- const compositeEvaluationResultError = baseEvaluationResultError.extend({
272
- errors: zod.z
273
- .record(zod.z.string(), // Evaluation uuid
274
- zod.z.object({
275
- uuid: zod.z.string(), // Result uuid (for side effects)
276
- name: zod.z.string(), // Evaluation name
277
- message: zod.z.string(),
278
- }))
279
- .optional(),
280
- });
281
- // AVERAGE
282
- const compositeEvaluationAverageConfiguration = compositeEvaluationConfiguration.extend({});
283
- compositeEvaluationResultMetadata.extend({
284
- configuration: compositeEvaluationAverageConfiguration,
285
- });
286
- compositeEvaluationResultError.extend({});
287
- const CompositeEvaluationAverageSpecification = {
288
- };
289
- // WEIGHTED
290
- const compositeEvaluationWeightedConfiguration = compositeEvaluationConfiguration.extend({
291
- weights: zod.z.record(zod.z.string(), // Evaluation uuid
292
- zod.z.number()),
293
- });
294
- compositeEvaluationResultMetadata.extend({
295
- configuration: compositeEvaluationWeightedConfiguration,
296
- });
297
- compositeEvaluationResultError.extend({});
298
- const CompositeEvaluationWeightedSpecification = {
141
+ var env = { EXPORTER_URL: getExporterUrl() };
142
+
143
+ // src/instrumentations/manual.ts
144
+ var otel = __toESM(require("@opentelemetry/api"), 1);
145
+ var import_api = require("@opentelemetry/api");
146
+ var ManualInstrumentation = class {
147
+ enabled;
148
+ _tracer;
149
+ constructor(tracer) {
150
+ this.enabled = false;
151
+ this._tracer = tracer;
152
+ }
153
+ get tracer() {
154
+ return this._tracer;
155
+ }
156
+ isEnabled() {
157
+ return this.enabled;
158
+ }
159
+ enable() {
160
+ this.enabled = true;
161
+ }
162
+ disable() {
163
+ this.enabled = false;
164
+ }
165
+ resume(ctx) {
166
+ const parts = ctx.traceparent.split("-");
167
+ if (parts.length !== 4) {
168
+ return otel.ROOT_CONTEXT;
169
+ }
170
+ const [, traceId, spanId, flags] = parts;
171
+ if (!traceId || !spanId) {
172
+ return otel.ROOT_CONTEXT;
173
+ }
174
+ const spanContext = {
175
+ traceId,
176
+ spanId,
177
+ traceFlags: parseInt(flags ?? "01", 16),
178
+ isRemote: true
299
179
  };
300
- // CUSTOM
301
- const compositeEvaluationCustomConfiguration = compositeEvaluationConfiguration.extend({
302
- formula: zod.z.string(),
303
- });
304
- compositeEvaluationResultMetadata.extend({
305
- configuration: compositeEvaluationCustomConfiguration,
306
- });
307
- compositeEvaluationResultError.extend({});
308
- const CompositeEvaluationCustomSpecification = {
309
- };
310
- /* ------------------------------------------------------------------------- */
311
- var CompositeEvaluationMetric;
312
- (function (CompositeEvaluationMetric) {
313
- CompositeEvaluationMetric["Average"] = "average";
314
- CompositeEvaluationMetric["Weighted"] = "weighted";
315
- CompositeEvaluationMetric["Custom"] = "custom";
316
- })(CompositeEvaluationMetric || (CompositeEvaluationMetric = {}));
317
- const CompositeEvaluationSpecification = {
318
- // prettier-ignore
319
- metrics: {
320
- [CompositeEvaluationMetric.Average]: CompositeEvaluationAverageSpecification,
321
- [CompositeEvaluationMetric.Weighted]: CompositeEvaluationWeightedSpecification,
322
- [CompositeEvaluationMetric.Custom]: CompositeEvaluationCustomSpecification,
323
- },
180
+ let context2 = import_api.trace.setSpanContext(otel.ROOT_CONTEXT, spanContext);
181
+ if (ctx.baggage) {
182
+ const baggageEntries = {};
183
+ for (const pair of ctx.baggage.split(",")) {
184
+ const [key, value] = pair.split("=", 2);
185
+ if (key && value) {
186
+ baggageEntries[decodeURIComponent(key)] = { value: decodeURIComponent(value) };
187
+ }
188
+ }
189
+ const baggage = import_api.propagation.createBaggage(baggageEntries);
190
+ context2 = import_api.propagation.setBaggage(context2, baggage);
191
+ }
192
+ return context2;
193
+ }
324
194
  };
325
195
 
326
- const selectedContextSchema = zod.z.object({
327
- messageIndex: zod.z.number().int().nonnegative(),
328
- contentBlockIndex: zod.z.number().int().nonnegative(),
329
- contentType: zod.z.enum([
330
- 'text',
331
- 'reasoning',
332
- 'image',
333
- 'file',
334
- 'tool-call',
335
- 'tool-result',
336
- ]),
337
- textRange: zod.z
338
- .object({
339
- start: zod.z.number().int().nonnegative(),
340
- end: zod.z.number().int().nonnegative(),
341
- })
342
- .optional(),
343
- selectedText: zod.z.string().optional(),
344
- toolCallId: zod.z.string().optional(),
345
- });
346
- const humanEvaluationConfiguration = baseEvaluationConfiguration.extend({
347
- enableControls: zod.z.boolean().optional(), // UI annotation controls
348
- criteria: zod.z.string().optional(),
349
- });
350
- const humanEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
351
- reason: zod.z.string().optional(),
352
- enrichedReason: zod.z.string().optional(),
353
- selectedContexts: zod.z.array(selectedContextSchema).optional(),
354
- });
355
- const humanEvaluationResultError = baseEvaluationResultError.extend({});
356
- // BINARY
357
- const humanEvaluationBinaryConfiguration = humanEvaluationConfiguration.extend({
358
- passDescription: zod.z.string().optional(),
359
- failDescription: zod.z.string().optional(),
360
- });
361
- humanEvaluationResultMetadata.extend({
362
- configuration: humanEvaluationBinaryConfiguration,
363
- });
364
- humanEvaluationResultError.extend({});
365
- const HumanEvaluationBinarySpecification = {
366
- };
367
- // RATING
368
- const humanEvaluationRatingConfiguration = humanEvaluationConfiguration.extend({
369
- minRating: zod.z.number(),
370
- minRatingDescription: zod.z.string().optional(),
371
- maxRating: zod.z.number(),
372
- maxRatingDescription: zod.z.string().optional(),
373
- minThreshold: zod.z.number().optional(), // Threshold in rating range
374
- maxThreshold: zod.z.number().optional(), // Threshold in rating range
375
- });
376
- humanEvaluationResultMetadata.extend({
377
- configuration: humanEvaluationRatingConfiguration,
378
- });
379
- humanEvaluationResultError.extend({});
380
- const HumanEvaluationRatingSpecification = {
381
- };
382
- /* ------------------------------------------------------------------------- */
383
- var HumanEvaluationMetric;
384
- (function (HumanEvaluationMetric) {
385
- HumanEvaluationMetric["Binary"] = "binary";
386
- HumanEvaluationMetric["Rating"] = "rating";
387
- })(HumanEvaluationMetric || (HumanEvaluationMetric = {}));
388
- const HumanEvaluationSpecification = {
389
- // prettier-ignore
390
- metrics: {
391
- [HumanEvaluationMetric.Binary]: HumanEvaluationBinarySpecification,
392
- [HumanEvaluationMetric.Rating]: HumanEvaluationRatingSpecification,
393
- },
196
+ // src/sdk/sdk.ts
197
+ var TRACES_URL = `${env.EXPORTER_URL}/v1/traces`;
198
+ var SERVICE_NAME = process.env.npm_package_name || "unknown";
199
+ var SCOPE_VERSION = process.env.npm_package_version || "unknown";
200
+ var ContextManager = class {
201
+ telemetry;
202
+ constructor(telemetry) {
203
+ this.telemetry = telemetry;
204
+ }
205
+ resume(ctx) {
206
+ return this.telemetry.resume(ctx);
207
+ }
208
+ active() {
209
+ return import_api2.context.active();
210
+ }
211
+ with(ctx, fn, thisArg, ...args) {
212
+ return import_api2.context.with(ctx, fn, thisArg, ...args);
213
+ }
394
214
  };
395
-
396
- const llmEvaluationConfiguration = baseEvaluationConfiguration.extend({
397
- provider: zod.z.string(),
398
- model: zod.z.string(),
399
- });
400
- const llmEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
401
- evaluationLogId: zod.z.number(),
402
- reason: zod.z.string(),
403
- tokens: zod.z.number(),
404
- cost: zod.z.number(),
405
- duration: zod.z.number(),
406
- });
407
- const llmEvaluationResultError = baseEvaluationResultError.extend({
408
- runErrorId: zod.z.number().optional(),
409
- });
410
- // BINARY
411
- const llmEvaluationBinaryConfiguration = llmEvaluationConfiguration.extend({
412
- criteria: zod.z.string(),
413
- passDescription: zod.z.string(),
414
- failDescription: zod.z.string(),
415
- });
416
- llmEvaluationResultMetadata.extend({
417
- configuration: llmEvaluationBinaryConfiguration,
418
- });
419
- llmEvaluationResultError.extend({});
420
- const LlmEvaluationBinarySpecification = {
421
- };
422
- // RATING
423
- const llmEvaluationRatingConfiguration = llmEvaluationConfiguration.extend({
424
- criteria: zod.z.string(),
425
- minRating: zod.z.number(),
426
- minRatingDescription: zod.z.string(),
427
- maxRating: zod.z.number(),
428
- maxRatingDescription: zod.z.string(),
429
- minThreshold: zod.z.number().optional(), // Threshold in rating range
430
- maxThreshold: zod.z.number().optional(), // Threshold in rating range
431
- });
432
- llmEvaluationResultMetadata.extend({
433
- configuration: llmEvaluationRatingConfiguration,
434
- });
435
- llmEvaluationResultError.extend({});
436
- const LlmEvaluationRatingSpecification = {
437
- };
438
- // COMPARISON
439
- const llmEvaluationComparisonConfiguration = llmEvaluationConfiguration.extend({
440
- criteria: zod.z.string(),
441
- passDescription: zod.z.string(),
442
- failDescription: zod.z.string(),
443
- minThreshold: zod.z.number().optional(), // Threshold percentage
444
- maxThreshold: zod.z.number().optional(), // Threshold percentage
445
- });
446
- llmEvaluationResultMetadata.extend({
447
- configuration: llmEvaluationComparisonConfiguration,
448
- });
449
- llmEvaluationResultError.extend({});
450
- const LlmEvaluationComparisonSpecification = {
451
- };
452
- // CUSTOM
453
- const llmEvaluationCustomConfiguration = llmEvaluationConfiguration.extend({
454
- prompt: zod.z.string(),
455
- minScore: zod.z.number(),
456
- maxScore: zod.z.number(),
457
- minThreshold: zod.z.number().optional(), // Threshold percentage
458
- maxThreshold: zod.z.number().optional(), // Threshold percentage
459
- });
460
- llmEvaluationResultMetadata.extend({
461
- configuration: llmEvaluationCustomConfiguration,
462
- });
463
- llmEvaluationResultError.extend({});
464
- const LlmEvaluationCustomSpecification = {
465
- };
466
- // CUSTOM LABELED
467
- const LlmEvaluationCustomLabeledSpecification = {
468
- };
469
- /* ------------------------------------------------------------------------- */
470
- var LlmEvaluationMetric;
471
- (function (LlmEvaluationMetric) {
472
- LlmEvaluationMetric["Binary"] = "binary";
473
- LlmEvaluationMetric["Rating"] = "rating";
474
- LlmEvaluationMetric["Comparison"] = "comparison";
475
- LlmEvaluationMetric["Custom"] = "custom";
476
- LlmEvaluationMetric["CustomLabeled"] = "custom_labeled";
477
- })(LlmEvaluationMetric || (LlmEvaluationMetric = {}));
478
- const LlmEvaluationSpecification = {
479
- // prettier-ignore
480
- metrics: {
481
- [LlmEvaluationMetric.Binary]: LlmEvaluationBinarySpecification,
482
- [LlmEvaluationMetric.Rating]: LlmEvaluationRatingSpecification,
483
- [LlmEvaluationMetric.Comparison]: LlmEvaluationComparisonSpecification,
484
- [LlmEvaluationMetric.Custom]: LlmEvaluationCustomSpecification,
485
- [LlmEvaluationMetric.CustomLabeled]: LlmEvaluationCustomLabeledSpecification,
486
- },
215
+ var InstrumentationManager = class {
216
+ instrumentations;
217
+ constructor(instrumentations) {
218
+ this.instrumentations = instrumentations;
219
+ }
220
+ enable() {
221
+ for (const instrumentation of this.instrumentations) {
222
+ if (!instrumentation.isEnabled()) instrumentation.enable();
223
+ }
224
+ }
225
+ disable() {
226
+ for (const instrumentation of this.instrumentations) {
227
+ if (instrumentation.isEnabled()) instrumentation.disable();
228
+ }
229
+ }
487
230
  };
488
-
489
- const ruleEvaluationConfiguration = baseEvaluationConfiguration.extend({});
490
- const ruleEvaluationResultMetadata = baseEvaluationResultMetadata.extend({
491
- reason: zod.z.string().optional(),
492
- });
493
- const ruleEvaluationResultError = baseEvaluationResultError.extend({});
494
- // EXACT MATCH
495
- const ruleEvaluationExactMatchConfiguration = ruleEvaluationConfiguration.extend({
496
- caseInsensitive: zod.z.boolean(),
497
- });
498
- ruleEvaluationResultMetadata.extend({
499
- configuration: ruleEvaluationExactMatchConfiguration,
500
- });
501
- ruleEvaluationResultError.extend({});
502
- const RuleEvaluationExactMatchSpecification = {
503
- };
504
- // REGULAR EXPRESSION
505
- const ruleEvaluationRegularExpressionConfiguration = ruleEvaluationConfiguration.extend({
506
- pattern: zod.z.string(),
507
- });
508
- ruleEvaluationResultMetadata.extend({
509
- configuration: ruleEvaluationRegularExpressionConfiguration,
510
- });
511
- ruleEvaluationResultError.extend({});
512
- const RuleEvaluationRegularExpressionSpecification = {
513
- };
514
- // SCHEMA VALIDATION
515
- const ruleEvaluationSchemaValidationConfiguration = ruleEvaluationConfiguration.extend({
516
- format: zod.z.enum(['json']),
517
- schema: zod.z.string(),
518
- });
519
- ruleEvaluationResultMetadata.extend({
520
- configuration: ruleEvaluationSchemaValidationConfiguration,
521
- });
522
- ruleEvaluationResultError.extend({});
523
- const RuleEvaluationSchemaValidationSpecification = {
524
- };
525
- // LENGTH COUNT
526
- const ruleEvaluationLengthCountConfiguration = ruleEvaluationConfiguration.extend({
527
- algorithm: zod.z.enum(['character', 'word', 'sentence']),
528
- minLength: zod.z.number().optional(),
529
- maxLength: zod.z.number().optional(),
530
- });
531
- ruleEvaluationResultMetadata.extend({
532
- configuration: ruleEvaluationLengthCountConfiguration,
533
- });
534
- ruleEvaluationResultError.extend({});
535
- const RuleEvaluationLengthCountSpecification = {
536
- };
537
- // LEXICAL OVERLAP
538
- const ruleEvaluationLexicalOverlapConfiguration = ruleEvaluationConfiguration.extend({
539
- algorithm: zod.z.enum(['substring', 'levenshtein_distance', 'rouge']),
540
- minOverlap: zod.z.number().optional(), // Percentage of overlap
541
- maxOverlap: zod.z.number().optional(), // Percentage of overlap
542
- });
543
- ruleEvaluationResultMetadata.extend({
544
- configuration: ruleEvaluationLexicalOverlapConfiguration,
545
- });
546
- ruleEvaluationResultError.extend({});
547
- const RuleEvaluationLexicalOverlapSpecification = {
548
- };
549
- // SEMANTIC SIMILARITY
550
- const ruleEvaluationSemanticSimilarityConfiguration = ruleEvaluationConfiguration.extend({
551
- algorithm: zod.z.enum(['cosine_distance']),
552
- minSimilarity: zod.z.number().optional(), // Percentage of similarity
553
- maxSimilarity: zod.z.number().optional(), // Percentage of similarity
554
- });
555
- ruleEvaluationResultMetadata.extend({
556
- configuration: ruleEvaluationSemanticSimilarityConfiguration,
557
- });
558
- ruleEvaluationResultError.extend({});
559
- const RuleEvaluationSemanticSimilaritySpecification = {
560
- };
561
- // NUMERIC SIMILARITY
562
- const ruleEvaluationNumericSimilarityConfiguration = ruleEvaluationConfiguration.extend({
563
- algorithm: zod.z.enum(['relative_difference']),
564
- minSimilarity: zod.z.number().optional(), // Percentage of similarity
565
- maxSimilarity: zod.z.number().optional(), // Percentage of similarity
566
- });
567
- ruleEvaluationResultMetadata.extend({
568
- configuration: ruleEvaluationNumericSimilarityConfiguration,
569
- });
570
- ruleEvaluationResultError.extend({});
571
- const RuleEvaluationNumericSimilaritySpecification = {
572
- };
573
- /* ------------------------------------------------------------------------- */
574
- var RuleEvaluationMetric;
575
- (function (RuleEvaluationMetric) {
576
- RuleEvaluationMetric["ExactMatch"] = "exact_match";
577
- RuleEvaluationMetric["RegularExpression"] = "regular_expression";
578
- RuleEvaluationMetric["SchemaValidation"] = "schema_validation";
579
- RuleEvaluationMetric["LengthCount"] = "length_count";
580
- RuleEvaluationMetric["LexicalOverlap"] = "lexical_overlap";
581
- RuleEvaluationMetric["SemanticSimilarity"] = "semantic_similarity";
582
- RuleEvaluationMetric["NumericSimilarity"] = "numeric_similarity";
583
- })(RuleEvaluationMetric || (RuleEvaluationMetric = {}));
584
- const RuleEvaluationSpecification = {
585
- // prettier-ignore
586
- metrics: {
587
- [RuleEvaluationMetric.ExactMatch]: RuleEvaluationExactMatchSpecification,
588
- [RuleEvaluationMetric.RegularExpression]: RuleEvaluationRegularExpressionSpecification,
589
- [RuleEvaluationMetric.SchemaValidation]: RuleEvaluationSchemaValidationSpecification,
590
- [RuleEvaluationMetric.LengthCount]: RuleEvaluationLengthCountSpecification,
591
- [RuleEvaluationMetric.LexicalOverlap]: RuleEvaluationLexicalOverlapSpecification,
592
- [RuleEvaluationMetric.SemanticSimilarity]: RuleEvaluationSemanticSimilaritySpecification,
593
- [RuleEvaluationMetric.NumericSimilarity]: RuleEvaluationNumericSimilaritySpecification,
594
- },
231
+ var TracerManager = class {
232
+ nodeProvider;
233
+ scopeVersion;
234
+ constructor(nodeProvider, scopeVersion) {
235
+ this.nodeProvider = nodeProvider;
236
+ this.scopeVersion = scopeVersion;
237
+ }
238
+ get(scope) {
239
+ return this.provider(scope).getTracer("");
240
+ }
241
+ provider(scope) {
242
+ return new ScopedTracerProvider(`${SCOPE_LATITUDE}.${scope}`, this.scopeVersion, this.nodeProvider);
243
+ }
595
244
  };
596
-
597
- var EvaluationType;
598
- (function (EvaluationType) {
599
- EvaluationType["Rule"] = "rule";
600
- EvaluationType["Llm"] = "llm";
601
- EvaluationType["Human"] = "human";
602
- EvaluationType["Composite"] = "composite";
603
- })(EvaluationType || (EvaluationType = {}));
604
- const EvaluationTypeSchema = zod.z.enum(EvaluationType);
605
- const EvaluationMetricSchema = zod.z.union([
606
- zod.z.enum(RuleEvaluationMetric),
607
- zod.z.enum(LlmEvaluationMetric),
608
- zod.z.enum(HumanEvaluationMetric),
609
- zod.z.enum(CompositeEvaluationMetric),
610
- ]);
611
- const EvaluationConfigurationSchema = zod.z.custom();
612
- // prettier-ignore
613
- zod.z.custom();
614
- // prettier-ignore
615
- zod.z.custom();
616
- ({
617
- [EvaluationType.Rule]: RuleEvaluationSpecification,
618
- [EvaluationType.Llm]: LlmEvaluationSpecification,
619
- [EvaluationType.Human]: HumanEvaluationSpecification,
620
- [EvaluationType.Composite]: CompositeEvaluationSpecification,
621
- });
622
- zod.z.object({
623
- name: zod.z.string(),
624
- description: zod.z.string(),
625
- type: EvaluationTypeSchema,
626
- metric: EvaluationMetricSchema,
627
- configuration: EvaluationConfigurationSchema,
628
- });
629
- zod.z.object({
630
- evaluateLiveLogs: zod.z.boolean().nullable().optional(),
631
- });
632
-
633
- var ChainEventTypes;
634
- (function (ChainEventTypes) {
635
- ChainEventTypes["ChainCompleted"] = "chain-completed";
636
- ChainEventTypes["ChainError"] = "chain-error";
637
- ChainEventTypes["ChainStarted"] = "chain-started";
638
- ChainEventTypes["IntegrationWakingUp"] = "integration-waking-up";
639
- ChainEventTypes["ProviderCompleted"] = "provider-completed";
640
- ChainEventTypes["ProviderStarted"] = "provider-started";
641
- ChainEventTypes["StepCompleted"] = "step-completed";
642
- ChainEventTypes["StepStarted"] = "step-started";
643
- ChainEventTypes["ToolCompleted"] = "tool-completed";
644
- ChainEventTypes["ToolResult"] = "tool-result";
645
- ChainEventTypes["ToolsStarted"] = "tools-started";
646
- })(ChainEventTypes || (ChainEventTypes = {}));
647
-
648
- zod.z.object({
649
- name: zod.z.string(),
650
- provider: zod.z.string(),
651
- model: zod.z.string(),
652
- temperature: zod.z.number(),
653
- });
654
- // Experiment ran from a dataset
655
- const experimentDatasetSourceSchema = zod.z.object({
656
- source: zod.z.literal('dataset'),
657
- datasetId: zod.z.number(),
658
- fromRow: zod.z.number(),
659
- toRow: zod.z.number(),
660
- datasetLabels: zod.z.record(zod.z.string(), zod.z.string()),
661
- parametersMap: zod.z.record(zod.z.string(), zod.z.number()),
662
- });
663
- // Experiment ran from last logs (from commit and creation time of experiment)
664
- const experimentLogsSourceSchema = zod.z.object({
665
- source: zod.z.literal('logs'),
666
- count: zod.z.number(),
667
- });
668
- // Experiment ran with manual parameters (currently only used for prompts with no parameters)
669
- const experimentManualSourceSchema = zod.z.object({
670
- source: zod.z.literal('manual'),
671
- count: zod.z.number(),
672
- parametersMap: zod.z.record(zod.z.string(), zod.z.number()),
673
- });
674
- zod.z.discriminatedUnion('source', [
675
- experimentDatasetSourceSchema,
676
- experimentLogsSourceSchema,
677
- experimentManualSourceSchema,
678
- ]);
679
-
680
- var QuotaType;
681
- (function (QuotaType) {
682
- QuotaType["Seats"] = "seats";
683
- QuotaType["Runs"] = "runs";
684
- QuotaType["Credits"] = "credits";
685
- })(QuotaType || (QuotaType = {}));
686
- var GrantSource;
687
- (function (GrantSource) {
688
- GrantSource["System"] = "system";
689
- GrantSource["Subscription"] = "subscription";
690
- GrantSource["Purchase"] = "purchase";
691
- GrantSource["Reward"] = "reward";
692
- GrantSource["Promocode"] = "promocode";
693
- })(GrantSource || (GrantSource = {}));
694
-
695
- var ModifiedDocumentType;
696
- (function (ModifiedDocumentType) {
697
- ModifiedDocumentType["Created"] = "created";
698
- ModifiedDocumentType["Updated"] = "updated";
699
- ModifiedDocumentType["UpdatedPath"] = "updated_path";
700
- ModifiedDocumentType["Deleted"] = "deleted";
701
- })(ModifiedDocumentType || (ModifiedDocumentType = {}));
702
-
703
- var IntegrationType;
704
- (function (IntegrationType) {
705
- IntegrationType["Latitude"] = "latitude";
706
- IntegrationType["ExternalMCP"] = "custom_mcp";
707
- IntegrationType["Pipedream"] = "pipedream";
708
- IntegrationType["HostedMCP"] = "mcp_server";
709
- })(IntegrationType || (IntegrationType = {}));
710
- var HostedIntegrationType;
711
- (function (HostedIntegrationType) {
712
- HostedIntegrationType["Stripe"] = "stripe";
713
- HostedIntegrationType["Slack"] = "slack";
714
- HostedIntegrationType["Github"] = "github";
715
- HostedIntegrationType["Notion"] = "notion";
716
- HostedIntegrationType["Twitter"] = "twitter";
717
- HostedIntegrationType["Airtable"] = "airtable";
718
- HostedIntegrationType["Linear"] = "linear";
719
- HostedIntegrationType["YoutubeCaptions"] = "youtube_captions";
720
- HostedIntegrationType["Reddit"] = "reddit";
721
- HostedIntegrationType["Telegram"] = "telegram";
722
- HostedIntegrationType["Tinybird"] = "tinybird";
723
- HostedIntegrationType["Perplexity"] = "perplexity";
724
- HostedIntegrationType["AwsKbRetrieval"] = "aws_kb_retrieval";
725
- HostedIntegrationType["BraveSearch"] = "brave_search";
726
- HostedIntegrationType["EverArt"] = "ever_art";
727
- HostedIntegrationType["Fetch"] = "fetch";
728
- HostedIntegrationType["GitLab"] = "gitlab";
729
- HostedIntegrationType["GoogleMaps"] = "google_maps";
730
- HostedIntegrationType["Sentry"] = "sentry";
731
- HostedIntegrationType["Puppeteer"] = "puppeteer";
732
- HostedIntegrationType["Time"] = "time";
733
- HostedIntegrationType["browserbase"] = "browserbase";
734
- HostedIntegrationType["Neon"] = "neon";
735
- HostedIntegrationType["Postgres"] = "postgres";
736
- HostedIntegrationType["Supabase"] = "supabase";
737
- HostedIntegrationType["Redis"] = "redis";
738
- HostedIntegrationType["Jira"] = "jira";
739
- HostedIntegrationType["Attio"] = "attio";
740
- HostedIntegrationType["Ghost"] = "ghost";
741
- HostedIntegrationType["Figma"] = "figma";
742
- HostedIntegrationType["Hyperbrowser"] = "hyperbrowser";
743
- HostedIntegrationType["Audiense"] = "audiense";
744
- HostedIntegrationType["Apify"] = "apify";
745
- HostedIntegrationType["Exa"] = "exa";
746
- HostedIntegrationType["YepCode"] = "yepcode";
747
- HostedIntegrationType["Monday"] = "monday";
748
- HostedIntegrationType["AgentQL"] = "agentql";
749
- HostedIntegrationType["AgentRPC"] = "agentrpc";
750
- HostedIntegrationType["AstraDB"] = "astra_db";
751
- HostedIntegrationType["Bankless"] = "bankless";
752
- HostedIntegrationType["Bicscan"] = "bicscan";
753
- HostedIntegrationType["Chargebee"] = "chargebee";
754
- HostedIntegrationType["Chronulus"] = "chronulus";
755
- HostedIntegrationType["CircleCI"] = "circleci";
756
- HostedIntegrationType["Codacy"] = "codacy";
757
- HostedIntegrationType["CodeLogic"] = "codelogic";
758
- HostedIntegrationType["Convex"] = "convex";
759
- HostedIntegrationType["Dart"] = "dart";
760
- HostedIntegrationType["DevHubCMS"] = "devhub_cms";
761
- HostedIntegrationType["Elasticsearch"] = "elasticsearch";
762
- HostedIntegrationType["ESignatures"] = "esignatures";
763
- HostedIntegrationType["Fewsats"] = "fewsats";
764
- HostedIntegrationType["Firecrawl"] = "firecrawl";
765
- HostedIntegrationType["Graphlit"] = "graphlit";
766
- HostedIntegrationType["Heroku"] = "heroku";
767
- HostedIntegrationType["IntegrationAppHubspot"] = "integration_app_hubspot";
768
- HostedIntegrationType["LaraTranslate"] = "lara_translate";
769
- HostedIntegrationType["Logfire"] = "logfire";
770
- HostedIntegrationType["Langfuse"] = "langfuse";
771
- HostedIntegrationType["LingoSupabase"] = "lingo_supabase";
772
- HostedIntegrationType["Make"] = "make";
773
- HostedIntegrationType["Meilisearch"] = "meilisearch";
774
- HostedIntegrationType["Momento"] = "momento";
775
- HostedIntegrationType["Neo4jAura"] = "neo4j_aura";
776
- HostedIntegrationType["Octagon"] = "octagon";
777
- HostedIntegrationType["Paddle"] = "paddle";
778
- HostedIntegrationType["PayPal"] = "paypal";
779
- HostedIntegrationType["Qdrant"] = "qdrant";
780
- HostedIntegrationType["Raygun"] = "raygun";
781
- HostedIntegrationType["Rember"] = "rember";
782
- HostedIntegrationType["Riza"] = "riza";
783
- HostedIntegrationType["Search1API"] = "search1api";
784
- HostedIntegrationType["Semgrep"] = "semgrep";
785
- HostedIntegrationType["Tavily"] = "tavily";
786
- HostedIntegrationType["Unstructured"] = "unstructured";
787
- HostedIntegrationType["Vectorize"] = "vectorize";
788
- HostedIntegrationType["Xero"] = "xero";
789
- HostedIntegrationType["Readwise"] = "readwise";
790
- HostedIntegrationType["Airbnb"] = "airbnb";
791
- HostedIntegrationType["Mintlify"] = "mintlify";
792
- // Require all auth file :point_down:
793
- // Gmail = 'google_drive',
794
- // GoogleCalendar = 'google_drive',
795
- // GoogleDrive = 'google_drive',
796
- // GoogleWorkspace = 'google_workspace', // env vars not supported (?)
797
- // TODO: implement these
798
- // Wordpress = 'wordpress', // Not on OpenTools
799
- // Discord = 'discord', // Not on OpenTools
800
- // Intercom = 'intercom', // Not on OpenTools
801
- // Hubspot = 'hubspot', // Docker based
802
- // Loops = 'loops', // Does not exist
803
- })(HostedIntegrationType || (HostedIntegrationType = {}));
804
-
805
- var LogSources;
806
- (function (LogSources) {
807
- LogSources["API"] = "api";
808
- LogSources["AgentAsTool"] = "agent_as_tool";
809
- LogSources["Copilot"] = "copilot";
810
- LogSources["EmailTrigger"] = "email_trigger";
811
- LogSources["Evaluation"] = "evaluation";
812
- LogSources["Experiment"] = "experiment";
813
- LogSources["IntegrationTrigger"] = "integration_trigger";
814
- LogSources["Playground"] = "playground";
815
- LogSources["ScheduledTrigger"] = "scheduled_trigger";
816
- LogSources["SharedPrompt"] = "shared_prompt";
817
- LogSources["ShadowTest"] = "shadow_test";
818
- LogSources["ABTestChallenger"] = "ab_test_challenger";
819
- LogSources["User"] = "user";
820
- LogSources["Optimization"] = "optimization";
821
- })(LogSources || (LogSources = {}));
822
-
823
- var RunSourceGroup;
824
- (function (RunSourceGroup) {
825
- RunSourceGroup["Production"] = "production";
826
- RunSourceGroup["Playground"] = "playground";
827
- })(RunSourceGroup || (RunSourceGroup = {}));
828
- ({
829
- [RunSourceGroup.Production]: [
830
- LogSources.API,
831
- LogSources.ShadowTest,
832
- LogSources.ABTestChallenger,
833
- LogSources.EmailTrigger,
834
- LogSources.IntegrationTrigger,
835
- LogSources.ScheduledTrigger,
836
- LogSources.SharedPrompt,
837
- LogSources.User,
838
- ],
839
- [RunSourceGroup.Playground]: [LogSources.Playground, LogSources.Experiment],
840
- });
841
-
842
- var SpanKind;
843
- (function (SpanKind) {
844
- SpanKind["Internal"] = "internal";
845
- SpanKind["Server"] = "server";
846
- SpanKind["Client"] = "client";
847
- SpanKind["Producer"] = "producer";
848
- SpanKind["Consumer"] = "consumer";
849
- })(SpanKind || (SpanKind = {}));
850
- // Note: loosely based on OpenTelemetry GenAI semantic conventions
851
- var SpanType;
852
- (function (SpanType) {
853
- // Latitude wrappers
854
- SpanType["Prompt"] = "prompt";
855
- SpanType["Chat"] = "chat";
856
- SpanType["External"] = "external";
857
- SpanType["UnresolvedExternal"] = "unresolved_external";
858
- // Added a HTTP span to capture raw HTTP requests and responses when running from Latitude
859
- SpanType["Http"] = "http";
860
- // Any known span from supported specifications will be grouped into one of these types
861
- SpanType["Completion"] = "completion";
862
- SpanType["Tool"] = "tool";
863
- SpanType["Embedding"] = "embedding";
864
- SpanType["Unknown"] = "unknown";
865
- })(SpanType || (SpanType = {}));
866
- [
867
- SpanType.Prompt,
868
- SpanType.External,
869
- SpanType.Chat,
870
- ];
871
- const SPAN_SPECIFICATIONS = {
872
- [SpanType.Prompt]: {
873
- name: 'Prompt',
874
- description: 'A prompt span',
875
- isGenAI: false,
876
- isHidden: false,
877
- },
878
- [SpanType.Chat]: {
879
- name: 'Chat',
880
- description: 'A chat continuation span',
881
- isGenAI: false,
882
- isHidden: false,
883
- },
884
- [SpanType.External]: {
885
- name: 'External',
886
- description: 'An external capture span',
887
- isGenAI: false,
888
- isHidden: false,
889
- },
890
- [SpanType.UnresolvedExternal]: {
891
- name: 'Unresolved External',
892
- description: 'An external span that needs path resolution before storage',
893
- isGenAI: false,
894
- isHidden: true,
895
- },
896
- [SpanType.Completion]: {
897
- name: 'Completion',
898
- description: 'A completion call',
899
- isGenAI: true,
900
- isHidden: false,
901
- },
902
- [SpanType.Embedding]: {
903
- name: 'Embedding',
904
- description: 'An embedding call',
905
- isGenAI: true,
906
- isHidden: false,
907
- },
908
- [SpanType.Tool]: {
909
- name: 'Tool',
910
- description: 'A tool call',
911
- isGenAI: true,
912
- isHidden: false,
913
- },
914
- [SpanType.Http]: {
915
- name: 'HTTP',
916
- description: 'An HTTP request',
917
- isGenAI: false,
918
- isHidden: true,
919
- },
920
- [SpanType.Unknown]: {
921
- name: 'Unknown',
922
- description: 'An unknown span',
923
- isGenAI: false,
924
- isHidden: true,
925
- },
245
+ var ScopedTracerProvider = class {
246
+ scope;
247
+ version;
248
+ provider;
249
+ constructor(scope, version, provider) {
250
+ this.scope = scope;
251
+ this.version = version;
252
+ this.provider = provider;
253
+ }
254
+ getTracer(_name, _version, options) {
255
+ return this.provider.getTracer(this.scope, this.version, options);
256
+ }
926
257
  };
927
- var SpanStatus;
928
- (function (SpanStatus) {
929
- SpanStatus["Unset"] = "unset";
930
- SpanStatus["Ok"] = "ok";
931
- SpanStatus["Error"] = "error";
932
- })(SpanStatus || (SpanStatus = {}));
933
- new Set([
934
- SpanType.Prompt,
935
- SpanType.Chat,
936
- SpanType.External,
937
- ]);
938
-
939
- // Note: Traces are unmaterialized but this context is used to propagate the trace
940
- // See www.w3.org/TR/trace-context and w3c.github.io/baggage
941
- zod.z.object({
942
- traceparent: zod.z.string(), // <version>-<trace-id>-<span-id>-<trace-flags>
943
- tracestate: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
944
- baggage: zod.z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]*
945
- });
946
-
947
- const ATTRIBUTES = {
948
- // Custom attributes added and used by Latitude spans (Prompt / External / Chat)
949
- LATITUDE: {
950
- type: 'latitude.type',
951
- documentUuid: 'latitude.document_uuid',
952
- promptPath: 'latitude.prompt_path',
953
- commitUuid: 'latitude.commit_uuid',
954
- documentLogUuid: 'latitude.document_log_uuid',
955
- projectId: 'latitude.project_id',
956
- experimentUuid: 'latitude.experiment_uuid',
957
- source: 'latitude.source',
958
- externalId: 'latitude.external_id',
959
- testDeploymentId: 'latitude.test_deployment_id',
960
- previousTraceId: 'latitude.previous_trace_id',
961
- // Custom additions to the GenAI semantic conventions (deprecated)
962
- request: {
963
- _root: 'gen_ai.request',
964
- configuration: 'gen_ai.request.configuration',
965
- template: 'gen_ai.request.template',
966
- parameters: 'gen_ai.request.parameters'},
967
- response: {
968
- _root: 'gen_ai.response'},
969
- usage: {
970
- promptTokens: 'gen_ai.usage.prompt_tokens',
971
- cachedTokens: 'gen_ai.usage.cached_tokens',
972
- reasoningTokens: 'gen_ai.usage.reasoning_tokens',
973
- completionTokens: 'gen_ai.usage.completion_tokens',
974
- },
975
- },
976
- // Official OpenTelemetry semantic conventions
977
- OPENTELEMETRY: {
978
- HTTP: {
979
- request: {
980
- url: 'http.request.url',
981
- body: 'http.request.body',
982
- header: 'http.request.header',
983
- method: semanticConventions.ATTR_HTTP_REQUEST_METHOD,
984
- },
985
- response: {
986
- body: 'http.response.body',
987
- header: 'http.response.header',
988
- statusCode: semanticConventions.ATTR_HTTP_RESPONSE_STATUS_CODE,
989
- },
990
- },
991
- // GenAI semantic conventions
992
- // https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/
993
- GEN_AI: {
994
- operation: incubating.ATTR_GEN_AI_OPERATION_NAME,
995
- response: {
996
- model: incubating.ATTR_GEN_AI_RESPONSE_MODEL,
997
- finishReasons: incubating.ATTR_GEN_AI_RESPONSE_FINISH_REASONS,
998
- },
999
- usage: {
1000
- inputTokens: incubating.ATTR_GEN_AI_USAGE_INPUT_TOKENS,
1001
- outputTokens: incubating.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
1002
- },
1003
- systemInstructions: 'gen_ai.system.instructions', // Contains the PARTS of the "system message"
1004
- tool: {
1005
- call: {
1006
- id: incubating.ATTR_GEN_AI_TOOL_CALL_ID,
1007
- arguments: 'gen_ai.tool.call.arguments'}},
1008
- input: {
1009
- messages: 'gen_ai.input.messages',
1010
- },
1011
- output: {
1012
- messages: 'gen_ai.output.messages',
1013
- },
1014
- _deprecated: {
1015
- system: incubating.ATTR_GEN_AI_SYSTEM,
1016
- tool: {
1017
- name: incubating.ATTR_GEN_AI_TOOL_NAME,
1018
- type: incubating.ATTR_GEN_AI_TOOL_TYPE,
1019
- result: {
1020
- value: 'gen_ai.tool.result.value',
1021
- isError: 'gen_ai.tool.result.is_error',
1022
- },
1023
- }},
1024
- },
1025
- }};
1026
- const VALUES = {
1027
- OPENTELEMETRY: {
1028
- GEN_AI: {
1029
- response: {
1030
- finishReasons: {
1031
- stop: 'stop',
1032
- toolCalls: 'tool_calls'},
1033
- },
1034
- tool: {
1035
- type: {
1036
- function: 'function',
1037
- },
1038
- }},
1039
- }};
1040
-
1041
- /* Note: Instrumentation scopes from all language SDKs */
1042
- const SCOPE_LATITUDE = 'so.latitude.instrumentation';
1043
- var InstrumentationScope;
1044
- (function (InstrumentationScope) {
1045
- InstrumentationScope["Manual"] = "manual";
1046
- InstrumentationScope["Latitude"] = "latitude";
1047
- InstrumentationScope["OpenAI"] = "openai";
1048
- InstrumentationScope["Anthropic"] = "anthropic";
1049
- InstrumentationScope["AzureOpenAI"] = "azure";
1050
- InstrumentationScope["VercelAI"] = "vercelai";
1051
- InstrumentationScope["VertexAI"] = "vertexai";
1052
- InstrumentationScope["AIPlatform"] = "aiplatform";
1053
- InstrumentationScope["MistralAI"] = "mistralai";
1054
- InstrumentationScope["Bedrock"] = "bedrock";
1055
- InstrumentationScope["Sagemaker"] = "sagemaker";
1056
- InstrumentationScope["TogetherAI"] = "togetherai";
1057
- InstrumentationScope["Replicate"] = "replicate";
1058
- InstrumentationScope["Groq"] = "groq";
1059
- InstrumentationScope["Cohere"] = "cohere";
1060
- InstrumentationScope["LiteLLM"] = "litellm";
1061
- InstrumentationScope["Langchain"] = "langchain";
1062
- InstrumentationScope["LlamaIndex"] = "llamaindex";
1063
- InstrumentationScope["DSPy"] = "dspy";
1064
- InstrumentationScope["Haystack"] = "haystack";
1065
- InstrumentationScope["Ollama"] = "ollama";
1066
- InstrumentationScope["Transformers"] = "transformers";
1067
- InstrumentationScope["AlephAlpha"] = "alephalpha";
1068
- })(InstrumentationScope || (InstrumentationScope = {}));
1069
- /* Note: Schemas for span ingestion following OpenTelemetry service request specification */
1070
- var Otlp;
1071
- (function (Otlp) {
1072
- Otlp.attributeValueSchema = zod.z.object({
1073
- stringValue: zod.z.string().optional(),
1074
- intValue: zod.z.number().optional(),
1075
- boolValue: zod.z.boolean().optional(),
1076
- arrayValue: zod.z
1077
- .object({
1078
- values: zod.z.array(zod.z.object({
1079
- stringValue: zod.z.string().optional(),
1080
- intValue: zod.z.number().optional(),
1081
- boolValue: zod.z.boolean().optional(),
1082
- })),
1083
- })
1084
- .optional(),
1085
- });
1086
- Otlp.attributeSchema = zod.z.object({
1087
- key: zod.z.string(),
1088
- value: Otlp.attributeValueSchema,
1089
- });
1090
- Otlp.eventSchema = zod.z.object({
1091
- name: zod.z.string(),
1092
- timeUnixNano: zod.z.string(),
1093
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
1094
- });
1095
- Otlp.linkSchema = zod.z.object({
1096
- traceId: zod.z.string(),
1097
- spanId: zod.z.string(),
1098
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
1099
- });
1100
- (function (StatusCode) {
1101
- StatusCode[StatusCode["Unset"] = 0] = "Unset";
1102
- StatusCode[StatusCode["Ok"] = 1] = "Ok";
1103
- StatusCode[StatusCode["Error"] = 2] = "Error";
1104
- })(Otlp.StatusCode || (Otlp.StatusCode = {}));
1105
- Otlp.statusSchema = zod.z.object({
1106
- code: zod.z.number(),
1107
- message: zod.z.string().optional(),
1108
- });
1109
- (function (SpanKind) {
1110
- SpanKind[SpanKind["Internal"] = 0] = "Internal";
1111
- SpanKind[SpanKind["Server"] = 1] = "Server";
1112
- SpanKind[SpanKind["Client"] = 2] = "Client";
1113
- SpanKind[SpanKind["Producer"] = 3] = "Producer";
1114
- SpanKind[SpanKind["Consumer"] = 4] = "Consumer";
1115
- })(Otlp.SpanKind || (Otlp.SpanKind = {}));
1116
- Otlp.spanSchema = zod.z.object({
1117
- traceId: zod.z.string(),
1118
- spanId: zod.z.string(),
1119
- parentSpanId: zod.z.string().optional(),
1120
- name: zod.z.string(),
1121
- kind: zod.z.number(),
1122
- startTimeUnixNano: zod.z.string(),
1123
- endTimeUnixNano: zod.z.string(),
1124
- status: Otlp.statusSchema.optional(),
1125
- events: zod.z.array(Otlp.eventSchema).optional(),
1126
- links: zod.z.array(Otlp.linkSchema).optional(),
1127
- attributes: zod.z.array(Otlp.attributeSchema).optional(),
1128
- });
1129
- Otlp.scopeSchema = zod.z.object({
1130
- name: zod.z.string(),
1131
- version: zod.z.string().optional(),
1132
- });
1133
- Otlp.scopeSpanSchema = zod.z.object({
1134
- scope: Otlp.scopeSchema,
1135
- spans: zod.z.array(Otlp.spanSchema),
1136
- });
1137
- Otlp.resourceSchema = zod.z.object({
1138
- attributes: zod.z.array(Otlp.attributeSchema),
1139
- });
1140
- Otlp.resourceSpanSchema = zod.z.object({
1141
- resource: Otlp.resourceSchema,
1142
- scopeSpans: zod.z.array(Otlp.scopeSpanSchema),
1143
- });
1144
- Otlp.serviceRequestSchema = zod.z.object({
1145
- resourceSpans: zod.z.array(Otlp.resourceSpanSchema),
258
+ var LifecycleManager = class {
259
+ nodeProvider;
260
+ exporter;
261
+ constructor(nodeProvider, exporter) {
262
+ this.nodeProvider = nodeProvider;
263
+ this.exporter = exporter;
264
+ }
265
+ async flush() {
266
+ await this.nodeProvider.forceFlush();
267
+ await this.exporter.forceFlush?.();
268
+ }
269
+ async shutdown() {
270
+ await this.flush();
271
+ await this.nodeProvider.shutdown();
272
+ await this.exporter.shutdown?.();
273
+ }
274
+ };
275
+ var DEFAULT_SPAN_EXPORTER = (apiKey, projectSlug) => new import_exporter_trace_otlp_http.OTLPTraceExporter({
276
+ url: TRACES_URL,
277
+ headers: {
278
+ Authorization: `Bearer ${apiKey}`,
279
+ "Content-Type": "application/json",
280
+ "X-Latitude-Project": projectSlug
281
+ },
282
+ timeoutMillis: 30 * 1e3
283
+ });
284
+ var Instrumentation = /* @__PURE__ */ ((Instrumentation2) => {
285
+ Instrumentation2["Anthropic"] = "anthropic";
286
+ Instrumentation2["AIPlatform"] = "aiplatform";
287
+ Instrumentation2["Bedrock"] = "bedrock";
288
+ Instrumentation2["Cohere"] = "cohere";
289
+ Instrumentation2["Langchain"] = "langchain";
290
+ Instrumentation2["LlamaIndex"] = "llamaindex";
291
+ Instrumentation2["Manual"] = "manual";
292
+ Instrumentation2["OpenAI"] = "openai";
293
+ Instrumentation2["TogetherAI"] = "togetherai";
294
+ Instrumentation2["VertexAI"] = "vertexai";
295
+ return Instrumentation2;
296
+ })(Instrumentation || {});
297
+ var LatitudeTelemetry = class {
298
+ options;
299
+ nodeProvider;
300
+ instrumentationsList;
301
+ /** OpenTelemetry tracer for creating custom spans. */
302
+ tracer;
303
+ context;
304
+ instrumentation;
305
+ lifecycle;
306
+ constructor(apiKey, projectSlug, options) {
307
+ this.options = options || {};
308
+ if (!this.options.exporter) {
309
+ this.options.exporter = DEFAULT_SPAN_EXPORTER(apiKey, projectSlug);
310
+ }
311
+ import_api2.context.setGlobalContextManager(new import_context_async_hooks.AsyncLocalStorageContextManager().enable());
312
+ import_api2.propagation.setGlobalPropagator(
313
+ new import_core.CompositePropagator({
314
+ propagators: [...this.options.propagators || [], new import_core.W3CTraceContextPropagator(), new import_core.W3CBaggagePropagator()]
315
+ })
316
+ );
317
+ const spanProcessors = [
318
+ // Must run before the exporter span processors
319
+ new import_baggage_span_processor.BaggageSpanProcessor(import_baggage_span_processor.ALLOW_ALL_BAGGAGE_KEYS),
320
+ ...this.options.processors ?? [DEFAULT_REDACT_SPAN_PROCESSOR()],
321
+ this.options.disableBatch ? new import_sdk_trace_node.SimpleSpanProcessor(this.options.exporter) : new import_sdk_trace_node.BatchSpanProcessor(this.options.exporter)
322
+ ];
323
+ this.nodeProvider = new import_sdk_trace_node.NodeTracerProvider({
324
+ resource: new import_resources.Resource({ [import_semantic_conventions.ATTR_SERVICE_NAME]: this.options.serviceName || SERVICE_NAME }),
325
+ spanProcessors
1146
326
  });
1147
- })(Otlp || (Otlp = {}));
1148
-
1149
- const MAX_SIMULATION_TURNS = 10;
1150
- const globalGoalSourceSchema = zod.z.object({
1151
- type: zod.z.literal('global'),
1152
- value: zod.z.string(),
1153
- });
1154
- const columnGoalSourceSchema = zod.z.object({
1155
- type: zod.z.literal('column'),
1156
- columnIndex: zod.z.number(),
1157
- });
1158
- const simulatedUserGoalSourceSchema = zod.z.discriminatedUnion('type', [
1159
- globalGoalSourceSchema,
1160
- columnGoalSourceSchema,
1161
- ]);
1162
- const SimulationSettingsSchema = zod.z.object({
1163
- simulateToolResponses: zod.z.boolean().optional(),
1164
- simulatedTools: zod.z.array(zod.z.string()).optional(), // Empty array means all tools are simulated (if simulateToolResponses is true).
1165
- toolSimulationInstructions: zod.z.string().optional(), // A prompt used to guide and generate the simulation result
1166
- maxTurns: zod.z.number().min(1).max(MAX_SIMULATION_TURNS).optional(), // The maximum number of turns to simulate. Default is 1, and any greater value will add a new user message to the simulated conversation.
1167
- simulatedUserGoal: zod.z.string().optional(), // Deprecated: Use simulatedUserGoalSource instead. Kept for backward compatibility.
1168
- simulatedUserGoalSource: simulatedUserGoalSourceSchema.optional(), // The source for the simulated user goal (global text or dataset column).
1169
- });
1170
-
1171
- var OptimizationEngine;
1172
- (function (OptimizationEngine) {
1173
- OptimizationEngine["Identity"] = "identity";
1174
- OptimizationEngine["Gepa"] = "gepa";
1175
- })(OptimizationEngine || (OptimizationEngine = {}));
1176
- const OptimizationBudgetSchema = zod.z.object({
1177
- time: zod.z.number().min(0).optional(),
1178
- tokens: zod.z.number().min(0).optional(),
1179
- });
1180
- zod.z.object({
1181
- dataset: zod.z
1182
- .object({
1183
- target: zod.z.number().min(0).optional(), // Note: number of rows to curate when not provided by the user
1184
- label: zod.z.string().optional(), // Note: expected output column when using a labeled evaluation
1185
- })
1186
- .optional(),
1187
- parameters: zod.z
1188
- .record(zod.z.string(), zod.z.object({
1189
- column: zod.z.string().optional(), // Note: corresponding column in the user-provided trainset and testset
1190
- isPii: zod.z.boolean().optional(),
1191
- }))
1192
- .optional(),
1193
- simulation: SimulationSettingsSchema.optional(),
1194
- scope: zod.z
1195
- .object({
1196
- configuration: zod.z.boolean().optional(),
1197
- instructions: zod.z.boolean().optional(),
1198
- })
1199
- .optional(),
1200
- budget: OptimizationBudgetSchema.optional(),
1201
- });
1202
-
1203
- // TODO(tracing): deprecated
1204
- const HEAD_COMMIT = 'live';
1205
- var Providers;
1206
- (function (Providers) {
1207
- Providers["OpenAI"] = "openai";
1208
- Providers["Anthropic"] = "anthropic";
1209
- Providers["Groq"] = "groq";
1210
- Providers["Mistral"] = "mistral";
1211
- Providers["Azure"] = "azure";
1212
- Providers["Google"] = "google";
1213
- Providers["GoogleVertex"] = "google_vertex";
1214
- Providers["AnthropicVertex"] = "anthropic_vertex";
1215
- Providers["Custom"] = "custom";
1216
- Providers["XAI"] = "xai";
1217
- Providers["AmazonBedrock"] = "amazon_bedrock";
1218
- Providers["DeepSeek"] = "deepseek";
1219
- Providers["Perplexity"] = "perplexity";
1220
- })(Providers || (Providers = {}));
1221
- var DocumentType;
1222
- (function (DocumentType) {
1223
- DocumentType["Prompt"] = "prompt";
1224
- DocumentType["Agent"] = "agent";
1225
- })(DocumentType || (DocumentType = {}));
1226
- var DocumentTriggerType;
1227
- (function (DocumentTriggerType) {
1228
- DocumentTriggerType["Email"] = "email";
1229
- DocumentTriggerType["Scheduled"] = "scheduled";
1230
- DocumentTriggerType["Integration"] = "integration";
1231
- })(DocumentTriggerType || (DocumentTriggerType = {}));
1232
- var DocumentTriggerStatus;
1233
- (function (DocumentTriggerStatus) {
1234
- DocumentTriggerStatus["Pending"] = "pending";
1235
- DocumentTriggerStatus["Deployed"] = "deployed";
1236
- DocumentTriggerStatus["Deprecated"] = "deprecated";
1237
- })(DocumentTriggerStatus || (DocumentTriggerStatus = {}));
1238
- var DocumentTriggerParameters;
1239
- (function (DocumentTriggerParameters) {
1240
- DocumentTriggerParameters["SenderEmail"] = "senderEmail";
1241
- DocumentTriggerParameters["SenderName"] = "senderName";
1242
- DocumentTriggerParameters["Subject"] = "subject";
1243
- DocumentTriggerParameters["Body"] = "body";
1244
- DocumentTriggerParameters["Attachments"] = "attachments";
1245
- })(DocumentTriggerParameters || (DocumentTriggerParameters = {}));
1246
- const DOCUMENT_PATH_REGEXP = /^([\w-]+\/)*([\w-.])+$/;
1247
-
1248
- const translator = new rosettaAi.Translator({
1249
- filterEmptyMessages: true,
1250
- providerMetadata: 'preserve',
1251
- });
1252
- class ManualInstrumentation {
1253
- enabled;
1254
- tracer;
1255
- options;
1256
- constructor(tracer, options) {
1257
- this.enabled = false;
1258
- this.tracer = tracer;
1259
- this.options = options ?? {};
1260
- }
1261
- isEnabled() {
1262
- return this.enabled;
1263
- }
1264
- enable() {
1265
- this.enabled = true;
1266
- }
1267
- disable() {
1268
- this.enabled = false;
1269
- }
1270
- resume(ctx) {
1271
- const parts = ctx.traceparent.split('-');
1272
- if (parts.length !== 4) {
1273
- return otel__namespace.ROOT_CONTEXT;
1274
- }
1275
- const [, traceId, spanId, flags] = parts;
1276
- if (!traceId || !spanId) {
1277
- return otel__namespace.ROOT_CONTEXT;
1278
- }
1279
- const spanContext = {
1280
- traceId,
1281
- spanId,
1282
- traceFlags: parseInt(flags ?? '01', 16),
1283
- isRemote: true,
1284
- };
1285
- let context = otel.trace.setSpanContext(otel__namespace.ROOT_CONTEXT, spanContext);
1286
- if (ctx.baggage) {
1287
- const baggageEntries = {};
1288
- for (const pair of ctx.baggage.split(',')) {
1289
- const [key, value] = pair.split('=');
1290
- if (key && value) {
1291
- baggageEntries[key] = { value: decodeURIComponent(value) };
1292
- }
1293
- }
1294
- const baggage = otel.propagation.createBaggage(baggageEntries);
1295
- context = otel.propagation.setBaggage(context, baggage);
1296
- }
1297
- return context;
1298
- }
1299
- error(span, error, options) {
1300
- options = options || {};
1301
- span.recordException(error);
1302
- span.setAttributes(options.attributes || {});
1303
- span.setStatus({
1304
- code: otel__namespace.SpanStatusCode.ERROR,
1305
- message: error.message || undefined,
1306
- });
1307
- span.end();
1308
- }
1309
- span(ctx, name, type, options) {
1310
- if (!this.enabled) {
1311
- return {
1312
- context: ctx,
1313
- end: (_options) => { },
1314
- fail: (_error, _options) => { },
1315
- };
1316
- }
1317
- const start = options || {};
1318
- let operation = undefined;
1319
- if (SPAN_SPECIFICATIONS[type].isGenAI) {
1320
- operation = type;
1321
- }
1322
- const span = this.tracer.startSpan(name, {
1323
- attributes: {
1324
- [ATTRIBUTES.LATITUDE.type]: type,
1325
- ...(operation && {
1326
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.operation]: operation,
1327
- }),
1328
- ...(start.attributes || {}),
1329
- },
1330
- kind: otel__namespace.SpanKind.CLIENT,
1331
- }, ctx);
1332
- const newCtx = otel.trace.setSpan(ctx, span);
1333
- return {
1334
- context: newCtx,
1335
- end: (options) => {
1336
- const end = options || {};
1337
- span.setAttributes(end.attributes || {});
1338
- span.setStatus({ code: otel__namespace.SpanStatusCode.OK });
1339
- span.end();
1340
- },
1341
- fail: (error, options) => {
1342
- this.error(span, error, options);
1343
- },
1344
- };
1345
- }
1346
- unknown(ctx, options) {
1347
- return this.span(ctx, options?.name || SPAN_SPECIFICATIONS[SpanType.Unknown].name, SpanType.Unknown, options);
1348
- }
1349
- tool(ctx, options) {
1350
- const start = options;
1351
- let jsonArguments = '';
1352
- try {
1353
- jsonArguments = JSON.stringify(start.call.arguments);
1354
- }
1355
- catch (_error) {
1356
- jsonArguments = '{}';
1357
- }
1358
- const span = this.span(ctx, start.name, SpanType.Tool, {
1359
- attributes: {
1360
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI._deprecated.tool.name]: start.name,
1361
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI._deprecated.tool.type]: VALUES.OPENTELEMETRY.GEN_AI.tool.type.function,
1362
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.tool.call.id]: start.call.id,
1363
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.tool.call.arguments]: jsonArguments,
1364
- ...(start.attributes || {}),
1365
- },
1366
- });
1367
- return {
1368
- ...span,
1369
- end: (options) => {
1370
- const end = options;
1371
- let stringResult = '';
1372
- if (typeof end.result.value !== 'string') {
1373
- try {
1374
- stringResult = JSON.stringify(end.result.value);
1375
- }
1376
- catch (_error) {
1377
- stringResult = '{}';
1378
- }
1379
- }
1380
- else {
1381
- stringResult = end.result.value;
1382
- }
1383
- span.end({
1384
- attributes: {
1385
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI._deprecated.tool.result.value]: stringResult,
1386
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI._deprecated.tool.result.isError]: end.result.isError,
1387
- ...(end.attributes || {}),
1388
- },
1389
- });
1390
- },
1391
- };
1392
- }
1393
- attribifyConfiguration(direction, configuration) {
1394
- const prefix = direction === 'input'
1395
- ? ATTRIBUTES.LATITUDE.request._root
1396
- : ATTRIBUTES.LATITUDE.response._root;
1397
- const attributes = {};
1398
- for (const key in configuration) {
1399
- const field = toSnakeCase(key);
1400
- let value = configuration[key];
1401
- if (value === null || value === undefined)
1402
- continue;
1403
- if (typeof value === 'object' && !Array.isArray(value)) {
1404
- try {
1405
- value = JSON.stringify(value);
1406
- }
1407
- catch (_error) {
1408
- value = '{}';
1409
- }
1410
- }
1411
- attributes[`${prefix}.${field}`] = value;
1412
- }
1413
- return attributes;
1414
- }
1415
- completion(ctx, options) {
1416
- const start = options;
1417
- const configuration = {
1418
- ...(start.configuration ?? {}),
1419
- model: start.model,
1420
- };
1421
- let jsonConfiguration = '';
1422
- try {
1423
- jsonConfiguration = JSON.stringify(configuration);
1424
- }
1425
- catch (_error) {
1426
- jsonConfiguration = '{}';
1427
- }
1428
- const attrConfiguration = this.attribifyConfiguration('input', configuration);
1429
- const input = start.input ?? [];
1430
- let jsonSystem = '';
1431
- let jsonInput = '';
1432
- try {
1433
- const translated = translator.translate(input, {
1434
- from: this.options.provider,
1435
- to: rosettaAi.Provider.GenAI,
1436
- direction: 'input',
1437
- });
1438
- jsonSystem = JSON.stringify(translated.system ?? []);
1439
- jsonInput = JSON.stringify(translated.messages ?? []);
1440
- }
1441
- catch (_error) {
1442
- jsonSystem = '[]';
1443
- jsonInput = '[]';
1444
- }
1445
- const span = this.span(ctx, start.name || `${start.provider} / ${start.model}`, SpanType.Completion, {
1446
- attributes: {
1447
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI._deprecated.system]: start.provider,
1448
- [ATTRIBUTES.LATITUDE.request.configuration]: jsonConfiguration,
1449
- ...attrConfiguration,
1450
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.systemInstructions]: jsonSystem,
1451
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.input.messages]: jsonInput,
1452
- ...(start.attributes || {}),
1453
- [ATTRIBUTES.LATITUDE.commitUuid]: start.versionUuid,
1454
- [ATTRIBUTES.LATITUDE.documentUuid]: start.promptUuid,
1455
- [ATTRIBUTES.LATITUDE.experimentUuid]: start.experimentUuid,
1456
- },
1457
- });
1458
- return {
1459
- ...span,
1460
- end: (options) => {
1461
- const end = options ?? {};
1462
- const output = end.output ?? [];
1463
- let jsonOutput = '';
1464
- try {
1465
- const translated = translator.translate(output, {
1466
- from: this.options.provider,
1467
- to: rosettaAi.Provider.GenAI,
1468
- direction: 'output',
1469
- });
1470
- jsonOutput = JSON.stringify(translated.messages ?? []);
1471
- }
1472
- catch (_error) {
1473
- jsonOutput = '[]';
1474
- }
1475
- const tokens = {
1476
- prompt: end.tokens?.prompt ?? 0,
1477
- cached: end.tokens?.cached ?? 0,
1478
- reasoning: end.tokens?.reasoning ?? 0,
1479
- completion: end.tokens?.completion ?? 0,
1480
- };
1481
- const inputTokens = tokens.prompt + tokens.cached;
1482
- const outputTokens = tokens.reasoning + tokens.completion;
1483
- const finishReason = end.finishReason ?? '';
1484
- span.end({
1485
- attributes: {
1486
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.output.messages]: jsonOutput,
1487
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.usage.inputTokens]: inputTokens,
1488
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.usage.outputTokens]: outputTokens,
1489
- [ATTRIBUTES.LATITUDE.usage.promptTokens]: tokens.prompt,
1490
- [ATTRIBUTES.LATITUDE.usage.cachedTokens]: tokens.cached,
1491
- [ATTRIBUTES.LATITUDE.usage.reasoningTokens]: tokens.reasoning,
1492
- [ATTRIBUTES.LATITUDE.usage.completionTokens]: tokens.completion,
1493
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.response.model]: start.model,
1494
- [ATTRIBUTES.OPENTELEMETRY.GEN_AI.response.finishReasons]: [
1495
- finishReason,
1496
- ],
1497
- ...(end.attributes || {}),
1498
- },
1499
- });
1500
- },
1501
- };
1502
- }
1503
- embedding(ctx, options) {
1504
- return this.span(ctx, options?.name || SPAN_SPECIFICATIONS[SpanType.Embedding].name, SpanType.Embedding, options);
1505
- }
1506
- attribifyHeaders(direction, headers) {
1507
- const prefix = direction === 'request'
1508
- ? ATTRIBUTES.OPENTELEMETRY.HTTP.request.header
1509
- : ATTRIBUTES.OPENTELEMETRY.HTTP.response.header;
1510
- const attributes = {};
1511
- for (const key in headers) {
1512
- const field = toKebabCase(key);
1513
- const value = headers[key];
1514
- if (value === null || value === undefined)
1515
- continue;
1516
- attributes[`${prefix}.${field}`] = value;
1517
- }
1518
- return attributes;
1519
- }
1520
- http(ctx, options) {
1521
- const start = options;
1522
- const method = start.request.method.toUpperCase();
1523
- // Note: do not serialize headers as a single attribute because fields won't be redacted
1524
- const attrHeaders = this.attribifyHeaders('request', start.request.headers);
1525
- let finalBody = '';
1526
- if (typeof start.request.body === 'string') {
1527
- finalBody = start.request.body;
1528
- }
1529
- else {
1530
- try {
1531
- finalBody = JSON.stringify(start.request.body);
1532
- }
1533
- catch (_error) {
1534
- finalBody = '{}';
1535
- }
1536
- }
1537
- const span = this.span(ctx, start.name || `${method} ${start.request.url}`, SpanType.Http, {
1538
- attributes: {
1539
- [ATTRIBUTES.OPENTELEMETRY.HTTP.request.method]: method,
1540
- [ATTRIBUTES.OPENTELEMETRY.HTTP.request.url]: start.request.url,
1541
- ...attrHeaders,
1542
- [ATTRIBUTES.OPENTELEMETRY.HTTP.request.body]: finalBody,
1543
- ...(start.attributes || {}),
1544
- },
1545
- });
1546
- return {
1547
- ...span,
1548
- end: (options) => {
1549
- const end = options;
1550
- // Note: do not serialize headers as a single attribute because fields won't be redacted
1551
- const attrHeaders = this.attribifyHeaders('response', end.response.headers);
1552
- let finalBody = '';
1553
- if (typeof end.response.body === 'string') {
1554
- finalBody = end.response.body;
1555
- }
1556
- else {
1557
- try {
1558
- finalBody = JSON.stringify(end.response.body);
1559
- }
1560
- catch (_error) {
1561
- finalBody = '{}';
1562
- }
1563
- }
1564
- span.end({
1565
- attributes: {
1566
- [ATTRIBUTES.OPENTELEMETRY.HTTP.response.statusCode]: end.response.status,
1567
- ...attrHeaders,
1568
- [ATTRIBUTES.OPENTELEMETRY.HTTP.response.body]: finalBody,
1569
- ...(end.attributes || {}),
1570
- },
1571
- });
1572
- },
1573
- };
1574
- }
1575
- prompt(ctx, { documentLogUuid, versionUuid, promptUuid, projectId, experimentUuid, testDeploymentId, externalId, template, parameters, name, source, ...rest }) {
1576
- let jsonParameters = '';
1577
- try {
1578
- jsonParameters = JSON.stringify(parameters || {});
1579
- }
1580
- catch (_error) {
1581
- jsonParameters = '{}';
1582
- }
1583
- const attributes = {
1584
- [ATTRIBUTES.LATITUDE.request.template]: template,
1585
- [ATTRIBUTES.LATITUDE.request.parameters]: jsonParameters,
1586
- [ATTRIBUTES.LATITUDE.commitUuid]: versionUuid || HEAD_COMMIT,
1587
- [ATTRIBUTES.LATITUDE.documentUuid]: promptUuid,
1588
- [ATTRIBUTES.LATITUDE.projectId]: projectId,
1589
- [ATTRIBUTES.LATITUDE.documentLogUuid]: documentLogUuid,
1590
- ...(experimentUuid && {
1591
- [ATTRIBUTES.LATITUDE.experimentUuid]: experimentUuid,
1592
- }),
1593
- ...(testDeploymentId && {
1594
- [ATTRIBUTES.LATITUDE.testDeploymentId]: testDeploymentId,
1595
- }),
1596
- ...(externalId && { [ATTRIBUTES.LATITUDE.externalId]: externalId }),
1597
- ...(source && { [ATTRIBUTES.LATITUDE.source]: source }),
1598
- ...(rest.attributes || {}),
1599
- };
1600
- return this.span(ctx, name || `prompt-${promptUuid}`, SpanType.Prompt, {
1601
- attributes,
1602
- });
1603
- }
1604
- chat(ctx, { documentLogUuid, previousTraceId, source, name, versionUuid, promptUuid, ...rest }) {
1605
- const attributes = {
1606
- [ATTRIBUTES.LATITUDE.documentLogUuid]: documentLogUuid,
1607
- [ATTRIBUTES.LATITUDE.previousTraceId]: previousTraceId,
1608
- ...(versionUuid && { [ATTRIBUTES.LATITUDE.commitUuid]: versionUuid }),
1609
- ...(promptUuid && { [ATTRIBUTES.LATITUDE.documentUuid]: promptUuid }),
1610
- ...(source && { [ATTRIBUTES.LATITUDE.source]: source }),
1611
- ...(rest.attributes || {}),
1612
- };
1613
- return this.span(ctx, name || `chat-${documentLogUuid}`, SpanType.Chat, {
1614
- attributes,
1615
- });
1616
- }
1617
- external(ctx, { promptUuid, documentLogUuid, source, versionUuid, externalId, name, ...rest }) {
1618
- const attributes = {
1619
- [ATTRIBUTES.LATITUDE.documentUuid]: promptUuid,
1620
- [ATTRIBUTES.LATITUDE.documentLogUuid]: documentLogUuid,
1621
- [ATTRIBUTES.LATITUDE.source]: source ?? LogSources.API,
1622
- ...(versionUuid && { [ATTRIBUTES.LATITUDE.commitUuid]: versionUuid }),
1623
- ...(externalId && { [ATTRIBUTES.LATITUDE.externalId]: externalId }),
1624
- ...(rest.attributes || {}),
1625
- };
1626
- return this.span(ctx, name || `external-${promptUuid}`, SpanType.External, {
1627
- attributes,
1628
- });
1629
- }
1630
- // TODO(tracing): deprecate
1631
- unresolvedExternal(ctx, { path, projectId, versionUuid, conversationUuid, name, ...rest }) {
1632
- const attributes = {
1633
- [ATTRIBUTES.LATITUDE.promptPath]: path,
1634
- [ATTRIBUTES.LATITUDE.projectId]: projectId,
1635
- ...(versionUuid && { [ATTRIBUTES.LATITUDE.commitUuid]: versionUuid }),
1636
- ...(conversationUuid && {
1637
- [ATTRIBUTES.LATITUDE.documentLogUuid]: conversationUuid,
1638
- }),
1639
- ...(rest.attributes || {}),
1640
- };
1641
- return this.span(ctx, name || `capture-${path}`, SpanType.UnresolvedExternal, { attributes });
1642
- }
1643
- }
1644
-
1645
- class LatitudeInstrumentation {
1646
- options;
1647
- manualTelemetry;
1648
- constructor(tracer, options) {
1649
- this.manualTelemetry = new ManualInstrumentation(tracer);
1650
- this.options = options;
1651
- }
1652
- isEnabled() {
1653
- return this.manualTelemetry.isEnabled();
1654
- }
1655
- enable() {
1656
- this.manualTelemetry.enable();
1657
- this.options.module.instrument(this);
1658
- }
1659
- disable() {
1660
- this.manualTelemetry.disable();
1661
- this.options.module.uninstrument();
1662
- }
1663
- countTokens(messages) {
1664
- let length = 0;
1665
- for (const message of messages) {
1666
- if (!('content' in message))
1667
- continue;
1668
- if (typeof message.content === 'string') {
1669
- length += message.content.length;
1670
- }
1671
- else if (Array.isArray(message.content)) {
1672
- for (const content of message.content) {
1673
- if (content.type === 'text') {
1674
- length += content.text.length;
1675
- }
1676
- }
1677
- }
1678
- }
1679
- // Note: this is an estimation to not bundle a tokenizer
1680
- return Math.ceil(length / 4);
1681
- }
1682
- async wrapRenderChain(fn, ...args) {
1683
- const { prompt, parameters } = args[0];
1684
- const $prompt = this.manualTelemetry.prompt(otel.context.active(), {
1685
- documentLogUuid: uuid.v4(),
1686
- versionUuid: prompt.versionUuid,
1687
- promptUuid: prompt.uuid,
1688
- template: prompt.content,
1689
- parameters: parameters,
1690
- });
1691
- let result;
1692
- try {
1693
- result = await otel.context.with($prompt.context, async () => await fn(...args));
1694
- }
1695
- catch (error) {
1696
- $prompt.fail(error);
1697
- throw error;
1698
- }
1699
- $prompt.end();
1700
- return result;
1701
- }
1702
- async wrapRenderCompletion(fn, ...args) {
1703
- if (!this.options.completions) {
1704
- return await fn(...args);
1705
- }
1706
- const { provider, config, messages } = args[0];
1707
- const model = config.model || 'unknown';
1708
- const $completion = this.manualTelemetry.completion(otel.context.active(), {
1709
- name: `${provider} / ${model}`,
1710
- provider: provider,
1711
- model: model,
1712
- configuration: config,
1713
- input: messages,
1714
- });
1715
- let result;
1716
- try {
1717
- result = await otel.context.with($completion.context, async () => await fn(...args));
1718
- }
1719
- catch (error) {
1720
- $completion.fail(error);
1721
- throw error;
1722
- }
1723
- // Note: enhance, this is just an estimation
1724
- const promptTokens = this.countTokens(messages);
1725
- const completionTokens = this.countTokens(result.messages);
1726
- $completion.end({
1727
- output: result.messages,
1728
- tokens: {
1729
- prompt: promptTokens,
1730
- cached: 0,
1731
- reasoning: 0,
1732
- completion: completionTokens,
1733
- },
1734
- finishReason: result.toolRequests.length > 0
1735
- ? VALUES.OPENTELEMETRY.GEN_AI.response.finishReasons.toolCalls
1736
- : VALUES.OPENTELEMETRY.GEN_AI.response.finishReasons.stop,
1737
- });
1738
- return result;
1739
- }
1740
- async wrapRenderTool(fn, ...args) {
1741
- const { toolRequest } = args[0];
1742
- const $tool = this.manualTelemetry.tool(otel.context.active(), {
1743
- name: toolRequest.toolName,
1744
- call: {
1745
- id: toolRequest.toolCallId,
1746
- arguments: toolRequest.toolArguments,
1747
- },
1748
- });
1749
- let result;
1750
- try {
1751
- result = await otel.context.with($tool.context, async () => await fn(...args));
1752
- }
1753
- catch (error) {
1754
- $tool.fail(error);
1755
- throw error;
1756
- }
1757
- $tool.end({
1758
- result: {
1759
- value: result.result,
1760
- isError: result.isError,
1761
- },
1762
- });
1763
- return result;
1764
- }
1765
- }
1766
-
1767
- var LatitudeErrorCodes;
1768
- (function (LatitudeErrorCodes) {
1769
- LatitudeErrorCodes["UnexpectedError"] = "UnexpectedError";
1770
- LatitudeErrorCodes["OverloadedError"] = "OverloadedError";
1771
- LatitudeErrorCodes["RateLimitError"] = "RateLimitError";
1772
- LatitudeErrorCodes["UnauthorizedError"] = "UnauthorizedError";
1773
- LatitudeErrorCodes["ForbiddenError"] = "ForbiddenError";
1774
- LatitudeErrorCodes["BadRequestError"] = "BadRequestError";
1775
- LatitudeErrorCodes["NotFoundError"] = "NotFoundError";
1776
- LatitudeErrorCodes["ConflictError"] = "ConflictError";
1777
- LatitudeErrorCodes["UnprocessableEntityError"] = "UnprocessableEntityError";
1778
- LatitudeErrorCodes["NotImplementedError"] = "NotImplementedError";
1779
- LatitudeErrorCodes["PaymentRequiredError"] = "PaymentRequiredError";
1780
- LatitudeErrorCodes["AbortedError"] = "AbortedError";
1781
- LatitudeErrorCodes["BillingError"] = "BillingError";
1782
- })(LatitudeErrorCodes || (LatitudeErrorCodes = {}));
1783
- // NOTE: If you add a new error code, please add it to the pg enum in models/runErrors.ts
1784
- var RunErrorCodes;
1785
- (function (RunErrorCodes) {
1786
- RunErrorCodes["AIProviderConfigError"] = "ai_provider_config_error";
1787
- RunErrorCodes["AIRunError"] = "ai_run_error";
1788
- RunErrorCodes["ChainCompileError"] = "chain_compile_error";
1789
- RunErrorCodes["DefaultProviderExceededQuota"] = "default_provider_exceeded_quota_error";
1790
- RunErrorCodes["DefaultProviderInvalidModel"] = "default_provider_invalid_model_error";
1791
- RunErrorCodes["DocumentConfigError"] = "document_config_error";
1792
- RunErrorCodes["ErrorGeneratingMockToolResult"] = "error_generating_mock_tool_result";
1793
- RunErrorCodes["FailedToWakeUpIntegrationError"] = "failed_to_wake_up_integration_error";
1794
- RunErrorCodes["InvalidResponseFormatError"] = "invalid_response_format_error";
1795
- RunErrorCodes["MaxStepCountExceededError"] = "max_step_count_exceeded_error";
1796
- RunErrorCodes["MissingProvider"] = "missing_provider_error";
1797
- RunErrorCodes["RateLimit"] = "rate_limit_error";
1798
- RunErrorCodes["Unknown"] = "unknown_error";
1799
- RunErrorCodes["UnsupportedProviderResponseTypeError"] = "unsupported_provider_response_type_error";
1800
- RunErrorCodes["PaymentRequiredError"] = "payment_required_error";
1801
- RunErrorCodes["AbortError"] = "abort_error";
1802
- // DEPRECATED, but do not delete
1803
- RunErrorCodes["EvaluationRunMissingProviderLogError"] = "ev_run_missing_provider_log_error";
1804
- RunErrorCodes["EvaluationRunMissingWorkspaceError"] = "ev_run_missing_workspace_error";
1805
- RunErrorCodes["EvaluationRunResponseJsonFormatError"] = "ev_run_response_json_format_error";
1806
- RunErrorCodes["EvaluationRunUnsupportedResultTypeError"] = "ev_run_unsupported_result_type_error";
1807
- })(RunErrorCodes || (RunErrorCodes = {}));
1808
- var ApiErrorCodes;
1809
- (function (ApiErrorCodes) {
1810
- ApiErrorCodes["HTTPException"] = "http_exception";
1811
- ApiErrorCodes["InternalServerError"] = "internal_server_error";
1812
- })(ApiErrorCodes || (ApiErrorCodes = {}));
1813
-
1814
- class LatitudeError extends Error {
1815
- statusCode = 500;
1816
- name = LatitudeErrorCodes.UnexpectedError;
1817
- headers = {};
1818
- details;
1819
- constructor(message, details, status, name) {
1820
- super(message);
1821
- this.details = details ?? {};
1822
- this.statusCode = status ?? this.statusCode;
1823
- this.name = name ?? this.constructor.name;
1824
- }
1825
- serialize() {
1826
- return {
1827
- name: this.name,
1828
- code: this.name,
1829
- status: this.statusCode,
1830
- message: this.message,
1831
- details: this.details,
1832
- };
1833
- }
1834
- static deserialize(json) {
1835
- return new LatitudeError(json.message, json.details, json.status, json.name);
1836
- }
1837
- }
1838
- class BadRequestError extends LatitudeError {
1839
- statusCode = 400;
1840
- name = LatitudeErrorCodes.BadRequestError;
1841
- }
1842
-
1843
- const TRACES_URL = `${env.GATEWAY_BASE_URL}/api/v3/traces`;
1844
- const SERVICE_NAME = process.env.npm_package_name || 'unknown';
1845
- const SCOPE_VERSION = process.env.npm_package_version || 'unknown';
1846
- const BACKGROUND = () => otel__namespace.ROOT_CONTEXT;
1847
- class SpanFactory {
1848
- telemetry;
1849
- constructor(telemetry) {
1850
- this.telemetry = telemetry;
1851
- }
1852
- span(options, ctx) {
1853
- return this.telemetry.unknown(ctx ?? otel.context.active(), options);
1854
- }
1855
- tool(options, ctx) {
1856
- return this.telemetry.tool(ctx ?? otel.context.active(), options);
1857
- }
1858
- completion(options, ctx) {
1859
- return this.telemetry.completion(ctx ?? otel.context.active(), options);
1860
- }
1861
- embedding(options, ctx) {
1862
- return this.telemetry.embedding(ctx ?? otel.context.active(), options);
1863
- }
1864
- http(options, ctx) {
1865
- return this.telemetry.http(ctx ?? otel.context.active(), options);
1866
- }
1867
- prompt(options, ctx) {
1868
- return this.telemetry.prompt(ctx ?? otel.context.active(), options);
1869
- }
1870
- chat(options, ctx) {
1871
- return this.telemetry.chat(ctx ?? otel.context.active(), options);
1872
- }
1873
- external(options, ctx) {
1874
- return this.telemetry.external(ctx ?? otel.context.active(), options);
1875
- }
1876
- }
1877
- class ContextManager {
1878
- telemetry;
1879
- constructor(telemetry) {
1880
- this.telemetry = telemetry;
1881
- }
1882
- resume(ctx) {
1883
- return this.telemetry.resume(ctx);
1884
- }
1885
- active() {
1886
- return otel.context.active();
1887
- }
1888
- with(ctx, fn, thisArg, ...args) {
1889
- return otel.context.with(ctx, fn, thisArg, ...args);
1890
- }
1891
- }
1892
- class InstrumentationManager {
1893
- instrumentations;
1894
- constructor(instrumentations) {
1895
- this.instrumentations = instrumentations;
1896
- }
1897
- enable() {
1898
- this.instrumentations.forEach((instrumentation) => {
1899
- if (!instrumentation.isEnabled())
1900
- instrumentation.enable();
1901
- });
1902
- }
1903
- disable() {
1904
- this.instrumentations.forEach((instrumentation) => {
1905
- if (instrumentation.isEnabled())
1906
- instrumentation.disable();
1907
- });
1908
- }
1909
- }
1910
- class TracerManager {
1911
- nodeProvider;
1912
- scopeVersion;
1913
- constructor(nodeProvider, scopeVersion) {
1914
- this.nodeProvider = nodeProvider;
1915
- this.scopeVersion = scopeVersion;
1916
- }
1917
- get(scope) {
1918
- return this.provider(scope).getTracer('');
1919
- }
1920
- provider(scope) {
1921
- return new ScopedTracerProvider(`${SCOPE_LATITUDE}.${scope}`, this.scopeVersion, this.nodeProvider);
1922
- }
1923
- }
1924
- class ScopedTracerProvider {
1925
- scope;
1926
- version;
1927
- provider;
1928
- constructor(scope, version, provider) {
1929
- this.scope = scope;
1930
- this.version = version;
1931
- this.provider = provider;
1932
- }
1933
- getTracer(_name, _version, options) {
1934
- return this.provider.getTracer(this.scope, this.version, options);
1935
- }
1936
- }
1937
- class LifecycleManager {
1938
- nodeProvider;
1939
- exporter;
1940
- constructor(nodeProvider, exporter) {
1941
- this.nodeProvider = nodeProvider;
1942
- this.exporter = exporter;
1943
- }
1944
- async flush() {
1945
- await this.nodeProvider.forceFlush();
1946
- await this.exporter.forceFlush?.();
1947
- }
1948
- async shutdown() {
1949
- await this.flush();
1950
- await this.nodeProvider.shutdown();
1951
- await this.exporter.shutdown?.();
1952
- }
1953
- }
1954
- const DEFAULT_SPAN_EXPORTER = (apiKey) => new exporterTraceOtlpHttp.OTLPTraceExporter({
1955
- url: TRACES_URL,
1956
- headers: {
1957
- Authorization: `Bearer ${apiKey}`,
1958
- 'Content-Type': 'application/json',
1959
- },
1960
- timeoutMillis: 30 * 1000,
1961
- });
1962
- // Note: Only exporting typescript instrumentations
1963
- exports.Instrumentation = void 0;
1964
- (function (Instrumentation) {
1965
- Instrumentation["Anthropic"] = "anthropic";
1966
- Instrumentation["AIPlatform"] = "aiplatform";
1967
- Instrumentation["Bedrock"] = "bedrock";
1968
- Instrumentation["Cohere"] = "cohere";
1969
- Instrumentation["Langchain"] = "langchain";
1970
- Instrumentation["Latitude"] = "latitude";
1971
- Instrumentation["LlamaIndex"] = "llamaindex";
1972
- Instrumentation["Manual"] = "manual";
1973
- Instrumentation["OpenAI"] = "openai";
1974
- Instrumentation["TogetherAI"] = "togetherai";
1975
- Instrumentation["VertexAI"] = "vertexai";
1976
- })(exports.Instrumentation || (exports.Instrumentation = {}));
1977
- class LatitudeTelemetry {
1978
- options;
1979
- nodeProvider;
1980
- manualInstrumentation;
1981
- instrumentationsList;
1982
- span;
1983
- context;
1984
- instrumentation;
1985
- tracer;
1986
- lifecycle;
1987
- constructor(apiKey, options) {
1988
- this.options = options || {};
1989
- if (!this.options.exporter) {
1990
- this.options.exporter = DEFAULT_SPAN_EXPORTER(apiKey);
1991
- }
1992
- otel.context.setGlobalContextManager(new contextAsyncHooks.AsyncLocalStorageContextManager().enable());
1993
- otel.propagation.setGlobalPropagator(new core.CompositePropagator({
1994
- propagators: [
1995
- ...(this.options.propagators || []),
1996
- new core.W3CTraceContextPropagator(),
1997
- new core.W3CBaggagePropagator(),
1998
- ],
1999
- }));
2000
- this.nodeProvider = new sdkTraceNode.NodeTracerProvider({
2001
- resource: new resources.Resource({ [semanticConventions.ATTR_SERVICE_NAME]: SERVICE_NAME }),
2002
- });
2003
- this.lifecycle = new LifecycleManager(this.nodeProvider, this.options.exporter);
2004
- // Note: important, must run before the exporter span processors
2005
- this.nodeProvider.addSpanProcessor(new baggageSpanProcessor.BaggageSpanProcessor(baggageSpanProcessor.ALLOW_ALL_BAGGAGE_KEYS));
2006
- if (this.options.processors) {
2007
- this.options.processors.forEach((processor) => {
2008
- this.nodeProvider.addSpanProcessor(processor);
2009
- });
2010
- }
2011
- else {
2012
- this.nodeProvider.addSpanProcessor(DEFAULT_REDACT_SPAN_PROCESSOR());
2013
- }
2014
- if (this.options.disableBatch) {
2015
- this.nodeProvider.addSpanProcessor(new sdkTraceNode.SimpleSpanProcessor(this.options.exporter));
2016
- }
2017
- else {
2018
- this.nodeProvider.addSpanProcessor(new sdkTraceNode.BatchSpanProcessor(this.options.exporter));
2019
- }
2020
- this.nodeProvider.register();
2021
- process.on('SIGTERM', async () => this.shutdown);
2022
- process.on('SIGINT', async () => this.shutdown);
2023
- this.manualInstrumentation = null;
2024
- this.instrumentationsList = [];
2025
- this.tracer = new TracerManager(this.nodeProvider, SCOPE_VERSION);
2026
- this.initInstrumentations();
2027
- this.instrumentation = new InstrumentationManager(this.instrumentationsList);
2028
- this.instrumentation.enable();
2029
- this.span = new SpanFactory(this.manualInstrumentation);
2030
- this.context = new ContextManager(this.manualInstrumentation);
2031
- }
2032
- async flush() {
2033
- await this.lifecycle.flush();
2034
- }
2035
- async shutdown() {
2036
- await this.lifecycle.shutdown();
2037
- }
2038
- // TODO(tracing): auto instrument outgoing HTTP requests
2039
- initInstrumentations() {
2040
- this.instrumentationsList = [];
2041
- const tracer = this.tracer.get(exports.Instrumentation.Manual);
2042
- this.manualInstrumentation = new ManualInstrumentation(tracer, this.options.instrumentations?.manual);
2043
- this.instrumentationsList.push(this.manualInstrumentation);
2044
- const latitude = this.options.instrumentations?.latitude;
2045
- if (latitude) {
2046
- const tracer = this.tracer.get(exports.Instrumentation.Latitude);
2047
- const instrumentation = new LatitudeInstrumentation(tracer, typeof latitude === 'object' ? latitude : { module: latitude });
2048
- this.instrumentationsList.push(instrumentation);
2049
- }
2050
- const configureInstrumentation = (instrumentationType, InstrumentationConstructor, instrumentationOptions) => {
2051
- const providerPkg = this.options.instrumentations?.[instrumentationType];
2052
- if (!providerPkg)
2053
- return;
2054
- const provider = this.tracer.provider(instrumentationType);
2055
- const instrumentation$1 = new InstrumentationConstructor(instrumentationOptions); // prettier-ignore
2056
- instrumentation$1.setTracerProvider(provider);
2057
- instrumentation$1.manuallyInstrument(providerPkg);
2058
- instrumentation.registerInstrumentations({
2059
- instrumentations: [instrumentation$1],
2060
- tracerProvider: provider,
2061
- });
2062
- this.instrumentationsList.push(instrumentation$1);
2063
- };
2064
- configureInstrumentation(exports.Instrumentation.Anthropic, instrumentationAnthropic.AnthropicInstrumentation); // prettier-ignore
2065
- configureInstrumentation(exports.Instrumentation.AIPlatform, instrumentationVertexai.AIPlatformInstrumentation); // prettier-ignore
2066
- configureInstrumentation(exports.Instrumentation.Bedrock, instrumentationBedrock.BedrockInstrumentation); // prettier-ignore
2067
- configureInstrumentation(exports.Instrumentation.Cohere, instrumentationCohere.CohereInstrumentation); // prettier-ignore
2068
- configureInstrumentation(exports.Instrumentation.Langchain, instrumentationLangchain.LangChainInstrumentation); // prettier-ignore
2069
- configureInstrumentation(exports.Instrumentation.LlamaIndex, instrumentationLlamaindex.LlamaIndexInstrumentation); // prettier-ignore
2070
- // NOTE: `stream: true` in OpenAI make enrichTokens fail, so disabling.
2071
- configureInstrumentation(exports.Instrumentation.OpenAI, instrumentationOpenai.OpenAIInstrumentation, { enrichTokens: false }); // prettier-ignore
2072
- configureInstrumentation(exports.Instrumentation.TogetherAI, instrumentationTogether.TogetherInstrumentation, { enrichTokens: false }); // prettier-ignore
2073
- configureInstrumentation(exports.Instrumentation.VertexAI, instrumentationVertexai.VertexAIInstrumentation); // prettier-ignore
2074
- }
2075
- async capture(options, fn) {
2076
- if (!DOCUMENT_PATH_REGEXP.test(options.path)) {
2077
- throw new BadRequestError("Invalid path, no spaces. Only letters, numbers, '.', '-' and '_'");
2078
- }
2079
- const captureBaggageEntries = {
2080
- [ATTRIBUTES.LATITUDE.promptPath]: { value: options.path },
2081
- [ATTRIBUTES.LATITUDE.projectId]: { value: String(options.projectId) },
2082
- };
2083
- if (options.versionUuid) {
2084
- captureBaggageEntries[ATTRIBUTES.LATITUDE.commitUuid] = {
2085
- value: options.versionUuid,
2086
- };
2087
- }
2088
- if (options.conversationUuid) {
2089
- captureBaggageEntries[ATTRIBUTES.LATITUDE.documentLogUuid] = {
2090
- value: options.conversationUuid,
2091
- };
2092
- }
2093
- const captureContext = otel.propagation.setBaggage(BACKGROUND(), otel.propagation.createBaggage(captureBaggageEntries));
2094
- const span = this.manualInstrumentation.unresolvedExternal(captureContext, options);
2095
- let result;
327
+ this.lifecycle = new LifecycleManager(this.nodeProvider, this.options.exporter);
328
+ this.nodeProvider.register();
329
+ process.on("SIGTERM", () => this.shutdown());
330
+ process.on("SIGINT", () => this.shutdown());
331
+ this.instrumentationsList = [];
332
+ const tracerManager = new TracerManager(this.nodeProvider, SCOPE_VERSION);
333
+ const manualTracer = tracerManager.get("manual" /* Manual */);
334
+ const manualInstrumentation = new ManualInstrumentation(manualTracer);
335
+ this.instrumentationsList.push(manualInstrumentation);
336
+ this.tracer = manualTracer;
337
+ this.initProviderInstrumentations(tracerManager);
338
+ this.instrumentation = new InstrumentationManager(this.instrumentationsList);
339
+ this.instrumentation.enable();
340
+ this.context = new ContextManager(manualInstrumentation);
341
+ }
342
+ async flush() {
343
+ await this.lifecycle.flush();
344
+ }
345
+ async shutdown() {
346
+ await this.lifecycle.shutdown();
347
+ }
348
+ initProviderInstrumentations(tracerManager) {
349
+ const configure = (type, Ctor, opts) => {
350
+ const providerPkg = this.options.instrumentations?.[type];
351
+ if (!providerPkg) return;
352
+ const provider = tracerManager.provider(type);
353
+ const inst = new Ctor(opts);
354
+ inst.setTracerProvider(provider);
355
+ inst.manuallyInstrument(providerPkg);
356
+ (0, import_instrumentation.registerInstrumentations)({
357
+ instrumentations: [inst],
358
+ tracerProvider: provider
359
+ });
360
+ this.instrumentationsList.push(inst);
361
+ };
362
+ configure("anthropic" /* Anthropic */, import_instrumentation_anthropic.AnthropicInstrumentation);
363
+ configure("aiplatform" /* AIPlatform */, import_instrumentation_vertexai.AIPlatformInstrumentation);
364
+ configure("bedrock" /* Bedrock */, import_instrumentation_bedrock.BedrockInstrumentation);
365
+ configure("cohere" /* Cohere */, import_instrumentation_cohere.CohereInstrumentation);
366
+ configure("langchain" /* Langchain */, import_instrumentation_langchain.LangChainInstrumentation);
367
+ configure("llamaindex" /* LlamaIndex */, import_instrumentation_llamaindex.LlamaIndexInstrumentation);
368
+ configure("openai" /* OpenAI */, import_instrumentation_openai.OpenAIInstrumentation, { enrichTokens: true });
369
+ configure("togetherai" /* TogetherAI */, import_instrumentation_together.TogetherInstrumentation, { enrichTokens: false });
370
+ configure("vertexai" /* VertexAI */, import_instrumentation_vertexai.VertexAIInstrumentation);
371
+ }
372
+ /**
373
+ * Wrap a block of code with trace-wide context attributes.
374
+ * Baggage entries (tags, metadata, sessionId, userId) are propagated
375
+ * to all spans created within the callback via BaggageSpanProcessor.
376
+ *
377
+ * If there is no active span, a root span is created so all child spans
378
+ * are grouped under a single trace. If a span is already active, only
379
+ * baggage is set without creating an extra wrapper span.
380
+ */
381
+ async capture(options, fn) {
382
+ const baggageEntries = {};
383
+ if (options.tags?.length) {
384
+ baggageEntries[ATTRIBUTES.tags] = { value: JSON.stringify(options.tags) };
385
+ }
386
+ if (options.metadata) {
387
+ baggageEntries[ATTRIBUTES.metadata] = { value: JSON.stringify(options.metadata) };
388
+ }
389
+ if (options.sessionId) {
390
+ baggageEntries[ATTRIBUTES.sessionId] = { value: options.sessionId };
391
+ }
392
+ if (options.userId) {
393
+ baggageEntries[ATTRIBUTES.userId] = { value: options.userId };
394
+ }
395
+ const baggage = import_api2.propagation.createBaggage(baggageEntries);
396
+ const activeSpan = otel2.trace.getSpan(import_api2.context.active());
397
+ if (activeSpan) {
398
+ const ctx = import_api2.propagation.setBaggage(import_api2.context.active(), baggage);
399
+ return await import_api2.context.with(ctx, async () => await fn(ctx));
400
+ }
401
+ const rootContext = import_api2.propagation.setBaggage(otel2.ROOT_CONTEXT, baggage);
402
+ return await this.tracer.startActiveSpan(
403
+ "latitude.trace",
404
+ { kind: otel2.SpanKind.INTERNAL },
405
+ rootContext,
406
+ async (span) => {
2096
407
  try {
2097
- result = await otel.context.with(span.context, async () => await fn(span.context));
2098
- }
2099
- catch (error) {
2100
- span.fail(error);
2101
- throw error;
2102
- }
2103
- span.end();
2104
- return result;
2105
- }
2106
- }
2107
-
2108
- exports.BACKGROUND = BACKGROUND;
2109
- exports.DEFAULT_REDACT_SPAN_PROCESSOR = DEFAULT_REDACT_SPAN_PROCESSOR;
2110
- exports.DEFAULT_SPAN_EXPORTER = DEFAULT_SPAN_EXPORTER;
2111
- exports.LatitudeTelemetry = LatitudeTelemetry;
2112
- exports.RedactSpanProcessor = RedactSpanProcessor;
2113
- //# sourceMappingURL=index.cjs.map
408
+ const result = await fn(import_api2.context.active());
409
+ span.setStatus({ code: otel2.SpanStatusCode.OK });
410
+ return result;
411
+ } catch (error) {
412
+ span.setStatus({
413
+ code: otel2.SpanStatusCode.ERROR,
414
+ message: error instanceof Error ? error.message : String(error)
415
+ });
416
+ span.recordException(error instanceof Error ? error : new Error(String(error)));
417
+ throw error;
418
+ } finally {
419
+ span.end();
420
+ }
421
+ }
422
+ );
423
+ }
424
+ };
425
+ // Annotate the CommonJS export names for ESM import in node:
426
+ 0 && (module.exports = {
427
+ DEFAULT_REDACT_SPAN_PROCESSOR,
428
+ DEFAULT_SPAN_EXPORTER,
429
+ Instrumentation,
430
+ LatitudeTelemetry,
431
+ RedactSpanProcessor
432
+ });
433
+ //# sourceMappingURL=index.cjs.map