ai_co_discuss 0.4.44 → 0.4.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -169,7 +169,13 @@ npx wrangler deploy --config deploy/updates.wrangler.jsonc
169
169
  npx wrangler deploy --config deploy/entry.wrangler.jsonc
170
170
  ```
171
171
 
172
- 它有自己的 `workers.dev` 地址(如 `https://codiscuss-entry.<账号子域>.workers.dev`),与线上站点不同源,托管账号的网页碰不到你已配对设备的密钥。托管电脑绑定线上站点后,在管理面板“概览 → 托管 → 登录入口”的“外网访问”选“线上中转”,填这个地址;面板给出的链接带电脑编号(`?m=…`),发给使用者即可。使用者的请求经入口 Worker 和中转到达这台电脑的登录入口,登录、会话与入口保护都在电脑上;Cloudflare 能看到这些往来内容(与自有域名隧道相同),每个页面的请求都计入 Workers 免费额度。
172
+ 它有自己的 `workers.dev` 地址(如 `https://codiscuss-entry.<账号子域>.workers.dev`),与线上站点不同源,托管账号的网页碰不到你已配对设备的密钥。把这个地址写进私有配置的 `vars`,重新部署 Pages,线上站点就会把它告诉来接入的托管电脑:
173
+
174
+ ```json
175
+ "vars": { "ENTRY_URL": "https://codiscuss-entry.<账号子域>.workers.dev" }
176
+ ```
177
+
178
+ 接入一台托管电脑:在你(部署线上站点的电脑)的管理面板“概览 → 托管 → 登录入口”选“线上中转”,展开“生成接入码”,填部署配置路径与站点地址后生成(需已登录 wrangler;一次性,再生成会让未用过的作废);把接入码发给托管电脑,粘贴到它的同一个窗口点“接入并切换”,它会绑定站点、取得入口地址、启动所需服务并切换。面板给出的链接带电脑编号(`?m=…`),发给使用者即可。使用者的请求经入口 Worker 和中转到达这台电脑的登录入口,登录、会话与入口保护都在电脑上;Cloudflare 能看到这些往来内容(与自有域名隧道相同),每个页面的请求都计入 Workers 免费额度。
173
179
 
174
180
  ## 常见问题
175
181
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai_co_discuss",
3
- "version": "0.4.44",
3
+ "version": "0.4.45",
4
4
  "bin": {
5
5
  "codiscuss": "runner/codiscuss.mjs"
6
6
  },
@@ -566,7 +566,7 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
566
566
  const op = { list_accounts, add_account, reset_password, set_account_disabled, remove_account, run_instance, create_os_user, remove_os_user,
567
567
  login_status, apply_login, logout, start_login, login_progress, submit_login, cancel_login, install_runtime, prepare_account, account_env, account_skills, account_workflows, owner_env, owner_skills, owner_prompts, save_owner_prompt, shared_cli_state,
568
568
  save_api_key, list_keys, remove_key, use_shared_programs, service_overview, run_owner_service, latest_version, begin_update,
569
- autostart_enabled, set_autostart, entry_overview, start_entry, stop_entry, keep_tunnel, start_tunnel_setup, set_entry_mode, set_relay_url, connect_site, protect_account, start_verified, hand_over: () => {}, ...ops };
569
+ autostart_enabled, set_autostart, entry_overview, start_entry, stop_entry, keep_tunnel, start_tunnel_setup, set_entry_mode, set_relay_url, connect_site, make_access_code, join_relay, protect_account, start_verified, hand_over: () => {}, ...ops };
570
570
  const hosts = new Set([`127.0.0.1:${port}`, `localhost:${port}`]);
571
571
 
