Metadata-Version: 2.4
Name: magic-campus-im
Version: 0.1.3
Summary: Magic Campus channel communication SDK
License-Expression: AGPL-3.0-or-later
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: httpx<1,>=0.27
Requires-Dist: protobuf<7,>=6.32.0
Requires-Dist: websocket-client<2,>=1.8.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: build>=1; extra == "dev"
Requires-Dist: twine>=6; extra == "dev"

# magic-campus-im

Channel-only communication SDK. It has no CLI, business clients, group-management
methods or public history/synchronization API. HTTP is retained for IM credentials,
profiles, media upload and channel reactions. It does not depend on either business
distribution.

```python
import os

from magiccampus_im import MagicCampusWSConfig, MagicCampusWSSDK

sdk = MagicCampusWSSDK(
    MagicCampusWSConfig(
        apiurl=os.environ["MAGIC_CAMPUS_BASE_URL"],
        apikey=os.environ["MAGIC_CAMPUS_API_KEY"],
        data_dir="./channel-state",
    ),
    on_recv_new_message=handle_message,
    on_recv_offline_new_message=handle_recovered_message,
)
sdk.login()
sdk.start()
# sdk.send_text("hello", recv_id="...")
# sdk.add_message_reaction(...), sdk.delete_message_reaction(...)
# sdk.logout() closes connection and state storage
```

## Receive recovery

A new installation establishes a current receive baseline, without pulling old
history. `auto_sync_on_connect=False` skips previously missed messages on startup;
set it to true to resume existing checkpoints after a process restart or controlled
handoff. `auto_sync_on_reconnect=True` recovers gaps after a connection interruption.
These switches control internal recovery only; there is no public `sync_once`.

Recovery pages are delivered through `on_recv_offline_new_message`. Live pushes are
buffered during recovery and deduplicated against recovered messages. A live push
ahead of the snapshot cannot advance the checkpoint over a gap. The startup
baseline is established once per connection, so retrying cannot skip later messages.
Each successful callback atomically records its message ID and receive cursor;
a failed callback leaves that message unacknowledged. Acknowledged sequences are
not replayed after the bounded ID cache is evicted.

Errors are reported through `on_error` and recovery retries use the configured
backoff/attempt limit. A failed conversation does not prevent healthy conversations
from recovering or receiving live messages. Server minimum-sequence and confirmed
range boundaries are honored when supplied. Unavailable sequences are reported as
recovery gaps; only server-confirmed progress is skipped. An absent/unconfirmed
range remains pending rather than silently advancing its cursor. Stop wakes pending
requests, and workers/sockets from an old connection cannot commit into a new one.

Recovery uses OpenIM sequence pulls (WebSocket request 1005), which can retrieve
messages even when conversation metadata is absent. A missing conversation in the
newest-sequence snapshot does not prevent recovery of a buffered live push. Only
returned sequences or an explicit server `endSeq` advance progress; `isEnd` alone
does not acknowledge the rest of a requested page.

`channel-progress_<user>.db` stores only checkpoints, initialization state and a
bounded set of message IDs (10,000). The data directory scopes the state, and
each database belongs to one OpenIM user ID. Keep separate data directories for
separate OpenIM deployments. An upgrade from an older endpoint-bound database
preserves its checkpoints and deduplication records. The first successful
recovery resumes from those checkpoints even when `auto_sync_on_connect=False`.
If that recovery fails or the process restarts, the next connection continues
from the saved progress. Callbacks must return successfully to record receipt.

The implementation uses the existing OpenIM protocol. Real server retention and
notification behavior still require staging validation before online migration.

## Development and validation

From the repository root:

```sh
uv sync --frozen --all-packages
uv run --frozen --no-sync pytest -q magic-campus-im/tests
uv build --no-sources --package magic-campus-im --out-dir dist/im --clear
```

Use `uv sync --frozen --package magic-campus-im --group dev` for an environment containing
only this member and development tools. See [workspace instructions](../README.md)
and [build/install checks](../PYPI_RELEASE.md).

## Offline receive-checkpoint handoff

The checkout retains a helper for a controlled migration from an existing legacy
Agent. Stop that Agent first, then run the following from the repository root:

```sh
uv run --frozen --no-sync python magic-campus-im/scripts/prepare_channel_handoff.py \
  --legacy-db /path/to/old/OpenIM_pyws_USER.db \
  --destination /path/to/new/channel-progress_USER.db
```

The helper reads only `conversation_seq` and refuses to replace existing new
state. It copies no message bodies or credentials and does not modify the old
database or upgrade an Agent. Preserve the iAgent replay guard separately and
enable startup recovery for the cutover. Normal new installations need no handoff.
