79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
|
|
export function httpTool(): plugins.ToolSet {
|
|
return {
|
|
http_get: plugins.tool({
|
|
description: 'Make an HTTP GET request and return the response.',
|
|
inputSchema: plugins.z.object({
|
|
url: plugins.z.string().describe('URL to request'),
|
|
headers: plugins.z
|
|
.record(plugins.z.string())
|
|
.optional()
|
|
.describe('Request headers'),
|
|
}),
|
|
execute: async ({
|
|
url,
|
|
headers,
|
|
}: {
|
|
url: string;
|
|
headers?: Record<string, string>;
|
|
}) => {
|
|
let req = plugins.smartrequest.default.create().url(url);
|
|
if (headers) {
|
|
req = req.headers(headers);
|
|
}
|
|
const response = await req.get();
|
|
let body: string;
|
|
try {
|
|
const json = await response.json();
|
|
body = JSON.stringify(json, null, 2);
|
|
} catch {
|
|
body = await response.text();
|
|
}
|
|
return plugins.truncateOutput(`HTTP ${response.status}\n${body}`).content;
|
|
},
|
|
}),
|
|
|
|
http_post: plugins.tool({
|
|
description: 'Make an HTTP POST request with a JSON body.',
|
|
inputSchema: plugins.z.object({
|
|
url: plugins.z.string().describe('URL to request'),
|
|
body: plugins.z
|
|
.record(plugins.z.unknown())
|
|
.optional()
|
|
.describe('JSON body to send'),
|
|
headers: plugins.z
|
|
.record(plugins.z.string())
|
|
.optional()
|
|
.describe('Request headers'),
|
|
}),
|
|
execute: async ({
|
|
url,
|
|
body,
|
|
headers,
|
|
}: {
|
|
url: string;
|
|
body?: Record<string, unknown>;
|
|
headers?: Record<string, string>;
|
|
}) => {
|
|
let req = plugins.smartrequest.default.create().url(url);
|
|
if (headers) {
|
|
req = req.headers(headers);
|
|
}
|
|
if (body) {
|
|
req = req.json(body);
|
|
}
|
|
const response = await req.post();
|
|
let responseBody: string;
|
|
try {
|
|
const json = await response.json();
|
|
responseBody = JSON.stringify(json, null, 2);
|
|
} catch {
|
|
responseBody = await response.text();
|
|
}
|
|
return plugins.truncateOutput(`HTTP ${response.status}\n${responseBody}`).content;
|
|
},
|
|
}),
|
|
};
|
|
}
|