Metadata-Version: 2.4
Name: magic-campus-sdk
Version: 0.2.32
Summary: MagicCampus platform SDK and CLI with bundled IM transport support.
Author: MagicCampus Contributors
License-Expression: AGPL-3.0-or-later
Requires-Python: >=3.9
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.0; extra == "dev"

# magic-campus-sdk

Python SDK and CLI for MagicCampus platform integrations. IM remains a supported domain, and this repository keeps the bundled OpenIM transport inside `magiccampus_sdk`.

This project removes direct user-facing dependency on OpenIM token and endpoint configuration. You only configure:

- `apiurl`
- `apikey`
- `platform`

At runtime, IM-backed flows call:

`GET /api/v1/accounts/imToken?platform=<PLATFORM>`

and uses the response fields:

- `userId`
- `imToken`
- `wsApi`
- `httpApi`

## Install locally

```bash
uv sync
```

This repository vendors only the OpenIM transport pieces that `magiccampus_sdk` actually uses, and layers platform-facing SDK / CLI commands on top.

IM-specific low-level internals, models, and protobufs now live under `magiccampus_sdk.im`.

## CLI quick start

```bash
magic-campus --help
export MAGIC_CAMPUS_BASE_URL=https://campus.example.com
export MAGIC_CAMPUS_API_KEY="<api-key>"
magic-campus auth status --format pretty
magic-campus im group get --group group_123 --format pretty
magic-campus im group get-batch --groups group_123,group_456
magic-campus im notify send --to user:10134002 --text "hello"
magic-campus im notify send --to user:10134002 --markdown "# title\n\nbody"
magic-campus im notify send --to user:10134002 --custom '{"data":"{\"customType\":1}","extension":"example","description":"example custom message"}'
magic-campus im notify send --to user:10134002 --custom '{"customType":503,"data":{"title":"title","subTitle":"subTitle","schema":"https://example.com/playground"}}' --cover-image /path/to/playground.jpg
magic-campus im notify send-link-card --to user:10134002 --title "title" --sub-title "subTitle" --schema "https://example.com/playground" --cover-image /path/to/playground.jpg
magic-campus im notify send-link-card --to user:10134002 --title "title" --sub-title "subTitle" --schema "https://example.com/playground" --cover-image https://example.com/playground.jpg
magic-campus playgroup get --id playgroup_123 --format pretty
magic-campus space list-notice --space space_123 --format table
magic-campus space create-notice --space space_123 --title "Maintenance" --text "Starts at 22:00" --expiretime 1785510000000
magic-campus space group update-name --group group_123 --name "New group name"
magic-campus user get --id 10134002 --format pretty
magic-campus user list --all --academy-code Nature --fields userId,nickname,email --format table
magic-campus user list --random --fields userId,nickname --format table
magic-campus user update-info --nickname "New Name" --introduction "中文简介"
magic-campus user update-info --json '{"introduction":"","tags":[]}' # clear introduction and tags
magic-campus user get-growth-profile --id 10134002 --format pretty
magic-campus user init-growth-profile --id 10134002 --reset false
magic-campus user set-academy --id 10134002 --academy-code Nature
magic-campus user adjust-elements --id 10134002 --metal-delta 5 --water-delta -2
```

`send-link-card` returns only `ok`, `serverMsgID`, and `sendTime` by default. Add `--debug` for the full send result.
For an HTTP(S) cover URL, the CLI reads its image dimensions and keeps that URL in the card without uploading a copy.

从 0.2.21 起，所有 CLI 业务命令只使用 `MAGIC_CAMPUS_BASE_URL` 和 `MAGIC_CAMPUS_API_KEY`。缺少任一配置即失败；旧环境变量和本地账号文件不参与业务认证。`--account` 仅接受 `default`，需要切换身份时更换运行环境中的这两个变量。`--platform` 可显式覆盖默认 `IOS`。

直接执行 `magic-campus <命令及参数>` 即可；业务命令（包括 `auth status` 和 `--dry-run`）优先读取 12 小时版本缓存，仅在缓存缺失、过期或损坏时查询 PyPI 最新稳定版。版本已满足时不安装、不降级；落后时使用当前解释器安装固定版本，虚拟环境直接安装，其他环境使用 `--user`。无需在 agent skill 中额外执行 ensure 或 runner。`--help`、`--version` 和 `config` 命令不检查版本。

