# `Spectre.Router.SemanticCache`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/router/semantic_cache.ex#L1)

Adapter boundary for semantic cache lookups.

Configure one of:

  * `semantic_lookup: &MyCache.lookup/2`
  * `semantic_cache: MyCache`
  * `semantic_cache: {MyCache, :lookup}`

The adapter should return `{:ok, map}` for accepted route-like results or
`{:error, reason}` for misses.

# `clear`
*optional* 

```elixir
@callback clear(
  module(),
  keyword()
) :: :ok | {:ok, term()} | {:error, term()}
```

# `delete`
*optional* 

```elixir
@callback delete(module(), String.t(), keyword()) ::
  :ok | {:ok, term()} | {:error, term()}
```

# `examples`
*optional* 

```elixir
@callback examples(
  module(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}
```

# `get_example`
*optional* 

```elixir
@callback get_example(module(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}
```

# `load_snapshot`
*optional* 

```elixir
@callback load_snapshot(module(), term(), keyword()) ::
  :ok | {:ok, term()} | {:error, term()}
```

# `lookup`

```elixir
@callback lookup(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}
```

# `put`
*optional* 

```elixir
@callback put(String.t(), map(), keyword()) :: :ok | {:ok, term()} | {:error, term()}
```

# `relabel`
*optional* 

```elixir
@callback relabel(module(), String.t(), atom(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

# `snapshot`
*optional* 

```elixir
@callback snapshot(
  module(),
  keyword()
) :: {:ok, term()} | {:error, term()}
```

# `update_example`
*optional* 

```elixir
@callback update_example(module(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

# `verify`
*optional* 

```elixir
@callback verify(module(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}
```

# `clear`

```elixir
@spec clear(
  module(),
  keyword()
) :: :ok | {:error, term()}
```

Clears semantic-cache state for an agent.

Spectre's built-in learned cache is always cleared. When a custom
`semantic_cache:` module is configured, that adapter must also implement
`clear/2`.

# `delete`

```elixir
@spec delete(module(), String.t(), keyword()) ::
  :ok | {:ok, term()} | {:error, term()}
```

Deletes an online learned example.

# `examples`

```elixir
@spec examples(
  module(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}
```

Lists semantic-cache examples for an agent.

# `get_example`

```elixir
@spec get_example(module(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}
```

Fetches one semantic-cache example by id.

# `learn_eligibility`

```elixir
@spec learn_eligibility(module(), Spectre.Route.t() | map() | nil) ::
  :ok | {:skip, atom()}
```

Explains whether a resolved route should be learned into the semantic cache.

Returns `:ok` when learning is safe, or `{:skip, reason}` with one of
`:missing_route`, `:route_not_accepted`, `:semantic_cache_route` (the route
was served from the cache itself), `:route_not_found`,
`:route_not_learnable` (the matched rule is not `learn: true`), or
`:protected_action` (the route stages a policy-protected action — learning
it would let the cache bypass future consent classification).

    case SemanticCache.learn_eligibility(MyApp.Agent, result.route) do
      :ok -> SemanticCache.put(text, result.route, opts)
      {:skip, _reason} -> :ok
    end

# `learnable?`

```elixir
@spec learnable?(module(), Spectre.Route.t() | map() | nil) :: boolean()
```

Convenience boolean form of `learn_eligibility/2`.

# `load_snapshot`

```elixir
@spec load_snapshot(module(), term(), keyword()) ::
  :ok | {:ok, term()} | {:error, term()}
```

Loads online learned examples and their stored embeddings from a snapshot.

# `lookup`

```elixir
@spec lookup(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}
```

Looks up a message in the configured semantic cache.

# `put`

```elixir
@spec put(String.t(), map(), keyword()) :: :ok | {:ok, term()} | {:error, term()}
```

Stores a semantic-cache example.

# `relabel`

```elixir
@spec relabel(module(), String.t(), atom(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Relabels an online learned example and marks it verified.

# `snapshot`

```elixir
@spec snapshot(
  module(),
  keyword()
) :: {:ok, term()} | {:error, term()}
```

Snapshots semantic-cache examples and their stored embeddings.

# `update_example`

```elixir
@spec update_example(module(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Edits an online learned example in place.

Supported attrs: `:text` (re-embedded through the configured embedding
adapter), `:label` (must name a cacheable route), and `:verified`.

    {:ok, updated} =
      Spectre.Router.SemanticCache.update_example(MyApp.Agent, id, %{label: :PRICING})

# `verify`

```elixir
@spec verify(module(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}
```

Marks an online learned example as verified.

---

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