refactor: analysis workflow architecture

fix: NEXTAUTH_URL

fix: prevent project model edits from affecting default model
This commit is contained in:
saturn
2026-03-16 21:48:57 +08:00
parent ecbd183a77
commit 9aff44e37a
58 changed files with 2753 additions and 7985 deletions

View File

@@ -132,4 +132,36 @@ describe('createWorkerLLMStreamCallbacks', () => {
expect(payload.stepTitle).toBe('A')
expect(payload.output).toBe('characters-final')
})
it('uses injected active controller for run-owned workflows', async () => {
const job = buildJob()
const context = createWorkerLLMStreamContext(job, 'story_to_script')
const assertActive = vi.fn(async (_stage: string) => undefined)
const isActive = vi.fn(async () => true)
const callbacks = createWorkerLLMStreamCallbacks(job, context, {
assertActive,
isActive,
})
callbacks.onChunk?.({
kind: 'text',
delta: 'hello',
seq: 1,
lane: 'main',
step: { id: 'split_clips', attempt: 1, title: 'split', index: 1, total: 1 },
})
await callbacks.flush()
expect(assertActive).toHaveBeenCalledWith('worker_llm_stream')
expect(assertTaskActiveMock).not.toHaveBeenCalled()
expect(reportTaskStreamChunkMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
delta: 'hello',
}),
expect.objectContaining({
stepId: 'split_clips',
}),
)
})
})

View File

