UNPKG

@langchain/core

Version:
87 lines (85 loc) 2.78 kB
import { __export } from "../_virtual/rolldown_runtime.js"; import { BaseTracer } from "./base.js"; import { getDefaultLangChainClientSingleton } from "../singletons/tracer.js"; import { getCurrentRunTree } from "langsmith/singletons/traceable"; import { RunTree } from "langsmith/run_trees"; import { getDefaultProjectName } from "langsmith"; //#region src/tracers/tracer_langchain.ts var tracer_langchain_exports = {}; __export(tracer_langchain_exports, { LangChainTracer: () => LangChainTracer }); var LangChainTracer = class LangChainTracer extends BaseTracer { name = "langchain_tracer"; projectName; exampleId; client; replicas; usesRunTreeMap = true; constructor(fields = {}) { super(fields); const { exampleId, projectName, client, replicas } = fields; this.projectName = projectName ?? getDefaultProjectName(); this.replicas = replicas; this.exampleId = exampleId; this.client = client ?? 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 RunTree({ ...runTree, client: this.client, project_name: this.projectName, replicas: this.replicas, reference_example_id: this.exampleId, tracingEnabled: true }); } static getTraceableRunTree() { try { return getCurrentRunTree(true); } catch { return void 0; } } }; //#endregion export { LangChainTracer, tracer_langchain_exports }; //# sourceMappingURL=tracer_langchain.js.map