import type { SymiPluginApi } from "symi/plugin-sdk";
import { emptyPluginConfigSchema } from "symi/plugin-sdk";

const memoryCorePlugin = {
  id: "memory-core",
  name: "Memory (Core)",
  description: "File-backed memory search tools and CLI",
  kind: "memory",
  configSchema: emptyPluginConfigSchema(),
  register(api: SymiPluginApi) {
    api.registerTool(
      (ctx) => {
        const memorySearchTool = api.runtime.tools.createMemorySearchTool({
          config: ctx.config,
          agentSessionKey: ctx.sessionKey,
        });
        const memoryGetTool = api.runtime.tools.createMemoryGetTool({
          config: ctx.config,
          agentSessionKey: ctx.sessionKey,
        });
        const memoryPinTool = api.runtime.tools.createMemoryPinTool({
          config: ctx.config,
          agentSessionKey: ctx.sessionKey,
        });
        const memoryUnpinTool = api.runtime.tools.createMemoryUnpinTool({
          config: ctx.config,
          agentSessionKey: ctx.sessionKey,
        });
        if (!memorySearchTool || !memoryGetTool) {
          return null;
        }
        const tools = [memorySearchTool, memoryGetTool];
        if (memoryPinTool) {
          tools.push(memoryPinTool);
        }
        if (memoryUnpinTool) {
          tools.push(memoryUnpinTool);
        }
        return tools;
      },
      { names: ["memory_search", "memory_get", "memory_pin", "memory_unpin"] },
    );

    api.registerCli(
      ({ program }) => {
        api.runtime.tools.registerMemoryCli(program);
      },
      { commands: ["memory"] },
    );
  },
};

export default memoryCorePlugin;
