|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/morluto/gitcontribute/internal/commitplan" |
| 7 | + "github.com/morluto/gitcontribute/internal/mcpserver" |
| 8 | +) |
| 9 | + |
| 10 | +// InspectCommitChanges implements the MCP commit-inventory capability. |
| 11 | +func (r *MCPReader) InspectCommitChanges(ctx context.Context, in mcpserver.InspectCommitChangesInput) (mcpserver.CommitInventoryOutput, error) { |
| 12 | + inventory, err := r.Service.InspectCommitChanges(ctx, in.WorkspaceID) |
| 13 | + if err != nil { |
| 14 | + return mcpserver.CommitInventoryOutput{}, err |
| 15 | + } |
| 16 | + return commitInventoryToMCP(inventory), nil |
| 17 | +} |
| 18 | + |
| 19 | +// PlanSemanticCommits implements the MCP read-only semantic planning capability. |
| 20 | +func (r *MCPReader) PlanSemanticCommits(ctx context.Context, in mcpserver.PlanSemanticCommitsInput) (mcpserver.SemanticCommitPlanOutput, error) { |
| 21 | + groups := make([]commitplan.GroupInput, len(in.Groups)) |
| 22 | + for index, group := range in.Groups { |
| 23 | + groups[index] = commitplan.GroupInput{ |
| 24 | + Name: group.Name, Intent: group.Intent, Type: group.Type, Scope: group.Scope, |
| 25 | + UnitIDs: group.UnitIDs, DependsOn: group.DependsOn, |
| 26 | + ValidationCommands: group.ValidationCommands, TestOwners: group.TestOwners, |
| 27 | + } |
| 28 | + } |
| 29 | + unresolved := make([]commitplan.UnresolvedInput, len(in.Unresolved)) |
| 30 | + for index, item := range in.Unresolved { |
| 31 | + unresolved[index] = commitplan.UnresolvedInput{UnitID: item.UnitID, Reason: item.Reason} |
| 32 | + } |
| 33 | + plan, err := r.Service.PlanSemanticCommits(ctx, in.WorkspaceID, in.ExpectedInventorySHA256, commitplan.PlanInput{Groups: groups, Unresolved: unresolved}) |
| 34 | + if err != nil { |
| 35 | + return mcpserver.SemanticCommitPlanOutput{}, err |
| 36 | + } |
| 37 | + return semanticCommitPlanToMCP(plan), nil |
| 38 | +} |
| 39 | + |
| 40 | +func commitInventoryToMCP(inventory commitplan.Inventory) mcpserver.CommitInventoryOutput { |
| 41 | + out := mcpserver.CommitInventoryOutput{SourcePatchSHA256: inventory.SourcePatchSHA256, InventorySHA256: inventory.InventorySHA256} |
| 42 | + for _, unit := range inventory.Units { |
| 43 | + out.Units = append(out.Units, mcpserver.CommitUnitOutput{ |
| 44 | + ID: unit.ID, Kind: unit.Kind, Path: unit.Path, OldPath: unit.OldPath, Operation: unit.Operation, |
| 45 | + OldStart: unit.OldStart, OldLines: unit.OldLines, NewStart: unit.NewStart, NewLines: unit.NewLines, |
| 46 | + Patch: unit.Patch, ContentSHA256: unit.ContentHash, Generated: unit.Generated, WhitespaceOnly: unit.WhitespaceOnly, |
| 47 | + }) |
| 48 | + } |
| 49 | + out.Warnings = commitWarningsToMCP(inventory.Warnings) |
| 50 | + return out |
| 51 | +} |
| 52 | + |
| 53 | +func semanticCommitPlanToMCP(plan commitplan.Plan) mcpserver.SemanticCommitPlanOutput { |
| 54 | + out := mcpserver.SemanticCommitPlanOutput{ |
| 55 | + Warnings: commitWarningsToMCP(plan.Warnings), |
| 56 | + Reconstruction: mcpserver.CommitReconstructionOutput{ |
| 57 | + SourcePatchSHA256: plan.Reconstruction.SourcePatchSHA256, InventorySHA256: plan.Reconstruction.InventorySHA256, |
| 58 | + AssignedSHA256: plan.Reconstruction.AssignedSHA256, UnitCount: plan.Reconstruction.UnitCount, |
| 59 | + AssignedCount: plan.Reconstruction.AssignedCount, Verified: plan.Reconstruction.Verified, |
| 60 | + }, |
| 61 | + } |
| 62 | + for _, group := range plan.Groups { |
| 63 | + out.Groups = append(out.Groups, mcpserver.SemanticCommitGroupOutput{ |
| 64 | + Name: group.Name, Intent: group.Intent, SuggestedSubject: group.SuggestedSubject, |
| 65 | + UnitIDs: group.UnitIDs, Files: group.Files, DependsOn: group.DependsOn, |
| 66 | + ValidationCommands: group.ValidationCommands, TestOwners: group.TestOwners, |
| 67 | + }) |
| 68 | + } |
| 69 | + for _, item := range plan.Unresolved { |
| 70 | + out.Unresolved = append(out.Unresolved, mcpserver.UnresolvedCommitUnitOutput{UnitID: item.UnitID, Reason: item.Reason}) |
| 71 | + } |
| 72 | + return out |
| 73 | +} |
| 74 | + |
| 75 | +func commitWarningsToMCP(warnings []commitplan.Warning) []mcpserver.CommitPlanWarningOutput { |
| 76 | + out := make([]mcpserver.CommitPlanWarningOutput, len(warnings)) |
| 77 | + for index, warning := range warnings { |
| 78 | + out[index] = mcpserver.CommitPlanWarningOutput{Code: warning.Code, Message: warning.Message, Path: warning.Path, UnitID: warning.UnitID} |
| 79 | + } |
| 80 | + return out |
| 81 | +} |
0 commit comments