Agent Skillsxberg-io/xberg › mime-detection-routing

mime-detection-routing

GitHub

提供MIME类型检测、提取器路由及格式注册表的开发指南。涵盖路径与字节检测逻辑、优先级选择、通配符支持,以及新增格式的标准化流程,用于调试路由错误或扩展系统能力。

.ai-rulez/skills/mime-detection-routing/SKILL.md xberg-io/xberg

Trigger Scenarios

添加新的文件格式支持 调试文件路由到错误提取器的问题 配置提取器与MIME类型的映射

Install

npx skills add xberg-io/xberg --skill mime-detection-routing -g -y
More Options

Non-standard path

npx skills add https://github.com/xberg-io/xberg/tree/main/.ai-rulez/skills/mime-detection-routing -g -y

Use without installing

npx skills use xberg-io/xberg@mime-detection-routing

指定 Agent (Claude Code)

npx skills add xberg-io/xberg --skill mime-detection-routing -a claude-code -g -y

安装 repo 全部 skill

npx skills add xberg-io/xberg --all -g -y

预览 repo 内 skill

npx skills add xberg-io/xberg --list

SKILL.md

Frontmatter
{
    "name": "mime-detection-routing",
    "description": "MIME type detection and extractor routing in core\/mime.rs — the FORMATS registry that EXT_TO_MIME and SUPPORTED_MIME_TYPES are derived from, the path-based and bytes-based detection functions, priority-based registry selection, wildcard MIME families, and the real procedure for adding a format. Load when adding a format, wiring an extractor to a MIME type, or debugging why a file routes to the wrong (or no) extractor."
}

MIME Detection & Routing

Detection Flow

Policy -> content and/or extension evidence -> validate_mime_type -> registry.get(mime) -> extractor

Key Functions

Function Location Behaviour
detect_mime_type(path, check_exists: bool) core/mime.rs Path-based only — never reads bytes. Lowercased extension → EXT_TO_MIME, then tree-sitter extension detection (feature tree-sitter), then mime_guess::from_path. check_exists gates a file-existence check, not content inspection.
detect_mime_type_from_bytes(bytes) core/mime.rs Magic-number detection via the infer crate. The only content-sniffing entry point.
validate_mime_type(mime) core/mime.rs Parses the media type, matches its case-insensitive essence against SUPPORTED_MIME_TYPES, and returns the registered MIME spelling. Parameters such as charset do not affect extractor routing. It does not consult the extractor registry.

The FORMATS registry is the single source of truth

FORMATS: &[FormatEntry { extensions, mime_type, aliases }] in core/mime.rs. EXT_TO_MIME and SUPPORTED_MIME_TYPES are LazyLocks derived from it by iteration — there is no m.insert call site to add to, and hand-editing either is impossible.

The full registry publishes 106 formats, 140 unique extensions, and 53 aliases, verified by scripts/sync_supported_counts.py verify. The published count constants describe that static registry; runtime availability is its intersection with registered extractors. Extension lookup is case-insensitive (the extension is lowercased before the map hit).

Registry Selection

let registry = get_document_extractor_registry();          // plugins/registry/mod.rs
let guard = registry.read()?;
let extractor: Arc<dyn DocumentExtractor> = guard.get(mime_type)?;  // Result, not Option

DocumentExtractorRegistry::get (plugins/registry/extractor.rs) returns the highest-priority() extractor for the MIME type, and returns Err — not None — when none matches.

Wildcard Support

