> ## Documentation Index
> Fetch the complete documentation index at: https://botmesh.afaya.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Coordinator

> Interface exported by @botmesh/core.

Defined in: [coordination/types.ts:164](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L164)

Cross-replica coordination backend. One instance per replica (per `Bot`); the bot
calls [start](#start) when it starts and [stop](#stop) when it stops.

## Properties

### replicaId

```ts theme={null}
readonly replicaId: string;
```

Defined in: [coordination/types.ts:166](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L166)

This replica's id; unique among the replicas sharing the backend.

## Methods

### start()

```ts theme={null}
start(): Promise<void>;
```

Defined in: [coordination/types.ts:168](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L168)

Begin heartbeating/renewing (idempotent).

#### Returns

`Promise`\<`void`>

***

### stop()

```ts theme={null}
stop(): Promise<void>;
```

Defined in: [coordination/types.ts:170](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L170)

Release every turn and leadership this replica holds and stop renewing (idempotent).

#### Returns

`Promise`\<`void`>

***

### reserve()

```ts theme={null}
reserve(key): Promise<Ticket>;
```

Defined in: [coordination/types.ts:172](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L172)

Reserve a place in `key`'s cross-replica FIFO. The bot calls it in ingress order.

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<[`Ticket`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Ticket)>

***

### acquireTurn()

```ts theme={null}
acquireTurn(ticket, opts?): Promise<ChatTurn>;
```

Defined in: [coordination/types.ts:179](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L179)

Wait until it is `ticket`'s turn (tickets of replicas that died are skipped), then
hold the key. Rejects with [LeaseLostError](https://botmesh.afaya.com.co/api/core/classes/LeaseLostError.md) (`'superseded'`) when the ticket was
skipped because this replica was presumed dead, and with
[CoordinationUnavailableError](https://botmesh.afaya.com.co/api/core/classes/CoordinationUnavailableError.md) on timeout or backend failure.

#### Parameters

| Parameter | Type |
| - | - |
| `ticket` | [`Ticket`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Ticket) |
| `opts?` | [`AcquireTurnOptions`](https://botmesh.afaya.com.co/api/core/spi/interfaces/AcquireTurnOptions) |

#### Returns

`Promise`\<[`ChatTurn`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ChatTurn)>

***

### lead()

```ts theme={null}
lead(name, opts?): Promise<Leadership>;
```

Defined in: [coordination/types.ts:181](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L181)

Campaign for `name` until elected (or `opts.signal` aborts, rejecting).

#### Parameters

| Parameter | Type |
| - | - |
| `name` | `string` |
| `opts?` | \{ `signal?`: `AbortSignal`; } |
| `opts.signal?` | `AbortSignal` |

#### Returns

`Promise`\<[`Leadership`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Leadership)>

***

### tryLead()

```ts theme={null}
tryLead(name): Promise<Leadership | null>;
```

Defined in: [coordination/types.ts:183](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L183)

One election attempt: the leadership, or `null` when another replica leads.

#### Parameters

| Parameter | Type |
| - | - |
| `name` | `string` |

#### Returns

`Promise`\<[`Leadership`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Leadership) | `null`>

***

### on()

```ts theme={null}
on<E>(event, listener): () => void;
```

Defined in: [coordination/types.ts:185](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/types.ts#L185)

Subscribe to coordinator events; returns an unsubscribe.

#### Type Parameters

| Type Parameter |
| - |
|  `E` *extends* keyof [`CoordinatorEvents`](https://botmesh.afaya.com.co/api/core/interfaces/CoordinatorEvents) |

#### Parameters

| Parameter | Type |
| - | - |
| `event` | `E` |
| `listener` | (`payload`) => `void` |

#### Returns

() => `void`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.