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