Spaces:
Build error
Build error
File size: 8,624 Bytes
c211499 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunTree = void 0;
const uuid = __importStar(require("uuid"));
const env_js_1 = require("./utils/env.cjs");
const client_js_1 = require("./client.cjs");
const warnedMessages = {};
function warnOnce(message) {
if (!warnedMessages[message]) {
console.warn(message);
warnedMessages[message] = true;
}
}
class RunTree {
constructor(config) {
Object.defineProperty(this, "id", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "run_type", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "project_name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "parent_run", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "child_runs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "execution_order", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "child_execution_order", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "start_time", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "end_time", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "extra", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "error", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "serialized", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "inputs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "outputs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "reference_example_id", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "client", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "events", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
const defaultConfig = RunTree.getDefaultConfig();
Object.assign(this, { ...defaultConfig, ...config });
}
static getDefaultConfig() {
return {
id: uuid.v4(),
project_name: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_PROJECT") ??
(0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION") ?? // TODO: Deprecate
"default",
child_runs: [],
execution_order: 1,
child_execution_order: 1,
api_url: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") ?? "http://localhost:1984",
api_key: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY"),
caller_options: {},
start_time: Date.now(),
serialized: {},
inputs: {},
extra: {},
client: new client_js_1.Client({}),
};
}
async createChild(config) {
const child = new RunTree({
...config,
parent_run: this,
project_name: this.project_name,
client: this.client,
execution_order: this.child_execution_order + 1,
child_execution_order: this.child_execution_order + 1,
});
this.child_runs.push(child);
return child;
}
async end(outputs, error, endTime = Date.now()) {
this.outputs = outputs;
this.error = error;
this.end_time = endTime;
if (this.parent_run) {
this.parent_run.child_execution_order = Math.max(this.parent_run.child_execution_order, this.child_execution_order);
}
}
async _convertToCreate(run, excludeChildRuns = true) {
const runExtra = run.extra ?? {};
if (!runExtra.runtime) {
runExtra.runtime = {};
}
const runtimeEnv = await (0, env_js_1.getRuntimeEnvironment)();
for (const [k, v] of Object.entries(runtimeEnv)) {
if (!runExtra.runtime[k]) {
runExtra.runtime[k] = v;
}
}
let child_runs;
let parent_run_id;
if (!excludeChildRuns) {
child_runs = await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run, excludeChildRuns)));
parent_run_id = undefined;
}
else {
parent_run_id = run.parent_run?.id;
child_runs = [];
}
const persistedRun = {
id: run.id,
name: run.name,
start_time: run.start_time,
end_time: run.end_time,
run_type: run.run_type,
reference_example_id: run.reference_example_id,
extra: runExtra,
execution_order: run.execution_order,
serialized: run.serialized,
error: run.error,
inputs: run.inputs,
outputs: run.outputs,
session_name: run.project_name,
child_runs: child_runs,
parent_run_id: parent_run_id,
};
return persistedRun;
}
async postRun(excludeChildRuns = true) {
const runCreate = await this._convertToCreate(this, true);
await this.client.createRun(runCreate);
if (!excludeChildRuns) {
warnOnce("Posting with excludeChildRuns=false is deprecated and will be removed in a future version.");
for (const childRun of this.child_runs) {
await childRun.postRun(false);
}
}
}
async patchRun() {
const runUpdate = {
end_time: this.end_time,
error: this.error,
outputs: this.outputs,
parent_run_id: this.parent_run?.id,
reference_example_id: this.reference_example_id,
extra: this.extra,
events: this.events,
};
await this.client.updateRun(this.id, runUpdate);
}
}
exports.RunTree = RunTree;
|