df48461a5c0123c2f3733c1f8239bae2d9d03258305d098a30893d52b2e3907a.json 17 KB

1
  1. {"ast":null,"code":"import _asyncToGenerator from \"F:/workspace/202226701027/huinongbao-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { RunTree } from \"langsmith/run_trees\";\nimport { getCurrentRunTree } from \"langsmith/singletons/traceable\";\nimport { getEnvironmentVariable, getRuntimeEnvironment } from \"../utils/env.js\";\nimport { BaseTracer } from \"./base.js\";\nimport { getDefaultLangChainClientSingleton } from \"../singletons/tracer.js\";\nexport class LangChainTracer extends BaseTracer {\n constructor(fields = {}) {\n var _ref;\n super(fields);\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"langchain_tracer\"\n });\n Object.defineProperty(this, \"projectName\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"exampleId\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"client\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n const {\n exampleId,\n projectName,\n client\n } = fields;\n this.projectName = (_ref = projectName !== null && projectName !== void 0 ? projectName : getEnvironmentVariable(\"LANGCHAIN_PROJECT\")) !== null && _ref !== void 0 ? _ref : getEnvironmentVariable(\"LANGCHAIN_SESSION\");\n this.exampleId = exampleId;\n this.client = client !== null && client !== void 0 ? client : getDefaultLangChainClientSingleton();\n const traceableTree = LangChainTracer.getTraceableRunTree();\n if (traceableTree) {\n this.updateFromRunTree(traceableTree);\n }\n }\n _convertToCreate(run, example_id = undefined) {\n var _this = this;\n return _asyncToGenerator(function* () {\n return {\n ...run,\n extra: {\n ...run.extra,\n runtime: yield getRuntimeEnvironment()\n },\n child_runs: undefined,\n session_name: _this.projectName,\n reference_example_id: run.parent_run_id ? undefined : example_id\n };\n })();\n }\n persistRun(_run) {\n return _asyncToGenerator(function* () {})();\n }\n onRunCreate(run) {\n var _this2 = this;\n return _asyncToGenerator(function* () {\n const persistedRun = yield _this2._convertToCreate(run, _this2.exampleId);\n yield _this2.client.createRun(persistedRun);\n })();\n }\n onRunUpdate(run) {\n var _this3 = this;\n return _asyncToGenerator(function* () {\n const runUpdate = {\n end_time: run.end_time,\n error: run.error,\n outputs: run.outputs,\n events: run.events,\n inputs: run.inputs,\n trace_id: run.trace_id,\n dotted_order: run.dotted_order,\n parent_run_id: run.parent_run_id\n };\n yield _this3.client.updateRun(run.id, runUpdate);\n })();\n }\n getRun(id) {\n return this.runMap.get(id);\n }\n updateFromRunTree(runTree) {\n var _runTree$client, _runTree$project_name, _runTree$reference_ex;\n let rootRun = runTree;\n const visited = new Set();\n while (rootRun.parent_run) {\n if (visited.has(rootRun.id)) break;\n visited.add(rootRun.id);\n if (!rootRun.parent_run) break;\n rootRun = rootRun.parent_run;\n }\n visited.clear();\n const queue = [rootRun];\n while (queue.length > 0) {\n const current = queue.shift();\n if (!current || visited.has(current.id)) continue;\n visited.add(current.id);\n // @ts-expect-error Types of property 'events' are incompatible.\n this.runMap.set(current.id, current);\n if (current.child_runs) {\n queue.push(...current.child_runs);\n }\n }\n this.client = (_runTree$client = runTree.client) !== null && _runTree$client !== void 0 ? _runTree$client : this.client;\n this.projectName = (_runTree$project_name = runTree.project_name) !== null && _runTree$project_name !== void 0 ? _runTree$project_name : this.projectName;\n this.exampleId = (_runTree$reference_ex = runTree.reference_example_id) !== null && _runTree$reference_ex !== void 0 ? _runTree$reference_ex : this.exampleId;\n }\n convertToRunTree(id) {\n const runTreeMap = {};\n const runTreeList = [];\n for (const [id, run] of this.runMap) {\n // by converting the run map to a run tree, we are doing a copy\n // thus, any mutation performed on the run tree will not be reflected\n // back in the run map\n // TODO: Stop using `this.runMap` in favour of LangSmith's `RunTree`\n const runTree = new RunTree({\n ...run,\n child_runs: [],\n parent_run: undefined,\n // inherited properties\n client: this.client,\n project_name: this.projectName,\n reference_example_id: this.exampleId,\n tracingEnabled: true\n });\n runTreeMap[id] = runTree;\n runTreeList.push([id, run.dotted_order]);\n }\n runTreeList.sort((a, b) => {\n if (!a[1] || !b[1]) return 0;\n return a[1].localeCompare(b[1]);\n });\n for (const [id] of runTreeList) {\n const run = this.runMap.get(id);\n const runTree = runTreeMap[id];\n if (!run || !runTree) continue;\n if (run.parent_run_id) {\n const parentRunTree = runTreeMap[run.parent_run_id];\n if (parentRunTree) {\n parentRunTree.child_runs.push(runTree);\n runTree.parent_run = parentRunTree;\n }\n }\n }\n return runTreeMap[id];\n }\n static getTraceableRunTree() {\n try {\n return getCurrentRunTree();\n } catch {\n return undefined;\n }\n }\n}","map":{"version":3,"names":["RunTree","getCurrentRunTree","getEnvironmentVariable","getRuntimeEnvironment","BaseTracer","getDefaultLangChainClientSingleton","LangChainTracer","constructor","fields","_ref","Object","defineProperty","enumerable","configurable","writable","value","exampleId","projectName","client","traceableTree","getTraceableRunTree","updateFromRunTree","_convertToCreate","run","example_id","undefined","_this","_asyncToGenerator","extra","runtime","child_runs","session_name","reference_example_id","parent_run_id","persistRun","_run","onRunCreate","_this2","persistedRun","createRun","onRunUpdate","_this3","runUpdate","end_time","error","outputs","events","inputs","trace_id","dotted_order","updateRun","id","getRun","runMap","get","runTree","_runTree$client","_runTree$project_name","_runTree$reference_ex","rootRun","visited","Set","parent_run","has","add","clear","queue","length","current","shift","set","push","project_name","convertToRunTree","runTreeMap","runTreeList","tracingEnabled","sort","a","b","localeCompare","parentRunTree"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/@langchain/core/dist/tracers/tracer_langchain.js"],"sourcesContent":["import { RunTree } from \"langsmith/run_trees\";\nimport { getCurrentRunTree } from \"langsmith/singletons/traceable\";\nimport { getEnvironmentVariable, getRuntimeEnvironment } from \"../utils/env.js\";\nimport { BaseTracer } from \"./base.js\";\nimport { getDefaultLangChainClientSingleton } from \"../singletons/tracer.js\";\nexport class LangChainTracer extends BaseTracer {\n constructor(fields = {}) {\n super(fields);\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"langchain_tracer\"\n });\n Object.defineProperty(this, \"projectName\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"exampleId\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"client\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n const { exampleId, projectName, client } = fields;\n this.projectName =\n projectName ??\n getEnvironmentVariable(\"LANGCHAIN_PROJECT\") ??\n getEnvironmentVariable(\"LANGCHAIN_SESSION\");\n this.exampleId = exampleId;\n this.client = client ?? getDefaultLangChainClientSingleton();\n const traceableTree = LangChainTracer.getTraceableRunTree();\n if (traceableTree) {\n this.updateFromRunTree(traceableTree);\n }\n }\n async _convertToCreate(run, example_id = undefined) {\n return {\n ...run,\n extra: {\n ...run.extra,\n runtime: await getRuntimeEnvironment(),\n },\n child_runs: undefined,\n session_name: this.projectName,\n reference_example_id: run.parent_run_id ? undefined : example_id,\n };\n }\n async persistRun(_run) { }\n async onRunCreate(run) {\n const persistedRun = await this._convertToCreate(run, this.exampleId);\n await this.client.createRun(persistedRun);\n }\n async onRunUpdate(run) {\n const runUpdate = {\n end_time: run.end_time,\n error: run.error,\n outputs: run.outputs,\n events: run.events,\n inputs: run.inputs,\n trace_id: run.trace_id,\n dotted_order: run.dotted_order,\n parent_run_id: run.parent_run_id,\n };\n await this.client.updateRun(run.id, runUpdate);\n }\n getRun(id) {\n return this.runMap.get(id);\n }\n updateFromRunTree(runTree) {\n let rootRun = runTree;\n const visited = new Set();\n while (rootRun.parent_run) {\n if (visited.has(rootRun.id))\n break;\n visited.add(rootRun.id);\n if (!rootRun.parent_run)\n break;\n rootRun = rootRun.parent_run;\n }\n visited.clear();\n const queue = [rootRun];\n while (queue.length > 0) {\n const current = queue.shift();\n if (!current || visited.has(current.id))\n continue;\n visited.add(current.id);\n // @ts-expect-error Types of property 'events' are incompatible.\n this.runMap.set(current.id, current);\n if (current.child_runs) {\n queue.push(...current.child_runs);\n }\n }\n this.client = runTree.client ?? this.client;\n this.projectName = runTree.project_name ?? this.projectName;\n this.exampleId = runTree.reference_example_id ?? this.exampleId;\n }\n convertToRunTree(id) {\n const runTreeMap = {};\n const runTreeList = [];\n for (const [id, run] of this.runMap) {\n // by converting the run map to a run tree, we are doing a copy\n // thus, any mutation performed on the run tree will not be reflected\n // back in the run map\n // TODO: Stop using `this.runMap` in favour of LangSmith's `RunTree`\n const runTree = new RunTree({\n ...run,\n child_runs: [],\n parent_run: undefined,\n // inherited properties\n client: this.client,\n project_name: this.projectName,\n reference_example_id: this.exampleId,\n tracingEnabled: true,\n });\n runTreeMap[id] = runTree;\n runTreeList.push([id, run.dotted_order]);\n }\n runTreeList.sort((a, b) => {\n if (!a[1] || !b[1])\n return 0;\n return a[1].localeCompare(b[1]);\n });\n for (const [id] of runTreeList) {\n const run = this.runMap.get(id);\n const runTree = runTreeMap[id];\n if (!run || !runTree)\n continue;\n if (run.parent_run_id) {\n const parentRunTree = runTreeMap[run.parent_run_id];\n if (parentRunTree) {\n parentRunTree.child_runs.push(runTree);\n runTree.parent_run = parentRunTree;\n }\n }\n }\n return runTreeMap[id];\n }\n static getTraceableRunTree() {\n try {\n return getCurrentRunTree();\n }\n catch {\n return undefined;\n }\n }\n}\n"],"mappings":";AAAA,SAASA,OAAO,QAAQ,qBAAqB;AAC7C,SAASC,iBAAiB,QAAQ,gCAAgC;AAClE,SAASC,sBAAsB,EAAEC,qBAAqB,QAAQ,iBAAiB;AAC/E,SAASC,UAAU,QAAQ,WAAW;AACtC,SAASC,kCAAkC,QAAQ,yBAAyB;AAC5E,OAAO,MAAMC,eAAe,SAASF,UAAU,CAAC;EAC5CG,WAAWA,CAACC,MAAM,GAAG,CAAC,CAAC,EAAE;IAAA,IAAAC,IAAA;IACrB,KAAK,CAACD,MAAM,CAAC;IACbE,MAAM,CAACC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE;MAChCC,UAAU,EAAE,IAAI;MAChB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