import { type } from "arktype"; import { log } from "../utils/cli.ts"; import type { ToolContext } from "./server.ts"; import { execute, tool } from "./shared.ts"; export const AddLabelsParams = type({ issue_number: type.number.describe("the issue or PR number to add labels to"), labels: type.string.array().atLeastLength(1).describe("array of label names to add"), }); export function AddLabelsTool(ctx: ToolContext) { return tool({ name: "add_labels", description: "Add labels to a Gitea issue or pull request. Only use labels that already exist in the repository.", parameters: AddLabelsParams, execute: execute(async ({ issue_number, labels }) => { // Resolve label names to IDs (Gitea uses IDs, not names) const allLabels = await ctx.gitea.paginate(ctx.gitea.rest.issue.issueListLabels, { owner: ctx.repo.owner, repo: ctx.repo.name, }); const labelIds = labels .map((name) => allLabels.find((l) => l.name === name)?.id) .filter((id): id is number => typeof id === "number"); if (labelIds.length === 0) { return { success: true, labels: [], message: "No matching labels found in repository" }; } const result = await ctx.gitea.rest.issue.issueAddLabel({ owner: ctx.repo.owner, repo: ctx.repo.name, index: issue_number, body: { labels: labelIds }, }); log.info(`ยป added labels [${labels.join(", ")}] to issue #${issue_number}`); return { success: true, labels: result.data.map((label) => label.name).filter(Boolean), }; }), }); }