using System; using System.Collections.Generic; namespace UnityTcp.Editor.Helpers { /// /// Provides static methods for creating standardized success and error response objects. /// Ensures consistent JSON structure for communication back to the Codely client. /// /// Response format: { success, message, data? } /// /// There is no pending/op_id/poll_interval variant: a tool call returns only when its work is /// finished, so there is nothing for a client to poll or correlate. /// /// Responses carry only the tool's own result. Editor state is an explicit /// read via manage_editor.get_state, not an ambient field on every /// response. /// public static class Response { /// /// Creates a standardized success response object. /// /// A message describing the successful operation. /// Optional additional data to include in the response. /// An object representing the success response. public static object Success(string message, object data = null) { var response = new Dictionary { { "success", true }, { "message", message } }; if (data != null) { response["data"] = data; } return response; } /// /// Creates a standardized error response object. /// /// A message describing the error. /// Optional additional data (e.g., error details) to include. /// An object representing the error response. public static object Error(string errorCodeOrMessage, object data = null) { var response = new Dictionary { { "success", false }, { "message", errorCodeOrMessage }, { "code", errorCodeOrMessage }, { "error", errorCodeOrMessage } }; if (data != null) { response["data"] = data; } return response; } /// /// Creates an error response whose code is a stable token clients can branch on, /// separate from the human-readable message. copies its single argument /// into all three of code, message and error, which is fine for a bare /// message but destroys the token as soon as the message says anything more. /// /// Carries no data: an asynchronous response is rendered as a success by the client /// whenever it has a payload, so a failure must be message-only. /// public static object ErrorWithCode(string code, string message) => new Dictionary { { "success", false }, { "message", message }, { "code", code }, { "error", code }, }; } }