langwatch 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/copy-types.sh +17 -0
  2. package/dist/chunk-2I4YLOQY.mjs +736 -0
  3. package/dist/chunk-2I4YLOQY.mjs.map +1 -0
  4. package/dist/index.d.mts +352 -4
  5. package/dist/index.d.ts +352 -4
  6. package/dist/index.js +6505 -413
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +571 -359
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/{utils-DDcm0z9v.d.mts → utils-CFtM8VVg.d.mts} +108 -31
  11. package/dist/{utils-DDcm0z9v.d.ts → utils-CFtM8VVg.d.ts} +108 -31
  12. package/dist/utils.d.mts +1 -2
  13. package/dist/utils.d.ts +1 -2
  14. package/dist/utils.js +437 -0
  15. package/dist/utils.js.map +1 -1
  16. package/dist/utils.mjs +3 -1
  17. package/example/README.md +3 -1
  18. package/example/app/(chat)/chat/[id]/page.tsx +1 -1
  19. package/example/app/(chat)/page.tsx +10 -5
  20. package/example/app/guardrails/page.tsx +26 -0
  21. package/example/app/langchain/page.tsx +27 -0
  22. package/example/app/langchain-rag/page.tsx +28 -0
  23. package/example/app/share/[id]/page.tsx +1 -1
  24. package/example/components/chat-list.tsx +1 -1
  25. package/example/components/chat-panel.tsx +1 -1
  26. package/example/components/header.tsx +39 -13
  27. package/example/components/prompt-form.tsx +1 -1
  28. package/example/components/stocks/stock-purchase.tsx +1 -1
  29. package/example/components/stocks/stocks.tsx +1 -1
  30. package/example/lib/chat/guardrails.tsx +181 -0
  31. package/example/lib/chat/langchain-rag.tsx +191 -0
  32. package/example/lib/chat/langchain.tsx +112 -0
  33. package/example/lib/chat/{actions.tsx → vercel-ai.tsx} +1 -1
  34. package/example/package-lock.json +289 -6
  35. package/example/package.json +1 -0
  36. package/package.json +13 -5
  37. package/src/evaluations.ts +219 -0
  38. package/src/index.test.ts +5 -0
  39. package/src/index.ts +190 -7
  40. package/src/langchain.ts +557 -0
  41. package/src/{helpers.ts → typeUtils.ts} +20 -2
  42. package/src/types.ts +7 -3
  43. package/src/utils.ts +25 -2
  44. package/ts-to-zod.config.js +2 -0
  45. package/dist/chunk-AP23NJ57.mjs +0 -296
  46. package/dist/chunk-AP23NJ57.mjs.map +0 -1
