Severian's picture
Upload 7464 files
c211499
raw
history blame
8.62 kB
"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;