sidebar-pptx-generator/scripts/get_slide_info.sh
4.2 KB| 1 | |
| 2 | # |
| 3 | # Inspect a Tencent Docs PPT and return compact JSON for LLM routing. |
| 4 | # |
| 5 | # Usage: |
| 6 | # bash scripts/get_slide_info.sh <file_url_or_id> [--file-id] |
| 7 | # |
| 8 | # Output: |
| 9 | # {"action":"write_design_md"|"proceed_next","reason":"...",...} |
| 10 | # |
| 11 | |
| 12 | set -euo pipefail |
| 13 | |
| 14 | if [[ $# -lt 1 ]]; then |
| 15 | echo "ERROR:missing_argument" |
| 16 | exit 1 |
| 17 | fi |
| 18 | |
| 19 | INPUT="$1" |
| 20 | FILE_KEY="file_url" |
| 21 | [[ "${2:-}" == "--file-id" ]] && FILE_KEY="file_id" |
| 22 | |
| 23 | BASE_ARGS=$(jq -n --arg key "$FILE_KEY" --arg val "$INPUT" '{($key): $val}') |
| 24 | |
| 25 | INFO_RESULT=$(mcporter call "slide-mcp" "slide_get_info" --args "$BASE_ARGS" 2>&1) || { |
| 26 | echo "ERROR:slide_get_info_failed" |
| 27 | exit 1 |
| 28 | } |
| 29 | |
| 30 | SLIDE_COUNT=$(echo "$INFO_RESULT" | jq -r '.slide_count // 0') |
| 31 | W_PT=$(echo "$INFO_RESULT" | jq -r '.w_pt // 0') |
| 32 | H_PT=$(echo "$INFO_RESULT" | jq -r '.h_pt // 0') |
| 33 | |
| 34 | # ── Permission check ── |
| 35 | # When slide_count=0 but dimensions are valid, it likely means we lack |
| 36 | # VIEW permission rather than the document being truly empty. |
| 37 | # Attempt to verify via tencent-docs check_access. |
| 38 | if [[ "$SLIDE_COUNT" -eq 0 ]] && [[ "$W_PT" -gt 0 ]] && [[ "$H_PT" -gt 0 ]]; then |
| 39 | # Try to extract file_id from URL (last path segment) |
| 40 | FILE_ID="" |
| 41 | if [[ "$FILE_KEY" == "file_url" ]]; then |
| 42 | FILE_ID=$(echo "$INPUT" | sed -E 's|.*/([^/?#]+).*|\1|') |
| 43 | else |
| 44 | FILE_ID="$INPUT" |
| 45 | fi |
| 46 | |
| 47 | if [[ -n "$FILE_ID" ]]; then |
| 48 | ACCESS_RESULT=$(mcporter call "tencent-docs" "check_access" --args "{\"file_id\":\"$FILE_ID\",\"actions\":[\"VIEW\"]}" 2>&1 || echo '{"granted_actions":[]}') |
| 49 | HAS_VIEW=$(echo "$ACCESS_RESULT" | jq -r '(.granted_actions // []) | map(select(. == "VIEW")) | length > 0' 2>/dev/null || echo "false") |
| 50 | |
| 51 | if [[ "$HAS_VIEW" != "true" ]]; then |
| 52 | jq -n --arg reason "permission_denied" --argjson sc "$SLIDE_COUNT" --argjson w "$W_PT" --argjson h "$H_PT" --arg fid "$FILE_ID" \ |
| 53 | '{action:"ask_user",reason:$reason,slide_count:$sc,w_pt:$w,h_pt:$h,file_id:$fid,hint:"当前账号无 VIEW 权限,请分享文档或下载到本地后重试"}' |
| 54 | exit 0 |
| 55 | fi |
| 56 | fi |
| 57 | fi |
| 58 | |
| 59 | if [[ "$SLIDE_COUNT" -eq 0 ]] || [[ "$W_PT" -eq 0 ]] || [[ "$H_PT" -eq 0 ]]; then |
| 60 | jq -n --arg reason "ppt_is_empty" --argjson sc "$SLIDE_COUNT" --argjson w "$W_PT" --argjson h "$H_PT" \ |
| 61 | '{action:"write_design_md",reason:$reason,slide_count:$sc,w_pt:$w,h_pt:$h}' |
| 62 | exit 0 |
| 63 | fi |
| 64 | |
| 65 | CONTENT_PAGE_COUNT=0 |
| 66 | |
| 67 | for i in $(seq 0 $((SLIDE_COUNT - 1))); do |
| 68 | PAGE_ARGS=$(echo "$BASE_ARGS" | jq --argjson idx "$i" '. + {page_index: $idx}') |
| 69 | PAGE_RESULT=$(mcporter call "slide-mcp" "slide_get_page_info" --args "$PAGE_ARGS" 2>&1) || continue |
| 70 | |
| 71 | HAS_CONTENT=$(echo "$PAGE_RESULT" | jq ' |
| 72 | [.shapes // [] | .[] | select((.text // "") | gsub("[\\s\\r\\n]"; "") != "")] | length > 0 |
| 73 | ' 2>/dev/null || echo "false") |
| 74 | |
| 75 | [[ "$HAS_CONTENT" == "true" ]] && CONTENT_PAGE_COUNT=$((CONTENT_PAGE_COUNT + 1)) |
| 76 | done |
| 77 | |
| 78 | if [[ "$CONTENT_PAGE_COUNT" -eq 0 ]]; then |
| 79 | jq -n --arg reason "ppt_content_is_empty" --argjson sc "$SLIDE_COUNT" --argjson w "$W_PT" --argjson h "$H_PT" --argjson cpc "$CONTENT_PAGE_COUNT" \ |
| 80 | '{action:"write_design_md",reason:$reason,slide_count:$sc,w_pt:$w,h_pt:$h,content_page_count:$cpc}' |
| 81 | exit 0 |
| 82 | fi |
| 83 | |
| 84 | DESIGN_RESULT=$(mcporter call "slide-mcp" "slide_get_design" --args "$BASE_ARGS" 2>&1) || { |
| 85 | echo "ERROR:slide_get_design_failed" |
| 86 | exit 1 |
| 87 | } |
| 88 | |
| 89 | DESIGN_EXISTS=$(echo "$DESIGN_RESULT" | jq -r '.exists // false') |
| 90 | DESIGN_MD=$(echo "$DESIGN_RESULT" | jq -r '.design_md // ""') |
| 91 | |
| 92 | if [[ "$DESIGN_EXISTS" == "false" ]] || [[ -z "$DESIGN_MD" ]] || [[ "$DESIGN_MD" == '""' ]]; then |
| 93 | jq -n --arg reason "design_is_empty" --argjson sc "$SLIDE_COUNT" --argjson w "$W_PT" --argjson h "$H_PT" --argjson cpc "$CONTENT_PAGE_COUNT" --argjson de "$DESIGN_EXISTS" \ |
| 94 | '{action:"write_design_md",reason:$reason,slide_count:$sc,w_pt:$w,h_pt:$h,content_page_count:$cpc,design_exists:$de}' |
| 95 | exit 0 |
| 96 | fi |
| 97 | |
| 98 | DESIGN_MD_LEN=${#DESIGN_MD} |
| 99 | UPDATED_AT=$(echo "$DESIGN_RESULT" | jq -r '.updated_at // "0"') |
| 100 | |
| 101 | jq -n --argjson sc "$SLIDE_COUNT" --argjson w "$W_PT" --argjson h "$H_PT" --argjson cpc "$CONTENT_PAGE_COUNT" --argjson de "$DESIGN_EXISTS" --argjson dml "$DESIGN_MD_LEN" --arg ua "$UPDATED_AT" \ |
| 102 | '{action:"proceed_next",reason:"design_exists",slide_count:$sc,w_pt:$w,h_pt:$h,content_page_count:$cpc,design_exists:$de,design_md_length:$dml,updated_at:$ua}' |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

