跳至主要內容

處理器

檔案式 Agent 會在其 processors/ 目錄中探索處理器。本頁說明檔案式慣例;如需了解內置處理器、自訂處理器、串流行為及進階模式,請參閱處理器指南。

processors/input/ 下的檔案會在訊息送達模型前執行。processors/output/ 下的檔案會在模型回應後執行。每個檔案都會預設匯出一個 Processor,而檔案名稱會成為其探索鍵。

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

將處理器檔案放在 processors/input/ 下,即可加入輸入處理器:

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

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

此處理器會在用戶訊息送達模型前執行。

加入輸出處理器
加入輸出處理器 的直接連結

輸出處理器適合在傳回模型回應前篩選或轉換回應。以下範例會遮蔽最終回應中的 PII。

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

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

次序
次序 的直接連結

探索到的處理器會加入在 config.ts 中定義的所有處理器之後。換言之,config 處理器會先執行。

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

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

與 config 的優先次序
與 config 的優先次序 的直接連結

如處理器需要在執行階段建立或由多個 Agent 共用,請使用 config.ts。如屬靜態、每個 Agent 專用,並應與 Agent 一併審閱的處理器,請使用檔案式處理器。

優先次序如下:

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

如需完整介面,請參閱 Processor 參考