grok-telegram-bot 2.2.1 β†’ 2.2.2

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/CHANGELOG.md CHANGED
@@ -9,6 +9,27 @@ The latest section is published verbatim as the GitHub Release notes by
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [2.2.2] - 2026-07-12
13
+
14
+ Patch release that hardens multi-account identity and documents the full **2.2.x**
15
+ batch (headless rotation, session auto-approve, pinned permission prompts).
16
+
17
+ ### Fixed
18
+
19
+ - **πŸ“§ Real account emails from `auth.json`.** Modern Grok access tokens often
20
+ omit the `email` claim; the bot now prefers `email` / `first_name` /
21
+ `last_name` written on the auth entry by `grok login`, then falls back to JWT
22
+ claims. `/accounts` and `/usage` show the host login correctly again.
23
+ - **πŸͺͺ Active account = live token hash.** The marked `activeId` is no longer
24
+ trusted when the host login changed outside the menu (`grok login` / `/reauth`).
25
+ Saving never overwrites a different saved account; mismatch is shown in the
26
+ `/accounts` UI (β€œHost Grok login” vs saved).
27
+
28
+ ### Docs
29
+
30
+ - README: headless account switch, device-code `/reauth`, auto-approve + pinned
31
+ permission prompts, `AUTO_APPROVE_PERMISSIONS`.
32
+
12
33
  ## [2.2.1] - 2026-07-12
13
34
 
14
35
  ### Fixed
@@ -677,6 +698,7 @@ from a single chat and switch between them, on a redesigned, compact menu.
677
698
  diffs, MarkdownV2 rendering, scheduled tasks, multi-image prompts, and a
678
699
  cross-platform 24/7 background service.
679
700
 
701
+ [2.2.2]: https://github.com/artickc/grok-telegram-bot/releases/tag/v2.2.2
680
702
  [2.2.1]: https://github.com/artickc/grok-telegram-bot/releases/tag/v2.2.1
681
703
  [2.2.0]: https://github.com/artickc/grok-telegram-bot/releases/tag/v2.2.0
682
704
  [2.1.0]: https://github.com/artickc/grok-telegram-bot/releases/tag/v2.1.0
package/README.md CHANGED
@@ -38,9 +38,10 @@ re-architected for the Grok Build CLI and extended into a full multi-session cli
38
38
  | 🧩 **MCP control** | `/mcp` lists MCP servers, **health-checks** them (which connected / failed and why), and **enables/disables** them β€” then restarts the agent to apply. |
39
39
  | πŸ‘₯ **Subagent visibility** | When Grok delegates to subagents and waits on them, you see each one **start / work / finish** plus a live `πŸ€– N running` summary β€” and subagent permission prompts route to your chat. |
40
40
  | πŸ“ˆ **Task progress bar** | The agent appends a `{progress: N%}` marker; the bot hides it and shows a **green 0–100% loading bar** on the live message, in the status panel, and on session cards (`SHOW_PROGRESS`). |
41
- | πŸ” **Sign in from chat** | `/reauth` signs you in without a terminal β€” **πŸ”‘ Sign in** runs `grok login` (any verification link/code streams to your chat), or **πŸ“₯ Import** an existing on-host login; the agent restarts under the new identity. |
42
- | πŸ‘₯ **Multiple accounts** | `/accounts` saves several Grok **sign-ins** (custom names) and switches between them in a tap β€” copies that login back over `~/.grok/auth.json` and restarts so sessions re-bind under the new account. |
43
- | πŸ” **Auto-rotate on give-up** | When a turn exhausts its retries, optionally cycle through your other saved accounts once and retry on each β€” the first that works wins (toggle in `/accounts`). |
41
+ | πŸ” **Sign in from chat** | `/reauth` signs you in without a terminal β€” **πŸ”‘ Sign in** runs headless `grok login --device-auth` (link/code streams to your chat, no host browser), or **πŸ“₯ Import** an existing on-host login; the agent restarts under the new identity. |
42
+ | πŸ‘₯ **Multiple accounts** | `/accounts` saves several Grok **sign-ins** (custom names) and switches between them in a tap β€” **stops the agent β†’ replaces `~/.grok/auth.json` β†’ restarts headlessly** (never opens a browser). |
43
+ | πŸ” **Auto-rotate on give-up** | When a turn exhausts its retries, optionally cycle through your other saved accounts once and retry on each β€” the first that works wins (toggle in `/accounts`). Same headless auth.json swap. |
44
+ | βœ… **Auto-approve tools** | By default, ACP permission prompts are auto-approved for the **session** (`AUTO_APPROVE_PERMISSIONS`). Turn both that and `GROK_TRUST_ALL_TOOLS` off for interactive Approve/Deny β€” those prompts are **pinned** until you act. |
44
45
  | πŸͺ™ **Credits & usage** | The `βœ… Done` line and `/usage` show credits used (when Grok reports them), turns this session, and account info. |