An extractor may register a family: "image/*" matches image/png, image/jpeg, and so on (prefix match on a registered type ending in /*).

Adding a New Format

  1. Add one FormatEntry to FORMATS in crates/xberg/src/core/mime.rs. EXT_TO_MIME and SUPPORTED_MIME_TYPES update automatically.
  2. Run scripts/sync_supported_counts.py sync to update published count claims, then run its verify command.
  3. Implement InternalDocumentExtractor (not DocumentExtractor — see plugin-architecture-patterns) with supported_mime_types() returning the MIME.
  4. Register in crates/xberg/src/extractors/mod.rs::register_default_extractors().

Critical Rules

  1. Call validate_mime_type() before extraction — but do not treat it as proof an extractor exists.
  2. Extension lookup is case-insensitive.
  3. detect_mime_type inspects no content. Extraction defaults to PreferContent, which performs bounded content inspection and falls back to a supported extension. Use ContentOnly when the filename must be ignored; use TrustExtension only for trusted sources.
  4. A specific explicit MIME type is authoritative. application/octet-stream is the exception: it is a generic placeholder and triggers policy-based detection.
  5. Never edit EXT_TO_MIME or SUPPORTED_MIME_TYPES — edit FORMATS.

Version History

  • d8e4815 Current 2026-08-28 18:30

    重构为基于FORMATS单一事实源的架构,移除手动编辑映射,更新检测函数行为描述及新增格式步骤。

  • 531e0f7 2026-08-20 07:47

Same Skill Collection

.ai-rulez/skills/alef-generated-bindings/SKILL.md
.ai-rulez/skills/benchmark-workflow/SKILL.md
.ai-rulez/skills/chunking-embeddings/SKILL.md
.ai-rulez/skills/config-loading-precedence/SKILL.md
.ai-rulez/skills/crate-structure/SKILL.md
.ai-rulez/skills/extraction-pipeline-patterns/SKILL.md
.ai-rulez/skills/feature-flag-policy/SKILL.md
.ai-rulez/skills/ocr-pipeline-and-quality/SKILL.md
.ai-rulez/skills/pdf-backends/SKILL.md
.ai-rulez/skills/plugin-architecture-patterns/SKILL.md
.ai-rulez/skills/polyrepo-boundaries/SKILL.md
.ai-rulez/skills/release-readiness/SKILL.md
.ai-rulez/skills/release-versioning/SKILL.md
.ai-rulez/skills/test-corpus/SKILL.md
.ai-rulez/skills/wasm-constraints/SKILL.md
.ai-rulez/skills/xberg-typescript-toolchain/SKILL.md
plugin/.ai-rulez/skills/batch-extraction/SKILL.md
plugin/.ai-rulez/skills/chunking/SKILL.md
plugin/.ai-rulez/skills/extracting-keywords/SKILL.md
plugin/.ai-rulez/skills/extracting-tables/SKILL.md
plugin/.ai-rulez/skills/extracting-with-ocr/SKILL.md
plugin/.ai-rulez/skills/picking-a-format/SKILL.md
plugin/.ai-rulez/skills/xberg/SKILL.md
plugin/.cursor-plugin/skills/batch-extraction/SKILL.md
plugin/.cursor-plugin/skills/chunking/SKILL.md
plugin/.cursor-plugin/skills/extracting-keywords/SKILL.md
plugin/.cursor-plugin/skills/extracting-tables/SKILL.md
plugin/.cursor-plugin/skills/extracting-with-ocr/SKILL.md
plugin/.cursor-plugin/skills/picking-a-format/SKILL.md
plugin/.cursor-plugin/skills/xberg/SKILL.md
plugin/skills/batch-extraction/SKILL.md
plugin/skills/chunking/SKILL.md
plugin/skills/extracting-keywords/SKILL.md
plugin/skills/extracting-tables/SKILL.md
plugin/skills/extracting-with-ocr/SKILL.md
plugin/skills/picking-a-format/SKILL.md
plugin/skills/xberg/SKILL.md
.ai-rulez/skills/api-server-mcp/SKILL.md
.ai-rulez/skills/format-specific-extraction/SKILL.md

Metadata

Files
0
Version
d8e4815
Hash
bd944f15
Indexed
2026-08-20 07:47

trang chủ - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-01 07:29
浙ICP备14020137号-1 $bản đồ khách truy cập$