> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # filterRun() 宣言的なオプションから `prepareRun` 関数を作成します。結果を `createScorer()` に渡すことで、メッセージをフィルタリングし、コンテキストサイズを制限できます。また、スコアラーのパイプラインを実行する前に不要なフィールドを削除します。 宣言的なフィルタリングには [`filterRun()`](#usage-example) を使用します。カスタム `prepareRun` 関数は、`filterRun()` では対応できない命令的なロジックが必要な場合に直接記述してください。詳しくは[カスタムスコアラー:入力のフィルタリング](https://mastra.zisheng.pro/ja/docs/evals/custom-scorers)を参照してください。 ## 使用例 次の例では、ツール呼び出しとテキストメッセージだけを参照し、直近20件のコンテキストメッセージに制限するスコアラーを作成します。 ```typescript import { createScorer, filterRun } from '@mastra/core/evals' const toolScorer = createScorer({ id: 'tool-usage', description: 'Evaluates tool usage patterns', type: 'agent', prepareRun: filterRun({ partTypes: ['tool-invocation', 'text'], maxRememberedMessages: 20, }), }).generateScore(({ run }) => { // run.input.rememberedMessages contains only tool and text messages // run.output contains only tool and text messages return 1 }) ``` ### ツール名でフィルタリング 特定のツールに関係するメッセージだけを残します。 ```typescript import { createScorer, filterRun } from '@mastra/core/evals' const fileEditScorer = createScorer({ id: 'file-edit-quality', description: 'Evaluates file editing patterns', type: 'agent', prepareRun: filterRun({ toolNames: ['write_file', 'string_replace_lsp', 'view'], }), }).generateScore(({ run }) => { // Only messages with these tool calls remain return 1 }) ``` ### フィールドを削除 スコアラーに不要なフィールドを削除します。 ```typescript import { createScorer, filterRun } from '@mastra/core/evals' const simpleScorer = createScorer({ id: 'response-length', description: 'Checks response length', type: 'agent', prepareRun: filterRun({ dropRequestContext: true, dropExpectedTrajectory: true, dropGroundTruth: true, maxOutputMessages: 5, }), }).generateScore(({ run }) => { return run.output.length > 0 ? 1 : 0 }) ``` ## パラメーター **options** (`FilterRunOptions`): スコアラーが受け取るデータを制御する設定オブジェクト。 **options.partTypes** (`MastraPartType[]`): パートがこれらの型に一致するメッセージだけを残します。各項目はメッセージパートの型に対して前方一致します。プレーンテキストメッセージ(ツール呼び出しなし)は、明示的に除外されない限り常に残ります。システムメッセージとタグ付きシステムメッセージはフィルタリングされません。 **options.toolNames** (`string[]`): 指定したツールのツール呼び出しメッセージだけを残します。各項目はツール名に対して前方一致します。ツール以外のメッセージ(テキスト、データ)には影響しません。 **options.maxRememberedMessages** (`number`): 記憶されたメッセージ(コンテキスト)に残すメッセージの最大数。末尾(最新)から取得します。型とツールによるフィルタリング後に適用されます。 **options.maxOutputMessages** (`number`): 出力に残すメッセージの最大数。末尾から取得します。型とツールによるフィルタリング後に適用されます。 **options.dropRequestContext** (`boolean`): run からリクエストコンテキストを完全に削除します。 **options.dropExpectedTrajectory** (`boolean`): run から期待される軌跡を削除します。 **options.dropGroundTruth** (`boolean`): run から正解データを削除します。 **戻り値:** `(run: ScorerRun) => ScorerRun`。`prepareRun` オプションとして [`createScorer()`](https://mastra.zisheng.pro/ja/reference/evals/create-scorer) に渡せる関数です。 ## パートの型 `partTypes` オプションは `MastraPartType` の値を受け取ります。各値は前方一致するため、`'data-'` はすべてのデータパート型に一致します。 | 型 | 説明 | | ------------------- | ----------------------------------- | | `'text'` | テキストコンテンツのパート | | `'tool-invocation'` | ツール呼び出しと結果のパート | | `'reasoning'` | 思考過程の推論パート | | `'step-start'` | ステップマーカーのパート | | `'image'` | 画像パート | | `'file'` | ファイルパート | | `'source'` | ソース参照パート | | `'source-document'` | ソースドキュメントのパート | | `'data-'` | すべてのデータパート(任意の `data-*` プレフィックスに一致) | | `'data-om-'` | Observational Memory のデータパート | | `'data-workspace-'` | Workspace のデータパート | | `'data-sandbox-'` | Sandbox のデータパート | | `'data-tool-'` | ツール関連のデータパート | ## フィルタリングの動作 - **パート型のフィルタリング**は、`input.rememberedMessages` と `output` に Agent メッセージ配列が含まれる場合、その両方に適用されます。 - **ツール名のフィルタリング**は、ツール呼び出しを含むメッセージだけに影響します。テキストのみのメッセージはそのまま通過します。 - **システムメッセージ**(`systemMessages`、`taggedSystemMessages`)は、`partTypes` や `toolNames` にかかわらずフィルタリングされません。 - **メッセージ数の制限**(`maxRememberedMessages`、`maxOutputMessages`)は型とツールによるフィルタリング後に適用され、最新のメッセージを取得します。 - `partTypes` と `toolNames` の両方を設定した場合、メッセージを残すには両方のフィルターを満たす必要があります。