slack-term 1.55.1 → 1.55.3
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 +6 -4
- package/dist/cli.js +241 -238
- package/package.json +1 -1
- package/ts/askPush.ts +20 -2
- package/ts/cli.ts +56 -14
- package/ts/identity.ts +16 -5
package/package.json
CHANGED
package/ts/askPush.ts
CHANGED
|
@@ -21,9 +21,27 @@ function clip(s: string, n: number): string {
|
|
|
21
21
|
return one.length <= n ? one : `${one.slice(0, n - 1)}…`;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** An answer this much older than its push is labelled late. */
|
|
25
|
+
export const PUSH_LATE_MS = 60 * 60_000;
|
|
26
|
+
|
|
27
|
+
function utc(ms: number): string {
|
|
28
|
+
return `${new Date(ms).toISOString().slice(0, 16).replace("T", " ")} UTC`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** `[ask-answer]`, or — for an answer that is not new (a backfill, a lane that
|
|
32
|
+
* was down) — `[ask-answer, late: …]` with when it was given. A pill press
|
|
33
|
+
* carries no time in Slack, so a late one says when the question was asked. */
|
|
34
|
+
export function askPushTag(askedAtMs: number, answeredAtMs: number | undefined, nowMs = Date.now()): string {
|
|
35
|
+
const since = answeredAtMs ?? askedAtMs;
|
|
36
|
+
if (nowMs - since < PUSH_LATE_MS) return "[ask-answer]";
|
|
37
|
+
return answeredAtMs !== undefined
|
|
38
|
+
? `[ask-answer, late: answered ${utc(answeredAtMs)}]`
|
|
39
|
+
: `[ask-answer, late: asked ${utc(askedAtMs)}, pressed since]`;
|
|
40
|
+
}
|
|
41
|
+
|
|
24
42
|
/** The one-line message the asker receives: link, answer, how, question. */
|
|
25
|
-
export function askPushMessage(p: { link: string; answer: string; how: string; question: string; freeText: boolean; collected?: boolean; goneAsker?: number }): string {
|
|
26
|
-
const head =
|
|
43
|
+
export function askPushMessage(p: { link: string; answer: string; how: string; question: string; freeText: boolean; collected?: boolean; goneAsker?: number; tag?: string }): string {
|
|
44
|
+
const head = `${p.tag ?? "[ask-answer]"} ${p.link} →`;
|
|
27
45
|
const ans = clip(p.answer, 300);
|
|
28
46
|
let tail = p.how ? ` (${p.how})` : "";
|
|
29
47
|
if (p.freeText) tail += " — a free-text reply, not a choice: the question stays open";
|
package/ts/cli.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { cmdAuthLogin, cmdAuthLoginChrome, cmdAuthChrome, cmdAuthFirefox, cmdAut
|
|
|
16
16
|
import { cmdTail, parseSince as parseDuration } from "./tail.ts";
|
|
17
17
|
import { phiDelay, runStream, webClient, type StreamMatch } from "./stream.ts";
|
|
18
18
|
import { relaySubscriber, relayTransport } from "./relay.ts";
|
|
19
|
-
import { acquirePushLock, askPushMessage, laneAgent, liveAgents, notifyAgent } from "./askPush.ts";
|
|
19
|
+
import { acquirePushLock, askPushMessage, askPushTag, laneAgent, liveAgents, notifyAgent } from "./askPush.ts";
|
|
20
20
|
import { DEFAULT_DIR, initRelay, initReport, relayHealth } from "./relayInit.ts";
|
|
21
21
|
import { agentCommands } from "./agent.ts";
|
|
22
22
|
|
|
@@ -3693,11 +3693,15 @@ async function cmdAsk(token: string, args: AskArgs): Promise<void> {
|
|
|
3693
3693
|
// Same silent-delivery footgun as `send`: Slack never notifies you about your
|
|
3694
3694
|
// own message, so a question asked in your own DM is delivered and then never
|
|
3695
3695
|
// seen — and `--wait` would sit there until it timed out.
|
|
3696
|
+
// From an agent it is refused (11-12: an agent holding the person's session
|
|
3697
|
+
// posted it under their name); a person at a terminal is only warned.
|
|
3698
|
+
const agent = inAgent();
|
|
3696
3699
|
if (!args.asBot && !!selfId && counterpart === selfId) {
|
|
3697
3700
|
console.error(
|
|
3698
|
-
|
|
3699
|
-
` To reach yourself with a notification, ask as the bot: slack ask '${args.target}' '...' --as-bot`,
|
|
3701
|
+
`${agent ? "Error" : "Warning"}: "${args.target}" is a DM to yourself — Slack will NOT notify you of your own message.\n` +
|
|
3702
|
+
` To reach yourself with a notification, ask as the bot: slack @<bot> ask '${args.target}' '...' (or --as-bot)`,
|
|
3700
3703
|
);
|
|
3704
|
+
if (agent) process.exit(ASK_EXIT_ERROR);
|
|
3701
3705
|
}
|
|
3702
3706
|
|
|
3703
3707
|
// --- who may answer -------------------------------------------------------
|
|
@@ -3730,6 +3734,18 @@ async function cmdAsk(token: string, args: AskArgs): Promise<void> {
|
|
|
3730
3734
|
// and posting anyway would silently drop someone the caller meant to ask.
|
|
3731
3735
|
askRefuseUnresolved(unresolved);
|
|
3732
3736
|
|
|
3737
|
+
// An agent asking the person whose session it holds (11-12: it tagged that
|
|
3738
|
+
// person in the bot's DM, so the question went out under the name of the
|
|
3739
|
+
// one it asked). The bot asks the person; a person asks others.
|
|
3740
|
+
const taggedSelf = !!selfId && (resolved.some((r) => r.userId === selfId) || new RegExp(`<@${selfId}(?:\\|[^>]*)?>`).test(`${question}\n${body}`));
|
|
3741
|
+
if (agent && !args.asBot && taggedSelf) {
|
|
3742
|
+
console.error(
|
|
3743
|
+
`Error: \`ask\` would post as you (${selfId}) a question addressed to you — the asker and the one asked are the same person.\n` +
|
|
3744
|
+
` To ask yourself, ask as the bot: slack @<bot> ask '${args.target}' '${args.question}' (or --as-bot)\n` +
|
|
3745
|
+
` To ask someone else as yourself, tag them, not you.`,
|
|
3746
|
+
);
|
|
3747
|
+
process.exit(ASK_EXIT_ERROR);
|
|
3748
|
+
}
|
|
3733
3749
|
const broadcast = broadcastKinds.size > 0;
|
|
3734
3750
|
// Tagging yourself grants nothing — your own reactions are the seeds.
|
|
3735
3751
|
const audience = new Set(resolved.map((r) => r.userId).filter((id) => id !== selfId));
|
|
@@ -4251,6 +4267,8 @@ interface AskPendingItem {
|
|
|
4251
4267
|
collect: string;
|
|
4252
4268
|
/** Where a delivery of this free-text reply ends (`--after` for the next look). */
|
|
4253
4269
|
cursor?: string;
|
|
4270
|
+
/** When the answer was given (a reply's ts); unknown for a pill press. */
|
|
4271
|
+
answeredTs?: string;
|
|
4254
4272
|
/** Set by `--push`: the pid that took the answer, and whether it was collected. */
|
|
4255
4273
|
pushed?: { pid: number; collected: boolean };
|
|
4256
4274
|
/** Set by `--push`: why this answer reached nobody. */
|
|
@@ -4321,8 +4339,12 @@ async function cmdAskPending(
|
|
|
4321
4339
|
only?: Set<string>; watch?: boolean;
|
|
4322
4340
|
/** Show only answers that reached nobody. */
|
|
4323
4341
|
undelivered?: boolean;
|
|
4342
|
+
/** `--watch`: questions already looked at in this round (`channel:ts`),
|
|
4343
|
+
* skipped unless asked within the last day. */
|
|
4344
|
+
skip?: Set<string>;
|
|
4324
4345
|
},
|
|
4325
|
-
): Promise<
|
|
4346
|
+
): Promise<{ checked: string[]; throttled: boolean }> {
|
|
4347
|
+
const checked: string[] = [];
|
|
4326
4348
|
// Never blocks: one look per question and no waiting out 429s. A throttled
|
|
4327
4349
|
// pass says how many it skipped; the next pass picks them up.
|
|
4328
4350
|
setRateLimitWait(0);
|
|
@@ -4331,8 +4353,10 @@ async function cmdAskPending(
|
|
|
4331
4353
|
console.error(`Error: ${SENT_LOG_UNAVAILABLE}\n (${sentDbPath()})`);
|
|
4332
4354
|
process.exit(ASK_EXIT_ERROR);
|
|
4333
4355
|
}
|
|
4356
|
+
const recent = Date.now() - 24 * 3600_000;
|
|
4334
4357
|
const rows = all.filter((r) =>
|
|
4335
|
-
(!opts.channel || r.channel === opts.channel || r.target === opts.channel) && (!opts.only || opts.only.has(`${r.channel}:${r.ts}`))
|
|
4358
|
+
(!opts.channel || r.channel === opts.channel || r.target === opts.channel) && (!opts.only || opts.only.has(`${r.channel}:${r.ts}`)) &&
|
|
4359
|
+
(!opts.skip || r.sent_at >= recent || !opts.skip.has(`${r.channel}:${r.ts}`)));
|
|
4336
4360
|
const pending: AskPendingItem[] = [];
|
|
4337
4361
|
const count = { unanswered: 0, ambiguous: 0, resolved: 0, void: 0, explain: 0, throttled: 0 };
|
|
4338
4362
|
/** Open questions someone pressed ℹ️ on — reported only: the thread note is
|
|
@@ -4358,6 +4382,7 @@ async function cmdAskPending(
|
|
|
4358
4382
|
}
|
|
4359
4383
|
const link = row.permalink || `${row.channel}:${row.ts}`;
|
|
4360
4384
|
const asBot = !!row.as_bot;
|
|
4385
|
+
checked.push(`${row.channel}:${row.ts}`);
|
|
4361
4386
|
// Resume from where the last delivery to a caller ended (an exit 5
|
|
4362
4387
|
// free-text reply), so that reply is not reported again — and the collect
|
|
4363
4388
|
// line carries the same cursor, so `--waitFor` does not re-deliver it.
|
|
@@ -4402,9 +4427,9 @@ async function cmdAskPending(
|
|
|
4402
4427
|
}
|
|
4403
4428
|
const base = { row, link, question: parsed.question, collect, alive: senderAlive(row) };
|
|
4404
4429
|
if (found) {
|
|
4405
|
-
pending.push({ ...base, answer: found.answer, how: found.how, freeText: false });
|
|
4430
|
+
pending.push({ ...base, answer: found.answer, how: found.how, freeText: false, ...(found.ts ? { answeredTs: found.ts } : {}) });
|
|
4406
4431
|
} else if (unchosen && !unchosen.ambiguous && unchosen.text !== row.delivered_answer) {
|
|
4407
|
-
pending.push({ ...base, answer: unchosen.text, how: "reply (not a choice)", freeText: true, cursor: askNextCursor(row.ts, after, unchosen.ts) });
|
|
4432
|
+
pending.push({ ...base, answer: unchosen.text, how: "reply (not a choice)", freeText: true, cursor: askNextCursor(row.ts, after, unchosen.ts), answeredTs: unchosen.ts });
|
|
4408
4433
|
} else if (unchosen?.ambiguous) {
|
|
4409
4434
|
count.ambiguous++;
|
|
4410
4435
|
} else {
|
|
@@ -4413,6 +4438,7 @@ async function cmdAskPending(
|
|
|
4413
4438
|
} catch (e: unknown) {
|
|
4414
4439
|
if (e instanceof RateLimitError) {
|
|
4415
4440
|
// Probing on would only collect more 429s for the same method.
|
|
4441
|
+
checked.pop(); // not looked at: the next round starts with it
|
|
4416
4442
|
count.throttled = rows.length - i;
|
|
4417
4443
|
problems.push(`rate-limited by Slack (${e.message}): ${count.throttled} question(s) not checked this pass`);
|
|
4418
4444
|
break;
|
|
@@ -4426,7 +4452,8 @@ async function cmdAskPending(
|
|
|
4426
4452
|
|
|
4427
4453
|
// A watcher's log carries what it did, not the whole list on every pass.
|
|
4428
4454
|
const shown = opts.watch ? pending.filter((it) => it.pushed) : opts.undelivered ? pending.filter(reachedNobody) : pending;
|
|
4429
|
-
|
|
4455
|
+
const result = { checked, throttled: count.throttled > 0 };
|
|
4456
|
+
if (opts.watch && !shown.length && !problems.length) return result;
|
|
4430
4457
|
if (opts.json) {
|
|
4431
4458
|
for (const it of shown) {
|
|
4432
4459
|
const r = it.row;
|
|
@@ -4479,6 +4506,7 @@ async function cmdAskPending(
|
|
|
4479
4506
|
(opts.undelivered ? `, ${pending.filter(reachedNobody).length} undelivered` : "") +
|
|
4480
4507
|
` — checked ${rows.length} uncollected question(s) (log: ${sentDbPath()})`,
|
|
4481
4508
|
);
|
|
4509
|
+
return result;
|
|
4482
4510
|
}
|
|
4483
4511
|
|
|
4484
4512
|
/** `ask --pending --push --watch`: a push pass every `intervalMs`, and — when
|
|
@@ -4486,7 +4514,7 @@ async function cmdAskPending(
|
|
|
4486
4514
|
* soon as the relay rings for it (a reaction on it, or a reply in its
|
|
4487
4515
|
* thread), so a pressed pill reaches the asker in seconds. The relay only
|
|
4488
4516
|
* sees the relay app's channels; the interval covers the rest. */
|
|
4489
|
-
async function askPushWatch(run: (only?: Set<string>) => Promise<
|
|
4517
|
+
async function askPushWatch(run: (only?: Set<string>) => Promise<unknown>, sinceMs: () => number, intervalMs: number | (() => number)): Promise<never> {
|
|
4490
4518
|
let busy = false;
|
|
4491
4519
|
const queued = new Set<string>();
|
|
4492
4520
|
let full = true;
|
|
@@ -4538,7 +4566,7 @@ async function askPushWatch(run: (only?: Set<string>) => Promise<void>, sinceMs:
|
|
|
4538
4566
|
for (;;) {
|
|
4539
4567
|
full = true;
|
|
4540
4568
|
await kick();
|
|
4541
|
-
await new Promise((r) => setTimeout(r, intervalMs));
|
|
4569
|
+
await new Promise((r) => setTimeout(r, typeof intervalMs === "number" ? intervalMs : intervalMs()));
|
|
4542
4570
|
}
|
|
4543
4571
|
}
|
|
4544
4572
|
|
|
@@ -4572,6 +4600,7 @@ function askPushAll(items: AskPendingItem[], selfArgs: string[], problems: strin
|
|
|
4572
4600
|
const msg = askPushMessage({
|
|
4573
4601
|
link: it.link, answer: it.answer, how: it.how, question: it.question, freeText: it.freeText, collected: !it.freeText,
|
|
4574
4602
|
...(it.alive ? {} : { goneAsker: asker }),
|
|
4603
|
+
tag: askPushTag(r.sent_at, it.answeredTs ? Number(it.answeredTs) * 1000 : undefined),
|
|
4575
4604
|
});
|
|
4576
4605
|
const took = notifyAgent(target, msg);
|
|
4577
4606
|
if (took === undefined) {
|
|
@@ -6117,7 +6146,7 @@ async function main(): Promise<void> {
|
|
|
6117
6146
|
process.exit(1);
|
|
6118
6147
|
})
|
|
6119
6148
|
.option("workspace", { alias: "w", type: "string", describe: "Workspace name" })
|
|
6120
|
-
.option("as-user", { type: "boolean", describe: "Act as the user session on purpose (
|
|
6149
|
+
.option("as-user", { type: "boolean", describe: "Act as the user session on purpose (not accepted for writes from inside an agent: name the person with slack @<who>)" })
|
|
6121
6150
|
.middleware(async (argv) => {
|
|
6122
6151
|
const cmd = String((argv._ ?? [])[0] ?? "");
|
|
6123
6152
|
// `@bot`: commands that have --as-bot act as the bot; `@user`: never.
|
|
@@ -6146,11 +6175,13 @@ async function main(): Promise<void> {
|
|
|
6146
6175
|
console.error(refusal);
|
|
6147
6176
|
process.exit(2);
|
|
6148
6177
|
}
|
|
6178
|
+
const ids = listIdentities();
|
|
6149
6179
|
const user = userSessionRefusal({
|
|
6150
6180
|
isWrite: isWriteCommand(argv._ ?? []) && !isReadMode(argv._ ?? [], argv as Record<string, unknown>),
|
|
6151
6181
|
who, asBot: argv["as-bot"] === true, asUser: argv["as-user"] === true,
|
|
6152
6182
|
activeIsBot: active.startsWith("xoxb-"),
|
|
6153
|
-
agent: inAgent(), identities: [...
|
|
6183
|
+
agent: inAgent(), identities: [...ids.keys()].sort(),
|
|
6184
|
+
userIdentities: [...ids].filter(([, id]) => id.kind === "user").map(([n]) => n).sort(),
|
|
6154
6185
|
cmd: (argv._ ?? []).slice(0, 2).join(" "),
|
|
6155
6186
|
});
|
|
6156
6187
|
if (user) {
|
|
@@ -7123,10 +7154,21 @@ async function main(): Promise<void> {
|
|
|
7123
7154
|
process.exit(ASK_EXIT_ERROR);
|
|
7124
7155
|
}
|
|
7125
7156
|
const since = String(argv.since);
|
|
7157
|
+
// A round: full passes until every question has been looked at
|
|
7158
|
+
// once. A pass cut short by a rate limit leaves the rest to the
|
|
7159
|
+
// next one instead of starting over from the newest (which left
|
|
7160
|
+
// most of a long backlog unread on every pass).
|
|
7161
|
+
const round = new Set<string>();
|
|
7126
7162
|
await askPushWatch(
|
|
7127
|
-
(only) =>
|
|
7163
|
+
async (only) => {
|
|
7164
|
+
const r = await cmdAskPending(tokens, { ...p, sinceMs: parseSince(since) ?? sinceMs, watch: true, ...(only ? { only } : { skip: round }) });
|
|
7165
|
+
if (only) return;
|
|
7166
|
+
if (r.throttled) for (const k of r.checked) round.add(k);
|
|
7167
|
+
else round.clear();
|
|
7168
|
+
},
|
|
7128
7169
|
() => parseSince(since) ?? sinceMs,
|
|
7129
|
-
|
|
7170
|
+
// Mid-round, the next pass comes sooner: a 429 window is a minute.
|
|
7171
|
+
() => (round.size ? Math.min(2, min) : min) * 60_000,
|
|
7130
7172
|
);
|
|
7131
7173
|
}
|
|
7132
7174
|
try {
|
package/ts/identity.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
// And the trap it closes (11-02: an agent's `slack ask` without --as-bot was
|
|
13
13
|
// posted AS a person): a write that would go out as a person's session is refused
|
|
14
14
|
// from inside an agent, or when several identities exist, unless it names one.
|
|
15
|
+
// From an agent, --as-user is not enough (11-12): it must name the person.
|
|
15
16
|
import { readFileSync } from "node:fs";
|
|
16
17
|
import { homedir } from "node:os";
|
|
17
18
|
import { join } from "node:path";
|
|
@@ -124,21 +125,31 @@ export function inAgent(env: NodeJS.ProcessEnv = process.env): string | undefine
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
/** Why a write that would post as a PERSON's session is refused, or null.
|
|
127
|
-
* Allowed when it names an identity (`@who`), says --as-bot or
|
|
128
|
-
*
|
|
128
|
+
* Allowed when it names an identity (`@who`), says --as-bot, or the active
|
|
129
|
+
* token is a bot's anyway. `--as-user` is a person's own switch: inside an
|
|
130
|
+
* agent it no longer counts (11-12: lanes added it by habit, and their posts
|
|
131
|
+
* went out under the person's name). An agent that must act as a person names
|
|
132
|
+
* that person's identity (`slack @alice …`), so the command line says who, and
|
|
133
|
+
* the sent log records it. A person at a terminal keeps --as-user as is. */
|
|
129
134
|
export function userSessionRefusal(o: {
|
|
130
135
|
isWrite: boolean; who?: string | undefined; asBot: boolean; asUser: boolean; activeIsBot: boolean;
|
|
131
|
-
agent?: string | undefined; identities: string[]; cmd: string;
|
|
136
|
+
agent?: string | undefined; identities: string[]; userIdentities?: string[]; cmd: string;
|
|
132
137
|
}): string | null {
|
|
133
|
-
if (!o.isWrite || o.who || o.asBot || o.
|
|
138
|
+
if (!o.isWrite || o.who || o.asBot || o.activeIsBot) return null;
|
|
139
|
+
const users = o.userIdentities ?? [];
|
|
140
|
+
if (o.asUser && !o.agent) return null; // a person at a terminal: their own call
|
|
134
141
|
const names = o.identities.map((n) => `@${n}`).join(" | ");
|
|
135
142
|
const pick = names ? ` Name who acts: slack ${o.identities[0] ? `@${o.identities[0]}` : "@<who>"} ${o.cmd} … (${names})` : " Add identities to .slack-term/settings.yml, then: slack @<who> " + o.cmd + " …";
|
|
136
143
|
if (o.agent) {
|
|
144
|
+
const person = users.length
|
|
145
|
+
? ` As a person, on purpose: slack @${users[0]} ${o.cmd} … (${users.map((n) => `@${n}`).join(" | ")})`
|
|
146
|
+
: " As a person, on purpose: add them to `identities:` (kind: user), then slack @<name> " + o.cmd + " …";
|
|
137
147
|
return [
|
|
138
148
|
`Error: refusing to post as the user session from inside an agent (${o.agent} is set): \`${o.cmd}\` would go out as a PERSON.`,
|
|
149
|
+
...(o.asUser ? [" --as-user is not accepted from an agent: it hides whose name the post goes out under."] : []),
|
|
139
150
|
pick,
|
|
140
151
|
" As the app: add --as-bot",
|
|
141
|
-
|
|
152
|
+
person,
|
|
142
153
|
].join("\n");
|
|
143
154
|
}
|
|
144
155
|
if (o.identities.length >= 2) {
|