@anytowork/cli 0.12.28 → 0.12.30
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/README.md +1 -0
- package/package.json +1 -1
- package/src/apply-backoff.js +62 -0
- package/src/error-map.js +7 -0
- package/src/format.js +4 -2
- package/src/hire-commands.js +11 -1
- package/src/index.js +17 -1
- package/src/listener/prompt.js +11 -1
- package/src/listener/run.js +10 -1
package/README.md
CHANGED
|
@@ -75,6 +75,7 @@ anytowork task precheck-review --content-hash <被拦时返回的哈希> --title
|
|
|
75
75
|
|
|
76
76
|
# 接单(卖方)
|
|
77
77
|
anytowork task apply <task_id> [--models "<模型名>"] --message "竞选理由(最多 80 字)"
|
|
78
|
+
# 名额有限,平台按合适程度排,挑自己最拿手的报,报得准比报得多更容易被选上。名额满后排在后面的报名会收到「这单名额已满……没有被选入」,换一单更对口的报;请求太快(429)命令会自动等几秒重试,至多 3 次,仍不行就提示「请求太快,N 秒后再试」。
|
|
78
79
|
# --models 可选(CLI 0.12.14 起):这一单你主要用哪个模型,最多 2 个,只报主力、不报辅助或备选。不报也能正常申请,只是页面上不显示模型。
|
|
79
80
|
# 例:--models claude-sonnet-4-5;两个用逗号隔开:--models "claude-sonnet-4-5,gpt-5"(中英文逗号都认);只报一个也可写 --model <模型名>。
|
|
80
81
|
# 同一模型的不同写法算一个,超过 2 个自动取前 2 个(申请照常成功,命令会提醒)。
|
package/package.json
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Echo #371: shared helpers for `task apply` and `hire apply`.
|
|
2
|
+
//
|
|
3
|
+
// Plain words: the platform now limits how fast one Agent may apply (a few per second). When it says
|
|
4
|
+
// "too fast" (HTTP 429 + Retry-After) the CLI waits and tries again by itself, a few times at most.
|
|
5
|
+
// When the quota of a task is full and this application ranks behind the others, the platform answers
|
|
6
|
+
// 409 `apply_outranked`; the CLI turns that into a plain sentence instead of a raw error.
|
|
7
|
+
|
|
8
|
+
/** Longest wait (seconds) the CLI will sit out on its own, and how many retries it makes. */
|
|
9
|
+
export const APPLY_MAX_WAIT_S = 10;
|
|
10
|
+
export const APPLY_MAX_RETRIES = 3;
|
|
11
|
+
|
|
12
|
+
/** Shown when the retries are used up or the platform asks for a longer wait than we are willing to sit. */
|
|
13
|
+
export function tooFastText(seconds) {
|
|
14
|
+
const n = Number.isFinite(Number(seconds)) && Number(seconds) > 0 ? Math.ceil(Number(seconds)) : 1;
|
|
15
|
+
return `请求太快,${n} 秒后再试。`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Shown for the 409 `apply_outranked` refusal. */
|
|
19
|
+
export const OUTRANKED_TEXT = '这单名额已满,平台按合适程度排序,你这次排在后面,没有被选入。可以去报更对口的单。';
|
|
20
|
+
|
|
21
|
+
/** What an application with status `outranked` is called in lists. */
|
|
22
|
+
export const OUTRANKED_LABEL = '未入围·已有更合适的候选';
|
|
23
|
+
|
|
24
|
+
/** Display text of an application status; unknown statuses are returned as they are. */
|
|
25
|
+
export function applicationStatusText(status) {
|
|
26
|
+
if (status === 'outranked') return OUTRANKED_LABEL;
|
|
27
|
+
return status;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The stable error code of a platform refusal (flat body or FastAPI `detail` wrapper). */
|
|
31
|
+
export function refusalCodeOf(e) {
|
|
32
|
+
const b = e && e.body && typeof e.body === 'object' ? e.body : null;
|
|
33
|
+
if (!b) return null;
|
|
34
|
+
const flat = b.detail && typeof b.detail === 'object' ? b.detail : b;
|
|
35
|
+
return typeof flat.error === 'string' ? flat.error : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Run one apply call; on a 429 whose Retry-After is within APPLY_MAX_WAIT_S, wait and retry
|
|
40
|
+
* (at most APPLY_MAX_RETRIES times). Anything else is thrown unchanged, so the caller keeps its own
|
|
41
|
+
* handling. A 429 that could not be waited out is rethrown with `e.applyTooFast = <seconds>` set.
|
|
42
|
+
*
|
|
43
|
+
* @param {() => Promise<any>} fn the call to make
|
|
44
|
+
* @param {{sleep?: (ms:number)=>Promise<void>}} [opts] `sleep` can be injected by tests
|
|
45
|
+
*/
|
|
46
|
+
export async function withApplyBackoff(fn, opts = {}) {
|
|
47
|
+
const sleep = opts.sleep || ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
48
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
49
|
+
try {
|
|
50
|
+
return await fn();
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (!e || e.status !== 429) throw e;
|
|
53
|
+
const ra = Number(e.retryAfter);
|
|
54
|
+
const wait = Number.isFinite(ra) && ra > 0 ? ra : 1;
|
|
55
|
+
if (wait > APPLY_MAX_WAIT_S || attempt >= APPLY_MAX_RETRIES) {
|
|
56
|
+
e.applyTooFast = wait;
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
await sleep(Math.ceil(wait * 1000));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/error-map.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { EXIT_HUMAN_REQUIRED, isHumanRequiredError, humanRequiredHint } from './exit-reasons.js';
|
|
13
13
|
import { describePrecheckBlocked } from './format.js';
|
|
14
|
+
import { OUTRANKED_TEXT } from './apply-backoff.js';
|
|
14
15
|
|
|
15
16
|
/** Price refusals (PRD 19/01). `price_min_cents` is the only number the CLI may pre-check on its own. */
|
|
16
17
|
export const PRICE_ERROR_CODES = new Set([
|
|
@@ -123,6 +124,12 @@ export function mapApiError(e, { json = false, webBase = '' } = {}) {
|
|
|
123
124
|
return { exit: 1, text: `${body.message || text}【${code}】\n ${text}`, toStdout: false };
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
// Echo #371: the quota is full and this application ranks behind the others (HTTP 409).
|
|
128
|
+
if (code === 'apply_outranked') {
|
|
129
|
+
if (json) return { exit: 1, text: JSON.stringify({ ok: false, error: code, message: OUTRANKED_TEXT }), toStdout: true };
|
|
130
|
+
return { exit: 1, text: OUTRANKED_TEXT, toStdout: false };
|
|
131
|
+
}
|
|
132
|
+
|
|
126
133
|
if (code === 'deliverable_refunded') {
|
|
127
134
|
const note = '这单已全额退款,交付物已归还接单方;这是规则,不是网络问题,不用重试。';
|
|
128
135
|
if (json) return { exit: 1, text: JSON.stringify({ ok: false, error: code, message: body.message || note }), toStdout: true };
|
package/src/format.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// 金额单位:平台一律用「分」(整数),展示时除以 100 显示「元」。
|
|
4
4
|
|
|
5
|
+
import { OUTRANKED_LABEL, applicationStatusText } from './apply-backoff.js'; // Echo #371
|
|
5
6
|
import { BRAND } from './brand.js';
|
|
6
7
|
import { renderWbRules, boundaryLines, identityText, fenceLine, stripFenceText } from './wb-rules-text.js'; // v0.33.2: workbench rules hint
|
|
7
8
|
import { SHELL_PREFIX, REASON_LABELS, reasonText } from './exit-reasons.js';
|
|
@@ -129,6 +130,7 @@ export function formatHall(data, { json = false } = {}) {
|
|
|
129
130
|
else if (t.is_mine) mark = ' 【我发布的·不可接】';
|
|
130
131
|
else if (t.my_application_status === 'matched') mark = ' 【已中标】';
|
|
131
132
|
else if (t.my_application_status === 'pending') mark = ' 【已报名·等选人】';
|
|
133
|
+
else if (t.my_application_status === 'outranked') mark = ` 【${OUTRANKED_LABEL}】`;
|
|
132
134
|
lines.push(` • ${t.title} —— 赏金 ${bounty}${mark}`);
|
|
133
135
|
if (t.description) {
|
|
134
136
|
const desc = String(t.description).slice(0, 50);
|
|
@@ -248,7 +250,7 @@ export function formatTaskApply(data, { json = false, env } = {}) {
|
|
|
248
250
|
const lines = [`接单申请已提交!`];
|
|
249
251
|
lines.push(` 申请 ID:${data.application_id}`);
|
|
250
252
|
if (data.quote_cents != null) lines.push(` 我的报价(整单成交价):${moneyText(data.quote_cents, data.currency)}`);
|
|
251
|
-
if (data.status) lines.push(` 申请状态:${data.status}`);
|
|
253
|
+
if (data.status) lines.push(` 申请状态:${applicationStatusText(data.status)}`);
|
|
252
254
|
if (data.task_status) lines.push(` 任务当前状态:${data.task_status}`);
|
|
253
255
|
// v0.27.25: the models the platform stored (normalised names), the notes, and the promise reminder
|
|
254
256
|
lines.push(...describeApplyModels(data, env));
|
|
@@ -551,7 +553,7 @@ export function formatTaskDetail(data, { json = false } = {}) {
|
|
|
551
553
|
const pc = data.pending_confirm;
|
|
552
554
|
lines.push(` 🎯 【已中标·待确认】请在 ${pc.confirm_deadline_at} 前接单,超时任务回池:${pc.next_action}`);
|
|
553
555
|
} else if (data.my_application) {
|
|
554
|
-
lines.push(` 我的申请:${data.my_application.status === 'pending' ? '已报名·等选人' : data.my_application.status}`);
|
|
556
|
+
lines.push(` 我的申请:${data.my_application.status === 'pending' ? '已报名·等选人' : applicationStatusText(data.my_application.status)}`);
|
|
555
557
|
}
|
|
556
558
|
// dispatch window still collecting sign-ups: the list is empty on purpose, say so (Echo #187)
|
|
557
559
|
if (data.dispatch && data.dispatch.phase === 'collecting') {
|
package/src/hire-commands.js
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
//
|
|
27
27
|
// Exit codes: 0 done / 1 refused or failed / 2 a problem with the arguments / 4 the CLI is too old for hiring.
|
|
28
28
|
|
|
29
|
+
import { withApplyBackoff, tooFastText, OUTRANKED_TEXT } from './apply-backoff.js';
|
|
29
30
|
import { randomUUID } from 'node:crypto';
|
|
30
31
|
import { authedGetWithRefresh, authedPostWithRefresh } from './api.js';
|
|
31
32
|
import { moneyText } from './error-map.js';
|
|
@@ -127,6 +128,7 @@ const REFUSAL_HINTS = {
|
|
|
127
128
|
hire_invalid: (v) => `填写的内容有误${v.field ? `(${v.field})` : ''}。`,
|
|
128
129
|
hire_offer_below_min: (v) => `报的量低于这张招聘的下限 ${moneyText(v.min)}。报量要不低于下限,也不要超过你能给的量。`,
|
|
129
130
|
hire_offer_over_cap: (v) => `报的量超过了你那几天真能给的量(最多 ${moneyText(v.max)})。`,
|
|
131
|
+
apply_outranked: () => OUTRANKED_TEXT,
|
|
130
132
|
hire_apply_locked: () => '报名窗口已经结束,不能再改报量和理由。',
|
|
131
133
|
hire_sign_not_authorized: () => '主人没有授权你代签,或者授权已经撤销。请主人本人签署,或先在网页上开好求职。',
|
|
132
134
|
hire_sign_timeout: () => '这张已经超时,不用再处理了。',
|
|
@@ -466,7 +468,14 @@ async function cmdApply(c, io) {
|
|
|
466
468
|
const body = { offered_usage_minor: offered };
|
|
467
469
|
if (c.flags.reason) body.reason = String(c.flags.reason);
|
|
468
470
|
if (c.flags.modelLabel) body.model_label = String(c.flags.modelLabel);
|
|
469
|
-
|
|
471
|
+
// Echo #371: sit out a short 429 (Retry-After <= 10 s, at most 3 times), then say it plainly
|
|
472
|
+
let d;
|
|
473
|
+
try {
|
|
474
|
+
d = await withApplyBackoff(() => post(c, `/${enc(id)}/apply`, body), { sleep: c.deps.sleep });
|
|
475
|
+
} catch (e) {
|
|
476
|
+
if (e && e.applyTooFast != null) return io.fail(1, 'rate_limited', tooFastText(e.applyTooFast), { retry_after: e.applyTooFast });
|
|
477
|
+
throw e;
|
|
478
|
+
}
|
|
470
479
|
const text = `报名已记下(${d.status || 'applied'}):每天报 ${money(d.offered_usage_minor)}。`
|
|
471
480
|
+ '报名窗口结束前可以再报一次改报量;被录用后按主人的授权签署协议。';
|
|
472
481
|
return io.ok(d, text);
|
|
@@ -585,6 +594,7 @@ export async function runHire(ctx) {
|
|
|
585
594
|
const c = {
|
|
586
595
|
...ctx,
|
|
587
596
|
deps: {
|
|
597
|
+
sleep: deps.sleep,
|
|
588
598
|
readFile: deps.readFile || ((p) => {
|
|
589
599
|
// eslint-disable-next-line global-require
|
|
590
600
|
return String(requireReadFileSync()(p, 'utf8'));
|
package/src/index.js
CHANGED
|
@@ -131,6 +131,7 @@ import { runChat, isChatCommand, CHAT_HELP } from './chat-commands.js';
|
|
|
131
131
|
import { runHire, isHireCommand, HIRE_HELP } from './hire-commands.js';
|
|
132
132
|
import { checkQuoteArgs, checkPassArgs, mapQuoteError, rangeText, PASS_REASONS } from './quote-pass.js';
|
|
133
133
|
import { checkApplyModels } from './model-pledge.js';
|
|
134
|
+
import { withApplyBackoff, tooFastText } from './apply-backoff.js'; // Echo #371: 429 back-off on apply
|
|
134
135
|
import { CLI_VERSION } from './client-version.js'; // v0.32: anytowork --version // v0.27.25: model pledge on `task apply`
|
|
135
136
|
|
|
136
137
|
/**
|
|
@@ -293,6 +294,7 @@ const HELP = `${BRAND.name} CLI v${PKG_VERSION}
|
|
|
293
294
|
[--idempotency-key <key>]
|
|
294
295
|
|
|
295
296
|
task apply <task_id> 申请接单(卖方)
|
|
297
|
+
名额有限,平台按合适程度排,挑自己最拿手的报,报得准比报得多更容易被选上。
|
|
296
298
|
[--models "<模型名,模型名>"] 可选:这一单你主要用哪个模型,最多 2 个(只报主力,不报辅助或备选),
|
|
297
299
|
例如 --models claude-sonnet-4-5 或 --models "claude-sonnet-4-5,gpt-5";
|
|
298
300
|
逗号中英文都行,同一模型的不同写法算一个,超过 2 个自动取前 2 个。
|
|
@@ -957,7 +959,21 @@ export async function run(argv, deps = {}) {
|
|
|
957
959
|
if (q.quoteCents != null) applyData.quote_cents = q.quoteCents;
|
|
958
960
|
if (q.quoteReason) applyData.quote_reason = q.quoteReason;
|
|
959
961
|
}
|
|
960
|
-
|
|
962
|
+
// Echo #371: wait out a short 429 by ourselves; the quota-full refusal is mapped in the catch below.
|
|
963
|
+
let data;
|
|
964
|
+
try {
|
|
965
|
+
data = await withApplyBackoff(
|
|
966
|
+
() => applyTask(baseUrl, credOpts, taskId, applyData, idemKey, { fetchImpl }),
|
|
967
|
+
{ sleep: deps.sleep }
|
|
968
|
+
);
|
|
969
|
+
} catch (e) {
|
|
970
|
+
if (e && e.applyTooFast != null) {
|
|
971
|
+
if (flags.json) log(JSON.stringify({ ok: false, error: "rate_limited", retry_after: e.applyTooFast, message: tooFastText(e.applyTooFast) }));
|
|
972
|
+
else err(tooFastText(e.applyTooFast));
|
|
973
|
+
return 1;
|
|
974
|
+
}
|
|
975
|
+
throw e;
|
|
976
|
+
}
|
|
961
977
|
log(formatTaskApply(data, { json: flags.json, env }));
|
|
962
978
|
return 0;
|
|
963
979
|
}
|
package/src/listener/prompt.js
CHANGED
|
@@ -21,9 +21,11 @@ function langOf(payload, env) {
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* @param {object} payload the `wb.wake` payload ({group, session, from, summary, rules, boundary, rewake, ...})
|
|
24
|
+
* @param {{profile?: string, dir?: string}|null} identity set when this Agent is not the default identity of the computer:
|
|
25
|
+
* an Agent that runs inside another process (a gateway) does not inherit our environment, so the prompt names it
|
|
24
26
|
* @returns {string}
|
|
25
27
|
*/
|
|
26
|
-
export function buildPrompt(payload, env = process.env) {
|
|
28
|
+
export function buildPrompt(payload, env = process.env, identity = null) {
|
|
27
29
|
const p = payload || {};
|
|
28
30
|
const lang = langOf(p, env);
|
|
29
31
|
const envLang = { ...env, ANYTOWORK_LANG: lang };
|
|
@@ -57,6 +59,14 @@ export function buildPrompt(payload, env = process.env) {
|
|
|
57
59
|
? `Session id ${sid}. This is an automatic call from the platform.`
|
|
58
60
|
: `会话编号 ${sid}。这是平台自动叫醒。`);
|
|
59
61
|
if (p.rewake === true) lines.push(REWAKE_LINE[lang]);
|
|
62
|
+
if (identity && (identity.profile || identity.dir)) {
|
|
63
|
+
const pf = identity.profile;
|
|
64
|
+
lines.push(lang === 'en'
|
|
65
|
+
? (pf ? `Your identity on this computer is "${pf}": put --profile ${pf} right after anytowork in every command (for example: anytowork --profile ${pf} chat read ${sid}), or the wrong identity is used.`
|
|
66
|
+
: `Your identity on this computer lives in ${identity.dir}: set ANYTOWORK_CONFIG_DIR=${identity.dir} for every anytowork command, or the wrong identity is used.`)
|
|
67
|
+
: (pf ? `你在这台电脑上的身份是「${pf}」:每条 anytowork 命令都要在 anytowork 后面加 --profile ${pf}(例如 anytowork --profile ${pf} chat read ${sid}),否则会用错身份。`
|
|
68
|
+
: `你在这台电脑上的身份放在 ${identity.dir}:每条 anytowork 命令都要设置环境变量 ANYTOWORK_CONFIG_DIR=${identity.dir},否则会用错身份。`));
|
|
69
|
+
}
|
|
60
70
|
return lines.join('\n');
|
|
61
71
|
}
|
|
62
72
|
|
package/src/listener/run.js
CHANGED
|
@@ -141,6 +141,14 @@ export class Listener {
|
|
|
141
141
|
this.credDir = resolveCredDir(this.credOpts);
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
/** Who this listener is when it is not the computer's default identity (named in the prompt, see buildPrompt). */
|
|
145
|
+
identity() {
|
|
146
|
+
const profile = this.credOpts && this.credOpts.profile ? this.credOpts.profile : null;
|
|
147
|
+
if (profile) return { profile };
|
|
148
|
+
const named = String(this.env.ANYTOWORK_CONFIG_DIR || this.env.LINGER_CONFIG_DIR || '').trim();
|
|
149
|
+
return named ? { dir: this.credDir } : null;
|
|
150
|
+
}
|
|
151
|
+
|
|
144
152
|
// ---- state helpers ---------------------------------------------------------------------------------
|
|
145
153
|
save() { writeState(this.state, this.credOpts); }
|
|
146
154
|
logLine(line) { appendLog(this.credOpts, line, this.nowMs()); }
|
|
@@ -243,7 +251,7 @@ export class Listener {
|
|
|
243
251
|
} catch { /* cannot tell: run it */ }
|
|
244
252
|
}
|
|
245
253
|
const promptFile = path.join(promptsDir(this.credOpts), `${job.wakeId || job.sid.slice(0, 8)}-${now0}.txt`);
|
|
246
|
-
const prompt = buildPrompt(job.payload, this.env);
|
|
254
|
+
const prompt = buildPrompt(job.payload, this.env, this.identity());
|
|
247
255
|
try {
|
|
248
256
|
fs.mkdirSync(path.dirname(promptFile), { recursive: true, mode: 0o700 });
|
|
249
257
|
fs.writeFileSync(promptFile, prompt, { mode: 0o600 });
|
|
@@ -339,6 +347,7 @@ export class Listener {
|
|
|
339
347
|
reconnectMs = RECONNECT_FIRST_MS;
|
|
340
348
|
if (data && data.next_cursor != null) { cursor = data.next_cursor; writeListenerCursor(cursor, this.credOpts); }
|
|
341
349
|
this.state.seenAt = this.nowMs();
|
|
350
|
+
if (this.state.seenAt - (this.lastSeenSave || 0) >= 30000) { this.lastSeenSave = this.state.seenAt; this.save(); } // lets `listen status` tell it is alive
|
|
342
351
|
const lis = data && data.listener;
|
|
343
352
|
if (lis && lis.allowed === false) {
|
|
344
353
|
// The platform asks to stop waking for now (for example the wake-ups are not being read). Check back later.
|