syntax = "proto3";

package golem.worker.v1;

import public "golem/common/empty.proto";
import public "golem/common/plugin_installation_id.proto";
import public "golem/common/revert_worker_response.proto";
import public "golem/worker/complete_parameters.proto";
import public "golem/worker/cursor.proto";
import public "golem/worker/idempotency_key.proto";
import public "golem/worker/invocation_context.proto";
import public "golem/worker/invoke_parameters.proto";
import public "golem/worker/invoke_result.proto";
import public "golem/worker/invoke_result_json.proto";
import public "golem/worker/v1/worker_error.proto";
import public "golem/worker/worker_filter.proto";
import public "golem/worker/worker_metadata.proto";
import public "golem/worker/log_event.proto";
import public "golem/worker/oplog_cursor.proto";
import public "golem/worker/public_oplog.proto";
import public "golem/worker/worker_id.proto";
import public "golem/component/component_id.proto";
import public "golem/worker/update_mode.proto";
import public "golem/worker/target_worker_id.proto";
import public "golem/worker/filesystem.proto";

service WorkerService {
  rpc LaunchNewWorker (LaunchNewWorkerRequest) returns (LaunchNewWorkerResponse);
  rpc CompletePromise (CompletePromiseRequest) returns (CompletePromiseResponse);
  rpc DeleteWorker (DeleteWorkerRequest) returns (DeleteWorkerResponse);
  rpc GetWorkerMetadata (GetWorkerMetadataRequest) returns (GetWorkerMetadataResponse);

  rpc InterruptWorker (InterruptWorkerRequest) returns (InterruptWorkerResponse);
  rpc InvokeAndAwait (InvokeAndAwaitRequest) returns (InvokeAndAwaitResponse);
  rpc InvokeAndAwaitJson (InvokeAndAwaitJsonRequest) returns (InvokeAndAwaitJsonResponse);
  rpc InvokeAndAwaitTyped (InvokeAndAwaitRequest) returns (InvokeAndAwaitTypedResponse);
  rpc Invoke (InvokeRequest) returns (InvokeResponse);
  rpc InvokeJson (InvokeJsonRequest) returns (InvokeResponse);
  rpc ResumeWorker (ResumeWorkerRequest) returns (ResumeWorkerResponse);
  rpc ConnectWorker(ConnectWorkerRequest) returns (stream golem.worker.LogEvent);
  rpc GetWorkersMetadata(GetWorkersMetadataRequest) returns (GetWorkersMetadataResponse);

  rpc UpdateWorker(UpdateWorkerRequest) returns (UpdateWorkerResponse);

  rpc GetOplog(GetOplogRequest) returns (GetOplogResponse);
  rpc SearchOplog(SearchOplogRequest) returns (SearchOplogResponse);

  rpc ListDirectory(ListDirectoryRequest) returns (ListDirectoryResponse);
  rpc GetFileContents(GetFileContentsRequest) returns (stream GetFileContentsResponse);

  rpc ActivatePlugin(ActivatePluginRequest) returns (ActivatePluginResponse);
  rpc DeactivatePlugin(DeactivatePluginRequest) returns (DeactivatePluginResponse);

  rpc ForkWorker(ForkWorkerRequest) returns (ForkWorkerResponse);

  rpc RevertWorker(RevertWorkerRequest) returns (RevertWorkerResponse);
  rpc CancelInvocation(CancelInvocationRequest) returns (CancelInvocationResponse);
}



message LaunchNewWorkerRequest {
  golem.component.ComponentId componentId = 1;
  string name = 2;
  repeated string args = 3;
  map<string, string> env = 4;
}

