123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- "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 () {
- var ownKeys = function(o) {
- ownKeys = Object.getOwnPropertyNames || function (o) {
- var ar = [];
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
- return ar;
- };
- return ownKeys(o);
- };
- return function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
- __setModuleDefault(result, mod);
- return result;
- };
- })();
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.RunTree = void 0;
- exports.convertToDottedOrderFormat = convertToDottedOrderFormat;
- exports.isRunTree = isRunTree;
- exports.isRunnableConfigLike = isRunnableConfigLike;
- const uuid = __importStar(require("uuid"));
- const env_js_1 = require("./utils/env.cjs");
- const client_js_1 = require("./client.cjs");
- const env_js_2 = require("./env.cjs");
- const warn_js_1 = require("./utils/warn.cjs");
- const constants_js_1 = require("./singletons/constants.cjs");
- function stripNonAlphanumeric(input) {
- return input.replace(/[-:.]/g, "");
- }
- function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
- // Date only has millisecond precision, so we use the microseconds to break
- // possible ties, avoiding incorrect run order
- const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
- return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
- }
- /**
- * Baggage header information
- */
- class Baggage {
- constructor(metadata, tags, project_name) {
- Object.defineProperty(this, "metadata", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "tags", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "project_name", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- this.metadata = metadata;
- this.tags = tags;
- this.project_name = project_name;
- }
- static fromHeader(value) {
- const items = value.split(",");
- let metadata = {};
- let tags = [];
- let project_name;
- for (const item of items) {
- const [key, uriValue] = item.split("=");
- const value = decodeURIComponent(uriValue);
- if (key === "langsmith-metadata") {
- metadata = JSON.parse(value);
- }
- else if (key === "langsmith-tags") {
- tags = value.split(",");
- }
- else if (key === "langsmith-project") {
- project_name = value;
- }
- }
- return new Baggage(metadata, tags, project_name);
- }
- toHeader() {
- const items = [];
- if (this.metadata && Object.keys(this.metadata).length > 0) {
- items.push(`langsmith-metadata=${encodeURIComponent(JSON.stringify(this.metadata))}`);
- }
- if (this.tags && this.tags.length > 0) {
- items.push(`langsmith-tags=${encodeURIComponent(this.tags.join(","))}`);
- }
- if (this.project_name) {
- items.push(`langsmith-project=${encodeURIComponent(this.project_name)}`);
- }
- return items.join(",");
- }
- }
- class RunTree {
- constructor(originalConfig) {
- 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, "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, "tags", {
- 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
- });
- Object.defineProperty(this, "trace_id", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "dotted_order", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(this, "tracingEnabled", {
- 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
- });
- /**
- * Attachments associated with the run.
- * Each entry is a tuple of [mime_type, bytes]
- */
- Object.defineProperty(this, "attachments", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- // If you pass in a run tree directly, return a shallow clone
- if (isRunTree(originalConfig)) {
- Object.assign(this, { ...originalConfig });
- return;
- }
- const defaultConfig = RunTree.getDefaultConfig();
- const { metadata, ...config } = originalConfig;
- const client = config.client ?? RunTree.getSharedClient();
- const dedupedMetadata = {
- ...metadata,
- ...config?.extra?.metadata,
- };
- config.extra = { ...config.extra, metadata: dedupedMetadata };
- Object.assign(this, { ...defaultConfig, ...config, client });
- if (!this.trace_id) {
- if (this.parent_run) {
- this.trace_id = this.parent_run.trace_id ?? this.id;
- }
- else {
- this.trace_id = this.id;
- }
- }
- this.execution_order ??= 1;
- this.child_execution_order ??= 1;
- if (!this.dotted_order) {
- const currentDottedOrder = convertToDottedOrderFormat(this.start_time, this.id, this.execution_order);
- if (this.parent_run) {
- this.dotted_order =
- this.parent_run.dotted_order + "." + currentDottedOrder;
- }
- else {
- this.dotted_order = currentDottedOrder;
- }
- }
- }
- static getDefaultConfig() {
- return {
- id: uuid.v4(),
- run_type: "chain",
- project_name: (0, env_js_1.getLangSmithEnvironmentVariable)("PROJECT") ??
- (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION") ?? // TODO: Deprecate
- "default",
- child_runs: [],
- 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: {},
- };
- }
- static getSharedClient() {
- if (!RunTree.sharedClient) {
- RunTree.sharedClient = new client_js_1.Client();
- }
- return RunTree.sharedClient;
- }
- createChild(config) {
- const child_execution_order = this.child_execution_order + 1;
- const child = new RunTree({
- ...config,
- parent_run: this,
- project_name: this.project_name,
- client: this.client,
- tracingEnabled: this.tracingEnabled,
- execution_order: child_execution_order,
- child_execution_order: child_execution_order,
- });
- // Copy context vars over into the new run tree.
- if (constants_js_1._LC_CONTEXT_VARIABLES_KEY in this) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- child[constants_js_1._LC_CONTEXT_VARIABLES_KEY] =
- this[constants_js_1._LC_CONTEXT_VARIABLES_KEY];
- }
- const LC_CHILD = Symbol.for("lc:child_config");
- const presentConfig = config.extra?.[LC_CHILD] ??
- this.extra[LC_CHILD];
- // tracing for LangChain is defined by the _parentRunId and runMap of the tracer
- if (isRunnableConfigLike(presentConfig)) {
- const newConfig = { ...presentConfig };
- const callbacks = isCallbackManagerLike(newConfig.callbacks)
- ? newConfig.callbacks.copy?.()
- : undefined;
- if (callbacks) {
- // update the parent run id
- Object.assign(callbacks, { _parentRunId: child.id });
- // only populate if we're in a newer LC.JS version
- callbacks.handlers
- ?.find(isLangChainTracerLike)
- ?.updateFromRunTree?.(child);
- newConfig.callbacks = callbacks;
- }
- child.extra[LC_CHILD] = newConfig;
- }
- // propagate child_execution_order upwards
- const visited = new Set();
- let current = this;
- while (current != null && !visited.has(current.id)) {
- visited.add(current.id);
- current.child_execution_order = Math.max(current.child_execution_order, child_execution_order);
- current = current.parent_run;
- }
- this.child_runs.push(child);
- return child;
- }
- async end(outputs, error, endTime = Date.now(), metadata) {
- this.outputs = this.outputs ?? outputs;
- this.error = this.error ?? error;
- this.end_time = this.end_time ?? endTime;
- if (metadata && Object.keys(metadata).length > 0) {
- this.extra = this.extra
- ? { ...this.extra, metadata: { ...this.extra.metadata, ...metadata } }
- : { metadata };
- }
- }
- _convertToCreate(run, runtimeEnv, excludeChildRuns = true) {
- const runExtra = run.extra ?? {};
- if (!runExtra.runtime) {
- runExtra.runtime = {};
- }
- if (runtimeEnv) {
- 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 = run.child_runs.map((child_run) => this._convertToCreate(child_run, runtimeEnv, 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,
- 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,
- trace_id: run.trace_id,
- dotted_order: run.dotted_order,
- tags: run.tags,
- attachments: run.attachments,
- };
- return persistedRun;
- }
- async postRun(excludeChildRuns = true) {
- try {
- const runtimeEnv = (0, env_js_1.getRuntimeEnvironment)();
- const runCreate = await this._convertToCreate(this, runtimeEnv, true);
- await this.client.createRun(runCreate);
- if (!excludeChildRuns) {
- (0, warn_js_1.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);
- }
- }
- }
- catch (error) {
- console.error(`Error in postRun for run ${this.id}:`, error);
- }
- }
- async patchRun() {
- try {
- const runUpdate = {
- end_time: this.end_time,
- error: this.error,
- inputs: this.inputs,
- outputs: this.outputs,
- parent_run_id: this.parent_run?.id,
- reference_example_id: this.reference_example_id,
- extra: this.extra,
- events: this.events,
- dotted_order: this.dotted_order,
- trace_id: this.trace_id,
- tags: this.tags,
- attachments: this.attachments,
- session_name: this.project_name,
- };
- await this.client.updateRun(this.id, runUpdate);
- }
- catch (error) {
- console.error(`Error in patchRun for run ${this.id}`, error);
- }
- }
- toJSON() {
- return this._convertToCreate(this, undefined, false);
- }
- /**
- * Add an event to the run tree.
- * @param event - A single event or string to add
- */
- addEvent(event) {
- if (!this.events) {
- this.events = [];
- }
- if (typeof event === "string") {
- this.events.push({
- name: "event",
- time: new Date().toISOString(),
- message: event,
- });
- }
- else {
- this.events.push({
- ...event,
- time: event.time ?? new Date().toISOString(),
- });
- }
- }
- static fromRunnableConfig(parentConfig, props) {
- // We only handle the callback manager case for now
- const callbackManager = parentConfig?.callbacks;
- let parentRun;
- let projectName;
- let client;
- let tracingEnabled = (0, env_js_2.isTracingEnabled)();
- if (callbackManager) {
- const parentRunId = callbackManager?.getParentRunId?.() ?? "";
- const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
- parentRun = langChainTracer?.getRun?.(parentRunId);
- projectName = langChainTracer?.projectName;
- client = langChainTracer?.client;
- tracingEnabled = tracingEnabled || !!langChainTracer;
- }
- if (!parentRun) {
- return new RunTree({
- ...props,
- client,
- tracingEnabled,
- project_name: projectName,
- });
- }
- const parentRunTree = new RunTree({
- name: parentRun.name,
- id: parentRun.id,
- trace_id: parentRun.trace_id,
- dotted_order: parentRun.dotted_order,
- client,
- tracingEnabled,
- project_name: projectName,
- tags: [
- ...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
- ],
- extra: {
- metadata: {
- ...parentRun?.extra?.metadata,
- ...parentConfig?.metadata,
- },
- },
- });
- return parentRunTree.createChild(props);
- }
- static fromDottedOrder(dottedOrder) {
- return this.fromHeaders({ "langsmith-trace": dottedOrder });
- }
- static fromHeaders(headers, inheritArgs) {
- const rawHeaders = "get" in headers && typeof headers.get === "function"
- ? {
- "langsmith-trace": headers.get("langsmith-trace"),
- baggage: headers.get("baggage"),
- }
- : headers;
- const headerTrace = rawHeaders["langsmith-trace"];
- if (!headerTrace || typeof headerTrace !== "string")
- return undefined;
- const parentDottedOrder = headerTrace.trim();
- const parsedDottedOrder = parentDottedOrder.split(".").map((part) => {
- const [strTime, uuid] = part.split("Z");
- return { strTime, time: Date.parse(strTime + "Z"), uuid };
- });
- const traceId = parsedDottedOrder[0].uuid;
- const config = {
- ...inheritArgs,
- name: inheritArgs?.["name"] ?? "parent",
- run_type: inheritArgs?.["run_type"] ?? "chain",
- start_time: inheritArgs?.["start_time"] ?? Date.now(),
- id: parsedDottedOrder.at(-1)?.uuid,
- trace_id: traceId,
- dotted_order: parentDottedOrder,
- };
- if (rawHeaders["baggage"] && typeof rawHeaders["baggage"] === "string") {
- const baggage = Baggage.fromHeader(rawHeaders["baggage"]);
- config.metadata = baggage.metadata;
- config.tags = baggage.tags;
- config.project_name = baggage.project_name;
- }
- return new RunTree(config);
- }
- toHeaders(headers) {
- const result = {
- "langsmith-trace": this.dotted_order,
- baggage: new Baggage(this.extra?.metadata, this.tags, this.project_name).toHeader(),
- };
- if (headers) {
- for (const [key, value] of Object.entries(result)) {
- headers.set(key, value);
- }
- }
- return result;
- }
- }
- exports.RunTree = RunTree;
- Object.defineProperty(RunTree, "sharedClient", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: null
- });
- function isRunTree(x) {
- return (x !== undefined &&
- typeof x.createChild === "function" &&
- typeof x.postRun === "function");
- }
- function isLangChainTracerLike(x) {
- return (typeof x === "object" &&
- x != null &&
- typeof x.name === "string" &&
- x.name === "langchain_tracer");
- }
- function containsLangChainTracerLike(x) {
- return (Array.isArray(x) && x.some((callback) => isLangChainTracerLike(callback)));
- }
- function isCallbackManagerLike(x) {
- return (typeof x === "object" &&
- x != null &&
- Array.isArray(x.handlers));
- }
- function isRunnableConfigLike(x) {
- // Check that it's an object with a callbacks arg
- // that has either a CallbackManagerLike object with a langchain tracer within it
- // or an array with a LangChainTracerLike object within it
- return (x !== undefined &&
- typeof x.callbacks === "object" &&
- // Callback manager with a langchain tracer
- (containsLangChainTracerLike(x.callbacks?.handlers) ||
- // Or it's an array with a LangChainTracerLike object within it
- containsLangChainTracerLike(x.callbacks)));
- }
|