Skip to content

Commit 7d71846

Browse files
committed
feat: implement agent and upgrade to node24
1 parent dbcb757 commit 7d71846

File tree

3 files changed

+12
-56
lines changed

3 files changed

+12
-56
lines changed

.devcontainer/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/devcontainers/javascript-node:0-20
2-
ARG MONGO_TOOLS_VERSION=6.0
1+
FROM mcr.microsoft.com/devcontainers/javascript-node:24-bookworm
2+
ARG MONGO_TOOLS_VERSION=8.0
33
RUN . /etc/os-release \
44
&& curl -sSL "https://www.mongodb.org/static/pgp/server-${MONGO_TOOLS_VERSION}.asc" | gpg --dearmor > /usr/share/keyrings/mongodb-archive-keyring.gpg \
5-
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] http://repo.mongodb.org/apt/debian ${VERSION_CODENAME}/mongodb-org/${MONGO_TOOLS_VERSION} main" | tee /etc/apt/sources.list.d/mongodb-org-${MONGO_TOOLS_VERSION}.list \
5+
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/debian ${VERSION_CODENAME}/mongodb-org/${MONGO_TOOLS_VERSION} main" | tee /etc/apt/sources.list.d/mongodb-org-${MONGO_TOOLS_VERSION}.list \
66
&& apt-get update && export DEBIAN_FRONTEND=noninteractive \
77
&& apt-get install -y mongodb-mongosh \
88
&& if [ "$(dpkg --print-architecture)" = "amd64" ]; then apt-get install -y mongodb-database-tools; fi \

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20
1+
FROM node:24
22

33
ENV RUNTIME_ENV container
44

src/clients/langchain.js

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
ChatOpenAI,
1616
} = require("@langchain/openai");
1717
const {
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-
239196
exports.useModel = () => model;
240197
exports.chatWithAI = chatWithAI;
241-
exports.chatWithTools = chatWithTools;
242198
exports.sliceContent = sliceContent;
243199
exports.translateText = translateText;
244200
exports.createToolsAgent = createToolsAgent;

0 commit comments

Comments
 (0)