langsmith 0.0.1-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.
Files changed (50) hide show
  1. package/README.md +262 -0
  2. package/client.cjs +1 -0
  3. package/client.d.ts +1 -0
  4. package/client.js +1 -0
  5. package/dist/cli/docker-compose.ngrok.yaml +17 -0
  6. package/dist/cli/docker-compose.yaml +43 -0
  7. package/dist/cli/main.cjs +278 -0
  8. package/dist/cli/main.d.ts +1 -0
  9. package/dist/cli/main.js +252 -0
  10. package/dist/cli/main.ts +292 -0
  11. package/dist/client.cjs +588 -0
  12. package/dist/client.d.ts +130 -0
  13. package/dist/client.js +561 -0
  14. package/dist/evaluation/evaluator.cjs +2 -0
  15. package/dist/evaluation/evaluator.d.ts +12 -0
  16. package/dist/evaluation/evaluator.js +1 -0
  17. package/dist/evaluation/index.cjs +5 -0
  18. package/dist/evaluation/index.d.ts +2 -0
  19. package/dist/evaluation/index.js +1 -0
  20. package/dist/evaluation/string_evaluator.cjs +66 -0
  21. package/dist/evaluation/string_evaluator.d.ts +30 -0
  22. package/dist/evaluation/string_evaluator.js +62 -0
  23. package/dist/index.cjs +7 -0
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.js +2 -0
  26. package/dist/run_trees.cjs +232 -0
  27. package/dist/run_trees.d.ts +47 -0
  28. package/dist/run_trees.js +205 -0
  29. package/dist/schemas.cjs +2 -0
  30. package/dist/schemas.d.ts +117 -0
  31. package/dist/schemas.js +1 -0
  32. package/dist/utils/async_caller.cjs +111 -0
  33. package/dist/utils/async_caller.d.ts +37 -0
  34. package/dist/utils/async_caller.js +104 -0
  35. package/dist/utils/env.cjs +80 -0
  36. package/dist/utils/env.d.ts +22 -0
  37. package/dist/utils/env.js +68 -0
  38. package/evaluation.cjs +1 -0
  39. package/evaluation.d.ts +1 -0
  40. package/evaluation.js +1 -0
  41. package/index.cjs +1 -0
  42. package/index.d.ts +1 -0
  43. package/index.js +1 -0
  44. package/package.json +121 -0
  45. package/run_trees.cjs +1 -0
  46. package/run_trees.d.ts +1 -0
  47. package/run_trees.js +1 -0
  48. package/schemas.cjs +1 -0
  49. package/schemas.d.ts +1 -0
  50. package/schemas.js +1 -0
