import { request, optionalResource } from '../../lib/http' import type { Checkpoint, CheckpointSummary } from '../workflows/types' import type { WorkflowExecutionStart } from '../workflows/execution.types' import type { CreateProjectInput, DramaState, Project, ProjectDetail, ProjectStageReadiness, ProjectDurationBudgetSummary, ProjectQualityPolicy, ProjectQualityPolicyResult, UpdateProjectInput } from './types' /** 项目与 Create Drama API;路径严格对应后端 dev。 */ export const projectsApi = { /** * 读取服务端解析后的完整质量策略。 * @param id 当前项目编号 * @param signal 请求取消信号 * @returns 项目质量策略 */ qualityPolicy: (id: string, signal?: AbortSignal) => request(`/projects/${encodeURIComponent(id)}/quality-policy`, { signal }), /** * 显式替换完整质量策略,不触发模型生成。 * @param id 当前项目编号 * @param policy 用户明确确认的完整策略 * @param expectedRevision 最后读取的服务端修订版本 * @returns 服务端实际保存的策略 */ saveQualityPolicy: (id: string, policy: ProjectQualityPolicy, expectedRevision: number) => request(`/projects/${encodeURIComponent(id)}/quality-policy`, { method: 'PUT', body: { policy, expectedRevision } }), /** * 用户明确确认当前规则;普通保存不能代替确认。 * @param id 当前项目 * @param expectedRevision 用户核对的服务端修订版本 * @returns 服务保存的确认状态 */ confirmQualityPolicy: (id: string, expectedRevision: number) => request(`/projects/${encodeURIComponent(id)}/quality-policy/confirm`, { method: 'POST', body: { expectedRevision } }), list: (signal?: AbortSignal) => request('/projects', { signal }), detail: (id: string, signal?: AbortSignal) => request(`/projects/${encodeURIComponent(id)}`, { signal }), durationBudget: (id: string, signal?: AbortSignal) => request(`/projects/${encodeURIComponent(id)}/duration-budget`, { signal }), stageReadiness: (id: string, signal?: AbortSignal) => request(`/projects/${encodeURIComponent(id)}/stage-readiness`, { signal }), create: (input: CreateProjectInput) => request<{ projectId: string; status: string; executionId: string }>('/projects', { method: 'POST', body: input }), update: (id: string, input: UpdateProjectInput) => request(`/projects/${encodeURIComponent(id)}`, { method: 'PUT', body: input }), deleteReadiness: (id: string, signal?: AbortSignal) => request<{ projectId: string canDelete: boolean totalActiveTasks: number activeTasks: Record }>(`/projects/${encodeURIComponent(id)}/delete-readiness`, { signal }), delete: (id: string) => request<{ id: string; deleted: boolean }>(`/projects/${encodeURIComponent(id)}`, { method: 'DELETE' }), state: (id: string, signal?: AbortSignal) => optionalResource(request(`/projects/${encodeURIComponent(id)}/state`, { signal })), /** * 读取每个工作流最新的执行状态,供导航与操作门禁使用。 * @param id 当前项目 * @param signal 请求取消信号 * @returns 最新保存记录摘要 */ async checkpointSummaries(id: string, signal?: AbortSignal) { const rows = await request( `/projects/${encodeURIComponent(id)}/checkpoints?view=summary`, { signal } ) if ( !Array.isArray(rows) || rows.some( row => !row?.workflowName || !row.checkpointId || !row.createdAt || !row.updatedAt || !row.state || typeof row.state !== 'object' || Array.isArray(row.state) || Object.keys(row.state).some(key => key !== 'workflowExecution') || (row.state.workflowExecution !== undefined && typeof row.state.workflowExecution?.status !== 'string') ) || new Set(rows.map(row => row.workflowName)).size !== rows.length ) throw new Error('任务摘要无法核对,请刷新后再试。') return rows }, /** * 读取完整记录,页面按实际需要指定工作流。 * @param id 当前项目 * @param signal 请求取消信号 * @param workflowName 完整记录所属的工作流 * @returns 保留历史顺序及来源信息的完整记录 */ async checkpoints(id: string, signal?: AbortSignal, workflowName?: string) { const rows = await request( `/projects/${encodeURIComponent(id)}/checkpoints` + (workflowName === undefined ? '' : `?${new URLSearchParams({ workflowName })}`), { signal } ) if ( !Array.isArray(rows) || (workflowName !== undefined && rows.some(row => row?.workflowName !== workflowName)) ) throw new Error('工作流记录无法核对,请刷新后再试。') return rows }, /** * 用户确认后恢复创作;改写明确携带服务端要求的确认标记。 * @param id 当前项目编号 * @param action 已确认的恢复动作 * @returns 后台任务句柄 */ resume: (id: string, action: 'resume-generation' | 'resume-rewrite') => request(`/projects/${encodeURIComponent(id)}/${action}`, { method: 'POST', body: action === 'resume-rewrite' ? { confirmed: true } : undefined }) }