model.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Local, in-memory review model. It neither calls a platform nor grants real access. */
  2. const SubscriptionDemo = (() => {
  3. const PLANS = {
  4. trial: { name: '两天试用', limit: 1 },
  5. starter: { name: '入门版', limit: 1 },
  6. advanced: { name: '进阶版', limit: 3 },
  7. enterprise: { name: '企业版', limit: 5 },
  8. };
  9. const STAGES = [
  10. { key: 'entitlement', title: '确认权益与容器名额', detail: '检查套餐权益,预留本次开通名额。' },
  11. { key: 'cloud', title: '准备独立云资源', detail: '配置独立后端、云函数与专属资源凭据。' },
  12. { key: 'matrix', title: '建立通信身份', detail: '创建归属于你的 Matrix 身份与会话通道。' },
  13. { key: 'container', title: '创建并初始化容器', detail: '准备平台容器,安装已验证的运行环境。' },
  14. { key: 'connectivity', title: '接入协作空间', detail: '验证身份与房间,完成通信接入。' },
  15. { key: 'verification', title: '完成可用性核验', detail: '核对真实回复、完成信号和新鲜心跳。' },
  16. ];
  17. const scopeFields = ['plan', 'access', 'trialClaimed', 'trialStartedAt', 'trialExpiresAt', 'containers', 'addon', 'application', 'job', 'jobSequence'];
  18. const captureScope = (state) => Object.fromEntries(scopeFields.map((key) => [key, JSON.parse(JSON.stringify(state[key]))]));
  19. function switchSubject(state, subject) {
  20. if (state.activeSubject === subject) return;
  21. const account = state[subject + 'Account'];
  22. if (!account) return;
  23. state[state.activeSubject + 'Account'] = captureScope(state);
  24. for (const key of scopeFields) state[key] = JSON.parse(JSON.stringify(account[key]));
  25. state.activeSubject = subject;
  26. state.company = subject === 'company';
  27. }
  28. const seedContainer = (index, company = false) => ({ id: (company ? 'demo-company-container-' : 'demo-container-') + index, name: ['我的协作伙伴', '内容助手', '研究助手', '项目伙伴', '知识助手'][index - 1] || '新的协作伙伴', owner: company ? '示例企业' : '我', company, ready: true });
  29. function initial(scenario = 'new') {
  30. 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: '' };
  31. if (['advanced', 'quota', 'enterprise'].includes(scenario)) {
  32. if (scenario === 'enterprise') { state.personalAccount = captureScope(state); state.activeSubject = 'company'; }
  33. state.plan = scenario === 'enterprise' ? 'enterprise' : 'advanced';
  34. state.company = scenario === 'enterprise';
  35. state.access = 'active';
  36. state.view = 'containers';
  37. state.containers = Array.from({ length: scenario === 'quota' ? 3 : scenario === 'enterprise' ? 2 : 1 }, (_, index) => seedContainer(index + 1, state.company));
  38. }
  39. if (scenario === 'expired') {
  40. state.plan = 'trial'; state.trialClaimed = true; state.access = 'expired'; state.view = 'expired';
  41. state.trialStartedAt = state.now - 48 * 3600000; state.trialExpiresAt = state.now;
  42. state.containers = [seedContainer(1)];
  43. }
  44. if (['waiting', 'failed'].includes(scenario)) {
  45. const started = reduce(state, { type: 'start', name: '我的第一个容器', plan: 'trial' });
  46. started.job.stage = 3; started.job.completed = 3; started.job.status = scenario === 'failed' ? 'failed' : 'provisioning';
  47. started.now += 6 * 60000; started.job.updatedAt = started.now;
  48. started.job.error = scenario === 'failed' ? '运行环境初始化暂时失败,平台保留了已完成的资源和任务进度。' : null;
  49. return started;
  50. }
  51. return state;
  52. }
  53. const limit = (state) => state.plan ? PLANS[state.plan].limit + (['advanced', 'enterprise'].includes(state.plan) ? state.addon : 0) : 0;
  54. const reserved = (state) => state.job && state.job.status !== 'ready' ? 1 : 0;
  55. const used = (state) => state.containers.length;
  56. const hasAccess = (state) => state.access === 'active';
  57. const canCreate = (state) => hasAccess(state) && !reserved(state) && used(state) < limit(state);
  58. const clone = (state) => JSON.parse(JSON.stringify(state));
  59. function reduce(previous, action) {
  60. const s = clone(previous);
  61. s.note = '';
  62. if (action.type === 'subject') {
  63. switchSubject(s, action.subject);
  64. s.view = s.access === 'expired' ? 'expired' : action.view || 'containers';
  65. return s;
  66. }
  67. if (action.type === 'view') {
  68. s.view = action.view;
  69. if (s.access === 'expired' && ['containers', 'access', 'chat'].includes(s.view)) s.view = 'expired';
  70. return s;
  71. }
  72. if (action.type === 'offline') { s.offline = !s.offline; s.note = s.offline ? '网络连接已断开' : '网络已恢复'; return s; }
  73. if (s.offline && ['start', 'apply', 'retry', 'addon'].includes(action.type)) { s.note = '网络暂时不可用,请重试。'; return s; }
  74. if (action.type === 'start') {
  75. if (s.job && s.job.status !== 'ready') { s.view = 'waiting'; s.note = '正在开通'; return s; }
  76. if (action.plan === 'trial') {
  77. if (s.trialClaimed) { s.note = '试用已领取'; return s; }
  78. s.plan = 'trial'; s.trialClaimed = true;
  79. } else if (!canCreate(s) && s.access !== 'unopened') { s.note = '名额不足'; return s; }
  80. s.jobSequence += 1;
  81. 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 };
  82. if (!s.containers.length) s.access = 'provisioning';
  83. s.view = 'waiting';
  84. }
  85. if (action.type === 'advance' && s.job && !['ready', 'failed'].includes(s.job.status)) {
  86. s.now += 90000; s.job.updatedAt = s.now; s.job.delayed = false;
  87. if (s.job.stage < STAGES.length - 1) {
  88. s.job.stage += 1; s.job.completed = s.job.stage;
  89. s.job.status = s.job.stage === STAGES.length - 1 ? 'verifying' : 'provisioning';
  90. } else {
  91. s.job.status = 'ready'; s.job.completed = STAGES.length;
  92. const container = { ...seedContainer(s.containers.length + 1, s.company), name: s.job.name };
  93. s.containers.push(container); s.job.agentId = container.id; s.access = 'active';
  94. if (s.plan === 'trial' && s.trialStartedAt === null) { s.trialStartedAt = s.now; s.trialExpiresAt = s.now + 48 * 3600000; }
  95. s.note = '已就绪';
  96. }
  97. }
  98. if (action.type === 'fail' && s.job && s.job.status !== 'ready') { s.job.status = 'failed'; s.job.error = '当前步骤执行失败。平台已保留完成的资源与原任务,可安全重试。'; s.job.updatedAt = s.now; }
  99. if (action.type === 'slow' && s.job && !['ready', 'failed'].includes(s.job.status)) { s.now += 45 * 60000; s.job.delayed = true; }
  100. 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 = '正在重试'; }
  101. if (action.type === 'apply') {
  102. if (action.plan === 'enterprise' && s.activeSubject !== 'company') { s.view = 'enterprise'; s.note = '请先核实企业身份,企业权益与个人权益分别管理。'; return s; }
  103. if (s.activeSubject === 'company' && action.plan !== 'enterprise') switchSubject(s, 'personal');
  104. if (s.access === 'active' && s.plan !== 'trial' && PLANS[action.plan].limit < limit(s)) { s.note = '更换套餐请联系客服。'; return s; }
  105. s.application = { kind: s.access === 'expired' ? 'restore' : 'plan', plan: action.plan, status: 'pending' }; s.note = '申请已提交';
  106. }
  107. if (action.type === 'addon') {
  108. if (!['advanced', 'enterprise'].includes(s.plan)) { s.note = '请先升级套餐'; return s; }
  109. s.application = { kind: 'addon', quantity: action.quantity, status: 'pending' }; s.note = '申请已提交';
  110. }
  111. if (action.type === 'approve' && s.application?.status === 'pending') {
  112. const application = s.application; s.application = null;
  113. if (application.kind === 'addon') { s.addon += application.quantity; s.note = '名额已更新'; }
  114. else {
  115. const hadContainer = s.containers.length > 0;
  116. s.plan = application.plan;
  117. s.access = 'active';
  118. if (hadContainer) { s.view = 'containers'; s.note = '已恢复使用'; }
  119. else return reduce(s, { type: 'start', plan: s.plan, name: '我的第一个容器' });
  120. }
  121. }
  122. if (action.type === 'expire' && s.plan === 'trial' && s.access === 'active') { s.now = s.trialExpiresAt; s.access = 'expired'; s.view = 'expired'; s.note = '试用已结束'; }
  123. if (action.type === 'join' && s.view === 'enterprise') {
  124. if (s.activeSubject === 'company') return s;
  125. 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 };
  126. switchSubject(s, 'company'); s.view = 'enterprise'; s.note = '已加入企业';
  127. }
  128. if (action.type === 'rename') { const target = s.containers.find((item) => item.id === action.id); if (target && hasAccess(s)) target.name = action.name; }
  129. return s;
  130. }
  131. return { PLANS, STAGES, initial, reduce, limit, reserved, used, hasAccess, canCreate };
  132. })();