🔧 Add timeout handling to IP fetch request in demo.ts
This commit is contained in:
19
demo.ts
19
demo.ts
@@ -33,8 +33,21 @@ export const get_my_ip = tool({
|
||||
description: "Get my IP",
|
||||
args: {},
|
||||
async execute(args): Promise<ToolResult> {
|
||||
const ipResp = await fetch("https://hatter.ink/ip/ip.jsonp");
|
||||
const ipResponse = await ipResp.json() as IpResponse;
|
||||
return ipResponse.ip;
|
||||
const timeoutMs = 10_000;
|
||||
const abortController = new AbortController();
|
||||
const timeoutHandler = setTimeout(() => {
|
||||
abortController.abort(
|
||||
`Fetch timeout: ${timeoutMs} ms`,
|
||||
);
|
||||
}, timeoutMs);
|
||||
try {
|
||||
const ipResp = await fetch("https://hatter.ink/ip/ip.jsonp", {
|
||||
signal: abortController.signal,
|
||||
});
|
||||
const ipResponse = await ipResp.json() as IpResponse;
|
||||
return ipResponse.ip;
|
||||
} finally {
|
||||
clearTimeout(timeoutHandler);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user