@@ -0,0 +1,12 @@
1
+ import { Example, KVMap, Run, ScoreType, ValueType } from "../schemas.js";
2
+ export interface EvaluationResult {
3
+ key: string;
4
+ score?: ScoreType;
5
+ value?: ValueType;
6
+ comment?: string;
7
+ correction?: string | object;
8
+ evaluatorInfo?: KVMap;
9
+ }
10
+ export interface RunEvaluator {
11
+ evaluateRun(run: Run, example?: Example): Promise<EvaluationResult>;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringEvaluator = void 0;
4
+ var string_evaluator_js_1 = require("./string_evaluator.cjs");
5
+ Object.defineProperty(exports, "StringEvaluator", { enumerable: true, get: function () { return string_evaluator_js_1.StringEvaluator; } });
@@ -0,0 +1,2 @@
1
+ export { RunEvaluator, EvaluationResult } from "./evaluator.js";
2
+ export { StringEvaluator, GradingFunctionParams, GradingFunctionResult, } from "./string_evaluator.js";
@@ -0,0 +1 @@
1
+ export { StringEvaluator, } from "./string_evaluator.js";
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringEvaluator = void 0;
4
+ class StringEvaluator {
5
+ constructor(params) {
6
+ Object.defineProperty(this, "evaluationName", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: void 0
11
+ });
12
+ Object.defineProperty(this, "inputKey", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "predictionKey", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "answerKey", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "gradingFunction", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ this.evaluationName = params.evaluationName;
37
+ this.inputKey = params.inputKey ?? "input";
38
+ this.predictionKey = params.predictionKey ?? "output";
39
+ this.answerKey =
40
+ params.answerKey !== undefined ? params.answerKey : "output";
41
+ this.gradingFunction = params.gradingFunction;
42
+ }
43
+ async evaluateRun(run, example) {
44
+ if (!run.outputs) {
45
+ throw new Error("Run outputs cannot be undefined.");
46
+ }
47
+ const functionInputs = {
48
+ input: run.inputs[this.inputKey],
49
+ prediction: run.outputs[this.predictionKey],
50
+ answer: this.answerKey ? example?.outputs?.[this.answerKey] : null,
51
+ };
52
+ const gradingResults = await this.gradingFunction(functionInputs);
53
+ const key = gradingResults.key || this.evaluationName;
54
+ if (!key) {
55
+ throw new Error("Evaluation name cannot be undefined.");
56
+ }
57
+ return {
58
+ key,
59
+ score: gradingResults.score,
60
+ value: gradingResults.value,
61
+ comment: gradingResults.comment,
62
+ correction: gradingResults.correction,
63
+ };
64
+ }
65
+ }
66
+ exports.StringEvaluator = StringEvaluator;
@@ -0,0 +1,30 @@
1
+ import { Example, Run, ScoreType, ValueType } from "../schemas.js";
2
+ import { EvaluationResult, RunEvaluator } from "./evaluator.js";
3
+ export interface GradingFunctionResult {
4
+ key?: string;
5
+ score?: ScoreType;
6
+ value?: ValueType;
7
+ comment?: string;
8
+ correction?: string | object;
9
+ }
10
+ export interface GradingFunctionParams {
11
+ input: string;
12
+ prediction: string;
13
+ answer?: string;
14
+ }
15
+ export interface StringEvaluatorParams {
16
+ evaluationName?: string;
17
+ inputKey?: string;
18
+ predictionKey?: string;
19
+ answerKey?: string;
20
+ gradingFunction: (params: GradingFunctionParams) => Promise<GradingFunctionResult>;
21
+ }
22
+ export declare class StringEvaluator implements RunEvaluator {
23
+ protected evaluationName?: string;
24
+ protected inputKey: string;
25
+ protected predictionKey: string;
26
+ protected answerKey?: string;
27
+ protected gradingFunction: (params: GradingFunctionParams) => Promise<GradingFunctionResult>;
28
+ constructor(params: StringEvaluatorParams);
29
+ evaluateRun(run: Run, example?: Example): Promise<EvaluationResult>;
30
+ }
@@ -0,0 +1,62 @@
1
+ export class StringEvaluator {
2
+ constructor(params) {
3
+ Object.defineProperty(this, "evaluationName", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: void 0
8
+ });
9
+ Object.defineProperty(this, "inputKey", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "predictionKey", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "answerKey", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "gradingFunction", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ this.evaluationName = params.evaluationName;
34
+ this.inputKey = params.inputKey ?? "input";
35
+ this.predictionKey = params.predictionKey ?? "output";
36
+ this.answerKey =
37
+ params.answerKey !== undefined ? params.answerKey : "output";
38
+ this.gradingFunction = params.gradingFunction;
39
+ }
40
+ async evaluateRun(run, example) {
41
+ if (!run.outputs) {
42
+ throw new Error("Run outputs cannot be undefined.");
43
+ }
44
+ const functionInputs = {
45
+ input: run.inputs[this.inputKey],
46
+ prediction: run.outputs[this.predictionKey],
47
+ answer: this.answerKey ? example?.outputs?.[this.answerKey] : null,
48
+ };
49
+ const gradingResults = await this.gradingFunction(functionInputs);
50
+ const key = gradingResults.key || this.evaluationName;
51
+ if (!key) {
52
+ throw new Error("Evaluation name cannot be undefined.");
53
+ }
54
+ return {
55
+ key,
56
+ score: gradingResults.score,
57
+ value: gradingResults.value,
58
+ comment: gradingResults.comment,
59
+ correction: gradingResults.correction,
60
+ };
61
+ }
62
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RunTree = exports.Client = void 0;
4
+ var client_js_1 = require("./client.cjs");
5
+ Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_js_1.Client; } });
6
+ var run_trees_js_1 = require("./run_trees.cjs");
7
+ Object.defineProperty(exports, "RunTree", { enumerable: true, get: function () { return run_trees_js_1.RunTree; } });
@@ -0,0 +1,3 @@
1
+ export { Client } from "./client.js";
2
+ export { Dataset, Example, TracerSession, Run, Feedback } from "./schemas.js";
3
+ export { RunTree } from "./run_trees.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Client } from "./client.js";
2
+ export { RunTree } from "./run_trees.js";
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.RunTree = void 0;
27
+ const uuid = __importStar(require("uuid"));
28
+ const env_js_1 = require("./utils/env.cjs");
29
+ const client_js_1 = require("./client.cjs");
30
+ class RunTree {
31
+ constructor(config) {
32
+ Object.defineProperty(this, "id", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "name", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ Object.defineProperty(this, "run_type", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "project_name", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "parent_run", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "child_runs", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ Object.defineProperty(this, "execution_order", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+ Object.defineProperty(this, "child_execution_order", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: void 0
79
+ });
80
+ Object.defineProperty(this, "start_time", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: void 0
85
+ });
86
+ Object.defineProperty(this, "end_time", {
87
+ enumerable: true,
88
+ configurable: true,
89
+ writable: true,
90
+ value: void 0
91
+ });
92
+ Object.defineProperty(this, "extra", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: void 0
97
+ });
98
+ Object.defineProperty(this, "error", {
99
+ enumerable: true,
100
+ configurable: true,
101
+ writable: true,
102
+ value: void 0
103
+ });
104
+ Object.defineProperty(this, "serialized", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
110
+ Object.defineProperty(this, "inputs", {
111
+ enumerable: true,
112
+ configurable: true,
113
+ writable: true,
114
+ value: void 0
115
+ });
116
+ Object.defineProperty(this, "outputs", {
117
+ enumerable: true,
118
+ configurable: true,
119
+ writable: true,
120
+ value: void 0
121
+ });
122
+ Object.defineProperty(this, "reference_example_id", {
123
+ enumerable: true,
124
+ configurable: true,
125
+ writable: true,
126
+ value: void 0
127
+ });
128
+ Object.defineProperty(this, "client", {
129
+ enumerable: true,
130
+ configurable: true,
131
+ writable: true,
132
+ value: void 0
133
+ });
134
+ const defaultConfig = RunTree.getDefaultConfig();
135
+ Object.assign(this, { ...defaultConfig, ...config });
136
+ }
137
+ static getDefaultConfig() {
138
+ return {
139
+ id: uuid.v4(),
140
+ project_name: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_PROJECT") ??
141
+ (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION") ?? // TODO: Deprecate
142
+ "default",
143
+ child_runs: [],
144
+ execution_order: 1,
145
+ child_execution_order: 1,
146
+ api_url: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") ?? "http://localhost:1984",
147
+ api_key: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY"),
148
+ caller_options: {},
149
+ start_time: Date.now(),
150
+ end_time: Date.now(),
151
+ serialized: {},
152
+ inputs: {},
153
+ extra: {},
154
+ client: new client_js_1.Client({}),
155
+ };
156
+ }
157
+ async createChild(config) {
158
+ const child = new RunTree({
159
+ ...config,
160
+ parent_run: this,
161
+ project_name: this.project_name,
162
+ client: this.client,
163
+ execution_order: this.child_execution_order + 1,
164
+ child_execution_order: this.child_execution_order + 1,
165
+ });
166
+ this.child_runs.push(child);
167
+ return child;
168
+ }
169
+ async end(outputs, error, endTime = Date.now()) {
170
+ this.outputs = outputs;
171
+ this.error = error;
172
+ this.end_time = endTime;
173
+ if (this.parent_run) {
174
+ this.parent_run.child_execution_order = Math.max(this.parent_run.child_execution_order, this.child_execution_order);
175
+ }
176
+ }
177
+ async _convertToCreate(run, excludeChildRuns = true) {
178
+ const runExtra = run.extra ?? {};
179
+ if (!runExtra.runtime) {
180
+ runExtra.runtime = {};
181
+ }
182
+ const runtimeEnv = await (0, env_js_1.getRuntimeEnvironment)();
183
+ for (const [k, v] of Object.entries(runtimeEnv)) {
184
+ if (!runExtra.runtime[k]) {
185
+ runExtra.runtime[k] = v;
186
+ }
187
+ }
188
+ let child_runs;
189
+ let parent_run_id;
190
+ if (!excludeChildRuns) {
191
+ child_runs = await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run, excludeChildRuns)));
192
+ parent_run_id = undefined;
193
+ }
194
+ else {
195
+ parent_run_id = run.parent_run?.id;
196
+ child_runs = [];
197
+ }
198
+ const persistedRun = {
199
+ id: run.id,
200
+ name: run.name,
201
+ start_time: run.start_time,
202
+ end_time: run.end_time,
203
+ run_type: run.run_type,
204
+ reference_example_id: run.reference_example_id,
205
+ extra: runExtra,
206
+ execution_order: run.execution_order,
207
+ serialized: run.serialized,
208
+ error: run.error,
209
+ inputs: run.inputs,
210
+ outputs: run.outputs,
211
+ session_name: run.project_name,
212
+ child_runs: child_runs,
213
+ parent_run_id: parent_run_id,
214
+ };
215
+ return persistedRun;
216
+ }
217
+ async postRun(excludeChildRuns = true) {
218
+ const runCreate = await this._convertToCreate(this, excludeChildRuns);
219
+ await this.client.createRun(runCreate);
220
+ }
221
+ async patchRun() {
222
+ const runUpdate = {
223
+ end_time: this.end_time,
224
+ error: this.error,
225
+ outputs: this.outputs,
226
+ parent_run_id: this.parent_run?.id,
227
+ reference_example_id: this.reference_example_id,
228
+ };
229
+ await this.client.updateRun(this.id, runUpdate);
230
+ }
231
+ }
232
+ exports.RunTree = RunTree;
@@ -0,0 +1,47 @@
1
+ import { BaseRun, KVMap, RunType } from "./schemas.js";
2
+ import { Client } from "./client.js";
3
+ export interface RunTreeConfig {
4
+ name: string;
5
+ run_type: RunType;
6
+ id?: string;
7
+ project_name?: string;
8
+ execution_order?: number;
9
+ child_execution_order?: number;
10
+ parent_run?: RunTree;
11
+ child_runs?: RunTree[];
12
+ start_time?: number;
13
+ end_time?: number;
14
+ extra?: KVMap;
15
+ error?: string;
16
+ serialized?: object;
17
+ inputs?: KVMap;
18
+ outputs?: KVMap;
19
+ reference_example_id?: string;
20
+ client?: Client;
21
+ }
22
+ export declare class RunTree implements BaseRun {
23
+ id: string;
24
+ name: RunTreeConfig["name"];
25
+ run_type: RunTreeConfig["run_type"];
26
+ project_name: string;
27
+ parent_run?: RunTree;
28
+ child_runs: RunTree[];
29
+ execution_order: number;
30
+ child_execution_order: number;
31
+ start_time: number;
32
+ end_time: number;
33
+ extra: KVMap;
34
+ error?: string;
35
+ serialized: object;
36
+ inputs: KVMap;
37
+ outputs?: KVMap;
38
+ reference_example_id?: string;
39
+ client: Client;
40
+ constructor(config: RunTreeConfig);
41
+ private static getDefaultConfig;
42
+ createChild(config: RunTreeConfig): Promise<RunTree>;
43
+ end(outputs?: KVMap, error?: string, endTime?: number): Promise<void>;
44
+ private _convertToCreate;
45
+ postRun(excludeChildRuns?: boolean): Promise<void>;
46
+ patchRun(): Promise<void>;
47
+ }
@@ -0,0 +1,205 @@
1
+ import * as uuid from "uuid";
2
+ import { getEnvironmentVariable, getRuntimeEnvironment } from "./utils/env.js";
3
+ import { Client } from "./client.js";
4
+ export class RunTree {
5
+ constructor(config) {
6
+ Object.defineProperty(this, "id", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: void 0
11
+ });
12
+ Object.defineProperty(this, "name", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "run_type", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "project_name", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "parent_run", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "child_runs", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "execution_order", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "child_execution_order", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "start_time", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "end_time", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ Object.defineProperty(this, "extra", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: void 0
71
+ });
72
+ Object.defineProperty(this, "error", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: void 0
77
+ });
78
+ Object.defineProperty(this, "serialized", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+ Object.defineProperty(this, "inputs", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: void 0
89
+ });
90
+ Object.defineProperty(this, "outputs", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: void 0
95
+ });
96
+ Object.defineProperty(this, "reference_example_id", {
97
+ enumerable: true,
98
+ configurable: true,
99
+ writable: true,
100
+ value: void 0
101
+ });
102
+ Object.defineProperty(this, "client", {
103
+ enumerable: true,
104
+ configurable: true,
105
+ writable: true,
106
+ value: void 0
107
+ });
108
+ const defaultConfig = RunTree.getDefaultConfig();
109
+ Object.assign(this, { ...defaultConfig, ...config });
110
+ }
111
+ static getDefaultConfig() {
112
+ return {
113
+ id: uuid.v4(),
114
+ project_name: getEnvironmentVariable("LANGCHAIN_PROJECT") ??
115
+ getEnvironmentVariable("LANGCHAIN_SESSION") ?? // TODO: Deprecate
116
+ "default",
117
+ child_runs: [],
118
+ execution_order: 1,
119
+ child_execution_order: 1,
120
+ api_url: getEnvironmentVariable("LANGCHAIN_ENDPOINT") ?? "http://localhost:1984",
121
+ api_key: getEnvironmentVariable("LANGCHAIN_API_KEY"),
122
+ caller_options: {},
123
+ start_time: Date.now(),
124
+ end_time: Date.now(),
125
+ serialized: {},
126
+ inputs: {},
127
+ extra: {},
128
+ client: new Client({}),
129
+ };
130
+ }
131
+ async createChild(config) {
132
+ const child = new RunTree({
133
+ ...config,
134
+ parent_run: this,
135
+ project_name: this.project_name,
136
+ client: this.client,
137
+ execution_order: this.child_execution_order + 1,
138
+ child_execution_order: this.child_execution_order + 1,
139
+ });
140
+ this.child_runs.push(child);
141
+ return child;
142
+ }
143
+ async end(outputs, error, endTime = Date.now()) {
144
+ this.outputs = outputs;
145
+ this.error = error;
146
+ this.end_time = endTime;
147
+ if (this.parent_run) {
148
+ this.parent_run.child_execution_order = Math.max(this.parent_run.child_execution_order, this.child_execution_order);
149
+ }
150
+ }
151
+ async _convertToCreate(run, excludeChildRuns = true) {
152
+ const runExtra = run.extra ?? {};
153
+ if (!runExtra.runtime) {
154
+ runExtra.runtime = {};
155
+ }
156
+ const runtimeEnv = await getRuntimeEnvironment();
157
+ for (const [k, v] of Object.entries(runtimeEnv)) {
158
+ if (!runExtra.runtime[k]) {
159
+ runExtra.runtime[k] = v;
160
+ }
161
+ }
162
+ let child_runs;
163
+ let parent_run_id;
164
+ if (!excludeChildRuns) {
165
+ child_runs = await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run, excludeChildRuns)));
166
+ parent_run_id = undefined;
167
+ }
168
+ else {
169
+ parent_run_id = run.parent_run?.id;
170
+ child_runs = [];
171
+ }
172
+ const persistedRun = {
173
+ id: run.id,
174
+ name: run.name,
175
+ start_time: run.start_time,
176
+ end_time: run.end_time,
177
+ run_type: run.run_type,
178
+ reference_example_id: run.reference_example_id,
179
+ extra: runExtra,
180
+ execution_order: run.execution_order,
181
+ serialized: run.serialized,
182
+ error: run.error,
183
+ inputs: run.inputs,
184
+ outputs: run.outputs,
185
+ session_name: run.project_name,
186
+ child_runs: child_runs,
187
+ parent_run_id: parent_run_id,
188
+ };
189
+ return persistedRun;
190
+ }
191
+ async postRun(excludeChildRuns = true) {
192
+ const runCreate = await this._convertToCreate(this, excludeChildRuns);
193
+ await this.client.createRun(runCreate);
194
+ }
195
+ async patchRun() {
196
+ const runUpdate = {
197
+ end_time: this.end_time,
198
+ error: this.error,
199
+ outputs: this.outputs,
200
+ parent_run_id: this.parent_run?.id,
201
+ reference_example_id: this.reference_example_id,
202
+ };
203
+ await this.client.updateRun(this.id, runUpdate);
204
+ }
205
+ }