Spaces:
Configuration error
Configuration error
File size: 4,911 Bytes
3a65265 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import { randomUUID } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { resolvePluginTools } from "./tools.js";
type TempPlugin = { dir: string; file: string; id: string };
const tempDirs: string[] = [];
const EMPTY_PLUGIN_SCHEMA = { type: "object", additionalProperties: false, properties: {} };
function makeTempDir() {
const dir = path.join(os.tmpdir(), `moltbot-plugin-tools-${randomUUID()}`);
fs.mkdirSync(dir, { recursive: true });
tempDirs.push(dir);
return dir;
}
function writePlugin(params: { id: string; body: string }): TempPlugin {
const dir = makeTempDir();
const file = path.join(dir, `${params.id}.js`);
fs.writeFileSync(file, params.body, "utf-8");
fs.writeFileSync(
path.join(dir, "moltbot.plugin.json"),
JSON.stringify(
{
id: params.id,
configSchema: EMPTY_PLUGIN_SCHEMA,
},
null,
2,
),
"utf-8",
);
return { dir, file, id: params.id };
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch {
// ignore cleanup failures
}
}
});
describe("resolvePluginTools optional tools", () => {
const pluginBody = `
export default { register(api) {
api.registerTool(
{
name: "optional_tool",
description: "optional tool",
parameters: { type: "object", properties: {} },
async execute() {
return { content: [{ type: "text", text: "ok" }] };
},
},
{ optional: true },
);
} }
`;
it("skips optional tools without explicit allowlist", () => {
const plugin = writePlugin({ id: "optional-demo", body: pluginBody });
const tools = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
});
expect(tools).toHaveLength(0);
});
it("allows optional tools by name", () => {
const plugin = writePlugin({ id: "optional-demo", body: pluginBody });
const tools = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
toolAllowlist: ["optional_tool"],
});
expect(tools.map((tool) => tool.name)).toContain("optional_tool");
});
it("allows optional tools via plugin groups", () => {
const plugin = writePlugin({ id: "optional-demo", body: pluginBody });
const toolsAll = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
toolAllowlist: ["group:plugins"],
});
expect(toolsAll.map((tool) => tool.name)).toContain("optional_tool");
const toolsPlugin = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
toolAllowlist: ["optional-demo"],
});
expect(toolsPlugin.map((tool) => tool.name)).toContain("optional_tool");
});
it("rejects plugin id collisions with core tool names", () => {
const plugin = writePlugin({ id: "message", body: pluginBody });
const tools = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
existingToolNames: new Set(["message"]),
toolAllowlist: ["message"],
});
expect(tools).toHaveLength(0);
});
it("skips conflicting tool names but keeps other tools", () => {
const plugin = writePlugin({
id: "multi",
body: `
export default { register(api) {
api.registerTool({
name: "message",
description: "conflict",
parameters: { type: "object", properties: {} },
async execute() {
return { content: [{ type: "text", text: "nope" }] };
},
});
api.registerTool({
name: "other_tool",
description: "ok",
parameters: { type: "object", properties: {} },
async execute() {
return { content: [{ type: "text", text: "ok" }] };
},
});
} }
`,
});
const tools = resolvePluginTools({
context: {
config: {
plugins: {
load: { paths: [plugin.file] },
allow: [plugin.id],
},
},
workspaceDir: plugin.dir,
},
existingToolNames: new Set(["message"]),
});
expect(tools.map((tool) => tool.name)).toEqual(["other_tool"]);
});
});
|