// src/tools/errors.ts
var ToolInputError = class extends Error {
  constructor(code, message, status = 400) {
    super(message);
    this.code = code;
    this.status = status;
    this.name = "ToolInputError";
  }
  code;
  status;
};

// src/tools/openai.ts
var APP_TOOL_NAMES = ["submit_proposal", "schedule_followup", "render_ui", "add_citation"];
var NAME_SET = new Set(APP_TOOL_NAMES);
function isAppToolName(name) {
  return NAME_SET.has(name);
}
function buildAppToolOpenAITools(taxonomy) {
  return [
    {
      type: "function",
      function: {
        name: "submit_proposal",
        description: "Route a regulated or state-changing action to a human for approval (a recommendation, contacting/soliciting a contact, outreach, a record/account change, scheduling). Queues it for a named certified human to approve before it executes.",
        parameters: {
          type: "object",
          properties: {
            type: { type: "string", enum: [...taxonomy.proposalTypes] },
            title: { type: "string", description: "Short label for the approval queue." },
            description: { type: "string", description: "The full drafted message/recommendation, with sources." }
          },
          required: ["type", "title"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "schedule_followup",
        description: "Register a dated cadence step (a reminder, chase, or check-in) on the follow-up calendar. Executes immediately.",
        parameters: {
          type: "object",
          properties: {
            title: { type: "string" },
            dueDate: { type: "string", description: "ISO date YYYY-MM-DD." },
            priority: { type: "string", enum: ["low", "medium", "high"] }
          },
          required: ["title", "dueDate"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "render_ui",
        description: "Show a generated view live in the workspace. Validates the OpenUI JSON and persists the artifact. Executes immediately.",
        parameters: {
          type: "object",
          properties: {
            title: { type: "string" },
            schema: { type: "object", description: "The OpenUI JSON object." }
          },
          required: ["title", "schema"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "add_citation",
        description: "Anchor a grounding reference: the exact quote from a file backing a figure or claim. Verifies the quote appears in the file. Executes immediately.",
        parameters: {
          type: "object",
          properties: {
            path: { type: "string", description: "The vault file path." },
            quote: { type: "string", description: "The exact text from it." }
          },
          required: ["path", "quote"]
        }
      }
    }
  ];
}

// src/tools/dispatch.ts
async function dispatchAppTool(toolName, rawArgs, ctx, opts) {
  try {
    if (!isAppToolName(toolName)) {
      return { ok: false, code: "unknown_tool", message: `${toolName} is not an app tool.` };
    }
    if (toolName === "submit_proposal") {
      const type = String(rawArgs.type ?? "").trim();
      const title = String(rawArgs.title ?? "").trim();
      if (!type || !opts.taxonomy.proposalTypes.includes(type)) {
        return { ok: false, code: "invalid_type", message: `type must be one of: ${opts.taxonomy.proposalTypes.join(", ")}.` };
      }
      if (!title) return { ok: false, code: "missing_title", message: "title is required." };
      const description = rawArgs.description == null ? null : String(rawArgs.description);
      let regulated = opts.taxonomy.regulatedTypes.includes(type);
      if (opts.needsApproval) {
        try {
          regulated = await opts.needsApproval(type, { title, description }, ctx);
        } catch {
          regulated = true;
        }
      }
      const r2 = await opts.handlers.submitProposal({ type, title, description, regulated }, ctx);
      const { proposalId, deduped, status, ...extra } = r2;
      const effectiveStatus = status ?? "queued_for_approval";
      opts.onProduced?.({
        type: "proposal_created",
        proposalId,
        title,
        status: effectiveStatus === "executed" ? "executed" : "pending",
        content: description ?? void 0
      });
      return { ok: true, result: { ...extra, status: effectiveStatus, proposalId, deduped, regulated } };
    }
    if (toolName === "schedule_followup") {
      const r2 = await opts.handlers.scheduleFollowup(
        { title: String(rawArgs.title ?? ""), dueDate: String(rawArgs.dueDate ?? ""), priority: rawArgs.priority },
        ctx
      );
      return { ok: true, result: { followupId: r2.id, dueDate: r2.dueDate, deduped: r2.deduped } };
    }
    if (toolName === "render_ui") {
      const r2 = await opts.handlers.renderUi({ title: String(rawArgs.title ?? ""), schema: rawArgs.schema }, ctx);
      opts.onProduced?.({ type: "artifact", path: r2.path, content: r2.content });
      return { ok: true, result: { path: r2.path } };
    }
    const r = await opts.handlers.addCitation(
      { path: String(rawArgs.path ?? ""), quote: String(rawArgs.quote ?? ""), label: rawArgs.label },
      ctx
    );
    return { ok: true, result: { citationId: r.citationId, path: r.path } };
  } catch (err) {
    if (err instanceof ToolInputError) return { ok: false, code: err.code, message: err.message, status: err.status };
    return { ok: false, code: "app_tool_error", message: err instanceof Error ? err.message : String(err), status: 500 };
  }
}
function outcomeStatus(outcome) {
  return outcome.status ?? 400;
}

// src/tools/runtime.ts
function createAppToolRuntimeExecutor(opts) {
  return ({ toolName, args }) => dispatchAppTool(toolName, args, opts.ctx, opts);
}

export {
  ToolInputError,
  APP_TOOL_NAMES,
  isAppToolName,
  buildAppToolOpenAITools,
  dispatchAppTool,
  outcomeStatus,
  createAppToolRuntimeExecutor
};
//# sourceMappingURL=chunk-JZZ6AWF4.js.map