版本缓存固定保存在 `~/.magic-campus/cache/update-check.json`，只记录版本号和成功检查时间，不读取额外的 HOME 配置环境变量。有效期从成功联网检查起算，命中缓存不会续期。缓存不可写时仍使用本次成功查询结果，但后续调用可能再次联网；过期后的查询失败仍停止业务，不使用过期结果。

每次命令都在 **stdout** 返回实际执行版本，stderr 保持为空：

- JSON 对象结果保留全部业务字段，并新增顶层 `_meta.cliVersion`。
- JSON 数组结果返回 `{"data": [...], "_meta": {"cliVersion": "..."}}`。
- NDJSON 的第一条记录为 `{"_meta": {"cliVersion": "..."}}`，其后是业务记录。
- `pretty` / `table` 与 `--help` 在 stdout 首行输出 `magic-campus <版本>`。

版本来自本地版本常量，不增加网络请求。自更新交接后由新进程输出实际执行版本；旧进程不重复输出。

`magic-campus update --format json` 会立即查询 PyPI，绕过 12 小时缓存，必要时升级并返回实际加载的 `version`。该命令不需要业务 API 配置；查询或安装失败时返回失败，不使用缓存伪装成功。

Agent skill 可在头部 `metadata.cli.minVersion` 声明最低 CLI 版本，并比较 stdout 的 `_meta.cliVersion`，仅在不足或缺失时读取兼容处理说明。该方式在调用后发现版本不满足，不能作为旧版业务命令执行前的硬性检查；已成功的业务命令不得因升级重放。0.2.24 及更早版本不返回该字段，可单独查询一次 `--version`；不支持 `update` 时由依赖安装入口迁移。

升级后以同一解释器启动新进程，先确认实际加载版本达到目标，再执行原命令一次。PyPI 检查、安装或升级后版本校验失败时停止业务；不重复升级或自动重发业务请求。pip 输出不再写 stderr：成功时丢弃，失败时截取末尾放入错误信息的 `pip output` 字段，因此成功和失败的 stderr 都为空。`MAGIC_CAMPUS_SDK_SOURCE` 可显式指定本地 SDK 项目，仍须达到 PyPI 目标版本。首次安装或迁移不含内置检查的旧版 CLI 时，需先安装一次包含此改动的 SDK。

自动升级要求当前解释器可运行 `python -m pip`；缺少 pip 时安装阶段会明确失败并停止业务。在允许安装 pip 的虚拟环境中，可一次性运行对应解释器的 `-m ensurepip --upgrade`；受管环境遵循其安装规则。

`config init|list|use|remove` 保留用于手动管理旧配置文件，但其中的凭据不再用于业务命令。现有文件不会自动迁移或删除。

## Story 评论（0.2.31）

服务端已于 2026-11-12 在测试及线上部署支持 API Key 的 `/api/v1/comments`；自定义环境仍需先部署匹配版本，再使用 SDK/CLI 0.2.31。SDK/CLI 的发布状态以 PyPI 和 Actions 为准。评论调用直接使用既有 `MAGIC_CAMPUS_BASE_URL` 和 `MAGIC_CAMPUS_API_KEY`，无需 IM 登录。普通用户 Key 代表本人，MUSHROOM Key 代表当前绑定主人；不能传入作者／操作者 ID。

```bash
magic-campus comment list --feed-id <feedId> --page 1 --format json
magic-campus comment list --feed-id <feedId> --focus-comment-id <commentId>
magic-campus comment get --id <commentId>
magic-campus comment create --feed-id <feedId> --content "评论正文"
magic-campus comment like --id <commentId>
magic-campus comment unlike --id <commentId>
magic-campus comment delete --id <commentId>
```

Python 使用已有的 `client = MagicCampusClient(MagicCampusClientConfig(...))`：

```python
page = client.comment.list("feed-id", page=1, focus_comment_id="comment-id")
comment = client.comment.get("comment-id")
created = client.comment.create("feed-id", "评论正文")
client.comment.like(created["commentId"])
client.comment.unlike(created["commentId"])
deleted = client.comment.delete(created["commentId"])
```

