docs(openapi): add AI index and annotate setting endpoints

- add openapi-index.py to generate a compact Markdown index from OpenAPI JSON/YAML
- generate docs/openapi.index.md from the export scripts by default
- annotate SettingController with tags, operations, parameters and public security
- document the AI index workflow in api-integration.md
This commit is contained in:
Timi
2026-11-25 13:18:08 +08:00
parent 3f4a85a3d3
commit dd82259221
7 changed files with 1335 additions and 18 deletions
+52 -3
View File
@@ -1,17 +1,19 @@
#!/usr/bin/env bash
# 导出本地 OpenAPI 文档。
# 导出本地 OpenAPI 文档,并默认生成面向 AI Agent 的接口索引。
#
# 用法:
# scripts/export-openapi.sh [baseUrl] [outFile]
# scripts/export-openapi.sh [baseUrl] [outFile] [indexFile]
#
# 输出文件扩展名为 .json 时导出 JSON,否则导出 YAML。
# 生成的文件已被 .gitignore 忽略,不会提交。
# 索引默认输出到 docs/openapi.index.md;设置 SKIP_INDEX=1 可跳过。
# 生成的 OpenAPI 快照已被 .gitignore 忽略;索引文件可按需提交或单独发送。
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASE_URL="${1:-http://localhost:18092}"
OUT_FILE="${2:-$ROOT_DIR/docs/openapi.local.yaml}"
INDEX_FILE="${3:-$ROOT_DIR/docs/openapi.index.md}"
BASE_URL="${BASE_URL%/}"
mkdir -p "$(dirname "$OUT_FILE")"
@@ -28,3 +30,50 @@ esac
echo "Fetching ${BASE_URL}${API_PATH}"
curl -fsS "${BASE_URL}${API_PATH}" -o "$OUT_FILE"
echo "OpenAPI exported to $OUT_FILE"
if [[ "${SKIP_INDEX:-0}" == "1" ]]; then
exit 0
fi
if [[ "$INDEX_FILE" == "$OUT_FILE" ]]; then
echo "warning: 接口索引不能与 OpenAPI 输出文件相同,跳过接口索引生成。" >&2
exit 0
fi
INDEX_SCRIPT="$ROOT_DIR/scripts/openapi-index.py"
if [[ ! -f "$INDEX_SCRIPT" ]]; then
echo "warning: 未找到 $INDEX_SCRIPT,跳过接口索引生成。" >&2
exit 0
fi
PYTHON="$(command -v python3 2>/dev/null || command -v python 2>/dev/null || true)"
if [[ -z "$PYTHON" ]]; then
echo "warning: 未找到 Python,跳过接口索引生成。安装 Python 或设置 SKIP_INDEX=1 关闭该提示。" >&2
exit 0
fi
INDEX_INPUT="$OUT_FILE"
TEMP_JSON=""
cleanup() {
if [[ -n "$TEMP_JSON" ]]; then
rm -f "$TEMP_JSON"
fi
}
trap cleanup EXIT
if [[ "$OUT_FILE" != *.json ]]; then
TEMP_JSON="$(mktemp)"
if curl -fsS "${BASE_URL}/v3/api-docs" -o "$TEMP_JSON"; then
INDEX_INPUT="$TEMP_JSON"
else
echo "warning: 获取 OpenAPI JSON 失败,跳过接口索引生成。" >&2
INDEX_INPUT=""
fi
fi
if [[ -n "$INDEX_INPUT" ]]; then
mkdir -p "$(dirname "$INDEX_FILE")"
if ! "$PYTHON" "$INDEX_SCRIPT" "$INDEX_INPUT" "$INDEX_FILE"; then
echo "warning: 接口索引生成失败。" >&2
fi
fi