From 8881d443c5d6974e29d8a10a774aa597b2938195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=B0=E6=98=8E?= Date: Fri, 30 Jan 2026 00:35:15 +0800 Subject: [PATCH] fix: duplicate wrap tracer --- .../lib/graph/CompiledStateGraphObject.ts | 6 ----- plugin/langchain/test/llm.test.ts | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/plugin/langchain/lib/graph/CompiledStateGraphObject.ts b/plugin/langchain/lib/graph/CompiledStateGraphObject.ts index 7e686389..7634b252 100644 --- a/plugin/langchain/lib/graph/CompiledStateGraphObject.ts +++ b/plugin/langchain/lib/graph/CompiledStateGraphObject.ts @@ -37,16 +37,10 @@ export class CompiledStateGraphObject implements EggObject { this._obj = await this.build(); const graph = this._obj as CompiledStateGraph; - const originalInvoke = graph.invoke; const originalStream = graph.stream; const langGraphTraceObj = await EggContainerFactory.getOrCreateEggObjectFromName('langGraphTracer'); const tracer = langGraphTraceObj.obj as LangGraphTracer; tracer.setName(this.graphName); - - - graph.invoke = (input: any, config?: any) => - this.wrapGraphMethod(originalInvoke.bind(graph), input, config); - graph.stream = (input: any, config?: any) => this.wrapGraphMethod(originalStream.bind(graph), input, config); diff --git a/plugin/langchain/test/llm.test.ts b/plugin/langchain/test/llm.test.ts index cfd43e62..d5c845da 100644 --- a/plugin/langchain/test/llm.test.ts +++ b/plugin/langchain/test/llm.test.ts @@ -81,5 +81,30 @@ describe('plugin/langchain/test/llm.test.ts', () => { app.expectLog(/Executing FooNode thread_id is 1/); app.expectLog(/traceId=test-trace-id/); }); + + it('should persistRun be triggered when graph.invoke is called', async () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { LangGraphTracer } = require('../lib/tracing/LangGraphTracer'); + + const persistRunCalls: any[] = []; + const originalPersistRun = LangGraphTracer.prototype.persistRun; + mm(LangGraphTracer.prototype, 'persistRun', function(this: any, run: any) { + persistRunCalls.push(run); + return originalPersistRun.call(this, run); + }); + + app.mockLog(); + mm(Tracer.prototype, 'traceId', 'test-persist-run-trace-id'); + + await app.httpRequest().get('/llm/graph'); + + assert(persistRunCalls.length > 0, 'persistRun should be called at least once'); + + const hasCorrectTraceId = persistRunCalls.some(run => run.trace_id === 'test-persist-run-trace-id'); + assert(hasCorrectTraceId, 'persistRun should receive correct trace_id from invoke call'); + + app.expectLog(/agent_run/); + app.expectLog(/traceId=test-persist-run-trace-id/); + }); } });