/* Local, in-memory review model. It neither calls a platform nor grants real access. */ const SubscriptionDemo = (() => { const PLANS = { trial: { name: '两天试用', limit: 1 }, starter: { name: '入门版', limit: 1 }, advanced: { name: '进阶版', limit: 3 }, enterprise: { name: '企业版', limit: 5 }, }; const STAGES = [ { key: 'entitlement', title: '确认权益与容器名额', detail: '检查套餐权益,预留本次开通名额。' }, { key: 'cloud', title: '准备独立云资源', detail: '配置独立后端、云函数与专属资源凭据。' }, { key: 'matrix', title: '建立通信身份', detail: '创建归属于你的 Matrix 身份与会话通道。' }, { key: 'container', title: '创建并初始化容器', detail: '准备平台容器,安装已验证的运行环境。' }, { key: 'connectivity', title: '接入协作空间', detail: '验证身份与房间,完成通信接入。' }, { key: 'verification', title: '完成可用性核验', detail: '核对真实回复、完成信号和新鲜心跳。' }, ]; const scopeFields = ['plan', 'access', 'trialClaimed', 'trialStartedAt', 'trialExpiresAt', 'containers', 'addon', 'application', 'job', 'jobSequence']; const captureScope = (state) => Object.fromEntries(scopeFields.map((key) => [key, JSON.parse(JSON.stringify(state[key]))])); function switchSubject(state, subject) { if (state.activeSubject === subject) return; const account = state[subject + 'Account']; if (!account) return; state[state.activeSubject + 'Account'] = captureScope(state); for (const key of scopeFields) state[key] = JSON.parse(JSON.stringify(account[key])); state.activeSubject = subject; state.company = subject === 'company'; } const seedContainer = (index, company = false) => ({ id: (company ? 'demo-company-container-' : 'demo-container-') + index, name: ['我的协作伙伴', '内容助手', '研究助手', '项目伙伴', '知识助手'][index - 1] || '新的协作伙伴', owner: company ? '示例企业' : '我', company, ready: true }); function initial(scenario = 'new') { const state = { scenario, view: 'plans', plan: null, access: 'unopened', trialClaimed: false, trialStartedAt: null, trialExpiresAt: null, now: Date.parse('2026-11-25T10:00:00+08:00'), containers: [], addon: 0, application: null, job: null, offline: false, jobSequence: 0, company: false, activeSubject: 'personal', personalAccount: null, companyAccount: null, note: '' }; if (['advanced', 'quota', 'enterprise'].includes(scenario)) { if (scenario === 'enterprise') { state.personalAccount = captureScope(state); state.activeSubject = 'company'; } state.plan = scenario === 'enterprise' ? 'enterprise' : 'advanced'; state.company = scenario === 'enterprise'; state.access = 'active'; state.view = 'containers'; state.containers = Array.from({ length: scenario === 'quota' ? 3 : scenario === 'enterprise' ? 2 : 1 }, (_, index) => seedContainer(index + 1, state.company)); } if (scenario === 'expired') { state.plan = 'trial'; state.trialClaimed = true; state.access = 'expired'; state.view = 'expired'; state.trialStartedAt = state.now - 48 * 3600000; state.trialExpiresAt = state.now; state.containers = [seedContainer(1)]; } if (['waiting', 'failed'].includes(scenario)) { const started = reduce(state, { type: 'start', name: '我的第一个容器', plan: 'trial' }); started.job.stage = 3; started.job.completed = 3; started.job.status = scenario === 'failed' ? 'failed' : 'provisioning'; started.now += 6 * 60000; started.job.updatedAt = started.now; started.job.error = scenario === 'failed' ? '运行环境初始化暂时失败,平台保留了已完成的资源和任务进度。' : null; return started; } return state; } const limit = (state) => state.plan ? PLANS[state.plan].limit + (['advanced', 'enterprise'].includes(state.plan) ? state.addon : 0) : 0; const reserved = (state) => state.job && state.job.status !== 'ready' ? 1 : 0; const used = (state) => state.containers.length; const hasAccess = (state) => state.access === 'active'; const canCreate = (state) => hasAccess(state) && !reserved(state) && used(state) < limit(state); const clone = (state) => JSON.parse(JSON.stringify(state)); function reduce(previous, action) { const s = clone(previous); s.note = ''; if (action.type === 'subject') { switchSubject(s, action.subject); s.view = s.access === 'expired' ? 'expired' : action.view || 'containers'; return s; } if (action.type === 'view') { s.view = action.view; if (s.access === 'expired' && ['containers', 'access', 'chat'].includes(s.view)) s.view = 'expired'; return s; } if (action.type === 'offline') { s.offline = !s.offline; s.note = s.offline ? '网络连接已断开' : '网络已恢复'; return s; } if (s.offline && ['start', 'apply', 'retry', 'addon'].includes(action.type)) { s.note = '网络暂时不可用,请重试。'; return s; } if (action.type === 'start') { if (s.job && s.job.status !== 'ready') { s.view = 'waiting'; s.note = '正在开通'; return s; } if (action.plan === 'trial') { if (s.trialClaimed) { s.note = '试用已领取'; return s; } s.plan = 'trial'; s.trialClaimed = true; } else if (!canCreate(s) && s.access !== 'unopened') { s.note = '名额不足'; return s; } s.jobSequence += 1; s.job = { id: 'DEMO-JOB-' + String(s.jobSequence).padStart(3, '0'), status: 'queued', stage: 0, completed: 0, name: action.name || '我的第一个容器', createdAt: s.now, updatedAt: s.now, attempt: 1, delayed: false, error: null }; if (!s.containers.length) s.access = 'provisioning'; s.view = 'waiting'; } if (action.type === 'advance' && s.job && !['ready', 'failed'].includes(s.job.status)) { s.now += 90000; s.job.updatedAt = s.now; s.job.delayed = false; if (s.job.stage < STAGES.length - 1) { s.job.stage += 1; s.job.completed = s.job.stage; s.job.status = s.job.stage === STAGES.length - 1 ? 'verifying' : 'provisioning'; } else { s.job.status = 'ready'; s.job.completed = STAGES.length; const container = { ...seedContainer(s.containers.length + 1, s.company), name: s.job.name }; s.containers.push(container); s.job.agentId = container.id; s.access = 'active'; if (s.plan === 'trial' && s.trialStartedAt === null) { s.trialStartedAt = s.now; s.trialExpiresAt = s.now + 48 * 3600000; } s.note = '已就绪'; } } if (action.type === 'fail' && s.job && s.job.status !== 'ready') { s.job.status = 'failed'; s.job.error = '当前步骤执行失败。平台已保留完成的资源与原任务,可安全重试。'; s.job.updatedAt = s.now; } if (action.type === 'slow' && s.job && !['ready', 'failed'].includes(s.job.status)) { s.now += 45 * 60000; s.job.delayed = true; } if (action.type === 'retry' && s.job?.status === 'failed') { s.job.status = s.job.stage === 5 ? 'verifying' : 'provisioning'; s.job.error = null; s.job.attempt += 1; s.job.updatedAt = s.now; s.view = 'waiting'; s.note = '正在重试'; } if (action.type === 'apply') { if (action.plan === 'enterprise' && s.activeSubject !== 'company') { s.view = 'enterprise'; s.note = '请先核实企业身份,企业权益与个人权益分别管理。'; return s; } if (s.activeSubject === 'company' && action.plan !== 'enterprise') switchSubject(s, 'personal'); if (s.access === 'active' && s.plan !== 'trial' && PLANS[action.plan].limit < limit(s)) { s.note = '更换套餐请联系客服。'; return s; } s.application = { kind: s.access === 'expired' ? 'restore' : 'plan', plan: action.plan, status: 'pending' }; s.note = '申请已提交'; } if (action.type === 'addon') { if (!['advanced', 'enterprise'].includes(s.plan)) { s.note = '请先升级套餐'; return s; } s.application = { kind: 'addon', quantity: action.quantity, status: 'pending' }; s.note = '申请已提交'; } if (action.type === 'approve' && s.application?.status === 'pending') { const application = s.application; s.application = null; if (application.kind === 'addon') { s.addon += application.quantity; s.note = '名额已更新'; } else { const hadContainer = s.containers.length > 0; s.plan = application.plan; s.access = 'active'; if (hadContainer) { s.view = 'containers'; s.note = '已恢复使用'; } else return reduce(s, { type: 'start', plan: s.plan, name: '我的第一个容器' }); } } if (action.type === 'expire' && s.plan === 'trial' && s.access === 'active') { s.now = s.trialExpiresAt; s.access = 'expired'; s.view = 'expired'; s.note = '试用已结束'; } if (action.type === 'join' && s.view === 'enterprise') { if (s.activeSubject === 'company') return s; if (!s.companyAccount) s.companyAccount = { plan: 'enterprise', access: 'active', trialClaimed: false, trialStartedAt: null, trialExpiresAt: null, containers: [seedContainer(1, true), seedContainer(2, true)], addon: 0, application: null, job: null, jobSequence: 0 }; switchSubject(s, 'company'); s.view = 'enterprise'; s.note = '已加入企业'; } if (action.type === 'rename') { const target = s.containers.find((item) => item.id === action.id); if (target && hasAccess(s)) target.name = action.name; } return s; } return { PLANS, STAGES, initial, reduce, limit, reserved, used, hasAccess, canCreate }; })();