572
572
  async function api(method, path, body) {
@@ -644,6 +644,10 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
644
644
  if (method === 'POST' && section === 'entry' && name === 'mode') return op.set_entry_mode(data, String(body.mode || ''));
645
645
  if (method === 'POST' && section === 'entry' && name === 'relay') return op.set_relay_url(data, body.url);
646
646
  if (method === 'POST' && section === 'site' && name === 'connect') return op.connect_site(data, body.site, body.setup_key);
647
+ // The access code: made on the computer that deployed the online site, pasted on a hosting computer.
648
+ if (method === 'GET' && section === 'site' && name === 'code') return { ...deploy_settings(data), site: deploy_settings(data).site || bound_site(data)?.site || '' };
649
+ if (method === 'POST' && section === 'site' && name === 'code') return op.make_access_code(data, body);
650
+ if (method === 'POST' && section === 'relay' && name === 'join') return op.join_relay(data, body.code);
647
651
  // The outside address: a Cloudflare tunnel set up by a job of its own (it waits for the owner in the browser).
648
652
  if (section === 'tunnel' && name === 'setup') {
649
653
  if (method === 'GET') return tunnel_setup_state(data);
@@ -749,30 +753,109 @@ async function relay_status(relay_url, machine) {
749
753
  } catch { return { reachable: false, online: false }; }
750
754
  }
751
755
 
756
+ /** What stands between people outside and this computer over the relay, in the order to look at it; '' when nothing. */
757
+ function relay_problem(relay, entry_running, service_running) {
758
+ if (!relay.reachable) return '入口地址连不上:检查网络与代理是否放行 *.workers.dev';
759
+ if (!service_running) return '本机服务没有在运行,电脑连不上线上站点:在上面“本机”里启动共议服务';
760
+ if (!relay.online) return '电脑还没连上线上站点:本机服务刚启动时稍等几秒;持续如此,可能这台电脑绑定的站点与入口不属于同一个 Cloudflare 账号,重新用接入码接入';
761
+ if (!entry_running) return '登录入口没有在运行:点上面的“启动”';
762
+ return '';
763
+ }
764
+
752
765
  async function entry_overview(data) {
753
766
  const { port, public_url, tunnel, mode, relay_url } = entry_settings(data), status = await entry_status(data);
754
767
  const bound = bound_site(data);
755
768
  const relay = { url: relay_url, site: bound?.site || '', link: relay_url && bound ? `${relay_url}/?m=${bound.machine}` : '',
756
769
  ...(mode === 'relay' && relay_url && bound ? await relay_status(relay_url, bound.machine) : {}) };
770
+ if (mode === 'relay') relay.problem = relay_problem(relay, status.running, Boolean(local_status(data).running));
757
771
  // The address people open in the mode in use.
758
772
  const address = mode === 'relay' ? relay.link : mode === 'tunnel' ? public_url : '';
759
773
  return { running: status.running, port: status.running ? status.port : port, public_url, mode, address, relay,
760
774
  tunnel: tunnel ? { host: tunnel.host, name: tunnel.name, ...(await tunnel_status(data)) } : null };
761
775
  }
762
776
 
763
- /** Switch how people outside reach the entry, once what that way needs is there; the tunnel follows at once. */
777
+ /**
778
+ * Switch how people outside reach the entry, once what that way needs is there. Reaching it from outside needs the entry
779
+ * running, and the relay needs this computer's service (which holds the relay connection): both are started here rather
780
+ * than left for the first person to find a broken link. The tunnel follows at once.
781
+ */
764
782
  async function set_entry_mode(data, mode) {
765
783
  const settings = entry_settings(data);
766
784
  if (mode === 'relay') {
767
- if (!bound_site(data)) throw new Error('这台电脑还没有绑定线上站点:先在下面绑定');
768
- if (!settings.relay_url) throw new Error('还没有填写入口地址(入口 Worker 的 workers.dev 地址)');
785
+ if (!bound_site(data)) throw new Error('这台电脑还没有接入线上站点:先用接入码接入');
786
+ if (!settings.relay_url) throw new Error('还没有入口地址:先用接入码接入');
769
787
  }
770
788
  if (mode === 'tunnel' && !settings.tunnel) throw new Error('还没有设好 Cloudflare 隧道:先点“设置”填写你的域名');
771
789
  save_entry_settings(data, { mode });
790
+ if (mode !== 'local' && !(await entry_status(data)).running) { await start_entry(data); save_entry_settings(data, { running: true }); }
791
+ if (mode === 'relay' && !local_status(data).running) await run_owner_service(data, 'start');
772
792
  await keep_tunnel(data);
773
793
  return entry_overview(data);
774
794
  }
775
795
 
796
+ // Where the panel of the computer that deployed the online site remembers its deployment config and the site's address.
797
+ const DEPLOY_FILE = 'panel-deploy.json';
798
+ const BIND_MARK = '#codiscuss-bind=';
799
+
800
+ function deploy_settings(data) {
801
+ try { return JSON.parse(readFileSync(join(data, DEPLOY_FILE), 'utf8')); } catch { return {}; }
802
+ }
803
+
804
+ /** The folder above a deployment config that has wrangler installed, so npx finds it rather than downloading it. */
805
+ function wrangler_folder(config) {
806
+ for (let folder = dirname(config); ; folder = dirname(folder)) {
807
+ if (existsSync(join(folder, 'node_modules', 'wrangler'))) return folder;
808
+ if (dirname(folder) === folder) return dirname(config);
809
+ }
810
+ }
811
+
812
+ /**
813
+ * On the computer that deployed the online site: make an access code for a hosting computer, the site's address with a
814
+ * one-time binding token (`codiscuss token`, written to the site's database through wrangler, in a child process so the
815
+ * panel keeps answering). A new code makes an unused earlier one invalid. The config and site are remembered.
816
+ */
817
+ function make_access_code(data, { config, site }) {
818
+ const saved = deploy_settings(data);
819
+ const path = resolve(String(config || saved.config || '').trim() || '.');
820
+ if (!existsSync(path) || !/\.jsonc?$/.test(path)) throw new Error('找不到线上站点的部署配置:填写源码仓库里 .wrangler\\deploy\\wrangler.jsonc 的完整路径');
821
+ let origin;
822
+ try { origin = new URL(String(site || saved.site || bound_site(data)?.site || '').trim()).origin; } catch { throw new Error('站点地址格式不对,例如 https://xxx.pages.dev'); }
823
+ return new Promise((done, fail) => {
824
+ const child = spawn(process.execPath, [installed_script('codiscuss.mjs'), 'token', '--config', path], { cwd: wrangler_folder(path), windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
825
+ let output = '', errors = '';
826
+ child.stdout.on('data', chunk => { output += chunk; });
827
+ child.stderr.on('data', chunk => { errors += chunk; });
828
+ child.on('error', fail);
829
+ child.on('exit', code => {
830
+ const token = output.match(/^([0-9a-f]{32,})$/m)?.[1];
831
+ if (code !== 0 || !token) return fail(new Error(errors.trim().split(/\r?\n/).pop() || '没有生成绑定令牌'));
832
+ write_settings(join(data, DEPLOY_FILE), { config: path, site: origin });
833
+ done({ code: `${origin}/${BIND_MARK}${token}`, site: origin });
834
+ });
835
+ });
836
+ }
837
+
838
+ /**
839
+ * On a hosting computer: join the online relay with an access code, each step reported. Bind to the site (unless bound
840
+ * already), take the entry address the site gives, then switch to the relay, which starts what it needs.
841
+ */
842
+ async function join_relay(data, text) {
843
+ const value = String(text || '').trim();
844
+ const at = value.indexOf(BIND_MARK);
845
+ if (at < 0) throw new Error('接入码不对:应是部署线上站点的电脑上生成的一整串,形如 https://xxx.pages.dev/#codiscuss-bind=…');
846
+ const site = new URL(value.slice(0, at)).origin, token = value.slice(at + BIND_MARK.length);
847
+ const steps = [];
848
+ if (bound_site(data)?.site === site) steps.push(`已接入 ${site}`);
849
+ else { await connect_site(data, site, token); steps.push(`已接入 ${site}`); }
850
+ const given = await fetch(`${site}/api/entry`, { signal: AbortSignal.timeout(RELAY_CHECK_MS) }).then(answer => answer.json()).catch(() => ({}));
851
+ if (!given.entry_url) throw new Error(`${site} 没有提供入口地址:请部署线上站点的人在部署配置里设置 ENTRY_URL 并重新发布`);
852
+ await set_relay_url(data, given.entry_url);
853
+ steps.push(`入口地址 ${given.entry_url}`);
854
+ const overview = await set_entry_mode(data, 'relay');
855
+ steps.push('已切换到线上中转');
856
+ return { steps, entry: overview };
857
+ }
858
+
776
859
  /** Save the relay's entry address after checking that it answers as one. */
777
860
  async function set_relay_url(data, url) {
778
861
  const origin = String(url || '').trim().replace(/\/+$/, '');