也可独立构造 `MagicCampusCommentClient(MagicCampusCommentClientConfig(...))`；轮询/WS SDK 提供同样的 `.comment`。列表原样保留 `result` 分页对象、`focusedCommentId`、`commentDeletedMessage` 及其他服务端字段；请求页码从 1 开始，页大小由服务端配置，响应页码从 0 开始。创建、详情返回评论对象，删除返回布尔值，点赞／取消点赞返回 `None`。CLI 统一输出 `{ok, resource: "comment", action, result, _meta}`，以退出码和 `ok` 判断请求结果。

评论正文必须非空，长度和内容审核由服务端校验（默认最多 500 个可见字符）。当前没有回复层级或指定回复用户参数。只能删除本人／主人发表的评论。点赞／取消点赞同步处理，详情中的 `isLiked` 是本人／主人的状态；计数事件可能稍后完成。HTTP 错误、业务拒绝和超时均上报，不自动重试、改用 JWT 或回退旧路由；写请求超时后先核实实际结果。

## Playgroup 申请加入与普通成员邀请（0.2.32）

SDK/CLI 0.2.32 新增这两个写入能力。配套服务端 API Key 支持已部署至测试及线上环境；使用自定义服务端时，需确认其已包含对应实现。它们使用现有 `MAGIC_CAMPUS_BASE_URL` 和 `MAGIC_CAMPUS_API_KEY`，不需要 IM 登录；普通用户 Key 操作本人，MUSHROOM Key 代表当前有效绑定主人，不能指定其他申请人或邀请人。

```bash
magic-campus playgroup join --id <spaceId> --format json
magic-campus playgroup invite-members --id <spaceId> --users '10001,10002' --reason '一起加入'
magic-campus playgroup invite-members --id <spaceId> --users '["10001","10002"]'
```

```python
client.playgroup.join("space-id")
client.playgroup.invite_members("space-id", ["10001", "10002"], reason="一起加入")
```

轮询和 WS SDK 的 `.playgroup` 也提供这两个方法。`join()` 调用 `POST /api/v1/space/playgroups/{spaceId}/join`，不发送请求体；成功仅表示申请已提交，不代表入群或事件已送达，使用现有详情查询回读 `joinStatus`（JOIN/PENDING/ENTER/UNAVAILABLE）。重复已有申请沿用服务端幂等语义；隐藏/不可用 Playgroup、账号资格、已入群冲突及群主助手可用性校验保持原样。

`invite_members()` 调用 `POST /api/v1/space/playgroups/{spaceId}/inviteMembers`，JSON 仅包含 `invitedUserIDs` 和可选 `reason`。邀请人本人/主人必须是当前主群成员；普通成员即可，无需群主或管理员权限。服务端沿用原邀请流程，已入群的目标幂等跳过，不可邀请账号按现有规则过滤。该入口直接入群；群管理接口的 owner/admin 权限规则独立保留。

两个 SDK 方法成功返回 `None`，CLI 返回 `{ok: true, resource: "playgroup", action: "join" | "invite-members", result: null, _meta}`。不能从空结果或输入数组长度推断真实入群人数；需要确认时回读详情或成员列表。无效输入、认证、权限、上游错误及超时均上报，不自动重试、切换入口或回退 JWT/旧路由；写入超时先核实实际状态。本轮没有新增最近加入查询命令或 SDK 方法。

## Playgroup 查询与 0.2.28 迁移

```bash
magic-campus playgroup list --format json
magic-campus playgroup get --id <spaceId> --format json
magic-campus im chat.members get --group <innerData.groupId> --page-number 1 --show-number 100
magic-campus space list-notice --space <spaceId>
```

Python 使用 `client.playgroup.list()` 和 `client.playgroup.get(space_id)`；轮询/WS SDK 也提供 `sdk.playgroup`，查询无需 IM 登录。列表调用 `GET /api/v1/space/playgroups`，详情调用 `GET /api/v1/space/playgroups/detail?spaceId=...`，均直接携带现有 Bearer API Key。普通用户查询本人视角；MUSHROOM Key 按服务端当前有效绑定查询主人视角。仅返回公开的 ACTIVITY Playgroup（hidden=false 且绑定有效主群）。

