@langchain/core
Version:
Core LangChain.js abstractions and schemas
93 lines (91 loc) • 3.18 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_tracers_base = require('./base.cjs');
const require_tracer = require('../singletons/tracer.cjs');
const langsmith_singletons_traceable = require_rolldown_runtime.__toESM(require("langsmith/singletons/traceable"));
const langsmith_run_trees = require_rolldown_runtime.__toESM(require("langsmith/run_trees"));
const langsmith = require_rolldown_runtime.__toESM(require("langsmith"));
//#region src/tracers/tracer_langchain.ts
var tracer_langchain_exports = {};
require_rolldown_runtime.__export(tracer_langchain_exports, { LangChainTracer: () => LangChainTracer });
var LangChainTracer = class LangChainTracer extends require_tracers_base.BaseTracer {
name = "langchain_tracer";
projectName;
exampleId;
client;
replicas;
usesRunTreeMap = true;
constructor(fields = {}) {
super(fields);
const { exampleId, projectName, client, replicas } = fields;
this.projectName = projectName ?? (0, langsmith.getDefaultProjectName)();
this.replicas = replicas;
this.exampleId = exampleId;
this.client = client ?? require_tracer.getDefaultLangChainClientSingleton();
const traceableTree = LangChainTracer.getTraceableRunTree();
if (traceableTree) this.updateFromRunTree(traceableTree);
}
async persistRun(_run) {}
async onRunCreate(run) {
const runTree = this.getRunTreeWithTracingConfig(run.id);
await runTree?.postRun();
}
async onRunUpdate(run) {
const runTree = this.getRunTreeWithTracingConfig(run.id);
await runTree?.patchRun();
}
getRun(id) {
return this.runTreeMap.get(id);
}
updateFromRunTree(runTree) {
this.runTreeMap.set(runTree.id, runTree);
let rootRun = runTree;
const visited = /* @__PURE__ */ new Set();
while (rootRun.parent_run) {
if (visited.has(rootRun.id)) break;
visited.add(rootRun.id);
if (!rootRun.parent_run) break;
rootRun = rootRun.parent_run;
}
visited.clear();
const queue = [rootRun];
while (queue.length > 0) {
const current = queue.shift();
if (!current || visited.has(current.id)) continue;
visited.add(current.id);
this.runTreeMap.set(current.id, current);
if (current.child_runs) queue.push(...current.child_runs);
}
this.client = runTree.client ?? this.client;
this.replicas = runTree.replicas ?? this.replicas;
this.projectName = runTree.project_name ?? this.projectName;
this.exampleId = runTree.reference_example_id ?? this.exampleId;
}
getRunTreeWithTracingConfig(id) {
const runTree = this.runTreeMap.get(id);
if (!runTree) return void 0;
return new langsmith_run_trees.RunTree({
...runTree,
client: this.client,
project_name: this.projectName,
replicas: this.replicas,
reference_example_id: this.exampleId,
tracingEnabled: true
});
}
static getTraceableRunTree() {
try {
return (0, langsmith_singletons_traceable.getCurrentRunTree)(true);
} catch {
return void 0;
}
}
};
//#endregion
exports.LangChainTracer = LangChainTracer;
Object.defineProperty(exports, 'tracer_langchain_exports', {
enumerable: true,
get: function () {
return tracer_langchain_exports;
}
});
//# sourceMappingURL=tracer_langchain.cjs.map