@@ -5,13 +5,17 @@ const {
55 getMust,
66} = require ( "../config" ) ;
77
8+ const {
9+ createAgent,
10+ } = require ( "langchain" ) ;
811const {
912 RedisChatMessageHistory,
1013} = require ( "@langchain/redis" ) ;
1114const {
1215 ChatOpenAI,
1316} = require ( "@langchain/openai" ) ;
1417const {
18+ AIMessage,
1519 HumanMessage,
1620 SystemMessage,
1721} = require ( "@langchain/core/messages" ) ;
@@ -41,9 +45,10 @@ const model = new ChatOpenAI({
4145 * Chat with the AI.
4246 * @param {string } chatId The chat ID to chat with the AI.
4347 * @param {string } humanInput The prompt to chat with the AI.
48+ * @param {object } [opts] Additional options.
4449 * @return {Promise<string> } The response from the AI.
4550 */
46- async function chatWithAI ( chatId , humanInput ) {
51+ async function chatWithAI ( chatId , humanInput , opts = { } ) {
4752 const chatHistory = new RedisChatMessageHistory ( {
4853 config : {
4954 url : redisUri ,
@@ -60,8 +65,12 @@ async function chatWithAI(chatId, humanInput) {
6065 const humanMsg = new HumanMessage ( humanInput ) ;
6166 const messages = [ systemMsg , ...historyMessages , humanMsg ] ;
6267
68+ // Wrap the model with tools as an agent
69+ const agent = await createToolsAgent ( opts ) ;
70+
6371 // Call the model directly with messages
64- const aiMsg = await model . invoke ( messages ) ;
72+ const { messages : responseMessages } = await agent . invoke ( { messages} ) ;
73+ const aiMsg = responseMessages . findLast ( AIMessage . isInstance ) ;
6574
6675 // Persist human + AI messages to history
6776 try {
@@ -179,67 +188,13 @@ async function createToolsAgent({openWeatherApiKey = null} = {}) {
179188 } ) ,
180189 ] ;
181190
182- const agentsModule = await import ( "@langchain/classic/agents" ) ;
183- const { initializeAgentExecutorWithOptions} = agentsModule ;
184- const executor = await initializeAgentExecutorWithOptions ( tools , model , {
185- agentType : "zero-shot-react-description" ,
186- maxIterations : 3 ,
187- returnIntermediateSteps : false ,
188- } ) ;
189-
190- return executor ;
191- }
192-
193- /**
194- * Chat with the AI using the agent + tools.
195- * @param {string } chatId
196- * @param {string } humanInput
197- * @param {object } [opts]
198- * @return {Promise<string> }
199- */
200- async function chatWithTools ( chatId , humanInput , opts = { } ) {
201- const chatHistory = new RedisChatMessageHistory ( {
202- config : { url : redisUri } ,
203- sessionId : `nymph:agent:${ chatId } ` ,
204- sessionTTL : 150 ,
205- } ) ;
206-
207- const historyMessages = await chatHistory . getMessages ( ) ;
208- const historyText = historyMessages . map ( ( m ) => (
209- typeof m . content === "string" ?
210- m . content : JSON . stringify ( m . content )
211- ) ) . join ( "\n" ) ;
212-
213- const systemMsg = systemPrompt ;
214- const prompt = [
215- systemMsg ,
216- historyText ,
217- humanInput ,
218- ] . filter ( Boolean ) . join ( "\n\n" ) ;
219-
220- const executor = await createToolsAgent ( opts ) ;
221-
222- // executor.call usually returns { output } or string; handle common shapes.
223- const result = await executor . call ( { input : prompt } ) ;
224-
225- const outputText = result ?. output ?. text || result ?. output || result ;
226-
227- // Save human + agent output back to history (best-effort)
228- try {
229- await chatHistory . addMessages ( [
230- new HumanMessage ( humanInput ) ,
231- new SystemMessage ( String ( outputText ) ) ,
232- ] ) ;
233- } catch ( e ) {
234- console . error ( "Failed to save agent messages to Redis history:" , e ) ;
235- }
191+ const agent = createAgent ( { model, tools} ) ;
236192
237- return String ( outputText ) ;
193+ return agent ;
238194}
239195
240196exports . useModel = ( ) => model ;
241197exports . chatWithAI = chatWithAI ;
242- exports . chatWithTools = chatWithTools ;
243198exports . sliceContent = sliceContent ;
244199exports . translateText = translateText ;
245200exports . createToolsAgent = createToolsAgent ;
0 commit comments