跳至主要內容

Workflow.createRun()

.createRun() 方法會建立新的 Workflow 執行個體,讓你能以指定的輸入資料執行 Workflow。這是目前會傳回 Run 個體的 API。

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

await workflow.createRun()

參數
參數 的直接連結

runId?:

string
Workflow 執行的選填自訂識別碼

resourceId?:

string
將 Workflow 執行與特定資源(例如使用者 ID、租戶 ID)關聯的選填識別碼。此值會隨 Workflow 執行一併持久保存,並可用於篩選及查詢執行記錄。

disableScorers?:

boolean
停用此 Workflow 執行的 scorers 的選填旗標

傳回值
傳回值 的直接連結

run:

Run
可用來執行 Workflow 的新 Workflow 執行個體

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

const workflow = mastra.getWorkflow('workflow')

const run = await workflow.createRun()

const result = await run.start({
inputData: {
value: 10,
},
})

使用 resourceId
using-resourceid 的直接連結

resourceId 參數會將 Workflow 執行與特定資源(例如使用者或租戶)關聯。這對多租戶應用程式,或需要追蹤是哪位使用者啟動 Workflow 的情況十分有用。

const workflow = mastra.getWorkflow('workflow')

// Create a run associated with a specific user
const run = await workflow.createRun({
resourceId: 'user-123',
})

const result = await run.start({
inputData: {
value: 10,
},
})

// Later, retrieve the run and access the resourceId
const storedRun = await workflow.getWorkflowRunById(run.runId)
console.log(storedRun.resourceId) // "user-123"