45
46
  | ⌨️ **Typing indicator** | Stays on for the whole turn, even through long tool chains. |
46
47
  | πŸ“₯ **Queued follow-ups** | Message while Grok is busy β€” it's queued and runs next. `/btw` runs it ASAP (now if idle, else right after the current task); `/flush` runs the queue now. |
@@ -62,9 +63,10 @@ re-architected for the Grok Build CLI and extended into a full multi-session cli
62
63
  | Attach to **live** PC sessions (watch / fork) | βœ… | ❌ |
63
64
  | **Kill a session by PID** (or all at once) | βœ… | ❌ |
64
65
  | **Live task-progress bars** (`{progress: N%}`) | βœ… | ❌ |
65
- | **Sign in from chat** (`/reauth`) | βœ… | ❌ |
66
- | **Multiple saved accounts** + one-tap switch (`/accounts`) | βœ… | ❌ |
66
+ | **Sign in from chat** (`/reauth`, device-code) | βœ… | ❌ |
67
+ | **Multiple saved accounts** + headless one-tap switch (`/accounts`) | βœ… | ❌ |
67
68
  | **Auto-rotate accounts** when a turn gives up | βœ… | ❌ |
69
+ | **Session auto-approve** + **pinned** interactive permissions | βœ… | ❌ |
68
70
  | Multiple isolated sessions | βœ… | ❌ (single shared) |
69
71
  | Queued follow-ups while busy | βœ… | ❌ |
70
72
  | **Scheduled tasks** (cron-like) | βœ… | ❌ |
@@ -326,9 +328,10 @@ takes precedence and the value never decreases. Disable the fallback with
326
328
  Grok Build signs in with your **xAI account** (SuperGrok / X Premium+), not an
327
329
  API key. Run **`/reauth`** to sign in without touching a terminal:
328
330
 
329
- - **πŸ”‘ Sign in** β€” runs `grok login`; if a verification link/code appears it
330
- streams into the chat so you can approve it on any device. The bot then
331
- restarts the agent so your next turn runs on the new identity.
331
+ - **πŸ”‘ Sign in** β€” runs `grok login --device-auth` (device code, **no browser on
332
+ the host**). The verification link/code streams into the chat so you can
333
+ approve it on any device. The bot then restarts the agent under the new
334
+ identity.
332
335
  - **πŸ“₯ Import existing** β€” adopt a `grok login` already done on this machine
333
336
  (`~/.grok/auth.json`).
334
337
 
@@ -338,16 +341,24 @@ It's refused while a turn is running. You can also sign in up front by running
338
341
  ## πŸ‘₯ Multiple accounts
339
342
 
340
343
  **`/accounts`** manages several Grok **sign-ins** side by side. Save the current
