syntax = "proto3";

package runme.parser.v1;

import "google/protobuf/wrappers.proto";

option go_package = "github.com/stateful/runme/internal/gen/proto/go/runme/parser/v1;parserv1";

message Notebook {
  repeated Cell cells = 1;
  map<string, string> metadata = 2;
  Frontmatter frontmatter = 3;
}

message ExecutionSummaryTiming {
  google.protobuf.Int64Value start_time = 1;
  google.protobuf.Int64Value end_time = 2;
}

message CellOutputItem {
  bytes data = 1;
  string type = 2;
  string mime = 3;
}

message ProcessInfoExitReason {
  string type = 1;
  google.protobuf.UInt32Value code = 2;
}

message CellOutputProcessInfo {
  ProcessInfoExitReason exit_reason = 1;
  google.protobuf.Int64Value pid = 2;
}

message CellOutput {
  repeated CellOutputItem items = 1;
  map<string, string> metadata = 2;
  CellOutputProcessInfo process_info = 3;
}

message CellExecutionSummary {
  google.protobuf.UInt32Value execution_order = 1;
  google.protobuf.BoolValue success = 2;
  ExecutionSummaryTiming timing = 3;
}

message TextRange {
  uint32 start = 1;
  uint32 end = 2;
}

enum CellKind {
  CELL_KIND_UNSPECIFIED = 0;
  CELL_KIND_MARKUP = 1;
  CELL_KIND_CODE = 2;
}

message Cell {
  CellKind kind = 1;
  string value = 2;
  string language_id = 3;
  map<string, string> metadata = 4;
  TextRange text_range = 5;
  repeated CellOutput outputs = 6;
  CellExecutionSummary execution_summary = 7;
}

enum RunmeIdentity {
  RUNME_IDENTITY_UNSPECIFIED = 0; // aka NONE
  RUNME_IDENTITY_ALL = 1;
  RUNME_IDENTITY_DOCUMENT = 2;
  RUNME_IDENTITY_CELL = 3;
}

message FrontmatterRunme {
  string id = 1;
  string version = 2;
}

message Frontmatter {
  string shell = 1;
  string cwd = 2;
  bool skip_prompts = 3;
  FrontmatterRunme runme = 4;
}

message DeserializeRequestOptions {
  RunmeIdentity identity = 1;
}

message DeserializeRequest {
  bytes source = 1;
  DeserializeRequestOptions options = 2;
}

message DeserializeResponse {
  Notebook notebook = 1;
}

message SerializeRequestOutputOptions {
  bool enabled = 1;
  bool summary = 2;
}

message SerializeRequestOptions {
  SerializeRequestOutputOptions outputs = 1;
}

message SerializeRequest {
  Notebook notebook = 1;
  SerializeRequestOptions options = 2;
}

message SerializeResponse {
  bytes result = 1;
}

service ParserService {
  rpc Deserialize(DeserializeRequest) returns (DeserializeResponse) {}
  rpc Serialize(SerializeRequest) returns (SerializeResponse) {}
}
