package gateway import ( "context" "fmt" "strings" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/forge" "github.com/samcharles93/archie-core/internal/prompts" "github.com/samcharles93/archie-core/internal/tools" ) // maxIssueListed bounds what issue_list returns, so a large backlog does not // land in the context window whole. const maxIssueListed = 200 // IssueSource is the forge surface the issue tools use, reached through the // daemon. type IssueSource interface { OpenIssues(ctx context.Context, owner, repo string) ([]forge.Issue, error) CreateIssue(ctx context.Context, owner, repo, title, body string, labels []string) (int, error) } // IssueSummary is one open issue as issue_list shows it. type IssueSummary struct { Number int `json:"number"` Title string `json:"title"` Labels []string `json:"labels,omitempty"` } // IssueTools returns issue_list and issue_create for curators. Filing work // feeds the daemon's issue intake, so both are checked as acting on tasks. // Nil source or authorizer registers none. func IssueTools(source IssueSource, authz access.Authorizer) []tools.ToolEntry { if source == nil || authz == nil { return nil } return []tools.ToolEntry{ guarded(authz, access.ActionRead, access.KindTask, nil, issueListTool(source)), guarded(authz, access.ActionCreate, access.KindTask, nil, issueCreateTool(source)), } } var repoProperty = map[string]any{ "type": "string", "description": "The repository as owner/name, as run_list reports it.", } func splitRepo(input map[string]any) (owner, repo string, err error) { raw, _ := input["repo"].(string) owner, repo, ok := strings.Cut(strings.TrimSpace(raw), "/") if !ok || owner == "" || repo == "" || strings.Contains(repo, "/") { return "", "", fmt.Errorf("repo must be owner/name") } return owner, repo, nil } func issueListTool(source IssueSource) tools.ToolEntry { return tools.ToolEntry{ Name: "issue_list", Toolset: "issues", Description: "List a repository's open issues (number, title, labels). Check it before filing so you never duplicate one.", Classification: tools.ClassIdempotent, Schema: tools.JSONSchema{ "type": "object", "properties": map[string]any{"repo": repoProperty}, "required": []string{"repo"}, }, Handler: func(ctx context.Context, input map[string]any) (any, error) { owner, repo, err := splitRepo(input) if err != nil { return nil, fmt.Errorf("issue_list: %w", err) } issues, err := source.OpenIssues(ctx, owner, repo) if err != nil { return nil, fmt.Errorf("issue_list: %w", err) } out := make([]IssueSummary, 0, min(len(issues), maxIssueListed)) for _, is := range issues[:min(len(issues), maxIssueListed)] { out = append(out, IssueSummary{Number: is.Number, Title: is.Title, Labels: is.Labels}) } return map[string]any{"notice": prompts.UntrustedForgeData(), "issues": out}, nil }, } } func issueCreateTool(source IssueSource) tools.ToolEntry { return tools.ToolEntry{ Name: "issue_create", Toolset: "issues", Description: "File a new issue in a repository. List open issues first and file only work no open issue covers. " + "The body must cite the run that produced it.", Classification: tools.ClassMutating, Schema: tools.JSONSchema{ "type": "object", "properties": map[string]any{ "repo": repoProperty, "title": map[string]any{"type": "string"}, "body": map[string]any{"type": "string"}, "labels": map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, }, "required": []string{"repo", "title", "body"}, }, Handler: func(ctx context.Context, input map[string]any) (any, error) { owner, repo, err := splitRepo(input) if err != nil { return nil, fmt.Errorf("issue_create: %w", err) } title, _ := input["title"].(string) body, _ := input["body"].(string) if strings.TrimSpace(title) == "" || strings.TrimSpace(body) == "" { return nil, fmt.Errorf("issue_create: title and body are required") } var labels []string if raw, ok := input["labels"].([]any); ok { for _, l := range raw { if s, ok := l.(string); ok && s != "" { labels = append(labels, s) } } } number, err := source.CreateIssue(ctx, owner, repo, title, body, labels) if err != nil { return nil, fmt.Errorf("issue_create: %w", err) } return map[string]any{"number": number}, nil }, } }