ドキュメント

インテグレーション

カスタム設定

ツールプロバイダーにカスタム設定オプションを追加する

ベータ版機能

プラグインサポートは現在プライベートベータ版です。こちらからベータ版に参加してください。

プラグインのユーザーがコードを変更することなく動作をカスタマイズできるように、ツールプロバイダーにカスタム設定オプションを追加できます。

以下の例では、ユーザーにフォルダー名を指定してもらい、そのフォルダー内に作業ディレクトリーとしてファイルを作成します。

まず、config.tsにconfigフィールドを追加します。

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で利用できます。