langsmith 0.2.0 → 0.2.1

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
@@ -8,4 +8,4 @@ Object.defineProperty(exports, "RunTree", { enumerable: true, get: function () {
8
8
  var fetch_js_1 = require("./singletons/fetch.cjs");
9
9
  Object.defineProperty(exports, "overrideFetchImplementation", { enumerable: true, get: function () { return fetch_js_1.overrideFetchImplementation; } });
10
10
  // Update using yarn bump-version
11
- exports.__version__ = "0.2.0";
11
+ exports.__version__ = "0.2.1";
package/dist/index.d.ts CHANGED
@@ -2,4 +2,4 @@ export { Client, type ClientConfig } from "./client.js";
2
2
  export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, } from "./schemas.js";
3
3
  export { RunTree, type RunTreeConfig } from "./run_trees.js";
4
4
  export { overrideFetchImplementation } from "./singletons/fetch.js";
5
- export declare const __version__ = "0.2.0";
5
+ export declare const __version__ = "0.2.1";
package/dist/index.js CHANGED
@@ -2,4 +2,4 @@ export { Client } from "./client.js";
2
2
  export { RunTree } from "./run_trees.js";
3
3
  export { overrideFetchImplementation } from "./singletons/fetch.js";
4
4
  // Update using yarn bump-version
5
- export const __version__ = "0.2.0";
5
+ export const __version__ = "0.2.1";
@@ -5,7 +5,3 @@
5
5
  * @param fetch The new fetch functino to use.
6
6
  */
7
7
  export declare const overrideFetchImplementation: (fetch: (...args: any[]) => any) => void;
8
- /**
9
- * @internal
10
- */
11
- export declare const _getFetchImplementation: () => (...args: any[]) => any;
@@ -0,0 +1,645 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AISDKExporter = void 0;
4
+ const index_js_1 = require("./index.cjs");
5
+ const uuid_1 = require("uuid");
6
+ const traceable_js_1 = require("./singletons/traceable.cjs");
7
+ const env_js_1 = require("./utils/env.cjs");
8
+ // Attempt to convert CoreMessage to a LangChain-compatible format
9
+ // which allows us to render messages more nicely in LangSmith
10
+ function convertCoreToSmith(message) {
11
+ if (message.role === "assistant") {
12
+ const data = { content: message.content };
13
+ if (Array.isArray(message.content)) {
14
+ data.content = message.content.map((part) => {
15
+ if (part.type === "text") {
16
+ return {
17
+ type: "text",
18
+ text: part.text,
19
+ ...part.experimental_providerMetadata,
20
+ };
21
+ }
22
+ if (part.type === "tool-call") {
23
+ return {
24
+ type: "tool_use",
25
+ name: part.toolName,
26
+ id: part.toolCallId,
27
+ input: part.args,
28
+ ...part.experimental_providerMetadata,
29
+ };
30
+ }
31
+ return part;
32
+ });
33
+ const toolCalls = message.content.filter((part) => part.type === "tool-call");
34
+ if (toolCalls.length > 0) {
35
+ data.additional_kwargs ??= {};
36
+ data.additional_kwargs.tool_calls = toolCalls.map((part) => {
37
+ return {
38
+ id: part.toolCallId,
39
+ type: "function",
40
+ function: {
41
+ name: part.toolName,
42
+ id: part.toolCallId,
43
+ arguments: JSON.stringify(part.args),
44
+ },
45
+ };
46
+ });
47
+ }
48
+ }
49
+ return { type: "ai", data };
50
+ }
51
+ if (message.role === "user") {
52
+ const data = { content: message.content };
53
+ if (Array.isArray(message.content)) {
54
+ data.content = message.content.map((part) => {
55
+ if (part.type === "text") {
56
+ return {
57
+ type: "text",
58
+ text: part.text,
59
+ ...part.experimental_providerMetadata,
60
+ };
61
+ }
62
+ if (part.type === "image") {
63
+ return {
64
+ type: "image_url",
65
+ image_url: part.image,
66
+ ...part.experimental_providerMetadata,
67
+ };
68
+ }
69
+ return part;
70
+ });
71
+ }
72
+ return { type: "human", data };
73
+ }
74
+ if (message.role === "system") {
75
+ return { type: "system", data: { content: message.content } };
76
+ }
77
+ if (message.role === "tool") {
78
+ const res = message.content.map((toolCall) => {
79
+ return {
80
+ type: "tool",
81
+ data: {
82
+ content: JSON.stringify(toolCall.result),
83
+ name: toolCall.toolName,
84
+ tool_call_id: toolCall.toolCallId,
85
+ },
86
+ };
87
+ });
88
+ if (res.length === 1)
89
+ return res[0];
90
+ return res;
91
+ }
92
+ return message;
93
+ }
94
+ const tryJson = (str) => {
95
+ try {
96
+ if (!str)
97
+ return str;
98
+ if (typeof str !== "string")
99
+ return str;
100
+ return JSON.parse(str);
101
+ }
102
+ catch {
103
+ return str;
104
+ }
105
+ };
106
+ function stripNonAlphanumeric(input) {
107
+ return input.replace(/[-:.]/g, "");
108
+ }
109
+ function convertToDottedOrderFormat([seconds, nanoseconds], runId, executionOrder) {
110
+ // Date only has millisecond precision, so we use the microseconds to break
111
+ // possible ties, avoiding incorrect run order
112
+ const ms = Number(String(nanoseconds).slice(0, 3));
113
+ const ns = String(Number(String(nanoseconds).slice(3, 6)) + executionOrder)
114
+ .padStart(3, "0")
115
+ .slice(0, 3);
116
+ return (stripNonAlphanumeric(`${new Date(seconds * 1000 + ms).toISOString().slice(0, -1)}${ns}Z`) + runId);
117
+ }
118
+ function convertToTimestamp([seconds, nanoseconds]) {
119
+ const ms = String(nanoseconds).slice(0, 3);
120
+ return Number(String(seconds) + ms);
121
+ }
122
+ function sortByHr(a, b) {
123
+ if (a[0] !== b[0])
124
+ return Math.sign(a[0] - b[0]);
125
+ return Math.sign(a[1] - b[1]);
126
+ }
127
+ const ROOT = "$";
128
+ const RUN_ID_NAMESPACE = "5c718b20-9078-11ef-9a3d-325096b39f47";
129
+ const RUN_ID_METADATA_KEY = {
130
+ input: "langsmith:runId",
131
+ output: "ai.telemetry.metadata.langsmith:runId",
132
+ };
133
+ const RUN_NAME_METADATA_KEY = {
134
+ input: "langsmith:runName",
135
+ output: "ai.telemetry.metadata.langsmith:runName",
136
+ };
137
+ const TRACE_METADATA_KEY = {
138
+ input: "langsmith:trace",
139
+ output: "ai.telemetry.metadata.langsmith:trace",
140
+ };
141
+ const BAGGAGE_METADATA_KEY = {
142
+ input: "langsmith:baggage",
143
+ output: "ai.telemetry.metadata.langsmith:baggage",
144
+ };
145
+ const RESERVED_METADATA_KEYS = [
146
+ RUN_ID_METADATA_KEY.output,
147
+ RUN_NAME_METADATA_KEY.output,
148
+ TRACE_METADATA_KEY.output,
149
+ BAGGAGE_METADATA_KEY.output,
150
+ ];
151
+ /**
152
+ * OpenTelemetry trace exporter for Vercel AI SDK.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * import { AISDKExporter } from "langsmith/vercel";
157
+ * import { Client } from "langsmith";
158
+ *
159
+ * import { generateText } from "ai";
160
+ * import { openai } from "@ai-sdk/openai";
161
+ *
162
+ * import { NodeSDK } from "@opentelemetry/sdk-node";
163
+ * import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
164
+ *
165
+ * const client = new Client();
166
+ *
167
+ * const sdk = new NodeSDK({
168
+ * traceExporter: new AISDKExporter({ client }),
169
+ * instrumentations: [getNodeAutoInstrumentations()],
170
+ * });
171
+ *
172
+ * sdk.start();
173
+ *
174
+ * const res = await generateText({
175
+ * model: openai("gpt-4o-mini"),
176
+ * messages: [
177
+ * {
178
+ * role: "user",
179
+ * content: "What color is the sky?",
180
+ * },
181
+ * ],
182
+ * experimental_telemetry: AISDKExporter.getSettings({
183
+ * runName: "langsmith_traced_call",
184
+ * metadata: { userId: "123", language: "english" },
185
+ * }),
186
+ * });
187
+ *
188
+ * await sdk.shutdown();
189
+ * ```
190
+ */
191
+ class AISDKExporter {
192
+ constructor(args) {
193
+ Object.defineProperty(this, "client", {
194
+ enumerable: true,
195
+ configurable: true,
196
+ writable: true,
197
+ value: void 0
198
+ });
199
+ Object.defineProperty(this, "traceByMap", {
200
+ enumerable: true,
201
+ configurable: true,
202
+ writable: true,
203
+ value: {}
204
+ });
205
+ /** @internal */
206
+ Object.defineProperty(this, "getSpanAttributeKey", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: (span, key) => {
211
+ const attributes = span.attributes;
212
+ return key in attributes && typeof attributes[key] === "string"
213
+ ? attributes[key]
214
+ : undefined;
215
+ }
216
+ });
217
+ this.client = args?.client ?? new index_js_1.Client();
218
+ }
219
+ static getSettings(settings) {
220
+ const { runId, runName, ...rest } = settings ?? {};
221
+ const metadata = { ...rest?.metadata };
222
+ if (runId != null)
223
+ metadata[RUN_ID_METADATA_KEY.input] = runId;
224
+ if (runName != null)
225
+ metadata[RUN_NAME_METADATA_KEY.input] = runName;
226
+ // attempt to obtain the run tree if used within a traceable function
227
+ let defaultEnabled = true;
228
+ try {
229
+ const runTree = (0, traceable_js_1.getCurrentRunTree)();
230
+ const headers = runTree.toHeaders();
231
+ metadata[TRACE_METADATA_KEY.input] = headers["langsmith-trace"];
232
+ metadata[BAGGAGE_METADATA_KEY.input] = headers["baggage"];
233
+ // honor the tracingEnabled flag if coming from traceable
234
+ if (runTree.tracingEnabled != null) {
235
+ defaultEnabled = runTree.tracingEnabled;
236
+ }
237
+ }
238
+ catch {
239
+ // pass
240
+ }
241
+ if (metadata[RUN_ID_METADATA_KEY.input] &&
242
+ metadata[TRACE_METADATA_KEY.input]) {
243
+ throw new Error("Cannot provide `runId` when used within traceable function.");
244
+ }
245
+ return { ...rest, isEnabled: rest.isEnabled ?? defaultEnabled, metadata };
246
+ }
247
+ /** @internal */
248
+ parseInteropFromMetadata(span) {
249
+ const userTraceId = this.getSpanAttributeKey(span, RUN_ID_METADATA_KEY.output);
250
+ const parentTrace = this.getSpanAttributeKey(span, TRACE_METADATA_KEY.output);
251
+ if (parentTrace && userTraceId) {
252
+ throw new Error(`Cannot provide both "${RUN_ID_METADATA_KEY.input}" and "${TRACE_METADATA_KEY.input}" metadata keys.`);
253
+ }
254
+ if (parentTrace) {
255
+ const parentRunTree = index_js_1.RunTree.fromHeaders({
256
+ "langsmith-trace": parentTrace,
257
+ baggage: this.getSpanAttributeKey(span, BAGGAGE_METADATA_KEY.output) || "",
258
+ });
259
+ if (!parentRunTree)
260
+ throw new Error("Unreachable code: empty parent run tree");
261
+ return { type: "traceable", parentRunTree };
262
+ }
263
+ if (userTraceId)
264
+ return { type: "user", userTraceId };
265
+ return undefined;
266
+ }
267
+ /** @internal */
268
+ getRunCreate(span) {
269
+ const runId = (0, uuid_1.v5)(span.spanContext().spanId, RUN_ID_NAMESPACE);
270
+ const parentRunId = span.parentSpanId
271
+ ? (0, uuid_1.v5)(span.parentSpanId, RUN_ID_NAMESPACE)
272
+ : undefined;
273
+ const asRunCreate = (rawConfig) => {
274
+ const aiMetadata = Object.keys(span.attributes)
275
+ .filter((key) => key.startsWith("ai.telemetry.metadata.") &&
276
+ !RESERVED_METADATA_KEYS.includes(key))
277
+ .reduce((acc, key) => {
278
+ acc[key.slice("ai.telemetry.metadata.".length)] =
279
+ span.attributes[key];
280
+ return acc;
281
+ }, {});
282
+ if (("ai.telemetry.functionId" in span.attributes &&
283
+ span.attributes["ai.telemetry.functionId"]) ||
284
+ ("resource.name" in span.attributes && span.attributes["resource.name"])) {
285
+ aiMetadata["functionId"] =
286
+ span.attributes["ai.telemetry.functionId"] ||
287
+ span.attributes["resource.name"];
288
+ }
289
+ const parsedStart = convertToTimestamp(span.startTime);
290
+ const parsedEnd = convertToTimestamp(span.endTime);
291
+ let name = rawConfig.name;
292
+ // if user provided a custom name, only use it if it's the root
293
+ if (span.parentSpanId == null) {
294
+ name =
295
+ this.getSpanAttributeKey(span, RUN_NAME_METADATA_KEY.output) || name;
296
+ }
297
+ const config = {
298
+ ...rawConfig,
299
+ name,
300
+ id: runId,
301
+ parent_run_id: parentRunId,
302
+ extra: {
303
+ ...rawConfig.extra,
304
+ metadata: {
305
+ ...rawConfig.extra?.metadata,
306
+ ...aiMetadata,
307
+ "ai.operationId": span.attributes["ai.operationId"],
308
+ },
309
+ },
310
+ session_name: (0, env_js_1.getLangSmithEnvironmentVariable)("PROJECT") ??
311
+ (0, env_js_1.getLangSmithEnvironmentVariable)("SESSION"),
312
+ start_time: Math.min(parsedStart, parsedEnd),
313
+ end_time: Math.max(parsedStart, parsedEnd),
314
+ };
315
+ return config;
316
+ };
317
+ switch (span.name) {
318
+ case "ai.generateText.doGenerate":
319
+ case "ai.generateText":
320
+ case "ai.streamText.doStream":
321
+ case "ai.streamText": {
322
+ const inputs = (() => {
323
+ if ("ai.prompt.messages" in span.attributes) {
324
+ return {
325
+ messages: tryJson(span.attributes["ai.prompt.messages"]).flatMap((i) => convertCoreToSmith(i)),
326
+ };
327
+ }
328
+ if ("ai.prompt" in span.attributes) {
329
+ const input = tryJson(span.attributes["ai.prompt"]);
330
+ if (typeof input === "object" &&
331
+ input != null &&
332
+ "messages" in input &&
333
+ Array.isArray(input.messages)) {
334
+ return {
335
+ messages: input.messages.flatMap((i) => convertCoreToSmith(i)),
336
+ };
337
+ }
338
+ return { input };
339
+ }
340
+ return {};
341
+ })();
342
+ const outputs = (() => {
343
+ let result = undefined;
344
+ if (span.attributes["ai.response.toolCalls"]) {
345
+ let content = tryJson(span.attributes["ai.response.toolCalls"]);
346
+ if (Array.isArray(content)) {
347
+ content = content.map((i) => ({
348
+ type: "tool-call",
349
+ ...i,
350
+ args: tryJson(i.args),
351
+ }));
352
+ }
353
+ result = {
354
+ llm_output: convertCoreToSmith({
355
+ role: "assistant",
356
+ content,
357
+ }),
358
+ };
359
+ }
360
+ else if (span.attributes["ai.response.text"]) {
361
+ result = {
362
+ llm_output: convertCoreToSmith({
363
+ role: "assistant",
364
+ content: span.attributes["ai.response.text"],
365
+ }),
366
+ };
367
+ }
368
+ if (span.attributes["ai.usage.completionTokens"]) {
369
+ result ??= {};
370
+ result.llm_output ??= {};
371
+ result.llm_output.token_usage ??= {};
372
+ result.llm_output.token_usage["completion_tokens"] =
373
+ span.attributes["ai.usage.completionTokens"];
374
+ }
375
+ if (span.attributes["ai.usage.promptTokens"]) {
376
+ result ??= {};
377
+ result.llm_output ??= {};
378
+ result.llm_output.token_usage ??= {};
379
+ result.llm_output.token_usage["prompt_tokens"] =
380
+ span.attributes["ai.usage.promptTokens"];
381
+ }
382
+ return result;
383
+ })();
384
+ const events = [];
385
+ const firstChunkEvent = span.events.find((i) => i.name === "ai.stream.firstChunk");
386
+ if (firstChunkEvent) {
387
+ events.push({
388
+ name: "new_token",
389
+ time: convertToTimestamp(firstChunkEvent.time),
390
+ });
391
+ }
392
+ // TODO: add first_token_time
393
+ return asRunCreate({
394
+ run_type: "llm",
395
+ name: span.attributes["ai.model.provider"],
396
+ inputs,
397
+ outputs,
398
+ events,
399
+ extra: {
400
+ batch_size: 1,
401
+ metadata: {
402
+ ls_provider: span.attributes["ai.model.provider"]
403
+ .split(".")
404
+ .at(0),
405
+ ls_model_type: span.attributes["ai.model.provider"]
406
+ .split(".")
407
+ .at(1),
408
+ ls_model_name: span.attributes["ai.model.id"],
409
+ },
410
+ },
411
+ });
412
+ }
413
+ case "ai.toolCall": {
414
+ const args = tryJson(span.attributes["ai.toolCall.args"]);
415
+ let inputs = { args };
416
+ if (typeof args === "object" && args != null) {
417
+ inputs = args;
418
+ }
419
+ const output = tryJson(span.attributes["ai.toolCall.result"]);
420
+ let outputs = { output };
421
+ if (typeof output === "object" && output != null) {
422
+ outputs = output;
423
+ }
424
+ return asRunCreate({
425
+ run_type: "tool",
426
+ name: span.attributes["ai.toolCall.name"],
427
+ inputs,
428
+ outputs,
429
+ });
430
+ }
431
+ case "ai.streamObject":
432
+ case "ai.streamObject.doStream":
433
+ case "ai.generateObject":
434
+ case "ai.generateObject.doGenerate": {
435
+ const inputs = (() => {
436
+ if ("ai.prompt.messages" in span.attributes) {
437
+ return {
438
+ messages: tryJson(span.attributes["ai.prompt.messages"]).flatMap((i) => convertCoreToSmith(i)),
439
+ };
440
+ }
441
+ if ("ai.prompt" in span.attributes) {
442
+ return { input: tryJson(span.attributes["ai.prompt"]) };
443
+ }
444
+ return {};
445
+ })();
446
+ const outputs = (() => {
447
+ let result = undefined;
448
+ if (span.attributes["ai.response.object"]) {
449
+ result = {
450
+ output: tryJson(span.attributes["ai.response.object"]),
451
+ };
452
+ }
453
+ if (span.attributes["ai.usage.completionTokens"]) {
454
+ result ??= {};
455
+ result.llm_output ??= {};
456
+ result.llm_output.token_usage ??= {};
457
+ result.llm_output.token_usage["completion_tokens"] =
458
+ span.attributes["ai.usage.completionTokens"];
459
+ }
460
+ if (span.attributes["ai.usage.promptTokens"]) {
461
+ result ??= {};
462
+ result.llm_output ??= {};
463
+ result.llm_output.token_usage ??= {};
464
+ result.llm_output.token_usage["prompt_tokens"] =
465
+ +span.attributes["ai.usage.promptTokens"];
466
+ }
467
+ return result;
468
+ })();
469
+ const events = [];
470
+ const firstChunkEvent = span.events.find((i) => i.name === "ai.stream.firstChunk");
471
+ if (firstChunkEvent) {
472
+ events.push({
473
+ name: "new_token",
474
+ time: convertToTimestamp(firstChunkEvent.time),
475
+ });
476
+ }
477
+ return asRunCreate({
478
+ run_type: "llm",
479
+ name: span.attributes["ai.model.provider"],
480
+ inputs,
481
+ outputs,
482
+ events,
483
+ extra: {
484
+ batch_size: 1,
485
+ metadata: {
486
+ ls_provider: span.attributes["ai.model.provider"]
487
+ .split(".")
488
+ .at(0),
489
+ ls_model_type: span.attributes["ai.model.provider"]
490
+ .split(".")
491
+ .at(1),
492
+ ls_model_name: span.attributes["ai.model.id"],
493
+ },
494
+ },
495
+ });
496
+ }
497
+ case "ai.embed":
498
+ case "ai.embed.doEmbed":
499
+ case "ai.embedMany":
500
+ case "ai.embedMany.doEmbed":
501
+ default:
502
+ return undefined;
503
+ }
504
+ }
505
+ /** @internal */
506
+ isRootRun(span) {
507
+ switch (span.name) {
508
+ case "ai.generateText":
509
+ case "ai.streamText":
510
+ case "ai.generateObject":
511
+ case "ai.streamObject":
512
+ case "ai.embed":
513
+ case "ai.embedMany":
514
+ return true;
515
+ default:
516
+ return false;
517
+ }
518
+ }
519
+ export(spans, resultCallback) {
520
+ const typedSpans = spans
521
+ .slice()
522
+ .sort((a, b) => sortByHr(a.startTime, b.startTime));
523
+ for (const span of typedSpans) {
524
+ const { traceId, spanId } = span.spanContext();
525
+ const parentId = span.parentSpanId ?? undefined;
526
+ this.traceByMap[traceId] ??= {
527
+ childMap: {},
528
+ nodeMap: {},
529
+ relativeExecutionOrder: {},
530
+ };
531
+ const runId = (0, uuid_1.v5)(spanId, RUN_ID_NAMESPACE);
532
+ let parentRunId = parentId
533
+ ? (0, uuid_1.v5)(parentId, RUN_ID_NAMESPACE)
534
+ : undefined;
535
+ // in LangSmith we currently only support certain spans
536
+ // which may be deeply nested within other traces
537
+ if (this.isRootRun(span))
538
+ parentRunId = undefined;
539
+ const traceMap = this.traceByMap[traceId];
540
+ const run = this.getRunCreate(span);
541
+ if (!run)
542
+ continue;
543
+ traceMap.relativeExecutionOrder[parentRunId ?? ROOT] ??= -1;
544
+ traceMap.relativeExecutionOrder[parentRunId ?? ROOT] += 1;
545
+ traceMap.nodeMap[runId] ??= {
546
+ id: runId,
547
+ parentId: parentRunId,
548
+ startTime: span.startTime,
549
+ run,
550
+ sent: false,
551
+ executionOrder: traceMap.relativeExecutionOrder[parentRunId ?? ROOT],
552
+ };
553
+ traceMap.childMap[parentRunId ?? ROOT] ??= [];
554
+ traceMap.childMap[parentRunId ?? ROOT].push(traceMap.nodeMap[runId]);
555
+ traceMap.interop = this.parseInteropFromMetadata(span);
556
+ }
557
+ // We separate `id`,
558
+ const sampled = [];
559
+ for (const traceId of Object.keys(this.traceByMap)) {
560
+ const traceMap = this.traceByMap[traceId];
561
+ const queue = traceMap.childMap[ROOT]?.map((item) => ({
562
+ item,
563
+ dottedOrder: convertToDottedOrderFormat(item.startTime, item.id, item.executionOrder),
564
+ traceId: item.id,
565
+ })) ?? [];
566
+ const seen = new Set();
567
+ while (queue.length) {
568
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
569
+ const task = queue.shift();
570
+ if (seen.has(task.item.id))
571
+ continue;
572
+ if (!task.item.sent) {
573
+ let override = {
574
+ id: task.item.id,
575
+ parent_run_id: task.item.parentId,
576
+ dotted_order: task.dottedOrder,
577
+ trace_id: task.traceId,
578
+ };
579
+ if (traceMap.interop) {
580
+ // attach the run to a parent run tree
581
+ // - id: preserve
582
+ // - parent_run_id: use existing parent run id or hook to the provided run tree
583
+ // - dotted_order: append to the dotted_order of the parent run tree
584
+ // - trace_id: use from the existing run tree
585
+ if (traceMap.interop.type === "traceable") {
586
+ override = {
587
+ id: override.id,
588
+ parent_run_id: override.parent_run_id ?? traceMap.interop.parentRunTree.id,
589
+ dotted_order: [
590
+ traceMap.interop.parentRunTree.dotted_order,
591
+ override.dotted_order,
592
+ ]
593
+ .filter(Boolean)
594
+ .join("."),
595
+ trace_id: traceMap.interop.parentRunTree.trace_id,
596
+ };
597
+ }
598
+ else if (traceMap.interop.type === "user") {
599
+ // Allow user to specify custom trace ID = run ID of the root run
600
+ // - id: use user provided run ID if root run, otherwise preserve
601
+ // - parent_run_id: use user provided run ID if root run, otherwise preserve
602
+ // - dotted_order: replace the trace_id with the user provided run ID
603
+ // - trace_id: use user provided run ID
604
+ const userTraceId = traceMap.interop.userTraceId ?? (0, uuid_1.v4)();
605
+ override = {
606
+ id: override.id === override.trace_id ? userTraceId : override.id,
607
+ parent_run_id: override.parent_run_id === override.trace_id
608
+ ? userTraceId
609
+ : override.parent_run_id,
610
+ dotted_order: override.dotted_order.replace(override.trace_id, userTraceId),
611
+ trace_id: userTraceId,
612
+ };
613
+ }
614
+ }
615
+ sampled.push([override, task.item.run]);
616
+ task.item.sent = true;
617
+ }
618
+ const children = traceMap.childMap[task.item.id] ?? [];
619
+ queue.push(...children.map((child) => {
620
+ return {
621
+ item: child,
622
+ dottedOrder: [
623
+ task.dottedOrder,
624
+ convertToDottedOrderFormat(child.startTime, child.id, child.executionOrder),
625
+ ].join("."),
626
+ traceId: task.traceId,
627
+ };
628
+ }));
629
+ }
630
+ }
631
+ Promise.all(sampled.map(([override, value]) => this.client.createRun({ ...value, ...override }))).then(() => resultCallback({ code: 0 }), (error) => resultCallback({ code: 1, error }));
632
+ }
633
+ async shutdown() {
634
+ // find nodes which are incomplete
635
+ const incompleteNodes = Object.values(this.traceByMap).flatMap((trace) => Object.values(trace.nodeMap).filter((i) => !i.sent));
636
+ if (incompleteNodes.length > 0) {
637
+ console.warn("Some incomplete nodes were found before shutdown and not sent to LangSmith.");
638
+ }
639
+ await this.client?.awaitPendingTraceBatches();
640
+ }
641
+ async forceFlush() {
642
+ await this.client?.awaitPendingTraceBatches();
643
+ }
644
+ }
645
+ exports.AISDKExporter = AISDKExporter;