langsmith 0.3.31 → 0.3.32-rc.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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/run_trees.cjs +154 -33
- package/dist/run_trees.d.ts +9 -1
- package/dist/run_trees.js +154 -33
- package/package.json +1 -1
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.3.
|
|
11
|
+
exports.__version__ = "0.3.32-rc.1";
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export { Client, type ClientConfig, type LangSmithTracingClientInterface, } from
|
|
|
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.3.
|
|
5
|
+
export declare const __version__ = "0.3.32-rc.1";
|
package/dist/index.js
CHANGED
package/dist/run_trees.cjs
CHANGED
|
@@ -56,7 +56,7 @@ function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
|
|
|
56
56
|
* Baggage header information
|
|
57
57
|
*/
|
|
58
58
|
class Baggage {
|
|
59
|
-
constructor(metadata, tags, project_name) {
|
|
59
|
+
constructor(metadata, tags, project_name, replicas) {
|
|
60
60
|
Object.defineProperty(this, "metadata", {
|
|
61
61
|
enumerable: true,
|
|
62
62
|
configurable: true,
|
|
@@ -75,15 +75,23 @@ class Baggage {
|
|
|
75
75
|
writable: true,
|
|
76
76
|
value: void 0
|
|
77
77
|
});
|
|
78
|
+
Object.defineProperty(this, "replicas", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: true,
|
|
82
|
+
value: void 0
|
|
83
|
+
});
|
|
78
84
|
this.metadata = metadata;
|
|
79
85
|
this.tags = tags;
|
|
80
86
|
this.project_name = project_name;
|
|
87
|
+
this.replicas = replicas;
|
|
81
88
|
}
|
|
82
89
|
static fromHeader(value) {
|
|
83
90
|
const items = value.split(",");
|
|
84
91
|
let metadata = {};
|
|
85
92
|
let tags = [];
|
|
86
93
|
let project_name;
|
|
94
|
+
let replicas;
|
|
87
95
|
for (const item of items) {
|
|
88
96
|
const [key, uriValue] = item.split("=");
|
|
89
97
|
const value = decodeURIComponent(uriValue);
|
|
@@ -96,8 +104,11 @@ class Baggage {
|
|
|
96
104
|
else if (key === "langsmith-project") {
|
|
97
105
|
project_name = value;
|
|
98
106
|
}
|
|
107
|
+
else if (key === "langsmith-replicas") {
|
|
108
|
+
replicas = JSON.parse(value);
|
|
109
|
+
}
|
|
99
110
|
}
|
|
100
|
-
return new Baggage(metadata, tags, project_name);
|
|
111
|
+
return new Baggage(metadata, tags, project_name, replicas);
|
|
101
112
|
}
|
|
102
113
|
toHeader() {
|
|
103
114
|
const items = [];
|
|
@@ -257,6 +268,15 @@ class RunTree {
|
|
|
257
268
|
writable: true,
|
|
258
269
|
value: void 0
|
|
259
270
|
});
|
|
271
|
+
/**
|
|
272
|
+
* Projects to replicate this run to with optional updates.
|
|
273
|
+
*/
|
|
274
|
+
Object.defineProperty(this, "replicas", {
|
|
275
|
+
enumerable: true,
|
|
276
|
+
configurable: true,
|
|
277
|
+
writable: true,
|
|
278
|
+
value: void 0
|
|
279
|
+
});
|
|
260
280
|
// If you pass in a run tree directly, return a shallow clone
|
|
261
281
|
if (isRunTree(originalConfig)) {
|
|
262
282
|
Object.assign(this, { ...originalConfig });
|
|
@@ -333,6 +353,7 @@ class RunTree {
|
|
|
333
353
|
...config,
|
|
334
354
|
parent_run: this,
|
|
335
355
|
project_name: this.project_name,
|
|
356
|
+
replicas: this.replicas,
|
|
336
357
|
client: this.client,
|
|
337
358
|
tracingEnabled: this.tracingEnabled,
|
|
338
359
|
execution_order: child_execution_order,
|
|
@@ -387,13 +408,16 @@ class RunTree {
|
|
|
387
408
|
}
|
|
388
409
|
_convertToCreate(run, runtimeEnv, excludeChildRuns = true) {
|
|
389
410
|
const runExtra = run.extra ?? {};
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
411
|
+
// Avoid overwriting the runtime environment if it's already set
|
|
412
|
+
if (runExtra?.runtime?.library === undefined) {
|
|
413
|
+
if (!runExtra.runtime) {
|
|
414
|
+
runExtra.runtime = {};
|
|
415
|
+
}
|
|
416
|
+
if (runtimeEnv) {
|
|
417
|
+
for (const [k, v] of Object.entries(runtimeEnv)) {
|
|
418
|
+
if (!runExtra.runtime[k]) {
|
|
419
|
+
runExtra.runtime[k] = v;
|
|
420
|
+
}
|
|
397
421
|
}
|
|
398
422
|
}
|
|
399
423
|
}
|
|
@@ -407,7 +431,7 @@ class RunTree {
|
|
|
407
431
|
parent_run_id = run.parent_run?.id;
|
|
408
432
|
child_runs = [];
|
|
409
433
|
}
|
|
410
|
-
|
|
434
|
+
return {
|
|
411
435
|
id: run.id,
|
|
412
436
|
name: run.name,
|
|
413
437
|
start_time: run.start_time,
|
|
@@ -426,14 +450,69 @@ class RunTree {
|
|
|
426
450
|
dotted_order: run.dotted_order,
|
|
427
451
|
tags: run.tags,
|
|
428
452
|
attachments: run.attachments,
|
|
453
|
+
events: run.events,
|
|
429
454
|
};
|
|
430
|
-
|
|
455
|
+
}
|
|
456
|
+
_remapForProject(projectName, runtimeEnv, excludeChildRuns = true) {
|
|
457
|
+
const baseRun = this._convertToCreate(this, runtimeEnv, excludeChildRuns);
|
|
458
|
+
if (projectName === this.project_name) {
|
|
459
|
+
return baseRun;
|
|
460
|
+
}
|
|
461
|
+
// Create a deterministic UUID mapping for this project
|
|
462
|
+
const createRemappedId = (originalId) => {
|
|
463
|
+
return uuid.v5(`${originalId}:${projectName}`, uuid.v5.DNS);
|
|
464
|
+
};
|
|
465
|
+
// Remap the current run's ID
|
|
466
|
+
const newId = createRemappedId(baseRun.id);
|
|
467
|
+
const newTraceId = baseRun.trace_id
|
|
468
|
+
? createRemappedId(baseRun.trace_id)
|
|
469
|
+
: undefined;
|
|
470
|
+
const newParentRunId = baseRun.parent_run_id
|
|
471
|
+
? createRemappedId(baseRun.parent_run_id)
|
|
472
|
+
: undefined;
|
|
473
|
+
let newDottedOrder;
|
|
474
|
+
if (baseRun.dotted_order) {
|
|
475
|
+
const segments = _parseDottedOrder(baseRun.dotted_order);
|
|
476
|
+
const rebuilt = [];
|
|
477
|
+
// Process all segments except the last one
|
|
478
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
479
|
+
const [timestamp, segmentId] = segments[i];
|
|
480
|
+
const remappedId = createRemappedId(segmentId);
|
|
481
|
+
rebuilt.push(timestamp.toISOString().replace(/[-:]/g, "").replace(".", "") +
|
|
482
|
+
remappedId);
|
|
483
|
+
}
|
|
484
|
+
// Process the last segment with the new run ID
|
|
485
|
+
const [lastTimestamp] = segments[segments.length - 1];
|
|
486
|
+
rebuilt.push(lastTimestamp.toISOString().replace(/[-:]/g, "").replace(".", "") +
|
|
487
|
+
newId);
|
|
488
|
+
newDottedOrder = rebuilt.join(".");
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
newDottedOrder = undefined;
|
|
492
|
+
}
|
|
493
|
+
const remappedRun = {
|
|
494
|
+
...baseRun,
|
|
495
|
+
id: newId,
|
|
496
|
+
trace_id: newTraceId,
|
|
497
|
+
parent_run_id: newParentRunId,
|
|
498
|
+
dotted_order: newDottedOrder,
|
|
499
|
+
session_name: projectName,
|
|
500
|
+
};
|
|
501
|
+
return remappedRun;
|
|
431
502
|
}
|
|
432
503
|
async postRun(excludeChildRuns = true) {
|
|
433
504
|
try {
|
|
434
505
|
const runtimeEnv = (0, env_js_1.getRuntimeEnvironment)();
|
|
435
|
-
|
|
436
|
-
|
|
506
|
+
if (this.replicas && this.replicas.length > 0) {
|
|
507
|
+
for (const [projectName] of this.replicas) {
|
|
508
|
+
const runCreate = this._remapForProject(projectName, runtimeEnv, true);
|
|
509
|
+
await this.client.createRun(runCreate);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
const runCreate = this._convertToCreate(this, runtimeEnv, excludeChildRuns);
|
|
514
|
+
await this.client.createRun(runCreate);
|
|
515
|
+
}
|
|
437
516
|
if (!excludeChildRuns) {
|
|
438
517
|
(0, warn_js_1.warnOnce)("Posting with excludeChildRuns=false is deprecated and will be removed in a future version.");
|
|
439
518
|
for (const childRun of this.child_runs) {
|
|
@@ -446,26 +525,49 @@ class RunTree {
|
|
|
446
525
|
}
|
|
447
526
|
}
|
|
448
527
|
async patchRun() {
|
|
449
|
-
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
528
|
+
if (this.replicas && this.replicas.length > 0) {
|
|
529
|
+
for (const [projectName, updates] of this.replicas) {
|
|
530
|
+
const runData = this._remapForProject(projectName);
|
|
531
|
+
await this.client.updateRun(runData.id, {
|
|
532
|
+
inputs: runData.inputs,
|
|
533
|
+
outputs: runData.outputs,
|
|
534
|
+
error: runData.error,
|
|
535
|
+
parent_run_id: runData.parent_run_id,
|
|
536
|
+
session_name: runData.session_name,
|
|
537
|
+
reference_example_id: runData.reference_example_id,
|
|
538
|
+
end_time: runData.end_time,
|
|
539
|
+
dotted_order: runData.dotted_order,
|
|
540
|
+
trace_id: runData.trace_id,
|
|
541
|
+
events: runData.events,
|
|
542
|
+
tags: runData.tags,
|
|
543
|
+
extra: runData.extra,
|
|
544
|
+
attachments: this.attachments,
|
|
545
|
+
...updates,
|
|
546
|
+
});
|
|
547
|
+
}
|
|
466
548
|
}
|
|
467
|
-
|
|
468
|
-
|
|
549
|
+
else {
|
|
550
|
+
try {
|
|
551
|
+
const runUpdate = {
|
|
552
|
+
end_time: this.end_time,
|
|
553
|
+
error: this.error,
|
|
554
|
+
inputs: this.inputs,
|
|
555
|
+
outputs: this.outputs,
|
|
556
|
+
parent_run_id: this.parent_run?.id,
|
|
557
|
+
reference_example_id: this.reference_example_id,
|
|
558
|
+
extra: this.extra,
|
|
559
|
+
events: this.events,
|
|
560
|
+
dotted_order: this.dotted_order,
|
|
561
|
+
trace_id: this.trace_id,
|
|
562
|
+
tags: this.tags,
|
|
563
|
+
attachments: this.attachments,
|
|
564
|
+
session_name: this.project_name,
|
|
565
|
+
};
|
|
566
|
+
await this.client.updateRun(this.id, runUpdate);
|
|
567
|
+
}
|
|
568
|
+
catch (error) {
|
|
569
|
+
console.error(`Error in patchRun for run ${this.id}`, error);
|
|
570
|
+
}
|
|
469
571
|
}
|
|
470
572
|
}
|
|
471
573
|
toJSON() {
|
|
@@ -569,13 +671,14 @@ class RunTree {
|
|
|
569
671
|
config.metadata = baggage.metadata;
|
|
570
672
|
config.tags = baggage.tags;
|
|
571
673
|
config.project_name = baggage.project_name;
|
|
674
|
+
config.replicas = baggage.replicas;
|
|
572
675
|
}
|
|
573
676
|
return new RunTree(config);
|
|
574
677
|
}
|
|
575
678
|
toHeaders(headers) {
|
|
576
679
|
const result = {
|
|
577
680
|
"langsmith-trace": this.dotted_order,
|
|
578
|
-
baggage: new Baggage(this.extra?.metadata, this.tags, this.project_name).toHeader(),
|
|
681
|
+
baggage: new Baggage(this.extra?.metadata, this.tags, this.project_name, this.replicas).toHeader(),
|
|
579
682
|
};
|
|
580
683
|
if (headers) {
|
|
581
684
|
for (const [key, value] of Object.entries(result)) {
|
|
@@ -622,3 +725,21 @@ function isRunnableConfigLike(x) {
|
|
|
622
725
|
// Or it's an array with a LangChainTracerLike object within it
|
|
623
726
|
containsLangChainTracerLike(x.callbacks)));
|
|
624
727
|
}
|
|
728
|
+
function _parseDottedOrder(dottedOrder) {
|
|
729
|
+
const parts = dottedOrder.split(".");
|
|
730
|
+
return parts.map((part) => {
|
|
731
|
+
const timestampStr = part.slice(0, -36);
|
|
732
|
+
const uuidStr = part.slice(-36);
|
|
733
|
+
// Parse timestamp: "%Y%m%dT%H%M%S%fZ" format
|
|
734
|
+
// Example: "20231215T143045123456Z"
|
|
735
|
+
const year = parseInt(timestampStr.slice(0, 4));
|
|
736
|
+
const month = parseInt(timestampStr.slice(4, 6)) - 1; // JS months are 0-indexed
|
|
737
|
+
const day = parseInt(timestampStr.slice(6, 8));
|
|
738
|
+
const hour = parseInt(timestampStr.slice(9, 11));
|
|
739
|
+
const minute = parseInt(timestampStr.slice(11, 13));
|
|
740
|
+
const second = parseInt(timestampStr.slice(13, 15));
|
|
741
|
+
const microsecond = parseInt(timestampStr.slice(15, 21));
|
|
742
|
+
const timestamp = new Date(year, month, day, hour, minute, second, microsecond / 1000);
|
|
743
|
+
return [timestamp, uuidStr];
|
|
744
|
+
});
|
|
745
|
+
}
|
package/dist/run_trees.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ export interface RunTreeConfig {
|
|
|
27
27
|
trace_id?: string;
|
|
28
28
|
dotted_order?: string;
|
|
29
29
|
attachments?: Attachments;
|
|
30
|
+
replicas?: [string, KVMap | undefined][];
|
|
30
31
|
}
|
|
31
32
|
export interface RunnableConfigLike {
|
|
32
33
|
/**
|
|
@@ -78,6 +79,10 @@ export declare class RunTree implements BaseRun {
|
|
|
78
79
|
* Each entry is a tuple of [mime_type, bytes]
|
|
79
80
|
*/
|
|
80
81
|
attachments?: Attachments;
|
|
82
|
+
/**
|
|
83
|
+
* Projects to replicate this run to with optional updates.
|
|
84
|
+
*/
|
|
85
|
+
replicas?: [string, KVMap | undefined][];
|
|
81
86
|
constructor(originalConfig: RunTreeConfig | RunTree);
|
|
82
87
|
set metadata(metadata: KVMap);
|
|
83
88
|
get metadata(): KVMap;
|
|
@@ -86,9 +91,12 @@ export declare class RunTree implements BaseRun {
|
|
|
86
91
|
createChild(config: RunTreeConfig): RunTree;
|
|
87
92
|
end(outputs?: KVMap, error?: string, endTime?: number, metadata?: KVMap): Promise<void>;
|
|
88
93
|
private _convertToCreate;
|
|
94
|
+
private _remapForProject;
|
|
89
95
|
postRun(excludeChildRuns?: boolean): Promise<void>;
|
|
90
96
|
patchRun(): Promise<void>;
|
|
91
|
-
toJSON(): RunCreate
|
|
97
|
+
toJSON(): RunCreate & {
|
|
98
|
+
id: string;
|
|
99
|
+
};
|
|
92
100
|
/**
|
|
93
101
|
* Add an event to the run tree.
|
|
94
102
|
* @param event - A single event or string to add
|
package/dist/run_trees.js
CHANGED
|
@@ -17,7 +17,7 @@ export function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
|
|
|
17
17
|
* Baggage header information
|
|
18
18
|
*/
|
|
19
19
|
class Baggage {
|
|
20
|
-
constructor(metadata, tags, project_name) {
|
|
20
|
+
constructor(metadata, tags, project_name, replicas) {
|
|
21
21
|
Object.defineProperty(this, "metadata", {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
configurable: true,
|
|
@@ -36,15 +36,23 @@ class Baggage {
|
|
|
36
36
|
writable: true,
|
|
37
37
|
value: void 0
|
|
38
38
|
});
|
|
39
|
+
Object.defineProperty(this, "replicas", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
39
45
|
this.metadata = metadata;
|
|
40
46
|
this.tags = tags;
|
|
41
47
|
this.project_name = project_name;
|
|
48
|
+
this.replicas = replicas;
|
|
42
49
|
}
|
|
43
50
|
static fromHeader(value) {
|
|
44
51
|
const items = value.split(",");
|
|
45
52
|
let metadata = {};
|
|
46
53
|
let tags = [];
|
|
47
54
|
let project_name;
|
|
55
|
+
let replicas;
|
|
48
56
|
for (const item of items) {
|
|
49
57
|
const [key, uriValue] = item.split("=");
|
|
50
58
|
const value = decodeURIComponent(uriValue);
|
|
@@ -57,8 +65,11 @@ class Baggage {
|
|
|
57
65
|
else if (key === "langsmith-project") {
|
|
58
66
|
project_name = value;
|
|
59
67
|
}
|
|
68
|
+
else if (key === "langsmith-replicas") {
|
|
69
|
+
replicas = JSON.parse(value);
|
|
70
|
+
}
|
|
60
71
|
}
|
|
61
|
-
return new Baggage(metadata, tags, project_name);
|
|
72
|
+
return new Baggage(metadata, tags, project_name, replicas);
|
|
62
73
|
}
|
|
63
74
|
toHeader() {
|
|
64
75
|
const items = [];
|
|
@@ -218,6 +229,15 @@ export class RunTree {
|
|
|
218
229
|
writable: true,
|
|
219
230
|
value: void 0
|
|
220
231
|
});
|
|
232
|
+
/**
|
|
233
|
+
* Projects to replicate this run to with optional updates.
|
|
234
|
+
*/
|
|
235
|
+
Object.defineProperty(this, "replicas", {
|
|
236
|
+
enumerable: true,
|
|
237
|
+
configurable: true,
|
|
238
|
+
writable: true,
|
|
239
|
+
value: void 0
|
|
240
|
+
});
|
|
221
241
|
// If you pass in a run tree directly, return a shallow clone
|
|
222
242
|
if (isRunTree(originalConfig)) {
|
|
223
243
|
Object.assign(this, { ...originalConfig });
|
|
@@ -294,6 +314,7 @@ export class RunTree {
|
|
|
294
314
|
...config,
|
|
295
315
|
parent_run: this,
|
|
296
316
|
project_name: this.project_name,
|
|
317
|
+
replicas: this.replicas,
|
|
297
318
|
client: this.client,
|
|
298
319
|
tracingEnabled: this.tracingEnabled,
|
|
299
320
|
execution_order: child_execution_order,
|
|
@@ -348,13 +369,16 @@ export class RunTree {
|
|
|
348
369
|
}
|
|
349
370
|
_convertToCreate(run, runtimeEnv, excludeChildRuns = true) {
|
|
350
371
|
const runExtra = run.extra ?? {};
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
372
|
+
// Avoid overwriting the runtime environment if it's already set
|
|
373
|
+
if (runExtra?.runtime?.library === undefined) {
|
|
374
|
+
if (!runExtra.runtime) {
|
|
375
|
+
runExtra.runtime = {};
|
|
376
|
+
}
|
|
377
|
+
if (runtimeEnv) {
|
|
378
|
+
for (const [k, v] of Object.entries(runtimeEnv)) {
|
|
379
|
+
if (!runExtra.runtime[k]) {
|
|
380
|
+
runExtra.runtime[k] = v;
|
|
381
|
+
}
|
|
358
382
|
}
|
|
359
383
|
}
|
|
360
384
|
}
|
|
@@ -368,7 +392,7 @@ export class RunTree {
|
|
|
368
392
|
parent_run_id = run.parent_run?.id;
|
|
369
393
|
child_runs = [];
|
|
370
394
|
}
|
|
371
|
-
|
|
395
|
+
return {
|
|
372
396
|
id: run.id,
|
|
373
397
|
name: run.name,
|
|
374
398
|
start_time: run.start_time,
|
|
@@ -387,14 +411,69 @@ export class RunTree {
|
|
|
387
411
|
dotted_order: run.dotted_order,
|
|
388
412
|
tags: run.tags,
|
|
389
413
|
attachments: run.attachments,
|
|
414
|
+
events: run.events,
|
|
390
415
|
};
|
|
391
|
-
|
|
416
|
+
}
|
|
417
|
+
_remapForProject(projectName, runtimeEnv, excludeChildRuns = true) {
|
|
418
|
+
const baseRun = this._convertToCreate(this, runtimeEnv, excludeChildRuns);
|
|
419
|
+
if (projectName === this.project_name) {
|
|
420
|
+
return baseRun;
|
|
421
|
+
}
|
|
422
|
+
// Create a deterministic UUID mapping for this project
|
|
423
|
+
const createRemappedId = (originalId) => {
|
|
424
|
+
return uuid.v5(`${originalId}:${projectName}`, uuid.v5.DNS);
|
|
425
|
+
};
|
|
426
|
+
// Remap the current run's ID
|
|
427
|
+
const newId = createRemappedId(baseRun.id);
|
|
428
|
+
const newTraceId = baseRun.trace_id
|
|
429
|
+
? createRemappedId(baseRun.trace_id)
|
|
430
|
+
: undefined;
|
|
431
|
+
const newParentRunId = baseRun.parent_run_id
|
|
432
|
+
? createRemappedId(baseRun.parent_run_id)
|
|
433
|
+
: undefined;
|
|
434
|
+
let newDottedOrder;
|
|
435
|
+
if (baseRun.dotted_order) {
|
|
436
|
+
const segments = _parseDottedOrder(baseRun.dotted_order);
|
|
437
|
+
const rebuilt = [];
|
|
438
|
+
// Process all segments except the last one
|
|
439
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
440
|
+
const [timestamp, segmentId] = segments[i];
|
|
441
|
+
const remappedId = createRemappedId(segmentId);
|
|
442
|
+
rebuilt.push(timestamp.toISOString().replace(/[-:]/g, "").replace(".", "") +
|
|
443
|
+
remappedId);
|
|
444
|
+
}
|
|
445
|
+
// Process the last segment with the new run ID
|
|
446
|
+
const [lastTimestamp] = segments[segments.length - 1];
|
|
447
|
+
rebuilt.push(lastTimestamp.toISOString().replace(/[-:]/g, "").replace(".", "") +
|
|
448
|
+
newId);
|
|
449
|
+
newDottedOrder = rebuilt.join(".");
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
newDottedOrder = undefined;
|
|
453
|
+
}
|
|
454
|
+
const remappedRun = {
|
|
455
|
+
...baseRun,
|
|
456
|
+
id: newId,
|
|
457
|
+
trace_id: newTraceId,
|
|
458
|
+
parent_run_id: newParentRunId,
|
|
459
|
+
dotted_order: newDottedOrder,
|
|
460
|
+
session_name: projectName,
|
|
461
|
+
};
|
|
462
|
+
return remappedRun;
|
|
392
463
|
}
|
|
393
464
|
async postRun(excludeChildRuns = true) {
|
|
394
465
|
try {
|
|
395
466
|
const runtimeEnv = getRuntimeEnvironment();
|
|
396
|
-
|
|
397
|
-
|
|
467
|
+
if (this.replicas && this.replicas.length > 0) {
|
|
468
|
+
for (const [projectName] of this.replicas) {
|
|
469
|
+
const runCreate = this._remapForProject(projectName, runtimeEnv, true);
|
|
470
|
+
await this.client.createRun(runCreate);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
const runCreate = this._convertToCreate(this, runtimeEnv, excludeChildRuns);
|
|
475
|
+
await this.client.createRun(runCreate);
|
|
476
|
+
}
|
|
398
477
|
if (!excludeChildRuns) {
|
|
399
478
|
warnOnce("Posting with excludeChildRuns=false is deprecated and will be removed in a future version.");
|
|
400
479
|
for (const childRun of this.child_runs) {
|
|
@@ -407,26 +486,49 @@ export class RunTree {
|
|
|
407
486
|
}
|
|
408
487
|
}
|
|
409
488
|
async patchRun() {
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
489
|
+
if (this.replicas && this.replicas.length > 0) {
|
|
490
|
+
for (const [projectName, updates] of this.replicas) {
|
|
491
|
+
const runData = this._remapForProject(projectName);
|
|
492
|
+
await this.client.updateRun(runData.id, {
|
|
493
|
+
inputs: runData.inputs,
|
|
494
|
+
outputs: runData.outputs,
|
|
495
|
+
error: runData.error,
|
|
496
|
+
parent_run_id: runData.parent_run_id,
|
|
497
|
+
session_name: runData.session_name,
|
|
498
|
+
reference_example_id: runData.reference_example_id,
|
|
499
|
+
end_time: runData.end_time,
|
|
500
|
+
dotted_order: runData.dotted_order,
|
|
501
|
+
trace_id: runData.trace_id,
|
|
502
|
+
events: runData.events,
|
|
503
|
+
tags: runData.tags,
|
|
504
|
+
extra: runData.extra,
|
|
505
|
+
attachments: this.attachments,
|
|
506
|
+
...updates,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
427
509
|
}
|
|
428
|
-
|
|
429
|
-
|
|
510
|
+
else {
|
|
511
|
+
try {
|
|
512
|
+
const runUpdate = {
|
|
513
|
+
end_time: this.end_time,
|
|
514
|
+
error: this.error,
|
|
515
|
+
inputs: this.inputs,
|
|
516
|
+
outputs: this.outputs,
|
|
517
|
+
parent_run_id: this.parent_run?.id,
|
|
518
|
+
reference_example_id: this.reference_example_id,
|
|
519
|
+
extra: this.extra,
|
|
520
|
+
events: this.events,
|
|
521
|
+
dotted_order: this.dotted_order,
|
|
522
|
+
trace_id: this.trace_id,
|
|
523
|
+
tags: this.tags,
|
|
524
|
+
attachments: this.attachments,
|
|
525
|
+
session_name: this.project_name,
|
|
526
|
+
};
|
|
527
|
+
await this.client.updateRun(this.id, runUpdate);
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
console.error(`Error in patchRun for run ${this.id}`, error);
|
|
531
|
+
}
|
|
430
532
|
}
|
|
431
533
|
}
|
|
432
534
|
toJSON() {
|
|
@@ -530,13 +632,14 @@ export class RunTree {
|
|
|
530
632
|
config.metadata = baggage.metadata;
|
|
531
633
|
config.tags = baggage.tags;
|
|
532
634
|
config.project_name = baggage.project_name;
|
|
635
|
+
config.replicas = baggage.replicas;
|
|
533
636
|
}
|
|
534
637
|
return new RunTree(config);
|
|
535
638
|
}
|
|
536
639
|
toHeaders(headers) {
|
|
537
640
|
const result = {
|
|
538
641
|
"langsmith-trace": this.dotted_order,
|
|
539
|
-
baggage: new Baggage(this.extra?.metadata, this.tags, this.project_name).toHeader(),
|
|
642
|
+
baggage: new Baggage(this.extra?.metadata, this.tags, this.project_name, this.replicas).toHeader(),
|
|
540
643
|
};
|
|
541
644
|
if (headers) {
|
|
542
645
|
for (const [key, value] of Object.entries(result)) {
|
|
@@ -582,3 +685,21 @@ export function isRunnableConfigLike(x) {
|
|
|
582
685
|
// Or it's an array with a LangChainTracerLike object within it
|
|
583
686
|
containsLangChainTracerLike(x.callbacks)));
|
|
584
687
|
}
|
|
688
|
+
function _parseDottedOrder(dottedOrder) {
|
|
689
|
+
const parts = dottedOrder.split(".");
|
|
690
|
+
return parts.map((part) => {
|
|
691
|
+
const timestampStr = part.slice(0, -36);
|
|
692
|
+
const uuidStr = part.slice(-36);
|
|
693
|
+
// Parse timestamp: "%Y%m%dT%H%M%S%fZ" format
|
|
694
|
+
// Example: "20231215T143045123456Z"
|
|
695
|
+
const year = parseInt(timestampStr.slice(0, 4));
|
|
696
|
+
const month = parseInt(timestampStr.slice(4, 6)) - 1; // JS months are 0-indexed
|
|
697
|
+
const day = parseInt(timestampStr.slice(6, 8));
|
|
698
|
+
const hour = parseInt(timestampStr.slice(9, 11));
|
|
699
|
+
const minute = parseInt(timestampStr.slice(11, 13));
|
|
700
|
+
const second = parseInt(timestampStr.slice(13, 15));
|
|
701
|
+
const microsecond = parseInt(timestampStr.slice(15, 21));
|
|
702
|
+
const timestamp = new Date(year, month, day, hour, minute, second, microsecond / 1000);
|
|
703
|
+
return [timestamp, uuidStr];
|
|
704
|
+
});
|
|
705
|
+
}
|