@@ -47,32 +47,17 @@ const runScriptToStoryboardOrchestratorMock = vi.hoisted(() =>
},
})),
)
const graphExecutorMock = vi.hoisted(() => ({
executePipelineGraph: vi.fn(async (input: {
runId: string
projectId: string
userId: string
state: Record<string, unknown>
nodes: Array<{ key: string; run: (ctx: Record<string, unknown>) => Promise<unknown> }>
}) => {
for (const node of input.nodes) {
await node.run({
runId: input.runId,
projectId: input.projectId,
userId: input.userId,
nodeKey: node.key,
attempt: 1,
state: input.state,
})
}
return input.state
}),
}))
const parseVoiceLinesJsonMock = vi.hoisted(() => vi.fn())
const persistStoryboardsAndPanelsMock = vi.hoisted(() => vi.fn())
const parseStoryboardRetryTargetMock = vi.hoisted(() => vi.fn())
const runScriptToStoryboardAtomicRetryMock = vi.hoisted(() => vi.fn())
const workflowLeaseMock = vi.hoisted(() => ({
assertWorkflowRunActive: vi.fn(async () => undefined),
withWorkflowRunLease: vi.fn(async (params: { run: () => Promise<unknown> }) => ({
claimed: true,
result: await params.run(),
})),
}))
const txState = vi.hoisted(() => ({
createdRows: [] as Array<Record<string, unknown>>,
@@ -145,10 +130,6 @@ vi.mock('@/lib/novel-promotion/script-to-storyboard/orchestrator', () => ({
}
},
}))
vi.mock('@/lib/run-runtime/graph-executor', () => ({
executePipelineGraph: graphExecutorMock.executePipelineGraph,
}))
vi.mock('@/lib/workers/handlers/llm-stream', () => ({
createWorkerLLMStreamContext: vi.fn(() => ({ streamRunId: 'run-1', nextSeqByStepLane: {} })),
createWorkerLLMStreamCallbacks: vi.fn(() => ({
@@ -192,6 +173,7 @@ vi.mock('@/lib/workers/handlers/script-to-storyboard-atomic-retry', () => ({
parseStoryboardRetryTarget: parseStoryboardRetryTargetMock,
runScriptToStoryboardAtomicRetry: runScriptToStoryboardAtomicRetryMock,
}))
vi.mock('@/lib/run-runtime/workflow-lease', () => workflowLeaseMock)
import { handleScriptToStoryboardTask } from '@/lib/workers/handlers/script-to-storyboard'

View File

@@ -0,0 +1,175 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { Job } from 'bullmq'
import type { TaskJobData } from '@/lib/task/types'
const tryUpdateTaskProgressMock = vi.hoisted(() => vi.fn(async () => true))
const publishTaskEventMock = vi.hoisted(() => vi.fn(async () => ({})))
const publishTaskStreamEventMock = vi.hoisted(() => vi.fn(async () => ({})))
const publishRunEventMock = vi.hoisted(() => vi.fn(async () => undefined))
const mapTaskSSEEventToRunEventsMock = vi.hoisted(() =>
vi.fn(() => [{
runId: 'run-1',
projectId: 'project-1',
userId: 'user-1',
eventType: 'step.start',
stepKey: 'split_clips',
attempt: 1,
lane: null,
payload: { mirrored: true },
}]),
)
vi.mock('@/lib/prisma', () => ({
prisma: {
project: {
findUnique: vi.fn(async () => null),
},
},
}))
vi.mock('@/lib/logging/core', () => ({
createScopedLogger: () => ({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}))
vi.mock('@/lib/task/service', () => ({
rollbackTaskBillingForTask: vi.fn(async () => ({ attempted: false, rolledBack: false, billingInfo: null })),
touchTaskHeartbeat: vi.fn(async () => undefined),
tryMarkTaskCompleted: vi.fn(async () => true),
tryMarkTaskFailed: vi.fn(async () => true),
tryMarkTaskProcessing: vi.fn(async () => true),
tryUpdateTaskProgress: tryUpdateTaskProgressMock,
updateTaskBillingInfo: vi.fn(async () => undefined),
}))
vi.mock('@/lib/task/publisher', () => ({
publishTaskEvent: publishTaskEventMock,
publishTaskStreamEvent: publishTaskStreamEventMock,
}))
vi.mock('@/lib/task/progress-message', () => ({
buildTaskProgressMessage: vi.fn(() => 'progress-message'),
getTaskStageLabel: vi.fn((stage: string) => `label:${stage}`),
}))
vi.mock('@/lib/errors/normalize', () => ({
normalizeAnyError: vi.fn((error: Error) => ({
code: 'ERROR',
message: error.message,
retryable: false,
provider: null,
})),
}))
vi.mock('@/lib/billing', () => ({
rollbackTaskBilling: vi.fn(async () => null),
settleTaskBilling: vi.fn(async () => null),
}))
vi.mock('@/lib/billing/runtime-usage', () => ({
withTextUsageCollection: vi.fn(async (fn: () => Promise<unknown>) => ({
result: await fn(),
textUsage: null,
})),
}))
vi.mock('@/lib/logging/file-writer', () => ({
onProjectNameAvailable: vi.fn(),
}))
vi.mock('@/lib/run-runtime/task-bridge', () => ({
mapTaskSSEEventToRunEvents: mapTaskSSEEventToRunEventsMock,
}))
vi.mock('@/lib/run-runtime/publisher', () => ({
publishRunEvent: publishRunEventMock,
}))
import { reportTaskProgress, reportTaskStreamChunk, withTaskLifecycle } from '@/lib/workers/shared'
function buildJob(taskType: TaskJobData['type']): Job<TaskJobData> {
return {
data: {
taskId: 'task-1',
type: taskType,
locale: 'zh',
projectId: 'project-1',
episodeId: 'episode-1',
targetType: 'NovelPromotionEpisode',
targetId: 'episode-1',
userId: 'user-1',
payload: {
runId: 'run-1',
},
trace: null,
},
queueName: 'text',
} as unknown as Job<TaskJobData>
}
describe('worker shared direct run events', () => {
beforeEach(() => {
tryUpdateTaskProgressMock.mockReset()
tryUpdateTaskProgressMock.mockResolvedValue(true)
publishTaskEventMock.mockReset()
publishTaskStreamEventMock.mockReset()
publishRunEventMock.mockReset()
mapTaskSSEEventToRunEventsMock.mockClear()
})
it('publishes run events directly for core analysis progress updates', async () => {
await reportTaskProgress(buildJob('story_to_script_run'), 42, {
stage: 'story_to_script_step',
stepId: 'split_clips',
stepTitle: 'Split',
})
expect(publishTaskEventMock).toHaveBeenCalledWith(expect.objectContaining({
taskType: 'story_to_script_run',
type: 'task.progress',
}))
expect(mapTaskSSEEventToRunEventsMock).toHaveBeenCalledTimes(1)
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
runId: 'run-1',
eventType: 'step.start',
stepKey: 'split_clips',
}))
})
it('publishes run events directly for core analysis stream chunks', async () => {
await reportTaskStreamChunk(buildJob('script_to_storyboard_run'), {
kind: 'text',
delta: 'hello',
seq: 1,
lane: 'main',
}, {
stepId: 'clip_1_phase1',
stepTitle: 'Phase 1',
})
expect(publishTaskStreamEventMock).toHaveBeenCalledWith(expect.objectContaining({
taskType: 'script_to_storyboard_run',
persist: true,
}))
expect(mapTaskSSEEventToRunEventsMock).toHaveBeenCalledTimes(1)
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
runId: 'run-1',
eventType: 'step.start',
stepKey: 'split_clips',
}))
})
it('emits run.start directly when the core analysis worker begins execution', async () => {
await withTaskLifecycle(buildJob('story_to_script_run'), async () => ({
ok: true,
}))
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
runId: 'run-1',
eventType: 'run.start',
}))
})
})

