Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/github/__toolsnaps__/create_automation.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"annotations": {
"destructiveHint": false,
"idempotentHint": false,
"readOnlyHint": false,
"title": "Create a repository automation"
},
"description": "Create a Copilot automation in a repository after explicit user confirmation.",
"description": "Create a Copilot automation in a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"continuous_history": {
"description": "Whether the automation keeps a continuous conversation history.",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/__toolsnaps__/delete_automation.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"description": "Delete a Copilot automation from a repository after explicit user confirmation.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/__toolsnaps__/get_automation.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "Get the configuration of a Copilot automation in a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/__toolsnaps__/get_automation_revision.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "Get a specific saved revision of a Copilot automation.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "Get whether automations are enabled, whether you can manage them, and the repository's automation trigger and write-scope policies.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"owner": {
"description": "Repository owner",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/__toolsnaps__/list_automation_revisions.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "List the saved revisions of a Copilot automation.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/__toolsnaps__/list_automations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "List Copilot automations for a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"owner": {
"description": "Repository owner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "List the tools that can be used by automations in a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"owner": {
"description": "Repository owner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"description": "List the trigger types that can be used by automations in a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"owner": {
"description": "Repository owner",
Expand Down
3 changes: 2 additions & 1 deletion pkg/github/__toolsnaps__/run_automation.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"readOnlyHint": false,
"title": "Run a repository automation"
},
"description": "Start a manual run of a Copilot automation in a repository after explicit user confirmation.",
"description": "Start a manual run of a Copilot automation in a repository.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
4 changes: 3 additions & 1 deletion pkg/github/__toolsnaps__/update_automation.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"annotations": {
"destructiveHint": false,
"idempotentHint": false,
"readOnlyHint": false,
"title": "Update a repository automation"
},
"description": "Update a Copilot automation in a repository after explicit user confirmation. Only the provided fields are changed.",
"description": "Update a Copilot automation in a repository. Only the provided fields are changed.",
"inputSchema": {
"additionalProperties": false,
"properties": {
"automation_id": {
"description": "The automation identifier.",
Expand Down
99 changes: 53 additions & 46 deletions pkg/github/agent_automations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"net/http"
"net/url"
"sort"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -225,12 +226,19 @@ func newAgentAutomationTool[Input any](
}
return utils.NewToolResultError("GitHub agent automations returned an empty response"), nil, nil
}
if httpResponse.Cursor != "" {
response, err = withAutomationNextCursor(response, httpResponse.Cursor)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to add the next page cursor", err), nil, nil
}
}
sanitizedResponse, err := sanitizeAutomationNames(response)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to sanitize GitHub agent automation response", err), nil, nil
}
return utils.NewToolResultText(string(sanitizedResponse)), sanitizedResponse, nil
},
rejectUnknownAutomationArguments(inputSchema),
)
tool.FeatureRule = featureEnabledRule(FeatureFlagAgentAutomations)
return tool
Expand Down Expand Up @@ -349,20 +357,6 @@ func agentAutomationConfirmationRequired(tool inventory.ServerTool) inventory.Se
return tool
}

func agentAutomationMutationConfirmation(action, target string, body map[string]any) *agentAutomationConfirmation {
fields := make([]string, 0, len(body))
for field := range body {
fields = append(fields, field)
}
sort.Strings(fields)
value := strings.ToUpper(action) + " " + target
return &agentAutomationConfirmation{
id: agentAutomationConfirmationIDPrefix + action,
value: value,
message: fmt.Sprintf("Review the proposed automation %s for %s. The request changes these fields: %s. Values are not shown here. Type %q to confirm.", action, target, strings.Join(fields, ", "), value),
}
}

