@anytowork/cli 0.12.60 → 0.12.62

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anytowork/cli",
3
- "version": "0.12.60",
3
+ "version": "0.12.62",
4
4
  "description": "AnytoWork 平台命令行工具——让任何能执行 shell 命令的 Agent 用一条命令接入 AnytoWork:登录、发单接单、管理能力。",
5
5
  "type": "module",
6
6
  "bin": {
package/src/format.js CHANGED
@@ -1112,7 +1112,7 @@ export function formatPoll(data, { json = false, isFirstRun = false, activeTasks
1112
1112
  for (const extra of hireEventLines(e)) lines.push(extra);
1113
1113
  }
1114
1114
  if (activeTasks && activeTasks.length > 0) {
1115
- lines.push(`你手上还有 ${activeTasks.length} 个进行中的任务(别让新事件把它们挤掉,长活放后台、定期 \`anytowork task heartbeat\` 上报):`);
1115
+ lines.push(`你手上还有 ${activeTasks.length} 个进行中的任务(别让新事件把它们挤掉,长活在被唤起的这次运行里做完、定期 \`anytowork task heartbeat\` 上报):`);
1116
1116
  for (const t of activeTasks) lines.push(` - ${t.task_id} ${t.title || ''}(${t.status})`);
1117
1117
  }
1118
1118
  return lines.join('\n');
package/src/index.js CHANGED
@@ -601,8 +601,9 @@ const HELP = `${BRAND.name} CLI v${PKG_VERSION}
601
601
  请求失败(网络/登录失效/限频)退出码 2
602
602
  游标自动记在本机,不用手动传,跨调用自动续接
603
603
  有时限或对方在等你的事件会标「⏰ 优先」并排在前面;
604
- 末尾附"你手上还有 N 个进行中的任务"。请把盯单一轮做短:
605
- 只发现+做最小处理,耗时长的活放后台,别堵住下一轮盯单
604
+ 末尾附"你手上还有 N 个进行中的任务"。盯单只负责拉取事件,有事件才唤起你;
605
+ 长活在被唤起的这次运行里做完,不另建调度或守护进程
606
+ / poll only fetches events and wakes you when there are some; finish long work in the run it started, add no scheduler or daemon
606
607
 
607
608
  ── 通用选项 ─────────────────────────────────────────────────
608
609
  --json 机器可读 JSON 输出(给 Agent 脚本解析)
@@ -4,10 +4,10 @@
4
4
  import fs from 'node:fs';
5
5
  import { configFromFlags, readConfig, writeConfig, readState, writeState, runsToday, statePath, configPath, cursorFile, effectiveDailyMax, LISTENER_SEEN_FRESH_MS } from './config.js';
6
6
  import { Listener, runStartTest } from './run.js';
7
- import { installAutostart, uninstallAutostart, labelOf, unitNameOf, defaultEntryPath } from './install.js';
7
+ import { installAutostart, uninstallAutostart, labelOf, unitNameOf, defaultEntryPath, homeDiffers } from './install.js';
8
8
  import { lt } from './text.js';
9
9
  import { reportListener } from '../api.js';
10
- import { resolveCredDir } from '../profile.js';
10
+ import { resolveCredDir, configRootFromEnv, profileRootFromEnv } from '../profile.js';
11
11
  import { CLI_VERSION } from '../client-version.js';
12
12
 
13
13
  // Exit code when there is no start command yet (kept apart from 1 "something happened" and 2 "login lost").
@@ -27,12 +27,26 @@ function whyText(why, env) {
27
27
  return m ? lt('why.exit', { code: m[1] }, env) : why;
28
28
  }
29
29
 
30
+ /**
31
+ * Which identity the start-at-login entry runs as: {profile, configDir, odd}.
32
+ * The entry starts without this process's environment, so it finds the login by the system home. When HOME here is not the
33
+ * system home (a sandbox, a temporary HOME), the entry would read another folder and find no login: write the folder in.
34
+ */
35
+ export function listenSlot(flags, env, deps = {}) {
36
+ const profile = flags.profile || null;
37
+ const named = env.ANYTOWORK_CONFIG_DIR || env.LINGER_CONFIG_DIR || null;
38
+ const odd = homeDiffers({ envHome: deps.envHome, loginHome: deps.loginHome });
39
+ let configDir = profile ? null : named;
40
+ if (odd) configDir = profile ? profileRootFromEnv(env) : configRootFromEnv(env);
41
+ return { profile, configDir, odd };
42
+ }
43
+
30
44
  export async function runListenInstall(p) {
31
45
  const { flags, credOpts, log, err, deps = {} } = p;
32
46
  const env = deps.env || process.env;
33
47
  if (!String(flags.exec || '').trim()) { err(lt('exec.missing', {}, env)); return EXIT_NOT_SET_UP; }
34
48
  const cfg = configFromFlags(flags);
35
- const profile = flags.profile || null;
49
+ const { profile, configDir, odd } = listenSlot(flags, env, deps);
36
50
  const dir = resolveCredDir(credOpts);
37
51
  // v0.12.40: start the command once before installing anything; a command that does not start is never installed.
38
52
  log(lt('install.trial', {}, env));
@@ -45,7 +59,7 @@ export async function runListenInstall(p) {
45
59
  } else {
46
60
  try {
47
61
  result = await (deps.installAutostart || installAutostart)({
48
- profile, configDir: profile ? null : (env.ANYTOWORK_CONFIG_DIR || env.LINGER_CONFIG_DIR || null), logFile: `${dir}/listener.log`,
62
+ profile, configDir, logFile: `${dir}/listener.log`,
49
63
  platform: deps.platform, home: deps.home, runCmd: deps.runCmd, node: deps.node, entry: deps.entry, pathEnv: deps.pathEnv,
50
64
  startNow: deps.startNow,
51
65
  });
@@ -59,6 +73,7 @@ export async function runListenInstall(p) {
59
73
  if (flags.noAutostart) { log(lt('install.noautostart', {}, env)); return 0; }
60
74
  if (failure) { err(lt('install.failed', { err: failure }, env)); return 1; }
61
75
  if (result.kind === 'none') { err(lt('install.nosupervisor', {}, env)); return 1; }
76
+ if (odd && configDir) log(lt('install.homefallback', { dir: configDir }, env));
62
77
  log(lt('install.done', {}, env));
63
78
  if (result.notes.includes('linger')) log(lt('install.linger', {}, env));
64
79
  return 0;
@@ -68,7 +83,8 @@ export async function runListenUninstall(p) {
68
83
  const { flags, credOpts, baseUrl, fetchImpl, log, deps = {} } = p;
69
84
  const env = deps.env || process.env;
70
85
  try {
71
- await (deps.uninstallAutostart || uninstallAutostart)({ profile: flags.profile || null, configDir: flags.profile ? null : (env.ANYTOWORK_CONFIG_DIR || env.LINGER_CONFIG_DIR || null), platform: deps.platform, home: deps.home, runCmd: deps.runCmd });
86
+ const slot = listenSlot(flags, env, deps);
87
+ await (deps.uninstallAutostart || uninstallAutostart)({ profile: slot.profile, configDir: slot.configDir, platform: deps.platform, home: deps.home, runCmd: deps.runCmd });
72
88
  } catch { /* nothing to undo */ }
73
89
  for (const f of [configPath(credOpts), cursorFile(credOpts), statePath(credOpts)]) { try { fs.unlinkSync(f); } catch { /* none */ } }
74
90
  try {
@@ -97,6 +97,18 @@ export function schtasksCreateArgs({ node, entry, profile, configDir }) {
97
97
  return ["/Create", "/TN", taskNameOf(slotOf(profile, configDir)), "/SC", "ONLOGON", "/TR", tr, "/F"];
98
98
  }
99
99
 
100
+ /** The login home of the system user. A start-at-login entry belongs there: the system reads it from there, whatever HOME says now. */
101
+ export function systemHomeDir() {
102
+ try { return os.userInfo().homedir || os.homedir(); } catch { return os.homedir(); }
103
+ }
104
+
105
+ /** True when HOME (what this process sees) is not the system home: a sandbox, a temporary HOME, a changed environment. */
106
+ export function homeDiffers({ envHome, loginHome } = {}) {
107
+ const a = envHome || os.homedir();
108
+ const b = loginHome || systemHomeDir();
109
+ return path.resolve(a) !== path.resolve(b);
110
+ }
111
+
100
112
  /** Default way to run a system command; resolves {code, stdout, stderr}, never rejects. */
101
113
  export function defaultRunCmd(cmd, args, opts = {}) {
102
114
  return new Promise((resolve) => {
@@ -122,7 +134,7 @@ export function defaultEntryPath() {
122
134
  export async function installAutostart(o) {
123
135
  const platform = o.platform || process.platform;
124
136
  const runCmd = o.runCmd || defaultRunCmd;
125
- const home = o.home || os.homedir();
137
+ const home = o.home || systemHomeDir();
126
138
  const node = o.node || process.execPath;
127
139
  const entry = o.entry || defaultEntryPath();
128
140
  const pathEnv = o.pathEnv != null ? o.pathEnv : process.env.PATH;
@@ -189,7 +201,7 @@ export async function uninstallAutostart(o) {
189
201
  const slot = slotOf(o.profile, o.configDir);
190
202
  const platform = o.platform || process.platform;
191
203
  const runCmd = o.runCmd || defaultRunCmd;
192
- const home = o.home || os.homedir();
204
+ const home = o.home || systemHomeDir();
193
205
  if (platform === 'darwin') {
194
206
  const label = labelOf(slot);
195
207
  const uid = String(o.uid != null ? o.uid : (process.getuid ? process.getuid() : 501));
@@ -84,9 +84,16 @@ export function buildPrompt(payload, env = process.env, identity = null, job = n
84
84
  // so `--profile <name>` and a plain command reach the same credentials. Never ask the Agent to unset the env var.
85
85
  lines.push(lang === 'en'
86
86
  ? (pf ? `Your identity here is "${pf}"; anytowork already points at that login, so plain commands use it (anytowork --profile ${pf} chat read ${sid} means the same).`
87
- : `Your identity here lives in ${identity.dir} (ANYTOWORK_CONFIG_DIR=${identity.dir} is already set); run anytowork commands as they are, no --profile needed.`)
87
+ : `Your identity here lives in ${identity.dir}; run anytowork commands with ANYTOWORK_CONFIG_DIR=${identity.dir} set, no --profile needed.`)
88
88
  : (pf ? `你在这台电脑上的身份是「${pf}」;anytowork 已指向这份登录,直接运行命令即可;写成 anytowork --profile ${pf} chat read ${sid} 也等价。`
89
- : `你在这台电脑上的身份放在 ${identity.dir}(已设好 ANYTOWORK_CONFIG_DIR=${identity.dir}),命令直接运行,不用加 --profile。`));
89
+ : `你在这台电脑上的身份放在 ${identity.dir};运行 anytowork 命令时要带上 ANYTOWORK_CONFIG_DIR=${identity.dir},不用加 --profile。`));
90
+ // The listener sets the variable for the process it starts, but a program that runs you in a separate service does not pass it on:
91
+ // when the command answers "not logged in" or shows another identity, set it yourself for every command.
92
+ if (identity.dir && pf) {
93
+ lines.push(lang === 'en'
94
+ ? `If a command says you are not logged in or shows another identity, your process did not inherit the environment: set ANYTOWORK_CONFIG_DIR=${identity.dir} for every anytowork command.`
95
+ : `命令若提示未登录或显示成别的身份,说明你的运行环境没有继承环境变量:给每条 anytowork 命令设上 ANYTOWORK_CONFIG_DIR=${identity.dir}。`);
96
+ }
90
97
  }
91
98
  // v0.38.39: the wake-up number, ONE fixed line at the very end, the same for every program. Usage is matched to this wake-up by it.
92
99
  const label = typeof p.wake_label === 'string' && p.wake_label.trim() ? p.wake_label.trim() : (typeof p.wake_id === 'string' && /^[0-9a-f-]{36}$/i.test(p.wake_id) ? wakeLabelLine(p.wake_id) : '');
@@ -188,8 +188,8 @@ export class Listener {
188
188
  /** Who this listener is when it is not the computer's default identity (named in the prompt, see buildPrompt). */
189
189
  identity() {
190
190
  const profile = this.credOpts && this.credOpts.profile ? this.credOpts.profile : null;
191
- if (profile) return { profile };
192
191
  const named = String(this.env.ANYTOWORK_CONFIG_DIR || this.env.LINGER_CONFIG_DIR || '').trim();
192
+ if (profile) return named ? { profile, dir: this.credDir } : { profile };
193
193
  return named ? { dir: this.credDir } : null;
194
194
  }
195
195
 
@@ -34,6 +34,11 @@ const TEXT = {
34
34
  zh: 'ℹ 这台机器上开机自启装不上(系统服务和开机定时项都不可用),方案 A 不可用。先运行 anytowork listen uninstall,再改用方案 B:每 1 分钟运行一次 anytowork poll,有新事件才启动模型。做法:https://a2a.linger.chimap.cn/docs/en/guide/connect#plan-b',
35
35
  en: 'ℹ Start at login cannot be installed on this machine (neither the service manager nor a boot-time schedule entry is available), so Plan A is unavailable. Run anytowork listen uninstall, then use Plan B: anytowork poll every minute, starting the model only when there are new events. Steps: https://a2a.linger.chimap.cn/docs/en/guide/connect#plan-b',
36
36
  },
37
+ // v0.12.61: HOME here is not the system home, so the start-at-login entry is given the login folder.
38
+ 'install.homefallback': {
39
+ zh: 'ℹ 当前 HOME 与系统用户主目录不同:已把登录目录 {dir} 写进启动项,监听用它找登录,不会去读默认目录。',
40
+ en: 'ℹ HOME here is not the system home: the login folder {dir} is written into the start-at-login entry, so the listener finds your login there, not in the default folder.',
41
+ },
37
42
  'install.linger': {
38
43
  zh: '想让它在电脑开机后、没人登录时也运行:loginctl enable-linger',
39
44
  en: 'To keep it running at boot even when nobody is logged in: loginctl enable-linger',