syntax = "proto3";

package runme.parser.v1;

import "google/protobuf/wrappers.proto";
import "runme/parser/v1/docresult.proto";

option go_package = "github.com/runmedev/runme/v3/api/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;
  CELL_KIND_DOC_RESULTS = 3; // todo(sebastian): is this needed?
}

enum CellRole {
  CELL_ROLE_UNSPECIFIED = 0;
  CELL_ROLE_USER = 1;
  CELL_ROLE_ASSISTANT = 2;
}

message Cell {
  // CellKind is the type of cell, e.g. markup or code.
  CellKind kind = 1;

  // Value is the contents of the cell.
  string value = 2;

  // LanguageID is the language of the cell.
  string language_id = 3;

  // Metadata is additional metadata about the cell in k/v format.
  map<string, string> metadata = 4;

  // TextRange is the range of the cell covering in the markdown source.
  TextRange text_range = 5;

  // Outputs are the outputs of the cell, e.g. stdout, stderr, or other media types.
  repeated CellOutput outputs = 6;

  // ExecutionSummary is the summary of the cell execution, e.g. success/failure and execution time.
  CellExecutionSummary execution_summary = 7;

  /* Agent-specific fields (ported from formerly agent.v1.Block) */

  // RefID is the agent's original unique identifier.
  string ref_id = 100;

  // Role is the role of the cell, e.g. user or assistant.
  CellRole role = 101;

  // CallID is the unique identifier of the cell call.
  string call_id = 102;

  // DocResults are doc results for a cell returned by, e.g., file search
  repeated DocResult doc_results = 103;
}

// RunmeIdentity controls if unique identifiers are inserted if not present.
enum RunmeIdentity {
  RUNME_IDENTITY_UNSPECIFIED = 0; // aka NONE
  RUNME_IDENTITY_ALL = 1;
  RUNME_IDENTITY_DOCUMENT = 2;
  RUNME_IDENTITY_CELL = 3;
}

message RunmeSessionDocument {
  string relative_path = 1;
}

message RunmeSession {
  string id = 1;
  RunmeSessionDocument document = 2;
}

message FrontmatterRunme {
  string id = 1;
  string version = 2;
  RunmeSession session = 3;
}

message Frontmatter {
  string shell = 1;
  string cwd = 2;
  bool skip_prompts = 3;
  FrontmatterRunme runme = 4;
  // Deprecated category in favor of Tag
  string category = 5;
  string terminal_rows = 6;
  string tag = 7;
}

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;
  RunmeSession session = 2;
}

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) {}
}
