import type {
  AnyAgentTool,
  MinionPluginApi,
  MinionPluginDefinition,
  MinionPluginToolFactory,
} from "../../src/plugins/types.js";
import { buildPaperclipContextBlock } from "./src/context.js";
import { createPaperclipTool } from "./src/tool.js";

const plugin: MinionPluginDefinition = {
  id: "paperclip",
  name: "Paperclip Integration",
  description:
    "Connects to the Paperclip control plane API for issue management, " +
    "agent coordination, and task tracking.",

  register(api: MinionPluginApi) {
    const pluginConfig = api.pluginConfig ?? {};

    api.registerTool(
      ((ctx) => {
        if (ctx.sandboxed) return null;
        return createPaperclipTool(pluginConfig) as AnyAgentTool;
      }) as MinionPluginToolFactory,
      { optional: true },
    );

    api.on("before_prompt_build", (_event, _ctx) => {
      const context = buildPaperclipContextBlock(api.config);
      if (!context) return;
      return { prependContext: context };
    });
  },
};

export default plugin;
