@anytowork/cli 0.12.39 → 0.12.41
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 +1 -1
- package/src/api.js +10 -0
- package/src/chat-commands.js +4 -1
- package/src/index.js +7 -0
- package/src/listener/commands.js +5 -2
- package/src/listener/config.js +12 -0
- package/src/listener/text.js +8 -0
- package/src/login-hint.js +51 -0
- package/src/reply-hint.js +21 -0
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
// 重连都在自杀重生,见下方 doRefreshToken 内详细说明)
|
|
29
29
|
|
|
30
30
|
import { BRAND } from "./brand.js";
|
|
31
|
+
import { withLoginHint } from './login-hint.js';
|
|
31
32
|
import { randomUUID } from 'node:crypto';
|
|
32
33
|
import { loadCredentials, saveCredentials } from './credentials.js';
|
|
33
34
|
import { clientHeaders, noteServerVersion, UPGRADE_COMMAND } from './client-version.js';
|
|
@@ -294,6 +295,15 @@ function persistRefreshedCreds(creds, newTokens, credOpts) {
|
|
|
294
295
|
* @returns {Promise<object>} 解析后的 JSON
|
|
295
296
|
*/
|
|
296
297
|
async function authedRequestWithRefresh(baseUrl, credOpts, httpOpts, makeRequest) {
|
|
298
|
+
try {
|
|
299
|
+
return await authedRequestInner(baseUrl, credOpts, httpOpts, makeRequest);
|
|
300
|
+
} catch (e) {
|
|
301
|
+
// v0.12.41: a lost login also says which login was read (several Agents on one computer)
|
|
302
|
+
throw withLoginHint(e, credOpts);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function authedRequestInner(baseUrl, credOpts, httpOpts, makeRequest) {
|
|
297
307
|
const fetchImpl = httpOpts.fetchImpl || fetch;
|
|
298
308
|
|
|
299
309
|
// 读当前凭证(已登录才能续)
|
package/src/chat-commands.js
CHANGED
|
@@ -23,6 +23,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
23
23
|
import { authedGetWithRefresh, authedPostWithRefresh, authedPatchWithRefresh } from './api.js';
|
|
24
24
|
import { EXIT_HUMAN_REQUIRED, EXIT_NEW_MESSAGES, isHumanRequiredError, humanRequiredHint } from './exit-reasons.js';
|
|
25
25
|
import { setupHintLines } from './setup-hints.js';
|
|
26
|
+
import { replyHintLine, hasUnhandledFromOthers } from './reply-hint.js';
|
|
26
27
|
import { renderWbRules, boundaryLines, identityText } from './wb-rules-text.js';
|
|
27
28
|
import { readCurrentSession, setCurrentSession, touchCurrentSession, clearCurrentSession, readCurrentHire } from './chat-state.js';
|
|
28
29
|
|
|
@@ -336,7 +337,9 @@ async function runRead(c, sid) {
|
|
|
336
337
|
// Reading new messages makes this the current session; looking back (--before) only keeps the clock alive.
|
|
337
338
|
if (before == null) setCurrentSession(data.current_session || sid, c.credOpts, c.nowMs(), data.hire_id || null);
|
|
338
339
|
else touchCurrentSession(c.credOpts, c.nowMs());
|
|
339
|
-
|
|
340
|
+
// v0.12.41: unhandled messages from others and no local listener: one platform line, last
|
|
341
|
+
const hint = !c.json && before == null && hasUnhandledFromOthers(data.messages) ? replyHintLine(c.credOpts, c.env, c.nowMs()) : '';
|
|
342
|
+
c.log(c.json ? JSON.stringify(data, null, 2) : [formatRead(data, sid, c.env), newsLine(data), hint].filter(Boolean).join('\n'));
|
|
340
343
|
return 0;
|
|
341
344
|
}
|
|
342
345
|
|
package/src/index.js
CHANGED
|
@@ -112,6 +112,8 @@ import {
|
|
|
112
112
|
formatCommandError,
|
|
113
113
|
} from './format.js';
|
|
114
114
|
import { readCursor, writeCursor, readNextPollAt, writeNextPollAt } from './poll-state.js';
|
|
115
|
+
import { replyHintLine } from './reply-hint.js';
|
|
116
|
+
import { setHintEnv } from './login-hint.js';
|
|
115
117
|
import { selectedTaskIds, autoAcceptSelected, formatAutoAccept } from './auto-accept.js';
|
|
116
118
|
import { resolveIntroInput, PROFILE_INTRO_MAX_CHARS, PROFILE_INTRO_LENGTH_HINT, codePointLength } from './profile-intro-input.js';
|
|
117
119
|
import { runMediaUpload, prepareIntroMedia } from './media-commands.js';
|
|
@@ -739,6 +741,7 @@ export async function run(argv, deps = {}) {
|
|
|
739
741
|
const log = deps.log || console.log;
|
|
740
742
|
const err = deps.err || console.error;
|
|
741
743
|
const env = deps.env || process.env;
|
|
744
|
+
setHintEnv(env);
|
|
742
745
|
const credDir = deps.credDir; // undefined → 用默认 ~/.anytowork(或 --profile 目录)
|
|
743
746
|
const fetchImpl = deps.fetchImpl; // undefined → api 函数用全局 fetch
|
|
744
747
|
|
|
@@ -1931,6 +1934,10 @@ export async function run(argv, deps = {}) {
|
|
|
1931
1934
|
if (text) log(text); // 无事件非 --json 时 text 为空字符串 → 不调用 log,保证 stdout 真正为空
|
|
1932
1935
|
// v0.37: the platform's "settings after connecting" reminder (at most once a day per item, decided by the platform)
|
|
1933
1936
|
if (!flags.json) { const hint = setupHintLines(data.setup_hint, env); if (hint.length) log(hint.join('\n')); }
|
|
1937
|
+
// v0.12.41: a workbench message is waiting and no local listener is running: one platform line about replying
|
|
1938
|
+
if (!flags.json && (data.events || []).some((e) => e && e.event_type === 'wb.wake')) {
|
|
1939
|
+
const rh = replyHintLine(credOpts, env, nowMs); if (rh) log(rh);
|
|
1940
|
+
}
|
|
1934
1941
|
|
|
1935
1942
|
return ((data.events || []).length > 0 || (data.pending_confirms || []).length > 0) ? 1 : 0;
|
|
1936
1943
|
} catch (e) {
|
package/src/listener/commands.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// There is no consent step and no switch: installing it is the standard way to connect (decision 11-15).
|
|
3
3
|
|
|
4
4
|
import fs from 'node:fs';
|
|
5
|
-
import { configFromFlags, readConfig, writeConfig, readState, writeState, runsToday, statePath, configPath, cursorFile, effectiveDailyMax } from './config.js';
|
|
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
7
|
import { installAutostart, uninstallAutostart, labelOf, unitNameOf, defaultEntryPath } from './install.js';
|
|
8
8
|
import { lt } from './text.js';
|
|
@@ -34,6 +34,10 @@ export async function runListenInstall(p) {
|
|
|
34
34
|
const cfg = configFromFlags(flags);
|
|
35
35
|
const profile = flags.profile || null;
|
|
36
36
|
const dir = resolveCredDir(credOpts);
|
|
37
|
+
// v0.12.40: start the command once before installing anything; a command that does not start is never installed.
|
|
38
|
+
log(lt('install.trial', {}, env));
|
|
39
|
+
const trial = await runStartTest({ cfg, credOpts, deps, env });
|
|
40
|
+
if (!trial.ok) { err(lt('install.trialfail', { why: whyText(trial.why, env) }, env)); return 1; }
|
|
37
41
|
let result = { kind: 'none', notes: [] };
|
|
38
42
|
let failure = null;
|
|
39
43
|
if (flags.noAutostart) {
|
|
@@ -86,7 +90,6 @@ export async function runListenTest(p) {
|
|
|
86
90
|
return 1;
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
const LISTENER_SEEN_FRESH_MS = 2 * 60 * 1000;
|
|
90
93
|
|
|
91
94
|
export async function runListenStatus(p) {
|
|
92
95
|
const { flags, credOpts, log, deps = {} } = p;
|
package/src/listener/config.js
CHANGED
|
@@ -131,6 +131,18 @@ export function readState(credOpts) {
|
|
|
131
131
|
return { ...emptyState(), ...(readJson(statePath(credOpts)) || {}) };
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
/** The running listener touches its state file every 30-60 s; a stopped one writes 0 on a clean stop. */
|
|
135
|
+
export const LISTENER_SEEN_FRESH_MS = 2 * 60 * 1000;
|
|
136
|
+
|
|
137
|
+
/** Is a listener installed here and seen recently? Anything unclear counts as "not running". */
|
|
138
|
+
export function listenerIsRunning(credOpts, nowMs = Date.now()) {
|
|
139
|
+
try {
|
|
140
|
+
const cfg = readConfig(credOpts);
|
|
141
|
+
const st = readState(credOpts);
|
|
142
|
+
return !!cfg && st.seenAt > 0 && nowMs - st.seenAt < LISTENER_SEEN_FRESH_MS;
|
|
143
|
+
} catch { return false; }
|
|
144
|
+
}
|
|
145
|
+
|
|
134
146
|
export function writeState(st, credOpts) {
|
|
135
147
|
try { atomicWriteFile(statePath(credOpts), JSON.stringify(st), 0o600); } catch { /* best effort */ }
|
|
136
148
|
}
|
package/src/listener/text.js
CHANGED
|
@@ -13,6 +13,14 @@ const TEXT = {
|
|
|
13
13
|
zh: '这个身份还没有装常驻监听。先运行:anytowork listen install --exec "<启动你自己的一条命令>"',
|
|
14
14
|
en: 'No always-on listener is set up for this identity yet. Run first: anytowork listen install --exec "<the one command that starts you>"',
|
|
15
15
|
},
|
|
16
|
+
'install.trial': {
|
|
17
|
+
zh: '安装前先试启动一次你的命令……',
|
|
18
|
+
en: 'Starting your command once before installing...',
|
|
19
|
+
},
|
|
20
|
+
'install.trialfail': {
|
|
21
|
+
zh: '启动命令没跑通:{why}。常驻监听没有安装。请修好命令后重新运行 anytowork listen install --exec "<启动你自己的一条命令>";确实没有这样的命令,改用 anytowork listen(后台任务)或每 1 分钟 anytowork poll。',
|
|
22
|
+
en: 'The start command did not work: {why}. The always-on listener was not installed. Fix the command and run anytowork listen install --exec "<the one command that starts you>" again; if you have no such command, use anytowork listen (background task) or anytowork poll every minute.',
|
|
23
|
+
},
|
|
16
24
|
'install.done': {
|
|
17
25
|
zh: '常驻监听已装好并开始运行。收到消息会立刻叫醒你。接下来运行 anytowork listen test 试一次,把结果告诉主人。',
|
|
18
26
|
en: 'The always-on listener is set up and running. You will be woken as soon as a message arrives. Next run anytowork listen test once and tell your owner the result.',
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// v0.12.41: when the login is lost, say WHICH login was read. On a computer with several Agents, an Agent that
|
|
2
|
+
// forgot --profile reads the default folder, which may hold somebody else's expired login.
|
|
3
|
+
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { resolveCredDir } from './profile.js';
|
|
6
|
+
import { loadCredentials } from './credentials.js';
|
|
7
|
+
import { pickLang } from './model-pledge.js';
|
|
8
|
+
|
|
9
|
+
let hintEnv = process.env;
|
|
10
|
+
/** run() tells us the environment it was started with (language). */
|
|
11
|
+
export function setHintEnv(env) { hintEnv = env || process.env; }
|
|
12
|
+
|
|
13
|
+
const TEXT = {
|
|
14
|
+
zh: (dir, who) => `读取的登录:${dir}(${who})。如果这台电脑有多个 Agent,请用 --profile 或 ANYTOWORK_CONFIG_DIR 指到你自己的那份。`,
|
|
15
|
+
en: (dir, who) => `Login read: ${dir} (${who}). If this computer runs several Agents, point --profile or ANYTOWORK_CONFIG_DIR at your own.`,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function nameFromToken(token) {
|
|
19
|
+
try {
|
|
20
|
+
const b = String(token || '').split('.')[1];
|
|
21
|
+
return b ? JSON.parse(Buffer.from(b, 'base64url').toString('utf8')).agent_name || null : null;
|
|
22
|
+
} catch { return null; }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Who the local login says it is: a saved name, else the profile folder name, else the Agent id, else "default". */
|
|
26
|
+
function identityOf(creds, dir, lang) {
|
|
27
|
+
const named = creds && (creds.agent_name || nameFromToken(creds.access_token));
|
|
28
|
+
if (named) return named;
|
|
29
|
+
if (path.basename(path.dirname(dir)) === 'profiles') return path.basename(dir);
|
|
30
|
+
if (creds && creds.agent_id) return String(creds.agent_id);
|
|
31
|
+
return lang === 'en' ? 'default login' : '默认登录';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The extra line for a lost login. */
|
|
35
|
+
export function loginHintLine(credOpts = {}) {
|
|
36
|
+
const lang = pickLang(hintEnv);
|
|
37
|
+
let dir = '';
|
|
38
|
+
try { dir = resolveCredDir(credOpts); } catch { dir = ''; }
|
|
39
|
+
let creds = null;
|
|
40
|
+
try { creds = loadCredentials(credOpts); } catch { creds = null; }
|
|
41
|
+
return TEXT[lang](dir, identityOf(creds, dir, lang));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Add the line to a lost-login error (once). Other errors pass through untouched. */
|
|
45
|
+
export function withLoginHint(e, credOpts) {
|
|
46
|
+
if (e && e.authExpired && typeof e.message === 'string' && !e.loginHinted) {
|
|
47
|
+
e.message = `${e.message}\n${loginHintLine(credOpts)}`;
|
|
48
|
+
e.loginHinted = true;
|
|
49
|
+
}
|
|
50
|
+
return e;
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// v0.12.41: one platform line after unhandled workbench messages, only when no local listener is running.
|
|
2
|
+
// (With a listener running, the platform wakes the Agent itself and this line is not needed.)
|
|
3
|
+
|
|
4
|
+
import { pickLang } from './model-pledge.js';
|
|
5
|
+
import { listenerIsRunning } from './listener/config.js';
|
|
6
|
+
|
|
7
|
+
const TEXT = {
|
|
8
|
+
zh: '回复要有实质内容:能当场做完就做完;做不完就如实告诉对方,并说明接下来怎么做。只回「收到」不算回复。',
|
|
9
|
+
en: 'Reply with substance: finish it now if you can; if you can\'t, tell them so, say what you will do next. A bare "received" is not a reply.',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** The hint line, or '' when the listener is running (or cannot be told: then it is treated as not running). */
|
|
13
|
+
export function replyHintLine(credOpts, env = process.env, nowMs = Date.now()) {
|
|
14
|
+
if (listenerIsRunning(credOpts, nowMs)) return '';
|
|
15
|
+
return TEXT[pickLang(env)];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Messages in a `chat read` answer that someone else wrote and nobody recalled. */
|
|
19
|
+
export function hasUnhandledFromOthers(messages) {
|
|
20
|
+
return (messages || []).some((m) => m && m.kind !== 'system' && !m.recalled && m.sender && !m.sender.is_me && m.sender.type !== 'system');
|
|
21
|
+
}
|