func agentAutomationTarget(owner, repo, automationID string) string {
target := url.PathEscape(owner) + "/" + url.PathEscape(repo)
if automationID != "" {
Expand Down Expand Up @@ -399,7 +393,27 @@ func agentAutomationConfigurationOutputSchema() json.RawMessage {
}

func agentAutomationInputSchema(properties map[string]*jsonschema.Schema, required ...string) *jsonschema.Schema {
return &jsonschema.Schema{Type: "object", Properties: properties, Required: required}
return &jsonschema.Schema{
Type: "object",
Properties: properties,
Required: required,
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
Comment thread
A-Georgiou marked this conversation as resolved.
Resolved
}
}

func rejectUnknownAutomationArguments(schema *jsonschema.Schema) inventory.InputNormalizer {
return func(raw json.RawMessage) (json.RawMessage, error) {
var args map[string]json.RawMessage
if err := json.Unmarshal(raw, &args); err != nil {
return nil, &inventory.ToolInputError{Message: "invalid arguments: " + err.Error()}
}
for _, name := range slices.Sorted(maps.Keys(args)) {
if _, ok := schema.Properties[name]; !ok {
return nil, &inventory.ToolInputError{Message: fmt.Sprintf("invalid arguments: unexpected argument %q", name)}
}
}
return raw, nil
}
}

func automationStringProperty(description string) *jsonschema.Schema {
Expand Down Expand Up @@ -491,6 +505,15 @@ func sanitizeAutomationNames(response json.RawMessage) (json.RawMessage, error)
return sanitizedResponse, nil
}

func withAutomationNextCursor(response json.RawMessage, cursor string) (json.RawMessage, error) {
var payload map[string]any
if err := json.Unmarshal(response, &payload); err != nil || payload == nil {
return response, err
}
payload["next_cursor"] = cursor
return json.Marshal(payload)
}

func sanitizeAutomationNamesValue(value any) {
switch value := value.(type) {
case map[string]any:
Expand Down Expand Up @@ -743,14 +766,14 @@ func CreateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool
properties := automationConfigurationProperties()
properties["owner"] = automationStringProperty(DescriptionRepositoryOwner)
properties["repo"] = automationStringProperty(DescriptionRepositoryName)
return agentAutomationConfirmationRequired(newAgentAutomationTool[CreateAutomationInput](
return newAgentAutomationTool[CreateAutomationInput](
Comment thread
A-Georgiou marked this conversation as resolved.
Dismissed
t,
"create_automation",
"Create a repository automation",
"Create a Copilot automation in a repository after explicit user confirmation.",
"Create a Copilot automation in a repository.",
agentAutomationInputSchema(properties, "owner", "repo", "name", "prompt"),
agentAutomationObjectOutputSchema(),
mcp.ToolAnnotations{},
mcp.ToolAnnotations{DestructiveHint: new(false)},
func(args CreateAutomationInput) (agentAutomationRequest, error) {
if err := requireAutomationRepository(args.Owner, args.Repo); err != nil {
return agentAutomationRequest{}, err
Expand Down Expand Up @@ -789,16 +812,14 @@ func CreateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool
if args.ContinuousHistory != nil {
body["continuous_history"] = *args.ContinuousHistory
}
target := agentAutomationTarget(args.Owner, args.Repo, "")
return agentAutomationRequest{
method: http.MethodPost,
path: automationPath(args.Owner, args.Repo, ""),
body: body,
successStatus: http.StatusCreated,
confirmation: agentAutomationMutationConfirmation("create", target, body),
}, nil
},
))
)
}

func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool {
Expand All @@ -807,14 +828,14 @@ func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool
properties["repo"] = automationStringProperty(DescriptionRepositoryName)
properties["automation_id"] = automationStringProperty("The automation identifier.")
delete(properties, "type")
return agentAutomationConfirmationRequired(newAgentAutomationTool[UpdateAutomationInput](
return newAgentAutomationTool[UpdateAutomationInput](
t,
"update_automation",
"Update a repository automation",
"Update a Copilot automation in a repository after explicit user confirmation. Only the provided fields are changed.",
"Update a Copilot automation in a repository. Only the provided fields are changed.",
agentAutomationInputSchema(properties, "owner", "repo", "automation_id"),
agentAutomationObjectOutputSchema(),
mcp.ToolAnnotations{},
mcp.ToolAnnotations{DestructiveHint: new(false)},
func(args UpdateAutomationInput) (agentAutomationRequest, error) {
if err := requireAutomationRepository(args.Owner, args.Repo); err != nil {
return agentAutomationRequest{}, err
Expand Down Expand Up @@ -853,15 +874,13 @@ func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool
if len(body) == 0 {
return agentAutomationRequest{}, errors.New("at least one automation field must be provided")
}
target := agentAutomationTarget(args.Owner, args.Repo, args.AutomationID)
return agentAutomationRequest{
method: http.MethodPatch,
path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)),
body: body,
confirmation: agentAutomationMutationConfirmation("update", target, body),
method: http.MethodPatch,
path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)),
body: body,
}, nil
},
))
)
}

func DeleteAutomation(t translations.TranslationHelperFunc) inventory.ServerTool {
Expand Down Expand Up @@ -902,11 +921,11 @@ func DeleteAutomation(t translations.TranslationHelperFunc) inventory.ServerTool
}

func RunAutomation(t translations.TranslationHelperFunc) inventory.ServerTool {
return agentAutomationConfirmationRequired(newAgentAutomationTool[RunAutomationInput](
return newAgentAutomationTool[RunAutomationInput](
Comment thread
A-Georgiou marked this conversation as resolved.
Dismissed
t,
"run_automation",
"Run a repository automation",
"Start a manual run of a Copilot automation in a repository after explicit user confirmation.",
"Start a manual run of a Copilot automation in a repository.",
agentAutomationInputSchema(map[string]*jsonschema.Schema{
"owner": automationStringProperty(DescriptionRepositoryOwner),
"repo": automationStringProperty(DescriptionRepositoryName),
Expand Down Expand Up @@ -941,24 +960,12 @@ func RunAutomation(t translations.TranslationHelperFunc) inventory.ServerTool {
if args.Context != nil {
body["context"] = *args.Context
}
target := agentAutomationTarget(args.Owner, args.Repo, args.AutomationID)
message := fmt.Sprintf("Review the automation run request for %s.", target)
if args.Context != nil {
message += fmt.Sprintf("\nA run context is included (%d characters).", utf8.RuneCountInString(*args.Context))
}
confirmationValue := "RUN " + target
message += fmt.Sprintf("\nType %q to start this run.", confirmationValue)
return agentAutomationRequest{
method: http.MethodPost,
path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)+"/tasks"),
body: body,
successStatus: http.StatusAccepted,
confirmation: &agentAutomationConfirmation{
id: agentAutomationConfirmationIDPrefix + "run",
value: confirmationValue,
message: message,
},
}, nil
},
))
)
}
Loading
Loading