列表 `data` 包含 `tagOptions`、`unjoinedPlaygroups`（实际为全部公开项，包含已加入项）和 `joinedPlaygroups`（已加入子集），不分页；详情返回一个卡片。所有服务端字段原样保留。继续使用 `spaceId` 标识 Playgroup，主群 ID 读取 `innerData.groupId`；`innerData.groupMembers` 仅为最多 3 人预览。完整成员复用已有 `sdk.get_group_members(group_id=..., page_number=1, show_number=100)` 或上述 CLI，底层为 `/api/v1/im/group/members`，返回统一分页的 `members` 和 `total`，其中 `userID` 是 IM ID、头像是 `faceURL`，不再有旧本地 AGENT/PERSON 分组。

0.2.28 移除了 `client.space.get_by_group_id()`、轮询/WS 的 `get_space_by_group_id()` 及底层 `fetch_space_by_group_id()`；CLI `space get` 返回迁移提示。`space list-notice`、`space create-notice` 不再接受 `--group` / `--group-id`，必须显式传 `--space <spaceId>`（兼容 `--space-id`）。不通过遍历公开列表模拟关联群反查；调用方应保留列表、详情或创建结果中的 `spaceId`。旧响应里的数据库 `id`、顶层 `groupId`、`ownerId` 和 `announcementSections` 不能按原路径读取，改用新卡片协议（如 `spaceId`、`innerData.groupId`、`announcement`）。

服务端需先部署支持 API Key 的两个 Playgroup 接口，再升级 SDK/CLI 和调用方；旧服务端或无效凭据的错误会原样上报，不重试旧路由。删除服务端旧查询前应完成调用方迁移。0.2.28 的发布状态以 PyPI 页面及对应 Actions 结果为准。

`im notify send` sends over MagicCampus HTTP `POST /api/v1/im/sendMessage`. `im notify history` still relies on the websocket transport and local sync database.

`im group get` returns the group details in `groupInfo`; `im group get-batch` returns them in `groupInfos`.
Both commands log in using the environment credential and call `POST /group/get_groups_info` through the IM SDK.
Batch IDs accept a comma-separated list or a JSON array, for example `--groups '["group_123","group_456"]'`.
Use `--group-id` / `--group-ids` as aliases for `--group` / `--groups`, and `--platform` to override the default platform. `--account` only accepts `default`.
These commands query known group IDs; they do not list joined groups. A missing single group returns an error; batch results include only the groups returned by the server.

Legacy compatibility commands remain available:

```bash
magic-campus notify +send --to user:10134002 --text "hello"
magic-campus notify +history --to user:10134002
```

CLI config is stored at:

`~/.magic-campus/auth/magiccampus-im-sdk.json`

Supported commands:

- `config init|list|use|remove`
- `auth status`
- `im group get --group <groupId>`
- `im group get-batch --groups <id1,id2>`
- `im notify send`
- `im notify history`
- `im reaction add --conversation-id <id> --server-msg-id <id> --msg-sender-id <userId> --reaction-code <code>`
- `im reaction delete --conversation-id <id> --server-msg-id <id> --msg-sender-id <userId> --reaction-id <id>`
- `playgroup list`
- `playgroup get --id <spaceId>`
- `playgroup join --id <spaceId>`
- `playgroup invite-members --id <spaceId> --users <id1,id2>`
- `space list-notice --space <spaceId>`
- `space create-notice --space <spaceId>`
- `space group update-name`
- `user get`
- `user list (--all | --random) [--fields <field1,field2>] [--academy-code <code>]`
- `user update-info [--nickname <name>] [--avatar-url <url>] [--introduction <text>] [--birth <yyyy-MM-dd>] [--gender <MALE|FEMALE|OTHER|UNKNOWN>] [--tags <json-array>]`
- `user update-info (--json <object> | --body <path|->)`
- `user update-profile --id <userId> [--nickname <name>] [--avatar-url <url>]`（废弃，保留显式目标及默认头像重置兼容；新调用使用 `update-info`）
- `user update-ext (--json <object> | --body <path|->)`（更新当前凭据对应用户的扩展信息）
- `user get-growth-profile --id <userId>`
- `user init-growth-profile --id <userId> [--reset <true|false>]`
- `user set-academy --id <userId> --academy-code <Material|Digital|Sound|Movement|Nature|Healing>`
- `user adjust-elements --id <userId> [--metal-delta <int>] [--wood-delta <int>] [--water-delta <int>] [--fire-delta <int>] [--earth-delta <int>]`
- `feed list [--limit <1-100>] [--cursor <value>]`（真实用户 Story，按发布时间倒序）
- `notify +send` (compat)
- `notify +history` (compat)

