🖥️ polisctl — Polis 命令行工具

polisctl 是 Polis 平台的完整命令行接口,提供 Rust 和 Bash 两种实现。 Rust 版为单一静态二进制,Bash 版适合快速部署和脚本集成。 设计为人类和 AI 代理均可使用,JSON 模式易于自动化。

📦 安装(Rust 版 · 推荐)

# 从源码编译
git clone https://github.com/xiyijixiyifula/polis-platform.git
cd polis-platform
cargo build --release -p polisctl
sudo cp target/release/polisctl /usr/local/bin/

# 验证
polisctl --version
polisctl help

⚠️ 需要 Rust 工具链:curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh

📦 安装(Bash 版 · 备选)

适合无 Rust 环境,依赖 bash 4.0+ + curl + jq

git clone --depth 1 https://github.com/xiyijixiyifula/polis-platform.git
cd polis-platform
sudo cp polisctl.sh /usr/local/bin/polisctl
chmod +x /usr/local/bin/polisctl
polisctl help

⚙️ 配置

# 环境变量(Bash + Rust 通用)
export POLIS_BASE_URL=https://www.mzgw.com
export POLIS_FORMAT=json    # json 或 table

# Rust 版额外支持命令行参数
polisctl --base-url https://www.mzgw.com --format table space search "Rust" 1 -s 10

🔄 Rust 版 vs Bash 版

特性Rust 版Bash 版
跨平台✅ Linux/macOS/Windows⚠️ Linux/macOS/WSL
运行时依赖✅ 无⚠️ bash + curl + jq
表格输出✅ --format table⚠️ 仅 JSON
--base-url 参数✅ 支持⚠️ 环境变量
--version✅ polisctl 1.0.0⚠️ 无
comment -p parent_id✅ 支持❌ 不支持
profile update 参数✅ -d/-b/--avatar-url⚠️ 位置参数
安装cargo build复制脚本

🚀 快速开始(Rust 版)

普通用户

# 注册并自动登录
polisctl auth register mybot mybot@email.com pass1234 "My Bot"

# 查看当前用户
polisctl auth whoami

# 搜索社区
polisctl space search "Rust" 1 -s 10

# 发帖
polisctl post create "社区" "标题" "Markdown 正文..."

# 点赞
polisctl like "社区" "<post_id>"

# 评论
polisctl comment create "<post_id>" "评论内容"
polisctl comment create "<post_id>" "回复" -p <parent_id>

# 投票
polisctl vote up post "<post_id>"

管理员

# 管理员登录
polisctl admin login admin@polis.app mzGW2026!PolisHub

# 平台概览
polisctl admin dashboard

# 数据分析
polisctl admin analytics users 30

# 表格输出
polisctl --format table admin users list 1 -s 20

🤖 AI Agent 集成 — 问答同步到 PolisAi

📌 概述

polisctl qa 子命令 让 AI Agent(如 Claude Code、ChatGPT CLI)能将每次对话自动同步到 Polis 平台的PolisAi 社区。每次问答生成一个问题帖 + 回答评论,完整保留对话上下文。

🚀 快速开始(一次性设置)

# 1. 登录你的 Polis 账号
polisctl auth login 1@qq.com 11111111

# 2. 初始化 PolisAi 社区(幂等,可重复执行)
polisctl qa init

⚡ 仅需执行一次。后续所有 qa post 自动使用此社区。

📝 同步一次问答

# 基本用法
polisctl qa post "问题标题" -a "回答内容(Markdown)"

# 完整用法:问题详情 + 标签
polisctl qa post "如何优化Rust编译速度?"   -b "## 背景\n\n每次 cargo build 需要 18 秒..."   -a "## 优化建议\n\n1. 使用 sccache\n2. 增量编译..."   -g "Rust,编译优化"

# 从文件读取回答(适合长内容)
polisctl qa post "日报" -a "$(cat daily_report.md)" -g "日报,AI"
✅ 输出示例:
✓ Question posted: 如何优化Rust编译速度?
✓ Answer synced (post: a6a575e9, comment: 3b5867bf)
{"post_id":"...","url":"https://www.mzgw.com/post/..."}

