文档

集成

处理中止

在您的工具提供程序中优雅地处理用户中止的工具执行

测试版功能

插件支持目前处于内部测试阶段。在此加入测试

当您的工具仍在运行时,用户可能会中止预测。在这种情况下,您应该通过处理作为工具实现函数的第二个参数传递的 AbortSignal 对象来优雅地处理中止。

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";

export async function toolsProvider(ctl: ToolsProviderController) {
  const tools: Tool[] = [];

  const fetchTool = tool({
    name: `fetch`,
    description: "Fetch a URL using GET method.",
    parameters: { url: z.string() },
    implementation: async ({ url }, { signal }) => {
      const response = await fetch(url, {
        method: "GET",
        signal, // <-- Here, we pass the signal to fetch to allow cancellation
      });
      if (!response.ok) {
        return `Error: Failed to fetch ${url}: ${response.statusText}`;
      }
      const data = await response.text();
      return {
        status: response.status,
        headers: Object.fromEntries(response.headers.entries()),
        data: data.substring(0, 1000), // Limit to 1000 characters
      };
    },
  });
  tools.push(fetchTool);

  return tools;
}

您可以在 MDN 文档中了解有关 AbortSignal 的更多信息。

此页面的源代码可在 GitHub 上获取