import Foundation /// Exit codes shared by the CLI, SSH and both HTTP surfaces. /// `internal/protocol/protocol.go` is the source of truth. nonisolated enum ExitCode: Int, Sendable { case ok = 0 case failure = 1 case usage = 2 case notFound = 3 case denied = 4 case protocolError = 5 } /// Every response from either API surface is the command's own JSON /// envelope with `exit_code` added. /// /// `data` is absent when a command emits an empty result — Go omits a nil /// slice — so an empty list arrives as no key at all rather than `[]`. /// `output` carries commands that write raw text instead of JSON (`mr /// diff`, `help`); the server wraps those. nonisolated struct Envelope: Decodable, Sendable { let protocolVersion: Int let data: Payload? let error: String? let stderr: String? let output: String? let exitCode: Int? enum CodingKeys: String, CodingKey { case protocolVersion = "protocol_version" case data, error, stderr, output case exitCode = "exit_code" } /// The message to put in front of a person. `error` and `stderr` were /// both written to be read by one. var message: String? { let text = error ?? stderr guard let text, !text.isEmpty else { return nil } return text } } /// Placeholder for commands whose `data` we do not decode. It accepts any /// JSON, so discarding a payload never fails on its shape. nonisolated struct NoPayload: Decodable, Sendable { init(from _: any Decoder) throws {} }