-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (49 loc) · 1.78 KB
/
index.ts
File metadata and controls
56 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { AIMessage, HumanMessage } from '@langchain/core/messages';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { createReactAgent } from '@langchain/langgraph/prebuilt';
import { MultiServerMCPClient } from '@langchain/mcp-adapters';
// import { ChatOllama } from '@langchain/ollama';
import { stdin as input, stdout as output } from 'node:process';
import readlinePromises from 'node:readline/promises';
import { mcpServers } from './servers/mcpServers';
import { prompt } from './SystemPrompts/playwrightPrompt';
import { customTools } from './tools/customTools';
const exitCommands = ['exit', 'bye', 'stop'];
async function runChat() {
const rl = readlinePromises.createInterface({ input, output });
const client = new MultiServerMCPClient(mcpServers);
const llm = new ChatGoogleGenerativeAI({ model: 'gemini-2.0-flash' });
const clientTools = await client.getTools();
const tools = [...customTools, ...clientTools];
const llmWithTools = llm.bindTools(tools);
const chatHistory = [prompt];
const prebuiltAgent = createReactAgent({
llm: llmWithTools,
tools,
});
while (true) {
const userInput = (await rl.question("You: ")).trim();
if (!userInput) continue;
if (exitCommands.includes(userInput.toLocaleLowerCase())) break;
chatHistory.push(new HumanMessage(userInput));
try {
const result = await prebuiltAgent.invoke({
messages: chatHistory,
});
const answer = result.messages?.at(-1)?.content;
if (answer) {
rl.write(`${answer}\n`);
chatHistory.push(new AIMessage(answer.toString()));
} else {
console.log("Something went wrong, there was no agent content.");
break;
}
} catch (err: any) {
console.error("Agent error: ", err.message);
}
}
rl.close();
await client?.close();
console.log("KthanxBai");
}
runChat();