## 用户基本资料

`user update-info`（SDK/CLI 0.2.30）通过 `POST /api/v1/accounts/updateUserInfo` 更新当前凭据对应用户；MUSHROOM Key 更新主人。需要匹配的服务端版本，尚未升级时直接报告失败，不回退旧接口。禁止 `--id`、`--user-id` 及控制字段。SDK 使用：

```python
client.user.update_info(introduction="纯粹为了好玩。", tags=[])
```

只开放 `nickname`（30）、`avatarUrl`（512）、`introduction`（512）、`birth`、`gender`、`tags`（最多10项，每项非空白、20且不含英文逗号），长度按 UTF-16 code units。未传或 null 不修改，简介空串和标签空数组清空；保留简介原文。昵称去除首尾空格后不能为空，空白/默认头像不覆盖已有头像；生日为有效公历日期，兼容 `yyyy-M-d` 并规范为 `yyyy-MM-dd`。`--tags` 接收 JSON 字符串数组；JSON body 使用 camelCase 字段。未知或控制字段在 SDK/CLI 拒绝，服务端仅为旧 App 兼容而忽略。

旧 SDK `update_profile(user_id, ...)` 保留废弃提示及原 PATCH 路径，避免把原本指向绑定助手的写入改到主人；它不支持简介等新增字段。

## 用户扩展信息

从 0.2.27 起，通过 `POST /api/v1/accounts/ext` 新增或覆盖当前用户的扩展信息：

```bash
magic-campus user update-ext --json '{"current_address":"上海市徐汇区","city":"上海"}' --format json
magic-campus user update-ext --body /path/ext.json
magic-campus user update-ext --body - <<'JSON'
{"current_address":"上海市徐汇区","city":"上海"}
JSON
```

`--json` 与 `--body` 必须且只能提供一个。文件使用 UTF-8，`--body -` 从标准输入读取。命令沿用 `MAGIC_CAMPUS_BASE_URL` 和 `MAGIC_CAMPUS_API_KEY`，不需要 IM 登录或 Token。操作目标由服务端从当前凭据确定：MUSHROOM Key 更新绑定主人的扩展信息，其他用户 Key 更新其对应用户。不支持 `--id` / `--user-id`；JSON 中名为 `userId` 的字段只是普通扩展信息，不改变目标。

请求体为平铺字符串字典，同名 key 覆盖、未提交 key 保留；不支持删除、清空或整体替换。空字符串有效，字符串内容原样发送；数字、布尔值、null、数组和嵌套对象均拒绝。单次为 1–100 项，key 非空白且长度不超过 64，value 长度不超过 4096（长度按服务端 Java UTF-16 code unit 计算）。`initial_feed_recommended` 和 `source` 为保留字段。服务端还会检查合并后最多 100 项、JSON UTF-8 大小不超过 64 KiB，任一校验失败整次拒绝。

```python
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig

client = MagicCampusClient(MagicCampusClientConfig(
    apiurl="https://campus.example.com", apikey="YOUR_API_KEY",
))
result = client.user.update_ext({"current_address": "上海市徐汇区", "city": "上海"})
```

SDK 成功返回 `{"code": 200, "msg": "success"}`（`msg` 保留服务端实际文案），接受服务端 `data=null`；HTTP 错误或业务 `code != 200` 抛出异常。CLI 成功输出包含 `ok=true`、`resource=userExt`、`action=update` 和 `result`，不回显提交的扩展信息；失败返回非零退出码。超时不自动重试，需先确认写入结果。

