/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var patchJSCode_exports = {};
__export(patchJSCode_exports, {
  default: () => patchJSCode_default
});
module.exports = __toCommonJS(patchJSCode_exports);
var import_ai = require("@nocobase/ai");
var import_zod = require("zod");
var patchJSCode_default = (0, import_ai.defineTools)({
  scope: "SPECIFIED",
  execution: "frontend",
  defaultPermission: "ALLOW",
  introduction: {
    title: `{{t("ai.tools.patchJSCode.title")}}`,
    about: `{{t("ai.tools.patchJSCode.about")}}`
  },
  definition: {
    name: "patchJSCode",
    description: "Apply a minimal unified diff patch to the code currently in the editor, then write the patched code back to the editor. Use this for adding, modifying, removing, fixing, or extending existing code. The tool reads the current editor code directly; only provide the patch. Keep patches surgical: include only changed lines plus the smallest necessary context, and do not include unchanged large blocks.",
    schema: import_zod.z.object({
      patch: import_zod.z.string().describe(
        "Minimal unified diff patch to apply to the current editor code. Include only changed lines plus the smallest necessary context."
      )
    })
  },
  invoke: async (ctx, _args, runtime) => {
    const { toolCallResults } = ctx.action.params.values || {};
    const { result } = (toolCallResults == null ? void 0 : toolCallResults.find((item) => item.id === runtime.toolCallId)) ?? {};
    if (toolCallResults && result) {
      return {
        status: result.status ?? "error",
        content: JSON.stringify(result.content ?? {})
      };
    }
    return {
      status: "error",
      content: JSON.stringify({ success: false, message: "Patch code failed: no result returned" })
    };
  }
});
