slack-term 1.55.3 → 1.57.1
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 +33 -717
- package/SKILL.md +9 -9
- package/dist/cli.js +269 -269
- package/locales/en.json +1 -1
- package/locales/ja.json +1 -1
- package/package.json +5 -5
- package/ts/ask.ts +3 -2
- package/ts/askPush.ts +5 -4
- package/ts/auth.ts +8 -8
- package/ts/brand.ts +50 -0
- package/ts/chromeCookieWin.ts +12 -12
- package/ts/cli.ts +33 -29
- package/ts/identity.ts +7 -7
- package/ts/inbox.ts +1 -1
- package/ts/locales.ts +1 -1
- package/ts/profiles.ts +10 -9
- package/ts/redact.ts +1 -1
- package/ts/relay.ts +5 -3
- package/ts/relayInit.ts +15 -15
- package/ts/scope.ts +23 -20
- package/ts/sentlog.ts +12 -10
- package/ts/stream.ts +1 -1
- package/worker/README.md +10 -10
- package/worker/src/hub.ts +6 -2
- package/worker/src/index.ts +4 -3
package/ts/profiles.ts
CHANGED
|
@@ -8,21 +8,22 @@
|
|
|
8
8
|
// Token resolution order (no --workspace flag):
|
|
9
9
|
// 1. process.env SLACK_TOKEN or SLACK_BOT_TOKEN
|
|
10
10
|
// 2. process.env SLACK_MCP_XOXP_TOKEN (legacy, yields to profiles if any exist)
|
|
11
|
-
// 3. Dir walk from cwd→$HOME: .slack-
|
|
11
|
+
// 3. Dir walk from cwd→$HOME: .slack-hub/.env.local, then .env.local (modern env-file names only)
|
|
12
12
|
// Note: cli.ts loadDotenvFiles() has already loaded cwd/.env.local into process.env (#1 catches
|
|
13
|
-
// that case), so the walk's extra value is finding .slack-
|
|
14
|
-
// 4. ~/.slack-
|
|
13
|
+
// that case), so the walk's extra value is finding .slack-hub/ variants and parent-dir files.
|
|
14
|
+
// 4. ~/.slack-hub/.env.local (global slack-hub config)
|
|
15
15
|
// 5. profiles.json via SLACK_WORKSPACE / lockfiles
|
|
16
16
|
// 6. throw with auth help
|
|
17
17
|
//
|
|
18
18
|
// --workspace flag skips #2-4 and goes straight to profiles.json.
|
|
19
19
|
//
|
|
20
20
|
// Note: lockfiles and profiles.json use .slack-cli/ (older name); new per-dir token storage
|
|
21
|
-
// uses .slack-
|
|
21
|
+
// uses .slack-hub/ (current project name; a legacy .slack-term/ is still read). Both coexist intentionally.
|
|
22
22
|
|
|
23
23
|
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
|
|
24
24
|
import { homedir } from "node:os";
|
|
25
25
|
import { join, dirname } from "node:path";
|
|
26
|
+
import { LEGACY_SCOPE_DIR, preferExisting, SCOPE_DIR } from "./brand.ts";
|
|
26
27
|
|
|
27
28
|
export type Profile = {
|
|
28
29
|
token: string;
|
|
@@ -115,14 +116,14 @@ function readEnvFile(filePath: string): Record<string, string> {
|
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
/** Walk from startDir up to $HOME checking .slack-
|
|
119
|
+
/** Walk from startDir up to $HOME checking .slack-hub/.env.local then .env.local.
|
|
119
120
|
* Returns first value found for any of the given keys. */
|
|
120
121
|
function walkDirEnv(startDir: string, keys: string[]): string | undefined {
|
|
121
122
|
const homeDir = home();
|
|
122
123
|
let dir = startDir;
|
|
123
124
|
|
|
124
125
|
while (true) {
|
|
125
|
-
for (const subdir of [join(dir, ".
|
|
126
|
+
for (const subdir of [join(dir, SCOPE_DIR, ".env.local"), join(dir, LEGACY_SCOPE_DIR, ".env.local"), join(dir, ".env.local")]) {
|
|
126
127
|
const vars = readEnvFile(subdir);
|
|
127
128
|
for (const key of keys) {
|
|
128
129
|
if (vars[key]) return vars[key];
|
|
@@ -134,8 +135,8 @@ function walkDirEnv(startDir: string, keys: string[]): string | undefined {
|
|
|
134
135
|
dir = parent;
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
// Global slack-
|
|
138
|
-
const globalVars = readEnvFile(join(homeDir, ".
|
|
138
|
+
// Global slack-hub config — separate from the walk, checked after $HOME
|
|
139
|
+
const globalVars = readEnvFile(preferExisting(join(homeDir, SCOPE_DIR, ".env.local"), join(homeDir, LEGACY_SCOPE_DIR, ".env.local")));
|
|
139
140
|
for (const key of keys) {
|
|
140
141
|
if (globalVars[key]) return globalVars[key];
|
|
141
142
|
}
|
|
@@ -333,7 +334,7 @@ export function resolveToken(workspaceFlag?: string): string {
|
|
|
333
334
|
" slack auth cookie — attach the xoxd cookie from Chrome (macOS/Windows)\n" +
|
|
334
335
|
" slack auth firefox — attach the xoxd cookie from Firefox (all platforms)\n" +
|
|
335
336
|
" slack auth token --token xoxp-... — paste an existing token\n" +
|
|
336
|
-
" Or set SLACK_TOKEN=xoxp-... in .slack-
|
|
337
|
+
" Or set SLACK_TOKEN=xoxp-... in .slack-hub/.env.local",
|
|
337
338
|
);
|
|
338
339
|
}
|
|
339
340
|
|
package/ts/redact.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Secret redaction for anything slack-
|
|
1
|
+
// Secret redaction for anything slack-hub writes to disk (the .slack-hub/
|
|
2
2
|
// inbox/outbox): each secret becomes `[redacted:<rule id>]`, and the original
|
|
3
3
|
// never touches the file. `slack read` fetches the real text live.
|
|
4
4
|
//
|
package/ts/relay.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Client for the slack-
|
|
1
|
+
// Client for the slack-hub relay (worker/): a Cloudflare Worker that receives
|
|
2
2
|
// Slack's Events API and serves "doorbells" — {channel, ts, thread_ts}, never
|
|
3
3
|
// message text — as a resumable stream. `slack stream` uses it to scan the one
|
|
4
4
|
// channel a bell names within a second of the post, instead of waiting for its
|
|
@@ -75,7 +75,9 @@ export function parseSse(buf: string, emit: (event: string, data: string, id: st
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/** The subprotocol the relay's /ws speaks; it echoes this one back. */
|
|
78
|
-
export const WS_PROTOCOL = "slack-
|
|
78
|
+
export const WS_PROTOCOL = "slack-hub-relay.v1";
|
|
79
|
+
/** Also offered, so a relay deployed before the slack-hub rename still accepts us. */
|
|
80
|
+
export const LEGACY_WS_PROTOCOL = "slack-term-relay.v1";
|
|
79
81
|
|
|
80
82
|
/** The subprotocol that carries the bearer token: subprotocols must be HTTP
|
|
81
83
|
* tokens, so the token is base64url-encoded. */
|
|
@@ -152,7 +154,7 @@ function wsSubscribe(
|
|
|
152
154
|
if (signal.aborted) return resolve(true);
|
|
153
155
|
let ws: WebSocketLike;
|
|
154
156
|
try {
|
|
155
|
-
ws = new WS(`${base.replace(/^http/i, "ws")}/ws${resumeQuery(after)}`, [WS_PROTOCOL, bearerProtocol(token)]);
|
|
157
|
+
ws = new WS(`${base.replace(/^http/i, "ws")}/ws${resumeQuery(after)}`, [WS_PROTOCOL, LEGACY_WS_PROTOCOL, bearerProtocol(token)]);
|
|
156
158
|
} catch {
|
|
157
159
|
return resolve(false);
|
|
158
160
|
}
|
package/ts/relayInit.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// `slack relay init` / `slack relay health`: set up and check the doorbell
|
|
2
2
|
// relay without cloning this repo.
|
|
3
3
|
//
|
|
4
|
-
// The relay's code ships in the npm package as `slack-
|
|
4
|
+
// The relay's code ships in the npm package as `slack-hub/worker`
|
|
5
5
|
// (worker/src/), so a deployable Worker is a two-line re-export plus a
|
|
6
6
|
// wrangler.jsonc. `init` writes that folder — local files only, no network,
|
|
7
7
|
// nothing that touches Slack — and prints a fresh RELAY_TOKEN with the next
|
|
@@ -17,8 +17,8 @@ import pkg from "../package.json" with { type: "json" };
|
|
|
17
17
|
export const WRANGLER_VERSION = "4.105.0";
|
|
18
18
|
/** worker/wrangler.jsonc's compatibility_date. */
|
|
19
19
|
export const COMPATIBILITY_DATE = "2026-07-01";
|
|
20
|
-
export const DEFAULT_DIR = "slack-
|
|
21
|
-
export const RELAY_DOCS_URL = "https://github.com/snomiao/slack-
|
|
20
|
+
export const DEFAULT_DIR = "slack-hub-relay";
|
|
21
|
+
export const RELAY_DOCS_URL = "https://github.com/snomiao/slack-hub/blob/main/worker/README.md";
|
|
22
22
|
|
|
23
23
|
/** A Workers script name: lowercase letters, digits and dashes, 1–63 chars,
|
|
24
24
|
* no leading or trailing dash. */
|
|
@@ -35,25 +35,25 @@ export function workerNameFrom(dir: string): string {
|
|
|
35
35
|
export type ScaffoldOptions = {
|
|
36
36
|
/** Worker name; default: the directory's basename. */
|
|
37
37
|
name?: string;
|
|
38
|
-
/** The `slack-
|
|
38
|
+
/** The `slack-hub` dependency spec; default: `^` this CLI's version (a source
|
|
39
39
|
* checkout carries a stale placeholder version; the range still resolves). */
|
|
40
|
-
|
|
40
|
+
slackHub?: string;
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
/** The scaffold's files, relative path → content. */
|
|
44
|
-
export function relayFiles(name: string, opts: {
|
|
44
|
+
export function relayFiles(name: string, opts: { slackHub?: string } = {}): Record<string, string> {
|
|
45
45
|
const packageJson = {
|
|
46
46
|
name,
|
|
47
47
|
private: true,
|
|
48
48
|
type: "module",
|
|
49
49
|
scripts: { deploy: "wrangler deploy", dev: "wrangler dev --local" },
|
|
50
|
-
dependencies: { "slack-
|
|
50
|
+
dependencies: { "slack-hub": opts.slackHub ?? `^${pkg.version}` },
|
|
51
51
|
devDependencies: { wrangler: WRANGLER_VERSION },
|
|
52
52
|
engines: { node: ">=22" },
|
|
53
53
|
};
|
|
54
54
|
return {
|
|
55
55
|
"package.json": `${JSON.stringify(packageJson, null, 2)}\n`,
|
|
56
|
-
"wrangler.jsonc": `// slack-
|
|
56
|
+
"wrangler.jsonc": `// slack-hub relay: Slack Events API -> doorbells for \`slack stream\`.
|
|
57
57
|
// Secrets (not in this file): wrangler secret put SLACK_SIGNING_SECRET / RELAY_TOKEN / SLACK_TEAM_ID (optional)
|
|
58
58
|
{
|
|
59
59
|
"$schema": "node_modules/wrangler/config-schema.json",
|
|
@@ -69,9 +69,9 @@ export function relayFiles(name: string, opts: { slackTerm?: string } = {}): Rec
|
|
|
69
69
|
"observability": { "enabled": false }
|
|
70
70
|
}
|
|
71
71
|
`,
|
|
72
|
-
"src/index.ts": `// The relay lives in the slack-
|
|
72
|
+
"src/index.ts": `// The relay lives in the slack-hub package. Relay (the Durable Object class)
|
|
73
73
|
// must be exported from this main module for the RELAY binding to find it.
|
|
74
|
-
export { default, Relay } from "slack-
|
|
74
|
+
export { default, Relay } from "slack-hub/worker";
|
|
75
75
|
`,
|
|
76
76
|
".gitignore": "node_modules/\n.wrangler/\n.dev.vars*\n.env*\n",
|
|
77
77
|
"README.md": readme(name),
|
|
@@ -109,9 +109,9 @@ export function nextSteps(name: string, token?: string): string {
|
|
|
109
109
|
function readme(name: string): string {
|
|
110
110
|
return `# ${name}
|
|
111
111
|
|
|
112
|
-
A [slack-
|
|
112
|
+
A [slack-hub](https://github.com/snomiao/slack-hub) relay: a Cloudflare Worker
|
|
113
113
|
that turns Slack's Events API into a resumable doorbell stream for
|
|
114
|
-
\`slack stream\`. The code is the \`slack-
|
|
114
|
+
\`slack stream\`. The code is the \`slack-hub/worker\` export; this folder only
|
|
115
115
|
configures and deploys it. Created by \`slack relay init\`.
|
|
116
116
|
|
|
117
117
|
## Set up
|
|
@@ -123,7 +123,7 @@ where messages are posted.
|
|
|
123
123
|
|
|
124
124
|
## Update
|
|
125
125
|
|
|
126
|
-
Bump \`slack-
|
|
126
|
+
Bump \`slack-hub\` in package.json, \`npm install\`, \`npx wrangler deploy\`.
|
|
127
127
|
Storage survives redeploys; clients reconnect and resume.
|
|
128
128
|
|
|
129
129
|
Details (endpoints, privacy, protocol): ${RELAY_DOCS_URL}
|
|
@@ -145,7 +145,7 @@ export function initRelay(
|
|
|
145
145
|
if (!validWorkerName(name)) {
|
|
146
146
|
return { ok: false, error: `invalid worker name "${name}": use lowercase letters, digits and dashes (1-63 chars, no leading/trailing dash)` };
|
|
147
147
|
}
|
|
148
|
-
const files = relayFiles(name, opts.
|
|
148
|
+
const files = relayFiles(name, opts.slackHub !== undefined ? { slackHub: opts.slackHub } : {});
|
|
149
149
|
const root = resolve(dir);
|
|
150
150
|
const existing = Object.keys(files).filter((f) => existsSync(join(root, f)));
|
|
151
151
|
if (existing.length && !opts.force) {
|
|
@@ -201,7 +201,7 @@ export async function relayHealth(
|
|
|
201
201
|
try {
|
|
202
202
|
json = JSON.parse(body) as typeof json;
|
|
203
203
|
} catch {
|
|
204
|
-
return { code: 1, out: `slack relay health: not a slack-
|
|
204
|
+
return { code: 1, out: `slack relay health: not a slack-hub relay (non-JSON reply from ${target})` };
|
|
205
205
|
}
|
|
206
206
|
return { code: json.ok === true ? 0 : 1, out: JSON.stringify(json, null, 2) };
|
|
207
207
|
} catch (e) {
|
package/ts/scope.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// `.slack-
|
|
1
|
+
// `.slack-hub/` — per-directory state, found like `.git`: the nearest one
|
|
2
2
|
// walking up from the cwd is the scope, so each lane's tree (or repo) owns its
|
|
3
3
|
// own aliases and settings without a pid, an env var or a daemon. No
|
|
4
|
-
// `.slack-
|
|
4
|
+
// `.slack-hub/` anywhere up the tree falls back to ~/.config/slack-hub/.
|
|
5
5
|
//
|
|
6
|
-
// .slack-
|
|
6
|
+
// .slack-hub/
|
|
7
7
|
// README.md what this is (written on init)
|
|
8
8
|
// .gitignore "*" — nothing in here is ever committed
|
|
9
9
|
// settings.yml written by people: prefix, channel, as_bot, subscriptions
|
|
@@ -17,25 +17,28 @@
|
|
|
17
17
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
18
18
|
import { homedir } from "node:os";
|
|
19
19
|
import { dirname, join, resolve } from "node:path";
|
|
20
|
+
import { hubEnv, LEGACY_SCOPE_DIR, namedDir, SCOPE_DIR, scopeDirIn } from "./brand.ts";
|
|
20
21
|
|
|
21
|
-
export
|
|
22
|
+
export { SCOPE_DIR };
|
|
22
23
|
export const SCOPE_SUBDIRS = ["inbox", "archived", "outbox", "sent"] as const;
|
|
23
24
|
|
|
24
25
|
export type Scope = { dir: string; global: boolean };
|
|
25
26
|
|
|
26
|
-
/** The global fallback: ~/.config/slack-
|
|
27
|
+
/** The global fallback: ~/.config/slack-hub (SLACK_HUB_HOME overrides, for tests). */
|
|
27
28
|
export function globalScopeDir(): string {
|
|
28
|
-
return
|
|
29
|
+
return hubEnv("HOME") ?? namedDir(join(homedir(), ".config"));
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
/** The nearest `.slack-
|
|
32
|
+
/** The nearest `.slack-hub/` at or above `cwd`, else the global one. */
|
|
32
33
|
export function findScope(cwd = process.cwd()): Scope {
|
|
33
34
|
let d = resolve(cwd);
|
|
34
35
|
for (;;) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
for (const name of [SCOPE_DIR, LEGACY_SCOPE_DIR]) {
|
|
37
|
+
const c = join(d, name);
|
|
38
|
+
try {
|
|
39
|
+
if (statSync(c).isDirectory()) return { dir: c, global: false };
|
|
40
|
+
} catch { /* not here */ }
|
|
41
|
+
}
|
|
39
42
|
const up = dirname(d);
|
|
40
43
|
if (up === d) break;
|
|
41
44
|
d = up;
|
|
@@ -43,12 +46,12 @@ export function findScope(cwd = process.cwd()): Scope {
|
|
|
43
46
|
return { dir: globalScopeDir(), global: true };
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
const README = `# .slack-
|
|
49
|
+
const README = `# .slack-hub/
|
|
47
50
|
|
|
48
|
-
State for [slack-
|
|
49
|
-
\`slack\` uses the nearest \`.slack-
|
|
51
|
+
State for [slack-hub](https://github.com/snomiao/slack-hub) in this directory.
|
|
52
|
+
\`slack\` uses the nearest \`.slack-hub/\` walking up from where it runs (like
|
|
50
53
|
\`.git\`), so everything below this directory shares it. Without one it uses
|
|
51
|
-
~/.config/slack-
|
|
54
|
+
~/.config/slack-hub/.
|
|
52
55
|
|
|
53
56
|
Nothing here is committed: \`.gitignore\` holds \`*\`.
|
|
54
57
|
|
|
@@ -87,7 +90,7 @@ subscriptions count, \`slack alias set\` refuses (the owner uses \`--shared\`),
|
|
|
87
90
|
slack alias who <channel> <ts> # which dirs subscribe a thread
|
|
88
91
|
`;
|
|
89
92
|
|
|
90
|
-
const SETTINGS = `# slack-
|
|
93
|
+
const SETTINGS = `# slack-hub settings for this directory. settings.local.yml (written by
|
|
91
94
|
# \`slack alias\`) has the same keys and wins over this file.
|
|
92
95
|
#
|
|
93
96
|
# prefix: "[lane]" # put in front of every \`slack reply\`; never doubled
|
|
@@ -125,9 +128,9 @@ export type Settings = {
|
|
|
125
128
|
owner?: string;
|
|
126
129
|
};
|
|
127
130
|
|
|
128
|
-
/** Create (or complete) `<parent>/.slack-
|
|
131
|
+
/** Create (or complete) `<parent>/.slack-hub/`. Idempotent: existing files are kept. */
|
|
129
132
|
export function initScope(parent: string, settings: Omit<Settings, "subscriptions"> = {}): { dir: string; created: string[] } {
|
|
130
|
-
const dir =
|
|
133
|
+
const dir = scopeDirIn(resolve(parent));
|
|
131
134
|
const created: string[] = [];
|
|
132
135
|
mkdirSync(dir, { recursive: true });
|
|
133
136
|
for (const sub of SCOPE_SUBDIRS) {
|
|
@@ -386,13 +389,13 @@ export class RestrictedError extends Error {
|
|
|
386
389
|
}
|
|
387
390
|
}
|
|
388
391
|
|
|
389
|
-
// --- the registry: every .slack-
|
|
392
|
+
// --- the registry: every .slack-hub/ on this machine, for the relay fan-out ---
|
|
390
393
|
|
|
391
394
|
function registryPath(): string {
|
|
392
395
|
return join(globalScopeDir(), "scopes");
|
|
393
396
|
}
|
|
394
397
|
|
|
395
|
-
/** Remember `dir` (a .slack-
|
|
398
|
+
/** Remember `dir` (a .slack-hub/ path) so a stream can deliver into it. */
|
|
396
399
|
export function registerScope(dir: string): void {
|
|
397
400
|
const p = registryPath();
|
|
398
401
|
withFileLock(p, () => {
|
package/ts/sentlog.ts
CHANGED
|
@@ -9,19 +9,20 @@
|
|
|
9
9
|
// Two places, on purpose:
|
|
10
10
|
// - a local SQLite log (~/.config/slack-cli/sent.sqlite), which `slack sent`
|
|
11
11
|
// and `slack ask --pending` read;
|
|
12
|
-
// - Slack message `metadata` (event_type `
|
|
12
|
+
// - Slack message `metadata` (event_type `slack_hub_sent`), invisible to
|
|
13
13
|
// readers but returned by the API, so a machine WITHOUT the log can still
|
|
14
14
|
// attribute a message.
|
|
15
15
|
//
|
|
16
16
|
// Everything here is fail-soft. A message that was posted must never be
|
|
17
17
|
// reported as failed because the bookkeeping about it failed.
|
|
18
18
|
//
|
|
19
|
-
// Opt out with
|
|
19
|
+
// Opt out with SLACK_HUB_ATTRIBUTION=off (no log, no metadata).
|
|
20
20
|
|
|
21
21
|
import { spawnSync } from "node:child_process";
|
|
22
22
|
import { chmodSync, existsSync, mkdirSync } from "node:fs";
|
|
23
23
|
import { homedir, hostname } from "node:os";
|
|
24
24
|
import { basename, dirname, join } from "node:path";
|
|
25
|
+
import { hubEnv } from "./brand.ts";
|
|
25
26
|
|
|
26
27
|
export type SentKind = "send" | "ask" | "edit" | "poll";
|
|
27
28
|
|
|
@@ -80,7 +81,7 @@ export interface SentRow {
|
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
export function attributionEnabled(): boolean {
|
|
83
|
-
return !/^(0|off|false|no)$/i.test(
|
|
84
|
+
return !/^(0|off|false|no)$/i.test(hubEnv("ATTRIBUTION") ?? "");
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
// --- attribution -------------------------------------------------------------
|
|
@@ -133,15 +134,15 @@ function positiveInt(s: string | undefined): number | undefined {
|
|
|
133
134
|
* (`claude-code_<version>_agent`). Other CLIs are recognised by the process
|
|
134
135
|
* name of an ancestor; their session ids are not read because their variable
|
|
135
136
|
* names were not verified. A wrapper that knows better sets the overrides:
|
|
136
|
-
*
|
|
137
|
+
* SLACK_HUB_AGENT_CLI, SLACK_HUB_AGENT_SESSION, SLACK_HUB_AGENT_PID. */
|
|
137
138
|
export function captureAttribution(env: NodeJS.ProcessEnv = process.env): Attribution {
|
|
138
139
|
const a: Attribution = { pid: process.pid, host: safeHostname(), cwd: process.cwd() };
|
|
139
140
|
|
|
140
|
-
let cli = env
|
|
141
|
+
let cli = hubEnv("AGENT_CLI", env) || undefined;
|
|
141
142
|
if (!cli && env.AI_AGENT) cli = env.AI_AGENT.split("_")[0] || undefined;
|
|
142
143
|
if (!cli && env.CLAUDECODE === "1") cli = "claude-code";
|
|
143
144
|
|
|
144
|
-
let agentPid = positiveInt(env
|
|
145
|
+
let agentPid = positiveInt(hubEnv("AGENT_PID", env)) ?? positiveInt(env.CLAUDE_PID);
|
|
145
146
|
if (!agentPid || !cli) {
|
|
146
147
|
const anc = findAgentAncestor();
|
|
147
148
|
if (anc) {
|
|
@@ -152,7 +153,7 @@ export function captureAttribution(env: NodeJS.ProcessEnv = process.env): Attrib
|
|
|
152
153
|
if (cli) a.cli = cli;
|
|
153
154
|
if (agentPid) a.agentPid = agentPid;
|
|
154
155
|
|
|
155
|
-
const session = env
|
|
156
|
+
const session = hubEnv("AGENT_SESSION", env) || env.CLAUDE_CODE_SESSION_ID;
|
|
156
157
|
if (session) a.sessionId = session;
|
|
157
158
|
|
|
158
159
|
const branch = gitBranch(a.cwd);
|
|
@@ -180,7 +181,7 @@ function gitBranch(cwd: string): string | undefined {
|
|
|
180
181
|
|
|
181
182
|
/** Slack message metadata: invisible in the client, returned by the API
|
|
182
183
|
* (`include_all_metadata`). Only defined fields — Slack rejects nulls. */
|
|
183
|
-
export const METADATA_EVENT_TYPE = "
|
|
184
|
+
export const METADATA_EVENT_TYPE = "slack_hub_sent";
|
|
184
185
|
|
|
185
186
|
export function attributionMetadata(kind: SentKind, a: Attribution): { event_type: string; event_payload: Record<string, string | number> } {
|
|
186
187
|
const p: Record<string, string | number> = { kind, pid: a.pid, host: a.host, cwd: a.cwd };
|
|
@@ -198,7 +199,8 @@ interface Stmt { run(...p: unknown[]): unknown; all(...p: unknown[]): unknown[];
|
|
|
198
199
|
interface Db { exec(sql: string): void; prepare(sql: string): Stmt; close(): void; }
|
|
199
200
|
|
|
200
201
|
export function sentDbPath(): string {
|
|
201
|
-
|
|
202
|
+
const db = hubEnv("SENT_DB");
|
|
203
|
+
if (db) return db;
|
|
202
204
|
return join(process.env.HOME || homedir(), ".config", "slack-cli", "sent.sqlite");
|
|
203
205
|
}
|
|
204
206
|
|
|
@@ -311,7 +313,7 @@ export function recordSent(r: RecordSent): boolean {
|
|
|
311
313
|
).run(
|
|
312
314
|
r.kind, r.team ?? null, r.channel, r.target ?? null, r.ts, r.threadTs ?? null, r.permalink ?? null, r.text,
|
|
313
315
|
Date.now(), a.pid, a.agentPid ?? null, a.host || null, a.cwd, a.gitBranch ?? null, a.cli ?? null, a.sessionId ?? null,
|
|
314
|
-
r.asBot ? 1 : 0,
|
|
316
|
+
r.asBot ? 1 : 0, hubEnv("IDENTITY") || null,
|
|
315
317
|
);
|
|
316
318
|
return true;
|
|
317
319
|
} catch (e: unknown) {
|
package/ts/stream.ts
CHANGED
|
@@ -208,7 +208,7 @@ export type StreamOpts = {
|
|
|
208
208
|
};
|
|
209
209
|
|
|
210
210
|
/** Where the real-time setup is documented — the polling warning points here. */
|
|
211
|
-
export const RELAY_DOCS = "https://github.com/snomiao/slack-
|
|
211
|
+
export const RELAY_DOCS = "https://github.com/snomiao/slack-hub/blob/main/worker/README.md";
|
|
212
212
|
|
|
213
213
|
export const PHI = 1.618;
|
|
214
214
|
/** Relay bells handled per turn of the loop before the poll gets a look in. */
|
package/worker/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# slack-
|
|
1
|
+
# slack-hub relay
|
|
2
2
|
|
|
3
3
|
A Cloudflare Worker that turns Slack's **Events API** into a resumable stream of
|
|
4
4
|
*doorbells* for `slack stream`. Slack posts each event to the Worker; the Worker
|
|
@@ -45,7 +45,7 @@ all (an older relay, a proxy that strips upgrades, a runtime without
|
|
|
45
45
|
| Path | Auth | What |
|
|
46
46
|
|---|---|---|
|
|
47
47
|
| `POST /slack/events` | Slack signing secret (`X-Slack-Signature`, ±5 min) | Answers `url_verification`; stores one bell per `message` / `app_mention` event. Acks with a single SQLite insert, well inside Slack's 3 s. Retries collapse on `(channel, ts)`. |
|
|
48
|
-
| `GET /ws?after=<seq>&epoch=<e>` | Subprotocols `slack-term-relay.v1` **and** `bearer.<base64url($RELAY_TOKEN)>` (or `Authorization: Bearer`) | WebSocket (below). Rejected with 401 before the upgrade is accepted. |
|
|
48
|
+
| `GET /ws?after=<seq>&epoch=<e>` | Subprotocols `slack-hub-relay.v1` (or the pre-rename `slack-term-relay.v1`) **and** `bearer.<base64url($RELAY_TOKEN)>` (or `Authorization: Bearer`) | WebSocket (below). Rejected with 401 before the upgrade is accepted. |
|
|
49
49
|
| `GET /stream?after=<seq>` | `Authorization: Bearer $RELAY_TOKEN` | SSE (kept for older clients). First an `event: hello` frame `{seq, gap, retention_sec}`, then the backlog after `seq`, then live `event: bell` frames (`id:` = seq). A keepalive comment goes out every 25 s. `Last-Event-ID` works as well as `?after`. Pass `&epoch=` from the last hello: the epoch names the relay's storage, so a client resuming against a reset relay is told `gap: true` and gets everything kept, even once the new numbers have passed its old seq. If `after` is omitted, the stream starts from now. `gap: true` means some bells after `after` are gone (expired, or the relay was reset): everything still kept is replayed, and the client should also catch up by polling. |
|
|
50
50
|
| `GET /health` | Bearer | `{epoch, latest, oldest, clients, websocket_clients, sse_clients, retention_sec}` |
|
|
51
51
|
|
|
@@ -55,11 +55,11 @@ At most 16 clients (both kinds) at once; more get 503.
|
|
|
55
55
|
|
|
56
56
|
**Auth.** The token is never put in the URL (query strings end up in logs).
|
|
57
57
|
Node's built-in `WebSocket` cannot send an `Authorization` header, so the client
|
|
58
|
-
offers
|
|
58
|
+
offers three subprotocols — `Sec-WebSocket-Protocol: slack-hub-relay.v1, slack-term-relay.v1,
|
|
59
59
|
bearer.<base64url(token)>` (base64url because subprotocols must be HTTP tokens;
|
|
60
60
|
the same scheme Kubernetes uses) — and the Worker compares the bearer one in
|
|
61
61
|
constant time before the request reaches the Durable Object. The server
|
|
62
|
-
selects only `slack-term-relay.v1
|
|
62
|
+
selects only `slack-hub-relay.v1` (or `slack-term-relay.v1` for an old client), never echoing the token.
|
|
63
63
|
|
|
64
64
|
**Messages** are JSON text, one per frame:
|
|
65
65
|
|
|
@@ -109,17 +109,17 @@ a hibernating object runs no timers.
|
|
|
109
109
|
|
|
110
110
|
### Without cloning (recommended)
|
|
111
111
|
|
|
112
|
-
The relay ships in the npm package as `slack-
|
|
112
|
+
The relay ships in the npm package as `slack-hub/worker`. Scaffold a Worker
|
|
113
113
|
folder with the CLI:
|
|
114
114
|
|
|
115
115
|
```sh
|
|
116
|
-
slack relay init my-relay # or: npx slack-
|
|
116
|
+
slack relay init my-relay # or: npx slack-hub relay init my-relay
|
|
117
117
|
cd my-relay && npm install
|
|
118
118
|
npx wrangler login # or CLOUDFLARE_API_TOKEN + CLOUDFLARE_ACCOUNT_ID (below)
|
|
119
119
|
npx wrangler deploy
|
|
120
120
|
```
|
|
121
121
|
|
|
122
|
-
`init` writes `package.json` (slack-
|
|
122
|
+
`init` writes `package.json` (slack-hub as `^` the CLI's version, wrangler
|
|
123
123
|
pinned), `wrangler.jsonc` (the same config as this folder's, with `--name` or
|
|
124
124
|
the folder's name), `src/index.ts`, `.gitignore` and a `README.md`. It makes no
|
|
125
125
|
network calls and refuses to overwrite existing files unless `--force`. It
|
|
@@ -130,15 +130,15 @@ as described below (run them from the new folder, with `npx wrangler`).
|
|
|
130
130
|
Already have a Worker project? `src/index.ts` is just
|
|
131
131
|
|
|
132
132
|
```ts
|
|
133
|
-
export { default, Relay } from "slack-
|
|
133
|
+
export { default, Relay } from "slack-hub/worker";
|
|
134
134
|
```
|
|
135
135
|
|
|
136
136
|
with a `RELAY` Durable Object binding to class `Relay` and a
|
|
137
137
|
`new_sqlite_classes: ["Relay"]` migration — copy them from
|
|
138
138
|
[`wrangler.jsonc`](wrangler.jsonc). `Env` is exported too, for typing.
|
|
139
|
-
`@cloudflare/workers-types` is an optional peer dependency of slack-
|
|
139
|
+
`@cloudflare/workers-types` is an optional peer dependency of slack-hub.
|
|
140
140
|
|
|
141
|
-
To update, bump `slack-
|
|
141
|
+
To update, bump `slack-hub` in the folder's package.json and redeploy.
|
|
142
142
|
|
|
143
143
|
### From a clone of this repo (contributors)
|
|
144
144
|
|
package/worker/src/hub.ts
CHANGED
|
@@ -60,7 +60,11 @@ export interface Store {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/** The subprotocol a client must offer; the server echoes it back. */
|
|
63
|
-
export const PROTOCOL = "slack-
|
|
63
|
+
export const PROTOCOL = "slack-hub-relay.v1";
|
|
64
|
+
/** Pre-rename name, still accepted from clients older than the slack-hub rename. */
|
|
65
|
+
export const LEGACY_PROTOCOL = "slack-term-relay.v1";
|
|
66
|
+
/** Accepted subprotocols, preferred first. */
|
|
67
|
+
export const PROTOCOLS: readonly string[] = [PROTOCOL, LEGACY_PROTOCOL];
|
|
64
68
|
/** The other offered subprotocol carries the bearer: `bearer.<base64url(token)>`.
|
|
65
69
|
* A header, not the URL: query strings end up in logs. */
|
|
66
70
|
export const BEARER_PREFIX = "bearer.";
|
|
@@ -92,7 +96,7 @@ export function protocolAuthorized(header: string | null, token: string): boolea
|
|
|
92
96
|
let ours = false;
|
|
93
97
|
for (const p of header.split(",")) {
|
|
94
98
|
const v = p.trim();
|
|
95
|
-
if (v
|
|
99
|
+
if (PROTOCOLS.includes(v)) ours = true;
|
|
96
100
|
if (safeEqual(v, want)) ok = true;
|
|
97
101
|
}
|
|
98
102
|
return ok && ours;
|
package/worker/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// slack-
|
|
1
|
+
// slack-hub relay — a Cloudflare Worker that turns Slack's Events API into a
|
|
2
2
|
// resumable doorbell stream for `slack stream`.
|
|
3
3
|
//
|
|
4
4
|
// Slack ──POST /slack/events──▶ Worker ──▶ Relay (Durable Object, SQLite)
|
|
@@ -238,8 +238,9 @@ export class Relay extends DurableObject<Env> {
|
|
|
238
238
|
const [client, server] = Object.values(new WebSocketPair()) as [WebSocket, WebSocket];
|
|
239
239
|
this.ctx.acceptWebSocket(server);
|
|
240
240
|
hub.open(this.sock(server), from, hello, this.store, now);
|
|
241
|
-
const offered = (req.headers.get("sec-websocket-protocol") ?? "").split(",").
|
|
242
|
-
|
|
241
|
+
const offered = (req.headers.get("sec-websocket-protocol") ?? "").split(",").map((p) => p.trim());
|
|
242
|
+
const pick = hub.PROTOCOLS.find((p) => offered.includes(p));
|
|
243
|
+
return new Response(null, { status: 101, webSocket: client, headers: pick ? { "sec-websocket-protocol": pick } : {} });
|
|
243
244
|
}
|
|
244
245
|
|
|
245
246
|
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|