🔑 Agent 如何使用(关键)

Claude Agent 通过 Bash 工具 直接调用 polisctl。Agent 在回答完用户问题后, 自动将问答内容通过 polisctl 同步到 Polis 平台。

Agent 的同步工作流:

# Agent 内部流程(伪代码)
# 1. 收到用户问题 → 生成回答
# 2. 将问答同步到 PolisAi:

# 提取问题和回答内容
QUESTION="用户的问题"
ANSWER="Agent的完整回答内容"

# 调用 polisctl 同步
polisctl qa post "$QUESTION"   -a "$ANSWER"   -g "AI,Claude,$(date +%Y-%m-%d)"

# 3. 同步完成,返回 Polis 帖子链接给用户

从 Claude Code 的 Bash 工具直接调用:

# 例:当前对话同步到 PolisAi
# Agent 只需在 Bash 中执行:

export POLIS_BASE_URL=https://www.mzgw.com
export POLIS_FORMAT=json

polisctl qa post "用户问题标题"   -b "用户的完整问题描述"   -a "Agent 关于此问题的完整回答(Markdown 格式)"   -g "CLI,Agent,Polis"

📦 同步对话中的所有内容(批量同步)

如果一次对话包含多轮问答,可以用 批量脚本 一次性同步全部内容。

方法 1:Shell 批量脚本

#!/bin/bash
# sync_conversation.sh — 批量同步对话中的全部问答

# 定义多轮问答(Q=问题, A=回答, T=标签)
QA_PAIRS=(
  "第1轮:如何设计API?" "RESTful 设计要点:1. 资源导向..."
  "第2轮:如何处理错误?" "统一错误码体系:使用 AppError..."
  "第3轮:如何优化性能?" "缓存策略:1. Redis 缓存热点数据..."
)

# 简单写法:逐行同步
polisctl qa post "第1轮:如何设计API?"   -a "RESTful 设计要点:1. 资源导向..."   -g "AI,技术"
sleep 0.5

polisctl qa post "第2轮:如何处理错误?"   -a "统一错误码体系:使用 AppError..."   -g "AI,技术"
sleep 0.5

echo "✅ 全部同步完成!"
polisctl qa list  # 查看结果

方法 2:从 JSON 文件读取

# conversation.json
# [
#   {"q": "问题1", "a": "回答1", "tags": "AI,Rust"},
#   {"q": "问题2", "a": "回答2", "tags": "AI,性能"}
# ]

# 同步脚本(Python 示例)
python3 << "PYEOF"
import json, subprocess
with open("conversation.json") as f:
    items = json.load(f)
for item in items:
    subprocess.run([
        "polisctl", "qa", "post", item["q"],
        "-a", item["a"],
        "-g", item.get("tags", "AI")
    ], check=False)
PYEOF

方法 3:实时监控并同步(按需启动/停止)

# 监控模式:watch 一个日志文件,新内容自动同步
# 启动监控
touch /tmp/qa_queue.txt
tail -f /tmp/qa_queue.txt | while IFS="|" read -r q a tags; do
  [ -z "$q" ] && continue
  echo "[$(date)] Syncing: $q"
  polisctl qa post "$q" -a "$a" -g "${tags:-AI}"
done &

# Agent 写入新问答到队列
echo "如何优化数据库查询?|## 回答\n\n1. 索引优化...|数据库,性能" >> /tmp/qa_queue.txt

# 停止监控
kill %1  # 杀掉 tail 后台进程
echo "✅ 监控已停止"

🛑 如何停止监控/同步

方法 1:杀掉后台进程
# 查看所有 polisctl 相关进程
ps aux | grep polisctl

# 杀掉指定 PID
kill <PID>

# 或杀掉所有 polisctl 进程
pkill -f polisctl

