From 35cfe6828056937a4cb241e944d0cc37b97cc8a3 Mon Sep 17 00:00:00 2001 From: yiling Date: Wed, 3 Jun 2026 09:10:31 +0800 Subject: [PATCH] feat(workflow): integrate temporal workflow bundles and support dev/prod mode --- AGENTS.md | 18 ++++---- apps/agent/src/index.ts | 12 +++++- apps/agent/src/workflows.ts | 5 +++ apps/transcode/src/index.ts | 15 +++++-- apps/transcode/src/workflows.ts | 1 + packages/agent/src/activities/agent.ts | 6 ++- packages/agent/src/activities/ai.ts | 5 +-- .../agent/src/workflows/agent-autofill.ts | 6 ++- packages/transcode/src/index.ts | 9 +--- packages/transcode/src/workflows/transcode.ts | 10 ++--- packages/workflow-core/src/activities/task.ts | 4 +- packages/workflow-core/src/workflow.ts | 43 +++++++++++++++++-- tsconfig.json | 5 ++- 13 files changed, 103 insertions(+), 36 deletions(-) create mode 100644 apps/agent/src/workflows.ts create mode 100644 apps/transcode/src/workflows.ts diff --git a/AGENTS.md b/AGENTS.md index d32c2ac9..0dcd02bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -113,15 +113,15 @@ The backend is built with: The project is a monorepo managed by **Bun Workspaces**. It follows a strictly decoupled architecture where each domain or layer is its own package: -- **WebUI (`packages/webui`)**: React-based frontend. -- **API (`packages/api`)**: Hono-based HTTP entry point. Handles requests and calls Core services. -- **Core (`packages/core`)**: Business logic, services, and infrastructure utilities. -- **Database (`packages/db`)**: Prisma client, schema, and migrations. -- **DTOs (`packages/dtos`)**: Shared type definitions and Zod schemas used by both API and WebUI. -- **Workers**: Specialized packages for background task execution: - - `@shumai/workflow-core`: Common workflow engine logic. - - `@shumai/agent`: AI agent workflows and activities. - - `@shumai/transcode`: Media processing workflows and activities. +- **WebUI (`packages/webui`)**: React-based frontend. +- **API (`packages/api`)**: Hono-based HTTP entry point. Handles requests and calls Core services. +- **Core (`packages/core`)**: Business logic, services, and infrastructure utilities. +- **Database (`packages/db`)**: Prisma client, schema, and migrations. +- **DTOs (`packages/dtos`)**: Shared type definitions and Zod schemas used by both API and WebUI. +- **Workers**: Specialized packages for background task execution: + - `@shumai/workflow-core`: Common workflow engine logic. + - `@shumai/agent`: AI agent workflows and activities. + - `@shumai/transcode`: Media processing workflows and activities. ### Layered Communication Rules diff --git a/apps/agent/src/index.ts b/apps/agent/src/index.ts index d8d8128d..c5be725e 100644 --- a/apps/agent/src/index.ts +++ b/apps/agent/src/index.ts @@ -10,7 +10,17 @@ async function run() { initAgentWorkflows() console.log('🚀 Starting Agent Worker...') - await workflowService.startWorkers(TaskQueueAgent) + + const options: { workflowBundle?: unknown; workflowsPath?: string } = {} + if (process.env.NODE_ENV === 'production') { + // @ts-ignore: Bun temporal plugin macro + const { default: workflowBundle } = await import('./workflows:::workflow') + options.workflowBundle = workflowBundle + } else { + options.workflowsPath = new URL('./workflows.ts', import.meta.url).pathname + } + + await workflowService.startWorkers(TaskQueueAgent, options) } run().catch((err) => { diff --git a/apps/agent/src/workflows.ts b/apps/agent/src/workflows.ts new file mode 100644 index 00000000..fb8491a8 --- /dev/null +++ b/apps/agent/src/workflows.ts @@ -0,0 +1,5 @@ +export * from '@shumai/agent/src/workflows/agent-autofill' +export * from '@shumai/agent/src/workflows/agent-chat' +export * from '@shumai/agent/src/workflows/agent-embedding' +export * from '@shumai/agent/src/workflows/agent-tool-call' +export * from '@shumai/agent/src/workflows/query-embedding-for-search' diff --git a/apps/transcode/src/index.ts b/apps/transcode/src/index.ts index fd9fa2df..63a093c1 100644 --- a/apps/transcode/src/index.ts +++ b/apps/transcode/src/index.ts @@ -1,16 +1,25 @@ import { loadEnvConfig } from '@shumai/core/src/env-loader' loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production') -import { workflowService } from '@shumai/workflow-core' -import { TaskQueueTranscode } from '@shumai/workflow-core' import { initTranscodeWorkflows } from '@shumai/transcode' +import { TaskQueueTranscode, workflowService } from '@shumai/workflow-core' async function run() { // Initialize workflows and activities initTranscodeWorkflows() console.log('🚀 Starting Transcode Worker...') - await workflowService.startWorkers(TaskQueueTranscode) + + const options: { workflowBundle?: unknown; workflowsPath?: string } = {} + if (process.env.NODE_ENV === 'production') { + // @ts-ignore: Bun temporal plugin macro + const { default: workflowBundle } = await import('./workflows:::workflow') + options.workflowBundle = workflowBundle + } else { + options.workflowsPath = new URL('./workflows.ts', import.meta.url).pathname + } + + await workflowService.startWorkers(TaskQueueTranscode, options) } run().catch((err) => { diff --git a/apps/transcode/src/workflows.ts b/apps/transcode/src/workflows.ts new file mode 100644 index 00000000..f4233ae5 --- /dev/null +++ b/apps/transcode/src/workflows.ts @@ -0,0 +1 @@ +export * from '@shumai/transcode/src/workflows/transcode' diff --git a/packages/agent/src/activities/agent.ts b/packages/agent/src/activities/agent.ts index 6032bbe5..d5234108 100644 --- a/packages/agent/src/activities/agent.ts +++ b/packages/agent/src/activities/agent.ts @@ -3,7 +3,11 @@ import { DatabaseSessionStorage } from '../database-session-storage' import { prisma, AssetType, Prisma, type Skill } from '@shumai/db' import { logger } from '@shumai/core/src/logger' import { s3Service } from '@shumai/core/src/s3/s3' -import { type AgentTool, type AgentMessage, type SessionTreeEntry } from '@earendil-works/pi-agent-core' +import { + type AgentTool, + type AgentMessage, + type SessionTreeEntry, +} from '@earendil-works/pi-agent-core' import { type ImageContent } from '@earendil-works/pi-ai' import { ApplicationFailure } from '@temporalio/activity' import { assetService } from '@shumai/core/src/asset/asset' diff --git a/packages/agent/src/activities/ai.ts b/packages/agent/src/activities/ai.ts index 26a18567..e0ea91e6 100644 --- a/packages/agent/src/activities/ai.ts +++ b/packages/agent/src/activities/ai.ts @@ -261,10 +261,7 @@ export async function extractAiMetadataActivity( return generatedFiles } -export async function getEmbeddingContextActivity(params: { - teamId: string - assetId: string -}) { +export async function getEmbeddingContextActivity(params: { teamId: string; assetId: string }) { const team = await prisma.team.findUnique({ where: { id: params.teamId }, }) diff --git a/packages/agent/src/workflows/agent-autofill.ts b/packages/agent/src/workflows/agent-autofill.ts index f0c9e173..2ec1dda5 100644 --- a/packages/agent/src/workflows/agent-autofill.ts +++ b/packages/agent/src/workflows/agent-autofill.ts @@ -93,7 +93,11 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { } // 3. Get Project Autofill Fields - const fields = await executeActivity(TaskQueueAgent, getProjectAutofillFieldsActivity, projectId) + const fields = await executeActivity( + TaskQueueAgent, + getProjectAutofillFieldsActivity, + projectId, + ) if (fields.length === 0) { if (placeholderCommentId) { await executeActivity(TaskQueueAgent, updateCommentActivity, { diff --git a/packages/transcode/src/index.ts b/packages/transcode/src/index.ts index 3d89e822..44ef590e 100644 --- a/packages/transcode/src/index.ts +++ b/packages/transcode/src/index.ts @@ -1,14 +1,9 @@ -import { registerWorkflow, registerActivities } from '@shumai/workflow-core' import { WorkflowTaskType } from '@shumai/db' -import { transcodeMedia } from './workflows/transcode' +import { registerActivities, registerWorkflow } from '@shumai/workflow-core' import * as transcodeActivities from './activities/transcode' +import { transcodeMedia } from './workflows/transcode' export function initTranscodeWorkflows() { registerWorkflow(WorkflowTaskType.transcode, transcodeMedia) registerActivities(transcodeActivities) } - -export * from './transcoder' -export * from './transcode' -export * from './workflows/transcode' -export * from './activities/transcode' diff --git a/packages/transcode/src/workflows/transcode.ts b/packages/transcode/src/workflows/transcode.ts index 41c8d972..b720fb67 100644 --- a/packages/transcode/src/workflows/transcode.ts +++ b/packages/transcode/src/workflows/transcode.ts @@ -1,10 +1,10 @@ import type { WorkflowTask } from '@shumai/db' -import { ApplicationFailure } from '@temporalio/workflow' import { - getActivities, - executeActivity, - TaskQueueTranscode, -} from '@shumai/workflow-core' + executeActivity, + getActivities, + TaskQueueTranscode, +} from '@shumai/workflow-core/src/workflow-utils' +import { ApplicationFailure } from '@temporalio/workflow' export async function transcodeMedia(task: WorkflowTask): Promise { const { diff --git a/packages/workflow-core/src/activities/task.ts b/packages/workflow-core/src/activities/task.ts index 94ce337c..fe3347f6 100644 --- a/packages/workflow-core/src/activities/task.ts +++ b/packages/workflow-core/src/activities/task.ts @@ -9,7 +9,9 @@ export async function getAgentWorkerQueueActivity(): Promise { return 'agent_queue' } -export async function getAssetStatusActivity(params: { assetId: string }): Promise { +export async function getAssetStatusActivity(params: { + assetId: string +}): Promise { const asset = await prisma.asset.findUnique({ where: { id: params.assetId }, select: { status: true }, diff --git a/packages/workflow-core/src/workflow.ts b/packages/workflow-core/src/workflow.ts index 92b6de78..39ba41f7 100644 --- a/packages/workflow-core/src/workflow.ts +++ b/packages/workflow-core/src/workflow.ts @@ -1,8 +1,8 @@ -import { WorkflowTask } from '@shumai/db' +import { prisma, registerWorkflowTrigger, WorkflowTask } from '@shumai/db' +import path from 'path' import { Executor } from './executor' import { LocalExecutor } from './local-executor' import { TemporalExecutor } from './temporal-executor' -import { prisma, registerWorkflowTrigger } from '@shumai/db' export class WorkflowService { private executor: Executor @@ -56,7 +56,10 @@ export class WorkflowService { } // Starts Temporal workers for specific task queues - async startWorkers(queue: string): Promise { + async startWorkers( + queue: string, + options: { workflowBundle?: unknown; workflowsPath?: string } = {}, + ): Promise { const type = process.env.WORKFLOW_EXECUTOR || 'local' if (type === 'temporal') { const { Worker, NativeConnection } = await import('@temporalio/worker') @@ -85,10 +88,31 @@ export class WorkflowService { sharedActivities.getTranscodeWorkerQueueActivity = async () => workerSpecificQueue } + const { workflowBundle, workflowsPath } = options + const sharedWorker = await Worker.create({ connection, + // The workflowBundle is a pre-bundled set of workflows, its internal structure + // is managed by the Temporal worker and doesn't need explicit typing here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ...(workflowBundle ? { workflowBundle: workflowBundle as any } : {}), + ...(workflowsPath ? { workflowsPath } : {}), activities: sharedActivities, taskQueue: queue, + + bundlerOptions: { + webpackConfigHook: (config) => { + config.resolve = config.resolve || {} + config.resolve.alias = { + ...config.resolve.alias, + // Find the exact absolute path to the package directory + '@temporalio/workflow': path.dirname( + require.resolve('@temporalio/workflow/package.json'), + ), + } + return config + }, + }, }) const specificWorker = await Worker.create({ @@ -96,6 +120,19 @@ export class WorkflowService { // No workflows needed for the unique queue activities, taskQueue: workerSpecificQueue, + bundlerOptions: { + webpackConfigHook: (config) => { + config.resolve = config.resolve || {} + config.resolve.alias = { + ...config.resolve.alias, + // Find the exact absolute path to the package directory + '@temporalio/workflow': path.dirname( + require.resolve('@temporalio/workflow/package.json'), + ), + } + return config + }, + }, }) await Promise.all([sharedWorker.run(), specificWorker.run()]) diff --git a/tsconfig.json b/tsconfig.json index d6e8c691..7b6ad519 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,8 +21,11 @@ "@shumai/db/test": ["packages/db/src/test.ts"], "@shumai/dtos": ["packages/dtos/src/index.ts"], "@shumai/workflow-core": ["packages/workflow-core/src/index.ts"], + "@shumai/workflow-core/src/*": ["packages/workflow-core/src/*"], "@shumai/agent": ["packages/agent/src/index.ts"], - "@shumai/transcode": ["packages/transcode/src/index.ts"] + "@shumai/agent/src/*": ["packages/agent/src/*"], + "@shumai/transcode": ["packages/transcode/src/index.ts"], + "@shumai/transcode/src/*": ["packages/transcode/src/*"] } }, "exclude": ["node_modules"]