文档

集成

自定义配置

为您的工具提供商添加自定义配置选项

测试版功能

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

您可以为您的工具提供商添加自定义配置选项,这样插件用户就可以在不修改代码的情况下自定义行为。

在下面的示例中,我们将要求用户指定一个文件夹名称,然后我们将在工作目录中该文件夹内创建文件。

首先,将配置字段添加到 config.ts

export const configSchematics = createConfigSchematics()
  .field(
    "folderName", // Key of the configuration field
    "string", // Type of the configuration field
    {
      displayName: "Folder Name",
      subtitle: "The name of the folder where files will be created.",
    },
    "default_folder", // Default value
  )
  .build();
信息

在此示例中,我们将字段添加到 configSchematics,这是“每个聊天”的配置。如果您想添加一个在不同聊天之间共享的全局配置字段,您应该在同一文件中的 globalConfigSchematics 部分下添加它。

自定义配置中了解更多关于配置的信息。

然后,修改工具提供商以使用配置值

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { existsSync } from "fs";
import { mkdir, writeFile } from "fs/promises";
import { join } from "path";
import { z } from "zod";
import { configSchematics } from "./config";

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

  const createFileTool = tool({
    name: `create_file`,
    description: "Create a file with the given name and content.",
    parameters: { file_name: z.string(), content: z.string() },
    implementation: async ({ file_name, content }) => {
      // Read the config field
      const folderName = ctl.getPluginConfig(configSchematics).get("folderName");
      const folderPath = join(ctl.getWorkingDirectory(), folderName);

      // Ensure the folder exists
      await mkdir(folderPath, { recursive: true });

      // Create the file
      const filePath = join(folderPath, file_name);
      if (existsSync(filePath)) {
        return "Error: File already exists.";
      }
      await writeFile(filePath, content, "utf-8");
      return "File created.";
    },
  });
  tools.push(createFileTool); // First tool

  return tools; // Return the tools array
}

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