341
- login as a named account (auto-named from its email, or **Save as…** with a
342
- custom name), rename or delete saved ones, and **switch** in a tap β€” the bot
343
- copies that account's token back over `~/.grok/auth.json` and restarts the agent
344
- so your next turn runs under it (the current login is snapshotted first so it's
345
- never lost). Snapshots live only in git-ignored per-account files.
344
+ login as a named account (auto-named from its email when available, or
345
+ **Save as…** with a custom name), rename or delete saved ones, and **switch** in
346
+ a tap. Switching is deliberately headless:
347
+
348
+ 1. Snapshot the current login (so it isn't lost),
349
+ 2. **Stop** the shared agent,
350
+ 3. **Replace** `~/.grok/auth.json` with the saved snapshot,
351
+ 4. **Start** the agent and authenticate with `cached_token` only β€” never a
352
+ browser method like `grok.com` (which would hang a service host).
353
+
354
+ The menu shows the **host Grok login** vs which saved account it matches, so a
355
+ manual `grok login` outside the bot doesn't leave a stale β€œactive” label.
346
356
 
347
357
  **πŸ” Auto-rotate on errors** (toggle here): when a turn exhausts its retries and
348
- can't recover, the bot cycles through your other saved logins **once**, retrying
349
- the prompt on each β€” the first that succeeds stays active; if they all fail it
350
- stops after one pass. Handy when an account gets throttled or runs out of quota.
358
+ can't recover, the bot cycles through your other saved logins **once** with the
359
+ same stop β†’ auth.json β†’ start path β€” the first that succeeds stays active; if
360
+ they all fail it stops after one pass. Handy when an account gets throttled or
361
+ runs out of quota.
351
362
 
352
363
  ---
353
364
 
@@ -390,7 +401,8 @@ Resuming an **idle** session loads it directly so you continue the exact thread.
390
401
  | `GROK_MODEL` | no | `grok-4.5` | Default model for new sessions. |
391
402
  | `GROK_TG_DIR` | no | `~/.grok/tg` | Folder holding this instance's `.env`, `logs/`, `data/`. Resolution: `--instance` β†’ `GROK_TG_DIR` β†’ a `.env` in the current folder β†’ `~/.grok/tg`. So a `.env` created once is loaded from any startup path. |
392
403
  | `GROK_AGENT` | no | β€” | Custom sub-agent name (informational; Grok delegates via its own `task`/`delegate` tools). |
393
- | `GROK_TRUST_ALL_TOOLS` | no | `true` | Run tools without prompts. |
404
+ | `GROK_TRUST_ALL_TOOLS` | no | `true` | Pass `--always-approve` so tools run without prompts. |
405
+ | `AUTO_APPROVE_PERMISSIONS` | no | `true` | Auto-approve ACP `session/request_permission` (prefer β€œallow for this session”). Set `false` **and** `GROK_TRUST_ALL_TOOLS=false` for interactive Approve/Deny buttons (those prompts are **pinned** until you act or they time out). |
394
406
  | `PROJECT_ROOTS` | no | workspace parent + home | Roots for `/projects`. |
395
407
  | `STREAM_THROTTLE_MS` | no | `1500` | Live-edit interval while streaming. |
396
408
  | `MESSAGE_BATCH_MS` | no | `800` | Window to coalesce rapid text messages (e.g. a long message Telegram split at 4096 chars) into one prompt. `0` disables. |
@@ -486,13 +498,20 @@ the bot inherits whatever MCP servers Grok CLI is configured with.
486
498
 
487
499
  ## πŸ” Tool approvals
488
500
 
489
- Over ACP, Grok honors a permission mode. With `GROK_TRUST_ALL_TOOLS=true`
490
- (default) the bot passes `--always-approve`, so tools run without prompts. Set it
491
- to `false` to run in ACP **"ask"** mode: Grok sends `session/request_permission`
492
- before risky tools (file writes, shell commands) and the bot surfaces
493
- **Approve / Approve always / Deny** buttons in Telegram, sending your choice back
494
- (unanswered prompts eventually cancel). You can also intervene on any live turn
495
- with the tool stream + **⏹ Stop** (`/cancel`), which cancels that session's turn.
501
+ Over ACP, Grok honors a permission mode. Defaults are built for unattended
502
+ mobile use:
503
+
504
+ | Setting | Default | Effect |
505
+ |---|---|---|
506
+ | `GROK_TRUST_ALL_TOOLS` | `true` | Passes `--always-approve` so tools run without prompts. |
507
+ | `AUTO_APPROVE_PERMISSIONS` | `true` | If a permission request still arrives, auto-pick **allow for this session** / always-allow. |
508
+
509
+ Set **both** to `false` for ACP **"ask"** mode: Grok sends
510
+ `session/request_permission` before risky tools and the bot surfaces
511
+ **Approve / Allow for session / Deny** buttons. Those prompts are **pinned** so
512
+ they aren't buried by streaming output; the pin is removed when you choose,
513
+ or when the request times out (status panel is re-pinned). You can always
514
+ intervene on a live turn with the tool stream + **⏹ Stop** (`/cancel`).
496
515
 
497
516
  ## πŸ” Security
498
517
 
@@ -514,10 +533,11 @@ user. See [SECURITY.md](./SECURITY.md) for the full model.
514
533
  - [x] Voice messages β†’ speech-to-text β†’ prompt (multi-language)
515
534
  - [x] Context-usage % in the status panel
516
535
  - [x] Inline approvals β€” approve/deny risky tools from buttons (non trust-all mode)
536
+ - [x] Session auto-approve + pinned permission prompts
517
537
  - [x] Account & context usage (`/usage`)
518
- - [x] Multiple accounts with one-tap switch + auto-rotate on errors (`/accounts`)
519
- - [x] Organization / Import-from-Grok-IDE login + credits on the Done line
520
- - [x] Release automation β€” downloadable zip + CHANGELOG-driven notes on tag push
538
+ - [x] Multiple accounts with headless one-tap switch + auto-rotate (`/accounts`)
539
+ - [x] Device-code `/reauth` (no host browser) + real email labels from auth.json
540
+ - [x] Rich per-kind tool-call detail (search / edit diffs / shell / MCP / …)
521
541
  - [x] README community sections β€” Contributors, Top Contributors, Stars, StarMapper
522
542
  - [ ] **Token & cost meter** β€” per-session token counts and an estimated spend tally
523
543
  - [ ] **Text-to-speech replies** β€” optionally speak answers back as voice notes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grok-telegram-bot",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "description": "Control the official Grok Build CLI from Telegram over the Agent Client Protocol (ACP). Sign in with your xAI account, switch projects, resume sessions, stream responses with diffs, queue follow-ups, manage multiple sign-ins, and run 24/7 as a cross-platform background service.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -1,14 +1,29 @@
1
+ /**
2
+ * Push current working-tree release files to main + create annotated tag.
3
+ * (Local git push is restricted in this environment.)
4
+ */
1
5
  import { readFileSync } from "node:fs";
2
6
  import { spawnSync } from "node:child_process";
3
7
 
4
- const files = process.argv.slice(2);
5
- if (files.length < 3) {
6
- console.error("Usage: node scripts/_push-branch.mjs <branch> <message> <file>...");
7
- process.exit(1);
8
- }
9
- const [branch, message, ...paths] = files;
10
8
  const owner = "artickc";
11
9
  const repo = "grok-telegram-bot";
10
+ const branch = "main";
11
+ const version = "2.2.2";
12
+ const tag = `v${version}`;
13
+ const message = "release: v2.2.2 β€” account identity, docs, 2.2.x multi-account batch";
14
+ const tagMessage =
15
+ "v2.2.2 β€” multi-account headless rotation, session auto-approve, pinned permissions, account identity";
16
+
17
+ const files = [
18
+ ".gitignore",
19
+ "CHANGELOG.md",
20
+ "README.md",
21
+ "package.json",
22
+ "src/app/accounts.ts",
23
+ "src/app/grok-credentials.ts",
24
+ "src/bot/handlers/accounts.ts",
25
+ "test/credentials.test.ts",
26
+ ];
12
27
 
13
28
  // eslint-disable-next-line no-control-regex
14
29
  const ANSI_RE = /\x1b\[[0-9;?]*[ -/]*[@-~]/g;
@@ -34,18 +49,6 @@ function ghApi(method, path, body) {
34
49
  return JSON.parse(text);
35
50
  }
36
51
 
37
- // Ensure branch exists from main if missing.
38
- try {
39
- ghApi("GET", `repos/${owner}/${repo}/git/ref/heads/${branch}`);
40
- } catch {
41
- const main = ghApi("GET", `repos/${owner}/${repo}/git/ref/heads/main`);
42
- ghApi("POST", `repos/${owner}/${repo}/git/refs`, {
43
- ref: `refs/heads/${branch}`,
44
- sha: main.object.sha,
45
- });
46
- console.log("created branch", branch, "from main");
47
- }
48
-
49
52
  const ref = ghApi("GET", `repos/${owner}/${repo}/git/ref/heads/${branch}`);
50
53
  const baseSha = ref.object.sha;
51
54
  const baseCommit = ghApi("GET", `repos/${owner}/${repo}/git/commits/${baseSha}`);
@@ -53,13 +56,13 @@ const baseTree = baseCommit.tree.sha;
53
56
  console.log("base", baseSha);
54
57
 
55
58
  const tree = [];
56
- for (const path of paths) {
59
+ for (const path of files) {
57
60
  const content = readFileSync(path, "utf8");
58
61
  const blob = ghApi("POST", `repos/${owner}/${repo}/git/blobs`, {
59
62
  content: Buffer.from(content, "utf8").toString("base64"),
60
63
  encoding: "base64",
61
64
  });
62
- tree.push({ path: path.replace(/\\/g, "/"), mode: "100644", type: "blob", sha: blob.sha });
65
+ tree.push({ path, mode: "100644", type: "blob", sha: blob.sha });
63
66
  console.log("blob", path, blob.sha.slice(0, 8));
64
67
  }
65
68
 
@@ -76,4 +79,21 @@ ghApi("PATCH", `repos/${owner}/${repo}/git/refs/heads/${branch}`, {
76
79
  sha: commit.sha,
77
80
  force: false,
78
81
  });
79
- console.log("updated", branch, "->", commit.sha);
82
+ console.log("main ->", commit.sha);
83
+
84
+ const tagObj = ghApi("POST", `repos/${owner}/${repo}/git/tags`, {
85
+ tag,
86
+ message: tagMessage,
87
+ object: commit.sha,
88
+ type: "commit",
89
+ tagger: {
90
+ name: "artickc",
91
+ email: "artickc@users.noreply.github.com",
92
+ date: new Date().toISOString(),
93
+ },
94
+ });
95
+ ghApi("POST", `repos/${owner}/${repo}/git/refs`, {
96
+ ref: `refs/tags/${tag}`,
97
+ sha: tagObj.sha,
98
+ });
99
+ console.log("tag", tag, "->", commit.sha);
@@ -76,17 +76,44 @@ export class AccountManager {
76
76
  }
77
77
 
78
78
  /**
79
- * Id of the currently active saved account. Prefers the last switch target
80
- * (`activeId`), then falls back to matching the live auth.json token hash.
79
+ * Id of the saved account that matches the live auth.json (if any).
80
+ * Token hash is authoritative β€” never trust a stale `activeId` when the host
81
+ * login has changed (e.g. `grok login` / /reauth outside this menu).
81
82
  */
82
83
  activeAccountId(): string | undefined {
83
84
  const data = this.store.get();
85
+ const lid = loginId();
86
+ if (lid) {
87
+ const byToken = data.accounts.find((a) => a.loginId === lid);
88
+ if (byToken) return byToken.id;
89
+ // Live login differs from every saved account (and any stale activeId).
90
+ return undefined;
91
+ }
92
+ // No readable token (API-key-only / missing file) β€” last switch target only.
84
93
  if (data.activeId && data.accounts.some((a) => a.id === data.activeId)) {
85
94
  return data.activeId;
86
95
  }
96
+ return undefined;
97
+ }
98
+
99
+ /**
100
+ * Last switch/save target stored by the app, even when the host Grok login
101
+ * no longer matches that snapshot (used only for mismatch UI copy).
102
+ */
103
+ markedActiveId(): string | undefined {
104
+ const id = this.store.get().activeId;
105
+ if (!id) return undefined;
106
+ return this.store.get().accounts.some((a) => a.id === id) ? id : undefined;
107
+ }
108
+
109
+ /** Whether the host's live login matches the saved account marked active. */
110
+ liveMatchesActive(): boolean {
87
111
  const lid = loginId();
88
- if (!lid) return undefined;
89
- return data.accounts.find((a) => a.loginId === lid)?.id;
112
+ if (!lid) return false;
113
+ const marked = this.markedActiveId();
114
+ if (!marked) return this.activeAccountId() !== undefined;
115
+ const meta = this.get(marked);
116
+ return !!meta?.loginId && meta.loginId === lid;
90
117
  }
91
118
 
92
119
  get(id: string): StoredAccount | undefined {
@@ -117,19 +144,32 @@ export class AccountManager {
117
144
  }
118
145
  const label = customLabel?.trim() || loginLabel() || `account ${lid.slice(0, 6)}`;
119
146
  const email = loginLabel();
120
- // Match by token hash first; fall back to the currently marked active slot
121
- // when the user is re-saving after a silent token refresh.
122
- const existing =
123
- this.store.get().accounts.find((a) => a.loginId === lid) ??
124
- (this.store.get().activeId ? this.get(this.store.get().activeId!) : undefined);
147
+ // Match by token hash first. Only reuse the marked active slot when it is
148
+ // the same login (token refresh) or the same email β€” never overwrite a
149
+ // different saved account when the host is signed in as someone else.
150
+ const accounts = this.store.get().accounts;
151
+ const byToken = accounts.find((a) => a.loginId === lid);
152
+ const active = this.store.get().activeId ? this.get(this.store.get().activeId!) : undefined;
153
+ const emailKey = email?.toLowerCase();
154
+ const byEmail =
155
+ emailKey && emailKey.includes("@")
156
+ ? accounts.find((a) => (a.email || a.label || "").toLowerCase() === emailKey)
157
+ : undefined;
158
+ const sameActiveRefresh =
159
+ active &&
160
+ (active.loginId === lid ||
161
+ (!!emailKey &&
162
+ emailKey.includes("@") &&
163
+ [active.email, active.label].some((v) => (v || "").toLowerCase() === emailKey)));
164
+ const existing = byToken ?? (sameActiveRefresh ? active : undefined) ?? byEmail;
125
165
  const id = existing?.id ?? makeId();
126
166
  await writeFile(this.snapshotPath(id), raw, "utf-8");
127
167
  const meta: StoredAccount = {
128
168
  id,
129
169
  label: customLabel?.trim() || existing?.label || label,
130
170
  loginId: lid,
131
- email: email || existing?.email,
132
- startUrl: email || existing?.email,
171
+ email: (email && email.includes("@") ? email : undefined) || existing?.email,
172
+ startUrl: (email && email.includes("@") ? email : undefined) || existing?.email || existing?.startUrl,
133
173
  savedAt: new Date().toISOString(),
134
174
  };
135
175
  this.store.update((d) => {
@@ -2,7 +2,7 @@
2
2
  * Grok Build login state. The official CLI signs in with your xAI account
3
3
  * (`grok login`, browser OIDC) and stores the token in `~/.grok/auth.json`:
4
4
  *
5
- * { "<scope_url>": { "key": "<token>" }, ... }
5
+ * { "<scope_url>": { "key": "<token>", "email"?: "...", ... }, ... }
6
6
  *
7
7
  * (An `XAI_API_KEY` env var is an alternative for non-browser hosts.) This
8
8
  * module reads that file so the bot can show who's signed in, detect a usable
@@ -36,7 +36,19 @@ export function authFileExists(): boolean {
36
36
  return existsSync(grokAuthPath());
37
37
  }
38
38
 
39
- type AuthFile = Record<string, { key?: string } | undefined>;
39
+ /** One scope entry inside ~/.grok/auth.json (fields beyond `key` are optional). */
40
+ export interface AuthEntry {
41
+ key?: string;
42
+ email?: string;
43
+ first_name?: string;
44
+ last_name?: string;
45
+ user_id?: string;
46
+ principal_id?: string;
47
+ auth_mode?: string;
48
+ [extra: string]: unknown;
49
+ }
50
+
51
+ export type AuthFile = Record<string, AuthEntry | undefined>;
40
52
 
41
53
  export function readAuth(): AuthFile {
42
54
  try {
@@ -46,12 +58,18 @@ export function readAuth(): AuthFile {
46
58
  }
47
59
  }
48
60
 
61
+ /** Active scope entry (OIDC preferred, then legacy). */
62
+ export function currentAuthEntry(auth: AuthFile = readAuth()): AuthEntry | undefined {
63
+ const oidc = auth[OIDC_SCOPE];
64
+ if (oidc?.key?.trim()) return oidc;
65
+ const legacy = auth[LEGACY_SCOPE];
66
+ if (legacy?.key?.trim()) return legacy;
67
+ return undefined;
68
+ }
69
+
49
70
  /** The active sign-in token from auth.json (OIDC preferred, then legacy). */
50
71
  export function currentToken(auth: AuthFile = readAuth()): string | undefined {
51
- const oidc = auth[OIDC_SCOPE]?.key?.trim();
52
- if (oidc) return oidc;
53
- const legacy = auth[LEGACY_SCOPE]?.key?.trim();
54
- return legacy || undefined;
72
+ return currentAuthEntry(auth)?.key?.trim() || undefined;
55
73
  }
56
74
 
57
75
  /** Whether a usable login exists (a token in auth.json, or XAI_API_KEY). */
@@ -81,16 +99,33 @@ function decodeJwtPayload(jwt: string): Record<string, unknown> | undefined {
81
99
  }
82
100
  }
83
101
 
84
- /** Best-effort identity (email/name) decoded from the sign-in JWT. */
102
+ /**
103
+ * Best-effort identity (email/name). Prefer fields Grok writes on the auth
104
+ * entry (`email`, `first_name`, …) β€” modern access tokens often omit email
105
+ * claims β€” then fall back to JWT claims when present.
106
+ */
85
107
  export function identityFromAuth(auth: AuthFile = readAuth()): LoginIdentity {
86
- const tok = currentToken(auth);
87
- if (!tok) return {};
88
- const claims = decodeJwtPayload(tok);
89
- if (!claims) return {};
90
- const str = (k: string): string | undefined => (typeof claims[k] === "string" ? (claims[k] as string) : undefined);
91
- const emailish = str("email") || str("preferred_username") || str("upn");
92
- const email = emailish && emailish.includes("@") ? emailish : undefined;
93
- return { email, name: str("name") || str("given_name") };
108
+ const entry = currentAuthEntry(auth);
109
+ if (!entry) return {};
110
+
111
+ const entryEmail = typeof entry.email === "string" && entry.email.includes("@") ? entry.email : undefined;
112
+ const entryName = [entry.first_name, entry.last_name]
113
+ .filter((p): p is string => typeof p === "string" && p.trim().length > 0)
114
+ .join(" ")
115
+ .trim() || undefined;
116
+
117
+ const tok = entry.key?.trim();
118
+ const claims = tok ? decodeJwtPayload(tok) : undefined;
119
+ const str = (k: string): string | undefined =>
120
+ claims && typeof claims[k] === "string" ? (claims[k] as string) : undefined;
121
+ const claimEmailish = str("email") || str("preferred_username") || str("upn");
122
+ const claimEmail = claimEmailish && claimEmailish.includes("@") ? claimEmailish : undefined;
123
+ const claimName = str("name") || str("given_name");
124
+
125
+ return {
126
+ email: entryEmail || claimEmail,
127
+ name: entryName || claimName,
128
+ };
94
129
  }
95
130
 
96
131
  /** A short human label for the active login (email, else a short token hash). */
@@ -25,14 +25,28 @@ async function view(deps: BotDeps, note?: string): Promise<{ text: string; keybo
25
25
  const list = deps.accounts.list();
26
26
  const acct = await deps.usage.account().catch(() => undefined);
27
27
  const active = deps.accounts.activeAccountId();
28
+ const marked = deps.accounts.markedActiveId();
28
29
  const loggedIn = await deps.usage.isLoggedIn().catch(() => false);
30
+ const liveLabel = acct?.email?.trim();
31
+ const activeMeta = active ? deps.accounts.get(active) : undefined;
32
+ const markedMeta = marked && marked !== active ? deps.accounts.get(marked) : undefined;
29
33
 
30
34
  const lines = ["\u{1F465} Grok accounts", ""];
31
- if (loggedIn && acct?.email && !active) {
32
- lines.push(`\u{1F7E2} Signed in as ${acct.email}`);
33
- lines.push(" \u2514 Not saved yet β€” tap \u201C\u{1F4BE} Save current login\u201D to keep it.", "");
34
- } else if (!loggedIn) {
35
+ if (!loggedIn) {
35
36
  lines.push("\u{1F534} Not signed in β€” sign in via /reauth.", "");
37
+ } else if (liveLabel) {
38
+ lines.push(`\u{1F7E2} Host Grok login: ${liveLabel}`);
39
+ if (activeMeta) {
40
+ lines.push(` \u2514 Matches saved: ${activeMeta.label}`);
41
+ } else {
42
+ lines.push(" \u2514 Not among saved accounts β€” tap \u201C\u{1F4BE} Save current login\u201D.");
43
+ if (markedMeta) {
44
+ lines.push(` \u2514 App still has ${markedMeta.label} selected β€” different from host login.`);
45
+ }
46
+ }
47
+ lines.push("");
48
+ } else {
49
+ lines.push("\u{1F7E2} Signed in (identity unknown).", "");
36
50
  }
37
51
  if (list.length === 0) {
38
52
  lines.push("No saved accounts yet.", "", "Save the current login below, or sign in via /reauth.");
@@ -1,52 +0,0 @@
1
- import { spawnSync } from "node:child_process";
2
-
3
- const owner = "artickc";
4
- const repo = "grok-telegram-bot";
5
- const version = process.argv[2] || "2.2.1";
6
- const tag = `v${version}`;
7
- const message = process.argv[3] || `${tag} β€” pin permission prompts until resolved`;
8
-
9
- // eslint-disable-next-line no-control-regex
10
- const ANSI_RE = /\x1b\[[0-9;?]*[ -/]*[@-~]/g;
11
-
12
- function ghApi(method, path, body) {
13
- const args = ["api", "-X", method, path];
14
- const opts = {
15
- encoding: "utf8",
16
- maxBuffer: 50 * 1024 * 1024,
17
- shell: false,
18
- env: { ...process.env, NO_COLOR: "1", CLICOLOR: "0", FORCE_COLOR: "0", GH_FORCE_TTY: "0" },
19
- };
20
- if (body !== undefined) {
21
- args.push("--input", "-");
22
- opts.input = JSON.stringify(body);
23
- }
24
- const r = spawnSync("gh", args, opts);
25
- if (r.status !== 0) {
26
- throw new Error(`gh api ${method} ${path} failed (${r.status}): ${r.stderr || r.stdout}`);
27
- }
28
- const text = (r.stdout || "").replace(ANSI_RE, "").trim();
29
- if (!text) return {};
30
- return JSON.parse(text);
31
- }
32
-
33
- const ref = ghApi("GET", `repos/${owner}/${repo}/git/ref/heads/main`);
34
- const sha = ref.object.sha;
35
- console.log("main", sha);
36
-
37
- const tagObj = ghApi("POST", `repos/${owner}/${repo}/git/tags`, {
38
- tag,
39
- message,
40
- object: sha,
41
- type: "commit",
42
- tagger: {
43
- name: "artickc",
44
- email: "artickc@users.noreply.github.com",
45
- date: new Date().toISOString(),
46
- },
47
- });
48
- ghApi("POST", `repos/${owner}/${repo}/git/refs`, {
49
- ref: `refs/tags/${tag}`,
50
- sha: tagObj.sha,
51
- });
52
- console.log("created tag", tag, "->", sha);