refactor(workflow): throw non-retryable ApplicationFailure for validation errors

This commit is contained in:
yiling
2026-05-28 09:57:30 +08:00
parent 11d2e89a48
commit 3b08ade55d
7 changed files with 161 additions and 24 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@/generated/prisma/client'
import {
getActivities,
@@ -50,7 +51,7 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
const asset = await executeActivity(TaskQueueDb, getAssetActivity, task.assetId)
const key = asset?.storageKey?.key
if (!asset || !asset.project || !key) {
throw new Error('Asset or project not found')
throw ApplicationFailure.create({ message: 'Asset or project not found', nonRetryable: true })
}
const teamId = asset.project.teamId
const projectId = asset.project.id
+17 -5
View File
@@ -1,3 +1,4 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@/generated/prisma/client'
import {
executeActivity,
@@ -31,18 +32,29 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
})
const payload = task.payload
if (!payload) throw new Error('Task payload is missing')
if (!payload) {
throw ApplicationFailure.create({ message: 'Task payload is missing', nonRetryable: true })
}
const agentId = payload.agent?.agentId
if (!agentId) throw new Error('agentId missing in payload')
if (!agentId) {
throw ApplicationFailure.create({ message: 'agentId missing in payload', nonRetryable: true })
}
let sessionId = payload.agent?.sessionId
// 1. Get User Comment
const userCommentId = payload.agent?.userCommentId
if (!userCommentId) throw new Error('userCommentId missing in payload')
if (!userCommentId) {
throw ApplicationFailure.create({
message: 'userCommentId missing in payload',
nonRetryable: true,
})
}
const userComment = await executeActivity(TaskQueueDb, getCommentActivity, userCommentId)
if (!userComment) throw new Error('User comment not found')
if (!userComment) {
throw ApplicationFailure.create({ message: 'User comment not found', nonRetryable: true })
}
// 2. Create Placeholder Comment
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
@@ -57,7 +69,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
// 3. Get Asset
const asset = await executeActivity(TaskQueueDb, getAssetActivity, task.assetId)
if (!asset || !asset.project) {
throw new Error('Asset or project not found')
throw ApplicationFailure.create({ message: 'Asset or project not found', nonRetryable: true })
}
const teamId = asset.project.teamId
+4 -1
View File
@@ -1,3 +1,4 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@/generated/prisma/client'
import { getActivities, executeActivity, TaskQueueDb, TaskQueueAgent } from '../workflow-utils'
@@ -32,7 +33,9 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
})
placeholderCommentId = placeholder.id
if (!task.teamId) throw new Error('Task has no teamId')
if (!task.teamId) {
throw ApplicationFailure.create({ message: 'Task has no teamId', nonRetryable: true })
}
// 1. Fetch Agent Context (Database Activity on db_queue)
const context = await executeActivity(TaskQueueDb, getEmbeddingContextActivity, {
+2 -1
View File
@@ -1,3 +1,4 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@/generated/prisma/client'
import {
getActivities,
@@ -44,7 +45,7 @@ export async function aiTranscriptionMedia(task: WorkflowTask): Promise<void> {
const asset = await executeActivity(TaskQueueDb, getAssetActivity, task.assetId)
const key = asset?.storageKey?.key
if (!asset || !key) {
throw new Error('Asset not found')
throw ApplicationFailure.create({ message: 'Asset not found', nonRetryable: true })
}
// 2. Extract Audio (Requires FFmpeg)