#if AIA_PRESENT using System; using System.Collections.Generic; using System.Threading.Tasks; using PlasticGui; using Unity.AI.Assistant.FunctionCalling; using Unity.Plastic.Newtonsoft.Json.Linq; using Unity.PlasticSCM.Editor.Assistant.UI.Interactions; using CodeReviewStatus = Codice.CM.Common.CodeReviewStatus; namespace Unity.PlasticSCM.Editor.Assistant.Tools { static class UVCSExecuteTool { [AgentTool( "Single entry point for ALL Unity Version Control (UVCS) operations. " + "Pick the 'operation' to run and pass its arguments as a JSON object string in 'parametersJson'. " + "Each operation maps to a specific UVCS capability (querying branches/changesets/history/reviews/locks/labels, " + "diffing, checkin/shelve, undo/revert, navigating the UVCS UI, and asking the user to choose or confirm). " + "Refer to the UVCS tool reference for the exact JSON parameter shape and return value of each operation. " + "Example: operation=FindQuery, parametersJson={\"query\":\"find branch where name like 'scm%'\"}. " + "Operations that take no parameters can omit 'parametersJson'.", "Unity.UVCS.Execute")] internal static async Task Execute( ToolExecutionContext context, [ToolParameter("The UVCS operation to run. Must be one of the listed values.")] UVCSOperation operation, [ToolParameter("JSON object string with the parameters for the chosen operation " + "(e.g. {\"branchName\":\"/main/task001\"}). See the UVCS tool reference for the exact " + "shape of each operation. Omit for operations that take no parameters.")] string parametersJson = null) { JObject p = ParseParams(operation, parametersJson); switch (operation) { // Onboarding / project management case UVCSOperation.GetKnownServers: return await ProjectManagementTools.GetKnownUVCSServers(); case UVCSOperation.GetAllRepositories: return await ProjectManagementTools.GetAllRepositories( GetParam(p, operation, "server")); case UVCSOperation.GetAllProjects: return await ProjectManagementTools.GetAllProjects( GetParam(p, operation, "server")); case UVCSOperation.GetWorkspaceFromPath: return await ProjectManagementTools.GetWorkspaceFromPath( GetParam(p, operation, "path")); case UVCSOperation.CreateRepository: await ProjectManagementTools.CreateRepository( GetParam(p, operation, "repositoryName"), GetParam(p, operation, "server")); return null; case UVCSOperation.CreateWorkspace: return await ProjectManagementTools.CreateWorkspace( context, GetParam(p, operation, "workspaceName"), GetParam(p, operation, "workspacePath"), GetParam(p, operation, "repositoryName")); case UVCSOperation.PerformInitialCheckin: await ProjectManagementTools.PerformInitialCheckin(context); return null; case UVCSOperation.RefreshUIAfterCreateWorkspace: await ProjectManagementTools.RefreshUIAfterCreateWorkspace(); return null; case UVCSOperation.CheckinPendingChanges: return await ProjectManagementTools.CheckinPendingChanges( context, GetParam(p, operation, "comment")); case UVCSOperation.ShelvePendingChanges: return await ProjectManagementTools.ShelvePendingChanges( context, GetParam(p, operation, "comment"), GetOptionalParam(p, "undoPendingChangesAfterShelve", false)); // Query case UVCSOperation.FindQuery: return await QueryTools.FindQuery(GetParam(p, operation, "query")); case UVCSOperation.GetWorkspaceConfiguration: return await QueryTools.GetWorkspaceConfiguration(); // Branches case UVCSOperation.GetBranchInfo: return await BranchTools.GetBranchInfo(GetParam(p, operation, "branchName")); case UVCSOperation.CreateBranch: await BranchTools.CreateBranch( GetParam(p, operation, "branchName"), GetOptionalParam(p, "comment", null), GetOptionalParam(p, "parentBranchName", null), GetOptionalParam(p, "changesetId", -1L)); return null; case UVCSOperation.RenameBranch: await BranchTools.RenameBranch( GetParam(p, operation, "branchName"), GetParam(p, operation, "newBranchName")); return null; case UVCSOperation.LaunchSwitchToBranchUI: await BranchTools.LaunchSwitchToBranchUI(GetParam(p, operation, "branchName")); return null; // Annotate / revisions case UVCSOperation.GetFileAnnotations: return await AnnotateTools.GetFileAnnotations( GetParam(p, operation, "filePath"), GetOptionalParam(p, "revisionId", -1L)); case UVCSOperation.GetRevisionInfo: return await AnnotateTools.GetRevisionInfo(GetParam(p, operation, "revisionId")); // History case UVCSOperation.GetFileHistory: return await HistoryTools.GetFileHistory(GetParam(p, operation, "filePath")); // Diff case UVCSOperation.GetBranchDifferences: return await DiffTools.GetBranchDifferences(GetParam(p, operation, "branchName")); case UVCSOperation.GetChangesetDifferences: return await DiffTools.GetChangesetDifferences(GetParam(p, operation, "changesetId")); case UVCSOperation.GetChangesetsDifferences: return await DiffTools.GetChangesetsDifferences( GetParam(p, operation, "srcChangesetId"), GetParam(p, operation, "dstChangesetId")); case UVCSOperation.GetLabelDifferences: return await DiffTools.GetLabelDifferences(GetParam(p, operation, "labelName")); case UVCSOperation.GetShelveDifferences: return await DiffTools.GetShelveDifferences(GetParam(p, operation, "shelveId")); case UVCSOperation.GetPendingChanges: return await DiffTools.GetPendingChanges(); case UVCSOperation.DownloadRevisionsToFiles: return await DiffTools.DownloadRevisionsToFiles( context, GetParam>(p, operation, "revisionIds")); case UVCSOperation.CleanupDiffTempFiles: await DiffTools.CleanupDiffTempFiles(context); return null; // Changesets case UVCSOperation.GetChangesetInfo: return await ChangesetTools.GetChangesetInfo(GetParam(p, operation, "changesetId")); // Code review case UVCSOperation.CreateCodeReviewForBranch: return await CodeReviewTools.CreateCodeReviewForBranch( GetParam(p, operation, "branchName")); case UVCSOperation.CreateCodeReviewForChangeset: return await CodeReviewTools.CreateCodeReviewForChangeset( GetParam(p, operation, "changesetId")); case UVCSOperation.CreateConversationComment: return await CodeReviewTools.CreateConversationComment( GetParam(p, operation, "reviewId"), GetParam(p, operation, "commentText")); case UVCSOperation.CreateCodeComment: return await CodeReviewTools.CreateCodeComment( GetParam(p, operation, "reviewId"), GetParam(p, operation, "revisionId"), GetParam(p, operation, "changesetId"), GetParam(p, operation, "locationSpec"), GetParam(p, operation, "commentText"), GetParam(p, operation, "commentType")); case UVCSOperation.GetCodeReviewLink: return await CodeReviewTools.GetCodeReviewLink(GetParam(p, operation, "reviewId")); case UVCSOperation.AddCurrentUserAsReviewer: await CodeReviewTools.AddCurrentUserAsReviewer(GetParam(p, operation, "reviewId")); return null; case UVCSOperation.SetReviewStatus: await CodeReviewTools.SetReviewStatus( GetParam(p, operation, "reviewId"), GetParam(p, operation, "status"), GetParam(p, operation, "comment")); return null; case UVCSOperation.GetReviewsInStatus: return await CodeReviewTools.GetReviewsInStatus( GetParam(p, operation, "status"), GetParam(p, operation, "targetId")); case UVCSOperation.GetReviewComments: return await CodeReviewTools.GetReviewComments(GetParam(p, operation, "reviewId")); case UVCSOperation.ReplyComment: return await CodeReviewTools.ReplyComment( GetParam(p, operation, "reviewId"), GetParam(p, operation, "parentCommentId"), GetParam(p, operation, "commentText"), GetParam(p, operation, "replyType")); case UVCSOperation.ReRequestReview: await CodeReviewTools.ReRequestReview(GetParam(p, operation, "reviewId")); return null; // Labels case UVCSOperation.CreateLabel: return await LabelTools.CreateLabel( GetParam(p, operation, "labelName"), GetParam(p, operation, "comment"), GetOptionalParam(p, "changesetId", -1L)); case UVCSOperation.RenameLabel: await LabelTools.RenameLabel( GetParam(p, operation, "labelName"), GetParam(p, operation, "newLabelName")); return null; case UVCSOperation.ApplyLabel: await LabelTools.ApplyLabel(GetParam(p, operation, "labelName")); return null; case UVCSOperation.DeleteLabel: await LabelTools.DeleteLabel(GetParam(p, operation, "labelName")); return null; // Locks case UVCSOperation.ListLocks: return await LockTools.ListLocks(); case UVCSOperation.GetLockRule: return await LockTools.GetLockRule(); case UVCSOperation.ReleaseLocks: await LockTools.ReleaseLocks(GetParam>(p, operation, "lockIds")); return null; case UVCSOperation.RemoveLocks: await LockTools.RemoveLocks(GetParam>(p, operation, "lockIds")); return null; // Undo / revert case UVCSOperation.UndoChanges: await UndoTools.UndoChanges(context, GetParam>(p, operation, "paths")); return null; case UVCSOperation.UndoUnchanged: await UndoTools.UndoUnchanged(); return null; case UVCSOperation.RevertToRevision: await UndoTools.RevertToRevision( context, GetParam(p, operation, "filePath"), GetParam(p, operation, "revisionId")); return null; // UI navigation case UVCSOperation.RefreshView: await UITools.RefreshView(GetParam(p, operation, "viewType")); return null; case UVCSOperation.ShowView: await UITools.ShowView(GetParam(p, operation, "viewType")); return null; case UVCSOperation.ShowBranchExplorer: await UITools.ShowBranchExplorer(); return null; case UVCSOperation.ShowFileHistory: await UITools.ShowFileHistory(GetParam(p, operation, "filePath")); return null; case UVCSOperation.ShowBranchesView: await UITools.ShowBranchesView(GetOptionalParam(p, "branchName", null)); return null; case UVCSOperation.ShowChangesetsView: await UITools.ShowChangesetsView(GetOptionalParam(p, "changesetId", -1L)); return null; case UVCSOperation.ShowShelvesView: await UITools.ShowShelvesView(GetOptionalParam(p, "shelveId", -1L)); return null; case UVCSOperation.LaunchMergeFromBranchUI: await UITools.LaunchMergeFromBranchUI(GetParam(p, operation, "branchName")); return null; case UVCSOperation.LaunchMergeFromChangesetUI: await UITools.LaunchMergeFromChangesetUI(GetParam(p, operation, "changesetId")); return null; case UVCSOperation.LaunchApplyShelveUI: await UITools.LaunchApplyShelveUI(GetParam(p, operation, "shelveId")); return null; // User interaction case UVCSOperation.SelectFromList: return await InteractionTools.SelectFromList( context, GetParam(p, operation, "title"), GetParam(p, operation, "message"), GetParam(p, operation, "label"), GetParam>(p, operation, "choices")); case UVCSOperation.SelectOption: return await InteractionTools.SelectOption( context, GetParam(p, operation, "title"), GetParam(p, operation, "message"), GetParam>(p, operation, "options")); case UVCSOperation.AskForConfirmation: return await InteractionTools.AskForConfirmation( context, GetParam(p, operation, "action"), GetParam(p, operation, "detail")); default: throw new ArgumentException( "Unknown UVCS operation '" + operation + "'."); } } static JObject ParseParams(UVCSOperation operation, string parametersJson) { if (string.IsNullOrWhiteSpace(parametersJson)) return new JObject(); try { return JObject.Parse(parametersJson); } catch (Exception ex) { throw new ArgumentException( "Could not parse parametersJson for operation '" + operation + "': " + ex.Message + ". Provide a JSON object, for example {\"branchName\":\"/main/task001\"}."); } } static T GetParam(JObject p, UVCSOperation operation, string name) { JToken token = p[name]; if (token == null || token.Type == JTokenType.Null) throw new ArgumentException( "Operation '" + operation + "' requires parameter '" + name + "' in parametersJson."); try { return token.ToObject(); } catch (Exception ex) { throw new ArgumentException( "Parameter '" + name + "' for operation '" + operation + "' is invalid: " + ex.Message); } } static T GetOptionalParam(JObject p, string name, T defaultValue) { JToken token = p[name]; if (token == null || token.Type == JTokenType.Null) return defaultValue; return token.ToObject(); } } } #endif