import { type } from "arktype"; import { buildShockbotFooter, stripExistingFooter } from "../utils/buildShockbotFooter.ts"; import { log } from "../utils/cli.ts"; import { fixDoubleEscapedString } from "../utils/fixDoubleEscapedString.ts"; import { $ } from "../utils/shell.ts"; import type { ToolContext } from "./server.ts"; import { execute, tool } from "./shared.ts"; interface GiteaPull { number: number; html_url?: string; title?: string; head?: { ref?: string }; base?: { ref?: string } } function buildPrBodyWithFooter(ctx: ToolContext, body: string): string { return `${stripExistingFooter(fixDoubleEscapedString(body))}${buildShockbotFooter({ model: ctx.toolState.model })}`; } export const UpdatePullRequestBody = type({ pull_number: type.number.describe("the pull request number to update"), body: type.string.describe("the new body content for the pull request"), }); export function UpdatePullRequestBodyTool(ctx: ToolContext) { return tool({ name: "update_pull_request_body", description: "Update the body/description of an existing pull request", parameters: UpdatePullRequestBody, execute: execute(async (params) => { const r = await ctx.gitea.request( "PATCH /repos/{owner}/{repo}/pulls/{index}", { owner: ctx.repo.owner, repo: ctx.repo.name, index: params.pull_number, body: buildPrBodyWithFooter(ctx, params.body) } ); const data = r.data as GiteaPull; log.info(`» updated pull request #${data.number}`); ctx.toolState.wasUpdated = true; return { success: true, number: data.number, url: data.html_url }; }), }); } export const PullRequest = type({ title: type.string.describe("the title of the pull request"), body: type.string.describe("the body content of the pull request"), base: type.string.describe("the base branch to merge into (e.g., 'main')"), "draft?": type.boolean, }); export function CreatePullRequestTool(ctx: ToolContext) { return tool({ name: "create_pull_request", description: "Create a pull request from the current branch", parameters: PullRequest, execute: execute(async (params) => { const currentBranch = $("git", ["rev-parse", "--abbrev-ref", "HEAD"], { log: false }).trim(); const r = await ctx.gitea.request( "POST /repos/{owner}/{repo}/pulls", { owner: ctx.repo.owner, repo: ctx.repo.name, title: params.title, body: buildPrBodyWithFooter(ctx, params.body), head: currentBranch, base: params.base, } ); const data = r.data as GiteaPull; log.info(`» created pull request #${data.number}`); const reviewer = ctx.payload.triggerer; if (reviewer && data.number) { try { await ctx.gitea.request( "POST /repos/{owner}/{repo}/pulls/{index}/requested_reviewers", { owner: ctx.repo.owner, repo: ctx.repo.name, index: data.number, reviewers: [reviewer] } ); } catch { log.debug(`failed to request review from ${reviewer}`); } } return { success: true, number: data.number, url: data.html_url, title: data.title, head: data.head?.ref, base: data.base?.ref }; }), }); }