跳至主要內容

createSkill()

建立可直接傳送至 Agent skills 設定的內嵌 Skill,毋須檔案系統或 Workspace。

傳回的物件會實作 Skill 介面,並可在任何支援透過檔案系統找到 Skill 的地方使用。

使用範例
使用範例 的直接連結

src/mastra/skills.ts
import { createSkill } from '@mastra/core/skills'

const reviewSkill = createSkill({
name: 'code-review',
description: 'Use when reviewing code changes.',
instructions: `
When reviewing code:
1. Check for correctness
2. Check for style consistency
3. Look for potential bugs
`,
references: {
'checklist.md': '# Review Checklist\n- No unused imports\n- Error handling present',
},
})

參數
參數 的直接連結

name:

string
獨有的 Skill 名稱。Agent 啟用 Skill 時會使用此名稱作為識別碼。必須是有效的 slug(小寫,允許使用連字號)。

description:

string
在系統訊息中向 Agent 顯示的簡短說明,協助 Agent 決定何時使用此 Skill。

instructions:

string
採用 Markdown 格式的完整 Skill 指示。Agent 啟用 Skill 時,這些指示會載入對話。

references?:

Record<string, string>
Agent 可使用 skill_read Tool 讀取的支援文件。鍵為檔案名稱,值為檔案內容。

license?:

string
Skill 的 SPDX 授權條款識別碼。

compatibility?:

unknown
兼容性要求。Agent Skills 規格容許彈性設定,可以是字串陣列、物件,或工具預期的任何結構。

user-invocable?:

boolean
終端用戶能否直接叫用此 Skill。預設為 undefined(由 Agent 決定)。

metadata?:

Record<string, unknown>
附加至 Skill 的任意中繼資料。

傳回值
傳回值 的直接連結

傳回實作 Skill 介面的 InlineSkill 物件:

interface InlineSkill extends Skill {
__inline: true
__referenceContents: Record<string, string>
}

此 Skill 會獲派合成路徑 inline/<name>(例如 inline/code-review)。

驗證
驗證 的直接連結

createSkill() 使用與檔案系統 Skill 相同的規則驗證輸入:

  • name 為必填,而且必須是有效的 slug
  • description 為必填
  • instructions 為必填,而且不得為空白

如果驗證失敗,便會擲回包含無效欄位詳細資料的 Error

// Throws: Invalid skill "": name is required
createSkill({ name: '', description: 'test', instructions: 'test' })