xonecas.io
5414
Sa

sat / files

sat / src / lib / agent.ts

2.8 KB
1import { tool } from "@langchain/core/tools";
2import { MemorySaver } from "@langchain/langgraph";
3import { ChatOllama } from "@langchain/ollama";
4import { createAgent, SystemMessage, summarizationMiddleware } from "langchain";
5import { z } from "zod";
6import { fetchURLContents } from "@/lib/fetchURLContents";
7
8interface SearchResult {
9  url: string;
10  title: string;
11  content: string;
12  score: number;
13}
14
15const searchWeb = tool(
16  async ({ query }: { query: string }) => {
17    const response = await fetch(
18      `http://localhost:8080/search?q=${query}&format=json`,
19    );
20    const data = await response.json();
21    const plainTextResults = data.results
22      .map((result: SearchResult) => {
23        console.log(
24          `result: ${result.title}: ${result.content}\n[score: ${result.score}] [source: ${result.url}]`,
25        );
26        return `${result.title}: ${result.content}\n[score: ${result.score}] [source: ${result.url}]`;
27      })
28      .join("\n\n");
29    return plainTextResults;
30  },
31  {
32    name: "search_web",
33    description: "search the web for a specified query string",
34    schema: z.object({
35      query: z.string().describe("Search terms to look for"),
36    }),
37  },
38);
39
40const fetchPageContents = tool(
41  async ({ url }: { url: string }) => {
42    const contents = await fetchURLContents(url);
43    return contents;
44  },
45  {
46    name: "fetch_page_contents",
47    description: "fetch the contents of a web page",
48    schema: z.object({ url: z.string().describe("The URL to fetch") }),
49  },
50);
51
52// TODO: https://docs.langchain.com/oss/javascript/langchain/short-term-memory#in-production
53const checkpointer = new MemorySaver();
54
55// Singleton agent instance
56let agentInstance: ReturnType<typeof createAgent> | null = null;
57
58export function getAgent() {
59  if (!agentInstance) {
60    const llm = new ChatOllama({
61      model: "qwen3:8b",
62    });
63
64    agentInstance = createAgent({
65      model: llm,
66      tools: [searchWeb, fetchPageContents],
67      middleware: [
68        summarizationMiddleware({
69          model: llm,
70          trigger: { tokens: 4000 },
71          keep: { messages: 10 },
72        }),
73      ],
74      checkpointer,
75      systemPrompt: new SystemMessage(
76        `You are a helpful web search assistant. Always respond in English, regardless of the language used in search results or user queries.
77You have access to a tool that searches the web, and a tool that fetches the contents of a web page.
78Create optimized search queries from the user's input and use them with the search_web tool to get the best results.
79Use the fetch_page_contents tool to fetch the contents of a web page from the results of the search_web tool if the user's query is about the content of a web page.
80Review the results and provide a concise summary of the information found in English. Include sources and links if available.`,
81      ),
82    });
83  }
84  return agentInstance;
85}