@@ -15,6 +15,7 @@ const {
1515 ChatOpenAI,
1616} = require ( "@langchain/openai" ) ;
1717const {
18+ AIMessage,
1819 HumanMessage,
1920 SystemMessage,
2021} = require ( "@langchain/core/messages" ) ;
@@ -44,9 +45,10 @@ const model = new ChatOpenAI({
4445 * Chat with the AI.
4546 * @param {string } chatId The chat ID to chat with the AI.
4647 * @param {string } humanInput The prompt to chat with the AI.
48+ * @param {object } [opts] Additional options.
4749 * @return {Promise<string> } The response from the AI.
4850 */
49- async function chatWithAI ( chatId , humanInput ) {
51+ async function chatWithAI ( chatId , humanInput , opts = { } ) {
5052 const chatHistory = new RedisChatMessageHistory ( {
5153 config : {
5254 url : redisUri ,
@@ -63,8 +65,12 @@ async function chatWithAI(chatId, humanInput) {
6365 const humanMsg = new HumanMessage ( humanInput ) ;
6466 const messages = [ systemMsg , ...historyMessages , humanMsg ] ;
6567
68+ // Wrap the model with tools as an agent
69+ const agent = await createToolsAgent ( opts ) ;
70+
6671 // Call the model directly with messages
67- const aiMsg = await model . invoke ( messages ) ;
72+ const { messages : responseMessages } = await agent . invoke ( { messages} ) ;
73+ const aiMsg = responseMessages . findLast ( AIMessage . isInstance ) ;
6874
6975 // Persist human + AI messages to history
7076 try {
@@ -187,58 +193,8 @@ async function createToolsAgent({openWeatherApiKey = null} = {}) {
187193 return agent ;
188194}
189195
190- /**
191- * Chat with the AI using the agent + tools.
192- * @param {string } chatId
193- * @param {string } humanInput
194- * @param {object } [opts]
195- * @return {Promise<string> }
196- */
197- async function chatWithTools ( chatId , humanInput , opts = { } ) {
198- const chatHistory = new RedisChatMessageHistory ( {
199- config : { url : redisUri } ,
200- sessionId : `nymph:agent:${ chatId } ` ,
201- sessionTTL : 150 ,
202- } ) ;
203-
204- const historyMessages = await chatHistory . getMessages ( ) ;
205- const historyText = historyMessages . map ( ( m ) => (
206- typeof m . content === "string" ?
207- m . content : JSON . stringify ( m . content )
208- ) ) . join ( "\n" ) ;
209-
210- const systemMsg = systemPrompt ;
211- const prompt = [
212- systemMsg ,
213- historyText ,
214- humanInput ,
215- ] . filter ( Boolean ) . join ( "\n\n" ) ;
216-
217- const agent = await createToolsAgent ( opts ) ;
218-
219- // Use agent.invoke with messages
220- const result = await agent . invoke ( {
221- messages : [ { role : "user" , content : prompt } ] ,
222- } ) ;
223-
224- const outputText = result ?. output ?. text || result ?. output || result ;
225-
226- // Save human + agent output back to history (best-effort)
227- try {
228- await chatHistory . addMessages ( [
229- new HumanMessage ( humanInput ) ,
230- new SystemMessage ( String ( outputText ) ) ,
231- ] ) ;
232- } catch ( e ) {
233- console . error ( "Failed to save agent messages to Redis history:" , e ) ;
234- }
235-
236- return String ( outputText ) ;
237- }
238-
239196exports . useModel = ( ) => model ;
240197exports . chatWithAI = chatWithAI ;
241- exports . chatWithTools = chatWithTools ;
242198exports . sliceContent = sliceContent ;
243199exports . translateText = translateText ;
244200exports . createToolsAgent = createToolsAgent ;
0 commit comments