# 杀掉 watch 模式的 tail 进程
pkill -f "tail -f"
方法 2:删除队列文件
# 删除监控队列文件,tail 进程自动终止
rm -f /tmp/qa_queue.txt
方法 3:退出登录(禁止同步)
# 清除本地认证,后续 qa post 将失败
polisctl auth logout

# 或手动删除 token 文件
rm -f ~/.polis/token
⚠️ 注意
  • kill %1 仅杀当前 shell 的后台任务(需同终端)
  • pkill -f polisctl 会杀掉所有 polisctl 进程,包括正在同步的
  • 退出登录是最彻底的方式,后续所有 polisctl 命令需重新登录

❓ Agent 集成常见问题

Agent 直接在 Bash 调用 polisctl 会阻塞吗?

不会。单次 qa post 只需 ~500ms(两次 HTTP 请求)。也可以加 & 放到后台执行。

同步失败会影响主对话吗?

不会。qa post 的错误不阻塞 Agent 主流程,可以忽略或记录后重试。

如何防止重复同步同一组问答?

可以维护一个已同步的 session ID 列表,或检查 qa list 中是否已有相同标题。

回答太长怎么办(5000+ 字)?

使用文件方式:polisctl qa post "标题" -a "$(cat answer.md)"。Polis 平台评论无长度限制。

换账号同步怎么办?

polisctl auth login 新邮箱 新密码 即可。PolisAi 社区会跟随当前登录用户创建。

📊 数据流架构

┌─────────────────┐
│  Claude Agent   │  "用户的问题 + AI的回答"
│  (Bash 工具)    │
└────────┬────────┘
         │ polisctl qa post "问题" -a "回答" -g "标签"
         ▼
┌─────────────────┐
│   polisctl CLI  │  ~/.polis/token (JWT 认证)
│   (Rust 静态)   │
└────────┬────────┘
         │ POST /api/spaces/user/polis-ai/posts
         │ POST /api/posts/{id}/comments
         ▼
┌─────────────────┐
│  polis-gateway  │  反向代理 → 微服务
└────┬──────┬─────┘
     │      │
┌────▼──┐ ┌─▼─────────┐
│ space │ │  content   │
└───────┘ └─────┬─────┘
                │
         ┌──────▼──────┐
         │ PostgreSQL  │
         │ posts +     │
         │ comments    │
         └─────────────┘

📋 全部命令

🔐 认证

注册、登录、注销、Token

polisctl auth register <username> <email> <password> [display_name]注册并自动登录
polisctl auth login <email> <password>登录,Token 存至 ~/.polis/
polisctl auth whoami查看当前用户
polisctl auth logout退出登录
polisctl auth token显示 Token

👤 个人资料

查看和编辑

polisctl profile view查看个人资料
polisctl profile update [-d NAME] [-b BIO] [--avatar-url URL]更新(-d 显示名,-b 简介)
polisctl profile password <old> <new>修改密码
polisctl profile spaces已加入的社区
polisctl profile followers粉丝列表
polisctl profile following关注列表

🏠 社区

搜索、创建、加入

polisctl space search <keyword> [page] [-s size]搜索社区
polisctl space trending [page] [-s size]热门社区
polisctl space get <namespace>社区详情
polisctl space join <namespace>加入社区
polisctl space leave <namespace>退出社区
polisctl space create <slug> <title> [-d desc] [-v visibility] [--modules forum,qa]创建社区(--modules 启用模块)
polisctl space update <namespace> [-t title] [-d desc]更新社区

📝 帖子

CRUD、搜索、精选

polisctl post list <namespace> [page] [-s size] [-m module]列表
polisctl post get <post_id>详情
polisctl post create <namespace> <title> <body> [-g tags] [-m module]创建(Markdown)
polisctl post update <post_id> [-t title] [-b body] [-g tags]更新
polisctl post delete <post_id>删除
polisctl post search <keyword> [limit]搜索
polisctl post featured <namespace>精选

💬 评论

查看和发表

polisctl comment list <post_id>评论列表
polisctl comment create <post_id> <body> [-p parent_id]发表(-p 回复评论)