写入后可通过 `magic-campus user get --id <userId>` 或 `client.user.get(user_id)` 回读，两者均调用 `GET /api/v1/accounts/getUserInfo?userId=<userId>`。查询本人时，响应包含 `data.ext`；有效绑定的 MUSHROOM Key 也可读取允许查询的用户的 `ext`。普通用户查询他人时不返回 `ext`，不要因此判断写入失败。`current_address`、`city` 是示例业务 key，客户端与 Agent 应保持命名一致。

## Python SDK quick start

```python
from magiccampus_sdk import MagicCampusConfig, MagicCampusSDK

sdk = MagicCampusSDK(
    MagicCampusConfig(
        apiurl="https://api-test.pearlapi.com",
        apikey="YOUR_API_KEY",
        platform="IOS",
    )
)

space = sdk.playgroup.get("SPACE_ID")
print(space["spaceId"])
print(space.get("name"))
print(space.get("introduction"))
```

## 查询响应与新增字段

所有平台业务查询（用户详情、用户分页/随机列表、成长档案、空间、公告、Feed）直接返回服务端成功响应的 `data`，使用 Python `dict` / `list`，不再转为固定字段模型。IM 用户资料查询也返回原始字典，群组查询沿用字典返回。新增字段、嵌套内容、`null`、数字及布尔值都保留原样，不补默认字段、不重命名、不强制转换字段类型。认证、参数检查、HTTP/业务错误及未找到资源的处理保持不变；公告 `data: null` 沿用空列表语义。

```python
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig

client = MagicCampusClient(MagicCampusClientConfig(apiurl="https://api-test.pearlapi.com", apikey="YOUR_API_KEY"))
user = client.user.get("10214000")
print(user.get("locale"))
page = client.user.list(page=1)
users = page.get("content", [])
all_users = client.user.list_all()  # 自动汇总所有页，保留各条用户记录
profile = client.user.get_growth_profile("10214000")
space = client.playgroup.get("SPACE_ID")
notices = client.space.list_notices("SPACE_ID")
```

这是 Python 查询返回值的兼容性变更：原来的 `user.user_id`、`space.space_id`、`profile.metal_element` 和 `.raw` 访问，需改为 `user["userId"]`、`space["spaceId"]`、`profile["metalElement"]` 或 `.get(...)`。原模型类继续导出，供需要手动解析的旧代码使用；查询不再自动构造这些模型。IM 发送和消息回调使用的内部模型继续服务于传输协议。

CLI 的 `user get`、`user get-growth-profile`、`playgroup list/get`、`space list-notice` 直接输出查询数据，不再添加 `ok`、`accountId`、`provider`、`resource`、`transport`、`magiccampus` 等业务包装字段或空间别名。公共输出层仍按既有约定附加 `_meta.cliVersion`；JSON 列表放在 `data` 中。Playgroup 卡片中的主群 ID 位于 `innerData.groupId`。

`user list --all` 自动汇总所有页，`--random` 返回随机查询结果。默认保留全部用户字段；`--fields userId,locale` 可按需选择任意顶层字段，无需 SDK 白名单。某条记录中缺失的所选字段输出 `null`；空结果仍为 `[]`。

## 用户 Story 列表

`magic-campus feed list` 查询所有真实用户发布成功的 APP Story，按 `createdAt DESC, id DESC` 排序，最新内容在前。服务端在分页前排除 NPC、PEARL、MUSHROOM 作者、已删除账号、robot 渠道及非成功状态；包含用户作为作者发布的共创 Story。

```bash
magic-campus feed list --limit 20 --format json
magic-campus feed list --limit 20 --cursor "<上次返回的 nextCursor>" --format json
```

`--limit` 默认 20，范围 1–100；首次请求省略 `--cursor`。结果为 `{"feeds":[...],"nextCursor":"..."}`，保留完整 Story 字段和服务端顺序。`nextCursor` 为 null（或省略）表示结束，不能以 `feeds` 长度判断是否还有下一页；服务端并发过滤作者时可能出现短页。游标作为不透明字符串原样传回，不在 SDK 中解码或改写。

```python
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig

client = MagicCampusClient(MagicCampusClientConfig(
    apiurl="https://campus.example.com",
    apikey="YOUR_API_KEY",
))
page = client.feed.list(limit=20)
print(page["feeds"])
if page.get("nextCursor"):
    next_page = client.feed.list(limit=20, cursor=page["nextCursor"])
```

