Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions core/langchain-decorator/src/decorator/GraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { IGraphNodeMetadata } from '../model/GraphNodeMetadata';
import { GraphNodeInfoUtil } from '../util/GraphNodeInfoUtil';
import { AnnotationRoot, StateDefinition, UpdateType } from '@langchain/langgraph';
import { AnnotationRoot, Runtime, StateDefinition, StateGraph, UpdateType } from '@langchain/langgraph';
import { ConfigurableModel } from 'langchain/chat_models/universal';
import { ToolNode } from '@langchain/langgraph/prebuilt';
import { BaseChatOpenAI } from '@langchain/openai';
Expand All @@ -26,10 +26,14 @@ export function GraphNode<S extends StateDefinition = StateDefinition>(params: I
};
}

export interface IGraphNode<S extends StateDefinition = StateDefinition, T = any> {
export type StateGraphAddNodeOptions = Parameters<typeof StateGraph['prototype']['addNode']>[2];

export type GraphRuntime<ContextType = Record<string, unknown>, InterruptType = any, WriterType = any> = Runtime<ContextType, InterruptType, WriterType>;

execute(state: AnnotationRoot<S>['State']): Promise<UpdateType<S> & Record<string, any>> | Promise<ToolNode<T>>;
export interface IGraphNode<S extends StateDefinition = StateDefinition, T = any> {

options?: StateGraphAddNodeOptions;
execute(state: AnnotationRoot<S>['State'], options?: GraphRuntime): Promise<UpdateType<S> & Record<string, any>> | Promise<ToolNode<T>>;
build?: (tools: Parameters<ConfigurableModel['bindTools']>['0']) => Promise<ReturnType<ConfigurableModel['bindTools']> | ReturnType<BaseChatOpenAI<any>['bindTools']>>;
}

Expand Down
10 changes: 9 additions & 1 deletion plugin/langchain/lib/graph/CompiledStateGraphObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class CompiledStateGraphObject implements EggObject {
const graph = this._obj as CompiledStateGraph<any, any>;

const originalInvoke = graph.invoke;
const originalStream = graph.stream;
const langGraphTraceObj = await EggContainerFactory.getOrCreateEggObjectFromName('langGraphTracer');
const tracer = langGraphTraceObj.obj as LangGraphTracer;
tracer.setName(this.graphName);
Expand All @@ -47,6 +48,13 @@ export class CompiledStateGraphObject implements EggObject {
return await originalInvoke.call(graph, input, config);
};

graph.stream = async (input: any, config?: any) => {
if (config?.tags?.includes('trace-log')) {
config.callbacks = [ tracer, ...(config?.callbacks || []) ];
}
return await originalStream.call(graph, input, config) as any;
};

this.status = EggObjectStatus.READY;
}

Expand Down Expand Up @@ -87,7 +95,7 @@ export class CompiledStateGraphObject implements EggObject {
if (TeggToolNode.prototype.isPrototypeOf(nodeObj)) {
graphObj.addNode(nodeMetadata.nodeName, (nodeObj as unknown as TeggToolNode).toolNode);
} else {
graphObj.addNode(nodeMetadata.nodeName, nodeObj.execute.bind(nodeObj));
graphObj.addNode(nodeMetadata.nodeName, nodeObj.execute.bind(nodeObj), nodeObj.options);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class AppController {
configurable: {
thread_id: '1',
},
tags: [ 'trace-log' ],
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AccessLevel, SingletonProto, ToolArgs, ToolArgsSchema } from '@eggjs/tegg';
import { Graph, GraphEdge, IGraphEdge, AbstractStateGraph, GraphNode, IGraphNode, GraphStateType, GraphTool, IGraphTool, TeggToolNode } from '@eggjs/tegg-langchain-decorator';
import { AccessLevel, Inject, Logger, SingletonProto, ToolArgs, ToolArgsSchema } from '@eggjs/tegg';
import { Graph, GraphEdge, IGraphEdge, AbstractStateGraph, GraphNode, IGraphNode, GraphStateType, GraphTool, IGraphTool, TeggToolNode, GraphRuntime } from '@eggjs/tegg-langchain-decorator';
import { Annotation, MemorySaver } from '@langchain/langgraph';
// import { AIMessage, BaseMessage, ToolMessage } from '@langchain/core/messages';
import * as z from 'zod/v4';
Expand Down Expand Up @@ -54,7 +54,11 @@ export class FooTool implements IGraphTool {
mcpServers: [ 'bar' ],
})
export class FooNode implements IGraphNode<fooAnnotationStateDefinitionType> {
async execute(state: GraphStateType<fooAnnotationStateDefinitionType>) {
@Inject()
logger: Logger;

async execute(state: GraphStateType<fooAnnotationStateDefinitionType>, options: GraphRuntime) {
this.logger.info('Executing FooNode thread_id is', options.configurable?.thread_id);
console.log('response: ', state.messages);
const messages = state.messages;
const lastMessage = messages[messages.length - 1];
Expand Down
3 changes: 3 additions & 0 deletions plugin/langchain/test/llm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ describe('plugin/langchain/test/llm.test.ts', () => {
});

it('should graph work', async () => {
app.mockLog();
await app.httpRequest()
.get('/llm/graph')
.expect(200, { value: 'hello graph toolhello world' });
app.expectLog(/agent_run/);
app.expectLog(/Executing FooNode thread_id is 1/);
});
}
});
Loading