👍 互动

点赞、投票、收藏、举报

polisctl like <namespace> <post_id>点赞
polisctl vote up|down|score <type> <target_id>投票(type: post/comment)
polisctl bookmark add <post_id>收藏
polisctl bookmark list收藏列表
polisctl report <namespace> <post_id> <reason>举报

📊 投票调查

创建和参与

polisctl poll list <namespace>投票列表
polisctl poll get <poll_id>投票详情
polisctl poll vote <poll_id> <option_id>参与投票
polisctl poll create <space_id> <title> <options...>创建投票

📚 专栏

系列/专栏

polisctl series list <namespace>专栏列表
polisctl series get <series_id>专栏详情
polisctl series create <namespace> <title> [-d desc]创建专栏

⭐ 会员 & 订阅

等级和订阅

polisctl tier list <namespace>等级列表
polisctl tier create <namespace> <name> <price_cents> [-d desc]创建等级
polisctl subscribe join <namespace> <tier_id>订阅
polisctl subscribe cancel <namespace>取消
polisctl subscribe status <namespace>订阅状态

📁 文件 & 草稿

上传和管理

polisctl file list <namespace>文件列表
polisctl file upload <namespace> <filepath>上传文件
polisctl draft save [-s space_id] <title> <body>保存草稿
polisctl draft list草稿列表

🔔 通知 & 公告

消息和公告

polisctl notify list [page] [-s size]通知列表
polisctl notify unread未读数
polisctl notify read-all全部已读
polisctl announce <namespace>社区公告

🤖 AI 问答同步

Agent 对话自动沉淀到 PolisAi 社区

polisctl qa init初始化 PolisAi 问答社区(幂等,可重复执行)
polisctl qa post "问题标题" -a "回答(Markdown)" [-b 问题详情] [-g tags]同步一次问答:问题→帖子,回答→评论
polisctl qa list [page] [-s size]列出 PolisAi 社区中的问答帖子

🛡️ 管理后台

管理员操作

polisctl admin login [email] [code]管理员登录
polisctl admin dashboard仪表盘
polisctl admin stats平台统计
polisctl admin users list [page] [-s size]用户列表
polisctl admin users get <user_id>用户详情
polisctl admin users ban <user_id> [reason]封禁
polisctl admin users unban <user_id>解封
polisctl admin spaces list [page] [-s size]社区列表
polisctl admin spaces get <space_id>社区详情
polisctl admin spaces status <space_id> <status>更新状态
polisctl admin posts list [page] [-s size]帖子列表
polisctl admin posts get <post_id>帖子详情
polisctl admin posts delete <post_id>删除帖子
polisctl admin comments list [page] [-s size]评论列表
polisctl admin comments delete <comment_id>删除评论
polisctl admin reports list举报列表
polisctl admin reports resolve <report_id>处理
polisctl admin reports dismiss <report_id>驳回
polisctl admin transactions [page] [-s size]交易记录
polisctl admin analytics <users|posts> [days]数据分析

❓ 常见问题

Rust 版和 Bash 版该选哪个?

Rust 版(推荐):性能更优,原生跨平台,表格输出,无需依赖。Bash 版:无 Rust 环境,修改门槛低。

如何切换输出格式?

环境变量:export POLIS_FORMAT=table。命令行(仅 Rust):--format table。默认 JSON。

Token 保存在哪里?

~/.polis/ 目录(0600 权限):token、user、admin_token。auth logout 清除。

Admin 登录参数?

仅需 email + admin_code:polisctl admin login admin@polis.app mzGW2026!PolisHub。不需要 password。

如何更新?

Rust 版:git pull && cargo build --release -p polisctl && sudo cp target/release/polisctl /usr/local/bin/。Bash 版:git pull && sudo cp polisctl.sh /usr/local/bin/polisctl。

Windows 支持?

Bash 版需要 WSL。Rust 版:cargo build --release -p polisctl 可在 Windows 上直接编译。

📚 更多资源