message LaunchNewWorkerResponse {
  oneof result {
    LaunchNewWorkerSuccessResponse success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}



message LaunchNewWorkerSuccessResponse {
  golem.worker.WorkerId workerId = 1;
  uint64 component_version = 2;
}

message ForkWorkerRequest {
  golem.worker.WorkerId source_worker_id = 2;
  golem.worker.WorkerId target_worker_id = 3;
  uint64 oplog_index_cutoff = 4;
}


message CompletePromiseRequest {
  golem.worker.WorkerId workerId = 1;
  golem.worker.CompleteParameters completeParameters = 2;
}

message CompletePromiseResponse {
  oneof result {
    bool success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message ForkWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    golem.worker.v1.WorkerError error  = 2;
  }
}


message DeleteWorkerRequest {
  golem.worker.WorkerId workerId = 1;
}

message DeleteWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message GetWorkerMetadataRequest {
  golem.worker.WorkerId workerId = 1;
}

message GetWorkerMetadataResponse {
  oneof result {
    golem.worker.WorkerMetadata success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InterruptWorkerRequest {
  golem.worker.WorkerId workerId = 1;
  bool recoverImmediately = 2;
}

message InterruptWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InvokeAndAwaitRequest {
  golem.worker.TargetWorkerId workerId = 1;
  golem.worker.IdempotencyKey idempotencyKey = 2;
  string function = 3;
  golem.worker.InvokeParameters invokeParameters = 4;
  optional golem.worker.InvocationContext context = 6;
}

message InvokeAndAwaitResponse {
  oneof result {
    golem.worker.InvokeResult success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InvokeAndAwaitTypedResponse {
  oneof result {
    golem.worker.InvokeResultTyped success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InvokeAndAwaitJsonRequest {
  golem.worker.TargetWorkerId workerId = 1;
  golem.worker.IdempotencyKey idempotencyKey = 2;
  string function = 3;
  repeated string invokeParameters = 4;
  optional golem.worker.InvocationContext context = 6;
}

message InvokeAndAwaitJsonResponse {
  oneof result {
    string success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InvokeRequest {
  golem.worker.TargetWorkerId workerId = 1;
  golem.worker.IdempotencyKey idempotencyKey = 2;
  string function = 3;
  golem.worker.InvokeParameters invokeParameters = 4;
  optional golem.worker.InvocationContext context = 6;
}

message InvokeResponse {
  oneof result {
    golem.common.Empty success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message InvokeJsonRequest {
  golem.worker.TargetWorkerId workerId = 1;
  golem.worker.IdempotencyKey idempotencyKey = 2;
  string function = 3;
  repeated string invokeParameters = 4;
  optional golem.worker.InvocationContext context = 6;
}

message ResumeWorkerRequest {
  golem.worker.WorkerId workerId = 1;
  optional bool force = 2;
}

message ResumeWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    golem.worker.v1.WorkerError error = 2;
  }
}

message ConnectWorkerRequest {
  golem.worker.WorkerId worker_id = 1;
}

message GetWorkersMetadataRequest {
  golem.component.ComponentId component_id = 1;
  golem.worker.WorkerFilter filter = 2;
  golem.worker.Cursor cursor = 3;
  uint64 count = 4;
  bool precise = 5;
}


message GetWorkersMetadataResponse {
  oneof result {
    GetWorkersMetadataSuccessResponse success = 1;
    WorkerError error = 2;
  }
}

message GetWorkersMetadataSuccessResponse {
  repeated golem.worker.WorkerMetadata workers = 1;
  optional golem.worker.Cursor cursor = 2;
}


message UpdateWorkerRequest {
  golem.worker.WorkerId worker_id = 1;
  uint64 target_version = 2;
  golem.worker.UpdateMode mode = 3;
}

message UpdateWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    WorkerError error = 2;
  }
}

message GetOplogRequest {
  golem.worker.WorkerId worker_id = 1;
  uint64 from_oplog_index = 3;
  optional golem.worker.OplogCursor cursor = 4;
  uint64 count = 5;
}

message GetOplogResponse {
  oneof result {
    GetOplogSuccessResponse success = 1;
    WorkerError error = 2;
  }
}

message GetOplogSuccessResponse {
  repeated golem.worker.OplogEntry entries = 1;
  optional golem.worker.OplogCursor next = 2;
  uint64 first_index_in_chunk = 3;
  uint64 last_index = 5;
}


message SearchOplogRequest {
  golem.worker.WorkerId worker_id = 1;
  string query = 3;
  optional golem.worker.OplogCursor cursor = 5;
  uint64 count = 6;
}

message SearchOplogResponse {
  oneof result {
    SearchOplogSuccessResponse success = 1;
    WorkerError error = 2;
  }
}

message SearchOplogSuccessResponse {
  repeated golem.worker.OplogEntryWithIndex entries = 1;
  optional golem.worker.OplogCursor next = 2;
  uint64 last_index = 5;
}

message ListDirectoryRequest {
  golem.worker.TargetWorkerId worker_id = 1;
  string path = 2;
}

message ListDirectoryResponse {
  oneof result {
    ListDirectorySuccessResponse success = 1;
    WorkerError error = 2;
  }
}

message ListDirectorySuccessResponse {
  repeated golem.worker.FileSystemNode nodes = 1;
}

message GetFileContentsRequest {
  golem.worker.TargetWorkerId worker_id = 1;
  string file_path = 2;
}

message GetFileContentsResponse {
  oneof result {
    bytes success = 1;
    WorkerError error = 2;
  }
}

message ActivatePluginRequest {
  golem.worker.WorkerId worker_id = 1;
  golem.common.PluginInstallationId installation_id = 2;
}

message ActivatePluginResponse {
  oneof result {
    golem.common.Empty success = 1;
    WorkerError error = 2;
  }
}

message DeactivatePluginRequest {
  golem.worker.WorkerId worker_id = 1;
  golem.common.PluginInstallationId installation_id = 2;
}

message DeactivatePluginResponse {
  oneof result {
    golem.common.Empty success = 1;
    WorkerError error = 2;
  }
}


message RevertWorkerRequest {
  golem.worker.WorkerId worker_id = 1;
  golem.common.RevertWorkerTarget target = 2;
}

message RevertWorkerResponse {
  oneof result {
    golem.common.Empty success = 1;
    WorkerError error = 2;
  }
}

message CancelInvocationRequest {
  golem.worker.WorkerId worker_id = 1;
  golem.worker.IdempotencyKey idempotency_key = 2;
}

message CancelInvocationResponse {
  oneof result {
    bool success = 1;
    WorkerError error = 2;
  }
}
