完善了agent

This commit is contained in:
lik
2026-05-30 16:41:26 +08:00
parent 797e69a0c2
commit b92853a717
30 changed files with 1972 additions and 22 deletions

34
agent/tools/web/fetch.js Normal file
View File

@@ -0,0 +1,34 @@
import { TavilyExtract } from "@langchain/tavily";
import { tool } from "@langchain/core/tools";
import * as z from "zod"
const webFetchTool = tool(
async ({
extractDepth = "basic",
includeImages = false,
format = "markdown",
urls = [],
}) => {
const tavilyExtract = new TavilyExtract({
tavilyApiKey: process.env.TAVILY_API_KEY,
extractDepth,
includeImages,
format
});
return await tavilyExtract._call({ urls });
},
{
name: "web_fetch",
description: "Run a web fetch",
schema: z.object({
extractDepth: z.enum(["basic", "advanced"]).default("basic"),
includeImages: z.boolean().default(false),
format: z
.enum(["markdown", "json"])
.default("markdown"),
urls: z.array(z.string().describe("The urls")).default([]),
}),
},
);
export { webFetchTool };

View File

@@ -0,0 +1,62 @@
import { tool } from 'langchain';
import * as z from 'zod';
export const httpGetTool = tool(
async ({ url, headers = {}, timeout = 30000 }) => {
try {
new URL(url);
} catch (e) {
return JSON.stringify({ success: false, error: `Invalid URL: ${url}` });
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
method: 'GET',
headers: {
'User-Agent': 'XAgent-Bot/1.0',
...headers
},
signal: controller.signal
});
clearTimeout(timeoutId);
const contentType = response.headers.get('content-type') || '';
let body;
if (contentType.includes('application/json')) {
body = await response.json();
} else if (contentType.includes('text/') || contentType.includes('application/javascript')) {
body = await response.text();
} else {
const buffer = await response.arrayBuffer();
body = Buffer.from(buffer).toString('base64');
}
return JSON.stringify({
success: true,
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
body
});
} catch (error) {
if (error.name === 'AbortError') {
return JSON.stringify({ success: false, error: `Request timed out after ${timeout}ms` });
}
return JSON.stringify({ success: false, error: error.message });
}
},
{
name: 'http_get',
description: 'Send HTTP GET request and return response content. Supports custom headers and timeout settings.',
schema: z.object({
url: z.string().describe('Request URL'),
headers: z.record(z.string(), z.string()).optional().describe('Request headers'),
timeout: z.number().optional().describe('Timeout in milliseconds, default 30000')
})
}
);

View File

@@ -0,0 +1,88 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
const httpPostTool = tool(
async ({ url, body, headers = {}, contentType = 'json', timeout = 30000 }) => {
try {
new URL(url);
} catch (e) {
return { success: false, error: `Invalid URL: ${url}` };
}
let requestBody;
const requestHeaders = {
'User-Agent': 'XAgent-Bot/1.0',
...headers
};
switch (contentType) {
case 'json':
requestHeaders['Content-Type'] = 'application/json';
requestBody = typeof body === 'string' ? body : JSON.stringify(body);
break;
case 'form':
requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
if (typeof body === 'object') {
requestBody = new URLSearchParams(body).toString();
} else {
requestBody = body;
}
break;
case 'text':
requestHeaders['Content-Type'] = 'text/plain';
requestBody = typeof body === 'string' ? body : JSON.stringify(body);
break;
default:
requestBody = typeof body === 'string' ? body : JSON.stringify(body);
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
method: 'POST',
headers: requestHeaders,
body: requestBody,
signal: controller.signal
});
clearTimeout(timeoutId);
const responseContentType = response.headers.get('content-type') || '';
let responseBody;
if (responseContentType.includes('application/json')) {
responseBody = await response.json();
} else {
responseBody = await response.text();
}
return {
success: true,
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
body: responseBody
};
} catch (error) {
if (error.name === 'AbortError') {
return { success: false, error: `Request timed out after ${timeout}ms` };
}
return { success: false, error: error.message };
}
},
{
name: "http_post",
description: "Send HTTP POST request. Supports JSON and form data.",
schema: z.object({
url: z.string().describe("Request URL"),
body: z.union([z.string(), z.object({})]).describe("Request body, can be string or JSON object"),
headers: z.record(z.string()).optional().describe("Request headers"),
contentType: z.enum(['json', 'form', 'text', 'raw']).optional().describe("Content type, default json"),
timeout: z.number().optional().describe("Timeout in milliseconds, default 30000")
}),
},
);
export { httpPostTool };

35
agent/tools/web/search.js Normal file
View File

@@ -0,0 +1,35 @@
import { TavilySearch } from "@langchain/tavily";
import { tool } from "@langchain/core/tools";
import * as z from "zod"
const webSearchTool = tool(
async ({
query,
maxResults = 5,
topic = "general",
includeRawContent = false,
}) => {
const tavilySearch = new TavilySearch({
maxResults,
tavilyApiKey: process.env.TAVILY_API_KEY,
includeRawContent,
topic,
});
return await tavilySearch._call({ query });
},
{
name: "internet_search",
description: "Run a web search",
schema: z.object({
query: z.string().describe("The search query"),
maxResults: z.number().optional().default(5),
topic: z
.enum(["general", "news", "finance"])
.optional()
.default("general"),
includeRawContent: z.boolean().optional().default(false),
}),
},
);
export { webSearchTool };