@@ -0,0 +1,736 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __commonJS = (cb, mod) => function __require() {
25
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
36
+ // If the importer is in node compatibility mode or this is not an ESM
37
+ // file that has been converted to a CommonJS file using a Babel-
38
+ // compatible transform (i.e. "__esModule" has not been set), then set
39
+ // "default" to the CommonJS "module.exports" for node compatibility.
40
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
41
+ mod
42
+ ));
43
+
44
+ // node_modules/secure-json-parse/index.js
45
+ var require_secure_json_parse = __commonJS({
46
+ "node_modules/secure-json-parse/index.js"(exports, module) {
47
+ "use strict";
48
+ var hasBuffer = typeof Buffer !== "undefined";
49
+ var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
50
+ var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
51
+ function _parse(text, reviver, options) {
52
+ if (options == null) {
53
+ if (reviver !== null && typeof reviver === "object") {
54
+ options = reviver;
55
+ reviver = void 0;
56
+ }
57
+ }
58
+ if (hasBuffer && Buffer.isBuffer(text)) {
59
+ text = text.toString();
60
+ }
61
+ if (text && text.charCodeAt(0) === 65279) {
62
+ text = text.slice(1);
63
+ }
64
+ const obj = JSON.parse(text, reviver);
65
+ if (obj === null || typeof obj !== "object") {
66
+ return obj;
67
+ }
68
+ const protoAction = options && options.protoAction || "error";
69
+ const constructorAction = options && options.constructorAction || "error";
70
+ if (protoAction === "ignore" && constructorAction === "ignore") {
71
+ return obj;
72
+ }
73
+ if (protoAction !== "ignore" && constructorAction !== "ignore") {
74
+ if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
75
+ return obj;
76
+ }
77
+ } else if (protoAction !== "ignore" && constructorAction === "ignore") {
78
+ if (suspectProtoRx.test(text) === false) {
79
+ return obj;
80
+ }
81
+ } else {
82
+ if (suspectConstructorRx.test(text) === false) {
83
+ return obj;
84
+ }
85
+ }
86
+ return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
87
+ }
88
+ function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
89
+ let next = [obj];
90
+ while (next.length) {
91
+ const nodes = next;
92
+ next = [];
93
+ for (const node of nodes) {
94
+ if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
95
+ if (safe === true) {
96
+ return null;
97
+ } else if (protoAction === "error") {
98
+ throw new SyntaxError("Object contains forbidden prototype property");
99
+ }
100
+ delete node.__proto__;
101
+ }
102
+ if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
103
+ if (safe === true) {
104
+ return null;
105
+ } else if (constructorAction === "error") {
106
+ throw new SyntaxError("Object contains forbidden prototype property");
107
+ }
108
+ delete node.constructor;
109
+ }
110
+ for (const key in node) {
111
+ const value = node[key];
112
+ if (value && typeof value === "object") {
113
+ next.push(value);
114
+ }
115
+ }
116
+ }
117
+ }
118
+ return obj;
119
+ }
120
+ function parse(text, reviver, options) {
121
+ const stackTraceLimit = Error.stackTraceLimit;
122
+ Error.stackTraceLimit = 0;
123
+ try {
124
+ return _parse(text, reviver, options);
125
+ } finally {
126
+ Error.stackTraceLimit = stackTraceLimit;
127
+ }
128
+ }
129
+ function safeParse(text, reviver) {
130
+ const stackTraceLimit = Error.stackTraceLimit;
131
+ Error.stackTraceLimit = 0;
132
+ try {
133
+ return _parse(text, reviver, { safe: true });
134
+ } catch (_e) {
135
+ return null;
136
+ } finally {
137
+ Error.stackTraceLimit = stackTraceLimit;
138
+ }
139
+ }
140
+ module.exports = parse;
141
+ module.exports.default = parse;
142
+ module.exports.parse = parse;
143
+ module.exports.safeParse = safeParse;
144
+ module.exports.scan = filter;
145
+ }
146
+ });
147
+
148
+ // node_modules/@ai-sdk/provider-utils/dist/index.mjs
149
+ import { customAlphabet } from "nanoid/non-secure";
150
+ var import_secure_json_parse = __toESM(require_secure_json_parse(), 1);
151
+ var generateId = customAlphabet(
152
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
153
+ 7
154
+ );
155
+ function convertUint8ArrayToBase64(array) {
156
+ let latin1string = "";
157
+ for (let i = 0; i < array.length; i++) {
158
+ latin1string += String.fromCodePoint(array[i]);
159
+ }
160
+ return globalThis.btoa(latin1string);
161
+ }
162
+
163
+ // src/utils.ts
164
+ import { z as z2 } from "zod";
165
+
166
+ // src/server/types/tracer.generated.ts
167
+ import { z } from "zod";
168
+ var chatRoleSchema = z.union([
169
+ z.literal("system"),
170
+ z.literal("user"),
171
+ z.literal("assistant"),
172
+ z.literal("function"),
173
+ z.literal("tool"),
174
+ z.literal("unknown")
175
+ ]);
176
+ var functionCallSchema = z.object({
177
+ name: z.string().optional(),
178
+ arguments: z.string().optional()
179
+ });
180
+ var toolCallSchema = z.object({
181
+ id: z.string(),
182
+ type: z.string(),
183
+ function: functionCallSchema
184
+ });
185
+ var rAGChunkSchema = z.object({
186
+ document_id: z.string().optional().nullable(),
187
+ chunk_id: z.string().optional().nullable(),
188
+ content: z.union([z.string(), z.record(z.any()), z.array(z.any())])
189
+ });
190
+ var chatRichContentSchema = z.union([
191
+ z.object({
192
+ type: z.literal("text"),
193
+ text: z.string().optional()
194
+ }),
195
+ z.object({
196
+ type: z.literal("image_url"),
197
+ image_url: z.object({
198
+ url: z.string(),
199
+ detail: z.union([z.literal("auto"), z.literal("low"), z.literal("high")]).optional()
200
+ }).optional()
201
+ })
202
+ ]);
203
+ var typedValueTextSchema = z.object({
204
+ type: z.literal("text"),
205
+ value: z.string()
206
+ });
207
+ var typedValueRawSchema = z.object({
208
+ type: z.literal("raw"),
209
+ value: z.string()
210
+ });
211
+ var jSONSerializableSchema = z.union([
212
+ z.string(),
213
+ z.number(),
214
+ z.boolean(),
215
+ z.record(z.any()),
216
+ z.array(z.any())
217
+ ]).nullable();
218
+ var typedValueJsonSchema = z.object({
219
+ type: z.literal("json"),
220
+ value: jSONSerializableSchema
221
+ });
222
+ var moneySchema = z.object({
223
+ currency: z.string(),
224
+ amount: z.number()
225
+ });
226
+ var evaluationResultSchema = z.object({
227
+ status: z.union([
228
+ z.literal("processed"),
229
+ z.literal("skipped"),
230
+ z.literal("error")
231
+ ]),
232
+ passed: z.boolean().optional().nullable(),
233
+ score: z.number().optional().nullable(),
234
+ label: z.string().optional().nullable(),
235
+ details: z.string().optional().nullable(),
236
+ cost: moneySchema.optional().nullable()
237
+ });
238
+ var typedValueGuardrailResultSchema = z.object({
239
+ type: z.literal("guardrail_result"),
240
+ value: evaluationResultSchema
241
+ });
242
+ var typedValueEvaluationResultSchema = z.object({
243
+ type: z.literal("evaluation_result"),
244
+ value: evaluationResultSchema
245
+ });
246
+ var errorCaptureSchema = z.object({
247
+ has_error: z.literal(true),
248
+ message: z.string(),
249
+ stacktrace: z.array(z.string())
250
+ });
251
+ var spanMetricsSchema = z.object({
252
+ prompt_tokens: z.number().optional().nullable(),
253
+ completion_tokens: z.number().optional().nullable(),
254
+ tokens_estimated: z.boolean().optional().nullable(),
255
+ cost: z.number().optional().nullable()
256
+ });
257
+ var reservedSpanParamsSchema = z.object({
258
+ frequency_penalty: z.number().optional().nullable(),
259
+ logit_bias: z.record(z.number()).optional().nullable(),
260
+ logprobs: z.boolean().optional().nullable(),
261
+ top_logprobs: z.number().optional().nullable(),
262
+ max_tokens: z.number().optional().nullable(),
263
+ n: z.number().optional().nullable(),
264
+ presence_penalty: z.number().optional().nullable(),
265
+ seed: z.number().optional().nullable(),
266
+ stop: z.union([z.string(), z.array(z.string())]).optional().nullable(),
267
+ stream: z.boolean().optional().nullable(),
268
+ temperature: z.number().optional().nullable(),
269
+ top_p: z.number().optional().nullable(),
270
+ tools: z.array(z.record(z.any())).optional().nullable(),
271
+ tool_choice: z.union([z.record(z.any()), z.string()]).optional().nullable(),
272
+ parallel_tool_calls: z.boolean().optional().nullable(),
273
+ functions: z.array(z.record(z.any())).optional().nullable(),
274
+ user: z.string().optional().nullable()
275
+ });
276
+ var spanParamsSchema = reservedSpanParamsSchema.and(z.record(z.any()));
277
+ var spanTimestampsSchema = z.object({
278
+ started_at: z.number(),
279
+ first_token_at: z.number().optional().nullable(),
280
+ finished_at: z.number()
281
+ });
282
+ var spanTypesSchema = z.union([
283
+ z.literal("span"),
284
+ z.literal("llm"),
285
+ z.literal("chain"),
286
+ z.literal("tool"),
287
+ z.literal("agent"),
288
+ z.literal("rag"),
289
+ z.literal("guardrail"),
290
+ z.literal("evaluation"),
291
+ z.literal("workflow"),
292
+ z.literal("component"),
293
+ z.literal("server"),
294
+ z.literal("client"),
295
+ z.literal("producer"),
296
+ z.literal("consumer"),
297
+ z.literal("task"),
298
+ z.literal("unknown")
299
+ ]);
300
+ var traceInputSchema = z.object({
301
+ value: z.string(),
302
+ embeddings: z.object({
303
+ model: z.string(),
304
+ embeddings: z.array(z.number())
305
+ }).optional(),
306
+ satisfaction_score: z.number().optional()
307
+ });
308
+ var traceOutputSchema = z.object({
309
+ value: z.string(),
310
+ embeddings: z.object({
311
+ model: z.string(),
312
+ embeddings: z.array(z.number())
313
+ }).optional()
314
+ });
315
+ var primitiveTypeSchema = z.union([z.string(), z.number(), z.boolean(), z.undefined()]).nullable();
316
+ var reservedTraceMetadataSchema = z.object({
317
+ thread_id: z.string().optional().nullable(),
318
+ user_id: z.string().optional().nullable(),
319
+ customer_id: z.string().optional().nullable(),
320
+ labels: z.array(z.string()).optional().nullable(),
321
+ topic_id: z.string().optional().nullable(),
322
+ subtopic_id: z.string().optional().nullable(),
323
+ sdk_version: z.string().optional().nullable(),
324
+ sdk_language: z.string().optional().nullable()
325
+ });
326
+ var customMetadataSchema = z.record(
327
+ z.union([
328
+ primitiveTypeSchema,
329
+ z.array(primitiveTypeSchema),
330
+ z.record(primitiveTypeSchema),
331
+ z.record(z.record(primitiveTypeSchema))
332
+ ])
333
+ );
334
+ var traceMetadataSchema = reservedTraceMetadataSchema.and(customMetadataSchema);
335
+ var eventSchema = z.object({
336
+ event_id: z.string(),
337
+ event_type: z.string(),
338
+ project_id: z.string(),
339
+ metrics: z.record(z.number()),
340
+ event_details: z.record(z.string()),
341
+ trace_id: z.string(),
342
+ timestamps: z.object({
343
+ started_at: z.number(),
344
+ inserted_at: z.number(),
345
+ updated_at: z.number()
346
+ })
347
+ });
348
+ var elasticSearchEventSchema = eventSchema.omit({ metrics: true, event_details: true }).and(
349
+ z.object({
350
+ metrics: z.array(
351
+ z.object({
352
+ key: z.string(),
353
+ value: z.number()
354
+ })
355
+ ),
356
+ event_details: z.array(
357
+ z.object({
358
+ key: z.string(),
359
+ value: z.string()
360
+ })
361
+ )
362
+ })
363
+ );
364
+ var evaluationStatusSchema = z.union([
365
+ z.literal("scheduled"),
366
+ z.literal("in_progress"),
367
+ z.literal("error"),
368
+ z.literal("skipped"),
369
+ z.literal("processed")
370
+ ]);
371
+ var trackEventRESTParamsValidatorSchema = eventSchema.omit({
372
+ event_id: true,
373
+ project_id: true,
374
+ timestamps: true,
375
+ event_details: true
376
+ }).and(
377
+ z.object({
378
+ event_id: z.string().optional(),
379
+ event_details: z.record(z.string()).optional(),
380
+ timestamp: z.number().optional()
381
+ })
382
+ );
383
+ var contextsSchema = z.object({
384
+ traceId: z.string(),
385
+ contexts: z.array(rAGChunkSchema)
386
+ });
387
+ var chatMessageSchema = z.object({
388
+ role: chatRoleSchema.optional(),
389
+ content: z.union([z.string(), z.array(chatRichContentSchema)]).optional().nullable(),
390
+ function_call: functionCallSchema.optional().nullable(),
391
+ tool_calls: z.array(toolCallSchema).optional().nullable(),
392
+ tool_call_id: z.string().optional().nullable(),
393
+ name: z.string().optional().nullable()
394
+ });
395
+ var typedValueChatMessagesSchema = z.object({
396
+ type: z.literal("chat_messages"),
397
+ value: z.array(chatMessageSchema)
398
+ });
399
+ var spanInputOutputSchema = z.lazy(
400
+ () => z.union([
401
+ typedValueTextSchema,
402
+ typedValueChatMessagesSchema,
403
+ typedValueGuardrailResultSchema,
404
+ typedValueEvaluationResultSchema,
405
+ typedValueJsonSchema,
406
+ typedValueRawSchema,
407
+ z.object({
408
+ type: z.literal("list"),
409
+ value: z.array(spanInputOutputSchema)
410
+ })
411
+ ])
412
+ );
413
+ var baseSpanSchema = z.object({
414
+ span_id: z.string(),
415
+ parent_id: z.string().optional().nullable(),
416
+ trace_id: z.string(),
417
+ type: spanTypesSchema,
418
+ name: z.string().optional().nullable(),
419
+ input: spanInputOutputSchema.optional().nullable(),
420
+ output: spanInputOutputSchema.optional().nullable(),
421
+ error: errorCaptureSchema.optional().nullable(),
422
+ timestamps: spanTimestampsSchema,
423
+ metrics: spanMetricsSchema.optional().nullable(),
424
+ params: spanParamsSchema.optional().nullable()
425
+ });
426
+ var lLMSpanSchema = baseSpanSchema.extend({
427
+ type: z.literal("llm"),
428
+ vendor: z.string().optional().nullable(),
429
+ model: z.string().optional().nullable()
430
+ });
431
+ var rAGSpanSchema = baseSpanSchema.extend({
432
+ type: z.literal("rag"),
433
+ contexts: z.array(rAGChunkSchema)
434
+ });
435
+ var spanSchema = z.union([
436
+ lLMSpanSchema,
437
+ rAGSpanSchema,
438
+ baseSpanSchema
439
+ ]);
440
+ var spanInputOutputValidatorSchema = spanInputOutputSchema.and(
441
+ z.object({
442
+ value: z.any()
443
+ })
444
+ );
445
+ var spanValidatorSchema = z.union([
446
+ lLMSpanSchema.omit({ input: true, output: true, params: true }),
447
+ rAGSpanSchema.omit({ input: true, output: true, params: true }),
448
+ baseSpanSchema.omit({ input: true, output: true, params: true })
449
+ ]).and(
450
+ z.object({
451
+ input: spanInputOutputValidatorSchema.optional().nullable(),
452
+ output: spanInputOutputValidatorSchema.optional().nullable(),
453
+ params: z.record(z.any()).optional().nullable()
454
+ })
455
+ );
456
+ var evaluationSchema = z.object({
457
+ evaluation_id: z.string(),
458
+ evaluator_id: z.string(),
459
+ span_id: z.string().optional().nullable(),
460
+ name: z.string(),
461
+ type: z.string().optional().nullable(),
462
+ is_guardrail: z.boolean().optional().nullable(),
463
+ status: evaluationStatusSchema,
464
+ passed: z.boolean().optional().nullable(),
465
+ score: z.number().optional().nullable(),
466
+ label: z.string().optional().nullable(),
467
+ details: z.string().optional().nullable(),
468
+ error: errorCaptureSchema.optional().nullable(),
469
+ retries: z.number().optional().nullable(),
470
+ timestamps: z.object({
471
+ inserted_at: z.number().optional().nullable(),
472
+ started_at: z.number().optional().nullable(),
473
+ finished_at: z.number().optional().nullable(),
474
+ updated_at: z.number().optional().nullable()
475
+ })
476
+ });
477
+ var rESTEvaluationSchema = evaluationSchema.omit({
478
+ evaluation_id: true,
479
+ evaluator_id: true,
480
+ status: true,
481
+ timestamps: true,
482
+ retries: true
483
+ }).and(
484
+ z.object({
485
+ evaluation_id: z.string().optional().nullable(),
486
+ evaluator_id: z.string().optional().nullable(),
487
+ status: z.union([
488
+ z.literal("processed"),
489
+ z.literal("skipped"),
490
+ z.literal("error")
491
+ ]).optional().nullable(),
492
+ timestamps: z.object({
493
+ started_at: z.number().optional().nullable(),
494
+ finished_at: z.number().optional().nullable()
495
+ }).optional().nullable()
496
+ })
497
+ );
498
+ var collectorRESTParamsSchema = z.object({
499
+ trace_id: z.union([z.string(), z.undefined()]).optional().nullable(),
500
+ spans: z.array(spanSchema),
501
+ metadata: z.object({
502
+ user_id: z.union([z.string(), z.undefined()]).optional().nullable(),
503
+ thread_id: z.union([z.string(), z.undefined()]).optional().nullable(),
504
+ customer_id: z.union([z.string(), z.undefined()]).optional().nullable(),
505
+ labels: z.union([z.array(z.string()), z.undefined()]).optional().nullable(),
506
+ sdk_version: z.union([z.string(), z.undefined()]).optional().nullable(),
507
+ sdk_language: z.union([z.string(), z.undefined()]).optional().nullable()
508
+ }).and(customMetadataSchema).optional(),
509
+ expected_output: z.string().optional().nullable(),
510
+ evaluations: z.array(rESTEvaluationSchema).optional()
511
+ });
512
+ var collectorRESTParamsValidatorSchema = collectorRESTParamsSchema.omit({ spans: true });
513
+ var datasetSpanSchema = z.union([
514
+ baseSpanSchema.omit({
515
+ project_id: true,
516
+ trace_id: true,
517
+ id: true,
518
+ timestamps: true,
519
+ metrics: true,
520
+ params: true
521
+ }).and(
522
+ z.object({
523
+ params: z.record(z.any())
524
+ })
525
+ ),
526
+ lLMSpanSchema.omit({
527
+ project_id: true,
528
+ trace_id: true,
529
+ id: true,
530
+ timestamps: true,
531
+ metrics: true,
532
+ params: true
533
+ }).and(
534
+ z.object({
535
+ params: z.record(z.any())
536
+ })
537
+ ),
538
+ rAGSpanSchema.omit({
539
+ project_id: true,
540
+ trace_id: true,
541
+ id: true,
542
+ timestamps: true,
543
+ metrics: true,
544
+ params: true
545
+ }).and(
546
+ z.object({
547
+ params: z.record(z.any())
548
+ })
549
+ )
550
+ ]);
551
+ var traceSchema = z.object({
552
+ trace_id: z.string(),
553
+ project_id: z.string(),
554
+ metadata: traceMetadataSchema,
555
+ timestamps: z.object({
556
+ started_at: z.number(),
557
+ inserted_at: z.number(),
558
+ updated_at: z.number()
559
+ }),
560
+ input: traceInputSchema.optional(),
561
+ output: traceOutputSchema.optional(),
562
+ contexts: z.array(rAGChunkSchema).optional(),
563
+ expected_output: z.object({
564
+ value: z.string()
565
+ }).optional(),
566
+ metrics: z.object({
567
+ first_token_ms: z.number().optional().nullable(),
568
+ total_time_ms: z.number().optional().nullable(),
569
+ prompt_tokens: z.number().optional().nullable(),
570
+ completion_tokens: z.number().optional().nullable(),
571
+ total_cost: z.number().optional().nullable(),
572
+ tokens_estimated: z.boolean().optional().nullable()
573
+ }).optional(),
574
+ error: errorCaptureSchema.optional().nullable(),
575
+ indexing_md5s: z.array(z.string()).optional(),
576
+ events: z.array(eventSchema).optional(),
577
+ evaluations: z.array(evaluationSchema).optional()
578
+ });
579
+
580
+ // src/utils.ts
581
+ var convertImageToUrl = (image, mimeType) => {
582
+ try {
583
+ return image instanceof URL ? image.toString() : typeof image === "string" ? image : `data:${mimeType != null ? mimeType : "image/jpeg"};base64,${convertUint8ArrayToBase64(
584
+ image
585
+ )}`;
586
+ } catch (e) {
587
+ console.error("[LangWatch] error converting vercel ui image to url:", e);
588
+ return "";
589
+ }
590
+ };
591
+ function convertFromVercelAIMessages(messages) {
592
+ var _a;
593
+ const lwMessages = [];
594
+ for (const { role, content } of messages) {
595
+ switch (role) {
596
+ case "system": {
597
+ lwMessages.push({ role: "system", content });
598
+ break;
599
+ }
600
+ case "user": {
601
+ if (Array.isArray(content) && content.length === 1 && ((_a = content[0]) == null ? void 0 : _a.type) === "text") {
602
+ lwMessages.push({ role: "user", content: content[0].text });
603
+ break;
604
+ }
605
+ lwMessages.push({
606
+ role: "user",
607
+ content: Array.isArray(content) ? content.map((part) => {
608
+ switch (part.type) {
609
+ case "text": {
610
+ return { type: "text", text: part.text };
611
+ }
612
+ case "image": {
613
+ return {
614
+ type: "image_url",
615
+ image_url: {
616
+ url: convertImageToUrl(part.image, part.mimeType)
617
+ }
618
+ };
619
+ }
620
+ }
621
+ }) : content
622
+ });
623
+ break;
624
+ }
625
+ case "assistant": {
626
+ let text = "";
627
+ const toolCalls = [];
628
+ if (Array.isArray(content)) {
629
+ for (const part of content) {
630
+ switch (part.type) {
631
+ case "text": {
632
+ text += part.text;
633
+ break;
634
+ }
635
+ case "tool-call": {
636
+ toolCalls.push({
637
+ id: part.toolCallId,
638
+ type: "function",
639
+ function: {
640
+ name: part.toolName,
641
+ arguments: JSON.stringify(part.args)
642
+ }
643
+ });
644
+ break;
645
+ }
646
+ default: {
647
+ const _exhaustiveCheck = part;
648
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
649
+ }
650
+ }
651
+ }
652
+ } else {
653
+ text = content;
654
+ }
655
+ lwMessages.push({
656
+ role: "assistant",
657
+ content: text,
658
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
659
+ });
660
+ break;
661
+ }
662
+ case "tool": {
663
+ for (const toolResponse of content) {
664
+ lwMessages.push({
665
+ role: "tool",
666
+ tool_call_id: toolResponse.toolCallId,
667
+ content: JSON.stringify(toolResponse.result)
668
+ });
669
+ }
670
+ break;
671
+ }
672
+ default: {
673
+ const _exhaustiveCheck = role;
674
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
675
+ }
676
+ }
677
+ }
678
+ return lwMessages;
679
+ }
680
+ var captureError = (error) => {
681
+ if (error && typeof error === "object" && "has_error" in error && "message" in error && "stacktrace" in error) {
682
+ return error;
683
+ } else if (error instanceof Error) {
684
+ return {
685
+ has_error: true,
686
+ message: error.message,
687
+ stacktrace: error.stack ? error.stack.split("\n") : []
688
+ };
689
+ } else if (typeof error === "object" && error !== null) {
690
+ const err = error;
691
+ const message = typeof err.message === "string" ? err.message : "An unknown error occurred";
692
+ const stacktrace = typeof err.stack === "string" ? err.stack.split("\n") : Array.isArray(err.stack) && err.stack.length > 0 && typeof err.stack[0] === "string" ? err.stack : ["No stack trace available"];
693
+ return {
694
+ has_error: true,
695
+ message,
696
+ stacktrace
697
+ };
698
+ } else {
699
+ return {
700
+ has_error: true,
701
+ message: String(error),
702
+ stacktrace: []
703
+ };
704
+ }
705
+ };
706
+ var autoconvertTypedValues = (value) => {
707
+ if (typeof value === "string") {
708
+ return { type: "text", value };
709
+ }
710
+ const chatMessages = z2.array(chatMessageSchema).safeParse(value);
711
+ if (Array.isArray(value) && chatMessages.success) {
712
+ return {
713
+ type: "chat_messages",
714
+ value: chatMessages.data
715
+ };
716
+ }
717
+ try {
718
+ JSON.stringify(value);
719
+ return { type: "json", value };
720
+ } catch (e) {
721
+ return { type: "raw", value };
722
+ }
723
+ };
724
+
725
+ export {
726
+ __spreadValues,
727
+ __spreadProps,
728
+ reservedSpanParamsSchema,
729
+ reservedTraceMetadataSchema,
730
+ spanSchema,
731
+ collectorRESTParamsSchema,
732
+ convertFromVercelAIMessages,
733
+ captureError,
734
+ autoconvertTypedValues
735
+ };
736
+ //# sourceMappingURL=chunk-2I4YLOQY.mjs.map