310 lines
8.7 KiB
Go
310 lines
8.7 KiB
Go
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package agentic
|
|
|
|
import (
|
|
"context"
|
|
|
|
core "dappco.re/go/core"
|
|
forge_types "dappco.re/go/core/forge/types"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
// --- agentic_create_pr ---
|
|
|
|
// CreatePRInput is the input for agentic_create_pr.
|
|
//
|
|
// input := agentic.CreatePRInput{Workspace: "core/go-io/task-42", Title: "Fix watcher panic"}
|
|
type CreatePRInput struct {
|
|
Workspace string `json:"workspace"` // workspace name (e.g. "core/go-io/task-42")
|
|
Title string `json:"title,omitempty"` // PR title (default: task description)
|
|
Body string `json:"body,omitempty"` // PR body (default: auto-generated)
|
|
Base string `json:"base,omitempty"` // base branch (default: "main")
|
|
DryRun bool `json:"dry_run,omitempty"` // preview without creating
|
|
}
|
|
|
|
// CreatePROutput is the output for agentic_create_pr.
|
|
//
|
|
// out := agentic.CreatePROutput{Success: true, PRURL: "https://forge.example/core/go-io/pulls/12", PRNum: 12}
|
|
type CreatePROutput struct {
|
|
Success bool `json:"success"`
|
|
PRURL string `json:"pr_url,omitempty"`
|
|
PRNum int `json:"pr_number,omitempty"`
|
|
Title string `json:"title"`
|
|
Branch string `json:"branch"`
|
|
Repo string `json:"repo"`
|
|
Pushed bool `json:"pushed"`
|
|
}
|
|
|
|
func (s *PrepSubsystem) registerCreatePRTool(server *mcp.Server) {
|
|
mcp.AddTool(server, &mcp.Tool{
|
|
Name: "agentic_create_pr",
|
|
Description: "Create a pull request from an agent workspace. Pushes the branch to Forge and opens a PR. Links to the source issue if one was tracked.",
|
|
}, s.createPR)
|
|
}
|
|
|
|
func (s *PrepSubsystem) createPR(ctx context.Context, _ *mcp.CallToolRequest, input CreatePRInput) (*mcp.CallToolResult, CreatePROutput, error) {
|
|
if input.Workspace == "" {
|
|
return nil, CreatePROutput{}, core.E("createPR", "workspace is required", nil)
|
|
}
|
|
if s.forgeToken == "" {
|
|
return nil, CreatePROutput{}, core.E("createPR", "no Forge token configured", nil)
|
|
}
|
|
|
|
wsDir := core.JoinPath(WorkspaceRoot(), input.Workspace)
|
|
repoDir := WorkspaceRepoDir(wsDir)
|
|
|
|
if !fs.IsDir(core.JoinPath(repoDir, ".git")) {
|
|
return nil, CreatePROutput{}, core.E("createPR", core.Concat("workspace not found: ", input.Workspace), nil)
|
|
}
|
|
|
|
// Read workspace status for repo, branch, issue context
|
|
st, err := ReadStatus(wsDir)
|
|
if err != nil {
|
|
return nil, CreatePROutput{}, core.E("createPR", "no status.json", err)
|
|
}
|
|
|
|
if st.Branch == "" {
|
|
process := s.Core().Process()
|
|
r := process.RunIn(ctx, repoDir, "git", "rev-parse", "--abbrev-ref", "HEAD")
|
|
if !r.OK {
|
|
return nil, CreatePROutput{}, core.E("createPR", "failed to detect branch", nil)
|
|
}
|
|
st.Branch = core.Trim(r.Value.(string))
|
|
if st.Branch == "" {
|
|
return nil, CreatePROutput{}, core.E("createPR", "failed to detect branch", nil)
|
|
}
|
|
}
|
|
|
|
org := st.Org
|
|
if org == "" {
|
|
org = "core"
|
|
}
|
|
base := input.Base
|
|
if base == "" {
|
|
base = "dev"
|
|
}
|
|
|
|
// Build PR title
|
|
title := input.Title
|
|
if title == "" {
|
|
title = st.Task
|
|
}
|
|
if title == "" {
|
|
title = core.Sprintf("Agent work on %s", st.Branch)
|
|
}
|
|
|
|
// Build PR body
|
|
body := input.Body
|
|
if body == "" {
|
|
body = s.buildPRBody(st)
|
|
}
|
|
|
|
if input.DryRun {
|
|
return nil, CreatePROutput{
|
|
Success: true,
|
|
Title: title,
|
|
Branch: st.Branch,
|
|
Repo: st.Repo,
|
|
}, nil
|
|
}
|
|
|
|
// Push branch to Forge (origin is the local clone, not Forge)
|
|
forgeRemote := core.Sprintf("ssh://git@forge.lthn.ai:2223/%s/%s.git", org, st.Repo)
|
|
r := s.Core().Process().RunIn(ctx, repoDir, "git", "push", forgeRemote, st.Branch)
|
|
if !r.OK {
|
|
return nil, CreatePROutput{}, core.E("createPR", core.Concat("git push failed: ", r.Value.(string)), nil)
|
|
}
|
|
|
|
// Create PR via Forge API
|
|
prURL, prNum, err := s.forgeCreatePR(ctx, org, st.Repo, st.Branch, base, title, body)
|
|
if err != nil {
|
|
return nil, CreatePROutput{}, core.E("createPR", "failed to create PR", err)
|
|
}
|
|
|
|
// Update status with PR URL
|
|
st.PRURL = prURL
|
|
writeStatus(wsDir, st)
|
|
|
|
// Comment on issue if tracked
|
|
if st.Issue > 0 {
|
|
comment := core.Sprintf("Pull request created: %s", prURL)
|
|
s.commentOnIssue(ctx, org, st.Repo, st.Issue, comment)
|
|
}
|
|
|
|
return nil, CreatePROutput{
|
|
Success: true,
|
|
PRURL: prURL,
|
|
PRNum: prNum,
|
|
Title: title,
|
|
Branch: st.Branch,
|
|
Repo: st.Repo,
|
|
Pushed: true,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PrepSubsystem) buildPRBody(st *WorkspaceStatus) string {
|
|
b := core.NewBuilder()
|
|
b.WriteString("## Summary\n\n")
|
|
if st.Task != "" {
|
|
b.WriteString(st.Task)
|
|
b.WriteString("\n\n")
|
|
}
|
|
if st.Issue > 0 {
|
|
b.WriteString(core.Sprintf("Closes #%d\n\n", st.Issue))
|
|
}
|
|
b.WriteString(core.Sprintf("**Agent:** %s\n", st.Agent))
|
|
b.WriteString(core.Sprintf("**Runs:** %d\n", st.Runs))
|
|
b.WriteString("\n---\n*Created by agentic dispatch*\n")
|
|
return b.String()
|
|
}
|
|
|
|
func (s *PrepSubsystem) forgeCreatePR(ctx context.Context, org, repo, head, base, title, body string) (string, int, error) {
|
|
var pr pullRequestView
|
|
err := s.forge.Client().Post(ctx, core.Sprintf("/api/v1/repos/%s/%s/pulls", org, repo), &forge_types.CreatePullRequestOption{
|
|
Title: title,
|
|
Body: body,
|
|
Head: head,
|
|
Base: base,
|
|
}, &pr)
|
|
if err != nil {
|
|
return "", 0, core.E("forgeCreatePR", "create PR failed", err)
|
|
}
|
|
return pr.HTMLURL, int(pullRequestNumber(pr)), nil
|
|
}
|
|
|
|
func (s *PrepSubsystem) commentOnIssue(ctx context.Context, org, repo string, issue int, comment string) {
|
|
s.forge.Issues.CreateComment(ctx, org, repo, int64(issue), comment)
|
|
}
|
|
|
|
// --- agentic_list_prs ---
|
|
|
|
// ListPRsInput is the input for agentic_list_prs.
|
|
//
|
|
// input := agentic.ListPRsInput{Org: "core", Repo: "go-io", State: "open", Limit: 10}
|
|
type ListPRsInput struct {
|
|
Org string `json:"org,omitempty"` // forge org (default "core")
|
|
Repo string `json:"repo,omitempty"` // specific repo, or empty for all
|
|
State string `json:"state,omitempty"` // "open" (default), "closed", "all"
|
|
Limit int `json:"limit,omitempty"` // max results (default 20)
|
|
}
|
|
|
|
// ListPRsOutput is the output for agentic_list_prs.
|
|
//
|
|
// out := agentic.ListPRsOutput{Success: true, Count: 2, PRs: []agentic.PRInfo{{Repo: "go-io", Number: 12}}}
|
|
type ListPRsOutput struct {
|
|
Success bool `json:"success"`
|
|
Count int `json:"count"`
|
|
PRs []PRInfo `json:"prs"`
|
|
}
|
|
|
|
// PRInfo represents a pull request.
|
|
//
|
|
// pr := agentic.PRInfo{Repo: "go-io", Number: 12, Title: "Migrate pkg/fs", Branch: "agent/migrate-fs"}
|
|
type PRInfo struct {
|
|
Repo string `json:"repo"`
|
|
Number int `json:"number"`
|
|
Title string `json:"title"`
|
|
State string `json:"state"`
|
|
Author string `json:"author"`
|
|
Branch string `json:"branch"`
|
|
Base string `json:"base"`
|
|
Labels []string `json:"labels,omitempty"`
|
|
Mergeable bool `json:"mergeable"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
func (s *PrepSubsystem) registerListPRsTool(server *mcp.Server) {
|
|
mcp.AddTool(server, &mcp.Tool{
|
|
Name: "agentic_list_prs",
|
|
Description: "List pull requests across Forge repos. Filter by org, repo, and state (open/closed/all).",
|
|
}, s.listPRs)
|
|
}
|
|
|
|
func (s *PrepSubsystem) listPRs(ctx context.Context, _ *mcp.CallToolRequest, input ListPRsInput) (*mcp.CallToolResult, ListPRsOutput, error) {
|
|
if s.forgeToken == "" {
|
|
return nil, ListPRsOutput{}, core.E("listPRs", "no Forge token configured", nil)
|
|
}
|
|
|
|
if input.Org == "" {
|
|
input.Org = "core"
|
|
}
|
|
if input.State == "" {
|
|
input.State = "open"
|
|
}
|
|
if input.Limit == 0 {
|
|
input.Limit = 20
|
|
}
|
|
|
|
var repos []string
|
|
if input.Repo != "" {
|
|
repos = []string{input.Repo}
|
|
} else {
|
|
var err error
|
|
repos, err = s.listOrgRepos(ctx, input.Org)
|
|
if err != nil {
|
|
return nil, ListPRsOutput{}, err
|
|
}
|
|
}
|
|
|
|
var allPRs []PRInfo
|
|
|
|
for _, repo := range repos {
|
|
prs, err := s.listRepoPRs(ctx, input.Org, repo, input.State)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
allPRs = append(allPRs, prs...)
|
|
|
|
if len(allPRs) >= input.Limit {
|
|
break
|
|
}
|
|
}
|
|
|
|
if len(allPRs) > input.Limit {
|
|
allPRs = allPRs[:input.Limit]
|
|
}
|
|
|
|
return nil, ListPRsOutput{
|
|
Success: true,
|
|
Count: len(allPRs),
|
|
PRs: allPRs,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PrepSubsystem) listRepoPRs(ctx context.Context, org, repo, state string) ([]PRInfo, error) {
|
|
var prs []pullRequestView
|
|
err := s.forge.Client().Get(ctx, core.Sprintf("/api/v1/repos/%s/%s/pulls?limit=50&page=1", org, repo), &prs)
|
|
if err != nil {
|
|
return nil, core.E("listRepoPRs", core.Concat("failed to list PRs for ", repo), err)
|
|
}
|
|
|
|
var result []PRInfo
|
|
for _, pr := range prs {
|
|
prState := pr.State
|
|
if prState == "" {
|
|
prState = "open"
|
|
}
|
|
if state != "" && state != "all" && prState != state {
|
|
continue
|
|
}
|
|
var labels []string
|
|
for _, l := range pr.Labels {
|
|
labels = append(labels, l.Name)
|
|
}
|
|
result = append(result, PRInfo{
|
|
Repo: repo,
|
|
Number: int(pullRequestNumber(pr)),
|
|
Title: pr.Title,
|
|
State: prState,
|
|
Author: pullRequestAuthor(pr),
|
|
Branch: pr.Head.Ref,
|
|
Base: pr.Base.Ref,
|
|
Labels: labels,
|
|
Mergeable: pr.Mergeable,
|
|
URL: pr.HTMLURL,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|