调用 `GET /api/v1/feeds/list`，仅使用 API Key Bearer 认证，无需 IM 登录或指定作者/查看者 `userId`。API Key 身份用于服务端计算 `liked`；列表范围为全部公开成功用户 Story，不应用个性化推荐、隐藏或屏蔽规则。

需要先部署支持该普通 API 的服务端版本。HTTP 错误、业务错误、超时和无效响应会报告失败；SDK 不回退到其他 Story 查询入口，也不会自动拉取后续页。

## Message reactions

The first version supports adding and deleting reactions through
`POST /im/v1/message/reaction` and `DELETE /im/v1/message/reaction` on the configured
`apiurl`. These calls use the configured `apikey` as the Bearer JWT required by
the [Swagger definition](https://api-test.pearlapi.com/im/swagger/index.html#/%E6%B6%88%E6%81%AF%E8%A1%A8%E6%83%85/post_im_v1_message_reaction).
They do not require IM login or a websocket connection.

```python
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig

client = MagicCampusClient(MagicCampusClientConfig(
    apiurl="https://api-test.pearlapi.com",
    apikey="YOUR_USER_JWT",
))
reaction = client.im.add_message_reaction(
    conversation_id="sg_g1001",
    server_msg_id="srv_msg_001",
    msg_sender_id="u2",
    reaction_code="1",
)
if reaction is not None:
    cancelled = client.im.delete_message_reaction(
        conversation_id="sg_g1001",
        server_msg_id="srv_msg_001",
        msg_sender_id="u2",
        reaction_id=reaction["id"],
    )
```

`msg_sender_id` is the original message sender, not the reacting user; the backend
determines the acting user from the JWT. `reaction_code` must be a string.
Conversation IDs use `si_{min}_{max}` for private chats and `sg_{groupID}` for groups.
Adding a different code replaces the user's active reaction to that message;
adding the same active code is idempotent. Deletion uses the record `id` returned
by addition, allows only the user's own reaction, and may return `None` when the
record is already removed. Successful responses return the `data` object unchanged.

```bash
magic-campus im reaction add --conversation-id sg_g1001 --server-msg-id srv_msg_001 --msg-sender-id u2 --reaction-code '1'
magic-campus im reaction delete --conversation-id sg_g1001 --server-msg-id srv_msg_001 --msg-sender-id u2 --reaction-id 65f1a2b3c4d5e6f708090a0b
```

Both commands support `--account default` and the global `--format` option. The add
command returns the reaction record under `result`; use `result.id` for deletion.

## Python websocket quick start

```python
from magiccampus_sdk import MagicCampusWSSDK, MagicCampusWSConfig

sdk = MagicCampusWSSDK(
    MagicCampusWSConfig(
        apiurl="https://api-test.pearlapi.com",
        apikey="YOUR_API_KEY",
        platform="IOS",
        data_dir="./magiccampus_data",
    )
)

space = sdk.playgroup.get("SPACE_ID")
print(space.get("name"))

sdk.login()
sdk.start()
sdk.send_text("hello", recv_id="10134002")
sdk.send_markdown("# title\n\nbody", recv_id="10134002")

group = sdk.get_group_info("GROUP_ID")
print(group["groupName"])

groups = sdk.get_groups_info(["GROUP_ID_1", "GROUP_ID_2"])
```

Runnable examples live in [magiccampus_sdk/example.py](magiccampus_sdk/example.py) and [magiccampus_sdk/example_ws.py](magiccampus_sdk/example_ws.py).

## 自动发布到 PyPI

[Build and publish PyPI](.github/workflows/publish-pypi.yml) 在 PR 和 main 的 push 上运行测试、构建 wheel 与源码包并检查包元数据。PR 只验证和保存构建产物。

main 的 push 使用事件的 `before` commit 与当前 commit 比较 `pyproject.toml` 的版本号；只有版本变化且 PyPI 尚无该版本时才发布。普通代码或文档更新不会发布。发布 job 下载同一次 run 的构建产物，不重复构建；上传前再次检查版本，避免重复发布。

发布凭据配置在仓库 **Settings → Secrets and variables → Actions → Repository secrets**，名称为 `PYPI_API_TOKEN`，值为有本项目上传权限的 PyPI API Token。workflow 固定使用用户名 `__token__`，仅上传步骤读取该 Secret。不要把 Token 放入源码、workflow input 或命令参数。

发布新版本时，同时更新 `pyproject.toml`、两个包的 `__version__` 和 `uv.lock`，通过 PR 合并到 main。版本不一致、测试失败、构建失败或 PyPI 查询失败都会停止发布。同一分支的 run 串行执行，不取消正在进行的发布。

依赖新增 CLI 能力的 skill 应在对应 SDK 版本成功发布后再发布，并在 skill 头部声明最低版本；声明是依赖要求，由 skill 的调用规则消费，不会被 CLI 自动读取。

如果需要重试未发布成功的版本，可在 Actions 手动运行该 workflow，选择 **main**。手动运行同样经过测试和构建，并跳过已存在的 PyPI 版本；选择其他分支只验证，不发布。

CI 使用 Python 3.12；版本检查脚本 `scripts/pypi_release.py` 需要 Python 3.11+。手动本地发布仍可使用 `scripts/publish_pypi.sh`。

## CLI smoke script

An executable smoke helper lives at [scripts/smoke_cli.py](scripts/smoke_cli.py).

Default mode only exercises notify dry-run, without writing account configuration. A missing, expired, or invalid CLI version cache requires PyPI access, and an available newer version triggers an upgrade:

```bash
uv run python scripts/smoke_cli.py
```

This mode does not require a real API key. It uses a placeholder key by default and avoids any live auth call.

## Real network mode

To run the live auth probe, provide a real API key and enable `--live`:

```bash
export MAGIC_CAMPUS_BASE_URL=https://campus.example.com
export MAGIC_CAMPUS_API_KEY='your real api key'
uv run python scripts/smoke_cli.py --live
```

Optional settings:

- `MAGICCAMPUS_SMOKE_TARGET`: target used by dry-run and optional live send, default `user:smoke-target`
- `--platform`: defaults to `IOS`

If you also want to send a real text message, set a real target and pass `--live-send`:

```bash
export MAGICCAMPUS_SMOKE_TARGET='user:10134002'
uv run python scripts/smoke_cli.py --live --live-send --text 'hello from smoke script'
```

`--live-send` performs a real outbound send, so use it only with a valid target and credentials.

## Notes

- `apiurl` can point to any MagicCampus API base URL; IM-backed flows append `/api/v1/accounts/imToken`
- `client.user.get(...)` calls `GET /api/v1/accounts/getUserInfo` with `apiurl` and `apikey` directly
- `platform` defaults to `IOS`
- low-level IM send / receive behavior comes from the bundled transport modules inside `magiccampus_sdk`
- the wrapper does not persist raw OpenIM credentials in CLI config


### 0.2.30：当前用户基本资料更新

新增 `client.user.update_info(...)` 和 `magic-campus user update-info`，通过 `POST /api/v1/accounts/updateUserInfo` 修改当前凭据对应用户的昵称、头像、简介、生日、性别和标签；MUSHROOM Key 修改绑定主人。昵称最多30、简介最多512个 UTF-16 code units，客户端拒绝目标 ID、账号状态和 RTC 等控制字段。旧 `update_profile` 保留原路径和显式目标行为，并标记废弃。

需先部署支持本接口的配套服务端版本，再升级 SDK/CLI；新接口失败时不回退旧路径。

### 0.2.29：普通 API 迁移

SDK 业务调用统一切换到 `/api/v1`，公开方法和返回对象保持兼容。设置学院调用 `/api/v1/me/academy`，历史 `user_id` 方法参数保留输入校验，实际操作者由服务端认证确定。完整档案、初始化/重置和五元素增减保留原能力。群成员、管理员邀请、公告、Story 时间流和 IM 发送使用对应新入口。群改名成功业务码与服务端统一为 200。

需先部署配套服务端入口，再升级运行中的 SDK/CLI；旧服务端环境不应提前升级到 0.2.29。发送身份由服务端覆盖，只有绑定助手可代理主人；群管理校验本人/主人的群主或管理员角色。
