import { defineTool, invoke } from "@flue/runtime";
import * as v from "valibot";

import resolveIssueWorkflow from "../workflows/resolve-issue";

export const triggerWorkflowTool = defineTool({
  description:
    "Trigger the resolve-issue workflow for a specific issue number. The workflow boots an isolated AgentOS VM, runs opencode to resolve the issue, then commits and opens a pull request. Returns the workflow run ID.",
  input: v.object({
    issueNumber: v.pipe(v.number(), v.integer()),
  }),
  name: "trigger_workflow",
  output: v.object({
    runId: v.string(),
  }),
  async run({ input }) {
    const receipt = await invoke(resolveIssueWorkflow, {
      input: { issueNumber: input.issueNumber },
    });
    return { runId: receipt.runId };
  },
});
