> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Collaboration

`@reductoai/pptx/collab` lets many writers edit one deck at the same time. Each writer keeps its own copy of the deck. Its edits go to a central sequencer as changes, and the other writers' changes come back and merge in. Writers merge by shape, property and character, and concurrent slide adds, removes and moves all land.

## The parts

| Part | Where it runs | Does |
| - | - | - |
| `CollabServer` | Your server (one per deck) | Orders the changes. `receive(base, change)` gives the change a sequence number and rebases it on what came first |
| `PptxReplica` | Each writer | Turns a `PowerPointPresentation`'s edits into changes and writes other writers' changes back |
| `PptxEditorCollab` | Each writer with a [headless editor](https://docs.reducto.ai/office/pptx/ui/headless-editor.md) | The same for a `PptxEditor`, plus presence (each writer's selection) |

Every writer opens the same bytes at the same sequence number. Each writer gets its own id **block** (1 and up), so two writers never make a shape with the same id. A reader that never edits needs no block.

## The loop

```ts theme={null}
import { CollabServer, PptxEditorCollab, type Outgoing } from "@reductoai/pptx/collab";
import { PptxEditor } from "@reductoai/pptx/editor";

const server = new CollabServer();

const outbox: Outgoing[] = [];
const editor = await PptxEditor.open(bytes);
const collab = new PptxEditorCollab(editor, {
  seq: 0,
  block: 1,
  send: (out) => outbox.push(out), // in production: send over your socket
});

await collab.apply({ type: "insertText", text: "Hello" });

// On the server: order each change, then broadcast it.
for (const out of outbox.splice(0)) {
  const { seq, change } = server.receive(out.base, out.change);
  collab.ack(seq); // to the writer that sent it
  // await other.remote(change, seq); // to every other writer
}
```

* `apply(command)` runs a command and sends its change.
* `remote(change, seq)` merges another writer's change and returns the new view.
* `ack(seq)` tells the writer that the server took its change.
* `resend()` sends what is not yet acknowledged again, for example after a reconnect.

## Without an editor

`PptxReplica` does the same for a `PowerPointPresentation`, for example a server-side agent that edits through the facade:

```ts theme={null}
import { PptxReplica, type Outgoing } from "@reductoai/pptx/collab";
import { PowerPointPresentation } from "@reductoai/pptx/officejs";

const outbox: Outgoing[] = [];
const deck = await PowerPointPresentation.fromBytes(bytes);
const replica = new PptxReplica(deck.session, { seq: 0, block: 2, send: (out) => outbox.push(out) });

deck.slides.getItemAt(0).shapes.addTextBox("Added by the agent", { left: 40, top: 40, width: 300, height: 40 });
replica.commit(); // sends the edits since the last commit
```

> [!NOTE]
>
> The server orders and rebases changes; it does not open the deck.