View File

@@ -26,33 +26,18 @@ const configMock = vi.hoisted(() => ({
const orchestratorMock = vi.hoisted(() => ({
runStoryToScriptOrchestrator: vi.fn(),
}))
const graphExecutorMock = vi.hoisted(() => ({
executePipelineGraph: vi.fn(async (input: {
runId: string
projectId: string
userId: string
state: Record<string, unknown>
nodes: Array<{ key: string; run: (ctx: Record<string, unknown>) => Promise<unknown> }>
}) => {
for (const node of input.nodes) {
await node.run({
runId: input.runId,
projectId: input.projectId,
userId: input.userId,
nodeKey: node.key,
attempt: 1,
state: input.state,
})
}
return input.state
}),
}))
const helperMock = vi.hoisted(() => ({
persistAnalyzedCharacters: vi.fn(async () => [{ id: 'character-new-1' }]),
persistAnalyzedLocations: vi.fn(async () => [{ id: 'location-new-1' }]),
persistClips: vi.fn(async () => [{ clipKey: 'clip-1', id: 'clip-row-1' }]),
}))
const workflowLeaseMock = vi.hoisted(() => ({
assertWorkflowRunActive: vi.fn(async () => undefined),
withWorkflowRunLease: vi.fn(async (params: { run: () => Promise<unknown> }) => ({
claimed: true,
result: await params.run(),
})),
}))
vi.mock('@/lib/prisma', () => ({ prisma: prismaMock }))
vi.mock('@/lib/llm-client', () => ({
@@ -69,9 +54,6 @@ vi.mock('@/lib/logging/file-writer', () => ({ onProjectNameAvailable: vi.fn() })
vi.mock('@/lib/workers/shared', () => ({ reportTaskProgress: workerMock.reportTaskProgress }))
vi.mock('@/lib/workers/utils', () => ({ assertTaskActive: workerMock.assertTaskActive }))
vi.mock('@/lib/novel-promotion/story-to-script/orchestrator', () => orchestratorMock)
vi.mock('@/lib/run-runtime/graph-executor', () => ({
executePipelineGraph: graphExecutorMock.executePipelineGraph,
}))
vi.mock('@/lib/workers/handlers/llm-stream', () => ({
createWorkerLLMStreamContext: vi.fn(() => ({ streamRunId: 'run-1', nextSeqByStepLane: {} })),
createWorkerLLMStreamCallbacks: vi.fn(() => ({
@@ -100,6 +82,7 @@ vi.mock('@/lib/workers/handlers/story-to-script-helpers', () => ({
persistClips: helperMock.persistClips,
resolveClipRecordId: (clipIdMap: Map<string, string>, clipId: string) => clipIdMap.get(clipId) ?? null,
}))
vi.mock('@/lib/run-runtime/workflow-lease', () => workflowLeaseMock)
import { handleStoryToScriptTask } from '@/lib/workers/handlers/story-to-script'