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