跳至主要內容

Processor

檔案式 Agent 會從 processors/ 目錄探索 processor。本頁說明檔案式慣例;若要了解內建 processor、自訂 processor、串流行為與進階模式,請參閱 processor 指南。

processors/input/ 下的檔案會在訊息送達模型前執行。processors/output/ 下的檔案則會在模型回應後執行。每個檔案會 default export 一個 Processor,而檔名會成為其探索 key。

快速開始
「快速開始」的直接連結

將 processor 檔案放在 processors/input/ 下,以新增輸入 processor:

src/mastra/agents/weather/processors/input/pii.ts
import { PIIDetector } from '@mastra/core/processors'

export default new PIIDetector({
strategy: 'block',
})

此 processor 會在使用者訊息送達模型前執行。

新增輸出 processor
「新增輸出 processor」的直接連結

輸出 processor 適合在模型回應傳回前進行篩選或轉換。以下範例會從最終回應中遮蔽 PII。

src/mastra/agents/weather/processors/output/redact.ts
import { PIIDetector } from '@mastra/core/processors'

export default new PIIDetector({
strategy: 'redact',
})

順序
「順序」的直接連結

探索到的 processor 會加在 config.ts 中定義的 processor 之後。也就是說,config processor 會先執行。

Ordering
config.inputProcessors → processors/input/* → model
model → config.outputProcessors → processors/output/* → response

如果 config.inputProcessorsconfig.outputProcessors 是函式,系統會忽略該類型的已探索 processor 並顯示警告,因為函式值 processor 無法以靜態方式合併。

與 config 的優先順序
「與 config 的優先順序」的直接連結

對於需要在執行階段建立或由多個 Agent 共用的 processor,請使用 config.ts。對於應與 Agent 一起檢閱的靜態、各 Agent 專用 processor,請使用檔案式 processor。

優先順序如下:

  1. 函式值 config.inputProcessorsconfig.outputProcessors
  2. 陣列值 config.inputProcessorsconfig.outputProcessors
  3. processors/input/processors/output/ 下探索到的檔案

完整 interface 請參閱 Processor 參考文件