openclaw - ✅(Solved) Fix feat: before_model_resolve フックに attachments メタデータを追加 [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#65754Fetched 2026-04-14 05:40:30
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Timeline (top)
referenced ×2cross-referenced ×1

Fix Action

Fixed

PR fix notes

PR #128: feat(model-router): ファイル種別によるモデルルーティング

Description (problem / solution / changelog)

Summary

  • 画像・動画・PDF・Office・テキスト等のファイル添付を検出し、Gemini Flash にルーティング
  • テキストのみのメッセージは従来どおり Sonnet/Haiku で処理(既存機能は後方互換維持)
  • MIME パターンマッチング、kind フォールバック、カスタムルール対応

背景

hongmong-ochi-agent で画像大量送信による月間コスト急増($140→$200)が発生。 ファイル処理を Gemini Flash にルーティングすることで約 80-95% のコスト削減を見込む。

変更内容

  • config.ts: FileRoutingRule 型、デフォルトルール定義を追加
  • classifier.ts: matchMimePattern(), routeByAttachments() を追加
  • index.ts: ファイルルーティングをテキスト分類より優先で評価
  • openclaw.plugin.json: configSchemafileRouting セクション追加
  • テスト: 50 テスト全通過(新規 20 テスト追加)

依存関係

  • openclaw/openclaw#65754: before_model_resolve フックに attachments メタデータ追加が必要
    • マージされるまでは attachments が undefined のため、ファイルルーティングは発動しない
    • テキストベースのルーティング(既存機能)は即座に利用可能

設定例

{
  "model-router": {
    "enabled": true,
    "config": {
      "fileRouting": {
        "enabled": true,
        "rules": [
          {
            "label": "image",
            "mimePatterns": ["image/*"],
            "model": "gemini-2.5-flash",
            "provider": "google"
          }
        ]
      }
    }
  }
}

Test plan

  • 全 50 テスト通過(npx vitest run packages/model-router
  • openclaw/openclaw#65754 マージ後に E2E 検証
  • hongmong-ochi-agent で有効化してコスト削減効果を測定

Related

  • estack-inc/easy-flow#210
  • estack-inc/easy-flow#211
  • openclaw/openclaw#65754

Changed files

  • packages/model-router/openclaw.plugin.json (modified, +54/-1)
  • packages/model-router/src/classifier.test.ts (modified, +202/-3)
  • packages/model-router/src/classifier.ts (modified, +66/-1)
  • packages/model-router/src/config.ts (modified, +60/-1)
  • packages/model-router/src/index.test.ts (modified, +154/-9)
  • packages/model-router/src/index.ts (modified, +32/-6)

Code Example

// src/plugins/hook-before-agent-start.types.ts
export type PluginHookBeforeModelResolveEvent = {
  prompt: string;
};

---

export type PluginHookBeforeModelResolveEvent = {
  prompt: string;
  /** Attachment metadata for routing decisions. No binary data, just kind/mimeType hints. */
  attachments?: ReadonlyArray<{
    kind: "image" | "video" | "audio" | "document" | "other";
    mimeType?: string;
  }>;
};
RAW_BUFFERClick to expand / collapse

背景

before_model_resolve フックのイベントは現在 { prompt: string } のみで、メッセージに含まれるファイル/画像の情報にアクセスできない。

model-router プラグインでファイル種別に基づくモデルルーティング(例: 画像 → Gemini Flash、テキスト → Sonnet)を実現するために、添付ファイルのメタデータが必要。

現状

// src/plugins/hook-before-agent-start.types.ts
export type PluginHookBeforeModelResolveEvent = {
  prompt: string;
};

RunEmbeddedPiAgentParams には images?: ImageContent[] が存在し、resolveHookModelSelection() 呼び出し時点で利用可能だが、フックイベントに渡されていない。

提案

export type PluginHookBeforeModelResolveEvent = {
  prompt: string;
  /** Attachment metadata for routing decisions. No binary data, just kind/mimeType hints. */
  attachments?: ReadonlyArray<{
    kind: "image" | "video" | "audio" | "document" | "other";
    mimeType?: string;
  }>;
};

変更箇所

  1. src/plugins/hook-before-agent-start.types.ts: イベント型に attachments を追加
  2. src/agents/pi-embedded-runner/run/setup.ts: resolveHookModelSelection() に images/files 情報を渡す
  3. src/agents/pi-embedded-runner/run.ts: params.images から attachments メタデータを構築

設計方針

  • バイナリデータは含めず、kindmimeType のヒント情報のみ
  • 既存プラグインへの後方互換性を維持(attachments はオプショナル)
  • ReadonlyArray で不変性を保証

関連

  • estack-inc/easy-flow#210 (model-router によるファイル種別ルーティング)

extent analysis

TL;DR

Update the PluginHookBeforeModelResolveEvent type to include an optional attachments property to provide metadata for routing decisions.

Guidance

  • Review the proposed PluginHookBeforeModelResolveEvent type update to ensure it meets the requirements for model routing based on file types.
  • Implement the changes outlined in the "変更箇所" section, specifically updating src/plugins/hook-before-agent-start.types.ts, src/agents/pi-embedded-runner/run/setup.ts, and src/agents/pi-embedded-runner/run.ts.
  • Verify that the attachments metadata is correctly passed to the resolveHookModelSelection() function and used for routing decisions.
  • Test the updated implementation to ensure it works as expected and maintains backwards compatibility with existing plugins.

Example

export type PluginHookBeforeModelResolveEvent = {
  prompt: string;
  attachments?: ReadonlyArray<{
    kind: "image" | "video" | "audio" | "document" | "other";
    mimeType?: string;
  }>;
};

Notes

The proposed solution assumes that the images property in RunEmbeddedPiAgentParams contains the necessary metadata for routing decisions. If this is not the case, additional changes may be required to extract the necessary information.

Recommendation

Apply the proposed workaround by updating the PluginHookBeforeModelResolveEvent type and implementing the changes outlined in the "変更箇所" section. This will provide the necessary metadata for model routing based on file types while maintaining backwards compatibility with existing plugins.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING