# `Gemini.Types.Operation`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L1)

Type definitions for long-running operations.

Long-running operations are used for tasks that may take significant time to complete,
such as video generation, file imports, model tuning, and batch processing.

## Operation Lifecycle

1. **Initiated** - Operation is created, `done: false`
2. **Running** - Operation is processing, `done: false`
3. **Completed** - Operation finished successfully, `done: true`, `response` populated
4. **Failed** - Operation failed, `done: true`, `error` populated

## Polling Pattern

    {:ok, operation} = some_long_running_call()

    # Poll until complete
    {:ok, completed} = Gemini.APIs.Operations.wait(operation.name,
      poll_interval: 5000,
      timeout: 300_000
    )

    case completed do
      %{done: true, error: nil, response: response} ->
        IO.puts("Success: #{inspect(response)}")
      %{done: true, error: error} ->
        IO.puts("Failed: #{error.message}")
    end

## Example

    # Video generation returns an operation
    {:ok, op} = Gemini.generate_video("A cat playing piano")

    # Wait for completion
    {:ok, completed} = Gemini.APIs.Operations.wait(op.name)

    # Get the result
    video_uri = completed.response["generatedVideos"]

# `operation_error`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L49)

```elixir
@type operation_error() :: %{
  optional(:code) =&gt; integer(),
  optional(:message) =&gt; String.t(),
  optional(:details) =&gt; [map()]
}
```

Operation error details.

# `t`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L56)

```elixir
@type t() :: %Gemini.Types.Operation{
  done: boolean(),
  error: operation_error() | nil,
  metadata: map() | nil,
  name: String.t() | nil,
  response: map() | nil
}
```

Represents a long-running operation.

## Fields

- `name` - Server-assigned unique identifier (e.g., "operations/abc123")
- `metadata` - Service-specific metadata about the operation progress
- `done` - Whether the operation is complete (true = finished, false = in progress)
- `error` - Error result if the operation failed (mutually exclusive with response)
- `response` - Success result if the operation completed (mutually exclusive with error)

# `complete?`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L107)

```elixir
@spec complete?(t()) :: boolean()
```

Checks if the operation is complete (successfully or failed).

# `failed?`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L121)

```elixir
@spec failed?(t()) :: boolean()
```

Checks if the operation failed.

# `from_api_response`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L93)

```elixir
@spec from_api_response(map()) :: t()
```

Creates an Operation from API response.

## Parameters

- `response` - Map from API response with string keys

## Examples

    response = %{
      "name" => "operations/abc123",
      "done" => false,
      "metadata" => %{"@type" => "...", "progress" => 50}
    }
    op = Gemini.Types.Operation.from_api_response(response)

# `get_id`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L156)

```elixir
@spec get_id(t()) :: String.t() | nil
```

Extracts the operation ID from the full name.

## Examples

    op = %Operation{name: "operations/abc123"}
    Operation.get_id(op)
    # => "abc123"

# `get_progress`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L137)

```elixir
@spec get_progress(t()) :: float() | nil
```

Gets the progress percentage from metadata, if available.

Returns nil if progress information is not available in metadata.

# `running?`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L128)

```elixir
@spec running?(t()) :: boolean()
```

Checks if the operation is still running.

# `succeeded?`
[🔗](https://github.com/nshkrdotcom/gemini_ex/blob/v0.11.0/lib/gemini/types/operation.ex#L114)

```elixir
@spec succeeded?(t()) :: boolean()
```

Checks if the operation completed successfully.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
