onboarding-cloud-patch.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /** Surgical source transform shared by local preview and the single-function deployment. */
  2. import fs from 'node:fs';
  3. import assert from 'node:assert/strict';
  4. import vm from 'node:vm';
  5. export const BEGIN = '// FMODE_SELF_HOSTED_ONBOARDING_BEGIN';
  6. export const END = '// FMODE_SELF_HOSTED_ONBOARDING_END';
  7. export function patchOnboarding(source, bundle) {
  8. assert.match(bundle.url, /^https:\/\/s3\.fmode\.cn\//);
  9. assert.match(bundle.sha256, /^[a-f0-9]{64}$/);
  10. const newline = source.includes('\r\n') ? '\r\n' : '\n';
  11. const helper = fs.readFileSync(new URL('../cloud/onboarding/prompt.js', import.meta.url), 'utf8').trim().replace(/\r?\n/g, newline);
  12. const owned = [BEGIN, 'var CONTAINER_ONBOARDING_BUNDLE = ' + JSON.stringify(bundle) + ';', helper, END, ''].join(newline);
  13. if (source.includes(BEGIN)) {
  14. const start = source.indexOf(BEGIN), end = source.indexOf(END, start);
  15. assert.ok(end > start && source.indexOf(BEGIN, start + BEGIN.length) < 0, '已有提示词区域不明确');
  16. source = source.slice(0, start) + owned + source.slice(end + END.length).replace(/^\r?\n/, '');
  17. } else {
  18. const anchor = "var CLASS_JOB = 'FmodeProvisionJob';";
  19. assert.equal(source.split(anchor).length, 2, '云函数头部结构发生变化,停止定向更新');
  20. source = source.replace(anchor, owned + newline + anchor);
  21. }
  22. // 2026-12-03:提示词调用改为携带容器专用凭证与有效期(多行形式,正则匹配)
  23. const callRe = /^\s*var instruction = _buildSelfHostedInstruction\(oa\.id,[\s\S]*?CONTAINER_ONBOARDING_BUNDLE\);$/m;
  24. /** 提示词数组结尾标记:源码里是 ] . join ( ' 反斜杠n ' ) */
  25. const JOIN_MARK = "].join('" + String.fromCharCode(92) + "n');";
  26. const start = source.indexOf(' var mxPass = derivedPass ||');
  27. /* 旧版内联提示词片段(mxPass + 数组)只做删除;新调用保持原样 */
  28. if (start >= 0) {
  29. const end = source.indexOf(JOIN_MARK, start);
  30. assert.ok(end > start && source.slice(start, end).includes('【Fmode 接入提示词】'), '原试用提示词片段不匹配,停止更新');
  31. source = source.slice(0, start) + source.slice(end + JOIN_MARK.length);
  32. }
  33. assert.ok(callRe.test(source), '没有找到已知试用接入分支');
  34. if (!source.includes('onboardingBundle: CONTAINER_ONBOARDING_BUNDLE')) {
  35. const marker = " mode: 'onboarding',";
  36. assert.equal(source.split(marker).length, 2, '试用响应结构发生变化');
  37. source = source.replace(marker, marker + newline + ' onboardingBundle: CONTAINER_ONBOARDING_BUNDLE,');
  38. }
  39. new vm.Script(source);
  40. return source;
  41. }